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

paint_vertex.c « sculpt_paint « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 12eb1524515af8f0237eb32a8e27da64daafb7b0 (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
/*
 * ***** 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.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/sculpt_paint/paint_vertex.c
 *  \ingroup edsculpt
 */

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_memarena.h"

#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"

#include "DNA_armature_types.h"
#include "DNA_mesh_types.h"
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_brush_types.h"
#include "DNA_object_types.h"

#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"

#include "BKE_DerivedMesh.h"
#include "BKE_action.h"
#include "BKE_brush.h"
#include "BKE_context.h"
#include "BKE_depsgraph.h"
#include "BKE_deform.h"
#include "BKE_mesh.h"
#include "BKE_mesh_mapping.h"
#include "BKE_modifier.h"
#include "BKE_object.h"
#include "BKE_object_deform.h"
#include "BKE_paint.h"
#include "BKE_report.h"
#include "BKE_colortools.h"

#include "WM_api.h"
#include "WM_types.h"

#include "GPU_buffers.h"

#include "ED_armature.h"
#include "ED_object.h"
#include "ED_mesh.h"
#include "ED_screen.h"
#include "ED_view3d.h"

#include "paint_intern.h"  /* own include */


/* check if we can do partial updates and have them draw realtime
 * (without rebuilding the 'derivedFinal') */
static bool vertex_paint_use_fast_update_check(Object *ob)
{
	DerivedMesh *dm = ob->derivedFinal;

	if (dm) {
		Mesh *me = BKE_mesh_from_object(ob);
		if (me && me->mcol) {
			return (me->mcol == CustomData_get_layer(&dm->faceData, CD_MCOL));
		}
	}

	return FALSE;
}

/* if the polygons from the mesh and the 'derivedFinal' match
 * we can assume that no modifiers are applied and that its worth adding tessellated faces
 * so 'vertex_paint_use_fast_update_check()' returns TRUE */
static bool vertex_paint_use_tessface_check(Object *ob, Mesh *me)
{
	DerivedMesh *dm = ob->derivedFinal;

	if (me && dm) {
		return (me->mpoly == CustomData_get_layer(&dm->polyData, CD_MPOLY));
	}

	return FALSE;
}

static void update_tessface_data(Object *ob, Mesh *me)
{
	if (vertex_paint_use_tessface_check(ob, me)) {
		/* assume if these exist, that they are up to date & valid */
		if (!me->mcol || !me->mface) {
			/* should always be true */
			/* XXX Why this clearing? tessface_calc will reset it anyway! */
#if 0
			if (me->mcol) {
				memset(me->mcol, 255, 4 * sizeof(MCol) * me->totface);
			}
#endif

			/* create tessfaces because they will be used for drawing & fast updates */
			BKE_mesh_tessface_calc(me); /* does own call to update pointers */
		}
	}
	else {
		if (me->totface) {
			/* this wont be used, theres no need to keep it */
			BKE_mesh_tessface_clear(me);
		}
	}

}
/* polling - retrieve whether cursor should be set or operator should be done */

/* Returns true if vertex paint mode is active */
int vertex_paint_mode_poll(bContext *C)
{
	Object *ob = CTX_data_active_object(C);

	return ob && ob->mode == OB_MODE_VERTEX_PAINT && ((Mesh *)ob->data)->totpoly;
}

int vertex_paint_poll(bContext *C)
{
	if (vertex_paint_mode_poll(C) && 
	    BKE_paint_brush(&CTX_data_tool_settings(C)->vpaint->paint))
	{
		ScrArea *sa = CTX_wm_area(C);
		if (sa && sa->spacetype == SPACE_VIEW3D) {
			ARegion *ar = CTX_wm_region(C);
			if (ar->regiontype == RGN_TYPE_WINDOW)
				return 1;
		}
	}
	return 0;
}

int weight_paint_mode_poll(bContext *C)
{
	Object *ob = CTX_data_active_object(C);

	return ob && ob->mode == OB_MODE_WEIGHT_PAINT && ((Mesh *)ob->data)->totpoly;
}

int weight_paint_poll(bContext *C)
{
	Object *ob = CTX_data_active_object(C);
	ScrArea *sa;

	if ((ob != NULL) &&
	    (ob->mode & OB_MODE_WEIGHT_PAINT) &&
	    (BKE_paint_brush(&CTX_data_tool_settings(C)->wpaint->paint) != NULL) &&
	    (sa = CTX_wm_area(C)) &&
	    (sa->spacetype == SPACE_VIEW3D))
	{
		ARegion *ar = CTX_wm_region(C);
		if (ar->regiontype == RGN_TYPE_WINDOW) {
			return 1;
		}
	}
	return 0;
}

static VPaint *new_vpaint(int wpaint)
{
	VPaint *vp = MEM_callocN(sizeof(VPaint), "VPaint");
	
	vp->flag = (wpaint) ? 0 : VP_SPRAY;
	vp->paint.flags |= PAINT_SHOW_BRUSH;

	return vp;
}

static int *get_indexarray(Mesh *me)
{
	return MEM_mallocN(sizeof(int) * (me->totpoly + 1), "vertexpaint");
}

unsigned int vpaint_get_current_col(VPaint *vp)
{
	Brush *brush = BKE_paint_brush(&vp->paint);
	unsigned char col[4];
	rgb_float_to_uchar(col, brush->rgb);
	col[3] = 255; /* alpha isn't used, could even be removed to speedup paint a little */
	return *(unsigned int *)col;
}

static void do_shared_vertex_tesscol(Mesh *me, bool *mfacetag)
{
	/* if no mcol: do not do */
	/* if tface: only the involved faces, otherwise all */
	const int use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL);
	MFace *mface;
	int a;
	short *scolmain, *scol;
	char *mcol;
	bool *mftag;
	
	if (me->mcol == NULL || me->totvert == 0 || me->totface == 0) return;
	
	scolmain = MEM_callocN(4 * sizeof(short) * me->totvert, "colmain");
	
	mface = me->mface;
	mcol = (char *)me->mcol;
	for (a = me->totface; a > 0; a--, mface++, mcol += 16) {
		if ((use_face_sel == FALSE) || (mface->flag & ME_FACE_SEL)) {
			scol = scolmain + 4 * mface->v1;
			scol[0]++; scol[1] += mcol[1]; scol[2] += mcol[2]; scol[3] += mcol[3];
			scol = scolmain + 4 * mface->v2;
			scol[0]++; scol[1] += mcol[5]; scol[2] += mcol[6]; scol[3] += mcol[7];
			scol = scolmain + 4 * mface->v3;
			scol[0]++; scol[1] += mcol[9]; scol[2] += mcol[10]; scol[3] += mcol[11];
			if (mface->v4) {
				scol = scolmain + 4 * mface->v4;
				scol[0]++; scol[1] += mcol[13]; scol[2] += mcol[14]; scol[3] += mcol[15];
			}
		}
	}
	
	a = me->totvert;
	scol = scolmain;
	while (a--) {
		if (scol[0] > 1) {
			scol[1] = divide_round_i(scol[1], scol[0]);
			scol[2] = divide_round_i(scol[2], scol[0]);
			scol[3] = divide_round_i(scol[3], scol[0]);
		}
		scol += 4;
	}

	mface = me->mface;
	mcol = (char *)me->mcol;
	mftag = mfacetag;
	for (a = me->totface; a > 0; a--, mface++, mcol += 16, mftag += 4) {
		if ((use_face_sel == FALSE) || (mface->flag & ME_FACE_SEL)) {
			if (mftag[0]) {
				scol = scolmain + 4 * mface->v1;
				mcol[1] = scol[1]; mcol[2] = scol[2]; mcol[3] = scol[3];
			}

			if (mftag[1]) {
				scol = scolmain + 4 * mface->v2;
				mcol[5] = scol[1]; mcol[6] = scol[2]; mcol[7] = scol[3];
			}

			if (mftag[2]) {
				scol = scolmain + 4 * mface->v3;
				mcol[9] = scol[1]; mcol[10] = scol[2]; mcol[11] = scol[3];
			}

			if (mface->v4 && mftag[3]) {
				scol = scolmain + 4 * mface->v4;
				mcol[13] = scol[1]; mcol[14] = scol[2]; mcol[15] = scol[3];
			}
		}
	}

	MEM_freeN(scolmain);
}

static void do_shared_vertexcol(Mesh *me, bool *mlooptag, bool *mfacetag, const bool do_tessface)
{
	const int use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL);
	MPoly *mp;
	int (*scol)[4];
	int i, j;
	bool has_shared = false;

	/* if no mloopcol: do not do */
	/* if mtexpoly: only the involved faces, otherwise all */

	if (me->mloopcol == NULL || me->totvert == 0 || me->totpoly == 0) return;

	scol = MEM_callocN(sizeof(int) * me->totvert * 5, "scol");

	for (i = 0, mp = me->mpoly; i < me->totpoly; i++, mp++) {
		if ((use_face_sel == FALSE) || (mp->flag & ME_FACE_SEL)) {
			MLoop *ml = me->mloop + mp->loopstart;
			MLoopCol *lcol = me->mloopcol + mp->loopstart;
			for (j = 0; j < mp->totloop; j++, ml++, lcol++) {
				scol[ml->v][0] += lcol->r;
				scol[ml->v][1] += lcol->g;
				scol[ml->v][2] += lcol->b;
				scol[ml->v][3] += 1;
				has_shared = 1;
			}
		}
	}

	if (has_shared) {
		for (i = 0; i < me->totvert; i++) {
			if (scol[i][3] != 0) {
				scol[i][0] = divide_round_i(scol[i][0], scol[i][3]);
				scol[i][1] = divide_round_i(scol[i][1], scol[i][3]);
				scol[i][2] = divide_round_i(scol[i][2], scol[i][3]);
			}
		}

		for (i = 0, mp = me->mpoly; i < me->totpoly; i++, mp++) {
			if ((use_face_sel == FALSE) || (mp->flag & ME_FACE_SEL)) {
				MLoop *ml = me->mloop + mp->loopstart;
				MLoopCol *lcol = me->mloopcol + mp->loopstart;
				for (j = 0; j < mp->totloop; j++, ml++, lcol++) {
					if (mlooptag[mp->loopstart + j]) {
						lcol->r = scol[ml->v][0];
						lcol->g = scol[ml->v][1];
						lcol->b = scol[ml->v][2];
					}
				}
			}
		}
	}

	MEM_freeN(scol);

	if (has_shared && do_tessface) {
		do_shared_vertex_tesscol(me, mfacetag);
	}
}

static bool make_vertexcol(Object *ob)  /* single ob */
{
	Mesh *me;

	if ((ob->id.lib) ||
	    ((me = BKE_mesh_from_object(ob)) == NULL) ||
	    (me->totpoly == 0) ||
	    (me->edit_btmesh))
	{
		return false;
	}

	/* copies from shadedisplist to mcol */
	if (!me->mloopcol && me->totloop) {
		if (!me->mcol) {
			CustomData_add_layer(&me->fdata, CD_MCOL, CD_DEFAULT, NULL, me->totface);
		}
		if (!me->mloopcol) {
			CustomData_add_layer(&me->ldata, CD_MLOOPCOL, CD_DEFAULT, NULL, me->totloop);
		}
		BKE_mesh_update_customdata_pointers(me, true);
	}

	update_tessface_data(ob, me);
	
	DAG_id_tag_update(&me->id, 0);
	
	return (me->mloopcol != NULL);
}

/* mirror_vgroup is set to -1 when invalid */
static int wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active)
{
	bDeformGroup *defgroup = BLI_findlink(&ob->defbase, vgroup_active);

	if (defgroup) {
		int mirrdef;
		char name_flip[MAXBONENAME];

		BKE_deform_flip_side_name(name_flip, defgroup->name, false);
		mirrdef = defgroup_name_index(ob, name_flip);
		if (mirrdef == -1) {
			if (BKE_defgroup_new(ob, name_flip)) {
				mirrdef = BLI_countlist(&ob->defbase) - 1;
			}
		}

		/* curdef should never be NULL unless this is
		 * a  lamp and ED_vgroup_add_name fails */
		return mirrdef;
	}

	return -1;
}

static void free_vpaint_prev(VPaint *vp)
{
	if (vp->vpaint_prev) {
		MEM_freeN(vp->vpaint_prev);
		vp->vpaint_prev = NULL;
		vp->tot = 0;
	}
}

static void free_wpaint_prev(VPaint *vp)
{
	if (vp->wpaint_prev) {
		BKE_defvert_array_free(vp->wpaint_prev, vp->tot);
		vp->wpaint_prev = NULL;
		vp->tot = 0;
	}
}

static void copy_vpaint_prev(VPaint *vp, unsigned int *lcol, int tot)
{
	free_vpaint_prev(vp);

	vp->tot = tot;
	
	if (lcol == NULL || tot == 0) return;
	
	vp->vpaint_prev = MEM_mallocN(sizeof(int) * tot, "vpaint_prev");
	memcpy(vp->vpaint_prev, lcol, sizeof(int) * tot);
	
}

static void copy_wpaint_prev(VPaint *wp, MDeformVert *dverts, int dcount)
{
	free_wpaint_prev(wp);
	
	if (dverts && dcount) {
		
		wp->wpaint_prev = MEM_mallocN(sizeof(MDeformVert) * dcount, "wpaint prev");
		wp->tot = dcount;
		BKE_defvert_array_copy(wp->wpaint_prev, dverts, dcount);
	}
}

bool ED_vpaint_fill(Object *ob, unsigned int paintcol)
{
	Mesh *me;
	MPoly *mp;
	int i, j;
	bool selected;

	if (((me = BKE_mesh_from_object(ob)) == NULL) ||
	    (me->mloopcol == NULL && (make_vertexcol(ob) == false)))
	{
		return false;
	}

	selected = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;

	mp = me->mpoly;
	for (i = 0; i < me->totpoly; i++, mp++) {
		MLoopCol *lcol = me->mloopcol + mp->loopstart;

		if (selected && !(mp->flag & ME_FACE_SEL))
			continue;

		for (j = 0; j < mp->totloop; j++, lcol++) {
			*(int *)lcol = paintcol;
		}
	}
	
	/* remove stale me->mcol, will be added later */
	BKE_mesh_tessface_clear(me);

	DAG_id_tag_update(&me->id, 0);

	return true;
}


