This is an automated email from the ASF dual-hosted git repository. erans pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-geometry.git
commit c206720b669c56dfb8af1e9c5d3579ab25e59b28 Author: Sven Rathgeber <[email protected]> AuthorDate: Mon Mar 25 16:35:12 2019 +0100 GEOMETRY-29 adds tests --- .../commons/geometry/euclidean/threed/Plane.java | 18 +- .../geometry/euclidean/threed/PlaneTest.java | 224 +++++++++++++-------- 2 files changed, 154 insertions(+), 88 deletions(-) diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Plane.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Plane.java index 59e83ea..d0556da 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Plane.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Plane.java @@ -78,7 +78,6 @@ public final class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Ve this.originOffset = originOffset; this.precision = precision; } - /** * Build a plane from a point and two (on plane) vectors. @@ -94,10 +93,10 @@ public final class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Ve { Vector3D u_norm = u.normalize(); Vector3D v_norm = v.normalize(); - Vector3D w = u_norm.cross(v_norm); - double originOffset = -p.dot(w); - Vector3D projectedOrigin = calculateOrigin(w, originOffset); - return new Plane(u_norm, v_norm, w, projectedOrigin, originOffset, precision); + Vector3D w_norm = u_norm.cross(v_norm).normalize(); + double originOffset = -p.dot(w_norm); + Vector3D projectedOrigin = calculateOrigin(w_norm, originOffset); + return new Plane(u_norm, v_norm, w_norm, projectedOrigin, originOffset, precision); } /** @@ -535,7 +534,14 @@ public final class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Ve return false; } - + /** Check, if the plane is parallel to the instance. + * @param plane plane to check. + * @return true if the plane is parallel to the instance, false otherwise. + */ + public boolean isParallel(Plane plane) { + return precision.eqZero(w.cross(plane.w).norm()); + } + /** * Get the offset (oriented distance) of a parallel plane. diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PlaneTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PlaneTest.java index ac0efae..5dde469 100644 --- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PlaneTest.java +++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PlaneTest.java @@ -31,17 +31,17 @@ public class PlaneTest { private static final DoublePrecisionContext TEST_PRECISION = new EpsilonDoublePrecisionContext(TEST_EPS); - @Test(expected=IllegalArgumentException.class) + @Test(expected=IllegalNormException.class) public void testUAndVAreIdentical() { Plane.fromPointAndPlaneVectors(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=IllegalNormException.class) public void testUAndVAreCollinear() { Plane.fromPointAndPlaneVectors(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 2), TEST_PRECISION); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=IllegalNormException.class) public void testUAndVAreCollinear2() { Plane.fromPointAndPlaneVectors(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), Vector3D.of(0, 0, -2), TEST_PRECISION); } @@ -53,43 +53,103 @@ public class PlaneTest { @Test public void testContains() { - Plane p = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); - Assert.assertTrue(p.contains(Vector3D.of(0, 0, 1))); - Assert.assertTrue(p.contains(Vector3D.of(17, -32, 1))); - Assert.assertTrue(! p.contains(Vector3D.of(17, -32, 1.001))); + Plane plane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); + Assert.assertTrue(plane.contains(Vector3D.of(0, 0, 1))); + Assert.assertTrue(plane.contains(Vector3D.of(17, -32, 1))); + Assert.assertTrue(! plane.contains(Vector3D.of(17, -32, 1.001))); } @Test public void testContainsLine() { - Plane p = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); + Plane plane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); Line line = new Line(Vector3D.of(1, 0, 1), Vector3D.of(2, 0, 1), TEST_PRECISION); - Assert.assertTrue(p.contains(line)); + Assert.assertTrue(plane.contains(line)); + } + + @Test + public void testIsParallelAndGetOffset() + { + Plane plane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); + Line parallelLine = new Line(Vector3D.of(1, 0, 2), Vector3D.of(2, 0, 2), TEST_PRECISION); + Assert.assertTrue(plane.isParallel(parallelLine)); + Assert.assertEquals(1.0, plane.getOffset(parallelLine), TEST_EPS); + Line nonParallelLine = new Line(Vector3D.of(1, 0, 2), Vector3D.of(2, 0, 1), TEST_PRECISION); + Assert.assertFalse(plane.isParallel(nonParallelLine)); + Assert.assertEquals(0.0, plane.getOffset(nonParallelLine), TEST_EPS); + } + + @Test + public void testCopy() + { + Plane plane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); + Plane copyPlane = Plane.of(plane); + Assert.assertEquals(plane, copyPlane); } + @Test + public void testCreation() + { + Vector3D normalAliasW = Vector3D.of(0, 0, 1); + Plane plane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1),normalAliasW , TEST_PRECISION); + Assert.assertEquals(normalAliasW, plane.getW()); + double expectedX = 1.0; + Assert.assertEquals(expectedX, Math.abs(plane.getV().getX()), TEST_EPS); + double expectedY = 1.0; + Assert.assertEquals(expectedY, Math.abs(plane.getU().getY()), TEST_EPS); + Assert.assertEquals(-1.0, plane.getOriginOffset(), TEST_EPS); + Vector3D expectedOrigin = Vector3D.of(0, 0, 1); + Assert.assertEquals(expectedOrigin, plane.getOrigin()); + } + + @Test + public void testReverse() + { + Vector3D normalAliasW = Vector3D.of(0, 0, 1); + Plane plane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1),normalAliasW , TEST_PRECISION); + Assert.assertEquals(-1.0, plane.getOriginOffset(), TEST_EPS); + Vector3D p1 = Vector3D.of(1,0,1); + Assert.assertTrue(plane.contains(p1)); + Plane reversePlane = plane.reverse(); + Assert.assertEquals(1.0, reversePlane.getOriginOffset(), TEST_EPS); + Vector3D p1XYswapped = Vector3D.of(0,1,1); + Assert.assertTrue(reversePlane.contains(p1XYswapped)); + } + + @Test + public void testIsPlaneParallel() + { + Plane plane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); + Plane parallelPlane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); + Plane parallelPlane2 = Plane.fromPointAndNormal(Vector3D.of(0, 0, 2), Vector3D.of(0, 0, 1), TEST_PRECISION); + Assert.assertTrue(plane.isParallel(parallelPlane)); + Assert.assertTrue(plane.isParallel(parallelPlane2)); + Plane nonParallelPlane = Plane.fromPointAndPlaneVectors(Vector3D.of(0, 0, 1), Vector3D.of(1, 1.5, 1), Vector3D.of(0,1,1), TEST_PRECISION); + Assert.assertFalse(plane.isParallel(nonParallelPlane)); + } @Test public void testProjectLine() { - Plane p = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); + Plane plane = Plane.fromPointAndNormal(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); Line line = new Line(Vector3D.of(1, 0, 1), Vector3D.of(2, 0, 2), TEST_PRECISION); Line expectedProjection = new Line(Vector3D.of(1, 0, 1),Vector3D.of(2, 0, 1), TEST_PRECISION); - Assert.assertEquals(expectedProjection, p.project(line)); + Assert.assertEquals(expectedProjection, plane.project(line)); } @Test public void testOffset() { Vector3D p1 = Vector3D.of(1, 1, 1); - Plane p = Plane.fromPointAndNormal(p1, Vector3D.of(0.2, 0, 0), TEST_PRECISION); - Assert.assertEquals(-5.0, p.getOffset(Vector3D.of(-4, 0, 0)), TEST_EPS); - Assert.assertEquals(+5.0, p.getOffset(Vector3D.of(6, 10, -12)), TEST_EPS); + Plane plane = Plane.fromPointAndNormal(p1, Vector3D.of(0.2, 0, 0), TEST_PRECISION); + Assert.assertEquals(-5.0, plane.getOffset(Vector3D.of(-4, 0, 0)), TEST_EPS); + Assert.assertEquals(+5.0, plane.getOffset(Vector3D.of(6, 10, -12)), TEST_EPS); Assert.assertEquals(0.3, - p.getOffset(Vector3D.linearCombination(1.0, p1, 0.3, p.getNormal())), + plane.getOffset(Vector3D.linearCombination(1.0, p1, 0.3, plane.getNormal())), TEST_EPS); Assert.assertEquals(-0.3, - p.getOffset(Vector3D.linearCombination(1.0, p1, -0.3, p.getNormal())), + plane.getOffset(Vector3D.linearCombination(1.0, p1, -0.3, plane.getNormal())), TEST_EPS); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=IllegalNormException.class) public void testVectorsAreColinear() { Plane.fromPointAndPlaneVectors(Vector3D.of(1, 1, 1), Vector3D.of(2, 0, 0), Vector3D.of(2,0,0), TEST_PRECISION); @@ -98,27 +158,27 @@ public class PlaneTest { @Test public void testVectorsAreNormalizedForSuppliedUAndV() { - Plane p = Plane.fromPointAndPlaneVectors(Vector3D.of(1, 1, 1), Vector3D.of(2, 0, 0), Vector3D.of(0,2,0), TEST_PRECISION); - Assert.assertEquals(1.0, p.getNormal().norm(), TEST_EPS); - Assert.assertEquals(1.0, p.getV().norm(), TEST_EPS); - Assert.assertEquals(1.0, p.getU().norm(), TEST_EPS); + Plane plane = Plane.fromPointAndPlaneVectors(Vector3D.of(1, 1, 1), Vector3D.of(2, 0, 0), Vector3D.of(0,2,0), TEST_PRECISION); + Assert.assertEquals(1.0, plane.getNormal().norm(), TEST_EPS); + Assert.assertEquals(1.0, plane.getV().norm(), TEST_EPS); + Assert.assertEquals(1.0, plane.getU().norm(), TEST_EPS); } @Test public void testVectorsAreNormalized() { - Plane p = Plane.fromPointAndNormal(Vector3D.of(2, -3, 1), Vector3D.of(1, 4, 9), TEST_PRECISION); - Assert.assertEquals(1.0, p.getNormal().norm(), TEST_EPS); - Assert.assertEquals(1.0, p.getV().norm(), TEST_EPS); - Assert.assertEquals(1.0, p.getU().norm(), TEST_EPS); + Plane plane = Plane.fromPointAndNormal(Vector3D.of(2, -3, 1), Vector3D.of(1, 4, 9), TEST_PRECISION); + Assert.assertEquals(1.0, plane.getNormal().norm(), TEST_EPS); + Assert.assertEquals(1.0, plane.getV().norm(), TEST_EPS); + Assert.assertEquals(1.0, plane.getU().norm(), TEST_EPS); } @Test public void testPoint() { - Plane p = Plane.fromPointAndNormal(Vector3D.of(2, -3, 1), Vector3D.of(1, 4, 9), TEST_PRECISION); - Assert.assertTrue(p.contains(p.getOrigin())); + Plane plane = Plane.fromPointAndNormal(Vector3D.of(2, -3, 1), Vector3D.of(1, 4, 9), TEST_PRECISION); + Assert.assertTrue(plane.contains(plane.getOrigin())); } @Test @@ -126,10 +186,10 @@ public class PlaneTest { Vector3D p1 = Vector3D.of(1.2, 3.4, -5.8); Vector3D p2 = Vector3D.of(3.4, -5.8, 1.2); Vector3D p3 = Vector3D.of(-2.0, 4.3, 0.7); - Plane p = Plane.fromPoints(p1, p2, p3, TEST_PRECISION); - Assert.assertTrue(p.contains(p1)); - Assert.assertTrue(p.contains(p2)); - Assert.assertTrue(p.contains(p3)); + Plane plane = Plane.fromPoints(p1, p2, p3, TEST_PRECISION); + Assert.assertTrue(plane.contains(p1)); + Assert.assertTrue(plane.contains(p2)); + Assert.assertTrue(plane.contains(p3)); } @Test @@ -137,23 +197,23 @@ public class PlaneTest { Vector3D p1 = Vector3D.of(1.2, 3.4, -5.8); Vector3D p2 = Vector3D.of(3.4, -5.8, 1.2); Vector3D p3 = Vector3D.of(-2.0, 4.3, 0.7); - Plane p = Plane.fromPoints(p1, p2, p3, TEST_PRECISION); - Vector3D oldNormal = p.getNormal(); + Plane plane = Plane.fromPoints(p1, p2, p3, TEST_PRECISION); + Vector3D oldNormal = plane.getNormal(); - p = p.rotate(p2, QuaternionRotation.fromAxisAngle(p2.subtract(p1), 1.7)); - Assert.assertTrue(p.contains(p1)); - Assert.assertTrue(p.contains(p2)); - Assert.assertTrue(! p.contains(p3)); + plane = plane.rotate(p2, QuaternionRotation.fromAxisAngle(p2.subtract(p1), 1.7)); + Assert.assertTrue(plane.contains(p1)); + Assert.assertTrue(plane.contains(p2)); + Assert.assertTrue(! plane.contains(p3)); - p = p.rotate(p2, QuaternionRotation.fromAxisAngle(oldNormal, 0.1)); - Assert.assertTrue(! p.contains(p1)); - Assert.assertTrue(p.contains(p2)); - Assert.assertTrue(! p.contains(p3)); + plane = plane.rotate(p2, QuaternionRotation.fromAxisAngle(oldNormal, 0.1)); + Assert.assertTrue(! plane.contains(p1)); + Assert.assertTrue(plane.contains(p2)); + Assert.assertTrue(! plane.contains(p3)); - p = p.rotate(p1, QuaternionRotation.fromAxisAngle(oldNormal, 0.1)); - Assert.assertTrue(! p.contains(p1)); - Assert.assertTrue(! p.contains(p2)); - Assert.assertTrue(! p.contains(p3)); + plane = plane.rotate(p1, QuaternionRotation.fromAxisAngle(oldNormal, 0.1)); + Assert.assertTrue(! plane.contains(p1)); + Assert.assertTrue(! plane.contains(p2)); + Assert.assertTrue(! plane.contains(p3)); } @@ -162,34 +222,34 @@ public class PlaneTest { Vector3D p1 = Vector3D.of(1.2, 3.4, -5.8); Vector3D p2 = Vector3D.of(3.4, -5.8, 1.2); Vector3D p3 = Vector3D.of(-2.0, 4.3, 0.7); - Plane p = Plane.fromPoints(p1, p2, p3, TEST_PRECISION); + Plane plane = Plane.fromPoints(p1, p2, p3, TEST_PRECISION); - p = p.translate(Vector3D.linearCombination(2.0, p.getU(), -1.5, p.getV())); - Assert.assertTrue(p.contains(p1)); - Assert.assertTrue(p.contains(p2)); - Assert.assertTrue(p.contains(p3)); + plane = plane.translate(Vector3D.linearCombination(2.0, plane.getU(), -1.5, plane.getV())); + Assert.assertTrue(plane.contains(p1)); + Assert.assertTrue(plane.contains(p2)); + Assert.assertTrue(plane.contains(p3)); - p = p.translate(Vector3D.linearCombination(-1.2, p.getNormal())); - Assert.assertTrue(! p.contains(p1)); - Assert.assertTrue(! p.contains(p2)); - Assert.assertTrue(! p.contains(p3)); + plane = plane.translate(Vector3D.linearCombination(-1.2, plane.getNormal())); + Assert.assertTrue(! plane.contains(p1)); + Assert.assertTrue(! plane.contains(p2)); + Assert.assertTrue(! plane.contains(p3)); - p = p.translate(Vector3D.linearCombination(+1.2, p.getNormal())); - Assert.assertTrue(p.contains(p1)); - Assert.assertTrue(p.contains(p2)); - Assert.assertTrue(p.contains(p3)); + plane = plane.translate(Vector3D.linearCombination(+1.2, plane.getNormal())); + Assert.assertTrue(plane.contains(p1)); + Assert.assertTrue(plane.contains(p2)); + Assert.assertTrue(plane.contains(p3)); } @Test public void testIntersection() { - Plane p = Plane.fromPointAndNormal(Vector3D.of(1, 2, 3), Vector3D.of(-4, 1, -5), TEST_PRECISION); - Line l = new Line(Vector3D.of(0.2, -3.5, 0.7), Vector3D.of(1.2, -2.5, -0.3), TEST_PRECISION); - Vector3D point = p.intersection(l); - Assert.assertTrue(p.contains(point)); - Assert.assertTrue(l.contains(point)); - Assert.assertNull(p.intersection(new Line(Vector3D.of(10, 10, 10), - Vector3D.of(10, 10, 10).add(p.getNormal().orthogonal()), + Plane plane = Plane.fromPointAndNormal(Vector3D.of(1, 2, 3), Vector3D.of(-4, 1, -5), TEST_PRECISION); + Line line = new Line(Vector3D.of(0.2, -3.5, 0.7), Vector3D.of(1.2, -2.5, -0.3), TEST_PRECISION); + Vector3D point = plane.intersection(line); + Assert.assertTrue(plane.contains(point)); + Assert.assertTrue(line.contains(point)); + Assert.assertNull(plane.intersection(new Line(Vector3D.of(10, 10, 10), + Vector3D.of(10, 10, 10).add(plane.getNormal().orthogonal()), TEST_PRECISION))); } @@ -197,12 +257,12 @@ public class PlaneTest { public void testIntersection2() { Vector3D p1 = Vector3D.of(1.2, 3.4, -5.8); Vector3D p2 = Vector3D.of(3.4, -5.8, 1.2); - Plane pA = Plane.fromPoints(p1, p2, Vector3D.of(-2.0, 4.3, 0.7), TEST_PRECISION); - Plane pB = Plane.fromPoints(p1, Vector3D.of(11.4, -3.8, 5.1), p2, TEST_PRECISION); - Line l = pA.intersection(pB); - Assert.assertTrue(l.contains(p1)); - Assert.assertTrue(l.contains(p2)); - Assert.assertNull(pA.intersection(pA)); + Plane planeA = Plane.fromPoints(p1, p2, Vector3D.of(-2.0, 4.3, 0.7), TEST_PRECISION); + Plane planeB = Plane.fromPoints(p1, Vector3D.of(11.4, -3.8, 5.1), p2, TEST_PRECISION); + Line line = planeA.intersection(planeB); + Assert.assertTrue(line.contains(p1)); + Assert.assertTrue(line.contains(p2)); + Assert.assertNull(planeA.intersection(planeA)); } @Test @@ -211,10 +271,10 @@ public class PlaneTest { Plane p1 = Plane.fromPointAndNormal(reference, Vector3D.of(1, 3, 3), TEST_PRECISION); Plane p2 = Plane.fromPointAndNormal(reference, Vector3D.of(-2, 4, 0), TEST_PRECISION); Plane p3 = Plane.fromPointAndNormal(reference, Vector3D.of(7, 0, -4), TEST_PRECISION); - Vector3D p = Plane.intersection(p1, p2, p3); - Assert.assertEquals(reference.getX(), p.getX(), TEST_EPS); - Assert.assertEquals(reference.getY(), p.getY(), TEST_EPS); - Assert.assertEquals(reference.getZ(), p.getZ(), TEST_EPS); + Vector3D plane = Plane.intersection(p1, p2, p3); + Assert.assertEquals(reference.getX(), plane.getX(), TEST_EPS); + Assert.assertEquals(reference.getY(), plane.getY(), TEST_EPS); + Assert.assertEquals(reference.getZ(), plane.getZ(), TEST_EPS); } @Test @@ -222,13 +282,13 @@ public class PlaneTest { Vector3D p1 = Vector3D.of(1.2, 3.4, -5.8); Vector3D p2 = Vector3D.of(3.4, -5.8, 1.2); Vector3D p3 = Vector3D.of(-2.0, 4.3, 0.7); - Plane pA = Plane.fromPoints(p1, p2, p3, TEST_PRECISION); - Plane pB = Plane.fromPoints(p1, Vector3D.of(11.4, -3.8, 5.1), p2, TEST_PRECISION); - Assert.assertTrue(! pA.contains(pB)); - Assert.assertTrue(pA.contains(pA)); - Assert.assertTrue(pA.contains(Plane.fromPoints(p1, p3, p2, TEST_PRECISION))); - Vector3D shift = Vector3D.linearCombination(0.3, pA.getNormal()); - Assert.assertTrue(! pA.contains(Plane.fromPoints(p1.add(shift), + Plane planeA = Plane.fromPoints(p1, p2, p3, TEST_PRECISION); + Plane planeB = Plane.fromPoints(p1, Vector3D.of(11.4, -3.8, 5.1), p2, TEST_PRECISION); + Assert.assertTrue(! planeA.contains(planeB)); + Assert.assertTrue(planeA.contains(planeA)); + Assert.assertTrue(planeA.contains(Plane.fromPoints(p1, p3, p2, TEST_PRECISION))); + Vector3D shift = Vector3D.linearCombination(0.3, planeA.getNormal()); + Assert.assertTrue(! planeA.contains(Plane.fromPoints(p1.add(shift), p3.add(shift), p2.add(shift), TEST_PRECISION)));
