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

bmesh_marking.c « intern « bmesh « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cd3c83258312d96ae8f9308fcdc1efc49928741e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
/*
 * ***** 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.
 *
 * Contributor(s): Joseph Eagar, Geoffrey Bantle, Campbell Barton
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/bmesh/intern/bmesh_marking.c
 *  \ingroup bmesh
 *
 * Selection routines for bmesh structures.
 * This is actually all old code ripped from
 * editmesh_lib.c and slightly modified to work
 * for bmesh's. This also means that it has some
 * of the same problems.... something that
 * that should be addressed eventually.
 */

#include <stddef.h>

#include "MEM_guardedalloc.h"

#include "DNA_scene_types.h"

#include "BLI_math.h"
#include "BLI_listbase.h"

#include "bmesh.h"
#include "bmesh_structure.h"

static void recount_totsels(BMesh *bm)
{
	const char iter_types[3] = {BM_VERTS_OF_MESH,
	                            BM_EDGES_OF_MESH,
	                            BM_FACES_OF_MESH};
	int *tots[3];
	int i;

	/* recount (tot * sel) variables */
	bm->totvertsel = bm->totedgesel = bm->totfacesel = 0;
	tots[0] = &bm->totvertsel;
	tots[1] = &bm->totedgesel;
	tots[2] = &bm->totfacesel;

#pragma omp parallel for schedule(static) if (bm->totvert + bm->totedge + bm->totface >= BM_OMP_LIMIT)
	for (i = 0; i < 3; i++) {
		BMIter iter;
		BMElem *ele;
		int count = 0;

		BM_ITER_MESH (ele, &iter, bm, iter_types[i]) {
			if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) count += 1;
		}
		*tots[i] = count;
	}
}

/** \name BMesh helper functions for selection flushing.
 * \{ */

static bool bm_vert_is_edge_select_any_other(const BMVert *v, const BMEdge *e_first)
{
	const BMEdge *e_iter = e_first;

	/* start by stepping over the current edge */
	while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first) {
		if (BM_elem_flag_test(e_iter, BM_ELEM_SELECT)) {
			return true;
		}
	}
	return false;
}

#if 0
static bool bm_vert_is_edge_select_any(const BMVert *v)
{
	if (v->e) {
		const BMEdge *e_iter, *e_first;
		e_iter = e_first = v->e;
		do {
			if (BM_elem_flag_test(e_iter, BM_ELEM_SELECT)) {
				return true;
			}
		} while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first);
	}
	return false;
}
#endif

#if 0
static bool bm_edge_is_face_select_any_other(BMLoop *l_first)
{
	const BMLoop *l_iter = l_first;

	/* start by stepping over the current face */
	while ((l_iter = l_iter->radial_next) != l_first) {
		if (BM_elem_flag_test(l_iter->f, BM_ELEM_SELECT)) {
			return true;
		}
	}
	return false;
}
#endif

#if 0
static bool bm_edge_is_face_select_any(const BMEdge *e)
{
	if (e->l) {
		const BMLoop *l_iter, *l_first;
		l_iter = l_first = e->l;
		do {
			if (BM_elem_flag_test(l_iter->f, BM_ELEM_SELECT)) {
				return true;
			}
		} while ((l_iter = l_iter->radial_next) != l_first);
	}
	return false;
}
#endif

/** \} */

/**
 * \brief Select Mode Clean
 *
 * Remove isolated selected elements when in a mode doesn't support them.
 * eg: in edge-mode a selected vertex must be connected to a selected edge.
 *
 * \note this could be made apart of #BM_mesh_select_mode_flush_ex
 */
void BM_mesh_select_mode_clean_ex(BMesh *bm, const short selectmode)
{
	if (selectmode & SCE_SELECT_VERTEX) {
		/* pass */
	}
	else if (selectmode & SCE_SELECT_EDGE) {
		BMIter iter;

		if (bm->totvertsel) {
			BMVert *v;
			BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
				BM_elem_flag_disable(v, BM_ELEM_SELECT);
			}
			bm->totvertsel = 0;
		}

		if (bm->totedgesel) {
			BMEdge *e;
			BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
				if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
					BM_vert_select_set(bm, e->v1, true);
					BM_vert_select_set(bm, e->v2, true);
				}
			}
		}
	}
	else if (selectmode & SCE_SELECT_FACE) {
		BMIter iter;

		if (bm->totvertsel) {
			BMVert *v;
			BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
				BM_elem_flag_disable(v, BM_ELEM_SELECT);
			}
			bm->totvertsel = 0;
		}

		if (bm->totedgesel) {
			BMEdge *e;
			BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
				BM_elem_flag_disable(e, BM_ELEM_SELECT);
			}
			bm->totedgesel = 0;
		}

		if (bm->totfacesel) {
			BMFace *f;
			BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
				if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
					BMLoop *l_iter, *l_first;
					l_iter = l_first = BM_FACE_FIRST_LOOP(f);
					do {
						BM_edge_select_set(bm, l_iter->e, true);
					} while ((l_iter = l_iter->next) != l_first);
				}
			}
		}
	}
}