/* fills in the selected faces with the current weight and vertex group */
bool ED_wpaint_fill(VPaint *wp, Object *ob, float paintweight)
{
	Mesh *me = ob->data;
	MPoly *mp;
	MDeformWeight *dw, *dw_prev;
	int vgroup_active, vgroup_mirror = -1;
	unsigned int index;
	const bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;

	/* mutually exclusive, could be made into a */
	const short paint_selmode = ME_EDIT_PAINT_SEL_MODE(me);

	if (me->totpoly == 0 || me->dvert == NULL || !me->mpoly) {
		return false;
	}
	
	vgroup_active = ob->actdef - 1;

	/* if mirror painting, find the other group */
	if (me->editflag & ME_EDIT_MIRROR_X) {
		vgroup_mirror = wpaint_mirror_vgroup_ensure(ob, vgroup_active);
	}
	
	copy_wpaint_prev(wp, me->dvert, me->totvert);
	
	for (index = 0, mp = me->mpoly; index < me->totpoly; index++, mp++) {
		unsigned int fidx = mp->totloop - 1;

		if ((paint_selmode == SCE_SELECT_FACE) && !(mp->flag & ME_FACE_SEL)) {
			continue;
		}

		do {
			unsigned int vidx = me->mloop[mp->loopstart + fidx].v;

			if (!me->dvert[vidx].flag) {
				if ((paint_selmode == SCE_SELECT_VERTEX) && !(me->mvert[vidx].flag & SELECT)) {
					continue;
				}

				dw = defvert_verify_index(&me->dvert[vidx], vgroup_active);
				if (dw) {
					dw_prev = defvert_verify_index(wp->wpaint_prev + vidx, vgroup_active);
					dw_prev->weight = dw->weight; /* set the undo weight */
					dw->weight = paintweight;

					if (me->editflag & ME_EDIT_MIRROR_X) {  /* x mirror painting */
						int j = mesh_get_x_mirror_vert(ob, vidx, topology);
						if (j >= 0) {
							/* copy, not paint again */
							if (vgroup_mirror != -1) {
								dw = defvert_verify_index(me->dvert + j, vgroup_mirror);
								dw_prev = defvert_verify_index(wp->wpaint_prev + j, vgroup_mirror);
							}
							else {
								dw = defvert_verify_index(me->dvert + j, vgroup_active);
								dw_prev = defvert_verify_index(wp->wpaint_prev + j, vgroup_active);
							}
							dw_prev->weight = dw->weight; /* set the undo weight */
							dw->weight = paintweight;
						}
					}
				}
				me->dvert[vidx].flag = 1;
			}

		} while (fidx--);
	}

	{
		MDeformVert *dv = me->dvert;
		for (index = me->totvert; index != 0; index--, dv++) {
			dv->flag = 0;
		}
	}

	copy_wpaint_prev(wp, NULL, 0);

	DAG_id_tag_update(&me->id, 0);

	return true;
}

bool ED_vpaint_smooth(Object *ob)
{
	Mesh *me;
	MPoly *mp;

	int i, j;

	bool *mlooptag;
	bool selected;

	if (((me = BKE_mesh_from_object(ob)) == NULL) ||
	    (me->mloopcol == NULL && (make_vertexcol(ob) == false)))
	{
		return false;
	}

	selected = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;

	mlooptag = MEM_callocN(sizeof(bool) * me->totloop, "VPaintData mlooptag");

	/* simply tag loops of selected faces */
	mp = me->mpoly;
	for (i = 0; i < me->totpoly; i++, mp++) {
		MLoop *ml = me->mloop + mp->loopstart;
		int ml_index = mp->loopstart;

		if (selected && !(mp->flag & ME_FACE_SEL))
			continue;

		for (j = 0; j < mp->totloop; j++, ml_index++, ml++) {
			mlooptag[ml_index] = true;
		}
	}

	/* remove stale me->mcol, will be added later */
	BKE_mesh_tessface_clear(me);

	do_shared_vertexcol(me, mlooptag, NULL, false);

	MEM_freeN(mlooptag);

	DAG_id_tag_update(&me->id, 0);

	return true;
}

/* XXX: should be re-implemented as a vertex/weight paint 'color correct' operator */
#if 0
void vpaint_dogamma(Scene *scene)
{
	VPaint *vp = scene->toolsettings->vpaint;
	Mesh *me;
	Object *ob;
	float igam, fac;
	int a, temp;
	unsigned char *cp, gamtab[256];

	ob = OBACT;
	me = BKE_mesh_from_object(ob);

	if (!(ob->mode & OB_MODE_VERTEX_PAINT)) return;
	if (me == 0 || me->mcol == 0 || me->totface == 0) return;

	igam = 1.0 / vp->gamma;
	for (a = 0; a < 256; a++) {

		fac = ((float)a) / 255.0;
		fac = vp->mul * pow(fac, igam);

		temp = 255.9 * fac;

		if (temp <= 0) gamtab[a] = 0;
		else if (temp >= 255) gamtab[a] = 255;
		else gamtab[a] = temp;
	}

	a = 4 * me->totface;
	cp = (unsigned char *)me->mcol;
	while (a--) {

		cp[1] = gamtab[cp[1]];
		cp[2] = gamtab[cp[2]];
		cp[3] = gamtab[cp[3]];

		cp += 4;
	}
}
#endif

BLI_INLINE unsigned int mcol_blend(unsigned int col1, unsigned int col2, int fac)
{
	unsigned char *cp1, *cp2, *cp;
	int mfac;
	unsigned int col = 0;

	if (fac == 0) {
		return col1;
	}

	if (fac >= 255) {
		return col2;
	}

	mfac = 255 - fac;

	cp1 = (unsigned char *)&col1;
	cp2 = (unsigned char *)&col2;
	cp  = (unsigned char *)&col;

	cp[0] = divide_round_i((mfac * cp1[0] + fac * cp2[0]), 255);
	cp[1] = divide_round_i((mfac * cp1[1] + fac * cp2[1]), 255);
	cp[2] = divide_round_i((mfac * cp1[2] + fac * cp2[2]), 255);
	cp[3] = 255;

	return col;
}

BLI_INLINE unsigned int mcol_add(unsigned int col1, unsigned int col2, int fac)
{
	unsigned char *cp1, *cp2, *cp;
	int temp;
	unsigned int col = 0;

	if (fac == 0) {
		return col1;
	}

	cp1 = (unsigned char *)&col1;
	cp2 = (unsigned char *)&col2;
	cp  = (unsigned char *)&col;

	temp = cp1[0] + divide_round_i((fac * cp2[0]), 255);
	cp[0] = (temp > 254) ? 255 : temp;
	temp = cp1[1] + divide_round_i((fac * cp2[1]), 255);
	cp[1] = (temp > 254) ? 255 : temp;
	temp = cp1[2] + divide_round_i((fac * cp2[2]), 255);
	cp[2] = (temp > 254) ? 255 : temp;
	cp[3] = 255;
	
	return col;
}

BLI_INLINE unsigned int mcol_sub(unsigned int col1, unsigned int col2, int fac)
{
	unsigned char *cp1, *cp2, *cp;
	int temp;
	unsigned int col = 0;

	if (fac == 0) {
		return col1;
	}

	cp1 = (unsigned char *)&col1;
	cp2 = (unsigned char *)&col2;
	cp  = (unsigned char *)&col;

	temp = cp1[0] - divide_round_i((fac * cp2[0]), 255);
	cp[0] = (temp < 0) ? 0 : temp;
	temp = cp1[1] - divide_round_i((fac * cp2[1]), 255);
	cp[1] = (temp < 0) ? 0 : temp;
	temp = cp1[2] - divide_round_i((fac * cp2[2]), 255);
	cp[2] = (temp < 0) ? 0 : temp;
	cp[3] = 255;

	return col;
}

BLI_INLINE unsigned int mcol_mul(unsigned int col1, unsigned int col2, int fac)
{
	unsigned char *cp1, *cp2, *cp;
	int mfac;
	unsigned int col = 0;

	if (fac == 0) {
		return col1;
	}

	mfac = 255 - fac;

	cp1 = (unsigned char *)&col1;
	cp2 = (unsigned char *)&col2;
	cp  = (unsigned char *)&col;

	/* first mul, then blend the fac */
	cp[0] = divide_round_i(mfac * cp1[0] * 255 + fac * cp2[0] * cp1[0], 255 * 255);
	cp[1] = divide_round_i(mfac * cp1[1] * 255 + fac * cp2[1] * cp1[1], 255 * 255);
	cp[2] = divide_round_i(mfac * cp1[2] * 255 + fac * cp2[2] * cp1[2], 255 * 255);
	cp[3] = 255;

	return col;
}

BLI_INLINE unsigned int mcol_lighten(unsigned int col1, unsigned int col2, int fac)
{
	unsigned char *cp1, *cp2, *cp;
	int mfac;
	unsigned int col = 0;

	if (fac == 0) {
		return col1;
	}
	else if (fac >= 255) {
		return col2;
	}

	mfac = 255 - fac;

	cp1 = (unsigned char *)&col1;
	cp2 = (unsigned char *)&col2;
	cp  = (unsigned char *)&col;

	/* See if are lighter, if so mix, else don't do anything.
	 * if the paint col is darker then the original, then ignore */
	if (rgb_to_grayscale_byte(cp1) > rgb_to_grayscale_byte(cp2)) {
		return col1;
	}

	cp[0] = divide_round_i(mfac * cp1[0] + fac * cp2[0], 255);
	cp[1] = divide_round_i(mfac * cp1[1] + fac * cp2[1], 255);
	cp[2] = divide_round_i(mfac * cp1[2] + fac * cp2[2], 255);
	cp[3] = 255;

	return col;
}

BLI_INLINE unsigned int mcol_darken(unsigned int col1, unsigned int col2, int fac)
{
	unsigned char *cp1, *cp2, *cp;
	int mfac;
	unsigned int col = 0;

	if (fac == 0) {
		return col1;
	}
	else if (fac >= 255) {
		return col2;
	}

	mfac = 255 - fac;

	cp1 = (unsigned char *)&col1;
	cp2 = (unsigned char *)&col2;
	cp  = (unsigned char *)&col;

	/* See if were darker, if so mix, else don't do anything.
	 * if the paint col is brighter then the original, then ignore */
	if (rgb_to_grayscale_byte(cp1) < rgb_to_grayscale_byte(cp2)) {
		return col1;
	}

	cp[0] = divide_round_i((mfac * cp1[0] + fac * cp2[0]), 255);
	cp[1] = divide_round_i((mfac * cp1[1] + fac * cp2[1]), 255);
	cp[2] = divide_round_i((mfac * cp1[2] + fac * cp2[2]), 255);
	cp[3] = 255;
	return col;
}

/* wpaint has 'wpaint_blend_tool' */
static unsigned int vpaint_blend_tool(const int tool, const unsigned int col,
                                      const unsigned int paintcol, const int alpha_i)
{
	switch (tool) {
		case PAINT_BLEND_MIX:
		case PAINT_BLEND_BLUR:     return mcol_blend(col, paintcol, alpha_i);
		case PAINT_BLEND_ADD:      return mcol_add(col, paintcol, alpha_i);
		case PAINT_BLEND_SUB:      return mcol_sub(col, paintcol, alpha_i);
		case PAINT_BLEND_MUL:      return mcol_mul(col, paintcol, alpha_i);
		case PAINT_BLEND_LIGHTEN:  return mcol_lighten(col, paintcol, alpha_i);
		case PAINT_BLEND_DARKEN:   return mcol_darken(col, paintcol, alpha_i);
		default:
			BLI_assert(0);
			return 0;
	}
}

/* wpaint has 'wpaint_blend' */
static unsigned int vpaint_blend(VPaint *vp, unsigned int col, unsigned int colorig, const
                                 unsigned int paintcol, const int alpha_i,
                                 /* pre scaled from [0-1] --> [0-255] */
                                 const int brush_alpha_value_i)
{
	Brush *brush = BKE_paint_brush(&vp->paint);
	const int tool = brush->vertexpaint_tool;

	col = vpaint_blend_tool(tool, col, paintcol, alpha_i);

	/* if no spray, clip color adding with colorig & orig alpha */
	if ((vp->flag & VP_SPRAY) == 0) {
		unsigned int testcol, a;
		char *cp, *ct, *co;
		
		testcol = vpaint_blend_tool(tool, colorig, paintcol, brush_alpha_value_i);
		
		cp = (char *)&col;
		ct = (char *)&testcol;
		co = (char *)&colorig;
		
		for (a = 0; a < 4; a++) {
			if (ct[a] < co[a]) {
				if (cp[a] < ct[a]) cp[a] = ct[a];
				else if (cp[a] > co[a]) cp[a] = co[a];
			}
			else {
				if (cp[a] < co[a]) cp[a] = co[a];
				else if (cp[a] > ct[a]) cp[a] = ct[a];
			}
		}
	}

	return col;
}


static int sample_backbuf_area(ViewContext *vc, int *indexar, int totface, int x, int y, float size)
{
	struct ImBuf *ibuf;
	int a, tot = 0, index;
	
	/* brecht: disabled this because it obviously fails for
	 * brushes with size > 64, why is this here? */
	/*if (size > 64.0) size = 64.0;*/
	
	ibuf = view3d_read_backbuf(vc, x - size, y - size, x + size, y + size);
	if (ibuf) {
		unsigned int *rt = ibuf->rect;

		memset(indexar, 0, sizeof(int) * (totface + 1));
		
		size = ibuf->x * ibuf->y;
		while (size--) {
				
			if (*rt) {
				index = WM_framebuffer_to_index(*rt);
				if (index > 0 && index <= totface)
					indexar[index] = 1;
			}
		
			rt++;
		}
		
		for (a = 1; a <= totface; a++) {
			if (indexar[a]) indexar[tot++] = a;
		}

		IMB_freeImBuf(ibuf);
	}
	
	return tot;
}

