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
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*/
/** \file
* \ingroup bke
*/
#include <math.h> // floor
#include <string.h>
#include <stdlib.h>
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BLI_ghash.h"
#include "BLI_linklist.h"
#include "DNA_anim_types.h"
#include "DNA_curve_types.h"
#include "DNA_material_types.h"
/* for dereferencing pointers */
#include "DNA_key_types.h"
#include "DNA_scene_types.h"
#include "DNA_vfont_types.h"
#include "DNA_object_types.h"
#include "BKE_animsys.h"
#include "BKE_curve.h"
#include "BKE_displist.h"
#include "BKE_font.h"
#include "BKE_key.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_object.h"
#include "BKE_material.h"
#include "DEG_depsgraph.h"
#include "CLG_log.h"
/* globals */
/* local */
static CLG_LogRef LOG = {"bke.curve"};
static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3],
short cox, short coy,
float *lambda, float *mu, float vec[3]);
/* frees editcurve entirely */
void BKE_curve_editfont_free(Curve *cu)
{
if (cu->editfont) {
EditFont *ef = cu->editfont;
if (ef->textbuf)
MEM_freeN(ef->textbuf);
if (ef->textbufinfo)
MEM_freeN(ef->textbufinfo);
if (ef->selboxes)
MEM_freeN(ef->selboxes);
MEM_freeN(ef);
cu->editfont = NULL;
}
}
static void curve_editNurb_keyIndex_cv_free_cb(void *val)
{
CVKeyIndex *index = val;
MEM_freeN(index->orig_cv);
MEM_freeN(val);
}
void BKE_curve_editNurb_keyIndex_delCV(GHash *keyindex, const void *cv)
{
BLI_assert(keyindex != NULL);
BLI_ghash_remove(keyindex, cv, NULL, curve_editNurb_keyIndex_cv_free_cb);
}
void BKE_curve_editNurb_keyIndex_free(GHash **keyindex)
{
if (!(*keyindex)) {
return;
}
BLI_ghash_free(*keyindex, NULL, curve_editNurb_keyIndex_cv_free_cb);
*keyindex = NULL;
}
void BKE_curve_editNurb_free(Curve *cu)
{
if (cu->editnurb) {
BKE_nurbList_free(&cu->editnurb->nurbs);
BKE_curve_editNurb_keyIndex_free(&cu->editnurb->keyindex);
MEM_freeN(cu->editnurb);
cu->editnurb = NULL;
}
}
/** Free (or release) any data used by this curve (does not free the curve itself). */
void BKE_curve_free(Curve *cu)
{
BKE_animdata_free((ID *)cu, false);
BKE_curve_batch_cache_free(cu);
BKE_nurbList_free(&cu->nurb);
BKE_curve_editfont_free(cu);
BKE_curve_editNurb_free(cu);
MEM_SAFE_FREE(cu->mat);
MEM_SAFE_FREE(cu->str);
MEM_SAFE_FREE(cu->strinfo);
MEM_SAFE_FREE(cu->bb);
MEM_SAFE_FREE(cu->tb);
}
void BKE_curve_init(Curve *cu)
{
/* BLI_assert(MEMCMP_STRUCT_OFS_IS_ZERO(cu, id)); */ /* cu->type is already initialized... */
copy_v3_fl(cu->size, 1.0f);
cu->flag = CU_FRONT | CU_BACK | CU_DEFORM_BOUNDS_OFF | CU_PATH_RADIUS;
cu->pathlen = 100;
cu->resolu = cu->resolv = (cu->type == OB_SURF) ? 4 : 12;
cu->width = 1.0;
cu->wordspace = 1.0;
cu->spacing = cu->linedist = 1.0;
cu->fsize = 1.0;
cu->ulheight = 0.05;
cu->texflag = CU_AUTOSPACE;
cu->smallcaps_scale = 0.75f;
/* XXX: this one seems to be the best one in most cases, at least for curve deform... */
cu->twist_mode = CU_TWIST_MINIMUM;
cu->bevfac1 = 0.0f;
cu->bevfac2 = 1.0f;
cu->bevfac1_mapping = CU_BEVFAC_MAP_RESOLU;
cu->bevfac2_mapping = CU_BEVFAC_MAP_RESOLU;
cu->bb = BKE_boundbox_alloc_unit();
if (cu->type == OB_FONT) {
cu->vfont = cu->vfontb = cu->vfonti = cu->vfontbi = BKE_vfont_builtin_get();
cu->vfont->id.us += 4;
cu->str = MEM_malloc_arrayN(12, sizeof(unsigned char), "str");
BLI_strncpy(cu->str, "Text", 12);
cu->len = cu->len_wchar = cu->pos = 4;
cu->strinfo = MEM_calloc_arrayN(12, sizeof(CharInfo), "strinfo new");
cu->totbox = cu->actbox = 1;
cu->tb = MEM_calloc_arrayN(MAXTEXTBOX, sizeof(TextBox), "textbox");
cu->tb[0].w = cu->tb[0].h = 0.0;
}
}
Curve *BKE_curve_add(Main *bmain, const char *name, int type)
{
Curve *cu;
cu = BKE_libblock_alloc(bmain, ID_CU, name, 0);
cu->type = type;
BKE_curve_init(cu);
return cu;
}
/**
* Only copy internal data of Curve ID from source to already allocated/initialized destination.
* You probably never want to use that directly, use BKE_id_copy or BKE_id_copy_ex for typical needs.
*
* WARNING! This function will not handle ID user count!
*
* \param flag: Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
*/
void BKE_curve_copy_data(Main *bmain, Curve *cu_dst, const Curve *cu_src, const int flag)
{
BLI_listbase_clear(&cu_dst->nurb);
BKE_nurbList_duplicate(&(cu_dst->nurb), &(cu_src->nurb));
cu_dst->mat = MEM_dupallocN(cu_src->mat);
cu_dst->str = MEM_dupallocN(cu_src->str);
cu_dst->strinfo = MEM_dupallocN(cu_src->strinfo);
cu_dst->tb = MEM_dupallocN(cu_src->tb);
cu_dst->bb = MEM_dupallocN(cu_src->bb);
cu_dst->batch_cache = NULL;
if (cu_src->key && (flag & LIB_ID_COPY_SHAPEKEY)) {
BKE_id_copy_ex(bmain, &cu_src->key->id, (ID **)&cu_dst->key, flag);
}
cu_dst->editnurb = NULL;
cu_dst->editfont = NULL;
}
Curve *BKE_curve_copy(Main *bmain, const Curve *cu)
{
Curve *cu_copy;
BKE_id_copy(bmain, &cu->id, (ID **)&cu_copy);
return cu_copy;
}
void BKE_curve_make_local(Main *bmain, Curve *cu, const bool lib_local)
{
BKE_id_make_local_generic(bmain, &cu->id, true, lib_local);
}
/* Get list of nurbs from editnurbs structure */
ListBase *BKE_curve_editNurbs_get(Curve *cu)
{
if (cu->editnurb) {
return &cu->editnurb->nurbs;
}
return NULL;
}
short BKE_curve_type_get(Curve *cu)
{
Nurb *nu;
int type = cu->type;
if (cu->vfont) {
return OB_FONT;
}
if (!cu->type) {
type = OB_CURVE;
for (nu = cu->nurb.first; nu; nu = nu->next) {
if (nu->pntsv > 1) {
type = OB_SURF;
}
}
}
return type;
}
void BKE_curve_curve_dimension_update(Curve *cu)
{
ListBase *nurbs = BKE_curve_nurbs_get(cu);
Nurb *nu = nurbs->first;
if (cu->flag & CU_3D) {
for (; nu; nu = nu->next) {
nu->flag &= ~CU_2D;
}
}
else {
for (; nu; nu = nu->next) {
nu->flag |= CU_2D;
BKE_nurb_test_2d(nu);
/* since the handles are moved they need to be auto-located again */
if (nu->type == CU_BEZIER)
BKE_nurb_handles_calc(nu);
}
}
}
void BKE_curve_type_test(Object *ob)
{
ob->type = BKE_curve_type_get(ob->data);
if (ob->type == OB_CURVE)
BKE_curve_curve_dimension_update((Curve *)ob->data);
}
void BKE_curve_boundbox_calc(Curve *cu, float r_loc[3], float r_size[3])
{
BoundBox *bb;
float min[3], max[3];
float mloc[3], msize[3];
if (cu->bb == NULL) cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
bb = cu->bb;
if (!r_loc) r_loc = mloc;
if (!r_size) r_size = msize;
INIT_MINMAX(min, max);
if (!BKE_curve_minmax(cu, true, min, max)) {
min[0] = min[1] = min[2] = -1.0f;
max[0] = max[1] = max[2] = 1.0f;
}
mid_v3_v3v3(r_loc, min, max);
r_size[0] = (max[0] - min[0]) / 2.0f;
r_size[1] = (max[1] - min[1]) / 2.0f;
r_size[2] = (max[2] - min[2]) / 2.0f;
BKE_boundbox_init_from_minmax(bb, min, max);
bb->flag &= ~BOUNDBOX_DIRTY;
}
BoundBox *BKE_curve_boundbox_get(Object *ob)
{
/* This is Object-level data access, DO NOT touch to Mesh's bb, would be totally thread-unsafe. */
if (ob->runtime.bb == NULL || ob->runtime.bb->flag & BOUNDBOX_DIRTY) {
Curve *cu = ob->data;
float min[3], max[3];
INIT_MINMAX(min, max);
BKE_curve_minmax(cu, true, min, max);
if (ob->runtime.bb == NULL) {
ob->runtime.bb = MEM_mallocN(sizeof(*ob->runtime.bb), __func__);
}
BKE_boundbox_init_from_minmax(ob->runtime.bb, min, max);
ob->runtime.bb->flag &= ~BOUNDBOX_DIRTY;
}
return ob->runtime.bb;
}
void BKE_curve_texspace_calc(Curve *cu)
{
float loc[3], size[3];
int a;
BKE_curve_boundbox_calc(cu, loc, size);
if (cu->texflag & CU_AUTOSPACE) {
for (a = 0; a < 3; a++) {
if (size[a] == 0.0f) size[a] = 1.0f;
else if (size[a] > 0.0f && size[a] < 0.00001f) size[a] = 0.00001f;
else if (size[a] < 0.0f && size[a] > -0.00001f) size[a] = -0.00001f;
}
copy_v3_v3(cu->loc, loc);
copy_v3_v3(cu->size, size);
zero_v3(cu->rot);
}
}
BoundBox *BKE_curve_texspace_get(Curve *cu, float r_loc[3], float r_rot[3], float r_size[3])
{
if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_DIRTY)) {
BKE_curve_texspace_calc(cu);
}
if (r_loc) copy_v3_v3(r_loc, cu->loc);
if (r_rot) copy_v3_v3(r_rot, cu->rot);
if (r_size) copy_v3_v3(r_size, cu->size);
return cu->bb;
}
bool BKE_nurbList_index_get_co(ListBase *nurb, const int index, float r_co[3])
{
Nurb *nu;
int tot = 0;
for (nu = nurb->first; nu; nu = nu->next) {
int tot_nu;
if (nu->type == CU_BEZIER) {
tot_nu = nu->pntsu;
if (index - tot < tot_nu) {
copy_v3_v3(r_co, nu->bezt[index - tot].vec[1]);
return true;
}
}
else {
tot_nu = nu->pntsu * nu->pntsv;
if (index - tot < tot_nu) {
copy_v3_v3(r_co, nu->bp[index - tot].vec);
return true;
}
}
tot += tot_nu;
}
return false;
}
int BKE_nurbList_verts_count(ListBase *nurb)
{
Nurb *nu;
int tot = 0;
nu = nurb->first;
while (nu) {
if (nu->bezt)
tot += 3 * nu->pntsu;
else if (nu->bp)
tot += nu->pntsu * nu->pntsv;
nu = nu->next;
}
return tot;
}
int BKE_nurbList_verts_count_without_handles(ListBase *nurb)
{
Nurb *nu;
int tot = 0;
nu = nurb->first;
while (nu) {
if (nu->bezt)
tot += nu->pntsu;
else if (nu->bp)
tot += nu->pntsu * nu->pntsv;
nu = nu->next;
}
return tot;
}
/* **************** NURBS ROUTINES ******************** */
void BKE_nurb_free(Nurb *nu)
{
if (nu == NULL) return;
if (nu->bezt)
MEM_freeN(nu->bezt);
nu->bezt = NULL;
if (nu->bp)
MEM_freeN(nu->bp);
nu->bp = NULL;
if (nu->knotsu)
MEM_freeN(nu->knotsu);
nu->knotsu = NULL;
if (nu->knotsv)
MEM_freeN(nu->knotsv);
nu->knotsv = NULL;
/* if (nu->trim.first) freeNurblist(&(nu->trim)); */
MEM_freeN(nu);
}
void BKE_nurbList_free(ListBase *lb)
{
Nurb *nu, *next;
if (lb == NULL) return;
nu = lb->first;
while (nu) {
next = nu->next;
BKE_nurb_free(nu);
nu = next;
}
BLI_listbase_clear(lb);
}
Nurb *BKE_nurb_duplicate(const Nurb *nu)
{
Nurb *newnu;
int len;
newnu = (Nurb *)MEM_mallocN(sizeof(Nurb), "duplicateNurb");
if (newnu == NULL) return NULL;
memcpy(newnu, nu, sizeof(Nurb));
if (nu->bezt) {
newnu->bezt =
(BezTriple *)MEM_malloc_arrayN(nu->pntsu, sizeof(BezTriple), "duplicateNurb2");
memcpy(newnu->bezt, nu->bezt, nu->pntsu * sizeof(BezTriple));
}
else {
len = nu->pntsu * nu->pntsv;
newnu->bp =
(BPoint *)MEM_malloc_arrayN(len, sizeof(BPoint), "duplicateNurb3");
memcpy(newnu->bp, nu->bp, len * sizeof(BPoint));
newnu->knotsu = newnu->knotsv = NULL;
if (nu->knotsu) {
len = KNOTSU(nu);
if (len) {
newnu->knotsu = MEM_malloc_arrayN(len, sizeof(float), "duplicateNurb4");
memcpy(newnu->knotsu, nu->knotsu, sizeof(float) * len);
}
}
if (nu->pntsv > 1 && nu->knotsv) {
len = KNOTSV(nu);
if (len) {
newnu->knotsv = MEM_malloc_arrayN(len, sizeof(float), "duplicateNurb5");
memcpy(newnu->knotsv, nu->knotsv, sizeof(float) * len);
}
}
}
return newnu;
}
/* copy the nurb but allow for different number of points (to be copied after this) */
Nurb *BKE_nurb_copy(Nurb *src, int pntsu, int pntsv)
{
Nurb *newnu = (Nurb *)MEM_mallocN(sizeof(Nurb), "copyNurb");
memcpy(newnu, src, sizeof(Nurb));
if (pntsu == 1) SWAP(int, pntsu, pntsv);
newnu->pntsu = pntsu;
newnu->pntsv = pntsv;
/* caller can manually handle these arrays */
newnu->knotsu = NULL;
newnu->knotsv = NULL;
if (src->bezt) {
newnu->bezt = (BezTriple *)MEM_malloc_arrayN(pntsu * pntsv, sizeof(BezTriple), "copyNurb2");
}
else {
newnu->bp = (BPoint *)MEM_malloc_arrayN(pntsu * pntsv, sizeof(BPoint), "copyNurb3");
}
return newnu;
}
void BKE_nurbList_duplicate(ListBase *lb1, const ListBase *lb2)
{
Nurb *nu, *nun;
BKE_nurbList_free(lb1);
nu = lb2->first;
while (nu) {
nun = BKE_nurb_duplicate(nu);
BLI_addtail(lb1, nun);
nu = nu->next;
}
}
void BKE_nurb_test_2d(Nurb *nu)
{
BezTriple *bezt;
BPoint *bp;
int a;
if ((nu->flag & CU_2D) == 0)
return;
if (nu->type == CU_BEZIER) {
a = nu->pntsu;
bezt = nu->bezt;
while (a--) {
bezt->vec[0][2] = 0.0;
bezt->vec[1][2] = 0.0;
bezt->vec[2][2] = 0.0;
bezt++;
}
}
else {
a = nu->pntsu * nu->pntsv;
bp = nu->bp;
while (a--) {
bp->vec[2] = 0.0;
bp++;
}
}
}
/**
* if use_radius is truth, minmax will take points' radius into account,
* which will make boundbox closer to beveled curve.
*/
void BKE_nurb_minmax(Nurb *nu, bool use_radius, float min[3], float max[3])
{
BezTriple *bezt;
BPoint *bp;
int a;
float point[3];
if (nu->type == CU_BEZIER) {
a = nu->pntsu;
bezt = nu->bezt;
while (a--) {
if (use_radius) {
float radius_vector[3];
radius_vector[0] = radius_vector[1] = radius_vector[2] = bezt->radius;
add_v3_v3v3(point, bezt->vec[1], radius_vector);
minmax_v3v3_v3(min, max, point);
sub_v3_v3v3(point, bezt->vec[1], radius_vector);
minmax_v3v3_v3(min, max, point);
}
else {
minmax_v3v3_v3(min, max, bezt->vec[1]);
}
minmax_v3v3_v3(min, max, bezt->vec[0]);
minmax_v3v3_v3(min, max, bezt->vec[2]);
bezt++;
}
}
else {
a = nu->pntsu * nu->pntsv;
bp = nu->bp;
while (a--) {
if (nu->pntsv == 1 && use_radius) {
float radius_vector[3];
radius_vector[0] = radius_vector[1] = radius_vector[2] = bp->radius;
add_v3_v3v3(point, bp->vec, radius_vector);
minmax_v3v3_v3(min, max, point);
sub_v3_v3v3(point, bp->vec, radius_vector);
minmax_v3v3_v3(min, max, point);
}
else {
/* Surfaces doesn't use bevel, so no need to take radius into account. */
minmax_v3v3_v3(min, max, bp->vec);
}
bp++;
}
}
}
float BKE_nurb_calc_length(const Nurb *nu, int resolution)
{
BezTriple *bezt, *prevbezt;
BPoint *bp, *prevbp;
int a, b;
float length = 0.0f;
int resolu = resolution ? resolution : nu->resolu;
int pntsu = nu->pntsu;
float *points, *pntsit, *prevpntsit;
if (nu->type == CU_POLY) {
a = nu->pntsu - 1;
bp = nu->bp;
if (nu->flagu & CU_NURB_CYCLIC) {
++a;
prevbp = nu->bp + (nu->pntsu - 1);
}
else {
prevbp = bp;
bp++;
}
while (a--) {
length += len_v3v3(prevbp->vec, bp->vec);
prevbp = bp;
++bp;
}
}
else if (nu->type == CU_BEZIER) {
points = MEM_mallocN(sizeof(float[3]) * (resolu + 1), "getLength_bezier");
a = nu->pntsu - 1;
bezt = nu->bezt;
if (nu->flagu & CU_NURB_CYCLIC) {
++a;
prevbezt = nu->bezt + (nu->pntsu - 1);
}
else {
prevbezt = bezt;
++bezt;
}
while (a--) {
if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
length += len_v3v3(prevbezt->vec[1], bezt->vec[1]);
}
else {
for (int j = 0; j < 3; j++) {
BKE_curve_forward_diff_bezier(
prevbezt->vec[1][j], prevbezt->vec[2][j],
bezt->vec[0][j], bezt->vec[1][j],
points + j, resolu, 3 * sizeof(float));
}
prevpntsit = pntsit = points;
b = resolu;
while (b--) {
pntsit += 3;
length += len_v3v3(prevpntsit, pntsit);
prevpntsit = pntsit;
}
}
prevbezt = bezt;
++bezt;
}
MEM_freeN(points);
}
else if (nu->type == CU_NURBS) {
if (nu->pntsv == 1) {
/* important to zero for BKE_nurb_makeCurve. */
points = MEM_callocN(sizeof(float[3]) * pntsu * resolu, "getLength_nurbs");
BKE_nurb_makeCurve(
nu, points,
NULL, NULL, NULL,
resolu, sizeof(float[3]));
if (nu->flagu & CU_NURB_CYCLIC) {
b = pntsu * resolu + 1;
prevpntsit = points + 3 * (pntsu * resolu - 1);
pntsit = points;
}
else {
b = (pntsu - 1) * resolu;
prevpntsit = points;
pntsit = points + 3;
}
while (--b) {
length += len_v3v3(prevpntsit, pntsit);
prevpntsit = pntsit;
pntsit += 3;
}
MEM_freeN(points);
}
}
return length;
}
/* be sure to call makeknots after this */
void BKE_nurb_points_add(Nurb *nu, int number)
{
BPoint *bp;
int i;
nu->bp = MEM_recallocN(nu->bp, (nu->pntsu + number) * sizeof(BPoint));
for (i = 0, bp = &nu->bp[nu->pntsu]; i < number; i++, bp++) {
bp->radius = 1.0f;
}
nu->pntsu += number;
}
void BKE_nurb_bezierPoints_add(Nurb *nu, int number)
{
BezTriple *bezt;
int i;
nu->bezt = MEM_recallocN(nu->bezt, (nu->pntsu + number) * sizeof(BezTriple));
for (i = 0, bezt = &nu->bezt[nu->pntsu]; i < number; i++, bezt++) {
bezt->radius = 1.0f;
}
nu->pntsu += number;
}
int BKE_nurb_index_from_uv(
Nurb *nu,
int u, int v)
{
const int totu = nu->pntsu;
const int totv = nu->pntsv;
if (nu->flagu & CU_NURB_CYCLIC) {
u = mod_i(u, totu);
}
else if (u < 0 || u >= totu) {
return -1;
}
if (nu->flagv & CU_NURB_CYCLIC) {
v = mod_i(v, totv);
}
else if (v < 0 || v >= totv) {
return -1;
}
return (v * totu) + u;
}
void BKE_nurb_index_to_uv(
Nurb *nu, int index,
int *r_u, int *r_v)
{
const int totu = nu->pntsu;
const int totv = nu->pntsv;
BLI_assert(index >= 0 && index < (nu->pntsu * nu->pntsv));
*r_u = (index % totu);
*r_v = (index / totu) % totv;
}
BezTriple *BKE_nurb_bezt_get_next(Nurb *nu, BezTriple *bezt)
{
BezTriple *bezt_next;
BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
if (bezt == &nu->bezt[nu->pntsu - 1]) {
if (nu->flagu & CU_NURB_CYCLIC) {
bezt_next = nu->bezt;
}
else {
bezt_next = NULL;
}
}
else {
bezt_next = bezt + 1;
}
return bezt_next;
}
BPoint *BKE_nurb_bpoint_get_next(Nurb *nu, BPoint *bp)
{
BPoint *bp_next;
BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
if (bp == &nu->bp[nu->pntsu - 1]) {
if (nu->flagu & CU_NURB_CYCLIC) {
bp_next = nu->bp;
}
else {
bp_next = NULL;
}
}
else {
bp_next = bp + 1;
}
return bp_next;
}
BezTriple *BKE_nurb_bezt_get_prev(Nurb *nu, BezTriple *bezt)
{
BezTriple *bezt_prev;
BLI_assert(ARRAY_HAS_ITEM(bezt, nu->bezt, nu->pntsu));
BLI_assert(nu->pntsv <= 1);
if (bezt == nu->bezt) {
if (nu->flagu & CU_NURB_CYCLIC) {
bezt_prev = &nu->bezt[nu->pntsu - 1];
}
else {
bezt_prev = NULL;
}
}
else {
bezt_prev = bezt - 1;
}
return bezt_prev;
}
BPoint *BKE_nurb_bpoint_get_prev(Nurb *nu, BPoint *bp)
{
BPoint *bp_prev;
BLI_assert(ARRAY_HAS_ITEM(bp, nu->bp, nu->pntsu));
BLI_assert(nu->pntsv == 1);
if (bp == nu->bp) {
if (nu->flagu & CU_NURB_CYCLIC) {
bp_prev = &nu->bp[nu->pntsu - 1];
}
else {
bp_prev = NULL;
}
}
else {
bp_prev = bp - 1;
}
return bp_prev;
}
void BKE_nurb_bezt_calc_normal(struct Nurb *UNUSED(nu), BezTriple *bezt, float r_normal[3])
{
/* calculate the axis matrix from the spline */
float dir_prev[3], dir_next[3];
sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
normalize_v3(dir_prev);
normalize_v3(dir_next);
add_v3_v3v3(r_normal, dir_prev, dir_next);
normalize_v3(r_normal);
}
void BKE_nurb_bezt_calc_plane(struct Nurb *nu, BezTriple *bezt, float r_plane[3])
{
float dir_prev[3], dir_next[3];
sub_v3_v3v3(dir_prev, bezt->vec[0], bezt->vec[1]);
sub_v3_v3v3(dir_next, bezt->vec[1], bezt->vec[2]);
normalize_v3(dir_prev);
normalize_v3(dir_next);
cross_v3_v3v3(r_plane, dir_prev, dir_next);
if (normalize_v3(r_plane) < FLT_EPSILON) {
BezTriple *bezt_prev = BKE_nurb_bezt_get_prev(nu, bezt);
BezTriple *bezt_next = BKE_nurb_bezt_get_next(nu, bezt);
if (bezt_prev) {
sub_v3_v3v3(dir_prev, bezt_prev->vec[1], bezt->vec[1]);
normalize_v3(dir_prev);
}
if (bezt_next) {
sub_v3_v3v3(dir_next, bezt->vec[1], bezt_next->vec[1]);
normalize_v3(dir_next);
}
cross_v3_v3v3(r_plane, dir_prev, dir_next);
}
/* matches with bones more closely */
{
float dir_mid[3], tvec[3];
add_v3_v3v3(dir_mid, dir_prev, dir_next);
cross_v3_v3v3(tvec, r_plane, dir_mid);
copy_v3_v3(r_plane, tvec);
}
normalize_v3(r_plane);
}
void BKE_nurb_bpoint_calc_normal(struct Nurb *nu, BPoint *bp, float r_normal[3])
{
BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp);
BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp);
zero_v3(r_normal);
if (bp_prev) {
float dir_prev[3];
sub_v3_v3v3(dir_prev, bp_prev->vec, bp->vec);
normalize_v3(dir_prev);
add_v3_v3(r_normal, dir_prev);
}
if (bp_next) {
float dir_next[3];
sub_v3_v3v3(dir_next, bp->vec, bp_next->vec);
normalize_v3(dir_next);
add_v3_v3(r_normal, dir_next);
}
normalize_v3(r_normal);
}
void BKE_nurb_bpoint_calc_plane(struct Nurb *nu, BPoint *bp, float r_plane[3])
{
BPoint *bp_prev = BKE_nurb_bpoint_get_prev(nu, bp);
BPoint *bp_next = BKE_nurb_bpoint_get_next(nu, bp);
float dir_prev[3] = {0.0f}, dir_next[3] = {0.0f};
if (bp_prev) {
sub_v3_v3v3(dir_prev, bp_prev->vec, bp->vec);
normalize_v3(dir_prev);
}
if (bp_next) {
sub_v3_v3v3(dir_next, bp->vec, bp_next->vec);
normalize_v3(dir_next);
}
cross_v3_v3v3(r_plane, dir_prev, dir_next);
/* matches with bones more closely */
{
float dir_mid[3], tvec[3];
add_v3_v3v3(dir_mid, dir_prev, dir_next);
cross_v3_v3v3(tvec, r_plane, dir_mid);
copy_v3_v3(r_plane, tvec);
}
normalize_v3(r_plane);
}
/* ~~~~~~~~~~~~~~~~~~~~Non Uniform Rational B Spline calculations ~~~~~~~~~~~ */
static void calcknots(float *knots, const int pnts, const short order, const short flag)
{
/* knots: number of pnts NOT corrected for cyclic */
const int pnts_order = pnts + order;
float k;
int a;
switch (flag & (CU_NURB_ENDPOINT | CU_NURB_BEZIER)) {
case CU_NURB_ENDPOINT:
k = 0.0;
for (a = 1; a <= pnts_order; a++) {
knots[a - 1] = k;
if (a >= order && a <= pnts)
k += 1.0f;
}
break;
case CU_NURB_BEZIER:
/* Warning, the order MUST be 2 or 4,
* if this is not enforced, the displist will be corrupt */
if (order == 4) {
k = 0.34;
for (a = 0; a < pnts_order; a++) {
knots[a] = floorf(k);
k += (1.0f / 3.0f);
}
}
else if (order == 3) {
k = 0.6f;
for (a = 0; a < pnts_order; a++) {
if (a >= order && a <= pnts)
k += 0.5f;
knots[a] = floorf(k);
}
}
else {
CLOG_ERROR(&LOG, "bez nurb curve order is not 3 or 4, should never happen");
}
break;
default:
for (a = 0; a < pnts_order; a++) {
knots[a] = (float)a;
}
break;
}
}
static void makecyclicknots(float *knots, int pnts, short order)
/* pnts, order: number of pnts NOT corrected for cyclic */
{
int a, b, order2, c;
if (knots == NULL)
return;
order2 = order - 1;
/* do first long rows (order -1), remove identical knots at endpoints */
if (order > 2) {
b = pnts + order2;
for (a = 1; a < order2; a++) {
if (knots[b] != knots[b - a])
break;
}
if (a == order2)
knots[pnts + order - 2] += 1.0f;
}
b = order;
c = pnts + order + order2;
for (a = pnts + order2; a < c; a++) {
knots[a] = knots[a - 1] + (knots[b] - knots[b - 1]);
b--;
}
}
static void makeknots(Nurb *nu, short uv)
{
if (nu->type == CU_NURBS) {
if (uv == 1) {
if (nu->knotsu)
MEM_freeN(nu->knotsu);
if (BKE_nurb_check_valid_u(nu)) {
nu->knotsu = MEM_calloc_arrayN(KNOTSU(nu) + 1, sizeof(float), "makeknots");
if (nu->flagu & CU_NURB_CYCLIC) {
calcknots(nu->knotsu, nu->pntsu, nu->orderu, 0); /* cyclic should be uniform */
makecyclicknots(nu->knotsu, nu->pntsu, nu->orderu);
}
else {
calcknots(nu->knotsu, nu->pntsu, nu->orderu, nu->flagu);
}
}
else
nu->knotsu = NULL;
}
else if (uv == 2) {
if (nu->knotsv)
MEM_freeN(nu->knotsv);
if (BKE_nurb_check_valid_v(nu)) {
nu->knotsv = MEM_calloc_arrayN(KNOTSV(nu) + 1, sizeof(float), "makeknots");
if (nu->flagv & CU_NURB_CYCLIC) {
calcknots(nu->knotsv, nu->pntsv, nu->orderv, 0); /* cyclic should be uniform */
makecyclicknots(nu->knotsv, nu->pntsv, nu->orderv);
}
else {
calcknots(nu->knotsv, nu->pntsv, nu->orderv, nu->flagv);
}
}
else {
nu->knotsv = NULL;
}
}
}
}
void BKE_nurb_knot_calc_u(Nurb *nu)
{
makeknots(nu, 1);
}
void BKE_nurb_knot_calc_v(Nurb *nu)
{
makeknots(nu, 2);
}
static void basisNurb(float t, short order, int pnts, float *knots, float *basis, int *start, int *end)
{
float d, e;
int i, i1 = 0, i2 = 0, j, orderpluspnts, opp2, o2;
orderpluspnts = order + pnts;
opp2 = orderpluspnts - 1;
/* this is for float inaccuracy */
if (t < knots[0])
t = knots[0];
else if (t > knots[opp2])
t = knots[opp2];
/* this part is order '1' */
o2 = order + 1;
for (i = 0; i < opp2; i++) {
if (knots[i] != knots[i + 1] && t >= knots[i] && t <= knots[i + 1]) {
basis[i] = 1.0;
i1 = i - o2;
if (i1 < 0) i1 = 0;
i2 = i;
i++;
while (i < opp2) {
basis[i] = 0.0;
i++;
}
break;
}
else
basis[i] = 0.0;
}
basis[i] = 0.0;
/* this is order 2, 3, ... */
for (j = 2; j <= order; j++) {
if (i2 + j >= orderpluspnts) i2 = opp2 - j;
for (i = i1; i <= i2; i++) {
if (basis[i] != 0.0f)
d = ((t - knots[i]) * basis[i]) / (knots[i + j - 1] - knots[i]);
else
d = 0.0f;
if (basis[i + 1] != 0.0f)
e = ((knots[i + j] - t) * basis[i + 1]) / (knots[i + j] - knots[i + 1]);
else
e = 0.0;
basis[i] = d + e;
}
}
*start = 1000;
*end = 0;
for (i = i1; i <= i2; i++) {
if (basis[i] > 0.0f) {
*end = i;
if (*start == 1000) *start = i;
}
}
}
/**
* \param coord_array: has to be (3 * 4 * resolu * resolv) in size, and zero-ed.
*/
void BKE_nurb_makeFaces(const Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv)
{
BPoint *bp;
float *basisu, *basis, *basisv, *sum, *fp, *in;
float u, v, ustart, uend, ustep, vstart, vend, vstep, sumdiv;
int i, j, iofs, jofs, cycl, len, curu, curv;
int istart, iend, jsta, jen, *jstart, *jend, ratcomp;
int totu = nu->pntsu * resolu, totv = nu->pntsv * resolv;
if (nu->knotsu == NULL || nu->knotsv == NULL)
return;
if (nu->orderu > nu->pntsu)
return;
if (nu->orderv > nu->pntsv)
return;
if (coord_array == NULL)
return;
/* allocate and initialize */
len = totu * totv;
if (len == 0)
return;
sum = (float *)MEM_calloc_arrayN(len, sizeof(float), "makeNurbfaces1");
bp = nu->bp;
i = nu->pntsu * nu->pntsv;
ratcomp = 0;
while (i--) {
if (bp->vec[3] != 1.0f) {
ratcomp = 1;
break;
}
bp++;
}
fp = nu->knotsu;
ustart = fp[nu->orderu - 1];
if (nu->flagu & CU_NURB_CYCLIC)
uend = fp[nu->pntsu + nu->orderu - 1];
else
uend = fp[nu->pntsu];
ustep = (uend - ustart) / ((nu->flagu & CU_NURB_CYCLIC) ? totu : totu - 1);
basisu = (float *)MEM_malloc_arrayN(KNOTSU(nu), sizeof(float), "makeNurbfaces3");
fp = nu->knotsv;
vstart = fp[nu->orderv - 1];
if (nu->flagv & CU_NURB_CYCLIC)
vend = fp[nu->pntsv + nu->orderv - 1];
else
vend = fp[nu->pntsv];
vstep = (vend - vstart) / ((nu->flagv & CU_NURB_CYCLIC) ? totv : totv - 1);
len = KNOTSV(nu);
basisv = (float *)MEM_malloc_arrayN(len * totv, sizeof(float), "makeNurbfaces3");
jstart = (int *)MEM_malloc_arrayN(totv, sizeof(float), "makeNurbfaces4");
jend = (int *)MEM_malloc_arrayN(totv, sizeof(float), "makeNurbfaces5");
/* precalculation of basisv and jstart, jend */
if (nu->flagv & CU_NURB_CYCLIC)
cycl = nu->orderv - 1;
else cycl = 0;
v = vstart;
basis = basisv;
curv = totv;
while (curv--) {
basisNurb(v, nu->orderv, nu->pntsv + cycl, nu->knotsv, basis, jstart + curv, jend + curv);
basis += KNOTSV(nu);
v += vstep;
}
if (nu->flagu & CU_NURB_CYCLIC)
cycl = nu->orderu - 1;
else
cycl = 0;
in = coord_array;
u = ustart;
curu = totu;
while (curu--) {
basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
basis = basisv;
curv = totv;
while (curv--) {
jsta = jstart[curv];
jen = jend[curv];
/* calculate sum */
sumdiv = 0.0;
fp = sum;
for (j = jsta; j <= jen; j++) {
if (j >= nu->pntsv)
jofs = (j - nu->pntsv);
else
jofs = j;
bp = nu->bp + nu->pntsu * jofs + istart - 1;
for (i = istart; i <= iend; i++, fp++) {
if (i >= nu->pntsu) {
iofs = i - nu->pntsu;
bp = nu->bp + nu->pntsu * jofs + iofs;
}
else
bp++;
if (ratcomp) {
*fp = basisu[i] * basis[j] * bp->vec[3];
sumdiv += *fp;
}
else
*fp = basisu[i] * basis[j];
}
}
if (ratcomp) {
fp = sum;
for (j = jsta; j <= jen; j++) {
for (i = istart; i <= iend; i++, fp++) {
*fp /= sumdiv;
}
}
}
zero_v3(in);
/* one! (1.0) real point now */
fp = sum;
for (j = jsta; j <= jen; j++) {
if (j >= nu->pntsv)
jofs = (j - nu->pntsv);
else
jofs = j;
bp = nu->bp + nu->pntsu * jofs + istart - 1;
for (i = istart; i <= iend; i++, fp++) {
if (i >= nu->pntsu) {
iofs = i - nu->pntsu;
bp = nu->bp + nu->pntsu * jofs + iofs;
}
else
bp++;
if (*fp != 0.0f) {
madd_v3_v3fl(in, bp->vec, *fp);
}
}
}
in += 3;
basis += KNOTSV(nu);
}
u += ustep;
if (rowstride != 0) {
in = (float *)(((unsigned char *)in) + (rowstride - 3 * totv * sizeof(*in)));
}
}
/* free */
MEM_freeN(sum);
MEM_freeN(basisu);
MEM_freeN(basisv);
MEM_freeN(jstart);
MEM_freeN(jend);
}
/**
* \param coord_array: Has to be 3 * 4 * pntsu * resolu in size and zero-ed
* \param tilt_array: set when non-NULL
* \param radius_array: set when non-NULL
*/
void BKE_nurb_makeCurve(
const Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array,
int resolu, int stride)
{
const float eps = 1e-6f;
BPoint *bp;
float u, ustart, uend, ustep, sumdiv;
float *basisu, *sum, *fp;
float *coord_fp = coord_array, *tilt_fp = tilt_array, *radius_fp = radius_array, *weight_fp = weight_array;
int i, len, istart, iend, cycl;
if (nu->knotsu == NULL)
return;
if (nu->orderu > nu->pntsu)
return;
if (coord_array == NULL)
return;
/* allocate and initialize */
len = nu->pntsu;
if (len == 0)
return;
sum = (float *)MEM_calloc_arrayN(len, sizeof(float), "makeNurbcurve1");
resolu = (resolu * SEGMENTSU(nu));
if (resolu == 0) {
MEM_freeN(sum);
return;
}
fp = nu->knotsu;
ustart = fp[nu->orderu - 1];
if (nu->flagu & CU_NURB_CYCLIC)
uend = fp[nu->pntsu + nu->orderu - 1];
else
uend = fp[nu->pntsu];
ustep = (uend - ustart) / (resolu - ((nu->flagu & CU_NURB_CYCLIC) ? 0 : 1));
basisu = (float *)MEM_malloc_arrayN(KNOTSU(nu), sizeof(float), "makeNurbcurve3");
if (nu->flagu & CU_NURB_CYCLIC)
cycl = nu->orderu - 1;
else
cycl = 0;
u = ustart;
while (resolu--) {
basisNurb(u, nu->orderu, nu->pntsu + cycl, nu->knotsu, basisu, &istart, &iend);
/* calc sum */
sumdiv = 0.0;
fp = sum;
bp = nu->bp + istart - 1;
for (i = istart; i <= iend; i++, fp++) {
if (i >= nu->pntsu)
bp = nu->bp + (i - nu->pntsu);
else
bp++;
*fp = basisu[i] * bp->vec[3];
sumdiv += *fp;
}
if ((sumdiv != 0.0f) && (sumdiv < 1.0f - eps || sumdiv > 1.0f + eps)) {
/* is normalizing needed? */
fp = sum;
for (i = istart; i <= iend; i++, fp++) {
*fp /= sumdiv;
}
}
zero_v3(coord_fp);
/* one! (1.0) real point */
fp = sum;
bp = nu->bp + istart - 1;
for (i = istart; i <= iend; i++, fp++) {
if (i >= nu->pntsu)
bp = nu->bp + (i - nu->pntsu);
else
bp++;
if (*fp != 0.0f) {
madd_v3_v3fl(coord_fp, bp->vec, *fp);
if (tilt_fp)
(*tilt_fp) += (*fp) * bp->tilt;
if (radius_fp)
(*radius_fp) += (*fp) * bp->radius;
if (weight_fp)
(*weight_fp) += (*fp) * bp->weight;
}
}
coord_fp = POINTER_OFFSET(coord_fp, stride);
if (tilt_fp)
tilt_fp = POINTER_OFFSET(tilt_fp, stride);
if (radius_fp)
radius_fp = POINTER_OFFSET(radius_fp, stride);
if (weight_fp)
weight_fp = POINTER_OFFSET(weight_fp, stride);
u += ustep;
}
/* free */
MEM_freeN(sum);
MEM_freeN(basisu);
}
/**
* Calculate the length for arrays filled in by #BKE_curve_calc_coords_axis.
*/
unsigned int BKE_curve_calc_coords_axis_len(
const unsigned int bezt_array_len, const unsigned int resolu,
const bool is_cyclic, const bool use_cyclic_duplicate_endpoint)
{
const unsigned int segments = bezt_array_len - (is_cyclic ? 0 : 1);
const unsigned int points_len = (segments * resolu) + (is_cyclic ? (use_cyclic_duplicate_endpoint) : 1);
return points_len;
}
/**
* Calculate an array for the entire curve (cyclic or non-cyclic).
* \note Call for each axis.
*
* \param use_cyclic_duplicate_endpoint: Duplicate values at the beginning & end of the array.
*/
void BKE_curve_calc_coords_axis(
const BezTriple *bezt_array, const unsigned int bezt_array_len, const unsigned int resolu,
const bool is_cyclic, const bool use_cyclic_duplicate_endpoint,
/* array params */
const unsigned int axis, const unsigned int stride,
float *r_points)
{
const unsigned int points_len = BKE_curve_calc_coords_axis_len(
bezt_array_len, resolu, is_cyclic, use_cyclic_duplicate_endpoint);
float *r_points_offset = r_points;
const unsigned int resolu_stride = resolu * stride;
const unsigned int bezt_array_last = bezt_array_len - 1;
for (unsigned int i = 0; i < bezt_array_last; i++) {
const BezTriple *bezt_curr = &bezt_array[i];
const BezTriple *bezt_next = &bezt_array[i + 1];
BKE_curve_forward_diff_bezier(
bezt_curr->vec[1][axis], bezt_curr->vec[2][axis],
bezt_next->vec[0][axis], bezt_next->vec[1][axis],
r_points_offset, (int)resolu, stride);
r_points_offset = POINTER_OFFSET(r_points_offset, resolu_stride);
}
if (is_cyclic) {
const BezTriple *bezt_curr = &bezt_array[bezt_array_last];
const BezTriple *bezt_next = &bezt_array[0];
BKE_curve_forward_diff_bezier(
bezt_curr->vec[1][axis], bezt_curr->vec[2][axis],
bezt_next->vec[0][axis], bezt_next->vec[1][axis],
r_points_offset, (int)resolu, stride);
r_points_offset = POINTER_OFFSET(r_points_offset, resolu_stride);
if (use_cyclic_duplicate_endpoint) {
*r_points_offset = *r_points;
r_points_offset = POINTER_OFFSET(r_points_offset, stride);
}
}
else {
float *r_points_last = POINTER_OFFSET(r_points, bezt_array_last * resolu_stride);
*r_points_last = bezt_array[bezt_array_last].vec[1][axis];
r_points_offset = POINTER_OFFSET(r_points_offset, stride);
}
BLI_assert(POINTER_OFFSET(r_points, points_len * stride) == r_points_offset);
UNUSED_VARS_NDEBUG(points_len);
}
/* forward differencing method for bezier curve */
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
{
float rt0, rt1, rt2, rt3, f;
int a;
f = (float)it;
rt0 = q0;
rt1 = 3.0f * (q1 - q0) / f;
f *= f;
rt2 = 3.0f * (q0 - 2.0f * q1 + q2) / f;
f *= it;
rt3 = (q3 - q0 + 3.0f * (q1 - q2)) / f;
q0 = rt0;
q1 = rt1 + rt2 + rt3;
q2 = 2 * rt2 + 6 * rt3;
q3 = 6 * rt3;
for (a = 0; a <= it; a++) {
*p = q0;
p = POINTER_OFFSET(p, stride);
q0 += q1;
q1 += q2;
q2 += q3;
}
}
/* forward differencing method for first derivative of cubic bezier curve */
void BKE_curve_forward_diff_tangent_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
{
float rt0, rt1, rt2, f;
int a;
f = 1.0f / (float)it;
rt0 = 3.0f * (q1 - q0);
rt1 = f * (3.0f * (q3 - q0) + 9.0f * (q1 - q2));
rt2 = 6.0f * (q0 + q2) - 12.0f * q1;
q0 = rt0;
q1 = f * (rt1 + rt2);
q2 = 2.0f * f * rt1;
for (a = 0; a <= it; a++) {
*p = q0;
p = POINTER_OFFSET(p, stride);
q0 += q1;
q1 += q2;
}
}
static void forward_diff_bezier_cotangent(const float p0[3], const float p1[3], const float p2[3], const float p3[3],
float p[3], int it, int stride)
{
/* note that these are not perpendicular to the curve
* they need to be rotated for this,
*
* This could also be optimized like BKE_curve_forward_diff_bezier */
int a;
for (a = 0; a <= it; a++) {
float t = (float)a / (float)it;
int i;
for (i = 0; i < 3; i++) {
p[i] = (-6.0f * t + 6.0f) * p0[i] +
( 18.0f * t - 12.0f) * p1[i] +
(-18.0f * t + 6.0f) * p2[i] +
( 6.0f * t) * p3[i];
}
normalize_v3(p);
p = POINTER_OFFSET(p, stride);
}
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
float *BKE_curve_surf_make_orco(Object *ob)
{
/* Note: this function is used in convertblender only atm, so
* suppose nonzero curve's render resolution should always be used */
Curve *cu = ob->data;
Nurb *nu;
int a, b, tot = 0;
int sizeu, sizev;
int resolu, resolv;
float *fp, *coord_array;
/* first calculate the size of the datablock */
nu = cu->nurb.first;
while (nu) {
/* as we want to avoid the seam in a cyclic nurbs
* texture wrapping, reserve extra orco data space to save these extra needed
* vertex based UV coordinates for the meridian vertices.
* Vertices on the 0/2pi boundary are not duplicated inside the displist but later in
* the renderface/vert construction.
*
* See also convertblender.c: init_render_surf()
*/
resolu = cu->resolu_ren ? cu->resolu_ren : nu->resolu;
resolv = cu->resolv_ren ? cu->resolv_ren : nu->resolv;
sizeu = nu->pntsu * resolu;
sizev = nu->pntsv * resolv;
if (nu->flagu & CU_NURB_CYCLIC) sizeu++;
if (nu->flagv & CU_NURB_CYCLIC) sizev++;
if (nu->pntsv > 1) tot += sizeu * sizev;
nu = nu->next;
}
/* makeNurbfaces wants zeros */
fp = coord_array = MEM_calloc_arrayN(tot, 3 * sizeof(float), "make_orco");
nu = cu->nurb.first;
while (nu) {
resolu = cu->resolu_ren ? cu->resolu_ren : nu->resolu;
resolv = cu->resolv_ren ? cu->resolv_ren : nu->resolv;
if (nu->pntsv > 1) {
sizeu = nu->pntsu * resolu;
sizev = nu->pntsv * resolv;
if (nu->flagu & CU_NURB_CYCLIC)
sizeu++;
if (nu->flagv & CU_NURB_CYCLIC)
sizev++;
if (cu->flag & CU_UV_ORCO) {
for (b = 0; b < sizeu; b++) {
for (a = 0; a < sizev; a++) {
if (sizev < 2)
fp[0] = 0.0f;
else
fp[0] = -1.0f + 2.0f * ((float)a) / (sizev - 1);
if (sizeu < 2)
fp[1] = 0.0f;
else
fp[1] = -1.0f + 2.0f * ((float)b) / (sizeu - 1);
fp[2] = 0.0;
fp += 3;
}
}
}
else {
int size = (nu->pntsu * resolu) * (nu->pntsv * resolv) * 3 * sizeof(float);
float *_tdata = MEM_mallocN(size, "temp data");
float *tdata = _tdata;
BKE_nurb_makeFaces(nu, tdata, 0, resolu, resolv);
for (b = 0; b < sizeu; b++) {
int use_b = b;
if (b == sizeu - 1 && (nu->flagu & CU_NURB_CYCLIC))
use_b = false;
for (a = 0; a < sizev; a++) {
int use_a = a;
if (a == sizev - 1 && (nu->flagv & CU_NURB_CYCLIC))
use_a = false;
tdata = _tdata + 3 * (use_b * (nu->pntsv * resolv) + use_a);
fp[0] = (tdata[0] - cu->loc[0]) / cu->size[0];
fp[1] = (tdata[1] - cu->loc[1]) / cu->size[1];
fp[2] = (tdata[2] - cu->loc[2]) / cu->size[2];
fp += 3;
}
}
MEM_freeN(_tdata);
}
}
nu = nu->next;
}
return coord_array;
}
/* NOTE: This routine is tied to the order of vertex
* built by displist and as passed to the renderer.
*/
float *BKE_curve_make_orco(Depsgraph *depsgraph, Scene *scene, Object *ob, int *r_numVerts)
{
Curve *cu = ob->data;
DispList *dl;
int u, v, numVerts;
float *fp, *coord_array;
ListBase disp = {NULL, NULL};
BKE_displist_make_curveTypes_forOrco(depsgraph, scene, ob, &disp, NULL);
numVerts = 0;
for (dl = disp.first; dl; dl = dl->next) {
if (dl->type == DL_INDEX3) {
numVerts += dl->nr;
}
else if (dl->type == DL_SURF) {
/* convertblender.c uses the Surface code for creating renderfaces when cyclic U only
* (closed circle beveling)
*/
if (dl->flag & DL_CYCL_U) {
if (dl->flag & DL_CYCL_V)
numVerts += (dl->parts + 1) * (dl->nr + 1);
else
numVerts += dl->parts * (dl->nr + 1);
}
else if (dl->flag & DL_CYCL_V) {
numVerts += (dl->parts + 1) * dl->nr;
}
else
numVerts += dl->parts * dl->nr;
}
}
if (r_numVerts)
*r_numVerts = numVerts;
fp = coord_array = MEM_malloc_arrayN(numVerts, 3 * sizeof(float), "cu_orco");
for (dl = disp.first; dl; dl = dl->next) {
if (dl->type == DL_INDEX3) {
for (u = 0; u < dl->nr; u++, fp += 3) {
if (cu->flag & CU_UV_ORCO) {
fp[0] = 2.0f * u / (dl->nr - 1) - 1.0f;
fp[1] = 0.0;
fp[2] = 0.0;
}
else {
copy_v3_v3(fp, &dl->verts[u * 3]);
fp[0] = (fp[0] - cu->loc[0]) / cu->size[0];
fp[1] = (fp[1] - cu->loc[1]) / cu->size[1];
fp[2] = (fp[2] - cu->loc[2]) / cu->size[2];
}
}
}
else if (dl->type == DL_SURF) {
int sizeu = dl->nr, sizev = dl->parts;
/* exception as handled in convertblender.c too */
if (dl->flag & DL_CYCL_U) {
sizeu++;
if (dl->flag & DL_CYCL_V)
sizev++;
}
else if (dl->flag & DL_CYCL_V) {
sizev++;
}
for (u = 0; u < sizev; u++) {
for (v = 0; v < sizeu; v++, fp += 3) {
if (cu->flag & CU_UV_ORCO) {
fp[0] = 2.0f * u / (sizev - 1) - 1.0f;
fp[1] = 2.0f * v / (sizeu - 1) - 1.0f;
fp[2] = 0.0;
}
else {
const float *vert;
int realv = v % dl->nr;
int realu = u % dl->parts;
vert = dl->verts + 3 * (dl->nr * realu + realv);
copy_v3_v3(fp, vert);
fp[0] = (fp[0] - cu->loc[0]) / cu->size[0];
fp[1] = (fp[1] - cu->loc[1]) / cu->size[1];
fp[2] = (fp[2] - cu->loc[2]) / cu->size[2];
}
}
}
}
}
BKE_displist_free(&disp);
return coord_array;
}
/* ***************** BEVEL ****************** */
void BKE_curve_bevel_make(
Depsgraph *depsgraph, Scene *scene, Object *ob, ListBase *disp,
const bool for_render, const bool use_render_resolution,
LinkNode *ob_cyclic_list)
{
DispList *dl, *dlnew;
Curve *bevcu, *cu;
float *fp, facx, facy, angle, dangle;
int nr, a;
cu = ob->data;
BLI_listbase_clear(disp);
/* if a font object is being edited, then do nothing */
// XXX if ( ob == obedit && ob->type == OB_FONT ) return;
if (cu->bevobj) {
if (cu->bevobj->type != OB_CURVE)
return;
bevcu = cu->bevobj->data;
if (bevcu->ext1 == 0.0f && bevcu->ext2 == 0.0f) {
ListBase bevdisp = {NULL, NULL};
facx = cu->bevobj->scale[0];
facy = cu->bevobj->scale[1];
if (for_render) {
if (BLI_linklist_index(ob_cyclic_list, cu->bevobj) == -1) {
BKE_displist_make_curveTypes_forRender(
depsgraph, scene, cu->bevobj, &bevdisp, NULL, false, use_render_resolution,
&(LinkNode){ .link = ob, .next = ob_cyclic_list, });
dl = bevdisp.first;
}
else {
dl = NULL;
}
}
else if (cu->bevobj->runtime.curve_cache) {
dl = cu->bevobj->runtime.curve_cache->disp.first;
}
else {
BLI_assert(cu->bevobj->runtime.curve_cache != NULL);
dl = NULL;
}
while (dl) {
if (ELEM(dl->type, DL_POLY, DL_SEGM)) {
dlnew = MEM_mallocN(sizeof(DispList), "makebevelcurve1");
*dlnew = *dl;
dlnew->verts = MEM_malloc_arrayN(dl->parts * dl->nr, 3 * sizeof(float), "makebevelcurve1");
memcpy(dlnew->verts, dl->verts, 3 * sizeof(float) * dl->parts * dl->nr);
if (dlnew->type == DL_SEGM)
dlnew->flag |= (DL_FRONT_CURVE | DL_BACK_CURVE);
BLI_addtail(disp, dlnew);
fp = dlnew->verts;
nr = dlnew->parts * dlnew->nr;
while (nr--) {
fp[2] = fp[1] * facy;
fp[1] = -fp[0] * facx;
fp[0] = 0.0;
fp += 3;
}
}
dl = dl->next;
}
BKE_displist_free(&bevdisp);
}
}
else if (cu->ext1 == 0.0f && cu->ext2 == 0.0f) {
/* pass */
}
else if (cu->ext2 == 0.0f) {
dl = MEM_callocN(sizeof(DispList), "makebevelcurve2");
dl->verts = MEM_malloc_arrayN(2, sizeof(float[3]), "makebevelcurve2");
BLI_addtail(disp, dl);
dl->type = DL_SEGM;
dl->parts = 1;
dl->flag = DL_FRONT_CURVE | DL_BACK_CURVE;
dl->nr = 2;
fp = dl->verts;
fp[0] = fp[1] = 0.0;
fp[2] = -cu->ext1;
fp[3] = fp[4] = 0.0;
fp[5] = cu->ext1;
}
else if ((cu->flag & (CU_FRONT | CU_BACK)) == 0 && cu->ext1 == 0.0f) { /* we make a full round bevel in that case */
nr = 4 + 2 * cu->bevresol;
dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
dl->verts = MEM_malloc_arrayN(nr, sizeof(float[3]), "makebevelcurve p1");
BLI_addtail(disp, dl);
dl->type = DL_POLY;
dl->parts = 1;
dl->flag = DL_BACK_CURVE;
dl->nr = nr;
/* a circle */
fp = dl->verts;
dangle = (2.0f * (float)M_PI / (nr));
angle = -(nr - 1) * dangle;
for (a = 0; a < nr; a++) {
fp[0] = 0.0;
fp[1] = (cosf(angle) * (cu->ext2));
fp[2] = (sinf(angle) * (cu->ext2)) - cu->ext1;
angle += dangle;
fp += 3;
}
}
else {
short dnr;
/* bevel now in three parts, for proper vertex normals */
/* part 1, back */
if ((cu->flag & CU_BACK) || !(cu->flag & CU_FRONT)) {
dnr = nr = 2 + cu->bevresol;
if ((cu->flag & (CU_FRONT | CU_BACK)) == 0) {
nr = 3 + 2 * cu->bevresol;
}
dl = MEM_callocN(sizeof(DispList), "makebevelcurve p1");
dl->verts = MEM_malloc_arrayN(nr, sizeof(float[3]), "makebevelcurve p1");
BLI_addtail(disp, dl);
dl->type = DL_SEGM;
dl->parts = 1;
dl->flag = DL_BACK_CURVE;
dl->nr = nr;
/* half a circle */
fp = dl->verts;
dangle = ((float)M_PI_2 / (dnr - 1));
angle = -(nr - 1) * dangle;
for (a = 0; a < nr; a++) {
fp[0] = 0.0;
fp[1] = (float)(cosf(angle) * (cu->ext2));
fp[2] = (float)(sinf(angle) * (cu->ext2)) - cu->ext1;
angle += dangle;
fp += 3;
}
}
/* part 2, sidefaces */
if (cu->ext1 != 0.0f) {
nr = 2;
dl = MEM_callocN(sizeof(DispList), "makebevelcurve p2");
dl->verts = MEM_malloc_arrayN(nr, sizeof(float[3]), "makebevelcurve p2");
BLI_addtail(disp, dl);
dl->type = DL_SEGM;
dl->parts = 1;
dl->nr = nr;
fp = dl->verts;
fp[1] = cu->ext2;
fp[2] = -cu->ext1;
fp[4] = cu->ext2;
fp[5] = cu->ext1;
if ((cu->flag & (CU_FRONT | CU_BACK)) == 0) {
dl = MEM_dupallocN(dl);
dl->verts = MEM_dupallocN(dl->verts);
BLI_addtail(disp, dl);
fp = dl->verts;
fp[1] = -fp[1];
fp[2] = -fp[2];
fp[4] = -fp[4];
fp[5] = -fp[5];
}
}
/* part 3, front */
if ((cu->flag & CU_FRONT) || !(cu->flag & CU_BACK)) {
dnr = nr = 2 + cu->bevresol;
if ((cu->flag & (CU_FRONT | CU_BACK)) == 0) {
nr = 3 + 2 * cu->bevresol;
}
dl = MEM_callocN(sizeof(DispList), "makebevelcurve p3");
dl->verts = MEM_malloc_arrayN(nr, sizeof(float[3]), "makebevelcurve p3");
BLI_addtail(disp, dl);
dl->type = DL_SEGM;
dl->flag = DL_FRONT_CURVE;
dl->parts = 1;
dl->nr = nr;
/* half a circle */
fp = dl->verts;
angle = 0.0;
dangle = ((float)M_PI_2 / (dnr - 1));
for (a = 0; a < nr; a++) {
fp[0] = 0.0;
fp[1] = (float)(cosf(angle) * (cu->ext2));
fp[2] = (float)(sinf(angle) * (cu->ext2)) + cu->ext1;
angle += dangle;
fp += 3;
}
}
}
}
static int cu_isectLL(const float v1[3], const float v2[3], const float v3[3], const float v4[3],
short cox, short coy,
float *lambda, float *mu, float vec[3])
{
/* return:
* -1: collinear
* 0: no intersection of segments
* 1: exact intersection of segments
* 2: cross-intersection of segments
*/
float deler;
deler = (v1[cox] - v2[cox]) * (v3[coy] - v4[coy]) - (v3[cox] - v4[cox]) * (v1[coy] - v2[coy]);
if (deler == 0.0f)
return -1;
*lambda = (v1[coy] - v3[coy]) * (v3[cox] - v4[cox]) - (v1[cox] - v3[cox]) * (v3[coy] - v4[coy]);
*lambda = -(*lambda / deler);
deler = v3[coy] - v4[coy];
if (deler == 0) {
deler = v3[cox] - v4[cox];
*mu = -(*lambda * (v2[cox] - v1[cox]) + v1[cox] - v3[cox]) / deler;
}
else {
*mu = -(*lambda * (v2[coy] - v1[coy]) + v1[coy] - v3[coy]) / deler;
}
vec[cox] = *lambda * (v2[cox] - v1[cox]) + v1[cox];
vec[coy] = *lambda * (v2[coy] - v1[coy]) + v1[coy];
if (*lambda >= 0.0f && *lambda <= 1.0f && *mu >= 0.0f && *mu <= 1.0f) {
if (*lambda == 0.0f || *lambda == 1.0f || *mu == 0.0f || *mu == 1.0f)
return 1;
return 2;
}
return 0;
}
static bool bevelinside(BevList *bl1, BevList *bl2)
{
/* is bl2 INSIDE bl1 ? with left-right method and "lambda's" */
/* returns '1' if correct hole */
BevPoint *bevp, *prevbevp;
float min, max, vec[3], hvec1[3], hvec2[3], lab, mu;
int nr, links = 0, rechts = 0, mode;
/* take first vertex of possible hole */
bevp = bl2->bevpoints;
hvec1[0] = bevp->vec[0];
hvec1[1] = bevp->vec[1];
hvec1[2] = 0.0;
copy_v3_v3(hvec2, hvec1);
hvec2[0] += 1000;
/* test it with all edges of potential surrounding poly */
/* count number of transitions left-right */
bevp = bl1->bevpoints;
nr = bl1->nr;
prevbevp = bevp + (nr - 1);
while (nr--) {
min = prevbevp->vec[1];
max = bevp->vec[1];
if (max < min) {
min = max;
max = prevbevp->vec[1];
}
if (min != max) {
if (min <= hvec1[1] && max >= hvec1[1]) {
/* there's a transition, calc intersection point */
mode = cu_isectLL(prevbevp->vec, bevp->vec, hvec1, hvec2, 0, 1, &lab, &mu, vec);
/* if lab==0.0 or lab==1.0 then the edge intersects exactly a transition
* only allow for one situation: we choose lab= 1.0
*/
if (mode >= 0 && lab != 0.0f) {
if (vec[0] < hvec1[0]) links++;
else rechts++;
}
}
}
prevbevp = bevp;
bevp++;
}
return (links & 1) && (rechts & 1);
}
struct BevelSort {
BevList *bl;
float left;
int dir;
};
static int vergxcobev(const void *a1, const void *a2)
{
const struct BevelSort *x1 = a1, *x2 = a2;
if (x1->left > x2->left)
return 1;
else if (x1->left < x2->left)
return -1;
return 0;
}
/* this function cannot be replaced with atan2, but why? */
static void calc_bevel_sin_cos(float x1, float y1, float x2, float y2,
float *r_sina, float *r_cosa)
{
float t01, t02, x3, y3;
t01 = sqrtf(x1 * x1 + y1 * y1);
t02 = sqrtf(x2 * x2 + y2 * y2);
if (t01 == 0.0f)
t01 = 1.0f;
if (t02 == 0.0f)
t02 = 1.0f;
x1 /= t01;
y1 /= t01;
x2 /= t02;
y2 /= t02;
t02 = x1 * x2 + y1 * y2;
if (fabsf(t02) >= 1.0f)
t02 = M_PI_2;
else
t02 = (saacos(t02)) / 2.0f;
t02 = sinf(t02);
if (t02 == 0.0f)
t02 = 1.0f;
x3 = x1 - x2;
y3 = y1 - y2;
if (x3 == 0 && y3 == 0) {
x3 = y1;
y3 = -x1;
}
else {
t01 = sqrtf(x3 * x3 + y3 * y3);
x3 /= t01;
y3 /= t01;
}
*r_sina = -y3 / t02;
*r_cosa = x3 / t02;
}
static void tilt_bezpart(
BezTriple *prevbezt, BezTriple *bezt, Nurb *nu, float *tilt_array, float *radius_array,
float *weight_array, int resolu, int stride)
{
BezTriple *pprev, *next, *last;
float fac, dfac, t[4];
int a;
if (tilt_array == NULL && radius_array == NULL)
return;
last = nu->bezt + (nu->pntsu - 1);
/* returns a point */
if (prevbezt == nu->bezt) {
if (nu->flagu & CU_NURB_CYCLIC)
pprev = last;
else
pprev = prevbezt;
}
else
pprev = prevbezt - 1;
/* next point */
if (bezt == last) {
if (nu->flagu & CU_NURB_CYCLIC)
next = nu->bezt;
else
next = bezt;
}
else
next = bezt + 1;
fac = 0.0;
dfac = 1.0f / (float)resolu;
for (a = 0; a < resolu; a++, fac += dfac) {
if (tilt_array) {
if (nu->tilt_interp == KEY_CU_EASE) { /* May as well support for tilt also 2.47 ease interp */
*tilt_array = prevbezt->tilt +
(bezt->tilt - prevbezt->tilt) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
}
else {
key_curve_position_weights(fac, t, nu->tilt_interp);
*tilt_array = t[0] * pprev->tilt + t[1] * prevbezt->tilt + t[2] * bezt->tilt + t[3] * next->tilt;
}
tilt_array = POINTER_OFFSET(tilt_array, stride);
}
if (radius_array) {
if (nu->radius_interp == KEY_CU_EASE) {
/* Support 2.47 ease interp
* Note! - this only takes the 2 points into account,
* giving much more localized results to changes in radius, sometimes you want that */
*radius_array = prevbezt->radius +
(bezt->radius - prevbezt->radius) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
}
else {
/* reuse interpolation from tilt if we can */
if (tilt_array == NULL || nu->tilt_interp != nu->radius_interp) {
key_curve_position_weights(fac, t, nu->radius_interp);
}
*radius_array =
t[0] * pprev->radius + t[1] * prevbezt->radius +
t[2] * bezt->radius + t[3] * next->radius;
}
radius_array = POINTER_OFFSET(radius_array, stride);
}
if (weight_array) {
/* basic interpolation for now, could copy tilt interp too */
*weight_array =
prevbezt->weight +
(bezt->weight - prevbezt->weight) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
weight_array = POINTER_OFFSET(weight_array, stride);
}
}
}
/* make_bevel_list_3D_* funcs, at a minimum these must
* fill in the bezp->quat and bezp->dir values */
/* utility for make_bevel_list_3D_* funcs */
static void bevel_list_calc_bisect(BevList *bl)
{
BevPoint *bevp2, *bevp1, *bevp0;
int nr;
bool is_cyclic = bl->poly != -1;
if (is_cyclic) {
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
nr = bl->nr;
}
else {
/* If spline is not cyclic, direction of first and
* last bevel points matches direction of CV handle.
*
* This is getting calculated earlier when we know
* CV's handles and here we might simply skip evaluation
* of direction for this guys.
*/
bevp0 = bl->bevpoints;
bevp1 = bevp0 + 1;
bevp2 = bevp1 + 1;
nr = bl->nr - 2;
}
while (nr--) {
/* totally simple */
bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
bevp0 = bevp1;
bevp1 = bevp2;
bevp2++;
}
}
static void bevel_list_flip_tangents(BevList *bl)
{
BevPoint *bevp2, *bevp1, *bevp0;
int nr;
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
nr = bl->nr;
while (nr--) {
if (angle_normalized_v3v3(bevp0->tan, bevp1->tan) > DEG2RADF(90.0f))
negate_v3(bevp1->tan);
bevp0 = bevp1;
bevp1 = bevp2;
bevp2++;
}
}
/* apply user tilt */
static void bevel_list_apply_tilt(BevList *bl)
{
BevPoint *bevp2, *bevp1;
int nr;
float q[4];
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
nr = bl->nr;
while (nr--) {
axis_angle_to_quat(q, bevp1->dir, bevp1->tilt);
mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
normalize_qt(bevp1->quat);
bevp1 = bevp2;
bevp2++;
}
}
/* smooth quats, this function should be optimized, it can get slow with many iterations. */
static void bevel_list_smooth(BevList *bl, int smooth_iter)
{
BevPoint *bevp2, *bevp1, *bevp0;
int nr;
float q[4];
float bevp0_quat[4];
int a;
for (a = 0; a < smooth_iter; a++) {
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
nr = bl->nr;
if (bl->poly == -1) { /* check its not cyclic */
/* skip the first point */
/* bevp0 = bevp1; */
bevp1 = bevp2;
bevp2++;
nr--;
bevp0 = bevp1;
bevp1 = bevp2;
bevp2++;
nr--;
}
copy_qt_qt(bevp0_quat, bevp0->quat);
while (nr--) {
/* interpolate quats */
float zaxis[3] = {0, 0, 1}, cross[3], q2[4];
interp_qt_qtqt(q, bevp0_quat, bevp2->quat, 0.5);
normalize_qt(q);
mul_qt_v3(q, zaxis);
cross_v3_v3v3(cross, zaxis, bevp1->dir);
axis_angle_to_quat(q2, cross, angle_normalized_v3v3(zaxis, bevp1->dir));
normalize_qt(q2);
copy_qt_qt(bevp0_quat, bevp1->quat);
mul_qt_qtqt(q, q2, q);
interp_qt_qtqt(bevp1->quat, bevp1->quat, q, 0.5);
normalize_qt(bevp1->quat);
/* bevp0 = bevp1; */ /* UNUSED */
bevp1 = bevp2;
bevp2++;
}
}
}
static void make_bevel_list_3D_zup(BevList *bl)
{
BevPoint *bevp = bl->bevpoints;
int nr = bl->nr;
bevel_list_calc_bisect(bl);
while (nr--) {
vec_to_quat(bevp->quat, bevp->dir, 5, 1);
bevp++;
}
}
static void minimum_twist_between_two_points(BevPoint *current_point, BevPoint *previous_point)
{
float angle = angle_normalized_v3v3(previous_point->dir, current_point->dir);
float q[4];
if (angle > 0.0f) { /* otherwise we can keep as is */
float cross_tmp[3];
cross_v3_v3v3(cross_tmp, previous_point->dir, current_point->dir);
axis_angle_to_quat(q, cross_tmp, angle);
mul_qt_qtqt(current_point->quat, q, previous_point->quat);
}
else {
copy_qt_qt(current_point->quat, previous_point->quat);
}
}
static void make_bevel_list_3D_minimum_twist(BevList *bl)
{
BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
int nr;
float q[4];
bevel_list_calc_bisect(bl);
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
nr = bl->nr;
while (nr--) {
if (nr + 4 > bl->nr) { /* first time and second time, otherwise first point adjusts last */
vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
}
else {
minimum_twist_between_two_points(bevp1, bevp0);
}
bevp0 = bevp1;
bevp1 = bevp2;
bevp2++;
}
if (bl->poly != -1) { /* check for cyclic */
/* Need to correct for the start/end points not matching
* do this by calculating the tilt angle difference, then apply
* the rotation gradually over the entire curve
*
* note that the split is between last and second last, rather than first/last as youd expect.
*
* real order is like this
* 0,1,2,3,4 --> 1,2,3,4,0
*
* this is why we compare last with second last
* */
float vec_1[3] = {0, 1, 0}, vec_2[3] = {0, 1, 0}, angle, ang_fac, cross_tmp[3];
BevPoint *bevp_first;
BevPoint *bevp_last;
bevp_first = bl->bevpoints;
bevp_first += bl->nr - 1;
bevp_last = bevp_first;
bevp_last--;
/* quats and vec's are normalized, should not need to re-normalize */
mul_qt_v3(bevp_first->quat, vec_1);
mul_qt_v3(bevp_last->quat, vec_2);
normalize_v3(vec_1);
normalize_v3(vec_2);
/* align the vector, can avoid this and it looks 98% OK but
* better to align the angle quat roll's before comparing */
{
cross_v3_v3v3(cross_tmp, bevp_last->dir, bevp_first->dir);
angle = angle_normalized_v3v3(bevp_first->dir, bevp_last->dir);
axis_angle_to_quat(q, cross_tmp, angle);
mul_qt_v3(q, vec_2);
}
angle = angle_normalized_v3v3(vec_1, vec_2);
/* flip rotation if needs be */
cross_v3_v3v3(cross_tmp, vec_1, vec_2);
normalize_v3(cross_tmp);
if (angle_normalized_v3v3(bevp_first->dir, cross_tmp) < DEG2RADF(90.0f))
angle = -angle;
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
nr = bl->nr;
while (nr--) {
ang_fac = angle * (1.0f - ((float)nr / bl->nr)); /* also works */
axis_angle_to_quat(q, bevp1->dir, ang_fac);
mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
bevp0 = bevp1;
bevp1 = bevp2;
bevp2++;
}
}
else {
/* Need to correct quat for the first/last point,
* this is so because previously it was only calculated
* using it's own direction, which might not correspond
* the twist of neighbor point.
*/
bevp1 = bl->bevpoints;
bevp0 = bevp1 + 1;
minimum_twist_between_two_points(bevp1, bevp0);
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
minimum_twist_between_two_points(bevp1, bevp0);
}
}
static void make_bevel_list_3D_tangent(BevList *bl)
{
BevPoint *bevp2, *bevp1, *bevp0; /* standard for all make_bevel_list_3D_* funcs */
int nr;
float bevp0_tan[3];
bevel_list_calc_bisect(bl);
bevel_list_flip_tangents(bl);
/* correct the tangents */
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
nr = bl->nr;
while (nr--) {
float cross_tmp[3];
cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
cross_v3_v3v3(bevp1->tan, cross_tmp, bevp1->dir);
normalize_v3(bevp1->tan);
bevp0 = bevp1;
bevp1 = bevp2;
bevp2++;
}
/* now for the real twist calc */
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
copy_v3_v3(bevp0_tan, bevp0->tan);
nr = bl->nr;
while (nr--) {
/* make perpendicular, modify tan in place, is ok */
float cross_tmp[3];
float zero[3] = {0, 0, 0};
cross_v3_v3v3(cross_tmp, bevp1->tan, bevp1->dir);
normalize_v3(cross_tmp);
tri_to_quat(bevp1->quat, zero, cross_tmp, bevp1->tan); /* XXX - could be faster */
/* bevp0 = bevp1; */ /* UNUSED */
bevp1 = bevp2;
bevp2++;
}
}
static void make_bevel_list_3D(BevList *bl, int smooth_iter, int twist_mode)
{
switch (twist_mode) {
case CU_TWIST_TANGENT:
make_bevel_list_3D_tangent(bl);
break;
case CU_TWIST_MINIMUM:
make_bevel_list_3D_minimum_twist(bl);
break;
default: /* CU_TWIST_Z_UP default, pre 2.49c */
make_bevel_list_3D_zup(bl);
break;
}
if (smooth_iter)
bevel_list_smooth(bl, smooth_iter);
bevel_list_apply_tilt(bl);
}
/* only for 2 points */
static void make_bevel_list_segment_3D(BevList *bl)
{
float q[4];
BevPoint *bevp2 = bl->bevpoints;
BevPoint *bevp1 = bevp2 + 1;
/* simple quat/dir */
sub_v3_v3v3(bevp1->dir, bevp1->vec, bevp2->vec);
normalize_v3(bevp1->dir);
vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
axis_angle_to_quat(q, bevp1->dir, bevp1->tilt);
mul_qt_qtqt(bevp1->quat, q, bevp1->quat);
normalize_qt(bevp1->quat);
copy_v3_v3(bevp2->dir, bevp1->dir);
copy_qt_qt(bevp2->quat, bevp1->quat);
}
/* only for 2 points */
static void make_bevel_list_segment_2D(BevList *bl)
{
BevPoint *bevp2 = bl->bevpoints;
BevPoint *bevp1 = bevp2 + 1;
const float x1 = bevp1->vec[0] - bevp2->vec[0];
const float y1 = bevp1->vec[1] - bevp2->vec[1];
calc_bevel_sin_cos(x1, y1, -x1, -y1, &(bevp1->sina), &(bevp1->cosa));
bevp2->sina = bevp1->sina;
bevp2->cosa = bevp1->cosa;
/* fill in dir & quat */
make_bevel_list_segment_3D(bl);
}
static void make_bevel_list_2D(BevList *bl)
{
/* note: bevp->dir and bevp->quat are not needed for beveling but are
* used when making a path from a 2D curve, therefore they need to be set - Campbell */
BevPoint *bevp0, *bevp1, *bevp2;
int nr;
if (bl->poly != -1) {
bevp2 = bl->bevpoints;
bevp1 = bevp2 + (bl->nr - 1);
bevp0 = bevp1 - 1;
nr = bl->nr;
}
else {
bevp0 = bl->bevpoints;
bevp1 = bevp0 + 1;
bevp2 = bevp1 + 1;
nr = bl->nr - 2;
}
while (nr--) {
const float x1 = bevp1->vec[0] - bevp0->vec[0];
const float x2 = bevp1->vec[0] - bevp2->vec[0];
const float y1 = bevp1->vec[1] - bevp0->vec[1];
const float y2 = bevp1->vec[1] - bevp2->vec[1];
calc_bevel_sin_cos(x1, y1, x2, y2, &(bevp1->sina), &(bevp1->cosa));
/* from: make_bevel_list_3D_zup, could call but avoid a second loop.
* no need for tricky tilt calculation as with 3D curves */
bisect_v3_v3v3v3(bevp1->dir, bevp0->vec, bevp1->vec, bevp2->vec);
vec_to_quat(bevp1->quat, bevp1->dir, 5, 1);
/* done with inline make_bevel_list_3D_zup */
bevp0 = bevp1;
bevp1 = bevp2;
bevp2++;
}
/* correct non-cyclic cases */
if (bl->poly == -1) {
BevPoint *bevp;
float angle;
/* first */
bevp = bl->bevpoints;
angle = atan2f(bevp->dir[0], bevp->dir[1]) - (float)M_PI_2;
bevp->sina = sinf(angle);
bevp->cosa = cosf(angle);
vec_to_quat(bevp->quat, bevp->dir, 5, 1);
/* last */
bevp = bl->bevpoints;
bevp += (bl->nr - 1);
angle = atan2f(bevp->dir[0], bevp->dir[1]) - (float)M_PI_2;
bevp->sina = sinf(angle);
bevp->cosa = cosf(angle);
vec_to_quat(bevp->quat, bevp->dir, 5, 1);
}
}
static void bevlist_firstlast_direction_calc_from_bpoint(Nurb *nu, BevList *bl)
{
if (nu->pntsu > 1) {
BPoint *first_bp = nu->bp, *last_bp = nu->bp + (nu->pntsu - 1);
BevPoint *first_bevp, *last_bevp;
first_bevp = bl->bevpoints;
last_bevp = first_bevp + (bl->nr - 1);
sub_v3_v3v3(first_bevp->dir, (first_bp + 1)->vec, first_bp->vec);
normalize_v3(first_bevp->dir);
sub_v3_v3v3(last_bevp->dir, last_bp->vec, (last_bp - 1)->vec);
normalize_v3(last_bevp->dir);
}
}
void BKE_curve_bevelList_free(ListBase *bev)
{
BevList *bl, *blnext;
for (bl = bev->first; bl != NULL; bl = blnext) {
blnext = bl->next;
if (bl->seglen != NULL) {
MEM_freeN(bl->seglen);
}
if (bl->segbevcount != NULL) {
MEM_freeN(bl->segbevcount);
}
if (bl->bevpoints != NULL) {
MEM_freeN(bl->bevpoints);
}
MEM_freeN(bl);
}
BLI_listbase_clear(bev);
}
void BKE_curve_bevelList_make(Object *ob, ListBase *nurbs, bool for_render)
{
/*
* - convert all curves to polys, with indication of resol and flags for double-vertices
* - possibly; do a smart vertice removal (in case Nurb)
* - separate in individual blocks with BoundBox
* - AutoHole detection
*/
/* this function needs an object, because of tflag and upflag */
Curve *cu = ob->data;
Nurb *nu;
BezTriple *bezt, *prevbezt;
BPoint *bp;
BevList *bl, *blnew, *blnext;
BevPoint *bevp2, *bevp1 = NULL, *bevp0;
const float treshold = 0.00001f;
float min, inp;
float *seglen = NULL;
struct BevelSort *sortdata, *sd, *sd1;
int a, b, nr, poly, resolu = 0, len = 0, segcount;
int *segbevcount;
bool do_tilt, do_radius, do_weight;
bool is_editmode = false;
ListBase *bev;
/* segbevcount alsp requires seglen. */
const bool need_seglen =
ELEM(cu->bevfac1_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE) ||
ELEM(cu->bevfac2_mapping, CU_BEVFAC_MAP_SEGMENT, CU_BEVFAC_MAP_SPLINE);
bev = &ob->runtime.curve_cache->bev;
/* do we need to calculate the radius for each point? */
/* do_radius = (cu->bevobj || cu->taperobj || (cu->flag & CU_FRONT) || (cu->flag & CU_BACK)) ? 0 : 1; */
/* STEP 1: MAKE POLYS */
BKE_curve_bevelList_free(&ob->runtime.curve_cache->bev);
nu = nurbs->first;
if (cu->editnurb && ob->type != OB_FONT) {
is_editmode = 1;
}
for (; nu; nu = nu->next) {
if (nu->hide && is_editmode)
continue;
/* check if we will calculate tilt data */
do_tilt = CU_DO_TILT(cu, nu);
do_radius = CU_DO_RADIUS(cu, nu); /* normal display uses the radius, better just to calculate them */
do_weight = true;
/* check we are a single point? also check we are not a surface and that the orderu is sane,
* enforced in the UI but can go wrong possibly */
if (!BKE_nurb_check_valid_u(nu)) {
bl = MEM_callocN(sizeof(BevList), "makeBevelList1");
bl->bevpoints = MEM_calloc_arrayN(1, sizeof(BevPoint), "makeBevelPoints1");
BLI_addtail(bev, bl);
bl->nr = 0;
bl->charidx = nu->charidx;
}
else {
BevPoint *bevp;
if (for_render && cu->resolu_ren != 0)
resolu = cu->resolu_ren;
else
resolu = nu->resolu;
segcount = SEGMENTSU(nu);
if (nu->type == CU_POLY) {
len = nu->pntsu;
bl = MEM_callocN(sizeof(BevList), "makeBevelList2");
bl->bevpoints = MEM_calloc_arrayN(len, sizeof(BevPoint), "makeBevelPoints2");
if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
bl->seglen = MEM_malloc_arrayN(segcount, sizeof(float), "makeBevelList2_seglen");
bl->segbevcount = MEM_malloc_arrayN(segcount, sizeof(int), "makeBevelList2_segbevcount");
}
BLI_addtail(bev, bl);
bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
bl->nr = len;
bl->dupe_nr = 0;
bl->charidx = nu->charidx;
bevp = bl->bevpoints;
bevp->offset = 0;
bp = nu->bp;
seglen = bl->seglen;
segbevcount = bl->segbevcount;
while (len--) {
copy_v3_v3(bevp->vec, bp->vec);
bevp->tilt = bp->tilt;
bevp->radius = bp->radius;
bevp->weight = bp->weight;
bevp->split_tag = true;
bp++;
if (seglen != NULL && len != 0) {
*seglen = len_v3v3(bevp->vec, bp->vec);
bevp++;
bevp->offset = *seglen;
if (*seglen > treshold) *segbevcount = 1;
else *segbevcount = 0;
seglen++;
segbevcount++;
}
else {
bevp++;
}
}
if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
}
}
else if (nu->type == CU_BEZIER) {
/* in case last point is not cyclic */
len = segcount * resolu + 1;
bl = MEM_callocN(sizeof(BevList), "makeBevelBPoints");
bl->bevpoints = MEM_calloc_arrayN(len, sizeof(BevPoint), "makeBevelBPointsPoints");
if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
bl->seglen = MEM_malloc_arrayN(segcount, sizeof(float), "makeBevelBPoints_seglen");
bl->segbevcount = MEM_malloc_arrayN(segcount, sizeof(int), "makeBevelBPoints_segbevcount");
}
BLI_addtail(bev, bl);
bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
bl->charidx = nu->charidx;
bevp = bl->bevpoints;
seglen = bl->seglen;
segbevcount = bl->segbevcount;
bevp->offset = 0;
if (seglen != NULL) {
*seglen = 0;
*segbevcount = 0;
}
a = nu->pntsu - 1;
bezt = nu->bezt;
if (nu->flagu & CU_NURB_CYCLIC) {
a++;
prevbezt = nu->bezt + (nu->pntsu - 1);
}
else {
prevbezt = bezt;
bezt++;
}
sub_v3_v3v3(bevp->dir, prevbezt->vec[2], prevbezt->vec[1]);
normalize_v3(bevp->dir);
BLI_assert(segcount >= a);
while (a--) {
if (prevbezt->h2 == HD_VECT && bezt->h1 == HD_VECT) {
copy_v3_v3(bevp->vec, prevbezt->vec[1]);
bevp->tilt = prevbezt->tilt;
bevp->radius = prevbezt->radius;
bevp->weight = prevbezt->weight;
bevp->split_tag = true;
bevp->dupe_tag = false;
bevp++;
bl->nr++;
bl->dupe_nr = 1;
if (seglen != NULL) {
*seglen = len_v3v3(prevbezt->vec[1], bezt->vec[1]);
bevp->offset = *seglen;
seglen++;
/* match segbevcount to the cleaned up bevel lists (see STEP 2) */
if (bevp->offset > treshold) *segbevcount = 1;
segbevcount++;
}
}
else {
/* always do all three, to prevent data hanging around */
int j;
/* BevPoint must stay aligned to 4 so sizeof(BevPoint)/sizeof(float) works */
for (j = 0; j < 3; j++) {
BKE_curve_forward_diff_bezier(prevbezt->vec[1][j], prevbezt->vec[2][j],
bezt->vec[0][j], bezt->vec[1][j],
&(bevp->vec[j]), resolu, sizeof(BevPoint));
}
/* if both arrays are NULL do nothiong */
tilt_bezpart(prevbezt, bezt, nu,
do_tilt ? &bevp->tilt : NULL,
do_radius ? &bevp->radius : NULL,
do_weight ? &bevp->weight : NULL,
resolu, sizeof(BevPoint));
if (cu->twist_mode == CU_TWIST_TANGENT) {
forward_diff_bezier_cotangent(prevbezt->vec[1], prevbezt->vec[2],
bezt->vec[0], bezt->vec[1],
bevp->tan, resolu, sizeof(BevPoint));
}
/* indicate with handlecodes double points */
if (prevbezt->h1 == prevbezt->h2) {
if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
bevp->split_tag = true;
}
else {
if (prevbezt->h1 == 0 || prevbezt->h1 == HD_VECT)
bevp->split_tag = true;
else if (prevbezt->h2 == 0 || prevbezt->h2 == HD_VECT)
bevp->split_tag = true;
}
/* seglen */
if (seglen != NULL) {
*seglen = 0;
*segbevcount = 0;
for (j = 0; j < resolu; j++) {
bevp0 = bevp;
bevp++;
bevp->offset = len_v3v3(bevp0->vec, bevp->vec);
/* match seglen and segbevcount to the cleaned up bevel lists (see STEP 2) */
if (bevp->offset > treshold) {
*seglen += bevp->offset;
*segbevcount += 1;
}
}
seglen++;
segbevcount++;
}
else {
bevp += resolu;
}
bl->nr += resolu;
}
prevbezt = bezt;
bezt++;
}
if ((nu->flagu & CU_NURB_CYCLIC) == 0) { /* not cyclic: endpoint */
copy_v3_v3(bevp->vec, prevbezt->vec[1]);
bevp->tilt = prevbezt->tilt;
bevp->radius = prevbezt->radius;
bevp->weight = prevbezt->weight;
sub_v3_v3v3(bevp->dir, prevbezt->vec[1], prevbezt->vec[0]);
normalize_v3(bevp->dir);
bl->nr++;
}
}
else if (nu->type == CU_NURBS) {
if (nu->pntsv == 1) {
len = (resolu * segcount);
bl = MEM_callocN(sizeof(BevList), "makeBevelList3");
bl->bevpoints = MEM_calloc_arrayN(len, sizeof(BevPoint), "makeBevelPoints3");
if (need_seglen && (nu->flagu & CU_NURB_CYCLIC) == 0) {
bl->seglen = MEM_malloc_arrayN(segcount, sizeof(float), "makeBevelList3_seglen");
bl->segbevcount = MEM_malloc_arrayN(segcount, sizeof(int), "makeBevelList3_segbevcount");
}
BLI_addtail(bev, bl);
bl->nr = len;
bl->dupe_nr = 0;
bl->poly = (nu->flagu & CU_NURB_CYCLIC) ? 0 : -1;
bl->charidx = nu->charidx;
bevp = bl->bevpoints;
seglen = bl->seglen;
segbevcount = bl->segbevcount;
BKE_nurb_makeCurve(nu, &bevp->vec[0],
do_tilt ? &bevp->tilt : NULL,
do_radius ? &bevp->radius : NULL,
do_weight ? &bevp->weight : NULL,
resolu, sizeof(BevPoint));
/* match seglen and segbevcount to the cleaned up bevel lists (see STEP 2) */
if (seglen != NULL) {
nr = segcount;
bevp0 = bevp;
bevp++;
while (nr) {
int j;
*seglen = 0;
*segbevcount = 0;
/* We keep last bevel segment zero-length. */
for (j = 0; j < ((nr == 1) ? (resolu - 1) : resolu); j++) {
bevp->offset = len_v3v3(bevp0->vec, bevp->vec);
if (bevp->offset > treshold) {
*seglen += bevp->offset;
*segbevcount += 1;
}
bevp0 = bevp;
bevp++;
}
seglen++;
segbevcount++;
nr--;
}
}
if ((nu->flagu & CU_NURB_CYCLIC) == 0) {
bevlist_firstlast_direction_calc_from_bpoint(nu, bl);
}
}
}
}
}
/* STEP 2: DOUBLE POINTS AND AUTOMATIC RESOLUTION, REDUCE DATABLOCKS */
bl = bev->first;
while (bl) {
if (bl->nr) { /* null bevel items come from single points */
bool is_cyclic = bl->poly != -1;
nr = bl->nr;
if (is_cyclic) {
bevp1 = bl->bevpoints;
bevp0 = bevp1 + (nr - 1);
}
else {
bevp0 = bl->bevpoints;
bevp0->offset = 0;
bevp1 = bevp0 + 1;
}
nr--;
while (nr--) {
if (seglen != NULL) {
if (fabsf(bevp1->offset) < treshold) {
bevp0->dupe_tag = true;
bl->dupe_nr++;
}
}
else {
if (fabsf(bevp0->vec[0] - bevp1->vec[0]) < 0.00001f) {
if (fabsf(bevp0->vec[1] - bevp1->vec[1]) < 0.00001f) {
if (fabsf(bevp0->vec[2] - bevp1->vec[2]) < 0.00001f) {
bevp0->dupe_tag = true;
bl->dupe_nr++;
}
}
}
}
bevp0 = bevp1;
bevp1++;
}
}
bl = bl->next;
}
bl = bev->first;
while (bl) {
blnext = bl->next;
if (bl->nr && bl->dupe_nr) {
nr = bl->nr - bl->dupe_nr + 1; /* +1 because vectorbezier sets flag too */
blnew = MEM_callocN(sizeof(BevList), "makeBevelList4");
memcpy(blnew, bl, sizeof(BevList));
blnew->bevpoints = MEM_calloc_arrayN(nr, sizeof(BevPoint), "makeBevelPoints4");
if (!blnew->bevpoints) {
MEM_freeN(blnew);
break;
}
blnew->segbevcount = bl->segbevcount;
blnew->seglen = bl->seglen;
blnew->nr = 0;
BLI_remlink(bev, bl);
BLI_insertlinkbefore(bev, blnext, blnew); /* to make sure bevlijst is tuned with nurblist */
bevp0 = bl->bevpoints;
bevp1 = blnew->bevpoints;
nr = bl->nr;
while (nr--) {
if (bevp0->dupe_tag == 0) {
memcpy(bevp1, bevp0, sizeof(BevPoint));
bevp1++;
blnew->nr++;
}
bevp0++;
}
if (bl->bevpoints != NULL) {
MEM_freeN(bl->bevpoints);
}
MEM_freeN(bl);
blnew->dupe_nr = 0;
}
bl = blnext;
}
/* STEP 3: POLYS COUNT AND AUTOHOLE */
bl = bev->first;
poly = 0;
while (bl) {
if (bl->nr && bl->poly >= 0) {
poly++;
bl->poly = poly;
bl->hole = 0;
}
bl = bl->next;
}
/* find extreme left points, also test (turning) direction */
if (poly > 0) {
sd = sortdata = MEM_malloc_arrayN(poly, sizeof(struct BevelSort), "makeBevelList5");
bl = bev->first;
while (bl) {
if (bl->poly > 0) {
BevPoint *bevp;
min = 300000.0;
bevp = bl->bevpoints;
nr = bl->nr;
while (nr--) {
if (min > bevp->vec[0]) {
min = bevp->vec[0];
bevp1 = bevp;
}
bevp++;
}
sd->bl = bl;
sd->left = min;
bevp = bl->bevpoints;
if (bevp1 == bevp)
bevp0 = bevp + (bl->nr - 1);
else
bevp0 = bevp1 - 1;
bevp = bevp + (bl->nr - 1);
if (bevp1 == bevp)
bevp2 = bl->bevpoints;
else
bevp2 = bevp1 + 1;
inp = ((bevp1->vec[0] - bevp0->vec[0]) * (bevp0->vec[1] - bevp2->vec[1]) +
(bevp0->vec[1] - bevp1->vec[1]) * (bevp0->vec[0] - bevp2->vec[0]));
if (inp > 0.0f)
sd->dir = 1;
else
sd->dir = 0;
sd++;
}
bl = bl->next;
}
qsort(sortdata, poly, sizeof(struct BevelSort), vergxcobev);
sd = sortdata + 1;
for (a = 1; a < poly; a++, sd++) {
bl = sd->bl; /* is bl a hole? */
sd1 = sortdata + (a - 1);
for (b = a - 1; b >= 0; b--, sd1--) { /* all polys to the left */
if (sd1->bl->charidx == bl->charidx) { /* for text, only check matching char */
if (bevelinside(sd1->bl, bl)) {
bl->hole = 1 - sd1->bl->hole;
break;
}
}
}
}
/* turning direction */
if ((cu->flag & CU_3D) == 0) {
sd = sortdata;
for (a = 0; a < poly; a++, sd++) {
if (sd->bl->hole == sd->dir) {
bl = sd->bl;
bevp1 = bl->bevpoints;
bevp2 = bevp1 + (bl->nr - 1);
nr = bl->nr / 2;
while (nr--) {
SWAP(BevPoint, *bevp1, *bevp2);
bevp1++;
bevp2--;
}
}
}
}
MEM_freeN(sortdata);
}
/* STEP 4: 2D-COSINES or 3D ORIENTATION */
if ((cu->flag & CU_3D) == 0) {
/* 2D Curves */
for (bl = bev->first; bl; bl = bl->next) {
if (bl->nr < 2) {
BevPoint *bevp = bl->bevpoints;
unit_qt(bevp->quat);
}
else if (bl->nr == 2) { /* 2 pnt, treat separate */
make_bevel_list_segment_2D(bl);
}
else {
make_bevel_list_2D(bl);
}
}
}
else {
/* 3D Curves */
for (bl = bev->first; bl; bl = bl->next) {
if (bl->nr < 2) {
BevPoint *bevp = bl->bevpoints;
unit_qt(bevp->quat);
}
else if (bl->nr == 2) { /* 2 pnt, treat separate */
make_bevel_list_segment_3D(bl);
}
else {
make_bevel_list_3D(bl, (int)(resolu * cu->twist_smooth), cu->twist_mode);
}
}
}
}
/* ****************** HANDLES ************** */
static void calchandleNurb_intern(
BezTriple *bezt, const BezTriple *prev, const BezTriple *next,
bool is_fcurve, bool skip_align, char fcurve_smoothing)
{
/* defines to avoid confusion */
#define p2_h1 ((p2) - 3)
#define p2_h2 ((p2) + 3)
const float *p1, *p3;
float *p2;
float pt[3];
float dvec_a[3], dvec_b[3];
float len, len_a, len_b;
float len_ratio;
const float eps = 1e-5;
/* assume normal handle until we check */
bezt->f5 = HD_AUTOTYPE_NORMAL;
if (bezt->h1 == 0 && bezt->h2 == 0) {
return;
}
p2 = bezt->vec[1];
if (prev == NULL) {
p3 = next->vec[1];
pt[0] = 2.0f * p2[0] - p3[0];
pt[1] = 2.0f * p2[1] - p3[1];
pt[2] = 2.0f * p2[2] - p3[2];
p1 = pt;
}
else {
p1 = prev->vec[1];
}
if (next == NULL) {
pt[0] = 2.0f * p2[0] - p1[0];
pt[1] = 2.0f * p2[1] - p1[1];
pt[2] = 2.0f * p2[2] - p1[2];
p3 = pt;
}
else {
p3 = next->vec[1];
}
sub_v3_v3v3(dvec_a, p2, p1);
sub_v3_v3v3(dvec_b, p3, p2);
if (is_fcurve) {
len_a = dvec_a[0];
len_b = dvec_b[0];
}
else {
len_a = len_v3(dvec_a);
len_b = len_v3(dvec_b);
}
if (len_a == 0.0f) len_a = 1.0f;
if (len_b == 0.0f) len_b = 1.0f;
len_ratio = len_a / len_b;
if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM) || ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) { /* auto */
float tvec[3];
tvec[0] = dvec_b[0] / len_b + dvec_a[0] / len_a;
tvec[1] = dvec_b[1] / len_b + dvec_a[1] / len_a;
tvec[2] = dvec_b[2] / len_b + dvec_a[2] / len_a;
if (is_fcurve) {
if (fcurve_smoothing != FCURVE_SMOOTH_NONE) {
/* force the horizontal handle size to be 1/3 of the key interval so that
* the X component of the parametric bezier curve is a linear spline */
len = 6.0f / 2.5614f;
}
else {
len = tvec[0];
}
}
else {
len = len_v3(tvec);
}
len *= 2.5614f;
if (len != 0.0f) {
/* only for fcurves */
bool leftviolate = false, rightviolate = false;
if (!is_fcurve || fcurve_smoothing == FCURVE_SMOOTH_NONE) {
if (len_a > 5.0f * len_b)
len_a = 5.0f * len_b;
if (len_b > 5.0f * len_a)
len_b = 5.0f * len_a;
}
if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) {
len_a /= len;
madd_v3_v3v3fl(p2_h1, p2, tvec, -len_a);
if ((bezt->h1 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
if ((ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f)) {
bezt->vec[0][1] = bezt->vec[1][1];
bezt->f5 = HD_AUTOTYPE_SPECIAL;
}
else { /* handles should not be beyond y coord of two others */
if (ydiff1 <= 0.0f) {
if (prev->vec[1][1] > bezt->vec[0][1]) {
bezt->vec[0][1] = prev->vec[1][1];
leftviolate = 1;
}
}
else {
if (prev->vec[1][1] < bezt->vec[0][1]) {
bezt->vec[0][1] = prev->vec[1][1];
leftviolate = 1;
}
}
}
}
}
if (ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
len_b /= len;
madd_v3_v3v3fl(p2_h2, p2, tvec, len_b);
if ((bezt->h2 == HD_AUTO_ANIM) && next && prev) { /* keep horizontal if extrema */
float ydiff1 = prev->vec[1][1] - bezt->vec[1][1];
float ydiff2 = next->vec[1][1] - bezt->vec[1][1];
if ((ydiff1 <= 0.0f && ydiff2 <= 0.0f) || (ydiff1 >= 0.0f && ydiff2 >= 0.0f)) {
bezt->vec[2][1] = bezt->vec[1][1];
bezt->f5 = HD_AUTOTYPE_SPECIAL;
}
else { /* handles should not be beyond y coord of two others */
if (ydiff1 <= 0.0f) {
if (next->vec[1][1] < bezt->vec[2][1]) {
bezt->vec[2][1] = next->vec[1][1];
rightviolate = 1;
}
}
else {
if (next->vec[1][1] > bezt->vec[2][1]) {
bezt->vec[2][1] = next->vec[1][1];
rightviolate = 1;
}
}
}
}
}
if (leftviolate || rightviolate) { /* align left handle */
BLI_assert(is_fcurve);
/* simple 2d calculation */
float h1_x = p2_h1[0] - p2[0];
float h2_x = p2[0] - p2_h2[0];
if (leftviolate) {
p2_h2[1] = p2[1] + ((p2[1] - p2_h1[1]) / h1_x) * h2_x;
}
else {
p2_h1[1] = p2[1] + ((p2[1] - p2_h2[1]) / h2_x) * h1_x;
}
}
}
}
if (bezt->h1 == HD_VECT) { /* vector */
madd_v3_v3v3fl(p2_h1, p2, dvec_a, -1.0f / 3.0f);
}
if (bezt->h2 == HD_VECT) {
madd_v3_v3v3fl(p2_h2, p2, dvec_b, 1.0f / 3.0f);
}
if (skip_align ||
/* when one handle is free, alignming makes no sense, see: T35952 */
(ELEM(HD_FREE, bezt->h1, bezt->h2)) ||
/* also when no handles are aligned, skip this step */
(!ELEM(HD_ALIGN, bezt->h1, bezt->h2) &&
!ELEM(HD_ALIGN_DOUBLESIDE, bezt->h1, bezt->h2)))
{
/* handles need to be updated during animation and applying stuff like hooks,
* but in such situations it's quite difficult to distinguish in which order
* align handles should be aligned so skip them for now */
return;
}
len_a = len_v3v3(p2, p2_h1);
len_b = len_v3v3(p2, p2_h2);
if (len_a == 0.0f) len_a = 1.0f;
if (len_b == 0.0f) len_b = 1.0f;
len_ratio = len_a / len_b;
if (bezt->f1 & SELECT) { /* order of calculation */
if (ELEM(bezt->h2, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) { /* aligned */
if (len_a > eps) {
len = 1.0f / len_ratio;
p2_h2[0] = p2[0] + len * (p2[0] - p2_h1[0]);
p2_h2[1] = p2[1] + len * (p2[1] - p2_h1[1]);
p2_h2[2] = p2[2] + len * (p2[2] - p2_h1[2]);
}
}
if (ELEM(bezt->h1, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) {
if (len_b > eps) {
len = len_ratio;
p2_h1[0] = p2[0] + len * (p2[0] - p2_h2[0]);
p2_h1[1] = p2[1] + len * (p2[1] - p2_h2[1]);
p2_h1[2] = p2[2] + len * (p2[2] - p2_h2[2]);
}
}
}
else {
if (ELEM(bezt->h1, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) {
if (len_b > eps) {
len = len_ratio;
p2_h1[0] = p2[0] + len * (p2[0] - p2_h2[0]);
p2_h1[1] = p2[1] + len * (p2[1] - p2_h2[1]);
p2_h1[2] = p2[2] + len * (p2[2] - p2_h2[2]);
}
}
if (ELEM(bezt->h2, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) { /* aligned */
if (len_a > eps) {
len = 1.0f / len_ratio;
p2_h2[0] = p2[0] + len * (p2[0] - p2_h1[0]);
p2_h2[1] = p2[1] + len * (p2[1] - p2_h1[1]);
p2_h2[2] = p2[2] + len * (p2[2] - p2_h1[2]);
}
}
}
#undef p2_h1
#undef p2_h2
}
static void calchandlesNurb_intern(Nurb *nu, bool skip_align)
{
BezTriple *bezt, *prev, *next;
int a;
if (nu->type != CU_BEZIER)
return;
if (nu->pntsu < 2)
return;
a = nu->pntsu;
bezt = nu->bezt;
if (nu->flagu & CU_NURB_CYCLIC) prev = bezt + (a - 1);
else prev = NULL;
next = bezt + 1;
while (a--) {
calchandleNurb_intern(bezt, prev, next, 0, skip_align, 0);
prev = bezt;
if (a == 1) {
if (nu->flagu & CU_NURB_CYCLIC)
next = nu->bezt;
else
next = NULL;
}
else
next++;
bezt++;
}
}
/* A utility function for allocating a number of arrays of the same length
* with easy error checking and deallocation, and an easy way to add or remove
* arrays that are processed in this way when changing code.
*
* floats, chars: NULL-terminated arrays of pointers to array pointers that need to be allocated.
*
* Returns: pointer to the buffer that contains all of the arrays.
*/
static void *allocate_arrays(int count, float ***floats, char ***chars, const char *name)
{
size_t num_floats = 0, num_chars = 0;
while (floats && floats[num_floats]) {
num_floats++;
}
while (chars && chars[num_chars]) {
num_chars++;
}
void *buffer = (float *)MEM_malloc_arrayN(count, (sizeof(float) * num_floats + num_chars), name);
if (!buffer)
return NULL;
float *fptr = buffer;
for (int i = 0; i < num_floats; i++, fptr += count) {
*floats[i] = fptr;
}
char *cptr = (char *)fptr;
for (int i = 0; i < num_chars; i++, cptr += count) {
*chars[i] = cptr;
}
return buffer;
}
static void free_arrays(void *buffer)
{
MEM_freeN(buffer);
}
/* computes in which direction to change h[i] to satisfy conditions better */
static float bezier_relax_direction(float *a, float *b, float *c, float *d, float *h, int i, int count)
{
/* current deviation between sides of the equation */
float state = a[i] * h[(i + count - 1) % count] +
b[i] * h[i] +
c[i] * h[(i + 1) % count] -
d[i];
/* only the sign is meaningful */
return -state * b[i];
}
static void bezier_lock_unknown(float *a, float *b, float *c, float *d, int i, float value)
{
a[i] = c[i] = 0.0f;
b[i] = 1.0f;
d[i] = value;
}
static void bezier_restore_equation(float *a, float *b, float *c, float *d, float *a0, float *b0, float *c0, float *d0, int i)
{
a[i] = a0[i];
b[i] = b0[i];
c[i] = c0[i];
d[i] = d0[i];
}
static bool tridiagonal_solve_with_limits(float *a, float *b, float *c, float *d, float *h, float *hmin, float *hmax, int solve_count)
{
float *a0, *b0, *c0, *d0;
float **arrays[] = { &a0, &b0, &c0, &d0, NULL };
char *is_locked, *num_unlocks;
char **flagarrays[] = { &is_locked, &num_unlocks, NULL };
void *tmps = allocate_arrays(solve_count, arrays, flagarrays, "tridiagonal_solve_with_limits");
if (!tmps)
return false;
memcpy(a0, a, sizeof(float) * solve_count);
memcpy(b0, b, sizeof(float) * solve_count);
memcpy(c0, c, sizeof(float) * solve_count);
memcpy(d0, d, sizeof(float) * solve_count);
memset(is_locked, 0, solve_count);
memset(num_unlocks, 0, solve_count);
bool overshoot, unlocked;
do {
if (!BLI_tridiagonal_solve_cyclic(a, b, c, d, h, solve_count)) {
free_arrays(tmps);
return false;
}
/* first check if any handles overshoot the limits, and lock them */
bool all = false, locked = false;
overshoot = unlocked = false;
do {
for (int i = 0; i < solve_count; i++) {
if (h[i] >= hmin[i] && h[i] <= hmax[i])
continue;
overshoot = true;
float target = h[i] > hmax[i] ? hmax[i] : hmin[i];
/* heuristically only lock handles that go in the right direction if there are such ones */
if (target != 0.0f || all) {
/* mark item locked */
is_locked[i] = 1;
bezier_lock_unknown(a, b, c, d, i, target);
locked = true;
}
}
all = true;
} while (overshoot && !locked);
/* if no handles overshot and were locked, see if it may be a good idea to unlock some handles */
if (!locked) {
for (int i = 0; i < solve_count; i++) {
// to definitely avoid infinite loops limit this to 2 times
if (!is_locked[i] || num_unlocks[i] >= 2)
continue;
/* if the handle wants to move in allowable direction, release it */
float relax = bezier_relax_direction(a0, b0, c0, d0, h, i, solve_count);
if ((relax > 0 && h[i] < hmax[i]) || (relax < 0 && h[i] > hmin[i])) {
bezier_restore_equation(a, b, c, d, a0, b0, c0, d0, i);
is_locked[i] = 0;
num_unlocks[i]++;
unlocked = true;
}
}
}
} while (overshoot || unlocked);
free_arrays(tmps);
return true;
}
/*
* This function computes the handles of a series of auto bezier points
* on the basis of 'no acceleration discontinuities' at the points.
* The first and last bezier points are considered 'fixed' (their handles are not touched)
* The result is the smoothest possible trajectory going through intermediate points.
* The difficulty is that the handles depends on their neighbors.
*
* The exact solution is found by solving a tridiagonal matrix equation formed
* by the continuity and boundary conditions. Although theoretically handle position
* is affected by all other points of the curve segment, in practice the influence
* decreases exponentially with distance.
*
* Note: this algorithm assumes that the handle horizontal size if always 1/3 of the
* of the interval to the next point. This rule ensures linear interpolation of time.
*
* ^ height (co 1)
* | yN
* | yN-1 |
* | y2 | |
* | y1 | | |
* | y0 | | | |
* | | | | | |
* | | | | | |
* | | | | | |
* |-------t1---------t2--------- ~ --------tN-------------------> time (co 0)
* Mathematical basis:
*
* 1. Handle lengths on either side of each point are connected by a factor
* ensuring continuity of the first derivative:
*
* l[i] = t[i+1]/t[i]
*
* 2. The tridiagonal system is formed by the following equation, which is derived
* by differentiating the bezier curve and specifies second derivative continuity
* at every point:
*
* l[i]^2 * h[i-1] + (2*l[i]+2) * h[i] + 1/l[i+1] * h[i+1] = (y[i]-y[i-1])*l[i]^2 + y[i+1]-y[i]
*
* 3. If this point is adjacent to a manually set handle with X size not equal to 1/3
* of the horizontal interval, this equation becomes slightly more complex:
*
* l[i]^2 * h[i-1] + (3*(1-R[i-1])*l[i] + 3*(1-L[i+1])) * h[i] + 1/l[i+1] * h[i+1] = (y[i]-y[i-1])*l[i]^2 + y[i+1]-y[i]
*
* The difference between equations amounts to this, and it's obvious that when R[i-1]
* and L[i+1] are both 1/3, it becomes zero:
*
* ( (1-3*R[i-1])*l[i] + (1-3*L[i+1]) ) * h[i]
*
* 4. The equations for zero acceleration border conditions are basically the above
* equation with parts omitted, so the handle size correction also applies.
*/
static void bezier_eq_continuous(float *a, float *b, float *c, float *d, float *dy, float *l, int i)
{
a[i] = l[i] * l[i];
b[i] = 2.0f * (l[i] + 1);
c[i] = 1.0f / l[i + 1];
d[i] = dy[i] * l[i] * l[i] + dy[i + 1];
}
static void bezier_eq_noaccel_right(float *a, float *b, float *c, float *d, float *dy, float *l, int i)
{
a[i] = 0.0f;
b[i] = 2.0f;
c[i] = 1.0f / l[i + 1];
d[i] = dy[i + 1];
}
static void bezier_eq_noaccel_left(float *a, float *b, float *c, float *d, float *dy, float *l, int i)
{
a[i] = l[i] * l[i];
b[i] = 2.0f * l[i];
c[i] = 0.0f;
d[i] = dy[i] * l[i] * l[i];
}
/* auto clamp prevents its own point going the wrong way, and adjacent handles overshooting */
static void bezier_clamp(float *hmax, float *hmin, int i, float dy, bool no_reverse, bool no_overshoot)
{
if (dy > 0) {
if (no_overshoot)
hmax[i] = min_ff(hmax[i], dy);
if (no_reverse)
hmin[i] = 0.0f;
}
else if (dy < 0) {
if (no_reverse)
hmax[i] = 0.0f;
if (no_overshoot)
hmin[i] = max_ff(hmin[i], dy);
}
else if (no_reverse || no_overshoot) {
hmax[i] = hmin[i] = 0.0f;
}
}
/* write changes to a bezier handle */
static void bezier_output_handle_inner(BezTriple *bezt, bool right, float newval[3], bool endpoint)
{
float tmp[3];
int idx = right ? 2 : 0;
char hr = right ? bezt->h2 : bezt->h1;
char hm = right ? bezt->h1 : bezt->h2;
/* only assign Auto/Vector handles */
if (!ELEM(hr, HD_AUTO, HD_AUTO_ANIM, HD_VECT))
return;
copy_v3_v3(bezt->vec[idx], newval);
/* fix up the Align handle if any */
if (ELEM(hm, HD_ALIGN, HD_ALIGN_DOUBLESIDE)) {
float hlen = len_v3v3(bezt->vec[1], bezt->vec[2 - idx]);
float h2len = len_v3v3(bezt->vec[1], bezt->vec[idx]);
sub_v3_v3v3(tmp, bezt->vec[1], bezt->vec[idx]);
madd_v3_v3v3fl(bezt->vec[2 - idx], bezt->vec[1], tmp, hlen / h2len);
}
/* at end points of the curve, mirror handle to the other side */
else if (endpoint && ELEM(hm, HD_AUTO, HD_AUTO_ANIM, HD_VECT)) {
sub_v3_v3v3(tmp, bezt->vec[1], bezt->vec[idx]);
add_v3_v3v3(bezt->vec[2 - idx], bezt->vec[1], tmp);
}
}
static void bezier_output_handle(BezTriple *bezt, bool right, float dy, bool endpoint)
{
float tmp[3];
copy_v3_v3(tmp, bezt->vec[right ? 2 : 0]);
tmp[1] = bezt->vec[1][1] + dy;
bezier_output_handle_inner(bezt, right, tmp, endpoint);
}
static bool bezier_check_solve_end_handle(BezTriple *bezt, char htype, bool end)
{
return (htype == HD_VECT) || (end && ELEM(htype, HD_AUTO, HD_AUTO_ANIM) && bezt->f5 == HD_AUTOTYPE_NORMAL);
}
static float bezier_calc_handle_adj(float hsize[2], float dx)
{
/* if handles intersect in x direction, they are scaled to fit */
float fac = dx / (hsize[0] + dx / 3.0f);
if (fac < 1.0f) {
mul_v2_fl(hsize, fac);
}
return 1.0f - 3.0f * hsize[0] / dx;
}
static void bezier_handle_calc_smooth_fcurve(BezTriple *bezt, int total, int start, int count, bool cycle)
{
float *dx, *dy, *l, *a, *b, *c, *d, *h, *hmax, *hmin;
float **arrays[] = { &dx, &dy, &l, &a, &b, &c, &d, &h, &hmax, &hmin, NULL };
int solve_count = count;
/* verify index ranges */
if (count < 2)
return;
BLI_assert(start < total - 1 && count <= total);
BLI_assert(start + count <= total || cycle);
bool full_cycle = (start == 0 && count == total && cycle);
BezTriple *bezt_first = &bezt[start];
BezTriple *bezt_last = &bezt[(start + count > total) ? start + count - total : start + count - 1];
bool solve_first = bezier_check_solve_end_handle(bezt_first, bezt_first->h2, start == 0);
bool solve_last = bezier_check_solve_end_handle(bezt_last, bezt_last->h1, start + count == total);
if (count == 2 && !full_cycle && solve_first == solve_last) {
return;
}
/* allocate all */
void *tmp_buffer = allocate_arrays(count, arrays, NULL, "bezier_calc_smooth_tmp");
if (!tmp_buffer)
return;
/* point locations */
dx[0] = dy[0] = NAN_FLT;
for (int i = 1, j = start + 1; i < count; i++, j++) {
dx[i] = bezt[j].vec[1][0] - bezt[j - 1].vec[1][0];
dy[i] = bezt[j].vec[1][1] - bezt[j - 1].vec[1][1];
/* when cyclic, jump from last point to first */
if (cycle && j == total - 1)
j = 0;
}
/* ratio of x intervals */
l[0] = l[count - 1] = 1.0f;
for (int i = 1; i < count - 1; i++) {
l[i] = dx[i + 1] / dx[i];
}
/* compute handle clamp ranges */
bool clamped_prev = false, clamped_cur = ELEM(HD_AUTO_ANIM, bezt_first->h1, bezt_first->h2);
for (int i = 0; i < count; i++) {
hmax[i] = FLT_MAX;
hmin[i] = -FLT_MAX;
}
for (int i = 1, j = start + 1; i < count; i++, j++) {
clamped_prev = clamped_cur;
clamped_cur = ELEM(HD_AUTO_ANIM, bezt[j].h1, bezt[j].h2);
if (cycle && j == total - 1) {
j = 0;
clamped_cur = clamped_cur || ELEM(HD_AUTO_ANIM, bezt[j].h1, bezt[j].h2);
}
bezier_clamp(hmax, hmin, i - 1, dy[i], clamped_prev, clamped_prev);
bezier_clamp(hmax, hmin, i, dy[i] * l[i], clamped_cur, clamped_cur);
}
/* full cycle merges first and last points into continuous loop */
float first_handle_adj = 0.0f, last_handle_adj = 0.0f;
if (full_cycle) {
/* reduce the number of unknowns by one */
int i = solve_count = count - 1;
dx[0] = dx[i];
dy[0] = dy[i];
l[0] = l[i] = dx[1] / dx[0];
hmin[0] = max_ff(hmin[0], hmin[i]);
hmax[0] = min_ff(hmax[0], hmax[i]);
solve_first = solve_last = true;
bezier_eq_continuous(a, b, c, d, dy, l, 0);
}
else {
float tmp[2];
/* boundary condition: fixed handles or zero curvature */
if (!solve_first) {
sub_v2_v2v2(tmp, bezt_first->vec[2], bezt_first->vec[1]);
first_handle_adj = bezier_calc_handle_adj(tmp, dx[1]);
bezier_lock_unknown(a, b, c, d, 0, tmp[1]);
}
else {
bezier_eq_noaccel_right(a, b, c, d, dy, l, 0);
}
if (!solve_last) {
sub_v2_v2v2(tmp, bezt_last->vec[1], bezt_last->vec[0]);
last_handle_adj = bezier_calc_handle_adj(tmp, dx[count - 1]);
bezier_lock_unknown(a, b, c, d, count - 1, tmp[1]);
}
else {
bezier_eq_noaccel_left(a, b, c, d, dy, l, count - 1);
}
}
/* main tridiagonal system of equations */
for (int i = 1; i < count - 1; i++) {
bezier_eq_continuous(a, b, c, d, dy, l, i);
}
/* apply correction for user-defined handles with nonstandard x positions */
if (!full_cycle) {
if (count > 2 || solve_last) {
b[1] += l[1] * first_handle_adj;
}
if (count > 2 || solve_first) {
b[count - 2] += last_handle_adj;
}
}
/* solve and output results */
if (tridiagonal_solve_with_limits(a, b, c, d, h, hmin, hmax, solve_count)) {
if (full_cycle) {
h[count - 1] = h[0];
}
for (int i = 1, j = start + 1; i < count - 1; i++, j++) {
bool end = (j == total - 1);
bezier_output_handle(&bezt[j], false, -h[i] / l[i], end);
if (end)
j = 0;
bezier_output_handle(&bezt[j], true, h[i], end);
}
if (solve_first) {
bezier_output_handle(bezt_first, true, h[0], start == 0);
}
if (solve_last) {
bezier_output_handle(bezt_last, false, -h[count - 1] / l[count - 1], start + count == total);
}
}
/* free all */
free_arrays(tmp_buffer);
}
static bool is_free_auto_point(BezTriple *bezt)
{
return BEZT_IS_AUTOH(bezt) && bezt->f5 == HD_AUTOTYPE_NORMAL;
}
void BKE_nurb_handle_smooth_fcurve(BezTriple *bezt, int total, bool cycle)
{
/* ignore cyclic extrapolation if end points are locked */
cycle = cycle && is_free_auto_point(&bezt[0]) && is_free_auto_point(&bezt[total - 1]);
/* if cyclic, try to find a sequence break point */
int search_base = 0;
if (cycle) {
for (int i = 1; i < total - 1; i++) {
if (!is_free_auto_point(&bezt[i])) {
search_base = i;
break;
}
}
/* all points of the curve are freely changeable auto handles - solve as full cycle */
if (search_base == 0) {
bezier_handle_calc_smooth_fcurve(bezt, total, 0, total, cycle);
return;
}
}
/* Find continuous subsequences of free auto handles and smooth them, starting at
* search_base. In cyclic mode these subsequences can span the cycle boundary. */
int start = search_base, count = 1;
for (int i = 1, j = start + 1; i < total; i++, j++) {
/* in cyclic mode: jump from last to first point when necessary */
if (j == total - 1 && cycle)
j = 0;
/* non auto handle closes the list (we come here at least for the last handle, see above) */
if (!is_free_auto_point(&bezt[j])) {
bezier_handle_calc_smooth_fcurve(bezt, total, start, count + 1, cycle);
start = j;
count = 1;
}
else {
count++;
}
}
if (count > 1) {
bezier_handle_calc_smooth_fcurve(bezt, total, start, count, cycle);
}
}
void BKE_nurb_handle_calc(BezTriple *bezt, BezTriple *prev, BezTriple *next, const bool is_fcurve, const char smoothing)
{
calchandleNurb_intern(bezt, prev, next, is_fcurve, false, smoothing);
}
void BKE_nurb_handles_calc(Nurb *nu) /* first, if needed, set handle flags */
{
calchandlesNurb_intern(nu, false);
}
/**
* Workaround #BKE_nurb_handles_calc logic
* that makes unselected align to the selected handle.
*/
static void nurbList_handles_swap_select(Nurb *nu)
{
BezTriple *bezt;
int i;
for (i = nu->pntsu, bezt = nu->bezt; i--; bezt++) {
if ((bezt->f1 & SELECT) != (bezt->f3 & SELECT)) {
bezt->f1 ^= SELECT;
bezt->f3 ^= SELECT;
}
}
}
/* internal use only (weak) */
static void nurb_handles_calc__align_selected(Nurb *nu)
{
nurbList_handles_swap_select(nu);
BKE_nurb_handles_calc(nu);
nurbList_handles_swap_select(nu);
}
/* similar to BKE_nurb_handle_calc but for curves and
* figures out the previous and next for us */
void BKE_nurb_handle_calc_simple(Nurb *nu, BezTriple *bezt)
{
if (nu->pntsu > 1) {
BezTriple *prev = BKE_nurb_bezt_get_prev(nu, bezt);
BezTriple *next = BKE_nurb_bezt_get_next(nu, bezt);
BKE_nurb_handle_calc(bezt, prev, next, 0, 0);
}
}
void BKE_nurb_handle_calc_simple_auto(Nurb *nu, BezTriple *bezt)
{
if (nu->pntsu > 1) {
const char h1_back = bezt->h1, h2_back = bezt->h2;
bezt->h1 = bezt->h2 = HD_AUTO;
/* Override handle types to HD_AUTO and recalculate */
BKE_nurb_handle_calc_simple(nu, bezt);
bezt->h1 = h1_back;
bezt->h2 = h2_back;
}
}
/**
* Use when something has changed handle positions.
*
* The caller needs to recalculate handles.
*/
void BKE_nurb_bezt_handle_test(BezTriple *bezt, const bool use_handle)
{
short flag = 0;
#define SEL_F1 (1 << 0)
#define SEL_F2 (1 << 1)
#define SEL_F3 (1 << 2)
if (use_handle) {
if (bezt->f1 & SELECT) flag |= SEL_F1;
if (bezt->f2 & SELECT) flag |= SEL_F2;
if (bezt->f3 & SELECT) flag |= SEL_F3;
}
else {
flag = (bezt->f2 & SELECT) ? (SEL_F1 | SEL_F2 | SEL_F3) : 0;
}
/* check for partial selection */
if (!ELEM(flag, 0, SEL_F1 | SEL_F2 | SEL_F3)) {
if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM)) {
bezt->h1 = HD_ALIGN;
}
if (ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM)) {
bezt->h2 = HD_ALIGN;
}
if (bezt->h1 == HD_VECT) {
if ((!(flag & SEL_F1)) != (!(flag & SEL_F2))) {
bezt->h1 = HD_FREE;
}
}
if (bezt->h2 == HD_VECT) {
if ((!(flag & SEL_F3)) != (!(flag & SEL_F2))) {
bezt->h2 = HD_FREE;
}
}
}
#undef SEL_F1
#undef SEL_F2
#undef SEL_F3
}
void BKE_nurb_handles_test(Nurb *nu, const bool use_handle)
{
BezTriple *bezt;
int a;
if (nu->type != CU_BEZIER)
return;
bezt = nu->bezt;
a = nu->pntsu;
while (a--) {
BKE_nurb_bezt_handle_test(bezt, use_handle);
bezt++;
}
BKE_nurb_handles_calc(nu);
}
void BKE_nurb_handles_autocalc(Nurb *nu, int flag)
{
/* checks handle coordinates and calculates type */
const float eps = 0.0001f;
const float eps_sq = eps * eps;
BezTriple *bezt2, *bezt1, *bezt0;
int i;
if (nu == NULL || nu->bezt == NULL)
return;
bezt2 = nu->bezt;
bezt1 = bezt2 + (nu->pntsu - 1);
bezt0 = bezt1 - 1;
i = nu->pntsu;
while (i--) {
bool align = false, leftsmall = false, rightsmall = false;
/* left handle: */
if (flag == 0 || (bezt1->f1 & flag)) {
bezt1->h1 = HD_FREE;
/* distance too short: vectorhandle */
if (len_squared_v3v3(bezt1->vec[1], bezt0->vec[1]) < eps_sq) {
bezt1->h1 = HD_VECT;
leftsmall = true;
}
else {
/* aligned handle? */
if (dist_squared_to_line_v3(bezt1->vec[1], bezt1->vec[0], bezt1->vec[2]) < eps_sq) {
align = true;
bezt1->h1 = HD_ALIGN;
}
/* or vector handle? */
if (dist_squared_to_line_v3(bezt1->vec[0], bezt1->vec[1], bezt0->vec[1]) < eps_sq)
bezt1->h1 = HD_VECT;
}
}
/* right handle: */
if (flag == 0 || (bezt1->f3 & flag)) {
bezt1->h2 = HD_FREE;
/* distance too short: vectorhandle */
if (len_squared_v3v3(bezt1->vec[1], bezt2->vec[1]) < eps_sq) {
bezt1->h2 = HD_VECT;
rightsmall = true;
}
else {
/* aligned handle? */
if (align) bezt1->h2 = HD_ALIGN;
/* or vector handle? */
if (dist_squared_to_line_v3(bezt1->vec[2], bezt1->vec[1], bezt2->vec[1]) < eps_sq)
bezt1->h2 = HD_VECT;
}
}
if (leftsmall && bezt1->h2 == HD_ALIGN)
bezt1->h2 = HD_FREE;
if (rightsmall && bezt1->h1 == HD_ALIGN)
bezt1->h1 = HD_FREE;
/* undesired combination: */
if (bezt1->h1 == HD_ALIGN && bezt1->h2 == HD_VECT)
bezt1->h1 = HD_FREE;
if (bezt1->h2 == HD_ALIGN && bezt1->h1 == HD_VECT)
bezt1->h2 = HD_FREE;
bezt0 = bezt1;
bezt1 = bezt2;
bezt2++;
}
BKE_nurb_handles_calc(nu);
}
void BKE_nurbList_handles_autocalc(ListBase *editnurb, int flag)
{
Nurb *nu;
nu = editnurb->first;
while (nu) {
BKE_nurb_handles_autocalc(nu, flag);
nu = nu->next;
}
}
void BKE_nurbList_handles_set(ListBase *editnurb, const char code)
{
/* code==1: set autohandle */
/* code==2: set vectorhandle */
/* code==3 (HD_ALIGN) it toggle, vectorhandles become HD_FREE */
/* code==4: sets icu flag to become IPO_AUTO_HORIZ, horizontal extremes on auto-handles */
/* code==5: Set align, like 3 but no toggle */
/* code==6: Clear align, like 3 but no toggle */
Nurb *nu;
BezTriple *bezt;
int a;
if (ELEM(code, HD_AUTO, HD_VECT)) {
nu = editnurb->first;
while (nu) {
if (nu->type == CU_BEZIER) {
bezt = nu->bezt;
a = nu->pntsu;
while (a--) {
if ((bezt->f1 & SELECT) || (bezt->f3 & SELECT)) {
if (bezt->f1 & SELECT)
bezt->h1 = code;
if (bezt->f3 & SELECT)
bezt->h2 = code;
if (bezt->h1 != bezt->h2) {
if (ELEM(bezt->h1, HD_ALIGN, HD_AUTO))
bezt->h1 = HD_FREE;
if (ELEM(bezt->h2, HD_ALIGN, HD_AUTO))
bezt->h2 = HD_FREE;
}
}
bezt++;
}
/* like BKE_nurb_handles_calc but moves selected */
nurb_handles_calc__align_selected(nu);
}
nu = nu->next;
}
}
else {
char h_new = HD_FREE;
/* there is 1 handle not FREE: FREE it all, else make ALIGNED */
if (code == 5) {
h_new = HD_ALIGN;
}
else if (code == 6) {
h_new = HD_FREE;
}
else {
/* Toggle */
for (nu = editnurb->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
bezt = nu->bezt;
a = nu->pntsu;
while (a--) {
if (((bezt->f1 & SELECT) && bezt->h1 != HD_FREE) ||
((bezt->f3 & SELECT) && bezt->h2 != HD_FREE))
{
h_new = HD_AUTO;
break;
}
bezt++;
}
}
}
h_new = (h_new == HD_FREE) ? HD_ALIGN : HD_FREE;
}
for (nu = editnurb->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
bezt = nu->bezt;
a = nu->pntsu;
while (a--) {
if (bezt->f1 & SELECT) bezt->h1 = h_new;
if (bezt->f3 & SELECT) bezt->h2 = h_new;
bezt++;
}
/* like BKE_nurb_handles_calc but moves selected */
nurb_handles_calc__align_selected(nu);
}
}
}
}
void BKE_nurbList_handles_recalculate(ListBase *editnurb, const bool calc_length, const char flag)
{
Nurb *nu;
BezTriple *bezt;
int a;
for (nu = editnurb->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
bool changed = false;
for (a = nu->pntsu, bezt = nu->bezt; a--; bezt++) {
const bool h1_select = (bezt->f1 & flag) == flag;
const bool h2_select = (bezt->f3 & flag) == flag;
if (h1_select || h2_select) {
float co1_back[3], co2_back[3];
copy_v3_v3(co1_back, bezt->vec[0]);
copy_v3_v3(co2_back, bezt->vec[2]);
BKE_nurb_handle_calc_simple_auto(nu, bezt);
if (h1_select) {
if (!calc_length) {
dist_ensure_v3_v3fl(bezt->vec[0], bezt->vec[1], len_v3v3(co1_back, bezt->vec[1]));
}
}
else {
copy_v3_v3(bezt->vec[0], co1_back);
}
if (h2_select) {
if (!calc_length) {
dist_ensure_v3_v3fl(bezt->vec[2], bezt->vec[1], len_v3v3(co2_back, bezt->vec[1]));
}
}
else {
copy_v3_v3(bezt->vec[2], co2_back);
}
changed = true;
}
}
if (changed) {
/* Recalculate the whole curve */
BKE_nurb_handles_calc(nu);
}
}
}
}
void BKE_nurbList_flag_set(ListBase *editnurb, short flag)
{
Nurb *nu;
BezTriple *bezt;
BPoint *bp;
int a;
for (nu = editnurb->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
a = nu->pntsu;
bezt = nu->bezt;
while (a--) {
bezt->f1 = bezt->f2 = bezt->f3 = flag;
bezt++;
}
}
else {
a = nu->pntsu * nu->pntsv;
bp = nu->bp;
while (a--) {
bp->f1 = flag;
bp++;
}
}
}
}
void BKE_nurb_direction_switch(Nurb *nu)
{
BezTriple *bezt1, *bezt2;
BPoint *bp1, *bp2;
float *fp1, *fp2, *tempf;
int a, b;
if (nu->pntsu == 1 && nu->pntsv == 1) {
return;
}
if (nu->type == CU_BEZIER) {
a = nu->pntsu;
bezt1 = nu->bezt;
bezt2 = bezt1 + (a - 1);
if (a & 1) a += 1; /* if odd, also swap middle content */
a /= 2;
while (a > 0) {
if (bezt1 != bezt2) {
SWAP(BezTriple, *bezt1, *bezt2);
}
swap_v3_v3(bezt1->vec[0], bezt1->vec[2]);
if (bezt1 != bezt2) {
swap_v3_v3(bezt2->vec[0], bezt2->vec[2]);
}
SWAP(char, bezt1->h1, bezt1->h2);
SWAP(char, bezt1->f1, bezt1->f3);
if (bezt1 != bezt2) {
SWAP(char, bezt2->h1, bezt2->h2);
SWAP(char, bezt2->f1, bezt2->f3);
bezt1->tilt = -bezt1->tilt;
bezt2->tilt = -bezt2->tilt;
}
else {
bezt1->tilt = -bezt1->tilt;
}
a--;
bezt1++;
bezt2--;
}
}
else if (nu->pntsv == 1) {
a = nu->pntsu;
bp1 = nu->bp;
bp2 = bp1 + (a - 1);
a /= 2;
while (bp1 != bp2 && a > 0) {
SWAP(BPoint, *bp1, *bp2);
a--;
bp1->tilt = -bp1->tilt;
bp2->tilt = -bp2->tilt;
bp1++;
bp2--;
}
/* If there're odd number of points no need to touch coord of middle one,
* but still need to change it's tilt.
*/
if (nu->pntsu & 1) {
bp1->tilt = -bp1->tilt;
}
if (nu->type == CU_NURBS) {
/* no knots for too short paths */
if (nu->knotsu) {
/* inverse knots */
a = KNOTSU(nu);
fp1 = nu->knotsu;
fp2 = fp1 + (a - 1);
a /= 2;
while (fp1 != fp2 && a > 0) {
SWAP(float, *fp1, *fp2);
a--;
fp1++;
fp2--;
}
/* and make in increasing order again */
a = KNOTSU(nu);
fp1 = nu->knotsu;
fp2 = tempf = MEM_malloc_arrayN(a, sizeof(float), "switchdirect");
a--;
fp2[a] = fp1[a];
while (a--) {
fp2[0] = fabsf(fp1[1] - fp1[0]);
fp1++;
fp2++;
}
a = KNOTSU(nu) - 1;
fp1 = nu->knotsu;
fp2 = tempf;
fp1[0] = 0.0;
fp1++;
while (a--) {
fp1[0] = fp1[-1] + fp2[0];
fp1++;
fp2++;
}
MEM_freeN(tempf);
}
}
}
else {
for (b = 0; b < nu->pntsv; b++) {
bp1 = nu->bp + b * nu->pntsu;
a = nu->pntsu;
bp2 = bp1 + (a - 1);
a /= 2;
while (bp1 != bp2 && a > 0) {
SWAP(BPoint, *bp1, *bp2);
a--;
bp1++;
bp2--;
}
}
}
}
float (*BKE_curve_nurbs_vertexCos_get(ListBase *lb, int *r_numVerts))[3]
{
int i, numVerts = *r_numVerts = BKE_nurbList_verts_count(lb);
float *co, (*cos)[3] = MEM_malloc_arrayN(numVerts, sizeof(*cos), "cu_vcos");
Nurb *nu;
co = cos[0];
for (nu = lb->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
BezTriple *bezt = nu->bezt;
for (i = 0; i < nu->pntsu; i++, bezt++) {
copy_v3_v3(co, bezt->vec[0]); co += 3;
copy_v3_v3(co, bezt->vec[1]); co += 3;
copy_v3_v3(co, bezt->vec[2]); co += 3;
}
}
else {
BPoint *bp = nu->bp;
for (i = 0; i < nu->pntsu * nu->pntsv; i++, bp++) {
copy_v3_v3(co, bp->vec); co += 3;
}
}
}
return cos;
}
void BK_curve_nurbs_vertexCos_apply(ListBase *lb, float (*vertexCos)[3])
{
const float *co = vertexCos[0];
Nurb *nu;
int i;
for (nu = lb->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
BezTriple *bezt = nu->bezt;
for (i = 0; i < nu->pntsu; i++, bezt++) {
copy_v3_v3(bezt->vec[0], co); co += 3;
copy_v3_v3(bezt->vec[1], co); co += 3;
copy_v3_v3(bezt->vec[2], co); co += 3;
}
}
else {
BPoint *bp = nu->bp;
for (i = 0; i < nu->pntsu * nu->pntsv; i++, bp++) {
copy_v3_v3(bp->vec, co); co += 3;
}
}
calchandlesNurb_intern(nu, true);
}
}
float (*BKE_curve_nurbs_keyVertexCos_get(ListBase *lb, float *key))[3]
{
int i, numVerts = BKE_nurbList_verts_count(lb);
float *co, (*cos)[3] = MEM_malloc_arrayN(numVerts, sizeof(*cos), "cu_vcos");
Nurb *nu;
co = cos[0];
for (nu = lb->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
BezTriple *bezt = nu->bezt;
for (i = 0; i < nu->pntsu; i++, bezt++) {
copy_v3_v3(co, &key[0]); co += 3;
copy_v3_v3(co, &key[3]); co += 3;
copy_v3_v3(co, &key[6]); co += 3;
key += KEYELEM_FLOAT_LEN_BEZTRIPLE;
}
}
else {
BPoint *bp = nu->bp;
for (i = 0; i < nu->pntsu * nu->pntsv; i++, bp++) {
copy_v3_v3(co, key); co += 3;
key += KEYELEM_FLOAT_LEN_BPOINT;
}
}
}
return cos;
}
void BKE_curve_nurbs_keyVertexTilts_apply(ListBase *lb, float *key)
{
Nurb *nu;
int i;
for (nu = lb->first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
BezTriple *bezt = nu->bezt;
for (i = 0; i < nu->pntsu; i++, bezt++) {
bezt->tilt = key[9];
bezt->radius = key[10];
key += KEYELEM_FLOAT_LEN_BEZTRIPLE;
}
}
else {
BPoint *bp = nu->bp;
for (i = 0; i < nu->pntsu * nu->pntsv; i++, bp++) {
bp->tilt = key[3];
bp->radius = key[4];
key += KEYELEM_FLOAT_LEN_BPOINT;
}
}
}
}
bool BKE_nurb_check_valid_u(struct Nurb *nu)
{
if (nu->pntsu <= 1)
return false;
if (nu->type != CU_NURBS)
return true; /* not a nurb, lets assume its valid */
if (nu->pntsu < nu->orderu) return false;
if (((nu->flagu & CU_NURB_CYCLIC) == 0) && (nu->flagu & CU_NURB_BEZIER)) { /* Bezier U Endpoints */
if (nu->orderu == 4) {
if (nu->pntsu < 5)
return false; /* bezier with 4 orderu needs 5 points */
}
else {
if (nu->orderu != 3)
return false; /* order must be 3 or 4 */
}
}
return true;
}
bool BKE_nurb_check_valid_v(struct Nurb *nu)
{
if (nu->pntsv <= 1)
return false;
if (nu->type != CU_NURBS)
return true; /* not a nurb, lets assume its valid */
if (nu->pntsv < nu->orderv)
return false;
if (((nu->flagv & CU_NURB_CYCLIC) == 0) && (nu->flagv & CU_NURB_BEZIER)) { /* Bezier V Endpoints */
if (nu->orderv == 4) {
if (nu->pntsv < 5)
return false; /* bezier with 4 orderu needs 5 points */
}
else {
if (nu->orderv != 3)
return false; /* order must be 3 or 4 */
}
}
return true;
}
bool BKE_nurb_check_valid_uv(struct Nurb *nu)
{
if (!BKE_nurb_check_valid_u(nu))
return false;
if ((nu->pntsv > 1) && !BKE_nurb_check_valid_v(nu))
return false;
return true;
}
bool BKE_nurb_order_clamp_u(struct Nurb *nu)
{
bool changed = false;
if (nu->pntsu < nu->orderu) {
nu->orderu = max_ii(2, nu->pntsu);
changed = true;
}
if (((nu->flagu & CU_NURB_CYCLIC) == 0) && (nu->flagu & CU_NURB_BEZIER)) {
CLAMP(nu->orderu, 3, 4);
changed = true;
}
return changed;
}
bool BKE_nurb_order_clamp_v(struct Nurb *nu)
{
bool changed = false;
if (nu->pntsv < nu->orderv) {
nu->orderv = max_ii(2, nu->pntsv);
changed = true;
}
if (((nu->flagv & CU_NURB_CYCLIC) == 0) && (nu->flagv & CU_NURB_BEZIER)) {
CLAMP(nu->orderv, 3, 4);
changed = true;
}
return changed;
}
/**
* \note caller must ensure active vertex remains valid.
*/
bool BKE_nurb_type_convert(Nurb *nu, const short type, const bool use_handles)
{
BezTriple *bezt;
BPoint *bp;
int a, c, nr;
if (nu->type == CU_POLY) {
if (type == CU_BEZIER) { /* to Bezier with vecthandles */
nr = nu->pntsu;
bezt = (BezTriple *)MEM_calloc_arrayN(nr, sizeof(BezTriple), "setsplinetype2");
nu->bezt = bezt;
a = nr;
bp = nu->bp;
while (a--) {
copy_v3_v3(bezt->vec[1], bp->vec);
bezt->f1 = bezt->f2 = bezt->f3 = bp->f1;
bezt->h1 = bezt->h2 = HD_VECT;
bezt->weight = bp->weight;
bezt->radius = bp->radius;
bp++;
bezt++;
}
MEM_freeN(nu->bp);
nu->bp = NULL;
nu->pntsu = nr;
nu->pntsv = 0;
nu->type = CU_BEZIER;
BKE_nurb_handles_calc(nu);
}
else if (type == CU_NURBS) {
nu->type = CU_NURBS;
nu->orderu = 4;
nu->flagu &= CU_NURB_CYCLIC; /* disable all flags except for cyclic */
BKE_nurb_knot_calc_u(nu);
a = nu->pntsu * nu->pntsv;
bp = nu->bp;
while (a--) {
bp->vec[3] = 1.0;
bp++;
}
}
}
else if (nu->type == CU_BEZIER) { /* Bezier */
if (type == CU_POLY || type == CU_NURBS) {
nr = use_handles ? (3 * nu->pntsu) : nu->pntsu;
nu->bp = MEM_calloc_arrayN(nr, sizeof(BPoint), "setsplinetype");
a = nu->pntsu;
bezt = nu->bezt;
bp = nu->bp;
while (a--) {
if ((type == CU_POLY && bezt->h1 == HD_VECT && bezt->h2 == HD_VECT) || (use_handles == false)) {
/* vector handle becomes 1 poly vertice */
copy_v3_v3(bp->vec, bezt->vec[1]);
bp->vec[3] = 1.0;
bp->f1 = bezt->f2;
if (use_handles) nr -= 2;
bp->radius = bezt->radius;
bp->weight = bezt->weight;
bp++;
}
else {
const char *f = &bezt->f1;
for (c = 0; c < 3; c++, f++) {
copy_v3_v3(bp->vec, bezt->vec[c]);
bp->vec[3] = 1.0;
bp->f1 = *f;
bp->radius = bezt->radius;
bp->weight = bezt->weight;
bp++;
}
}
bezt++;
}
MEM_freeN(nu->bezt);
nu->bezt = NULL;
nu->pntsu = nr;
nu->pntsv = 1;
nu->orderu = 4;
nu->orderv = 1;
nu->type = type;
if (type == CU_NURBS) {
nu->flagu &= CU_NURB_CYCLIC; /* disable all flags except for cyclic */
nu->flagu |= CU_NURB_BEZIER;
BKE_nurb_knot_calc_u(nu);
}
}
}
else if (nu->type == CU_NURBS) {
if (type == CU_POLY) {
nu->type = CU_POLY;
if (nu->knotsu) MEM_freeN(nu->knotsu); /* python created nurbs have a knotsu of zero */
nu->knotsu = NULL;
if (nu->knotsv) MEM_freeN(nu->knotsv);
nu->knotsv = NULL;
}
else if (type == CU_BEZIER) { /* to Bezier */
nr = nu->pntsu / 3;
if (nr < 2) {
return false; /* conversion impossible */
}
else {
bezt = MEM_calloc_arrayN(nr, sizeof(BezTriple), "setsplinetype2");
nu->bezt = bezt;
a = nr;
bp = nu->bp;
while (a--) {
copy_v3_v3(bezt->vec[0], bp->vec);
bezt->f1 = bp->f1;
bp++;
copy_v3_v3(bezt->vec[1], bp->vec);
bezt->f2 = bp->f1;
bp++;
copy_v3_v3(bezt->vec[2], bp->vec);
bezt->f3 = bp->f1;
bezt->radius = bp->radius;
bezt->weight = bp->weight;
bp++;
bezt++;
}
MEM_freeN(nu->bp);
nu->bp = NULL;
MEM_freeN(nu->knotsu);
nu->knotsu = NULL;
nu->pntsu = nr;
nu->type = CU_BEZIER;
}
}
}
return true;
}
/* Get edit nurbs or normal nurbs list */
ListBase *BKE_curve_nurbs_get(Curve *cu)
{
if (cu->editnurb) {
return BKE_curve_editNurbs_get(cu);
}
return &cu->nurb;
}
void BKE_curve_nurb_active_set(Curve *cu, const Nurb *nu)
{
if (nu == NULL) {
cu->actnu = CU_ACT_NONE;
}
else {
BLI_assert(!nu->hide);
ListBase *nurbs = BKE_curve_editNurbs_get(cu);
cu->actnu = BLI_findindex(nurbs, nu);
}
}
Nurb *BKE_curve_nurb_active_get(Curve *cu)
{
ListBase *nurbs = BKE_curve_editNurbs_get(cu);
return BLI_findlink(nurbs, cu->actnu);
}
/* Get active vert for curve */
void *BKE_curve_vert_active_get(Curve *cu)
{
Nurb *nu = NULL;
void *vert = NULL;
BKE_curve_nurb_vert_active_get(cu, &nu, &vert);
return vert;
}
int BKE_curve_nurb_vert_index_get(const Nurb *nu, const void *vert)
{
if (nu->type == CU_BEZIER) {
BLI_assert(ARRAY_HAS_ITEM((BezTriple *)vert, nu->bezt, nu->pntsu));
return (BezTriple *)vert - nu->bezt;
}
else {
BLI_assert(ARRAY_HAS_ITEM((BPoint *)vert, nu->bp, nu->pntsu * nu->pntsv));
return (BPoint *)vert - nu->bp;
}
}
/* Set active nurb and active vert for curve */
void BKE_curve_nurb_vert_active_set(Curve *cu, const Nurb *nu, const void *vert)
{
if (nu) {
BKE_curve_nurb_active_set(cu, nu);
if (vert) {
cu->actvert = BKE_curve_nurb_vert_index_get(nu, vert);
}
else {
cu->actvert = CU_ACT_NONE;
}
}
else {
cu->actnu = cu->actvert = CU_ACT_NONE;
}
}
/* Get points to active active nurb and active vert for curve */
bool BKE_curve_nurb_vert_active_get(Curve *cu, Nurb **r_nu, void **r_vert)
{
Nurb *nu = NULL;
void *vert = NULL;
if (cu->actvert != CU_ACT_NONE) {
ListBase *nurbs = BKE_curve_editNurbs_get(cu);
nu = BLI_findlink(nurbs, cu->actnu);
if (nu) {
if (nu->type == CU_BEZIER) {
BLI_assert(nu->pntsu > cu->actvert);
vert = &nu->bezt[cu->actvert];
}
else {
BLI_assert((nu->pntsu * nu->pntsv) > cu->actvert);
vert = &nu->bp[cu->actvert];
}
}
}
*r_nu = nu;
*r_vert = vert;
return (*r_vert != NULL);
}
void BKE_curve_nurb_vert_active_validate(Curve *cu)
{
Nurb *nu;
void *vert;
if (BKE_curve_nurb_vert_active_get(cu, &nu, &vert)) {
if (nu->type == CU_BEZIER) {
BezTriple *bezt = vert;
if (BEZT_ISSEL_ANY(bezt) == 0) {
cu->actvert = CU_ACT_NONE;
}
}
else {
BPoint *bp = vert;
if ((bp->f1 & SELECT) == 0) {
cu->actvert = CU_ACT_NONE;
}
}
if (nu->hide) {
cu->actnu = CU_ACT_NONE;
}
}
}
/* basic vertex data functions */
bool BKE_curve_minmax(Curve *cu, bool use_radius, float min[3], float max[3])
{
ListBase *nurb_lb = BKE_curve_nurbs_get(cu);
ListBase temp_nurb_lb = {NULL, NULL};
const bool is_font = (BLI_listbase_is_empty(nurb_lb)) && (cu->len != 0);
/* For font curves we generate temp list of splines.
*
* This is likely to be fine, this function is not supposed to be called
* often, and it's the only way to get meaningful bounds for fonts.
*/
if (is_font) {
nurb_lb = &temp_nurb_lb;
BKE_vfont_to_curve_ex(NULL, cu, FO_EDIT, nurb_lb,
NULL, NULL, NULL, NULL);
use_radius = false;
}
/* Do bounding box based on splines. */
for (Nurb *nu = nurb_lb->first; nu; nu = nu->next) {
BKE_nurb_minmax(nu, use_radius, min, max);
}
const bool result = (BLI_listbase_is_empty(nurb_lb) == false);
/* Cleanup if needed. */
BKE_nurbList_free(&temp_nurb_lb);
return result;
}
bool BKE_curve_center_median(Curve *cu, float cent[3])
{
ListBase *nurb_lb = BKE_curve_nurbs_get(cu);
Nurb *nu;
int total = 0;
zero_v3(cent);
for (nu = nurb_lb->first; nu; nu = nu->next) {
int i;
if (nu->type == CU_BEZIER) {
BezTriple *bezt;
i = nu->pntsu;
total += i * 3;
for (bezt = nu->bezt; i--; bezt++) {
add_v3_v3(cent, bezt->vec[0]);
add_v3_v3(cent, bezt->vec[1]);
add_v3_v3(cent, bezt->vec[2]);
}
}
else {
BPoint *bp;
i = nu->pntsu * nu->pntsv;
total += i;
for (bp = nu->bp; i--; bp++) {
add_v3_v3(cent, bp->vec);
}
}
}
if (total) {
mul_v3_fl(cent, 1.0f / (float)total);
}
return (total != 0);
}
bool BKE_curve_center_bounds(Curve *cu, float cent[3])
{
float min[3], max[3];
INIT_MINMAX(min, max);
if (BKE_curve_minmax(cu, false, min, max)) {
mid_v3_v3v3(cent, min, max);
return true;
}
return false;
}
void BKE_curve_transform_ex(
Curve *cu, float mat[4][4],
const bool do_keys, const bool do_props, const float unit_scale)
{
Nurb *nu;
BPoint *bp;
BezTriple *bezt;
int i;
for (nu = cu->nurb.first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
i = nu->pntsu;
for (bezt = nu->bezt; i--; bezt++) {
mul_m4_v3(mat, bezt->vec[0]);
mul_m4_v3(mat, bezt->vec[1]);
mul_m4_v3(mat, bezt->vec[2]);
if (do_props) {
bezt->radius *= unit_scale;
}
}
BKE_nurb_handles_calc(nu);
}
else {
i = nu->pntsu * nu->pntsv;
for (bp = nu->bp; i--; bp++) {
mul_m4_v3(mat, bp->vec);
if (do_props) {
bp->radius *= unit_scale;
}
}
}
}
if (do_keys && cu->key) {
KeyBlock *kb;
for (kb = cu->key->block.first; kb; kb = kb->next) {
float *fp = kb->data;
int n = kb->totelem;
for (nu = cu->nurb.first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
for (i = nu->pntsu; i && (n -= KEYELEM_ELEM_LEN_BEZTRIPLE) >= 0; i--) {
mul_m4_v3(mat, &fp[0]);
mul_m4_v3(mat, &fp[3]);
mul_m4_v3(mat, &fp[6]);
if (do_props) {
fp[10] *= unit_scale; /* radius */
}
fp += KEYELEM_FLOAT_LEN_BEZTRIPLE;
}
}
else {
for (i = nu->pntsu * nu->pntsv; i && (n -= KEYELEM_ELEM_LEN_BPOINT) >= 0; i--) {
mul_m4_v3(mat, fp);
if (do_props) {
fp[4] *= unit_scale; /* radius */
}
fp += KEYELEM_FLOAT_LEN_BPOINT;
}
}
}
}
}
}
void BKE_curve_transform(
Curve *cu, float mat[4][4],
const bool do_keys, const bool do_props)
{
float unit_scale = mat4_to_scale(mat);
BKE_curve_transform_ex(cu, mat, do_keys, do_props, unit_scale);
}
void BKE_curve_translate(Curve *cu, float offset[3], const bool do_keys)
{
ListBase *nurb_lb = BKE_curve_nurbs_get(cu);
Nurb *nu;
int i;
for (nu = nurb_lb->first; nu; nu = nu->next) {
BezTriple *bezt;
BPoint *bp;
if (nu->type == CU_BEZIER) {
i = nu->pntsu;
for (bezt = nu->bezt; i--; bezt++) {
add_v3_v3(bezt->vec[0], offset);
add_v3_v3(bezt->vec[1], offset);
add_v3_v3(bezt->vec[2], offset);
}
}
else {
i = nu->pntsu * nu->pntsv;
for (bp = nu->bp; i--; bp++) {
add_v3_v3(bp->vec, offset);
}
}
}
if (do_keys && cu->key) {
KeyBlock *kb;
for (kb = cu->key->block.first; kb; kb = kb->next) {
float *fp = kb->data;
int n = kb->totelem;
for (nu = cu->nurb.first; nu; nu = nu->next) {
if (nu->type == CU_BEZIER) {
for (i = nu->pntsu; i && (n -= KEYELEM_ELEM_LEN_BEZTRIPLE) >= 0; i--) {
add_v3_v3(&fp[0], offset);
add_v3_v3(&fp[3], offset);
add_v3_v3(&fp[6], offset);
fp += KEYELEM_FLOAT_LEN_BEZTRIPLE;
}
}
else {
for (i = nu->pntsu * nu->pntsv; i && (n -= KEYELEM_ELEM_LEN_BPOINT) >= 0; i--) {
add_v3_v3(fp, offset);
fp += KEYELEM_FLOAT_LEN_BPOINT;
}
}
}
}
}
}
void BKE_curve_material_index_remove(Curve *cu, int index)
{
const int curvetype = BKE_curve_type_get(cu);
if (curvetype == OB_FONT) {
struct CharInfo *info = cu->strinfo;
int i;
for (i = cu->len_wchar - 1; i >= 0; i--, info++) {
if (info->mat_nr && info->mat_nr >= index) {
info->mat_nr--;
}
}
}
else {
Nurb *nu;
for (nu = cu->nurb.first; nu; nu = nu->next) {
if (nu->mat_nr && nu->mat_nr >= index) {
nu->mat_nr--;
}
}
}
}
void BKE_curve_material_index_clear(Curve *cu)
{
const int curvetype = BKE_curve_type_get(cu);
if (curvetype == OB_FONT) {
struct CharInfo *info = cu->strinfo;
int i;
for (i = cu->len_wchar - 1; i >= 0; i--, info++) {
info->mat_nr = 0;
}
}
else {
Nurb *nu;
for (nu = cu->nurb.first; nu; nu = nu->next) {
nu->mat_nr = 0;
}
}
}
bool BKE_curve_material_index_validate(Curve *cu)
{
const int curvetype = BKE_curve_type_get(cu);
bool is_valid = true;
if (curvetype == OB_FONT) {
CharInfo *info = cu->strinfo;
const int max_idx = max_ii(0, cu->totcol); /* OB_FONT use 1 as first mat index, not 0!!! */
int i;
for (i = cu->len_wchar - 1; i >= 0; i--, info++) {
if (info->mat_nr > max_idx) {
info->mat_nr = 0;
is_valid = false;
}
}
}
else {
Nurb *nu;
const int max_idx = max_ii(0, cu->totcol - 1);
for (nu = cu->nurb.first; nu; nu = nu->next) {
if (nu->mat_nr > max_idx) {
nu->mat_nr = 0;
is_valid = false;
}
}
}
if (!is_valid) {
DEG_id_tag_update(&cu->id, ID_RECALC_GEOMETRY);
return true;
}
else {
return false;
}
}
void BKE_curve_material_remap(Curve *cu, const unsigned int *remap, unsigned int remap_len)
{
const int curvetype = BKE_curve_type_get(cu);
const short remap_len_short = (short)remap_len;
#define MAT_NR_REMAP(n) \
if (n < remap_len_short) { \
BLI_assert(n >= 0 && remap[n] < remap_len_short); \
n = remap[n]; \
} ((void)0)
if (curvetype == OB_FONT) {
struct CharInfo *strinfo;
int charinfo_len, i;
if (cu->editfont) {
EditFont *ef = cu->editfont;
strinfo = ef->textbufinfo;
charinfo_len = ef->len;
}
else {
strinfo = cu->strinfo;
charinfo_len = cu->len_wchar;
}
for (i = 0; i <= charinfo_len; i++) {
if (strinfo[i].mat_nr > 0) {
strinfo[i].mat_nr -= 1;
MAT_NR_REMAP(strinfo[i].mat_nr);
strinfo[i].mat_nr += 1;
}
}
}
else {
Nurb *nu;
ListBase *nurbs = BKE_curve_editNurbs_get(cu);
if (nurbs) {
for (nu = nurbs->first; nu; nu = nu->next) {
MAT_NR_REMAP(nu->mat_nr);
}
}
}
#undef MAT_NR_REMAP
}
void BKE_curve_rect_from_textbox(const struct Curve *cu, const struct TextBox *tb, struct rctf *r_rect)
{
r_rect->xmin = cu->xof + tb->x;
r_rect->ymax = cu->yof + tb->y + cu->fsize;
r_rect->xmax = r_rect->xmin + tb->w;
r_rect->ymin = r_rect->ymax - tb->h;
}
/* **** Depsgraph evaluation **** */
void BKE_curve_eval_geometry(Depsgraph *depsgraph,
Curve *curve)
{
DEG_debug_print_eval(depsgraph, __func__, curve->id.name, curve);
if (curve->bb == NULL || (curve->bb->flag & BOUNDBOX_DIRTY)) {
BKE_curve_texspace_calc(curve);
}
}
/* Draw Engine */
void (*BKE_curve_batch_cache_dirty_tag_cb)(Curve *cu, int mode) = NULL;
void (*BKE_curve_batch_cache_free_cb)(Curve *cu) = NULL;
void BKE_curve_batch_cache_dirty_tag(Curve *cu, int mode)
{
if (cu->batch_cache) {
BKE_curve_batch_cache_dirty_tag_cb(cu, mode);
}
}
void BKE_curve_batch_cache_free(Curve *cu)
{
if (cu->batch_cache) {
BKE_curve_batch_cache_free_cb(cu);
}
}
|