Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fracture.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9456cbac4be4b0728e95aee88591dbc130e1022d (plain)
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
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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.
 *
 * Copyright (C) 2014 by Martin Felke.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/blenkernel/intern/fracture.c
 *  \ingroup blenkernel
 */

#include <stdio.h>
#include <stdlib.h>

#include "MEM_guardedalloc.h"

#include "BKE_cdderivedmesh.h"
#include "BKE_customdata.h"
#include "BKE_deform.h"
#include "BKE_DerivedMesh.h"
#include "BKE_fracture.h"
#include "BKE_fracture_util.h"
#include "BKE_global.h"
#include "BKE_material.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_pointcache.h"
#include "BKE_rigidbody.h"

#include "BLI_edgehash.h"
#include "BLI_kdtree.h"
#include "BLI_listbase.h"
#include "BLI_math_vector.h"
#include "BLI_mempool.h"
#include "BLI_path_util.h"
#include "BLI_rand.h"
#include "BLI_string.h"
#include "BLI_sort.h"
#include "BLI_task.h"
#include "BLI_utildefines.h"

#include "DNA_scene_types.h"
#include "DNA_fracture_types.h"
#include "DNA_gpencil_types.h"
#include "DNA_group_types.h"
#include "DNA_material_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_modifier_types.h"
#include "DNA_rigidbody_types.h"

#include "bmesh.h"

#include "RBI_api.h"
//#include "GPU_glew.h" /* uaahh, direct access to modelview matrix */

/* debug timing */
#define USE_DEBUG_TIMER

#ifdef USE_DEBUG_TIMER
#include "PIL_time.h"
#endif

#ifdef WITH_VORO
#include "../../../../extern/voro++/src/c_interface.hh"
#endif

/* prototypes */
static void add_shard(FracMesh *fm, Shard *s, float mat[4][4]);
static Shard *parse_cell(cell c);
static void parse_cell_verts(cell c, MVert *mvert, int totvert);
static void parse_cell_polys(cell c, MPoly *mpoly, int totpoly, int *r_totloop);
static void parse_cell_loops(cell c, MLoop *mloop, int totloop, MPoly *mpoly, int totpoly);
static void parse_cell_neighbors(cell c, int *neighbors, int totpoly);
static void fracture_collect_layers(Shard *shard, DerivedMesh *result, int vertstart, int polystart, int loopstart, int edgestart);
static void remove_participants(RigidBodyShardCon *con, MeshIsland *mi);

/* Append the given shard to the end of the shardmap. Shard Map should contain a list of the resulting shards of a fracture
 * operation. Transform all vertices and centroid with the matrix parameter, e.g. to get to local space or world space
 * (TODO need to check callers where what applies */
static void add_shard(FracMesh *fm, Shard *s, float mat[4][4])
{
	MVert *mv;
	int i = 0;

	for (i = 0, mv = s->mvert; i < s->totvert; i++, mv++ )
	{
		mul_m4_v3(mat, mv->co);
	}

	mul_m4_v3(mat, s->centroid);

	BLI_addtail(&fm->shard_map, s);
	s->shard_id = fm->shard_count;
	fm->shard_count++;
}

/* Convert the given Shard via DerivedMesh to BMesh. Used for splitting loose islands via the faster recursive halving
 * mechanism. This executes split_loose in each recursion step only on smaller mesh portions TODO, check callers */
static BMesh *shard_to_bmesh(Shard *s)
{
	DerivedMesh *dm_parent;
	BMesh *bm_parent;
	BMIter iter;
	BMFace *f;

	bm_parent = BM_mesh_create(&bm_mesh_allocsize_default,  &((struct BMeshCreateParams){.use_toolflags = true,}));
	dm_parent = BKE_shard_create_dm(s, true);
	DM_to_bmesh_ex(dm_parent, bm_parent, true);
	BM_mesh_elem_table_ensure(bm_parent, BM_VERT | BM_FACE);

	BM_ITER_MESH (f, &iter, bm_parent, BM_FACES_OF_MESH)
	{
		BM_elem_flag_disable(f, BM_ELEM_SELECT);
	}

	dm_parent->needsFree = 1;
	dm_parent->release(dm_parent);
	dm_parent = NULL;

	return bm_parent;
}

/* Copied from mesh boundbox calculation, perform operation on shard */
static void shard_boundbox(Shard *s, float r_loc[3], float r_size[3])
{
	float min[3], max[3];
	float mloc[3], msize[3];

	if (!r_loc) r_loc = mloc;
	if (!r_size) r_size = msize;

	if (!BKE_shard_calc_minmax(s)) {
		min[0] = min[1] = min[2] = -1.0f;
		max[0] = max[1] = max[2] = 1.0f;
	}

	copy_v3_v3(max, s->max);
	copy_v3_v3(min, s->min);

	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;
}

/* Sort shards by distance of their centroids. Shards closer to each other will be processed first / earlier
 * Again, TODO Check Caller for context */
static int shard_sortdist(const void *s1, const void *s2, void* context)
{
	Shard **sh1 = (Shard **)s1;
	Shard **sh2 = (Shard **)s2;
	cell *sh = (cell*)context;

	float val_a,  val_b;

	if ((*sh1 == NULL) || (*sh2 == NULL)) {
		return -1;
	}

	val_a = len_squared_v3v3(sh->centroid, (*sh1)->centroid);
	val_b = len_squared_v3v3(sh->centroid, (*sh2)->centroid);

	/* sort descending */
	if      (val_a < val_b) return -1;
	else if (val_a > val_b) return 1;
	return 0;
}

/* Sort shards by squared diameter of their bbox as size approximation */
static int shard_sortsize(const void *s1, const void *s2, void* UNUSED(context))
{
	Shard **sh1 = (Shard **)s1;
	Shard **sh2 = (Shard **)s2;

	float size1[3], size2[3], loc[3];
	float val_a,  val_b;

	if ((*sh1 == NULL) || (*sh2 == NULL)) {
		return -1;
	}

	shard_boundbox(*sh1, loc, size1);
	shard_boundbox(*sh2, loc, size2);

	//squared diameter
	val_a = size1[0]*size1[0] + size1[1]*size1[1] + size1[2]*size1[2];
	val_b = size2[0]*size2[0] + size2[1]*size2[1] + size2[2]*size2[2];

	/* sort descending */
	if      (val_a < val_b) return 1;
	else if (val_a > val_b) return -1;
	return 0;
}

/* Checks for missing custom data layers and adds them, if missing */
void* check_add_layer(CustomData *src, CustomData *dst, int type, int totelem, const char* name)
{
	void *layer =  CustomData_get_layer_named(dst, type, name);

	if (!layer) {
		void* orig = NULL;

		if (src) {
			orig = CustomData_get_layer_named(src, type, name);
		}

		if (orig) {
			return CustomData_add_layer_named(dst, type, CD_DUPLICATE, orig, totelem, name);
		}
		else{
			return CustomData_add_layer_named(dst, type, CD_CALLOC, NULL, totelem, name);
		}
	}
	else {
		return layer;
	}
}

/* Copy over customdata layers from DerivedMesh to Shard, additionally add velocity vertex (float) layers (for motionblur (TODO, check Caller ))*/
Shard *BKE_custom_data_to_shard(Shard *s, DerivedMesh *dm)
{
	CustomData_reset(&s->vertData);
	CustomData_reset(&s->loopData);
	CustomData_reset(&s->polyData);
	CustomData_reset(&s->edgeData);

	CustomData_copy(&dm->vertData, &s->vertData, CD_MASK_MDEFORMVERT, CD_DUPLICATE, s->totvert);
	CustomData_copy(&dm->loopData, &s->loopData, CD_MASK_MLOOPUV, CD_DUPLICATE, s->totloop);
	CustomData_copy(&dm->polyData, &s->polyData, CD_MASK_MTEXPOLY, CD_DUPLICATE, s->totpoly);
	CustomData_copy(&dm->edgeData, &s->edgeData, CD_MASK_CREASE | CD_MASK_BWEIGHT | CD_MASK_MEDGE, CD_DUPLICATE, s->totedge);

	//add velocity vertex layers...
	check_add_layer(&dm->vertData, &s->vertData, CD_PROP_FLT, s->totvert, "velX");
	check_add_layer(&dm->vertData, &s->vertData, CD_PROP_FLT, s->totvert, "velY");
	check_add_layer(&dm->vertData, &s->vertData, CD_PROP_FLT, s->totvert, "velZ");

	return s;
}

/* modified from BKE_mesh_center_median */
bool BKE_fracture_shard_center_median(Shard *shard, float cent[3])
{
	int i = shard->totvert;
	MVert *mvert;
	zero_v3(cent);
	for (mvert = shard->mvert; i--; mvert++) {
		add_v3_v3(cent, mvert->co);
	}
	/* otherwise we get NAN for 0 verts */
	if (shard->totvert) {
		mul_v3_fl(cent, 1.0f / (float)shard->totvert);
	}

	return (shard->totvert != 0);
}

/* copied from mesh_evaluate.c */
/**
 * Calculate the volume and volume-weighted centroid of the volume formed by the polygon and the origin.
 * Results will be negative if the origin is "outside" the polygon
 * (+ve normal side), but the polygon may be non-planar with no effect.
 *
 * Method from:
 * - http://forums.cgsociety.org/archive/index.php?t-756235.html
 * - http://www.globalspec.com/reference/52702/203279/4-8-the-centroid-of-a-tetrahedron
 *
 * \note volume is 6x actual volume, and centroid is 4x actual volume-weighted centroid
 * (so division can be done once at the end)
 * \note results will have bias if polygon is non-planar.
 */
static float mesh_calc_poly_volume_and_weighted_centroid(
        const MPoly *mpoly, const MLoop *loopstart, const MVert *mvarray,
        float r_cent[3])
{
	const float *v_pivot, *v_step1;
	float total_volume = 0.0f;

	zero_v3(r_cent);

	v_pivot = mvarray[loopstart[0].v].co;
	v_step1 = mvarray[loopstart[1].v].co;

	for (int i = 2; i < mpoly->totloop; i++) {
		const float *v_step2 = mvarray[loopstart[i].v].co;

		/* Calculate the 6x volume of the tetrahedron formed by the 3 vertices
		 * of the triangle and the origin as the fourth vertex */
		float v_cross[3];
		cross_v3_v3v3(v_cross, v_pivot, v_step1);
		const float tetra_volume = dot_v3v3 (v_cross, v_step2);
		total_volume += tetra_volume;

		/* Calculate the centroid of the tetrahedron formed by the 3 vertices
		 * of the triangle and the origin as the fourth vertex.
		 * The centroid is simply the average of the 4 vertices.
		 *
		 * Note that the vector is 4x the actual centroid so the division can be done once at the end. */
		for (uint j = 0; j < 3; j++) {
			r_cent[j] += tetra_volume * (v_pivot[j] + v_step1[j] + v_step2[j]);
		}

		v_step1 = v_step2;
	}

	return total_volume;
}

/* modified from BKE_mesh_center_centroid */
bool BKE_fracture_shard_center_centroid(Shard *shard, float r_cent[3])
{
	int i = shard->totpoly;
	MPoly *mpoly;
	float poly_volume;
	float total_volume = 0.0f;
	float poly_cent[3];

	zero_v3(r_cent);

	/* calculate a weighted average of polyhedron centroids */
	for (mpoly = shard->mpoly; i--; mpoly++) {
		poly_volume = mesh_calc_poly_volume_and_weighted_centroid(mpoly, shard->mloop + mpoly->loopstart, shard->mvert, poly_cent);

		/* poly_cent is already volume-weighted, so no need to multiply by the volume */
		add_v3_v3(r_cent, poly_cent);
		total_volume += poly_volume;
	}
	/* otherwise we get NAN for 0 polys */
	if (total_volume != 0.0f) {
		/* multipy by 0.25 to get the correct centroid */
		/* no need to divide volume by 6 as the centroid is weighted by 6x the volume, so it all cancels out */
		mul_v3_fl(r_cent, 0.25f / total_volume);
	}

	/* this can happen for non-manifold objects, first fallback to old area based method, then fallback to median there */
	if (!is_finite_v3(r_cent) || total_volume < 0.000001f) {
		return BKE_fracture_shard_center_centroid_area(shard, r_cent);
	}

	copy_v3_v3(shard->centroid, r_cent);
	return (shard->totpoly != 0);
}

/* note, results won't be correct if polygon is non-planar */
/* copied from mesh_evaluate.c */
static float mesh_calc_poly_planar_area_centroid(
        const MPoly *mpoly, const MLoop *loopstart, const MVert *mvarray,
        float r_cent[3])
{
	int i;
	float tri_area;
	float total_area = 0.0f;
	float v1[3], v2[3], v3[3], normal[3], tri_cent[3];

	BKE_mesh_calc_poly_normal(mpoly, loopstart, mvarray, normal);
	copy_v3_v3(v1, mvarray[loopstart[0].v].co);
	copy_v3_v3(v2, mvarray[loopstart[1].v].co);
	zero_v3(r_cent);

	for (i = 2; i < mpoly->totloop; i++) {
		copy_v3_v3(v3, mvarray[loopstart[i].v].co);

		tri_area = area_tri_signed_v3(v1, v2, v3, normal);
		total_area += tri_area;

		mid_v3_v3v3v3(tri_cent, v1, v2, v3);
		madd_v3_v3fl(r_cent, tri_cent, tri_area);

		copy_v3_v3(v2, v3);
	}

	mul_v3_fl(r_cent, 1.0f / total_area);

	return total_area;
}

// old method, keep for now in case new has different results
/* modified from BKE_mesh_center_centroid */
bool BKE_fracture_shard_center_centroid_area(Shard *shard, float cent[3])
{
	int i = shard->totpoly;
	MPoly *mpoly;
	float poly_area;
	float total_area = 0.0f;
	float poly_cent[3];

	zero_v3(cent);

	/* calculate a weighted average of polygon centroids */
	for (mpoly = shard->mpoly; i--; mpoly++) {
		BKE_mesh_calc_poly_center(mpoly, shard->mloop + mpoly->loopstart, shard->mvert, poly_cent);
//		poly_area = BKE_mesh_calc_poly_area(mpoly, shard->mloop + mpoly->loopstart, shard->mvert);
		poly_area = mesh_calc_poly_planar_area_centroid(mpoly, shard->mloop + mpoly->loopstart, shard->mvert,
		                                                poly_cent);
		madd_v3_v3fl(cent, poly_cent, poly_area);
		total_area += poly_area;
	}
	/* otherwise we get NAN for 0 polys */
	if (shard->totpoly) {
		mul_v3_fl(cent, 1.0f / total_area);
	}

	/* zero area faces cause this, fallback to median */
	if (UNLIKELY(!is_finite_v3(cent))) {
		return BKE_fracture_shard_center_median(shard, cent);
	}
	copy_v3_v3(shard->centroid, cent);

	return (shard->totpoly != 0);
}

void BKE_shard_free(Shard *s, bool doCustomData)
{
	if (s->mvert) {
		MEM_freeN(s->mvert);
	}
	if (s->mloop) {
		MEM_freeN(s->mloop);
	}
	if (s->mpoly) {
		MEM_freeN(s->mpoly);
	}
	if (s->medge) {
		MEM_freeN(s->medge);
	}
	if (s->neighbor_ids) {
		MEM_freeN(s->neighbor_ids);
	}
	if (s->cluster_colors) {
		MEM_freeN(s->cluster_colors);
	}

	if (doCustomData) {
		CustomData_free(&s->vertData, s->totvert);
		CustomData_free(&s->loopData, s->totloop);
		CustomData_free(&s->polyData, s->totpoly);
		CustomData_free(&s->edgeData, s->totedge);
	}

	MEM_freeN(s);
}

float BKE_shard_calc_minmax(Shard *shard)
{
	float min[3], max[3], diff[3];
	int i;
	
	INIT_MINMAX(min, max);
	for (i = 0; i < shard->totvert; i++) {
		minmax_v3v3_v3(min, max, shard->mvert[i].co);
	}
	
	copy_v3_v3(shard->min, min);
	copy_v3_v3(shard->max, max);

	sub_v3_v3v3(diff, max, min);
	return len_v3(diff);
}

/* Turns the entire initial mesh into the first shard, covering everything of the mesh.
 * flag marks this as intact shard (still valid in sim, FRACTURED would be a shard if replaced by its children, about to be
 * deleted or already deleted, TODO check caller */
Shard* BKE_create_initial_shard(DerivedMesh *dm)
{
	/* create temporary shard covering the entire mesh */
	Shard *s = BKE_create_fracture_shard(dm->getVertArray(dm), dm->getPolyArray(dm), dm->getLoopArray(dm), dm->getEdgeArray(dm),
	                                     dm->numVertData, dm->numPolyData, dm->numLoopData, dm->numEdgeData, true);
	s = BKE_custom_data_to_shard(s, dm);
	s->flag = SHARD_INTACT;
	s->shard_id = -2;
	return s;
}

/* Deep copy of shard, including customdata layers */
Shard* BKE_fracture_shard_copy(Shard *s)
{
	Shard *t = BKE_create_fracture_shard(s->mvert, s->mpoly, s->mloop, s->medge, s->totvert, s->totpoly, s->totloop, s->totedge, true);
	copy_v3_v3(t->centroid, s->centroid);
	t->neighbor_count = s->neighbor_count;
	t->neighbor_ids = MEM_mallocN(sizeof(int) * t->neighbor_count, __func__);
	memcpy(t->neighbor_ids, s->neighbor_ids, sizeof(int) * t->neighbor_count);
	copy_v3_v3(t->raw_centroid, s->raw_centroid);
	t->raw_volume = s->raw_volume;
	t->shard_id = s->shard_id;
	t->setting_id = s->setting_id;
	t->flag = s->flag;
	t->parent_id = s->parent_id;
	copy_v3_v3(t->max, s->max);
	copy_v3_v3(t->min, s->min);
	copy_v3_v3(t->impact_loc, s->impact_loc);
	copy_v3_v3(t->impact_size, s->impact_size);
	//TODO, maybe cluster colors too ?

	CustomData_reset(&t->vertData);
	CustomData_reset(&t->loopData);
	CustomData_reset(&t->polyData);
	CustomData_reset(&t->edgeData);

	CustomData_copy(&s->vertData, &t->vertData, CD_MASK_MDEFORMVERT, CD_DUPLICATE, s->totvert);
	CustomData_copy(&s->loopData, &t->loopData, CD_MASK_MLOOPUV, CD_DUPLICATE, s->totloop);
	CustomData_copy(&s->polyData, &t->polyData, CD_MASK_MTEXPOLY, CD_DUPLICATE, s->totpoly);
	CustomData_copy(&s->edgeData, &t->edgeData, CD_MASK_CREASE | CD_MASK_BWEIGHT | CD_MASK_MEDGE, CD_DUPLICATE, s->totedge);

	return t;
}


/*access shard directly by index / id*/
Shard *BKE_shard_by_id(FracMesh *mesh, ShardID id, DerivedMesh *dm) {
	if (/*(id < mesh->shard_count) && */(id >= 0)) {
		//return mesh->shard_map[id];
		//return (Shard *)BLI_findlink(&mesh->shard_map, id);
		Shard *s = mesh->shard_map.first;
		while (s)
		{
			if (s->shard_id == id)
			{
				return s;
			}
			s = s->next;
		}

		return NULL;
	}
	else if (id == -1 && dm != NULL)
	{
		/* create temporary shard covering the entire mesh */
		return BKE_create_initial_shard(dm);
	}
	
	return NULL;
}

