/** * Add a vertex to this polygon. * * @param vertex the new vertex. Not modified. * @throws ReferenceFrameMismatchException if {@code vertex} and {@code this} are not expressed * in the same reference frame. * @see #addVertex(Point2DReadOnly) */ default void addVertex(FramePoint2DReadOnly vertex) { checkReferenceFrameMatch(vertex); ConvexPolygon2DBasics.super.addVertex(vertex); }
/** * Add a vertex to this polygon using only the x and y coordinates of the given {@code vertex}. * * @param vertex the new vertex. Not modified. * @throws ReferenceFrameMismatchException if {@code vertex} and {@code this} are not expressed * in the same reference frame. * @see #addVertex(Point3DReadOnly) */ default void addVertex(FramePoint3DReadOnly vertex) { checkReferenceFrameMatch(vertex); ConvexPolygon2DBasics.super.addVertex(vertex); }
/** * Clears this polygon, adds a single vertex at (0, 0), and updates it. */ @Override default void setToZero() { clear(); addVertex(0.0, 0.0); update(); }
/** * Clears this polygon, adds a single vertex at ({@link Double#NaN}, {@link Double#NaN}), and * updates it. */ @Override default void setToNaN() { clear(); addVertex(Double.NaN, Double.NaN); update(); }
/** * Add a vertex to this polygon using the x and y coordinates of the given {@code vertex}. * <p> * Note that this polygon is marked as being out-of-date. The method {@link #update()} has to be * called afterward before being able to perform operations with this polygon. * </p> * * @param vertex the new vertex. Not modified. */ default void addVertex(Point3DReadOnly vertex) { addVertex(vertex.getX(), vertex.getY()); }
/** * Add a vertex to this polygon. * <p> * Note that this polygon is marked as being out-of-date. The method {@link #update()} has to be * called afterward before being able to perform operations with this polygon. * </p> * * @param vertex the new vertex. Not modified. */ default void addVertex(Point2DReadOnly vertex) { addVertex(vertex.getX(), vertex.getY()); }
/** * Adds the vertices from the given vertex supplier. * <p> * Note that this polygon is marked as being out-of-date. The method {@link #update()} has to be * called afterward before being able to perform operations with this polygon. * </p> * * @param vertex2DSupplier the supplier of vertices. * @see Vertex2DSupplier */ default void addVertices(Vertex2DSupplier vertex2DSupplier) { for (int index = 0; index < vertex2DSupplier.getNumberOfVertices(); index++) { addVertex(vertex2DSupplier.getVertex(index)); } }
/** * Adds the vertices from the given vertex supplier. * <p> * Note that this polygon is marked as being out-of-date. The method {@link #update()} has to be * called afterward before being able to perform operations with this polygon. * </p> * <p> * Only the x and y coordinates of each vertex is used to add a vertex to this polygon. * </p> * * @param vertex3DSupplier the supplier of vertices. * @see Vertex3DSupplier */ default void addVertices(Vertex3DSupplier vertex3DSupplier) { for (int index = 0; index < vertex3DSupplier.getNumberOfVertices(); index++) { addVertex(vertex3DSupplier.getVertex(index)); } }
@Test public void testIsPointInside5() { // foot polygon T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(-0.06, -0.08)); polygon.addVertex(new Point2D(0.14, -0.08)); polygon.addVertex(new Point2D(0.14, -0.19)); polygon.addVertex(new Point2D(-0.06, -0.19)); polygon.update(); Point2D point1 = new Point2D(0.03, 0.0); assertFalse(polygon.isPointInside(point1, 0.02)); Point2D point2 = new Point2D(0.03, -0.09); assertTrue(polygon.isPointInside(point2)); }
@Test public void testGetSignedDistance1() { // single point polygon T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(0.0, 0.0)); polygon.update(); Point2D point = new Point2D(2.5, 1.0); double distance = polygon.signedDistance(point); assertDistanceCorrect(Math.sqrt(2.5 * 2.5 + 1.0 * 1.0), distance); }
@Test public void testCanObserverSeeEdge2() { // line polygon T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(1.0, 0.0)); polygon.addVertex(new Point2D(0.0, 1.0)); polygon.update(); // should be able to see one edge Point2D observer1 = new Point2D(0.0, 0.0); boolean seeEdge1 = polygon.canObserverSeeEdge(0, observer1); boolean seeEdge2 = polygon.canObserverSeeEdge(1, observer1); assertTrue((seeEdge1 || seeEdge2) && !(seeEdge1 && seeEdge2)); }
@Test public void testGetSignedDistance2() { // line polygon T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(0.0, 0.0)); polygon.addVertex(new Point2D(1.0, 0.0)); polygon.update(); Point2D point1 = new Point2D(2.5, 1.0); double distance1 = polygon.signedDistance(point1); assertDistanceCorrect(Math.sqrt(1.5 * 1.5 + 1.0 * 1.0), distance1); Point2D point2 = new Point2D(0.5, 1.0); double distance2 = polygon.signedDistance(point2); assertDistanceCorrect(1.0, distance2); }
@Test public void testGetClosestEdge3() { T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D()); polygon.update(); assertTrue(polygon.getClosestEdgeCopy(new Point2D()) == null); }
@Test public void testCanObserverSeeEdge3() { // point polygon T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(1.0, 1.0)); polygon.update(); Point2D observer1 = new Point2D(0.0, 0.0); assertFalse(polygon.canObserverSeeEdge(0, observer1)); }
@Test public void testGetClosestEdge2() { Point2D vertex1 = new Point2D(2.0, 2.0); Point2D vertex2 = new Point2D(3.0, 3.0); T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(vertex1); polygon.addVertex(vertex2); polygon.update(); LineSegment2D edge1 = new LineSegment2D(vertex1, vertex2); Point2D point1 = new Point2D(0.5, 0.1); EuclidGeometryTestTools.assertLineSegment2DGeometricallyEquals(edge1, polygon.getClosestEdgeCopy(point1), EPSILON); Point2D point2 = new Point2D(4.0, 4.0); EuclidGeometryTestTools.assertLineSegment2DGeometricallyEquals(edge1, polygon.getClosestEdgeCopy(point2), EPSILON); Point2D point3 = new Point2D(1.0, 1.0); EuclidGeometryTestTools.assertLineSegment2DGeometricallyEquals(edge1, polygon.getClosestEdgeCopy(point3), EPSILON); }
@Test public void testTranslate() { T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(0.0, 0.0)); polygon.update(); Vector2D translation1 = new Vector2D(-0.1, 0.0); polygon.translate(translation1); assertTrue(polygon.getVertex(0).epsilonEquals(translation1, EPSILON)); }
@Test public void testOrthogonalProjection4() { // line polygon T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(1.0, 2.0)); polygon.addVertex(new Point2D(1.0, 1.0)); polygon.update(); Point2D point1 = new Point2D(1.0, -1.0); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(1.0, 1.0), polygon.orthogonalProjectionCopy(point1), EPSILON); Point2D point2 = new Point2D(3.0, 2.1); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(1.0, 2.0), polygon.orthogonalProjectionCopy(point2), EPSILON); Point2D point3 = new Point2D(0.2, 1.2); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(1.0, 1.2), polygon.orthogonalProjectionCopy(point3), EPSILON); }
@Test public void testOrthogonalProjection3() { // single point polygon Point2D vertex = new Point2D(1.0, 2.0); T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(vertex); polygon.update(); EuclidCoreTestTools.assertTuple2DEquals(vertex, polygon.orthogonalProjectionCopy(new Point2D(0.0, 0.0)), EPSILON); EuclidCoreTestTools.assertTuple2DEquals(vertex, polygon.orthogonalProjectionCopy(new Point2D(1.0, -0.2)), EPSILON); EuclidCoreTestTools.assertTuple2DEquals(vertex, polygon.orthogonalProjectionCopy(new Point2D(1.0, 2.0)), EPSILON); }
@Test public void testGetClosestPointToRay3() { Point2D vertex = new Point2D(1.0, -1.0); T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(vertex); polygon.update(); Line2D ray1 = new Line2D(new Point2D(5.0, -3.0), new Vector2D(0.0, 1.0)); EuclidCoreTestTools.assertTuple2DEquals(vertex, polygon.getClosestPointWithRay(ray1), EPSILON); Line2D ray2 = new Line2D(new Point2D(0.0, 0.0), new Vector2D(1.0, 0.0)); EuclidCoreTestTools.assertTuple2DEquals(vertex, polygon.getClosestPointWithRay(ray2), EPSILON); }
@Test public void testIntersectionWithLine3() { // point polygon T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(1.0, 0.0)); polygon.update(); Line2D line1 = new Line2D(new Point2D(3.0, 1.0), new Vector2D(-2.0, -1.0)); Point2D[] expected1 = new Point2D[] {new Point2D(1.0, 0.0)}; assertPointsEqual(expected1, polygon.intersectionWith(line1), false); Line2D line2 = new Line2D(new Point2D(2.0, 1.0), new Vector2D(-1.3, -0.8)); Point2D[] expected2 = null; assertPointsEqual(expected2, polygon.intersectionWith(line2), false); }