Skip to content

Commit

Permalink
- UtilPolygons2D
Browse files Browse the repository at this point in the history
 * Added bound(poly, rect int)
  • Loading branch information
lessthanoptimal committed May 10, 2023
1 parent e7b39c4 commit 5e36020
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
2 changes: 2 additions & 0 deletions change.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Version : 0.26
* Note was already in some specific implementations
- Rectangle2D (all types)
* getCorner()
- UtilPolygons2D
* Added bound(poly, rect int)

---------------------------------------------
Date : 2023-Feb-15
Expand Down
53 changes: 47 additions & 6 deletions main/src/georegression/geometry/UtilPolygons2D_F64.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import georegression.metric.Intersection2D_F64;
import georegression.struct.line.LineSegment2D_F64;
import georegression.struct.point.Point2D_F64;
import georegression.struct.shapes.Polygon2D_F64;
import georegression.struct.shapes.Quadrilateral_F64;
import georegression.struct.shapes.Rectangle2D_F64;
import georegression.struct.shapes.RectangleLength2D_I32;
import georegression.struct.shapes.*;
import org.ddogleg.struct.DogArray;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -303,7 +300,7 @@ public static void bounding( Quadrilateral_F64 quad , Rectangle2D_F64 rectangle

/**
* Finds the minimum area bounding rectangle around the quadrilateral that is aligned with coordinate
* system axises.
* system axises. This is also known as the Axis Aligned Bounding Box (AABB).
*
* @param polygon (Input) Polygon
* @param rectangle (Output) Minimum area rectangle
Expand All @@ -329,6 +326,50 @@ public static void bounding( Polygon2D_F64 polygon , Rectangle2D_F64 rectangle )
}
}

/**
* Finds the minimum area bounding rectangle around the quadrilateral that is aligned with coordinate
* system axises. This is also known as the Axis Aligned Bounding Box (AABB).
*
* @param polygon (Input) Polygon
* @param aabb (Output) Minimum area rectangle a.k.a. AABB. lower extent is inclusive, upper is exclusive.
*/
public static Rectangle2D_I32 bounding( Polygon2D_F64 polygon, @Nullable Rectangle2D_I32 aabb ) {
double x0 = polygon.vertexes.get(0).x;
double y0 = polygon.vertexes.get(0).y;
double x1 = x0;
double y1 = y0;

for (int i = 1; i < polygon.size(); i++) {
Point2D_F64 p = polygon.get(i);
if (p.x < x0)
x0 = p.x;
else if (p.x > x1)
x1 = p.x;

if (p.y < y0)
y0 = p.y;
else if (p.y > y1)
y1 = p.y;
}

if (aabb == null)
aabb = new Rectangle2D_I32();

aabb.x0 = (int)Math.floor(x0);
aabb.y0 = (int)Math.floor(y0);
aabb.x1 = (int)Math.ceil(x1);
aabb.y1 = (int)Math.ceil(y1);

// Upper limit is exclusive. Handle the special case.
if (aabb.x1 == x1)
aabb.x1 += 1;

if (aabb.y1 == y1)
aabb.y1 += 1;

return aabb;
}

/**
* Computes the center or average point in the quadrilateral.
*
Expand All @@ -352,7 +393,7 @@ public static Point2D_F64 center( Quadrilateral_F64 quad , @Nullable Point2D_F64
/**
* Returns true if the polygon is ordered in a counter-clockwise order. This is done by summing up the interior
* angles.
*
*
* @param polygon List of ordered points which define a polygon
* @return true if CCW and false if CW
*/
Expand Down
30 changes: 26 additions & 4 deletions main/test/georegression/geometry/TestUtilPolygons2D_F64.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import georegression.geometry.polygon.ThreeIndexes;
import georegression.geometry.polygon.TriangulateSimpleRemoveEars_F64;
import georegression.struct.point.Point2D_F64;
import georegression.struct.shapes.Polygon2D_F64;
import georegression.struct.shapes.Quadrilateral_F64;
import georegression.struct.shapes.Rectangle2D_F64;
import georegression.struct.shapes.RectangleLength2D_I32;
import georegression.struct.shapes.*;
import org.ddogleg.struct.DogArray;
import org.ejml.UtilEjml;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -273,6 +270,31 @@ public class TestUtilPolygons2D_F64 {
assertEquals(5, out.p1.y, TEST_F64);
}

@Test void bounding_polygon_aabbInt() {
var q = new Polygon2D_F64(3, 0, 2, -3, -2, 3, 1, 5);
var out = new Rectangle2D_I32();

UtilPolygons2D_F64.bounding(q, out);

assertEquals(-2, out.x0);
assertEquals(-3, out.y0);
assertEquals(4, out.x1);
assertEquals(6, out.y1);

// Test to see if it rounds correctly
q.get(2).x += 0.01;
q.get(0).x += 0.01;
q.get(1).y += 0.01;
q.get(3).y += 0.01;

UtilPolygons2D_F64.bounding(q, out);

assertEquals(-2, out.x0);
assertEquals(-3, out.y0);
assertEquals(4, out.x1);
assertEquals(6, out.y1);
}

@Test void center_quadrilateral() {
Quadrilateral_F64 q = new Quadrilateral_F64(3, 0, 2, -3, -2, 3, 1, 5);

Expand Down

0 comments on commit 5e36020

Please sign in to comment.