/* whats _dl mean? */
static float calc_vp_strength_col_dl(VPaint *vp, ViewContext *vc, const float co[3],
                                 const float mval[2], const float brush_size_pressure, float rgba[4])
{
	float co_ss[2];  /* screenspace */

	if (ED_view3d_project_float_object(vc->ar,
	                                   co, co_ss,
	                                   V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
	{
		const float dist_sq = len_squared_v2v2(mval, co_ss);

		if (dist_sq <= brush_size_pressure * brush_size_pressure) {
			Brush *brush = BKE_paint_brush(&vp->paint);
			const float dist = sqrtf(dist_sq);
			float factor;

			if (brush->mtex.tex && rgba) {
				if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D) {
					BKE_brush_sample_tex_3D(vc->scene, brush, co, rgba, 0, NULL);
				}
				else {
					const float co_ss_3d[3] = {co_ss[0], co_ss[1], 0.0f};  /* we need a 3rd empty value */
					BKE_brush_sample_tex_3D(vc->scene, brush, co_ss_3d, rgba, 0, NULL);
				}
				factor = rgba[3];
			}
			else {
				factor = 1.0f;
			}
			return factor * BKE_brush_curve_strength_clamp(brush, dist, brush_size_pressure);
		}
	}
	if (rgba)
		zero_v4(rgba);
	return 0.0f;
}

static float calc_vp_alpha_col_dl(VPaint *vp, ViewContext *vc,
                              float vpimat[3][3], const DMCoNo *v_co_no,
                              const float mval[2],
                              const float brush_size_pressure, const float brush_alpha_pressure, float rgba[4])
{
	float strength = calc_vp_strength_col_dl(vp, vc, v_co_no->co, mval, brush_size_pressure, rgba);

	if (strength > 0.0f) {
		float alpha = brush_alpha_pressure * strength;

		if (vp->flag & VP_NORMALS) {
			float dvec[3];

			/* transpose ! */
			dvec[2] = dot_v3v3(vpimat[2], v_co_no->no);
			if (dvec[2] > 0.0f) {
				dvec[0] = dot_v3v3(vpimat[0], v_co_no->no);
				dvec[1] = dot_v3v3(vpimat[1], v_co_no->no);

				alpha *= dvec[2] / len_v3(dvec);
			}
			else {
				return 0.0f;
			}
		}

		return alpha;
	}

	return 0.0f;
}


BLI_INLINE float wval_blend(const float weight, const float paintval, const float alpha)
{
	const float talpha = min_ff(alpha, 1.0f);  /* blending with values over 1 doesn't make sense */
	return (paintval * talpha) + (weight * (1.0f - talpha));
}
BLI_INLINE float wval_add(const float weight, const float paintval, const float alpha)
{
	return weight + (paintval * alpha);
}
BLI_INLINE float wval_sub(const float weight, const float paintval, const float alpha)
{
	return weight - (paintval * alpha);
}
BLI_INLINE float wval_mul(const float weight, const float paintval, const float alpha)
{   /* first mul, then blend the fac */
	return ((1.0f - alpha) + (alpha * paintval)) * weight;
}
BLI_INLINE float wval_lighten(const float weight, const float paintval, const float alpha)
{
	return (weight < paintval) ? wval_blend(weight, paintval, alpha) : weight;
}
BLI_INLINE float wval_darken(const float weight, const float paintval, const float alpha)
{
	return (weight > paintval) ? wval_blend(weight, paintval, alpha) : weight;
}


/* vpaint has 'vpaint_blend_tool' */
/* result is not clamped from [0-1] */
static float wpaint_blend_tool(const int tool,
                               /* dw->weight */
                               const float weight,
                               const float paintval, const float alpha)
{
	switch (tool) {
		case PAINT_BLEND_MIX:
		case PAINT_BLEND_BLUR:     return wval_blend(weight, paintval, alpha);
		case PAINT_BLEND_ADD:      return wval_add(weight, paintval, alpha);
		case PAINT_BLEND_SUB:      return wval_sub(weight, paintval, alpha);
		case PAINT_BLEND_MUL:      return wval_mul(weight, paintval, alpha);
		case PAINT_BLEND_LIGHTEN:  return wval_lighten(weight, paintval, alpha);
		case PAINT_BLEND_DARKEN:   return wval_darken(weight, paintval, alpha);
		default:
			BLI_assert(0);
			return 0.0f;
	}
}

/* vpaint has 'vpaint_blend' */
static float wpaint_blend(VPaint *wp, float weight, float weight_prev,
                          const float alpha, float paintval,
                          const float brush_alpha_value,
                          const short do_flip, const short do_multipaint_totsel)
{
	Brush *brush = BKE_paint_brush(&wp->paint);
	int tool = brush->vertexpaint_tool;

	if (do_flip) {
		switch (tool) {
			case PAINT_BLEND_MIX:
				paintval = 1.f - paintval; break;
			case PAINT_BLEND_ADD:
				tool = PAINT_BLEND_SUB; break;
			case PAINT_BLEND_SUB:
				tool = PAINT_BLEND_ADD; break;
			case PAINT_BLEND_LIGHTEN:
				tool = PAINT_BLEND_DARKEN; break;
			case PAINT_BLEND_DARKEN:
				tool = PAINT_BLEND_LIGHTEN; break;
		}
	}
	
	weight = wpaint_blend_tool(tool, weight, paintval, alpha);

	/* delay clamping until the end so multi-paint can function when the active group is at the limits */
	if (do_multipaint_totsel == FALSE) {
		CLAMP(weight, 0.0f, 1.0f);
	}
	
	/* if no spray, clip result with orig weight & orig alpha */
	if ((wp->flag & VP_SPRAY) == 0) {
		if (do_multipaint_totsel == FALSE) {
			float testw = wpaint_blend_tool(tool, weight_prev, paintval, brush_alpha_value);

			CLAMP(testw, 0.0f, 1.0f);
			if (testw < weight_prev) {
				if (weight < testw) weight = testw;
				else if (weight > weight_prev) weight = weight_prev;
			}
			else {
				if (weight > testw) weight = testw;
				else if (weight < weight_prev) weight = weight_prev;
			}
		}
	}

	return weight;
}

/* ----------------------------------------------------- */


/* sets wp->weight to the closest weight value to vertex */
/* note: we cant sample frontbuf, weight colors are interpolated too unpredictable */
static int weight_sample_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	ViewContext vc;
	Mesh *me;
	bool changed = false;

	view3d_set_viewcontext(C, &vc);
	me = BKE_mesh_from_object(vc.obact);

	if (me && me->dvert && vc.v3d && vc.rv3d) {
		const int use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
		int v_idx_best = -1;
		unsigned int index;

		view3d_operator_needs_opengl(C);
		ED_view3d_init_mats_rv3d(vc.obact, vc.rv3d);

		if (use_vert_sel) {
			if (ED_mesh_pick_vert(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_VERT_SIZE, TRUE)) {
				v_idx_best = index;
			}
		}
		else {
			if (ED_mesh_pick_face_vert(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) {
				v_idx_best = index;
			}
			else if (ED_mesh_pick_face(C, vc.obact, event->mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) {
				/* this relies on knowning the internal worksings of ED_mesh_pick_face_vert() */
				BKE_report(op->reports, RPT_WARNING, "The modifier used does not support deformed locations");
			}
		}

		if (v_idx_best != -1) { /* should always be valid */
			ToolSettings *ts = vc.scene->toolsettings;
			Brush *brush = BKE_paint_brush(&ts->wpaint->paint);
			const int vgroup_active = vc.obact->actdef - 1;
			float vgroup_weight = defvert_find_weight(&me->dvert[v_idx_best], vgroup_active);
			BKE_brush_weight_set(vc.scene, brush, vgroup_weight);
			changed = true;
		}
	}

	if (changed) {
		/* not really correct since the brush didnt change, but redraws the toolbar */
		WM_main_add_notifier(NC_BRUSH | NA_EDITED, NULL); /* ts->wpaint->paint.brush */

		return OPERATOR_FINISHED;
	}
	else {
		return OPERATOR_CANCELLED;
	}
}

void PAINT_OT_weight_sample(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Weight Paint Sample Weight";
	ot->idname = "PAINT_OT_weight_sample";
	ot->description = "Use the mouse to sample a weight in the 3D view";

	/* api callbacks */
	ot->invoke = weight_sample_invoke;
	ot->poll = weight_paint_mode_poll;

	/* flags */
	ot->flag = OPTYPE_UNDO;
}

/* samples cursor location, and gives menu with vertex groups to activate */
static int weight_paint_sample_enum_itemf__helper(const MDeformVert *dvert, const int defbase_tot, int *groups)
{
	/* this func fills in used vgroup's */
	int found = FALSE;
	int i = dvert->totweight;
	MDeformWeight *dw;
	for (dw = dvert->dw; i > 0; dw++, i--) {
		if (dw->def_nr < defbase_tot) {
			groups[dw->def_nr] = TRUE;
			found = TRUE;
		}
	}
	return found;
}
static EnumPropertyItem *weight_paint_sample_enum_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
{
	if (C) {
		wmWindow *win = CTX_wm_window(C);
		if (win && win->eventstate) {
			ViewContext vc;
			Mesh *me;

			view3d_set_viewcontext(C, &vc);
			me = BKE_mesh_from_object(vc.obact);

			if (me && me->dvert && vc.v3d && vc.rv3d && vc.obact->defbase.first) {
				const int defbase_tot = BLI_countlist(&vc.obact->defbase);
				const int use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
				int *groups = MEM_callocN(defbase_tot * sizeof(int), "groups");
				int found = FALSE;
				unsigned int index;

				int mval[2] = {win->eventstate->x - vc.ar->winrct.xmin,
				               win->eventstate->y - vc.ar->winrct.ymin};

				view3d_operator_needs_opengl(C);
				ED_view3d_init_mats_rv3d(vc.obact, vc.rv3d);

				if (use_vert_sel) {
					if (ED_mesh_pick_vert(C, vc.obact, mval, &index, ED_MESH_PICK_DEFAULT_VERT_SIZE, TRUE)) {
						MDeformVert *dvert = &me->dvert[index];
						found |= weight_paint_sample_enum_itemf__helper(dvert, defbase_tot, groups);
					}
				}
				else {
					if (ED_mesh_pick_face(C, vc.obact, mval, &index, ED_MESH_PICK_DEFAULT_FACE_SIZE)) {
						MPoly *mp = &me->mpoly[index];
						unsigned int fidx = mp->totloop - 1;

						do {
							MDeformVert *dvert = &me->dvert[me->mloop[mp->loopstart + fidx].v];
							found |= weight_paint_sample_enum_itemf__helper(dvert, defbase_tot, groups);
						} while (fidx--);
					}
				}

				if (found == FALSE) {
					MEM_freeN(groups);
				}
				else {
					EnumPropertyItem *item = NULL, item_tmp = {0};
					int totitem = 0;
					int i = 0;
					bDeformGroup *dg;
					for (dg = vc.obact->defbase.first; dg && i < defbase_tot; i++, dg = dg->next) {
						if (groups[i]) {
							item_tmp.identifier = item_tmp.name = dg->name;
							item_tmp.value = i;
							RNA_enum_item_add(&item, &totitem, &item_tmp);
						}
					}

					RNA_enum_item_end(&item, &totitem);
					*r_free = true;

					MEM_freeN(groups);
					return item;
				}
			}
		}
	}

	return DummyRNA_NULL_items;
}

static int weight_sample_group_exec(bContext *C, wmOperator *op)
{
	int type = RNA_enum_get(op->ptr, "group");
	ViewContext vc;
	view3d_set_viewcontext(C, &vc);

	BLI_assert(type + 1 >= 0);
	vc.obact->actdef = type + 1;

	DAG_id_tag_update(&vc.obact->id, OB_RECALC_DATA);
	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, vc.obact);
	return OPERATOR_FINISHED;
}

/* TODO, we could make this a menu into OBJECT_OT_vertex_group_set_active rather than its own operator */
void PAINT_OT_weight_sample_group(wmOperatorType *ot)
{
	PropertyRNA *prop = NULL;

	/* identifiers */
	ot->name = "Weight Paint Sample Group";
	ot->idname = "PAINT_OT_weight_sample_group";
	ot->description = "Select one of the vertex groups available under current mouse position";

	/* api callbacks */
	ot->exec = weight_sample_group_exec;
	ot->invoke = WM_menu_invoke;
	ot->poll = weight_paint_mode_poll;

	/* flags */
	ot->flag = OPTYPE_UNDO;

	/* keyingset to use (dynamic enum) */
	prop = RNA_def_enum(ot->srna, "group", DummyRNA_DEFAULT_items, 0, "Keying Set", "The Keying Set to use");
	RNA_def_enum_funcs(prop, weight_paint_sample_enum_itemf);
	ot->prop = prop;
}

static void do_weight_paint_normalize_all(MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap)
{
	float sum = 0.0f, fac;
	unsigned int i, tot = 0;
	MDeformWeight *dw;

	for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
		if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
			tot++;
			sum += dw->weight;
		}
	}

	if ((tot == 0) || (sum == 1.0f)) {
		return;
	}

	if (sum != 0.0f) {
		fac = 1.0f / sum;

		for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
			if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
				dw->weight *= fac;
			}
		}
	}
	else {
		/* hrmf, not a factor in this case */
		fac = 1.0f / tot;

		for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
			if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
				dw->weight = fac;
			}
		}
	}
}

/* same as function above except it normalizes against the active vgroup which remains unchanged
 *
 * note that the active is just the group which is unchanged, it can be any,
 * can also be -1 to normalize all but in that case call 'do_weight_paint_normalize_all' */
static void do_weight_paint_normalize_all_active(MDeformVert *dvert, const int defbase_tot, const bool *vgroup_validmap,
                                                 const int vgroup_active)
{
	float sum = 0.0f, fac;
	unsigned int i, tot = 0;
	MDeformWeight *dw;
	float act_weight = 0.0f;

	for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
		if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
			if (dw->def_nr != vgroup_active) {
				sum += dw->weight;
				tot++;
			}
			else {
				act_weight = dw->weight;
			}
		}
	}

	if ((tot == 0) || (sum + act_weight == 1.0f)) {
		return;
	}

	if (sum != 0.0f) {
		fac = (1.0f / sum) * (1.0f - act_weight);

		for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
			if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
				if (dw->def_nr != vgroup_active) {
					dw->weight *= fac;

					/* paranoid but possibly with float error */
					CLAMP(dw->weight, 0.0f, 1.0f);
				}
			}
		}
	}
	else {
		/* corner case where we need to scale all weights evenly because they're all zero */

		/* hrmf, not a factor in this case */
		fac = (1.0f - act_weight) / tot;

		/* paranoid but possibly with float error */
		CLAMP(fac, 0.0f, 1.0f);

		for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
			if (dw->def_nr < defbase_tot && vgroup_validmap[dw->def_nr]) {
				if (dw->def_nr != vgroup_active) {
					dw->weight = fac;
				}
			}
		}
	}
}

