diff --git a/photon-core/src/main/java/org/photonvision/vision/target/TargetCalculations.java b/photon-core/src/main/java/org/photonvision/vision/target/TargetCalculations.java index d8bead93c..a58be5f8b 100644 --- a/photon-core/src/main/java/org/photonvision/vision/target/TargetCalculations.java +++ b/photon-core/src/main/java/org/photonvision/vision/target/TargetCalculations.java @@ -54,26 +54,27 @@ public class TargetCalculations { minAreaRect.points(vertices); - Point bl = getMiddle(vertices[0], vertices[1]); - Point tl = getMiddle(vertices[1], vertices[2]); - Point tr = getMiddle(vertices[2], vertices[3]); - Point br = getMiddle(vertices[3], vertices[0]); - boolean orientation; - if (isLandscape) { - orientation = minAreaRect.size.width > minAreaRect.size.height; - } else { - orientation = minAreaRect.size.width < minAreaRect.size.height; - } + Point bottom = getMiddle(vertices[0], vertices[1]); + Point left = getMiddle(vertices[1], vertices[2]); + Point top = getMiddle(vertices[2], vertices[3]); + Point right = getMiddle(vertices[3], vertices[0]); + + boolean orientationCorrect = minAreaRect.size.width > minAreaRect.size.height; + if (!isLandscape) orientationCorrect = !orientationCorrect; switch (offsetRegion) { case Top: - return orientation ? tl : tr; + if (orientationCorrect) return (left.y < right.y) ? left : right; + else return (top.y < bottom.y) ? top : bottom; case Bottom: - return orientation ? br : bl; + if (orientationCorrect) return (left.y > right.y) ? left : right; + else return (top.y > bottom.y) ? top : bottom; case Left: - return orientation ? bl : tl; + if (orientationCorrect) return (top.x < bottom.x) ? top : bottom; + else return (left.x < right.x) ? left : right; case Right: - return orientation ? tr : br; + if (orientationCorrect) return (top.x > bottom.x) ? top : bottom; + else return (left.x > right.x) ? left : right; default: return minAreaRect.center; } diff --git a/photon-core/src/test/java/org/photonvision/vision/target/TargetCalculationsTest.java b/photon-core/src/test/java/org/photonvision/vision/target/TargetCalculationsTest.java index 72df9cad5..787714569 100644 --- a/photon-core/src/test/java/org/photonvision/vision/target/TargetCalculationsTest.java +++ b/photon-core/src/test/java/org/photonvision/vision/target/TargetCalculationsTest.java @@ -92,10 +92,21 @@ public class TargetCalculationsTest { Size rectSize = new Size(10, 5); double angle = 30; RotatedRect rect = new RotatedRect(center, rectSize, angle); - Point result = - TargetCalculations.calculateTargetOffsetPoint(false, TargetOffsetPointEdge.Top, rect); - assertEquals(4.3, result.x, 0.33, "Target offset x not as expected"); - assertEquals(2.5, result.y, 0.05, "Target offset Y not as expected"); + + // We pretend like x/y are in pixels, so the "top" is actually the bottom + var result = + TargetCalculations.calculateTargetOffsetPoint(true, TargetOffsetPointEdge.Top, rect); + assertEquals(1.25, result.x, 0.1, "Target offset x not as expected"); + assertEquals(-2.17, result.y, 0.1, "Target offset Y not as expected"); + result = + TargetCalculations.calculateTargetOffsetPoint(true, TargetOffsetPointEdge.Bottom, rect); + assertEquals(-1.25, result.x, 0.1, "Target offset x not as expected"); + assertEquals(2.17, result.y, 0.1, "Target offset Y not as expected"); + } + + public static void main(String[] args) { + TestUtils.loadLibraries(); + new TargetCalculationsTest().targetOffsetTest(); } @Test