/** * 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(); }
/** * This method does: * <ol> * <li>{@link #clear()}. * <li>{@link #addVertices(Vertex2DSupplier)}. * <li>{@link #update()}. * </ol> * * @param vertex2DSupplier the supplier of vertices. * @see Vertex2DSupplier * @see #addVertices(Vertex2DSupplier) */ default void set(Vertex2DSupplier vertex2DSupplier) { clear(); addVertices(vertex2DSupplier); update(); }
/** * This method does: * <ol> * <li>{@link #clear()}. * <li>{@link #addVertices(Vertex3DSupplier)}. * <li>{@link #update()}. * </ol> * * @param vertex3DSupplier the supplier of vertices. * @see Vertex3DSupplier * @see #addVertices(Vertex3DSupplier) */ default void set(Vertex3DSupplier vertex3DSupplier) { clear(); addVertices(vertex3DSupplier); update(); }
/** * Sets this polygon such that it represents the smallest convex hull that contains all the vertices * supplied by the two suppliers. * <p> * Note that the resulting polygon is ready to be used for any operations, no need to call * {@link #update()}. * </p> * * @param firstVertex2DSupplier the first supplier of vertices. * @param secondVertex2DSupplier the second supplier of vertices. */ default void set(Vertex2DSupplier firstVertex2DSupplier, Vertex2DSupplier secondVertex2DSupplier) { clear(); addVertices(firstVertex2DSupplier); addVertices(secondVertex2DSupplier); update(); }
@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); }
/** * Transforms this convex polygon using the inverse of the given homogeneous transformation matrix. * * @param transform the transform to apply on the vertices of this convex polygon. Not modified. * @param checkIfTransformInXYPlane whether this method should assert that the rotation part of the * given transform represents a transformation in the XY plane. * @throws OutdatedPolygonException if {@link #update()} has not been called since last time this * polygon's vertices were edited. * @throws EmptyPolygonException if this polygon is empty when calling this method. * @throws NotAMatrix2DException if {@code checkIfTransformInXYPlane == true} and the rotation part * of {@code transform} is not a transformation in the XY plane. */ default void applyInverseTransform(Transform transform, boolean checkIfTransformInXYPlane) { checkIfUpToDate(); notifyVerticesChanged(); for (int i = 0; i < getNumberOfVertices(); i++) { getVertexUnsafe(i).applyInverseTransform(transform, checkIfTransformInXYPlane); } update(); } }
@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 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 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 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 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 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 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 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 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 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); }
@Test public void testIsPointInside1() { // single point polygon T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(1.0, 1.0)); polygon.update(); Point2D point1 = new Point2D(1.0, 1.0); assertTrue(polygon.isPointInside(point1, EPSILON)); Point2D point2 = new Point2D(0.8, 0.9); assertFalse(polygon.isPointInside(point2)); Point2D point3 = new Point2D(0.8, 1.1); assertTrue(polygon.isPointInside(point3, 0.3)); Point2D point4 = new Point2D(1.0, 0.9); assertFalse(polygon.isPointInside(point4)); Point2D point5 = new Point2D(2.0, 1.0); assertFalse(polygon.isPointInside(point5)); assertTrue(polygon.isPointInside(point5, 1.0)); Point2D point6 = new Point2D(1.0, 2.0); assertFalse(polygon.isPointInside(point6)); assertTrue(polygon.isPointInside(point6, 1.0)); }
@Test public void testGetClosestPointToRay4() { T polygon = createEmptyConvexPolygon2D(); polygon.addVertex(new Point2D(2.0, -5.0)); polygon.addVertex(new Point2D(1.0, -6.0)); polygon.update(); Line2D ray1 = new Line2D(new Point2D(1.0, -5.0), new Vector2D(1.0, 0.1)); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(2.0, -5.0), polygon.getClosestPointWithRay(ray1), EPSILON); Line2D ray2 = new Line2D(new Point2D(1.25, -5.25), new Vector2D(0.75, 0.3)); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(2.0, -5.0), polygon.getClosestPointWithRay(ray2), EPSILON); Line2D ray3 = new Line2D(new Point2D(1.25, -5.25), new Vector2D(0.75, 0.8)); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(1.5, -5.5), polygon.getClosestPointWithRay(ray3), EPSILON); Line2D ray4 = new Line2D(new Point2D(1.25, -5.25), new Vector2D(1.0, 1.0)); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(1.5, -5.5), polygon.getClosestPointWithRay(ray4), EPSILON); Line2D ray5 = new Line2D(new Point2D(1.25, -5.25), new Vector2D(-1.0, -1.0)); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(1.5, -5.5), polygon.getClosestPointWithRay(ray5), EPSILON); Line2D ray6 = new Line2D(new Point2D(1.75, -5.75), new Vector2D(1.0, 1.0)); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(1.5, -5.5), polygon.getClosestPointWithRay(ray6), EPSILON); Line2D ray7 = new Line2D(new Point2D(1.75, -5.75), new Vector2D(-1.0, -1.0)); EuclidCoreTestTools.assertTuple2DEquals(new Point2D(1.5, -5.5), polygon.getClosestPointWithRay(ray7), EPSILON); }