void BM_mesh_select_mode_clean(BMesh *bm)
{
	BM_mesh_select_mode_clean_ex(bm, bm->selectmode);
}

/**
 * \brief Select Mode Flush
 *
 * Makes sure to flush selections 'upwards'
 * (ie: all verts of an edge selects the edge and so on).
 * This should only be called by system and not tool authors.
 */
void BM_mesh_select_mode_flush_ex(BMesh *bm, const short selectmode)
{
	BMEdge *e;
	BMLoop *l_iter;
	BMLoop *l_first;
	BMFace *f;

	BMIter eiter;
	BMIter fiter;

	if (selectmode & SCE_SELECT_VERTEX) {
		/* both loops only set edge/face flags and read off verts */
#pragma omp parallel sections if (bm->totedge + bm->totface >= BM_OMP_LIMIT)
		{
#pragma omp section
			{
				BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
					if (BM_elem_flag_test(e->v1, BM_ELEM_SELECT) &&
					    BM_elem_flag_test(e->v2, BM_ELEM_SELECT) &&
					    !BM_elem_flag_test(e, BM_ELEM_HIDDEN))
					{
						BM_elem_flag_enable(e, BM_ELEM_SELECT);
					}
					else {
						BM_elem_flag_disable(e, BM_ELEM_SELECT);
					}
				}
			}
#pragma omp section
			{
				BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
					bool ok = true;
					if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
						l_iter = l_first = BM_FACE_FIRST_LOOP(f);
						do {
							if (!BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)) {
								ok = false;
								break;
							}
						} while ((l_iter = l_iter->next) != l_first);
					}
					else {
						ok = false;
					}

					BM_elem_flag_set(f, BM_ELEM_SELECT, ok);
				}
			}
		}
		/* end sections */
	}
	else if (selectmode & SCE_SELECT_EDGE) {
		BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
			bool ok = true;
			if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
				l_iter = l_first = BM_FACE_FIRST_LOOP(f);
				do {
					if (!BM_elem_flag_test(l_iter->e, BM_ELEM_SELECT)) {
						ok = false;
						break;
					}
				} while ((l_iter = l_iter->next) != l_first);
			}
			else {
				ok = false;
			}

			BM_elem_flag_set(f, BM_ELEM_SELECT, ok);
		}
	}

	/* Remove any deselected elements from the BMEditSelection */
	BM_select_history_validate(bm);

	recount_totsels(bm);
}

void BM_mesh_select_mode_flush(BMesh *bm)
{
	BM_mesh_select_mode_flush_ex(bm, bm->selectmode);
}

/**
 * mode independent flushing up/down
 */
void BM_mesh_deselect_flush(BMesh *bm)
{
	BMIter eiter;
	BMEdge *e;

	BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
		if (!BM_elem_flag_test(e, BM_ELEM_HIDDEN)) {
			if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
				if (!BM_elem_flag_test(e->v1, BM_ELEM_SELECT) ||
				    !BM_elem_flag_test(e->v2, BM_ELEM_SELECT))
				{
					BM_elem_flag_disable(e, BM_ELEM_SELECT);
				}
			}

			if (e->l && !BM_elem_flag_test(e, BM_ELEM_SELECT)) {
				BMLoop *l_iter;
				BMLoop *l_first;

				l_iter = l_first = e->l;
				do {
					BM_elem_flag_disable(l_iter->f, BM_ELEM_SELECT);
				} while ((l_iter = l_iter->radial_next) != l_first);
			}
		}
	}

	/* Remove any deselected elements from the BMEditSelection */
	BM_select_history_validate(bm);

	recount_totsels(bm);
}


/**
 * mode independent flushing up/down
 */
void BM_mesh_select_flush(BMesh *bm)
{
	BMEdge *e;
	BMLoop *l_iter;
	BMLoop *l_first;
	BMFace *f;

	BMIter eiter;
	BMIter fiter;

	bool ok;

	/* we can use 2 sections here because the second loop isnt checking edge selection */
#pragma omp parallel sections if (bm->totedge + bm->totface >= BM_OMP_LIMIT)
	{
#pragma omp section
		{
			BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
				if (BM_elem_flag_test(e->v1, BM_ELEM_SELECT) &&
				    BM_elem_flag_test(e->v2, BM_ELEM_SELECT) &&
				    !BM_elem_flag_test(e, BM_ELEM_HIDDEN))
				{
					BM_elem_flag_enable(e, BM_ELEM_SELECT);
				}
			}
		}

#pragma omp section
		{
			BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
				ok = true;
				if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
					l_iter = l_first = BM_FACE_FIRST_LOOP(f);
					do {
						if (!BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)) {
							ok = false;
							break;
						}
					} while ((l_iter = l_iter->next) != l_first);
				}
				else {
					ok = false;
				}

				if (ok) {
					BM_elem_flag_enable(f, BM_ELEM_SELECT);
				}
			}
		}
	}

	recount_totsels(bm);
}

