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
|
/* $Id$ */
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "CCGSubSurf.h"
/***/
typedef unsigned char byte;
/***/
static int kHashSizes[] = {
1, 3, 5, 11, 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209,
16411, 32771, 65537, 131101, 262147, 524309, 1048583, 2097169,
4194319, 8388617, 16777259, 33554467, 67108879, 134217757, 268435459
};
typedef struct _EHEntry EHEntry;
struct _EHEntry {
EHEntry *next;
void *key;
};
typedef struct _EHash {
EHEntry **buckets;
int numEntries, curSize, curSizeIdx;
CCGAllocatorIFC allocatorIFC;
CCGAllocatorHDL allocator;
} EHash;
#define EHASH_alloc(eh, nb) ((eh)->allocatorIFC.alloc((eh)->allocator, nb))
#define EHASH_free(eh, ptr) ((eh)->allocatorIFC.free((eh)->allocator, ptr))
#define EHASH_hash(eh, item) (((unsigned long) (item))%((unsigned int) (eh)->curSize))
static EHash *_ehash_new(int estimatedNumEntries, CCGAllocatorIFC *allocatorIFC, CCGAllocatorHDL allocator) {
EHash *eh = allocatorIFC->alloc(allocator, sizeof(*eh));
eh->allocatorIFC = *allocatorIFC;
eh->allocator = allocator;
eh->numEntries = 0;
eh->curSizeIdx = 0;
while (kHashSizes[eh->curSizeIdx]<estimatedNumEntries)
eh->curSizeIdx++;
eh->curSize = kHashSizes[eh->curSizeIdx];
eh->buckets = EHASH_alloc(eh, eh->curSize*sizeof(*eh->buckets));
memset(eh->buckets, 0, eh->curSize*sizeof(*eh->buckets));
return eh;
}
typedef void (*EHEntryFreeFP)(EHEntry *, void *);
static void _ehash_free(EHash *eh, EHEntryFreeFP freeEntry, void *userData) {
int numBuckets = eh->curSize;
while (numBuckets--) {
EHEntry *entry = eh->buckets[numBuckets];
while (entry) {
EHEntry *next = entry->next;
freeEntry(entry, userData);
entry = next;
}
}
EHASH_free(eh, eh->buckets);
EHASH_free(eh, eh);
}
static void _ehash_insert(EHash *eh, EHEntry *entry) {
int numBuckets = eh->curSize;
int hash = EHASH_hash(eh, entry->key);
entry->next = eh->buckets[hash];
eh->buckets[hash] = entry;
eh->numEntries++;
if (eh->numEntries > (numBuckets*3)) {
EHEntry **oldBuckets = eh->buckets;
eh->curSize = kHashSizes[++eh->curSizeIdx];
eh->buckets = EHASH_alloc(eh, eh->curSize*sizeof(*eh->buckets));
memset(eh->buckets, 0, eh->curSize*sizeof(*eh->buckets));
while (numBuckets--) {
for (entry = oldBuckets[numBuckets]; entry;) {
EHEntry *next = entry->next;
hash = EHASH_hash(eh, entry->key);
entry->next = eh->buckets[hash];
eh->buckets[hash] = entry;
entry = next;
}
}
EHASH_free(eh, oldBuckets);
}
}
static void *_ehash_lookupWithPrev(EHash *eh, void *key, void ***prevp_r) {
int hash = EHASH_hash(eh, key);
void **prevp = (void**) &eh->buckets[hash];
EHEntry *entry;
for (; (entry = *prevp); prevp = (void**) &entry->next) {
if (entry->key==key) {
*prevp_r = (void**) prevp;
return entry;
}
}
return NULL;
}
static void *_ehash_lookup(EHash *eh, void *key) {
int hash = EHASH_hash(eh, key);
EHEntry *entry;
for (entry = eh->buckets[hash]; entry; entry = entry->next)
if (entry->key==key)
break;
return entry;
}
/**/
typedef struct _EHashIterator {
EHash *eh;
int curBucket;
EHEntry *curEntry;
} EHashIterator;
static EHashIterator *_ehashIterator_new(EHash *eh) {
EHashIterator *ehi = EHASH_alloc(eh, sizeof(*ehi));
ehi->eh = eh;
ehi->curEntry = NULL;
ehi->curBucket = -1;
while (!ehi->curEntry) {
ehi->curBucket++;
if (ehi->curBucket==ehi->eh->curSize)
break;
ehi->curEntry = ehi->eh->buckets[ehi->curBucket];
}
return ehi;
}
static void _ehashIterator_free(EHashIterator *ehi) {
EHASH_free(ehi->eh, ehi);
}
static void *_ehashIterator_getCurrent(EHashIterator *ehi) {
return ehi->curEntry;
}
static void _ehashIterator_next(EHashIterator *ehi) {
if (ehi->curEntry) {
ehi->curEntry = ehi->curEntry->next;
while (!ehi->curEntry) {
ehi->curBucket++;
if (ehi->curBucket==ehi->eh->curSize)
break;
ehi->curEntry = ehi->eh->buckets[ehi->curBucket];
}
}
}
static int _ehashIterator_isStopped(EHashIterator *ehi) {
return !ehi->curEntry;
}
/***/
static void *_stdAllocator_alloc(CCGAllocatorHDL a, int numBytes) {
return malloc(numBytes);
}
static void *_stdAllocator_realloc(CCGAllocatorHDL a, void *ptr, int newSize, int oldSize) {
return realloc(ptr, newSize);
}
static void _stdAllocator_free(CCGAllocatorHDL a, void *ptr) {
free(ptr);
}
static CCGAllocatorIFC *_getStandardAllocatorIFC(void) {
static CCGAllocatorIFC ifc;
ifc.alloc = _stdAllocator_alloc;
ifc.realloc = _stdAllocator_realloc;
ifc.free = _stdAllocator_free;
ifc.release = NULL;
return &ifc;
}
/***/
static int _edge_isBoundary(CCGEdge *e);
/***/
enum {
Vert_eEffected= (1<<0),
Vert_eChanged= (1<<1),
} VertFlags;
enum {
Edge_eEffected= (1<<0),
} CCGEdgeFlags;
enum {
Face_eEffected= (1<<0),
} FaceFlags;
struct _CCGVert {
CCGVert *next; /* EHData.next */
CCGVertHDL vHDL; /* EHData.key */
short numEdges, numFaces, flags, pad;
CCGEdge **edges;
CCGFace **faces;
// byte *levelData;
// byte *userData;
};
#define VERT_getLevelData(v) ((byte*) &(v)[1])
struct _CCGEdge {
CCGEdge *next; /* EHData.next */
CCGEdgeHDL eHDL; /* EHData.key */
short numFaces, flags;
float crease;
CCGVert *v0,*v1;
CCGFace **faces;
// byte *levelData;
// byte *userData;
};
#define EDGE_getLevelData(e) ((byte*) &(e)[1])
struct _CCGFace {
CCGFace *next; /* EHData.next */
CCGFaceHDL fHDL; /* EHData.key */
short numVerts, flags, pad1, pad2;
// CCGVert **verts;
// CCGEdge **edges;
// byte *centerData;
// byte **gridData;
// byte *userData;
};
#define FACE_getVerts(f) ((CCGVert**) &(f)[1])
#define FACE_getEdges(f) ((CCGEdge**) &(FACE_getVerts(f)[(f)->numVerts]))
#define FACE_getCenterData(f) ((byte*) &(FACE_getEdges(f)[(f)->numVerts]))
typedef enum {
eSyncState_None = 0,
eSyncState_Vert,
eSyncState_Edge,
eSyncState_Face,
eSyncState_Partial,
} SyncState;
struct _CCGSubSurf {
EHash *vMap; /* map of CCGVertHDL -> Vert */
EHash *eMap; /* map of CCGEdgeHDL -> Edge */
EHash *fMap; /* map of CCGFaceHDL -> Face */
CCGMeshIFC meshIFC;
void *meshData;
CCGAllocatorIFC allocatorIFC;
CCGAllocatorHDL allocator;
int subdivLevels;
int numGrids;
int allowEdgeCreation;
float defaultCreaseValue;
void *q, *r;
// data for calc vert normals
int calcVertNormals;
int normalDataOffset;
// data for age'ing (to debug sync)
int currentAge;
int useAgeCounts;
int vertUserAgeOffset;
int edgeUserAgeOffset;
int faceUserAgeOffset;
// data used during syncing
SyncState syncState;
EHash *oldVMap, *oldEMap, *oldFMap;
int lenTempArrays;
CCGVert **tempVerts;
CCGEdge **tempEdges;
};
#define CCGSUBSURF_alloc(ss, nb) ((ss)->allocatorIFC.alloc((ss)->allocator, nb))
#define CCGSUBSURF_realloc(ss, ptr, nb, ob) ((ss)->allocatorIFC.realloc((ss)->allocator, ptr, nb, ob))
#define CCGSUBSURF_free(ss, ptr) ((ss)->allocatorIFC.free((ss)->allocator, ptr))
/***/
static CCGVert *_vert_new(CCGVertHDL vHDL, int levels, int dataSize, CCGSubSurf *ss) {
CCGVert *v = CCGSUBSURF_alloc(ss, sizeof(CCGVert) + ss->meshIFC.vertDataSize * (ss->subdivLevels+1) + ss->meshIFC.vertUserSize);
byte *userData;
v->vHDL = vHDL;
v->edges = NULL;
v->faces = NULL;
v->numEdges = v->numFaces = 0;
v->flags = 0;
userData = ccgSubSurf_getVertUserData(ss, v);
memset(userData, 0, ss->meshIFC.vertUserSize);
if (ss->useAgeCounts) *((int*) &userData[ss->vertUserAgeOffset]) = ss->currentAge;
return v;
}
static void _vert_remEdge(CCGVert *v, CCGEdge *e, CCGSubSurf *ss) {
int i;
for (i=0; i<v->numEdges; i++) {
if (v->edges[i]==e) {
v->edges[i] = v->edges[--v->numEdges];
break;
}
}
}
static void _vert_remFace(CCGVert *v, CCGFace *f, CCGSubSurf *ss) {
int i;
for (i=0; i<v->numFaces; i++) {
if (v->faces[i]==f) {
v->faces[i] = v->faces[--v->numFaces];
break;
}
}
}
static void _vert_addEdge(CCGVert *v, CCGEdge *e, CCGSubSurf *ss) {
v->edges = CCGSUBSURF_realloc(ss, v->edges, (v->numEdges+1)*sizeof(*v->edges), v->numEdges*sizeof(*v->edges));
v->edges[v->numEdges++] = e;
}
static void _vert_addFace(CCGVert *v, CCGFace *f, CCGSubSurf *ss) {
v->faces = CCGSUBSURF_realloc(ss, v->faces, (v->numFaces+1)*sizeof(*v->faces), v->numFaces*sizeof(*v->faces));
v->faces[v->numFaces++] = f;
}
static CCGEdge *_vert_findEdgeTo(CCGVert *v, CCGVert *vQ) {
int i;
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[v->numEdges-1-i]; // XXX, note reverse
if ( (e->v0==v && e->v1==vQ) ||
(e->v1==v && e->v0==vQ))
return e;
}
return 0;
}
static int _vert_isBoundary(CCGVert *v) {
int i;
for (i=0; i<v->numEdges; i++)
if (_edge_isBoundary(v->edges[i]))
return 1;
return 0;
}
static void *_vert_getCo(CCGVert *v, int lvl, int dataSize) {
return &VERT_getLevelData(v)[lvl*dataSize];
}
static void _vert_free(CCGVert *v, CCGSubSurf *ss) {
CCGSUBSURF_free(ss, v->edges);
CCGSUBSURF_free(ss, v->faces);
CCGSUBSURF_free(ss, v);
}
/***/
static CCGEdge *_edge_new(CCGEdgeHDL eHDL, CCGVert *v0, CCGVert *v1, float crease, int levels, int dataSize, CCGSubSurf *ss) {
CCGEdge *e = CCGSUBSURF_alloc(ss, sizeof(CCGEdge) + ss->meshIFC.vertDataSize *((ss->subdivLevels+1) + (1<<(ss->subdivLevels+1))-1) + ss->meshIFC.edgeUserSize);
byte *userData;
e->eHDL = eHDL;
e->v0 = v0;
e->v1 = v1;
e->crease = crease;
e->faces = NULL;
e->numFaces = 0;
e->flags = 0;
_vert_addEdge(v0, e, ss);
_vert_addEdge(v1, e, ss);
userData = ccgSubSurf_getEdgeUserData(ss, e);
memset(userData, 0, ss->meshIFC.edgeUserSize);
if (ss->useAgeCounts) *((int*) &userData[ss->edgeUserAgeOffset]) = ss->currentAge;
return e;
}
static void _edge_remFace(CCGEdge *e, CCGFace *f, CCGSubSurf *ss) {
int i;
for (i=0; i<e->numFaces; i++) {
if (e->faces[i]==f) {
e->faces[i] = e->faces[--e->numFaces];
break;
}
}
}
static void _edge_addFace(CCGEdge *e, CCGFace *f, CCGSubSurf *ss) {
e->faces = CCGSUBSURF_realloc(ss, e->faces, (e->numFaces+1)*sizeof(*e->faces), e->numFaces*sizeof(*e->faces));
e->faces[e->numFaces++] = f;
}
static int _edge_isBoundary(CCGEdge *e) {
return e->numFaces<2;
}
static CCGVert *_edge_getOtherVert(CCGEdge *e, CCGVert *vQ) {
if (vQ==e->v0) {
return e->v1;
} else {
return e->v0;
}
}
static void *_edge_getCo(CCGEdge *e, int lvl, int x, int dataSize) {
int levelBase = lvl + (1<<lvl) - 1;
return &EDGE_getLevelData(e)[dataSize*(levelBase + x)];
}
static float *_edge_getNo(CCGEdge *e, int lvl, int x, int dataSize, int normalDataOffset) {
int levelBase = lvl + (1<<lvl) - 1;
return (float*) &EDGE_getLevelData(e)[dataSize*(levelBase + x) + normalDataOffset];
}
static void *_edge_getCoVert(CCGEdge *e, CCGVert *v, int lvl, int x, int dataSize) {
int levelBase = lvl + (1<<lvl) - 1;
if (v==e->v0) {
return &EDGE_getLevelData(e)[dataSize*(levelBase + x)];
} else {
return &EDGE_getLevelData(e)[dataSize*(levelBase + (1<<lvl) - x)];
}
}
static void _edge_free(CCGEdge *e, CCGSubSurf *ss) {
CCGSUBSURF_free(ss, e->faces);
CCGSUBSURF_free(ss, e);
}
static void _edge_unlinkMarkAndFree(CCGEdge *e, CCGSubSurf *ss) {
_vert_remEdge(e->v0, e, ss);
_vert_remEdge(e->v1, e, ss);
e->v0->flags |= Vert_eEffected;
e->v1->flags |= Vert_eEffected;
_edge_free(e, ss);
}
static float EDGE_getSharpness(CCGEdge *e, int lvl, CCGSubSurf *ss) {
float sharpness = e->crease;
while (lvl--) {
if (sharpness>1.0) {
sharpness -= 1.0;
} else {
sharpness = 0.0;
}
}
return sharpness;
}
/***/
static CCGFace *_face_new(CCGFaceHDL fHDL, CCGVert **verts, CCGEdge **edges, int numVerts, int levels, int dataSize, CCGSubSurf *ss) {
int maxGridSize = 1 + (1<<(ss->subdivLevels-1));
CCGFace *f = CCGSUBSURF_alloc(ss, sizeof(CCGFace) + sizeof(CCGVert*)*numVerts + sizeof(CCGEdge*)*numVerts + ss->meshIFC.vertDataSize *(1 + numVerts*maxGridSize + numVerts*maxGridSize*maxGridSize) + ss->meshIFC.faceUserSize);
byte *userData;
int i;
f->numVerts = numVerts;
f->fHDL = fHDL;
f->flags = 0;
for (i=0; i<numVerts; i++) {
FACE_getVerts(f)[i] = verts[i];
FACE_getEdges(f)[i] = edges[i];
_vert_addFace(verts[i], f, ss);
_edge_addFace(edges[i], f, ss);
}
userData = ccgSubSurf_getFaceUserData(ss, f);
memset(userData, 0, ss->meshIFC.faceUserSize);
if (ss->useAgeCounts) *((int*) &userData[ss->faceUserAgeOffset]) = ss->currentAge;
return f;
}
static void *_face_getIECo(CCGFace *f, int lvl, int S, int x, int levels, int dataSize) {
int maxGridSize = 1 + (1<<(levels-1));
int spacing = 1<<(levels-lvl);
byte *gridBase = FACE_getCenterData(f) + dataSize*(1 + S*(maxGridSize + maxGridSize*maxGridSize));
return &gridBase[dataSize*x*spacing];
}
static void *_face_getIFCo(CCGFace *f, int lvl, int S, int x, int y, int levels, int dataSize) {
int maxGridSize = 1 + (1<<(levels-1));
int spacing = 1<<(levels-lvl);
byte *gridBase = FACE_getCenterData(f) + dataSize*(1 + S*(maxGridSize + maxGridSize*maxGridSize));
return &gridBase[dataSize*(maxGridSize + (y*maxGridSize + x)*spacing)];
}
static float *_face_getIFNo(CCGFace *f, int lvl, int S, int x, int y, int levels, int dataSize, int normalDataOffset) {
int maxGridSize = 1 + (1<<(levels-1));
int spacing = 1<<(levels-lvl);
byte *gridBase = FACE_getCenterData(f) + dataSize*(1 + S*(maxGridSize + maxGridSize*maxGridSize));
return (float*) &gridBase[dataSize*(maxGridSize + (y*maxGridSize + x)*spacing) + normalDataOffset];
}
static int _face_getVertIndex(CCGFace *f, CCGVert *v) {
int i;
for (i=0; i<f->numVerts; i++)
if (FACE_getVerts(f)[i]==v)
return i;
return -1;
}
static void *_face_getIFCoEdge(CCGFace *f, CCGEdge *e, int lvl, int eX, int eY, int levels, int dataSize) {
int maxGridSize = 1 + (1<<(levels-1));
int spacing = 1<<(levels-lvl);
int S, x, y, cx, cy;
for (S=0; S<f->numVerts; S++)
if (FACE_getEdges(f)[S]==e)
break;
eX = eX*spacing;
eY = eY*spacing;
if (e->v0!=FACE_getVerts(f)[S]) {
eX = (maxGridSize*2 - 1)-1 - eX;
}
y = maxGridSize - 1 - eX;
x = maxGridSize - 1 - eY;
if (x<0) {
S = (S+f->numVerts-1)%f->numVerts;
cx = y;
cy = -x;
} else if (y<0) {
S = (S+1)%f->numVerts;
cx = -y;
cy = x;
} else {
cx = x;
cy = y;
}
return _face_getIFCo(f, levels, S, cx, cy, levels, dataSize);
}
static float *_face_getIFNoEdge(CCGFace *f, CCGEdge *e, int lvl, int eX, int eY, int levels, int dataSize, int normalDataOffset) {
return (float*) ((byte*) _face_getIFCoEdge(f, e, lvl, eX, eY, levels, dataSize) + normalDataOffset);
}
static void _face_free(CCGFace *f, CCGSubSurf *ss) {
CCGSUBSURF_free(ss, f);
}
static void _face_unlinkMarkAndFree(CCGFace *f, CCGSubSurf *ss) {
int j;
for (j=0; j<f->numVerts; j++) {
_vert_remFace(FACE_getVerts(f)[j], f, ss);
_edge_remFace(FACE_getEdges(f)[j], f, ss);
FACE_getVerts(f)[j]->flags |= Vert_eEffected;
}
_face_free(f, ss);
}
/***/
CCGSubSurf *ccgSubSurf_new(CCGMeshIFC *ifc, CCGMeshHDL meshData, int subdivLevels, CCGAllocatorIFC *allocatorIFC, CCGAllocatorHDL allocator) {
if (!allocatorIFC) {
allocatorIFC = _getStandardAllocatorIFC();
allocator = NULL;
}
if (subdivLevels<1) {
return NULL;
} else {
CCGSubSurf *ss = allocatorIFC->alloc(allocator, sizeof(*ss));
ss->allocatorIFC = *allocatorIFC;
ss->allocator = allocator;
ss->vMap = _ehash_new(0, &ss->allocatorIFC, ss->allocator);
ss->eMap = _ehash_new(0, &ss->allocatorIFC, ss->allocator);
ss->fMap = _ehash_new(0, &ss->allocatorIFC, ss->allocator);
ss->meshIFC = *ifc;
ss->meshData = meshData;
ss->subdivLevels = subdivLevels;
ss->numGrids = 0;
ss->allowEdgeCreation = 0;
ss->defaultCreaseValue = 0;
ss->useAgeCounts = 0;
ss->vertUserAgeOffset = ss->edgeUserAgeOffset = ss->faceUserAgeOffset = 0;
ss->calcVertNormals = 0;
ss->normalDataOffset = 0;
ss->q = CCGSUBSURF_alloc(ss, ss->meshIFC.vertDataSize);
ss->r = CCGSUBSURF_alloc(ss, ss->meshIFC.vertDataSize);
ss->currentAge = 0;
ss->syncState = eSyncState_None;
ss->oldVMap = ss->oldEMap = ss->oldFMap = NULL;
ss->lenTempArrays = 0;
ss->tempVerts = NULL;
ss->tempEdges = NULL;
return ss;
}
}
void ccgSubSurf_free(CCGSubSurf *ss) {
CCGAllocatorIFC allocatorIFC = ss->allocatorIFC;
CCGAllocatorHDL allocator = ss->allocator;
if (ss->syncState) {
_ehash_free(ss->oldFMap, (EHEntryFreeFP) _face_free, ss);
_ehash_free(ss->oldEMap, (EHEntryFreeFP) _edge_free, ss);
_ehash_free(ss->oldVMap, (EHEntryFreeFP) _vert_free, ss);
CCGSUBSURF_free(ss, ss->tempVerts);
CCGSUBSURF_free(ss, ss->tempEdges);
}
CCGSUBSURF_free(ss, ss->r);
CCGSUBSURF_free(ss, ss->q);
_ehash_free(ss->fMap, (EHEntryFreeFP) _face_free, ss);
_ehash_free(ss->eMap, (EHEntryFreeFP) _edge_free, ss);
_ehash_free(ss->vMap, (EHEntryFreeFP) _vert_free, ss);
CCGSUBSURF_free(ss, ss);
if (allocatorIFC.release) {
allocatorIFC.release(allocator);
}
}
CCGError ccgSubSurf_setAllowEdgeCreation(CCGSubSurf *ss, int allowEdgeCreation, float defaultCreaseValue) {
ss->allowEdgeCreation = !!allowEdgeCreation;
ss->defaultCreaseValue = defaultCreaseValue;
return eCCGError_None;
}
CCGError ccgSubSurf_setSubdivisionLevels(CCGSubSurf *ss, int subdivisionLevels) {
if (subdivisionLevels<=0) {
return eCCGError_InvalidValue;
} else if (subdivisionLevels!=ss->subdivLevels) {
ss->numGrids = 0;
ss->subdivLevels = subdivisionLevels;
_ehash_free(ss->vMap, (EHEntryFreeFP) _vert_free, ss);
_ehash_free(ss->eMap, (EHEntryFreeFP) _edge_free, ss);
_ehash_free(ss->fMap, (EHEntryFreeFP) _face_free, ss);
ss->vMap = _ehash_new(0, &ss->allocatorIFC, ss->allocator);
ss->eMap = _ehash_new(0, &ss->allocatorIFC, ss->allocator);
ss->fMap = _ehash_new(0, &ss->allocatorIFC, ss->allocator);
}
return eCCGError_None;
}
CCGError ccgSubSurf_setUseAgeCounts(CCGSubSurf *ss, int useAgeCounts, int vertUserOffset, int edgeUserOffset, int faceUserOffset) {
if (useAgeCounts) {
if ( (vertUserOffset+4>ss->meshIFC.vertUserSize) ||
(edgeUserOffset+4>ss->meshIFC.edgeUserSize) ||
(faceUserOffset+4>ss->meshIFC.faceUserSize)) {
return eCCGError_InvalidValue;
} else {
ss->useAgeCounts = 1;
ss->vertUserAgeOffset = vertUserOffset;
ss->edgeUserAgeOffset = edgeUserOffset;
ss->faceUserAgeOffset = faceUserOffset;
}
} else {
ss->useAgeCounts = 0;
ss->vertUserAgeOffset = ss->edgeUserAgeOffset = ss->faceUserAgeOffset = 0;
}
return eCCGError_None;
}
CCGError ccgSubSurf_setCalcVertexNormals(CCGSubSurf *ss, int useVertNormals, int normalDataOffset) {
if (useVertNormals) {
if (normalDataOffset<0 || normalDataOffset+12>ss->meshIFC.vertDataSize) {
return eCCGError_InvalidValue;
} else {
ss->calcVertNormals = 1;
ss->normalDataOffset = normalDataOffset;
}
} else {
ss->calcVertNormals = 0;
ss->normalDataOffset = 0;
}
return eCCGError_None;
}
/***/
CCGError ccgSubSurf_initFullSync(CCGSubSurf *ss) {
if (ss->syncState!=eSyncState_None) {
return eCCGError_InvalidSyncState;
}
ss->currentAge++;
ss->oldVMap = ss->vMap;
ss->oldEMap = ss->eMap;
ss->oldFMap = ss->fMap;
ss->vMap = _ehash_new(ss->oldVMap->numEntries, &ss->allocatorIFC, ss->allocator);
ss->eMap = _ehash_new(ss->oldFMap->numEntries, &ss->allocatorIFC, ss->allocator);
ss->fMap = _ehash_new(ss->oldEMap->numEntries, &ss->allocatorIFC, ss->allocator);
ss->numGrids = 0;
ss->lenTempArrays = 12;
ss->tempVerts = CCGSUBSURF_alloc(ss, sizeof(*ss->tempVerts)*ss->lenTempArrays);
ss->tempEdges = CCGSUBSURF_alloc(ss, sizeof(*ss->tempEdges)*ss->lenTempArrays);
ss->syncState = eSyncState_Vert;
return eCCGError_None;
}
CCGError ccgSubSurf_initPartialSync(CCGSubSurf *ss) {
if (ss->syncState!=eSyncState_None) {
return eCCGError_InvalidSyncState;
}
ss->currentAge++;
ss->syncState = eSyncState_Partial;
return eCCGError_None;
}
CCGError ccgSubSurf_syncVertDel(CCGSubSurf *ss, CCGVertHDL vHDL) {
if (ss->syncState!=eSyncState_Partial) {
return eCCGError_InvalidSyncState;
} else {
void **prevp;
CCGVert *v = _ehash_lookupWithPrev(ss->vMap, vHDL, &prevp);
if (!v || v->numFaces || v->numEdges) {
return eCCGError_InvalidValue;
} else {
*prevp = v->next;
_vert_free(v, ss);
}
}
return eCCGError_None;
}
CCGError ccgSubSurf_syncEdgeDel(CCGSubSurf *ss, CCGEdgeHDL eHDL) {
if (ss->syncState!=eSyncState_Partial) {
return eCCGError_InvalidSyncState;
} else {
void **prevp;
CCGEdge *e = _ehash_lookupWithPrev(ss->eMap, eHDL, &prevp);
if (!e || e->numFaces) {
return eCCGError_InvalidValue;
} else {
*prevp = e->next;
_edge_unlinkMarkAndFree(e, ss);
}
}
return eCCGError_None;
}
CCGError ccgSubSurf_syncFaceDel(CCGSubSurf *ss, CCGFaceHDL fHDL) {
if (ss->syncState!=eSyncState_Partial) {
return eCCGError_InvalidSyncState;
} else {
void **prevp;
CCGFace *f = _ehash_lookupWithPrev(ss->fMap, fHDL, &prevp);
if (!f) {
return eCCGError_InvalidValue;
} else {
*prevp = f->next;
_face_unlinkMarkAndFree(f, ss);
}
}
return eCCGError_None;
}
CCGError ccgSubSurf_syncVert(CCGSubSurf *ss, CCGVertHDL vHDL, void *vertData) {
void **prevp;
CCGVert *v;
if (ss->syncState==eSyncState_Partial) {
v = _ehash_lookupWithPrev(ss->vMap, vHDL, &prevp);
if (!v) {
v = _vert_new(vHDL, ss->subdivLevels, ss->meshIFC.vertDataSize, ss);
ss->meshIFC.vertDataCopy(ss->meshData, _vert_getCo(v,0,ss->meshIFC.vertDataSize), vertData);
_ehash_insert(ss->vMap, (EHEntry*) v);
v->flags = Vert_eEffected;
} else if (!ss->meshIFC.vertDataEqual(ss->meshData, vertData, _vert_getCo(v, 0, ss->meshIFC.vertDataSize))) {
int i, j;
ss->meshIFC.vertDataCopy(ss->meshData, _vert_getCo(v,0,ss->meshIFC.vertDataSize), vertData);
v->flags = Vert_eEffected;
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
e->v0->flags |= Vert_eEffected;
e->v1->flags |= Vert_eEffected;
}
for (i=0; i<v->numFaces; i++) {
CCGFace *f = v->faces[i];
for (j=0; j<f->numVerts; j++) {
FACE_getVerts(f)[j]->flags |= Vert_eEffected;
}
}
}
} else {
if (ss->syncState!=eSyncState_Vert) {
return eCCGError_InvalidSyncState;
}
v = _ehash_lookupWithPrev(ss->oldVMap, vHDL, &prevp);
if (!v) {
v = _vert_new(vHDL, ss->subdivLevels, ss->meshIFC.vertDataSize, ss);
ss->meshIFC.vertDataCopy(ss->meshData, _vert_getCo(v,0,ss->meshIFC.vertDataSize), vertData);
_ehash_insert(ss->vMap, (EHEntry*) v);
v->flags = Vert_eEffected;
} else if (!ss->meshIFC.vertDataEqual(ss->meshData, vertData, _vert_getCo(v, 0, ss->meshIFC.vertDataSize))) {
*prevp = v->next;
_ehash_insert(ss->vMap, (EHEntry*) v);
ss->meshIFC.vertDataCopy(ss->meshData, _vert_getCo(v,0,ss->meshIFC.vertDataSize), vertData);
v->flags = Vert_eEffected|Vert_eChanged;
} else {
*prevp = v->next;
_ehash_insert(ss->vMap, (EHEntry*) v);
v->flags = 0;
}
}
return eCCGError_None;
}
CCGError ccgSubSurf_syncEdge(CCGSubSurf *ss, CCGEdgeHDL eHDL, CCGVertHDL e_vHDL0, CCGVertHDL e_vHDL1, float crease) {
void **prevp;
CCGEdge *e, *eNew;
if (ss->syncState==eSyncState_Partial) {
e = _ehash_lookupWithPrev(ss->eMap, eHDL, &prevp);
if (!e || e->v0->vHDL!=e_vHDL0 || e->v1->vHDL!=e_vHDL1 || crease!=e->crease) {
CCGVert *v0 = _ehash_lookup(ss->vMap, e_vHDL0);
CCGVert *v1 = _ehash_lookup(ss->vMap, e_vHDL1);
eNew = _edge_new(eHDL, v0, v1, crease, ss->subdivLevels, ss->meshIFC.vertDataSize, ss);
if (e) {
*prevp = eNew;
eNew->next = e->next;
_edge_unlinkMarkAndFree(e, ss);
} else {
_ehash_insert(ss->eMap, (EHEntry*) eNew);
}
eNew->v0->flags |= Vert_eEffected;
eNew->v1->flags |= Vert_eEffected;
}
} else {
if (ss->syncState==eSyncState_Vert) {
ss->syncState = eSyncState_Edge;
} else if (ss->syncState!=eSyncState_Edge) {
return eCCGError_InvalidSyncState;
}
e = _ehash_lookupWithPrev(ss->oldEMap, eHDL, &prevp);
if (!e || e->v0->vHDL!=e_vHDL0 || e->v1->vHDL!=e_vHDL1|| e->crease!=crease) {
CCGVert *v0 = _ehash_lookup(ss->vMap, e_vHDL0);
CCGVert *v1 = _ehash_lookup(ss->vMap, e_vHDL1);
e = _edge_new(eHDL, v0, v1, crease, ss->subdivLevels, ss->meshIFC.vertDataSize, ss);
_ehash_insert(ss->eMap, (EHEntry*) e);
e->v0->flags |= Vert_eEffected;
e->v1->flags |= Vert_eEffected;
} else {
*prevp = e->next;
_ehash_insert(ss->eMap, (EHEntry*) e);
e->flags = 0;
if ((e->v0->flags|e->v1->flags)&Vert_eChanged) {
e->v0->flags |= Vert_eEffected;
e->v1->flags |= Vert_eEffected;
}
}
}
return eCCGError_None;
}
CCGError ccgSubSurf_syncFace(CCGSubSurf *ss, CCGFaceHDL fHDL, int numVerts, CCGVertHDL *vHDLs) {
void **prevp;
CCGFace *f, *fNew;
int j, k, topologyChanged = 0;
if (numVerts>ss->lenTempArrays) {
int oldLen = ss->lenTempArrays;
ss->lenTempArrays = (numVerts<ss->lenTempArrays*2)?ss->lenTempArrays*2:numVerts;
ss->tempVerts = CCGSUBSURF_realloc(ss, ss->tempVerts, sizeof(*ss->tempVerts)*ss->lenTempArrays, sizeof(*ss->tempVerts)*oldLen);
ss->tempEdges = CCGSUBSURF_realloc(ss, ss->tempEdges, sizeof(*ss->tempEdges)*ss->lenTempArrays, sizeof(*ss->tempEdges)*oldLen);
}
if (ss->syncState==eSyncState_Partial) {
f = _ehash_lookupWithPrev(ss->fMap, fHDL, &prevp);
for (k=0; k<numVerts; k++) {
ss->tempVerts[k] = _ehash_lookup(ss->vMap, vHDLs[k]);
}
for (k=0; k<numVerts; k++) {
ss->tempEdges[k] = _vert_findEdgeTo(ss->tempVerts[k], ss->tempVerts[(k+1)%numVerts]);
}
if (f) {
if ( f->numVerts!=numVerts ||
memcmp(FACE_getVerts(f), ss->tempVerts, sizeof(*ss->tempVerts)*numVerts) ||
memcmp(FACE_getEdges(f), ss->tempEdges, sizeof(*ss->tempEdges)*numVerts))
topologyChanged = 1;
}
if (!f || topologyChanged) {
fNew = _face_new(fHDL, ss->tempVerts, ss->tempEdges, numVerts, ss->subdivLevels, ss->meshIFC.vertDataSize, ss);
if (f) {
ss->numGrids += numVerts - f->numVerts;
*prevp = fNew;
fNew->next = f->next;
_face_unlinkMarkAndFree(f, ss);
} else {
ss->numGrids += numVerts;
_ehash_insert(ss->fMap, (EHEntry*) fNew);
}
for (k=0; k<numVerts; k++)
FACE_getVerts(fNew)[k]->flags |= Vert_eEffected;
}
} else {
if (ss->syncState==eSyncState_Vert || ss->syncState==eSyncState_Edge) {
ss->syncState = eSyncState_Face;
} else if (ss->syncState!=eSyncState_Face) {
return eCCGError_InvalidSyncState;
}
f = _ehash_lookupWithPrev(ss->oldFMap, fHDL, &prevp);
for (k=0; k<numVerts; k++) {
ss->tempVerts[k] = _ehash_lookup(ss->vMap, vHDLs[k]);
}
for (k=0; k<numVerts; k++) {
ss->tempEdges[k] = _vert_findEdgeTo(ss->tempVerts[k], ss->tempVerts[(k+1)%numVerts]);
if (ss->allowEdgeCreation && !ss->tempEdges[k]) {
CCGEdge *e = ss->tempEdges[k] = _edge_new((CCGEdgeHDL) -1, ss->tempVerts[k], ss->tempVerts[(k+1)%numVerts], ss->defaultCreaseValue, ss->subdivLevels, ss->meshIFC.vertDataSize, ss);
_ehash_insert(ss->eMap, (EHEntry*) e);
e->v0->flags |= Vert_eEffected;
e->v1->flags |= Vert_eEffected;
}
}
if (f) {
if ( f->numVerts!=numVerts ||
memcmp(FACE_getVerts(f), ss->tempVerts, sizeof(*ss->tempVerts)*numVerts) ||
memcmp(FACE_getEdges(f), ss->tempEdges, sizeof(*ss->tempEdges)*numVerts))
topologyChanged = 1;
}
if (!f || topologyChanged) {
f = _face_new(fHDL, ss->tempVerts, ss->tempEdges, numVerts, ss->subdivLevels, ss->meshIFC.vertDataSize, ss);
_ehash_insert(ss->fMap, (EHEntry*) f);
ss->numGrids += numVerts;
for (k=0; k<numVerts; k++)
FACE_getVerts(f)[k]->flags |= Vert_eEffected;
} else {
*prevp = f->next;
_ehash_insert(ss->fMap, (EHEntry*) f);
f->flags = 0;
ss->numGrids += f->numVerts;
for (j=0; j<f->numVerts; j++) {
if (FACE_getVerts(f)[j]->flags&Vert_eChanged) {
for (k=0; k<f->numVerts; k++)
FACE_getVerts(f)[k]->flags |= Vert_eEffected;
break;
}
}
}
}
return eCCGError_None;
}
static void ccgSubSurf__sync(CCGSubSurf *ss);
CCGError ccgSubSurf_processSync(CCGSubSurf *ss) {
if (ss->syncState==eSyncState_Partial) {
ss->syncState = eSyncState_None;
ccgSubSurf__sync(ss);
} else if (ss->syncState) {
_ehash_free(ss->oldFMap, (EHEntryFreeFP) _face_unlinkMarkAndFree, ss);
_ehash_free(ss->oldEMap, (EHEntryFreeFP) _edge_unlinkMarkAndFree, ss);
_ehash_free(ss->oldVMap, (EHEntryFreeFP) _vert_free, ss);
CCGSUBSURF_free(ss, ss->tempEdges);
CCGSUBSURF_free(ss, ss->tempVerts);
ss->lenTempArrays = 0;
ss->oldFMap = ss->oldEMap = ss->oldVMap = NULL;
ss->tempVerts = NULL;
ss->tempEdges = NULL;
ss->syncState = eSyncState_None;
ccgSubSurf__sync(ss);
} else {
return eCCGError_InvalidSyncState;
}
return eCCGError_None;
}
static void ccgSubSurf__sync(CCGSubSurf *ss) {
CCGVert **effectedV;
CCGEdge **effectedE;
CCGFace **effectedF;
int numEffectedV, numEffectedE, numEffectedF;
int subdivLevels = ss->subdivLevels;
int vertDataSize = ss->meshIFC.vertDataSize;
int i,ptrIdx,cornerIdx;
int S,x,y;
void *q = ss->q, *r = ss->r;
int curLvl, nextLvl;
int j;
effectedV = CCGSUBSURF_alloc(ss, sizeof(*effectedV)*ss->vMap->numEntries);
effectedE = CCGSUBSURF_alloc(ss, sizeof(*effectedE)*ss->eMap->numEntries);
effectedF = CCGSUBSURF_alloc(ss, sizeof(*effectedF)*ss->fMap->numEntries);
numEffectedV = numEffectedE = numEffectedF = 0;
for (i=0; i<ss->vMap->curSize; i++) {
CCGVert *v = (CCGVert*) ss->vMap->buckets[i];
for (; v; v = v->next) {
if (v->flags&Vert_eEffected) {
effectedV[numEffectedV++] = v;
for (j=0; j<v->numEdges; j++) {
CCGEdge *e = v->edges[j];
if (!(e->flags&Edge_eEffected)) {
effectedE[numEffectedE++] = e;
e->flags |= Edge_eEffected;
}
}
for (j=0; j<v->numFaces; j++) {
CCGFace *f = v->faces[j];
if (!(f->flags&Face_eEffected)) {
effectedF[numEffectedF++] = f;
f->flags |= Face_eEffected;
}
}
}
}
}
#define VERT_getCo(v, lvl) _vert_getCo(v, lvl, vertDataSize)
#define EDGE_getCo(e, lvl, x) _edge_getCo(e, lvl, x, vertDataSize)
#define FACE_getIECo(f, lvl, S, x) _face_getIECo(f, lvl, S, x, subdivLevels, vertDataSize)
#define FACE_getIFCo(f, lvl, S, x, y) _face_getIFCo(f, lvl, S, x, y, subdivLevels, vertDataSize)
curLvl = 0;
nextLvl = curLvl+1;
for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) {
CCGFace *f = effectedF[ptrIdx];
void *co = FACE_getCenterData(f);
ss->meshIFC.vertDataZero(ss->meshData, co);
for (i=0; i<f->numVerts; i++) {
ss->meshIFC.vertDataAdd(ss->meshData, co, VERT_getCo(FACE_getVerts(f)[i], curLvl));
}
ss->meshIFC.vertDataMulN(ss->meshData, co, 1.0f/f->numVerts);
f->flags = 0;
}
for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) {
CCGEdge *e = effectedE[ptrIdx];
void *co = EDGE_getCo(e, nextLvl, 1);
float sharpness = EDGE_getSharpness(e, curLvl, ss);
if (_edge_isBoundary(e) || sharpness>=1.0) {
ss->meshIFC.vertDataCopy(ss->meshData, co, VERT_getCo(e->v0, curLvl));
ss->meshIFC.vertDataAdd(ss->meshData, co, VERT_getCo(e->v1, curLvl));
ss->meshIFC.vertDataMulN(ss->meshData, co, 0.5f);
} else {
int numFaces = 0;
ss->meshIFC.vertDataCopy(ss->meshData, q, VERT_getCo(e->v0, curLvl));
ss->meshIFC.vertDataAdd(ss->meshData, q, VERT_getCo(e->v1, curLvl));
for (i=0; i<e->numFaces; i++) {
CCGFace *f = e->faces[i];
ss->meshIFC.vertDataAdd(ss->meshData, q, FACE_getCenterData(f));
numFaces++;
}
ss->meshIFC.vertDataMulN(ss->meshData, q, 1.0f/(2.0f+numFaces));
ss->meshIFC.vertDataCopy(ss->meshData, r, VERT_getCo(e->v0, curLvl));
ss->meshIFC.vertDataAdd(ss->meshData, r, VERT_getCo(e->v1, curLvl));
ss->meshIFC.vertDataMulN(ss->meshData, r, 0.5f);
ss->meshIFC.vertDataCopy(ss->meshData, co, q);
ss->meshIFC.vertDataSub(ss->meshData, r, q);
ss->meshIFC.vertDataMulN(ss->meshData, r, sharpness);
ss->meshIFC.vertDataAdd(ss->meshData, co, r);
}
e->flags = 0;
}
for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) {
CCGVert *v = effectedV[ptrIdx];
void *co = VERT_getCo(v, curLvl);
void *nCo = VERT_getCo(v, nextLvl);
int sharpCount = 0, allSharp = 1;
float avgSharpness = 0.0;
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
float sharpness = EDGE_getSharpness(e, curLvl, ss);
if (sharpness!=0.0f) {
sharpCount++;
avgSharpness += sharpness;
} else {
allSharp = 0;
}
}
avgSharpness /= sharpCount;
if (avgSharpness>1.0) {
avgSharpness = 1.0;
}
if (!v->numEdges) {
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
} else if (_vert_isBoundary(v)) {
int numBoundary = 0;
ss->meshIFC.vertDataZero(ss->meshData, r);
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
if (_edge_isBoundary(e)) {
ss->meshIFC.vertDataAdd(ss->meshData, r, VERT_getCo(_edge_getOtherVert(e, v), curLvl));
numBoundary++;
}
}
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, 0.75);
ss->meshIFC.vertDataMulN(ss->meshData, r, 0.25f/numBoundary);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
} else {
int numEdges = 0, numFaces = 0;
ss->meshIFC.vertDataZero(ss->meshData, q);
for (i=0; i<v->numFaces; i++) {
CCGFace *f = v->faces[i];
ss->meshIFC.vertDataAdd(ss->meshData, q, FACE_getCenterData(f));
numFaces++;
}
ss->meshIFC.vertDataMulN(ss->meshData, q, 1.0f/numFaces);
ss->meshIFC.vertDataZero(ss->meshData, r);
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
ss->meshIFC.vertDataAdd(ss->meshData, r, VERT_getCo(_edge_getOtherVert(e, v), curLvl));
numEdges++;
}
ss->meshIFC.vertDataMulN(ss->meshData, r, 1.0f/numEdges);
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, numEdges-2.0f);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, q);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, 1.0f/numEdges);
}
if (sharpCount>1) {
ss->meshIFC.vertDataZero(ss->meshData, q);
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
float sharpness = EDGE_getSharpness(e, curLvl, ss);
if (sharpness != 0.0) {
CCGVert *oV = _edge_getOtherVert(e, v);
ss->meshIFC.vertDataAdd(ss->meshData, q, VERT_getCo(oV, curLvl));
}
}
ss->meshIFC.vertDataMulN(ss->meshData, q, (float) 1/sharpCount);
if (sharpCount!=2 || allSharp) {
// q = q + (co-q)*avgSharpness
ss->meshIFC.vertDataCopy(ss->meshData, r, co);
ss->meshIFC.vertDataSub(ss->meshData, r, q);
ss->meshIFC.vertDataMulN(ss->meshData, r, avgSharpness);
ss->meshIFC.vertDataAdd(ss->meshData, q, r);
}
// r = co*.75 + q*.25
ss->meshIFC.vertDataCopy(ss->meshData, r, co);
ss->meshIFC.vertDataMulN(ss->meshData, r, .75);
ss->meshIFC.vertDataMulN(ss->meshData, q, .25);
ss->meshIFC.vertDataAdd(ss->meshData, r, q);
// nCo = nCo + (r-nCo)*avgSharpness
ss->meshIFC.vertDataSub(ss->meshData, r, nCo);
ss->meshIFC.vertDataMulN(ss->meshData, r, avgSharpness);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
}
v->flags = 0;
}
if (ss->useAgeCounts) {
for (i=0; i<numEffectedV; i++) {
CCGVert *v = effectedV[i];
byte *userData = ccgSubSurf_getVertUserData(ss, v);
*((int*) &userData[ss->vertUserAgeOffset]) = ss->currentAge;
}
for (i=0; i<numEffectedE; i++) {
CCGEdge *e = effectedE[i];
byte *userData = ccgSubSurf_getEdgeUserData(ss, e);
*((int*) &userData[ss->edgeUserAgeOffset]) = ss->currentAge;
}
for (i=0; i<numEffectedF; i++) {
CCGFace *f = effectedF[i];
byte *userData = ccgSubSurf_getFaceUserData(ss, f);
*((int*) &userData[ss->faceUserAgeOffset]) = ss->currentAge;
}
}
for (i=0; i<numEffectedE; i++) {
CCGEdge *e = effectedE[i];
ss->meshIFC.vertDataCopy(ss->meshData, EDGE_getCo(e, nextLvl, 0), VERT_getCo(e->v0, nextLvl));
ss->meshIFC.vertDataCopy(ss->meshData, EDGE_getCo(e, nextLvl, 2), VERT_getCo(e->v1, nextLvl));
}
for (i=0; i<numEffectedF; i++) {
CCGFace *f = effectedF[i];
for (S=0; S<f->numVerts; S++) {
CCGEdge *e = FACE_getEdges(f)[S];
CCGEdge *prevE = FACE_getEdges(f)[(S+f->numVerts-1)%f->numVerts];
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, 0, 0), FACE_getCenterData(f));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIECo(f, nextLvl, S, 0), FACE_getCenterData(f));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, 1, 1), VERT_getCo(FACE_getVerts(f)[S], nextLvl));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIECo(f, nextLvl, S, 1), EDGE_getCo(FACE_getEdges(f)[S], nextLvl, 1));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, 1, 0), _edge_getCoVert(e, FACE_getVerts(f)[S], nextLvl, 1, vertDataSize));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, 0, 1), _edge_getCoVert(prevE, FACE_getVerts(f)[S], nextLvl, 1, vertDataSize));
}
}
for (curLvl=1; curLvl<subdivLevels; curLvl++) {
int edgeSize = 1 + (1<<curLvl);
int gridSize = 1 + (1<<(curLvl-1));
nextLvl = curLvl+1;
for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) {
CCGFace *f = (CCGFace*) effectedF[ptrIdx];
/* interior face midpoints
* o old interior face points
*/
for (S=0; S<f->numVerts; S++) {
for (y=0; y<gridSize-1; y++) {
for (x=0; x<gridSize-1; x++) {
int fx = 1 + 2*x;
int fy = 1 + 2*y;
void *co0 = FACE_getIFCo(f, curLvl, S, x+0, y+0);
void *co1 = FACE_getIFCo(f, curLvl, S, x+1, y+0);
void *co2 = FACE_getIFCo(f, curLvl, S, x+1, y+1);
void *co3 = FACE_getIFCo(f, curLvl, S, x+0, y+1);
void *co = FACE_getIFCo(f, nextLvl, S, fx, fy);
ss->meshIFC.vertDataAvg4(ss->meshData, co, co0, co1, co2, co3);
}
}
}
/* interior edge midpoints
* o old interior edge points
* o new interior face midpoints
*/
for (S=0; S<f->numVerts; S++) {
for (x=0; x<gridSize-1; x++) {
int fx = x*2 + 1;
void *co0 = FACE_getIECo(f, curLvl, S, x+0);
void *co1 = FACE_getIECo(f, curLvl, S, x+1);
void *co2 = FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx);
void *co3 = FACE_getIFCo(f, nextLvl, S, fx, 1);
void *co = FACE_getIECo(f, nextLvl, S, fx);
ss->meshIFC.vertDataAvg4(ss->meshData, co, co0, co1, co2, co3);
}
/* interior face interior edge midpoints
* o old interior face points
* o new interior face midpoints
*/
/* vertical */
for (x=1; x<gridSize-1; x++) {
for (y=0; y<gridSize-1; y++) {
int fx = x*2;
int fy = y*2+1;
void *co0 = FACE_getIFCo(f, curLvl, S, x, y+0);
void *co1 = FACE_getIFCo(f, curLvl, S, x, y+1);
void *co2 = FACE_getIFCo(f, nextLvl, S, fx-1, fy);
void *co3 = FACE_getIFCo(f, nextLvl, S, fx+1, fy);
void *co = FACE_getIFCo(f, nextLvl, S, fx, fy);
ss->meshIFC.vertDataAvg4(ss->meshData, co, co0, co1, co2, co3);
}
}
/* horizontal */
for (y=1; y<gridSize-1; y++) {
for (x=0; x<gridSize-1; x++) {
int fx = x*2+1;
int fy = y*2;
void *co0 = FACE_getIFCo(f, curLvl, S, x+0, y);
void *co1 = FACE_getIFCo(f, curLvl, S, x+1, y);
void *co2 = FACE_getIFCo(f, nextLvl, S, fx, fy-1);
void *co3 = FACE_getIFCo(f, nextLvl, S, fx, fy+1);
void *co = FACE_getIFCo(f, nextLvl, S, fx, fy);
ss->meshIFC.vertDataAvg4(ss->meshData, co, co0, co1, co2, co3);
}
}
}
}
/* exterior edge midpoints
* o old exterior edge points
* o new interior face midpoints
*/
for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) {
CCGEdge *e = (CCGEdge*) effectedE[ptrIdx];
float sharpness = EDGE_getSharpness(e, curLvl, ss);
if (_edge_isBoundary(e) || sharpness>1.0) {
for (x=0; x<edgeSize-1; x++) {
int fx = x*2 + 1;
void *co0 = EDGE_getCo(e, curLvl, x+0);
void *co1 = EDGE_getCo(e, curLvl, x+1);
void *co = EDGE_getCo(e, nextLvl, fx);
ss->meshIFC.vertDataCopy(ss->meshData, co, co0);
ss->meshIFC.vertDataAdd(ss->meshData, co, co1);
ss->meshIFC.vertDataMulN(ss->meshData, co, 0.5);
}
} else {
for (x=0; x<edgeSize-1; x++) {
int fx = x*2 + 1;
void *co0 = EDGE_getCo(e, curLvl, x+0);
void *co1 = EDGE_getCo(e, curLvl, x+1);
void *co = EDGE_getCo(e, nextLvl, fx);
int numFaces = 0;
ss->meshIFC.vertDataCopy(ss->meshData, q, co0);
ss->meshIFC.vertDataAdd(ss->meshData, q, co1);
for (i=0; i<e->numFaces; i++) {
CCGFace *f = e->faces[i];
ss->meshIFC.vertDataAdd(ss->meshData, q, _face_getIFCoEdge(f, e, nextLvl, fx, 1, subdivLevels, vertDataSize));
numFaces++;
}
ss->meshIFC.vertDataMulN(ss->meshData, q, 1.0f/(2.0f+numFaces));
ss->meshIFC.vertDataCopy(ss->meshData, r, co0);
ss->meshIFC.vertDataAdd(ss->meshData, r, co1);
ss->meshIFC.vertDataMulN(ss->meshData, r, 0.5);
ss->meshIFC.vertDataCopy(ss->meshData, co, q);
ss->meshIFC.vertDataSub(ss->meshData, r, q);
ss->meshIFC.vertDataMulN(ss->meshData, r, sharpness);
ss->meshIFC.vertDataAdd(ss->meshData, co, r);
}
}
}
/* exterior vertex shift
* o old vertex points (shifting)
* o old exterior edge points
* o new interior face midpoints
*/
for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) {
CCGVert *v = (CCGVert*) effectedV[ptrIdx];
void *co = VERT_getCo(v, curLvl);
void *nCo = VERT_getCo(v, nextLvl);
int sharpCount = 0, allSharp = 1;
float avgSharpness = 0.0;
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
float sharpness = EDGE_getSharpness(e, curLvl, ss);
if (sharpness!=0.0f) {
sharpCount++;
avgSharpness += sharpness;
} else {
allSharp = 0;
}
}
avgSharpness /= sharpCount;
if (avgSharpness>1.0) {
avgSharpness = 1.0;
}
if (!v->numEdges) {
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
} else if (_vert_isBoundary(v)) {
int numBoundary = 0;
ss->meshIFC.vertDataZero(ss->meshData, r);
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
if (_edge_isBoundary(e)) {
ss->meshIFC.vertDataAdd(ss->meshData, r, _edge_getCoVert(e, v, curLvl, 1, vertDataSize));
numBoundary++;
}
}
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, 0.75);
ss->meshIFC.vertDataMulN(ss->meshData, r, 0.25f/numBoundary);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
} else {
int cornerIdx = (1 + (1<<(curLvl))) - 2;
int numEdges = 0, numFaces = 0;
ss->meshIFC.vertDataZero(ss->meshData, q);
for (i=0; i<v->numFaces; i++) {
CCGFace *f = v->faces[i];
ss->meshIFC.vertDataAdd(ss->meshData, q, FACE_getIFCo(f, nextLvl, _face_getVertIndex(f,v), cornerIdx, cornerIdx));
numFaces++;
}
ss->meshIFC.vertDataMulN(ss->meshData, q, 1.0f/numFaces);
ss->meshIFC.vertDataZero(ss->meshData, r);
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
ss->meshIFC.vertDataAdd(ss->meshData, r, _edge_getCoVert(e, v, curLvl, 1,vertDataSize));
numEdges++;
}
ss->meshIFC.vertDataMulN(ss->meshData, r, 1.0f/numEdges);
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, numEdges-2.0f);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, q);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, 1.0f/numEdges);
}
if (sharpCount>1) {
ss->meshIFC.vertDataZero(ss->meshData, q);
for (i=0; i<v->numEdges; i++) {
CCGEdge *e = v->edges[i];
float sharpness = EDGE_getSharpness(e, curLvl, ss);
if (sharpness != 0.0) {
ss->meshIFC.vertDataAdd(ss->meshData, q, _edge_getCoVert(e, v, curLvl, 1, vertDataSize));
}
}
ss->meshIFC.vertDataMulN(ss->meshData, q, (float) 1/sharpCount);
if (sharpCount!=2 || allSharp) {
// q = q + (co-q)*avgSharpness
ss->meshIFC.vertDataCopy(ss->meshData, r, co);
ss->meshIFC.vertDataSub(ss->meshData, r, q);
ss->meshIFC.vertDataMulN(ss->meshData, r, avgSharpness);
ss->meshIFC.vertDataAdd(ss->meshData, q, r);
}
// r = co*.75 + q*.25
ss->meshIFC.vertDataCopy(ss->meshData, r, co);
ss->meshIFC.vertDataMulN(ss->meshData, r, .75);
ss->meshIFC.vertDataMulN(ss->meshData, q, .25);
ss->meshIFC.vertDataAdd(ss->meshData, r, q);
// nCo = nCo + (r-nCo)*avgSharpness
ss->meshIFC.vertDataSub(ss->meshData, r, nCo);
ss->meshIFC.vertDataMulN(ss->meshData, r, avgSharpness);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
}
}
/* exterior edge interior shift
* o old exterior edge midpoints (shifting)
* o old exterior edge midpoints
* o new interior face midpoints
*/
for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) {
CCGEdge *e = (CCGEdge*) effectedE[ptrIdx];
float sharpness = EDGE_getSharpness(e, curLvl, ss);
int sharpCount = 0;
float avgSharpness = 0.0;
if (sharpness!=0.0f) {
sharpCount = 2;
avgSharpness += sharpness;
if (avgSharpness>1.0) {
avgSharpness = 1.0;
}
} else {
sharpCount = 0;
avgSharpness = 0;
}
if (_edge_isBoundary(e) && sharpCount<2) {
for (x=1; x<edgeSize-1; x++) {
int fx = x*2;
void *co = EDGE_getCo(e, curLvl, x);
void *nCo = EDGE_getCo(e, nextLvl, fx);
ss->meshIFC.vertDataCopy(ss->meshData, r, EDGE_getCo(e, curLvl, x-1));
ss->meshIFC.vertDataAdd(ss->meshData, r, EDGE_getCo(e, curLvl, x+1));
ss->meshIFC.vertDataMulN(ss->meshData, r, 0.5);
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, 0.75);
ss->meshIFC.vertDataMulN(ss->meshData, r, 0.25);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
}
} else {
for (x=1; x<edgeSize-1; x++) {
int fx = x*2;
void *co = EDGE_getCo(e, curLvl, x);
void *nCo = EDGE_getCo(e, nextLvl, fx);
int numFaces = 0;
ss->meshIFC.vertDataZero(ss->meshData, q);
ss->meshIFC.vertDataZero(ss->meshData, r);
ss->meshIFC.vertDataAdd(ss->meshData, r, EDGE_getCo(e, curLvl, x-1));
ss->meshIFC.vertDataAdd(ss->meshData, r, EDGE_getCo(e, curLvl, x+1));
for (i=0; i<e->numFaces; i++) {
CCGFace *f = e->faces[i];
ss->meshIFC.vertDataAdd(ss->meshData, q, _face_getIFCoEdge(f, e, nextLvl, fx-1, 1, subdivLevels, vertDataSize));
ss->meshIFC.vertDataAdd(ss->meshData, q, _face_getIFCoEdge(f, e, nextLvl, fx+1, 1, subdivLevels, vertDataSize));
ss->meshIFC.vertDataAdd(ss->meshData, r, _face_getIFCoEdge(f, e, curLvl, x, 1, subdivLevels, vertDataSize));
numFaces++;
}
ss->meshIFC.vertDataMulN(ss->meshData, q, 1.0/(numFaces*2.0f));
ss->meshIFC.vertDataMulN(ss->meshData, r, 1.0/(2.0f + numFaces));
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, (float) numFaces);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, q);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, 1.0f/(2+numFaces));
if (sharpCount==2) {
ss->meshIFC.vertDataCopy(ss->meshData, q, co);
ss->meshIFC.vertDataMulN(ss->meshData, q, 6.0f);
ss->meshIFC.vertDataAdd(ss->meshData, q, EDGE_getCo(e, curLvl, x-1));
ss->meshIFC.vertDataAdd(ss->meshData, q, EDGE_getCo(e, curLvl, x+1));
ss->meshIFC.vertDataMulN(ss->meshData, q, 1/8.0f);
ss->meshIFC.vertDataSub(ss->meshData, q, nCo);
ss->meshIFC.vertDataMulN(ss->meshData, q, avgSharpness);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, q);
}
}
}
}
for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) {
CCGFace *f = (CCGFace*) effectedF[ptrIdx];
/* interior center point shift
* o old face center point (shifting)
* o old interior edge points
* o new interior face midpoints
*/
ss->meshIFC.vertDataZero(ss->meshData, q);
for (S=0; S<f->numVerts; S++) {
ss->meshIFC.vertDataAdd(ss->meshData, q, FACE_getIFCo(f, nextLvl, S, 1, 1));
}
ss->meshIFC.vertDataMulN(ss->meshData, q, 1.0f/f->numVerts);
ss->meshIFC.vertDataZero(ss->meshData, r);
for (S=0; S<f->numVerts; S++) {
ss->meshIFC.vertDataAdd(ss->meshData, r, FACE_getIECo(f, curLvl, S, 1));
}
ss->meshIFC.vertDataMulN(ss->meshData, r, 1.0f/f->numVerts);
ss->meshIFC.vertDataMulN(ss->meshData, FACE_getCenterData(f), f->numVerts-2.0f);
ss->meshIFC.vertDataAdd(ss->meshData, FACE_getCenterData(f), q);
ss->meshIFC.vertDataAdd(ss->meshData, FACE_getCenterData(f), r);
ss->meshIFC.vertDataMulN(ss->meshData, FACE_getCenterData(f), 1.0f/f->numVerts);
for (S=0; S<f->numVerts; S++) {
/* interior face shift
* o old interior face point (shifting)
* o new interior edge midpoints
* o new interior face midpoints
*/
for (x=1; x<gridSize-1; x++) {
for (y=1; y<gridSize-1; y++) {
int fx = x*2;
int fy = y*2;
void *co = FACE_getIFCo(f, curLvl, S, x, y);
void *nCo = FACE_getIFCo(f, nextLvl, S, fx, fy);
ss->meshIFC.vertDataAvg4(ss->meshData, q, FACE_getIFCo(f, nextLvl, S, fx-1, fy-1),
FACE_getIFCo(f, nextLvl, S, fx+1, fy-1),
FACE_getIFCo(f, nextLvl, S, fx+1, fy+1),
FACE_getIFCo(f, nextLvl, S, fx-1, fy+1));
ss->meshIFC.vertDataAvg4(ss->meshData, r, FACE_getIFCo(f, nextLvl, S, fx-1, fy+0),
FACE_getIFCo(f, nextLvl, S, fx+1, fy+0),
FACE_getIFCo(f, nextLvl, S, fx+0, fy-1),
FACE_getIFCo(f, nextLvl, S, fx+0, fy+1));
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
ss->meshIFC.vertDataSub(ss->meshData, nCo, q);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, 0.25f);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
}
}
/* interior edge interior shift
* o old interior edge point (shifting)
* o new interior edge midpoints
* o new interior face midpoints
*/
for (x=1; x<gridSize-1; x++) {
int fx = x*2;
void *co = FACE_getIECo(f, curLvl, S, x);
void *nCo = FACE_getIECo(f, nextLvl, S, fx);
ss->meshIFC.vertDataAvg4(ss->meshData, q, FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx-1),
FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx+1),
FACE_getIFCo(f, nextLvl, S, fx+1, +1),
FACE_getIFCo(f, nextLvl, S, fx-1, +1));
ss->meshIFC.vertDataAvg4(ss->meshData, r, FACE_getIECo(f, nextLvl, S, fx-1),
FACE_getIECo(f, nextLvl, S, fx+1),
FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 1, fx),
FACE_getIFCo(f, nextLvl, S, fx, 1));
ss->meshIFC.vertDataCopy(ss->meshData, nCo, co);
ss->meshIFC.vertDataSub(ss->meshData, nCo, q);
ss->meshIFC.vertDataMulN(ss->meshData, nCo, 0.25f);
ss->meshIFC.vertDataAdd(ss->meshData, nCo, r);
}
}
}
/* copy down */
edgeSize = 1 + (1<<(nextLvl));
gridSize = 1 + (1<<((nextLvl)-1));
cornerIdx = gridSize-1;
for (i=0; i<numEffectedE; i++) {
CCGEdge *e = effectedE[i];
ss->meshIFC.vertDataCopy(ss->meshData, EDGE_getCo(e, nextLvl, 0), VERT_getCo(e->v0, nextLvl));
ss->meshIFC.vertDataCopy(ss->meshData, EDGE_getCo(e, nextLvl, edgeSize-1), VERT_getCo(e->v1, nextLvl));
}
for (i=0; i<numEffectedF; i++) {
CCGFace *f = effectedF[i];
for (S=0; S<f->numVerts; S++) {
CCGEdge *e = FACE_getEdges(f)[S];
CCGEdge *prevE = FACE_getEdges(f)[(S+f->numVerts-1)%f->numVerts];
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, 0, 0), FACE_getCenterData(f));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIECo(f, nextLvl, S, 0), FACE_getCenterData(f));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, cornerIdx, cornerIdx), VERT_getCo(FACE_getVerts(f)[S], nextLvl));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIECo(f, nextLvl, S, cornerIdx), EDGE_getCo(FACE_getEdges(f)[S], nextLvl, cornerIdx));
for (x=1; x<gridSize-1; x++) {
void *co = FACE_getIECo(f, nextLvl, S, x);
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, x, 0), co);
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, (S+1)%f->numVerts, 0, x), co);
}
for (x=0; x<gridSize-1; x++) {
int eI = gridSize-1-x;
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, cornerIdx, x), _edge_getCoVert(e, FACE_getVerts(f)[S], nextLvl, eI,vertDataSize));
ss->meshIFC.vertDataCopy(ss->meshData, FACE_getIFCo(f, nextLvl, S, x, cornerIdx), _edge_getCoVert(prevE, FACE_getVerts(f)[S], nextLvl, eI,vertDataSize));
}
}
}
}
#define FACE_getIFNo(f, lvl, S, x, y) _face_getIFNo(f, lvl, S, x, y, subdivLevels, vertDataSize, normalDataOffset)
#define AddNo(a, b) {a[0]+= b[0], a[1]+= b[1], a[2] += b[2];}
#define CopyNo(a, b) {a[0] = b[0], a[1] = b[1], a[2] = b[2];}
if (ss->calcVertNormals) {
int lvl = ss->subdivLevels;
int edgeSize = 1 + (1<<lvl);
int gridSize = 1 + (1<<(lvl-1));
int normalDataOffset = ss->normalDataOffset;
for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) {
CCGFace *f = (CCGFace*) effectedF[ptrIdx];
int S, x, y;
for (S=0; S<f->numVerts; S++) {
for (y=0; y<gridSize; y++) {
for (x=0; x<gridSize; x++) {
float *no = FACE_getIFNo(f, lvl, S, x, y);
no[0] = no[1] = no[2] = 0.0;
}
}
}
}
for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) {
CCGFace *f = (CCGFace*) effectedF[ptrIdx];
int S, x, y;
for (S=0; S<f->numVerts; S++) {
for (y=0; y<gridSize-1; y++) {
for (x=0; x<gridSize-1; x++) {
float *a = FACE_getIFCo(f, lvl, S, x+0, y+0);
float *b = FACE_getIFCo(f, lvl, S, x+1, y+0);
float *c = FACE_getIFCo(f, lvl, S, x+1, y+1);
float *d = FACE_getIFCo(f, lvl, S, x+0, y+1);
float a_cX = c[0]-a[0], a_cY = c[1]-a[1], a_cZ = c[2]-a[2];
float b_dX = d[0]-b[0], b_dY = d[1]-b[1], b_dZ = d[2]-b[2];
float no[3];
no[0] = b_dY*a_cZ - b_dZ*a_cY;
no[1] = b_dZ*a_cX - b_dX*a_cZ;
no[2] = b_dX*a_cY - b_dY*a_cX;
AddNo(FACE_getIFNo(f, lvl, S, x+0, y+0), no);
AddNo(FACE_getIFNo(f, lvl, S, x+1, y+0), no);
AddNo(FACE_getIFNo(f, lvl, S, x+1, y+1), no);
AddNo(FACE_getIFNo(f, lvl, S, x+0, y+1), no);
if (x==0 && y==0) {
int K;
AddNo(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, 1), no);
AddNo(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, 1, 0), no);
for (K=0; K<f->numVerts; K++) {
if (K!=S) {
AddNo(FACE_getIFNo(f, lvl, K, 0, 0), no);
}
}
} else if (y==0) {
AddNo(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, x), no);
AddNo(FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, x+1), no);
} else if (x==0) {
AddNo(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, y, 0), no);
AddNo(FACE_getIFNo(f, lvl, (S-1+f->numVerts)%f->numVerts, y+1, 0), no);
}
}
}
}
}
for (ptrIdx=0; ptrIdx<numEffectedV; ptrIdx++) {
CCGVert *v = (CCGVert*) effectedV[ptrIdx];
float no[3] = {0};
for (i=0; i<v->numFaces; i++) {
CCGFace *f = v->faces[i];
AddNo(no, FACE_getIFNo(f, lvl, _face_getVertIndex(f,v), gridSize-1, gridSize-1));
}
for (i=0; i<v->numFaces; i++) {
CCGFace *f = v->faces[i];
CopyNo(FACE_getIFNo(f, lvl, _face_getVertIndex(f,v), gridSize-1, gridSize-1), no);
}
}
for (ptrIdx=0; ptrIdx<numEffectedE; ptrIdx++) {
CCGEdge *e = (CCGEdge*) effectedE[ptrIdx];
int x;
for (x=0; x<edgeSize; x++) {
float *no = _edge_getNo(e, lvl, x, vertDataSize, normalDataOffset);
no[0] = no[1] = no[2] = 0.0;
}
for (i=0; i<e->numFaces; i++) {
CCGFace *f = e->faces[i];
for (x=1; x<edgeSize-1; x++) {
AddNo( _edge_getNo(e, lvl, x, vertDataSize, normalDataOffset),
_face_getIFNoEdge(f, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset));
}
}
for (i=0; i<e->numFaces; i++) {
CCGFace *f = e->faces[i];
for (x=1; x<edgeSize-1; x++) {
CopyNo( _face_getIFNoEdge(f, e, lvl, x, 0, subdivLevels, vertDataSize, normalDataOffset),
_edge_getNo(e, lvl, x, vertDataSize, normalDataOffset));
}
}
}
for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) {
CCGFace *f = (CCGFace*) effectedF[ptrIdx];
int S;
for (S=0; S<f->numVerts; S++) {
CopyNo( FACE_getIFNo(f, lvl, (S+1)%f->numVerts, 0, gridSize-1),
FACE_getIFNo(f, lvl, S, gridSize-1, 0));
}
}
for (ptrIdx=0; ptrIdx<numEffectedF; ptrIdx++) {
CCGFace *f = (CCGFace*) effectedF[ptrIdx];
int S, x, y;
for (S=0; S<f->numVerts; S++) {
for (y=0; y<gridSize; y++) {
for (x=0; x<gridSize; x++) {
float *no = FACE_getIFNo(f, lvl, S, x, y);
float length = sqrt(no[0]*no[0] + no[1]*no[1] + no[2]*no[2]);
if (length>FLT_EPSILON) {
float invLength = 1.0f/length;
no[0] *= invLength;
no[1] *= invLength;
no[2] *= invLength;
} else {
no[0] = no[1] = no[2] = 0.0;
}
}
}
}
}
}
#undef CopyNo
#undef AddNo
#undef FACE_getIFNo
#undef VERT_getCo
#undef EDGE_getCo
#undef FACE_getIECo
#undef FACE_getIFCo
CCGSUBSURF_free(ss, effectedF);
CCGSUBSURF_free(ss, effectedE);
CCGSUBSURF_free(ss, effectedV);
}
/*** External API accessor functions ***/
int ccgSubSurf_getNumVerts(CCGSubSurf *ss) {
return ss->vMap->numEntries;
}
int ccgSubSurf_getNumEdges(CCGSubSurf *ss) {
return ss->eMap->numEntries;
}
int ccgSubSurf_getNumFaces(CCGSubSurf *ss) {
return ss->fMap->numEntries;
}
CCGVert *ccgSubSurf_getVert(CCGSubSurf *ss, CCGVertHDL v) {
return (CCGVert*) _ehash_lookup(ss->vMap, v);
}
CCGEdge *ccgSubSurf_getEdge(CCGSubSurf *ss, CCGEdgeHDL e) {
return (CCGEdge*) _ehash_lookup(ss->eMap, e);
}
CCGFace *ccgSubSurf_getFace(CCGSubSurf *ss, CCGFaceHDL f) {
return (CCGFace*) _ehash_lookup(ss->fMap, f);
}
int ccgSubSurf_getSubdivisionLevels(CCGSubSurf *ss) {
return ss->subdivLevels;
}
int ccgSubSurf_getEdgeSize(CCGSubSurf *ss) {
return ccgSubSurf_getEdgeLevelSize(ss, ss->subdivLevels);
}
int ccgSubSurf_getEdgeLevelSize(CCGSubSurf *ss, int level) {
if (level<1 || level>ss->subdivLevels) {
return -1;
} else {
return 1 + (1<<level);
}
}
int ccgSubSurf_getGridSize(CCGSubSurf *ss) {
return ccgSubSurf_getGridLevelSize(ss, ss->subdivLevels);
}
int ccgSubSurf_getGridLevelSize(CCGSubSurf *ss, int level) {
if (level<1 || level>ss->subdivLevels) {
return -1;
} else {
return 1 + (1<<(level-1));
}
}
/* Vert accessors */
CCGVertHDL ccgSubSurf_getVertVertHandle(CCGSubSurf *ss, CCGVert *v) {
return v->vHDL;
}
int ccgSubSurf_getVertAge(CCGSubSurf *ss, CCGVert *v) {
if (ss->useAgeCounts) {
byte *userData = ccgSubSurf_getVertUserData(ss, v);
return ss->currentAge - *((int*) &userData[ss->vertUserAgeOffset]);
} else {
return 0;
}
}
void *ccgSubSurf_getVertUserData(CCGSubSurf *ss, CCGVert *v) {
return VERT_getLevelData(v) + ss->meshIFC.vertDataSize*(ss->subdivLevels+1);
}
int ccgSubSurf_getVertNumFaces(CCGSubSurf *ss, CCGVert *v) {
return v->numFaces;
}
CCGFace *ccgSubSurf_getVertFace(CCGSubSurf *ss, CCGVert *v, int index) {
if (index<0 || index>=v->numFaces) {
return NULL;
} else {
return v->faces[index];
}
}
int ccgSubSurf_getVertNumEdges(CCGSubSurf *ss, CCGVert *v) {
return v->numEdges;
}
CCGEdge *ccgSubSurf_getVertEdge(CCGSubSurf *ss, CCGVert *v, int index) {
if (index<0 || index>=v->numEdges) {
return NULL;
} else {
return v->edges[index];
}
}
void *ccgSubSurf_getVertData(CCGSubSurf *ss, CCGVert *v) {
return ccgSubSurf_getVertLevelData(ss, v, ss->subdivLevels);
}
void *ccgSubSurf_getVertLevelData(CCGSubSurf *ss, CCGVert *v, int level) {
if (level<0 || level>ss->subdivLevels) {
return NULL;
} else {
return _vert_getCo(v, level, ss->meshIFC.vertDataSize);
}
}
/* Edge accessors */
CCGEdgeHDL ccgSubSurf_getEdgeEdgeHandle(CCGSubSurf *ss, CCGEdge *e) {
return e->eHDL;
}
int ccgSubSurf_getEdgeAge(CCGSubSurf *ss, CCGEdge *e) {
if (ss->useAgeCounts) {
byte *userData = ccgSubSurf_getEdgeUserData(ss, e);
return ss->currentAge - *((int*) &userData[ss->edgeUserAgeOffset]);
} else {
return 0;
}
}
void *ccgSubSurf_getEdgeUserData(CCGSubSurf *ss, CCGEdge *e) {
return EDGE_getLevelData(e) + ss->meshIFC.vertDataSize *((ss->subdivLevels+1) + (1<<(ss->subdivLevels+1))-1);
}
int ccgSubSurf_getEdgeNumFaces(CCGSubSurf *ss, CCGEdge *e) {
return e->numFaces;
}
CCGFace *ccgSubSurf_getEdgeFace(CCGSubSurf *ss, CCGEdge *e, int index) {
if (index<0 || index>=e->numFaces) {
return NULL;
} else {
return e->faces[index];
}
}
CCGVert *ccgSubSurf_getEdgeVert0(CCGSubSurf *ss, CCGEdge *e) {
return e->v0;
}
CCGVert *ccgSubSurf_getEdgeVert1(CCGSubSurf *ss, CCGEdge *e) {
return e->v1;
}
void *ccgSubSurf_getEdgeDataArray(CCGSubSurf *ss, CCGEdge *e) {
return ccgSubSurf_getEdgeData(ss, e, 0);
}
void *ccgSubSurf_getEdgeData(CCGSubSurf *ss, CCGEdge *e, int x) {
return ccgSubSurf_getEdgeLevelData(ss, e, x, ss->subdivLevels);
}
void *ccgSubSurf_getEdgeLevelData(CCGSubSurf *ss, CCGEdge *e, int x, int level) {
if (level<0 || level>ss->subdivLevels) {
return NULL;
} else {
return _edge_getCo(e, level, x, ss->meshIFC.vertDataSize);
}
}
/* Face accessors */
CCGFaceHDL ccgSubSurf_getFaceFaceHandle(CCGSubSurf *ss, CCGFace *f) {
return f->fHDL;
}
int ccgSubSurf_getFaceAge(CCGSubSurf *ss, CCGFace *f) {
if (ss->useAgeCounts) {
byte *userData = ccgSubSurf_getFaceUserData(ss, f);
return ss->currentAge - *((int*) &userData[ss->faceUserAgeOffset]);
} else {
return 0;
}
}
void *ccgSubSurf_getFaceUserData(CCGSubSurf *ss, CCGFace *f) {
int maxGridSize = 1 + (1<<(ss->subdivLevels-1));
return FACE_getCenterData(f) + ss->meshIFC.vertDataSize *(1 + f->numVerts*maxGridSize + f->numVerts*maxGridSize*maxGridSize);
}
int ccgSubSurf_getFaceNumVerts(CCGSubSurf *ss, CCGFace *f) {
return f->numVerts;
}
CCGVert *ccgSubSurf_getFaceVert(CCGSubSurf *ss, CCGFace *f, int index) {
if (index<0 || index>=f->numVerts) {
return NULL;
} else {
return FACE_getVerts(f)[index];
}
}
CCGEdge *ccgSubSurf_getFaceEdge(CCGSubSurf *ss, CCGFace *f, int index) {
if (index<0 || index>=f->numVerts) {
return NULL;
} else {
return FACE_getEdges(f)[index];
}
}
void *ccgSubSurf_getFaceCenterData(CCGSubSurf *ss, CCGFace *f) {
return FACE_getCenterData(f);
}
void *ccgSubSurf_getFaceGridEdgeDataArray(CCGSubSurf *ss, CCGFace *f, int gridIndex) {
return ccgSubSurf_getFaceGridEdgeData(ss, f, gridIndex, 0);
}
void *ccgSubSurf_getFaceGridEdgeData(CCGSubSurf *ss, CCGFace *f, int gridIndex, int x) {
return _face_getIECo(f, ss->subdivLevels, gridIndex, x, ss->subdivLevels, ss->meshIFC.vertDataSize);
}
void *ccgSubSurf_getFaceGridDataArray(CCGSubSurf *ss, CCGFace *f, int gridIndex) {
return ccgSubSurf_getFaceGridData(ss, f, gridIndex, 0, 0);
}
void *ccgSubSurf_getFaceGridData(CCGSubSurf *ss, CCGFace *f, int gridIndex, int x, int y) {
return _face_getIFCo(f, ss->subdivLevels, gridIndex, x, y, ss->subdivLevels, ss->meshIFC.vertDataSize);
}
/*** External API iterator functions ***/
CCGVertIterator *ccgSubSurf_getVertIterator(CCGSubSurf *ss) {
return (CCGVertIterator*) _ehashIterator_new(ss->vMap);
}
CCGEdgeIterator *ccgSubSurf_getEdgeIterator(CCGSubSurf *ss) {
return (CCGEdgeIterator*) _ehashIterator_new(ss->eMap);
}
CCGFaceIterator *ccgSubSurf_getFaceIterator(CCGSubSurf *ss) {
return (CCGFaceIterator*) _ehashIterator_new(ss->fMap);
}
CCGVert *ccgVertIterator_getCurrent(CCGVertIterator *vi) {
return (CCGVert*) _ehashIterator_getCurrent((EHashIterator*) vi);
}
int ccgVertIterator_isStopped(CCGVertIterator *vi) {
return _ehashIterator_isStopped((EHashIterator*) vi);
}
void ccgVertIterator_next(CCGVertIterator *vi) {
_ehashIterator_next((EHashIterator*) vi);
}
void ccgVertIterator_free(CCGVertIterator *vi) {
_ehashIterator_free((EHashIterator*) vi);
}
CCGEdge *ccgEdgeIterator_getCurrent(CCGEdgeIterator *vi) {
return (CCGEdge*) _ehashIterator_getCurrent((EHashIterator*) vi);
}
int ccgEdgeIterator_isStopped(CCGEdgeIterator *vi) {
return _ehashIterator_isStopped((EHashIterator*) vi);
}
void ccgEdgeIterator_next(CCGEdgeIterator *vi) {
_ehashIterator_next((EHashIterator*) vi);
}
void ccgEdgeIterator_free(CCGEdgeIterator *vi) {
_ehashIterator_free((EHashIterator*) vi);
}
CCGFace *ccgFaceIterator_getCurrent(CCGFaceIterator *vi) {
return (CCGFace*) _ehashIterator_getCurrent((EHashIterator*) vi);
}
int ccgFaceIterator_isStopped(CCGFaceIterator *vi) {
return _ehashIterator_isStopped((EHashIterator*) vi);
}
void ccgFaceIterator_next(CCGFaceIterator *vi) {
_ehashIterator_next((EHashIterator*) vi);
}
void ccgFaceIterator_free(CCGFaceIterator *vi) {
_ehashIterator_free((EHashIterator*) vi);
}
/*** Extern API final vert/edge/face interface ***/
int ccgSubSurf_getNumFinalVerts(CCGSubSurf *ss) {
int edgeSize = 1 + (1<<ss->subdivLevels);
int gridSize = 1 + (1<<(ss->subdivLevels-1));
int numFinalVerts = ss->vMap->numEntries + ss->eMap->numEntries*(edgeSize-2) + ss->fMap->numEntries + ss->numGrids*((gridSize-2) + ((gridSize-2)*(gridSize-2)));
return numFinalVerts;
}
int ccgSubSurf_getNumFinalEdges(CCGSubSurf *ss) {
int edgeSize = 1 + (1<<ss->subdivLevels);
int gridSize = 1 + (1<<(ss->subdivLevels-1));
int numFinalEdges = ss->eMap->numEntries*(edgeSize-1) + ss->numGrids*((gridSize-1) + 2*((gridSize-2)*(gridSize-1)));
return numFinalEdges;
}
int ccgSubSurf_getNumFinalFaces(CCGSubSurf *ss) {
int gridSize = 1 + (1<<(ss->subdivLevels-1));
int numFinalFaces = ss->numGrids*((gridSize-1)*(gridSize-1));
return numFinalFaces;
}
|