EmbeddedAreaPlaneConvexSubset.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.  *      http://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. package org.apache.commons.geometry.euclidean.threed;

  18. import java.util.List;

  19. import org.apache.commons.geometry.core.Transform;
  20. import org.apache.commons.geometry.core.partitioning.Hyperplane;
  21. import org.apache.commons.geometry.core.partitioning.Split;
  22. import org.apache.commons.geometry.euclidean.twod.AffineTransformMatrix2D;
  23. import org.apache.commons.geometry.euclidean.twod.ConvexArea;
  24. import org.apache.commons.geometry.euclidean.twod.Vector2D;

  25. /** Internal implementation of {@link PlaneConvexSubset} that uses an embedded
  26.  * {@link ConvexArea} to represent the subspace region. This class is capable of
  27.  * representing regions of infinite size.
  28.  */
  29. final class EmbeddedAreaPlaneConvexSubset extends AbstractEmbeddedRegionPlaneSubset
  30.     implements PlaneConvexSubset, PlaneConvexSubset.Embedded {

  31.     /** The embedded 2D area. */
  32.     private final ConvexArea area;

  33.     /** Create a new instance from its component parts.
  34.      * @param plane plane the the convex area is embedded in
  35.      * @param area the embedded convex area
  36.      */
  37.     EmbeddedAreaPlaneConvexSubset(final EmbeddingPlane plane, final ConvexArea area) {
  38.         super(plane);

  39.         this.area = area;
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public PlaneConvexSubset.Embedded getEmbedded() {
  44.         return this;
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public ConvexArea getSubspaceRegion() {
  49.         return area;
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public List<Vector3D> getVertices() {
  54.         return getPlane().toSpace(area.getVertices());
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public Bounds3D getBounds() {
  59.         return getBoundsFromSubspace(area);
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public List<Triangle3D> toTriangles() {
  64.         if (isInfinite()) {
  65.             throw new IllegalStateException("Cannot convert infinite plane subset to triangles: " + this);
  66.         }

  67.         final EmbeddingPlane plane = getPlane();
  68.         final List<Vector3D> vertices = plane.toSpace(area.getVertices());

  69.         return Planes.convexPolygonToTriangleFan(plane, vertices);
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public EmbeddedAreaPlaneConvexSubset transform(final Transform<Vector3D> transform) {
  74.         final EmbeddingPlane.SubspaceTransform st = getPlane().subspaceTransform(transform);
  75.         final ConvexArea tArea = area.transform(st.getTransform());

  76.         return new EmbeddedAreaPlaneConvexSubset(st.getPlane().getEmbedding(), tArea);
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     public EmbeddedAreaPlaneConvexSubset reverse() {
  81.         final EmbeddingPlane plane = getPlane();
  82.         final EmbeddingPlane rPlane = plane.reverse();

  83.         final Vector2D rU = rPlane.toSubspace(plane.toSpace(Vector2D.Unit.PLUS_X));
  84.         final Vector2D rV = rPlane.toSubspace(plane.toSpace(Vector2D.Unit.PLUS_Y));

  85.         final AffineTransformMatrix2D transform =
  86.                 AffineTransformMatrix2D.fromColumnVectors(rU, rV);

  87.         return new EmbeddedAreaPlaneConvexSubset(rPlane, area.transform(transform));
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public Split<PlaneConvexSubset> split(final Hyperplane<Vector3D> splitter) {
  92.         // delegate back to the Planes factory method so that it has a chance to decide
  93.         // on the best possible implementation for the given area
  94.         return Planes.subspaceSplit((Plane) splitter, this,
  95.             (p, r) -> Planes.subsetFromConvexArea(p, (ConvexArea) r));
  96.     }
  97. }