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 * http://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.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 /** Class that performs linecast operations against arbitrary {@link BoundarySource3D}
30 * instances. This class performs a brute-force computation of the intersections of the
31 * line or line convex subset against all boundaries. Some data structures may support more
32 * efficient algorithms and should therefore prefer those instead.
33 */
34 final class BoundarySourceLinecaster3D implements Linecastable3D {
35
36 /** The boundary source instance providing boundaries for the linecast operation. */
37 private final BoundarySource3D boundarySrc;
38
39 /** Construct a new instance for linecasting against the given boundary source.
40 * @param boundarySrc boundary source to linecast against.
41 */
42 BoundarySourceLinecaster3D(final BoundarySource3D boundarySrc) {
43 this.boundarySrc = boundarySrc;
44 }
45
46 /** {@inheritDoc} */
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 /** {@inheritDoc} */
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 /** Return a stream containing intersections between the boundary source and the
68 * given line convex subset.
69 * @param subset line subset to intersect
70 * @return a stream containing linecast intersections
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 /** Compute the intersection between a boundary plane subset and line subset. Null is
79 * returned if no intersection is discovered.
80 * @param planeSubset plane subset from the boundary source
81 * @param lineSubset line subset to intersect with
82 * @return the linecast intersection between the two arguments or null if there is no such
83 * intersection
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; // no intersection
96 }
97 }