1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.euclidean.threed;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24
25 import org.apache.commons.geometry.euclidean.threed.line.LineConvexSubset3D;
26 import org.apache.commons.geometry.euclidean.threed.line.LinecastPoint3D;
27 import org.apache.commons.geometry.euclidean.threed.line.Linecastable3D;
28
29
30
31
32
33
34 final class BoundarySourceLinecaster3D implements Linecastable3D {
35
36
37 private final BoundarySource3D boundarySrc;
38
39
40
41
42 BoundarySourceLinecaster3D(final BoundarySource3D boundarySrc) {
43 this.boundarySrc = boundarySrc;
44 }
45
46
47 @Override
48 public List<LinecastPoint3D> linecast(final LineConvexSubset3D subset) {
49 try (Stream<LinecastPoint3D> stream = getIntersectionStream(subset)) {
50
51 final List<LinecastPoint3D> results = stream.collect(Collectors.toCollection(ArrayList::new));
52 LinecastPoint3D.sortAndFilter(results);
53
54 return results;
55 }
56 }
57
58
59 @Override
60 public LinecastPoint3D linecastFirst(final LineConvexSubset3D subset) {
61 try (Stream<LinecastPoint3D> stream = getIntersectionStream(subset)) {
62 return stream.min(LinecastPoint3D.ABSCISSA_ORDER)
63 .orElse(null);
64 }
65 }
66
67
68
69
70
71
72 private Stream<LinecastPoint3D> getIntersectionStream(final LineConvexSubset3D subset) {
73 return boundarySrc.boundaryStream()
74 .map(boundary -> computeIntersection(boundary, subset))
75 .filter(Objects::nonNull);
76 }
77
78
79
80
81
82
83
84
85 private LinecastPoint3D computeIntersection(final PlaneConvexSubset planeSubset,
86 final LineConvexSubset3D lineSubset) {
87 final Vector3D intersectionPt = planeSubset.intersection(lineSubset);
88
89 if (intersectionPt != null) {
90 final Vector3D normal = planeSubset.getPlane().getNormal();
91
92 return new LinecastPoint3D(intersectionPt, normal, lineSubset.getLine());
93 }
94
95 return null;
96 }
97 }