AbstractEmbeddedRegionPlaneSubset.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 org.apache.commons.geometry.core.RegionLocation;
  19. import org.apache.commons.geometry.core.internal.HyperplaneSubsets;
  20. import org.apache.commons.geometry.euclidean.twod.BoundarySource2D;
  21. import org.apache.commons.geometry.euclidean.twod.Bounds2D;
  22. import org.apache.commons.geometry.euclidean.twod.Vector2D;

  23. /** Base class for {@link PlaneSubset} implementations that use an embedded subspace region
  24.  * to define their plane subsets.
  25.  */
  26. abstract class AbstractEmbeddedRegionPlaneSubset extends AbstractPlaneSubset implements PlaneSubset.Embedded {

  27.     /** The plane containing the embedded region. */
  28.     private final EmbeddingPlane plane;

  29.     /** Construct a new instance in the given plane.
  30.      * @param plane plane containing the subset
  31.      */
  32.     AbstractEmbeddedRegionPlaneSubset(final EmbeddingPlane plane) {
  33.         this.plane = plane;
  34.     }

  35.     /** {@inheritDoc} */
  36.     @Override
  37.     public EmbeddingPlane getPlane() {
  38.         return plane;
  39.     }

  40.     /** {@inheritDoc} */
  41.     @Override
  42.     public EmbeddingPlane getHyperplane() {
  43.         return getPlane();
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public boolean isFull() {
  48.         return getSubspaceRegion().isFull();
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     public boolean isEmpty() {
  53.         return getSubspaceRegion().isEmpty();
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public double getSize() {
  58.         return getSubspaceRegion().getSize();
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public Vector3D getCentroid() {
  63.         final Vector2D subspaceCentroid = getSubspaceRegion().getCentroid();
  64.         if (subspaceCentroid != null) {
  65.             return getPlane().toSpace(subspaceCentroid);
  66.         }
  67.         return null;
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public Vector3D toSpace(final Vector2D pt) {
  72.         return plane.toSpace(pt);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public Vector2D toSubspace(final Vector3D pt) {
  77.         return plane.toSubspace(pt);
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public RegionLocation classify(final Vector3D pt) {
  82.         return HyperplaneSubsets.classifyAgainstEmbeddedRegion(pt, plane, getSubspaceRegion());
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public Vector3D closest(final Vector3D pt) {
  87.         return HyperplaneSubsets.closestToEmbeddedRegion(pt, plane, getSubspaceRegion());
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public String toString() {
  92.         final StringBuilder sb = new StringBuilder();
  93.         sb.append(getClass().getSimpleName())
  94.             .append("[plane= ")
  95.             .append(getPlane())
  96.             .append(", subspaceRegion= ")
  97.             .append(getSubspaceRegion())
  98.             .append(']');

  99.         return sb.toString();
  100.     }

  101.     /** Compute 3D bounds from a subspace boundary source.
  102.      * @param src subspace boundary source
  103.      * @return 3D bounds from the given embedded subspace boundary source or null
  104.      *      if no valid bounds could be determined
  105.      */
  106.     protected Bounds3D getBoundsFromSubspace(final BoundarySource2D src) {
  107.         final Bounds2D subspaceBounds = src.getBounds();
  108.         if (subspaceBounds != null) {
  109.             final Vector3D min = plane.toSpace(subspaceBounds.getMin());
  110.             final Vector3D max = plane.toSpace(subspaceBounds.getMax());

  111.             return Bounds3D.builder()
  112.                     .add(min)
  113.                     .add(max)
  114.                     .build();
  115.         }

  116.         return null;
  117.     }
  118. }