/**
 * \brief Select Vert
 *
 * Changes selection state of a single vertex
 * in a mesh
 */
void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
{
	BLI_assert(v->head.htype == BM_VERT);

	if (BM_elem_flag_test(v, BM_ELEM_HIDDEN)) {
		return;
	}

	if (select) {
		if (!BM_elem_flag_test(v, BM_ELEM_SELECT)) {
			BM_elem_flag_enable(v, BM_ELEM_SELECT);
			bm->totvertsel += 1;
		}
	}
	else {
		if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
			bm->totvertsel -= 1;
			BM_elem_flag_disable(v, BM_ELEM_SELECT);
		}
	}
}

/**
 * \brief Select Edge
 *
 * Changes selection state of a single edge in a mesh.
 */
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
{
	BLI_assert(e->head.htype == BM_EDGE);

	if (BM_elem_flag_test(e, BM_ELEM_HIDDEN)) {
		return;
	}

	if (select) {
		if (!BM_elem_flag_test(e, BM_ELEM_SELECT)) {
			BM_elem_flag_enable(e, BM_ELEM_SELECT);
			bm->totedgesel += 1;
		}
		BM_vert_select_set(bm, e->v1, true);
		BM_vert_select_set(bm, e->v2, true);
	}
	else {
		if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
			BM_elem_flag_disable(e, BM_ELEM_SELECT);
			bm->totedgesel -= 1;
		}

		if ((bm->selectmode & SCE_SELECT_VERTEX) == 0) {
			int i;

			/* check if the vert is used by a selected edge */
			for (i = 0; i < 2; i++) {
				BMVert *v = *((&e->v1) + i);
				if (bm_vert_is_edge_select_any_other(v, e) == false) {
					BM_vert_select_set(bm, v, false);
				}
			}
		}
		else {
			BM_vert_select_set(bm, e->v1, false);
			BM_vert_select_set(bm, e->v2, false);
		}

	}
}

/**
 * \brief Select Face
 *
 * Changes selection state of a single
 * face in a mesh.
 */
void BM_face_select_set(BMesh *bm, BMFace *f, const bool select)
{
	BMLoop *l_iter;
	BMLoop *l_first;

	BLI_assert(f->head.htype == BM_FACE);

	if (BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
		return;
	}

	if (select) {
		if (!BM_elem_flag_test(f, BM_ELEM_SELECT)) {
			BM_elem_flag_enable(f, BM_ELEM_SELECT);
			bm->totfacesel += 1;
		}

		l_iter = l_first = BM_FACE_FIRST_LOOP(f);
		do {
			BM_vert_select_set(bm, l_iter->v, true);
			BM_edge_select_set(bm, l_iter->e, true);
		} while ((l_iter = l_iter->next) != l_first);
	}
	else {

		if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
			BM_elem_flag_disable(f, BM_ELEM_SELECT);
			bm->totfacesel -= 1;
		}
		/**
		 * \note This allows a temporarily invalid state - where for eg
		 * an edge bay be de-selected, but an adjacent face remains selected.
		 *
		 * Rely on #BM_mesh_select_mode_flush to correct these cases.
		 */
#if 1
		l_iter = l_first = BM_FACE_FIRST_LOOP(f);
		do {
			BM_vert_select_set(bm, l_iter->v, false);
			BM_edge_select_set(bm, l_iter->e, false);
		} while ((l_iter = l_iter->next) != l_first);
#else
		/* disabled, see T46494 */

		/* flush down to edges */
		l_iter = l_first = BM_FACE_FIRST_LOOP(f);
		do {
			/* vertex flushing is handled below */
			if (bm_edge_is_face_select_any_other(l_iter) == false) {
				BM_edge_select_set_noflush(bm, l_iter->e, false);
			}
		} while ((l_iter = l_iter->next) != l_first);

		/* flush down to verts */
		l_iter = l_first = BM_FACE_FIRST_LOOP(f);
		do {
			if (bm_vert_is_edge_select_any_other(l_iter->v, l_iter->e) == false) {
				BM_vert_select_set(bm, l_iter->v, false);
			}
		} while ((l_iter = l_iter->next) != l_first);
#endif
	}
}

/** \name Non flushing versions element selection.
 * \{ */