/*
 * See if the current deform vertex has a locked group
 */
static bool has_locked_group(MDeformVert *dvert, const int defbase_tot,
                             const bool *bone_groups, const bool *lock_flags)
{
	int i;
	MDeformWeight *dw;

	for (i = dvert->totweight, dw = dvert->dw; i != 0; i--, dw++) {
		if (dw->def_nr < defbase_tot) {
			if (bone_groups[dw->def_nr] && lock_flags[dw->def_nr] && dw->weight > 0.0f) {
				return TRUE;
			}
		}
	}
	return FALSE;
}

static bool has_locked_group_selected(int defbase_tot, const bool *defbase_sel, const bool *lock_flags)
{
	int i;
	for (i = 0; i < defbase_tot; i++) {
		if (defbase_sel[i] && lock_flags[i]) {
			return true;
		}
	}
	return false;
}


#if 0 /* UNUSED */
static bool has_unselected_unlocked_bone_group(int defbase_tot, bool *defbase_sel, int selected,
                                               const bool *lock_flags, const bool *vgroup_validmap)
{
	int i;
	if (defbase_tot == selected) {
		return FALSE;
	}
	for (i = 0; i < defbase_tot; i++) {
		if (vgroup_validmap[i] && !defbase_sel[i] && !lock_flags[i]) {
			return TRUE;
		}
	}
	return FALSE;
}
#endif


static void multipaint_selection(MDeformVert *dvert, const int defbase_tot, float change, const bool *defbase_sel)
{
	int i;
	MDeformWeight *dw;
	float val;
	/* make sure they are all at most 1 after the change */
	for (i = 0; i < defbase_tot; i++) {
		if (defbase_sel[i]) {
			dw = defvert_find_index(dvert, i);
			if (dw && dw->weight) {
				val = dw->weight * change;
				if (val > 1) {
					/* TODO: when the change is reduced, you need to recheck
					 * the earlier values to make sure they are not 0
					 * (precision error) */
					change = 1.0f / dw->weight;
				}
				/* the value should never reach zero while multi-painting if it
				 * was nonzero beforehand */
				if (val <= 0) {
					return;
				}
			}
		}
	}
	/* apply the valid change */
	for (i = 0; i < defbase_tot; i++) {
		if (defbase_sel[i]) {
			dw = defvert_find_index(dvert, i);
			if (dw && dw->weight) {
				dw->weight = dw->weight * change;
			}
		}
	}
}

/* move all change onto valid, unchanged groups.  If there is change left over,
 * then return it.
 * assumes there are valid groups to shift weight onto */
static float redistribute_change(MDeformVert *ndv, const int defbase_tot,
                                 char *change_status, const char change_me, int changeto,
                                 float totchange, float total_valid,
                                 bool do_auto_normalize)
{
	bool changed;
	float change;
	float oldval;
	MDeformWeight *ndw;
	int i;
	do {
		/* assume there is no change until you see one */
		changed = false;
		/* change each group by the same amount each time */
		change = totchange / total_valid;
		for (i = 0; i < ndv->totweight && total_valid && totchange; i++) {
			ndw = (ndv->dw + i);

			/* ignore anything outside the value range */
			if (ndw->def_nr < defbase_tot) {

				/* change only the groups with a valid status */
				if (change_status[ndw->def_nr] == change_me) {
					oldval = ndw->weight;
					/* if auto normalize is active, don't worry about upper bounds */
					if (do_auto_normalize == FALSE && ndw->weight + change > 1) {
						totchange -= 1.0f - ndw->weight;
						ndw->weight = 1.0f;
						/* stop the changes to this group */
						change_status[ndw->def_nr] = changeto;
						total_valid--;
					}
					else if (ndw->weight + change < 0) { /* check the lower bound */
						totchange -= ndw->weight;
						ndw->weight = 0;
						change_status[ndw->def_nr] = changeto;
						total_valid--;
					}
					else { /* a perfectly valid change occurred to ndw->weight */
						totchange -= change;
						ndw->weight += change;
					}
					/* see if there was a change */
					if (oldval != ndw->weight) {
						changed = true;
					}
				}
			}
		}
		/* don't go again if there was no change, if there is no valid group,
		 * or there is no change left */
	} while (changed && total_valid && totchange);
	/* left overs */
	return totchange;
}
static float get_mp_change(MDeformVert *odv, const int defbase_tot, const bool *defbase_sel, float brush_change);
/* observe the changes made to the weights of groups.
 * make sure all locked groups on the vertex have the same deformation
 * by moving the changes made to groups onto other unlocked groups */
static void enforce_locks(MDeformVert *odv, MDeformVert *ndv,
                          const int defbase_tot, const bool *defbase_sel,
                          const bool *lock_flags, const bool *vgroup_validmap,
                          bool do_auto_normalize, bool do_multipaint)
{
	float totchange = 0.0f;
	float totchange_allowed = 0.0f;
	float left_over;

	int total_valid = 0;
	int total_changed = 0;
	unsigned int i;
	MDeformWeight *ndw;
	MDeformWeight *odw;

	// float changed_sum = 0.0f;  // UNUSED

	char *change_status;

	if (!lock_flags || !has_locked_group(ndv, defbase_tot, vgroup_validmap, lock_flags)) {
		return;
	}
	/* record if a group was changed, unlocked and not changed, or locked */
	change_status = MEM_callocN(sizeof(char) * defbase_tot, "unlocked_unchanged");

	for (i = 0; i < defbase_tot; i++) {
		ndw = defvert_find_index(ndv, i);
		odw = defvert_find_index(odv, i);
		/* the weights are zero, so we can assume a lot */
		if (!ndw || !odw) {
			if (!lock_flags[i] && vgroup_validmap[i]) {
				defvert_verify_index(odv, i);
				defvert_verify_index(ndv, i);
				total_valid++;
				change_status[i] = 1; /* can be altered while redistributing */
			}
			continue;
		}
		/* locked groups should not be changed */
		if (lock_flags[i]) {
			ndw->weight = odw->weight;
		}
		else if (ndw->weight != odw->weight) { /* changed groups are handled here */
			totchange += ndw->weight - odw->weight;
			// changed_sum += ndw->weight;  // UNUSED
			change_status[i] = 2; /* was altered already */
			total_changed++;
		} /* unchanged, unlocked bone groups are handled here */
		else if (vgroup_validmap[i]) {
			totchange_allowed += ndw->weight;
			total_valid++;
			change_status[i] = 1; /* can be altered while redistributing */
		}
	}
	/* if there was any change, redistribute it */
	if (total_changed) {
		/* auto normalize will allow weights to temporarily go above 1 in redistribution */
		if (vgroup_validmap && total_changed < 0 && total_valid) {
			totchange_allowed = total_valid;
		}
		/* the way you modify the unlocked + unchanged groups is different depending
		 * on whether or not you are painting the weight(s) up or down */
		if (totchange < 0) {
			totchange_allowed = total_valid - totchange_allowed;
		}
		else {
			totchange_allowed *= -1;
		}
		/* there needs to be change allowed, or you should not bother */
		if (totchange_allowed) {
			left_over = 0;
			if (fabsf(totchange_allowed) < fabsf(totchange)) {
				/* this amount goes back onto the changed, unlocked weights */
				left_over = fabsf(fabsf(totchange) - fabsf(totchange_allowed));
				if (totchange > 0) {
					left_over *= -1;
				}
			}
			else {
				/* all of the change will be permitted */
				totchange_allowed = -totchange;
			}
			/* move the weight evenly between the allowed groups, move excess back onto the used groups based on the change */
			totchange_allowed = redistribute_change(ndv, defbase_tot, change_status, 1, -1, totchange_allowed, total_valid, do_auto_normalize);
			left_over += totchange_allowed;
			if (left_over) {
				/* more than one nonzero weights were changed with the same ratio with multipaint, so keep them changed that way! */
				if (total_changed > 1 && do_multipaint) {
					float undo_change = get_mp_change(ndv, defbase_tot, defbase_sel, left_over);
					multipaint_selection(ndv, defbase_tot, undo_change, defbase_sel);
				}
				/* or designatedw is still -1 put weight back as evenly as possible */
				else {
					redistribute_change(ndv, defbase_tot, change_status, 2, -2, left_over, total_changed, do_auto_normalize);
				}
			}
		}
		else {
			/* reset the weights */
			MDeformWeight *dw_old = odv->dw;
			MDeformWeight *dw_new = ndv->dw;

			for (i = odv->totweight; i != 0; i--, dw_old++, dw_new++) {
				dw_new->weight = dw_old->weight;
			}
		}
	}

	MEM_freeN(change_status);
}

/* multi-paint's initial, potential change is computed here based on the user's stroke */
static float get_mp_change(MDeformVert *odv, const int defbase_tot, const bool *defbase_sel, float brush_change)
{
	float selwsum = 0.0f;
	unsigned int i;
	MDeformWeight *dw = odv->dw;

	for (i = odv->totweight; i != 0; i--, dw++) {
		if (dw->def_nr < defbase_tot) {
			if (defbase_sel[dw->def_nr]) {
				selwsum += dw->weight;
			}
		}
	}
	if (selwsum && selwsum + brush_change > 0) {
		return (selwsum + brush_change) / selwsum;
	}
	return 0.0f;
}

/* change the weights back to the wv's weights
 * it assumes you already have the correct pointer index */
static void defvert_reset_to_prev(MDeformVert *dv_prev, MDeformVert *dv)
{
	MDeformWeight *dw = dv->dw;
	MDeformWeight *dw_prev;
	unsigned int i;
	for (i = dv->totweight; i != 0; i--, dw++) {
		dw_prev = defvert_find_index(dv_prev, dw->def_nr);
		/* if there was no w when there is a d, then the old weight was 0 */
		dw->weight = dw_prev ? dw_prev->weight : 0.0f;
	}
}

static void clamp_weights(MDeformVert *dvert)
{
	MDeformWeight *dw = dvert->dw;
	unsigned int i;
	for (i = dvert->totweight; i != 0; i--, dw++) {
		CLAMP(dw->weight, 0.0f, 1.0f);
	}
}

/* struct to avoid passing many args each call to do_weight_paint_vertex()
 * this _could_ be made a part of the operators 'WPaintData' struct, or at
 * least a member, but for now keep its own struct, initialized on every
 * paint stroke update - campbell */
typedef struct WeightPaintInfo {

	int defbase_tot;

	/* both must add up to 'defbase_tot' */
	int defbase_tot_sel;
	int defbase_tot_unsel;

	int vgroup_active; /* (ob->actdef - 1) */
	int vgroup_mirror; /* mirror group or -1 */

	const bool *lock_flags;  /* boolean array for locked bones,
	                          * length of defbase_tot */
	const bool *defbase_sel; /* boolean array for selected bones,
	                          * length of defbase_tot, cant be const because of how its passed */

	const bool *vgroup_validmap; /* same as WeightPaintData.vgroup_validmap,
	                              * only added here for convenience */

	bool do_flip;
	bool do_multipaint;
	bool do_auto_normalize;

	float brush_alpha_value;  /* result of BKE_brush_alpha_get() */
} WeightPaintInfo;

/* fresh start to make multi-paint and locking modular */
/* returns TRUE if it thinks you need to reset the weights due to
 * normalizing while multi-painting
 *
 * note: this assumes dw->def_nr range has been checked by the caller
 */
static int apply_mp_locks_normalize(Mesh *me, const WeightPaintInfo *wpi,
                                    const unsigned int index,
                                    MDeformWeight *dw, MDeformWeight *tdw,
                                    float change, float oldChange,
                                    float oldw, float neww)
{
	MDeformVert *dv = &me->dvert[index];
	MDeformVert dv_test = {NULL};

	dv_test.dw = MEM_dupallocN(dv->dw);
	dv_test.flag = dv->flag;
	dv_test.totweight = dv->totweight;
	/* do not multi-paint if a locked group is selected or the active group is locked
	 * !lock_flags[dw->def_nr] helps if nothing is selected, but active group is locked */
	if ((wpi->lock_flags == NULL) ||
	    ((wpi->lock_flags[dw->def_nr] == false) && /* def_nr range has to be checked for by caller */
	     has_locked_group_selected(wpi->defbase_tot, wpi->defbase_sel, wpi->lock_flags) == false))
	{
		if (wpi->do_multipaint && wpi->defbase_tot_sel > 1) {
			if (change && change != 1) {
				multipaint_selection(dv, wpi->defbase_tot, change, wpi->defbase_sel);
			}
		}
		else { /* this lets users paint normally, but don't let them paint locked groups */
			dw->weight = neww;
		}
	}
	clamp_weights(dv);

	enforce_locks(&dv_test, dv, wpi->defbase_tot, wpi->defbase_sel, wpi->lock_flags, wpi->vgroup_validmap, wpi->do_auto_normalize, wpi->do_multipaint);

	if (wpi->do_auto_normalize) {
		/* XXX - should we pass the active group? - currently '-1' */
		do_weight_paint_normalize_all(dv, wpi->defbase_tot, wpi->vgroup_validmap);
	}

	if (oldChange && wpi->do_multipaint && wpi->defbase_tot_sel > 1) {
		if (tdw->weight != oldw) {
			if (neww > oldw) {
				if (tdw->weight <= oldw) {
					MEM_freeN(dv_test.dw);
					return TRUE;
				}
			}
			else {
				if (tdw->weight >= oldw) {
					MEM_freeN(dv_test.dw);
					return TRUE;
				}
			}
		}
	}
	MEM_freeN(dv_test.dw);
	return FALSE;
}

/* within the current dvert index, get the dw that is selected and has a weight
 * above 0, this helps multi-paint */
