View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.geometry.spherical.twod;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.hipparchus.geometry.partitioning.BSPTree;
23  import org.hipparchus.geometry.partitioning.BSPTreeVisitor;
24  import org.hipparchus.geometry.partitioning.BoundaryAttribute;
25  import org.hipparchus.geometry.spherical.oned.Arc;
26  import org.hipparchus.geometry.spherical.oned.ArcsSet;
27  import org.hipparchus.geometry.spherical.oned.S1Point;
28  import org.hipparchus.util.FastMath;
29  
30  /** Visitor building edges.
31   * @since 1.4
32   */
33  class EdgesWithNodeInfoBuilder implements BSPTreeVisitor<Sphere2D, S2Point, Circle, SubCircle> {
34  
35      /** Tolerance for close nodes connection. */
36      private final double tolerance;
37  
38      /** Built segments. */
39      private final List<EdgeWithNodeInfo> edges;
40  
41      /** Simple constructor.
42       * @param tolerance below which points are consider to be identical
43       */
44      EdgesWithNodeInfoBuilder(final double tolerance) {
45          this.tolerance = tolerance;
46          this.edges     = new ArrayList<>();
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      public Order visitOrder(final BSPTree<Sphere2D, S2Point, Circle, SubCircle> node) {
52          return Order.MINUS_SUB_PLUS;
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public void visitInternalNode(final BSPTree<Sphere2D, S2Point, Circle, SubCircle> node) {
58          @SuppressWarnings("unchecked")
59          final BoundaryAttribute<Sphere2D, S2Point, Circle, SubCircle> attribute =
60                  (BoundaryAttribute<Sphere2D, S2Point, Circle, SubCircle>) node.getAttribute();
61          final Iterable<BSPTree<Sphere2D, S2Point, Circle, SubCircle>> splitters = attribute.getSplitters();
62          if (attribute.getPlusOutside() != null) {
63              addContribution(attribute.getPlusOutside(), node, splitters, false);
64          }
65          if (attribute.getPlusInside() != null) {
66              addContribution(attribute.getPlusInside(), node, splitters, true);
67          }
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public void visitLeafNode(final BSPTree<Sphere2D, S2Point, Circle, SubCircle> node) {
73      }
74  
75      /** Add the contribution of a boundary edge.
76       * @param sub boundary facet
77       * @param node node to which the edge belongs
78       * @param splitters splitters for the boundary facet
79       * @param reversed if true, the facet has the inside on its plus side
80       */
81      private void addContribution(final SubCircle sub, final BSPTree<Sphere2D, S2Point, Circle, SubCircle> node,
82                                   final Iterable<BSPTree<Sphere2D, S2Point, Circle, SubCircle>> splitters,
83                                   final boolean reversed) {
84          final Circle circle  = sub.getHyperplane();
85          final List<Arc> arcs = ((ArcsSet) sub.getRemainingRegion()).asList();
86          for (final Arc a : arcs) {
87  
88              // find the 2D points
89              final Vertex startS = new Vertex(circle.toSpace(new S1Point(a.getInf())));
90              final Vertex endS   = new Vertex(circle.toSpace(new S1Point(a.getSup())));
91  
92              // recover the connectivity information
93              final BSPTree<Sphere2D, S2Point, Circle, SubCircle> startN = selectClosest(startS.getLocation(), splitters);
94              final BSPTree<Sphere2D, S2Point, Circle, SubCircle> endN   = selectClosest(endS.getLocation(), splitters);
95  
96              if (reversed) {
97                  edges.add(new EdgeWithNodeInfo(endS, startS, a.getSize(), circle.getReverse(), node, endN, startN));
98              } else {
99                  edges.add(new EdgeWithNodeInfo(startS, endS, a.getSize(), circle, node, startN, endN));
100             }
101 
102         }
103     }
104 
105     /** Select the node whose cut sub-hyperplane is closest to specified point.
106      * @param point reference point
107      * @param candidates candidate nodes
108      * @return node closest to point, or null if no node is closer than tolerance
109      */
110     private BSPTree<Sphere2D, S2Point, Circle, SubCircle>
111         selectClosest(final S2Point point, final Iterable<BSPTree<Sphere2D, S2Point, Circle, SubCircle>> candidates) {
112 
113         if (point == null) {
114             return null;
115         }
116 
117         BSPTree<Sphere2D, S2Point, Circle, SubCircle> selected = null;
118 
119         double min = Double.POSITIVE_INFINITY;
120         for (final BSPTree<Sphere2D, S2Point, Circle, SubCircle> node : candidates) {
121             final double distance = FastMath.abs(node.getCut().getHyperplane().getOffset(point));
122             if (distance < min) {
123                 selected = node;
124                 min      = distance;
125             }
126         }
127 
128         return min <= tolerance ? selected : null;
129 
130     }
131 
132     /** Get the edges.
133      * @return built edges
134      */
135     public List<EdgeWithNodeInfo> getEdges() {
136         return edges;
137     }
138 
139 }