1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
|
/*
* ***** 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) 2004 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/mesh/editmesh_select.c
* \ingroup edmesh
*/
#include "MEM_guardedalloc.h"
#include "BLI_bitmap.h"
#include "BLI_listbase.h"
#include "BLI_linklist.h"
#include "BLI_linklist_stack.h"
#include "BLI_math.h"
#include "BLI_math_bits.h"
#include "BLI_rand.h"
#include "BLI_array.h"
#include "BKE_context.h"
#include "BKE_report.h"
#include "BKE_paint.h"
#include "BKE_editmesh.h"
#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"
#include "WM_api.h"
#include "WM_types.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"
#include "ED_mesh.h"
#include "ED_screen.h"
#include "ED_view3d.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "UI_resources.h"
#include "bmesh_tools.h"
#include "mesh_intern.h" /* own include */
/* use bmesh operator flags for a few operators */
#define BMO_ELE_TAG 1
/* ****************************** MIRROR **************** */
void EDBM_select_mirrored(BMEditMesh *em, bool extend,
int *r_totmirr, int *r_totfail)
{
Mesh *me = (Mesh *)em->ob->data;
BMesh *bm = em->bm;
BMIter iter;
int totmirr = 0;
int totfail = 0;
bool use_topology = (me && (me->editflag & ME_EDIT_MIRROR_TOPO));
*r_totmirr = *r_totfail = 0;
/* select -> tag */
if (bm->selectmode & SCE_SELECT_VERTEX) {
BMVert *v;
BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
BM_elem_flag_set(v, BM_ELEM_TAG, BM_elem_flag_test(v, BM_ELEM_SELECT));
}
}
else if (em->selectmode & SCE_SELECT_EDGE) {
BMEdge *e;
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
BM_elem_flag_set(e, BM_ELEM_TAG, BM_elem_flag_test(e, BM_ELEM_SELECT));
}
}
else {
BMFace *f;
BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
BM_elem_flag_set(f, BM_ELEM_TAG, BM_elem_flag_test(f, BM_ELEM_SELECT));
}
}
EDBM_verts_mirror_cache_begin(em, 0, true, true, use_topology);
if (!extend)
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
if (bm->selectmode & SCE_SELECT_VERTEX) {
BMVert *v;
BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
if (!BM_elem_flag_test(v, BM_ELEM_HIDDEN) && BM_elem_flag_test(v, BM_ELEM_TAG)) {
BMVert *v_mirr = EDBM_verts_mirror_get(em, v);
if (v_mirr && !BM_elem_flag_test(v_mirr, BM_ELEM_HIDDEN)) {
BM_vert_select_set(bm, v_mirr, true);
totmirr++;
}
else {
totfail++;
}
}
}
}
else if (em->selectmode & SCE_SELECT_EDGE) {
BMEdge *e;
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
if (!BM_elem_flag_test(e, BM_ELEM_HIDDEN) && BM_elem_flag_test(e, BM_ELEM_TAG)) {
BMEdge *e_mirr = EDBM_verts_mirror_get_edge(em, e);
if (e_mirr && !BM_elem_flag_test(e_mirr, BM_ELEM_HIDDEN)) {
BM_edge_select_set(bm, e_mirr, true);
totmirr++;
}
else {
totfail++;
}
}
}
}
else {
BMFace *f;
BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN) && BM_elem_flag_test(f, BM_ELEM_TAG)) {
BMFace *f_mirr = EDBM_verts_mirror_get_face(em, f);
if (f_mirr && !BM_elem_flag_test(f_mirr, BM_ELEM_HIDDEN)) {
BM_face_select_set(bm, f_mirr, true);
totmirr++;
}
else {
totfail++;
}
}
}
}
EDBM_verts_mirror_cache_end(em);
*r_totmirr = totmirr;
*r_totfail = totfail;
}
void EDBM_automerge(Scene *scene, Object *obedit, bool update, const char hflag)
{
bool ok;
BMEditMesh *em = BKE_editmesh_from_object(obedit);
ok = BMO_op_callf(em->bm, BMO_FLAG_DEFAULTS,
"automerge verts=%hv dist=%f",
hflag, scene->toolsettings->doublimit);
if (LIKELY(ok) && update) {
EDBM_update_generic(em, true, true);
}
}
/* ****************************** SELECTION ROUTINES **************** */
unsigned int bm_solidoffs = 0, bm_wireoffs = 0, bm_vertoffs = 0; /* set in drawobject.c ... for colorindices */
/* facilities for border select and circle select */
static BLI_bitmap *selbuf = NULL;
static BLI_bitmap *edbm_backbuf_alloc(const int size)
{
return BLI_BITMAP_NEW(size, "selbuf");
}
/* reads rect, and builds selection array for quick lookup */
/* returns if all is OK */
bool EDBM_backbuf_border_init(ViewContext *vc, short xmin, short ymin, short xmax, short ymax)
{
struct ImBuf *buf;
unsigned int *dr;
int a;
if (vc->obedit == NULL || !V3D_IS_ZBUF(vc->v3d)) {
return false;
}
buf = ED_view3d_backbuf_read(vc, xmin, ymin, xmax, ymax);
if ((buf == NULL) || (bm_vertoffs == 0)) {
return false;
}
dr = buf->rect;
/* build selection lookup */
selbuf = edbm_backbuf_alloc(bm_vertoffs + 1);
a = (xmax - xmin + 1) * (ymax - ymin + 1);
while (a--) {
if (*dr > 0 && *dr <= bm_vertoffs) {
BLI_BITMAP_ENABLE(selbuf, *dr);
}
dr++;
}
IMB_freeImBuf(buf);
return true;
}
bool EDBM_backbuf_check(unsigned int index)
{
/* odd logic, if selbuf is NULL we assume no zbuf-selection is enabled
* and just ignore the depth buffer, this is error prone since its possible
* code doesn't set the depth buffer by accident, but leave for now. - Campbell */
if (selbuf == NULL)
return true;
if (index > 0 && index <= bm_vertoffs)
return BLI_BITMAP_TEST_BOOL(selbuf, index);
return false;
}
void EDBM_backbuf_free(void)
{
if (selbuf) MEM_freeN(selbuf);
selbuf = NULL;
}
struct LassoMaskData {
unsigned int *px;
int width;
};
static void edbm_mask_lasso_px_cb(int x, int y, void *user_data)
{
struct LassoMaskData *data = user_data;
data->px[(y * data->width) + x] = true;
}
/* mcords is a polygon mask
* - grab backbuffer,
* - draw with black in backbuffer,
* - grab again and compare
* returns 'OK'
*/
bool EDBM_backbuf_border_mask_init(ViewContext *vc, const int mcords[][2], short tot, short xmin, short ymin, short xmax, short ymax)
{
unsigned int *dr, *dr_mask, *dr_mask_arr;
struct ImBuf *buf;
int a;
struct LassoMaskData lasso_mask_data;
/* method in use for face selecting too */
if (vc->obedit == NULL) {
if (!BKE_paint_select_elem_test(vc->obact)) {
return false;
}
}
else if (!V3D_IS_ZBUF(vc->v3d)) {
return false;
}
buf = ED_view3d_backbuf_read(vc, xmin, ymin, xmax, ymax);
if ((buf == NULL) || (bm_vertoffs == 0)) {
return false;
}
dr = buf->rect;
dr_mask = dr_mask_arr = MEM_callocN(sizeof(*dr_mask) * buf->x * buf->y, __func__);
lasso_mask_data.px = dr_mask;
lasso_mask_data.width = (xmax - xmin) + 1;
fill_poly_v2i_n(
xmin, ymin, xmax + 1, ymax + 1,
mcords, tot,
edbm_mask_lasso_px_cb, &lasso_mask_data);
/* build selection lookup */
selbuf = edbm_backbuf_alloc(bm_vertoffs + 1);
a = (xmax - xmin + 1) * (ymax - ymin + 1);
while (a--) {
if (*dr > 0 && *dr <= bm_vertoffs && *dr_mask == true) {
BLI_BITMAP_ENABLE(selbuf, *dr);
}
dr++; dr_mask++;
}
IMB_freeImBuf(buf);
MEM_freeN(dr_mask_arr);
return true;
}
/* circle shaped sample area */
bool EDBM_backbuf_circle_init(ViewContext *vc, short xs, short ys, short rads)
{
struct ImBuf *buf;
unsigned int *dr;
short xmin, ymin, xmax, ymax, xc, yc;
int radsq;
/* method in use for face selecting too */
if (vc->obedit == NULL) {
if (!BKE_paint_select_elem_test(vc->obact)) {
return false;
}
}
else if (!V3D_IS_ZBUF(vc->v3d)) {
return false;
}
xmin = xs - rads; xmax = xs + rads;
ymin = ys - rads; ymax = ys + rads;
buf = ED_view3d_backbuf_read(vc, xmin, ymin, xmax, ymax);
if ((buf == NULL) || (bm_vertoffs == 0)) {
return false;
}
dr = buf->rect;
/* build selection lookup */
selbuf = edbm_backbuf_alloc(bm_vertoffs + 1);
radsq = rads * rads;
for (yc = -rads; yc <= rads; yc++) {
for (xc = -rads; xc <= rads; xc++, dr++) {
if (xc * xc + yc * yc < radsq) {
if (*dr > 0 && *dr <= bm_vertoffs) {
BLI_BITMAP_ENABLE(selbuf, *dr);
}
}
}
}
IMB_freeImBuf(buf);
return true;
}
/* -------------------------------------------------------------------- */
/** \name Find Nearest Vert/Edge/Face
*
* \note Screen-space manhatten distances are used here,
* since its faster and good enough for the purpose of selection.
*
* \note \a dist_bias is used so we can bias against selected items.
* when choosing between elements of a single type, but return the real distance
* to avoid the bias interfering with distance comparisons when mixing types.
* \{ */
#define FIND_NEAR_SELECT_BIAS 5
#define FIND_NEAR_CYCLE_THRESHOLD_MIN 3
struct NearestVertUserData_Hit {
float dist;
float dist_bias;
int index;
BMVert *vert;
};
struct NearestVertUserData {
float mval_fl[2];
bool use_select_bias;
bool use_cycle;
int cycle_index_prev;
struct NearestVertUserData_Hit hit;
struct NearestVertUserData_Hit hit_cycle;
};
static void findnearestvert__doClosest(void *userData, BMVert *eve, const float screen_co[2], int index)
{
struct NearestVertUserData *data = userData;
float dist_test, dist_test_bias;
dist_test = dist_test_bias = len_manhattan_v2v2(data->mval_fl, screen_co);
if (data->use_select_bias && BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
dist_test_bias += FIND_NEAR_SELECT_BIAS;
}
if (dist_test_bias < data->hit.dist_bias) {
data->hit.dist_bias = dist_test_bias;
data->hit.dist = dist_test;
data->hit.index = index;
data->hit.vert = eve;
}
if (data->use_cycle) {
if ((data->hit_cycle.vert == NULL) &&
(index > data->cycle_index_prev) &&
(dist_test_bias < FIND_NEAR_CYCLE_THRESHOLD_MIN))
{
data->hit_cycle.dist_bias = dist_test_bias;
data->hit_cycle.dist = dist_test;
data->hit_cycle.index = index;
data->hit_cycle.vert = eve;
}
}
}
/**
* Nearest vertex under the cursor.
*
* \param r_dist (in/out), minimal distance to the nearest and at the end, actual distance
* \param use_select_bias
* - When true, selected vertice are given a 5 pixel bias to make them further than unselect verts.
* - When false, unselected vertice are given the bias.
* \param use_cycle Cycle over elements within #FIND_NEAR_CYCLE_THRESHOLD_MIN in order of index.
*/
BMVert *EDBM_vert_find_nearest_ex(
ViewContext *vc, float *r_dist,
const bool use_select_bias, bool use_cycle)
{
BMesh *bm = vc->em->bm;
if (V3D_IS_ZBUF(vc->v3d)) {
const int dist_px = ED_view3d_backbuf_sample_size_clamp(vc->ar, *r_dist);
float dist_test;
unsigned int index;
BMVert *eve;
index = ED_view3d_backbuf_sample_rect(
vc, vc->mval, dist_px, bm_wireoffs, 0xFFFFFF, &dist_test);
eve = index ? BM_vert_at_index_find_or_table(bm, index - 1) : NULL;
if (eve) {
if (dist_test < *r_dist) {
*r_dist = dist_test;
return eve;
}
}
return NULL;
}
else {
struct NearestVertUserData data = {{0}};
const struct NearestVertUserData_Hit *hit;
const eV3DProjTest clip_flag = V3D_PROJ_TEST_CLIP_DEFAULT;
static int prev_select_index = 0;
static const BMVert *prev_select_elem = NULL;
if ((use_cycle == false) ||
(prev_select_elem && (prev_select_elem != BM_vert_at_index_find_or_table(bm, prev_select_index))))
{
prev_select_index = 0;
prev_select_elem = NULL;
}
data.mval_fl[0] = vc->mval[0];
data.mval_fl[1] = vc->mval[1];
data.use_select_bias = use_select_bias;
data.use_cycle = use_cycle;
data.hit.dist = data.hit_cycle.dist = \
data.hit.dist_bias = data.hit_cycle.dist_bias = *r_dist;
data.cycle_index_prev = prev_select_index;
ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
mesh_foreachScreenVert(vc, findnearestvert__doClosest, &data, clip_flag);
hit = (data.use_cycle && data.hit_cycle.vert) ? &data.hit_cycle : &data.hit;
*r_dist = hit->dist;
prev_select_elem = hit->vert;
prev_select_index = hit->index;
return hit->vert;
}
}
BMVert *EDBM_vert_find_nearest(ViewContext *vc, float *r_dist)
{
return EDBM_vert_find_nearest_ex(vc, r_dist, false, false);
}
/* find the distance to the edge we already have */
struct NearestEdgeUserData_ZBuf {
float mval_fl[2];
float dist;
const BMEdge *edge_test;
};
static void find_nearest_edge_center__doZBuf(
void *userData, BMEdge *eed,
const float screen_co_a[2], const float screen_co_b[2],
int UNUSED(index))
{
struct NearestEdgeUserData_ZBuf *data = userData;
if (eed == data->edge_test) {
float dist_test;
float screen_co_mid[2];
mid_v2_v2v2(screen_co_mid, screen_co_a, screen_co_b);
dist_test = len_manhattan_v2v2(data->mval_fl, screen_co_mid);
if (dist_test < data->dist) {
data->dist = dist_test;
}
}
}
struct NearestEdgeUserData_Hit {
float dist;
float dist_bias;
int index;
BMEdge *edge;
/* edges only, un-biased manhatten distance to which ever edge we pick
* (not used for choosing) */
float dist_center;
};
struct NearestEdgeUserData {
ViewContext vc;
float mval_fl[2];
bool use_select_bias;
bool use_cycle;
int cycle_index_prev;
struct NearestEdgeUserData_Hit hit;
struct NearestEdgeUserData_Hit hit_cycle;
};
/* note; uses v3d, so needs active 3d window */
static void find_nearest_edge__doClosest(
void *userData, BMEdge *eed,
const float screen_co_a[2], const float screen_co_b[2],
int index)
{
struct NearestEdgeUserData *data = userData;
float dist_test, dist_test_bias;
float fac = line_point_factor_v2(data->mval_fl, screen_co_a, screen_co_b);
float screen_co[2];
if (fac <= 0.0f) {
fac = 0.0f;
copy_v2_v2(screen_co, screen_co_a);
}
else if (fac >= 1.0f) {
fac = 1.0f;
copy_v2_v2(screen_co, screen_co_b);
}
else {
interp_v2_v2v2(screen_co, screen_co_a, screen_co_b, fac);
}
dist_test = dist_test_bias = len_manhattan_v2v2(data->mval_fl, screen_co);
if (data->use_select_bias && BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
dist_test_bias += FIND_NEAR_SELECT_BIAS;
}
if (data->vc.rv3d->rflag & RV3D_CLIPPING) {
float vec[3];
interp_v3_v3v3(vec, eed->v1->co, eed->v2->co, fac);
if (ED_view3d_clipping_test(data->vc.rv3d, vec, true)) {
return;
}
}
if (dist_test_bias < data->hit.dist_bias) {
float screen_co_mid[2];
data->hit.dist_bias = dist_test_bias;
data->hit.dist = dist_test;
data->hit.index = index;
data->hit.edge = eed;
mid_v2_v2v2(screen_co_mid, screen_co_a, screen_co_b);
data->hit.dist_center = len_manhattan_v2v2(data->mval_fl, screen_co_mid);
}
if (data->use_cycle) {
if ((data->hit_cycle.edge == NULL) &&
(index > data->cycle_index_prev) &&
(dist_test_bias < FIND_NEAR_CYCLE_THRESHOLD_MIN))
{
float screen_co_mid[2];
data->hit_cycle.dist_bias = dist_test_bias;
data->hit_cycle.dist = dist_test;
data->hit_cycle.index = index;
data->hit_cycle.edge = eed;
mid_v2_v2v2(screen_co_mid, screen_co_a, screen_co_b);
data->hit_cycle.dist_center = len_manhattan_v2v2(data->mval_fl, screen_co_mid);
}
}
}
BMEdge *EDBM_edge_find_nearest_ex(
ViewContext *vc, float *r_dist,
float *r_dist_center,
const bool use_select_bias, const bool use_cycle,
BMEdge **r_eed_zbuf)
{
BMesh *bm = vc->em->bm;
if (V3D_IS_ZBUF(vc->v3d)) {
const int dist_px = ED_view3d_backbuf_sample_size_clamp(vc->ar, *r_dist);
float dist_test = 0.0f;
unsigned int index;
BMEdge *eed;
ED_view3d_backbuf_validate(vc);
index = ED_view3d_backbuf_sample_rect(vc, vc->mval, dist_px, bm_solidoffs, bm_wireoffs, &dist_test);
eed = index ? BM_edge_at_index_find_or_table(bm, index - 1) : NULL;
if (r_eed_zbuf) {
*r_eed_zbuf = eed;
}
/* exception for faces (verts don't need this) */
if (r_dist_center && eed) {
struct NearestEdgeUserData_ZBuf data;
data.mval_fl[0] = vc->mval[0];
data.mval_fl[1] = vc->mval[1];
data.dist = FLT_MAX;
data.edge_test = eed;
ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
mesh_foreachScreenEdge(vc, find_nearest_edge_center__doZBuf, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
*r_dist_center = data.dist;
}
/* end exception */
if (eed) {
if (dist_test < *r_dist) {
*r_dist = dist_test;
return eed;
}
}
return NULL;
}
else {
struct NearestEdgeUserData data = {{0}};
const struct NearestEdgeUserData_Hit *hit;
/* interpolate along the edge before doing a clipping plane test */
const eV3DProjTest clip_flag = V3D_PROJ_TEST_CLIP_DEFAULT & ~V3D_PROJ_TEST_CLIP_BB;
static int prev_select_index = 0;
static const BMEdge *prev_select_elem = NULL;
if ((use_cycle == false) ||
(prev_select_elem && (prev_select_elem != BM_edge_at_index_find_or_table(bm, prev_select_index))))
{
prev_select_index = 0;
prev_select_elem = NULL;
}
data.vc = *vc;
data.mval_fl[0] = vc->mval[0];
data.mval_fl[1] = vc->mval[1];
data.use_select_bias = use_select_bias;
data.use_cycle = use_cycle;
data.hit.dist = data.hit_cycle.dist = \
data.hit.dist_bias = data.hit_cycle.dist_bias = *r_dist;
data.cycle_index_prev = prev_select_index;
ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
mesh_foreachScreenEdge(vc, find_nearest_edge__doClosest, &data, clip_flag);
hit = (data.use_cycle && data.hit_cycle.edge) ? &data.hit_cycle : &data.hit;
*r_dist = hit->dist;
if (r_dist_center) {
*r_dist_center = hit->dist_center;
}
prev_select_elem = hit->edge;
prev_select_index = hit->index;
return hit->edge;
}
}
BMEdge *EDBM_edge_find_nearest(
ViewContext *vc, float *r_dist)
{
return EDBM_edge_find_nearest_ex(vc, r_dist, NULL, false, false, NULL);
}
/* find the distance to the face we already have */
struct NearestFaceUserData_ZBuf {
float mval_fl[2];
float dist;
const BMFace *face_test;
};
static void find_nearest_face_center__doZBuf(void *userData, BMFace *efa, const float screen_co[2], int UNUSED(index))
{
struct NearestFaceUserData_ZBuf *data = userData;
if (efa == data->face_test) {
const float dist_test = len_manhattan_v2v2(data->mval_fl, screen_co);
if (dist_test < data->dist) {
data->dist = dist_test;
}
}
}
struct NearestFaceUserData_Hit {
float dist;
float dist_bias;
int index;
BMFace *face;
};
struct NearestFaceUserData {
float mval_fl[2];
bool use_select_bias;
bool use_cycle;
int cycle_index_prev;
struct NearestFaceUserData_Hit hit;
struct NearestFaceUserData_Hit hit_cycle;
};
static void findnearestface__doClosest(void *userData, BMFace *efa, const float screen_co[2], int index)
{
struct NearestFaceUserData *data = userData;
float dist_test, dist_test_bias;
dist_test = dist_test_bias = len_manhattan_v2v2(data->mval_fl, screen_co);
if (data->use_select_bias && BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
dist_test_bias += FIND_NEAR_SELECT_BIAS;
}
if (dist_test_bias < data->hit.dist_bias) {
data->hit.dist_bias = dist_test_bias;
data->hit.dist = dist_test;
data->hit.index = index;
data->hit.face = efa;
}
if (data->use_cycle) {
if ((data->hit_cycle.face == NULL) &&
(index > data->cycle_index_prev) &&
(dist_test_bias < FIND_NEAR_CYCLE_THRESHOLD_MIN))
{
data->hit_cycle.dist_bias = dist_test_bias;
data->hit_cycle.dist = dist_test;
data->hit_cycle.index = index;
data->hit_cycle.face = efa;
}
}
}
BMFace *EDBM_face_find_nearest_ex(
ViewContext *vc, float *r_dist,
float *r_dist_center,
const bool use_select_bias, const bool use_cycle,
BMFace **r_efa_zbuf)
{
BMesh *bm = vc->em->bm;
if (V3D_IS_ZBUF(vc->v3d)) {
float dist_test = 0.0f;
unsigned int index;
BMFace *efa;
ED_view3d_backbuf_validate(vc);
index = ED_view3d_backbuf_sample(vc, vc->mval[0], vc->mval[1]);
efa = index ? BM_face_at_index_find_or_table(bm, index - 1) : NULL;
if (r_efa_zbuf) {
*r_efa_zbuf = efa;
}
/* exception for faces (verts don't need this) */
if (r_dist_center && efa) {
struct NearestFaceUserData_ZBuf data;
data.mval_fl[0] = vc->mval[0];
data.mval_fl[1] = vc->mval[1];
data.dist = FLT_MAX;
data.face_test = efa;
ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
mesh_foreachScreenFace(vc, find_nearest_face_center__doZBuf, &data, V3D_PROJ_TEST_CLIP_DEFAULT);
*r_dist_center = data.dist;
}
/* end exception */
if (efa) {
if (dist_test < *r_dist) {
*r_dist = dist_test;
return efa;
}
}
return NULL;
}
else {
struct NearestFaceUserData data = {{0}};
const struct NearestFaceUserData_Hit *hit;
const eV3DProjTest clip_flag = V3D_PROJ_TEST_CLIP_DEFAULT;
static int prev_select_index = 0;
static const BMFace *prev_select_elem = NULL;
if ((use_cycle == false) ||
(prev_select_elem && (prev_select_elem != BM_face_at_index_find_or_table(bm, prev_select_index))))
{
prev_select_index = 0;
prev_select_elem = NULL;
}
data.mval_fl[0] = vc->mval[0];
data.mval_fl[1] = vc->mval[1];
data.use_select_bias = use_select_bias;
data.use_cycle = use_cycle;
data.hit.dist = data.hit_cycle.dist = \
data.hit.dist_bias = data.hit_cycle.dist_bias = *r_dist;
data.cycle_index_prev = prev_select_index;
ED_view3d_init_mats_rv3d(vc->obedit, vc->rv3d);
mesh_foreachScreenFace(vc, findnearestface__doClosest, &data, clip_flag);
hit = (data.use_cycle && data.hit_cycle.face) ? &data.hit_cycle : &data.hit;
*r_dist = hit->dist;
if (r_dist_center) {
*r_dist_center = hit->dist;
}
prev_select_elem = hit->face;
prev_select_index = hit->index;
return hit->face;
}
}
BMFace *EDBM_face_find_nearest(ViewContext *vc, float *r_dist)
{
return EDBM_face_find_nearest_ex(vc, r_dist, NULL, false, false, NULL);
}
#undef FIND_NEAR_SELECT_BIAS
#undef FIND_NEAR_CYCLE_THRESHOLD_MIN
/* best distance based on screen coords.
* use em->selectmode to define how to use
* selected vertices and edges get disadvantage
* return 1 if found one
*/
static int unified_findnearest(ViewContext *vc, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
{
BMEditMesh *em = vc->em;
static short mval_prev[2] = {-1, -1};
/* only cycle while the mouse remains still */
const bool use_cycle = ((mval_prev[0] == vc->mval[0]) && (mval_prev[1] == vc->mval[1]));
const float dist_init = ED_view3d_select_dist_px();
/* since edges select lines, we give dots advantage of ~20 pix */
const float dist_margin = (dist_init / 2);
float dist = dist_init;
BMFace *efa_zbuf = NULL;
BMEdge *eed_zbuf = NULL;
BMVert *eve = NULL;
BMEdge *eed = NULL;
BMFace *efa = NULL;
/* no afterqueue (yet), so we check it now, otherwise the em_xxxofs indices are bad */
ED_view3d_backbuf_validate(vc);
if ((dist > 0.0f) && em->selectmode & SCE_SELECT_FACE) {
float dist_center = 0.0f;
float *dist_center_p = (em->selectmode & (SCE_SELECT_EDGE | SCE_SELECT_VERTEX)) ? &dist_center : NULL;
efa = EDBM_face_find_nearest_ex(vc, &dist, dist_center_p, true, use_cycle, &efa_zbuf);
if (efa && dist_center_p) {
dist = min_ff(dist_margin, dist_center);
}
}
if ((dist > 0.0f) && (em->selectmode & SCE_SELECT_EDGE)) {
float dist_center = 0.0f;
float *dist_center_p = (em->selectmode & SCE_SELECT_VERTEX) ? &dist_center : NULL;
eed = EDBM_edge_find_nearest_ex(vc, &dist, dist_center_p, true, use_cycle, &eed_zbuf);
if (eed && dist_center_p) {
dist = min_ff(dist_margin, dist_center);
}
}
if ((dist > 0.0f) && em->selectmode & SCE_SELECT_VERTEX) {
eve = EDBM_vert_find_nearest_ex(vc, &dist, true, use_cycle);
}
/* return only one of 3 pointers, for frontbuffer redraws */
if (eve) {
efa = NULL; eed = NULL;
}
else if (eed) {
efa = NULL;
}
/* there may be a face under the cursor, who's center if too far away
* use this if all else fails, it makes sense to select this */
if ((eve || eed || efa) == 0) {
if (eed_zbuf) {
eed = eed_zbuf;
}
else if (efa_zbuf) {
efa = efa_zbuf;
}
}
mval_prev[0] = vc->mval[0];
mval_prev[1] = vc->mval[1];
*r_eve = eve;
*r_eed = eed;
*r_efa = efa;
return (eve || eed || efa);
}
/** \} */
/* **************** SIMILAR "group" SELECTS. FACE, EDGE AND VERTEX ************** */
static EnumPropertyItem prop_similar_compare_types[] = {
{SIM_CMP_EQ, "EQUAL", 0, "Equal", ""},
{SIM_CMP_GT, "GREATER", 0, "Greater", ""},
{SIM_CMP_LT, "LESS", 0, "Less", ""},
{0, NULL, 0, NULL, NULL}
};
static EnumPropertyItem prop_similar_types[] = {
{SIMVERT_NORMAL, "NORMAL", 0, "Normal", ""},
{SIMVERT_FACE, "FACE", 0, "Amount of Adjacent Faces", ""},
{SIMVERT_VGROUP, "VGROUP", 0, "Vertex Groups", ""},
{SIMVERT_EDGE, "EDGE", 0, "Amount of connecting edges", ""},
{SIMEDGE_LENGTH, "LENGTH", 0, "Length", ""},
{SIMEDGE_DIR, "DIR", 0, "Direction", ""},
{SIMEDGE_FACE, "FACE", 0, "Amount of Faces Around an Edge", ""},
{SIMEDGE_FACE_ANGLE, "FACE_ANGLE", 0, "Face Angles", ""},
{SIMEDGE_CREASE, "CREASE", 0, "Crease", ""},
{SIMEDGE_BEVEL, "BEVEL", 0, "Bevel", ""},
{SIMEDGE_SEAM, "SEAM", 0, "Seam", ""},
{SIMEDGE_SHARP, "SHARP", 0, "Sharpness", ""},
#ifdef WITH_FREESTYLE
{SIMEDGE_FREESTYLE, "FREESTYLE_EDGE", 0, "Freestyle Edge Marks", ""},
#endif
{SIMFACE_MATERIAL, "MATERIAL", 0, "Material", ""},
{SIMFACE_IMAGE, "IMAGE", 0, "Image", ""},
{SIMFACE_AREA, "AREA", 0, "Area", ""},
{SIMFACE_SIDES, "SIDES", 0, "Polygon Sides", ""},
{SIMFACE_PERIMETER, "PERIMETER", 0, "Perimeter", ""},
{SIMFACE_NORMAL, "NORMAL", 0, "Normal", ""},
{SIMFACE_COPLANAR, "COPLANAR", 0, "Co-planar", ""},
{SIMFACE_SMOOTH, "SMOOTH", 0, "Flat/Smooth", ""},
#ifdef WITH_FREESTYLE
{SIMFACE_FREESTYLE, "FREESTYLE_FACE", 0, "Freestyle Face Marks", ""},
#endif
{0, NULL, 0, NULL, NULL}
};
/* selects new faces/edges/verts based on the existing selection */
static int similar_face_select_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(ob);
BMOperator bmop;
/* get the type from RNA */
const int type = RNA_enum_get(op->ptr, "type");
const float thresh = RNA_float_get(op->ptr, "threshold");
const int compare = RNA_enum_get(op->ptr, "compare");
/* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
EDBM_op_init(em, &bmop, op,
"similar_faces faces=%hf type=%i thresh=%f compare=%i",
BM_ELEM_SELECT, type, thresh, compare);
/* execute the operator */
BMO_op_exec(em->bm, &bmop);
/* clear the existing selection */
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
/* select the output */
BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "faces.out", BM_FACE, BM_ELEM_SELECT, true);
/* finish the operator */
if (!EDBM_op_finish(em, &bmop, op, true)) {
return OPERATOR_CANCELLED;
}
EDBM_update_generic(em, false, false);
return OPERATOR_FINISHED;
}
/* ***************************************************** */
/* EDGE GROUP */
/* wrap the above function but do selection flushing edge to face */
static int similar_edge_select_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(ob);
BMOperator bmop;
/* get the type from RNA */
const int type = RNA_enum_get(op->ptr, "type");
const float thresh = RNA_float_get(op->ptr, "threshold");
const int compare = RNA_enum_get(op->ptr, "compare");
/* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
EDBM_op_init(em, &bmop, op,
"similar_edges edges=%he type=%i thresh=%f compare=%i",
BM_ELEM_SELECT, type, thresh, compare);
/* execute the operator */
BMO_op_exec(em->bm, &bmop);
/* clear the existing selection */
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
/* select the output */
BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "edges.out", BM_EDGE, BM_ELEM_SELECT, true);
EDBM_selectmode_flush(em);
/* finish the operator */
if (!EDBM_op_finish(em, &bmop, op, true)) {
return OPERATOR_CANCELLED;
}
EDBM_update_generic(em, false, false);
return OPERATOR_FINISHED;
}
/* ********************************* */
/*
* VERT GROUP
* mode 1: same normal
* mode 2: same number of face users
* mode 3: same vertex groups
*/
static int similar_vert_select_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(ob);
BMOperator bmop;
/* get the type from RNA */
const int type = RNA_enum_get(op->ptr, "type");
const float thresh = RNA_float_get(op->ptr, "threshold");
const int compare = RNA_enum_get(op->ptr, "compare");
/* initialize the bmop using EDBM api, which does various ui error reporting and other stuff */
EDBM_op_init(em, &bmop, op,
"similar_verts verts=%hv type=%i thresh=%f compare=%i",
BM_ELEM_SELECT, type, thresh, compare);
/* execute the operator */
BMO_op_exec(em->bm, &bmop);
/* clear the existing selection */
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
/* select the output */
BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "verts.out", BM_VERT, BM_ELEM_SELECT, true);
/* finish the operator */
if (!EDBM_op_finish(em, &bmop, op, true)) {
return OPERATOR_CANCELLED;
}
EDBM_selectmode_flush(em);
EDBM_update_generic(em, false, false);
return OPERATOR_FINISHED;
}
static int edbm_select_similar_exec(bContext *C, wmOperator *op)
{
ToolSettings *ts = CTX_data_tool_settings(C);
PropertyRNA *prop = RNA_struct_find_property(op->ptr, "threshold");
const int type = RNA_enum_get(op->ptr, "type");
if (!RNA_property_is_set(op->ptr, prop)) {
RNA_property_float_set(op->ptr, prop, ts->select_thresh);
}
else {
ts->select_thresh = RNA_property_float_get(op->ptr, prop);
}
if (type < 100) return similar_vert_select_exec(C, op);
else if (type < 200) return similar_edge_select_exec(C, op);
else return similar_face_select_exec(C, op);
}
static EnumPropertyItem *select_similar_type_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop),
bool *r_free)
{
Object *obedit;
if (!C) /* needed for docs and i18n tools */
return prop_similar_types;
obedit = CTX_data_edit_object(C);
if (obedit && obedit->type == OB_MESH) {
EnumPropertyItem *item = NULL;
int a, totitem = 0;
BMEditMesh *em = BKE_editmesh_from_object(obedit);
if (em->selectmode & SCE_SELECT_VERTEX) {
for (a = SIMVERT_NORMAL; a < SIMEDGE_LENGTH; a++) {
RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
}
}
else if (em->selectmode & SCE_SELECT_EDGE) {
for (a = SIMEDGE_LENGTH; a < SIMFACE_MATERIAL; a++) {
RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
}
}
else if (em->selectmode & SCE_SELECT_FACE) {
#ifdef WITH_FREESTYLE
const int a_end = SIMFACE_FREESTYLE;
#else
const int a_end = SIMFACE_SMOOTH;
#endif
for (a = SIMFACE_MATERIAL; a <= a_end; a++) {
RNA_enum_items_add_value(&item, &totitem, prop_similar_types, a);
}
}
RNA_enum_item_end(&item, &totitem);
*r_free = true;
return item;
}
return prop_similar_types;
}
void MESH_OT_select_similar(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Select Similar";
ot->idname = "MESH_OT_select_similar";
ot->description = "Select similar vertices, edges or faces by property types";
/* api callbacks */
ot->invoke = WM_menu_invoke;
ot->exec = edbm_select_similar_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
prop = ot->prop = RNA_def_enum(ot->srna, "type", prop_similar_types, SIMVERT_NORMAL, "Type", "");
RNA_def_enum_funcs(prop, select_similar_type_itemf);
RNA_def_enum(ot->srna, "compare", prop_similar_compare_types, SIM_CMP_EQ, "Compare", "");
RNA_def_float(ot->srna, "threshold", 0.0f, 0.0f, 1.0f, "Threshold", "", 0.0f, 1.0f);
}
/* -------------------------------------------------------------------- */
/* Select Similar Regions */
static int edbm_select_similar_region_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMesh *bm = em->bm;
bool changed = false;
/* group vars */
int *groups_array;
int (*group_index)[2];
int group_tot;
int i;
if (bm->totfacesel < 2) {
BKE_report(op->reports, RPT_ERROR, "No face regions selected");
return OPERATOR_CANCELLED;
}
groups_array = MEM_mallocN(sizeof(*groups_array) * bm->totfacesel, __func__);
group_tot = BM_mesh_calc_face_groups(bm, groups_array, &group_index,
NULL, NULL,
BM_ELEM_SELECT, BM_VERT);
BM_mesh_elem_table_ensure(bm, BM_FACE);
for (i = 0; i < group_tot; i++) {
ListBase faces_regions;
int tot;
const int fg_sta = group_index[i][0];
const int fg_len = group_index[i][1];
int j;
BMFace **fg = MEM_mallocN(sizeof(*fg) * fg_len, __func__);
for (j = 0; j < fg_len; j++) {
fg[j] = BM_face_at_index(bm, groups_array[fg_sta + j]);
}
tot = BM_mesh_region_match(bm, fg, fg_len, &faces_regions);
MEM_freeN(fg);
if (tot) {
LinkData *link;
while ((link = BLI_pophead(&faces_regions))) {
BMFace *f, **faces = link->data;
while ((f = *(faces++))) {
BM_face_select_set(bm, f, true);
}
MEM_freeN(link->data);
MEM_freeN(link);
changed = true;
}
}
}
MEM_freeN(groups_array);
MEM_freeN(group_index);
if (changed) {
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
}
else {
BKE_report(op->reports, RPT_WARNING, "No matching face regions found");
}
return OPERATOR_FINISHED;
}
void MESH_OT_select_similar_region(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Similar Regions";
ot->idname = "MESH_OT_select_similar_region";
ot->description = "Select similar face regions to the current selection";
/* api callbacks */
ot->exec = edbm_select_similar_region_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* **************** Mode Select *************** */
static int edbm_select_mode_exec(bContext *C, wmOperator *op)
{
const int type = RNA_enum_get(op->ptr, "type");
const int action = RNA_enum_get(op->ptr, "action");
const bool use_extend = RNA_boolean_get(op->ptr, "use_extend");
const bool use_expand = RNA_boolean_get(op->ptr, "use_expand");
if (EDBM_selectmode_toggle(C, type, action, use_extend, use_expand)) {
return OPERATOR_FINISHED;
}
else {
return OPERATOR_CANCELLED;
}
}
static int edbm_select_mode_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
/* detecting these options based on shift/ctrl here is weak, but it's done
* to make this work when clicking buttons or menus */
if (!RNA_struct_property_is_set(op->ptr, "use_extend"))
RNA_boolean_set(op->ptr, "use_extend", event->shift);
if (!RNA_struct_property_is_set(op->ptr, "use_expand"))
RNA_boolean_set(op->ptr, "use_expand", event->ctrl);
return edbm_select_mode_exec(C, op);
}
void MESH_OT_select_mode(wmOperatorType *ot)
{
PropertyRNA *prop;
static EnumPropertyItem elem_items[] = {
{SCE_SELECT_VERTEX, "VERT", ICON_VERTEXSEL, "Vertices", ""},
{SCE_SELECT_EDGE, "EDGE", ICON_EDGESEL, "Edges", ""},
{SCE_SELECT_FACE, "FACE", ICON_FACESEL, "Faces", ""},
{0, NULL, 0, NULL, NULL},
};
static EnumPropertyItem actions_items[] = {
{0, "DISABLE", 0, "Disable", "Disable selected markers"},
{1, "ENABLE", 0, "Enable", "Enable selected markers"},
{2, "TOGGLE", 0, "Toggle", "Toggle disabled flag for selected markers"},
{0, NULL, 0, NULL, NULL}
};
/* identifiers */
ot->name = "Select Mode";
ot->idname = "MESH_OT_select_mode";
ot->description = "Change selection mode";
/* api callbacks */
ot->invoke = edbm_select_mode_invoke;
ot->exec = edbm_select_mode_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
prop = RNA_def_boolean(ot->srna, "use_extend", false, "Extend", "");
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
prop = RNA_def_boolean(ot->srna, "use_expand", false, "Expand", "");
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
ot->prop = prop = RNA_def_enum(ot->srna, "type", elem_items, 0, "Type", "");
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
RNA_def_enum(ot->srna, "action", actions_items, 2, "Action", "Selection action to execute");
}
/* ***************************************************** */
/* **************** LOOP SELECTS *************** */
static void walker_select_count(BMEditMesh *em, int walkercode, void *start, const bool select, const bool select_mix,
int *r_totsel, int *r_totunsel)
{
BMesh *bm = em->bm;
BMElem *ele;
BMWalker walker;
int tot[2] = {0, 0};
BMW_init(&walker, bm, walkercode,
BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
for (ele = BMW_begin(&walker, start); ele; ele = BMW_step(&walker)) {
tot[(BM_elem_flag_test_bool(ele, BM_ELEM_SELECT) != select)] += 1;
if (!select_mix && tot[0] && tot[1]) {
tot[0] = tot[1] = -1;
break;
}
}
*r_totsel = tot[0];
*r_totunsel = tot[1];
BMW_end(&walker);
}
static void walker_select(BMEditMesh *em, int walkercode, void *start, const bool select)
{
BMesh *bm = em->bm;
BMElem *ele;
BMWalker walker;
BMW_init(&walker, bm, walkercode,
BMW_MASK_NOP, BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
for (ele = BMW_begin(&walker, start); ele; ele = BMW_step(&walker)) {
if (!select) {
BM_select_history_remove(bm, ele);
}
BM_elem_select_set(bm, ele, select);
}
BMW_end(&walker);
}
static int edbm_loop_multiselect_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMEdge *eed;
BMEdge **edarray;
int edindex;
const bool is_ring = RNA_boolean_get(op->ptr, "ring");
BMIter iter;
int totedgesel = 0;
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
totedgesel++;
}
}
edarray = MEM_mallocN(sizeof(BMEdge *) * totedgesel, "edge array");
edindex = 0;
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
edarray[edindex] = eed;
edindex++;
}
}
if (is_ring) {
for (edindex = 0; edindex < totedgesel; edindex += 1) {
eed = edarray[edindex];
walker_select(em, BMW_EDGERING, eed, true);
}
EDBM_selectmode_flush(em);
}
else {
for (edindex = 0; edindex < totedgesel; edindex += 1) {
eed = edarray[edindex];
walker_select(em, BMW_EDGELOOP, eed, true);
}
EDBM_selectmode_flush(em);
}
MEM_freeN(edarray);
// if (EM_texFaceCheck())
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
void MESH_OT_loop_multi_select(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Multi Select Loops";
ot->idname = "MESH_OT_loop_multi_select";
ot->description = "Select a loop of connected edges by connection type";
/* api callbacks */
ot->exec = edbm_loop_multiselect_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_boolean(ot->srna, "ring", 0, "Ring", "");
}
/* ***************** MAIN MOUSE SELECTION ************** */
/* ***************** loop select (non modal) ************** */
static void mouse_mesh_loop_face(BMEditMesh *em, BMEdge *eed, bool select, bool select_clear)
{
if (select_clear) {
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
}
walker_select(em, BMW_FACELOOP, eed, select);
}
static void mouse_mesh_loop_edge_ring(BMEditMesh *em, BMEdge *eed, bool select, bool select_clear)
{
if (select_clear) {
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
}
walker_select(em, BMW_EDGERING, eed, select);
}
static void mouse_mesh_loop_edge(BMEditMesh *em, BMEdge *eed, bool select, bool select_clear, bool select_cycle)
{
bool edge_boundary = false;
/* cycle between BMW_EDGELOOP / BMW_EDGEBOUNDARY */
if (select_cycle && BM_edge_is_boundary(eed)) {
int tot[2];
/* if the loops selected toggle the boundaries */
walker_select_count(em, BMW_EDGELOOP, eed, select, false,
&tot[0], &tot[1]);
if (tot[select] == 0) {
edge_boundary = true;
/* if the boundaries selected, toggle back to the loop */
walker_select_count(em, BMW_EDGEBOUNDARY, eed, select, false,
&tot[0], &tot[1]);
if (tot[select] == 0) {
edge_boundary = false;
}
}
}
if (select_clear) {
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
}
if (edge_boundary) {
walker_select(em, BMW_EDGEBOUNDARY, eed, select);
}
else {
walker_select(em, BMW_EDGELOOP, eed, select);
}
}
static bool mouse_mesh_loop(bContext *C, const int mval[2], bool extend, bool deselect, bool toggle, bool ring)
{
ViewContext vc;
BMEditMesh *em;
BMEdge *eed;
bool select = true;
bool select_clear = false;
bool select_cycle = true;
float dist = ED_view3d_select_dist_px() * 0.6666f;
float mvalf[2];
em_setup_viewcontext(C, &vc);
mvalf[0] = (float)(vc.mval[0] = mval[0]);
mvalf[1] = (float)(vc.mval[1] = mval[1]);
em = vc.em;
/* no afterqueue (yet), so we check it now, otherwise the bm_xxxofs indices are bad */
ED_view3d_backbuf_validate(&vc);
eed = EDBM_edge_find_nearest_ex(&vc, &dist, NULL, true, true, NULL);
if (eed == NULL) {
return false;
}
if (extend == false && deselect == false && toggle == false) {
select_clear = true;
}
if (extend) {
select = true;
}
else if (deselect) {
select = false;
}
else if (select_clear || (BM_elem_flag_test(eed, BM_ELEM_SELECT) == 0)) {
select = true;
}
else if (toggle) {
select = false;
select_cycle = false;
}
if (em->selectmode & SCE_SELECT_FACE) {
mouse_mesh_loop_face(em, eed, select, select_clear);
}
else {
if (ring) {
mouse_mesh_loop_edge_ring(em, eed, select, select_clear);
}
else {
mouse_mesh_loop_edge(em, eed, select, select_clear, select_cycle);
}
}
EDBM_selectmode_flush(em);
/* sets as active, useful for other tools */
if (select) {
if (em->selectmode & SCE_SELECT_VERTEX) {
/* Find nearest vert from mouse
* (initialize to large values incase only one vertex can be projected) */
float v1_co[2], v2_co[2];
float length_1 = FLT_MAX;
float length_2 = FLT_MAX;
/* We can't be sure this has already been set... */
ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);
if (ED_view3d_project_float_object(vc.ar, eed->v1->co, v1_co, V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK) {
length_1 = len_squared_v2v2(mvalf, v1_co);
}
if (ED_view3d_project_float_object(vc.ar, eed->v2->co, v2_co, V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK) {
length_2 = len_squared_v2v2(mvalf, v2_co);
}
#if 0
printf("mouse to v1: %f\nmouse to v2: %f\n", len_squared_v2v2(mvalf, v1_co),
len_squared_v2v2(mvalf, v2_co));
#endif
BM_select_history_store(em->bm, (length_1 < length_2) ? eed->v1 : eed->v2);
}
else if (em->selectmode & SCE_SELECT_EDGE) {
BM_select_history_store(em->bm, eed);
}
else if (em->selectmode & SCE_SELECT_FACE) {
/* Select the face of eed which is the nearest of mouse. */
BMFace *f, *efa = NULL;
BMIter iterf;
float best_dist = FLT_MAX;
/* We can't be sure this has already been set... */
ED_view3d_init_mats_rv3d(vc.obedit, vc.rv3d);
BM_ITER_ELEM (f, &iterf, eed, BM_FACES_OF_EDGE) {
if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
float cent[3];
float co[2], tdist;
BM_face_calc_center_mean(f, cent);
if (ED_view3d_project_float_object(vc.ar, cent, co, V3D_PROJ_TEST_CLIP_NEAR) == V3D_PROJ_RET_OK) {
tdist = len_squared_v2v2(mvalf, co);
if (tdist < best_dist) {
/* printf("Best face: %p (%f)\n", f, tdist);*/
best_dist = tdist;
efa = f;
}
}
}
}
if (efa) {
BM_mesh_active_face_set(em->bm, efa);
BM_select_history_store(em->bm, efa);
}
}
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit);
return true;
}
static int edbm_select_loop_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
view3d_operator_needs_opengl(C);
if (mouse_mesh_loop(C, event->mval,
RNA_boolean_get(op->ptr, "extend"),
RNA_boolean_get(op->ptr, "deselect"),
RNA_boolean_get(op->ptr, "toggle"),
RNA_boolean_get(op->ptr, "ring")))
{
return OPERATOR_FINISHED;
}
else {
return OPERATOR_CANCELLED;
}
}
void MESH_OT_loop_select(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Loop Select";
ot->idname = "MESH_OT_loop_select";
ot->description = "Select a loop of connected edges";
/* api callbacks */
ot->invoke = edbm_select_loop_invoke;
ot->poll = ED_operator_editmesh_region_view3d;
/* flags */
ot->flag = OPTYPE_UNDO;
/* properties */
RNA_def_boolean(ot->srna, "extend", 0, "Extend Select", "Extend the selection");
RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Remove from the selection");
RNA_def_boolean(ot->srna, "toggle", 0, "Toggle Select", "Toggle the selection");
RNA_def_boolean(ot->srna, "ring", 0, "Select Ring", "Select ring");
}
void MESH_OT_edgering_select(wmOperatorType *ot)
{
/* description */
ot->name = "Edge Ring Select";
ot->idname = "MESH_OT_edgering_select";
ot->description = "Select an edge ring";
/* callbacks */
ot->invoke = edbm_select_loop_invoke;
ot->poll = ED_operator_editmesh_region_view3d;
/* flags */
ot->flag = OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Remove from the selection");
RNA_def_boolean(ot->srna, "toggle", 0, "Toggle Select", "Toggle the selection");
RNA_def_boolean(ot->srna, "ring", 1, "Select Ring", "Select ring");
}
/* ******************** (de)select all operator **************** */
static int edbm_select_all_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
const int action = RNA_enum_get(op->ptr, "action");
switch (action) {
case SEL_TOGGLE:
EDBM_select_toggle_all(em);
break;
case SEL_SELECT:
EDBM_flag_enable_all(em, BM_ELEM_SELECT);
break;
case SEL_DESELECT:
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
break;
case SEL_INVERT:
EDBM_select_swap(em);
EDBM_selectmode_flush(em);
break;
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
void MESH_OT_select_all(wmOperatorType *ot)
{
/* identifiers */
ot->name = "(De)select All";
ot->idname = "MESH_OT_select_all";
ot->description = "(De)select all vertices, edges or faces";
/* api callbacks */
ot->exec = edbm_select_all_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
WM_operator_properties_select_all(ot);
}
static int edbm_faces_select_interior_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
if (EDBM_select_interior_faces(em)) {
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
else {
return OPERATOR_CANCELLED;
}
}
void MESH_OT_select_interior_faces(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Interior Faces";
ot->idname = "MESH_OT_select_interior_faces";
ot->description = "Select faces where all edges have more than 2 face users";
/* api callbacks */
ot->exec = edbm_faces_select_interior_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ************************************************** */
/* here actual select happens */
/* gets called via generic mouse select operator */
bool EDBM_select_pick(bContext *C, const int mval[2], bool extend, bool deselect, bool toggle)
{
ViewContext vc;
BMVert *eve = NULL;
BMEdge *eed = NULL;
BMFace *efa = NULL;
/* setup view context for argument to callbacks */
em_setup_viewcontext(C, &vc);
vc.mval[0] = mval[0];
vc.mval[1] = mval[1];
if (unified_findnearest(&vc, &eve, &eed, &efa)) {
/* Deselect everything */
if (extend == false && deselect == false && toggle == false)
EDBM_flag_disable_all(vc.em, BM_ELEM_SELECT);
if (efa) {
if (extend) {
/* set the last selected face */
BM_mesh_active_face_set(vc.em->bm, efa);
/* Work-around: deselect first, so we can guarantee it will */
/* be active even if it was already selected */
BM_select_history_remove(vc.em->bm, efa);
BM_face_select_set(vc.em->bm, efa, false);
BM_select_history_store(vc.em->bm, efa);
BM_face_select_set(vc.em->bm, efa, true);
}
else if (deselect) {
BM_select_history_remove(vc.em->bm, efa);
BM_face_select_set(vc.em->bm, efa, false);
}
else {
/* set the last selected face */
BM_mesh_active_face_set(vc.em->bm, efa);
if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
BM_select_history_store(vc.em->bm, efa);
BM_face_select_set(vc.em->bm, efa, true);
}
else if (toggle) {
BM_select_history_remove(vc.em->bm, efa);
BM_face_select_set(vc.em->bm, efa, false);
}
}
}
else if (eed) {
if (extend) {
/* Work-around: deselect first, so we can guarantee it will */
/* be active even if it was already selected */
BM_select_history_remove(vc.em->bm, eed);
BM_edge_select_set(vc.em->bm, eed, false);
BM_select_history_store(vc.em->bm, eed);
BM_edge_select_set(vc.em->bm, eed, true);
}
else if (deselect) {
BM_select_history_remove(vc.em->bm, eed);
BM_edge_select_set(vc.em->bm, eed, false);
}
else {
if (!BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
BM_select_history_store(vc.em->bm, eed);
BM_edge_select_set(vc.em->bm, eed, true);
}
else if (toggle) {
BM_select_history_remove(vc.em->bm, eed);
BM_edge_select_set(vc.em->bm, eed, false);
}
}
}
else if (eve) {
if (extend) {
/* Work-around: deselect first, so we can guarantee it will */
/* be active even if it was already selected */
BM_select_history_remove(vc.em->bm, eve);
BM_vert_select_set(vc.em->bm, eve, false);
BM_select_history_store(vc.em->bm, eve);
BM_vert_select_set(vc.em->bm, eve, true);
}
else if (deselect) {
BM_select_history_remove(vc.em->bm, eve);
BM_vert_select_set(vc.em->bm, eve, false);
}
else {
if (!BM_elem_flag_test(eve, BM_ELEM_SELECT)) {
BM_select_history_store(vc.em->bm, eve);
BM_vert_select_set(vc.em->bm, eve, true);
}
else if (toggle) {
BM_select_history_remove(vc.em->bm, eve);
BM_vert_select_set(vc.em->bm, eve, false);
}
}
}
EDBM_selectmode_flush(vc.em);
/* change active material on object */
if (efa && efa->mat_nr != vc.obedit->actcol - 1) {
vc.obedit->actcol = efa->mat_nr + 1;
vc.em->mat_nr = efa->mat_nr;
WM_event_add_notifier(C, NC_MATERIAL | ND_SHADING_LINKS, NULL);
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, vc.obedit);
return true;
}
return false;
}
static void edbm_strip_selections(BMEditMesh *em)
{
BMEditSelection *ese, *nextese;
if (!(em->selectmode & SCE_SELECT_VERTEX)) {
ese = em->bm->selected.first;
while (ese) {
nextese = ese->next;
if (ese->htype == BM_VERT) BLI_freelinkN(&(em->bm->selected), ese);
ese = nextese;
}
}
if (!(em->selectmode & SCE_SELECT_EDGE)) {
ese = em->bm->selected.first;
while (ese) {
nextese = ese->next;
if (ese->htype == BM_EDGE) BLI_freelinkN(&(em->bm->selected), ese);
ese = nextese;
}
}
if (!(em->selectmode & SCE_SELECT_FACE)) {
ese = em->bm->selected.first;
while (ese) {
nextese = ese->next;
if (ese->htype == BM_FACE) BLI_freelinkN(&(em->bm->selected), ese);
ese = nextese;
}
}
}
/* when switching select mode, makes sure selection is consistent for editing */
/* also for paranoia checks to make sure edge or face mode works */
void EDBM_selectmode_set(BMEditMesh *em)
{
BMVert *eve;
BMEdge *eed;
BMFace *efa;
BMIter iter;
em->bm->selectmode = em->selectmode;
edbm_strip_selections(em); /* strip BMEditSelections from em->selected that are not relevant to new mode */
if (em->bm->totvertsel == 0 &&
em->bm->totedgesel == 0 &&
em->bm->totfacesel == 0)
{
return;
}
if (em->selectmode & SCE_SELECT_VERTEX) {
if (em->bm->totvertsel) {
EDBM_select_flush(em);
}
}
else if (em->selectmode & SCE_SELECT_EDGE) {
/* deselect vertices, and select again based on edge select */
BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
BM_vert_select_set(em->bm, eve, false);
}
if (em->bm->totedgesel) {
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
BM_edge_select_set(em->bm, eed, true);
}
}
/* selects faces based on edge status */
EDBM_selectmode_flush(em);
}
}
else if (em->selectmode & SCE_SELECT_FACE) {
/* deselect eges, and select again based on face select */
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
BM_edge_select_set(em->bm, eed, false);
}
if (em->bm->totfacesel) {
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
BM_face_select_set(em->bm, efa, true);
}
}
}
}
}
/**
* Expand & Contract the Selection
* (used when changing modes and Ctrl key held)
*
* Flush the selection up:
* - vert -> edge
* - vert -> face
* - edge -> face
*
* Flush the selection down:
* - face -> edge
* - face -> vert
* - edge -> vert
*/
void EDBM_selectmode_convert(BMEditMesh *em, const short selectmode_old, const short selectmode_new)
{
BMesh *bm = em->bm;
BMVert *eve;
BMEdge *eed;
BMFace *efa;
BMIter iter;
/* first tag-to-select, then select --- this avoids a feedback loop */
/* have to find out what the selectionmode was previously */
if (selectmode_old == SCE_SELECT_VERTEX) {
if (bm->totvertsel == 0) {
/* pass */
}
else if (selectmode_new == SCE_SELECT_EDGE) {
/* flush up (vert -> edge) */
/* select all edges associated with every selected vert */
BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
BM_elem_flag_set(eed, BM_ELEM_TAG, BM_edge_is_any_vert_flag_test(eed, BM_ELEM_SELECT));
}
BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(eed, BM_ELEM_TAG)) {
BM_edge_select_set(bm, eed, true);
}
}
}
else if (selectmode_new == SCE_SELECT_FACE) {
/* flush up (vert -> face) */
/* select all faces associated with every selected vert */
BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
BM_elem_flag_set(efa, BM_ELEM_TAG, BM_face_is_any_vert_flag_test(efa, BM_ELEM_SELECT));
}
BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
BM_face_select_set(bm, efa, true);
}
}
}
}
else if (selectmode_old == SCE_SELECT_EDGE) {
if (bm->totedgesel == 0) {
/* pass */
}
else if (selectmode_new == SCE_SELECT_FACE) {
/* flush up (edge -> face) */
/* select all faces associated with every selected edge */
BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
BM_elem_flag_set(efa, BM_ELEM_TAG, BM_face_is_any_edge_flag_test(efa, BM_ELEM_SELECT));
}
BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
BM_face_select_set(bm, efa, true);
}
}
}
else if (selectmode_new == SCE_SELECT_VERTEX) {
/* flush down (edge -> vert) */
BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
if (!BM_vert_is_all_edge_flag_test(eve, BM_ELEM_SELECT, true)) {
BM_vert_select_set(bm, eve, false);
}
}
/* deselect edges without both verts selected */
BM_mesh_deselect_flush(bm);
}
}
else if (selectmode_old == SCE_SELECT_FACE) {
if (bm->totfacesel == 0) {
/* pass */
}
else if (selectmode_new == SCE_SELECT_EDGE) {
/* flush down (face -> edge) */
BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
if (!BM_edge_is_all_face_flag_test(eed, BM_ELEM_SELECT, true)) {
BM_edge_select_set(bm, eed, false);
}
}
}
else if (selectmode_new == SCE_SELECT_VERTEX) {
/* flush down (face -> vert) */
BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
if (!BM_vert_is_all_face_flag_test(eve, BM_ELEM_SELECT, true)) {
BM_vert_select_set(bm, eve, false);
}
}
/* deselect faces without verts selected */
BM_mesh_deselect_flush(bm);
}
}
}
/* user facing function, does notification */
bool EDBM_selectmode_toggle(bContext *C, const short selectmode_new,
const int action, const bool use_extend, const bool use_expand)
{
ToolSettings *ts = CTX_data_tool_settings(C);
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = NULL;
bool ret = false;
if (obedit && obedit->type == OB_MESH) {
em = BKE_editmesh_from_object(obedit);
}
if (em == NULL) {
return ret;
}
switch (action) {
case -1:
/* already set */
break;
case 0: /* disable */
/* check we have something to do */
if ((em->selectmode & selectmode_new) == 0) {
return false;
}
em->selectmode &= ~selectmode_new;
break;
case 1: /* enable */
/* check we have something to do */
if ((em->selectmode & selectmode_new) != 0) {
return false;
}
em->selectmode |= selectmode_new;
break;
case 2: /* toggle */
/* can't disable this flag if its the only one set */
if (em->selectmode == selectmode_new) {
return false;
}
em->selectmode ^= selectmode_new;
break;
default:
BLI_assert(0);
break;
}
if (use_extend == 0 || em->selectmode == 0) {
if (use_expand) {
const short selmode_max = highest_order_bit_s(ts->selectmode);
EDBM_selectmode_convert(em, selmode_max, selectmode_new);
}
}
switch (selectmode_new) {
case SCE_SELECT_VERTEX:
if (use_extend == 0 || em->selectmode == 0) {
em->selectmode = SCE_SELECT_VERTEX;
}
ts->selectmode = em->selectmode;
EDBM_selectmode_set(em);
ret = true;
break;
case SCE_SELECT_EDGE:
if (use_extend == 0 || em->selectmode == 0) {
em->selectmode = SCE_SELECT_EDGE;
}
ts->selectmode = em->selectmode;
EDBM_selectmode_set(em);
ret = true;
break;
case SCE_SELECT_FACE:
if (use_extend == 0 || em->selectmode == 0) {
em->selectmode = SCE_SELECT_FACE;
}
ts->selectmode = em->selectmode;
EDBM_selectmode_set(em);
ret = true;
break;
default:
BLI_assert(0);
break;
}
if (ret == true) {
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
WM_main_add_notifier(NC_SCENE | ND_TOOLSETTINGS, NULL);
}
return ret;
}
/**
* Use to disable a selectmode if its enabled, Using another mode as a fallback
* if the disabled mode is the only mode set.
*
* \return true if the mode is changed.
*/
bool EDBM_selectmode_disable(Scene *scene, BMEditMesh *em,
const short selectmode_disable,
const short selectmode_fallback)
{
/* note essential, but switch out of vertex mode since the
* selected regions wont be nicely isolated after flushing */
if (em->selectmode & selectmode_disable) {
if (em->selectmode == selectmode_disable) {
em->selectmode = selectmode_fallback;
}
else {
em->selectmode &= ~selectmode_disable;
}
scene->toolsettings->selectmode = em->selectmode;
EDBM_selectmode_set(em);
WM_main_add_notifier(NC_SCENE | ND_TOOLSETTINGS, scene);
return true;
}
else {
return false;
}
}
void EDBM_deselect_by_material(BMEditMesh *em, const short index, const bool select)
{
BMIter iter;
BMFace *efa;
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN))
continue;
if (efa->mat_nr == index) {
BM_face_select_set(em->bm, efa, select);
}
}
}
void EDBM_select_toggle_all(BMEditMesh *em) /* exported for UV */
{
if (em->bm->totvertsel || em->bm->totedgesel || em->bm->totfacesel)
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
else
EDBM_flag_enable_all(em, BM_ELEM_SELECT);
}
void EDBM_select_swap(BMEditMesh *em) /* exported for UV */
{
BMIter iter;
BMVert *eve;
BMEdge *eed;
BMFace *efa;
if (em->bm->selectmode & SCE_SELECT_VERTEX) {
BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(eve, BM_ELEM_HIDDEN))
continue;
BM_vert_select_set(em->bm, eve, !BM_elem_flag_test(eve, BM_ELEM_SELECT));
}
}
else if (em->selectmode & SCE_SELECT_EDGE) {
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(eed, BM_ELEM_HIDDEN))
continue;
BM_edge_select_set(em->bm, eed, !BM_elem_flag_test(eed, BM_ELEM_SELECT));
}
}
else {
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN))
continue;
BM_face_select_set(em->bm, efa, !BM_elem_flag_test(efa, BM_ELEM_SELECT));
}
}
// if (EM_texFaceCheck())
}
bool EDBM_select_interior_faces(BMEditMesh *em)
{
BMesh *bm = em->bm;
BMIter iter;
BMIter eiter;
BMFace *efa;
BMEdge *eed;
bool ok;
bool changed = false;
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN))
continue;
ok = true;
BM_ITER_ELEM (eed, &eiter, efa, BM_EDGES_OF_FACE) {
if (!BM_edge_face_count_is_over(eed, 2)) {
ok = false;
break;
}
}
if (ok) {
BM_face_select_set(bm, efa, true);
changed = true;
}
}
return changed;
}
/************************ Select Linked Operator *************************/
/* so we can have last-used default depend on selection mode (rare exception!) */
#define USE_LINKED_SELECT_DEFAULT_HACK
struct DelimitData {
int cd_loop_type;
int cd_loop_offset;
};
static bool select_linked_delimit_test(
BMEdge *e, int delimit,
const struct DelimitData *delimit_data)
{
BLI_assert(delimit);
if (delimit & BMO_DELIM_SEAM) {
if (BM_elem_flag_test(e, BM_ELEM_SEAM)) {
return true;
}
}
if (delimit & BMO_DELIM_SHARP) {
if (BM_elem_flag_test(e, BM_ELEM_SMOOTH) == 0) {
return true;
}
}
if (delimit & BMO_DELIM_NORMAL) {
if (!BM_edge_is_contiguous(e)) {
return true;
}
}
if (delimit & BMO_DELIM_MATERIAL) {
if (e->l && e->l->radial_next != e->l) {
const short mat_nr = e->l->f->mat_nr;
BMLoop *l_iter = e->l->radial_next;
do {
if (l_iter->f->mat_nr != mat_nr) {
return true;
}
} while ((l_iter = l_iter->radial_next) != e->l);
}
}
if (delimit & BMO_DELIM_UV) {
if (BM_edge_is_contiguous_loop_cd(e, delimit_data->cd_loop_type, delimit_data->cd_loop_offset) == 0) {
return true;
}
}
return false;
}
#ifdef USE_LINKED_SELECT_DEFAULT_HACK
/**
* Gets the default from the operator fallback to own last-used value
* (selected based on mode)
*/
static int select_linked_delimit_default_from_op(wmOperator *op, BMEditMesh *em)
{
static char delimit_last_store[2] = {0, BMO_DELIM_SEAM};
int delimit_last_index = (em->selectmode & (SCE_SELECT_VERTEX | SCE_SELECT_EDGE)) == 0;
char *delimit_last = &delimit_last_store[delimit_last_index];
PropertyRNA *prop_delimit = RNA_struct_find_property(op->ptr, "delimit");
int delimit;
if (RNA_property_is_set(op->ptr, prop_delimit)) {
delimit = RNA_property_enum_get(op->ptr, prop_delimit);
*delimit_last = delimit;
}
else {
delimit = *delimit_last;
RNA_property_enum_set(op->ptr, prop_delimit, delimit);
}
return delimit;
}
#endif
static void select_linked_delimit_validate(BMesh *bm, int *delimit)
{
if ((*delimit) & BMO_DELIM_UV) {
if (!CustomData_has_layer(&bm->ldata, CD_MLOOPUV)) {
(*delimit) &= ~BMO_DELIM_UV;
}
}
}
static void select_linked_delimit_begin(BMesh *bm, short selectmode, int delimit)
{
struct DelimitData delimit_data = {0};
BMIter iter;
BMEdge *e;
if (delimit & BMO_DELIM_UV) {
delimit_data.cd_loop_type = CD_MLOOPUV;
delimit_data.cd_loop_offset = CustomData_get_offset(&bm->ldata, delimit_data.cd_loop_type);
if (delimit_data.cd_loop_offset == -1) {
delimit &= ~BMO_DELIM_UV;
}
}
/* grr, shouldn't need to alloc BMO flags here */
BM_mesh_elem_toolflags_ensure(bm);
if (selectmode == SCE_SELECT_FACE) {
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
const bool is_walk_ok = (
(select_linked_delimit_test(e, delimit, &delimit_data) == false));
BMO_elem_flag_set(bm, e, BMO_ELE_TAG, is_walk_ok);
}
}
else {
/* don't delimit selected edges in vert/edge mode */
BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
const bool is_walk_ok = (
BM_elem_flag_test(e, BM_ELEM_SELECT) ||
(select_linked_delimit_test(e, delimit, &delimit_data) == false));
BMO_elem_flag_set(bm, e, BMO_ELE_TAG, is_walk_ok);
}
}
}
static void select_linked_delimit_end(BMEditMesh *em)
{
BMesh *bm = em->bm;
BM_mesh_elem_toolflags_clear(bm);
}
static int edbm_select_linked_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMesh *bm = em->bm;
BMIter iter;
BMWalker walker;
#ifdef USE_LINKED_SELECT_DEFAULT_HACK
int delimit = select_linked_delimit_default_from_op(op, em);
#else
int delimit = RNA_enum_get(op->ptr, "delimit");
#endif
select_linked_delimit_validate(bm, &delimit);
if (delimit) {
select_linked_delimit_begin(em->bm, em->selectmode, delimit);
}
if (em->selectmode & SCE_SELECT_VERTEX) {
BMVert *v;
BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
BM_elem_flag_set(v, BM_ELEM_TAG, BM_elem_flag_test(v, BM_ELEM_SELECT));
}
BMW_init(&walker, em->bm, delimit ? BMW_LOOP_SHELL_WIRE : BMW_VERT_SHELL,
BMW_MASK_NOP, delimit ? BMO_ELE_TAG : BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
if (delimit) {
BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
BMElem *ele_walk;
BMW_ITER (ele_walk, &walker, v) {
if (ele_walk->head.htype == BM_LOOP) {
BMVert *v_step = ((BMLoop *)ele_walk)->v;
BM_vert_select_set(em->bm, v_step, true);
BM_elem_flag_disable(v_step, BM_ELEM_TAG);
}
else {
BMEdge *e_step = (BMEdge *)ele_walk;
BLI_assert(ele_walk->head.htype == BM_EDGE);
BM_edge_select_set(em->bm, e_step, true);
BM_elem_flag_disable(e_step->v1, BM_ELEM_TAG);
BM_elem_flag_disable(e_step->v2, BM_ELEM_TAG);
}
}
}
}
}
else {
BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
BMEdge *e_walk;
BMW_ITER (e_walk, &walker, v) {
BM_edge_select_set(em->bm, e_walk, true);
BM_elem_flag_disable(e_walk, BM_ELEM_TAG);
}
}
}
}
BMW_end(&walker);
EDBM_selectmode_flush(em);
}
else if (em->selectmode & SCE_SELECT_EDGE) {
BMEdge *e;
BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
BM_elem_flag_set(e, BM_ELEM_TAG, BM_elem_flag_test(e, BM_ELEM_SELECT));
}
BMW_init(&walker, em->bm, delimit ? BMW_LOOP_SHELL_WIRE : BMW_VERT_SHELL,
BMW_MASK_NOP, delimit ? BMO_ELE_TAG : BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
if (delimit) {
BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
BMElem *ele_walk;
BMW_ITER (ele_walk, &walker, e) {
if (ele_walk->head.htype == BM_LOOP) {
BMLoop *l_step = (BMLoop *)ele_walk;
BM_edge_select_set(em->bm, l_step->e, true);
BM_edge_select_set(em->bm, l_step->prev->e, true);
BM_elem_flag_disable(l_step->e, BM_ELEM_TAG);
}
else {
BMEdge *e_step = (BMEdge *)ele_walk;
BLI_assert(ele_walk->head.htype == BM_EDGE);
BM_edge_select_set(em->bm, e_step, true);
BM_elem_flag_disable(e_step, BM_ELEM_TAG);
}
}
}
}
}
else {
BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
BMEdge *e_walk;
BMW_ITER (e_walk, &walker, e) {
BM_edge_select_set(em->bm, e_walk, true);
BM_elem_flag_disable(e_walk, BM_ELEM_TAG);
}
}
}
}
BMW_end(&walker);
EDBM_selectmode_flush(em);
}
else {
BMFace *f;
BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
BM_elem_flag_set(f, BM_ELEM_TAG, BM_elem_flag_test(f, BM_ELEM_SELECT));
}
BMW_init(&walker, bm, BMW_ISLAND,
BMW_MASK_NOP, delimit ? BMO_ELE_TAG : BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(f, BM_ELEM_TAG)) {
BMFace *f_walk;
BMW_ITER (f_walk, &walker, f) {
BM_face_select_set(bm, f_walk, true);
BM_elem_flag_disable(f_walk, BM_ELEM_TAG);
}
}
}
BMW_end(&walker);
}
if (delimit) {
select_linked_delimit_end(em);
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
void MESH_OT_select_linked(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Select Linked All";
ot->idname = "MESH_OT_select_linked";
ot->description = "Select all vertices linked to the active mesh";
/* api callbacks */
ot->exec = edbm_select_linked_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
prop = RNA_def_enum_flag(ot->srna, "delimit", rna_enum_mesh_delimit_mode_items, BMO_DELIM_SEAM, "Delimit",
"Delimit selected region");
#ifdef USE_LINKED_SELECT_DEFAULT_HACK
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
#else
UNUSED_VARS(prop);
#endif
}
static int edbm_select_linked_pick_exec(bContext *C, wmOperator *op);
static void edbm_select_linked_pick_ex(
BMEditMesh *em,
BMVert *eve, BMEdge *eed, BMFace *efa,
bool sel, int delimit)
{
BMesh *bm = em->bm;
BMWalker walker;
select_linked_delimit_validate(bm, &delimit);
if (delimit) {
select_linked_delimit_begin(bm, em->selectmode, delimit);
}
/* Note: logic closely matches 'edbm_select_linked_exec', keep in sync */
if ((em->selectmode & SCE_SELECT_VERTEX) && eve) {
BMW_init(&walker, bm, delimit ? BMW_LOOP_SHELL_WIRE : BMW_VERT_SHELL,
BMW_MASK_NOP, delimit ? BMO_ELE_TAG : BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
if (delimit) {
BMElem *ele_walk;
BMW_ITER (ele_walk, &walker, eve) {
if (ele_walk->head.htype == BM_LOOP) {
BMVert *v_step = ((BMLoop *)ele_walk)->v;
BM_vert_select_set(bm, v_step, sel);
}
else {
BMEdge *e_step = (BMEdge *)ele_walk;
BLI_assert(ele_walk->head.htype == BM_EDGE);
BM_edge_select_set(bm, e_step, sel);
}
}
}
else {
BMEdge *e_walk;
BMW_ITER (e_walk, &walker, eve) {
BM_edge_select_set(bm, e_walk, sel);
}
}
BMW_end(&walker);
EDBM_selectmode_flush(em);
}
else if ((em->selectmode & SCE_SELECT_EDGE) && eed) {
BMW_init(&walker, bm, delimit ? BMW_LOOP_SHELL_WIRE : BMW_VERT_SHELL,
BMW_MASK_NOP, delimit ? BMO_ELE_TAG : BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
if (delimit) {
BMElem *ele_walk;
BMW_ITER (ele_walk, &walker, eed) {
if (ele_walk->head.htype == BM_LOOP) {
BMEdge *e_step = ((BMLoop *)ele_walk)->e;
BM_edge_select_set(bm, e_step, sel);
}
else {
BMEdge *e_step = (BMEdge *)ele_walk;
BLI_assert(ele_walk->head.htype == BM_EDGE);
BM_edge_select_set(bm, e_step, sel);
}
}
}
else {
BMEdge *e_walk;
BMW_ITER (e_walk, &walker, eed) {
BM_edge_select_set(bm, e_walk, sel);
}
}
BMW_end(&walker);
EDBM_selectmode_flush(em);
}
else if ((em->selectmode & SCE_SELECT_FACE) && efa) {
BMW_init(&walker, bm, BMW_ISLAND,
BMW_MASK_NOP, delimit ? BMO_ELE_TAG : BMW_MASK_NOP, BMW_MASK_NOP,
BMW_FLAG_TEST_HIDDEN,
BMW_NIL_LAY);
{
BMFace *f_walk;
BMW_ITER (f_walk, &walker, efa) {
BM_face_select_set(bm, f_walk, sel);
BM_elem_flag_disable(f_walk, BM_ELEM_TAG);
}
}
BMW_end(&walker);
}
if (delimit) {
select_linked_delimit_end(em);
}
}
static int edbm_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
Object *obedit = CTX_data_edit_object(C);
ViewContext vc;
BMEditMesh *em;
BMesh *bm;
BMVert *eve;
BMEdge *eed;
BMFace *efa;
const bool sel = !RNA_boolean_get(op->ptr, "deselect");
int index;
if (RNA_struct_property_is_set(op->ptr, "index")) {
return edbm_select_linked_pick_exec(C, op);
}
/* unified_finednearest needs ogl */
view3d_operator_needs_opengl(C);
/* setup view context for argument to callbacks */
em_setup_viewcontext(C, &vc);
em = vc.em;
bm = em->bm;
if (bm->totedge == 0) {
return OPERATOR_CANCELLED;
}
vc.mval[0] = event->mval[0];
vc.mval[1] = event->mval[1];
/* return warning! */
if (unified_findnearest(&vc, &eve, &eed, &efa) == 0) {
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_CANCELLED;
}
#ifdef USE_LINKED_SELECT_DEFAULT_HACK
int delimit = select_linked_delimit_default_from_op(op, em);
#else
int delimit = RNA_enum_get(op->ptr, "delimit");
#endif
edbm_select_linked_pick_ex(em, eve, eed, efa, sel, delimit);
/* to support redo */
index = EDBM_elem_to_index_any_selectmode(em, eve, eed, efa);
RNA_int_set(op->ptr, "index", index);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
static int edbm_select_linked_pick_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMesh *bm = em->bm;
int index;
BMVert *eve = NULL;
BMEdge *eed = NULL;
BMFace *efa = NULL;
const bool sel = !RNA_boolean_get(op->ptr, "deselect");
index = RNA_int_get(op->ptr, "index");
if (index < 0 || index >= (bm->totvert + bm->totedge + bm->totface)) {
return OPERATOR_CANCELLED;
}
BMElem *ele = EDBM_elem_from_index_any(em, index);
switch (ele->head.htype) {
case BM_VERT:
eve = (BMVert *)ele;
break;
case BM_EDGE:
eed = (BMEdge *)ele;
break;
case BM_FACE:
efa = (BMFace *)ele;
break;
}
#ifdef USE_LINKED_SELECT_DEFAULT_HACK
int delimit = select_linked_delimit_default_from_op(op, em);
#else
int delimit = RNA_enum_get(op->ptr, "delimit");
#endif
edbm_select_linked_pick_ex(em, eve, eed, efa, sel, delimit);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
void MESH_OT_select_linked_pick(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Select Linked";
ot->idname = "MESH_OT_select_linked_pick";
ot->description = "(De)select all vertices linked to the edge under the mouse cursor";
/* api callbacks */
ot->invoke = edbm_select_linked_pick_invoke;
ot->exec = edbm_select_linked_pick_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "");
prop = RNA_def_enum_flag(ot->srna, "delimit", rna_enum_mesh_delimit_mode_items, BMO_DELIM_SEAM, "Delimit",
"Delimit selected region");
#ifdef USE_LINKED_SELECT_DEFAULT_HACK
RNA_def_property_flag(prop, PROP_SKIP_SAVE);
#endif
/* use for redo */
prop = RNA_def_int(ot->srna, "index", -1, -1, INT_MAX, "", "", 0, INT_MAX);
RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
}
static int edbm_select_face_by_sides_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMFace *efa;
BMIter iter;
const int numverts = RNA_int_get(op->ptr, "number");
const int type = RNA_enum_get(op->ptr, "type");
if (!RNA_boolean_get(op->ptr, "extend"))
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
bool select;
switch (type) {
case 0:
select = (efa->len < numverts);
break;
case 1:
select = (efa->len == numverts);
break;
case 2:
select = (efa->len > numverts);
break;
case 3:
select = (efa->len != numverts);
break;
default:
BLI_assert(0);
select = false;
break;
}
if (select) {
BM_face_select_set(em->bm, efa, true);
}
}
EDBM_selectmode_flush(em);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_select_face_by_sides(wmOperatorType *ot)
{
static const EnumPropertyItem type_items[] = {
{0, "LESS", 0, "Less Than", ""},
{1, "EQUAL", 0, "Equal To", ""},
{2, "GREATER", 0, "Greater Than", ""},
{3, "NOTEQUAL", 0, "Not Equal To", ""},
{0, NULL, 0, NULL, NULL}
};
/* identifiers */
ot->name = "Select Faces by Sides";
ot->description = "Select vertices or faces by the number of polygon sides";
ot->idname = "MESH_OT_select_face_by_sides";
/* api callbacks */
ot->exec = edbm_select_face_by_sides_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_int(ot->srna, "number", 4, 3, INT_MAX, "Number of Vertices", "", 3, INT_MAX);
RNA_def_enum(ot->srna, "type", type_items, 1, "Type", "Type of comparison to make");
RNA_def_boolean(ot->srna, "extend", true, "Extend", "Extend the selection");
}
static int edbm_select_loose_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMesh *bm = em->bm;
BMIter iter;
if (!RNA_boolean_get(op->ptr, "extend"))
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
if (em->selectmode & SCE_SELECT_VERTEX) {
BMVert *eve;
BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
if (!eve->e) {
BM_vert_select_set(bm, eve, true);
}
}
}
if (em->selectmode & SCE_SELECT_EDGE) {
BMEdge *eed;
BM_ITER_MESH (eed, &iter, bm, BM_EDGES_OF_MESH) {
if (BM_edge_is_wire(eed)) {
BM_edge_select_set(bm, eed, true);
}
}
}
if (em->selectmode & SCE_SELECT_FACE) {
BMFace *efa;
BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
BMIter liter;
BMLoop *l;
bool is_loose = true;
BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
if (!BM_edge_is_boundary(l->e)) {
is_loose = false;
break;
}
}
if (is_loose) {
BM_face_select_set(bm, efa, true);
}
}
}
EDBM_selectmode_flush(em);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_select_loose(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Loose Geometry";
ot->description = "Select loose geometry based on the selection mode";
ot->idname = "MESH_OT_select_loose";
/* api callbacks */
ot->exec = edbm_select_loose_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
}
static int edbm_select_mirror_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
bool extend = RNA_boolean_get(op->ptr, "extend");
if (em->bm->totvert && em->bm->totvertsel) {
int totmirr, totfail;
EDBM_select_mirrored(em, extend, &totmirr, &totfail);
if (totmirr) {
EDBM_selectmode_flush(em);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
}
ED_mesh_report_mirror_ex(op, totmirr, totfail, em->bm->selectmode);
}
return OPERATOR_FINISHED;
}
void MESH_OT_select_mirror(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Mirror";
ot->description = "Select mesh items at mirrored locations";
ot->idname = "MESH_OT_select_mirror";
/* api callbacks */
ot->exec = edbm_select_mirror_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the existing selection");
}
/* ******************** **************** */
static int edbm_select_more_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
const bool use_face_step = RNA_boolean_get(op->ptr, "use_face_step");
EDBM_select_more(em, use_face_step);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
void MESH_OT_select_more(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select More";
ot->idname = "MESH_OT_select_more";
ot->description = "Select more vertices, edges or faces connected to initial selection";
/* api callbacks */
ot->exec = edbm_select_more_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "use_face_step", true, "Face Step", "Connected faces (instead of edges)");
}
static int edbm_select_less_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
const bool use_face_step = RNA_boolean_get(op->ptr, "use_face_step");
EDBM_select_less(em, use_face_step);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
void MESH_OT_select_less(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Less";
ot->idname = "MESH_OT_select_less";
ot->description = "Deselect vertices, edges or faces at the boundary of each selection region";
/* api callbacks */
ot->exec = edbm_select_less_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "use_face_step", true, "Face Step", "Connected faces (instead of edges)");
}
/**
* Check if we're connected to another selected efge.
*/
static bool bm_edge_is_select_isolated(BMEdge *e)
{
BMIter viter;
BMVert *v;
BM_ITER_ELEM (v, &viter, e, BM_VERTS_OF_EDGE) {
BMIter eiter;
BMEdge *e_other;
BM_ITER_ELEM (e_other, &eiter, v, BM_EDGES_OF_VERT) {
if ((e_other != e) && BM_elem_flag_test(e_other, BM_ELEM_SELECT)) {
return false;
}
}
}
return true;
}
/* Walk all reachable elements of the same type as h_act in breadth-first
* order, starting from h_act. Deselects elements if the depth when they
* are reached is not a multiple of "nth". */
static void walker_deselect_nth(
BMEditMesh *em, const struct CheckerIntervalParams *op_params,
BMHeader *h_act)
{
BMElem *ele;
BMesh *bm = em->bm;
BMWalker walker;
BMIter iter;
int walktype = 0, itertype = 0, flushtype = 0;
short mask_vert = 0, mask_edge = 0, mask_face = 0;
/* No active element from which to start - nothing to do */
if (h_act == NULL) {
return;
}
/* Determine which type of iter, walker, and select flush to use
* based on type of the elements being deselected */
switch (h_act->htype) {
case BM_VERT:
itertype = BM_VERTS_OF_MESH;
walktype = BMW_CONNECTED_VERTEX;
flushtype = SCE_SELECT_VERTEX;
mask_vert = BMO_ELE_TAG;
break;
case BM_EDGE:
/* When an edge has no connected-selected edges,
* use face-stepping (supports edge-rings) */
itertype = BM_EDGES_OF_MESH;
walktype = bm_edge_is_select_isolated((BMEdge *)h_act) ? BMW_FACE_SHELL : BMW_VERT_SHELL;
flushtype = SCE_SELECT_EDGE;
mask_edge = BMO_ELE_TAG;
break;
case BM_FACE:
itertype = BM_FACES_OF_MESH;
walktype = BMW_ISLAND;
flushtype = SCE_SELECT_FACE;
mask_face = BMO_ELE_TAG;
break;
}
/* grr, shouldn't need to alloc BMO flags here */
BM_mesh_elem_toolflags_ensure(bm);
/* Walker restrictions uses BMO flags, not header flags,
* so transfer BM_ELEM_SELECT from HFlags onto a BMO flag layer. */
BMO_push(bm, NULL);
BM_ITER_MESH (ele, &iter, bm, itertype) {
if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
BMO_elem_flag_enable(bm, (BMElemF *)ele, BMO_ELE_TAG);
}
}
/* Walk over selected elements starting at active */
BMW_init(&walker, bm, walktype,
mask_vert, mask_edge, mask_face,
BMW_FLAG_NOP, /* don't use BMW_FLAG_TEST_HIDDEN here since we want to desel all */
BMW_NIL_LAY);
/* use tag to avoid touching the same verts twice */
BM_ITER_MESH (ele, &iter, bm, itertype) {
BM_elem_flag_disable(ele, BM_ELEM_TAG);
}
BLI_assert(walker.order == BMW_BREADTH_FIRST);
for (ele = BMW_begin(&walker, h_act); ele != NULL; ele = BMW_step(&walker)) {
if (!BM_elem_flag_test(ele, BM_ELEM_TAG)) {
/* Deselect elements that aren't at "nth" depth from active */
const int depth = BMW_current_depth(&walker) - 1;
if (WM_operator_properties_checker_interval_test(op_params, depth)) {
BM_elem_select_set(bm, ele, false);
}
BM_elem_flag_enable(ele, BM_ELEM_TAG);
}
}
BMW_end(&walker);
BMO_pop(bm);
/* Flush selection up */
EDBM_selectmode_flush_ex(em, flushtype);
}
static void deselect_nth_active(BMEditMesh *em, BMVert **r_eve, BMEdge **r_eed, BMFace **r_efa)
{
BMIter iter;
BMElem *ele;
*r_eve = NULL;
*r_eed = NULL;
*r_efa = NULL;
EDBM_selectmode_flush(em);
ele = BM_mesh_active_elem_get(em->bm);
if (ele && BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
switch (ele->head.htype) {
case BM_VERT:
*r_eve = (BMVert *)ele;
return;
case BM_EDGE:
*r_eed = (BMEdge *)ele;
return;
case BM_FACE:
*r_efa = (BMFace *)ele;
return;
}
}
if (em->selectmode & SCE_SELECT_VERTEX) {
BMVert *v;
BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
*r_eve = v;
return;
}
}
}
else if (em->selectmode & SCE_SELECT_EDGE) {
BMEdge *e;
BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
*r_eed = e;
return;
}
}
}
else if (em->selectmode & SCE_SELECT_FACE) {
BMFace *f = BM_mesh_active_face_get(em->bm, true, false);
if (f && BM_elem_flag_test(f, BM_ELEM_SELECT)) {
*r_efa = f;
return;
}
}
}
static bool edbm_deselect_nth(BMEditMesh *em, const struct CheckerIntervalParams *op_params)
{
BMVert *v;
BMEdge *e;
BMFace *f;
deselect_nth_active(em, &v, &e, &f);
if (v) {
walker_deselect_nth(em, op_params, &v->head);
return true;
}
else if (e) {
walker_deselect_nth(em, op_params, &e->head);
return true;
}
else if (f) {
walker_deselect_nth(em, op_params, &f->head);
return true;
}
return false;
}
static int edbm_select_nth_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
struct CheckerIntervalParams op_params;
WM_operator_properties_checker_interval_from_op(op, &op_params);
if (edbm_deselect_nth(em, &op_params) == false) {
BKE_report(op->reports, RPT_ERROR, "Mesh has no active vert/edge/face");
return OPERATOR_CANCELLED;
}
EDBM_update_generic(em, false, false);
return OPERATOR_FINISHED;
}
void MESH_OT_select_nth(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Checker Deselect";
ot->idname = "MESH_OT_select_nth";
ot->description = "Deselect every Nth element starting from the active vertex, edge or face";
/* api callbacks */
ot->exec = edbm_select_nth_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
WM_operator_properties_checker_interval(ot, false);
}
void em_setup_viewcontext(bContext *C, ViewContext *vc)
{
view3d_set_viewcontext(C, vc);
if (vc->obedit) {
vc->em = BKE_editmesh_from_object(vc->obedit);
}
}
static int edbm_select_sharp_edges_exec(bContext *C, wmOperator *op)
{
/* Find edges that have exactly two neighboring faces,
* check the angle between those faces, and if angle is
* small enough, select the edge
*/
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMIter iter;
BMEdge *e;
BMLoop *l1, *l2;
const float angle_limit_cos = cosf(RNA_float_get(op->ptr, "sharpness"));
BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(e, BM_ELEM_HIDDEN) == false &&
BM_edge_loop_pair(e, &l1, &l2))
{
/* edge has exactly two neighboring faces, check angle */
const float angle_cos = dot_v3v3(l1->f->no, l2->f->no);
if (angle_cos < angle_limit_cos) {
BM_edge_select_set(em->bm, e, true);
}
}
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_edges_select_sharp(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Select Sharp Edges";
ot->description = "Select all sharp-enough edges";
ot->idname = "MESH_OT_edges_select_sharp";
/* api callbacks */
ot->exec = edbm_select_sharp_edges_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
prop = RNA_def_float_rotation(ot->srna, "sharpness", 0, NULL, DEG2RADF(0.01f), DEG2RADF(180.0f),
"Sharpness", "", DEG2RADF(1.0f), DEG2RADF(180.0f));
RNA_def_property_float_default(prop, DEG2RADF(30.0f));
}
static int edbm_select_linked_flat_faces_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMesh *bm = em->bm;
BLI_LINKSTACK_DECLARE(stack, BMFace *);
BMIter iter, liter, liter2;
BMFace *f;
BMLoop *l, *l2;
const float angle_limit_cos = cosf(RNA_float_get(op->ptr, "sharpness"));
BM_mesh_elem_hflag_disable_all(bm, BM_FACE, BM_ELEM_TAG, false);
BLI_LINKSTACK_INIT(stack);
BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
if ((BM_elem_flag_test(f, BM_ELEM_HIDDEN) != 0) ||
(BM_elem_flag_test(f, BM_ELEM_TAG) != 0) ||
(BM_elem_flag_test(f, BM_ELEM_SELECT) == 0))
{
continue;
}
BLI_assert(BLI_LINKSTACK_SIZE(stack) == 0);
do {
BM_face_select_set(bm, f, true);
BM_elem_flag_enable(f, BM_ELEM_TAG);
BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
BM_ITER_ELEM (l2, &liter2, l, BM_LOOPS_OF_LOOP) {
float angle_cos;
if (BM_elem_flag_test(l2->f, BM_ELEM_TAG) ||
BM_elem_flag_test(l2->f, BM_ELEM_HIDDEN))
{
continue;
}
angle_cos = dot_v3v3(f->no, l2->f->no);
if (angle_cos > angle_limit_cos) {
BLI_LINKSTACK_PUSH(stack, l2->f);
}
}
}
} while ((f = BLI_LINKSTACK_POP(stack)));
}
BLI_LINKSTACK_FREE(stack);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_faces_select_linked_flat(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Select Linked Flat Faces";
ot->description = "Select linked faces by angle";
ot->idname = "MESH_OT_faces_select_linked_flat";
/* api callbacks */
ot->exec = edbm_select_linked_flat_faces_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
prop = RNA_def_float_rotation(ot->srna, "sharpness", 0, NULL, DEG2RADF(0.01f), DEG2RADF(180.0f),
"Sharpness", "", DEG2RADF(1.0f), DEG2RADF(180.0f));
RNA_def_property_float_default(prop, DEG2RADF(1.0f));
}
static int edbm_select_non_manifold_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMVert *v;
BMEdge *e;
BMIter iter;
const bool use_wire = RNA_boolean_get(op->ptr, "use_wire");
const bool use_boundary = RNA_boolean_get(op->ptr, "use_boundary");
const bool use_multi_face = RNA_boolean_get(op->ptr, "use_multi_face");
const bool use_non_contiguous = RNA_boolean_get(op->ptr, "use_non_contiguous");
const bool use_verts = RNA_boolean_get(op->ptr, "use_verts");
if (!RNA_boolean_get(op->ptr, "extend"))
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
/* Selects isolated verts, and edges that do not have 2 neighboring
* faces
*/
if (em->selectmode == SCE_SELECT_FACE) {
BKE_report(op->reports, RPT_ERROR, "Does not work in face selection mode");
return OPERATOR_CANCELLED;
}
if (use_verts) {
BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
if (!BM_elem_flag_test(v, BM_ELEM_HIDDEN)) {
if (!BM_vert_is_manifold(v)) {
BM_vert_select_set(em->bm, v, true);
}
}
}
}
if (use_wire || use_boundary || use_multi_face || use_non_contiguous) {
BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
if (!BM_elem_flag_test(e, BM_ELEM_HIDDEN)) {
if ((use_wire && BM_edge_is_wire(e)) ||
(use_boundary && BM_edge_is_boundary(e)) ||
(use_non_contiguous && (BM_edge_is_manifold(e) && !BM_edge_is_contiguous(e))) ||
(use_multi_face && (BM_edge_face_count_is_over(e, 2))))
{
/* check we never select perfect edge (in test above) */
BLI_assert(!(BM_edge_is_manifold(e) && BM_edge_is_contiguous(e)));
BM_edge_select_set(em->bm, e, true);
}
}
}
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
EDBM_selectmode_flush(em);
return OPERATOR_FINISHED;
}
void MESH_OT_select_non_manifold(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Non Manifold";
ot->description = "Select all non-manifold vertices or edges";
ot->idname = "MESH_OT_select_non_manifold";
/* api callbacks */
ot->exec = edbm_select_non_manifold_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_boolean(ot->srna, "extend", true, "Extend", "Extend the selection");
/* edges */
RNA_def_boolean(ot->srna, "use_wire", true, "Wire",
"Wire edges");
RNA_def_boolean(ot->srna, "use_boundary", true, "Boundaries",
"Boundary edges");
RNA_def_boolean(ot->srna, "use_multi_face", true,
"Multiple Faces", "Edges shared by 3+ faces");
RNA_def_boolean(ot->srna, "use_non_contiguous", true, "Non Contiguous",
"Edges between faces pointing in alternate directions");
/* verts */
RNA_def_boolean(ot->srna, "use_verts", true, "Vertices",
"Vertices connecting multiple face regions");
}
static int edbm_select_random_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
const bool select = (RNA_enum_get(op->ptr, "action") == SEL_SELECT);
const float randfac = RNA_float_get(op->ptr, "percent") / 100.0f;
const int seed = RNA_int_get(op->ptr, "seed");
BMIter iter;
RNG *rng = BLI_rng_new_srandom(seed);
if (em->selectmode & SCE_SELECT_VERTEX) {
BMVert *eve;
BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && BLI_rng_get_float(rng) < randfac) {
BM_vert_select_set(em->bm, eve, select);
}
}
}
else if (em->selectmode & SCE_SELECT_EDGE) {
BMEdge *eed;
BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && BLI_rng_get_float(rng) < randfac) {
BM_edge_select_set(em->bm, eed, select);
}
}
}
else {
BMFace *efa;
BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN) && BLI_rng_get_float(rng) < randfac) {
BM_face_select_set(em->bm, efa, select);
}
}
}
BLI_rng_free(rng);
if (select) {
/* was EDBM_select_flush, but it over select in edge/face mode */
EDBM_selectmode_flush(em);
}
else {
EDBM_deselect_flush(em);
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_select_random(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Random";
ot->description = "Randomly select vertices";
ot->idname = "MESH_OT_select_random";
/* api callbacks */
ot->exec = edbm_select_random_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
WM_operator_properties_select_random(ot);
}
static int edbm_select_ungrouped_poll(bContext *C)
{
if (ED_operator_editmesh(C)) {
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
if ((em->selectmode & SCE_SELECT_VERTEX) == 0) {
CTX_wm_operator_poll_msg_set(C, "Must be in vertex selection mode");
}
else if (BLI_listbase_is_empty(&obedit->defbase) || cd_dvert_offset == -1) {
CTX_wm_operator_poll_msg_set(C, "No weights/vertex groups on object");
}
else {
return true;
}
}
return false;
}
static int edbm_select_ungrouped_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
const int cd_dvert_offset = CustomData_get_offset(&em->bm->vdata, CD_MDEFORMVERT);
BMVert *eve;
BMIter iter;
if (!RNA_boolean_get(op->ptr, "extend")) {
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
}
BM_ITER_MESH (eve, &iter, em->bm, BM_VERTS_OF_MESH) {
if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
MDeformVert *dv = BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset);
/* no dv or dv set with no weight */
if (ELEM(NULL, dv, dv->dw)) {
BM_vert_select_set(em->bm, eve, true);
}
}
}
EDBM_selectmode_flush(em);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_select_ungrouped(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Ungrouped";
ot->idname = "MESH_OT_select_ungrouped";
ot->description = "Select vertices without a group";
/* api callbacks */
ot->exec = edbm_select_ungrouped_exec;
ot->poll = edbm_select_ungrouped_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
}
/* BMESH_TODO - some way to select on an arbitrary axis */
static int edbm_select_axis_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMesh *bm = em->bm;
BMVert *v_act = BM_mesh_active_vert_get(bm);
const int axis = RNA_enum_get(op->ptr, "axis");
const int mode = RNA_enum_get(op->ptr, "mode"); /* -1 == aligned, 0 == neg, 1 == pos */
if (v_act == NULL) {
BKE_report(op->reports, RPT_WARNING, "This operator requires an active vertex (last selected)");
return OPERATOR_CANCELLED;
}
else {
BMVert *v;
BMIter iter;
const float limit = RNA_float_get(op->ptr, "threshold");
float value = v_act->co[axis];
if (mode == 0)
value -= limit;
else if (mode == 1)
value += limit;
BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
if (!BM_elem_flag_test(v, BM_ELEM_HIDDEN)) {
switch (mode) {
case -1: /* aligned */
if (fabsf(v->co[axis] - value) < limit)
BM_vert_select_set(bm, v, true);
break;
case 0: /* neg */
if (v->co[axis] > value)
BM_vert_select_set(bm, v, true);
break;
case 1: /* pos */
if (v->co[axis] < value)
BM_vert_select_set(bm, v, true);
break;
}
}
}
}
EDBM_selectmode_flush(em);
WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_select_axis(wmOperatorType *ot)
{
static EnumPropertyItem axis_mode_items[] = {
{0, "POSITIVE", 0, "Positive Axis", ""},
{1, "NEGATIVE", 0, "Negative Axis", ""},
{-1, "ALIGNED", 0, "Aligned Axis", ""},
{0, NULL, 0, NULL, NULL}
};
static EnumPropertyItem axis_items_xyz[] = {
{0, "X_AXIS", 0, "X Axis", ""},
{1, "Y_AXIS", 0, "Y Axis", ""},
{2, "Z_AXIS", 0, "Z Axis", ""},
{0, NULL, 0, NULL, NULL}
};
/* identifiers */
ot->name = "Select Axis";
ot->description = "Select all data in the mesh on a single axis";
ot->idname = "MESH_OT_select_axis";
/* api callbacks */
ot->exec = edbm_select_axis_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_enum(ot->srna, "mode", axis_mode_items, 0, "Axis Mode", "Axis side to use when selecting");
RNA_def_enum(ot->srna, "axis", axis_items_xyz, 0, "Axis", "Select the axis to compare each vertex on");
RNA_def_float(ot->srna, "threshold", 0.0001f, 0.000001f, 50.0f, "Threshold", "", 0.00001f, 10.0f);
}
static int edbm_select_next_loop_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMFace *f;
BMVert *v;
BMIter iter;
BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
BM_elem_flag_disable(v, BM_ELEM_TAG);
}
BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
BMLoop *l;
BMIter liter;
BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
if (BM_elem_flag_test(l->v, BM_ELEM_SELECT)) {
BM_elem_flag_enable(l->next->v, BM_ELEM_TAG);
BM_vert_select_set(em->bm, l->v, false);
}
}
}
BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
BM_vert_select_set(em->bm, v, true);
}
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit);
return OPERATOR_FINISHED;
}
void MESH_OT_select_next_loop(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Next Loop";
ot->idname = "MESH_OT_select_next_loop";
ot->description = "Select next edge loop adjacent to a selected loop";
/* api callbacks */
ot->exec = edbm_select_next_loop_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int edbm_region_to_loop_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMFace *f;
BMEdge *e;
BMIter iter;
BM_mesh_elem_hflag_disable_all(em->bm, BM_EDGE, BM_ELEM_TAG, false);
BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
BMLoop *l1, *l2;
BMIter liter1, liter2;
BM_ITER_ELEM (l1, &liter1, f, BM_LOOPS_OF_FACE) {
int tot = 0, totsel = 0;
BM_ITER_ELEM (l2, &liter2, l1->e, BM_LOOPS_OF_EDGE) {
tot++;
totsel += BM_elem_flag_test(l2->f, BM_ELEM_SELECT) != 0;
}
if ((tot != totsel && totsel > 0) || (totsel == 1 && tot == 1))
BM_elem_flag_enable(l1->e, BM_ELEM_TAG);
}
}
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
BM_edge_select_set(em->bm, e, true);
}
}
/* If in face-only select mode, switch to edge select mode so that
* an edge-only selection is not inconsistent state */
if (em->selectmode == SCE_SELECT_FACE) {
em->selectmode = SCE_SELECT_EDGE;
EDBM_selectmode_set(em);
EDBM_selectmode_to_scene(C);
}
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_region_to_loop(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Boundary Loop";
ot->idname = "MESH_OT_region_to_loop";
ot->description = "Select boundary edges around the selected faces";
/* api callbacks */
ot->exec = edbm_region_to_loop_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static int loop_find_region(BMLoop *l, int flag,
GSet *visit_face_set, BMFace ***region_out)
{
BMFace **region = NULL;
BMFace **stack = NULL;
BLI_array_declare(region);
BLI_array_declare(stack);
BMFace *f;
BLI_array_append(stack, l->f);
BLI_gset_insert(visit_face_set, l->f);
while (BLI_array_count(stack) > 0) {
BMIter liter1, liter2;
BMLoop *l1, *l2;
f = BLI_array_pop(stack);
BLI_array_append(region, f);
BM_ITER_ELEM (l1, &liter1, f, BM_LOOPS_OF_FACE) {
if (BM_elem_flag_test(l1->e, flag))
continue;
BM_ITER_ELEM (l2, &liter2, l1->e, BM_LOOPS_OF_EDGE) {
/* avoids finding same region twice
* (otherwise) the logic works fine without */
if (BM_elem_flag_test(l2->f, BM_ELEM_TAG)) {
continue;
}
if (BLI_gset_add(visit_face_set, l2->f)) {
BLI_array_append(stack, l2->f);
}
}
}
}
BLI_array_free(stack);
*region_out = region;
return BLI_array_count(region);
}
static int verg_radial(const void *va, const void *vb)
{
const BMEdge *e_a = *((const BMEdge **)va);
const BMEdge *e_b = *((const BMEdge **)vb);
int a, b;
a = BM_edge_face_count(e_a);
b = BM_edge_face_count(e_b);
if (a > b) return -1;
if (a < b) return 1;
return 0;
}
/**
* This function leaves faces tagged which are apart of the new region.
*
* \note faces already tagged are ignored, to avoid finding the same regions twice:
* important when we have regions with equal face counts, see: T40309
*/
static int loop_find_regions(BMEditMesh *em, const bool selbigger)
{
GSet *visit_face_set;
BMIter iter;
const int edges_len = em->bm->totedgesel;
BMEdge *e, **edges;
int count = 0, i;
visit_face_set = BLI_gset_ptr_new_ex(__func__, edges_len);
edges = MEM_mallocN(sizeof(*edges) * edges_len, __func__);
i = 0;
BM_ITER_MESH (e, &iter, em->bm, BM_EDGES_OF_MESH) {
if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
edges[i++] = e;
BM_elem_flag_enable(e, BM_ELEM_TAG);
}
else {
BM_elem_flag_disable(e, BM_ELEM_TAG);
}
}
/* sort edges by radial cycle length */
qsort(edges, edges_len, sizeof(*edges), verg_radial);
for (i = 0; i < edges_len; i++) {
BMIter liter;
BMLoop *l;
BMFace **region = NULL, **region_out;
int c, tot = 0;
e = edges[i];
if (!BM_elem_flag_test(e, BM_ELEM_TAG))
continue;
BM_ITER_ELEM (l, &liter, e, BM_LOOPS_OF_EDGE) {
if (BLI_gset_haskey(visit_face_set, l->f))
continue;
c = loop_find_region(l, BM_ELEM_SELECT, visit_face_set, ®ion_out);
if (!region || (selbigger ? c >= tot : c < tot)) {
/* this region is the best seen so far */
tot = c;
if (region) {
/* free the previous best */
MEM_freeN(region);
}
/* track the current region as the new best */
region = region_out;
}
else {
/* this region is not as good as best so far, just free it */
MEM_freeN(region_out);
}
}
if (region) {
int j;
for (j = 0; j < tot; j++) {
BM_elem_flag_enable(region[j], BM_ELEM_TAG);
BM_ITER_ELEM (l, &liter, region[j], BM_LOOPS_OF_FACE) {
BM_elem_flag_disable(l->e, BM_ELEM_TAG);
}
}
count += tot;
MEM_freeN(region);
}
}
MEM_freeN(edges);
BLI_gset_free(visit_face_set, NULL);
return count;
}
static int edbm_loop_to_region_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(obedit);
BMIter iter;
BMFace *f;
const bool select_bigger = RNA_boolean_get(op->ptr, "select_bigger");
int a, b;
/* find the set of regions with smallest number of total faces */
BM_mesh_elem_hflag_disable_all(em->bm, BM_FACE, BM_ELEM_TAG, false);
a = loop_find_regions(em, select_bigger);
b = loop_find_regions(em, !select_bigger);
BM_mesh_elem_hflag_disable_all(em->bm, BM_FACE, BM_ELEM_TAG, false);
loop_find_regions(em, ((a <= b) != select_bigger) ? select_bigger : !select_bigger);
EDBM_flag_disable_all(em, BM_ELEM_SELECT);
BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
if (BM_elem_flag_test(f, BM_ELEM_TAG) && !BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
BM_face_select_set(em->bm, f, true);
}
}
EDBM_selectmode_flush(em);
WM_event_add_notifier(C, NC_GEOM | ND_SELECT, obedit->data);
return OPERATOR_FINISHED;
}
void MESH_OT_loop_to_region(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Select Loop Inner-Region";
ot->idname = "MESH_OT_loop_to_region";
ot->description = "Select region of faces inside of a selected loop of edges";
/* api callbacks */
ot->exec = edbm_loop_to_region_exec;
ot->poll = ED_operator_editmesh;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "select_bigger", 0, "Select Bigger", "Select bigger regions instead of smaller ones");
}
/************************ Select Path Operator *************************/
|