static int get_first_selected_nonzero_weight(MDeformVert *dvert, const int defbase_tot, const bool *defbase_sel)
{
	int i;
	MDeformWeight *dw = dvert->dw;
	for (i = 0; i < dvert->totweight; i++, dw++) {
		if (dw->def_nr < defbase_tot) {
			if (defbase_sel[dw->def_nr] && dw->weight > 0.0f) {
				return i;
			}
		}
	}
	return -1;
}

static void do_weight_paint_vertex(
        /* vars which remain the same for every vert */
        VPaint *wp, Object *ob, const WeightPaintInfo *wpi,
        /* vars which change on each stroke */
        const unsigned int index, float alpha, float paintweight
        )
{
	Mesh *me = ob->data;
	MDeformVert *dv = &me->dvert[index];
	bool topology = (me->editflag & ME_EDIT_MIRROR_TOPO) != 0;
	
	MDeformWeight *dw, *dw_prev;

	/* mirror vars */
	int index_mirr;
	int vgroup_mirr;

	MDeformVert *dv_mirr;
	MDeformWeight *dw_mirr;

	const short do_multipaint_totsel = (wpi->do_multipaint && wpi->defbase_tot_sel > 1);

	if (wp->flag & VP_ONLYVGROUP) {
		dw = defvert_find_index(dv, wpi->vgroup_active);
		dw_prev = defvert_find_index(wp->wpaint_prev + index, wpi->vgroup_active);
	}
	else {
		dw = defvert_verify_index(dv, wpi->vgroup_active);
		dw_prev = defvert_verify_index(wp->wpaint_prev + index, wpi->vgroup_active);
	}

	if (dw == NULL || dw_prev == NULL) {
		return;
	}


	/* from now on we can check if mirrors enabled if this var is -1 and not bother with the flag */
	if (me->editflag & ME_EDIT_MIRROR_X) {
		index_mirr = mesh_get_x_mirror_vert(ob, index, topology);
		vgroup_mirr = (wpi->vgroup_mirror != -1) ? wpi->vgroup_mirror : wpi->vgroup_active;

		/* another possible error - mirror group _and_ active group are the same (which is fine),
		 * but we also are painting onto a center vertex - this would paint the same weight twice */
		if (index_mirr == index && vgroup_mirr == wpi->vgroup_active) {
			index_mirr = vgroup_mirr = -1;
		}
	}
	else {
		index_mirr = vgroup_mirr = -1;
	}


	/* get the mirror def vars */
	if (index_mirr != -1) {
		dv_mirr = &me->dvert[index_mirr];
		if (wp->flag & VP_ONLYVGROUP) {
			dw_mirr = defvert_find_index(dv_mirr, vgroup_mirr);

			if (dw_mirr == NULL) {
				index_mirr = vgroup_mirr = -1;
				dv_mirr = NULL;
			}
		}
		else {
			if (index != index_mirr) {
				dw_mirr = defvert_verify_index(dv_mirr, vgroup_mirr);
			}
			else {
				/* dv and dv_mirr are the same */
				int totweight_prev = dv_mirr->totweight;
				int dw_offset = (int)(dw - dv_mirr->dw);
				dw_mirr = defvert_verify_index(dv_mirr, vgroup_mirr);

				/* if we added another, get our old one back */
				if (totweight_prev != dv_mirr->totweight) {
					dw = &dv_mirr->dw[dw_offset];
				}
			}
		}
	}
	else {
		dv_mirr = NULL;
		dw_mirr = NULL;
	}


	/* TODO: De-duplicate the simple weight paint - jason */
	/* ... or not, since its <10 SLOC - campbell */

	/* If there are no locks or multipaint,
	 * then there is no need to run the more complicated checks */
	if ((do_multipaint_totsel == FALSE) &&
	    (wpi->lock_flags == NULL || has_locked_group(dv, wpi->defbase_tot, wpi->vgroup_validmap, wpi->lock_flags) == FALSE))
	{
		dw->weight = wpaint_blend(wp, dw->weight, dw_prev->weight, alpha, paintweight,
		                          wpi->brush_alpha_value, wpi->do_flip, FALSE);

		/* WATCH IT: take care of the ordering of applying mirror -> normalize,
		 * can give wrong results [#26193], least confusing if normalize is done last */

		/* apply mirror */
		if (index_mirr != -1) {
			/* copy, not paint again */
			dw_mirr->weight = dw->weight;
		}

		/* apply normalize */
		if (wpi->do_auto_normalize) {
			/* note on normalize - this used to be applied after painting and normalize all weights,
			 * in some ways this is good because there is feedback where the more weights involved would
			 * 'resist' so you couldn't instantly zero out other weights by painting 1.0 on the active.
			 *
			 * However this gave a problem since applying mirror, then normalize both verts
			 * the resulting weight wont match on both sides.
			 *
			 * If this 'resisting', slower normalize is nicer, we could call
			 * do_weight_paint_normalize_all() and only use...
			 * do_weight_paint_normalize_all_active() when normalizing the mirror vertex.
			 * - campbell
			 */
			do_weight_paint_normalize_all_active(dv, wpi->defbase_tot, wpi->vgroup_validmap, wpi->vgroup_active);

			if (index_mirr != -1) {
				/* only normalize if this is not a center vertex, else we get a conflict, normalizing twice */
				if (index != index_mirr) {
					do_weight_paint_normalize_all_active(dv_mirr, wpi->defbase_tot, wpi->vgroup_validmap, vgroup_mirr);
				}
				else {
					/* this case accounts for...
					 * - painting onto a center vertex of a mesh
					 * - x mirror is enabled
					 * - auto normalize is enabled
					 * - the group you are painting onto has a L / R version
					 *
					 * We want L/R vgroups to have the same weight but this cant be if both are over 0.5,
					 * We _could_ have special check for that, but this would need its own normalize function which
					 * holds 2 groups from changing at once.
					 *
					 * So! just balance out the 2 weights, it keeps them equal and everything normalized.
					 *
					 * While it wont hit the desired weight immediately as the user waggles their mouse,
					 * constant painting and re-normalizing will get there. this is also just simpler logic.
					 * - campbell */
					dw_mirr->weight = dw->weight = (dw_mirr->weight + dw->weight) * 0.5f;
				}
			}
		}
	}
	else {
		/* use locks and/or multipaint */
		float oldw;
		float neww;
		/* float testw = 0; */ /* UNUSED */
		float observedChange = 0;
		float change = 0;
		float oldChange = 0;
		int i;
		MDeformWeight *tdw = NULL, *tdw_prev;
		MDeformVert dv_copy = {NULL};

		oldw = dw->weight;
		neww = wpaint_blend(wp, dw->weight, dw_prev->weight, alpha, paintweight,
		                    wpi->brush_alpha_value, wpi->do_flip, do_multipaint_totsel);
		
		/* setup multi-paint */
		observedChange = neww - oldw;
		if (do_multipaint_totsel && observedChange) {
			dv_copy.dw = MEM_dupallocN(dv->dw);
			dv_copy.flag = dv->flag;
			dv_copy.totweight = dv->totweight;
			tdw = dw;
			tdw_prev = dw_prev;
			change = get_mp_change(&wp->wpaint_prev[index], wpi->defbase_tot, wpi->defbase_sel, observedChange);
			if (change) {
				if (!tdw->weight) {
					i = get_first_selected_nonzero_weight(dv, wpi->defbase_tot, wpi->defbase_sel);
					if (i >= 0) {
						tdw = &(dv->dw[i]);
						tdw_prev = defvert_verify_index(&wp->wpaint_prev[index], tdw->def_nr);
					}
					else {
						change = 0;
					}
				}
				if (change && tdw_prev->weight && tdw_prev->weight * change) {
					if (tdw->weight != tdw_prev->weight) {
						oldChange = tdw->weight / tdw_prev->weight;
						/* testw = tdw_prev->weight * change; */ /* UNUSED */
						if (observedChange > 0) {
							if (change > oldChange) {
								/* reset the weights and use the new change */
								defvert_reset_to_prev(wp->wpaint_prev + index, dv);
							}
							else {
								/* the old change was more significant, so set
								 * the change to 0 so that it will not do another multi-paint */
								change = 0;
							}
						}
						else {
							if (change < oldChange) {
								defvert_reset_to_prev(wp->wpaint_prev + index, dv);
							}
							else {
								change = 0;
							}
						}
					}
				}
				else {
					change = 0;
				}
			}
		}
		
		if (apply_mp_locks_normalize(me, wpi, index, dw, tdw, change, oldChange, oldw, neww)) {
			defvert_reset_to_prev(&dv_copy, dv);
			change = 0;
			oldChange = 0;
		}
		if (dv_copy.dw) {
			MEM_freeN(dv_copy.dw);
		}
#if 0
		/* dv may have been altered greatly */
		dw = defvert_find_index(dv, vgroup);
#else
		dw = NULL; /* UNUSED after assignment, set to NULL to ensure we don't
		            * use again, we thats needed un-ifdef the line above */
		(void)dw;  /* quiet warnigns */
#endif

		/* x mirror painting */
		if (index_mirr != -1) {
			/* copy, not paint again */

			/* dw_mirr->weight = dw->weight; */  /* TODO, explain the logic in not assigning weight! - campbell */
			apply_mp_locks_normalize(me, wpi, index_mirr, dw_mirr, tdw, change, oldChange, oldw, neww);
		}
	}
}


/* *************** set wpaint operator ****************** */

/**
 * \note Keep in sync with #vpaint_mode_toggle_exec
 */
static int wpaint_mode_toggle_exec(bContext *C, wmOperator *op)
{		
	Object *ob = CTX_data_active_object(C);
	const int mode_flag = OB_MODE_WEIGHT_PAINT;
	const bool is_mode_set = (ob->mode & mode_flag) != 0;
	Scene *scene = CTX_data_scene(C);
	VPaint *wp = scene->toolsettings->wpaint;
	Mesh *me;

	if (!is_mode_set) {
		if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
			return OPERATOR_CANCELLED;
		}
	}

	me = BKE_mesh_from_object(ob);

	if (ob->mode & mode_flag) {
		ob->mode &= ~mode_flag;

		if (me->editflag & ME_EDIT_PAINT_VERT_SEL) {
			BKE_mesh_flush_select_from_verts(me);
		}
		else if (me->editflag & ME_EDIT_PAINT_FACE_SEL) {
			BKE_mesh_flush_select_from_polys(me);
		}

		/* weight paint specific */
		mesh_octree_table(NULL, NULL, NULL, 'e');
		mesh_mirrtopo_table(NULL, 'e');

		paint_cursor_delete_textures();
	}
	else {
		ob->mode |= mode_flag;

		if (wp == NULL)
			wp = scene->toolsettings->wpaint = new_vpaint(1);

		paint_cursor_start(C, weight_paint_poll);

		BKE_paint_init(&wp->paint, PAINT_CURSOR_WEIGHT_PAINT);

		/* weight paint specific */
		mesh_octree_table(ob, NULL, NULL, 's');
		ED_vgroup_sync_from_pose(ob);
	}
	
	/* Weightpaint works by overriding colors in mesh,
	 * so need to make sure we recalc on enter and
	 * exit (exit needs doing regardless because we
	 * should redeform).
	 */
	DAG_id_tag_update(&me->id, 0);

	WM_event_add_notifier(C, NC_SCENE | ND_MODE, scene);

	return OPERATOR_FINISHED;
}

/* for switching to/from mode */
static int paint_poll_test(bContext *C)
{
	Object *ob = CTX_data_active_object(C);
	if (ob == NULL || ob->type != OB_MESH)
		return 0;
	if (!ob->data || ((ID *)ob->data)->lib)
		return 0;
	if (CTX_data_edit_object(C))
		return 0;
	return 1;
}

void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
{
	
	/* identifiers */
	ot->name = "Weight Paint Mode";
	ot->idname = "PAINT_OT_weight_paint_toggle";
	ot->description = "Toggle weight paint mode in 3D view";
	
	/* api callbacks */
	ot->exec = wpaint_mode_toggle_exec;
	ot->poll = paint_poll_test;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
	
}

/* ************ weight paint operator ********** */

struct WPaintData {
	ViewContext vc;
	int *indexar;
	int vgroup_active;
	int vgroup_mirror;

	void *vp_handle;
	DMCoNo *vertexcosnos;

	float wpimat[3][3];

	/* variables for auto normalize */
	const bool *vgroup_validmap; /* stores if vgroups tie to deforming bones or not */
	const bool *lock_flags;
	int defbase_tot;
};

/* ensure we have data on wpaint start, add if needed */
static bool wpaint_ensure_data(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	Object *ob = CTX_data_active_object(C);
	Mesh *me = BKE_mesh_from_object(ob);

	if (scene->obedit) {
		return FALSE;
	}

	if (me == NULL || me->totpoly == 0) {
		return FALSE;
	}

	/* if nothing was added yet, we make dverts and a vertex deform group */
	if (!me->dvert) {
		ED_vgroup_data_create(&me->id);
		WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);
	}

	/* this happens on a Bone select, when no vgroup existed yet */
	if (ob->actdef <= 0) {
		Object *modob;
		if ((modob = modifiers_isDeformedByArmature(ob))) {
			Bone *actbone = ((bArmature *)modob->data)->act_bone;
			if (actbone) {
				bPoseChannel *pchan = BKE_pose_channel_find_name(modob->pose, actbone->name);

				if (pchan) {
					bDeformGroup *dg = defgroup_find_name(ob, pchan->name);
					if (dg == NULL) {
						dg = ED_vgroup_add_name(ob, pchan->name);  /* sets actdef */
					}
					else {
						int actdef = 1 + BLI_findindex(&ob->defbase, dg);
						BLI_assert(actdef >= 0);
						ob->actdef = actdef;
					}
				}
			}
		}
	}
	if (BLI_listbase_is_empty(&ob->defbase)) {
		ED_vgroup_add(ob);
	}

	/* ensure we don't try paint onto an invalid group */
	if (ob->actdef <= 0) {
		BKE_report(op->reports, RPT_WARNING, "No active vertex group for painting, aborting");
		return FALSE;
	}

	return TRUE;
}

