SubPlane.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.geometry.euclidean.oned.Vector1D;
  23. import org.hipparchus.geometry.euclidean.twod.Euclidean2D;
  24. import org.hipparchus.geometry.euclidean.twod.PolygonsSet;
  25. import org.hipparchus.geometry.euclidean.twod.SubLine;
  26. import org.hipparchus.geometry.euclidean.twod.Vector2D;
  27. import org.hipparchus.geometry.partitioning.AbstractSubHyperplane;
  28. import org.hipparchus.geometry.partitioning.BSPTree;
  29. import org.hipparchus.geometry.partitioning.Region;

  30. /** This class represents a sub-hyperplane for {@link Plane}.
  31.  */
  32. public class SubPlane extends AbstractSubHyperplane<Euclidean3D, Vector3D, Plane, SubPlane,
  33.                                                     Euclidean2D, Vector2D, org.hipparchus.geometry.euclidean.twod.Line, SubLine> {

  34.     /** Simple constructor.
  35.      * @param hyperplane underlying hyperplane
  36.      * @param remainingRegion remaining region of the hyperplane
  37.      */
  38.     public SubPlane(final Plane hyperplane,
  39.                     final Region<Euclidean2D, Vector2D, org.hipparchus.geometry.euclidean.twod.Line, SubLine> remainingRegion) {
  40.         super(hyperplane, remainingRegion);
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     protected SubPlane buildNew(final Plane hyperplane,
  45.                                 final Region<Euclidean2D, Vector2D, org.hipparchus.geometry.euclidean.twod.Line, SubLine> remainingRegion) {
  46.         return new SubPlane(hyperplane, remainingRegion);
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public Vector3D getInteriorPoint() {
  51.         return isEmpty() ? null : getHyperplane().toSpace(getRemainingRegion().getInteriorPoint());
  52.     }

  53.     /** Split the instance in two parts by an hyperplane.
  54.      * @param hyperplane splitting hyperplane
  55.      * @return an object containing both the part of the instance
  56.      * on the plus side of the instance and the part of the
  57.      * instance on the minus side of the instance
  58.      */
  59.     @Override
  60.     public SplitSubHyperplane<Euclidean3D, Vector3D, Plane, SubPlane> split(Plane hyperplane) {

  61.         final Line  inter      = hyperplane.intersection(getHyperplane());
  62.         final double tolerance = getHyperplane().getTolerance();

  63.         if (inter == null) {
  64.             // the hyperplanes are parallel
  65.             final double global = hyperplane.getOffset(getHyperplane());
  66.             if (global < -tolerance) {
  67.                 return new SplitSubHyperplane<>(null, this);
  68.             } else if (global > tolerance) {
  69.                 return new SplitSubHyperplane<>(this, null);
  70.             } else {
  71.                 return new SplitSubHyperplane<>(null, null);
  72.             }
  73.         }

  74.         // the hyperplanes do intersect
  75.         Vector2D p = getHyperplane().toSubSpace(inter.toSpace(Vector1D.ZERO));
  76.         Vector2D q = getHyperplane().toSubSpace(inter.toSpace(Vector1D.ONE));
  77.         Vector3D crossP = Vector3D.crossProduct(inter.getDirection(), getHyperplane().getNormal());
  78.         if (crossP.dotProduct(hyperplane.getNormal()) < 0) {
  79.             final Vector2D tmp = p;
  80.             p           = q;
  81.             q           = tmp;
  82.         }
  83.         final SubLine l2DMinus = new org.hipparchus.geometry.euclidean.twod.Line(p, q, tolerance).wholeHyperplane();
  84.         final SubLine l2DPlus  = new org.hipparchus.geometry.euclidean.twod.Line(q, p, tolerance).wholeHyperplane();

  85.         final BSPTree<Euclidean2D, Vector2D, org.hipparchus.geometry.euclidean.twod.Line, SubLine> splitTree =
  86.                 getRemainingRegion().getTree(false).split(l2DMinus);
  87.         final BSPTree<Euclidean2D, Vector2D, org.hipparchus.geometry.euclidean.twod.Line, SubLine> plusTree  =
  88.                 getRemainingRegion().isEmpty(splitTree.getPlus()) ?
  89.                                              new BSPTree<>(Boolean.FALSE) :
  90.                                              new BSPTree<>(l2DPlus, new BSPTree<>(Boolean.FALSE), splitTree.getPlus(), null);

  91.         final BSPTree<Euclidean2D, Vector2D, org.hipparchus.geometry.euclidean.twod.Line, SubLine> minusTree =
  92.                 getRemainingRegion().isEmpty(splitTree.getMinus()) ?
  93.                                              new BSPTree<>(Boolean.FALSE) :
  94.                                              new BSPTree<>(l2DMinus, new BSPTree<>(Boolean.FALSE), splitTree.getMinus(), null);

  95.         return new SplitSubHyperplane<>(new SubPlane(getHyperplane().copySelf(), new PolygonsSet(plusTree, tolerance)),
  96.                                         new SubPlane(getHyperplane().copySelf(), new PolygonsSet(minusTree, tolerance)));

  97.     }

  98. }