void BM_edge_select_set_noflush(BMesh *bm, BMEdge *e, const bool select)
{
	BLI_assert(e->head.htype == BM_EDGE);

	if (BM_elem_flag_test(e, BM_ELEM_HIDDEN)) {
		return;
	}

	if (select) {
		if (!BM_elem_flag_test(e, BM_ELEM_SELECT)) {
			BM_elem_flag_enable(e, BM_ELEM_SELECT);
			bm->totedgesel += 1;
		}
	}
	else {
		if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
			BM_elem_flag_disable(e, BM_ELEM_SELECT);
			bm->totedgesel -= 1;
		}
	}
}

void BM_face_select_set_noflush(BMesh *bm, BMFace *f, const bool select)
{
	BLI_assert(f->head.htype == BM_FACE);

	if (BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
		return;
	}

	if (select) {
		if (!BM_elem_flag_test(f, BM_ELEM_SELECT)) {
			BM_elem_flag_enable(f, BM_ELEM_SELECT);
			bm->totfacesel += 1;
		}
	}
	else {
		if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
			BM_elem_flag_disable(f, BM_ELEM_SELECT);
			bm->totfacesel -= 1;
		}
	}
}

/** \} */

/**
 * Select Mode Set
 *
 * Sets the selection mode for the bmesh,
 * updating the selection state.
 */
void BM_mesh_select_mode_set(BMesh *bm, int selectmode)
{
	BMIter iter;
	BMElem *ele;
	
	bm->selectmode = selectmode;

	if (bm->selectmode & SCE_SELECT_VERTEX) {
		/* disabled because selection flushing handles these */
#if 0
		BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
			BM_elem_flag_disable(ele, BM_ELEM_SELECT);
		}
		BM_ITER_MESH (ele, &iter, bm, BM_FACES_OF_MESH) {
			BM_elem_flag_disable(ele, BM_ELEM_SELECT);
		}
#endif
		BM_mesh_select_mode_flush(bm);
	}
	else if (bm->selectmode & SCE_SELECT_EDGE) {
		/* disabled because selection flushing handles these */
#if 0
		BM_ITER_MESH (ele, &iter, bm, BM_VERTS_OF_MESH) {
			BM_elem_flag_disable(ele, BM_ELEM_SELECT);
		}
#endif

		BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
			if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
				BM_edge_select_set(bm, (BMEdge *)ele, true);
			}
		}
		BM_mesh_select_mode_flush(bm);
	}
	else if (bm->selectmode & SCE_SELECT_FACE) {
		/* disabled because selection flushing handles these */
#if 0
		BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
			BM_elem_flag_disable(ele, BM_ELEM_SELECT);
		}
#endif
		BM_ITER_MESH (ele, &iter, bm, BM_FACES_OF_MESH) {
			if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
				BM_face_select_set(bm, (BMFace *)ele, true);
			}
		}
		BM_mesh_select_mode_flush(bm);
	}
}

/**
 * counts number of elements with flag enabled/disabled
 */
static int bm_mesh_flag_count(
        BMesh *bm, const char htype, const char hflag,
        const bool respecthide, const bool test_for_enabled)
{
	BMElem *ele;
	BMIter iter;
	int tot = 0;

	BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);

	if (htype & BM_VERT) {
		BM_ITER_MESH (ele, &iter, bm, BM_VERTS_OF_MESH) {
			if (respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN)) continue;
			if (BM_elem_flag_test_bool(ele, hflag) == test_for_enabled) tot++;
		}
	}
	if (htype & BM_EDGE) {
		BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
			if (respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN)) continue;
			if (BM_elem_flag_test_bool(ele, hflag) == test_for_enabled) tot++;
		}
	}
	if (htype & BM_FACE) {
		BM_ITER_MESH (ele, &iter, bm, BM_FACES_OF_MESH) {
			if (respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN)) continue;
			if (BM_elem_flag_test_bool(ele, hflag) == test_for_enabled) tot++;
		}
	}

	return tot;
}

int BM_mesh_elem_hflag_count_enabled(BMesh *bm, const char htype, const char hflag, const bool respecthide)
{
	return bm_mesh_flag_count(bm, htype, hflag, respecthide, true);
}

int BM_mesh_elem_hflag_count_disabled(BMesh *bm, const char htype, const char hflag, const bool respecthide)
{
	return bm_mesh_flag_count(bm, htype, hflag, respecthide, false);
}

/**
 * \note use BM_elem_flag_test(ele, BM_ELEM_SELECT) to test selection
 * \note by design, this will not touch the editselection history stuff
 */
void BM_elem_select_set(BMesh *bm, BMElem *ele, const bool select)
{
	switch (ele->head.htype) {
		case BM_VERT:
			BM_vert_select_set(bm, (BMVert *)ele, select);
			break;
		case BM_EDGE:
			BM_edge_select_set(bm, (BMEdge *)ele, select);
			break;
		case BM_FACE:
			BM_face_select_set(bm, (BMFace *)ele, select);
			break;
		default:
			BLI_assert(0);
			break;
	}
}

