forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPGraphics2D.java
More file actions
2144 lines (1657 loc) · 59.5 KB
/
PGraphics2D.java
File metadata and controls
2144 lines (1657 loc) · 59.5 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
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2006-08 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.core;
import java.awt.Toolkit;
import java.awt.image.DirectColorModel;
import java.awt.image.MemoryImageSource;
import java.util.Arrays;
/**
* Subclass of PGraphics that handles fast 2D rendering using a
* MemoryImageSource. The renderer found in this class is not as accurate as
* PGraphicsJava2D, but offers certain speed tradeoffs, particular when
* messing with the pixels array, or displaying image or video data.
*/
public class PGraphics2D extends PGraphics {
PMatrix2D ctm = new PMatrix2D();
//PPolygon polygon; // general polygon to use for shape
PPolygon fpolygon; // used to fill polys for tri or quad strips
PPolygon spolygon; // stroke/line polygon
float svertices[][]; // temp vertices used for stroking end of poly
PPolygon tpolygon;
int[] vertexOrder;
PLine line;
float[][] matrixStack = new float[MATRIX_STACK_DEPTH][6];
int matrixStackDepth;
DirectColorModel cm;
MemoryImageSource mis;
//////////////////////////////////////////////////////////////
public PGraphics2D() { }
//public void setParent(PApplet parent)
//public void setPrimary(boolean primary)
//public void setPath(String path)
//public void setSize(int iwidth, int iheight)
protected void allocate() {
pixelCount = width * height;
pixels = new int[pixelCount];
if (primarySurface) {
cm = new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff);;
mis = new MemoryImageSource(width, height, pixels, 0, width);
mis.setFullBufferUpdates(true);
mis.setAnimated(true);
image = Toolkit.getDefaultToolkit().createImage(mis);
}
}
//public void dispose()
//////////////////////////////////////////////////////////////
public boolean canDraw() {
return true;
}
public void beginDraw() {
// need to call defaults(), but can only be done when it's ok to draw
// (i.e. for OpenGL, no drawing can be done outside beginDraw/endDraw).
if (!settingsInited) {
defaultSettings();
// polygon = new PPolygon(this);
fpolygon = new PPolygon(this);
spolygon = new PPolygon(this);
spolygon.vertexCount = 4;
svertices = new float[2][];
}
resetMatrix(); // reset model matrix
// reset vertices
vertexCount = 0;
}
public void endDraw() {
if (mis != null) {
mis.newPixels(pixels, cm, 0, width);
}
// mark pixels as having been updated, so that they'll work properly
// when this PGraphics is drawn using image().
updatePixels();
}
// public void flush()
//////////////////////////////////////////////////////////////
//protected void checkSettings()
//protected void defaultSettings()
//protected void reapplySettings()
//////////////////////////////////////////////////////////////
//public void hint(int which)
//////////////////////////////////////////////////////////////
//public void beginShape()
public void beginShape(int kind) {
shape = kind;
vertexCount = 0;
curveVertexCount = 0;
// polygon.reset(0);
fpolygon.reset(4);
spolygon.reset(4);
textureImage = null;
// polygon.interpUV = false;
}
//public void edge(boolean e)
//public void normal(float nx, float ny, float nz)
//public void textureMode(int mode)
//public void texture(PImage image)
/*
public void vertex(float x, float y) {
if (shape == POINTS) {
point(x, y);
} else {
super.vertex(x, y);
}
}
*/
public void vertex(float x, float y, float z) {
showDepthWarningXYZ("vertex");
}
//public void vertex(float x, float y, float u, float v)
public void vertex(float x, float y, float z, float u, float v) {
showDepthWarningXYZ("vertex");
}
//protected void vertexTexture(float u, float v);
public void breakShape() {
showWarning("This renderer cannot handle concave shapes " +
"or shapes with holes.");
}
//public void endShape()
public void endShape(int mode) {
if (ctm.isIdentity()) {
for (int i = 0; i < vertexCount; i++) {
vertices[i][TX] = vertices[i][X];
vertices[i][TY] = vertices[i][Y];
}
} else {
for (int i = 0; i < vertexCount; i++) {
vertices[i][TX] = ctm.multX(vertices[i][X], vertices[i][Y]);
vertices[i][TY] = ctm.multY(vertices[i][X], vertices[i][Y]);
}
}
// ------------------------------------------------------------------
// TEXTURES
fpolygon.texture(textureImage);
// ------------------------------------------------------------------
// COLORS
// calculate RGB for each vertex
spolygon.interpARGB = true; //strokeChanged; //false;
fpolygon.interpARGB = true; //fillChanged; //false;
// all the values for r, g, b have been set with calls to vertex()
// (no need to re-calculate anything here)
// ------------------------------------------------------------------
// RENDER SHAPES
int increment;
switch (shape) {
case POINTS:
// stroke cannot change inside beginShape(POINTS);
if (stroke) {
if ((ctm.m00 == ctm.m11) && (strokeWeight == 1)) {
for (int i = 0; i < vertexCount; i++) {
thin_point(vertices[i][TX], vertices[i][TY], strokeColor);
}
} else {
for (int i = 0; i < vertexCount; i++) {
float[] v = vertices[i];
thick_point(v[TX], v[TY], v[TZ], v[SR], v[SG], v[SB], v[SA]);
}
}
}
break;
case LINES:
if (stroke) {
// increment by two for individual lines
increment = (shape == LINES) ? 2 : 1;
draw_lines(vertices, vertexCount-1, 1, increment, 0);
}
break;
case TRIANGLE_FAN:
// do fill and stroke separately because otherwise
// the lines will be stroked more than necessary
if (fill || textureImage != null) {
fpolygon.vertexCount = 3;
for (int i = 1; i < vertexCount-1; i++) {
// System.out.println(i + " of " + vertexCount);
fpolygon.vertices[2][R] = vertices[0][R];
fpolygon.vertices[2][G] = vertices[0][G];
fpolygon.vertices[2][B] = vertices[0][B];
fpolygon.vertices[2][A] = vertices[0][A];
fpolygon.vertices[2][TX] = vertices[0][TX];
fpolygon.vertices[2][TY] = vertices[0][TY];
if (textureImage != null) {
fpolygon.vertices[2][U] = vertices[0][U];
fpolygon.vertices[2][V] = vertices[0][V];
}
// System.out.println(fpolygon.vertices[2][TX] + " " + fpolygon.vertices[2][TY]);
for (int j = 0; j < 2; j++) {
fpolygon.vertices[j][R] = vertices[i+j][R];
fpolygon.vertices[j][G] = vertices[i+j][G];
fpolygon.vertices[j][B] = vertices[i+j][B];
fpolygon.vertices[j][A] = vertices[i+j][A];
fpolygon.vertices[j][TX] = vertices[i+j][TX];
fpolygon.vertices[j][TY] = vertices[i+j][TY];
// System.out.println(fpolygon.vertices[j][TX] + " " + fpolygon.vertices[j][TY]);
if (textureImage != null) {
fpolygon.vertices[j][U] = vertices[i+j][U];
fpolygon.vertices[j][V] = vertices[i+j][V];
}
}
// System.out.println();
fpolygon.render();
}
}
if (stroke) {
// draw internal lines
for (int i = 1; i < vertexCount; i++) {
draw_line(vertices[0], vertices[i]);
}
// draw a ring around the outside
for (int i = 1; i < vertexCount-1; i++) {
draw_line(vertices[i], vertices[i+1]);
}
// close the shape
draw_line(vertices[vertexCount-1], vertices[1]);
}
break;
case TRIANGLES:
case TRIANGLE_STRIP:
increment = (shape == TRIANGLES) ? 3 : 1;
// do fill and stroke separately because otherwise
// the lines will be stroked more than necessary
if (fill || textureImage != null) {
fpolygon.vertexCount = 3;
for (int i = 0; i < vertexCount-2; i += increment) {
for (int j = 0; j < 3; j++) {
fpolygon.vertices[j][R] = vertices[i+j][R];
fpolygon.vertices[j][G] = vertices[i+j][G];
fpolygon.vertices[j][B] = vertices[i+j][B];
fpolygon.vertices[j][A] = vertices[i+j][A];
fpolygon.vertices[j][TX] = vertices[i+j][TX];
fpolygon.vertices[j][TY] = vertices[i+j][TY];
fpolygon.vertices[j][TZ] = vertices[i+j][TZ];
if (textureImage != null) {
fpolygon.vertices[j][U] = vertices[i+j][U];
fpolygon.vertices[j][V] = vertices[i+j][V];
}
}
fpolygon.render();
}
}
if (stroke) {
// first draw all vertices as a line strip
if (shape == TRIANGLE_STRIP) {
draw_lines(vertices, vertexCount-1, 1, 1, 0);
} else {
draw_lines(vertices, vertexCount-1, 1, 1, 3);
}
// then draw from vertex (n) to (n+2)
// incrementing n using the same as above
draw_lines(vertices, vertexCount-2, 2, increment, 0);
// changed this to vertexCount-2, because it seemed
// to be adding an extra (nonexistant) line
}
break;
case QUADS:
if (fill || textureImage != null) {
fpolygon.vertexCount = 4;
for (int i = 0; i < vertexCount-3; i += 4) {
for (int j = 0; j < 4; j++) {
int jj = i+j;
fpolygon.vertices[j][R] = vertices[jj][R];
fpolygon.vertices[j][G] = vertices[jj][G];
fpolygon.vertices[j][B] = vertices[jj][B];
fpolygon.vertices[j][A] = vertices[jj][A];
fpolygon.vertices[j][TX] = vertices[jj][TX];
fpolygon.vertices[j][TY] = vertices[jj][TY];
fpolygon.vertices[j][TZ] = vertices[jj][TZ];
if (textureImage != null) {
fpolygon.vertices[j][U] = vertices[jj][U];
fpolygon.vertices[j][V] = vertices[jj][V];
}
}
fpolygon.render();
}
}
if (stroke) {
for (int i = 0; i < vertexCount-3; i += 4) {
draw_line(vertices[i+0], vertices[i+1]);
draw_line(vertices[i+1], vertices[i+2]);
draw_line(vertices[i+2], vertices[i+3]);
draw_line(vertices[i+3], vertices[i+0]);
}
}
break;
case QUAD_STRIP:
if (fill || textureImage != null) {
fpolygon.vertexCount = 4;
for (int i = 0; i < vertexCount-3; i += 2) {
for (int j = 0; j < 4; j++) {
int jj = i+j;
if (j == 2) jj = i+3; // swap 2nd and 3rd vertex
if (j == 3) jj = i+2;
fpolygon.vertices[j][R] = vertices[jj][R];
fpolygon.vertices[j][G] = vertices[jj][G];
fpolygon.vertices[j][B] = vertices[jj][B];
fpolygon.vertices[j][A] = vertices[jj][A];
fpolygon.vertices[j][TX] = vertices[jj][TX];
fpolygon.vertices[j][TY] = vertices[jj][TY];
fpolygon.vertices[j][TZ] = vertices[jj][TZ];
if (textureImage != null) {
fpolygon.vertices[j][U] = vertices[jj][U];
fpolygon.vertices[j][V] = vertices[jj][V];
}
}
fpolygon.render();
}
}
if (stroke) {
draw_lines(vertices, vertexCount-1, 1, 2, 0); // inner lines
draw_lines(vertices, vertexCount-2, 2, 1, 0); // outer lines
}
break;
case POLYGON:
if (isConvex()) {
if (fill || textureImage != null) {
//System.out.println("convex");
fpolygon.renderPolygon(vertices, vertexCount);
//if (stroke) polygon.unexpand();
}
if (stroke) {
draw_lines(vertices, vertexCount-1, 1, 1, 0);
if (mode == CLOSE) {
// draw the last line connecting back to the first point in poly
//svertices[0] = vertices[vertexCount-1];
//svertices[1] = vertices[0];
//draw_lines(svertices, 1, 1, 1, 0);
draw_line(vertices[vertexCount-1], vertices[0]);
}
}
} else { // not convex
//System.out.println("concave");
if (fill || textureImage != null) {
// the triangulator produces polygons that don't align
// when smoothing is enabled. but if there is a stroke around
// the polygon, then smoothing can be temporarily disabled.
boolean smoov = smooth;
//if (stroke && !hints[DISABLE_SMOOTH_HACK]) smooth = false;
if (stroke) smooth = false;
concaveRender();
//if (stroke && !hints[DISABLE_SMOOTH_HACK]) smooth = smoov;
if (stroke) smooth = smoov;
}
if (stroke) {
draw_lines(vertices, vertexCount-1, 1, 1, 0);
if (mode == CLOSE) {
// draw the last line connecting back
// to the first point in poly
// svertices[0] = vertices[vertexCount-1];
// svertices[1] = vertices[0];
// draw_lines(svertices, 1, 1, 1, 0);
draw_line(vertices[vertexCount-1], vertices[0]);
}
}
}
break;
}
// to signify no shape being drawn
shape = 0;
}
//////////////////////////////////////////////////////////////
// CONCAVE/CONVEX POLYGONS
private boolean isConvex() {
//float v[][] = polygon.vertices;
//int n = polygon.vertexCount;
//int j,k;
//float tol = 0.001f;
if (vertexCount < 3) {
// ERROR: this is a line or a point, render as convex
return true;
}
int flag = 0;
// iterate along border doing dot product.
// if the sign of the result changes, then is concave
for (int i = 0; i < vertexCount; i++) {
float[] vi = vertices[i];
float[] vj = vertices[(i + 1) % vertexCount];
float[] vk = vertices[(i + 2) % vertexCount];
float calc = ((vj[TX] - vi[TX]) * (vk[TY] - vj[TY]) -
(vj[TY] - vi[TY]) * (vk[TX] - vj[TX]));
if (calc < 0) {
flag |= 1;
} else if (calc > 0) {
flag |= 2;
}
if (flag == 3) {
return false; // CONCAVE
}
}
if (flag != 0) {
return true; // CONVEX
} else {
// ERROR: colinear points, self intersection
// treat as CONVEX
return true;
}
}
/**
* Triangulate the current polygon.
* <BR> <BR>
* Simple ear clipping polygon triangulation adapted from code by
* John W. Ratcliff (jratcliff at verant.com). Presumably
* <A HREF="http://www.flipcode.org/cgi-bin/fcarticles.cgi?show=63943">this</A>
* bit of code from the web.
*/
protected void concaveRender() {
if (vertexOrder == null || vertexOrder.length != vertices.length) {
vertexOrder = new int[vertices.length];
// int[] temp = new int[vertices.length];
// // since vertex_start may not be zero, might need to keep old stuff around
// PApplet.arrayCopy(vertexOrder, temp, vertexCount);
// vertexOrder = temp;
}
if (tpolygon == null) {
tpolygon = new PPolygon(this);
}
tpolygon.reset(3);
// first we check if the polygon goes clockwise or counterclockwise
float area = 0;
for (int p = vertexCount - 1, q = 0; q < vertexCount; p = q++) {
area += (vertices[q][X] * vertices[p][Y] -
vertices[p][X] * vertices[q][Y]);
}
// ain't nuthin there
if (area == 0) return;
// don't allow polygons to come back and meet themselves,
// otherwise it will anger the triangulator
// http://dev.processing.org/bugs/show_bug.cgi?id=97
float vfirst[] = vertices[0];
float vlast[] = vertices[vertexCount-1];
if ((Math.abs(vfirst[X] - vlast[X]) < EPSILON) &&
(Math.abs(vfirst[Y] - vlast[Y]) < EPSILON) &&
(Math.abs(vfirst[Z] - vlast[Z]) < EPSILON)) {
vertexCount--;
}
// then sort the vertices so they are always in a counterclockwise order
for (int i = 0; i < vertexCount; i++) {
vertexOrder[i] = (area > 0) ? i : (vertexCount-1 - i);
}
// remove vc-2 Vertices, creating 1 triangle every time
int vc = vertexCount; // vc will be decremented while working
int count = 2*vc; // complex polygon detection
for (int m = 0, v = vc - 1; vc > 2; ) {
boolean snip = true;
// if we start over again, is a complex polygon
if (0 >= (count--)) {
break; // triangulation failed
}
// get 3 consecutive vertices <u,v,w>
int u = v ; if (vc <= u) u = 0; // previous
v = u + 1; if (vc <= v) v = 0; // current
int w = v + 1; if (vc <= w) w = 0; // next
// Upgrade values to doubles, and multiply by 10 so that we can have
// some better accuracy as we tessellate. This seems to have negligible
// speed differences on Windows and Intel Macs, but causes a 50% speed
// drop for PPC Macs with the bug's example code that draws ~200 points
// in a concave polygon. Apple has abandoned PPC so we may as well too.
// http://dev.processing.org/bugs/show_bug.cgi?id=774
// triangle A B C
double Ax = -10 * vertices[vertexOrder[u]][X];
double Ay = 10 * vertices[vertexOrder[u]][Y];
double Bx = -10 * vertices[vertexOrder[v]][X];
double By = 10 * vertices[vertexOrder[v]][Y];
double Cx = -10 * vertices[vertexOrder[w]][X];
double Cy = 10 * vertices[vertexOrder[w]][Y];
// first we check if <u,v,w> continues going ccw
if (EPSILON > (((Bx-Ax) * (Cy-Ay)) - ((By-Ay) * (Cx-Ax)))) {
continue;
}
for (int p = 0; p < vc; p++) {
if ((p == u) || (p == v) || (p == w)) {
continue;
}
double Px = -10 * vertices[vertexOrder[p]][X];
double Py = 10 * vertices[vertexOrder[p]][Y];
double ax = Cx - Bx; double ay = Cy - By;
double bx = Ax - Cx; double by = Ay - Cy;
double cx = Bx - Ax; double cy = By - Ay;
double apx = Px - Ax; double apy = Py - Ay;
double bpx = Px - Bx; double bpy = Py - By;
double cpx = Px - Cx; double cpy = Py - Cy;
double aCROSSbp = ax * bpy - ay * bpx;
double cCROSSap = cx * apy - cy * apx;
double bCROSScp = bx * cpy - by * cpx;
if ((aCROSSbp >= 0.0) && (bCROSScp >= 0.0) && (cCROSSap >= 0.0)) {
snip = false;
}
}
if (snip) {
tpolygon.renderTriangle(vertices[vertexOrder[u]],
vertices[vertexOrder[v]],
vertices[vertexOrder[w]]);
m++;
// remove v from remaining polygon
for (int s = v, t = v + 1; t < vc; s++, t++) {
vertexOrder[s] = vertexOrder[t];
}
vc--;
// reset error detection counter
count = 2 * vc;
}
}
}
/*
// triangulate the current polygon
private void concaveRender() {
float polyVertices[][] = polygon.vertices;
if (tpolygon == null) {
// allocate on first use, rather than slowing
// the startup of the class.
tpolygon = new PPolygon(this);
tpolygon_vertex_order = new int[TPOLYGON_MAX_VERTICES];
}
tpolygon.reset(3);
// copy render parameters
if (textureImage != null) {
tpolygon.texture(textureImage); //polygon.timage);
}
tpolygon.interpX = polygon.interpX;
tpolygon.interpUV = polygon.interpUV;
tpolygon.interpARGB = polygon.interpARGB;
// simple ear clipping polygon triangulation
// addapted from code by john w. ratcliff ([email protected])
// 1 - first we check if the polygon goes CW or CCW
// CW-CCW ordering adapted from code by
// Joseph O'Rourke [email protected]
// 1A - we start by finding the lowest-right most vertex
boolean ccw = false; // clockwise
int n = polygon.vertexCount;
int mm; // postion for LR vertex
//float min[] = new float[2];
float minX = polyVertices[0][TX];
float minY = polyVertices[0][TY];
mm = 0;
for(int i = 0; i < n; i++ ) {
if ((polyVertices[i][TY] < minY) ||
((polyVertices[i][TY] == minY) && (polyVertices[i][TX] > minX) )
) {
mm = i;
minX = polyVertices[mm][TX];
minY = polyVertices[mm][TY];
}
}
// 1B - now we compute the cross product of the edges of this vertex
float cp;
int mm1;
// just for renaming
float a[] = new float[2];
float b[] = new float[2];
float c[] = new float[2];
mm1 = (mm + (n-1)) % n;
// assign a[0] to point to poly[m1][0] etc.
for(int i = 0; i < 2; i++ ) {
a[i] = polyVertices[mm1][i];
b[i] = polyVertices[mm][i];
c[i] = polyVertices[(mm+1)%n][i];
}
cp = a[0] * b[1] - a[1] * b[0] +
a[1] * c[0] - a[0] * c[1] +
b[0] * c[1] - c[0] * b[1];
if ( cp > 0 )
ccw = true; // CCW
else
ccw = false; // CW
// 1C - then we sort the vertices so they
// are always in a counterclockwise order
//int j = 0;
if (!ccw) {
// keep the same order
for (int i = 0; i < n; i++) {
tpolygon_vertex_order[i] = i;
}
} else {
// invert the order
for (int i = 0; i < n; i++) {
tpolygon_vertex_order[i] = (n - 1) - i;
}
}
// 2 - begin triangulation
// resulting triangles are stored in the triangle array
// remove vc-2 Vertices, creating 1 triangle every time
int vc = n;
int count = 2*vc; // complex polygon detection
for (int m = 0, v = vc - 1; vc > 2; ) {
boolean snip = true;
// if we start over again, is a complex polygon
if (0 >= (count--)) {
break; // triangulation failed
}
// get 3 consecutive vertices <u,v,w>
int u = v ; if (vc <= u) u = 0; // previous
v = u+1; if (vc <= v) v = 0; // current
int w = v+1; if (vc <= w) w = 0; // next
// triangle A B C
float Ax, Ay, Bx, By, Cx, Cy, Px, Py;
Ax = -polyVertices[tpolygon_vertex_order[u]][TX];
Ay = polyVertices[tpolygon_vertex_order[u]][TY];
Bx = -polyVertices[tpolygon_vertex_order[v]][TX];
By = polyVertices[tpolygon_vertex_order[v]][TY];
Cx = -polyVertices[tpolygon_vertex_order[w]][TX];
Cy = polyVertices[tpolygon_vertex_order[w]][TY];
if ( EPSILON > (((Bx-Ax) * (Cy-Ay)) - ((By-Ay) * (Cx-Ax)))) {
continue;
}
for (int p = 0; p < vc; p++) {
// this part is a bit osbscure, basically what it does
// is test if this tree vertices are and ear or not, looking for
// intersections with the remaining vertices using a cross product
float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
float cCROSSap, bCROSScp, aCROSSbp;
if( (p == u) || (p == v) || (p == w) ) {
continue;
}
Px = -polyVertices[tpolygon_vertex_order[p]][TX];
Py = polyVertices[tpolygon_vertex_order[p]][TY];
ax = Cx - Bx; ay = Cy - By;
bx = Ax - Cx; by = Ay - Cy;
cx = Bx - Ax; cy = By - Ay;
apx= Px - Ax; apy= Py - Ay;
bpx= Px - Bx; bpy= Py - By;
cpx= Px - Cx; cpy= Py - Cy;
aCROSSbp = ax * bpy - ay * bpx;
cCROSSap = cx * apy - cy * apx;
bCROSScp = bx * cpy - by * cpx;
if ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f)) {
snip = false;
}
}
if (snip) {
// yes, the trio is an ear, render it and cut it
int triangle_vertices[] = new int[3];
int s,t;
// true names of the vertices
triangle_vertices[0] = tpolygon_vertex_order[u];
triangle_vertices[1] = tpolygon_vertex_order[v];
triangle_vertices[2] = tpolygon_vertex_order[w];
// create triangle
//render_triangle(triangle_vertices);
//private final void render_triangle(int[] triangle_vertices) {
// copy all fields of the triangle vertices
for (int i = 0; i < 3; i++) {
float[] src = polygon.vertices[triangle_vertices[i]];
float[] dest = tpolygon.vertices[i];
for (int k = 0; k < VERTEX_FIELD_COUNT; k++) {
dest[k] = src[k];
}
}
// render triangle
tpolygon.render();
//}
m++;
// remove v from remaining polygon
for( s = v, t = v + 1; t < vc; s++, t++) {
tpolygon_vertex_order[s] = tpolygon_vertex_order[t];
}
vc--;
// resest error detection counter
count = 2 * vc;
}
}
}
*/
//////////////////////////////////////////////////////////////
// BEZIER VERTICES
//public void bezierVertex(float x2, float y2,
// float x3, float y3,
// float x4, float y4)
//public void bezierVertex(float x2, float y2, float z2,
// float x3, float y3, float z3,
// float x4, float y4, float z4)
//////////////////////////////////////////////////////////////
// CURVE VERTICES
//public void curveVertex(float x, float y)
//public void curveVertex(float x, float y, float z)
//////////////////////////////////////////////////////////////
// FLUSH
//public void flush()
//////////////////////////////////////////////////////////////
// PRIMITIVES
//public void point(float x, float y)
public void point(float x, float y, float z) {
showDepthWarningXYZ("point");
}
//public void line(float x1, float y1, float x2, float y2)
//public void line(float x1, float y1, float z1,
// float x2, float y2, float z2)
//public void triangle(float x1, float y1,
// float x2, float y2,
// float x3, float y3)
//public void quad(float x1, float y1, float x2, float y2,
// float x3, float y3, float x4, float y4)
//////////////////////////////////////////////////////////////
// RECT
protected void rectImpl(float x1f, float y1f, float x2f, float y2f) {
if (smooth || strokeAlpha || ctm.isWarped()) {
// screw the efficiency, send this off to beginShape().
super.rectImpl(x1f, y1f, x2f, y2f);
} else {
int x1 = (int) (x1f + ctm.m02);
int y1 = (int) (y1f + ctm.m12);
int x2 = (int) (x2f + ctm.m02);
int y2 = (int) (y2f + ctm.m12);
if (fill) {
simple_rect_fill(x1, y1, x2, y2);
}
if (stroke) {
if (strokeWeight == 1) {
thin_flat_line(x1, y1, x2, y1);
thin_flat_line(x2, y1, x2, y2);
thin_flat_line(x2, y2, x1, y2);
thin_flat_line(x1, y2, x1, y1);
} else {
thick_flat_line(x1, y1, strokeR, strokeG, strokeB, strokeA,
x2, y1, strokeR, strokeG, strokeB, strokeA);
thick_flat_line(x2, y1, strokeR, strokeG, strokeB, strokeA,
x2, y2, strokeR, strokeG, strokeB, strokeA);
thick_flat_line(x2, y2, strokeR, strokeG, strokeB, strokeA,
x1, y2, strokeR, strokeG, strokeB, strokeA);
thick_flat_line(x1, y2, strokeR, strokeG, strokeB, strokeA,
x1, y1, strokeR, strokeG, strokeB, strokeA);
}
}
}
}
/**
* Draw a rectangle that hasn't been warped by the CTM (though it may be
* translated). Just fill a bunch of pixels, or blend them if there's alpha.
*/
private void simple_rect_fill(int x1, int y1, int x2, int y2) {
if (y2 < y1) {
int temp = y1; y1 = y2; y2 = temp;
}
if (x2 < x1) {
int temp = x1; x1 = x2; x2 = temp;
}
// check to see if completely off-screen (e.g. if the left edge of the
// rectangle is bigger than the width, and so on.)
if ((x1 > width1) || (x2 < 0) ||
(y1 > height1) || (y2 < 0)) return;
// these only affect the fill, not the stroke
// (otherwise strange boogers at edges b/c frame changes shape)
if (x1 < 0) x1 = 0;
if (x2 > width) x2 = width;
if (y1 < 0) y1 = 0;