static bool wpaint_stroke_test_start(bContext *C, wmOperator *op, const float UNUSED(mouse[2]))
{
	Scene *scene = CTX_data_scene(C);
	struct PaintStroke *stroke = op->customdata;
	ToolSettings *ts = scene->toolsettings;
	VPaint *wp = ts->wpaint;
	Object *ob = CTX_data_active_object(C);
	Mesh *me = BKE_mesh_from_object(ob);
	struct WPaintData *wpd;

	float mat[4][4], imat[4][4];

	if (wpaint_ensure_data(C, op) == FALSE) {
		return FALSE;
	}

	{
		/* check if we are attempting to paint onto a locked vertex group,
		 * and other options disallow it from doing anything useful */
		bDeformGroup *dg = BLI_findlink(&ob->defbase, (ob->actdef - 1));
		if (dg->flag & DG_LOCK_WEIGHT) {
			BKE_report(op->reports, RPT_WARNING, "Active group is locked, aborting");
			return FALSE;
		}
	}

	/* ALLOCATIONS! no return after this line */
	/* make mode data storage */
	wpd = MEM_callocN(sizeof(struct WPaintData), "WPaintData");
	paint_stroke_set_mode_data(stroke, wpd);
	view3d_set_viewcontext(C, &wpd->vc);

	wpd->vgroup_active = ob->actdef - 1;
	wpd->vgroup_mirror = -1;

	/* set up auto-normalize, and generate map for detecting which
	 * vgroups affect deform bones */
	wpd->defbase_tot = BLI_countlist(&ob->defbase);
	wpd->lock_flags = BKE_objdef_lock_flags_get(ob, wpd->defbase_tot);
	if (ts->auto_normalize || ts->multipaint || wpd->lock_flags) {
		wpd->vgroup_validmap = BKE_objdef_validmap_get(ob, wpd->defbase_tot);
	}

	/* painting on subsurfs should give correct points too, this returns me->totvert amount */
	wpd->vp_handle = ED_vpaint_proj_handle_create(scene, ob, &wpd->vertexcosnos);

	wpd->indexar = get_indexarray(me);
	copy_wpaint_prev(wp, me->dvert, me->totvert);

	/* imat for normals */
	mul_m4_m4m4(mat, wpd->vc.rv3d->viewmat, ob->obmat);
	invert_m4_m4(imat, mat);
	copy_m3_m4(wpd->wpimat, imat);

	/* if mirror painting, find the other group */
	if (me->editflag & ME_EDIT_MIRROR_X) {
		wpd->vgroup_mirror = wpaint_mirror_vgroup_ensure(ob, wpd->vgroup_active);
	}
	
	return TRUE;
}

static void wpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
{
	Scene *scene = CTX_data_scene(C);
	ToolSettings *ts = CTX_data_tool_settings(C);
	VPaint *wp = ts->wpaint;
	Brush *brush = BKE_paint_brush(&wp->paint);
	struct WPaintData *wpd = paint_stroke_mode_data(stroke);
	ViewContext *vc;
	Object *ob;
	Mesh *me;
	float mat[4][4];
	float paintweight;
	int *indexar;
	float totw;
	unsigned int index, totindex;
	float alpha;
	float mval[2];
	bool use_vert_sel;
	bool use_face_sel;
	bool use_depth;

	MDeformWeight *(*dw_func)(MDeformVert *, const int) =
	        (brush->vertexpaint_tool == PAINT_BLEND_BLUR) ?
	        ((wp->flag & VP_ONLYVGROUP) ?
	             (MDeformWeight *(*)(MDeformVert *, const int))defvert_find_index :
	              defvert_verify_index) : NULL;

	const float pressure = RNA_float_get(itemptr, "pressure");
	const float brush_size_pressure = BKE_brush_size_get(scene, brush) * (BKE_brush_use_size_pressure(scene, brush) ? pressure : 1.0f);
	const float brush_alpha_value = BKE_brush_alpha_get(scene, brush);
	const float brush_alpha_pressure = brush_alpha_value * (BKE_brush_use_alpha_pressure(scene, brush) ? pressure : 1.0f);

	/* intentionally don't initialize as NULL, make sure we initialize all members below */
	WeightPaintInfo wpi;

	/* cannot paint if there is no stroke data */
	if (wpd == NULL) {
		/* XXX: force a redraw here, since even though we can't paint,
		 * at least view won't freeze until stroke ends */
		ED_region_tag_redraw(CTX_wm_region(C));
		return;
	}

	vc = &wpd->vc;
	ob = vc->obact;
	me = ob->data;
	indexar = wpd->indexar;
	
	view3d_operator_needs_opengl(C);
	ED_view3d_init_mats_rv3d(ob, vc->rv3d);

	/* load projection matrix */
	mul_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat);

	RNA_float_get_array(itemptr, "mouse", mval);

	/* *** setup WeightPaintInfo - pass onto do_weight_paint_vertex *** */
	wpi.defbase_tot =        wpd->defbase_tot;
	wpi.defbase_sel = BKE_objdef_selected_get(ob, wpi.defbase_tot, &wpi.defbase_tot_sel);
	if (wpi.defbase_tot_sel == 0 && ob->actdef > 0) {
		wpi.defbase_tot_sel = 1;
	}

	wpi.defbase_tot_unsel =  wpi.defbase_tot - wpi.defbase_tot_sel;
	wpi.vgroup_active =      wpd->vgroup_active;
	wpi.vgroup_mirror =      wpd->vgroup_mirror;
	wpi.lock_flags =         wpd->lock_flags;
	wpi.vgroup_validmap =    wpd->vgroup_validmap;
	wpi.do_flip =            RNA_boolean_get(itemptr, "pen_flip");
	wpi.do_multipaint =      (ts->multipaint != 0);
	wpi.do_auto_normalize =  ((ts->auto_normalize != 0) && (wpi.vgroup_validmap != NULL));
	wpi.brush_alpha_value =  brush_alpha_value;
	/* *** done setting up WeightPaintInfo *** */



	swap_m4m4(wpd->vc.rv3d->persmat, mat);

	use_vert_sel = (me->editflag & ME_EDIT_PAINT_VERT_SEL) != 0;
	use_face_sel = (me->editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
	use_depth = (vc->v3d->flag & V3D_ZBUF_SELECT) != 0;

	/* which faces are involved */
	if (use_depth) {
		char editflag_prev = me->editflag;

		/* Ugly hack, to avoid drawing vertex index when getting the face index buffer - campbell */
		me->editflag &= ~ME_EDIT_PAINT_VERT_SEL;
		if (use_vert_sel) {
			/* Ugly x2, we need this so hidden faces don't draw */
			me->editflag |= ME_EDIT_PAINT_FACE_SEL;
		}
		totindex = sample_backbuf_area(vc, indexar, me->totpoly, mval[0], mval[1], brush_size_pressure);
		me->editflag = editflag_prev;

		if (use_face_sel && me->totpoly) {
			MPoly *mpoly = me->mpoly;
			for (index = 0; index < totindex; index++) {
				if (indexar[index] && indexar[index] <= me->totpoly) {
					MPoly *mp = &mpoly[indexar[index] - 1];

					if ((mp->flag & ME_FACE_SEL) == 0) {
						indexar[index] = 0;
					}
				}
			}
		}
	}
	else {
		indexar = NULL;
	}

	/* incase we have modifiers */
	ED_vpaint_proj_handle_update(wpd->vp_handle, vc->ar, mval);

	/* make sure each vertex gets treated only once */
	/* and calculate filter weight */
	totw = 0.0f;
	if (brush->vertexpaint_tool == PAINT_BLEND_BLUR)
		paintweight = 0.0f;
	else
		paintweight = BKE_brush_weight_get(scene, brush);

#define WP_BLUR_ACCUM(v_idx_var)  \
	{ \
		const unsigned int vidx = v_idx_var; \
		const float fac = calc_vp_strength_col_dl(wp, vc, wpd->vertexcosnos[vidx].co, mval, brush_size_pressure, NULL); \
		if (fac > 0.0f) { \
			MDeformWeight *dw = dw_func(&me->dvert[vidx], wpi.vgroup_active); \
			paintweight += dw ? (dw->weight * fac) : 0.0f; \
			totw += fac; \
		} \
	} (void)0


	if (use_depth) {
		for (index = 0; index < totindex; index++) {
			if (indexar[index] && indexar[index] <= me->totpoly) {
				MPoly *mpoly = me->mpoly + (indexar[index] - 1);
				MLoop *ml = me->mloop + mpoly->loopstart;
				int i;

				if (use_vert_sel) {
					for (i = 0; i < mpoly->totloop; i++, ml++) {
						me->dvert[ml->v].flag = (me->mvert[ml->v].flag & SELECT);
					}
				}
				else {
					for (i = 0; i < mpoly->totloop; i++, ml++) {
						me->dvert[ml->v].flag = 1;
					}
				}

				if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
					ml = me->mloop + mpoly->loopstart;
					for (i = 0; i < mpoly->totloop; i++, ml++) {
						WP_BLUR_ACCUM(ml->v);
					}
				}
			}
		}
	}
	else {
		const unsigned int totvert = me->totvert;
		unsigned int       i;

		/* in the case of face selection we need to flush */
		if (use_vert_sel || use_face_sel) {
			for (i = 0; i < totvert; i++) {
				me->dvert[i].flag = me->mvert[i].flag & SELECT;
			}
		}
		else {
			for (i = 0; i < totvert; i++) {
				me->dvert[i].flag = SELECT;
			}
		}

		if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
			for (i = 0; i < totvert; i++) {
				WP_BLUR_ACCUM(i);
			}
		}
	}

#undef WP_BLUR_ACCUM


	if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
		paintweight /= totw;
	}

#define WP_PAINT(v_idx_var)  \
	{ \
		unsigned int vidx = v_idx_var; \
		if (me->dvert[vidx].flag) { \
			alpha = calc_vp_alpha_col_dl(wp, vc, wpd->wpimat, &wpd->vertexcosnos[vidx], \
			                         mval, brush_size_pressure, brush_alpha_pressure, NULL); \
			if (alpha) { \
				do_weight_paint_vertex(wp, ob, &wpi, vidx, alpha, paintweight); \
			} \
			me->dvert[vidx].flag = 0; \
		} \
	} (void)0

	if (use_depth) {
		for (index = 0; index < totindex; index++) {

			if (indexar[index] && indexar[index] <= me->totpoly) {
				MPoly *mpoly = me->mpoly + (indexar[index] - 1);
				MLoop *ml = me->mloop + mpoly->loopstart;
				int i;

				for (i = 0; i < mpoly->totloop; i++, ml++) {
					WP_PAINT(ml->v);
				}
			}
		}
	}
	else {
		const unsigned int totvert = me->totvert;
		unsigned int       i;

		for (i = 0; i < totvert; i++) {
			WP_PAINT(i);
		}
	}
#undef WP_PAINT


	/* *** free wpi members */
	MEM_freeN((void *)wpi.defbase_sel);
	/* *** done freeing wpi members */


	swap_m4m4(vc->rv3d->persmat, mat);

	{
		UnifiedPaintSettings *ups = &ts->unified_paint_settings;
		ups->pressure_value = pressure;
	}

	DAG_id_tag_update(ob->data, 0);
	ED_region_tag_redraw(vc->ar);
}

static void wpaint_stroke_done(const bContext *C, struct PaintStroke *stroke)
{
	ToolSettings *ts = CTX_data_tool_settings(C);
	Object *ob = CTX_data_active_object(C);
	struct WPaintData *wpd = paint_stroke_mode_data(stroke);
	
	if (wpd) {
		ED_vpaint_proj_handle_free(wpd->vp_handle);
		MEM_freeN(wpd->indexar);
		
		if (wpd->vgroup_validmap)
			MEM_freeN((void *)wpd->vgroup_validmap);
		if (wpd->lock_flags)
			MEM_freeN((void *)wpd->lock_flags);

		MEM_freeN(wpd);
	}
	
	/* frees prev buffer */
	copy_wpaint_prev(ts->wpaint, NULL, 0);
	
	/* and particles too */
	if (ob->particlesystem.first) {
		ParticleSystem *psys;
		int i;
		
		for (psys = ob->particlesystem.first; psys; psys = psys->next) {
			for (i = 0; i < PSYS_TOT_VG; i++) {
				if (psys->vgroup[i] == ob->actdef) {
					psys->recalc |= PSYS_RECALC_RESET;
					break;
				}
			}
		}
	}

	DAG_id_tag_update(ob->data, 0);

	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
}


static int wpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	int retval;

	op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
	                                  wpaint_stroke_update_step, NULL,
	                                  wpaint_stroke_done, event->type);
	
	/* add modal handler */
	WM_event_add_modal_handler(C, op);

	retval = op->type->modal(C, op, event);
	OPERATOR_RETVAL_CHECK(retval);
	BLI_assert(retval == OPERATOR_RUNNING_MODAL);
	
	return OPERATOR_RUNNING_MODAL;
}

static int wpaint_exec(bContext *C, wmOperator *op)
{
	op->customdata = paint_stroke_new(C, NULL, wpaint_stroke_test_start,
	                                  wpaint_stroke_update_step, NULL,
	                                  wpaint_stroke_done, 0);

	/* frees op->customdata */
	paint_stroke_exec(C, op);

	return OPERATOR_FINISHED;
}

static void wpaint_cancel(bContext *C, wmOperator *op)
{
	paint_stroke_cancel(C, op);
}

void PAINT_OT_weight_paint(wmOperatorType *ot)
{
	
	/* identifiers */
	ot->name = "Weight Paint";
	ot->idname = "PAINT_OT_weight_paint";
	ot->description = "Paint a stroke in the current vertex group's weights";
	
	/* api callbacks */
	ot->invoke = wpaint_invoke;
	ot->modal = paint_stroke_modal;
	ot->exec = wpaint_exec;
	ot->poll = weight_paint_poll;
	ot->cancel = wpaint_cancel;
	
	/* flags */
	ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING;

	RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
}

static int weight_paint_set_exec(bContext *C, wmOperator *op)
{
	struct Scene *scene = CTX_data_scene(C);
	Object *obact = CTX_data_active_object(C);
	ToolSettings *ts = CTX_data_tool_settings(C);
	Brush *brush = BKE_paint_brush(&ts->wpaint->paint);
	float vgroup_weight = BKE_brush_weight_get(scene, brush);

	if (wpaint_ensure_data(C, op) == FALSE) {
		return OPERATOR_CANCELLED;
	}

	if (ED_wpaint_fill(scene->toolsettings->wpaint, obact, vgroup_weight)) {
		ED_region_tag_redraw(CTX_wm_region(C)); /* XXX - should redraw all 3D views */
		return OPERATOR_FINISHED;
	}
	else {
		return OPERATOR_CANCELLED;
	}
}