/* gets the min and max if shard exists, if it was the intial shard, delete it here (why, check caller, TODO */
bool BKE_get_shard_minmax(FracMesh *mesh, ShardID id, float min_r[3], float max_r[3], DerivedMesh *dm)
{
	Shard *shard = BKE_shard_by_id(mesh, id, dm);
	if (shard != NULL) {
		BKE_shard_calc_minmax(shard);
		copy_v3_v3(min_r, shard->min);
		copy_v3_v3(max_r, shard->max);

		if (shard->shard_id == -2)
		{
			BKE_shard_free(shard, true);
		}

		return true;
	}
	return false;
}

/* Create a shard from Mesh / DerivedMesh elements, in most cases copy is true, means it will duplicate everything
 * TODO is there a case with copy = false still at all ? */
Shard *BKE_create_fracture_shard(MVert *mvert, MPoly *mpoly, MLoop *mloop, MEdge* medge,  int totvert, int totpoly,
                                 int totloop, int totedge, bool copy)
{
	Shard *shard = MEM_mallocN(sizeof(Shard), __func__);
	shard->totvert = totvert;
	shard->totpoly = totpoly;
	shard->totloop = totloop;
	shard->totedge = totedge;
	shard->cluster_colors = NULL;
	shard->neighbor_ids = NULL;
	shard->neighbor_count = 0;
	
	if (copy) {
		shard->mvert = MEM_mallocN(sizeof(MVert) * totvert, "shard vertices");
		shard->mpoly = MEM_mallocN(sizeof(MPoly) * totpoly, "shard polys");
		shard->mloop = MEM_mallocN(sizeof(MLoop) * totloop, "shard loops");
		shard->medge = MEM_mallocN(sizeof(MEdge) * totedge, "shard edges");
		memcpy(shard->mvert, mvert, sizeof(MVert) * totvert);
		memcpy(shard->mpoly, mpoly, sizeof(MPoly) * totpoly);
		memcpy(shard->mloop, mloop, sizeof(MLoop) * totloop);
		memcpy(shard->medge, medge, sizeof(MEdge) * totedge);
	}
	else {
		shard->mvert = mvert;
		shard->mpoly = mpoly;
		shard->mloop = mloop;
		shard->medge = medge;
	}

	shard->shard_id = -1;
	shard->setting_id = -1;
	shard->parent_id = -1;

	shard->flag = SHARD_INTACT;
	BKE_shard_calc_minmax(shard);

	BKE_fracture_shard_center_centroid_area(shard, shard->centroid);
	copy_v3_v3(shard->raw_centroid, shard->centroid);
	zero_v3(shard->impact_loc);
	shard->impact_size[0] = 1.0f;
	shard->impact_size[1] = 1.0f;
	shard->impact_size[2] = 1.0f;

	return shard;
}

/* create the FracMesh container where the shards live */
FracMesh *BKE_create_fracture_container(void)
{
	FracMesh *fmesh;
	
	fmesh = MEM_mallocN(sizeof(FracMesh), __func__);
	fmesh->shard_map.first = NULL;
	fmesh->shard_map.last = NULL;
	fmesh->shard_count = 0;
	fmesh->cancel = 0; // remainder of threading attempt with Job system, to be able to cancel long running fractures
	fmesh->running = 0; // remainder of threading attempt with Job system
	fmesh->progress_counter = 0;
	fmesh->last_shards = NULL;
	fmesh->last_shard_tree = NULL;
	fmesh->last_expected_shards = 0;
	
	return fmesh;
}

/* Fast bisect is a method which just uses one random face of a voronoi cell as cutter plane; this will cut the mesh in two halves;
 * and keep both halves if the cut was successful; eg the mesh was not missed etc */
static void handle_fast_bisect(FracMesh *fm, int expected_shards, int algorithm, BMesh** bm_parent, float obmat[4][4],
                               float centroid[3], short inner_material_index, int parent_id, Shard **tempshards, Shard ***tempresults,
                               char uv_layer[64], cell* cells, float fac, Shard* parent)
{
	int i = 0, index = 0;
	float factor = 1 - fac;

	float dim[3];
	sub_v3_v3v3(dim, parent->max, parent->min);

	for (i = 0; i < expected_shards; i++) {
		Shard *s = NULL;
		Shard *s2 = NULL;
		Shard *t;
		float vec[3];
		int max_axis;

		if (fm->cancel == 1) {
			break;
		}

		printf("Processing shard: %d\n", i);
		t = tempshards[i];

		if (t != NULL) {
			t->parent_id = parent_id;
			t->flag = SHARD_INTACT;
		}

		if (t == NULL || t->totvert == 0 || t->totloop == 0 || t->totpoly == 0) {
			/* invalid shard, stop parsing*/
			continue;
		}

		//index = (int)(BLI_frand() * (t->totpoly - 1));

		if (index > (t->totpoly - 1)){
			index = 0;
		}

		//make a random vector (interpret as cutter plane)
		vec[0] = BLI_frand() * 2 - 1;
		vec[1] = BLI_frand() * 2 - 1;
		vec[2] = BLI_frand() * 2 - 1;

		//multiply two minor dimensions with a factor to emphasize the max dimension
		/* This is an attempt to actually get axial cuts, means 90 degree "straight" cuts, not angled */
		max_axis = axis_dominant_v3_single(dim);
		switch (max_axis) {
			case 0:
				vec[1] *= factor;
				vec[2] *= factor;
				break;
			case 1:
				vec[0] *= factor;
				vec[2] *= factor;
				break;
			case 2:
				vec[0] *= factor;
				vec[1] *= factor;
				break;
		}

		printf("Bisecting cell %d...\n", i);
		printf("Bisecting cell %d...\n", i + 1);

		s = BKE_fracture_shard_bisect(*bm_parent, t, obmat, algorithm == MOD_FRACTURE_BISECT_FAST_FILL,
		                              false, true, index, centroid, inner_material_index, uv_layer, NULL, vec);
		s2 = BKE_fracture_shard_bisect(*bm_parent, t, obmat, algorithm == MOD_FRACTURE_BISECT_FAST_FILL,
		                               true, false, index, centroid, inner_material_index, uv_layer, NULL, vec);

		index++;

		if (s == NULL || s2 == NULL) {
			printf("Shard missed....\n");
			continue;
		}

		if (s != NULL && s2 != NULL && tempresults != NULL) {
			int j = 0;

			fm->progress_counter++;

			s->parent_id = parent_id;
			s->flag = SHARD_INTACT;

			s2->parent_id = parent_id;
			s2->flag = SHARD_INTACT;

			if (*bm_parent != NULL) {
				BM_mesh_free(*bm_parent);
				*bm_parent = NULL;
			}

			(*tempresults)[i] = s;
			(*tempresults)[i + 1] = s2;

			//BLI_qsort_r(*tempresults, i + 1, sizeof(Shard *), shard_sortdist, &(cells[i]));

			/*prefer bigger shards for cutting here, so sort bigger ones to the front of the array */
			BLI_qsort_r(*tempresults, i + 1, sizeof(Shard *), shard_sortsize, &(cells[i]));

			while ((*tempresults)[j] == NULL && j < (i + 1)) {
				/* ignore invalid shards */
				j++;
			}

			/* continue splitting if not all expected shards exist yet */
			if ((i + 2) < expected_shards) {
				*bm_parent = shard_to_bmesh((*tempresults)[j]);
				copy_v3_v3(centroid, (*tempresults)[j]->centroid);
				sub_v3_v3v3(dim, (*tempresults)[j]->max, (*tempresults)[j]->min);

				BKE_shard_free((*tempresults)[j], true);
				(*tempresults)[j] = NULL;
			}
			i++;
		}
	}
}

/* Fractal boolean, this works with Quad Grids as cutter planes, the higher the num_cuts, the more smaller quads (the slower)
 * Grid subdivision is supposed to subdivide the inner faces of resulting geometry. Cutting happens plane-based, so halve, halve the halves and so on.
 * Actually the cutter planes are already subdivided, and can have fractal displacement on them. They will cut the geometry into 2 matching displaced halves.
 * (using boolean cuts). Note: has still issues with proper autohide etc, TODO */
static void handle_boolean_fractal(Shard* p, Shard* t, int expected_shards, DerivedMesh* dm_parent, Object *obj, short inner_material_index,
                                   int num_cuts, float fractal, int num_levels, bool smooth,int parent_id, int* i, Shard ***tempresults,
                                   DerivedMesh **dm_p, char uv_layer[64], int thresh, float fac)
{
	/* physics shard and fractalized shard, so we need to booleanize twice */
	/* and we need both halves, so twice again */
	Shard *s2 = NULL;
	Shard *s = NULL;
	int index = 0;
	int max_retries = 3;
	float factor = 1 - fac;

	/*continue with "halves", randomly*/
	if ((*i) == 0) {
		*dm_p = CDDM_copy(dm_parent);
	}

	while (s == NULL || s2 == NULL) {

		float radius;
		float size[3];
		float quat[4];
		float loc[3], vec[3];
		float min[3], max[3];
		float one[3] = {1.0f, 1.0f, 1.0f};
		float matrix[4][4];
		int max_axis;

		/*make a plane as cutter*/
//		shard_boundbox(p, loc, size);
		INIT_MINMAX(min, max);
		(*dm_p)->getMinMax(*dm_p, min, max);

		mid_v3_v3v3(loc, min, max);
		size[0] = (max[0] - min[0]) / 2.0f;
		size[1] = (max[1] - min[1]) / 2.0f;
		size[2] = (max[2] - min[2]) / 2.0f;

		radius = sqrt(size[0]*size[0] + size[1]*size[1] + size[2]*size[2]);

		vec[0] = BLI_frand() * 2 - 1;
		vec[1] = BLI_frand() * 2 - 1;
		vec[2] = BLI_frand() * 2 - 1;

		//multiply two minor dimensions with a factor to emphasize the max dimension
		/* same idea as with fast bisect, try to get 90 degree angles between the cutter planes */
		max_axis = axis_dominant_v3_single(size);
		switch (max_axis) {
			case 0:
				vec[1] *= factor;
				vec[2] *= factor;
				break;
			case 1:
				vec[0] *= factor;
				vec[2] *= factor;
				break;
			case 2:
				vec[0] *= factor;
				vec[1] *= factor;
				break;
		}

		//printf("(%f %f %f) (%f %f %f) \n", size[0], size[1], size[2], eul[0], eul[1], eul[2]);*/
		//loc_eul_size_to_mat4(matrix, loc, vec, one);
		vec_to_quat(quat, vec, OB_POSZ, OB_POSX);
		loc_quat_size_to_mat4(matrix, loc, quat, one);

		/*visual shards next, fractalized cuts */
		s = BKE_fracture_shard_boolean(obj, *dm_p, t, inner_material_index, num_cuts,fractal, &s2, matrix, radius, smooth, num_levels, uv_layer, thresh);

		if (index < max_retries)
		{
			printf("Retrying...%d\n", index);
			index++;
		}
		else if (s == NULL || s2 == NULL)
		{
			(*i)++;
			break;
		}
	}

	if ((s != NULL) && (s2 != NULL)) {
		int j = 0;

		s->parent_id = parent_id;
		s->flag = SHARD_INTACT;
		(*tempresults)[(*i)+1] = s;

		s2->parent_id = parent_id;
		s2->flag = SHARD_INTACT;
		(*tempresults)[*i] = s2;

		/* and also prefer bigger shards first */
		BLI_qsort_r(*tempresults, (*i) + 1, sizeof(Shard *), shard_sortsize, i);
		while ((*tempresults)[j] == NULL && j < ((*i) + 1)) {
			/* ignore invalid shards */
			j++;
		}

		/* continue splitting if not all expected shards exist yet */
		if (((*i) + 2) < expected_shards) {

			Shard *p = (*tempresults)[j];

			if (*dm_p != NULL) {
				(*dm_p)->needsFree = 1;
				(*dm_p)->release(*dm_p);
				*dm_p = NULL;
			}

			if (p != NULL) {
				*dm_p = BKE_shard_create_dm(p, true);
				BKE_shard_free((*tempresults)[j], true);
				(*tempresults)[j] = NULL;
			}
		}
		(*i)++; //XXX remember to "double" the shard amount....
	}
}

/* Use bisect instead of boolean cuts for intersecting with the original geometry, to allow also nonmanifold geometry. Note, filling methods
 * might be less reliable than with boolean cuts. This method also handles boolean cuts, should better be properly split up TODO*/
static bool handle_boolean_bisect(FracMesh *fm, Object *obj, int expected_shards, int algorithm, int parent_id, Shard **tempshards,
                                  DerivedMesh *dm_parent, BMesh* bm_parent, float obmat[4][4], short inner_material_index, int num_cuts,
                                  int num_levels, float fractal, int *i, bool smooth, Shard*** tempresults, DerivedMesh **dm_p, char uv_layer[64],
                                  KDTree *preselect_tree, int thresh, Shard* p, float fac)
{
	Shard *s = NULL, *t = NULL;
	if (fm->cancel == 1)
		return true;

	t = tempshards[*i];

	if (t != NULL) {
		t->parent_id = parent_id;
		t->flag = SHARD_INTACT;
	}

	if (t == NULL || t->totvert == 0 || t->totloop == 0 || t->totpoly == 0) {
		/* invalid shard, stop parsing */
		return true;
	}

	printf("Processing shard: %d\n", *i);

	/* XXX TODO, need object for material as well, or atleast a material index... */
	if (algorithm == MOD_FRACTURE_BOOLEAN) {
		s = BKE_fracture_shard_boolean(obj, dm_parent, t, inner_material_index, 0, 0.0f, NULL, NULL, 0.0f, false, 0, uv_layer, thresh);
	}
	else if (algorithm == MOD_FRACTURE_BOOLEAN_FRACTAL) {
		handle_boolean_fractal(p, t, expected_shards, dm_parent, obj, inner_material_index, num_cuts, fractal,
		                       num_levels, smooth, parent_id, i, tempresults, dm_p, uv_layer, thresh, fac);
	}
	else if (algorithm == MOD_FRACTURE_BISECT || algorithm == MOD_FRACTURE_BISECT_FILL) {
		float co[3] = {0, 0, 0}, quat[4] =  {1, 0, 0, 0};
		printf("Bisecting cell %d...\n", *i);
		s = BKE_fracture_shard_bisect(bm_parent, t, obmat, algorithm == MOD_FRACTURE_BISECT_FILL, false, true, -1, co, inner_material_index, uv_layer,
		                              preselect_tree, quat);
	}
	else {
		/* do not fracture case */
		s = t;
	}

	if ((s != NULL) && (algorithm != MOD_FRACTURE_BOOLEAN_FRACTAL)) {
		s->parent_id = parent_id;
		s->flag = SHARD_INTACT;

		(*tempresults)[*i] = s;
	}

	fm->progress_counter++;
	return false;
}

/* Here attempts are made to keep track of unchanged raw cells which potentially will lead to unchanged intersected cells, avoiding expensive
 * boolean or bisect cuts. Mainly should be used together with "Mouse based" fracture, which interactively fractures on mouse click / drag (done via the addon)
 * TODO, better comment ?)
 * To determine shards being the same, their centroid positions and volumes are compared as approximation. Then skip and deletemaps to determine which shards dont
 * refractures respectively which shards need to be removed are created */

static void do_prepare_cells(FracMesh *fm, cell *cells, int expected_shards, int algorithm, Shard *p, float (*centroid)[3],
                             DerivedMesh **dm_parent, BMesh** bm_parent, Shard ***tempshards, Shard ***tempresults, int override_count,
                             FractureModifierData *fmd)
{
	int i;
	Shard *s = NULL;
	int *skipmap = MEM_callocN(sizeof(int) * expected_shards, "skipmap");
	int *deletemap = MEM_callocN(sizeof(int) * fm->shard_count, "deletemap");

	/* again, multiple variants are handled here, split up ?, TODO */
	if ((algorithm == MOD_FRACTURE_BOOLEAN) || (algorithm == MOD_FRACTURE_BOOLEAN_FRACTAL)) {
		MPoly *mpoly, *mp;
		int totpoly, po;

		*dm_parent = BKE_shard_create_dm(p, true);
		mpoly = (*dm_parent)->getPolyArray(*dm_parent);
		totpoly = (*dm_parent)->getNumPolys(*dm_parent);
		for (po = 0, mp = mpoly; po < totpoly; po++, mp++) {
			mp->flag &= ~ME_FACE_SEL;
		}
	}
	else if (algorithm == MOD_FRACTURE_BISECT || algorithm == MOD_FRACTURE_BISECT_FILL ||
	         algorithm == MOD_FRACTURE_BISECT_FAST || algorithm == MOD_FRACTURE_BISECT_FAST_FILL)
	{
		*bm_parent = shard_to_bmesh(p);
		copy_v3_v3(*centroid, p->centroid);
	}

	if (algorithm == MOD_FRACTURE_BISECT_FAST ||
	    algorithm == MOD_FRACTURE_BISECT_FAST_FILL ||
	    algorithm == MOD_FRACTURE_BOOLEAN_FRACTAL)
	{
		copy_vn_i(deletemap, fm->shard_count, 1);
	}

	if (fm->last_shard_tree)
	{
		copy_vn_i(deletemap, fm->shard_count, 1);
		copy_vn_i(skipmap, expected_shards, 1);

		for (i = 0; i < expected_shards; i++)
		{
			KDTreeNearest n;
			int l, j;
			float max = 0;
			for (l = 0; l < cells[i].totpoly; l++)
			{
				int index = cells[i].neighbors[l];
				if (index > -1)
				{
					float dist = len_squared_v3v3(cells[index].centroid, cells[i].centroid);
					if (dist > max)
					{
						max = dist;
					}
				}
			}

			j = BLI_kdtree_find_nearest(fm->last_shard_tree, cells[i].centroid, &n);
			if (j > -1)
			{
				float epsilon = 0.00001;
				Shard *t = fm->last_shards[j];
				if (t != NULL && n.dist < max)
				{
					if (n.dist < epsilon) {
						if ((fabsf(cells[i].volume - t->raw_volume) < epsilon))
						{
							deletemap[j] = false;
						}
						else
						{
							skipmap[i] = false;
						}
					}
					else
					{
						skipmap[i] = false;
					}
				}
				else
				{
					skipmap[i] = false;
				}
			}
			else {
				skipmap[i] = true;
			}
		}
	}

	//BLI_lock_thread(LOCK_CUSTOM1);
	//skipping /deletion pass


	for (i = 0; i < expected_shards; i++)
	{
		if (fm->cancel == 1) {
			break;
		}

		if (skipmap[i])
		{
			printf("Skipping shard: %d\n", i);
			(*tempshards)[i] = NULL;
		}
		else
		{
			printf("Parsing shard: %d\n", i);
			s = parse_cell(cells[i]);
			(*tempshards)[i] = s;
		}

		(*tempresults)[i] = NULL;

		fm->progress_counter++;
	}

	for (i = 0; i < fm->shard_count; i++)
	{
		if (deletemap[i] && fm->last_shards)
		{
			Shard *t = fm->last_shards[i];

			if (!t)
				continue;

			//seems this override count was a totally wrong thought, just passing -1 or 0 here... hmm
			if ((override_count == -1) || ((override_count > 0) && (i < override_count+1)))
			{
				printf("Deleting shard: %d %d %d\n", i, t->shard_id, t->setting_id);
				BLI_remlink_safe(&fm->shard_map, t);
				BKE_shard_free(t, false);
				fm->last_shards[i] = NULL;
			}
			else
			{
				printf("NOT Deleting shard: %d %d %d\n", i, t->shard_id, t->setting_id);
			}
		}
	}

	if (override_count > -1) {
		printf("Deleting island shards!\n");
		while (fmd->islandShards.first) {
			Shard *sh = fmd->islandShards.first;
			if (sh) {
				BLI_remlink_safe(&fmd->islandShards, sh);
				BKE_shard_free(sh, false);
			}
		}
	}
	//BLI_unlock_thread(LOCK_CUSTOM1); //yuck what was that ? old attempt of what ?

	fm->last_expected_shards = expected_shards;

	MEM_freeN(skipmap);
	MEM_freeN(deletemap);
}


