From 2eadedbfaf20fb727d6c3364496074f716ff5e16 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Mon, 22 Apr 2024 01:36:22 -0400 Subject: [PATCH] [apriltag] Cache layout loaded from AprilTagFields resource json (#6385) --- .../wpi/first/apriltag/AprilTagFieldLayout.java | 17 ++++++++++++----- .../edu/wpi/first/apriltag/AprilTagFields.java | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java index d2a5dd1cdfe..bae5007f545 100644 --- a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java +++ b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFieldLayout.java @@ -230,12 +230,19 @@ public void serialize(Path path) throws IOException { * @throws UncheckedIOException If the layout does not exist. */ public static AprilTagFieldLayout loadField(AprilTagFields field) { - try { - return loadFromResource(field.m_resourceFile); - } catch (IOException e) { - throw new UncheckedIOException( - "Could not load AprilTagFieldLayout from " + field.m_resourceFile, e); + if (field.m_fieldLayout == null) { + try { + field.m_fieldLayout = loadFromResource(field.m_resourceFile); + } catch (IOException e) { + throw new UncheckedIOException( + "Could not load AprilTagFieldLayout from " + field.m_resourceFile, e); + } } + // Copy layout because the layout's origin is mutable + return new AprilTagFieldLayout( + field.m_fieldLayout.getTags(), + field.m_fieldLayout.getFieldLength(), + field.m_fieldLayout.getFieldWidth()); } /** diff --git a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java index 776050c2749..c569a9c55ff 100644 --- a/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java +++ b/apriltag/src/main/java/edu/wpi/first/apriltag/AprilTagFields.java @@ -24,6 +24,8 @@ public enum AprilTagFields { /** Resource filename. */ public final String m_resourceFile; + AprilTagFieldLayout m_fieldLayout; + AprilTagFields(String resourceFile) { m_resourceFile = kBaseResourceDir + resourceFile; }