/* this replaces the active flag used in uv/face mode */
void BM_mesh_active_face_set(BMesh *bm, BMFace *efa)
{
	bm->act_face = efa;
}

BMFace *BM_mesh_active_face_get(BMesh *bm, const bool is_sloppy, const bool is_selected)
{
	if (bm->act_face && (!is_selected || BM_elem_flag_test(bm->act_face, BM_ELEM_SELECT))) {
		return bm->act_face;
	}
	else if (is_sloppy) {
		BMIter iter;
		BMFace *f = NULL;
		BMEditSelection *ese;
		
		/* Find the latest non-hidden face from the BMEditSelection */
		ese = bm->selected.last;
		for ( ; ese; ese = ese->prev) {
			if (ese->htype == BM_FACE) {
				f = (BMFace *)ese->ele;
				
				if (BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
					f = NULL;
				}
				else if (is_selected && !BM_elem_flag_test(f, BM_ELEM_SELECT)) {
					f = NULL;
				}
				else {
					break;
				}
			}
		}
		/* Last attempt: try to find any selected face */
		if (f == NULL) {
			BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
				if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
					break;
				}
			}
		}
		return f; /* can still be null */
	}
	return NULL;
}

BMEdge *BM_mesh_active_edge_get(BMesh *bm)
{
	if (bm->selected.last) {
		BMEditSelection *ese = bm->selected.last;

		if (ese && ese->htype == BM_EDGE) {
			return (BMEdge *)ese->ele;
		}
	}

	return NULL;
}

BMVert *BM_mesh_active_vert_get(BMesh *bm)
{
	if (bm->selected.last) {
		BMEditSelection *ese = bm->selected.last;

		if (ese && ese->htype == BM_VERT) {
			return (BMVert *)ese->ele;
		}
	}

	return NULL;
}

BMElem *BM_mesh_active_elem_get(BMesh *bm)
{
	if (bm->selected.last) {
		BMEditSelection *ese = bm->selected.last;

		if (ese) {
			return ese->ele;
		}
	}

	return NULL;
}

/**
 * Generic way to get data from an EditSelection type
 * These functions were written to be used by the Modifier widget
 * when in Rotate about active mode, but can be used anywhere.
 *
 * - #BM_editselection_center
 * - #BM_editselection_normal
 * - #BM_editselection_plane
 */
void BM_editselection_center(BMEditSelection *ese, float r_center[3])
{
	if (ese->htype == BM_VERT) {
		BMVert *eve = (BMVert *)ese->ele;
		copy_v3_v3(r_center, eve->co);
	}
	else if (ese->htype == BM_EDGE) {
		BMEdge *eed = (BMEdge *)ese->ele;
		mid_v3_v3v3(r_center, eed->v1->co, eed->v2->co);
	}
	else if (ese->htype == BM_FACE) {
		BMFace *efa = (BMFace *)ese->ele;
		BM_face_calc_center_mean(efa, r_center);
	}
}

void BM_editselection_normal(BMEditSelection *ese, float r_normal[3])
{
	if (ese->htype == BM_VERT) {
		BMVert *eve = (BMVert *)ese->ele;
		copy_v3_v3(r_normal, eve->no);
	}
	else if (ese->htype == BM_EDGE) {
		BMEdge *eed = (BMEdge *)ese->ele;
		float plane[3]; /* need a plane to correct the normal */
		float vec[3]; /* temp vec storage */
		
		add_v3_v3v3(r_normal, eed->v1->no, eed->v2->no);
		sub_v3_v3v3(plane, eed->v2->co, eed->v1->co);
		
		/* the 2 vertex normals will be close but not at rightangles to the edge
		 * for rotate about edge we want them to be at right angles, so we need to
		 * do some extra colculation to correct the vert normals,
		 * we need the plane for this */
		cross_v3_v3v3(vec, r_normal, plane);
		cross_v3_v3v3(r_normal, plane, vec);
		normalize_v3(r_normal);
		
	}
	else if (ese->htype == BM_FACE) {
		BMFace *efa = (BMFace *)ese->ele;
		copy_v3_v3(r_normal, efa->no);
	}
}

/* Calculate a plane that is rightangles to the edge/vert/faces normal
 * also make the plane run along an axis that is related to the geometry,
 * because this is used for the manipulators Y axis. */