/* parse the voro++ cell data */
/* voronoi cell data is just vertex positions, and face index arrays. This allows to construct ngon-faced raw cells out of
 * the returned data from voro++. Shard tree was used (?) to keep track of last positions of shards in KDTree (to find it quickly)
 * TODO, need to split this very long and clunky function */

//static ThreadMutex prep_lock = BLI_MUTEX_INITIALIZER;
static void parse_cells(cell *cells, int expected_shards, ShardID parent_id, FracMesh *fm, int algorithm, Object *obj, DerivedMesh *dm,
                        short inner_material_index, float mat[4][4], int num_cuts, float fractal, bool smooth, int num_levels, int mode,
                        bool reset, int active_setting, int num_settings, char uv_layer[64], bool threaded, float thresh, int override_count,
                        float factor)
{
	/*Parse voronoi raw data*/
	int i = 0, j = 0, count = 0;
	Shard *p = BKE_shard_by_id(fm, parent_id, dm); // *t;
	float obmat[4][4]; /* use unit matrix for now */
	float centroid[3], pcentroid[3] = {0,0,0};
	BMesh *bm_parent = NULL;
	DerivedMesh *dm_parent = NULL;
	DerivedMesh *dm_p = NULL;
	Shard **tempshards;
	Shard **tempresults;
	bool do_tree = (algorithm != MOD_FRACTURE_BISECT_FAST &&
					algorithm != MOD_FRACTURE_BISECT_FAST_FILL &&
					algorithm != MOD_FRACTURE_BOOLEAN_FRACTAL);
	FractureModifierData *fmd = (FractureModifierData*) modifiers_findByType(obj, eModifierType_Fracture);

	if (p == NULL || reset)
	{
		if (fm->last_shard_tree)
		{
			BLI_kdtree_free(fm->last_shard_tree);
			fm->last_shard_tree = NULL;
		}

		if (fm->last_shards)
		{
			MEM_freeN(fm->last_shards);
			fm->last_shards = NULL;
		}

		if (!p)
			return;
	}

	if (mode == MOD_FRACTURE_PREFRACTURED && reset && !threaded)
	{
		while (fm->shard_map.first)
		{
			Shard *t = fm->shard_map.first;
			BLI_remlink_safe(&fm->shard_map, t);
			printf("Resetting shard: %d\n", t->shard_id);
			BKE_shard_free(t, true);
		}
	}

	if (mode == MOD_FRACTURE_PREFRACTURED && !reset)
	{
		//rebuild tree
		if (!fm->last_shard_tree && mode == MOD_FRACTURE_PREFRACTURED)
		{
			Shard *t;
			int ti = 0;
			count = BLI_listbase_count(&fm->shard_map);
			fm->shard_count = count;
			if (do_tree)
			{
				fm->last_shard_tree = BLI_kdtree_new(fm->shard_count);
			}

			fm->last_shards = MEM_callocN(sizeof(Shard*) * fm->shard_count, "last_shards");

			//fill tree from current shardmap
			for (t = fm->shard_map.first; t; t = t->next)
			{
				t->flag &=~ (SHARD_SKIP | SHARD_DELETE);

				if (do_tree)
				{
					BLI_kdtree_insert(fm->last_shard_tree, ti, t->raw_centroid);
				}

				fm->last_shards[ti] = t;
				ti++;
			}

			if (do_tree)
			{
				BLI_kdtree_balance(fm->last_shard_tree);
			}

			p->flag |= SHARD_DELETE;
		}
	}
	else
	{
		fm->last_shard_tree = NULL;
		fm->last_shards = NULL;
	}

	tempshards = MEM_callocN(sizeof(Shard *) * expected_shards, "tempshards");
	tempresults = MEM_callocN(sizeof(Shard *) * expected_shards, "tempresults");

	p->flag = 0;
	p->flag |= SHARD_FRACTURED;

	if (mode == MOD_FRACTURE_DYNAMIC)
	{
		copy_v3_v3(pcentroid, p->centroid);
		parent_id = p->shard_id;
		//remove parent shard from map as well
		BLI_remlink(&fm->shard_map, p);
		fm->shard_count--;
		p->shard_id = -2;
	}

	unit_m4(obmat);
	do_prepare_cells(fm, cells, expected_shards, algorithm, p, &centroid, &dm_parent, &bm_parent, &tempshards, &tempresults, override_count, fmd);

	if (fm->last_shard_tree)
	{
		BLI_kdtree_free(fm->last_shard_tree);
		fm->last_shard_tree = NULL;
	}

	if (fm->last_shards)
	{
		MEM_freeN(fm->last_shards);
		fm->last_shards = NULL;
	}

	if (algorithm != MOD_FRACTURE_BISECT_FAST && algorithm != MOD_FRACTURE_BISECT_FAST_FILL) {
		int totvert = p->totvert;
		MVert *mvert = p->mvert;

		KDTree *preselect_tree = BLI_kdtree_new(totvert);
		for (i = 0; i < totvert; i++) {
			BLI_kdtree_insert(preselect_tree, i, mvert[i].co);
		}

		BLI_kdtree_balance(preselect_tree);

		if (algorithm == MOD_FRACTURE_BOOLEAN_FRACTAL)
		{
			//attempt to have some variance atleast here too
			BLI_srandom(fmd->point_seed);
		}

		if ((algorithm == MOD_FRACTURE_BOOLEAN) && !threaded)
		{
			#pragma omp parallel for
			for (i = 0; i < expected_shards; i++)	{
				handle_boolean_bisect(fm, obj, expected_shards, algorithm, parent_id, tempshards, dm_parent,
										bm_parent, obmat, inner_material_index, num_cuts, num_levels, fractal,
										&i, smooth, &tempresults, &dm_p, uv_layer, preselect_tree, thresh, p,
										fmd->orthogonality_factor);
			}
		}
		else {
			for (i = 0; i < expected_shards; i++)	{
				handle_boolean_bisect(fm, obj, expected_shards, algorithm, parent_id, tempshards, dm_parent,
										bm_parent, obmat, inner_material_index, num_cuts, num_levels, fractal,
										&i, smooth, &tempresults, &dm_p, uv_layer, preselect_tree, thresh, p,
										fmd->orthogonality_factor);
			}
		}

		BLI_kdtree_free(preselect_tree);
	}
	else {

		if (expected_shards == 1)
		{
			/* do not fracture case */
			tempresults[0] = p;
			p->shard_id = -1;
		}
		else
		{
			handle_fast_bisect(fm, expected_shards, algorithm, &bm_parent, obmat, centroid, inner_material_index, parent_id,
			                   tempshards, &tempresults, uv_layer, cells, factor, p);
		}
	}

	if (bm_parent != NULL) {
		BM_mesh_free(bm_parent);
		bm_parent = NULL;
	}

	if (dm_parent != NULL) {
		dm_parent->needsFree = 1;
		dm_parent->release(dm_parent);
		dm_parent = NULL;
	}

	/*only used with fractal, and is doubly freed in case of 1 shard (doubled) */
	if (dm_p != NULL && expected_shards > 2) {
		dm_p->needsFree = 1;
		dm_p->release(dm_p);
		dm_p = NULL;
	}

	if (p)
	{
		BLI_remlink_safe(&fm->shard_map, p);
		BKE_shard_free(p, true);
		p = NULL;
	}

	fm->shard_count = 0; /* may be not matching with expected shards, so reset... did increment this for
	                      *progressbar only */

	//keep empty ids... need to catch this later
	if (mode == MOD_FRACTURE_DYNAMIC || active_setting > -1)
	{
		j = 1;

		if (fm->shard_map.last)
		{
			j += ((Shard*)(fm->shard_map.last))->shard_id;
		}
	}
	else
	{
		j = 1;
	}

	for (i = 0; i < expected_shards; i++) {
		Shard *s = tempresults[i];
		Shard *t = tempshards[i];

		if (s != NULL) {
			add_shard(fm, s, mat);
			s->shard_id += j;
			s->parent_id = parent_id;
			s->setting_id = active_setting;
			//printf("ADDED: %d %d %d\n", i, j, s->shard_id);
			if (parent_id > -1)
			{
				int si = 0;
				MVert *v;

				sub_v3_v3(s->centroid, pcentroid);
				for (si = 0, v = s->mvert; si < s->totvert; si++, v++)
				{
					sub_v3_v3(v->co, pcentroid);
				}
			}
		}

		if (t != NULL && t != s) {
			BKE_shard_free(t, false);
		}
	}

	if (fm->shard_count == 0)
	{
		//might happen if all has been skipped, but this distracts the halving method (thinks shardmap is empty)
		//so better correct this here
		fm->shard_count = BLI_listbase_count(&fm->shard_map);
	}

	MEM_freeN(tempshards);
	MEM_freeN(tempresults);
}

static Shard *parse_cell(cell c)
{
	Shard *s;
	MVert *mvert = NULL;
	MPoly *mpoly = NULL;
	MLoop *mloop = NULL;
	int *neighbors = NULL;
	int totpoly = 0, totloop = 0, totvert = 0;
	float centr[3];

	totvert = c.totvert;
	if (totvert > 0) {
		mvert = MEM_callocN(sizeof(MVert) * totvert, __func__);
		parse_cell_verts(c, mvert, totvert);
	}

	totpoly = c.totpoly;
	if (totpoly > 0) {
		mpoly = MEM_callocN(sizeof(MPoly) * totpoly, __func__);
		parse_cell_polys(c, mpoly, totpoly, &totloop);
	}
	else
		totloop = 0;

	if (totloop > 0) {
		mloop = MEM_callocN(sizeof(MLoop) * totloop, __func__);
		parse_cell_loops(c, mloop, totloop, mpoly, totpoly);
	}

	if (totpoly > 0) {
		neighbors = MEM_callocN(sizeof(int) * totpoly, __func__);
		parse_cell_neighbors(c, neighbors, totpoly);
	}

	copy_v3_v3(centr, c.centroid);

	/* HERE we take ownership of the generated Vert, Loop, Polydata in the shards, no copy */
	s = BKE_create_fracture_shard(mvert, mpoly, mloop, NULL, totvert, totpoly, totloop, 0, false);

	//s->flag &= ~(SHARD_SKIP | SHARD_DELETE);
	s->neighbor_ids = neighbors;
	s->neighbor_count = totpoly;
	copy_v3_v3(s->centroid, centr);
	copy_v3_v3(s->raw_centroid, centr);
	s->raw_volume = c.volume;

	return s;
}

static void parse_cell_verts(cell c, MVert *mvert, int totvert)
{
	int i;

	for (i = 0; i < totvert; i++) {
		float *co = mvert[i].co;
		copy_v3_v3(co, c.verts[i]);
	}
}

static void parse_cell_polys(cell c, MPoly *mpoly, int totpoly, int *r_totloop)
{
	int i;
	int totloop = 0;

	for (i = 0; i < totpoly; ++i) {
		int numloop;

		numloop = c.poly_totvert[i];

		mpoly[i].loopstart = totloop;
		mpoly[i].totloop = numloop;

		totloop += numloop;
	}

	*r_totloop = totloop;
}

static void parse_cell_loops(cell c, MLoop *mloop, int UNUSED(totloop), MPoly *mpoly, int totpoly)
{
	int i, k;

	for (i = 0; i < totpoly; ++i) {
		int loopstart = mpoly[i].loopstart;
		int numloop = mpoly[i].totloop;

		for (k = 0; k < numloop; ++k) {
			int index;

			index = c.poly_indices[i][k];

			/* note: invert vertex order here,
			 * otherwise normals are pointing inward
			 */
			mloop[loopstart + (numloop - 1) - k].v = index;
		}
	}
}

static void parse_cell_neighbors(cell c, int *neighbors, int totpoly)
{
	int i;

	for (i = 0; i < totpoly; i++) {
		int n;
		n = c.neighbors[i];
		neighbors[i] = n;
	}
}

/* This is necessary for line based greasepencil fracture. Worked only half, subject to major overhaul in 2.8
 * Can be probably added later or left out. Basically it turns a gp stroke into a mesh cutter plane. */
static void stroke_to_faces(FractureModifierData *fmd, BMesh** bm, bGPDstroke *gps, int inner_material_index)
{
	BMVert *lastv1 = NULL;
	BMVert *lastv2 = NULL;
	int p = 0;
	float thresh = (float)fmd->grease_decimate / 100.0f;
	float half[3] = {0, 0, 1};

	for (p = 0; p < gps->totpoints; p++) {

		if ((BLI_frand() < thresh) || (p == 0) || (p == gps->totpoints-1)) {
			BMVert *v1, *v2;
			float point[3] = {0, 0, 0};

			point[0] = gps->points[p].x;
			point[1] = gps->points[p].y;
			point[2] = gps->points[p].z;

			v1 = BM_vert_create(*bm, point, NULL, 0);

			if (lastv1)
			{
				BMFace* f;
				float nvec[3] = {0.0f, 0.0f, 0.0f}, co1[3], co2[3];

				/*also "extrude" this along the normal, no...use global axises instead*/
				if (fmd->cutter_axis == MOD_FRACTURE_CUTTER_X)
				{
					nvec[0] = 1.0f;
					nvec[1] = 0.0f;
					nvec[2] = 0.0f;
				}

				if (fmd->cutter_axis == MOD_FRACTURE_CUTTER_Y)
				{
					nvec[0] = 0.0f;
					nvec[1] = 1.0f;
					nvec[2] = 0.0f;
				}

				if (fmd->cutter_axis == MOD_FRACTURE_CUTTER_Z)
				{
					nvec[0] = 0.0f;
					nvec[1] = 0.0f;
					nvec[2] = 1.0f;
				}

				mul_v3_fl(nvec, fmd->grease_offset);
				mul_v3_v3fl(half, nvec, 0.5f);

				add_v3_v3v3(co1, v1->co, nvec);
				v2 = BM_vert_create(*bm, co1, NULL, 0);

				if (!lastv2)
				{
					add_v3_v3v3(co2, lastv1->co, nvec);
					lastv2 = BM_vert_create(*bm, co2, NULL, 0);
				}

				f = BM_face_create_quad_tri(*bm, lastv1, v1, v2, lastv2, NULL, 0);
				f->mat_nr = inner_material_index;
				lastv2 = v2;
			}

			lastv1 = v1;
		}
	}

	{
		/* move the stroke mesh a bit out, half of offset */
		BMIter iter;
		BMVert *v;

		BM_ITER_MESH(v, &iter, *bm, BM_VERTS_OF_MESH)
		{
			sub_v3_v3(v->co, half);
		}

		BM_mesh_elem_hflag_enable_all(*bm, BM_FACE | BM_EDGE | BM_VERT, BM_ELEM_SELECT, false);
		BMO_op_callf(*bm, (BMO_FLAG_DEFAULTS & ~BMO_FLAG_RESPECT_HIDE),
		             "remove_doubles verts=%av dist=%f", BM_VERTS_OF_MESH, 0.01, false);
	}
}

/* add inner (?) vgroup ? or vgroup in general during fracture ? TODO, check caller) */
static void add_vgroup(Shard *s, Object *ob, const char* name)
{
	int index = 0, i = 0;
	MDeformVert *dvert;
	if (!defgroup_find_name(ob, name)) {
		BKE_defgroup_new(ob, name);
	}
	index = defgroup_name_index(ob, name);
	dvert = CustomData_get_layer(&s->vertData, CD_MDEFORMVERT);
	if (dvert == NULL) {
		dvert = CustomData_add_layer(&s->vertData, CD_MDEFORMVERT, CD_CALLOC, NULL, s->totvert);
	}
	for (i = 0; i < s->totvert; i++) {
		MDeformVert* dv = dvert + i;
		defvert_add_index_notest(dv, index, 1.0f);
	}
}

/* same as with vgroups, but with materials, see above */
static void add_material(Shard* s, Object *ob, short mat_ofs) {

	/* only use material offsets if we have 3 or more materials; since FM material handling still is a bit odd  */
	/* todo, use index for inner material too, then this hack here isnt necessary any more */

	const short mat_nr_max = ob->totcol > 1 ? ob->totcol - 1 : 0;
	mat_ofs = mat_nr_max ? mat_ofs : 0;

	if (mat_ofs) {
		MPoly *mp;
		int i = 0;

		for (i = 0; i < s->totpoly; i++)
		{
			mp = s->mpoly + i;
			mp->mat_nr = mat_ofs;

			//hrm first material and second (inner) should be untouched...
			CLAMP(mp->mat_nr, 0, mat_nr_max);
		}
	}
}

/* Used with cuttergroups, works not really properly as well, Leave off / fix later ? */
static void do_intersect(FractureModifierData *fmd, Object* ob, Shard *t, short inner_mat_index,
                         bool is_zero, float mat[4][4], int **shard_counts, int* count,
                         int k, DerivedMesh **dm_parent, bool keep_other_shard, float thresh)
{
	/*just keep appending items at the end here */
	MPoly *mpoly, *mp;
	int totpoly;
	Shard *parent = NULL;
	Shard *s = NULL, *s2 = NULL;
	int shards = 0, j = 0;

	if (is_zero == false && *dm_parent == NULL) {
		parent = BLI_findlink(&fmd->frac_mesh->shard_map, k);
		*dm_parent = BKE_shard_create_dm(parent, true);
	}

	mpoly = (*dm_parent)->getPolyArray(*dm_parent);
	totpoly = (*dm_parent)->getNumPolys(*dm_parent);

	for (j = 0, mp = mpoly; j < totpoly; j++, mp++) {
		mp->flag &= ~ME_FACE_SEL;
	}

	if (keep_other_shard)
	{
		s = BKE_fracture_shard_boolean(ob, *dm_parent, t, inner_mat_index, 0, 0.0f, &s2, NULL, 0.0f, false, 0, fmd->uvlayer_name, thresh);
	}
	else
	{
		s = BKE_fracture_shard_boolean(ob, *dm_parent, t, inner_mat_index, 0, 0.0f, NULL, NULL, 0.0f, false, 0, fmd->uvlayer_name, thresh);
	}

	//printf("Fractured: %d\n", k);

	if (ELEM(fmd->keep_cutter_shards, MOD_FRACTURE_KEEP_BOTH, MOD_FRACTURE_KEEP_INTERSECT)) {
		if (s != NULL) {
			add_vgroup(s, ob, "Intersect");
			add_material(s, ob, fmd->mat_ofs_intersect);
			add_shard(fmd->frac_mesh, s, mat);
			shards++;
			s = NULL;
		}
	}

	if (ELEM(fmd->keep_cutter_shards, MOD_FRACTURE_KEEP_BOTH, MOD_FRACTURE_KEEP_DIFFERENCE)) {
		if (s2 != NULL) {
			add_vgroup(s2, ob, "Difference");
			add_material(s2, ob, fmd->mat_ofs_difference);
			add_shard(fmd->frac_mesh, s2, mat);
			shards++;
			s2 = NULL;
		}
	}

	//if ((is_zero && ob->derivedFinal == NULL) || !is_zero) {
	{
		if (is_zero) {
			*count = 0;
		}

		(*dm_parent)->needsFree = 1;
		(*dm_parent)->release(*dm_parent);
		*dm_parent = NULL;
	}

	if (is_zero) {
		shards = 0;
	}

	(*shard_counts)[k] = shards;
	//printf("k, shards: %d %d \n", k, shards);
	//shards = 0;
}


