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 73b19620e5b701845b75f71ce3cfbe0af82bc318 Author: Sven Rathgeber <[email protected]> AuthorDate: Fri Mar 15 15:10:48 2019 +0100 GEOMETRY-29 first version. --- .../threed/enclosing/SphereGenerator.java | 2 +- .../commons/geometry/euclidean/threed/Plane.java | 495 ++++++++++++--------- .../geometry/euclidean/threed/PolyhedronsSet.java | 14 +- .../geometry/euclidean/threed/Vector3D.java | 2 +- .../geometry/euclidean/EuclideanTestUtils.java | 2 +- .../geometry/euclidean/threed/PlaneTest.java | 54 +-- .../euclidean/threed/PolyhedronsSetTest.java | 38 +- 7 files changed, 349 insertions(+), 258 deletions(-) diff --git a/commons-geometry-enclosing/src/main/java/org/apache/commons/geometry/euclidean/threed/enclosing/SphereGenerator.java b/commons-geometry-enclosing/src/main/java/org/apache/commons/geometry/euclidean/threed/enclosing/SphereGenerator.java index 471fab8..c7e3d47 100644 --- a/commons-geometry-enclosing/src/main/java/org/apache/commons/geometry/euclidean/threed/enclosing/SphereGenerator.java +++ b/commons-geometry-enclosing/src/main/java/org/apache/commons/geometry/euclidean/threed/enclosing/SphereGenerator.java @@ -58,7 +58,7 @@ public class SphereGenerator implements SupportBallGenerator<Vector3D> { // delegate to 2D disk generator final DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(BASE_EPS * (norm1(vA) + norm1(vB) + norm1(vC))); - final Plane p = new Plane(vA, vB, vC, precision); + final Plane p = Plane.fromThreePoints(vA, vB, vC, precision); final EnclosingBall<Vector2D> disk = new DiskGenerator().ballOnSupport(Arrays.asList(p.toSubSpace(vA), p.toSubSpace(vB), 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 f390de6..8060a8b 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 @@ -16,7 +16,8 @@ */ package org.apache.commons.geometry.euclidean.threed; -import org.apache.commons.geometry.core.exception.IllegalNormException; +import java.util.Objects; + import org.apache.commons.geometry.core.partitioning.Embedding; import org.apache.commons.geometry.core.partitioning.Hyperplane; import org.apache.commons.geometry.core.precision.DoublePrecisionContext; @@ -26,152 +27,173 @@ import org.apache.commons.geometry.euclidean.threed.rotation.QuaternionRotation; import org.apache.commons.geometry.euclidean.twod.PolygonsSet; import org.apache.commons.geometry.euclidean.twod.Vector2D; -/** The class represent planes in a three dimensional space. +/** + * The class represent planes in a three dimensional space. */ -public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D> { +public final class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D> { /** Offset of the origin with respect to the plane. */ - private double originOffset; + private final double originOffset; /** Origin of the plane frame. */ - private Vector3D origin; + private final Vector3D origin; /** First vector of the plane frame (in plane). */ - private Vector3D u; + private final Vector3D u; /** Second vector of the plane frame (in plane). */ - private Vector3D v; + private final Vector3D v; /** Third vector of the plane frame (plane normal). */ - private Vector3D w; + private final Vector3D w; /** Precision context used to compare floating point numbers. */ private final DoublePrecisionContext precision; - /** Build a plane normal to a given direction and containing the origin. - * @param normal normal direction to the plane - * @param precision precision context used to compare floating point values - * @exception IllegalArgumentException if the normal norm is too small - */ - public Plane(final Vector3D normal, final DoublePrecisionContext precision) - throws IllegalArgumentException { - setNormal(normal); - this.precision = precision; - originOffset = 0; - setFrame(); + public static Plane fromTwoVectors (final Vector3D u, final Vector3D v, final DoublePrecisionContext precision) + { + Vector3D w = u.cross(v); + double originOffset = 0; + Vector3D origin = calculateOrigin(w, originOffset); + return new Plane(u, v, w, origin, originOffset, precision); + } + + public static Plane fromNormal(final Vector3D normal, final DoublePrecisionContext precision) + throws IllegalArgumentException { + Vector3D w = calculateW(normal); + double originOffset = 0; + Vector3D origin = calculateOrigin(w, originOffset); + Vector3D u = w.orthogonal(); + Vector3D v = w.cross(u); + return new Plane(u, v, w, origin, originOffset, precision); } - /** Build a plane from a point and a normal. - * @param p point belonging to the plane - * @param normal normal direction to the plane + private static Vector3D calculateOrigin(Vector3D w, double originOffset) { + return w.multiply(-originOffset); + } + + private static Vector3D calculateW(final Vector3D normal) { + final double norm = Vectors.checkedNorm(normal); + return normal.multiply(1.0 / norm); + } + + /** + * Build a plane from a point and a normal. + * + * @param p point belonging to the plane + * @param normal normal direction to the plane * @param precision precision context used to compare floating point values * @exception IllegalArgumentException if the normal norm is too small */ - public Plane(final Vector3D p, final Vector3D normal, final DoublePrecisionContext precision) - throws IllegalArgumentException { - setNormal(normal); - this.precision = precision; - this.originOffset = -p.dot(w); - setFrame(); + public static Plane fromPointAndNormal(final Vector3D p, final Vector3D normal, final DoublePrecisionContext precision) + throws IllegalArgumentException { + Vector3D w = calculateW(normal); + double originOffset = -p.dot(w); + Vector3D origin = calculateOrigin(w, originOffset); + Vector3D u = w.orthogonal(); + Vector3D v = w.cross(u); + return new Plane(u, v, w, origin, originOffset, precision); } - /** Build a plane from three points. - * <p>The plane is oriented in the direction of - * {@code (p2-p1) ^ (p3-p1)}</p> - * @param p1 first point belonging to the plane - * @param p2 second point belonging to the plane - * @param p3 third point belonging to the plane + // TODO check this one + public static Plane of(Vector3D u, Vector3D v, Vector3D w, Vector3D origin, double originOffset, + DoublePrecisionContext precision) { + if (!w.orthogonal().equals(u, precision)) + { + throw new IllegalArgumentException("w must be orthogonal to u."); + } + return new Plane(u, v, w, origin, originOffset, precision); + } + + /** + * Build a plane from three points. + * <p> + * The plane is oriented in the direction of {@code (p2-p1) ^ (p3-p1)} + * </p> + * + * @param p1 first point belonging to the plane + * @param p2 second point belonging to the plane + * @param p3 third point belonging to the plane * @param precision precision context used to compare floating point values * @exception IllegalArgumentException if the points do not constitute a plane */ - public Plane(final Vector3D p1, final Vector3D p2, final Vector3D p3, final DoublePrecisionContext precision) - throws IllegalArgumentException { - this(p1, p2.subtract(p1).cross(p3.subtract(p1)), precision); + public static Plane fromThreePoints(final Vector3D p1, final Vector3D p2, final Vector3D p3, + final DoublePrecisionContext precision) throws IllegalArgumentException { + return Plane.fromPointAndNormal(p1, p2.subtract(p1).cross(p3.subtract(p1)), precision); } - /** Copy constructor. - * <p>The instance created is completely independent of the original - * one. A deep copy is used, none of the underlying object are - * shared.</p> + /** + * Copy constructor. + * <p> + * The instance created is completely independent of the original one. A deep + * copy is used, none of the underlying object are shared. + * </p> + * * @param plane plane to copy */ - public Plane(final Plane plane) { - originOffset = plane.originOffset; - origin = plane.origin; - u = plane.u; - v = plane.v; - w = plane.w; - precision = plane.precision; - } - - /** Copy the instance. - * <p>The instance created is completely independant of the original - * one. A deep copy is used, none of the underlying objects are - * shared (except for immutable objects).</p> - * @return a new hyperplane, copy of the instance - */ - @Override - public Plane copySelf() { - return new Plane(this); - } - - /** Reset the instance as if built from a point and a normal. - * @param p point belonging to the plane - * @param normal normal direction to the plane - * @exception IllegalArgumentException if the normal norm is too small - */ - public void reset(final Vector3D p, final Vector3D normal) { - setNormal(normal); - originOffset = -p.dot(w); - setFrame(); + public static Plane of(final Plane plane) { + return new Plane(plane.u, plane.v, plane.w, plane.origin, plane.originOffset, plane.getPrecision()); } - /** Reset the instance from another one. - * <p>The updated instance is completely independant of the original - * one. A deep reset is used none of the underlying object is - * shared.</p> - * @param original plane to reset from + /** + * Constructor, made private to prevent inheritance. + * + * Creates a new instance with the given values. + * @param u u axis + * @param v v axis + * @param w w axis + * @param origin origin + * @param originOffset offset of the origin + * @param precision the precision context */ - public void reset(final Plane original) { - originOffset = original.originOffset; - origin = original.origin; - u = original.u; - v = original.v; - w = original.w; + private Plane(Vector3D u, Vector3D v, Vector3D w, Vector3D origin, double originOffset, + DoublePrecisionContext precision) { + this.u = u; + this.v = v; + this.w = w; + this.origin = origin; + this.originOffset = originOffset; + this.precision = precision; } - /** Set the normal vactor. - * @param normal normal direction to the plane (will be copied) - * @exception IllegalNormException if the normal norm zero, NaN, or infinite + /** + * Copy the instance. + * <p> + * The instance created is completely independent of the original one. A deep + * copy is used, none of the underlying objects are shared (except for immutable + * objects). + * </p> + * + * @return a new hyperplane, copy of the instance */ - private void setNormal(final Vector3D normal) { - final double norm = Vectors.checkedNorm(normal); - - w = Vector3D.linearCombination(1.0 / norm, normal); + @Deprecated + @Override + public Plane copySelf() { + return new Plane(this.u, this.v, this.w, this.origin, this.originOffset, this.precision); } - /** Reset the plane frame. - */ - private void setFrame() { - origin = Vector3D.linearCombination(-originOffset, w); - u = w.orthogonal(); - v = w.cross(u); - } - /** Get the origin point of the plane frame. - * <p>The point returned is the orthogonal projection of the - * 3D-space origin in the plane.</p> - * @return the origin point of the plane frame (point closest to the - * 3D-space origin) + /** + * Get the origin point of the plane frame. + * <p> + * The point returned is the orthogonal projection of the 3D-space origin in the + * plane. + * </p> + * + * @return the origin point of the plane frame (point closest to the 3D-space + * origin) */ public Vector3D getOrigin() { return origin; } - /** Get the normalized normal vector. - * <p>The frame defined by ({@link #getU getU}, {@link #getV getV}, - * {@link #getNormal getNormal}) is a rigth-handed orthonormalized - * frame).</p> + /** + * Get the normalized normal vector. + * <p> + * The frame defined by ({@link #getU getU}, {@link #getV getV}, + * {@link #getNormal getNormal}) is a rigth-handed orthonormalized frame). + * </p> + * * @return normalized normal vector * @see #getU * @see #getV @@ -180,10 +202,13 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return w; } - /** Get the plane first canonical vector. - * <p>The frame defined by ({@link #getU getU}, {@link #getV getV}, - * {@link #getNormal getNormal}) is a rigth-handed orthonormalized - * frame).</p> + /** + * Get the plane first canonical vector. + * <p> + * The frame defined by ({@link #getU getU}, {@link #getV getV}, + * {@link #getNormal getNormal}) is a rigth-handed orthonormalized frame). + * </p> + * * @return normalized first canonical vector * @see #getV * @see #getNormal @@ -192,10 +217,13 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return u; } - /** Get the plane second canonical vector. - * <p>The frame defined by ({@link #getU getU}, {@link #getV getV}, - * {@link #getNormal getNormal}) is a rigth-handed orthonormalized - * frame).</p> + /** + * Get the plane second canonical vector. + * <p> + * The frame defined by ({@link #getU getU}, {@link #getV getV}, + * {@link #getNormal getNormal}) is a rigth-handed orthonormalized frame). + * </p> + * * @return normalized second canonical vector * @see #getU * @see #getNormal @@ -216,26 +244,31 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return precision; } - /** Revert the plane. - * <p>Replace the instance by a similar plane with opposite orientation.</p> - * <p>The new plane frame is chosen in such a way that a 3D point that had - * {@code (x, y)} in-plane coordinates and {@code z} offset with - * respect to the plane and is unaffected by the change will have - * {@code (y, x)} in-plane coordinates and {@code -z} offset with - * respect to the new plane. This means that the {@code u} and {@code v} - * vectors returned by the {@link #getU} and {@link #getV} methods are exchanged, - * and the {@code w} vector returned by the {@link #getNormal} method is - * reversed.</p> + /** + * Creates a new reverted version of this plane, with opposite orientation. + * <p> + * The new plane frame is chosen in such a way that a 3D point that had + * {@code (x, y)} in-plane coordinates and {@code z} offset with respect to the + * plane and is unaffected by the change will have {@code (y, x)} in-plane + * coordinates and {@code -z} offset with respect to the new plane. This means + * that the {@code u} and {@code v} vectors returned by the {@link #getU} and + * {@link #getV} methods are exchanged, and the {@code w} vector returned by the + * {@link #getNormal} method is reversed. + * </p> */ - public void revertSelf() { + public Plane revert() { final Vector3D tmp = u; - u = v; - v = tmp; - w = w.negate(); - originOffset = -originOffset; + Vector3D u = v; + Vector3D v = tmp; + Vector3D w = this.w.negate(); + double originOffset = -this.originOffset; + Vector3D origin = calculateOrigin(w, originOffset); + return new Plane(u, v, w, origin, originOffset, this.precision); } - /** Transform a 3D space point into an in-plane point. + /** + * Transform a 3D space point into an in-plane point. + * * @param point point of the space (must be a {@link Vector3D} instance) * @return in-plane point * @see #toSpace @@ -246,7 +279,9 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return Vector2D.of(vec.dot(u), vec.dot(v)); } - /** Transform an in-plane point into a 3D space point. + /** + * Transform an in-plane point into a 3D space point. + * * @param point in-plane point (must be a {@link Vector2D} instance) * @return 3D space point * @see #toSubSpace @@ -256,99 +291,112 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return Vector3D.linearCombination(point.getX(), u, point.getY(), v, -originOffset, w); } - /** Get one point from the 3D-space. - * @param inPlane desired in-plane coordinates for the point in the - * plane - * @param offset desired offset for the point - * @return one point in the 3D-space, with given coordinates and offset - * relative to the plane + /** + * Get one point from the 3D-space. + * + * @param inPlane desired in-plane coordinates for the point in the plane + * @param offset desired offset for the point + * @return one point in the 3D-space, with given coordinates and offset relative + * to the plane */ public Vector3D getPointAt(final Vector2D inPlane, final double offset) { return Vector3D.linearCombination(inPlane.getX(), u, inPlane.getY(), v, offset - originOffset, w); } - /** Check if the instance is similar to another plane. - * <p>Planes are considered similar if they contain the same - * points. This does not mean they are equal since they can have - * opposite normals.</p> + /** + * Check if the instance is similar to another plane. + * <p> + * Planes are considered similar if they contain the same points. This does not + * mean they are equal since they can have opposite normals. + * </p> + * * @param plane plane to which the instance is compared * @return true if the planes are similar */ public boolean isSimilarTo(final Plane plane) { final double angle = w.angle(plane.w); - return ((precision.eqZero(angle)) && precision.eq(originOffset, plane.originOffset)) || - ((precision.eq(angle, Math.PI)) && precision.eq(originOffset, -plane.originOffset)); + return ((precision.eqZero(angle)) && precision.eq(originOffset, plane.originOffset)) + || ((precision.eq(angle, Math.PI)) && precision.eq(originOffset, -plane.originOffset)); } - /** Rotate the plane around the specified point. - * <p>The instance is not modified, a new instance is created.</p> - * @param center rotation center + /** + * Rotate the plane around the specified point. + * <p> + * The instance is not modified, a new instance is created. + * </p> + * + * @param center rotation center * @param rotation 3-dimensional rotation * @return a new plane */ public Plane rotate(final Vector3D center, final QuaternionRotation rotation) { - final Vector3D delta = origin.subtract(center); - final Plane plane = new Plane(center.add(rotation.apply(delta)), - rotation.apply(w), precision); - - // make sure the frame is transformed as desired - plane.u = rotation.apply(u); - plane.v = rotation.apply(v); - - return plane; - + Vector3D p = center.add(rotation.apply(delta)); + Vector3D normal = rotation.apply(this.w); + Vector3D w = calculateW(normal); + double originOffset = -p.dot(w); + Vector3D origin = calculateOrigin(w, originOffset); + Vector3D u = rotation.apply(this.u); + Vector3D v = rotation.apply(this.v); + return new Plane(u, v, w, origin, originOffset, this.precision); } - /** Translate the plane by the specified amount. - * <p>The instance is not modified, a new instance is created.</p> + /** + * Translate the plane by the specified amount. + * <p> + * The instance is not modified, a new instance is created. + * </p> + * * @param translation translation to apply * @return a new plane */ public Plane translate(final Vector3D translation) { - - final Plane plane = new Plane(origin.add(translation), w, precision); - - // make sure the frame is transformed as desired - plane.u = u; - plane.v = v; - - return plane; - + Vector3D p = origin.add(translation); + Vector3D normal = this.w; + Vector3D w = calculateW(normal); + double originOffset = -p.dot(w); + Vector3D origin = calculateOrigin(w, originOffset); + return new Plane(this.u, this.v, w, origin, originOffset, this.precision); } - /** Get the intersection of a line with the instance. + /** + * Get the intersection of a line with the instance. + * * @param line line intersecting the instance - * @return intersection point between between the line and the - * instance (null if the line is parallel to the instance) + * @return intersection point between between the line and the instance (null if + * the line is parallel to the instance) */ public Vector3D intersection(final Line line) { final Vector3D direction = line.getDirection(); - final double dot = w.dot(direction); + final double dot = w.dot(direction); if (precision.eqZero(dot)) { return null; } final Vector3D point = line.toSpace(Vector1D.ZERO); - final double k = -(originOffset + w.dot(point)) / dot; + final double k = -(originOffset + w.dot(point)) / dot; return Vector3D.linearCombination(1.0, point, k, direction); } - /** Build the line shared by the instance and another plane. + /** + * Build the line shared by the instance and another plane. + * * @param other other plane - * @return line at the intersection of the instance and the - * other plane (really a {@link Line Line} instance) + * @return line at the intersection of the instance and the other plane (really + * a {@link Line Line} instance) */ public Line intersection(final Plane other) { final Vector3D direction = w.cross(other.w); if (precision.eqZero(direction.norm())) { return null; } - final Vector3D point = intersection(this, other, new Plane(direction, precision)); + final Vector3D point = intersection(this, other, Plane.fromNormal(direction, precision)); return new Line(point, point.add(direction), precision); } - /** Get the intersection point of three planes. + /** + * Get the intersection point of three planes. + * * @param plane1 first plane1 * @param plane2 second plane2 * @param plane3 third plane2 @@ -374,23 +422,24 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D // direct Cramer resolution of the linear system // (this is still feasible for a 3x3 system) - final double a23 = b2 * c3 - b3 * c2; - final double b23 = c2 * a3 - c3 * a2; - final double c23 = a2 * b3 - a3 * b2; + final double a23 = b2 * c3 - b3 * c2; + final double b23 = c2 * a3 - c3 * a2; + final double c23 = a2 * b3 - a3 * b2; final double determinant = a1 * a23 + b1 * b23 + c1 * c23; if (Math.abs(determinant) < 1.0e-10) { return null; } final double r = 1.0 / determinant; - return Vector3D.of( - (-a23 * d1 - (c1 * b3 - c3 * b1) * d2 - (c2 * b1 - c1 * b2) * d3) * r, - (-b23 * d1 - (c3 * a1 - c1 * a3) * d2 - (c1 * a2 - c2 * a1) * d3) * r, - (-c23 * d1 - (b1 * a3 - b3 * a1) * d2 - (b2 * a1 - b1 * a2) * d3) * r); + return Vector3D.of((-a23 * d1 - (c1 * b3 - c3 * b1) * d2 - (c2 * b1 - c1 * b2) * d3) * r, + (-b23 * d1 - (c3 * a1 - c1 * a3) * d2 - (c1 * a2 - c2 * a1) * d3) * r, + (-c23 * d1 - (b1 * a3 - b3 * a1) * d2 - (b2 * a1 - b1 * a2) * d3) * r); } - /** Build a region covering the whole hyperplane. + /** + * Build a region covering the whole hyperplane. + * * @return a region covering the whole hyperplane */ @Override @@ -398,16 +447,20 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return new SubPlane(this, new PolygonsSet(precision)); } - /** Build a region covering the whole space. - * @return a region containing the instance (really a {@link - * PolyhedronsSet PolyhedronsSet} instance) + /** + * Build a region covering the whole space. + * + * @return a region containing the instance (really a {@link PolyhedronsSet + * PolyhedronsSet} instance) */ @Override public PolyhedronsSet wholeSpace() { return new PolyhedronsSet(precision); } - /** Check if the instance contains a point. + /** + * Check if the instance contains a point. + * * @param p point to check * @return true if p belongs to the plane */ @@ -415,13 +468,18 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return precision.eqZero(getOffset(p)); } - /** Get the offset (oriented distance) of a parallel plane. - * <p>This method should be called only for parallel planes otherwise - * the result is not meaningful.</p> - * <p>The offset is 0 if both planes are the same, it is - * positive if the plane is on the plus side of the instance and - * negative if it is on the minus side, according to its natural - * orientation.</p> + /** + * Get the offset (oriented distance) of a parallel plane. + * <p> + * This method should be called only for parallel planes otherwise the result is + * not meaningful. + * </p> + * <p> + * The offset is 0 if both planes are the same, it is positive if the plane is + * on the plus side of the instance and negative if it is on the minus side, + * according to its natural orientation. + * </p> + * * @param plane plane to check * @return offset of the plane */ @@ -429,11 +487,15 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return originOffset + (sameOrientationAs(plane) ? -plane.originOffset : plane.originOffset); } - /** Get the offset (oriented distance) of a point. - * <p>The offset is 0 if the point is on the underlying hyperplane, - * it is positive if the point is on one particular side of the - * hyperplane, and it is negative if the point is on the other side, - * according to the hyperplane natural orientation.</p> + /** + * Get the offset (oriented distance) of a point. + * <p> + * The offset is 0 if the point is on the underlying hyperplane, it is positive + * if the point is on one particular side of the hyperplane, and it is negative + * if the point is on the other side, according to the hyperplane natural + * orientation. + * </p> + * * @param point point to check * @return offset of the point */ @@ -442,14 +504,43 @@ public class Plane implements Hyperplane<Vector3D>, Embedding<Vector3D, Vector2D return point.dot(w) + originOffset; } - /** Check if the instance has the same orientation as another hyperplane. + /** + * Check if the instance has the same orientation as another hyperplane. + * * @param other other hyperplane to check against the instance - * @return true if the instance and the other hyperplane have - * the same orientation + * @return true if the instance and the other hyperplane have the same + * orientation */ @Override public boolean sameOrientationAs(final Hyperplane<Vector3D> other) { return (((Plane) other).w).dot(w) > 0.0; } + /** {@inheritDoc} */ + @Override + public String toString() { + return "Plane [originOffset=" + originOffset + ", origin=" + origin + ", u=" + u + ", v=" + v + ", w=" + w + + ", precision=" + precision + "]"; + } + + /** {@inheritDoc} */ + @Override + public int hashCode() { + return Objects.hash(origin, originOffset, u, v, w); + } + + /** {@inheritDoc} */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Plane other = (Plane) obj; + return Objects.equals(origin, other.origin) + && Double.doubleToLongBits(originOffset) == Double.doubleToLongBits(other.originOffset) + && Objects.equals(u, other.u) && Objects.equals(v, other.v) && Objects.equals(w, other.w); + } } diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/PolyhedronsSet.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/PolyhedronsSet.java index d522f7c..66e35b1 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/PolyhedronsSet.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/PolyhedronsSet.java @@ -155,12 +155,12 @@ public class PolyhedronsSet extends AbstractRegion<Vector3D, Vector2D> { // too thin box, build an empty polygons set return new BSPTree<>(Boolean.FALSE); } - final Plane pxMin = new Plane(Vector3D.of(xMin, 0, 0), Vector3D.MINUS_X, precision); - final Plane pxMax = new Plane(Vector3D.of(xMax, 0, 0), Vector3D.PLUS_X, precision); - final Plane pyMin = new Plane(Vector3D.of(0, yMin, 0), Vector3D.MINUS_Y, precision); - final Plane pyMax = new Plane(Vector3D.of(0, yMax, 0), Vector3D.PLUS_Y, precision); - final Plane pzMin = new Plane(Vector3D.of(0, 0, zMin), Vector3D.MINUS_Z, precision); - final Plane pzMax = new Plane(Vector3D.of(0, 0, zMax), Vector3D.PLUS_Z, precision); + final Plane pxMin = Plane.fromPointAndNormal(Vector3D.of(xMin, 0, 0), Vector3D.MINUS_X, precision); + final Plane pxMax = Plane.fromPointAndNormal(Vector3D.of(xMax, 0, 0), Vector3D.PLUS_X, precision); + final Plane pyMin = Plane.fromPointAndNormal(Vector3D.of(0, yMin, 0), Vector3D.MINUS_Y, precision); + final Plane pyMax = Plane.fromPointAndNormal(Vector3D.of(0, yMax, 0), Vector3D.PLUS_Y, precision); + final Plane pzMin = Plane.fromPointAndNormal(Vector3D.of(0, 0, zMin), Vector3D.MINUS_Z, precision); + final Plane pzMax = Plane.fromPointAndNormal(Vector3D.of(0, 0, zMax), Vector3D.PLUS_Z, precision); final Region<Vector3D> boundary = new RegionFactory<Vector3D>().buildConvex(pxMin, pxMax, pyMin, pyMax, pzMin, pzMax); return boundary.getTree(false); @@ -218,7 +218,7 @@ public class PolyhedronsSet extends AbstractRegion<Vector3D, Vector2D> { for (final int[] facet : facets) { // define facet plane from the first 3 points - Plane plane = new Plane(vertices.get(facet[0]), vertices.get(facet[1]), vertices.get(facet[2]), + Plane plane = Plane.fromThreePoints(vertices.get(facet[0]), vertices.get(facet[1]), vertices.get(facet[2]), precision); // check all points are in the plane diff --git a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java index 232675c..b503b7c 100644 --- a/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java +++ b/commons-geometry-euclidean/src/main/java/org/apache/commons/geometry/euclidean/threed/Vector3D.java @@ -528,7 +528,7 @@ public class Vector3D extends MultiDimensionalEuclideanVector<Vector3D> { * @return vector with coordinates calculated by {@code a * c} */ public static Vector3D linearCombination(double a, Vector3D c) { - return new Vector3D(a * c.x, a * c.y, a * c.z); + return c.multiply(a); } /** Returns a vector consisting of the linear combination of the inputs. diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/EuclideanTestUtils.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/EuclideanTestUtils.java index 7d6bf18..5b7b0db 100644 --- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/EuclideanTestUtils.java +++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/EuclideanTestUtils.java @@ -340,7 +340,7 @@ public class EuclideanTestUtils { @Override public Plane parseHyperplane() throws ParseException { - return new Plane(Vector3D.of(getNumber(), getNumber(), getNumber()), + return Plane.fromPointAndNormal(Vector3D.of(getNumber(), getNumber(), getNumber()), Vector3D.of(getNumber(), getNumber(), getNumber()), getPrecision()); } 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 f76471a..8480646 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,29 +31,29 @@ public class PlaneTest { @Test public void testContains() { - Plane p = new Plane(Vector3D.of(0, 0, 1), Vector3D.of(0, 0, 1), TEST_PRECISION); + 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))); } @Test - public void testOffset() { - Vector3D p1 = Vector3D.of(1, 1, 1); - Plane p = new Plane(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); - Assert.assertEquals(0.3, - p.getOffset(Vector3D.linearCombination(1.0, p1, 0.3, p.getNormal())), - TEST_EPS); - Assert.assertEquals(-0.3, - p.getOffset(Vector3D.linearCombination(1.0, p1, -0.3, p.getNormal())), - TEST_EPS); - } + public void testFromNormalfset() { + 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); + Assert.assertEquals(0.3, + p.getOffset(Vector3D.linearCombination(1.0, p1, 0.3, p.getNormal())), + TEST_EPS); + Assert.assertEquals(-0.3, + p.getOffset(Vector3D.linearCombination(1.0, p1, -0.3, p.getNormal())), + TEST_EPS); + } @Test public void testPoint() { - Plane p = new Plane(Vector3D.of(2, -3, 1), Vector3D.of(1, 4, 9), TEST_PRECISION); + Plane p = Plane.fromPointAndNormal(Vector3D.of(2, -3, 1), Vector3D.of(1, 4, 9), TEST_PRECISION); Assert.assertTrue(p.contains(p.getOrigin())); } @@ -62,7 +62,7 @@ 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 = new Plane(p1, p2, p3, TEST_PRECISION); + Plane p = Plane.fromThreePoints(p1, p2, p3, TEST_PRECISION); Assert.assertTrue(p.contains(p1)); Assert.assertTrue(p.contains(p2)); Assert.assertTrue(p.contains(p3)); @@ -73,7 +73,7 @@ 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 = new Plane(p1, p2, p3, TEST_PRECISION); + Plane p = Plane.fromThreePoints(p1, p2, p3, TEST_PRECISION); Vector3D oldNormal = p.getNormal(); p = p.rotate(p2, QuaternionRotation.fromAxisAngle(p2.subtract(p1), 1.7)); @@ -98,7 +98,7 @@ 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 = new Plane(p1, p2, p3, TEST_PRECISION); + Plane p = Plane.fromThreePoints(p1, p2, p3, TEST_PRECISION); p = p.translate(Vector3D.linearCombination(2.0, p.getU(), -1.5, p.getV())); Assert.assertTrue(p.contains(p1)); @@ -119,7 +119,7 @@ public class PlaneTest { @Test public void testIntersection() { - Plane p = new Plane(Vector3D.of(1, 2, 3), Vector3D.of(-4, 1, -5), TEST_PRECISION); + 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)); @@ -133,8 +133,8 @@ 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 = new Plane(p1, p2, Vector3D.of(-2.0, 4.3, 0.7), TEST_PRECISION); - Plane pB = new Plane(p1, Vector3D.of(11.4, -3.8, 5.1), p2, TEST_PRECISION); + Plane pA = Plane.fromThreePoints(p1, p2, Vector3D.of(-2.0, 4.3, 0.7), TEST_PRECISION); + Plane pB = Plane.fromThreePoints(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)); @@ -144,9 +144,9 @@ public class PlaneTest { @Test public void testIntersection3() { Vector3D reference = Vector3D.of(1.2, 3.4, -5.8); - Plane p1 = new Plane(reference, Vector3D.of(1, 3, 3), TEST_PRECISION); - Plane p2 = new Plane(reference, Vector3D.of(-2, 4, 0), TEST_PRECISION); - Plane p3 = new Plane(reference, Vector3D.of(7, 0, -4), TEST_PRECISION); + 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); @@ -158,13 +158,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 = new Plane(p1, p2, p3, TEST_PRECISION); - Plane pB = new Plane(p1, Vector3D.of(11.4, -3.8, 5.1), p2, TEST_PRECISION); + Plane pA = Plane.fromThreePoints(p1, p2, p3, TEST_PRECISION); + Plane pB = Plane.fromThreePoints(p1, Vector3D.of(11.4, -3.8, 5.1), p2, TEST_PRECISION); Assert.assertTrue(! pA.isSimilarTo(pB)); Assert.assertTrue(pA.isSimilarTo(pA)); - Assert.assertTrue(pA.isSimilarTo(new Plane(p1, p3, p2, TEST_PRECISION))); + Assert.assertTrue(pA.isSimilarTo(Plane.fromThreePoints(p1, p3, p2, TEST_PRECISION))); Vector3D shift = Vector3D.linearCombination(0.3, pA.getNormal()); - Assert.assertTrue(! pA.isSimilarTo(new Plane(p1.add(shift), + Assert.assertTrue(! pA.isSimilarTo(Plane.fromThreePoints(p1.add(shift), p3.add(shift), p2.add(shift), TEST_PRECISION))); diff --git a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PolyhedronsSetTest.java b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PolyhedronsSetTest.java index a635e46..52c8ff9 100644 --- a/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PolyhedronsSetTest.java +++ b/commons-geometry-euclidean/src/test/java/org/apache/commons/geometry/euclidean/threed/PolyhedronsSetTest.java @@ -96,7 +96,7 @@ public class PolyhedronsSetTest { public void testHalfSpace() { // arrange List<SubHyperplane<Vector3D>> boundaries = new ArrayList<>(); - boundaries.add(new SubPlane(new Plane(Vector3D.ZERO, Vector3D.PLUS_Y, TEST_PRECISION), + boundaries.add(new SubPlane(Plane.fromPointAndNormal(Vector3D.ZERO, Vector3D.PLUS_Y, TEST_PRECISION), new PolygonsSet(TEST_PRECISION))); // act @@ -465,10 +465,10 @@ public class PolyhedronsSetTest { // act PolyhedronsSet tree = (PolyhedronsSet) new RegionFactory<Vector3D>().buildConvex( - new Plane(vertex3, vertex2, vertex1, TEST_PRECISION), - new Plane(vertex2, vertex3, vertex4, TEST_PRECISION), - new Plane(vertex4, vertex3, vertex1, TEST_PRECISION), - new Plane(vertex1, vertex2, vertex4, TEST_PRECISION)); + Plane.fromThreePoints(vertex3, vertex2, vertex1, TEST_PRECISION), + Plane.fromThreePoints(vertex2, vertex3, vertex4, TEST_PRECISION), + Plane.fromThreePoints(vertex4, vertex3, vertex1, TEST_PRECISION), + Plane.fromThreePoints(vertex1, vertex2, vertex4, TEST_PRECISION)); // assert Assert.assertEquals(1.0 / 3.0, tree.getSize(), TEST_EPS); @@ -539,10 +539,10 @@ public class PolyhedronsSetTest { // act PolyhedronsSet tree = (PolyhedronsSet) new RegionFactory<Vector3D>().buildConvex( - new Plane(vertex3, vertex2, vertex1, TEST_PRECISION), - new Plane(vertex2, vertex3, vertex4, TEST_PRECISION), - new Plane(vertex4, vertex3, vertex1, TEST_PRECISION), - new Plane(vertex1, vertex2, vertex4, TEST_PRECISION)); + Plane.fromThreePoints(vertex3, vertex2, vertex1, TEST_PRECISION), + Plane.fromThreePoints(vertex2, vertex3, vertex4, TEST_PRECISION), + Plane.fromThreePoints(vertex4, vertex3, vertex1, TEST_PRECISION), + Plane.fromThreePoints(vertex1, vertex2, vertex4, TEST_PRECISION)); // assert Vector3D barycenter = tree.getBarycenter(); @@ -691,7 +691,7 @@ public class PolyhedronsSetTest { Vector3D v_2 = Vector3D.of(coords[idxB], coords[idxB + 1], coords[idxB + 2]); Vector3D v_3 = Vector3D.of(coords[idxC], coords[idxC + 1], coords[idxC + 2]); Vector3D[] vertices = {v_1, v_2, v_3}; - Plane polyPlane = new Plane(v_1, v_2, v_3, TEST_PRECISION); + Plane polyPlane = Plane.fromThreePoints(v_1, v_2, v_3, TEST_PRECISION); ArrayList<SubHyperplane<Vector2D>> lines = new ArrayList<>(); Vector2D[] projPts = new Vector2D[vertices.length]; @@ -1476,12 +1476,12 @@ public class PolyhedronsSetTest { double offset = size * 0.5; DoublePrecisionContext precision = new EpsilonDoublePrecisionContext(eps); - Plane xMinus = new Plane(center.add(Vector3D.of(-offset, 0, 0)), Vector3D.MINUS_X, precision); - Plane xPlus = new Plane(center.add(Vector3D.of(offset, 0, 0)), Vector3D.PLUS_X, precision); - Plane yPlus = new Plane(center.add(Vector3D.of(0, offset, 0)), Vector3D.PLUS_Y, precision); - Plane yMinus = new Plane(center.add(Vector3D.of(0, -offset, 0)), Vector3D.MINUS_Y, precision); - Plane zPlus = new Plane(center.add(Vector3D.of(0, 0, offset)), Vector3D.PLUS_Z, precision); - Plane zMinus = new Plane(center.add(Vector3D.of(0, 0, -offset)), Vector3D.MINUS_Z, precision); + Plane xMinus = Plane.fromPointAndNormal(center.add(Vector3D.of(-offset, 0, 0)), Vector3D.MINUS_X, precision); + Plane xPlus = Plane.fromPointAndNormal(center.add(Vector3D.of(offset, 0, 0)), Vector3D.PLUS_X, precision); + Plane yPlus = Plane.fromPointAndNormal(center.add(Vector3D.of(0, offset, 0)), Vector3D.PLUS_Y, precision); + Plane yMinus = Plane.fromPointAndNormal(center.add(Vector3D.of(0, -offset, 0)), Vector3D.MINUS_Y, precision); + Plane zPlus = Plane.fromPointAndNormal(center.add(Vector3D.of(0, 0, offset)), Vector3D.PLUS_Z, precision); + Plane zMinus = Plane.fromPointAndNormal(center.add(Vector3D.of(0, 0, -offset)), Vector3D.MINUS_Z, precision); // +x boundaries.add(createSubPlane(xPlus, @@ -1546,8 +1546,8 @@ public class PolyhedronsSetTest { Vector3D topZ = Vector3D.of(center.getX(), center.getY(), center.getZ() + radius); Vector3D bottomZ = Vector3D.of(center.getX(), center.getY(), center.getZ() - radius); - planes.add(new Plane(topZ, Vector3D.PLUS_Z, TEST_PRECISION)); - planes.add(new Plane(bottomZ, Vector3D.MINUS_Z, TEST_PRECISION)); + planes.add(Plane.fromPointAndNormal(topZ, Vector3D.PLUS_Z, TEST_PRECISION)); + planes.add(Plane.fromPointAndNormal(bottomZ, Vector3D.MINUS_Z, TEST_PRECISION)); // add the side planes double vDelta = Math.PI / stacks; @@ -1580,7 +1580,7 @@ public class PolyhedronsSetTest { norm = Vector3D.of(x, y, stackHeight).normalize(); pt = center.add(norm.multiply(adjustedRadius)); - planes.add(new Plane(pt, norm, TEST_PRECISION)); + planes.add(Plane.fromPointAndNormal(pt, norm, TEST_PRECISION)); } }
