1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.euclidean.threed.line;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.geometry.core.Transform;
23 import org.apache.commons.geometry.euclidean.oned.Interval;
24 import org.apache.commons.geometry.euclidean.oned.RegionBSPTree1D;
25 import org.apache.commons.geometry.euclidean.oned.Vector1D;
26 import org.apache.commons.geometry.euclidean.threed.Bounds3D;
27 import org.apache.commons.geometry.euclidean.threed.Vector3D;
28 import org.apache.commons.geometry.euclidean.threed.line.Line3D.SubspaceTransform;
29
30
31
32
33
34
35
36 public final class EmbeddedTreeLineSubset3D extends LineSubset3D {
37
38 private final RegionBSPTree1D region;
39
40
41
42
43 public EmbeddedTreeLineSubset3D(final Line3D line) {
44 this(line, false);
45 }
46
47
48
49
50
51
52
53
54 public EmbeddedTreeLineSubset3D(final Line3D line, final boolean full) {
55 this(line, new RegionBSPTree1D(full));
56 }
57
58
59
60
61
62 public EmbeddedTreeLineSubset3D(final Line3D line, final RegionBSPTree1D region) {
63 super(line);
64
65 this.region = region;
66 }
67
68
69 @Override
70 public double getSize() {
71 return region.getSize();
72 }
73
74
75 @Override
76 public RegionBSPTree1D getSubspaceRegion() {
77 return region;
78 }
79
80
81 @Override
82 public Vector3D getCentroid() {
83 final Vector1D subcenter = region.getCentroid();
84 return subcenter != null ?
85 getLine().toSpace(subcenter) :
86 null;
87 }
88
89
90 @Override
91 public Bounds3D getBounds() {
92 final double min = region.getMin();
93 final double max = region.getMax();
94
95 if (Double.isFinite(min) && Double.isFinite(max)) {
96 final Line3D line = getLine();
97
98 return Bounds3D.builder()
99 .add(line.toSpace(min))
100 .add(line.toSpace(max))
101 .build();
102 }
103
104 return null;
105 }
106
107
108
109
110
111 public EmbeddedTreeLineSubset3D transform(final Transform<Vector3D> transform) {
112 final SubspaceTransform st = getLine().subspaceTransform(transform);
113
114 final RegionBSPTree1D tRegion = RegionBSPTree1D.empty();
115 tRegion.copy(region);
116 tRegion.transform(st.getTransform());
117
118 return new EmbeddedTreeLineSubset3D(st.getLine(), tRegion);
119 }
120
121
122
123
124
125
126 public List<LineConvexSubset3D> toConvex() {
127 final List<Interval> intervals = region.toIntervals();
128
129 final Line3D line = getLine();
130 final List<LineConvexSubset3D> convex = new ArrayList<>(intervals.size());
131
132 for (final Interval interval : intervals) {
133 convex.add(Lines3D.subsetFromInterval(line, interval));
134 }
135
136 return convex;
137 }
138
139
140 @Override
141 public String toString() {
142 final Line3D line = getLine();
143
144 final StringBuilder sb = new StringBuilder();
145 sb.append(this.getClass().getSimpleName())
146 .append('[')
147 .append("lineOrigin= ")
148 .append(line.getOrigin())
149 .append(", lineDirection= ")
150 .append(line.getDirection())
151 .append(", region= ")
152 .append(region)
153 .append(']');
154
155 return sb.toString();
156 }
157 }