/* Also used by cutter groups feature, to intersect each shard individually by the given DM */
static void intersect_shards_by_dm(FractureModifierData *fmd, DerivedMesh *d, Object *ob, Object *ob2, short inner_mat_index, float mat[4][4],
                                   bool keep_other_shard, float thresh)
{
	Shard *t = NULL;
	int i = 0, count = 0, k = 0;
	float imat[4][4];
	int* shard_counts = NULL;
	bool is_zero = false;
	MVert *mv;
	DerivedMesh *dm_parent = NULL;

	t = BKE_create_fracture_shard(d->getVertArray(d), d->getPolyArray(d), d->getLoopArray(d), d->getEdgeArray(d),
	                              d->getNumVerts(d), d->getNumPolys(d), d->getNumLoops(d), d->getNumEdges(d), true);
	t = BKE_custom_data_to_shard(t, d);


	invert_m4_m4(imat, ob->obmat);
	for (i = 0, mv = t->mvert; i < t->totvert; mv++, i++){
		if (ob2)
			mul_m4_v3(ob2->obmat, mv->co);
		mul_m4_v3(imat, mv->co);
	}

	count = fmd->frac_mesh->shard_count;

	/*TODO, pass modifier mesh here !!! */
	if (count == 0 && keep_other_shard) {

		if (ob->derivedFinal != NULL) {
			dm_parent = CDDM_copy(ob->derivedFinal);
		}

		if (dm_parent == NULL) {
			dm_parent = CDDM_from_mesh(ob->data);
		}

		count = 1;
		is_zero = true;
	}

	shard_counts = MEM_mallocN(sizeof(int) * count, "shard_counts");

	for (k = 0; k < count; k++) {
		do_intersect(fmd, ob, t, inner_mat_index, is_zero, mat, &shard_counts, &count, k, &dm_parent, keep_other_shard, thresh);
	}

	for (k = 0; k < count; k++)
	{
		int cnt = shard_counts[k];

		if (cnt > 0)
		{
			if (keep_other_shard)
			{
				/*clean up old entries here to avoid unnecessary shards*/
				Shard *first = fmd->frac_mesh->shard_map.first;
				BLI_remlink_safe(&fmd->frac_mesh->shard_map,first);
				BKE_shard_free(first, true);
				first = NULL;
			}

			/* keep asynchronous by intent, to keep track of original shard count */
			fmd->frac_mesh->shard_count--;
		}
	}

	MEM_freeN(shard_counts);
	shard_counts = NULL;

	BKE_shard_free(t, true);
}

static void reset_shards(FractureModifierData *fmd)
{
	if (fmd->fracture_mode == MOD_FRACTURE_PREFRACTURED && fmd->reset_shards)
	{
		FracMesh *fm = fmd->frac_mesh;
		while (fm && fm->shard_map.first)
		{
			Shard *t = fm->shard_map.first;
			BLI_remlink_safe(&fm->shard_map, t);
			printf("Resetting shard (Greasepencil/Cutter Plane): %d\n", t->shard_id);
			BKE_shard_free(t, true);
		}
		fm->shard_count = 0;
		/* do not reset again afterwards, in case we have multiple point sources */
		if (!fmd->execute_threaded) {
			fmd->reset_shards = false;
		}
	}
}

/* main function of GP based fracture, by lines / cutter planes */
void BKE_fracture_shard_by_greasepencil(FractureModifierData *fmd, Object *obj, short inner_material_index, float mat[4][4])
{
	bGPDlayer *gpl;
	bGPDframe *gpf;
	bGPDstroke *gps;

	reset_shards(fmd);

	if ((obj->gpd) && (obj->gpd->layers.first)) {

		float imat[4][4];
		invert_m4_m4(imat, mat);
		for (gpl = obj->gpd->layers.first; gpl; gpl = gpl->next) {
			for (gpf = gpl->frames.first; gpf; gpf = gpf->next) {
				for (gps = gpf->strokes.first; gps; gps = gps->next) {
					BMesh *bm = BM_mesh_create(&bm_mesh_allocsize_default, &((struct BMeshCreateParams){.use_toolflags = true,}));
					DerivedMesh *dm = NULL;


					/*create stroke mesh */
					stroke_to_faces(fmd, &bm, gps, inner_material_index);
					dm = CDDM_from_bmesh(bm, true);
#if 0
					{
						/*create debug mesh*/
						Object* o;
						o = BKE_object_add(G.main, fmd->modifier.scene, OB_MESH, "DUMMY");
						BM_mesh_bm_to_me(bm, o->data, (&(struct BMeshToMeshParams){0}));
					}
#endif

					BM_mesh_free(bm);

					/*do intersection*/
					intersect_shards_by_dm(fmd, dm, obj, NULL, inner_material_index, mat, true, fmd->boolean_double_threshold);

					dm->needsFree = 1;
					dm->release(dm);
					dm = NULL;
				}
			}
		}
	}
}

/* main function of cuttergroup fracture */
void BKE_fracture_shard_by_planes(FractureModifierData *fmd, Object *obj, short inner_material_index, float mat[4][4])
{
	if (fmd->cutter_group != NULL && obj->type == OB_MESH)
	{
		GroupObject* go;
		float imat[4][4];

		reset_shards(fmd);
		invert_m4_m4(imat, obj->obmat);

		for (go = fmd->cutter_group->gobject.first; go; go = go->next)
		{
			Object* ob = go->ob;

			printf("Cutting with %s ...\n", ob->id.name);
			/*simple case....one cutter object per object*/
			if (ob->type == OB_MESH) {

				FractureModifierData *fmd2 = (FractureModifierData*)modifiers_findByType(ob, eModifierType_Fracture);
				if (fmd2 && BLI_listbase_count(&fmd2->meshIslands) > 0)
				{
					MeshIsland* mi = NULL;
					int j = 0;
					for (mi = fmd2->meshIslands.first; mi; mi = mi->next)
					{
						DerivedMesh *dm = CDDM_copy(mi->physics_mesh);
						MVert *mv = dm->getVertArray(dm), *v = NULL;
						int totvert = dm->getNumVerts(dm);
						int i = 0;

						//printf("Cutting with %s, island %d...\n", ob->id.name, j);
						for (i = 0, v = mv; i < totvert; i++, v++)
						{
							add_v3_v3(v->co, mi->centroid);
						}

						intersect_shards_by_dm(fmd, dm, obj, ob, inner_material_index, mat, false, fmd->boolean_double_threshold);

						dm->needsFree = 1;
						dm->release(dm);
						dm = NULL;
						j++;
					}

					/*now delete first shards, those are the old ones */
					while (fmd->frac_mesh->shard_count > 0)
					{
						/*clean up old entries here to avoid unnecessary shards*/
						Shard *first = fmd->frac_mesh->shard_map.first;
						BLI_remlink_safe(&fmd->frac_mesh->shard_map,first);
						BKE_shard_free(first, true);
						first = NULL;
						fmd->frac_mesh->shard_count--;
					}

					/* re-synchronize counts, was possibly different before */
					fmd->frac_mesh->shard_count = BLI_listbase_count(&fmd->frac_mesh->shard_map);
				}
				else
				{

					DerivedMesh *d;
					bool copied = false;
					if (ob->derivedFinal == NULL) {
						d = CDDM_from_mesh(ob->data);
						copied = true;
					}
					else {
						d = ob->derivedFinal;
					}

					intersect_shards_by_dm(fmd, d, obj, ob, inner_material_index, mat, true, fmd->boolean_double_threshold);

					if (copied)
					{	/*was copied before */
						d->needsFree = 1;
						d->release(d);
						d = NULL;
					}
				}
			}
		}
	}
}

/* this should really be passed around here everywhere !!! TODO*/
typedef struct FractureData {
	cell *voro_cells;
	FracMesh *fmesh;
	ShardID id;
	int totpoints;
	int algorithm;
	Object *obj;
	DerivedMesh *dm;
	short inner_material_index;
	float mat[4][4];
	int num_cuts;
	float fractal;
	bool smooth;
	int num_levels;
	int mode;
	bool reset;
	int active_setting;
	int num_settings;
	char uv_layer[64];
	float thresh;
	int override_count;
	float factor;
} FractureData;


static void compute_fracture(TaskPool *UNUSED(pool), void *taskdata, int UNUSED(threadid))
{
    FractureData *fd = (FractureData*)taskdata;

	/*Evaluate result*/
	if (fd->totpoints > 0) {
		parse_cells(fd->voro_cells, fd->totpoints, fd->id, fd->fmesh, fd->algorithm, fd->obj, fd->dm, fd->inner_material_index, fd->mat,
	                fd->num_cuts, fd->fractal, fd->smooth, fd->num_levels,fd->mode, fd->reset, fd->active_setting, fd->num_settings, fd->uv_layer,
		            true, fd->thresh, fd->override_count, fd->factor);
	}
}


static FractureData segment_cells(cell *voro_cells, int startcell, int totcells, FracMesh *fmesh, ShardID id, FracPointCloud *pointcloud,
                    int algorithm, Object *obj, DerivedMesh *dm, short
                    inner_material_index, float mat[4][4], int num_cuts, float fractal, bool smooth, int num_levels, int mode,
                    bool reset, int active_setting, int num_settings, char uv_layer[64], float thresh, int override_count,
                    float factor)
{
	FractureData fd;
	fd.fmesh = fmesh;
	fd.id = id;
	fd.totpoints = totcells;
	fd.algorithm = algorithm;
	fd.obj = obj;
	fd.dm = dm;
	fd.inner_material_index = inner_material_index;
	copy_m4_m4(fd.mat, mat);
	fd.num_cuts = num_cuts;
	fd.fractal = fractal;
	fd.smooth = smooth;
	fd.num_levels = num_levels;
	fd.mode = mode;
	fd.reset = reset;
	fd.active_setting = active_setting;
	fd.num_settings = num_settings;
	strncpy(fd.uv_layer, uv_layer, 64);
	fd.thresh = thresh;
	fd.override_count = override_count;
	fd.factor = factor;

	//cell start pointer, only take fd.totpoints cells out
	fd.voro_cells = voro_cells + startcell;

	return fd;
}

/* main function for pointcloud based fracture */
void BKE_fracture_shard_by_points(FracMesh *fmesh, ShardID id, FracPointCloud *pointcloud, int algorithm, Object *obj, DerivedMesh *dm, short
                                  inner_material_index, float mat[4][4], int num_cuts, float fractal, bool smooth, int num_levels, int mode,
                                  bool reset, int active_setting, int num_settings, char uv_layer[64], bool threaded,
                                  float thresh, bool shards_to_islands, int override_count, float factor, int point_source,
                                  int resolution[3], float spacing[3])
{
	int n_size = 8;
	
	Shard *shard;
	
	float min[3], max[3];
	float theta = 0.001f; /* TODO, container enlargement, because boundbox exact container and boolean might create artifacts */
	int p, i = 0, num = 0, totcell = 0, remainder_start = 0;
	
	container *voro_container;
	particle_order *voro_particle_order;
	cell *voro_cells;

	TaskScheduler *scheduler = BLI_task_scheduler_get();
	TaskPool *pool;
	FractureData *fdata;

#ifdef USE_DEBUG_TIMER
	double time_start;
#endif
	
	shard = BKE_shard_by_id(fmesh, id, dm);
	if (!shard || (shard->flag & SHARD_FRACTURED && (mode == MOD_FRACTURE_DYNAMIC))) {
		int val = shards_to_islands ? -1 : 0;
		if (id == val)
		{
			//fallback to entire mesh
			shard = BKE_shard_by_id(fmesh, -1 , dm);
		}
		else
		{
			return;
		}
	}

	printf("Fracturing with %d points...\n", pointcloud->totpoints);
	/* calculate bounding box with theta margin */
	copy_v3_v3(min, shard->min);
	copy_v3_v3(max, shard->max);

	if (shard->shard_id == -2) {
		BKE_shard_free(shard, true);
	}

	add_v3_fl(min, -theta);
	add_v3_fl(max, theta);

	mul_m4_v3(mat, min);
	mul_m4_v3(mat, max);

	
	if (point_source & MOD_FRACTURE_GRID)
	{
		float off[3] =  {0, 0, 0};
		for (p = 0; p < pointcloud->totpoints; p++)
		{
			//find "max" offset (where atleast 1 axis is > 0)
			if (pointcloud->points[p].offset[0] > 0 ||
			    pointcloud->points[p].offset[1] > 0 ||
			    pointcloud->points[p].offset[2] > 0)
			{
				copy_v3_v3(off, pointcloud->points[p].offset);
				break;
			}
		}

		if (off[0] > 0 || off[1] > 0 || off[2] > 0)
		{
			sub_v3_v3(min, off);

			//special treatment for grid pointsource... with offsets
			voro_container = container_new(min[0], max[0], min[1], max[1], min[2], max[2],
										   resolution[0] * 2, resolution[1] * 2, resolution[2] * 2, false, false, false,
										   pointcloud->totpoints);

		}
		else {
			voro_container = container_new(min[0], max[0], min[1], max[1], min[2], max[2],
										   n_size, n_size, n_size, false, false, false,
										   pointcloud->totpoints);
		}
	}
	else {
		voro_container = container_new(min[0], max[0], min[1], max[1], min[2], max[2],
									   n_size, n_size, n_size, false, false, false,
									   pointcloud->totpoints);
	}
	
	voro_particle_order = particle_order_new();
	for (p = 0; p < pointcloud->totpoints; p++) {
		float *co = pointcloud->points[p].co;
		container_put(voro_container, voro_particle_order, p, co[0], co[1], co[2]);
	}

#ifdef USE_DEBUG_TIMER
	time_start = PIL_check_seconds_timer();
#endif

	/* we expect as many raw cells as we have particles */
	voro_cells = cells_new(pointcloud->totpoints);

	/*Compute directly...*/
	container_compute_cells(voro_container, voro_cells);

	/*Apply offsets (if any, grid only */
	if (point_source & MOD_FRACTURE_GRID)
	{
		int v = 0;
		float fact[3] = {1 - spacing[0], 1 - spacing[1], 1 - spacing[2]};
		for (p = 0; p < pointcloud->totpoints; p++)
		{
			//adjust centroid and...
			float off[3], cent[3];
			copy_v3_v3(off, pointcloud->points[p].offset);
			add_v3_v3(voro_cells[p].centroid, off);
			copy_v3_v3(cent, voro_cells[p].centroid);
			mul_v3_v3(cent, fact);

			//vertex coordinates
			for (v = 0; v < voro_cells[p].totvert; v++)
			{
				add_v3_v3(voro_cells[p].verts[v], off);
				//print_v3("Vert", voro_cells[p].verts[v]);
				sub_v3_v3(voro_cells[p].verts[v], cent);
				add_v3_v3(voro_cells[p].verts[v], voro_cells[p].centroid);
			}
		}
	}

	/*Disable for fast bisect/fill, dynamic and mousebased for now -> errors and crashes */
	if (mode != MOD_FRACTURE_DYNAMIC && reset == true && algorithm != MOD_FRACTURE_BISECT_FAST && algorithm != MOD_FRACTURE_BISECT_FAST_FILL && threaded == true) {
		/*segment cells, give each thread a chunk to work on */
		pool = BLI_task_pool_create(scheduler, NULL);
		num = BLI_task_scheduler_num_threads(scheduler);
		fdata = MEM_callocN(sizeof(FractureData) * (num + 1), "threaded fracture data");
		totcell = pointcloud->totpoints / num;
		for (i = 0; i < num; i++) {
			//give each task a segment of the shards...
			int startcell = i * totcell;
			FractureData fd = segment_cells(voro_cells, startcell, totcell, fmesh, id, pointcloud, algorithm, obj, dm, inner_material_index,
											mat, num_cuts, fractal,smooth, num_levels, mode, reset, active_setting, num_settings, uv_layer,
											thresh, override_count, factor);
			fdata[i] = fd;
		}

		//remainder...
		remainder_start = fdata[0].totpoints * num;
		if (remainder_start < pointcloud->totpoints) {
			int remainder = pointcloud->totpoints - remainder_start;
			int startcell = remainder_start;
			printf("REMAINDER %d %d\n", startcell, remainder);
			fdata[num] = segment_cells(voro_cells, startcell, remainder, fmesh, id, pointcloud, algorithm, obj, dm, inner_material_index,
										mat, num_cuts, fractal,smooth, num_levels, mode, reset, active_setting, num_settings, uv_layer,
										thresh, override_count, factor);
		}

		for (i = 0; i < num+1; i++) {
			BLI_task_pool_push(pool, compute_fracture, &fdata[i], false, TASK_PRIORITY_HIGH);
		}

		BLI_task_pool_work_and_wait(pool);
		BLI_task_pool_free(pool);
		MEM_freeN(fdata);
	}
	else {
		/*Evaluate result*/
		parse_cells(voro_cells, pointcloud->totpoints, id, fmesh, algorithm, obj, dm, inner_material_index, mat,
					num_cuts, fractal, smooth, num_levels, mode, reset, active_setting, num_settings, uv_layer,
					false, thresh, override_count, factor);
	}

	/*Free structs in C++ area of memory */
	cells_free(voro_cells, pointcloud->totpoints);
	particle_order_free(voro_particle_order);
	container_free(voro_container);

#ifdef USE_DEBUG_TIMER
	printf("Fracture done, %g\n", PIL_check_seconds_timer() - time_start);
#endif

}

void BKE_fracmesh_free(FracMesh *fm, bool doCustomData)
{
	if (fm == NULL) {
		return;
	}

	while (fm->shard_map.first) {
		Shard* s = (Shard*)fm->shard_map.first;
		BLI_remlink(&fm->shard_map, s);
		BKE_shard_free(s, doCustomData);
	}

	if (fm->last_shard_tree)
	{
		BLI_kdtree_free(fm->last_shard_tree);
		fm->last_shard_tree = NULL;
	}

	if (fm->last_shards)
	{
		MEM_freeN(fm->last_shards);
		fm->last_shards = NULL;
	}
}

