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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
/**
* Copyright 2010 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package com.jogamp.graph.curve;
import java.util.ArrayList;
import java.util.Collections;
import com.jogamp.graph.curve.tess.Triangulation;
import com.jogamp.graph.curve.tess.Triangulator;
import com.jogamp.graph.geom.Outline;
import com.jogamp.graph.geom.Triangle;
import com.jogamp.graph.geom.Vertex;
import com.jogamp.opengl.math.VectorUtil;
import com.jogamp.opengl.math.geom.AABBox;
/** A Generic shape objects which is defined by a list of Outlines.
* This Shape can be transformed to Triangulations.
* The list of triangles generated are render-able by a Region object.
* The triangulation produced by this Shape will define the
* closed region defined by the outlines.
*
* One or more OutlineShape Object can be associated to a region
* this is left as a high-level representation of the Objects. For
* optimizations, flexibility requirements for future features.
*
* <br><br>
* Example to creating an Outline Shape:
* <pre>
addVertex(...)
addVertex(...)
addVertex(...)
addEmptyOutline()
addVertex(...)
addVertex(...)
addVertex(...)
* </pre>
*
* The above will create two outlines each with three vertices. By adding these two outlines to
* the OutlineShape, we are stating that the combination of the two outlines represent the shape.
* <br>
*
* To specify that the shape is curved at a region, the on-curve flag should be set to false
* for the vertex that is in the middle of the curved region (if the curved region is defined by 3
* vertices (quadratic curve).
* <br>
* In case the curved region is defined by 4 or more vertices the middle vertices should both have
* the on-curve flag set to false.
*
* <br>Example: <br>
* <pre>
addVertex(0,0, true);
addVertex(0,1, false);
addVertex(1,1, false);
addVertex(1,0, true);
* </pre>
*
* The above snippet defines a cubic nurbs curve where (0,1 and 1,1)
* do not belong to the final rendered shape.
*
* <i>Implementation Notes:</i><br>
* <ul>
* <li> The first vertex of any outline belonging to the shape should be on-curve</li>
* <li> Intersections between off-curved parts of the outline is not handled</li>
* </ul>
*
* @see Outline
* @see Region
*/
public class OutlineShape implements Comparable<OutlineShape> {
/**
* Outline's vertices have undefined state until transformed.
*/
public enum VerticesState {
UNDEFINED(0), QUADRATIC_NURBS(1);
public final int state;
VerticesState(int state){
this.state = state;
}
}
public static final int DIRTY_BOUNDS = 1 << 0;
/**
* Modified shape, requires to update the vertices and triangles, here: vertices.
*/
public static final int DIRTY_VERTICES = 1 << 1;
/**
* Modified shape, requires to update the vertices and triangles, here: triangulation.
*/
public static final int DIRTY_TRIANGLES = 1 << 2;
private final Vertex.Factory<? extends Vertex> vertexFactory;
/** The list of {@link Outline}s that are part of this
* outline shape.
*/
/* pp */ final ArrayList<Outline> outlines;
private final AABBox bbox;
private final ArrayList<Triangle> triangles;
private final ArrayList<Vertex> vertices;
private VerticesState outlineState;
/** dirty bits DIRTY_BOUNDS */
private int dirtyBits;
/** Create a new Outline based Shape
*/
public OutlineShape(Vertex.Factory<? extends Vertex> factory) {
this.vertexFactory = factory;
this.outlines = new ArrayList<Outline>(3);
this.outlines.add(new Outline());
this.outlineState = VerticesState.UNDEFINED;
this.bbox = new AABBox();
this.triangles = new ArrayList<Triangle>();
this.vertices = new ArrayList<Vertex>();
this.dirtyBits = 0;
}
/** Clears all data and reset all states as if this instance was newly created */
public void clear() {
outlines.clear();
outlines.add(new Outline());
outlineState = VerticesState.UNDEFINED;
bbox.reset();
vertices.clear();
triangles.clear();
dirtyBits = 0;
}
/**
* Returns the associated vertex factory of this outline shape
* @return Vertex.Factory object
*/
public final Vertex.Factory<? extends Vertex> vertexFactory() { return vertexFactory; }
public final int getOutlineNumber() {
return outlines.size();
}
/**
* Add a new empty {@link Outline}
* to the end of this shape's outline list.
* <p>If the {@link #getLastOutline()} is empty already, no new one will be added.</p>
*
* After a call to this function all new vertices added
* will belong to the new outline
*/
public final void addEmptyOutline() {
if( !getLastOutline().isEmpty() ) {
outlines.add(new Outline());
}
}
/**
* Appends the {@link Outline} element to the end,
* ensuring a clean tail.
*
* <p>A clean tail is ensured, no double empty Outlines are produced
* and a pre-existing empty outline will be replaced with the given one. </p>
*
* @param outline Outline object to be added
* @throws NullPointerException if the {@link Outline} element is null
*/
public final void addOutline(Outline outline) throws NullPointerException {
addOutline(outlines.size(), outline);
}
/**
* Insert the {@link Outline} element at the given {@code position}.
*
* <p>If the {@code position} indicates the end of this list,
* a clean tail is ensured, no double empty Outlines are produced
* and a pre-existing empty outline will be replaced with the given one. </p>
*
* @param position of the added Outline
* @param outline Outline object to be added
* @throws NullPointerException if the {@link Outline} element is null
* @throws IndexOutOfBoundsException if position is out of range (position < 0 || position > getOutlineNumber())
*/
public final void addOutline(int position, Outline outline) throws NullPointerException, IndexOutOfBoundsException {
if (null == outline) {
throw new NullPointerException("outline is null");
}
if( outlines.size() == position ) {
final Outline lastOutline = getLastOutline();
if( outline.isEmpty() && lastOutline.isEmpty() ) {
return;
}
if( lastOutline.isEmpty() ) {
outlines.set(position-1, outline);
if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) {
bbox.resize(outline.getBounds());
}
// vertices.addAll(outline.getVertices()); // FIXME: can do and remove DIRTY_VERTICES ?
dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES;
return;
}
}
outlines.add(position, outline);
if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) {
bbox.resize(outline.getBounds());
}
dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES;
}
/**
* Insert the {@link OutlineShape} elements of type {@link Outline}, .. at the end of this shape,
* using {@link #addOutline(Outline)} for each element.
* <p>Closes the current last outline via {@link #closeLastOutline()} before adding the new ones.</p>
* @param outlineShape OutlineShape elements to be added.
* @throws NullPointerException if the {@link OutlineShape} is null
* @throws IndexOutOfBoundsException if position is out of range (position < 0 || position > getOutlineNumber())
*/
public final void addOutlineShape(OutlineShape outlineShape) throws NullPointerException {
if (null == outlineShape) {
throw new NullPointerException("OutlineShape is null");
}
closeLastOutline();
for(int i=0; i<outlineShape.getOutlineNumber(); i++) {
addOutline(outlineShape.getOutline(i));
}
}
/**
* Replaces the {@link Outline} element at the given {@code position}.
* <p>Sets the bounding box dirty, hence a next call to {@link #getBounds()} will validate it.</p>
*
* @param position of the replaced Outline
* @param outline replacement Outline object
* @throws NullPointerException if the {@link Outline} element is null
* @throws IndexOutOfBoundsException if position is out of range (position < 0 || position >= getOutlineNumber())
*/
public final void setOutline(int position, Outline outline) throws NullPointerException, IndexOutOfBoundsException {
if (null == outline) {
throw new NullPointerException("outline is null");
}
outlines.set(position, outline);
dirtyBits |= DIRTY_BOUNDS | DIRTY_TRIANGLES | DIRTY_VERTICES;
}
/**
* Removes the {@link Outline} element at the given {@code position}.
* <p>Sets the bounding box dirty, hence a next call to {@link #getBounds()} will validate it.</p>
*
* @param position of the to be removed Outline
* @throws IndexOutOfBoundsException if position is out of range (position < 0 || position >= getOutlineNumber())
*/
public final Outline removeOutline(int position) throws IndexOutOfBoundsException {
dirtyBits |= DIRTY_BOUNDS | DIRTY_TRIANGLES | DIRTY_VERTICES;
return outlines.remove(position);
}
/**
* Get the last added outline to the list
* of outlines that define the shape
* @return the last outline
*/
public final Outline getLastOutline() {
return outlines.get(outlines.size()-1);
}
/**
* Returns the {@code Outline} at {@code position}
* @throws IndexOutOfBoundsException if position is out of range (position < 0 || position >= getOutlineNumber())
*/
public final Outline getOutline(int position) throws IndexOutOfBoundsException {
return outlines.get(position);
}
/**
* Adds a vertex to the last open outline to the shape's tail.
* @param v the vertex to be added to the OutlineShape
*/
public final void addVertex(Vertex v) {
final Outline lo = getLastOutline();
lo.addVertex(v);
if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) {
bbox.resize(lo.getBounds());
}
// vertices.add(v); // FIXME: can do and remove DIRTY_VERTICES ?
dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES;
}
/**
* Adds a vertex to the last open outline to the shape at {@code position}
* @param position indx at which the vertex will be added
* @param v the vertex to be added to the OutlineShape
*/
public final void addVertex(int position, Vertex v) {
final Outline lo = getLastOutline();
lo.addVertex(position, v);
if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) {
bbox.resize(lo.getBounds());
}
dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES;
}
/**
* Add a 2D {@link Vertex} to the last outline by defining the coordinate attribute
* of the vertex. The 2D vertex will be represented as Z=0.
*
* @param x the x coordinate
* @param y the y coordniate
* @param onCurve flag if this vertex is on the final curve or defines a curved region
* of the shape around this vertex.
*/
public final void addVertex(float x, float y, boolean onCurve) {
addVertex(vertexFactory.create(x, y, 0f, onCurve));
}
/**
* Add a 3D {@link Vertex} to the last outline by defining the coordniate attribute
* of the vertex.
* @param x the x coordinate
* @param y the y coordinate
* @param z the z coordinate
* @param onCurve flag if this vertex is on the final curve or defines a curved region
* of the shape around this vertex.
*/
public final void addVertex(float x, float y, float z, boolean onCurve) {
addVertex(vertexFactory.create(x, y, z, onCurve));
}
/**
* Add a vertex to the last outline by passing a float array and specifying the
* offset and length in which. The attributes of the vertex are located.
* The attributes should be continuous (stride = 0).
* Attributes which value are not set (when length less than 3)
* are set implicitly to zero.
* @param coordsBuffer the coordinate array where the vertex attributes are to be picked from
* @param offset the offset in the buffer to the x coordinate
* @param length the number of attributes to pick from the buffer (maximum 3)
* @param onCurve flag if this vertex is on the final curve or defines a curved region
* of the shape around this vertex.
*/
public final void addVertex(float[] coordsBuffer, int offset, int length, boolean onCurve) {
addVertex(vertexFactory.create(coordsBuffer, offset, length, onCurve));
}
/**
* Closes the last outline in the shape.
* <p>If last vertex is not equal to first vertex.
* A new temp vertex is added at the end which
* is equal to the first.</p>
*/
public final void closeLastOutline() {
if( getLastOutline().setClosed(true) ) {
dirtyBits |= DIRTY_TRIANGLES | DIRTY_VERTICES;
}
}
/**
* Return the outline's vertices state, {@link OutlineShape.VerticesState}
*/
public final VerticesState getOutlineState() {
return outlineState;
}
/**
* Ensure the outlines represent
* the specified destinationType.
* and removes all overlaps in boundary triangles
* @param destinationType the target outline's vertices state. Currently only
* {@link OutlineShape.VerticesState#QUADRATIC_NURBS} are supported.
*/
protected final void transformOutlines(VerticesState destinationType) {
if(outlineState != destinationType){
if(destinationType == VerticesState.QUADRATIC_NURBS){
transformOutlines2Quadratic();
checkOverlaps();
} else {
throw new IllegalStateException("destinationType "+destinationType.name()+" not supported (currently "+outlineState.name()+")");
}
}
}
private void subdivideTriangle(final Outline outline, Vertex a, Vertex b, Vertex c, int index){
float[] v1 = VectorUtil.mid(a.getCoord(), b.getCoord());
float[] v3 = VectorUtil.mid(b.getCoord(), c.getCoord());
float[] v2 = VectorUtil.mid(v1, v3);
//drop off-curve vertex to image on the curve
b.setCoord(v2, 0, 3);
b.setOnCurve(true);
outline.addVertex(index, vertexFactory.create(v1, 0, 3, false));
outline.addVertex(index+2, vertexFactory.create(v3, 0, 3, false));
}
/**
* Check overlaps between curved triangles
* first check if any vertex in triangle a is in triangle b
* second check if edges of triangle a intersect segments of triangle b
* if any of the two tests is true we divide current triangle
* and add the other to the list of overlaps
*
* Loop until overlap array is empty. (check only in first pass)
*/
private void checkOverlaps() {
final ArrayList<Vertex> overlaps = new ArrayList<Vertex>(3);
final int count = getOutlineNumber();
boolean firstpass = true;
do {
for (int cc = 0; cc < count; cc++) {
final Outline outline = getOutline(cc);
int vertexCount = outline.getVertexCount();
for(int i=0; i < outline.getVertexCount(); i++) {
final Vertex currentVertex = outline.getVertex(i);
if ( !currentVertex.isOnCurve()) {
final Vertex nextV = outline.getVertex((i+1)%vertexCount);
final Vertex prevV = outline.getVertex((i+vertexCount-1)%vertexCount);
Vertex overlap =null;
//check for overlap even if already set for subdivision
//ensuring both trianglur overlaps get divided
//for pref. only check in first pass
//second pass to clear the overlaps arrray(reduces precision errors)
if(firstpass) {
overlap = checkTriOverlaps(prevV, currentVertex, nextV);
}
if(overlaps.contains(currentVertex) || overlap != null) {
overlaps.remove(currentVertex);
subdivideTriangle(outline, prevV, currentVertex, nextV, i);
i+=3;
vertexCount+=2;
if(overlap != null && !overlap.isOnCurve()) {
if(!overlaps.contains(overlap)) {
overlaps.add(overlap);
}
}
}
}
}
}
firstpass = false;
}while(!overlaps.isEmpty());
}
private final float[] tempVecAC = new float[3];
private final float[] tempVecAB = new float[3];
private final float[] tempVecAP = new float[3];
private Vertex checkTriOverlaps(Vertex a, Vertex b, Vertex c) {
int count = getOutlineNumber();
for (int cc = 0; cc < count; cc++) {
final Outline outline = getOutline(cc);
int vertexCount = outline.getVertexCount();
for(int i=0; i < vertexCount; i++) {
final Vertex current = outline.getVertex(i);
if(current.isOnCurve() || current == a || current == b || current == c) {
continue;
}
final Vertex nextV = outline.getVertex((i+1)%vertexCount);
final Vertex prevV = outline.getVertex((i+vertexCount-1)%vertexCount);
//skip neighboring triangles
if(prevV == c || nextV == a) {
continue;
}
if( VectorUtil.vertexInTriangle3(a.getCoord(), b.getCoord(), c.getCoord(),
current.getCoord(), nextV.getCoord(), prevV.getCoord(),
tempVecAC, tempVecAB, tempVecAP) ) {
return current;
}
if(VectorUtil.testTri2SegIntersection(a, b, c, prevV, current) ||
VectorUtil.testTri2SegIntersection(a, b, c, current, nextV) ||
VectorUtil.testTri2SegIntersection(a, b, c, prevV, nextV)) {
return current;
}
}
}
return null;
}
private void transformOutlines2Quadratic() {
int count = getOutlineNumber();
for (int cc = 0; cc < count; cc++) {
final Outline outline = getOutline(cc);
int vertexCount = outline.getVertexCount();
for(int i=0; i < vertexCount; i++) {
final Vertex currentVertex = outline.getVertex(i);
final Vertex nextVertex = outline.getVertex((i+1)%vertexCount);
if ( !currentVertex.isOnCurve() && !nextVertex.isOnCurve() ) {
final float[] newCoords = VectorUtil.mid(currentVertex.getCoord(),
nextVertex.getCoord());
final Vertex v = vertexFactory.create(newCoords, 0, 3, true);
i++;
vertexCount++;
outline.addVertex(i, v);
}
}
if(vertexCount <= 0) {
outlines.remove(outline);
cc--;
count--;
continue;
}
if( vertexCount > 0 ) {
if(VectorUtil.checkEquality(outline.getVertex(0).getCoord(),
outline.getLastVertex().getCoord())) {
outline.removeVertex(vertexCount-1);
}
}
}
outlineState = VerticesState.QUADRATIC_NURBS;
}
private int generateVertexIds() {
int maxVertexId = 0;
for(int i=0; i<outlines.size(); i++) {
final ArrayList<Vertex> vertices = outlines.get(i).getVertices();
for(int pos=0; pos<vertices.size(); pos++) {
vertices.get(pos).setId(maxVertexId++);
}
}
return maxVertexId;
}
/**
* Return list of concatenated vertices associated with all
* {@code Outline}s of this object.
* <p>
* Vertices are cached until marked dirty.
* </p>
* <p>
* Should always be called <i>after</i> {@link #getTriangles(VerticesState)},
* since the latter will mark all cached vertices dirty!
* </p>
* FIXME: Add memory optimization, i.e. VBO layout
* FIXME: Add Runnable task-per-vertices !
*/
public final ArrayList<Vertex> getVertices() {
final boolean updated;
if( 0 != ( DIRTY_VERTICES & dirtyBits ) ) {
vertices.clear();
for(int i=0; i<outlines.size(); i++) {
vertices.addAll(outlines.get(i).getVertices());
}
dirtyBits &= ~DIRTY_VERTICES;
updated = true;
} else {
updated = false;
}
if(Region.DEBUG_INSTANCE) {
System.err.println("OutlineShape.getVertices(): o "+outlines.size()+", v "+vertices.size()+", updated "+updated);
}
return vertices;
}
private void triangulateImpl() {
if( 0 < outlines.size() ) {
sortOutlines();
generateVertexIds();
triangles.clear();
final Triangulator triangulator2d = Triangulation.create();
for(int index = 0; index<outlines.size(); index++) {
triangulator2d.addCurve(triangles, outlines.get(index));
}
triangulator2d.generate(triangles);
triangulator2d.reset();
}
}
/**
* Triangulate the {@link OutlineShape} generating a list of triangles,
* while {@link #transformOutlines(VerticesState)} beforehand.
* <p>
* Triangles are cached until marked dirty.
* </p>
* @return an arraylist of triangles representing the filled region
* which is produced by the combination of the outlines
* FIXME: Add memory optimization, i.e. VBO layout
*/
public ArrayList<Triangle> getTriangles(VerticesState destinationType) {
final boolean updated;
if( 0 != ( DIRTY_TRIANGLES & dirtyBits ) ) {
transformOutlines(destinationType);
triangulateImpl();
updated = true;
dirtyBits |= DIRTY_VERTICES;
dirtyBits &= ~DIRTY_TRIANGLES;
} else {
updated = false;
}
if(Region.DEBUG_INSTANCE) {
System.err.println("OutlineShape.getTriangles().2: "+triangles.size()+", updated "+updated);
}
return triangles;
}
/** Sort the outlines from large
* to small depending on the AABox
*/
private void sortOutlines() {
Collections.sort(outlines);
Collections.reverse(outlines);
}
/**
* Compare two outline shapes with Bounding Box area
* as criteria.
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public final int compareTo(final OutlineShape other) {
final float thisSize = getBounds().getSize();
final float otherSize = other.getBounds().getSize();
if( thisSize < otherSize ){
return -1;
} else if( thisSize > otherSize ) {
return 1;
}
return 0; // FIXME: No epsilon, i.e. smallest accurate float value ?
}
private void validateBoundingBox() {
dirtyBits &= ~DIRTY_BOUNDS;
bbox.reset();
for (int i=0; i<outlines.size(); i++) {
bbox.resize(outlines.get(i).getBounds());
}
}
public final AABBox getBounds() {
if( 0 == ( dirtyBits & DIRTY_BOUNDS ) ) {
validateBoundingBox();
}
return bbox;
}
/**
* @param obj the Object to compare this OutlineShape with
* @return true if {@code obj} is an OutlineShape, not null,
* same outlineState, equal bounds and equal outlines in the same order
*/
@Override
public boolean equals(Object obj) {
if( obj == this) {
return true;
}
if( null == obj || !(obj instanceof OutlineShape) ) {
return false;
}
final OutlineShape o = (OutlineShape) obj;
if(getOutlineState() != o.getOutlineState()) {
return false;
}
if(getOutlineNumber() != o.getOutlineNumber()) {
return false;
}
if( !getBounds().equals( o.getBounds() ) ) {
return false;
}
for (int i=getOutlineNumber()-1; i>=0; i--) {
if( ! getOutline(i).equals( o.getOutline(i) ) ) {
return false;
}
}
return true;
}
}
|