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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Denis M. Kishenko
* @author Sven Gothel
*/
package com.jogamp.graph.geom.plane;
import java.io.PrintStream;
import java.util.NoSuchElementException;
import com.jogamp.opengl.math.geom.AABBox;
/**
* Path2F represents and provides construction method for a 2D shape using float[2] points.
*/
public final class Path2F implements Cloneable {
static final String invalidWindingRuleValue = "Invalid winding rule value";
static final String iteratorOutOfBounds = "Iterator out of bounds";
/** A Path2D segment type */
public static enum SegmentType {
MOVETO(1),
LINETO(1),
QUADTO(2),
CUBICTO(3),
CLOSE(0);
/** Number of points associated with this segment type */
public final int point_count;
/** Return the integer segment type value as a byte */
public byte integer() {
return (byte) this.ordinal();
}
/** Return the SegmentType associated with the integer segment type */
public static SegmentType valueOf(final int type) {
switch( type ) {
case 0: return MOVETO;
case 1: return LINETO;
case 2: return QUADTO;
case 3: return CUBICTO;
case 4: return CLOSE;
default:
throw new IllegalArgumentException("Unhandled Segment Type: "+type);
}
}
/** Return the number of points associated with the integer segment type */
public static int getPointCount(final int type) {
switch( type ) {
case 0: return MOVETO.point_count;
case 1: return LINETO.point_count;
case 2: return QUADTO.point_count;
case 3: return CUBICTO.point_count;
case 4: return CLOSE.point_count;
default:
throw new IllegalArgumentException("Unhandled Segment Type: "+type);
}
}
SegmentType(final int v) {
this.point_count = v;
}
}
/**
* The buffers size
*/
private static final int BUFFER_SIZE = 10;
/**
* The buffers capacity
*/
private static final int BUFFER_CAPACITY = 10;
/**
* The point's types buffer
*/
private byte[] m_types;
/**
* The points buffer
*/
private float[] m_points;
/**
* The point's type buffer size
*/
private int m_typeSize;
/**
* The points buffer size
*/
private int m_pointSize;
/**
* The winding path rule
*/
private WindingRule m_rule;
/*
* GeneralPath path iterator
*/
public static final class Iterator {
/**
* The source GeneralPath object
*/
private final Path2F p;
/**
* The path iterator transformation
*/
private final AffineTransform t;
/**
* The current cursor position in types buffer
*/
private int typeIndex;
/**
* The current cursor position in points buffer
*/
private int pointIndex;
/**
* Constructs a new GeneralPath.Iterator for given general path
* @param path - the source GeneralPath object
*/
Iterator(final Path2F path) {
this(path, null);
}
/**
* Constructs a new GeneralPath.Iterator for given general path and transformation
* @param path - the source GeneralPath object
* @param at - the AffineTransform object to apply rectangle path
*/
public Iterator(final Path2F path, final AffineTransform at) {
this.p = path;
this.t = at;
reset();
}
private void reset() {
typeIndex = 0;
pointIndex = 0;
}
/** Return the {@link WindingRule} set */
public WindingRule getWindingRule() {
return p.getWindingRule();
}
/**
* Compute the general winding of the vertices
* @return CCW or CW {@link Winding}
*/
public Winding getWinding() {
return area() >= 0 ? Winding.CCW : Winding.CW ;
}
/** Returns reference of the point array covering the whole iteration of Path2D, use {@link #index()} to access the current point. */
public float[] points() { return p.m_points; }
/** Return the {@link #points()} index for the current segment. */
public int index() { return pointIndex; }
/** Return current segment type */
public SegmentType getType() { return SegmentType.valueOf( p.m_types[typeIndex] ); }
/**
* Return the current segment type and copies the current segment's points to given storage
* @param coords storage for current segment's points
* @return current segment type
* @see #points()
* @see #type_index()
* @see #getType()
* @deprecated try to use {@link #index()}, {@link #points()} and {@link #next()} to avoid copying
*/
@Deprecated
public SegmentType currentSegment(final float[] coords) {
if (!hasNext()) {
throw new NoSuchElementException(iteratorOutOfBounds);
}
final SegmentType type = getType();
final int count = type.point_count;
System.arraycopy(p.m_points, pointIndex, coords, 0, count*2);
if (t != null) {
t.transform(coords, 0, coords, 0, count);
}
return type;
}
/** Returns true if the iteration has more elements. */
public boolean hasNext() {
return typeIndex < p.m_typeSize;
}
/** Returns the current segment type in the iteration, then moving to the next path segment. */
public SegmentType next() {
final SegmentType t = getType();
pointIndex += 2 * t.point_count;
++typeIndex;
return t;
}
/**
* Computes the area of the path to check if ccw
* @return positive area if ccw else negative area value
*/
private float area() {
float area = 0.0f;
final float[] points = points();
final float[] pCoord = new float[2];
while ( hasNext() ) {
final int idx = index();
final SegmentType type = next();
switch ( type ) {
case MOVETO:
pCoord[0] = points[idx+0];
pCoord[1] = points[idx+1];
break;
case LINETO:
area += pCoord[0] * points[idx+1] - points[idx+0] * pCoord[1];
pCoord[0] = points[idx+0];
pCoord[1] = points[idx+1];
break;
case QUADTO:
area += pCoord[0] * points[idx+1] - points[idx+0] * pCoord[1];
area += points[idx+0] * points[idx+3] - points[idx+2] * points[idx+1];
pCoord[0] = points[idx+2];
pCoord[1] = points[idx+3];
break;
case CUBICTO:
area += pCoord[0] * points[idx+1] - points[idx+0] * pCoord[1];
area += points[idx+0] * points[idx+3] - points[idx+2] * points[idx+1];
area += points[idx+2] * points[idx+5] - points[idx+4] * points[idx+3];
pCoord[0] = points[idx+4];
pCoord[1] = points[idx+5];
break;
case CLOSE:
break;
}
}
reset();
return area;
}
}
public Path2F() {
this(WindingRule.NON_ZERO, BUFFER_SIZE, BUFFER_SIZE);
}
public Path2F(final WindingRule rule) {
this(rule, BUFFER_SIZE, BUFFER_SIZE);
}
public Path2F(final WindingRule rule, final int initialCapacity) {
this(rule, initialCapacity, initialCapacity);
}
public Path2F(final WindingRule rule, final int initialTypeCapacity, final int initialPointCapacity) {
setWindingRule(rule);
m_types = new byte[initialTypeCapacity];
m_points = new float[initialPointCapacity * 2];
}
public Path2F(final Path2F path) {
this(WindingRule.NON_ZERO, BUFFER_SIZE);
final Iterator p = path.iterator(null);
setWindingRule(p.getWindingRule());
append(p, false);
}
/** Set the {@link WindingRule} set */
public void setWindingRule(final WindingRule rule) {
this.m_rule = rule;
}
/** Return the {@link WindingRule} set */
public WindingRule getWindingRule() {
return m_rule;
}
/**
* Checks points and types buffer size to add pointCount points. If necessary realloc buffers to enlarge size.
* @param pointCount - the point count to be added in buffer
*/
private void checkBuf(final int pointCount, final boolean checkMove) {
if (checkMove && m_typeSize == 0) {
throw new IllegalPathStateException("First segment should be SEG_MOVETO type");
}
if (m_typeSize == m_types.length) {
final byte tmp[] = new byte[m_typeSize + BUFFER_CAPACITY];
System.arraycopy(m_types, 0, tmp, 0, m_typeSize);
m_types = tmp;
}
if (m_pointSize + pointCount > m_points.length) {
final float tmp[] = new float[m_pointSize + Math.max(BUFFER_CAPACITY * 2, pointCount)];
System.arraycopy(m_points, 0, tmp, 0, m_pointSize);
m_points = tmp;
}
}
/**
* Start a new position for the next line segment at given point x/y (P1).
* @param x point (P1)
* @param y point (P1)
*/
public void moveTo(final float x, final float y) {
if ( m_typeSize > 0 && m_types[m_typeSize - 1] == SegmentType.MOVETO.integer() ) {
m_points[m_pointSize - 2] = x;
m_points[m_pointSize - 1] = y;
} else {
checkBuf(2, false);
m_types[m_typeSize++] = SegmentType.MOVETO.integer();
m_points[m_pointSize++] = x;
m_points[m_pointSize++] = y;
}
}
/**
* Add a line segment, intersecting the last point and the given point x/y (P1).
* @param x final point (P1)
* @param y final point (P1)
*/
public void lineTo(final float x, final float y) {
checkBuf(2, true);
m_types[m_typeSize++] = SegmentType.LINETO.integer();
m_points[m_pointSize++] = x;
m_points[m_pointSize++] = y;
}
/**
* Add a quadratic curve segment, intersecting the last point and the second given point x2/y2 (P2).
* @param x1 quadratic parametric control point (P1)
* @param y1 quadratic parametric control point (P1)
* @param x2 final interpolated control point (P2)
* @param y2 final interpolated control point (P2)
*/
public void quadTo(final float x1, final float y1, final float x2, final float y2) {
checkBuf(4, true);
m_types[m_typeSize++] = SegmentType.QUADTO.integer();
m_points[m_pointSize++] = x1;
m_points[m_pointSize++] = y1;
m_points[m_pointSize++] = x2;
m_points[m_pointSize++] = y2;
}
/**
* Add a cubic Bézier curve segment, intersecting the last point and the second given point x3/y3 (P3).
* @param x1 Bézier control point (P1)
* @param y1 Bézier control point (P1)
* @param x2 Bézier control point (P2)
* @param y2 Bézier control point (P2)
* @param x3 final interpolated control point (P3)
* @param y3 final interpolated control point (P3)
*/
public void cubicTo(final float x1, final float y1, final float x2, final float y2, final float x3, final float y3) {
checkBuf(6, true);
m_types[m_typeSize++] = SegmentType.CUBICTO.integer();
m_points[m_pointSize++] = x1;
m_points[m_pointSize++] = y1;
m_points[m_pointSize++] = x2;
m_points[m_pointSize++] = y2;
m_points[m_pointSize++] = x3;
m_points[m_pointSize++] = y3;
}
/**
* Closes the current sub-path segment by drawing a straight line back to the coordinates of the last moveTo. If the path is already closed then this method has no effect.
*/
public void closePath() {
if (!isClosed()) {
checkBuf(0, true);
m_types[m_typeSize++] = SegmentType.CLOSE.integer();
}
}
final public int size() {
return m_typeSize;
}
/**
* Returns true if the last sub-path is closed, otherwise false.
*/
final public boolean isClosed() {
return m_typeSize > 0 && m_types[m_typeSize - 1] == SegmentType.CLOSE.integer() ;
}
/**
* Compute the general winding of the vertices
* @param vertices array of Vertices
* @return CCW or CW {@link Winding}
*/
public Winding getWinding() {
return iterator(null).getWinding();
}
@Override
public String toString() {
return "[size "+size()+", closed "+isClosed()+", winding[rule "+getWindingRule()+", "+getWinding()+"]]";
}
/**
* Append the given path geometry to this instance
* @param path the {@link Path2F} to append to this instance
* @param connect pass true to turn an initial moveTo segment into a lineTo segment to connect the new geometry to the existing path, otherwise pass false.
*/
public void append(final Path2F path, final boolean connect) {
append(path.iterator(null), connect);
}
/**
* Append the given path geometry to this instance
* @param path the {@link Path2F.Iterator} to append to this instance
* @param connect pass true to turn an initial moveTo segment into a lineTo segment to connect the new geometry to the existing path, otherwise pass false.
*/
public void append(final Iterator path, boolean connect) {
final float[] points = path.points();
while ( path.hasNext() ) {
final int idx = path.index();
final SegmentType type = path.next();
switch ( type ) {
case MOVETO:
if ( !connect || 0 == m_typeSize ) {
moveTo(points[idx+0], points[idx+1]);
break;
}
if ( m_types[m_typeSize - 1] != SegmentType.CLOSE.integer() &&
m_points[m_pointSize - 2] == points[idx+0] &&
m_points[m_pointSize - 1] == points[idx+1]
)
{
break;
}
// fallthrough: MOVETO -> LINETO
case LINETO:
lineTo(points[idx+0], points[idx+1]);
break;
case QUADTO:
quadTo(points[idx+0], points[idx+1], points[idx+2], points[idx+3]);
break;
case CUBICTO:
cubicTo(points[idx+0], points[idx+1], points[idx+2], points[idx+3], points[idx+4], points[idx+5]);
break;
case CLOSE:
closePath();
break;
}
connect = false;
}
}
public void printSegments(final PrintStream out) {
final Iterator path = iterator();
final float[] points = path.points();
int i = 0;
while ( path.hasNext() ) {
final int idx = path.index();
final SegmentType type = path.next();
switch ( type ) {
case MOVETO:
out.printf("%2d: moveTo(%.4f/%.4f)%n", i, points[idx+0], points[idx+1]);
break;
case LINETO:
out.printf("%2d: lineTo(%.4f/%.4f)%n", i, points[idx+0], points[idx+1]);
break;
case QUADTO:
out.printf("%2d: quadTo(%.4f/%.4f, %.4f/%.4f)%n", i, points[idx+0], points[idx+1], points[idx+2], points[idx+3]);
break;
case CUBICTO:
out.printf("%2d: cubicTo(%.4f/%.4f, %.4f/%.4f, %.4f/%.4f)%n", i, points[idx+0], points[idx+1], points[idx+2], points[idx+3], points[idx+4], points[idx+5]);
break;
case CLOSE:
out.printf("%2d: closePath()%n", i);
break;
}
++i;
}
}
public void reset() {
m_typeSize = 0;
m_pointSize = 0;
}
public void transform(final AffineTransform t) {
t.transform(m_points, 0, m_points, 0, m_pointSize / 2);
}
public Path2F createTransformedShape(final AffineTransform t) {
final Path2F p = (Path2F)clone();
if (t != null) {
p.transform(t);
}
return p;
}
public final synchronized AABBox getBounds2D() {
float rx1, ry1, rx2, ry2;
if (m_pointSize == 0) {
rx1 = ry1 = rx2 = ry2 = 0.0f;
} else {
int i = m_pointSize - 1;
ry1 = ry2 = m_points[i--];
rx1 = rx2 = m_points[i--];
while (i > 0) {
final float y = m_points[i--];
final float x = m_points[i--];
if (x < rx1) {
rx1 = x;
} else
if (x > rx2) {
rx2 = x;
}
if (y < ry1) {
ry1 = y;
} else
if (y > ry2) {
ry2 = y;
}
}
}
return new AABBox(rx1, ry1, 0f, rx2, ry2, 0f);
}
/**
* Checks cross count according to path rule to define is it point inside shape or not.
* @param cross - the point cross count
* @return true if point is inside path, or false otherwise
*/
boolean isInside(final int cross) {
if (m_rule == WindingRule.NON_ZERO) {
return Crossing2F.isInsideNonZero(cross);
}
return Crossing2F.isInsideEvenOdd(cross);
}
public boolean contains(final float px, final float py) {
return isInside(Crossing2F.crossShape(this, px, py));
}
public boolean contains(final float rx, final float ry, final float rw, final float rh) {
final int cross = Crossing2F.intersectShape(this, rx, ry, rw, rh);
return cross != Crossing2F.CROSSING && isInside(cross);
}
public boolean intersects(final float rx, final float ry, final float rw, final float rh) {
final int cross = Crossing2F.intersectShape(this, rx, ry, rw, rh);
return cross == Crossing2F.CROSSING || isInside(cross);
}
public boolean contains(final AABBox r) {
return contains(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight());
}
public boolean intersects(final AABBox r) {
return intersects(r.getMinX(), r.getMinY(), r.getWidth(), r.getHeight());
}
public Iterator iterator() {
return new Iterator(this);
}
public Iterator iterator(final AffineTransform t) {
return new Iterator(this, t);
}
/* public Path2F.Iterator getPathIterator(AffineTransform t, float flatness) {
return new FlatteningPathIterator(getPathIterator(t), flatness);
} */
@Override
public Object clone() {
try {
final Path2F p = (Path2F) super.clone();
p.m_types = m_types.clone();
p.m_points = m_points.clone();
return p;
} catch (final CloneNotSupportedException e) {
throw new InternalError();
}
}
}
|