Plane.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.geometry.euclidean.threed;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathRuntimeException;
  24. import org.hipparchus.geometry.euclidean.oned.Vector1D;
  25. import org.hipparchus.geometry.euclidean.twod.Euclidean2D;
  26. import org.hipparchus.geometry.euclidean.twod.PolygonsSet;
  27. import org.hipparchus.geometry.euclidean.twod.SubLine;
  28. import org.hipparchus.geometry.euclidean.twod.Vector2D;
  29. import org.hipparchus.geometry.partitioning.Embedding;
  30. import org.hipparchus.geometry.partitioning.Hyperplane;
  31. import org.hipparchus.geometry.partitioning.RegionFactory;
  32. import org.hipparchus.util.FastMath;

  33. /** The class represent planes in a three dimensional space.
  34.  */
  35. public class Plane
  36.     implements Hyperplane<Euclidean3D, Vector3D, Plane, SubPlane>,
  37.                Embedding<Euclidean3D, Vector3D,  Euclidean2D, Vector2D> {

  38.     /** Offset of the origin with respect to the plane. */
  39.     private double originOffset;

  40.     /** Origin of the plane frame. */
  41.     private Vector3D origin;

  42.     /** First vector of the plane frame (in plane). */
  43.     private Vector3D u;

  44.     /** Second vector of the plane frame (in plane). */
  45.     private Vector3D v;

  46.     /** Third vector of the plane frame (plane normal). */
  47.     private Vector3D w;

  48.     /** Tolerance below which points are considered identical. */
  49.     private final double tolerance;

  50.     /** Build a plane normal to a given direction and containing the origin.
  51.      * @param normal normal direction to the plane
  52.      * @param tolerance tolerance below which points are considered identical
  53.      * @exception MathRuntimeException if the normal norm is too small
  54.      */
  55.     public Plane(final Vector3D normal, final double tolerance)
  56.         throws MathRuntimeException {
  57.         setNormal(normal);
  58.         this.tolerance = tolerance;
  59.         originOffset = 0;
  60.         setFrame();
  61.     }

  62.     /** Build a plane from a point and a normal.
  63.      * @param p point belonging to the plane
  64.      * @param normal normal direction to the plane
  65.      * @param tolerance tolerance below which points are considered identical
  66.      * @exception MathRuntimeException if the normal norm is too small
  67.      */
  68.     public Plane(final Vector3D p, final Vector3D normal, final double tolerance)
  69.         throws MathRuntimeException {
  70.         setNormal(normal);
  71.         this.tolerance = tolerance;
  72.         originOffset = -p.dotProduct(w);
  73.         setFrame();
  74.     }

  75.     /** Build a plane from three points.
  76.      * <p>The plane is oriented in the direction of
  77.      * {@code (p2-p1) ^ (p3-p1)}</p>
  78.      * @param p1 first point belonging to the plane
  79.      * @param p2 second point belonging to the plane
  80.      * @param p3 third point belonging to the plane
  81.      * @param tolerance tolerance below which points are considered identical
  82.      * @exception MathRuntimeException if the points do not constitute a plane
  83.      */
  84.     public Plane(final Vector3D p1, final Vector3D p2, final Vector3D p3, final double tolerance)
  85.         throws MathRuntimeException {
  86.         this(p1, p2.subtract(p1).crossProduct(p3.subtract(p1)), tolerance);
  87.     }

  88.     /** Copy constructor.
  89.      * <p>The instance created is completely independent of the original
  90.      * one. A deep copy is used, none of the underlying object are
  91.      * shared.</p>
  92.      * @param plane plane to copy
  93.      */
  94.     public Plane(final Plane plane) {
  95.         originOffset = plane.originOffset;
  96.         origin       = plane.origin;
  97.         u            = plane.u;
  98.         v            = plane.v;
  99.         w            = plane.w;
  100.         tolerance    = plane.tolerance;
  101.     }

  102.     /** Copy the instance.
  103.      * <p>The instance created is completely independant of the original
  104.      * one. A deep copy is used, none of the underlying objects are
  105.      * shared (except for immutable objects).</p>
  106.      * @return a new hyperplane, copy of the instance
  107.      */
  108.     @Override
  109.     public Plane copySelf() {
  110.         return new Plane(this);
  111.     }

  112.     /** Reset the instance as if built from a point and a normal.
  113.      * @param p point belonging to the plane
  114.      * @param normal normal direction to the plane
  115.      * @exception MathRuntimeException if the normal norm is too small
  116.      */
  117.     public void reset(final Vector3D p, final Vector3D normal) throws MathRuntimeException {
  118.         setNormal(normal);
  119.         originOffset = -p.dotProduct(w);
  120.         setFrame();
  121.     }

  122.     /** Reset the instance from another one.
  123.      * <p>The updated instance is completely independant of the original
  124.      * one. A deep reset is used none of the underlying object is
  125.      * shared.</p>
  126.      * @param original plane to reset from
  127.      */
  128.     public void reset(final Plane original) {
  129.         originOffset = original.originOffset;
  130.         origin       = original.origin;
  131.         u            = original.u;
  132.         v            = original.v;
  133.         w            = original.w;
  134.     }

  135.     /** Set the normal vactor.
  136.      * @param normal normal direction to the plane (will be copied)
  137.      * @exception MathRuntimeException if the normal norm is too small
  138.      */
  139.     private void setNormal(final Vector3D normal) throws MathRuntimeException {
  140.         final double norm = normal.getNorm();
  141.         if (norm < 1.0e-10) {
  142.             throw new MathRuntimeException(LocalizedCoreFormats.ZERO_NORM);
  143.         }
  144.         w = new Vector3D(1.0 / norm, normal);
  145.     }

  146.     /** Reset the plane frame.
  147.      */
  148.     private void setFrame() {
  149.         origin = new Vector3D(-originOffset, w);
  150.         u = w.orthogonal();
  151.         v = Vector3D.crossProduct(w, u);
  152.     }

  153.     /** Get the origin point of the plane frame.
  154.      * <p>The point returned is the orthogonal projection of the
  155.      * 3D-space origin in the plane.</p>
  156.      * @return the origin point of the plane frame (point closest to the
  157.      * 3D-space origin)
  158.      */
  159.     public Vector3D getOrigin() {
  160.         return origin;
  161.     }

  162.     /** Get the normalized normal vector.
  163.      * <p>The frame defined by ({@link #getU() getU()}, {@link #getV() getV()},
  164.      * {@code getNormal()}) is a right-handed orthonormalized
  165.      * frame).</p>
  166.      * @return normalized normal vector
  167.      * @see #getU
  168.      * @see #getV
  169.      */
  170.     public Vector3D getNormal() {
  171.         return w;
  172.     }

  173.     /** Get the plane first canonical vector.
  174.      * <p>The frame defined by ({@code getU()}, {@link #getV() getV()},
  175.      * {@link #getNormal() getNormal()}) is a right-handed orthonormalized
  176.      * frame).</p>
  177.      * @return normalized first canonical vector
  178.      * @see #getV
  179.      * @see #getNormal
  180.      */
  181.     public Vector3D getU() {
  182.         return u;
  183.     }

  184.     /** Get the plane second canonical vector.
  185.      * <p>The frame defined by ({@link #getU() getU()}, {@code getV()},
  186.      * {@link #getNormal() getNormal()}) is a right-handed orthonormalized
  187.      * frame).</p>
  188.      * @return normalized second canonical vector
  189.      * @see #getU
  190.      * @see #getNormal
  191.      */
  192.     public Vector3D getV() {
  193.         return v;
  194.     }

  195.     /** {@inheritDoc}
  196.      */
  197.     @Override
  198.     public Vector3D project(Vector3D point) {
  199.         return toSpace(toSubSpace(point));
  200.     }

  201.     /** {@inheritDoc}
  202.      */
  203.     @Override
  204.     public double getTolerance() {
  205.         return tolerance;
  206.     }

  207.     /** Revert the plane.
  208.      * <p>Replace the instance by a similar plane with opposite orientation.</p>
  209.      * <p>The new plane frame is chosen in such a way that a 3D point that had
  210.      * {@code (x, y)} in-plane coordinates and {@code z} offset with
  211.      * respect to the plane and is unaffected by the change will have
  212.      * {@code (y, x)} in-plane coordinates and {@code -z} offset with
  213.      * respect to the new plane. This means that the {@code u} and {@code v}
  214.      * vectors returned by the {@link #getU} and {@link #getV} methods are exchanged,
  215.      * and the {@code w} vector returned by the {@link #getNormal} method is
  216.      * reversed.</p>
  217.      */
  218.     public void revertSelf() {
  219.         final Vector3D tmp = u;
  220.         u = v;
  221.         v = tmp;
  222.         w = w.negate();
  223.         originOffset = -originOffset;
  224.     }

  225.     /** Transform a 3D space point into an in-plane point.
  226.      * @param point point of the space (must be a {@link Vector3D
  227.      * Vector3D} instance)
  228.      * @return in-plane point (really a {@link
  229.      * org.hipparchus.geometry.euclidean.twod.Vector2D Vector2D} instance)
  230.      * @see #toSpace
  231.      */
  232.     @Override
  233.     public Vector2D toSubSpace(final Vector3D point) {
  234.         return new Vector2D(point.dotProduct(u), point.dotProduct(v));
  235.     }

  236.     /** Transform an in-plane point into a 3D space point.
  237.      * @param point in-plane point (must be a {@link
  238.      * org.hipparchus.geometry.euclidean.twod.Vector2D Vector2D} instance)
  239.      * @return 3D space point (really a {@link Vector3D Vector3D} instance)
  240.      * @see #toSubSpace
  241.      */
  242.     @Override
  243.     public Vector3D toSpace(final Vector2D point) {
  244.         return new Vector3D(point.getX(), u, point.getY(), v, -originOffset, w);
  245.     }

  246.     /** Get one point from the 3D-space.
  247.      * @param inPlane desired in-plane coordinates for the point in the
  248.      * plane
  249.      * @param offset desired offset for the point
  250.      * @return one point in the 3D-space, with given coordinates and offset
  251.      * relative to the plane
  252.      */
  253.     public Vector3D getPointAt(final Vector2D inPlane, final double offset) {
  254.         return new Vector3D(inPlane.getX(), u, inPlane.getY(), v, offset - originOffset, w);
  255.     }

  256.     /** Check if the instance is similar to another plane.
  257.      * <p>Planes are considered similar if they contain the same
  258.      * points. This does not mean they are equal since they can have
  259.      * opposite normals.</p>
  260.      * @param plane plane to which the instance is compared
  261.      * @return true if the planes are similar
  262.      */
  263.     public boolean isSimilarTo(final Plane plane) {
  264.         final double angle = Vector3D.angle(w, plane.w);
  265.         return ((angle < 1.0e-10) && (FastMath.abs(originOffset - plane.originOffset) < tolerance)) ||
  266.                ((angle > (FastMath.PI - 1.0e-10)) && (FastMath.abs(originOffset + plane.originOffset) < tolerance));
  267.     }

  268.     /** Rotate the plane around the specified point.
  269.      * <p>The instance is not modified, a new instance is created.</p>
  270.      * @param center rotation center
  271.      * @param rotation vectorial rotation operator
  272.      * @return a new plane
  273.      */
  274.     public Plane rotate(final Vector3D center, final Rotation rotation) {

  275.         final Vector3D delta = origin.subtract(center);
  276.         final Plane plane = new Plane(center.add(rotation.applyTo(delta)),
  277.                                       rotation.applyTo(w), tolerance);

  278.         // make sure the frame is transformed as desired
  279.         plane.u = rotation.applyTo(u);
  280.         plane.v = rotation.applyTo(v);

  281.         return plane;

  282.     }

  283.     /** Translate the plane by the specified amount.
  284.      * <p>The instance is not modified, a new instance is created.</p>
  285.      * @param translation translation to apply
  286.      * @return a new plane
  287.      */
  288.     public Plane translate(final Vector3D translation) {

  289.         final Plane plane = new Plane(origin.add(translation), w, tolerance);

  290.         // make sure the frame is transformed as desired
  291.         plane.u = u;
  292.         plane.v = v;

  293.         return plane;

  294.     }

  295.     /** Get the intersection of a line with the instance.
  296.      * @param line line intersecting the instance
  297.      * @return intersection point between between the line and the
  298.      * instance (null if the line is parallel to the instance)
  299.      */
  300.     public Vector3D intersection(final Line line) {
  301.         final Vector3D direction = line.getDirection();
  302.         final double   dot       = w.dotProduct(direction);
  303.         if (FastMath.abs(dot) < 1.0e-10) {
  304.             return null;
  305.         }
  306.         final Vector3D point = line.toSpace(Vector1D.ZERO);
  307.         final double   k     = -(originOffset + w.dotProduct(point)) / dot;
  308.         return new Vector3D(1.0, point, k, direction);
  309.     }

  310.     /** Build the line shared by the instance and another plane.
  311.      * @param other other plane
  312.      * @return line at the intersection of the instance and the
  313.      * other plane (really a {@link Line Line} instance)
  314.      */
  315.     public Line intersection(final Plane other) {
  316.         final Vector3D direction = Vector3D.crossProduct(w, other.w);
  317.         if (direction.getNorm() < tolerance) {
  318.             return null;
  319.         }
  320.         final Vector3D point = intersection(this, other, new Plane(direction, tolerance));
  321.         return new Line(point, point.add(direction), tolerance);
  322.     }

  323.     /** Get the intersection point of three planes.
  324.      * @param plane1 first plane1
  325.      * @param plane2 second plane2
  326.      * @param plane3 third plane2
  327.      * @return intersection point of three planes, null if some planes are parallel
  328.      */
  329.     public static Vector3D intersection(final Plane plane1, final Plane plane2, final Plane plane3) {

  330.         // coefficients of the three planes linear equations
  331.         final double a1 = plane1.w.getX();
  332.         final double b1 = plane1.w.getY();
  333.         final double c1 = plane1.w.getZ();
  334.         final double d1 = plane1.originOffset;

  335.         final double a2 = plane2.w.getX();
  336.         final double b2 = plane2.w.getY();
  337.         final double c2 = plane2.w.getZ();
  338.         final double d2 = plane2.originOffset;

  339.         final double a3 = plane3.w.getX();
  340.         final double b3 = plane3.w.getY();
  341.         final double c3 = plane3.w.getZ();
  342.         final double d3 = plane3.originOffset;

  343.         // direct Cramer resolution of the linear system
  344.         // (this is still feasible for a 3x3 system)
  345.         final double a23         = b2 * c3 - b3 * c2;
  346.         final double b23         = c2 * a3 - c3 * a2;
  347.         final double c23         = a2 * b3 - a3 * b2;
  348.         final double determinant = a1 * a23 + b1 * b23 + c1 * c23;
  349.         if (FastMath.abs(determinant) < 1.0e-10) {
  350.             return null;
  351.         }

  352.         final double r = 1.0 / determinant;
  353.         return new Vector3D(
  354.                             (-a23 * d1 - (c1 * b3 - c3 * b1) * d2 - (c2 * b1 - c1 * b2) * d3) * r,
  355.                             (-b23 * d1 - (c3 * a1 - c1 * a3) * d2 - (c1 * a2 - c2 * a1) * d3) * r,
  356.                             (-c23 * d1 - (b1 * a3 - b3 * a1) * d2 - (b2 * a1 - b1 * a2) * d3) * r);

  357.     }

  358.     /** Build a region covering the whole hyperplane.
  359.      * @return a region covering the whole hyperplane
  360.      */
  361.     @Override
  362.     public SubPlane wholeHyperplane() {
  363.         return new SubPlane(this, new PolygonsSet(tolerance));
  364.     }

  365.     /** {@inheritDoc} */
  366.     @Override
  367.     public SubPlane emptyHyperplane() {
  368.         final RegionFactory<Euclidean2D, Vector2D, org.hipparchus.geometry.euclidean.twod.Line, SubLine> factory = new RegionFactory<>();
  369.         return new SubPlane(this, factory.getComplement(new PolygonsSet(tolerance)));
  370.     }

  371.     /** Build a region covering the whole space.
  372.      * @return a region containing the instance (really a {@link
  373.      * PolyhedronsSet PolyhedronsSet} instance)
  374.      */
  375.     @Override
  376.     public PolyhedronsSet wholeSpace() {
  377.         return new PolyhedronsSet(tolerance);
  378.     }

  379.     /** Check if the instance contains a point.
  380.      * @param p point to check
  381.      * @return true if p belongs to the plane
  382.      */
  383.     public boolean contains(final Vector3D p) {
  384.         return FastMath.abs(getOffset(p)) < tolerance;
  385.     }

  386.     /** Get the offset (oriented distance) of a parallel plane.
  387.      * <p>This method should be called only for parallel planes otherwise
  388.      * the result is not meaningful.</p>
  389.      * <p>The offset is 0 if both planes are the same, it is
  390.      * positive if the plane is on the plus side of the instance and
  391.      * negative if it is on the minus side, according to its natural
  392.      * orientation.</p>
  393.      * @param plane plane to check
  394.      * @return offset of the plane
  395.      */
  396.     public double getOffset(final Plane plane) {
  397.         return originOffset + (sameOrientationAs(plane) ? -plane.originOffset : plane.originOffset);
  398.     }

  399.     /** Get the offset (oriented distance) of a point.
  400.      * <p>The offset is 0 if the point is on the underlying hyperplane,
  401.      * it is positive if the point is on one particular side of the
  402.      * hyperplane, and it is negative if the point is on the other side,
  403.      * according to the hyperplane natural orientation.</p>
  404.      * @param point point to check
  405.      * @return offset of the point
  406.      */
  407.     @Override
  408.     public double getOffset(final Vector3D point) {
  409.         return point.dotProduct(w) + originOffset;
  410.     }

  411.     /** {@inheritDoc} */
  412.     @Override
  413.     public Vector3D moveToOffset(final Vector3D point, final double offset) {
  414.         final double delta = offset - getOffset(point);
  415.         return new Vector3D(point.getX() + delta * w.getX(),
  416.                             point.getY() + delta * w.getY(),
  417.                             point.getZ() + delta * w.getZ());
  418.     }

  419.     /** {@inheritDoc} */
  420.     @Override
  421.     public Vector3D arbitraryPoint() {
  422.         return origin;
  423.     }

  424.     /** Check if the instance has the same orientation as another hyperplane.
  425.      * @param other other hyperplane to check against the instance
  426.      * @return true if the instance and the other hyperplane have
  427.      * the same orientation
  428.      */
  429.     @Override
  430.     public boolean sameOrientationAs(final Plane other) {
  431.         return other.w.dotProduct(w) > 0.0;
  432.     }

  433. }