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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
|
/*
** License Applicability. Except to the extent portions of this file are
** made subject to an alternative license as permitted in the SGI Free
** Software License B, Version 1.1 (the "License"), the contents of this
** file are subject only to the provisions of the License. You may not use
** this file except in compliance with the License. You may obtain a copy
** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
**
** http://oss.sgi.com/projects/FreeB
**
** Note that, as provided in the License, the Software is distributed on an
** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
**
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
**
** Additional Notice Provisions: The application programming interfaces
** established by SGI in conjunction with the Original Code are The
** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
** Window System(R) (Version 1.3), released October 19, 1998. This software
** was created using the OpenGL(R) version 1.2.1 Sample Implementation
** published by SGI, but has not been independently verified as being
** compliant with the OpenGL(R) version 1.2.1 Specification.
**
** $Date: 2006/03/29 18:46:46 $ $Revision: 1.5 $
*/
/*
** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/nurbtess/monoTriangulation.cc,v 1.5 2006/03/29 18:46:46 brianp Exp $
*/
#include <stdlib.h>
#include <stdio.h>
#include "gluos.h"
#include "glimports.h"
#include "zlassert.h"
#include "monoTriangulation.h"
#include "polyUtil.h" /*for area*/
#include "partitionX.h"
#include "monoPolyPart.h"
extern directedLine* polygonConvert(directedLine* polygon);
/*poly is NOT deleted
*/
void monoTriangulationOpt(directedLine* poly, primStream* pStream)
{
Int n_cusps;
Int n_edges = poly->numEdges();
directedLine** cusps = (directedLine**) malloc(sizeof(directedLine*)*n_edges);
assert(cusps);
findInteriorCuspsX(poly, n_cusps, cusps);
if(n_cusps ==0) //u monotine
{
monoTriangulationFun(poly, compV2InX, pStream);
}
else if(n_cusps == 1) // one interior cusp
{
directedLine* new_polygon = polygonConvert(cusps[0]);
directedLine* other = findDiagonal_singleCuspX(new_polygon);
//<other> should NOT be null unless there are self-intersecting
//trim curves. In that case, we don't want to core dump, instead,
//we triangulate anyway, and print out error message.
if(other == NULL)
{
monoTriangulationFun(poly, compV2InX, pStream);
}
else
{
directedLine* ret_p1;
directedLine* ret_p2;
new_polygon->connectDiagonal_2slines(new_polygon, other,
&ret_p1,
&ret_p2,
new_polygon);
monoTriangulationFun(ret_p1, compV2InX, pStream);
monoTriangulationFun(ret_p2, compV2InX, pStream);
ret_p1->deleteSinglePolygonWithSline();
ret_p2->deleteSinglePolygonWithSline();
}
}
else
{
//we need a general partitionX funtion (supposed to be in partitionX.C,
//not implemented yet. XXX
monoTriangulationFun(poly, compV2InY, pStream);
}
free(cusps);
}
void monoTriangulationRecOpt(Real* topVertex, Real* botVertex,
vertexArray* left_chain, Int left_current,
vertexArray* right_chain, Int right_current,
primStream* pStream)
{
Int i,j;
Int n_left = left_chain->getNumElements();
Int n_right = right_chain->getNumElements();
if(left_current>= n_left-1 ||
right_current>= n_right-1)
{
monoTriangulationRec(topVertex, botVertex, left_chain, left_current,
right_chain, right_current, pStream);
return;
}
//now both left and right have at least two vertices each.
Real left_v = left_chain->getVertex(left_current)[1];
Real right_v = right_chain->getVertex(right_current)[1];
if(left_v <= right_v) //first left vertex is below right
{
//find the last vertex of right which is above or equal to left
for(j=right_current; j<=n_right-1; j++)
{
if(right_chain->getVertex(j)[1] < left_v)
break;
}
monoTriangulationRecGen(topVertex, left_chain->getVertex(left_current),
left_chain, left_current, left_current,
right_chain, right_current, j-1,
pStream);
monoTriangulationRecOpt(right_chain->getVertex(j-1),
botVertex,
left_chain, left_current,
right_chain, j,
pStream);
}
else //first right vertex is strictly below left
{
//find the last vertex of left which is strictly above right
for(i=left_current; i<=n_left-1; i++)
{
if(left_chain->getVertex(i)[1] <= right_v)
break;
}
monoTriangulationRecGen(topVertex, right_chain->getVertex(right_current),
left_chain, left_current, i-1,
right_chain, right_current, right_current,
pStream);
monoTriangulationRecOpt(left_chain->getVertex(i-1),
botVertex,
left_chain, i,
right_chain, right_current,
pStream);
}
}
void monoTriangulationRecGenTBOpt(Real* topVertex, Real* botVertex,
vertexArray* inc_chain, Int inc_current, Int inc_end,
vertexArray* dec_chain, Int dec_current, Int dec_end,
primStream* pStream)
{
pStream->triangle(topVertex, inc_chain->getVertex(inc_current), dec_chain->getVertex(dec_current));
/*printf("**(%f,%f)\n", inc_chain->getArray()[0][0],inc_chain->getArray()[0][1]);*/
triangulateXYMonoTB(inc_end-inc_current+1, inc_chain->getArray()+inc_current, dec_end-dec_current+1, dec_chain->getArray()+dec_current, pStream);
pStream->triangle(botVertex, dec_chain->getVertex(dec_end), inc_chain->getVertex(inc_end));
}
/*n_left>=1
*n_right>=1
*the strip is going top to bottom. compared to the funtion
* triangulateXYmono()
*/
void triangulateXYMonoTB(Int n_left, Real** leftVerts,
Int n_right, Real** rightVerts,
primStream* pStream)
{
Int i,j,k,l;
Real* topMostV;
assert(n_left>=1 && n_right>=1);
if(leftVerts[0][1] >= rightVerts[0][1])
{
i=1;
j=0;
topMostV = leftVerts[0];
}
else
{
i=0;
j=1;
topMostV = rightVerts[0];
}
while(1)
{
if(i >= n_left) /*case1: no more in left*/
{
if(j<n_right-1) /*at least two vertices in right*/
{
pStream->begin();
pStream->insert(topMostV);
for(k=n_right-1; k>=j; k--)
pStream->insert(rightVerts[j]);
pStream->end(PRIMITIVE_STREAM_FAN);
}
break;
}
else if(j>= n_right) /*case2: no more in right*/
{
if(i<n_left-1) /*at least two vertices in left*/
{
pStream->begin();
pStream->insert(topMostV);
for(k=i; k<n_left; k++)
pStream->insert(leftVerts[k]);
pStream->end(PRIMITIVE_STREAM_FAN);
}
break;
}
else /* case3: neither is empty, plus the topMostV, there is at least one triangle to output*/
{
if(leftVerts[i][1] >= rightVerts[j][1])
{
pStream->begin();
pStream->insert(rightVerts[j]); /*the origin of this fan*/
pStream->insert(topMostV);
/*find the last k>=i such that
*leftverts[k][1] >= rightverts[j][1]
*/
k=i;
while(k<n_left)
{
if(leftVerts[k][1] < rightVerts[j][1])
break;
k++;
}
k--;
for(l=i; l<=k; l++)
{
pStream->insert(leftVerts[l]);
}
pStream->end(PRIMITIVE_STREAM_FAN);
//update i for next loop
i = k+1;
topMostV = leftVerts[k];
}
else /*leftVerts[i][1] < rightVerts[j][1]*/
{
pStream->begin();
pStream->insert(leftVerts[i]);/*the origion of this fan*/
/*find the last k>=j such that
*rightverts[k][1] > leftverts[i][1]*/
k=j;
while(k< n_right)
{
if(rightVerts[k][1] <= leftVerts[i][1])
break;
k++;
}
k--;
for(l=k; l>= j; l--)
pStream->insert(rightVerts[l]);
pStream->insert(topMostV);
pStream->end(PRIMITIVE_STREAM_FAN);
j=k+1;
topMostV = rightVerts[j-1];
}
}
}
}
static int chainConvex(vertexArray* inc_chain, Int inc_current, Int inc_end)
{
Int i;
//if there are no more than 2 vertices, return 1
if(inc_current >= inc_end-1) return 1;
for(i=inc_current; i<= inc_end-2; i++)
{
if(area(inc_chain->getVertex(i), inc_chain->getVertex(i+1), inc_chain->getVertex(i+2)) <0)
return 0;
}
return 1;
}
static int chainConcave(vertexArray* dec_chain, Int dec_current, Int dec_end)
{
Int i;
//if there are no more than 2 vertices, return 1
if(dec_current >= dec_end -1) return 1;
for(i=dec_current; i<=dec_end-2; i++)
{
if(area(dec_chain->getVertex(i), dec_chain->getVertex(i+1), dec_chain->getVertex(i+2)) >0)
return 0;
}
return 1;
}
void monoTriangulationRecGenInU(Real* topVertex, Real* botVertex,
vertexArray* inc_chain, Int inc_current, Int inc_end,
vertexArray* dec_chain, Int dec_current, Int dec_end,
primStream* pStream)
{
}
void monoTriangulationRecGenOpt(Real* topVertex, Real* botVertex,
vertexArray* inc_chain, Int inc_current, Int inc_end,
vertexArray* dec_chain, Int dec_current, Int dec_end,
primStream* pStream)
{
Int i;
//copy this to a polygon: directedLine Lioop
sampledLine* sline;
directedLine* dline;
directedLine* poly;
if(inc_current <= inc_end) //at least one vertex in inc_chain
{
sline = new sampledLine(topVertex, inc_chain->getVertex(inc_current));
poly = new directedLine(INCREASING, sline);
for(i=inc_current; i<=inc_end-1; i++)
{
sline = new sampledLine(inc_chain->getVertex(i), inc_chain->getVertex(i+1));
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
}
sline = new sampledLine(inc_chain->getVertex(inc_end), botVertex);
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
}
else //inc_chian is empty
{
sline = new sampledLine(topVertex, botVertex);
dline = new directedLine(INCREASING, sline);
poly = dline;
}
assert(poly != NULL);
if(dec_current <= dec_end) //at least on vertex in dec_Chain
{
sline = new sampledLine(botVertex, dec_chain->getVertex(dec_end));
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
for(i=dec_end; i>dec_current; i--)
{
sline = new sampledLine(dec_chain->getVertex(i), dec_chain->getVertex(i-1));
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
}
sline = new sampledLine(dec_chain->getVertex(dec_current), topVertex);
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
}
else //dec_chain is empty
{
sline = new sampledLine(botVertex, topVertex);
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
}
{
Int n_cusps;
Int n_edges = poly->numEdges();
directedLine** cusps = (directedLine**) malloc(sizeof(directedLine*)*n_edges);
assert(cusps);
findInteriorCuspsX(poly, n_cusps, cusps);
if(n_cusps ==0) //u monotine
{
monoTriangulationFun(poly, compV2InX, pStream);
}
else if(n_cusps == 1) // one interior cusp
{
directedLine* new_polygon = polygonConvert(cusps[0]);
directedLine* other = findDiagonal_singleCuspX(new_polygon);
//<other> should NOT be null unless there are self-intersecting
//trim curves. In that case, we don't want to core dump, instead,
//we triangulate anyway, and print out error message.
if(other == NULL)
{
monoTriangulationFun(poly, compV2InX, pStream);
}
else
{
directedLine* ret_p1;
directedLine* ret_p2;
new_polygon->connectDiagonal_2slines(new_polygon, other,
&ret_p1,
&ret_p2,
new_polygon);
monoTriangulationFun(ret_p1, compV2InX, pStream);
monoTriangulationFun(ret_p2, compV2InX, pStream);
ret_p1->deleteSinglePolygonWithSline();
ret_p2->deleteSinglePolygonWithSline();
}
}
else
{
//we need a general partitionX funtion (supposed to be in partitionX.C,
//not implemented yet. XXX
//monoTriangulationFun(poly, compV2InY, pStream);
directedLine* new_polygon = polygonConvert(poly);
directedLine* list = monoPolyPart(new_polygon);
for(directedLine* temp = list; temp != NULL; temp = temp->getNextPolygon())
{
monoTriangulationFun(temp, compV2InX, pStream);
}
//clean up
list->deletePolygonListWithSline();
}
free(cusps);
/*
if(numInteriorCuspsX(poly) == 0) //is u monotone
monoTriangulationFun(poly, compV2InX, pStream);
else //it is not u motone
monoTriangulationFun(poly, compV2InY, pStream);
*/
//clean up space
poly->deleteSinglePolygonWithSline();
return;
}
//apparently the following code is not reachable,
//it is for test purpose
if(inc_current > inc_end || dec_current>dec_end)
{
monoTriangulationRecGen(topVertex, botVertex, inc_chain, inc_current, inc_end,
dec_chain, dec_current, dec_end,
pStream);
return;
}
if(
area(dec_chain->getVertex(dec_current),
topVertex,
inc_chain->getVertex(inc_current)) >=0
&& chainConvex(inc_chain, inc_current, inc_end)
&& chainConcave(dec_chain, dec_current, dec_end)
&& area(inc_chain->getVertex(inc_end), botVertex, dec_chain->getVertex(dec_end)) >=0
)
{
monoTriangulationRecFunGen(topVertex, botVertex,
inc_chain, inc_current, inc_end,
dec_chain, dec_current, dec_end,
compV2InX, pStream);
}
else
{
monoTriangulationRecGen(topVertex, botVertex, inc_chain, inc_current, inc_end,
dec_chain, dec_current, dec_end,
pStream);
}
}
/*if inc_current>inc_end, then inc_chain has no points to be considered
*same for dec_chain
*/
void monoTriangulationRecGen(Real* topVertex, Real* botVertex,
vertexArray* inc_chain, Int inc_current, Int inc_end,
vertexArray* dec_chain, Int dec_current, Int dec_end,
primStream* pStream)
{
Real** inc_array ;
Real** dec_array ;
Int i;
if(inc_current > inc_end && dec_current>dec_end)
return;
else if(inc_current>inc_end) /*no more vertices on inc_chain*/
{
dec_array = dec_chain->getArray();
reflexChain rChain(100,0);
/*put the top vertex into the reflex chain*/
rChain.processNewVertex(topVertex, pStream);
/*process all the vertices on the dec_chain*/
for(i=dec_current; i<=dec_end; i++){
rChain.processNewVertex(dec_array[i], pStream);
}
/*process the bottom vertex*/
rChain.processNewVertex(botVertex, pStream);
}
else if(dec_current> dec_end) /*no more vertices on dec_chain*/
{
inc_array = inc_chain->getArray();
reflexChain rChain(100,1);
/*put the top vertex into the reflex chain*/
rChain.processNewVertex(topVertex, pStream);
/*process all the vertices on the inc_chain*/
for(i=inc_current; i<=inc_end; i++){
rChain.processNewVertex(inc_array[i], pStream);
}
/*process the bottom vertex*/
rChain.processNewVertex(botVertex, pStream);
}
else /*neither chain is empty*/
{
inc_array = inc_chain -> getArray();
dec_array = dec_chain -> getArray();
/*if top of inc_chain is 'lower' than top of dec_chain, process all the
*vertices on the dec_chain which are higher than top of inc_chain
*/
if(compV2InY(inc_array[inc_current], dec_array[dec_current]) <= 0)
{
reflexChain rChain(100, 0);
rChain.processNewVertex(topVertex, pStream);
for(i=dec_current; i<=dec_end; i++)
{
if(compV2InY(inc_array[inc_current], dec_array[i]) <= 0)
rChain.processNewVertex(dec_array[i], pStream);
else
break;
}
rChain.outputFan(inc_array[inc_current], pStream);
monoTriangulationRecGen(dec_array[i-1], botVertex,
inc_chain, inc_current, inc_end,
dec_chain, i, dec_end,
pStream);
}
else /*compV2InY(inc_array[inc_current], dec_array[dec_current]) > 0*/
{
reflexChain rChain(100, 1);
rChain.processNewVertex(topVertex, pStream);
for(i=inc_current; i<=inc_end; i++)
{
if(compV2InY(inc_array[i], dec_array[dec_current]) >0)
rChain.processNewVertex(inc_array[i], pStream);
else
break;
}
rChain.outputFan(dec_array[dec_current], pStream);
monoTriangulationRecGen(inc_array[i-1], botVertex,
inc_chain, i, inc_end,
dec_chain, dec_current,dec_end,
pStream);
}
}/*end case neither is empty*/
}
void monoTriangulationFun(directedLine* monoPolygon, Int (*compFun)(Real*, Real*), primStream* pStream)
{
Int i;
/*find the top vertex, bottom vertex, inccreasing chain, and decreasing chain,
*then call monoTriangulationRec
*/
directedLine* tempV;
directedLine* topV;
directedLine* botV;
topV = botV = monoPolygon;
for(tempV = monoPolygon->getNext(); tempV != monoPolygon; tempV = tempV->getNext())
{
if(compFun(topV->head(), tempV->head())<0) {
topV = tempV;
}
if(compFun(botV->head(), tempV->head())>0) {
botV = tempV;
}
}
/*creat increase and decrease chains*/
vertexArray inc_chain(20); /*this is a dynamic array*/
for(i=1; i<=topV->get_npoints()-2; i++) { /*the first vertex is the top vertex which doesn't belong to inc_chain*/
inc_chain.appendVertex(topV->getVertex(i));
}
for(tempV = topV->getNext(); tempV != botV; tempV = tempV->getNext())
{
for(i=0; i<=tempV->get_npoints()-2; i++){
inc_chain.appendVertex(tempV->getVertex(i));
}
}
vertexArray dec_chain(20);
for(tempV = topV->getPrev(); tempV != botV; tempV = tempV->getPrev())
{
for(i=tempV->get_npoints()-2; i>=0; i--){
dec_chain.appendVertex(tempV->getVertex(i));
}
}
for(i=botV->get_npoints()-2; i>=1; i--){
dec_chain.appendVertex(tempV->getVertex(i));
}
if (!(0 == inc_chain.getNumElements() && 0 == dec_chain.getNumElements())) {
monoTriangulationRecFun(topV->head(), botV->head(), &inc_chain, 0,
&dec_chain, 0, compFun, pStream);
}
}
void monoTriangulation(directedLine* monoPolygon, primStream* pStream)
{
Int i;
/*find the top vertex, bottom vertex, inccreasing chain, and decreasing chain,
*then call monoTriangulationRec
*/
directedLine* tempV;
directedLine* topV;
directedLine* botV;
topV = botV = monoPolygon;
for(tempV = monoPolygon->getNext(); tempV != monoPolygon; tempV = tempV->getNext())
{
if(compV2InY(topV->head(), tempV->head())<0) {
topV = tempV;
}
if(compV2InY(botV->head(), tempV->head())>0) {
botV = tempV;
}
}
/*creat increase and decrease chains*/
vertexArray inc_chain(20); /*this is a dynamic array*/
for(i=1; i<=topV->get_npoints()-2; i++) { /*the first vertex is the top vertex which doesn't belong to inc_chain*/
inc_chain.appendVertex(topV->getVertex(i));
}
for(tempV = topV->getNext(); tempV != botV; tempV = tempV->getNext())
{
for(i=0; i<=tempV->get_npoints()-2; i++){
inc_chain.appendVertex(tempV->getVertex(i));
}
}
vertexArray dec_chain(20);
for(tempV = topV->getPrev(); tempV != botV; tempV = tempV->getPrev())
{
for(i=tempV->get_npoints()-2; i>=0; i--){
dec_chain.appendVertex(tempV->getVertex(i));
}
}
for(i=botV->get_npoints()-2; i>=1; i--){
dec_chain.appendVertex(tempV->getVertex(i));
}
monoTriangulationRec(topV->head(), botV->head(), &inc_chain, 0, &dec_chain, 0, pStream);
}
/*the chain could be increasing or decreasing, although we use the
* name inc_chain.
*the argument is_increase_chain indicates whether this chain
*is increasing (left chain in V-monotone case) or decreaing (right chain
*in V-monotone case).
*/
void monoTriangulation2(Real* topVertex, Real* botVertex,
vertexArray* inc_chain, Int inc_smallIndex,
Int inc_largeIndex,
Int is_increase_chain,
primStream* pStream)
{
assert( inc_chain != NULL);
Real** inc_array ;
if(inc_smallIndex > inc_largeIndex)
return; //no triangles
if(inc_smallIndex == inc_largeIndex)
{
if(is_increase_chain)
pStream->triangle(inc_chain->getVertex(inc_smallIndex), botVertex, topVertex);
else
pStream->triangle(inc_chain->getVertex(inc_smallIndex), topVertex, botVertex);
return;
}
Int i;
if(is_increase_chain && botVertex[1] == inc_chain->getVertex(inc_largeIndex)[1])
{
pStream->triangle(botVertex, inc_chain->getVertex(inc_largeIndex-1),
inc_chain->getVertex(inc_largeIndex));
monoTriangulation2(topVertex, botVertex, inc_chain, inc_smallIndex,
inc_largeIndex-1,
is_increase_chain,
pStream);
return;
}
else if( (!is_increase_chain) && topVertex[1] == inc_chain->getVertex(inc_smallIndex)[1])
{
pStream->triangle(topVertex, inc_chain->getVertex(inc_smallIndex+1),
inc_chain->getVertex(inc_smallIndex));
monoTriangulation2(topVertex, botVertex, inc_chain, inc_smallIndex+1,
inc_largeIndex, is_increase_chain, pStream);
return ;
}
inc_array = inc_chain->getArray();
reflexChain rChain(20,is_increase_chain); /*1 means the chain is increasing*/
rChain.processNewVertex(topVertex, pStream);
for(i=inc_smallIndex; i<=inc_largeIndex; i++){
rChain.processNewVertex(inc_array[i], pStream);
}
rChain.processNewVertex(botVertex, pStream);
}
/*if compFun == compV2InY, top to bottom: V-monotone
*if compFun == compV2InX, right to left: U-monotone
*/
void monoTriangulationRecFunGen(Real* topVertex, Real* botVertex,
vertexArray* inc_chain, Int inc_current, Int inc_end,
vertexArray* dec_chain, Int dec_current, Int dec_end,
Int (*compFun)(Real*, Real*),
primStream* pStream)
{
assert( inc_chain != NULL && dec_chain != NULL);
assert( ! (inc_current> inc_end &&
dec_current> dec_end));
/*
Int inc_nVertices;
Int dec_nVertices;
*/
Real** inc_array ;
Real** dec_array ;
Int i;
assert( ! ( (inc_chain==NULL) && (dec_chain==NULL)));
if(inc_current> inc_end) /*no more vertices on inc_chain*/
{
dec_array = dec_chain->getArray();
reflexChain rChain(20,0);
/*put the top vertex into the reflex chain*/
rChain.processNewVertex(topVertex, pStream);
/*process all the vertices on the dec_chain*/
for(i=dec_current; i<=dec_end; i++){
rChain.processNewVertex(dec_array[i], pStream);
}
/*process the bottom vertex*/
rChain.processNewVertex(botVertex, pStream);
}
else if(dec_current> dec_end) /*no more vertices on dec_chain*/
{
inc_array = inc_chain->getArray();
reflexChain rChain(20,1);
/*put the top vertex into the reflex chain*/
rChain.processNewVertex(topVertex, pStream);
/*process all the vertices on the inc_chain*/
for(i=inc_current; i<=inc_end; i++){
rChain.processNewVertex(inc_array[i], pStream);
}
/*process the bottom vertex*/
rChain.processNewVertex(botVertex, pStream);
}
else /*neither chain is empty*/
{
inc_array = inc_chain -> getArray();
dec_array = dec_chain -> getArray();
/*if top of inc_chain is 'lower' than top of dec_chain, process all the
*vertices on the dec_chain which are higher than top of inc_chain
*/
if(compFun(inc_array[inc_current], dec_array[dec_current]) <= 0)
{
reflexChain rChain(20, 0);
rChain.processNewVertex(topVertex, pStream);
for(i=dec_current; i<=dec_end; i++)
{
if(compFun(inc_array[inc_current], dec_array[i]) <= 0)
rChain.processNewVertex(dec_array[i], pStream);
else
break;
}
rChain.outputFan(inc_array[inc_current], pStream);
monoTriangulationRecFunGen(dec_array[i-1], botVertex,
inc_chain, inc_current, inc_end,
dec_chain, i, dec_end,
compFun,
pStream);
}
else /*compFun(inc_array[inc_current], dec_array[dec_current]) > 0*/
{
reflexChain rChain(20, 1);
rChain.processNewVertex(topVertex, pStream);
for(i=inc_current; i<=inc_end; i++)
{
if(compFun(inc_array[i], dec_array[dec_current]) >0)
rChain.processNewVertex(inc_array[i], pStream);
else
break;
}
rChain.outputFan(dec_array[dec_current], pStream);
monoTriangulationRecFunGen(inc_array[i-1], botVertex,
inc_chain, i,inc_end,
dec_chain, dec_current,dec_end,
compFun,
pStream);
}
}/*end case neither is empty*/
}
/*if compFun == compV2InY, top to bottom: V-monotone
*if compFun == compV2InX, right to left: U-monotone
*/
void monoTriangulationRecFun(Real* topVertex, Real* botVertex,
vertexArray* inc_chain, Int inc_current,
vertexArray* dec_chain, Int dec_current,
Int (*compFun)(Real*, Real*),
primStream* pStream)
{
assert( inc_chain != NULL && dec_chain != NULL);
assert( ! (inc_current>=inc_chain->getNumElements() &&
dec_current>=dec_chain->getNumElements()));
Int inc_nVertices;
Int dec_nVertices;
Real** inc_array ;
Real** dec_array ;
Int i;
assert( ! ( (inc_chain==NULL) && (dec_chain==NULL)));
if(inc_current>=inc_chain->getNumElements()) /*no more vertices on inc_chain*/
{
dec_array = dec_chain->getArray();
dec_nVertices = dec_chain->getNumElements();
reflexChain rChain(20,0);
/*put the top vertex into the reflex chain*/
rChain.processNewVertex(topVertex, pStream);
/*process all the vertices on the dec_chain*/
for(i=dec_current; i<dec_nVertices; i++){
rChain.processNewVertex(dec_array[i], pStream);
}
/*process the bottom vertex*/
rChain.processNewVertex(botVertex, pStream);
}
else if(dec_current>= dec_chain->getNumElements()) /*no more vertices on dec_chain*/
{
inc_array = inc_chain->getArray();
inc_nVertices= inc_chain->getNumElements();
reflexChain rChain(20,1);
/*put the top vertex into the reflex chain*/
rChain.processNewVertex(topVertex, pStream);
/*process all the vertices on the inc_chain*/
for(i=inc_current; i<inc_nVertices; i++){
rChain.processNewVertex(inc_array[i], pStream);
}
/*process the bottom vertex*/
rChain.processNewVertex(botVertex, pStream);
}
else /*neither chain is empty*/
{
inc_array = inc_chain -> getArray();
dec_array = dec_chain -> getArray();
inc_nVertices= inc_chain->getNumElements();
dec_nVertices= dec_chain->getNumElements();
/*if top of inc_chain is 'lower' than top of dec_chain, process all the
*vertices on the dec_chain which are higher than top of inc_chain
*/
if(compFun(inc_array[inc_current], dec_array[dec_current]) <= 0)
{
reflexChain rChain(20, 0);
rChain.processNewVertex(topVertex, pStream);
for(i=dec_current; i<dec_nVertices; i++)
{
if(compFun(inc_array[inc_current], dec_array[i]) <= 0)
rChain.processNewVertex(dec_array[i], pStream);
else
break;
}
rChain.outputFan(inc_array[inc_current], pStream);
monoTriangulationRecFun(dec_array[i-1], botVertex,
inc_chain, inc_current,
dec_chain, i,
compFun,
pStream);
}
else /*compFun(inc_array[inc_current], dec_array[dec_current]) > 0*/
{
reflexChain rChain(20, 1);
rChain.processNewVertex(topVertex, pStream);
for(i=inc_current; i<inc_nVertices; i++)
{
if(compFun(inc_array[i], dec_array[dec_current]) >0)
rChain.processNewVertex(inc_array[i], pStream);
else
break;
}
rChain.outputFan(dec_array[dec_current], pStream);
monoTriangulationRecFun(inc_array[i-1], botVertex,
inc_chain, i,
dec_chain, dec_current,
compFun,
pStream);
}
}/*end case neither is empty*/
}
void monoTriangulationRec(Real* topVertex, Real* botVertex,
vertexArray* inc_chain, Int inc_current,
vertexArray* dec_chain, Int dec_current,
primStream* pStream)
{
assert( inc_chain != NULL && dec_chain != NULL);
assert( ! (inc_current>=inc_chain->getNumElements() &&
dec_current>=dec_chain->getNumElements()));
Int inc_nVertices;
Int dec_nVertices;
Real** inc_array ;
Real** dec_array ;
Int i;
assert( ! ( (inc_chain==NULL) && (dec_chain==NULL)));
if(inc_current>=inc_chain->getNumElements()) /*no more vertices on inc_chain*/
{
dec_array = dec_chain->getArray();
dec_nVertices = dec_chain->getNumElements();
reflexChain rChain(20,0);
/*put the top vertex into the reflex chain*/
rChain.processNewVertex(topVertex, pStream);
/*process all the vertices on the dec_chain*/
for(i=dec_current; i<dec_nVertices; i++){
rChain.processNewVertex(dec_array[i], pStream);
}
/*process the bottom vertex*/
rChain.processNewVertex(botVertex, pStream);
}
else if(dec_current>= dec_chain->getNumElements()) /*no more vertices on dec_chain*/
{
inc_array = inc_chain->getArray();
inc_nVertices= inc_chain->getNumElements();
reflexChain rChain(20,1);
/*put the top vertex into the reflex chain*/
rChain.processNewVertex(topVertex, pStream);
/*process all the vertices on the inc_chain*/
for(i=inc_current; i<inc_nVertices; i++){
rChain.processNewVertex(inc_array[i], pStream);
}
/*process the bottom vertex*/
rChain.processNewVertex(botVertex, pStream);
}
else /*neither chain is empty*/
{
inc_array = inc_chain -> getArray();
dec_array = dec_chain -> getArray();
inc_nVertices= inc_chain->getNumElements();
dec_nVertices= dec_chain->getNumElements();
/*if top of inc_chain is 'lower' than top of dec_chain, process all the
*vertices on the dec_chain which are higher than top of inc_chain
*/
if(compV2InY(inc_array[inc_current], dec_array[dec_current]) <= 0)
{
reflexChain rChain(20, 0);
rChain.processNewVertex(topVertex, pStream);
for(i=dec_current; i<dec_nVertices; i++)
{
if(compV2InY(inc_array[inc_current], dec_array[i]) <= 0)
rChain.processNewVertex(dec_array[i], pStream);
else
break;
}
rChain.outputFan(inc_array[inc_current], pStream);
monoTriangulationRec(dec_array[i-1], botVertex,
inc_chain, inc_current,
dec_chain, i,
pStream);
}
else /*compV2InY(inc_array[inc_current], dec_array[dec_current]) > 0*/
{
reflexChain rChain(20, 1);
rChain.processNewVertex(topVertex, pStream);
for(i=inc_current; i<inc_nVertices; i++)
{
if(compV2InY(inc_array[i], dec_array[dec_current]) >0)
rChain.processNewVertex(inc_array[i], pStream);
else
break;
}
rChain.outputFan(dec_array[dec_current], pStream);
monoTriangulationRec(inc_array[i-1], botVertex,
inc_chain, i,
dec_chain, dec_current,
pStream);
}
}/*end case neither is empty*/
}
/* the name here assumes that the polygon is Y-monotone, but
*this function also works for X-monotone polygons.
* a monotne polygon consists of two extrem verteices: topVertex and botVertex, and
*two monotone chains: inc_chain, and dec_chain. The edges of the increasing chain (inc_chain)
*is ordered by following pointer: next, while the edges of the decreasing chain (dec_chain)
*is ordered by following pointer: prev
* inc_index index the vertex which is the toppest of the inc_chain which we are handling currently.
* dec_index index the vertex which is the toppest of the dec_chain which we are handling currently.
*/
void monoTriangulationRec(directedLine* inc_chain, Int inc_index,
directedLine* dec_chain, Int dec_index,
directedLine* topVertex, Int top_index,
directedLine* botVertex,
primStream* pStream)
{
Int i;
directedLine *temp, *oldtemp = NULL;
Int tempIndex, oldtempIndex = 0;
assert(inc_chain != NULL && dec_chain != NULL);
if(inc_chain == botVertex) {
reflexChain rChain(20, 0);
rChain.processNewVertex(topVertex->getVertex(top_index), pStream);
for(i=dec_index; i< dec_chain->get_npoints(); i++){
rChain.processNewVertex(dec_chain->getVertex(i), pStream);
}
for(temp = dec_chain->getPrev(); temp != botVertex; temp = temp->getPrev())
{
for(i=0; i<temp->get_npoints(); i++){
rChain.processNewVertex(temp->getVertex(i), pStream);
}
}
}
else if(dec_chain==botVertex) {
reflexChain rChain(20, 1);
rChain.processNewVertex(topVertex->getVertex(top_index), pStream);
for(i=inc_index; i< inc_chain->get_npoints(); i++){
rChain.processNewVertex(inc_chain->getVertex(i), pStream);
}
for(temp = inc_chain->getPrev(); temp != botVertex; temp = temp->getNext())
{
for(i=0; i<temp->get_npoints(); i++){
rChain.processNewVertex(temp->getVertex(i), pStream);
}
}
}
else /*neither reached the bottom*/{
if(compV2InY(inc_chain->getVertex(inc_index), dec_chain->getVertex(dec_index)) <=0) {
reflexChain rChain(20, 0);
rChain.processNewVertex(topVertex -> getVertex(top_index), pStream);
temp = dec_chain;
tempIndex = dec_index;
while( compV2InY(inc_chain->getVertex(inc_index), temp->getVertex(tempIndex))<=0) {
oldtemp = temp;
oldtempIndex = tempIndex;
rChain.processNewVertex(temp->getVertex(tempIndex), pStream);
if(tempIndex == temp->get_npoints()-1){
tempIndex = 0;
temp = temp->getPrev();
}
else{
tempIndex++;
}
}
rChain.outputFan(inc_chain->getVertex(inc_index), pStream);
monoTriangulationRec(inc_chain, inc_index, temp, tempIndex, oldtemp, oldtempIndex, botVertex, pStream);
}
else /* >0*/ {
reflexChain rChain(20, 1);
rChain.processNewVertex(topVertex -> getVertex(top_index), pStream);
temp = inc_chain;
tempIndex = inc_index;
while( compV2InY(temp->getVertex(tempIndex), dec_chain->getVertex(dec_index))>0){
oldtemp = temp;
oldtempIndex = tempIndex;
rChain.processNewVertex(temp->getVertex(tempIndex), pStream);
if(tempIndex == temp->get_npoints()-1){
tempIndex = 0;
temp = temp->getNext();
}
else{
tempIndex++;
}
}
rChain.outputFan(dec_chain->getVertex(dec_index), pStream);
monoTriangulationRec(temp, tempIndex, dec_chain, dec_index, oldtemp, oldtempIndex, botVertex, pStream);
}
} /*end case neither reached the bottom*/
}
/***************************vertexArray begin here**********************************/
vertexArray::vertexArray(Real2* vertices, Int nVertices)
{
Int i;
size = index = nVertices;
array = (Real**) malloc(sizeof(Real*) * nVertices);
assert(array);
for(i=0; i<nVertices; i++)
{
array[i] = vertices[i];
array[i] = vertices[i];
}
}
vertexArray::vertexArray(Int s)
{
size = s;
array = (Real**) malloc(sizeof(Real*) * s);
assert(array);
index = 0;
}
vertexArray::~vertexArray()
{
free(array);
}
void vertexArray::appendVertex(Real* ptr)
{
Int i;
if(index >= size){
Real** temp = (Real**) malloc(sizeof(Real*) * (2*size +1));
assert(temp);
for(i=0; i<index; i++)
temp[i] = array[i];
free(array);
array = temp;
size = 2*size+1;
}
array[index++] = ptr;
}
void vertexArray::print()
{
printf("vertex Array:index=%i, size=%i\n", index, size);
for(Int i=0; i<index; i++)
{
printf("(%f,%f) ", array[i][0], array[i][1]);
}
printf("\n");
}
/*find the first i such that array[i][1] >= v
* and array[i+1][1] <v
* if index == 0 (the array is empty, return -1.
* if v is above all, return -1.
* if v is below all, return index-1.
*/
Int vertexArray::findIndexAbove(Real v)
{
Int i;
if(index == 0)
return -1;
else if(array[0][1] < v)
return -1;
else
{
for(i=1; i<index; i++)
{
if(array[i][1] < v)
break;
}
return i-1;
}
}
/*find the first i<=endIndex such that array[i][1] <= v
* and array[i-1][1] > v
*if sartIndex>endIndex, then return endIndex+1.
*otherwise, startIndex<=endIndex, it is assumed that
* 0<=startIndex<=endIndex<index.
* if v is below all, return endIndex+1
* if v is above all, return startIndex.
*/
Int vertexArray::findIndexBelowGen(Real v, Int startIndex, Int endIndex)
{
Int i;
if(startIndex > endIndex)
return endIndex+1;
else if(array[endIndex][1] > v)
return endIndex+1;
else //now array[endIndex][1] <= v
{
for(i=endIndex-1; i>=startIndex; i--)
{
if(array[i][1] > v)
break;
}
return i+1;
}
}
/*find the first i<=endIndex such that array[i-1][1] >= v
* and array[i][1] < v
*if sartIndex>endIndex, then return endIndex+1.
*otherwise, startIndex<=endIndex, it is assumed that
* 0<=startIndex<=endIndex<index.
* if v is below or equal to all, return endIndex+1
* if v is strictly above all, return startIndex.
*/
Int vertexArray::findIndexStrictBelowGen(Real v, Int startIndex, Int endIndex)
{
Int i;
if(startIndex > endIndex)
return endIndex+1;
else if(array[endIndex][1] >= v)
return endIndex+1;
else //now array[endIndex][1] < v
{
for(i=endIndex-1; i>=startIndex; i--)
{
if(array[i][1] >= v)
break;
}
return i+1;
}
}
/*find the first i>startIndex such that array[i-1][1] > v
* and array[i][1] >=v
*if sartIndex>endIndex, then return startIndex-1.
*otherwise, startIndex<=endIndex, it is assumed that
* 0<=startIndex<=endIndex<index.
* if v is strictly above all, return startIndex-1
* if v is strictly below all, return endIndex.
*/
Int vertexArray::findIndexFirstAboveEqualGen(Real v, Int startIndex, Int endIndex)
{
Int i;
if(startIndex > endIndex)
return startIndex-1;
else if(array[startIndex][1] < v)
return startIndex-1;
else //now array[startIndex][1] >= v
{
for(i=startIndex; i<=endIndex; i++)
{
if(array[i][1] <= v)
break;
}
if(i>endIndex) // v is strictly below all
return endIndex;
else if(array[i][1] == v)
return i;
else
return i-1;
}
}
/*find the first i>=startIndex such that array[i][1] >= v
* and array[i+1][1] <v
*if sartIndex>endIndex, then return startIndex-1.
*otherwise, startIndex<=endIndex, it is assumed that
* 0<=startIndex<=endIndex<index.
* if v is above all, return startIndex-1
* if v is below all, return endIndex.
*/
Int vertexArray::findIndexAboveGen(Real v, Int startIndex, Int endIndex)
{
Int i;
if(startIndex > endIndex)
return startIndex-1;
else if(array[startIndex][1] < v)
return startIndex-1;
else //now array[startIndex][1] >= v
{
for(i=startIndex+1; i<=endIndex; i++)
{
if(array[i][1] < v)
break;
}
return i-1;
}
}
Int vertexArray::findDecreaseChainFromEnd(Int begin, Int end)
{
Int i = end;
Real prevU = array[i][0];
Real thisU;
for(i=end-1; i>=begin; i--){
thisU = array[i][0];
if(thisU < prevU)
prevU = thisU;
else
break;
}
return i;
}
//if(V(start) == v, return start, other wise return the
//last i so that V(i)==v
Int vertexArray::skipEqualityFromStart(Real v, Int start, Int end)
{
Int i;
if(array[start][1] != v)
return start;
//now array[start][1] == v
for(i=start+1; i<= end; i++)
if(array[i][1] != v)
break;
return i-1;
}
/***************************vertexArray end****************************************/
/***************************relfex chain stuff begin here*****************************/
reflexChain::reflexChain(Int size, Int is_increasing)
{
queue = (Real2*) malloc(sizeof(Real2) * size);
assert(queue);
index_queue = 0;
size_queue = size;
isIncreasing = is_increasing;
}
reflexChain::~reflexChain()
{
free(queue);
}
/*put (u,v) at the end of the queue
*pay attention to space
*/
void reflexChain::insert(Real u, Real v)
{
Int i;
if(index_queue >= size_queue) {
Real2 *temp = (Real2*) malloc(sizeof(Real2) * (2*size_queue+1));
assert(temp);
/*copy*/
for(i=0; i<index_queue; i++){
temp[i][0] = queue[i][0];
temp[i][1] = queue[i][1];
}
free(queue);
queue = temp;
size_queue = 2*size_queue + 1;
}
queue[index_queue][0] = u;
queue[index_queue][1] = v;
index_queue ++;
}
void reflexChain::insert(Real v[2])
{
insert(v[0], v[1]);
}
/*
static Real area(Real A[2], Real B[2], Real C[2])
{
Real Bx, By, Cx, Cy;
Bx = B[0] - A[0];
By = B[1] - A[1];
Cx = C[0] - A[0];
Cy = C[1] - A[1];
return Bx*Cy - Cx*By;
}
*/
/*the chain is reflex, and the vertex v is
*on the other side of the chain, so that
*we can outout the fan with v as the
*the center
*/
void reflexChain::outputFan(Real v[2], primStream* pStream)
{
Int i;
pStream->begin();
pStream->insert(v);
if(isIncreasing) {
for(i=0; i<index_queue; i++)
pStream->insert(queue[i]);
}
else {
for(i=index_queue-1; i>=0; i--)
pStream->insert(queue[i]);
}
pStream->end(PRIMITIVE_STREAM_FAN);
}
void reflexChain::processNewVertex(Real v[2], primStream* pStream)
{
Int i,j,k;
Int isReflex;
/*if there are at most one vertex in the queue, then simply insert
*/
if(index_queue <=1){
insert(v);
return;
}
/*there are at least two vertices in the queue*/
j=index_queue-1;
for(i=j; i>=1; i--) {
if(isIncreasing) {
isReflex = (area(queue[i-1], queue[i], v) <= 0.0);
}
else /*decreasing*/{
isReflex = (area(v, queue[i], queue[i-1]) <= 0.0);
}
if(isReflex) {
break;
}
}
/*
*if i<j then vertices: i+1--j are convex
* output triangle fan:
* v, and queue[i], i+1, ..., j
*/
if(i<j)
{
pStream->begin();
pStream->insert(v);
if(isIncreasing) {
for(k=i; k<=j; k++)
pStream->insert(queue[k]);
}
else {
for(k=j; k>=i; k--)
pStream->insert(queue[k]);
}
pStream->end(PRIMITIVE_STREAM_FAN);
}
/*delete vertices i+1--j from the queue*/
index_queue = i+1;
/*finally insert v at the end of the queue*/
insert(v);
}
void reflexChain::print()
{
Int i;
printf("reflex chain: isIncreasing=%i\n", isIncreasing);
for(i=0; i<index_queue; i++) {
printf("(%f,%f) ", queue[i][0], queue[i][1]);
}
printf("\n");
}
|