Plane { public Vec3[] clip(Vec3 pointA, Vec3 pointB) { if (pointA == null || pointB == null) { throw new IllegalArgumentException( Logger.logMessage(Logger.ERROR, "Plane", "clip", "missingPoint")); } if (pointA.equals(pointB)) { return null; } Line line = new Line().setToSegment(pointA, pointB); double lDotV = this.normal.dot(line.direction); if (lDotV == 0) { double lDotS = this.dot(line.origin); if (lDotS == 0) { return new Vec3[]{pointA, pointB}; } else { return null; } } double t = -this.dot(line.origin) / lDotV; if (t < 0 || t > 1) { return null; } Vec3 p = line.pointAt(t, new Vec3()); if (lDotV > 0) { return new Vec3[]{p, pointB}; } else { return new Vec3[]{pointA, p}; } } Plane();  Plane(double x, double y, double z, double distance);  Plane(Plane plane); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); double distanceToPoint(Vec3 point); Plane set(double x, double y, double z, double distance); Plane set(Plane plane); Plane transformByMatrix(Matrix4 matrix); double dot(Vec3 vector); boolean intersectsSegment(Vec3 endPoint1, Vec3 endPoint2); int onSameSide(Vec3 pointA, Vec3 pointB); Vec3[] clip(Vec3 pointA, Vec3 pointB);  }
@Test public void testClip() throws Exception { Plane p = new Plane(0, 0, -1, 0); Vec3[] result; Vec3 a = new Vec3(1, 2, 0); Vec3 b = new Vec3(3, 4, 0); result = p.clip(a, b); assertNotNull("Segment coincident with plane", result); assertEquals("Coincident segment, start point unchanged", result[0], a); assertEquals("Coincident segment, end point unchanged", result[1], b); }
@Test public void testClip_NonIntersecting() throws Exception { Plane p = new Plane(0, 0, -1, 0); Vec3[] result; Vec3 a = new Vec3(1, 2, -1); Vec3 b = new Vec3(3, 4, -1); result = p.clip(a, b); assertNull("Non-intersecting points", result); }
@Test public void testClip_PositiveDirection() throws Exception { Plane p = new Plane(0, 0, -1, 0); Vec3[] result; Vec3 a = new Vec3(1, 2, 1); Vec3 b = new Vec3(3, 4, -1); Vec3 expected0 = new Vec3(2, 3, 0); Vec3 expected1 = new Vec3(b); result = p.clip(a, b); assertNotNull("Positive direction with respect normal, intersecting the plane", result); assertEquals("Positive direction, the start point is the segment's original begin point", expected0, result[0]); assertEquals("Positive direction, the end point is the segment's intersection with the plane", expected1, result[1]); }
@Test public void testClip_NegativeDirection() throws Exception { Plane p = new Plane(0, 0, -1, 0); Vec3[] result; Vec3 a = new Vec3(1, 2, -1); Vec3 b = new Vec3(3, 4, 1); Vec3 expected0 = new Vec3(a); Vec3 expected1 = new Vec3(2, 3, 0); result = p.clip(a, b); assertNotNull("Negative direction with respect normal, intersecting the plane", result); assertEquals("Negative direction, the start point is the segment's original begin point", expected0, result[0]); assertEquals("Negative direction, the end point is the segment's intersection with the plane", expected1, result[1]); }