void PAINT_OT_weight_set(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Set Weight";
	ot->idname = "PAINT_OT_weight_set";
	ot->description = "Fill the active vertex group with the current paint weight";

	/* api callbacks */
	ot->exec = weight_paint_set_exec;
	ot->poll = mask_paint_poll;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}

/* ************ set / clear vertex paint mode ********** */

/**
 * \note Keep in sync with #wpaint_mode_toggle_exec
 */
static int vpaint_mode_toggle_exec(bContext *C, wmOperator *op)
{	
	Object *ob = CTX_data_active_object(C);
	const int mode_flag = OB_MODE_VERTEX_PAINT;
	const bool is_mode_set = (ob->mode & mode_flag) != 0;
	Scene *scene = CTX_data_scene(C);
	VPaint *vp = scene->toolsettings->vpaint;
	Mesh *me;

	if (!is_mode_set) {
		if (!ED_object_mode_compat_set(C, ob, mode_flag, op->reports)) {
			return OPERATOR_CANCELLED;
		}
	}

	me = BKE_mesh_from_object(ob);
	
	/* toggle: end vpaint */
	if (is_mode_set) {
		ob->mode &= ~mode_flag;

		if (me->editflag & ME_EDIT_PAINT_FACE_SEL) {
			BKE_mesh_flush_select_from_polys(me);
		}

		paint_cursor_delete_textures();
	}
	else {
		ob->mode |= mode_flag;

		if (me->mloopcol == NULL) {
			make_vertexcol(ob);
		}

		if (vp == NULL)
			vp = scene->toolsettings->vpaint = new_vpaint(0);
		
		paint_cursor_start(C, vertex_paint_poll);

		BKE_paint_init(&vp->paint, PAINT_CURSOR_VERTEX_PAINT);
	}
	
	/* update modifier stack for mapping requirements */
	DAG_id_tag_update(&me->id, 0);
	
	WM_event_add_notifier(C, NC_SCENE | ND_MODE, scene);
	
	return OPERATOR_FINISHED;
}

void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
{
	
	/* identifiers */
	ot->name = "Vertex Paint Mode";
	ot->idname = "PAINT_OT_vertex_paint_toggle";
	ot->description = "Toggle the vertex paint mode in 3D view";
	
	/* api callbacks */
	ot->exec = vpaint_mode_toggle_exec;
	ot->poll = paint_poll_test;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}



/* ********************** vertex paint operator ******************* */

/* Implementation notes:
 *
 * Operator->invoke()
 * - validate context (add mcol)
 * - create customdata storage
 * - call paint once (mouse click)
 * - add modal handler 
 *
 * Operator->modal()
 * - for every mousemove, apply vertex paint
 * - exit on mouse release, free customdata
 *   (return OPERATOR_FINISHED also removes handler and operator)
 *
 * For future:
 * - implement a stroke event (or mousemove with past positons)
 * - revise whether op->customdata should be added in object, in set_vpaint
 */

typedef struct PolyFaceMap {
	struct PolyFaceMap *next, *prev;
	int facenr;
} PolyFaceMap;

typedef struct VPaintData {
	ViewContext vc;
	unsigned int paintcol;
	int *indexar;

	struct VertProjHandle *vp_handle;
	DMCoNo *vertexcosnos;

	float vpimat[3][3];

	/* modify 'me->mcol' directly, since the derived mesh is drawing from this
	 * array, otherwise we need to refresh the modifier stack */
	bool use_fast_update;

	/* mpoly -> mface mapping */
	MeshElemMap *polyfacemap;
	void *polyfacemap_mem;

	/* loops tagged as having been painted, to apply shared vertex color
	 * blending only to modified loops */
	bool *mlooptag;
	bool *mfacetag;

	bool is_texbrush;
} VPaintData;

static void vpaint_build_poly_facemap(struct VPaintData *vd, Mesh *me)
{
	const int *tessface_origindex;

	vd->polyfacemap = NULL;
	vd->polyfacemap_mem = NULL;

	tessface_origindex = CustomData_get_layer(&me->fdata, CD_ORIGINDEX);

	if (!tessface_origindex)
		return;

	BKE_mesh_origindex_map_create(&vd->polyfacemap, (int **)&vd->polyfacemap_mem,
	                              me->totpoly,
	                              tessface_origindex, me->totface);
}

static bool vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const float UNUSED(mouse[2]))
{
	ToolSettings *ts = CTX_data_tool_settings(C);
	struct PaintStroke *stroke = op->customdata;
	VPaint *vp = ts->vpaint;
	Brush *brush = BKE_paint_brush(&vp->paint);
	struct VPaintData *vpd;
	Object *ob = CTX_data_active_object(C);
	Mesh *me;
	float mat[4][4], imat[4][4];

	/* context checks could be a poll() */
	me = BKE_mesh_from_object(ob);
	if (me == NULL || me->totpoly == 0)
		return false;
	
	if (me->mloopcol == NULL)
		make_vertexcol(ob);
	if (me->mloopcol == NULL)
		return false;

	/* Update tessface data if needed
	 * Added here too because e.g. switching to/from edit mode would remove tessface data,
	 * yet "fast_update" could still be used! */
	update_tessface_data(ob, me);

	/* make mode data storage */
	vpd = MEM_callocN(sizeof(struct VPaintData), "VPaintData");
	paint_stroke_set_mode_data(stroke, vpd);
	view3d_set_viewcontext(C, &vpd->vc);
	
	vpd->vp_handle = ED_vpaint_proj_handle_create(vpd->vc.scene, ob, &vpd->vertexcosnos);

	vpd->indexar = get_indexarray(me);
	vpd->paintcol = vpaint_get_current_col(vp);

	vpd->is_texbrush = !(brush->vertexpaint_tool == PAINT_BLEND_BLUR) &&
	                   brush->mtex.tex;

	/* are we painting onto a modified mesh?,
	 * if not we can skip face map trickyness */
	if (vertex_paint_use_fast_update_check(ob)) {
		vpaint_build_poly_facemap(vpd, me);
		vpd->use_fast_update = TRUE;
/*		printf("Fast update!\n");*/
	}
	else {
		vpd->use_fast_update = FALSE;
/*		printf("No fast update!\n");*/
	}

	/* to keep tracked of modified loops for shared vertex color blending */
	if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
		vpd->mlooptag = MEM_mallocN(sizeof(bool) * me->totloop, "VPaintData mlooptag");
		if (vpd->use_fast_update)
			vpd->mfacetag = MEM_mallocN(sizeof(bool) * me->totface * 4, "VPaintData mfacetag");
	}

	/* for filtering */
	copy_vpaint_prev(vp, (unsigned int *)me->mloopcol, me->totloop);
	
	/* some old cruft to sort out later */
	mul_m4_m4m4(mat, vpd->vc.rv3d->viewmat, ob->obmat);
	invert_m4_m4(imat, mat);
	copy_m3_m4(vpd->vpimat, imat);

	return 1;
}

static void vpaint_paint_poly(VPaint *vp, VPaintData *vpd, Mesh *me,
                              const unsigned int index, const float mval[2],
                              const float brush_size_pressure, const float brush_alpha_pressure)
{
	ViewContext *vc = &vpd->vc;
	Brush *brush = BKE_paint_brush(&vp->paint);
	MPoly *mpoly = &me->mpoly[index];
	MFace *mf;
	MCol *mc;
	MLoop *ml;
	MLoopCol *mlc;
	unsigned int *lcol = ((unsigned int *)me->mloopcol) + mpoly->loopstart;
	unsigned int *lcolorig = ((unsigned int *)vp->vpaint_prev) + mpoly->loopstart;
	bool *mlooptag = (vpd->mlooptag) ? vpd->mlooptag + mpoly->loopstart : NULL;
	bool *mftag;
	float alpha;
	int i, j;
	int totloop = mpoly->totloop;

	int brush_alpha_pressure_i = (int)(brush_alpha_pressure * 255.0f);

	if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
		unsigned int blend[4] = {0};
		unsigned int tcol;
		char *col;

		for (j = 0; j < totloop; j++) {
			col = (char *)(lcol + j);
			blend[0] += col[0];
			blend[1] += col[1];
			blend[2] += col[2];
			blend[3] += col[3];
		}

		blend[0] = divide_round_i(blend[0], totloop);
		blend[1] = divide_round_i(blend[1], totloop);
		blend[2] = divide_round_i(blend[2], totloop);
		blend[3] = divide_round_i(blend[3], totloop);
		col = (char *)&tcol;
		col[0] = blend[0];
		col[1] = blend[1];
		col[2] = blend[2];
		col[3] = blend[3];

		vpd->paintcol = *((unsigned int *)col);
	}

	ml = me->mloop + mpoly->loopstart;
	for (i = 0; i < totloop; i++, ml++) {
		float rgba[4];
		unsigned int paintcol;
		alpha = calc_vp_alpha_col_dl(vp, vc, vpd->vpimat,
		                         &vpd->vertexcosnos[ml->v], mval,
		                         brush_size_pressure, brush_alpha_pressure, rgba);

		if (vpd->is_texbrush) {
			float rgba_br[3];
			rgb_uchar_to_float(rgba_br, (const unsigned char *)&vpd->paintcol);
			mul_v3_v3(rgba_br, rgba);
			rgb_float_to_uchar((unsigned char *)&paintcol, rgba_br);
		}
		else
			paintcol = vpd->paintcol;

		if (alpha > 0.0f) {
			const int alpha_i = (int)(alpha * 255.0f);
			lcol[i] = vpaint_blend(vp, lcol[i], lcolorig[i], paintcol, alpha_i, brush_alpha_pressure_i);

			if (mlooptag) mlooptag[i] = 1;
		}
	}

	if (vpd->use_fast_update) {
		const MeshElemMap *map = &vpd->polyfacemap[index];

		/* update vertex colors for tessellations incrementally,
		 * rather then regenerating the tessellation altogether */
		for (i = 0; i < map->count; i++) {
			const int index_tessface = map->indices[i];

			mf = &me->mface[index_tessface];
			mc = &me->mcol[index_tessface * 4];
			mftag = &vpd->mfacetag[index_tessface * 4];

			ml = me->mloop + mpoly->loopstart;
			mlc = me->mloopcol + mpoly->loopstart;

			for (j = 0; j < totloop; j++, ml++, mlc++) {
				/* search for the loop vertex within the tessface */
				const int fidx = BKE_MESH_TESSFACE_VINDEX_ORDER(mf, ml->v);
				if (fidx != -1) {
					MESH_MLOOPCOL_TO_MCOL(mlc, mc + fidx);
					if (mlooptag) {
						mftag[fidx] = mlooptag[j];
					}
				}
			}
		}
	}

}

static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
{
	Scene *scene = CTX_data_scene(C);
	ToolSettings *ts = CTX_data_tool_settings(C);
	struct VPaintData *vpd = paint_stroke_mode_data(stroke);
	VPaint *vp = ts->vpaint;
	Brush *brush = BKE_paint_brush(&vp->paint);
	ViewContext *vc = &vpd->vc;
	Object *ob = vc->obact;
	Mesh *me = ob->data;
	float mat[4][4];
	int *indexar = vpd->indexar;
	int totindex, index;
	float mval[2];

	const float pressure = RNA_float_get(itemptr, "pressure");
	const float brush_size_pressure = BKE_brush_size_get(scene, brush) * (BKE_brush_use_size_pressure(scene, brush) ? pressure : 1.0f);
	const float brush_alpha_pressure = BKE_brush_alpha_get(scene, brush) * (BKE_brush_use_alpha_pressure(scene, brush) ? pressure : 1.0f);

	RNA_float_get_array(itemptr, "mouse", mval);

	view3d_operator_needs_opengl(C);
	ED_view3d_init_mats_rv3d(ob, vc->rv3d);

	/* load projection matrix */
	mul_m4_m4m4(mat, vc->rv3d->persmat, ob->obmat);

	/* which faces are involved */
	totindex = sample_backbuf_area(vc, indexar, me->totpoly, mval[0], mval[1], brush_size_pressure);

	if ((me->editflag & ME_EDIT_PAINT_FACE_SEL) && me->mpoly) {
		for (index = 0; index < totindex; index++) {
			if (indexar[index] && indexar[index] <= me->totpoly) {
				MPoly *mpoly = ((MPoly *)me->mpoly) + (indexar[index] - 1);
						
				if ((mpoly->flag & ME_FACE_SEL) == 0)
					indexar[index] = 0;
			}
		}
	}
	
	swap_m4m4(vc->rv3d->persmat, mat);

	/* incase we have modifiers */
	ED_vpaint_proj_handle_update(vpd->vp_handle, vc->ar, mval);

	/* clear modified tag for blur tool */
	if (vpd->mlooptag)
		memset(vpd->mlooptag, 0, sizeof(bool) * me->totloop);
	if (vpd->mfacetag)
		memset(vpd->mfacetag, 0, sizeof(bool) * me->totface * 4);

	for (index = 0; index < totindex; index++) {
		if (indexar[index] && indexar[index] <= me->totpoly) {
			vpaint_paint_poly(vp, vpd, me, indexar[index] - 1, mval, brush_size_pressure, brush_alpha_pressure);
		}
	}
		
	swap_m4m4(vc->rv3d->persmat, mat);

	/* was disabled because it is slow, but necessary for blur */
	if (brush->vertexpaint_tool == PAINT_BLEND_BLUR) {
		int do_tessface = vpd->use_fast_update;
		do_shared_vertexcol(me, vpd->mlooptag, vpd->mfacetag, do_tessface);
	}

	{
		UnifiedPaintSettings *ups = &ts->unified_paint_settings;
		ups->pressure_value = pressure;
	}

	ED_region_tag_redraw(vc->ar);

	if (vpd->use_fast_update == FALSE) {
		/* recalculate modifier stack to get new colors, slow,
		 * avoid this if we can! */
		DAG_id_tag_update(ob->data, 0);
	}
	else if (!GPU_buffer_legacy(ob->derivedFinal)) {
		/* If using new VBO drawing, mark mcol as dirty to force colors gpu buffer refresh! */
		ob->derivedFinal->dirty |= DM_DIRTY_MCOL_UPDATE_DRAW;
	}
}

