-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathGeometry.java
More file actions
438 lines (380 loc) · 11.6 KB
/
Geometry.java
File metadata and controls
438 lines (380 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package nodebox.graphics;
import com.google.common.base.Function;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Geometry extends AbstractGeometry implements Colorizable {
private ArrayList<Path> paths;
private Path currentPath;
private boolean lengthDirty = true;
private ArrayList<Double> pathLengths;
private double groupLength;
public Geometry() {
paths = new ArrayList<Path>();
currentPath = null;
}
public Geometry(Geometry other) {
paths = new ArrayList<Path>(other.paths.size());
for (Path path : other.paths) {
paths.add(path.clone());
}
// TODO: We might want to refer to the latest Path object in the items.
currentPath = null;
}
//// Container operations ////
/**
* Get the subshapes of a geometry object.
* <p/>
* This method returns live references to the geometric objects.
* Changing them will change the original geometry.
*
* @return a list of primitives
*/
public java.util.List<Path> getPaths() {
return paths;
}
/**
* Add geometry to the group.
* <p/>
* Added geometry is not cloned.
*
* @param path the geometry to add.
*/
public void add(Path path) {
paths.add(path);
currentPath = path;
invalidate(false);
}
/**
* Convenience function that extends the current geometry with the given geometry.
* <p/>
* Alias for extend().
*
* @param geometry the geometry to add.
* @see #extend(Geometry)
*/
public void add(Geometry geometry) {
extend(geometry);
}
public int size() {
return paths.size();
}
/**
* Check if the group contains any paths.
* This method does not check if the paths themselves are empty.
*
* @return true if the group contains no paths.
*/
public boolean isEmpty() {
return paths.isEmpty();
}
public void clear() {
paths.clear();
currentPath = null;
invalidate(false);
}
/**
* Create copies of all paths in the given group and append them to myself.
*
* @param g the group whose paths are appended.
*/
public void extend(Geometry g) {
for (Path path : g.paths) {
paths.add(path.clone());
}
invalidate(false);
}
/**
* Check if the last path in this group is closed.
* <p/>
* A group (or path) can't technically be called "closed", only specific contours in the path can.
* This method provides a reasonable heuristic for a "closed" group by checking the closed state
* of the last contour on the last path. It returns false if this path contains no contours.
*
* @return true if the last contour on the last path is closed.
*/
public boolean isClosed() {
if (isEmpty()) return false;
Path lastPath = paths.get(paths.size() - 1);
return lastPath.isClosed();
}
//// Color operations ////
public void setFillColor(Color fillColor) {
for (Path path : paths) {
path.setFillColor(fillColor);
}
}
public void setFill(Color c) {
setFillColor(c);
}
public void setStrokeColor(Color strokeColor) {
for (Path path : paths) {
path.setStrokeColor(strokeColor);
}
}
public void setStroke(Color c) {
setStrokeColor(c);
}
public void setStrokeWidth(double strokeWidth) {
for (Path path : paths) {
path.setStrokeWidth(strokeWidth);
}
}
//// Point operations ////
public int getPointCount() {
int pointCount = 0;
for (Path path : paths) {
pointCount += path.getPointCount();
}
return pointCount;
}
/**
* Get the points for this geometry.
* <p/>
* This returns a live reference to the points of the geometry. Changing the points will change the geometry.
*
* @return a list of Points.
*/
public java.util.List<Point> getPoints() {
ArrayList<Point> points = new ArrayList<Point>();
for (Path path : paths) {
points.addAll(path.getPoints());
}
return points;
}
public void addPoint(Point pt) {
ensureCurrentPath();
currentPath.addPoint(pt);
invalidate(false);
}
public void addPoint(double x, double y) {
ensureCurrentPath();
currentPath.addPoint(x, y);
invalidate(false);
}
private void ensureCurrentPath() {
if (currentPath != null) return;
currentPath = new Path();
add(currentPath);
}
/**
* Invalidates the cache. Querying the path length or asking for getGeneralPath will return an up-to-date result.
* <p/>
* This operation recursively invalidates all underlying geometry.
* <p/>
* Cache invalidation happens automatically when using the Path methods, such as rect/ellipse,
* or container operations such as add/extend/clear. You should invalidate the cache when manually changing the
* point positions or adding points to the underlying contours.
* <p/>
* Invalidating the cache is a lightweight operation; it doesn't recalculate anything. Only when querying the
* new length will the values be recalculated.
*/
public void invalidate() {
invalidate(true);
}
private void invalidate(boolean recursive) {
lengthDirty = true;
if (recursive) {
for (Path path : paths) {
path.invalidate();
}
}
}
//// Geometric queries ////
/**
* Returns the bounding box of all elements in the group.
*
* @return a bounding box that contains all elements in the group.
*/
public Rect getBounds() {
if (isEmpty()) return new Rect();
Rect r = null;
for (Grob g : paths) {
if (r == null) {
r = g.getBounds();
}
if (!g.isEmpty()) {
r = r.united(g.getBounds());
}
}
return r != null ? r : new Rect();
}
//// Geometric math ////
/**
* Calculate the length of the path. This is not the number of segments, but rather the sum of all segment lengths.
*
* @return the length of the path.
*/
public double getLength() {
if (lengthDirty) {
updatePathLengths();
}
return groupLength;
}
private void updatePathLengths() {
pathLengths = new ArrayList<Double>(paths.size());
groupLength = 0;
double length;
for (Path p : paths) {
length = p.getLength();
pathLengths.add(length);
groupLength += length;
}
lengthDirty = false;
}
/**
* Returns coordinates for point at t on the group.
* <p/>
* Gets the length of the group, based on the length
* of each path in the group.
* Determines in what path t falls.
* Gets the point on that path.
*
* @param t relative coordinate of the point (between 0.0 and 1.0).
* Results outside of this range are undefined.
* @return coordinates for point at t.
*/
public Point pointAt(double t) {
double length = getLength();
// Since t is relative, convert it to the absolute length.
double absT = t * length;
// The resT is what remains of t after we traversed all segments.
double resT = t;
// Find the contour that contains t.
double cLength;
Path currentPath = null;
for (Path p : paths) {
currentPath = p;
cLength = p.getLength();
if (absT <= cLength) break;
absT -= cLength;
resT -= cLength / length;
}
if (currentPath == null) return Point.ZERO;
resT /= (currentPath.getLength() / length);
return currentPath.pointAt(resT);
}
//// Geometric queries ////
public boolean contains(Point pt) {
for (Path p : paths) {
if (p.contains(pt)) {
return true;
}
}
return false;
}
public boolean contains(double x, double y) {
for (Path p : paths) {
if (p.contains(x, y)) {
return true;
}
}
return false;
}
public boolean contains(Rect r) {
for (Path p : paths) {
if (p.contains(r)) {
return true;
}
}
return false;
}
//// Geometric operations ////
public boolean intersects(Geometry g2) {
for (Path p1 : getPaths()) {
for (Path p2 : g2.getPaths()) {
if (p1.intersects(p2)) return true;
}
}
return false;
}
public boolean intersects(Path p) {
for (Path p1 : getPaths()) {
if (p1.intersects(p)) return true;
}
return false;
}
public Point[] makePoints(int amount, boolean perContour) {
if (perContour) {
ArrayList<Point> points = new ArrayList<Point>();
for (Path p : getPaths()) {
for (Contour c : p.getContours()) {
Point[] pointsFromContour = c.makePoints(amount);
points.addAll(Arrays.asList(pointsFromContour));
}
}
return (Point[]) points.toArray();
} else {
// Distribute all points evenly along the combined length of the contours.
double delta = pointDelta(amount, isClosed());
Point[] points = new Point[amount];
for (int i = 0; i < amount; i++) {
points[i] = pointAt(delta * i);
}
return points;
}
}
public Geometry resampleByAmount(int amount, boolean perContour) {
if (perContour) {
Geometry g = new Geometry();
for (Path p : paths) {
g.add(p.resampleByAmount(amount, true));
}
return g;
} else {
Geometry g = new Geometry();
double delta = pointDelta(amount, isClosed());
for (int i = 0; i < amount; i++) {
g.addPoint(pointAt(delta * i));
}
if (isClosed() && g.paths.size() == 1) {
g.paths.get(0).close();
g.invalidate();
}
return g;
}
}
public Geometry resampleByLength(double segmentLength) {
Geometry g = new Geometry();
for (Path p : paths) {
g.add(p.resampleByLength(segmentLength));
}
return g;
}
//// Transformations ////
public void transform(Transform t) {
for (Path path : paths) {
path.transform(t);
}
invalidate(true);
}
//// Drawing operations ////
public void draw(Graphics2D g) {
for (Grob grob : paths) {
grob.draw(g);
}
}
public void flatten() {
throw new UnsupportedOperationException();
}
public IGeometry flattened() {
throw new UnsupportedOperationException();
}
//// Functional operations ////
public AbstractGeometry mapPoints(Function<Point, Point> pointFunction) {
Geometry newGeometry = new Geometry();
for (Path p : getPaths()) {
Path newPath = (Path) p.mapPoints(pointFunction);
newGeometry.add(newPath);
}
return newGeometry;
}
//// Object methods ////
public Geometry clone() {
return new Geometry(this);
}
@Override
public String toString() {
return "<Geometry>";
}
}