void BM_editselection_plane(BMEditSelection *ese, float r_plane[3])
{
	if (ese->htype == BM_VERT) {
		BMVert *eve = (BMVert *)ese->ele;
		float vec[3] = {0.0f, 0.0f, 0.0f};
		
		if (ese->prev) { /* use previously selected data to make a useful vertex plane */
			BM_editselection_center(ese->prev, vec);
			sub_v3_v3v3(r_plane, vec, eve->co);
		}
		else {
			/* make a fake  plane thats at rightangles to the normal
			 * we cant make a crossvec from a vec thats the same as the vec
			 * unlikely but possible, so make sure if the normal is (0, 0, 1)
			 * that vec isn't the same or in the same direction even. */
			if      (eve->no[0] < 0.5f) vec[0] = 1.0f;
			else if (eve->no[1] < 0.5f) vec[1] = 1.0f;
			else                        vec[2] = 1.0f;
			cross_v3_v3v3(r_plane, eve->no, vec);
		}
		normalize_v3(r_plane);
	}
	else if (ese->htype == BM_EDGE) {
		BMEdge *eed = (BMEdge *)ese->ele;

		if (BM_edge_is_boundary(eed)) {
			sub_v3_v3v3(r_plane, eed->l->v->co, eed->l->next->v->co);
		}
		else {
			/* the plane is simple, it runs along the edge
			 * however selecting different edges can swap the direction of the y axis.
			 * this makes it less likely for the y axis of the manipulator
			 * (running along the edge).. to flip less often.
			 * at least its more predictable */
			if (eed->v2->co[1] > eed->v1->co[1]) {  /* check which to do first */
				sub_v3_v3v3(r_plane, eed->v2->co, eed->v1->co);
			}
			else {
				sub_v3_v3v3(r_plane, eed->v1->co, eed->v2->co);
			}
		}

		normalize_v3(r_plane);
	}
	else if (ese->htype == BM_FACE) {
		BMFace *efa = (BMFace *)ese->ele;
		BM_face_calc_plane(efa, r_plane);
	}
}

static BMEditSelection *bm_select_history_create(BMHeader *ele)
{
	BMEditSelection *ese = (BMEditSelection *) MEM_callocN(sizeof(BMEditSelection), "BMEdit Selection");
	ese->htype = ele->htype;
	ese->ele = (BMElem *)ele;
	return ese;
}

/* --- macro wrapped funcs --- */
bool _bm_select_history_check(BMesh *bm, const BMHeader *ele)
{
	return (BLI_findptr(&bm->selected, ele, offsetof(BMEditSelection, ele)) != NULL);
}

bool _bm_select_history_remove(BMesh *bm, BMHeader *ele)
{
	BMEditSelection *ese = BLI_findptr(&bm->selected, ele, offsetof(BMEditSelection, ele));
	if (ese) {
		BLI_freelinkN(&bm->selected, ese);
		return true;
	}
	else {
		return false;
	}
}

void _bm_select_history_store_notest(BMesh *bm, BMHeader *ele)
{
	BMEditSelection *ese = bm_select_history_create(ele);
	BLI_addtail(&(bm->selected), ese);
}

void _bm_select_history_store_head_notest(BMesh *bm, BMHeader *ele)
{
	BMEditSelection *ese = bm_select_history_create(ele);
	BLI_addhead(&(bm->selected), ese);
}

void _bm_select_history_store(BMesh *bm, BMHeader *ele)
{
	if (!BM_select_history_check(bm, (BMElem *)ele)) {
		BM_select_history_store_notest(bm, (BMElem *)ele);
	}
}

void _bm_select_history_store_head(BMesh *bm, BMHeader *ele)
{
	if (!BM_select_history_check(bm, (BMElem *)ele)) {
		BM_select_history_store_head_notest(bm, (BMElem *)ele);
	}
}

void _bm_select_history_store_after_notest(BMesh *bm, BMEditSelection *ese_ref, BMHeader *ele)
{
	BMEditSelection *ese = bm_select_history_create(ele);
	BLI_insertlinkafter(&(bm->selected), ese_ref, ese);
}

void _bm_select_history_store_after(BMesh *bm, BMEditSelection *ese_ref, BMHeader *ele)
{
	if (!BM_select_history_check(bm, (BMElem *)ele)) {
		BM_select_history_store_after_notest(bm, ese_ref, (BMElem *)ele);
	}
}
/* --- end macro wrapped funcs --- */


void BM_select_history_clear(BMesh *bm)
{
	BLI_freelistN(&bm->selected);
}


void BM_select_history_validate(BMesh *bm)
{
	BMEditSelection *ese, *ese_next;

	for (ese = bm->selected.first; ese; ese = ese_next) {
		ese_next = ese->next;
		if (!BM_elem_flag_test(ese->ele, BM_ELEM_SELECT)) {
			BLI_freelinkN(&(bm->selected), ese);
		}
	}
}

/**
 * Get the active mesh element (with active-face fallback).
 */
