1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.core.partitioning.test;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22
23 import org.apache.commons.geometry.core.RegionLocation;
24 import org.apache.commons.geometry.core.Transform;
25 import org.apache.commons.geometry.core.partitioning.Hyperplane;
26 import org.apache.commons.geometry.core.partitioning.HyperplaneConvexSubset;
27 import org.apache.commons.geometry.core.partitioning.HyperplaneSubset;
28 import org.apache.commons.geometry.core.partitioning.Split;
29
30
31
32
33 public final class TestLineSegmentCollection implements HyperplaneSubset<TestPoint2D> {
34
35
36 private final List<TestLineSegment> segments;
37
38
39
40
41
42
43 public TestLineSegmentCollection(final List<TestLineSegment> segments) {
44 this.segments = Collections.unmodifiableList(new ArrayList<>(segments));
45 }
46
47
48
49
50 public List<TestLineSegment> getLineSegments() {
51 return segments;
52 }
53
54
55 @Override
56 public Hyperplane<TestPoint2D> getHyperplane() {
57 throw new UnsupportedOperationException();
58 }
59
60
61 @Override
62 public boolean isFull() {
63 for (final TestLineSegment seg : segments) {
64 if (seg.isFull()) {
65 return true;
66 }
67 }
68 return false;
69 }
70
71
72 @Override
73 public boolean isEmpty() {
74 for (final TestLineSegment seg : segments) {
75 if (!seg.isEmpty()) {
76 return false;
77 }
78 }
79 return true;
80 }
81
82
83 @Override
84 public boolean isInfinite() {
85 for (final TestLineSegment seg : segments) {
86 if (seg.isInfinite()) {
87 return true;
88 }
89 }
90 return false;
91 }
92
93
94 @Override
95 public boolean isFinite() {
96 return !isInfinite();
97 }
98
99
100 @Override
101 public double getSize() {
102 double size = 0.0;
103
104 for (final TestLineSegment seg : segments) {
105 size += seg.getSize();
106 }
107
108 return size;
109 }
110
111
112 @Override
113 public TestPoint2D getCentroid() {
114 throw new UnsupportedOperationException();
115 }
116
117
118 @Override
119 public Split<TestLineSegmentCollection> split(final Hyperplane<TestPoint2D> splitter) {
120 final List<TestLineSegment> minusList = new ArrayList<>();
121 final List<TestLineSegment> plusList = new ArrayList<>();
122
123 for (final TestLineSegment segment : segments) {
124 final Split<TestLineSegment> split = segment.split(splitter);
125
126 if (split.getMinus() != null) {
127 minusList.add(split.getMinus());
128 }
129
130 if (split.getPlus() != null) {
131 plusList.add(split.getPlus());
132 }
133 }
134
135 final TestLineSegmentCollection minus = minusList.isEmpty() ?
136 null :
137 new TestLineSegmentCollection(minusList);
138
139 final TestLineSegmentCollection plus = plusList.isEmpty() ?
140 null :
141 new TestLineSegmentCollection(plusList);
142
143 return new Split<>(minus, plus);
144 }
145
146
147 @Override
148 public RegionLocation classify(final TestPoint2D point) {
149
150
151
152 for (final TestLineSegment seg : segments) {
153 final RegionLocation loc = seg.classify(point);
154 if (loc != RegionLocation.OUTSIDE) {
155 return loc;
156 }
157 }
158
159 return RegionLocation.OUTSIDE;
160 }
161
162
163 @Override
164 public TestPoint2D closest(final TestPoint2D point) {
165 TestPoint2D closest = null;
166 double minDist = -1;
167
168 for (final TestLineSegment seg : segments) {
169 final TestPoint2D pt = seg.closest(point);
170 final double dist = pt.distance(point);
171 if (minDist < 0 || dist < minDist) {
172 minDist = dist;
173 closest = pt;
174 }
175 }
176
177 return closest;
178
179 }
180
181
182 @Override
183 public List<HyperplaneConvexSubset<TestPoint2D>> toConvex() {
184 return new ArrayList<>(segments);
185 }
186
187
188 @Override
189 public HyperplaneSubset<TestPoint2D> transform(final Transform<TestPoint2D> transform) {
190 throw new UnsupportedOperationException();
191 }
192 }