/* marking ? what is being marked ? ah sharp for edges which are being created by cutting etc... */
static void do_marking(FractureModifierData *fmd, DerivedMesh *result)
{
	MEdge *medge = result->getEdgeArray(result);
	MPoly *mpoly = result->getPolyArray(result), *mp = NULL;
	MLoop *mloop = result->getLoopArray(result);
	MVert *mvert = result->getVertArray(result);
	int totpoly = result->getNumPolys(result);
	int i = 0;
	for (i = 0, mp = mpoly; i < totpoly; i++, mp++)
	{
		if (mp->flag & ME_FACE_SEL)
		{
			int j = 0;
			for (j = 0; j < mp->totloop; j++)
			{
				MLoop ml;
				ml = mloop[mp->loopstart + j];
				medge[ml.e].flag |= ME_SHARP;
				medge[ml.e].crease = fmd->inner_crease * 255.0f;
				mvert[ml.v].flag |= ME_VERT_TMP_TAG;
			}

			if (fmd->use_smooth)
				mp->flag |= ME_SMOOTH;
		}
		else
		{
			/*remove verts from unselected faces again*/
			int j = 0;
			for (j = 0; j < mp->totloop; j++)
			{
				MLoop ml;
				ml = mloop[mp->loopstart + j];
				mvert[ml.v].flag &= ~ME_VERT_TMP_TAG;
			}
		}
	}
}

/* create a Derivedmesh from a collection of Shards of an FM */
static DerivedMesh* do_create(FractureModifierData *fmd, int num_verts, int num_loops, int num_polys, int num_edges,
                              bool doCustomData, bool use_packed)
{
	int shard_count = fmd->shards_to_islands ? BLI_listbase_count(&fmd->islandShards) : fmd->frac_mesh->shard_count;
	ListBase *shardlist;
	Shard *shard;

	int vertstart, polystart, loopstart, edgestart;

	MVert *mverts;
	MPoly *mpolys;
	MLoop *mloops;
	MEdge *medges;

	DerivedMesh *result = CDDM_new(num_verts, num_edges, 0, num_loops, num_polys);
	mverts = CDDM_get_verts(result);
	mloops = CDDM_get_loops(result);
	mpolys = CDDM_get_polys(result);

	if (num_edges > 0) {
		medges = CDDM_get_edges(result);
	}

#if 0
	if (doCustomData && shard_count > 0) {

		Shard *s;

		if (fmd->shards_to_islands) {
			s = (Shard *)fmd->islandShards.first;
		}
		else {
			s = (Shard *)fmd->frac_mesh->shard_map.first;
		}

		//if (fmd->fracture_mode == MOD_FRACTURE_PREFRACTURED || fmd->fracture_mode == MOD_FRACTURE_DYNAMIC)
		{
			/*keep old behavior for now for older modes */
			CustomData_merge(&s->vertData, &result->vertData, CD_MASK_MDEFORMVERT, CD_CALLOC, num_verts);
			CustomData_merge(&s->polyData, &result->polyData, CD_MASK_MTEXPOLY, CD_CALLOC, num_polys);
			CustomData_merge(&s->loopData, &result->loopData, CD_MASK_MLOOPUV, CD_CALLOC, num_loops);
			CustomData_merge(&s->edgeData, &result->edgeData, CD_MASK_CREASE | CD_MASK_BWEIGHT | CD_MASK_MEDGE, CD_CALLOC, num_edges);
		}
		else
		{
			/*just create new empty layers */
			CustomData_add_layer(&result->vertData, CD_MDEFORMVERT, CD_CALLOC, NULL, num_verts);
			CustomData_add_layer(&result->polyData, CD_MTEXPOLY, CD_CALLOC, NULL, num_polys);
			CustomData_add_layer(&result->loopData, CD_MLOOPUV, CD_CALLOC, NULL, num_loops);
			CustomData_add_layer(&result->edgeData, CD_CREASE, CD_CALLOC, NULL, num_edges);
			CustomData_add_layer(&result->edgeData, CD_BWEIGHT, CD_CALLOC, NULL, num_edges);
		}
	}
#endif

	vertstart = polystart = loopstart = edgestart = 0;
	if (use_packed)
	{
		shardlist = &fmd->pack_storage;
	}
	else if (fmd->shards_to_islands) {
		shardlist = &fmd->islandShards;
	}
	else {
		shardlist = &fmd->frac_mesh->shard_map;
	}

	for (shard = shardlist->first; shard; shard = shard->next)
	{
		MPoly *mp;
		MLoop *ml;
		MEdge *me;
		int i;

		memcpy(mverts + vertstart, shard->mvert, shard->totvert * sizeof(MVert));
		memcpy(mpolys + polystart, shard->mpoly, shard->totpoly * sizeof(MPoly));

		for (i = 0, mp = mpolys + polystart; i < shard->totpoly; ++i, ++mp) {
			/* adjust loopstart index */
			mp->loopstart += loopstart;
		}

		memcpy(mloops + loopstart, shard->mloop, shard->totloop * sizeof(MLoop));

		for (i = 0, ml = mloops + loopstart; i < shard->totloop; ++i, ++ml) {
			/* adjust vertex index */
			ml->v += vertstart;
			ml->e += edgestart;
		}

		if (num_edges > 0) {
			memcpy(medges + edgestart, shard->medge, shard->totedge * sizeof(MEdge));

			for (i = 0, me = medges + edgestart; i < shard->totedge; ++i, ++me) {
				/* adjust vertex indices */
				me->v1 += vertstart;
				me->v2 += vertstart;
			}
		}

#if 0
		if (doCustomData) {

			if (shard->totvert > 1) {
				CustomData_copy_data(&shard->vertData, &result->vertData, 0, vertstart, shard->totvert);
			}

			if (shard->totloop > 0) {
				CustomData_copy_data(&shard->loopData, &result->loopData, 0, loopstart, shard->totloop);
			}

			if (shard->totpoly > 0) {
				CustomData_copy_data(&shard->polyData, &result->polyData, 0, polystart, shard->totpoly);
			}
		}
#endif

		if (doCustomData) {
			fracture_collect_layers(shard, result, vertstart, polystart, loopstart, edgestart);
		}

		vertstart += shard->totvert;
		polystart += shard->totpoly;
		loopstart += shard->totloop;
		edgestart += shard->totedge;
	}

	return result;
}

/* main function of creating DerivedMesh from shards */
static DerivedMesh *create_dm(FractureModifierData *fmd, bool doCustomData, bool use_packed)
{
	Shard *s;
	int num_verts, num_polys, num_loops, num_edges;
	DerivedMesh *result;
	
	num_verts = num_polys = num_loops = num_edges = 0;

	if (use_packed)
	{
		for (s = fmd->pack_storage.first; s; s = s->next) {
			num_verts += s->totvert;
			num_polys += s->totpoly;
			num_loops += s->totloop;
			num_edges += s->totedge;
		}
	}
	else if (fmd->shards_to_islands) {
		for (s = fmd->islandShards.first; s; s = s->next) {
			num_verts += s->totvert;
			num_polys += s->totpoly;
			num_loops += s->totloop;
			num_edges += s->totedge;
		}
	}
	else {

		if (!fmd->frac_mesh)
			return NULL;

		for (s = fmd->frac_mesh->shard_map.first; s; s = s->next) {
			num_verts += s->totvert;
			num_polys += s->totpoly;
			num_loops += s->totloop;
			num_edges += s->totedge;
		}
	}
	
	result = do_create(fmd, num_verts, num_loops, num_polys, num_edges, doCustomData, use_packed);

	if (num_edges == 0) {
		CustomData_free(&result->edgeData, 0);
		CDDM_calc_edges(result);
	}

	if (fmd->fracture_mode != MOD_FRACTURE_EXTERNAL)
	{
		do_marking(fmd, result);
	}
	
	result->dirty |= DM_DIRTY_NORMALS;
	CDDM_calc_normals_mapping(result);
	return result;
}

/*well... HERE is the main function TODO */
DerivedMesh* BKE_fracture_create_dm(FractureModifierData *fmd, bool doCustomData, bool use_packed)
{
	DerivedMesh *dm_final = NULL;
	
	if (fmd->dm && !use_packed) {
		fmd->dm->needsFree = 1;
		fmd->dm->release(fmd->dm);
		fmd->dm = NULL;
	}
	
	dm_final = create_dm(fmd, doCustomData, use_packed);

	if (!use_packed) {
		fmd->dm = dm_final;
	}

	return dm_final;
}

void BKE_copy_customdata_layers(CustomData* dest, CustomData *src, int type, int count)
{
	int layer;
	for (layer = 0; layer < src->totlayer; layer++)
	{
		if (src->layers[layer].type == type)
		{
			CustomData_add_layer(dest, type, CD_DUPLICATE, src->layers[layer].data, count);
		}
	}
}

/* create a DerivedMesh from a single Shard */
DerivedMesh *BKE_shard_create_dm(Shard *s, bool doCustomData)
{
	DerivedMesh *dm;
	MVert *mverts;
	MLoop *mloops;
	MPoly *mpolys;
	
	dm = CDDM_new(s->totvert, s->totedge, 0, s->totloop, s->totpoly);

	mverts = CDDM_get_verts(dm);
	mloops = CDDM_get_loops(dm);
	mpolys = CDDM_get_polys(dm);

	memcpy(mverts, s->mvert, s->totvert * sizeof(MVert));
	memcpy(mloops, s->mloop, s->totloop * sizeof(MLoop));
	memcpy(mpolys, s->mpoly, s->totpoly * sizeof(MPoly));

	if (s->totedge > 0) {
		MEdge *medges = CDDM_get_edges(dm);
		memcpy(medges, s->medge, s->totedge * sizeof(MEdge));
	}
	else {
		CustomData_free(&dm->edgeData, 0);
		CDDM_calc_edges(dm);
	}

	dm->dirty |= DM_DIRTY_NORMALS;
	CDDM_calc_normals_mapping(dm);

	if (doCustomData) {
		/*if (s->totvert > 0) {
			BKE_copy_customdata_layers(&dm->vertData, &s->vertData, CD_MDEFORMVERT, s->totvert);
		}
		if (s->totloop > 0) {
			BKE_copy_customdata_layers(&dm->loopData, &s->loopData, CD_MLOOPUV, s->totloop);
		}
		if (s->totpoly > 0) {
			BKE_copy_customdata_layers(&dm->polyData, &s->polyData, CD_MTEXPOLY, s->totpoly);
		}*/

		fracture_collect_layers(s, dm, 0, 0, 0, 0);
	}

	return dm;
}

void BKE_get_next_entries(FractureModifierData *fmd)
{
	/*meshislands and shards SHOULD be synchronized !!!!*/
	if (fmd->current_mi_entry && fmd->current_mi_entry->next)
	{ // && fmd->current_mi_entry->next->is_new == false) {
		fmd->current_mi_entry = fmd->current_mi_entry->next;
		fmd->meshIslands = fmd->current_mi_entry->meshIslands;
		fmd->visible_mesh_cached = fmd->current_mi_entry->visible_dm;
	}

	if (fmd->current_shard_entry && fmd->current_shard_entry->next)
	{
		fmd->current_shard_entry = fmd->current_shard_entry->next;
		fmd->frac_mesh = fmd->current_shard_entry->frac_mesh;
	}
}

void BKE_get_prev_entries(FractureModifierData *fmd)
{
	/*meshislands and shards SHOULD be synchronized !!!!*/
	if (fmd->current_mi_entry && fmd->current_mi_entry->prev)
	{
		fmd->current_mi_entry = fmd->current_mi_entry->prev;
		fmd->meshIslands = fmd->current_mi_entry->meshIslands;
		fmd->visible_mesh_cached = fmd->current_mi_entry->visible_dm;
	}

	if (fmd->current_shard_entry && fmd->current_shard_entry->prev)
	{
		fmd->current_shard_entry = fmd->current_shard_entry->prev;
		fmd->frac_mesh = fmd->current_shard_entry->frac_mesh;
	}
}

/* find the correct current mesh state according to whether the current frame is in its frame range */
bool BKE_lookup_mesh_state(FractureModifierData *fmd, int frame, int do_lookup)
{
	bool changed = false;
	bool forward = false;
	bool backward = false;

	backward = ((fmd->last_frame > frame) && fmd->current_mi_entry && fmd->current_mi_entry->prev);
	forward = ((fmd->last_frame < frame) && (fmd->current_mi_entry) && (fmd->current_mi_entry->next != NULL) &&
	           (fmd->current_mi_entry->is_new == false));

	if (backward)
	{
		if (do_lookup)
		{
			while (fmd->current_mi_entry && fmd->current_mi_entry->prev &&
				   frame <= fmd->current_mi_entry->prev->frame)
			{
				printf("Jumping backward because %d is smaller than %d\n", frame, fmd->current_mi_entry->prev->frame);
				changed = true;
				//BKE_free_constraints(fmd);
				BKE_get_prev_entries(fmd);
			}
		}
	}
	else if (forward)
	{
		if (do_lookup)
		{
			while ((fmd->current_mi_entry) && (fmd->current_mi_entry->next != NULL) &&
				   (fmd->current_mi_entry->is_new == false) &&
				   frame > fmd->current_mi_entry->frame)
			{
				printf("Jumping forward because %d is greater than %d\n", frame, fmd->current_mi_entry->frame);
				changed = true;
				//BKE_free_constraints(fmd);
				BKE_get_next_entries(fmd);
			}
		}
	}

	if (do_lookup)
	{
		return changed;
	}
	else
	{
		if (forward || backward)
		{
			fmd->modifier.scene->rigidbody_world->flag |= RBW_FLAG_REFRESH_MODIFIERS;
			fmd->modifier.scene->rigidbody_world->flag |= RBW_FLAG_OBJECT_CHANGED;
		}

		return forward || backward;
	}
}

void BKE_match_vertex_coords(MeshIsland* mi, MeshIsland *par, Object *ob, int frame, bool is_parent, bool shards_to_islands)
{
	float loc[3] = {0.0f, 0.0f, 0.0f};
	float rot[4] = {1.0f, 0.0f, 0.0f, 0.0f};
	int j = 0;

	float centr[3] = {0.0f, 0.0f, 0.0f};

	float mat[4][4];
	float quat[4] = {1.0f, 0.0f, 0.0f, 0.0f};
	float qrot[4] = {1.0f, 0.0f, 0.0f, 0.0f};
	float iquat[4] =  {1.0f, 0.0f, 0.0f, 0.0f};

	int val = shards_to_islands ? -1 : 0;

	invert_m4_m4(mat, ob->obmat);

	mi->locs[0] = loc[0] = par->locs[3*frame];
	mi->locs[1] = loc[1] = par->locs[3*frame+1];
	mi->locs[2] = loc[2] = par->locs[3*frame+2];

	mi->rots[0] = rot[0] = par->rots[4*frame];
	mi->rots[1] = rot[1] = par->rots[4*frame+1];
	mi->rots[2] = rot[2] = par->rots[4*frame+2];
	mi->rots[3] = rot[3] = par->rots[4*frame+3];

	mul_m4_v3(mat, loc);
	mat4_to_quat(quat, ob->obmat);
	invert_qt_qt(iquat, quat);

	if (par->id == val)
	{
		invert_qt_qt(qrot, par->rot);
		mul_qt_qtqt(qrot, rot, qrot);
		mul_qt_qtqt(qrot, iquat, qrot);
	}
	else
	{
		mul_qt_qtqt(qrot, rot, par->rot);
		mul_qt_qtqt(qrot, iquat, qrot);
	}

	if (is_parent)
	{
		copy_v3_v3(centr, mi->centroid);
		mul_qt_v3(qrot, centr);
		add_v3_v3(centr, loc);
	}
	else
	{
		copy_v3_v3(centr, loc);
	}

	for (j = 0; j < mi->vertex_count; j++)
	{
		float co[3];

		//first add vert to centroid, then rotate
		copy_v3_v3(co, mi->vertices_cached[j]->co);

		sub_v3_v3(co, mi->centroid);
		mul_qt_v3(qrot, co);
		add_v3_v3(co, centr);

		copy_v3_v3(mi->vertices_cached[j]->co, co);

		mi->vertco[3*j]   = co[0];
		mi->vertco[3*j+1] = co[1];
		mi->vertco[3*j+2] = co[2];
	}

	{
		DerivedMesh *dm = mi->physics_mesh;
		MVert* mv, *mvert = dm->getVertArray(dm);
		int numVert = dm->getNumVerts(dm);

		for (j = 0, mv = mvert; j < numVert; j++, mv++)
		{
			//also rotate physicsmesh (shouldnt be necessary,
			//but lets do it to check whether its correct then)
			mul_qt_v3(qrot, mv->co);
		}
	}

	//init rigidbody properly ?
	copy_v3_v3(mi->centroid, centr);
	copy_qt_qt(mi->rot, qrot);
}

void BKE_free_constraints(FractureModifierData *fmd)
{
	MeshIsland *mi = NULL;
	RigidBodyShardCon *rbsc = NULL;

	//hmm after loading the pointers might be out of sync...
	if (fmd->fracture_mode == MOD_FRACTURE_DYNAMIC) {
		if (fmd->current_mi_entry) {
			fmd->meshIslands = fmd->current_mi_entry->meshIslands;
		}
		else {
			fmd->meshIslands.first = NULL;
			fmd->meshIslands.last = NULL;
		}
	}



	for (mi = fmd->meshIslands.first; mi; mi = mi->next) {
		if (mi->participating_constraints != NULL && mi->participating_constraint_count > 0) {
			int i;
			for (i = 0; i < mi->participating_constraint_count; i++)
			{
				RigidBodyShardCon *con = mi->participating_constraints[i];
				if (con) {
					con->mi1 = NULL;
					con->mi2 = NULL;
				}
			}

			MEM_freeN(mi->participating_constraints);
			mi->participating_constraints = NULL;
			mi->participating_constraint_count = 0;
		}
	}

	while (fmd->meshConstraints.first) {
		rbsc = fmd->meshConstraints.first;
		BLI_remlink(&fmd->meshConstraints, rbsc);

		if (fmd->fracture_mode == MOD_FRACTURE_DYNAMIC && fmd->modifier.scene)
		{
			BKE_rigidbody_remove_shard_con(fmd->modifier.scene, rbsc);
		}
		MEM_freeN(rbsc);
		rbsc = NULL;
	}

	fmd->meshConstraints.first = NULL;
	fmd->meshConstraints.last = NULL;
}