bool BM_select_history_active_get(BMesh *bm, BMEditSelection *ese)
{
	BMEditSelection *ese_last = bm->selected.last;
	BMFace *efa = BM_mesh_active_face_get(bm, false, false);

	ese->next = ese->prev = NULL;

	if (ese_last) {
		if (ese_last->htype == BM_FACE) { /* if there is an active face, use it over the last selected face */
			if (efa) {
				ese->ele = (BMElem *)efa;
			}
			else {
				ese->ele = ese_last->ele;
			}
			ese->htype = BM_FACE;
		}
		else {
			ese->ele =   ese_last->ele;
			ese->htype = ese_last->htype;
		}
	}
	else if (efa) {
		/* no edit-selection, fallback to active face */
		ese->ele   = (BMElem *)efa;
		ese->htype = BM_FACE;
	}
	else {
		ese->ele = NULL;
		return false;
	}

	return true;
}

/**
 * Return a map from BMVert/Edge/Face -> BMEditSelection
 */
GHash *BM_select_history_map_create(BMesh *bm)
{
	BMEditSelection *ese;
	GHash *map;

	if (BLI_listbase_is_empty(&bm->selected)) {
		return NULL;
	}

	map = BLI_ghash_ptr_new(__func__);

	for (ese = bm->selected.first; ese; ese = ese->next) {
		BLI_ghash_insert(map, ese->ele, ese);
	}

	return map;
}

void BM_mesh_elem_hflag_disable_test(
        BMesh *bm, const char htype, const char hflag,
        const bool respecthide, const bool overwrite, const char hflag_test)
{
	const char iter_types[3] = {BM_VERTS_OF_MESH,
	                            BM_EDGES_OF_MESH,
	                            BM_FACES_OF_MESH};

	const char flag_types[3] = {BM_VERT, BM_EDGE, BM_FACE};

	const char hflag_nosel = hflag & ~BM_ELEM_SELECT;

	int i;

	BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);

	if (hflag & BM_ELEM_SELECT) {
		BM_select_history_clear(bm);
	}

	if ((htype == (BM_VERT | BM_EDGE | BM_FACE)) &&
	    (hflag == BM_ELEM_SELECT) &&
	    (respecthide == false) &&
	    (hflag_test == 0))
	{
		/* fast path for deselect all, avoid topology loops
		 * since we know all will be de-selected anyway. */

#pragma omp parallel for schedule(static) if (bm->totvert + bm->totedge + bm->totface >= BM_OMP_LIMIT)
		for (i = 0; i < 3; i++) {
			BMIter iter;
			BMElem *ele;

			ele = BM_iter_new(&iter, bm, iter_types[i], NULL);
			for ( ; ele; ele = BM_iter_step(&iter)) {
				BM_elem_flag_disable(ele, BM_ELEM_SELECT);
			}
		}

		bm->totvertsel = bm->totedgesel = bm->totfacesel = 0;
	}
	else {
		for (i = 0; i < 3; i++) {
			BMIter iter;
			BMElem *ele;

			if (htype & flag_types[i]) {
				ele = BM_iter_new(&iter, bm, iter_types[i], NULL);
				for ( ; ele; ele = BM_iter_step(&iter)) {

					if (UNLIKELY(respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN))) {
						/* pass */
					}
					else if (!hflag_test || BM_elem_flag_test(ele, hflag_test)) {
						if (hflag & BM_ELEM_SELECT) {
							BM_elem_select_set(bm, ele, false);
						}
						BM_elem_flag_disable(ele, hflag);
					}
					else if (overwrite) {
						/* no match! */
						if (hflag & BM_ELEM_SELECT) {
							BM_elem_select_set(bm, ele, true);
						}
						BM_elem_flag_enable(ele, hflag_nosel);
					}
				}
			}
		}
	}
}

void BM_mesh_elem_hflag_enable_test(
        BMesh *bm, const char htype, const char hflag,
        const bool respecthide, const bool overwrite, const char hflag_test)
{
	const char iter_types[3] = {BM_VERTS_OF_MESH,
	                            BM_EDGES_OF_MESH,
	                            BM_FACES_OF_MESH};

	const char flag_types[3] = {BM_VERT, BM_EDGE, BM_FACE};

	/* use the nosel version when setting so under no
	 * condition may a hidden face become selected.
	 * Applying other flags to hidden faces is OK. */
	const char hflag_nosel = hflag & ~BM_ELEM_SELECT;

	BMIter iter;
	BMElem *ele;
	int i;

	BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);

	if (hflag & BM_ELEM_SELECT) {
		BM_select_history_clear(bm);
	}

	/* note, better not attempt a fast path for selection as done with de-select
	 * because hidden geometry and different selection modes can give different results,
	 * we could of course check for no hidden faces and then use quicker method but its not worth it. */

	for (i = 0; i < 3; i++) {
		if (htype & flag_types[i]) {
			ele = BM_iter_new(&iter, bm, iter_types[i], NULL);
			for ( ; ele; ele = BM_iter_step(&iter)) {

				if (UNLIKELY(respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN))) {
					/* pass */
				}
				else if (!hflag_test || BM_elem_flag_test(ele, hflag_test)) {
					/* match! */
					if (hflag & BM_ELEM_SELECT) {
						BM_elem_select_set(bm, ele, true);
					}
					BM_elem_flag_enable(ele, hflag_nosel);
				}
				else if (overwrite) {
					/* no match! */
					if (hflag & BM_ELEM_SELECT) {
						BM_elem_select_set(bm, ele, false);
					}
					BM_elem_flag_disable(ele, hflag);
				}
			}
		}
	}
}

