1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.geometry.spherical.twod;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.hipparchus.exception.MathRuntimeException;
28 import org.hipparchus.geometry.euclidean.threed.Vector3D;
29 import org.hipparchus.geometry.partitioning.BSPTree;
30 import org.hipparchus.geometry.partitioning.BSPTreeVisitor;
31 import org.hipparchus.util.FastMath;
32 import org.hipparchus.util.MathUtils;
33
34
35
36 class PropertiesComputer implements BSPTreeVisitor<Sphere2D> {
37
38
39 private final double tolerance;
40
41
42 private double summedArea;
43
44
45 private Vector3D summedBarycenter;
46
47
48 private final List<Vector3D> convexCellsInsidePoints;
49
50
51
52
53 PropertiesComputer(final double tolerance) {
54 this.tolerance = tolerance;
55 this.summedArea = 0;
56 this.summedBarycenter = Vector3D.ZERO;
57 this.convexCellsInsidePoints = new ArrayList<>();
58 }
59
60
61 @Override
62 public Order visitOrder(final BSPTree<Sphere2D> node) {
63 return Order.MINUS_SUB_PLUS;
64 }
65
66
67 @Override
68 public void visitInternalNode(final BSPTree<Sphere2D> node) {
69
70 }
71
72
73 @Override
74 public void visitLeafNode(final BSPTree<Sphere2D> node) {
75 if ((Boolean) node.getAttribute()) {
76
77
78 final SphericalPolygonsSet convex =
79 new SphericalPolygonsSet(node.pruneAroundConvexCell(Boolean.TRUE,
80 Boolean.FALSE,
81 null),
82 tolerance);
83
84
85 final List<Vertex> boundary = convex.getBoundaryLoops();
86 if (boundary.size() != 1) {
87
88 throw MathRuntimeException.createInternalError();
89 }
90
91
92 final double area = convexCellArea(boundary.get(0));
93 final Vector3D barycenter = convexCellBarycenter(boundary.get(0));
94 convexCellsInsidePoints.add(barycenter);
95
96
97 summedArea += area;
98 summedBarycenter = new Vector3D(1, summedBarycenter, area, barycenter);
99
100 }
101 }
102
103
104
105
106
107 private double convexCellArea(final Vertex start) {
108
109 int n = 0;
110 double sum = 0;
111
112
113 for (Edge e = start.getOutgoing(); n == 0 || e.getStart() != start; e = e.getEnd().getOutgoing()) {
114
115
116 final Vector3D previousPole = e.getCircle().getPole();
117 final Vector3D nextPole = e.getEnd().getOutgoing().getCircle().getPole();
118 final Vector3D point = e.getEnd().getLocation().getVector();
119 double alpha = FastMath.atan2(Vector3D.dotProduct(nextPole, Vector3D.crossProduct(point, previousPole)),
120 -Vector3D.dotProduct(nextPole, previousPole));
121 if (alpha < 0) {
122 alpha += MathUtils.TWO_PI;
123 }
124 sum += alpha;
125 n++;
126 }
127
128
129
130
131
132 return sum - (n - 2) * FastMath.PI;
133
134 }
135
136
137
138
139
140 private Vector3D convexCellBarycenter(final Vertex start) {
141
142 int n = 0;
143 Vector3D sumB = Vector3D.ZERO;
144
145
146 for (Edge e = start.getOutgoing(); n == 0 || e.getStart() != start; e = e.getEnd().getOutgoing()) {
147 sumB = new Vector3D(1, sumB, e.getLength(), e.getCircle().getPole());
148 n++;
149 }
150
151 return sumB.normalize();
152
153 }
154
155
156
157
158 public double getArea() {
159 return summedArea;
160 }
161
162
163
164
165 public S2Point getBarycenter() {
166 if (summedBarycenter.getNormSq() == 0) {
167 return S2Point.NaN;
168 } else {
169 return new S2Point(summedBarycenter);
170 }
171 }
172
173
174
175
176 public List<Vector3D> getConvexCellsInsidePoints() {
177 return convexCellsInsidePoints;
178 }
179
180 }