/** * Determines if the polygonToTest is inside the convex polygon. */ public static boolean isPolygonInside(ConvexPolygon2d polygonToTest, ConvexPolygon2d polygon) { return isPolygonInside(polygonToTest, 0.0, polygon); }
assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line1, 0, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line1, 1, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line1, 2, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line1, 3, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line1, 4, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line2, 0, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line2, 1, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line2, 2, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line2, 3, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line2, 4, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line3, 0, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line3, 1, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line3, 2, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line3, 3, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line3, 4, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line4, 0, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line4, 1, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line4, 2, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line4, 3, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line4, 4, polygon));
public static LineSegment2D[] getIntersectingEdgesCopy(Line2DReadOnly line, ConvexPolygon2DReadOnly polygon) { LineSegment2D edge1 = new LineSegment2D(); LineSegment2D edge2 = new LineSegment2D(); int edges = getIntersectingEdges(line, edge1, edge2, polygon); if (edges == 2) return new LineSegment2D[] {edge1, edge2}; if (edges == 1) return new LineSegment2D[] {edge1}; return null; } }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testGetIntersectingEdges4() { // empty polygon ConvexPolygon2D polygon = new ConvexPolygon2D(); LineSegment2D result1 = new LineSegment2D(); LineSegment2D result2 = new LineSegment2D(); Line2D line1 = new Line2D(new Point2D(0.5, 1.5), new Vector2D(0.0, 0.1)); assertEquals(ConvexPolygon2dCalculator.getIntersectingEdges(line1, result1, result2, polygon), 0); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line1, polygon) == null); }
assertEquals(1, polygonWithOnePoint.getNumberOfVertices()); assertTrue(polygonWithOnePoint.getVertexCCW(0).equals(pointThatDefinesThePolygon)); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(arbitraryLine, polygonWithOnePoint) == null); assertTrue(polygonWithOnePoint.getVertex(polygonWithOnePoint.lineOfSightStartIndex(arbitraryPoint0)).equals(pointThatDefinesThePolygon)); assertTrue(polygonWithOnePoint.getVertex(polygonWithOnePoint.lineOfSightEndIndex(arbitraryPoint0)).equals(pointThatDefinesThePolygon)); assertTrue(polygonWithOnePoint.intersectionWith(arbitraryLine) == null); assertFalse(polygonWithOnePoint.isPointInside(arbitraryPoint0)); assertFalse(ConvexPolygon2dCalculator.isPolygonInside(sparePolygon, polygonWithOnePoint)); assertTrue(polygonWithOnePoint.orthogonalProjectionCopy(arbitraryPoint0).equals(pointThatDefinesThePolygon)); assertTrue(polygonWithOnePoint.pointIsOnPerimeter(pointThatDefinesThePolygon));
for (int i = 0; i < polygon.getNumberOfVertices(); i++) if (doesLineIntersectEdge(line, i, polygon)) Point2d lineStart = line.getPoint(); Vector2d lineDirection = line.getNormalizedVector(); double lambda = getIntersectionLambda(edgeStart.x, edgeStart.y, edgeVectorX, edgeVectorY, lineStart.x, lineStart.y, lineDirection.x, lineDirection.y);
public LineSegment2d[] getIntersectingEdgesCopy(Line2d line) { return ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line, this); }
private void initSidePlanes() { Vector2D normal2d = new Vector2D(); for (int i = 0; i < convexPolygon.getNumberOfVertices(); i++) { ConvexPolygon2dCalculator.getEdgeNormal(i, normal2d, convexPolygon); Vector3D normal = new Vector3D(normal2d.getX(), normal2d.getY(), 0.0); int secondIndex = (i + 1 < convexPolygon.getNumberOfVertices()) ? i + 1 : 0; int[] indices = {i, secondIndex}; Point3D centerPoint = new Point3D(); Point3D checkingPoint = new Point3D(); for (int j : indices) { Point2DReadOnly vertex = convexPolygon.getVertex(j); checkingPoint.set(vertex.getX(), vertex.getY(), boundingBox.getMinZ()); checkingPoint.scale(0.25); centerPoint.add(checkingPoint); checkingPoint.set(vertex.getX(), vertex.getY(), heightAt(vertex.getX(), vertex.getY(), 0.0)); checkingPoint.scale(0.25); centerPoint.add(checkingPoint); } Plane3D sidePlane = new Plane3D(centerPoint, normal); sidePlanes.add(sidePlane); } }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testConstruction() { new ConvexPolygon2dCalculator(); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testGetIntersectingEdges3() { // point polygon ConvexPolygon2D polygon = new ConvexPolygon2D(); polygon.addVertex(new Point2D(-1.0, -0.5)); polygon.update(); LineSegment2D result1 = new LineSegment2D(); LineSegment2D result2 = new LineSegment2D(); Line2D line1 = new Line2D(new Point2D(0.0, 0.0), new Vector2D(-0.5, -0.25)); assertEquals(ConvexPolygon2dCalculator.getIntersectingEdges(line1, result1, result2, polygon), 0); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line1, polygon) == null); Line2D line2 = new Line2D(new Point2D(0.5, 1.5), new Vector2D(0.0, 0.1)); assertEquals(ConvexPolygon2dCalculator.getIntersectingEdges(line2, result1, result2, polygon), 0); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line2, polygon) == null); Line2D line3 = new Line2D(new Point2D(-1.0, -0.5), new Vector2D(1.0, 0.1)); assertEquals(ConvexPolygon2dCalculator.getIntersectingEdges(line3, result1, result2, polygon), 0); assertTrue(ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line3, polygon) == null); }
polygonWithTwoPoints.getVertex(1)); assertFalse(polygonWithTwoPoints.isPointInside(arbitraryPoint0)); assertFalse(ConvexPolygon2dCalculator.isPolygonInside(sparePolygon, polygonWithTwoPoints)); assertEquals(2, polygonWithTwoPoints.getNumberOfVertices()); assertTrue(polygonWithTwoPoints.getCentroid().getX() == 0.5 * (pointThatDefinesThePolygon0.getX() + pointThatDefinesThePolygon1.getX())); LineSegment2D[] intersectingEdges = ConvexPolygon2dCalculator.getIntersectingEdgesCopy(arbitraryLine, polygonWithTwoPoints); boolean isLineAbovePoint0 = ((pointThatDefinesThePolygon0.getX() - arbitraryLine.getPoint().getX()) * arbitraryLine.slope() + arbitraryLine.getPoint().getY()) >= pointThatDefinesThePolygon0.getY();
/** * Returns the intersecting edges between this polygon and the given FrameLine2d. * The FrameLine2d is treated as a ray. This method returns null if: * - The start point of the ray starts inside the Polygon, * - The ray points away from the polygon. * - The intersection is a single vertex, * - There is no intersection. * * @param frameLine2d FrameLine2d Ray to check intersection with this Polygon. * @return FrameLineSegment2d[] The two edges that the Ray intersects on this Polygon. */ public FrameLineSegment2d[] getIntersectingEdges(FrameLine2d frameLine2d) { frameLine2d.checkReferenceFrameMatch(referenceFrame); LineSegment2d[] lineSegments = ConvexPolygon2dCalculator.getIntersectingEdgesCopy(frameLine2d.getLine2dCopy(), convexPolygon); if (lineSegments == null) return null; FrameLineSegment2d[] ret = new FrameLineSegment2d[lineSegments.length]; for (int i = 0; i < lineSegments.length; i++) { ret[i] = new FrameLineSegment2d(referenceFrame, lineSegments[i]); } return ret; }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testGetEdgeNormal() { ConvexPolygon2D polygon = new ConvexPolygon2D(); polygon.addVertex(new Point2D(-1.0, 0.0)); polygon.addVertex(new Point2D(0.0, 1.0)); polygon.addVertex(new Point2D(2.0, 0.0)); polygon.addVertex(new Point2D(1.0, -1.0)); polygon.update(); for (int i = 0; i < polygon.getNumberOfVertices(); i++) { Vector2D normal = new Vector2D(); ConvexPolygon2dCalculator.getEdgeNormal(i, normal, polygon); Vector2D expected = new Vector2D(); Point2DReadOnly edgeStart = polygon.getVertex(i); Point2DReadOnly edgeEnd = polygon.getNextVertex(i); Vector2D edgeVector = new Vector2D(); edgeVector.sub(edgeEnd, edgeStart); EuclidGeometryTools.perpendicularVector2D(edgeVector, expected); expected.normalize(); assertTrue("Expected normal Vector did not match computed one.", expected.epsilonEquals(normal, epsilon)); } }
/** * Determines if the polygonToTest is inside the convex polygon. */ public static boolean isPolygonInside(ConvexPolygon2DReadOnly polygonToTest, ConvexPolygon2DReadOnly polygon) { return isPolygonInside(polygonToTest, 0.0, polygon); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testGetIntersectingEdges2() { // line polygon Point2D vertex1 = new Point2D(1.0, 1.0); Point2D vertex2 = new Point2D(1.0, 0.0); LineSegment2D edge1 = new LineSegment2D(vertex1, vertex2); LineSegment2D edge2 = new LineSegment2D(vertex2, vertex1); // add in order so update does not change indices: ConvexPolygon2D polygon = new ConvexPolygon2D(); polygon.addVertex(vertex1); polygon.addVertex(vertex2); polygon.update(); LineSegment2D result1 = new LineSegment2D(); LineSegment2D result2 = new LineSegment2D(); Line2D line1 = new Line2D(new Point2D(0.5, 1.5), new Vector2D(0.0, 0.1)); LineSegment2D[] expected1 = null; assertEdgesEqual(expected1, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line1, polygon), false); assertEquals(0, ConvexPolygon2dCalculator.getIntersectingEdges(line1, result1, result2, polygon)); Line2D line2 = new Line2D(new Point2D(-0.5, 0.0), new Vector2D(0.75, 0.25)); LineSegment2D[] expected2 = new LineSegment2D[] {edge1, edge2}; assertEdgesEqual(expected2, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line2, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line2, result1, result2, polygon)); assertEdgesEqual(expected2, new LineSegment2D[] {result1, result2}, false); Line2D line3 = new Line2D(new Point2D(1.0, -0.5), new Vector2D(0.0, 0.1)); LineSegment2D[] expected3 = null; assertEdgesEqual(expected3, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line3, polygon), false); assertEquals(0, ConvexPolygon2dCalculator.getIntersectingEdges(line3, result1, result2, polygon)); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testDoesLineIntersectEdge2() { // add in order so update does not change indices: ConvexPolygon2D polygon = new ConvexPolygon2D(); polygon.addVertex(new Point2D(0.0, 1.0)); polygon.addVertex(new Point2D(1.0, 1.0)); polygon.update(); Line2D line1 = new Line2D(new Point2D(0.0, 0.3), new Vector2D(1.0, 0.0)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line1, 0, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line1, 1, polygon)); Line2D line2 = new Line2D(new Point2D(0.0, 0.3), new Vector2D(0.0, 1.0)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line2, 0, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line2, 1, polygon)); Line2D line3 = new Line2D(new Point2D(0.0, 0.3), new Vector2D(0.0, -1.0)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line3, 0, polygon)); assertTrue(ConvexPolygon2dCalculator.doesLineIntersectEdge(line3, 1, polygon)); Line2D line4 = new Line2D(new Point2D(2.0, 0.3), new Vector2D(0.0, -1.0)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line4, 0, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line4, 1, polygon)); Line2D line5 = new Line2D(new Point2D(-1.0e-6, 0.3), new Vector2D(0.0, -1.0)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line5, 0, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line5, 1, polygon)); Line2D line6 = new Line2D(new Point2D(0.0, 0.3), new Vector2D(1.0, 0.0)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line6, 0, polygon)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line6, 1, polygon)); }
public static LineSegment2d[] getIntersectingEdgesCopy(Line2d line, ConvexPolygon2d polygon) { LineSegment2d edge1 = new LineSegment2d(); LineSegment2d edge2 = new LineSegment2d(); int edges = getIntersectingEdges(line, edge1, edge2, polygon); if (edges == 2) return new LineSegment2d[] {edge1, edge2}; if (edges == 1) return new LineSegment2d[] {edge1}; return null; }
Assert.assertTrue(ConvexPolygon2dCalculator.isPolygonInside(polygon, 10E-10, originalPolygon));
assertEdgesEqual(expected1, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line1, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line1, result1, result2, polygon)); assertEdgesEqual(expected1, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected2, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line2, polygon), false); assertEquals(0, ConvexPolygon2dCalculator.getIntersectingEdges(line2, result1, result2, polygon)); assertEdgesEqual(expected3, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line3, polygon), false); assertEquals(1, ConvexPolygon2dCalculator.getIntersectingEdges(line3, result1, result2, polygon)); assertEdgesEqual(expected3, new LineSegment2D[] {result1}, false); assertEdgesEqual(expected4, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line4, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line4, result1, result2, polygon)); assertEdgesEqual(expected4, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected5, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line5, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line5, result1, result2, polygon)); assertEdgesEqual(expected5, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected6, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line6, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line6, result1, result2, polygon)); assertEdgesEqual(expected6, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected7, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line7, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line7, result1, result2, polygon)); assertEdgesEqual(expected7, new LineSegment2D[] {result1, result2}, false); assertEdgesEqual(expected8, ConvexPolygon2dCalculator.getIntersectingEdgesCopy(line8, polygon), false); assertEquals(2, ConvexPolygon2dCalculator.getIntersectingEdges(line8, result1, result2, polygon));
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testDoesLineIntersectEdge3() { ConvexPolygon2D polygon = new ConvexPolygon2D(); Line2D line5 = new Line2D(new Point2D(0.0, 0.0), new Vector2D(1.0, 0.0)); assertFalse(ConvexPolygon2dCalculator.doesLineIntersectEdge(line5, 0, polygon)); }