View Javadoc
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  
19  import org.apache.commons.geometry.core.RegionLocation;
20  import org.apache.commons.geometry.core.internal.HyperplaneSubsets;
21  import org.apache.commons.geometry.euclidean.twod.BoundarySource2D;
22  import org.apache.commons.geometry.euclidean.twod.Bounds2D;
23  import org.apache.commons.geometry.euclidean.twod.Vector2D;
24  
25  /** Base class for {@link PlaneSubset} implementations that use an embedded subspace region
26   * to define their plane subsets.
27   */
28  abstract class AbstractEmbeddedRegionPlaneSubset extends AbstractPlaneSubset implements PlaneSubset.Embedded {
29  
30      /** The plane containing the embedded region. */
31      private final EmbeddingPlane plane;
32  
33      /** Construct a new instance in the given plane.
34       * @param plane plane containing the subset
35       */
36      AbstractEmbeddedRegionPlaneSubset(final EmbeddingPlane plane) {
37          this.plane = plane;
38      }
39  
40      /** {@inheritDoc} */
41      @Override
42      public EmbeddingPlane getPlane() {
43          return plane;
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public EmbeddingPlane getHyperplane() {
49          return getPlane();
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public boolean isFull() {
55          return getSubspaceRegion().isFull();
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public boolean isEmpty() {
61          return getSubspaceRegion().isEmpty();
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public double getSize() {
67          return getSubspaceRegion().getSize();
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public Vector3D getCentroid() {
73          final Vector2D subspaceCentroid = getSubspaceRegion().getCentroid();
74          if (subspaceCentroid != null) {
75              return getPlane().toSpace(subspaceCentroid);
76          }
77          return null;
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public Vector3D toSpace(final Vector2D pt) {
83          return plane.toSpace(pt);
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public Vector2D toSubspace(final Vector3D pt) {
89          return plane.toSubspace(pt);
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public RegionLocation classify(final Vector3D pt) {
95          return HyperplaneSubsets.classifyAgainstEmbeddedRegion(pt, plane, getSubspaceRegion());
96      }
97  
98      /** {@inheritDoc} */
99      @Override
100     public Vector3D closest(final Vector3D pt) {
101         return HyperplaneSubsets.closestToEmbeddedRegion(pt, plane, getSubspaceRegion());
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public String toString() {
107         final StringBuilder sb = new StringBuilder();
108         sb.append(getClass().getSimpleName())
109             .append("[plane= ")
110             .append(getPlane())
111             .append(", subspaceRegion= ")
112             .append(getSubspaceRegion())
113             .append(']');
114 
115         return sb.toString();
116     }
117 
118     /** Compute 3D bounds from a subspace boundary source.
119      * @param src subspace boundary source
120      * @return 3D bounds from the given embedded subspace boundary source or null
121      *      if no valid bounds could be determined
122      */
123     protected Bounds3D getBoundsFromSubspace(final BoundarySource2D src) {
124         final Bounds2D subspaceBounds = src.getBounds();
125         if (subspaceBounds != null) {
126             final Vector3D min = plane.toSpace(subspaceBounds.getMin());
127             final Vector3D max = plane.toSpace(subspaceBounds.getMax());
128 
129             return Bounds3D.builder()
130                     .add(min)
131                     .add(max)
132                     .build();
133         }
134 
135         return null;
136     }
137 }