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.math3.geometry.partitioning;
18  
19  import org.apache.commons.math3.geometry.Space;
20  
21  /** This class implements the dimension-independent parts of {@link SubHyperplane}.
22  
23   * <p>sub-hyperplanes are obtained when parts of an {@link
24   * Hyperplane hyperplane} are chopped off by other hyperplanes that
25   * intersect it. The remaining part is a convex region. Such objects
26   * appear in {@link BSPTree BSP trees} as the intersection of a cut
27   * hyperplane with the convex region which it splits, the chopping
28   * hyperplanes are the cut hyperplanes closer to the tree root.</p>
29  
30   * @param <S> Type of the embedding space.
31   * @param <T> Type of the embedded sub-space.
32  
33   * @since 3.0
34   */
35  public abstract class AbstractSubHyperplane<S extends Space, T extends Space>
36      implements SubHyperplane<S> {
37  
38      /** Underlying hyperplane. */
39      private final Hyperplane<S> hyperplane;
40  
41      /** Remaining region of the hyperplane. */
42      private final Region<T> remainingRegion;
43  
44      /** Build a sub-hyperplane from an hyperplane and a region.
45       * @param hyperplane underlying hyperplane
46       * @param remainingRegion remaining region of the hyperplane
47       */
48      protected AbstractSubHyperplane(final Hyperplane<S> hyperplane,
49                                      final Region<T> remainingRegion) {
50          this.hyperplane      = hyperplane;
51          this.remainingRegion = remainingRegion;
52      }
53  
54      /** Build a sub-hyperplane from an hyperplane and a region.
55       * @param hyper underlying hyperplane
56       * @param remaining remaining region of the hyperplane
57       * @return a new sub-hyperplane
58       */
59      protected abstract AbstractSubHyperplane<S, T> buildNew(final Hyperplane<S> hyper,
60                                                              final Region<T> remaining);
61  
62      /** {@inheritDoc} */
63      public AbstractSubHyperplane<S, T> copySelf() {
64          return buildNew(hyperplane.copySelf(), remainingRegion);
65      }
66  
67      /** Get the underlying hyperplane.
68       * @return underlying hyperplane
69       */
70      public Hyperplane<S> getHyperplane() {
71          return hyperplane;
72      }
73  
74      /** Get the remaining region of the hyperplane.
75       * <p>The returned region is expressed in the canonical hyperplane
76       * frame and has the hyperplane dimension. For example a chopped
77       * hyperplane in the 3D euclidean is a 2D plane and the
78       * corresponding region is a convex 2D polygon.</p>
79       * @return remaining region of the hyperplane
80       */
81      public Region<T> getRemainingRegion() {
82          return remainingRegion;
83      }
84  
85      /** {@inheritDoc} */
86      public double getSize() {
87          return remainingRegion.getSize();
88      }
89  
90      /** {@inheritDoc} */
91      public AbstractSubHyperplane<S, T> reunite(final SubHyperplane<S> other) {
92          @SuppressWarnings("unchecked")
93          AbstractSubHyperplane<S, T> o = (AbstractSubHyperplane<S, T>) other;
94          return buildNew(hyperplane,
95                          new RegionFactory<T>().union(remainingRegion, o.remainingRegion));
96      }
97  
98      /** Apply a transform to the instance.
99       * <p>The instance must be a (D-1)-dimension sub-hyperplane with
100      * respect to the transform <em>not</em> a (D-2)-dimension
101      * sub-hyperplane the transform knows how to transform by
102      * itself. The transform will consist in transforming first the
103      * hyperplane and then the all region using the various methods
104      * provided by the transform.</p>
105      * @param transform D-dimension transform to apply
106      * @return the transformed instance
107      */
108     public AbstractSubHyperplane<S, T> applyTransform(final Transform<S, T> transform) {
109         final Hyperplane<S> tHyperplane = transform.apply(hyperplane);
110         final BSPTree<T> tTree =
111             recurseTransform(remainingRegion.getTree(false), tHyperplane, transform);
112         return buildNew(tHyperplane, remainingRegion.buildNew(tTree));
113     }
114 
115     /** Recursively transform a BSP-tree from a sub-hyperplane.
116      * @param node current BSP tree node
117      * @param transformed image of the instance hyperplane by the transform
118      * @param transform transform to apply
119      * @return a new tree
120      */
121     private BSPTree<T> recurseTransform(final BSPTree<T> node,
122                                         final Hyperplane<S> transformed,
123                                         final Transform<S, T> transform) {
124         if (node.getCut() == null) {
125             return new BSPTree<T>(node.getAttribute());
126         }
127 
128         @SuppressWarnings("unchecked")
129         BoundaryAttribute<T> attribute =
130             (BoundaryAttribute<T>) node.getAttribute();
131         if (attribute != null) {
132             final SubHyperplane<T> tPO = (attribute.getPlusOutside() == null) ?
133                 null : transform.apply(attribute.getPlusOutside(), hyperplane, transformed);
134             final SubHyperplane<T> tPI = (attribute.getPlusInside() == null) ?
135                 null : transform.apply(attribute.getPlusInside(), hyperplane, transformed);
136             attribute = new BoundaryAttribute<T>(tPO, tPI);
137         }
138 
139         return new BSPTree<T>(transform.apply(node.getCut(), hyperplane, transformed),
140                               recurseTransform(node.getPlus(), transformed, transform),
141                               recurseTransform(node.getMinus(), transformed, transform),
142                               attribute);
143 
144     }
145 
146     /** {@inheritDoc} */
147     public abstract Side side(Hyperplane<S> hyper);
148 
149     /** {@inheritDoc} */
150     public abstract SplitSubHyperplane<S> split(Hyperplane<S> hyper);
151 
152     /** {@inheritDoc} */
153     public boolean isEmpty() {
154         return remainingRegion.isEmpty();
155     }
156 
157 }