/*currently unused, was attempt to have different settings for different parts of the mesh */
void BKE_fracture_load_settings(FractureModifierData *fmd, FractureSetting *fs)
{
	/*copy settings values to FM itself....*/

	/* vgroups  XXX TODO non ascii strings ?*/
	strncpy(fmd->thresh_defgrp_name, fs->thresh_defgrp_name, strlen(fs->thresh_defgrp_name));
	strncpy(fmd->ground_defgrp_name, fs->ground_defgrp_name, strlen(fs->ground_defgrp_name));
	strncpy(fmd->inner_defgrp_name, fs->inner_defgrp_name, strlen(fs->inner_defgrp_name));

	fmd->inner_material = fs->inner_material;
	fmd->extra_group = fs->extra_group;
	fmd->cluster_group = fs->cluster_group;
	fmd->cutter_group = fs->cutter_group;

	fmd->breaking_threshold = fs->breaking_threshold;
	fmd->use_constraints = fs->use_constraints;
	fmd->contact_dist = fs->contact_dist;
	fmd->use_mass_dependent_thresholds = fs->use_mass_dependent_thresholds;

	fmd->constraint_limit = fs->constraint_limit;
	fmd->breaking_angle = fs->breaking_angle;
	fmd->breaking_distance = fs->breaking_distance;
	fmd->breaking_percentage = fs->breaking_percentage;
	fmd->use_experimental = fs->use_experimental;

	fmd->cluster_count = fs->cluster_count;
	fmd->cluster_breaking_threshold = fs->cluster_breaking_threshold;
	fmd->solver_iterations_override = fs->solver_iterations_override;
	fmd->shards_to_islands = fs->shards_to_islands;

	fmd->shard_count = fs->shard_count;
	fmd->frac_algorithm = fs->frac_algorithm;

	fmd->solver_iterations_override = fs->solver_iterations_override;

	fmd->breaking_angle_weighted = fs->breaking_angle_weighted;
	fmd->breaking_distance_weighted = fs->breaking_distance_weighted;
	fmd->breaking_percentage_weighted = fs->breaking_percentage_weighted;

	fmd->point_seed = fs->point_seed;
	fmd->point_source = fs->point_source;

	fmd->use_particle_birth_coordinates = fs->use_particle_birth_coordinates;
	fmd->splinter_axis = fs->splinter_axis;
	fmd->splinter_length = fs->splinter_length;
	fmd->cluster_solver_iterations_override = fs->cluster_solver_iterations_override;

	fmd->cluster_breaking_angle = fs->cluster_breaking_angle;
	fmd->cluster_breaking_distance = fs->cluster_breaking_distance;
	fmd->cluster_breaking_percentage = fs->cluster_breaking_percentage;

	fmd->use_breaking = fs->use_breaking;
	fmd->use_smooth = fs->use_smooth;
	fmd->fractal_cuts = fs->fractal_cuts;
	fmd->fractal_amount = fs->fractal_amount;

	fmd->grease_decimate = fs->grease_decimate;
	fmd->grease_offset = fs->grease_offset;
	fmd->use_greasepencil_edges = fs->use_greasepencil_edges;
	fmd->cutter_axis = fs->cutter_axis;

	// add more constraint types, as in special ones (3x generic and so on)
	fmd->cluster_constraint_type = fs->cluster_constraint_type;
	fmd->constraint_target = fs->constraint_target;

	fmd->impulse_dampening = fs->impulse_dampening;
	fmd->minimum_impulse = fs->minimum_impulse;
	fmd->directional_factor = fs->directional_factor;
	fmd->mass_threshold_factor = fs->mass_threshold_factor;
	fmd->use_compounds = fs->use_compounds;
}

void BKE_fracture_store_settings(FractureModifierData *fs, FractureSetting *fmd)
{
	//just invert fmd and fs here, same variables
	/*copy settings values to FM itself....*/

	/* vgroups  XXX TODO non ascii strings ?*/
	if (!fmd || !fs)
		return;

	strncpy(fmd->thresh_defgrp_name, fs->thresh_defgrp_name, strlen(fs->thresh_defgrp_name));
	strncpy(fmd->ground_defgrp_name, fs->ground_defgrp_name, strlen(fs->ground_defgrp_name));
	strncpy(fmd->inner_defgrp_name, fs->inner_defgrp_name, strlen(fs->inner_defgrp_name));

	fmd->inner_material = fs->inner_material;
	fmd->extra_group = fs->extra_group;
	fmd->cluster_group = fs->cluster_group;
	fmd->cutter_group = fs->cutter_group;

	fmd->breaking_threshold = fs->breaking_threshold;
	fmd->use_constraints = fs->use_constraints;
	fmd->contact_dist = fs->contact_dist;
	fmd->use_mass_dependent_thresholds = fs->use_mass_dependent_thresholds;

	fmd->constraint_limit = fs->constraint_limit;
	fmd->breaking_angle = fs->breaking_angle;
	fmd->breaking_distance = fs->breaking_distance;
	fmd->breaking_percentage = fs->breaking_percentage;
	fmd->use_experimental = fs->use_experimental;

	fmd->cluster_count = fs->cluster_count;
	fmd->cluster_breaking_threshold = fs->cluster_breaking_threshold;
	fmd->solver_iterations_override = fs->solver_iterations_override;
	fmd->shards_to_islands = fs->shards_to_islands;

	fmd->shard_count = fs->shard_count;
	fmd->frac_algorithm = fs->frac_algorithm;

	fmd->solver_iterations_override = fs->solver_iterations_override;

	fmd->breaking_angle_weighted = fs->breaking_angle_weighted;
	fmd->breaking_distance_weighted = fs->breaking_distance_weighted;
	fmd->breaking_percentage_weighted = fs->breaking_percentage_weighted;

	fmd->point_seed = fs->point_seed;
	fmd->point_source = fs->point_source;

	fmd->use_particle_birth_coordinates = fs->use_particle_birth_coordinates;
	fmd->splinter_length = fs->splinter_length;
	fmd->splinter_axis = fs->splinter_axis;
	fmd->cluster_solver_iterations_override = fs->cluster_solver_iterations_override;

	fmd->cluster_breaking_angle = fs->cluster_breaking_angle;
	fmd->cluster_breaking_distance = fs->cluster_breaking_distance;
	fmd->cluster_breaking_percentage = fs->cluster_breaking_percentage;

	fmd->use_breaking = fs->use_breaking;
	fmd->use_smooth = fs->use_smooth;
	fmd->fractal_cuts = fs->fractal_cuts;
	fmd->fractal_amount = fs->fractal_amount;

	fmd->grease_decimate = fs->grease_decimate;
	fmd->grease_offset = fs->grease_offset;
	fmd->use_greasepencil_edges = fs->use_greasepencil_edges;
	fmd->cutter_axis = fs->cutter_axis;

	// add more constraint types, as in special ones (3x generic and so on)
	fmd->cluster_constraint_type = fs->cluster_constraint_type;
	fmd->constraint_target = fs->constraint_target;

	fmd->impulse_dampening = fs->impulse_dampening;
	fmd->minimum_impulse = fs->minimum_impulse;
	fmd->directional_factor = fs->directional_factor;
	fmd->mass_threshold_factor = fs->mass_threshold_factor;
	fmd->use_compounds = fs->use_compounds;
}

static Shard* fracture_object_to_shard( Object *own, Object* target)
{
	DerivedMesh *dm;
	Shard *s = NULL;

	MVert* mvert, *mv;
	MPoly* mpoly;
	MLoop* mloop;
	MEdge* medge;
	SpaceTransform trans;
	float mat[4][4], size[3] = {1.0f, 1.0f, 1.0f};

	int totvert, totpoly, totloop, totedge, v;
	bool do_free = false;

	dm = target->derivedFinal;
	if (!dm)
	{	//fallback if no derivedFinal available
		dm = CDDM_from_mesh((Mesh*)target->data);
		do_free = true;
	}

	unit_m4(mat);
	BLI_space_transform_from_matrices(&trans, target->obmat, mat);
	//BLI_SPACE_TRANSFORM_SETUP(&trans, target, own);
	mat4_to_size(size, target->obmat);

	mvert = dm->getVertArray(dm);
	mpoly = dm->getPolyArray(dm);
	mloop = dm->getLoopArray(dm);
	medge = dm->getEdgeArray(dm);
	totvert = dm->getNumVerts(dm);
	totpoly = dm->getNumPolys(dm);
	totloop = dm->getNumLoops(dm);
	totedge = dm->getNumEdges(dm);

	// create temp shard -> that necessary at all ?
	s = BKE_create_fracture_shard(mvert, mpoly, mloop, medge, totvert, totpoly, totloop, totedge, true);

	//use this as size holder, and rawcentroid is the old ob location
	copy_v3_v3(s->impact_size, size);

	//compare centroid in worldspace with location
	mul_v3_m4v3(s->raw_centroid, target->obmat, s->centroid);

	for (v = 0, mv = s->mvert; v < s->totvert; v++, mv++)
	{
		//mul_v3_v3(mv->co, size);

		//shrink the shard ? (and take centroid diff into account here, too)
		BLI_space_transform_apply(&trans, mv->co);

		//add_v3_v3(mv->co, target->loc);
		//sub_v3_v3(mv->co, s->raw_centroid);
	}

//	BLI_space_transform_apply(&trans, s->raw_centroid);
	BLI_space_transform_apply(&trans, s->centroid);

	s = BKE_custom_data_to_shard(s, dm);
	BKE_shard_calc_minmax(s);

	if (do_free && dm)
	{
		dm->needsFree = 1;
		dm->release(dm);
	}

	return s;
}

static void fracture_update_shards(FractureModifierData *fmd, Shard *s)
{
	FracMesh* fm;

	if (!fmd->frac_mesh)
	{
		fmd->frac_mesh = BKE_create_fracture_container();
		fmd->frac_mesh->progress_counter = 0; //XXXX ABUSE this for vertstart now, threading doesnt work anyway yet
		fmd->matstart = 1; //TODO, is this 1-based ?
	}

	fm = fmd->frac_mesh;
	BLI_addtail(&fm->shard_map, s);
	s->shard_id = fm->shard_count;
	fm->shard_count++;
}

static MeshIsland* fracture_shard_to_island(FractureModifierData *fmd, Shard *s, int vertstart, float quat[4])
{
	MeshIsland *mi;
	int k = 0, j = 0, totvert;
	MVert *mverts = NULL, *verts, *mv;

	//create mesh island and intialize
	mi = MEM_callocN(sizeof(MeshIsland), "meshIsland");
	BLI_addtail(&fmd->meshIslands, mi);
	mi->participating_constraints = NULL;
	mi->participating_constraint_count = 0;
	mi->thresh_weight = 0.0f;
	mi->ground_weight = 0.0f;
	mi->vertex_count = s->totvert;
	mi->totcol = 0;
	mi->totdef = 0;

	//link up the visual mesh verts
	mi->vertices_cached = MEM_mallocN(sizeof(MVert *) * s->totvert, "vert_cache");
	if (fmd->visible_mesh_cached) /*ensure to be NULL in "pack, unpack" methods */
		mverts = CDDM_get_verts(fmd->visible_mesh_cached);
	mi->vertex_indices = MEM_mallocN(sizeof(int) * mi->vertex_count, "mi->vertex_indices");

	for (k = 0; k < s->totvert; k++) {
		if (mverts)
		{
			mi->vertices_cached[k] = mverts + vertstart + k;
		}
		else
		{
			mi->vertices_cached[k] = NULL;
		}
		mi->vertex_indices[k] = vertstart + k;
	}

	//some dummy setup, necessary here ?
	mi->locs = MEM_mallocN(sizeof(float)*3, "mi->locs");
	mi->rots = MEM_mallocN(sizeof(float)*4, "mi->rots");
	mi->frame_count = 0;
	if (fmd->modifier.scene && fmd->modifier.scene->rigidbody_world)
	{
		/*modifier might have no linked scene yet after creation on an inactive layer */
		/*so just try a fallback here */
		mi->start_frame = fmd->modifier.scene->rigidbody_world->pointcache->startframe;
	}
	else
	{
		mi->start_frame = 1;
	}

	mi->physics_mesh = BKE_shard_create_dm(s, true);
	totvert = mi->physics_mesh->getNumVerts(mi->physics_mesh);
	verts = mi->physics_mesh->getVertArray(mi->physics_mesh);

	mi->vertco = MEM_mallocN(sizeof(float) * 3 * totvert, "vertco");
	mi->vertno = MEM_mallocN(sizeof(short) * 3 * totvert, "vertno");

	for (mv = verts, j = 0; j < totvert; mv++, j++) {
		short no[3];

		mi->vertco[j * 3] = mv->co[0];
		mi->vertco[j * 3 + 1] = mv->co[1];
		mi->vertco[j * 3 + 2] = mv->co[2];

		copy_v3_v3_short(no, mv->no);

		mi->vertno[j * 3] = no[0];
		mi->vertno[j * 3 + 1] = no[1];
		mi->vertno[j * 3 + 2] = no[2];

		/* then eliminate centroid in vertex coords*/
		sub_v3_v3(mv->co, s->centroid);

		mul_qt_v3(quat, mv->co);
	}

	copy_v3_v3(mi->centroid, s->centroid);
	mi->id = s->shard_id;
	mi->bb = BKE_boundbox_alloc_unit();
	BKE_boundbox_init_from_minmax(mi->bb, s->min, s->max);
	mi->particle_index = -1;

	//this info isnt necessary here... constraints will be provided too !
	mi->neighbor_ids = s->neighbor_ids;
	mi->neighbor_count = s->neighbor_count;

	return mi;
}

int BKE_fracture_update_visual_mesh(FractureModifierData *fmd, Object *ob, bool do_custom_data)
{
	MeshIsland *mi;
	DerivedMesh *dm = fmd->visible_mesh_cached;
	int vertstart = 0, totvert = 0, totpoly = 0, polystart = 0, matstart = 1, defstart = 0, loopstart = 0;
	MVert *mv = NULL;
	MPoly *mp = NULL, *mpoly = NULL, *ppoly = NULL, *pp = NULL, *spoly = NULL, *sp = NULL, *tpoly = NULL, *tp = NULL;
	int i = 0, j = 0;
	MDeformVert *dvert = NULL;
	Mesh *me = (Mesh*)ob->data;
	Shard *s, *t;

	if (dm)
	{
		vertstart = dm->getNumVerts(dm);
		dm->needsFree = 1;
		dm->release(dm);
		dm = fmd->visible_mesh_cached = NULL;
	}

	fmd->visible_mesh_cached = create_dm(fmd, do_custom_data, fmd->pack_storage.first != NULL);

	if (!fmd->visible_mesh_cached)
		return 0;

	//store start mesh in order to be able to change autohide dist based on it later in sim too !
	if (fmd->dm) {
		fmd->dm->needsFree = 1;
		fmd->dm->release(fmd->dm);
		fmd->dm = NULL;
	}

	fmd->dm = CDDM_copy(fmd->visible_mesh_cached);

	dm = fmd->visible_mesh_cached;
	mv = dm->getVertArray(dm);
	totvert = dm->getNumVerts(dm);
	mpoly = dm->getPolyArray(dm);
	dvert = CustomData_get_layer(&dm->vertData, CD_MDEFORMVERT);

	CustomData_merge(&dm->loopData, &me->ldata, CD_MASK_MLOOPUV, CD_CALLOC, dm->getNumLoops(dm));
	CustomData_merge(&dm->polyData, &me->pdata, CD_MASK_MTEXPOLY, CD_CALLOC, dm->getNumPolys(dm));

	s = fmd->frac_mesh->shard_map.first;
	t = fmd->pack_storage.first;

	//update existing island's vert refs, if any...should have used indexes instead :S
	for (mi = fmd->meshIslands.first; mi; mi = mi->next)
	{
		MVert *pvert = mi->physics_mesh->getVertArray(mi->physics_mesh);
		float inv_size[3] = {1.0f, 1.0f, 1.0f};
		//Shard *s = BLI_findlink(&fmd->frac_mesh->shard_map, mi->id);
		//Shard *t = BLI_findlink(&fmd->pack_storage, mi->id);

		//if (!s)
		//	continue;

		//CustomData_copy_data(&dm->loopData, &me->ldata, loopstart, loopstart, s->totloop);
		//CustomData_copy_data(&dm->polyData, &me->pdata, polystart, polystart, s->totpoly);

		inv_size[0] = 1.0f / s->impact_size[0];
		inv_size[1] = 1.0f / s->impact_size[1];
		inv_size[2] = 1.0f / s->impact_size[2];

		for (i = 0; i < mi->vertex_count; i++)
		{
			//just update pointers, dont need to reallocate something
			MVert *v = NULL, *pv = NULL;
			int index;

			//also correct indexes
			if (mi->vertex_indices[i] >= totvert)
			{
				index = mi->vertex_indices[i];
				mi->vertex_indices[i] -= (vertstart - totvert);
				printf("I: %d, O: %d, N: %d\n", i, index, mi->vertex_indices[i]);
			}

			index = mi->vertex_indices[i];
			v = mv + index;
			mi->vertices_cached[i] = v;

			pv = pvert + i;
			mul_v3_v3(pv->co, inv_size);

			//transform vertex properly ? compensate for shrunken shard ?
			//sub_v3_v3v3(loc, mi->centroid, s->raw_centroid);
			//loc_quat_size_to_mat4(mat, loc , rot, s->impact_size);

			//invert_m4_m4(imat, mat);
#if 0
			pv = pvert + i;
			mul_m4_v3(imat, pv->co);
#endif

			//eliminate shrink but take also difference in centroids into account here
			//sub_v3_v3(v->co, s->centroid);
			//mul_m4_v3(imat, v->co);
			//add_v3_v3(v->co, s->centroid);

//			sub_v3_v3(v->co, s->centroid);
//			mul_v3_v3(v->co, inv_size);
//			add_v3_v3(v->co, s->centroid);

			//printf("%d %d\n", index, dm->getNumVerts(dm));

			//hrm perhaps we need to update rest coordinates, too...
			mi->vertco[3*i] = v->co[0];
			mi->vertco[3*i+1] = v->co[1];
			mi->vertco[3*i+2] = v->co[2];

			mi->vertno[3*i] = v->no[0];
			mi->vertno[3*i+1] = v->no[1];
			mi->vertno[3*i+2] = v->no[2];

			if (ob && dvert)
			{
				int l;
				MDeformVert *dv = dvert + mi->vertex_indices[i];
				if (dv && dv->dw)
				{
					for (l = 0; l < dv->totweight; l++)
					{
						MDeformWeight *dw = dv->dw;
						//refill mapping data, to make it accessible for each vert (for dumb mapping function)
						int key = defstart + l;
						int index = GET_INT_FROM_POINTER(BLI_ghash_lookup(fmd->defgrp_index_map, SET_INT_IN_POINTER(key)));
						//printf("Got: %d %d\n", key, index);
						if (dw->def_nr == l)
							dw->def_nr = index;
					}

					//XXX TODO store this on physics mesh too ? to be able to reload it from blend
				}
			}
		}

		defstart += mi->totdef;

		totpoly = mi->physics_mesh->getNumPolys(mi->physics_mesh);
		ppoly = mi->physics_mesh->getPolyArray(mi->physics_mesh);
		spoly = s->mpoly;
		if (t) {
			tpoly = t->mpoly;
		}

		for (j = 0, mp = mpoly + polystart, pp = ppoly, sp = spoly; j < totpoly; j++, mp++, pp++, sp++)
		{
			/* material index lookup and correction, avoid having the same material in different slots */
			int index = GET_INT_FROM_POINTER(BLI_ghash_lookup(fmd->material_index_map,
			                                 SET_INT_IN_POINTER(mp->mat_nr + matstart)));

			if (index > 0)
				index--;

			mp->mat_nr = index;
			//store this on physics mesh as well, and on shard too so for being able to reload it from blend later (without
			// having a materialmap then)
			pp->mat_nr = index;
			sp->mat_nr = index;

			//also dont forget pack storage
			if (tpoly) {
				tp = tpoly + j;
				tp->mat_nr = index;
			}
		}

		/* fortunately we know how many faces "belong" to this meshisland, too */
		polystart += totpoly;
		matstart += mi->totcol;
		loopstart += s->totloop;

		if (s) {
			s = s->next;
		}

		if (t) {
			t = t->next;
		}
	}

	return vertstart;
}