void BM_mesh_elem_hflag_disable_all(
        BMesh *bm, const char htype, const char hflag,
        const bool respecthide)
{
	/* call with 0 hflag_test */
	BM_mesh_elem_hflag_disable_test(bm, htype, hflag, respecthide, false, 0);
}

void BM_mesh_elem_hflag_enable_all(
        BMesh *bm, const char htype, const char hflag,
        const bool respecthide)
{
	/* call with 0 hflag_test */
	BM_mesh_elem_hflag_enable_test(bm, htype, hflag, respecthide, false, 0);
}

/***************** Mesh Hiding stuff *********** */

static void vert_flush_hide_set(BMVert *v)
{
	BMIter iter;
	BMEdge *e;
	bool hide = true;

	BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
		hide = hide && BM_elem_flag_test(e, BM_ELEM_HIDDEN);
	}

	BM_elem_flag_set(v, BM_ELEM_HIDDEN, hide);
}

static void edge_flush_hide(BMEdge *e)
{
	BMIter iter;
	BMFace *f;
	bool hide = true;

	BM_ITER_ELEM (f, &iter, e, BM_FACES_OF_EDGE) {
		hide = hide && BM_elem_flag_test(f, BM_ELEM_HIDDEN);
	}

	BM_elem_flag_set(e, BM_ELEM_HIDDEN, hide);
}

void BM_vert_hide_set(BMVert *v, const bool hide)
{
	/* vert hiding: vert + surrounding edges and faces */
	BMIter iter, fiter;
	BMEdge *e;
	BMFace *f;

	BLI_assert(v->head.htype == BM_VERT);

	BM_elem_flag_set(v, BM_ELEM_HIDDEN, hide);

	BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
		BM_elem_flag_set(e, BM_ELEM_HIDDEN, hide);

		BM_ITER_ELEM (f, &fiter, e, BM_FACES_OF_EDGE) {
			BM_elem_flag_set(f, BM_ELEM_HIDDEN, hide);
		}
	}
}

void BM_edge_hide_set(BMEdge *e, const bool hide)
{
	BMIter iter;
	BMFace *f;
	/* BMVert *v; */

	BLI_assert(e->head.htype == BM_EDGE);

	/* edge hiding: faces around the edge */
	BM_ITER_ELEM (f, &iter, e, BM_FACES_OF_EDGE) {
		BM_elem_flag_set(f, BM_ELEM_HIDDEN, hide);
	}
	
	BM_elem_flag_set(e, BM_ELEM_HIDDEN, hide);

	/* hide vertices if necessary */
	vert_flush_hide_set(e->v1);
	vert_flush_hide_set(e->v2);
}

void BM_face_hide_set(BMFace *f, const bool hide)
{
	BMIter iter;
	BMLoop *l;

	BLI_assert(f->head.htype == BM_FACE);

	BM_elem_flag_set(f, BM_ELEM_HIDDEN, hide);

	BM_ITER_ELEM (l, &iter, f, BM_LOOPS_OF_FACE) {
		edge_flush_hide(l->e);
	}

	BM_ITER_ELEM (l, &iter, f, BM_LOOPS_OF_FACE) {
		vert_flush_hide_set(l->v);
	}
}

void _bm_elem_hide_set(BMesh *bm, BMHeader *head, const bool hide)
{
	/* Follow convention of always deselecting before
	 * hiding an element */
	switch (head->htype) {
		case BM_VERT:
			if (hide) BM_vert_select_set(bm, (BMVert *)head, false);
			BM_vert_hide_set((BMVert *)head, hide);
			break;
		case BM_EDGE:
			if (hide) BM_edge_select_set(bm, (BMEdge *)head, false);
			BM_edge_hide_set((BMEdge *)head, hide);
			break;
		case BM_FACE:
			if (hide) BM_face_select_set(bm, (BMFace *)head, false);
			BM_face_hide_set((BMFace *)head, hide);
			break;
		default:
			BMESH_ASSERT(0);
			break;
	}
}