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
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
|
/*
** 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.
**
*/
/*
*/
#include "gluos.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#ifndef max
#define max(a,b) ((a>b)? a:b)
#endif
#ifndef min
#define min(a,b) ((a>b)? b:a)
#endif
#include <GL/gl.h>
#include "glimports.h"
#include "zlassert.h"
#include "sampleMonoPoly.h"
#include "sampleComp.h"
#include "polyDBG.h"
#include "partitionX.h"
#define ZERO 0.00001
//#define MYDEBUG
//#define SHORTEN_GRID_LINE
//see work/newtess/internal/test/problems
/*split a polygon so that each vertex correcpond to one edge
*the head of the first edge of the returned plygon must be the head of the first
*edge of the origianl polygon. This is crucial for the code in sampleMonoPoly function
*/
directedLine* polygonConvert(directedLine* polygon)
{
int i;
directedLine* ret;
sampledLine* sline;
sline = new sampledLine(2);
sline->setPoint(0, polygon->getVertex(0));
sline->setPoint(1, polygon->getVertex(1));
ret=new directedLine(INCREASING, sline);
for(i=1; i<= polygon->get_npoints()-2; i++)
{
sline = new sampledLine(2);
sline->setPoint(0, polygon->getVertex(i));
sline->setPoint(1, polygon->getVertex(i+1));
ret->insert(new directedLine(INCREASING, sline));
}
for(directedLine *temp = polygon->getNext(); temp != polygon; temp = temp->getNext())
{
for(i=0; i<= temp->get_npoints()-2; i++)
{
sline = new sampledLine(2);
sline->setPoint(0, temp->getVertex(i));
sline->setPoint(1, temp->getVertex(i+1));
ret->insert(new directedLine(INCREASING, sline));
}
}
return ret;
}
void triangulateConvexPolyVertical(directedLine* topV, directedLine* botV, primStream *pStream)
{
Int i,j;
Int n_leftVerts;
Int n_rightVerts;
Real** leftVerts;
Real** rightVerts;
directedLine* tempV;
n_leftVerts = 0;
for(tempV = topV; tempV != botV; tempV = tempV->getNext())
{
n_leftVerts += tempV->get_npoints();
}
n_rightVerts=0;
for(tempV = botV; tempV != topV; tempV = tempV->getNext())
{
n_rightVerts += tempV->get_npoints();
}
Real2* temp_leftVerts = (Real2 *) malloc(sizeof(Real2) * n_leftVerts);
assert(temp_leftVerts);
Real2* temp_rightVerts = (Real2 *) malloc(sizeof(Real2) * n_rightVerts);
assert(temp_rightVerts);
leftVerts = (Real**) malloc(sizeof(Real2*) * n_leftVerts);
assert(leftVerts);
rightVerts = (Real**) malloc(sizeof(Real2*) * n_rightVerts);
assert(rightVerts);
for(i=0; i<n_leftVerts; i++)
leftVerts[i] = temp_leftVerts[i];
for(i=0; i<n_rightVerts; i++)
rightVerts[i] = temp_rightVerts[i];
i=0;
for(tempV = topV; tempV != botV; tempV = tempV->getNext())
{
for(j=1; j<tempV->get_npoints(); j++)
{
leftVerts[i][0] = tempV->getVertex(j)[0];
leftVerts[i][1] = tempV->getVertex(j)[1];
i++;
}
}
n_leftVerts = i;
i=0;
for(tempV = topV->getPrev(); tempV != botV->getPrev(); tempV = tempV->getPrev())
{
for(j=tempV->get_npoints()-1; j>=1; j--)
{
rightVerts[i][0] = tempV->getVertex(j)[0];
rightVerts[i][1] = tempV->getVertex(j)[1];
i++;
}
}
n_rightVerts = i;
triangulateXYMonoTB(n_leftVerts, leftVerts, n_rightVerts, rightVerts, pStream);
free(leftVerts);
free(rightVerts);
free(temp_leftVerts);
free(temp_rightVerts);
}
void triangulateConvexPolyHoriz(directedLine* leftV, directedLine* rightV, primStream *pStream)
{
Int i,j;
Int n_lowerVerts;
Int n_upperVerts;
Real2 *lowerVerts;
Real2 *upperVerts;
directedLine* tempV;
n_lowerVerts=0;
for(tempV = leftV; tempV != rightV; tempV = tempV->getNext())
{
n_lowerVerts += tempV->get_npoints();
}
n_upperVerts=0;
for(tempV = rightV; tempV != leftV; tempV = tempV->getNext())
{
n_upperVerts += tempV->get_npoints();
}
lowerVerts = (Real2 *) malloc(sizeof(Real2) * n_lowerVerts);
assert(n_lowerVerts);
upperVerts = (Real2 *) malloc(sizeof(Real2) * n_upperVerts);
assert(n_upperVerts);
i=0;
for(tempV = leftV; tempV != rightV; tempV = tempV->getNext())
{
for(j=0; j<tempV->get_npoints(); j++)
{
lowerVerts[i][0] = tempV->getVertex(j)[0];
lowerVerts[i][1] = tempV->getVertex(j)[1];
i++;
}
}
i=0;
for(tempV = leftV->getPrev(); tempV != rightV->getPrev(); tempV = tempV->getPrev())
{
for(j=tempV->get_npoints()-1; j>=0; j--)
{
upperVerts[i][0] = tempV->getVertex(j)[0];
upperVerts[i][1] = tempV->getVertex(j)[1];
i++;
}
}
triangulateXYMono(n_upperVerts, upperVerts, n_lowerVerts, lowerVerts, pStream);
free(lowerVerts);
free(upperVerts);
}
void triangulateConvexPoly(directedLine* polygon, Int ulinear, Int vlinear, primStream* pStream)
{
/*find left, right, top , bot
*/
directedLine* tempV;
directedLine* topV;
directedLine* botV;
directedLine* leftV;
directedLine* rightV;
topV = botV = polygon;
for(tempV = polygon->getNext(); tempV != polygon; tempV = tempV->getNext())
{
if(compV2InY(topV->head(), tempV->head())<0) {
topV = tempV;
}
if(compV2InY(botV->head(), tempV->head())>0) {
botV = tempV;
}
}
//find leftV
for(tempV = topV; tempV != botV; tempV = tempV->getNext())
{
if(tempV->tail()[0] >= tempV->head()[0])
break;
}
leftV = tempV;
//find rightV
for(tempV = botV; tempV != topV; tempV = tempV->getNext())
{
if(tempV->tail()[0] <= tempV->head()[0])
break;
}
rightV = tempV;
if(vlinear)
{
triangulateConvexPolyHoriz( leftV, rightV, pStream);
}
else if(ulinear)
{
triangulateConvexPolyVertical(topV, botV, pStream);
}
else
{
if(DBG_is_U_direction(polygon))
{
triangulateConvexPolyHoriz( leftV, rightV, pStream);
}
else
triangulateConvexPolyVertical(topV, botV, pStream);
}
}
/*for debug purpose*/
void drawCorners(
Real* topV, Real* botV,
vertexArray* leftChain,
vertexArray* rightChain,
gridBoundaryChain* leftGridChain,
gridBoundaryChain* rightGridChain,
Int gridIndex1,
Int gridIndex2,
Int leftCornerWhere,
Int leftCornerIndex,
Int rightCornerWhere,
Int rightCornerIndex,
Int bot_leftCornerWhere,
Int bot_leftCornerIndex,
Int bot_rightCornerWhere,
Int bot_rightCornerIndex)
{
Real* leftCornerV;
Real* rightCornerV;
Real* bot_leftCornerV;
Real* bot_rightCornerV;
if(leftCornerWhere == 1)
leftCornerV = topV;
else if(leftCornerWhere == 0)
leftCornerV = leftChain->getVertex(leftCornerIndex);
else
leftCornerV = rightChain->getVertex(leftCornerIndex);
if(rightCornerWhere == 1)
rightCornerV = topV;
else if(rightCornerWhere == 0)
rightCornerV = leftChain->getVertex(rightCornerIndex);
else
rightCornerV = rightChain->getVertex(rightCornerIndex);
if(bot_leftCornerWhere == 1)
bot_leftCornerV = botV;
else if(bot_leftCornerWhere == 0)
bot_leftCornerV = leftChain->getVertex(bot_leftCornerIndex);
else
bot_leftCornerV = rightChain->getVertex(bot_leftCornerIndex);
if(bot_rightCornerWhere == 1)
bot_rightCornerV = botV;
else if(bot_rightCornerWhere == 0)
bot_rightCornerV = leftChain->getVertex(bot_rightCornerIndex);
else
bot_rightCornerV = rightChain->getVertex(bot_rightCornerIndex);
Real topGridV = leftGridChain->get_v_value(gridIndex1);
Real topGridU1 = leftGridChain->get_u_value(gridIndex1);
Real topGridU2 = rightGridChain->get_u_value(gridIndex1);
Real botGridV = leftGridChain->get_v_value(gridIndex2);
Real botGridU1 = leftGridChain->get_u_value(gridIndex2);
Real botGridU2 = rightGridChain->get_u_value(gridIndex2);
glBegin(GL_LINE_STRIP);
glVertex2fv(leftCornerV);
glVertex2f(topGridU1, topGridV);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2fv(rightCornerV);
glVertex2f(topGridU2, topGridV);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2fv(bot_leftCornerV);
glVertex2f(botGridU1, botGridV);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex2fv(bot_rightCornerV);
glVertex2f(botGridU2, botGridV);
glEnd();
}
void toVertexArrays(directedLine* topV, directedLine* botV, vertexArray& leftChain, vertexArray& rightChain)
{
Int i;
directedLine* tempV;
for(i=1; i<=topV->get_npoints()-2; i++) { /*the first vertex is the top vertex which doesn't belong to inc_chain*/
leftChain.appendVertex(topV->getVertex(i));
}
for(tempV = topV->getNext(); tempV != botV; tempV = tempV->getNext())
{
for(i=0; i<=tempV->get_npoints()-2; i++){
leftChain.appendVertex(tempV->getVertex(i));
}
}
for(tempV = topV->getPrev(); tempV != botV; tempV = tempV->getPrev())
{
for(i=tempV->get_npoints()-2; i>=0; i--){
rightChain.appendVertex(tempV->getVertex(i));
}
}
for(i=botV->get_npoints()-2; i>=1; i--){
rightChain.appendVertex(tempV->getVertex(i));
}
}
void findTopAndBot(directedLine* polygon, directedLine*& topV, directedLine*& botV)
{
assert(polygon);
directedLine* tempV;
topV = botV = polygon;
for(tempV = polygon->getNext(); tempV != polygon; tempV = tempV->getNext())
{
if(compV2InY(topV->head(), tempV->head())<0) {
topV = tempV;
}
if(compV2InY(botV->head(), tempV->head())>0) {
botV = tempV;
}
}
}
void findGridChains(directedLine* topV, directedLine* botV,
gridWrap* grid,
gridBoundaryChain*& leftGridChain,
gridBoundaryChain*& rightGridChain)
{
/*find the first(top) and the last (bottom) grid line which intersect the
*this polygon
*/
Int firstGridIndex; /*the index in the grid*/
Int lastGridIndex;
firstGridIndex = (Int) ((topV->head()[1] - grid->get_v_min()) / (grid->get_v_max() - grid->get_v_min()) * (grid->get_n_vlines()-1));
if(botV->head()[1] < grid->get_v_min())
lastGridIndex = 0;
else
lastGridIndex = (Int) ((botV->head()[1] - grid->get_v_min()) / (grid->get_v_max() - grid->get_v_min()) * (grid->get_n_vlines()-1)) + 1;
/*find the interval inside the polygon for each gridline*/
Int *leftGridIndices = (Int*) malloc(sizeof(Int) * (firstGridIndex - lastGridIndex +1));
assert(leftGridIndices);
Int *rightGridIndices = (Int*) malloc(sizeof(Int) * (firstGridIndex - lastGridIndex +1));
assert(rightGridIndices);
Int *leftGridInnerIndices = (Int*) malloc(sizeof(Int) * (firstGridIndex - lastGridIndex +1));
assert(leftGridInnerIndices);
Int *rightGridInnerIndices = (Int*) malloc(sizeof(Int) * (firstGridIndex - lastGridIndex +1));
assert(rightGridInnerIndices);
findLeftGridIndices(topV, firstGridIndex, lastGridIndex, grid, leftGridIndices, leftGridInnerIndices);
findRightGridIndices(topV, firstGridIndex, lastGridIndex, grid, rightGridIndices, rightGridInnerIndices);
leftGridChain = new gridBoundaryChain(grid, firstGridIndex, firstGridIndex-lastGridIndex+1, leftGridIndices, leftGridInnerIndices);
rightGridChain = new gridBoundaryChain(grid, firstGridIndex, firstGridIndex-lastGridIndex+1, rightGridIndices, rightGridInnerIndices);
free(leftGridIndices);
free(rightGridIndices);
free(leftGridInnerIndices);
free(rightGridInnerIndices);
}
void findDownCorners(Real *botVertex,
vertexArray *leftChain, Int leftChainStartIndex, Int leftChainEndIndex,
vertexArray *rightChain, Int rightChainStartIndex, Int rightChainEndIndex,
Real v,
Real uleft,
Real uright,
Int& ret_leftCornerWhere, /*0: left chain, 1: topvertex, 2: rightchain*/
Int& ret_leftCornerIndex, /*useful when ret_leftCornerWhere == 0 or 2*/
Int& ret_rightCornerWhere, /*0: left chain, 1: topvertex, 2: rightchain*/
Int& ret_rightCornerIndex /*useful when ret_leftCornerWhere == 0 or 2*/
)
{
#ifdef MYDEBUG
printf("*************enter find donw corner\n");
printf("finddownCorner: v=%f, uleft=%f, uright=%f\n", v, uleft, uright);
printf("(%i,%i,%i,%i)\n", leftChainStartIndex, leftChainEndIndex,rightChainStartIndex, rightChainEndIndex);
printf("left chain is\n");
leftChain->print();
printf("right chain is\n");
rightChain->print();
#endif
assert(v > botVertex[1]);
Real leftGridPoint[2];
leftGridPoint[0] = uleft;
leftGridPoint[1] = v;
Real rightGridPoint[2];
rightGridPoint[0] = uright;
rightGridPoint[1] = v;
Int i;
Int index1, index2;
index1 = leftChain->findIndexBelowGen(v, leftChainStartIndex, leftChainEndIndex);
index2 = rightChain->findIndexBelowGen(v, rightChainStartIndex, rightChainEndIndex);
if(index2 <= rightChainEndIndex) //index2 was found above
index2 = rightChain->skipEqualityFromStart(v, index2, rightChainEndIndex);
if(index1>leftChainEndIndex && index2 > rightChainEndIndex) /*no point below v on left chain or right chain*/
{
/*the botVertex is the only vertex below v*/
ret_leftCornerWhere = 1;
ret_rightCornerWhere = 1;
}
else if(index1>leftChainEndIndex ) /*index2 <= rightChainEndIndex*/
{
ret_rightCornerWhere = 2; /*on right chain*/
ret_rightCornerIndex = index2;
Real tempMin = rightChain->getVertex(index2)[0];
Int tempI = index2;
for(i=index2+1; i<= rightChainEndIndex; i++)
if(rightChain->getVertex(i)[0] < tempMin)
{
tempI = i;
tempMin = rightChain->getVertex(i)[0];
}
//we consider whether we can use botVertex as left corner. First check
//if (leftGirdPoint, botVertex) interesects right chian or not.
if(DBG_intersectChain(rightChain, rightChainStartIndex,rightChainEndIndex,
leftGridPoint, botVertex))
{
ret_leftCornerWhere = 2;//right
ret_leftCornerIndex = index2; //should use tempI???
}
else if(botVertex[0] < tempMin)
ret_leftCornerWhere = 1; //bot
else
{
ret_leftCornerWhere = 2; //right
ret_leftCornerIndex = tempI;
}
}
else if(index2> rightChainEndIndex) /*index1<=leftChainEndIndex*/
{
ret_leftCornerWhere = 0; /*left chain*/
ret_leftCornerIndex = index1;
/*find the vertex on the left chain with the maximum u,
*either this vertex or the botvertex can be used as the right corner
*/
Int tempI;
//skip those points which are equal to v. (avoid degeneratcy)
for(tempI = index1; tempI <= leftChainEndIndex; tempI++)
if(leftChain->getVertex(tempI)[1] < v)
break;
if(tempI > leftChainEndIndex)
ret_rightCornerWhere = 1;
else
{
Real tempMax = leftChain->getVertex(tempI)[0];
for(i=tempI; i<= leftChainEndIndex; i++)
if(leftChain->getVertex(i)[0] > tempMax)
{
tempI = i;
tempMax = leftChain->getVertex(i)[0];
}
//we consider whether we can use botVertex as a corner. So first we check
//whether (rightGridPoint, botVertex) interescts the left chain or not.
if(DBG_intersectChain(leftChain, leftChainStartIndex,leftChainEndIndex,
rightGridPoint, botVertex))
{
ret_rightCornerWhere = 0;
ret_rightCornerIndex = index1; //should use tempI???
}
else if(botVertex[0] > tempMax)
{
ret_rightCornerWhere = 1;
}
else
{
ret_rightCornerWhere = 0;
ret_rightCornerIndex = tempI;
}
}
}
else /*index1<=leftChainEndIndex and index2 <=rightChainEndIndex*/
{
if(leftChain->getVertex(index1)[1] >= rightChain->getVertex(index2)[1]) /*left point above right point*/
{
ret_leftCornerWhere = 0; /*on left chain*/
ret_leftCornerIndex = index1;
Real tempMax;
Int tempI;
tempI = index1;
tempMax = leftChain->getVertex(index1)[0];
/*find the maximum u for all the points on the left above the right point index2*/
for(i=index1+1; i<= leftChainEndIndex; i++)
{
if(leftChain->getVertex(i)[1] < rightChain->getVertex(index2)[1])
break;
if(leftChain->getVertex(i)[0]>tempMax)
{
tempI = i;
tempMax = leftChain->getVertex(i)[0];
}
}
//we consider if we can use rightChain(index2) as right corner
//we check if (rightChain(index2), rightGidPoint) intersecs left chain or not.
if(DBG_intersectChain(leftChain, leftChainStartIndex,leftChainEndIndex, rightGridPoint, rightChain->getVertex(index2)))
{
ret_rightCornerWhere = 0;
ret_rightCornerIndex = index1; //should use tempI???
}
else if(tempMax >= rightChain->getVertex(index2)[0] ||
tempMax >= uright
)
{
ret_rightCornerWhere = 0; /*on left Chain*/
ret_rightCornerIndex = tempI;
}
else
{
ret_rightCornerWhere = 2; /*on right chain*/
ret_rightCornerIndex = index2;
}
}
else /*left below right*/
{
ret_rightCornerWhere = 2; /*on the right*/
ret_rightCornerIndex = index2;
Real tempMin;
Int tempI;
tempI = index2;
tempMin = rightChain->getVertex(index2)[0];
/*find the minimum u for all the points on the right above the left poitn index1*/
for(i=index2+1; i<= rightChainEndIndex; i++)
{
if( rightChain->getVertex(i)[1] < leftChain->getVertex(index1)[1])
break;
if(rightChain->getVertex(i)[0] < tempMin)
{
tempI = i;
tempMin = rightChain->getVertex(i)[0];
}
}
//we consider if we can use leftchain(index1) as left corner.
//we check if (leftChain(index1) intersects right chian or not
if(DBG_intersectChain(rightChain, rightChainStartIndex, rightChainEndIndex, leftGridPoint, leftChain->getVertex(index1)))
{
ret_leftCornerWhere = 2;
ret_leftCornerIndex = index2; //should use tempI???
}
else if(tempMin <= leftChain->getVertex(index1)[0] ||
tempMin <= uleft)
{
ret_leftCornerWhere = 2; /* on right chain*/
ret_leftCornerIndex = tempI;
}
else
{
ret_leftCornerWhere = 0; /*on left chain*/
ret_leftCornerIndex = index1;
}
}
}
}
void findUpCorners(Real *topVertex,
vertexArray *leftChain, Int leftChainStartIndex, Int leftChainEndIndex,
vertexArray *rightChain, Int rightChainStartIndex, Int rightChainEndIndex,
Real v,
Real uleft,
Real uright,
Int& ret_leftCornerWhere, /*0: left chain, 1: topvertex, 2: rightchain*/
Int& ret_leftCornerIndex, /*useful when ret_leftCornerWhere == 0 or 2*/
Int& ret_rightCornerWhere, /*0: left chain, 1: topvertex, 2: rightchain*/
Int& ret_rightCornerIndex /*useful when ret_leftCornerWhere == 0 or 2*/
)
{
#ifdef MYDEBUG
printf("***********enter findUpCorners\n");
#endif
assert(v < topVertex[1]);
Real leftGridPoint[2];
leftGridPoint[0] = uleft;
leftGridPoint[1] = v;
Real rightGridPoint[2];
rightGridPoint[0] = uright;
rightGridPoint[1] = v;
Int i;
Int index1, index2;
index1 = leftChain->findIndexFirstAboveEqualGen(v, leftChainStartIndex, leftChainEndIndex);
index2 = rightChain->findIndexFirstAboveEqualGen(v, rightChainStartIndex, rightChainEndIndex);
if(index2>= leftChainStartIndex) //index2 was found above
index2 = rightChain->skipEqualityFromStart(v, index2, rightChainEndIndex);
if(index1<leftChainStartIndex && index2 <rightChainStartIndex) /*no point above v on left chain or right chain*/
{
/*the topVertex is the only vertex above v*/
ret_leftCornerWhere = 1;
ret_rightCornerWhere = 1;
}
else if(index1<leftChainStartIndex ) /*index2 >= rightChainStartIndex*/
{
ret_rightCornerWhere = 2; /*on right chain*/
ret_rightCornerIndex = index2;
//find the minimum u on right top, either that, or top, or right[index2] is the left corner
Real tempMin = rightChain->getVertex(index2)[0];
Int tempI = index2;
for(i=index2-1; i>=rightChainStartIndex; i--)
if(rightChain->getVertex(i)[0] < tempMin)
{
tempMin = rightChain->getVertex(i)[0];
tempI = i;
}
//chech whether (leftGridPoint, top) intersects rightchai,
//if yes, use right corner as left corner
//if not, use top or right[tempI] as left corner
if(DBG_intersectChain(rightChain, rightChainStartIndex, rightChainEndIndex,
leftGridPoint, topVertex))
{
ret_leftCornerWhere = 2; //rightChain
ret_leftCornerIndex = index2;
}
else if(topVertex[0] < tempMin)
ret_leftCornerWhere = 1; /*topvertex*/
else
{
ret_leftCornerWhere = 2; //right chain
ret_leftCornerIndex = tempI;
}
}
else if(index2< rightChainStartIndex) /*index1>=leftChainStartIndex*/
{
ret_leftCornerWhere = 0; /*left chain*/
ret_leftCornerIndex = index1;
//find the maximum u on the left top section. either that or topvertex, or left[index1] is the right corner
Real tempMax = leftChain->getVertex(index1)[0];
Int tempI = index1;
for(i=index1-1; i>=leftChainStartIndex; i--){
if(leftChain->getVertex(i)[0] > tempMax)
{
tempMax = leftChain->getVertex(i)[0];
tempI = i;
}
}
//check whether (rightGridPoint, top) intersects leftChain or not
//if yes, we use leftCorner as the right corner
//if not, we use either top or left[tempI] as the right corner
if(DBG_intersectChain(leftChain, leftChainStartIndex,leftChainEndIndex,
rightGridPoint, topVertex))
{
ret_rightCornerWhere = 0; //left chan
ret_rightCornerIndex = index1;
}
else if(topVertex[0] > tempMax)
ret_rightCornerWhere = 1;//topVertex
else
{
ret_rightCornerWhere = 0;//left chain
ret_rightCornerIndex = tempI;
}
}
else /*index1>=leftChainStartIndex and index2 >=rightChainStartIndex*/
{
if(leftChain->getVertex(index1)[1] <= rightChain->getVertex(index2)[1]) /*left point below right point*/
{
ret_leftCornerWhere = 0; /*on left chain*/
ret_leftCornerIndex = index1;
Real tempMax;
Int tempI;
tempI = index1;
tempMax = leftChain->getVertex(index1)[0];
/*find the maximum u for all the points on the left below the right point index2*/
for(i=index1-1; i>= leftChainStartIndex; i--)
{
if(leftChain->getVertex(i)[1] > rightChain->getVertex(index2)[1])
break;
if(leftChain->getVertex(i)[0]>tempMax)
{
tempI = i;
tempMax = leftChain->getVertex(i)[0];
}
}
//chek whether (rightChain(index2), rightGridPoint) intersects leftchian or not
if(DBG_intersectChain(leftChain, leftChainStartIndex, leftChainEndIndex, rightGridPoint, rightChain->getVertex(index2)))
{
ret_rightCornerWhere = 0;
ret_rightCornerIndex = index1;
}
else if(tempMax >= rightChain->getVertex(index2)[0] ||
tempMax >= uright)
{
ret_rightCornerWhere = 0; /*on left Chain*/
ret_rightCornerIndex = tempI;
}
else
{
ret_rightCornerWhere = 2; /*on right chain*/
ret_rightCornerIndex = index2;
}
}
else /*left above right*/
{
ret_rightCornerWhere = 2; /*on the right*/
ret_rightCornerIndex = index2;
Real tempMin;
Int tempI;
tempI = index2;
tempMin = rightChain->getVertex(index2)[0];
/*find the minimum u for all the points on the right below the left poitn index1*/
for(i=index2-1; i>= rightChainStartIndex; i--)
{
if( rightChain->getVertex(i)[1] > leftChain->getVertex(index1)[1])
break;
if(rightChain->getVertex(i)[0] < tempMin)
{
tempI = i;
tempMin = rightChain->getVertex(i)[0];
}
}
//check whether (leftGRidPoint,left(index1)) interesect right chain
if(DBG_intersectChain(rightChain, rightChainStartIndex, rightChainEndIndex,
leftGridPoint, leftChain->getVertex(index1)))
{
ret_leftCornerWhere = 2; //right
ret_leftCornerIndex = index2;
}
else if(tempMin <= leftChain->getVertex(index1)[0] ||
tempMin <= uleft)
{
ret_leftCornerWhere = 2; /* on right chain*/
ret_leftCornerIndex = tempI;
}
else
{
ret_leftCornerWhere = 0; /*on left chain*/
ret_leftCornerIndex = index1;
}
}
}
#ifdef MYDEBUG
printf("***********leave findUpCorners\n");
#endif
}
//return 1 if neck exists, 0 othewise
Int findNeckF(vertexArray *leftChain, Int botLeftIndex,
vertexArray *rightChain, Int botRightIndex,
gridBoundaryChain* leftGridChain,
gridBoundaryChain* rightGridChain,
Int gridStartIndex,
Int& neckLeft,
Int& neckRight)
{
/*
printf("enter findNeckF, botleft, botright=%i,%i,gstartindex=%i\n",botLeftIndex,botRightIndex,gridStartIndex);
printf("leftChain is\n");
leftChain->print();
printf("rightChain is\n");
rightChain->print();
*/
Int lowerGridIndex; //the grid below leftChain and rightChian vertices
Int i;
Int n_vlines = leftGridChain->get_nVlines();
Real v;
if(botLeftIndex >= leftChain->getNumElements() ||
botRightIndex >= rightChain->getNumElements())
return 0; //no neck exists
v=min(leftChain->getVertex(botLeftIndex)[1], rightChain->getVertex(botRightIndex)[1]);
for(i=gridStartIndex; i<n_vlines; i++)
if(leftGridChain->get_v_value(i) <= v &&
leftGridChain->getUlineIndex(i)<= rightGridChain->getUlineIndex(i))
break;
lowerGridIndex = i;
if(lowerGridIndex == n_vlines) //the two trm vertex are higher than all gridlines
return 0;
else
{
Int botLeft2, botRight2;
/*
printf("leftGridChain->get_v_)value=%f\n",leftGridChain->get_v_value(lowerGridIndex), botLeftIndex);
printf("leftChain->get_vertex(0)=(%f,%f)\n", leftChain->getVertex(0)[0],leftChain->getVertex(0)[1]);
printf("leftChain->get_vertex(1)=(%f,%f)\n", leftChain->getVertex(1)[0],leftChain->getVertex(1)[1]);
printf("leftChain->get_vertex(2)=(%f,%f)\n", leftChain->getVertex(2)[0],leftChain->getVertex(2)[1]);
*/
botLeft2 = leftChain->findIndexFirstAboveEqualGen(leftGridChain->get_v_value(lowerGridIndex), botLeftIndex, leftChain->getNumElements()-1) -1 ;
/*
printf("botLeft2=%i\n", botLeft2);
printf("leftChain->getNumElements=%i\n", leftChain->getNumElements());
*/
botRight2 = rightChain->findIndexFirstAboveEqualGen(leftGridChain->get_v_value(lowerGridIndex), botRightIndex, rightChain->getNumElements()-1) -1;
if(botRight2 < botRightIndex) botRight2=botRightIndex;
if(botLeft2 < botLeftIndex) botLeft2 = botLeftIndex;
assert(botLeft2 >= botLeftIndex);
assert(botRight2 >= botRightIndex);
//find nectLeft so that it is th erightmost vertex on letChain
Int tempI = botLeftIndex;
Real temp = leftChain->getVertex(tempI)[0];
for(i=botLeftIndex+1; i<= botLeft2; i++)
if(leftChain->getVertex(i)[0] > temp)
{
temp = leftChain->getVertex(i)[0];
tempI = i;
}
neckLeft = tempI;
tempI = botRightIndex;
temp = rightChain->getVertex(tempI)[0];
for(i=botRightIndex+1; i<= botRight2; i++)
if(rightChain->getVertex(i)[0] < temp)
{
temp = rightChain->getVertex(i)[0];
tempI = i;
}
neckRight = tempI;
return 1;
}
}
/*find i>=botLeftIndex,j>=botRightIndex so that
*(leftChain[i], rightChain[j]) is a neck.
*/
void findNeck(vertexArray *leftChain, Int botLeftIndex,
vertexArray *rightChain, Int botRightIndex,
Int& leftLastIndex, /*left point of the neck*/
Int& rightLastIndex /*right point of the neck*/
)
{
assert(botLeftIndex < leftChain->getNumElements() &&
botRightIndex < rightChain->getNumElements());
/*now the neck exists for sure*/
if(leftChain->getVertex(botLeftIndex)[1] <= rightChain->getVertex(botRightIndex)[1]) //left below right
{
leftLastIndex = botLeftIndex;
/*find i so that rightChain[i][1] >= leftchainbotverte[1], and i+1<
*/
rightLastIndex=rightChain->findIndexAboveGen(leftChain->getVertex(botLeftIndex)[1], botRightIndex+1, rightChain->getNumElements()-1);
}
else //left above right
{
rightLastIndex = botRightIndex;
leftLastIndex = leftChain->findIndexAboveGen(rightChain->getVertex(botRightIndex)[1],
botLeftIndex+1,
leftChain->getNumElements()-1);
}
}
void findLeftGridIndices(directedLine* topEdge, Int firstGridIndex, Int lastGridIndex, gridWrap* grid, Int* ret_indices, Int* ret_innerIndices)
{
Int i,k,isHoriz = 0;
Int n_ulines = grid->get_n_ulines();
Real uMin = grid->get_u_min();
Real uMax = grid->get_u_max();
/*
Real vMin = grid->get_v_min();
Real vMax = grid->get_v_max();
*/
Real slop = 0.0, uinterc;
#ifdef SHORTEN_GRID_LINE
//uintercBuf stores all the interction u value for each grid line
//notice that lastGridIndex<= firstGridIndex
Real *uintercBuf = (Real *) malloc (sizeof(Real) * (firstGridIndex-lastGridIndex+1));
assert(uintercBuf);
#endif
/*initialization to make vtail bigger than grid->...*/
directedLine* dLine = topEdge;
Real vtail = grid->get_v_value(firstGridIndex) + 1.0;
Real tempMaxU = grid->get_u_min();
/*for each grid line*/
for(k=0, i=firstGridIndex; i>=lastGridIndex; i--, k++)
{
Real grid_v_value = grid->get_v_value(i);
/*check whether this grid line is below the current trim edge.*/
if(vtail > grid_v_value)
{
/*since the grid line is below the trim edge, we
*find the trim edge which will contain the trim line
*/
while( (vtail=dLine->tail()[1]) > grid_v_value){
tempMaxU = max(tempMaxU, dLine->tail()[0]);
dLine = dLine -> getNext();
}
if( fabs(dLine->head()[1] - vtail) < ZERO)
isHoriz = 1;
else
{
isHoriz = 0;
slop = (dLine->head()[0] - dLine->tail()[0]) / (dLine->head()[1]-vtail);
}
}
if(isHoriz)
{
uinterc = max(dLine->head()[0], dLine->tail()[0]);
}
else
{
uinterc = slop * (grid_v_value - vtail) + dLine->tail()[0];
}
tempMaxU = max(tempMaxU, uinterc);
if(uinterc < uMin && uinterc >= uMin - ZERO)
uinterc = uMin;
if(uinterc > uMax && uinterc <= uMax + ZERO)
uinterc = uMax;
#ifdef SHORTEN_GRID_LINE
uintercBuf[k] = uinterc;
#endif
assert(uinterc >= uMin && uinterc <= uMax);
if(uinterc == uMax)
ret_indices[k] = n_ulines-1;
else
ret_indices[k] = (Int)(((uinterc-uMin)/(uMax - uMin)) * (n_ulines-1)) + 1;
if(ret_indices[k] >= n_ulines)
ret_indices[k] = n_ulines-1;
ret_innerIndices[k] = (Int)(((tempMaxU-uMin)/(uMax - uMin)) * (n_ulines-1)) + 1;
/*reinitialize tempMaxU for next grdiLine*/
tempMaxU = uinterc;
}
#ifdef SHORTEN_GRID_LINE
//for each grid line, compare the left grid point with the
//intersection point. If the two points are too close, then
//we should move the grid point one grid to the right
//and accordingly we should update the inner index.
for(k=0, i=firstGridIndex; i>=lastGridIndex; i--, k++)
{
//check gridLine i
//check ret_indices[k]
Real a = grid->get_u_value(ret_indices[k]-1);
Real b = grid->get_u_value(ret_indices[k]);
assert(uintercBuf[k] >= a && uintercBuf < b);
if( (b-uintercBuf[k]) <= 0.2 * (b-a)) //interc is very close to b
{
ret_indices[k]++;
}
//check ret_innerIndices[k]
if(k>0)
{
if(ret_innerIndices[k] < ret_indices[k-1])
ret_innerIndices[k] = ret_indices[k-1];
if(ret_innerIndices[k] < ret_indices[k])
ret_innerIndices[k] = ret_indices[k];
}
}
//clean up
free(uintercBuf);
#endif
}
void findRightGridIndices(directedLine* topEdge, Int firstGridIndex, Int lastGridIndex, gridWrap* grid, Int* ret_indices, Int* ret_innerIndices)
{
Int i,k;
Int n_ulines = grid->get_n_ulines();
Real uMin = grid->get_u_min();
Real uMax = grid->get_u_max();
/*
Real vMin = grid->get_v_min();
Real vMax = grid->get_v_max();
*/
Real slop = 0.0, uinterc;
#ifdef SHORTEN_GRID_LINE
//uintercBuf stores all the interction u value for each grid line
//notice that firstGridIndex >= lastGridIndex
Real *uintercBuf = (Real *) malloc (sizeof(Real) * (firstGridIndex-lastGridIndex+1));
assert(uintercBuf);
#endif
/*initialization to make vhead bigger than grid->v_value...*/
directedLine* dLine = topEdge->getPrev();
Real vhead = dLine->tail()[1];
Real tempMinU = grid->get_u_max();
/*for each grid line*/
for(k=0, i=firstGridIndex; i>=lastGridIndex; i--, k++)
{
Real grid_v_value = grid->get_v_value(i);
/*check whether this grid line is below the current trim edge.*/
if(vhead >= grid_v_value)
{
/*since the grid line is below the tail of the trim edge, we
*find the trim edge which will contain the trim line
*/
while( (vhead=dLine->head()[1]) > grid_v_value){
tempMinU = min(tempMinU, dLine->head()[0]);
dLine = dLine -> getPrev();
}
/*skip the equality in the case of degenerat case: horizontal */
while(dLine->head()[1] == grid_v_value)
dLine = dLine->getPrev();
assert( dLine->tail()[1] != dLine->head()[1]);
slop = (dLine->tail()[0] - dLine->head()[0]) / (dLine->tail()[1]-dLine->head()[1]);
/*
if(dLine->tail()[1] == vhead)
isHoriz = 1;
else
{
isHoriz = 0;
slop = (dLine->tail()[0] - dLine->head()[0]) / (dLine->tail()[1]-vhead);
}
*/
}
uinterc = slop * (grid_v_value - dLine->head()[1]) + dLine->head()[0];
//in case unterc is outside of the grid due to floating point
if(uinterc < uMin)
uinterc = uMin;
else if(uinterc > uMax)
uinterc = uMax;
#ifdef SHORTEN_GRID_LINE
uintercBuf[k] = uinterc;
#endif
tempMinU = min(tempMinU, uinterc);
assert(uinterc >= uMin && uinterc <= uMax);
if(uinterc == uMin)
ret_indices[k] = 0;
else
ret_indices[k] = (int)ceil((((uinterc-uMin)/(uMax - uMin)) * (n_ulines-1))) -1;
/*
if(ret_indices[k] >= grid->get_n_ulines())
{
printf("ERROR3\n");
exit(0);
}
if(ret_indices[k] < 0)
{
printf("ERROR4\n");
exit(0);
}
*/
ret_innerIndices[k] = (int)ceil ((((tempMinU-uMin)/(uMax - uMin)) * (n_ulines-1))) -1;
tempMinU = uinterc;
}
#ifdef SHORTEN_GRID_LINE
//for each grid line, compare the left grid point with the
//intersection point. If the two points are too close, then
//we should move the grid point one grid to the right
//and accordingly we should update the inner index.
for(k=0, i=firstGridIndex; i>=lastGridIndex; i--, k++)
{
//check gridLine i
//check ret_indices[k]
Real a = grid->get_u_value(ret_indices[k]);
Real b = grid->get_u_value(ret_indices[k]+1);
assert(uintercBuf[k] > a && uintercBuf <= b);
if( (uintercBuf[k]-a) <= 0.2 * (b-a)) //interc is very close to a
{
ret_indices[k]--;
}
//check ret_innerIndices[k]
if(k>0)
{
if(ret_innerIndices[k] > ret_indices[k-1])
ret_innerIndices[k] = ret_indices[k-1];
if(ret_innerIndices[k] > ret_indices[k])
ret_innerIndices[k] = ret_indices[k];
}
}
//clean up
free(uintercBuf);
#endif
}
void sampleMonoPoly(directedLine* polygon, gridWrap* grid, Int ulinear, Int vlinear, primStream* pStream, rectBlockArray* rbArray)
{
/*
{
grid->print();
polygon->writeAllPolygons("zloutputFile");
exit(0);
}
*/
if(grid->get_n_ulines() == 2 ||
grid->get_n_vlines() == 2)
{
if(ulinear && grid->get_n_ulines() == 2)
{
monoTriangulationFun(polygon, compV2InY, pStream);
return;
}
else if(DBG_isConvex(polygon) && polygon->numEdges() >=4)
{
triangulateConvexPoly(polygon, ulinear, vlinear, pStream);
return;
}
else if(vlinear || DBG_is_U_direction(polygon))
{
Int n_cusps;//num interior cusps
Int n_edges = polygon->numEdges();
directedLine** cusps = (directedLine**) malloc(sizeof(directedLine*) * n_edges);
assert(cusps);
findInteriorCuspsX(polygon, n_cusps, cusps);
if(n_cusps == 0) //u_monotone
{
monoTriangulationFun(polygon, compV2InX, pStream);
free(cusps);
return;
}
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(polygon, compV2InX, pStream);
free(cusps);
return;
}
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();
free(cusps);
return;
}
free(cusps);
}
}
/*find the top and bottom of the polygon. It is supposed to be
*a V-monotone polygon
*/
directedLine* tempV;
directedLine* topV;
directedLine* botV;
topV = botV = polygon;
for(tempV = polygon->getNext(); tempV != polygon; tempV = tempV->getNext())
{
if(compV2InY(topV->head(), tempV->head())<0) {
topV = tempV;
}
if(compV2InY(botV->head(), tempV->head())>0) {
botV = tempV;
}
}
/*find the first(top) and the last (bottom) grid line which intersect the
*this polygon
*/
Int firstGridIndex; /*the index in the grid*/
Int lastGridIndex;
firstGridIndex = (Int) ((topV->head()[1] - grid->get_v_min()) / (grid->get_v_max() - grid->get_v_min()) * (grid->get_n_vlines()-1));
lastGridIndex = (Int) ((botV->head()[1] - grid->get_v_min()) / (grid->get_v_max() - grid->get_v_min()) * (grid->get_n_vlines()-1)) + 1;
/*find the interval inside the polygon for each gridline*/
Int *leftGridIndices = (Int*) malloc(sizeof(Int) * (firstGridIndex - lastGridIndex +1));
assert(leftGridIndices);
Int *rightGridIndices = (Int*) malloc(sizeof(Int) * (firstGridIndex - lastGridIndex +1));
assert(rightGridIndices);
Int *leftGridInnerIndices = (Int*) malloc(sizeof(Int) * (firstGridIndex - lastGridIndex +1));
assert(leftGridInnerIndices);
Int *rightGridInnerIndices = (Int*) malloc(sizeof(Int) * (firstGridIndex - lastGridIndex +1));
assert(rightGridInnerIndices);
findLeftGridIndices(topV, firstGridIndex, lastGridIndex, grid, leftGridIndices, leftGridInnerIndices);
findRightGridIndices(topV, firstGridIndex, lastGridIndex, grid, rightGridIndices, rightGridInnerIndices);
gridBoundaryChain leftGridChain(grid, firstGridIndex, firstGridIndex-lastGridIndex+1, leftGridIndices, leftGridInnerIndices);
gridBoundaryChain rightGridChain(grid, firstGridIndex, firstGridIndex-lastGridIndex+1, rightGridIndices, rightGridInnerIndices);
// leftGridChain.draw();
// leftGridChain.drawInner();
// rightGridChain.draw();
// rightGridChain.drawInner();
/*(1) determine the grid boundaries (left and right).
*(2) process polygon into two monotone chaines: use vertexArray
*(3) call sampleMonoPolyRec
*/
/*copy the two chains into vertexArray datastructure*/
Int i;
vertexArray leftChain(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*/
leftChain.appendVertex(topV->getVertex(i));
}
for(tempV = topV->getNext(); tempV != botV; tempV = tempV->getNext())
{
for(i=0; i<=tempV->get_npoints()-2; i++){
leftChain.appendVertex(tempV->getVertex(i));
}
}
vertexArray rightChain(20);
for(tempV = topV->getPrev(); tempV != botV; tempV = tempV->getPrev())
{
for(i=tempV->get_npoints()-2; i>=0; i--){
rightChain.appendVertex(tempV->getVertex(i));
}
}
for(i=botV->get_npoints()-2; i>=1; i--){
rightChain.appendVertex(tempV->getVertex(i));
}
sampleMonoPolyRec(topV->head(),
botV->head(),
&leftChain,
0,
&rightChain,
0,
&leftGridChain,
&rightGridChain,
0,
pStream,
rbArray);
/*cleanup space*/
free(leftGridIndices);
free(rightGridIndices);
free(leftGridInnerIndices);
free(rightGridInnerIndices);
}
void sampleMonoPolyRec(
Real* topVertex,
Real* botVertex,
vertexArray* leftChain,
Int leftStartIndex,
vertexArray* rightChain,
Int rightStartIndex,
gridBoundaryChain* leftGridChain,
gridBoundaryChain* rightGridChain,
Int gridStartIndex,
primStream* pStream,
rectBlockArray* rbArray)
{
/*find the first connected component, and the four corners.
*/
Int index1, index2; /*the first and last grid line of the first connected component*/
if(topVertex[1] <= botVertex[1])
return;
/*find i so that the grid line is below the top vertex*/
Int i=gridStartIndex;
while (i < leftGridChain->get_nVlines())
{
if(leftGridChain->get_v_value(i) < topVertex[1])
break;
i++;
}
/*find the first connected component starting with i*/
/*find index1 so that left_uline_index <= right_uline_index, that is, this
*grid line contains at least one inner grid point
*/
index1=i;
int num_skipped_grid_lines=0;
while(index1 < leftGridChain->get_nVlines())
{
if(leftGridChain->getUlineIndex(index1) <= rightGridChain->getUlineIndex(index1))
break;
num_skipped_grid_lines++;
index1++;
}
if(index1 >= leftGridChain->get_nVlines()) /*no grid line exists which has inner point*/
{
/*stop recursion, ...*/
/*monotone triangulate it...*/
// printf("no grid line exists\n");
/*
monoTriangulationRecOpt(topVertex, botVertex, leftChain, leftStartIndex,
rightChain, rightStartIndex, pStream);
*/
if(num_skipped_grid_lines <2)
{
monoTriangulationRecGenOpt(topVertex, botVertex, leftChain, leftStartIndex,
leftChain->getNumElements()-1,
rightChain, rightStartIndex,
rightChain->getNumElements()-1,
pStream);
}
else
{
//the optimum way to triangulate is top-down since this polygon
//is narrow-long.
monoTriangulationRec(topVertex, botVertex, leftChain, leftStartIndex,
rightChain, rightStartIndex, pStream);
}
/*
monoTriangulationRec(topVertex, botVertex, leftChain, leftStartIndex,
rightChain, rightStartIndex, pStream);
*/
/* monoTriangulationRecGenTBOpt(topVertex, botVertex,
leftChain, leftStartIndex, leftChain->getNumElements()-1,
rightChain, rightStartIndex, rightChain->getNumElements()-1,
pStream);*/
}
else
{
/*find index2 so that left_inner_index <= right_inner_index holds until index2*/
index2=index1+1;
if(index2 < leftGridChain->get_nVlines())
while(leftGridChain->getInnerIndex(index2) <= rightGridChain->getInnerIndex(index2))
{
index2++;
if(index2 >= leftGridChain->get_nVlines())
break;
}
index2--;
/*the neck*/
Int neckLeftIndex;
Int neckRightIndex;
/*the four corners*/
Int up_leftCornerWhere;
Int up_leftCornerIndex;
Int up_rightCornerWhere;
Int up_rightCornerIndex;
Int down_leftCornerWhere;
Int down_leftCornerIndex;
Int down_rightCornerWhere;
Int down_rightCornerIndex;
Real* tempBotVertex; /*the bottom vertex for this component*/
Real* nextTopVertex=NULL; /*for the recursion*/
Int nextLeftStartIndex=0;
Int nextRightStartIndex=0;
/*find the points below the grid line index2 on both chains*/
Int botLeftIndex = leftChain->findIndexStrictBelowGen(
leftGridChain->get_v_value(index2),
leftStartIndex,
leftChain->getNumElements()-1);
Int botRightIndex = rightChain->findIndexStrictBelowGen(
rightGridChain->get_v_value(index2),
rightStartIndex,
rightChain->getNumElements()-1);
/*if either botLeftIndex>= numelements,
* or botRightIndex >= numelemnet,
*then there is no neck exists. the bottom vertex is botVertex,
*/
if(! findNeckF(leftChain, botLeftIndex, rightChain, botRightIndex,
leftGridChain, rightGridChain, index2, neckLeftIndex, neckRightIndex))
/*
if(botLeftIndex == leftChain->getNumElements() ||
botRightIndex == rightChain->getNumElements())
*/
{
#ifdef MYDEBUG
printf("neck NOT exists, botRightIndex=%i\n", botRightIndex);
#endif
tempBotVertex = botVertex;
nextTopVertex = botVertex;
botLeftIndex = leftChain->getNumElements()-1;
botRightIndex = rightChain->getNumElements()-1;
}
else /*neck exists*/
{
#ifdef MYDEBUG
printf("neck exists\n");
#endif
/*
findNeck(leftChain, botLeftIndex,
rightChain, botRightIndex,
neckLeftIndex,
neckRightIndex);
*/
#ifdef MYDEBUG
printf("neck is found, neckLeftIndex=%i, neckRightIndex=%i\n", neckLeftIndex, neckRightIndex);
glBegin(GL_LINES);
glVertex2fv(leftChain->getVertex(neckLeftIndex));
glVertex2fv(rightChain->getVertex(neckRightIndex));
glEnd();
#endif
if(leftChain->getVertex(neckLeftIndex)[1] <= rightChain->getVertex(neckRightIndex)[1])
{
tempBotVertex = leftChain->getVertex(neckLeftIndex);
botLeftIndex = neckLeftIndex-1;
botRightIndex = neckRightIndex;
nextTopVertex = rightChain->getVertex(neckRightIndex);
nextLeftStartIndex = neckLeftIndex;
nextRightStartIndex = neckRightIndex+1;
}
else
{
tempBotVertex = rightChain->getVertex(neckRightIndex);
botLeftIndex = neckLeftIndex;
botRightIndex = neckRightIndex-1;
nextTopVertex = leftChain->getVertex(neckLeftIndex);
nextLeftStartIndex = neckLeftIndex+1;
nextRightStartIndex = neckRightIndex;
}
}
findUpCorners(topVertex,
leftChain,
leftStartIndex, botLeftIndex,
rightChain,
rightStartIndex, botRightIndex,
leftGridChain->get_v_value(index1),
leftGridChain->get_u_value(index1),
rightGridChain->get_u_value(index1),
up_leftCornerWhere,
up_leftCornerIndex,
up_rightCornerWhere,
up_rightCornerIndex);
findDownCorners(tempBotVertex,
leftChain,
leftStartIndex, botLeftIndex,
rightChain,
rightStartIndex, botRightIndex,
leftGridChain->get_v_value(index2),
leftGridChain->get_u_value(index2),
rightGridChain->get_u_value(index2),
down_leftCornerWhere,
down_leftCornerIndex,
down_rightCornerWhere,
down_rightCornerIndex);
#ifdef MYDEBUG
printf("find corners done, down_leftwhere=%i, down_righwhere=%i,\n",down_leftCornerWhere, down_rightCornerWhere );
printf("find corners done, up_leftwhere=%i, up_righwhere=%i,\n",up_leftCornerWhere, up_rightCornerWhere );
printf("find corners done, up_leftindex=%i, up_righindex=%i,\n",up_leftCornerIndex, up_rightCornerIndex );
printf("find corners done, down_leftindex=%i, down_righindex=%i,\n",down_leftCornerIndex, down_rightCornerIndex );
#endif
/*
drawCorners(topVertex,
tempBotVertex,
leftChain,
rightChain,
leftGridChain,
rightGridChain,
index1,
index2,
up_leftCornerWhere,
up_leftCornerIndex,
up_rightCornerWhere,
up_rightCornerIndex,
down_leftCornerWhere,
down_leftCornerIndex,
down_rightCornerWhere,
down_rightCornerIndex);
*/
sampleConnectedComp(topVertex, tempBotVertex,
leftChain,
leftStartIndex, botLeftIndex,
rightChain,
rightStartIndex, botRightIndex,
leftGridChain,
rightGridChain,
index1, index2,
up_leftCornerWhere,
up_leftCornerIndex,
up_rightCornerWhere,
up_rightCornerIndex,
down_leftCornerWhere,
down_leftCornerIndex,
down_rightCornerWhere,
down_rightCornerIndex,
pStream,
rbArray
);
/*recursion*/
sampleMonoPolyRec(
nextTopVertex,
botVertex,
leftChain,
nextLeftStartIndex,
rightChain,
nextRightStartIndex,
leftGridChain,
rightGridChain,
index2+1,
pStream, rbArray);
}
}
void sampleLeftStrip(vertexArray* leftChain,
Int topLeftIndex,
Int botLeftIndex,
gridBoundaryChain* leftGridChain,
Int leftGridChainStartIndex,
Int leftGridChainEndIndex,
primStream* pStream
)
{
assert(leftChain->getVertex(topLeftIndex)[1] > leftGridChain->get_v_value(leftGridChainStartIndex));
assert(leftChain->getVertex(topLeftIndex+1)[1] <= leftGridChain->get_v_value(leftGridChainStartIndex));
assert(leftChain->getVertex(botLeftIndex)[1] <= leftGridChain->get_v_value(leftGridChainEndIndex));
assert(leftChain->getVertex(botLeftIndex-1)[1] > leftGridChain->get_v_value(leftGridChainEndIndex));
/*
*(1)find the last grid line which doesn'; pass below
* this first edge, sample this region: one trim edge and
* possily multiple grid lines.
*/
Real *upperVert, *lowerVert; /*the end points of the first trim edge*/
upperVert = leftChain->getVertex(topLeftIndex);
lowerVert = leftChain->getVertex(topLeftIndex+1);
Int index = leftGridChainStartIndex;
while(leftGridChain->get_v_value(index) >= lowerVert[1]){
index++;
if(index > leftGridChainEndIndex)
break;
}
index--;
sampleLeftSingleTrimEdgeRegion(upperVert, lowerVert,
leftGridChain,
leftGridChainStartIndex,
index,
pStream);
sampleLeftStripRec(leftChain, topLeftIndex+1, botLeftIndex,
leftGridChain, index, leftGridChainEndIndex,
pStream);
}
void sampleLeftStripRec(vertexArray* leftChain,
Int topLeftIndex,
Int botLeftIndex,
gridBoundaryChain* leftGridChain,
Int leftGridChainStartIndex,
Int leftGridChainEndIndex,
primStream* pStream
)
{
/*now top left trim vertex is below the top grid line.
*/
/*stop condition: if topLeftIndex >= botLeftIndex, then stop.
*/
if(topLeftIndex >= botLeftIndex)
return;
/*find the last trim vertex which is above the second top grid line:
* index1.
*and sampleLeftOneGridStep(leftchain, topLeftIndex, index1, leftGridChain,
* leftGridChainStartIndex).
* index1 could be equal to topLeftIndex.
*/
Real secondGridChainV = leftGridChain->get_v_value(leftGridChainStartIndex+1);
assert(leftGridChainStartIndex < leftGridChainEndIndex);
Int index1 = topLeftIndex;
while(leftChain->getVertex(index1)[1] > secondGridChainV)
index1++;
index1--;
sampleLeftOneGridStep(leftChain, topLeftIndex, index1, leftGridChain, leftGridChainStartIndex, pStream);
/*
* Let the next trim vertex be nextTrimVertIndex (which should be
* below the second grid line).
* Find the last grid line index2 which is above nextTrimVert.
* sampleLeftSingleTrimEdgeRegion(uppervert[2], lowervert[2],
* leftGridChain, leftGridChainStartIndex+1, index2).
*/
Real *uppervert, *lowervert;
uppervert = leftChain->getVertex(index1);
lowervert = leftChain->getVertex(index1+1);
Int index2 = leftGridChainStartIndex+1;
while(leftGridChain->get_v_value(index2) >= lowervert[1])
{
index2++;
if(index2 > leftGridChainEndIndex)
break;
}
index2--;
sampleLeftSingleTrimEdgeRegion(uppervert, lowervert, leftGridChain, leftGridChainStartIndex+1, index2, pStream);
/* sampleLeftStripRec(leftChain,
nextTrimVertIndex,
botLeftIndex,
leftGridChain,
index2,
leftGridChainEndIndex
)
*
*/
sampleLeftStripRec(leftChain, index1+1, botLeftIndex, leftGridChain, index2, leftGridChainEndIndex, pStream);
}
/***************begin RecF***********************/
/* the gridlines from leftGridChainStartIndex to
* leftGridChainEndIndex are assumed to form a
* connected component.
* the trim vertex of topLeftIndex is assumed to
* be below the first gridline, and the tim vertex
* of botLeftIndex is assumed to be above the last
* grid line.
* If botLeftIndex < topLeftIndex, then no connected componeent exists, and this funcion returns without
* outputing any triangles.
* Otherwise botLeftIndex >= topLeftIndex, there is at least one triangle to output.
*/
void sampleLeftStripRecF(vertexArray* leftChain,
Int topLeftIndex,
Int botLeftIndex,
gridBoundaryChain* leftGridChain,
Int leftGridChainStartIndex,
Int leftGridChainEndIndex,
primStream* pStream
)
{
/*now top left trim vertex is below the top grid line.
*/
/*stop condition: if topLeftIndex > botLeftIndex, then stop.
*/
if(topLeftIndex > botLeftIndex)
return;
/*if there is only one grid Line, return.*/
if(leftGridChainStartIndex>=leftGridChainEndIndex)
return;
assert(leftChain->getVertex(topLeftIndex)[1] <= leftGridChain->get_v_value(leftGridChainStartIndex) &&
leftChain->getVertex(botLeftIndex)[1] >= leftGridChain->get_v_value(leftGridChainEndIndex));
/*firs find the first trim vertex which is below or equal to the second top grid line:
* index1.
*/
Real secondGridChainV = leftGridChain->get_v_value(leftGridChainStartIndex+1);
Int index1 = topLeftIndex;
while(leftChain->getVertex(index1)[1] > secondGridChainV){
index1++;
if(index1>botLeftIndex)
break;
}
/*now leftChain->getVertex(index-1)[1] > secondGridChainV and
* leftChain->getVertex(index)[1] <= secondGridChainV
*If equality holds, then we should include the vertex index1, otherwise we include only index1-1, to
*perform sampleOneGridStep.
*/
if(index1>botLeftIndex)
index1--;
else if(leftChain->getVertex(index1)[1] < secondGridChainV)
index1--;
/*now we have leftChain->getVertex(index1)[1] >= secondGridChainV, and
* leftChain->getVertex(index1+1)[1] <= secondGridChainV
*/
sampleLeftOneGridStep(leftChain, topLeftIndex, index1, leftGridChain, leftGridChainStartIndex, pStream);
/*if leftChain->getVertex(index1)[1] == secondGridChainV, then we can recursively do the rest.
*/
if(leftChain->getVertex(index1)[1] == secondGridChainV)
{
sampleLeftStripRecF(leftChain, index1, botLeftIndex,leftGridChain, leftGridChainStartIndex+1, leftGridChainEndIndex, pStream);
}
else if(index1 < botLeftIndex)
{
/* Otherwise, we have leftChain->getVertex(index1)[1] > secondGridChainV,
* let the next trim vertex be nextTrimVertIndex (which should be strictly
* below the second grid line).
* Find the last grid line index2 which is above nextTrimVert.
* sampleLeftSingleTrimEdgeRegion(uppervert[2], lowervert[2],
* leftGridChain, leftGridChainStartIndex+1, index2).
*/
Real *uppervert, *lowervert;
uppervert = leftChain->getVertex(index1);
lowervert = leftChain->getVertex(index1+1); //okay since index1<botLeftIndex
Int index2 = leftGridChainStartIndex+1;
while(leftGridChain->get_v_value(index2) >= lowervert[1])
{
index2++;
if(index2 > leftGridChainEndIndex)
break;
}
index2--;
sampleLeftSingleTrimEdgeRegion(uppervert, lowervert, leftGridChain, leftGridChainStartIndex+1, index2, pStream);
/*recursion*/
sampleLeftStripRecF(leftChain, index1+1, botLeftIndex, leftGridChain, index2, leftGridChainEndIndex, pStream);
}
}
/***************End RecF***********************/
/*sample the left area in between one trim edge and multiple grid lines.
* all the grid lines should be in between the two end poins of the
*trim edge.
*/
void sampleLeftSingleTrimEdgeRegion(Real upperVert[2], Real lowerVert[2],
gridBoundaryChain* gridChain,
Int beginIndex,
Int endIndex,
primStream* pStream)
{
Int i,j,k;
vertexArray vArray(endIndex-beginIndex+1);
vArray.appendVertex(gridChain->get_vertex(beginIndex));
for(k=1, i=beginIndex+1; i<=endIndex; i++, k++)
{
vArray.appendVertex(gridChain->get_vertex(i));
/*output the fan of the grid points of the (i)th and (i-1)th grid line.
*/
if(gridChain->getUlineIndex(i) < gridChain->getUlineIndex(i-1))
{
pStream->begin();
pStream->insert(gridChain->get_vertex(i-1));
for(j=gridChain->getUlineIndex(i); j<= gridChain->getUlineIndex(i-1); j++)
pStream->insert(gridChain->getGrid()->get_u_value(j), gridChain->get_v_value(i));
pStream->end(PRIMITIVE_STREAM_FAN);
}
else if(gridChain->getUlineIndex(i) > gridChain->getUlineIndex(i-1))
{
pStream->begin();
pStream->insert(gridChain->get_vertex(i));
for(j=gridChain->getUlineIndex(i); j>= gridChain->getUlineIndex(i-1); j--)
pStream->insert(gridChain->getGrid()->get_u_value(j), gridChain->get_v_value(i-1));
pStream->end(PRIMITIVE_STREAM_FAN);
}
/*otherwisem, the two are equal, so there is no fan to outout*/
}
monoTriangulation2(upperVert, lowerVert, &vArray, 0, endIndex-beginIndex,
0, /*decreasing chain*/
pStream);
}
/*return i, such that from begin to i-1 the chain is strictly u-monotone.
*/
Int findIncreaseChainFromBegin(vertexArray* chain, Int begin ,Int end)
{
Int i=begin;
Real prevU = chain->getVertex(i)[0];
Real thisU;
for(i=begin+1; i<=end; i++){
thisU = chain->getVertex(i)[0];
if(prevU < thisU){
prevU = thisU;
}
else
break;
}
return i;
}
/*check whether there is a vertex whose v value is strictly
*inbetween vup vbelow
*if no middle exists return -1, else return the idnex.
*/
Int checkMiddle(vertexArray* chain, Int begin, Int end,
Real vup, Real vbelow)
{
Int i;
for(i=begin; i<=end; i++)
{
if(chain->getVertex(i)[1] < vup && chain->getVertex(i)[1]>vbelow)
return i;
}
return -1;
}
/*the degenerat case of sampleLeftOneGridStep*/
void sampleLeftOneGridStepNoMiddle(vertexArray* leftChain,
Int beginLeftIndex,
Int endLeftIndex,
gridBoundaryChain* leftGridChain,
Int leftGridChainStartIndex,
primStream* pStream)
{
/*since there is no middle, there is at most one point which is on the
*second grid line, there could be multiple points on the first (top)
*grid line.
*/
leftGridChain->leftEndFan(leftGridChainStartIndex+1, pStream);
monoTriangulation2(leftGridChain->get_vertex(leftGridChainStartIndex),
leftGridChain->get_vertex(leftGridChainStartIndex+1),
leftChain,
beginLeftIndex,
endLeftIndex,
1, //is increase chain.
pStream);
}
/*sampling the left area in between two grid lines.
*/
void sampleLeftOneGridStep(vertexArray* leftChain,
Int beginLeftIndex,
Int endLeftIndex,
gridBoundaryChain* leftGridChain,
Int leftGridChainStartIndex,
primStream* pStream
)
{
if(checkMiddle(leftChain, beginLeftIndex, endLeftIndex,
leftGridChain->get_v_value(leftGridChainStartIndex),
leftGridChain->get_v_value(leftGridChainStartIndex+1))<0)
{
sampleLeftOneGridStepNoMiddle(leftChain, beginLeftIndex, endLeftIndex, leftGridChain, leftGridChainStartIndex, pStream);
return;
}
//copy into a polygon
{
directedLine* poly = NULL;
sampledLine* sline;
directedLine* dline;
gridWrap* grid = leftGridChain->getGrid();
Real vert1[2];
Real vert2[2];
Int i;
Int innerInd = leftGridChain->getInnerIndex(leftGridChainStartIndex+1);
Int upperInd = leftGridChain->getUlineIndex(leftGridChainStartIndex);
Int lowerInd = leftGridChain->getUlineIndex(leftGridChainStartIndex+1);
Real upperV = leftGridChain->get_v_value(leftGridChainStartIndex);
Real lowerV = leftGridChain->get_v_value(leftGridChainStartIndex+1);
//the upper gridline
vert1[1] = vert2[1] = upperV;
for(i=innerInd; i>upperInd; i--)
{
vert1[0]=grid->get_u_value(i);
vert2[0]=grid->get_u_value(i-1);
sline = new sampledLine(vert1, vert2);
dline = new directedLine(INCREASING, sline);
if(poly == NULL)
poly = dline;
else
poly->insert(dline);
}
//the edge connecting upper grid with left chain
vert1[0] = grid->get_u_value(upperInd);
vert1[1] = upperV;
sline = new sampledLine(vert1, leftChain->getVertex(beginLeftIndex));
dline = new directedLine(INCREASING, sline);
if(poly == NULL)
poly = dline;
else
poly->insert(dline);
//the left chain
for(i=beginLeftIndex; i<endLeftIndex; i++)
{
sline = new sampledLine(leftChain->getVertex(i), leftChain->getVertex(i+1));
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
}
//the edge connecting left chain with lower gridline
vert2[0] = grid->get_u_value(lowerInd);
vert2[1] = lowerV;
sline = new sampledLine(leftChain->getVertex(endLeftIndex), vert2);
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
//the lower grid line
vert1[1] = vert2[1] = lowerV;
for(i=lowerInd; i<innerInd; i++)
{
vert1[0] = grid->get_u_value(i);
vert2[0] = grid->get_u_value(i+1);
sline = new sampledLine(vert1, vert2);
dline = new directedLine(INCREASING, sline);
poly->insert(dline);
}
//the vertical grid line segement
vert1[0]=vert2[0] = grid->get_u_value(innerInd);
vert2[1]=upperV;
vert1[1]=lowerV;
sline=new sampledLine(vert1, vert2);
dline=new directedLine(INCREASING, sline);
poly->insert(dline);
monoTriangulationOpt(poly, pStream);
//cleanup
poly->deleteSinglePolygonWithSline();
return;
}
Int i;
if(1/*leftGridChain->getUlineIndex(leftGridChainStartIndex) >=
leftGridChain->getUlineIndex(leftGridChainStartIndex+1)*/
) /*the second grid line is beyond the first one to the left*/
{
/*find the maximal U-monotone chain
* of endLeftIndex, endLeftIndex-1, ...,
*/
i=endLeftIndex;
Real prevU = leftChain->getVertex(i)[0];
for(i=endLeftIndex-1; i>=beginLeftIndex; i--){
Real thisU = leftChain->getVertex(i)[0];
if( prevU < thisU){
prevU = thisU;
}
else
break;
}
/*from endLeftIndex to i+1 is strictly U- monotone */
/*if i+1==endLeftIndex and the vertex and leftchain is on the second gridline, then
*we should use 2 vertices on the leftchain. If we only use one (endLeftIndex), then we
*we would output degenerate triangles
*/
if(i+1 == endLeftIndex && leftChain->getVertex(endLeftIndex)[1] == leftGridChain->get_v_value(1+leftGridChainStartIndex))
i--;
Int j = beginLeftIndex/*endLeftIndex*/+1;
if(leftGridChain->getInnerIndex(leftGridChainStartIndex+1) > leftGridChain->getUlineIndex(leftGridChainStartIndex))
{
j = findIncreaseChainFromBegin(leftChain, beginLeftIndex, i+1/*endLeftIndex*/);
Int temp = beginLeftIndex;
/*now from begin to j-1 is strictly u-monotone*/
/*if j-1 is on the first grid line, then we want to skip to the vertex which is strictly
*below the grid line. This vertexmust exist since there is a 'corner turn' inbetween the two grid lines
*/
if(j-1 == beginLeftIndex)
{
while(leftChain->getVertex(j-1)[1] == leftGridChain->get_v_value(leftGridChainStartIndex))
j++;
Real vert[2];
vert[0] = leftGridChain->get_u_value(leftGridChainStartIndex);
vert[1] = leftGridChain->get_v_value(leftGridChainStartIndex);
monoTriangulation2(
vert/*leftChain->getVertex(beginLeftIndex)*/,
leftChain->getVertex(j-1),
leftChain,
beginLeftIndex,
j-2,
1,
pStream //increase chain
);
temp = j-1;
}
stripOfFanLeft(leftChain, j-1, temp/*beginLeftIndex*/, leftGridChain->getGrid(),
leftGridChain->getVlineIndex(leftGridChainStartIndex),
leftGridChain->getUlineIndex(leftGridChainStartIndex),
leftGridChain->getInnerIndex(leftGridChainStartIndex+1),
pStream,
1 /*the grid line is above the trim line*/
);
}
stripOfFanLeft(leftChain, endLeftIndex, i+1, leftGridChain->getGrid(),
leftGridChain->getVlineIndex(leftGridChainStartIndex+1),
leftGridChain->getUlineIndex(leftGridChainStartIndex+1),
leftGridChain->getInnerIndex(leftGridChainStartIndex+1),
pStream,
0 /*the grid line is below the trim lines*/
);
/*monotone triangulate the remaining left chain togther with the
*two vertices on the two grid v-lines.
*/
Real vert[2][2];
vert[0][0]=vert[1][0] = leftGridChain->getInner_u_value(leftGridChainStartIndex+1);
vert[0][1] = leftGridChain->get_v_value(leftGridChainStartIndex);
vert[1][1] = leftGridChain->get_v_value(leftGridChainStartIndex+1);
// vertexArray right(vert, 2);
monoTriangulation2(
&vert[0][0], /*top vertex */
&vert[1][0], /*bottom vertex*/
leftChain,
/*beginLeftIndex*/j-1,
i+1,
1, /*an increasing chain*/
pStream);
}
else /*the second one is shorter than the first one to the left*/
{
/*find the maximal U-monotone chain of beginLeftIndex, beginLeftIndex+1,...,
*/
i=beginLeftIndex;
Real prevU = leftChain->getVertex(i)[0];
for(i=beginLeftIndex+1; i<=endLeftIndex; i++){
Real thisU = leftChain->getVertex(i)[0];
if(prevU < thisU){
prevU = thisU;
}
else
break;
}
/*from beginLeftIndex to i-1 is strictly U-monotone*/
stripOfFanLeft(leftChain, i-1, beginLeftIndex, leftGridChain->getGrid(),
leftGridChain->getVlineIndex(leftGridChainStartIndex),
leftGridChain->getUlineIndex(leftGridChainStartIndex),
leftGridChain->getUlineIndex(leftGridChainStartIndex+1),
pStream,
1 /*the grid line is above the trim lines*/
);
/*monotone triangulate the remaining left chain together with the
*two vertices on the two grid v-lines.
*/
Real vert[2][2];
vert[0][0]=vert[1][0] = leftGridChain->get_u_value(leftGridChainStartIndex+1);
vert[0][1] = leftGridChain->get_v_value(leftGridChainStartIndex);
vert[1][1] = leftGridChain->get_v_value(leftGridChainStartIndex+1);
vertexArray right(vert, 2);
monoTriangulation2(
&vert[0][0], //top vertex
&vert[1][0], //bottom vertex
leftChain,
i-1,
endLeftIndex,
1, /*an increase chain*/
pStream);
}
}
/*n_upper>=1
*n_lower>=1
*/
void triangulateXYMono(Int n_upper, Real upperVerts[][2],
Int n_lower, Real lowerVerts[][2],
primStream* pStream)
{
Int i,j,k,l;
Real* leftMostV;
assert(n_upper>=1 && n_lower>=1);
if(upperVerts[0][0] <= lowerVerts[0][0])
{
i=1;
j=0;
leftMostV = upperVerts[0];
}
else
{
i=0;
j=1;
leftMostV = lowerVerts[0];
}
while(1)
{
if(i >= n_upper) /*case1: no more in upper*/
{
if(j<n_lower-1) /*at least two vertices in lower*/
{
pStream->begin();
pStream->insert(leftMostV);
while(j<n_lower){
pStream->insert(lowerVerts[j]);
j++;
}
pStream->end(PRIMITIVE_STREAM_FAN);
}
break;
}
else if(j>= n_lower) /*case2: no more in lower*/
{
if(i<n_upper-1) /*at least two vertices in upper*/
{
pStream->begin();
pStream->insert(leftMostV);
for(k=n_upper-1; k>=i; k--)
pStream->insert(upperVerts[k]);
pStream->end(PRIMITIVE_STREAM_FAN);
}
break;
}
else /* case3: neither is empty, plus the leftMostV, there is at least one triangle to output*/
{
if(upperVerts[i][0] <= lowerVerts[j][0])
{
pStream->begin();
pStream->insert(lowerVerts[j]); /*the origin of this fan*/
/*find the last k>=i such that
*upperverts[k][0] <= lowerverts[j][0]
*/
k=i;
while(k<n_upper)
{
if(upperVerts[k][0] > lowerVerts[j][0])
break;
k++;
}
k--;
for(l=k; l>=i; l--)/*the reverse is for two-face lighting*/
{
pStream->insert(upperVerts[l]);
}
pStream->insert(leftMostV);
pStream->end(PRIMITIVE_STREAM_FAN);
//update i for next loop
i = k+1;
leftMostV = upperVerts[k];
}
else /*upperVerts[i][0] > lowerVerts[j][0]*/
{
pStream->begin();
pStream->insert(upperVerts[i]);/*the origion of this fan*/
pStream->insert(leftMostV);
/*find the last k>=j such that
*lowerverts[k][0] < upperverts[i][0]*/
k=j;
while(k< n_lower)
{
if(lowerVerts[k][0] >= upperVerts[i][0])
break;
pStream->insert(lowerVerts[k]);
k++;
}
pStream->end(PRIMITIVE_STREAM_FAN);
j=k;
leftMostV = lowerVerts[j-1];
}
}
}
}
void stripOfFanLeft(vertexArray* leftChain,
Int largeIndex,
Int smallIndex,
gridWrap* grid,
Int vlineIndex,
Int ulineSmallIndex,
Int ulineLargeIndex,
primStream* pStream,
Int gridLineUp /*1 if the grid line is above the trim lines*/
)
{
assert(largeIndex >= smallIndex);
Real grid_v_value;
grid_v_value = grid->get_v_value(vlineIndex);
Real2* trimVerts=(Real2*) malloc(sizeof(Real2)* (largeIndex-smallIndex+1));
assert(trimVerts);
Real2* gridVerts=(Real2*) malloc(sizeof(Real2)* (ulineLargeIndex-ulineSmallIndex+1));
assert(gridVerts);
Int k,i;
if(gridLineUp) /*trim line is below grid line, so trim vertices are going right when index increases*/
for(k=0, i=smallIndex; i<=largeIndex; i++, k++)
{
trimVerts[k][0] = leftChain->getVertex(i)[0];
trimVerts[k][1] = leftChain->getVertex(i)[1];
}
else
for(k=0, i=largeIndex; i>=smallIndex; i--, k++)
{
trimVerts[k][0] = leftChain->getVertex(i)[0];
trimVerts[k][1] = leftChain->getVertex(i)[1];
}
for(k=0, i=ulineSmallIndex; i<= ulineLargeIndex; i++, k++)
{
gridVerts[k][0] = grid->get_u_value(i);
gridVerts[k][1] = grid_v_value;
}
if(gridLineUp)
triangulateXYMono(
ulineLargeIndex-ulineSmallIndex+1, gridVerts,
largeIndex-smallIndex+1, trimVerts,
pStream);
else
triangulateXYMono(largeIndex-smallIndex+1, trimVerts,
ulineLargeIndex-ulineSmallIndex+1, gridVerts,
pStream);
free(trimVerts);
free(gridVerts);
}
|