int fracture_collect_defgrp(Object* o, Object* ob, int defstart, GHash** def_index_map)
{
	bDeformGroup *vgroup, *ngroup;
	int k = 0;

	/* create vertexgroups on new object, if they dont exist already there*/
	for (vgroup = o->defbase.first; vgroup; vgroup = vgroup->next) {
		int index = defgroup_name_index(ob, vgroup->name);
		int key = defstart + k;

		if (index == -1) {
			// old group index + defstart to make it somehow linearized
			ngroup = MEM_callocN(sizeof(bDeformGroup), "collect deformGroup");
			memcpy(ngroup, vgroup, sizeof(bDeformGroup));
			BLI_addtail(&ob->defbase, ngroup);
			index = BLI_listbase_count(&ob->defbase)-1;
		}

		if (!BLI_ghash_haskey(*def_index_map, SET_INT_IN_POINTER(key)))
			BLI_ghash_insert(*def_index_map, SET_INT_IN_POINTER(key), SET_INT_IN_POINTER(index));

		k++;
	}

	if (ob->defbase.first && ob->actdef == 0)
		ob->actdef = 1;

	return k;
}

short BKE_fracture_collect_materials(Object* o, Object* ob, int matstart, GHash** mat_index_map)
{
	short *totcolp = NULL;
	Material ***matarar = NULL;
	int j;

	/* append materials to target object, if not existing yet */
	totcolp = give_totcolp(o);
	matarar = give_matarar(o);

	for (j = 0; j < (*totcolp); j++)
	{
		void *key;
		int index = BKE_object_material_slot_find_index(ob, (*matarar)[j]);
		if (index == 0)
		{
			index = ob->totcol+1;
			assign_material(ob, (*matarar)[j], index, BKE_MAT_ASSIGN_USERPREF);
		}

		key = SET_INT_IN_POINTER(matstart+j);
		if (!BLI_ghash_haskey(*mat_index_map, key))
			BLI_ghash_insert(*mat_index_map, key, SET_INT_IN_POINTER(index));
	}

	return (*totcolp);
}

/* was attempt to carry over packed shards from external mode to other modes like prefracture or dynamic */
void pack_storage_add(FractureModifierData *fmd, Shard* s)
{
	Shard *t = BKE_fracture_shard_copy(s);
	BLI_addtail(&fmd->pack_storage, t);
}

void fracture_collect_layer(CustomData* src, CustomData *dst, int totelem, int cd_type, int dst_offset, int count)
{
	int layerstart = CustomData_get_layer_index(src, cd_type);
	int totlayer = CustomData_number_of_layers(src, cd_type);
	int j;

	for (j = 0; j < totlayer; j++)
	{
		const char *name = CustomData_get_layer_name(src, cd_type, j);

		//find index of named layer in dst mesh
		int index = CustomData_get_named_layer_index(dst, cd_type, name);
		if (index == -1)
		{
			//add layer if not there
			//void *layer = CustomData_get_layer_named(src, cd_type, name);
			CustomData_add_layer_named(dst, cd_type, CD_CALLOC, NULL, totelem, name);
		}

		index = CustomData_get_named_layer_index(dst, cd_type, name);
		CustomData_copy_data_layer(src, dst, j+layerstart, index, 0, dst_offset, count);
	}
}

void fracture_collect_layers(Shard* s, DerivedMesh *dm, int vertstart, int polystart, int loopstart, int edgestart)
{
	int totloop = dm->getNumLoops(dm);
	int totvert = dm->getNumVerts(dm);
	int totpoly = dm->getNumPolys(dm);
	int totedge = dm->getNumEdges(dm);

	fracture_collect_layer(&s->vertData, &dm->vertData, totvert, CD_MDEFORMVERT, vertstart, s->totvert);
	fracture_collect_layer(&s->loopData, &dm->loopData, totloop, CD_MLOOPUV, loopstart, s->totloop);
	fracture_collect_layer(&s->polyData, &dm->polyData, totpoly, CD_MTEXPOLY, polystart, s->totpoly);
	fracture_collect_layer(&s->edgeData, &dm->edgeData, totedge, CD_CREASE, edgestart, s->totedge);
	fracture_collect_layer(&s->edgeData, &dm->edgeData, totedge, CD_BWEIGHT, edgestart, s->totedge);
	//fracture_collect_layer(&s->edgeData, &dm->edgeData, totedge, CD_MEDGE, edgestart, s->totedge);
	fracture_collect_layer(&s->vertData, &dm->vertData, totvert, CD_PROP_FLT, vertstart, s->totvert);
}

/* for "external" mode ! basically this packs an object into the FM mesh */
MeshIsland* BKE_fracture_mesh_island_add(FractureModifierData *fmd, Object* own, Object *target)
{
	MeshIsland *mi;
	Shard *s;
	int vertstart = 0;
	short totcol = 0, totdef = 0;
	float loc[3], quat[4], iquat[4];

	if (fmd->fracture_mode != MOD_FRACTURE_EXTERNAL || own->type != OB_MESH || !own->data)
		return NULL;

	if (target->type != OB_MESH || !target->data)
		return NULL;

	//lets see whether we need to add loc here too XXX TODO
	mat4_to_loc_quat(loc, quat, target->obmat);

	s = fracture_object_to_shard(own, target);
	copy_v3_v3(s->centroid, loc);

	fracture_update_shards(fmd, s);

	vertstart = fmd->frac_mesh->progress_counter;
	fmd->frac_mesh->progress_counter += s->totvert;

	//hrm need to rebuild ALL islands since vertex refs are bonkers now after mesh has changed
	invert_qt_qt(iquat, quat);
	mi = fracture_shard_to_island(fmd, s, vertstart, iquat);

	copy_qt_qt(mi->rot, quat);
	copy_v3_v3(mi->centroid, loc);

	mi->rigidbody = BKE_rigidbody_create_shard(fmd->modifier.scene, own, target, mi);
	if (mi->rigidbody)
	{
		mi->rigidbody->meshisland_index = mi->id;
	}

	BLI_strncpy(mi->name, target->id.name + 2, MAX_ID_NAME - 2);

	//handle materials
	if (!fmd->material_index_map)
	{
		fmd->material_index_map = BLI_ghash_int_new("mat_index_map");
		fmd->matstart = 1;
	}

	totcol = BKE_fracture_collect_materials(target, own, fmd->matstart, &fmd->material_index_map);
	if (totcol < 0)
	    totcol = 0;
	fmd->matstart += totcol;
	mi->totcol = totcol;

	/*XXXXX TODO deal with material deletion, and reorder (in material code) */

	//handle vertexgroups, too
	if (!fmd->defgrp_index_map)
	{
		fmd->defgrp_index_map = BLI_ghash_int_new("defgrp_index_map");
		fmd->defstart = 0;
	}

	totdef = fracture_collect_defgrp(target, own, fmd->defstart, &fmd->defgrp_index_map);
	if (totdef < 0)
		totdef = 0;
	fmd->defstart += totdef;
	mi->totdef = totdef;

	//XXX TODO handle UVs, shapekeys and more ?
//	fracture_collect_uv_tex(target, own);

	//add shard to pack storage
	pack_storage_add(fmd, s);

	return mi;
}

void BKE_fracture_free_mesh_island(FractureModifierData *rmd, MeshIsland *mi, bool remove_rigidbody)
{
	if (mi->physics_mesh) {
		mi->physics_mesh->needsFree = 1;
		mi->physics_mesh->release(mi->physics_mesh);
		mi->physics_mesh = NULL;
	}

	if (mi->rigidbody) {
		if (remove_rigidbody && rmd->modifier.scene)
			BKE_rigidbody_remove_shard(rmd->modifier.scene, mi);
		MEM_freeN(mi->rigidbody);
		mi->rigidbody = NULL;
	}

	if (mi->vertco) {
		MEM_freeN(mi->vertco);
		mi->vertco = NULL;
	}

	if (mi->vertno) {
		MEM_freeN(mi->vertno);
		mi->vertno = NULL;
	}

	if (mi->vertices) {
		//MEM_freeN(mi->vertices);
		mi->vertices = NULL; /*borrowed only !!!*/
	}

	if (mi->vertices_cached) {
		MEM_freeN(mi->vertices_cached);
		mi->vertices_cached = NULL;
	}

	if (mi->bb != NULL) {
		MEM_freeN(mi->bb);
		mi->bb = NULL;
	}

	if (mi->participating_constraints != NULL) {
		int i = 0;
		for (i = 0; i < mi->participating_constraint_count; i++)
		{
			RigidBodyShardCon *con = mi->participating_constraints[i];
			if (con) {
				con->mi1 = NULL;
				con->mi2 = NULL;
			}
		}

		MEM_freeN(mi->participating_constraints);
		mi->participating_constraints = NULL;
		mi->participating_constraint_count = 0;
	}

	if (mi->vertex_indices) {
		MEM_freeN(mi->vertex_indices);
		mi->vertex_indices = NULL;
	}

	if (mi->rots) {
		MEM_freeN(mi->rots);
		mi->rots = NULL;
	}

	if (mi->locs) {
		MEM_freeN(mi->locs);
		mi->locs = NULL;
	}

	if (mi->acc_sequence)
	{
		MEM_freeN(mi->acc_sequence);
		mi->acc_sequence = NULL;
	}

	mi->frame_count = 0;

	MEM_freeN(mi);
	mi = NULL;
}

void pack_storage_remove(FractureModifierData *fmd, Shard *s)
{
	Shard *t = fmd->pack_storage.first;
	while(t)
	{
		if (t->shard_id == s->shard_id)
		{
			BLI_remlink(&fmd->pack_storage, t);
			BKE_shard_free(t, true);
			break;
		}

		t = t->next;
	}
}

void BKE_fracture_mesh_island_remove(FractureModifierData *fmd, MeshIsland *mi)
{
	if (BLI_listbase_is_single(&fmd->meshIslands))
	{
		BKE_fracture_mesh_island_remove_all(fmd);
		return;
	}

	if (fmd->frac_mesh && mi)
	{
		int index = BLI_findindex(&fmd->meshIslands, mi);
		Shard *s = BLI_findlink(&fmd->frac_mesh->shard_map, index);
		if (s)
		{
			int i;
			BLI_remlink(&fmd->frac_mesh->shard_map, s);
			pack_storage_remove(fmd, s);
			BKE_shard_free(s, true);
			fmd->frac_mesh->shard_count--;

			BLI_remlink(&fmd->meshIslands, mi);
			for (i = 0; i < mi->participating_constraint_count; i++)
			{
				RigidBodyShardCon *con = mi->participating_constraints[i];
				BLI_remlink(&fmd->meshConstraints, con);
				BKE_rigidbody_remove_shard_con(fmd->modifier.scene, con);
			}

			BKE_fracture_free_mesh_island(fmd, mi, true);
		}
	}
}

void pack_storage_remove_all(FractureModifierData*fmd)
{
	Shard *s;
	while (fmd->pack_storage.first) {
		s = fmd->pack_storage.first;
		BLI_remlink(&fmd->pack_storage, s);
		BKE_shard_free(s, true);
	}
}

void BKE_fracture_mesh_island_remove_all(FractureModifierData *fmd)
{
	MeshIsland *mi;

	//free all shards
	BKE_fracmesh_free(fmd->frac_mesh, true);
	MEM_freeN(fmd->frac_mesh);
	fmd->frac_mesh = NULL;

	//free pack storage
	pack_storage_remove_all(fmd);

	//free all constraints first
	BKE_free_constraints(fmd);

	//free all meshislands
	while (fmd->meshIslands.first) {
		mi = fmd->meshIslands.first;
		BLI_remlink(&fmd->meshIslands, mi);
		BKE_fracture_free_mesh_island(fmd, mi, true);
	}

	fmd->meshIslands.first = NULL;
	fmd->meshIslands.last = NULL;

	//free visual_mesh
	if (fmd->visible_mesh_cached)
	{
		fmd->visible_mesh_cached->needsFree = 1;
		fmd->visible_mesh_cached->release(fmd->visible_mesh_cached);
		fmd->visible_mesh_cached = NULL;
	}

	if (fmd->material_index_map)
	{
		BLI_ghash_free(fmd->material_index_map, NULL, NULL);
		fmd->material_index_map = NULL;
		fmd->matstart = 1;
	}

	if (fmd->defgrp_index_map)
	{
		BLI_ghash_free(fmd->defgrp_index_map, NULL, NULL);
		fmd->defgrp_index_map = NULL;
		fmd->defstart = 0;
	}
}

RigidBodyShardCon *BKE_fracture_mesh_islands_connect(FractureModifierData *fmd, MeshIsland *mi1, MeshIsland *mi2, short con_type)
{
	RigidBodyShardCon *rbsc;

	if (mi1 == NULL || mi2 == NULL)
		return NULL;

	rbsc = BKE_rigidbody_create_shard_constraint(fmd->modifier.scene, con_type, false);
	rbsc->mi1 = mi1;
	rbsc->mi2 = mi2;

	if (fmd->fracture_mode == MOD_FRACTURE_EXTERNAL)
	{
		/* disable breaking flag here by default, only enable later via python if necessary */
		rbsc->flag &= ~RBC_FLAG_USE_BREAKING;

		/* also delete all other "default" flags here, let them being overriden from python too */
		//rbsc->flag &= ~RBC_FLAG_ENABLED;
		rbsc->flag |= RBC_FLAG_DISABLE_COLLISIONS;

#if 0
		/* and dont allow to let constrained objects collide per default, as with regular constraints */
		rbsc->flag |= RBC_FLAG_DISABLE_COLLISIONS;
#endif
	}

	/* moved default meshconstraint pos calculation here to creation, so you can override it later on*/
	/* do this for all constraints */
	/* location for fixed constraints doesnt matter, so keep old setting */
	if (rbsc->type == RBC_TYPE_FIXED) {
		copy_v3_v3(rbsc->pos, rbsc->mi1->rigidbody->pos);
	}
	else {
		/* else set location to center */
		add_v3_v3v3(rbsc->pos, rbsc->mi1->rigidbody->pos, rbsc->mi2->rigidbody->pos);
		mul_v3_fl(rbsc->pos, 0.5f);
	}

	copy_qt_qt(rbsc->orn, rbsc->mi1->rigidbody->orn);

	if (BLI_listbase_is_empty(&fmd->meshConstraints))
	{
		fmd->constraint_count = 0;
	}

	BLI_addtail(&fmd->meshConstraints, rbsc);
	fmd->constraint_count++;

#if 0
	if (index > -1)
	{
		rbsc->id = index;
	}
	else
	{
		rbsc->id = fmd->constraint_count-1;
	}
#endif

	/* store constraints per meshisland too, to allow breaking percentage */
	if (mi1->participating_constraints == NULL) {
		mi1->participating_constraints = MEM_mallocN(sizeof(RigidBodyShardCon *), "part_constraints_mi1");
		mi1->participating_constraints[0] = rbsc;
		mi1->participating_constraint_count = 1;
	}
	else
	{
		mi1->participating_constraints = MEM_reallocN(mi1->participating_constraints, sizeof(RigidBodyShardCon *) * (mi1->participating_constraint_count + 1));
		mi1->participating_constraints[mi1->participating_constraint_count] = rbsc;
		mi1->participating_constraint_count++;
	}

	if (mi2->participating_constraints == NULL) {
		mi2->participating_constraints = MEM_mallocN(sizeof(RigidBodyShardCon *), "part_constraints_mi2");
		mi2->participating_constraints[0] = rbsc;
		mi2->participating_constraint_count = 1;
	}
	else
	{
		mi2->participating_constraints = MEM_reallocN(mi2->participating_constraints, sizeof(RigidBodyShardCon *) * (mi2->participating_constraint_count + 1));
		mi2->participating_constraints[mi2->participating_constraint_count] = rbsc;
		mi2->participating_constraint_count++;
	}

	return rbsc;
}

static void remove_participants(RigidBodyShardCon* con, MeshIsland *mi)
{
	RigidBodyShardCon **cons;
	/* Probably wrong, would need to shrink array size... listbase would have been better here */
	/* info not necessary so omit */
	int count = 0;

	if (!mi)
		return;

	count = mi->participating_constraint_count;

	if (count > 1)
	{
		int i, j = 0;
		mi->participating_constraint_count--;
		cons = MEM_callocN(sizeof(RigidBodyShardCon *) * (count-1) , "temp cons");
		for (i = 0; i < mi->participating_constraint_count; i++)
		{
			if (mi->participating_constraints[i] != con)
			{
				cons[j] = con;
				j++;
			}
		}

		MEM_freeN(mi->participating_constraints);
		mi->participating_constraints = cons;
	}
}

void BKE_fracture_mesh_constraint_remove(FractureModifierData *fmd, RigidBodyShardCon* con)
{
	remove_participants(con, con->mi1);
	remove_participants(con, con->mi2);

	BLI_remlink(&fmd->meshConstraints, con);
	BKE_rigidbody_remove_shard_con(fmd->modifier.scene, con);
	MEM_freeN(con);
	if (fmd->constraint_count > 0)
	{
		fmd->constraint_count--;
	}
}

void BKE_fracture_mesh_constraint_remove_all(FractureModifierData *fmd)
{
	BKE_free_constraints(fmd);
	fmd->constraint_count = 0;
}

/* flush a hflag to from verts to edges/faces */
void BKE_bm_mesh_hflag_flush_vert(BMesh *bm, const char hflag)
{
	BMEdge *e;
	BMLoop *l_iter;
	BMLoop *l_first;
	BMFace *f;

	BMIter eiter;
	BMIter fiter;

	int ok;

	BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
		if (BM_elem_flag_test(e->v1, hflag) &&
		    BM_elem_flag_test(e->v2, hflag))
		{
			BM_elem_flag_enable(e, hflag);
		}
		else {
			BM_elem_flag_disable(e, hflag);
		}
	}
	BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
		ok = true;
		l_iter = l_first = BM_FACE_FIRST_LOOP(f);
		do {
			if (!BM_elem_flag_test(l_iter->v, hflag)) {
				ok = false;
				break;
			}
		} while ((l_iter = l_iter->next) != l_first);

		BM_elem_flag_set(f, hflag, ok);
	}
}

