1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.euclidean.twod;
18
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.stream.Stream;
24
25 import org.apache.commons.geometry.core.Transform;
26 import org.apache.commons.geometry.core.partitioning.AbstractConvexHyperplaneBoundedRegion;
27 import org.apache.commons.geometry.core.partitioning.Hyperplane;
28 import org.apache.commons.geometry.core.partitioning.HyperplaneConvexSubset;
29 import org.apache.commons.geometry.core.partitioning.Split;
30 import org.apache.commons.geometry.euclidean.twod.path.InteriorAngleLinePathConnector;
31 import org.apache.commons.geometry.euclidean.twod.path.LinePath;
32 import org.apache.commons.numbers.core.Precision;
33
34
35
36
37 public class ConvexArea extends AbstractConvexHyperplaneBoundedRegion<Vector2D, LineConvexSubset>
38 implements BoundarySource2D {
39
40
41 private static final String NON_CONVEX_PATH_ERROR = "Cannot construct convex polygon from non-convex path: ";
42
43
44 private static final ConvexArea FULL = new ConvexArea(Collections.emptyList());
45
46
47
48
49
50 protected ConvexArea(final List<LineConvexSubset> boundaries) {
51 super(boundaries);
52 }
53
54
55 @Override
56 public Stream<LineConvexSubset> boundaryStream() {
57 return getBoundaries().stream();
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public List<LinePath> getBoundaryPaths() {
75
76
77 return InteriorAngleLinePathConnector.connectMaximized(getBoundaries());
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 public List<Vector2D> getVertices() {
94 final List<LinePath> paths = getBoundaryPaths();
95
96
97
98 if (paths.size() == 1) {
99 final LinePath path = paths.get(0);
100 final List<Vector2D> vertices = path.getVertexSequence();
101
102 if (path.isClosed()) {
103
104 return vertices.subList(0, vertices.size() - 1);
105 }
106 return vertices;
107 }
108
109 return Collections.emptyList();
110 }
111
112
113
114
115
116 public ConvexArea transform(final Transform<Vector2D> transform) {
117 return transformInternal(transform, this, LineConvexSubset.class, ConvexArea::new);
118 }
119
120
121 @Override
122 public LineConvexSubset trim(final HyperplaneConvexSubset<Vector2D> convexSubset) {
123 return (LineConvexSubset) super.trim(convexSubset);
124 }
125
126
127 @Override
128 public double getSize() {
129 if (isFull()) {
130 return Double.POSITIVE_INFINITY;
131 }
132
133 double quadrilateralAreaSum = 0.0;
134
135 for (final LineConvexSubset boundary : getBoundaries()) {
136 if (boundary.isInfinite()) {
137 return Double.POSITIVE_INFINITY;
138 }
139
140 quadrilateralAreaSum += boundary.getStartPoint().signedArea(boundary.getEndPoint());
141 }
142
143 return 0.5 * quadrilateralAreaSum;
144 }
145
146
147 @Override
148 public Vector2D getCentroid() {
149 final List<LineConvexSubset> boundaries = getBoundaries();
150
151 double quadrilateralAreaSum = 0.0;
152 double scaledSumX = 0.0;
153 double scaledSumY = 0.0;
154
155 double signedArea;
156 Vector2D startPoint;
157 Vector2D endPoint;
158
159 for (final LineConvexSubset seg : boundaries) {
160 if (seg.isInfinite()) {
161
162 return null;
163 }
164
165 startPoint = seg.getStartPoint();
166 endPoint = seg.getEndPoint();
167
168 signedArea = startPoint.signedArea(endPoint);
169
170 quadrilateralAreaSum += signedArea;
171
172 scaledSumX += signedArea * (startPoint.getX() + endPoint.getX());
173 scaledSumY += signedArea * (startPoint.getY() + endPoint.getY());
174 }
175
176 if (quadrilateralAreaSum > 0) {
177 return Vector2D.of(scaledSumX, scaledSumY).multiply(1.0 / (3.0 * quadrilateralAreaSum));
178 }
179
180 return null;
181 }
182
183
184 @Override
185 public Split<ConvexArea> split(final Hyperplane<Vector2D> splitter) {
186 return splitInternal(splitter, this, LineConvexSubset.class, ConvexArea::new);
187 }
188
189
190
191 @Override
192 public RegionBSPTree2D toTree() {
193 return RegionBSPTree2D.from(getBoundaries(), true);
194 }
195
196
197
198
199 public static ConvexArea full() {
200 return FULL;
201 }
202
203
204
205
206
207
208
209
210
211 public static ConvexArea convexPolygonFromVertices(final Collection<Vector2D> vertices,
212 final Precision.DoubleEquivalence precision) {
213 return convexPolygonFromPath(LinePath.fromVertexLoop(vertices, precision));
214 }
215
216
217
218
219
220
221 public static ConvexArea convexPolygonFromPath(final LinePath path) {
222
223 if (!path.isClosed()) {
224 throw new IllegalArgumentException("Cannot construct convex polygon from unclosed path: " + path);
225 }
226
227 final List<LineConvexSubset> elements = path.getElements();
228 if (elements.size() < 3) {
229 throw new IllegalArgumentException(
230 "Cannot construct convex polygon from path with less than 3 elements: " + path);
231 }
232
233
234
235 final LineConvexSubset startElement = elements.get(0);
236 final Vector2D startVertex = startElement.getStartPoint();
237 final Precision.DoubleEquivalence precision = startElement.getPrecision();
238
239 Vector2D curVector;
240 Vector2D prevVector = null;
241
242 double signedArea;
243 double totalSignedArea = 0.0;
244
245 LineConvexSubset element;
246
247
248
249 for (int i = 0; i < elements.size() - 1; ++i) {
250 element = elements.get(i);
251
252 curVector = startVertex.vectorTo(element.getEndPoint());
253
254 if (prevVector != null) {
255 signedArea = prevVector.signedArea(curVector);
256 if (precision.lt(signedArea, 0.0)) {
257 throw new IllegalArgumentException(NON_CONVEX_PATH_ERROR + path);
258 }
259
260 totalSignedArea += signedArea;
261 }
262
263 prevVector = curVector;
264 }
265
266 if (precision.lte(totalSignedArea, 0.0)) {
267 throw new IllegalArgumentException(NON_CONVEX_PATH_ERROR + path);
268 }
269
270 return new ConvexArea(elements);
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284 public static ConvexArea fromBounds(final Line... bounds) {
285 return fromBounds(Arrays.asList(bounds));
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299 public static ConvexArea fromBounds(final Iterable<Line> bounds) {
300 final List<LineConvexSubset> subsets =
301 new ConvexRegionBoundaryBuilder<>(LineConvexSubset.class).build(bounds);
302 return subsets.isEmpty() ? full() : new ConvexArea(subsets);
303 }
304 }