1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.euclidean.threed.shape;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.apache.commons.geometry.core.partitioning.bsp.RegionCutRule;
25 import org.apache.commons.geometry.euclidean.AbstractNSphere;
26 import org.apache.commons.geometry.euclidean.threed.Plane;
27 import org.apache.commons.geometry.euclidean.threed.Planes;
28 import org.apache.commons.geometry.euclidean.threed.RegionBSPTree3D;
29 import org.apache.commons.geometry.euclidean.threed.RegionBSPTree3D.RegionNode3D;
30 import org.apache.commons.geometry.euclidean.threed.Vector3D;
31 import org.apache.commons.geometry.euclidean.threed.line.Line3D;
32 import org.apache.commons.geometry.euclidean.threed.line.LineConvexSubset3D;
33 import org.apache.commons.geometry.euclidean.threed.line.LinecastPoint3D;
34 import org.apache.commons.geometry.euclidean.threed.line.Linecastable3D;
35 import org.apache.commons.geometry.euclidean.threed.mesh.SimpleTriangleMesh;
36 import org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh;
37 import org.apache.commons.numbers.core.Precision;
38
39
40
41 public final class Sphere extends AbstractNSphere<Vector3D> implements Linecastable3D {
42
43
44 private static final String INVALID_SUBDIVISION_MESSAGE =
45 "Number of sphere approximation subdivisions must be greater than or equal to zero; was {0}";
46
47
48 private static final double FOUR_PI = 4.0 * Math.PI;
49
50
51 private static final double FOUR_THIRDS_PI = FOUR_PI / 3.0;
52
53
54
55
56
57
58
59
60 private Sphere(final Vector3D center, final double radius, final Precision.DoubleEquivalence precision) {
61 super(center, radius, precision);
62 }
63
64
65 @Override
66 public double getSize() {
67 final double r = getRadius();
68 return FOUR_THIRDS_PI * r * r * r;
69 }
70
71
72 @Override
73 public double getBoundarySize() {
74 final double r = getRadius();
75 return FOUR_PI * r * r;
76 }
77
78
79 @Override
80 public Vector3D project(final Vector3D pt) {
81 return project(pt, Vector3D.Unit.PLUS_X);
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public RegionBSPTree3D toTree(final int subdivisions) {
126 if (subdivisions < 0) {
127 throw new IllegalArgumentException(MessageFormat.format(INVALID_SUBDIVISION_MESSAGE, subdivisions));
128 }
129 return new SphereTreeApproximationBuilder(this, subdivisions).build();
130 }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public TriangleMesh toTriangleMesh(final int subdivisions) {
173 if (subdivisions < 0) {
174 throw new IllegalArgumentException(MessageFormat.format(INVALID_SUBDIVISION_MESSAGE, subdivisions));
175 }
176 return new SphereMeshApproximationBuilder(this, subdivisions).build();
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public List<Vector3D> intersections(final Line3D line) {
196 return intersections(line, Line3D::abscissa, Line3D::distance);
197 }
198
199
200
201
202
203
204
205
206 public Vector3D firstIntersection(final Line3D line) {
207 return firstIntersection(line, Line3D::abscissa, Line3D::distance);
208 }
209
210
211 @Override
212 public List<LinecastPoint3D> linecast(final LineConvexSubset3D subset) {
213 return getLinecastStream(subset)
214 .collect(Collectors.toList());
215 }
216
217
218 @Override
219 public LinecastPoint3D linecastFirst(final LineConvexSubset3D subset) {
220 return getLinecastStream(subset)
221 .findFirst()
222 .orElse(null);
223 }
224
225
226
227
228
229
230 private Stream<LinecastPoint3D> getLinecastStream(final LineConvexSubset3D subset) {
231 return intersections(subset.getLine()).stream()
232 .filter(subset::contains)
233 .map(pt -> new LinecastPoint3D(pt, getCenter().directionTo(pt), subset.getLine()));
234 }
235
236
237
238
239
240
241
242
243
244 public static Sphere from(final Vector3D center, final double radius, final Precision.DoubleEquivalence precision) {
245 return new Sphere(center, radius, precision);
246 }
247
248
249
250
251
252 private static final class SphereTreeApproximationBuilder {
253
254
255 private static final int PARTITION_THRESHOLD = 2;
256
257
258 private final Sphere sphere;
259
260
261 private final int subdivisions;
262
263
264
265
266
267 SphereTreeApproximationBuilder(final Sphere sphere, final int subdivisions) {
268 this.sphere = sphere;
269 this.subdivisions = subdivisions;
270 }
271
272
273
274
275
276 RegionBSPTree3D build() {
277 final RegionBSPTree3D tree = RegionBSPTree3D.empty();
278
279 final Vector3D center = sphere.getCenter();
280 final double radius = sphere.getRadius();
281 final Precision.DoubleEquivalence precision = sphere.getPrecision();
282
283
284 final Plane plusXPlane = Planes.fromPointAndNormal(center, Vector3D.Unit.PLUS_X, precision);
285 final Plane plusYPlane = Planes.fromPointAndNormal(center, Vector3D.Unit.PLUS_Y, precision);
286 final Plane plusZPlane = Planes.fromPointAndNormal(center, Vector3D.Unit.PLUS_Z, precision);
287
288 tree.insert(plusXPlane.span(), RegionCutRule.INHERIT);
289 tree.insert(plusYPlane.span(), RegionCutRule.INHERIT);
290 tree.insert(plusZPlane.span(), RegionCutRule.INHERIT);
291
292
293 final double cx = center.getX();
294 final double cy = center.getY();
295 final double cz = center.getZ();
296
297 final Vector3D maxX = Vector3D.of(cx + radius, cy, cz);
298 final Vector3D minX = Vector3D.of(cx - radius, cy, cz);
299
300 final Vector3D maxY = Vector3D.of(cx, cy + radius, cz);
301 final Vector3D minY = Vector3D.of(cx, cy - radius, cz);
302
303 final Vector3D maxZ = Vector3D.of(cx, cy, cz + radius);
304 final Vector3D minZ = Vector3D.of(cx, cy, cz - radius);
305
306
307 final RegionNode3D root = tree.getRoot();
308
309 try {
310 partitionAndInsert(root.getMinus().getMinus().getMinus(), minX, minZ, minY, 0);
311 partitionAndInsert(root.getMinus().getMinus().getPlus(), minX, minY, maxZ, 0);
312
313 partitionAndInsert(root.getMinus().getPlus().getMinus(), minX, maxY, minZ, 0);
314 partitionAndInsert(root.getMinus().getPlus().getPlus(), minX, maxZ, maxY, 0);
315
316 partitionAndInsert(root.getPlus().getMinus().getMinus(), maxX, minY, minZ, 0);
317 partitionAndInsert(root.getPlus().getMinus().getPlus(), maxX, maxZ, minY, 0);
318
319 partitionAndInsert(root.getPlus().getPlus().getMinus(), maxX, minZ, maxY, 0);
320 partitionAndInsert(root.getPlus().getPlus().getPlus(), maxX, maxY, maxZ, 0);
321 } catch (final IllegalStateException | IllegalArgumentException exc) {
322
323 throw new IllegalStateException("Failed to construct sphere approximation with subdivision count " +
324 subdivisions + ": " + exc.getMessage(), exc);
325 }
326
327 return tree;
328 }
329
330
331
332
333
334
335
336
337
338
339 private void partitionAndInsert(final RegionNode3D node,
340 final Vector3D p1, final Vector3D p2, final Vector3D p3, final int level) {
341
342 if (subdivisions - level > PARTITION_THRESHOLD) {
343 final int nextLevel = level + 1;
344
345 final Vector3D center = sphere.getCenter();
346
347 final Vector3D m1 = sphere.project(p1.lerp(p2, 0.5));
348 final Vector3D m2 = sphere.project(p2.lerp(p3, 0.5));
349 final Vector3D m3 = sphere.project(p3.lerp(p1, 0.5));
350
351 RegionNode3D curNode = node;
352
353 checkedCut(curNode, createPlane(m3, m2, center), RegionCutRule.INHERIT);
354 partitionAndInsert(curNode.getPlus(), m3, m2, p3, nextLevel);
355
356 curNode = curNode.getMinus();
357 checkedCut(curNode, createPlane(m2, m1, center), RegionCutRule.INHERIT);
358 partitionAndInsert(curNode.getPlus(), m1, p2, m2, nextLevel);
359
360 curNode = curNode.getMinus();
361 checkedCut(curNode, createPlane(m1, m3, center), RegionCutRule.INHERIT);
362 partitionAndInsert(curNode.getPlus(), p1, m1, m3, nextLevel);
363
364 partitionAndInsert(curNode.getMinus(), m1, m2, m3, nextLevel);
365 } else {
366 insertSubdividedTriangles(node, p1, p2, p3, level);
367 }
368 }
369
370
371
372
373
374
375
376
377
378
379 private RegionNode3D insertSubdividedTriangles(final RegionNode3D node,
380 final Vector3D p1, final Vector3D p2, final Vector3D p3,
381 final int level) {
382
383 if (level >= subdivisions) {
384
385 checkedCut(node, createPlane(p1, p2, p3), RegionCutRule.MINUS_INSIDE);
386 return node.getMinus();
387 } else {
388 final int nextLevel = level + 1;
389
390 final Vector3D m1 = sphere.project(p1.lerp(p2, 0.5));
391 final Vector3D m2 = sphere.project(p2.lerp(p3, 0.5));
392 final Vector3D m3 = sphere.project(p3.lerp(p1, 0.5));
393
394 RegionNode3D curNode = node;
395 curNode = insertSubdividedTriangles(curNode, p1, m1, m3, nextLevel);
396 curNode = insertSubdividedTriangles(curNode, m1, p2, m2, nextLevel);
397 curNode = insertSubdividedTriangles(curNode, m3, m2, p3, nextLevel);
398 curNode = insertSubdividedTriangles(curNode, m1, m2, m3, nextLevel);
399
400 return curNode;
401 }
402 }
403
404
405
406
407
408
409
410 private Plane createPlane(final Vector3D p1, final Vector3D p2, final Vector3D p3) {
411 return Planes.fromPoints(p1, p2, p3, sphere.getPrecision());
412 }
413
414
415
416
417
418
419
420
421 private void checkedCut(final RegionNode3D node, final Plane cutter, final RegionCutRule cutRule) {
422 if (!node.insertCut(cutter, cutRule)) {
423 throw new IllegalStateException("Failed to cut BSP tree node with plane: " + cutter);
424 }
425 }
426 }
427
428
429
430
431 private static final class SphereMeshApproximationBuilder {
432
433
434 private final Sphere sphere;
435
436
437 private final int subdivisions;
438
439
440 private final SimpleTriangleMesh.Builder builder;
441
442
443
444
445
446 SphereMeshApproximationBuilder(final Sphere sphere, final int subdivisions) {
447 this.sphere = sphere;
448 this.subdivisions = subdivisions;
449 this.builder = SimpleTriangleMesh.builder(sphere.getPrecision());
450 }
451
452
453
454
455 public SimpleTriangleMesh build() {
456 final Vector3D center = sphere.getCenter();
457 final double radius = sphere.getRadius();
458
459
460 final double cx = center.getX();
461 final double cy = center.getY();
462 final double cz = center.getZ();
463
464 final Vector3D maxX = Vector3D.of(cx + radius, cy, cz);
465 final Vector3D minX = Vector3D.of(cx - radius, cy, cz);
466
467 final Vector3D maxY = Vector3D.of(cx, cy + radius, cz);
468 final Vector3D minY = Vector3D.of(cx, cy - radius, cz);
469
470 final Vector3D maxZ = Vector3D.of(cx, cy, cz + radius);
471 final Vector3D minZ = Vector3D.of(cx, cy, cz - radius);
472
473 addSubdivided(minX, minZ, minY, 0);
474 addSubdivided(minX, minY, maxZ, 0);
475
476 addSubdivided(minX, maxY, minZ, 0);
477 addSubdivided(minX, maxZ, maxY, 0);
478
479 addSubdivided(maxX, minY, minZ, 0);
480 addSubdivided(maxX, maxZ, minY, 0);
481
482 addSubdivided(maxX, minZ, maxY, 0);
483 addSubdivided(maxX, maxY, maxZ, 0);
484
485 return builder.build();
486 }
487
488
489
490
491
492
493
494 private void addSubdivided(final Vector3D p1, final Vector3D p2, final Vector3D p3, final int level) {
495 if (level >= subdivisions) {
496
497 builder.addFaceUsingVertices(p1, p2, p3);
498 } else {
499
500 final int nextLevel = level + 1;
501
502 final Vector3D m1 = sphere.project(p1.lerp(p2, 0.5));
503 final Vector3D m2 = sphere.project(p2.lerp(p3, 0.5));
504 final Vector3D m3 = sphere.project(p3.lerp(p1, 0.5));
505
506 addSubdivided(p1, m1, m3, nextLevel);
507 addSubdivided(m1, p2, m2, nextLevel);
508 addSubdivided(m3, m2, p3, nextLevel);
509 addSubdivided(m1, m2, m3, nextLevel);
510 }
511 }
512 }
513 }