static void vpaint_stroke_done(const bContext *C, struct PaintStroke *stroke)
{
	ToolSettings *ts = CTX_data_tool_settings(C);
	struct VPaintData *vpd = paint_stroke_mode_data(stroke);
	ViewContext *vc = &vpd->vc;
	Object *ob = vc->obact;
	
	ED_vpaint_proj_handle_free(vpd->vp_handle);
	MEM_freeN(vpd->indexar);
	
	/* frees prev buffer */
	copy_vpaint_prev(ts->vpaint, NULL, 0);

	if (vpd->polyfacemap) {
		MEM_freeN(vpd->polyfacemap);
	}

	if (vpd->polyfacemap_mem) {
		MEM_freeN(vpd->polyfacemap_mem);
	}

	if (vpd->mlooptag)
		MEM_freeN(vpd->mlooptag);

	if (vpd->mfacetag)
		MEM_freeN(vpd->mfacetag);

	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);

	MEM_freeN(vpd);
}

static int vpaint_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	int retval;

	op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
	                                  vpaint_stroke_update_step, NULL,
	                                  vpaint_stroke_done, event->type);
	
	/* add modal handler */
	WM_event_add_modal_handler(C, op);

	retval = op->type->modal(C, op, event);
	OPERATOR_RETVAL_CHECK(retval);
	BLI_assert(retval == OPERATOR_RUNNING_MODAL);
	
	return OPERATOR_RUNNING_MODAL;
}

static int vpaint_exec(bContext *C, wmOperator *op)
{
	op->customdata = paint_stroke_new(C, NULL, vpaint_stroke_test_start,
	                                  vpaint_stroke_update_step, NULL,
	                                  vpaint_stroke_done, 0);

	/* frees op->customdata */
	paint_stroke_exec(C, op);

	return OPERATOR_FINISHED;
}

static void vpaint_cancel(bContext *C, wmOperator *op)
{
	paint_stroke_cancel(C, op);
}

void PAINT_OT_vertex_paint(wmOperatorType *ot)
{
	/* identifiers */
	ot->name = "Vertex Paint";
	ot->idname = "PAINT_OT_vertex_paint";
	ot->description = "Paint a stroke in the active vertex color layer";
	
	/* api callbacks */
	ot->invoke = vpaint_invoke;
	ot->modal = paint_stroke_modal;
	ot->exec = vpaint_exec;
	ot->poll = vertex_paint_poll;
	ot->cancel = vpaint_cancel;
	
	/* flags */
	ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING;

	RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
}

/* ********************** weight from bones operator ******************* */

static int weight_from_bones_poll(bContext *C)
{
	Object *ob = CTX_data_active_object(C);

	return (ob && (ob->mode & OB_MODE_WEIGHT_PAINT) && modifiers_isDeformedByArmature(ob));
}

static int weight_from_bones_exec(bContext *C, wmOperator *op)
{
	Scene *scene = CTX_data_scene(C);
	Object *ob = CTX_data_active_object(C);
	Object *armob = modifiers_isDeformedByArmature(ob);
	Mesh *me = ob->data;
	int type = RNA_enum_get(op->ptr, "type");

	create_vgroups_from_armature(op->reports, scene, ob, armob, type, (me->editflag & ME_EDIT_MIRROR_X));

	DAG_id_tag_update(&me->id, 0);
	WM_event_add_notifier(C, NC_GEOM | ND_DATA, me);

	return OPERATOR_FINISHED;
}

void PAINT_OT_weight_from_bones(wmOperatorType *ot)
{
	static EnumPropertyItem type_items[] = {
		{ARM_GROUPS_AUTO, "AUTOMATIC", 0, "Automatic", "Automatic weights from bones"},
		{ARM_GROUPS_ENVELOPE, "ENVELOPES", 0, "From Envelopes", "Weights from envelopes with user defined radius"},
		{0, NULL, 0, NULL, NULL}};

	/* identifiers */
	ot->name = "Weight from Bones";
	ot->idname = "PAINT_OT_weight_from_bones";
	ot->description = "Set the weights of the groups matching the attached armature's selected bones, "
	                  "using the distance between the vertices and the bones";
	
	/* api callbacks */
	ot->exec = weight_from_bones_exec;
	ot->invoke = WM_menu_invoke;
	ot->poll = weight_from_bones_poll;
	
	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	/* properties */
	ot->prop = RNA_def_enum(ot->srna, "type", type_items, 0, "Type", "Method to use for assigning weights");
}

/* *** VGroups Gradient *** */
typedef struct DMGradient_vertStore {
	float sco[2];
	float weight_orig;
	enum {
		VGRAD_STORE_NOP      = 0,
		VGRAD_STORE_DW_EXIST = (1 << 0)
	} flag;
} DMGradient_vertStore;

typedef struct DMGradient_userData {
	struct ARegion *ar;
	Scene *scene;
	Mesh *me;
	Brush *brush;
	const float *sco_start;     /* [2] */
	const float *sco_end;       /* [2] */
	float        sco_line_div;  /* store (1.0f / len_v2v2(sco_start, sco_end)) */
	int def_nr;
	short is_init;
	DMGradient_vertStore *vert_cache;

	/* options */
	short use_select;
	short type;
	float weightpaint;
} DMGradient_userData;

static void gradientVert__mapFunc(void *userData, int index, const float co[3],
                                  const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
{
	DMGradient_userData *grad_data = userData;
	Mesh *me = grad_data->me;

	if (grad_data->use_select == FALSE || (me->mvert[index].flag & SELECT)) {
		DMGradient_vertStore *vs = &grad_data->vert_cache[index];

		/* run first pass only, could be split into its own mapFunc
		 * the screen coords of the verts need to be cached because
		 * updating the mesh may move them about (entering feedback loop) */
		if (grad_data->is_init) {
			if (ED_view3d_project_float_object(grad_data->ar,
			                                   co, vs->sco,
			                                   V3D_PROJ_TEST_CLIP_BB | V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK)
			{
				/* ok */
				MDeformVert *dv = &me->dvert[index];
				MDeformWeight *dw;
				dw = defvert_find_index(dv, grad_data->def_nr);
				if (dw) {
					vs->weight_orig = dw->weight;
					vs->flag = VGRAD_STORE_DW_EXIST;
				}
				else {
					vs->weight_orig = 0.0f;
					vs->flag = VGRAD_STORE_NOP;
				}
			}
			else {
				/* no go */
				copy_v2_fl(vs->sco, FLT_MAX);
			}
		}
		/* end init */

		if (vs->sco[0] != FLT_MAX) {
			float alpha;

			if (grad_data->type == WPAINT_GRADIENT_TYPE_LINEAR) {
				alpha = line_point_factor_v2(vs->sco, grad_data->sco_start, grad_data->sco_end);
			}
			else {
				BLI_assert(grad_data->type == WPAINT_GRADIENT_TYPE_RADIAL);
				alpha = len_v2v2(grad_data->sco_start, vs->sco) * grad_data->sco_line_div;
			}
			/* no need to clamp 'alpha' yet */

			/* adjust weight */
			alpha = BKE_brush_curve_strength_clamp(grad_data->brush, alpha, 1.0f);

			if (alpha != 0.0f) {
				MDeformVert *dv = &me->dvert[index];
				MDeformWeight *dw = defvert_verify_index(dv, grad_data->def_nr);
				// dw->weight = alpha; // testing
				int tool = grad_data->brush->vertexpaint_tool;
				float testw;

				/* init if we just added */
				testw = wpaint_blend_tool(tool, vs->weight_orig, grad_data->weightpaint, alpha * grad_data->brush->alpha);
				CLAMP(testw, 0.0f, 1.0f);
				dw->weight = testw;
			}
			else {
				MDeformVert *dv = &me->dvert[index];
				if (vs->flag & VGRAD_STORE_DW_EXIST) {
					/* normally we NULL check, but in this case we know it exists */
					MDeformWeight *dw = defvert_find_index(dv, grad_data->def_nr);
					dw->weight = vs->weight_orig;
				}
				else {
					/* wasn't originally existing, remove */
					MDeformWeight *dw = defvert_find_index(dv, grad_data->def_nr);
					if (dw) {
						defvert_remove_group(dv, dw);
					}
				}
			}
		}
	}
}

static int paint_weight_gradient_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
	int ret = WM_gesture_straightline_modal(C, op, event);

	if (ret & OPERATOR_RUNNING_MODAL) {
		if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {  /* XXX, hardcoded */
			/* generally crap! redo! */
			WM_gesture_straightline_cancel(C, op);
			ret &= ~OPERATOR_RUNNING_MODAL;
			ret |= OPERATOR_FINISHED;
		}
	}

	if (ret & OPERATOR_CANCELLED) {
		ToolSettings *ts = CTX_data_tool_settings(C);
		VPaint *wp = ts->wpaint;
		Object *ob = CTX_data_active_object(C);
		Mesh *me = ob->data;
		if (wp->wpaint_prev) {
			BKE_defvert_array_free_elems(me->dvert, me->totvert);
			BKE_defvert_array_copy(me->dvert, wp->wpaint_prev, me->totvert);
			free_wpaint_prev(wp);
		}

		DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
		WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);
	}
	else if (ret & OPERATOR_FINISHED) {
		ToolSettings *ts = CTX_data_tool_settings(C);
		VPaint *wp = ts->wpaint;
		free_wpaint_prev(wp);
	}

	return ret;
}

static int paint_weight_gradient_exec(bContext *C, wmOperator *op)
{
	wmGesture *gesture = op->customdata;
	DMGradient_vertStore *vert_cache;
	struct ARegion *ar = CTX_wm_region(C);
	Scene *scene = CTX_data_scene(C);
	Object *ob = CTX_data_active_object(C);
	Mesh *me = ob->data;
	int x_start = RNA_int_get(op->ptr, "xstart");
	int y_start = RNA_int_get(op->ptr, "ystart");
	int x_end = RNA_int_get(op->ptr, "xend");
	int y_end = RNA_int_get(op->ptr, "yend");
	float sco_start[2] = {x_start, y_start};
	float sco_end[2] = {x_end, y_end};
	const bool is_interactive = (gesture != NULL);
	DerivedMesh *dm = mesh_get_derived_final(scene, ob, scene->customdata_mask);

	DMGradient_userData data = {NULL};

	if (is_interactive) {
		if (gesture->userdata == NULL) {
			VPaint *wp = scene->toolsettings->wpaint;

			gesture->userdata = MEM_mallocN(sizeof(DMGradient_vertStore) * me->totvert, __func__);
			data.is_init = true;

			copy_wpaint_prev(wp, me->dvert, me->totvert);

			/* on init only, convert face -> vert sel  */
			if (me->editflag & ME_EDIT_PAINT_FACE_SEL) {
				BKE_mesh_flush_select_from_polys(me);
			}
		}

		vert_cache = gesture->userdata;
	}
	else {
		if (wpaint_ensure_data(C, op) == FALSE) {
			return OPERATOR_CANCELLED;
		}

		data.is_init = true;
		vert_cache = MEM_mallocN(sizeof(DMGradient_vertStore) * me->totvert, __func__);
	}

	data.ar = ar;
	data.scene = scene;
	data.me = ob->data;
	data.sco_start = sco_start;
	data.sco_end   = sco_end;
	data.sco_line_div = 1.0f / len_v2v2(sco_start, sco_end);
	data.def_nr = ob->actdef - 1;
	data.use_select = (me->editflag & (ME_EDIT_PAINT_FACE_SEL | ME_EDIT_PAINT_VERT_SEL));
	data.vert_cache = vert_cache;
	data.type = RNA_enum_get(op->ptr, "type");

	{
		ToolSettings *ts = CTX_data_tool_settings(C);
		VPaint *wp = ts->wpaint;
		struct Brush *brush = BKE_paint_brush(&wp->paint);

		curvemapping_initialize(brush->curve);

		data.brush = brush;
		data.weightpaint = BKE_brush_weight_get(scene, brush);
	}

	ED_view3d_init_mats_rv3d(ob, ar->regiondata);

	dm->foreachMappedVert(dm, gradientVert__mapFunc, &data, DM_FOREACH_NOP);

	DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
	WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);

	if (is_interactive == false) {
		MEM_freeN(vert_cache);
	}

	return OPERATOR_FINISHED;
}

static int paint_weight_gradient_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
	int ret;

	if (wpaint_ensure_data(C, op) == FALSE) {
		return OPERATOR_CANCELLED;
	}

	ret = WM_gesture_straightline_invoke(C, op, event);
	if (ret & OPERATOR_RUNNING_MODAL) {
		struct ARegion *ar = CTX_wm_region(C);
		if (ar->regiontype == RGN_TYPE_WINDOW) {
			if (event->type == LEFTMOUSE && event->val == KM_PRESS) {  /* TODO, hardcoded, extend WM_gesture_straightline_ */
				wmGesture *gesture = op->customdata;
				gesture->mode = 1;
			}
		}
	}
	return ret;
}

void PAINT_OT_weight_gradient(wmOperatorType *ot)
{
	/* defined in DNA_space_types.h */
	static EnumPropertyItem gradient_types[] = {
		{WPAINT_GRADIENT_TYPE_LINEAR, "LINEAR", 0, "Linear", ""},
		{WPAINT_GRADIENT_TYPE_RADIAL, "RADIAL", 0, "Radial", ""},
		{0, NULL, 0, NULL, NULL}
	};

	PropertyRNA *prop;

	/* identifiers */
	ot->name = "Weight Gradient";
	ot->idname = "PAINT_OT_weight_gradient";
	ot->description = "Draw a line to apply a weight gradient to selected vertices";

	/* api callbacks */
	ot->invoke = paint_weight_gradient_invoke;
	ot->modal = paint_weight_gradient_modal;
	ot->exec = paint_weight_gradient_exec;
	ot->poll = weight_paint_poll;
	ot->cancel = WM_gesture_straightline_cancel;

	/* flags */
	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

	prop = RNA_def_enum(ot->srna, "type", gradient_types, 0, "Type", "");
	RNA_def_property_flag(prop, PROP_SKIP_SAVE);

	WM_operator_properties_gesture_straightline(ot, CURSOR_EDIT);
}