void BKE_meshisland_constraint_create(FractureModifierData* fmd, MeshIsland *mi1, MeshIsland *mi2, int con_type, float thresh)
{
	RigidBodyShardCon *rbsc;
	rbsc = BKE_rigidbody_create_shard_constraint(fmd->modifier.scene, con_type, fmd->fracture_mode != MOD_FRACTURE_DYNAMIC);
	rbsc->mi1 = mi1;
	rbsc->mi2 = mi2;

	BLI_snprintf(rbsc->name, 64, "%s-%s", rbsc->mi1->name, rbsc->mi2->name);

	if (thresh == 0 || fmd->use_breaking == false) {
		rbsc->flag &= ~RBC_FLAG_USE_BREAKING;
	}

	if (!fmd->use_constraint_collision) {
		rbsc->flag |= RBC_FLAG_DISABLE_COLLISIONS;
	}
	else {
		rbsc->flag &= ~RBC_FLAG_DISABLE_COLLISIONS;
	}

	if ((mi1->particle_index != -1) && (mi2->particle_index != -1) &&
		(mi1->particle_index == mi2->particle_index))
	{
		if (fmd->cluster_count > 1) {
			rbsc->breaking_threshold = fmd->cluster_breaking_threshold;
		}
		else {
			rbsc->breaking_threshold = thresh;
		}
	}
	else
	{
		if ((mi1->particle_index != -1) && (mi2->particle_index != -1) &&
			(mi1->particle_index != mi2->particle_index))
		{
			/* set a different type of constraint between clusters */
			rbsc->type = fmd->cluster_constraint_type;
		}
		rbsc->breaking_threshold = thresh;
	}

	if (fmd->thresh_defgrp_name[0]) {
		/* modify maximum threshold by minimum weight */
		rbsc->breaking_threshold = thresh * MIN2(mi1->thresh_weight, mi2->thresh_weight);
	}

	BLI_addtail(&fmd->meshConstraints, rbsc);

	if ((mi1->object_index == -1) && (mi2->object_index == -1))
	{
		/* store constraints per meshisland too, to allow breaking percentage */
		if (mi1->participating_constraints == NULL) {
			mi1->participating_constraints = MEM_callocN(sizeof(RigidBodyShardCon *), "part_constraints_mi1");
			mi1->participating_constraint_count = 0;
		}
		mi1->participating_constraints = MEM_reallocN(mi1->participating_constraints,
		                                              sizeof(RigidBodyShardCon *) * (mi1->participating_constraint_count + 1));
		mi1->participating_constraints[mi1->participating_constraint_count] = rbsc;
		mi1->participating_constraint_count++;

		if (mi2->participating_constraints == NULL) {
			mi2->participating_constraints = MEM_callocN(sizeof(RigidBodyShardCon *), "part_constraints_mi2");
			mi2->participating_constraint_count = 0;
		}
		mi2->participating_constraints = MEM_reallocN(mi2->participating_constraints,
		                                              sizeof(RigidBodyShardCon *) * (mi2->participating_constraint_count + 1));
		mi2->participating_constraints[mi2->participating_constraint_count] = rbsc;
		mi2->participating_constraint_count++;
	}
}

void BKE_update_acceleration_map(FractureModifierData *fmd, MeshIsland* mi, Object* ob, int ctime, float acc, RigidBodyWorld *rbw)
{
	const int acc_defgrp_index = defgroup_name_index(ob, fmd->acceleration_defgrp_name);
	DerivedMesh *dm = fmd->visible_mesh_cached;
	MDeformVert *dvert = NULL, *dv = NULL;
	MDeformWeight *dw = NULL;
	float weight = 0.0f, denom;
	int i = 0, w = 0;
	int totvert = dm ? dm->getNumVerts(dm) : 0;

	if (!dm)
		return;

	dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);

	if (dvert == NULL)
	{
		dvert = CustomData_add_layer(&dm->vertData, CD_MDEFORMVERT, CD_CALLOC,
	                             NULL, totvert);
	}

	//calculate weight from force...
	denom = fmd->max_acceleration - fmd->min_acceleration;

	//sanity check
	if (denom == 0.0f)
		denom = 1.0f;

	//if (mi->acc_sequence)
	{
		weight = (acc - fmd->min_acceleration) / denom;

		for (i = 0; i < mi->vertex_count; i++)
		{
			dv = dvert + mi->vertex_indices[i];
			if (dv) {
				if (dv->dw == NULL && acc_defgrp_index >= 0) {
					defvert_add_index_notest(dv, acc_defgrp_index, 0.0f);
				}

				for (dw = dv->dw, w = 0; w < dv->totweight; dw++, w++)
				{
					if (dw->def_nr == acc_defgrp_index) {

						if (weight >= 0.0f && weight <= 1.0f) {
							dw->weight = weight;
						}
						else if (ctime == rbw->pointcache->startframe) {
							dw->weight = 0.0f;
						}

						if (ctime == rbw->ltime + 1)
							dw->weight *= fmd->acceleration_fade;
					}
				}
			}
		}
	}
}

void BKE_update_velocity_layer(FractureModifierData *fmd, MeshIsland *mi)
{
	DerivedMesh *dm = fmd->visible_mesh_cached;
	float *velX=NULL, *velY=NULL, *velZ = NULL;
	RigidBodyOb *rbo = mi->rigidbody;
	Shard *s, *t = NULL;
	void *pX, *pY, *pZ, *spX = NULL, *spY = NULL, *spZ = NULL;
	float *sX=NULL, *sY=NULL, *sZ=NULL;
	int i = 0;
	ListBase *lb;

	if (!dm)
		return;

	//XXX TODO deal with split shards to islands etc, here take only "real" shards for now
	if (fmd->shards_to_islands) {
		lb = &fmd->islandShards;
	}
	else {
		lb = &fmd->frac_mesh->shard_map;
	}

	for (s = lb->first; s; s = s->next)
	{
		if (s->shard_id == mi->id)
		{
			t = s;
			break;
		}
	}

	pX = CustomData_get_layer_named(&dm->vertData, CD_PROP_FLT, "velX");
	pY = CustomData_get_layer_named(&dm->vertData, CD_PROP_FLT, "velY");
	pZ = CustomData_get_layer_named(&dm->vertData, CD_PROP_FLT, "velZ");

	if (!pX ||!pY || !pZ)
		return;

	velX = (float*)pX;
	velY = (float*)pY;
	velZ = (float*)pZ;

	//XXX how to represent this in mblur ?
	//zero_v3(rbo->ang_vel);

	if (t)
	{
		spX = check_add_layer(NULL, &t->vertData, CD_PROP_FLT, t->totvert, "velX");
		spY = check_add_layer(NULL, &t->vertData, CD_PROP_FLT, t->totvert, "velY");
		spZ = check_add_layer(NULL, &t->vertData, CD_PROP_FLT, t->totvert, "velZ");
	}

	for (i = 0; i < mi->vertex_count; i++)
	{
		if (spX && spY && spZ)
		{
			sX = (float*)spX;
			sY = (float*)spY;
			sZ = (float*)spZ;

			sX[i] = rbo->lin_vel[0] + rbo->ang_vel[0];
			sY[i] = rbo->lin_vel[1] + rbo->ang_vel[1];
			sZ[i] = rbo->lin_vel[2] + rbo->ang_vel[2];
		}

		velX[mi->vertex_indices[i]] = rbo->lin_vel[0] + rbo->ang_vel[0];
		velY[mi->vertex_indices[i]] = rbo->lin_vel[1] + rbo->ang_vel[1];
		velZ[mi->vertex_indices[i]] = rbo->lin_vel[2] + rbo->ang_vel[2];
	}
}

/* gah, it could be that simple, if each mod handled its stuff itself */
static DerivedMesh *eval_mod_stack_simple(Object *ob)
{
	DerivedMesh *dm = NULL;
	ModifierData *md;

	if (ob->type != OB_MESH)
		return NULL;

	if (ob->data == NULL)
		return NULL;

	dm = CDDM_from_mesh(ob->data);

	for (md = ob->modifiers.first; md; md = md->next)
	{
		const ModifierTypeInfo *mti = modifierType_getInfo(md->type);
		if (mti->deformVerts && (md->mode & (eModifierMode_Realtime | eModifierMode_Render)))
		{
			float (*vertexCos)[3];
			int totvert = dm->getNumVerts(dm);

			vertexCos = MEM_callocN(sizeof(float) * 3 * totvert, "Vertex Cos");
			dm->getVertCos(dm, vertexCos);
			mti->deformVerts(md, ob, dm, vertexCos, totvert, 0);
			CDDM_apply_vert_coords(dm, vertexCos);
			MEM_freeN(vertexCos);
			CDDM_calc_normals(dm);
		}

		if (mti->applyModifier && (md->mode & (eModifierMode_Realtime | eModifierMode_Render)))
		{
			DerivedMesh *ndm;

			ndm = mti->applyModifier(md, ob, dm, 0);
			if (ndm != dm)
			{
				dm->needsFree = 1;
				dm->release(dm);
			}
			dm = ndm;
			CDDM_calc_normals(dm);
		}
	}

	return dm;
}

void activate(MeshIsland *mi, AnimBind *bind)
{
	bind->v = -1;
	bind->v1 = -1;
	bind->v2 = -1;
	bind->mi = -1;
	zero_v3(bind->no);
	zero_v3(bind->offset);
	unit_qt(bind->quat);

	if (mi->rigidbody->type == RBO_TYPE_ACTIVE)
	{
		RigidBodyOb* rbo = mi->rigidbody;

		rbo->flag &= ~RBO_FLAG_KINEMATIC;
		rbo->flag &= ~RBO_FLAG_KINEMATIC_BOUND;
		rbo->flag |= RBO_FLAG_NEEDS_VALIDATE;

		if (rbo->physics_object)
		{
			RB_body_set_mass(rbo->physics_object, rbo->mass);
			RB_body_set_kinematic_state(rbo->physics_object, false);
			RB_body_activate(rbo->physics_object);
		}
	}
}

void BKE_read_animated_loc_rot(FractureModifierData *fmd, Object *ob, bool do_bind)
{
	//to be called after rigidbodies have been actually created... from MOD_fracture.c
	//rotation is optional, remesher + particlesystem can provide it
	float *quatX, *quatY, *quatZ, *quatW;
	MVert *mvert = NULL;
	MPoly *mpoly = NULL;
	MLoop *mloop = NULL;
	MeshIsland *mi;
	DerivedMesh *dm = NULL;
	int totvert, count = 0, i = 0, *orig_index = NULL, totpoly, items;
	KDTree *tree = NULL;
	float anim_quat[4], anim_imat[4][4], imat[4][4];

	if (!fmd->anim_mesh_ob)
		return;

	if (fmd->anim_mesh_ob == ob)
		return;

	dm = eval_mod_stack_simple(fmd->anim_mesh_ob);

	if (!dm)
		return;

	totvert = dm->getNumVerts(dm);
	totpoly = dm->getNumPolys(dm);

	invert_m4_m4(anim_imat, fmd->anim_mesh_ob->obmat);
	invert_m4_m4(imat, ob->obmat);

	if (totvert == 0) {
		dm->release(dm);
		return;
	}

	if (do_bind) {

		items = totpoly > 0 ? totpoly : totvert;

		count = BLI_listbase_count(&fmd->meshIslands);
		tree = BLI_kdtree_new(items);

		fmd->anim_bind_len = count;
		if (fmd->anim_bind) {
			MEM_freeN(fmd->anim_bind);
		}

		//TODO, possibly a weak solution, but do we really want to store the length too ?
		fmd->anim_bind = MEM_mallocN(sizeof(AnimBind) * fmd->anim_bind_len, "anim_bind");
		for (i = 0; i < fmd->anim_bind_len; i++)
		{
			fmd->anim_bind[i].mi = -1;
			fmd->anim_bind[i].v = -1;
			fmd->anim_bind[i].v1 = -1;
			fmd->anim_bind[i].v2 = -1;
			zero_v3(fmd->anim_bind[i].offset);
			zero_v3(fmd->anim_bind[i].no);
			unit_qt(fmd->anim_bind[i].quat);
		}
	}

	i = 0;
	mvert = dm->getVertArray(dm);
	mpoly = dm->getPolyArray(dm);
	mloop = dm->getLoopArray(dm);
	if (do_bind)
	{
		if (totpoly > 0)
		{
			//poly based bind
			for (i = 0; i < totpoly; i++)
			{
				float co[3];
				copy_v3_v3(co, mvert[mloop[mpoly[i].loopstart].v].co);
				BLI_kdtree_insert(tree, i, co);
			}
		}
		else
		{
			//no faces -> vertex based bind
			for (i = 0; i < totvert; i++)
			{
				float co[3];
				copy_v3_v3(co, mvert[i].co);
				BLI_kdtree_insert(tree, i, co);
			}
		}

		BLI_kdtree_balance(tree);
	}


	quatX = CustomData_get_layer_named(&dm->vertData, CD_PROP_FLT, "quatX");
	quatY = CustomData_get_layer_named(&dm->vertData, CD_PROP_FLT, "quatY");
	quatZ = CustomData_get_layer_named(&dm->vertData, CD_PROP_FLT, "quatZ");
	quatW = CustomData_get_layer_named(&dm->vertData, CD_PROP_FLT, "quatW");
	orig_index = CustomData_get_layer(&dm->vertData, CD_ORIGINDEX);

	//check vertexcount and islandcount, TODO for splitshards... there it might differ, ignore then for now
	//later do interpolation ? propagate to islands somehow then, not yet now...
	//also maybe skip for dynamic for now, since this is totally different

	//bind loop, bind the verts to the shards
	if (do_bind)
	{
		i = 0;
		for (mi = fmd->meshIslands.first; mi; mi = mi->next, i++)
		{
			KDTreeNearest n;
			float co[3], diff[3] = {0, 0, 0}, f_no[3];
			int j = 0;

			copy_v3_v3(co, mi->rigidbody->pos);
			mul_m4_v3(anim_imat, co);
			BLI_kdtree_find_nearest(tree, co, &n);

			if (n.dist <= fmd->anim_bind_limit || fmd->anim_bind_limit == 0)
			{
				if (totpoly > 0)
				{
					int v1, v2, v3;
					float limit = 0.0001f;
					MPoly *mp  = mpoly + n.index;
					MLoop *ml = mloop + mp->loopstart;
					BKE_mesh_calc_poly_normal(mp, ml, mvert, f_no);

					v1 = ml->v;
					v2 = (ml + 1)->v;
					v3 = (ml + 2)->v;

					if (mpoly[n.index].totloop < 3)
					{
						printf("Degenerate face, skipping\n");
						activate(mi, &fmd->anim_bind[i]);
						continue;
					}

					if (compare_v3v3(mvert[v1].co, mvert[v2].co, limit) ||
					    compare_v3v3(mvert[v1].co, mvert[v3].co, limit) ||
					    compare_v3v3(mvert[v2].co, mvert[v3].co, limit))
					{
						printf("Very close coordinates, skipping %d %d %d in %d\n", v1, v2, v3, mi->id);
						activate(mi, &fmd->anim_bind[i]);
						continue;
					}

					fmd->anim_bind[i].v = v1;
					fmd->anim_bind[i].v1 = v2;
					fmd->anim_bind[i].v2 = v3;
					fmd->anim_bind[i].poly = n.index;
					mi->rigidbody->flag |= RBO_FLAG_KINEMATIC_BOUND;
				}
				else {
					fmd->anim_bind[i].v = n.index;
					fmd->anim_bind[i].v1 = -1;
					fmd->anim_bind[i].v2 = -1;
					mi->rigidbody->flag |= RBO_FLAG_KINEMATIC_BOUND;
				}

				if (fmd->anim_bind[i].v != -1)
				{
					fmd->anim_bind[i].mi = i;
					sub_v3_v3v3(diff, n.co, co);

					copy_v3_v3(fmd->anim_bind[i].offset, diff);

					if ((fmd->anim_bind[i].v1 == -1 || fmd->anim_bind[i].v2 == -1)) {
						//fallback if not enough verts around
						normal_short_to_float_v3(fmd->anim_bind[i].no, mvert[n.index].no);
						normalize_v3(fmd->anim_bind[i].no);
					}
					else if (totpoly > 0) {
						tri_to_quat_ex(fmd->anim_bind[i].quat,
								mvert[fmd->anim_bind[i].v].co,
								mvert[fmd->anim_bind[i].v1].co,
								mvert[fmd->anim_bind[i].v2].co, f_no);
						copy_v3_v3(fmd->anim_bind[i].no, f_no);
					}
				}
			}
			else
			{
				activate(mi, &fmd->anim_bind[i]);
			}
		}

		if (tree)
			BLI_kdtree_free(tree);
	}
	else
	{
		mi = fmd->meshIslands.first;
		for (i = 0; i < fmd->anim_bind_len; i++, mi = mi->next)
		{
			float co[3];
			int index = -1;
			int vindex = -1;

			index = fmd->anim_bind[i].mi;
			vindex = fmd->anim_bind[i].v;

			if (index == -1 || vindex == -1)
			{
				activate(mi, &fmd->anim_bind[i]);
				continue;
			}

			//only let kinematic rbs do this, active ones are being taken care of by bullet
			if (mi && mi->rigidbody && (mi->rigidbody->flag & RBO_FLAG_KINEMATIC))
			{
				//the 4 rot layers *should* be aligned, caller needs to ensure !
				bool quats = quatX && quatY && quatZ && quatW;
				float quat[4], vec[3], no[3], off[3];
				int v = fmd->anim_bind[i].v;
				unit_qt(quat);

				if (v >= totvert) {
					activate(mi, &fmd->anim_bind[i]);
					continue;
				}

				if ((orig_index && orig_index[v] != v && (fmd->anim_bind[i].v1 == -1 || fmd->anim_bind[i].v2 == -1)))
				{
					activate(mi, &fmd->anim_bind[i]);
					continue;
				}

				copy_v3_v3(co, mvert[v].co);
				copy_v3_v3(off, fmd->anim_bind[i].offset);

				if (fmd->anim_mesh_rot)
				{
					if (quats)
					{
						quat[0] = quatX[v];
						quat[1] = quatY[v];
						quat[2] = quatZ[v];
						quat[3] = quatW[v];
					}
					else
					{
						copy_v3_v3(vec, fmd->anim_bind[i].no);
						if (fmd->anim_bind[i].v1 == -1 || fmd->anim_bind[i].v2 == -1) {
							//fallback if not enough verts around;
							normal_short_to_float_v3(no, mvert[v].no);
							normalize_v3(no);
							rotation_between_vecs_to_quat(quat, vec, no);
						}
						else {
							float rot[4], iquat[4], fno[3];
							MPoly *mp = mpoly + fmd->anim_bind[i].poly;
							MLoop *ml = mloop + mp->loopstart;
							BKE_mesh_calc_poly_normal(mp, ml, mvert, fno);

							tri_to_quat_ex(rot, mvert[fmd->anim_bind[i].v].co,
											  mvert[fmd->anim_bind[i].v1].co,
											  mvert[fmd->anim_bind[i].v2].co,
											  fno);
							invert_qt_qt(iquat, fmd->anim_bind[i].quat);
							mul_qt_qtqt(quat, rot, iquat);
						}
					}
				}

				mul_qt_v3(quat, off);
				sub_v3_v3(co, off);
				mul_m4_v3(fmd->anim_mesh_ob->obmat, co);

				copy_v3_v3(mi->rigidbody->pos, co);

				if (fmd->anim_mesh_rot)
				{
					float ob_quat[4];
					mat4_to_quat(ob_quat, ob->obmat);
					mul_qt_qtqt(quat, ob_quat, quat);
					copy_qt_qt(mi->rigidbody->orn, quat);
				}

				mi->rigidbody->flag |= RBO_FLAG_NEEDS_VALIDATE;
				if (mi->rigidbody->physics_object)
				{
					RB_body_set_loc_rot(mi->rigidbody->physics_object, mi->rigidbody->pos, mi->rigidbody->orn);
				}
			}
		}
	}

	if (dm)
		dm->release(dm);
}