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    *      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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.geometry.partitioning;
23  
24  import org.hipparchus.geometry.Space;
25  
26  /** Visitor building boundary shell tree.
27   * <p>
28   * The boundary shell is represented as {@link BoundaryAttribute boundary attributes}
29   * at each internal node.
30   * </p>
31   * @param <S> Type of the space.
32   */
33  class BoundaryBuilder<S extends Space> implements BSPTreeVisitor<S> {
34  
35      /** {@inheritDoc} */
36      @Override
37      public Order visitOrder(BSPTree<S> node) {
38          return Order.PLUS_MINUS_SUB;
39      }
40  
41      /** {@inheritDoc} */
42      @Override
43      public void visitInternalNode(BSPTree<S> node) {
44  
45          SubHyperplane<S> plusOutside = null;
46          SubHyperplane<S> plusInside  = null;
47          NodesSet<S>      splitters   = null;
48  
49          // characterize the cut sub-hyperplane,
50          // first with respect to the plus sub-tree
51          final Characterization<S> plusChar = new Characterization<>(node.getPlus(), node.getCut().copySelf());
52  
53          if (plusChar.touchOutside()) {
54              // plusChar.outsideTouching() corresponds to a subset of the cut sub-hyperplane
55              // known to have outside cells on its plus side, we want to check if parts
56              // of this subset do have inside cells on their minus side
57              final Characterization<S> minusChar = new Characterization<>(node.getMinus(), plusChar.outsideTouching());
58              if (minusChar.touchInside()) {
59                  // this part belongs to the boundary,
60                  // it has the outside on its plus side and the inside on its minus side
61                  plusOutside = minusChar.insideTouching();
62                  splitters = new NodesSet<>();
63                  splitters.addAll(minusChar.getInsideSplitters());
64                  splitters.addAll(plusChar.getOutsideSplitters());
65              }
66          }
67  
68          if (plusChar.touchInside()) {
69              // plusChar.insideTouching() corresponds to a subset of the cut sub-hyperplane
70              // known to have inside cells on its plus side, we want to check if parts
71              // of this subset do have outside cells on their minus side
72              final Characterization<S> minusChar = new Characterization<>(node.getMinus(), plusChar.insideTouching());
73              if (minusChar.touchOutside()) {
74                  // this part belongs to the boundary,
75                  // it has the inside on its plus side and the outside on its minus side
76                  plusInside = minusChar.outsideTouching();
77                  if (splitters == null) {
78                      splitters = new NodesSet<>();
79                  }
80                  splitters.addAll(minusChar.getOutsideSplitters());
81                  splitters.addAll(plusChar.getInsideSplitters());
82              }
83          }
84  
85          if (splitters != null) {
86              // the parent nodes are natural splitters for boundary sub-hyperplanes
87              for (BSPTree<S> up = node.getParent(); up != null; up = up.getParent()) {
88                  splitters.add(up);
89              }
90          }
91  
92          // set the boundary attribute at non-leaf nodes
93          node.setAttribute(new BoundaryAttribute<>(plusOutside, plusInside, splitters));
94  
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public void visitLeafNode(BSPTree<S> node) {
100     }
101 
102 }