doxygen: add newline after \file
[blender.git] / source / blender / bmesh / intern / bmesh_marking.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup bmesh
19  *
20  * Selection routines for bmesh structures.
21  * This is actually all old code ripped from
22  * editmesh_lib.c and slightly modified to work
23  * for bmesh's. This also means that it has some
24  * of the same problems.... something that
25  * that should be addressed eventually.
26  */
27
28 #include <stddef.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_scene_types.h"
33
34 #include "BLI_math.h"
35 #include "BLI_listbase.h"
36
37 #include "bmesh.h"
38 #include "bmesh_structure.h"
39
40 /* For '_FLAG_OVERLAP'. */
41 #include "bmesh_private.h"
42
43 static void recount_totsels(BMesh *bm)
44 {
45         const char iter_types[3] = {BM_VERTS_OF_MESH,
46                                     BM_EDGES_OF_MESH,
47                                     BM_FACES_OF_MESH};
48         int *tots[3];
49         int i;
50
51         /* recount (tot * sel) variables */
52         bm->totvertsel = bm->totedgesel = bm->totfacesel = 0;
53         tots[0] = &bm->totvertsel;
54         tots[1] = &bm->totedgesel;
55         tots[2] = &bm->totfacesel;
56
57         for (i = 0; i < 3; i++) {
58                 BMIter iter;
59                 BMElem *ele;
60                 int count = 0;
61
62                 BM_ITER_MESH (ele, &iter, bm, iter_types[i]) {
63                         if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) count += 1;
64                 }
65                 *tots[i] = count;
66         }
67 }
68
69 /** \name BMesh helper functions for selection & hide flushing.
70  * \{ */
71
72 static bool bm_vert_is_edge_select_any_other(const BMVert *v, const BMEdge *e_first)
73 {
74         const BMEdge *e_iter = e_first;
75
76         /* start by stepping over the current edge */
77         while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first) {
78                 if (BM_elem_flag_test(e_iter, BM_ELEM_SELECT)) {
79                         return true;
80                 }
81         }
82         return false;
83 }
84
85 #if 0
86 static bool bm_vert_is_edge_select_any(const BMVert *v)
87 {
88         if (v->e) {
89                 const BMEdge *e_iter, *e_first;
90                 e_iter = e_first = v->e;
91                 do {
92                         if (BM_elem_flag_test(e_iter, BM_ELEM_SELECT)) {
93                                 return true;
94                         }
95                 } while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first);
96         }
97         return false;
98 }
99 #endif
100
101 static bool bm_vert_is_edge_visible_any(const BMVert *v)
102 {
103         if (v->e) {
104                 const BMEdge *e_iter, *e_first;
105                 e_iter = e_first = v->e;
106                 do {
107                         if (!BM_elem_flag_test(e_iter, BM_ELEM_HIDDEN)) {
108                                 return true;
109                         }
110                 } while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first);
111         }
112         return false;
113 }
114
115 static bool bm_edge_is_face_select_any_other(BMLoop *l_first)
116 {
117         const BMLoop *l_iter = l_first;
118
119         /* start by stepping over the current face */
120         while ((l_iter = l_iter->radial_next) != l_first) {
121                 if (BM_elem_flag_test(l_iter->f, BM_ELEM_SELECT)) {
122                         return true;
123                 }
124         }
125         return false;
126 }
127
128 #if 0
129 static bool bm_edge_is_face_select_any(const BMEdge *e)
130 {
131         if (e->l) {
132                 const BMLoop *l_iter, *l_first;
133                 l_iter = l_first = e->l;
134                 do {
135                         if (BM_elem_flag_test(l_iter->f, BM_ELEM_SELECT)) {
136                                 return true;
137                         }
138                 } while ((l_iter = l_iter->radial_next) != l_first);
139         }
140         return false;
141 }
142 #endif
143
144 static bool bm_edge_is_face_visible_any(const BMEdge *e)
145 {
146         if (e->l) {
147                 const BMLoop *l_iter, *l_first;
148                 l_iter = l_first = e->l;
149                 do {
150                         if (!BM_elem_flag_test(l_iter->f, BM_ELEM_HIDDEN)) {
151                                 return true;
152                         }
153                 } while ((l_iter = l_iter->radial_next) != l_first);
154         }
155         return false;
156 }
157
158 /** \} */
159
160 /**
161  * \brief Select Mode Clean
162  *
163  * Remove isolated selected elements when in a mode doesn't support them.
164  * eg: in edge-mode a selected vertex must be connected to a selected edge.
165  *
166  * \note this could be made apart of #BM_mesh_select_mode_flush_ex
167  */
168 void BM_mesh_select_mode_clean_ex(BMesh *bm, const short selectmode)
169 {
170         if (selectmode & SCE_SELECT_VERTEX) {
171                 /* pass */
172         }
173         else if (selectmode & SCE_SELECT_EDGE) {
174                 BMIter iter;
175
176                 if (bm->totvertsel) {
177                         BMVert *v;
178                         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
179                                 BM_elem_flag_disable(v, BM_ELEM_SELECT);
180                         }
181                         bm->totvertsel = 0;
182                 }
183
184                 if (bm->totedgesel) {
185                         BMEdge *e;
186                         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
187                                 if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
188                                         BM_vert_select_set(bm, e->v1, true);
189                                         BM_vert_select_set(bm, e->v2, true);
190                                 }
191                         }
192                 }
193         }
194         else if (selectmode & SCE_SELECT_FACE) {
195                 BMIter iter;
196
197                 if (bm->totvertsel) {
198                         BMVert *v;
199                         BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
200                                 BM_elem_flag_disable(v, BM_ELEM_SELECT);
201                         }
202                         bm->totvertsel = 0;
203                 }
204
205                 if (bm->totedgesel) {
206                         BMEdge *e;
207                         BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
208                                 BM_elem_flag_disable(e, BM_ELEM_SELECT);
209                         }
210                         bm->totedgesel = 0;
211                 }
212
213                 if (bm->totfacesel) {
214                         BMFace *f;
215                         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
216                                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
217                                         BMLoop *l_iter, *l_first;
218                                         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
219                                         do {
220                                                 BM_edge_select_set(bm, l_iter->e, true);
221                                         } while ((l_iter = l_iter->next) != l_first);
222                                 }
223                         }
224                 }
225         }
226 }
227
228 void BM_mesh_select_mode_clean(BMesh *bm)
229 {
230         BM_mesh_select_mode_clean_ex(bm, bm->selectmode);
231 }
232
233 /**
234  * \brief Select Mode Flush
235  *
236  * Makes sure to flush selections 'upwards'
237  * (ie: all verts of an edge selects the edge and so on).
238  * This should only be called by system and not tool authors.
239  */
240 void BM_mesh_select_mode_flush_ex(BMesh *bm, const short selectmode)
241 {
242         BMEdge *e;
243         BMLoop *l_iter;
244         BMLoop *l_first;
245         BMFace *f;
246
247         BMIter eiter;
248         BMIter fiter;
249
250         if (selectmode & SCE_SELECT_VERTEX) {
251                 /* both loops only set edge/face flags and read off verts */
252                 BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
253                         if (BM_elem_flag_test(e->v1, BM_ELEM_SELECT) &&
254                             BM_elem_flag_test(e->v2, BM_ELEM_SELECT) &&
255                             !BM_elem_flag_test(e, BM_ELEM_HIDDEN))
256                         {
257                                 BM_elem_flag_enable(e, BM_ELEM_SELECT);
258                         }
259                         else {
260                                 BM_elem_flag_disable(e, BM_ELEM_SELECT);
261                         }
262                 }
263                 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
264                         bool ok = true;
265                         if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
266                                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
267                                 do {
268                                         if (!BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)) {
269                                                 ok = false;
270                                                 break;
271                                         }
272                                 } while ((l_iter = l_iter->next) != l_first);
273                         }
274                         else {
275                                 ok = false;
276                         }
277
278                         BM_elem_flag_set(f, BM_ELEM_SELECT, ok);
279                 }
280         }
281         else if (selectmode & SCE_SELECT_EDGE) {
282                 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
283                         bool ok = true;
284                         if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
285                                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
286                                 do {
287                                         if (!BM_elem_flag_test(l_iter->e, BM_ELEM_SELECT)) {
288                                                 ok = false;
289                                                 break;
290                                         }
291                                 } while ((l_iter = l_iter->next) != l_first);
292                         }
293                         else {
294                                 ok = false;
295                         }
296
297                         BM_elem_flag_set(f, BM_ELEM_SELECT, ok);
298                 }
299         }
300
301         /* Remove any deselected elements from the BMEditSelection */
302         BM_select_history_validate(bm);
303
304         recount_totsels(bm);
305 }
306
307 void BM_mesh_select_mode_flush(BMesh *bm)
308 {
309         BM_mesh_select_mode_flush_ex(bm, bm->selectmode);
310 }
311
312 /**
313  * mode independent flushing up/down
314  */
315 void BM_mesh_deselect_flush(BMesh *bm)
316 {
317         BMIter eiter;
318         BMEdge *e;
319
320         BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
321                 if (!BM_elem_flag_test(e, BM_ELEM_HIDDEN)) {
322                         if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
323                                 if (!BM_elem_flag_test(e->v1, BM_ELEM_SELECT) ||
324                                     !BM_elem_flag_test(e->v2, BM_ELEM_SELECT))
325                                 {
326                                         BM_elem_flag_disable(e, BM_ELEM_SELECT);
327                                 }
328                         }
329
330                         if (e->l && !BM_elem_flag_test(e, BM_ELEM_SELECT)) {
331                                 BMLoop *l_iter;
332                                 BMLoop *l_first;
333
334                                 l_iter = l_first = e->l;
335                                 do {
336                                         BM_elem_flag_disable(l_iter->f, BM_ELEM_SELECT);
337                                 } while ((l_iter = l_iter->radial_next) != l_first);
338                         }
339                 }
340         }
341
342         /* Remove any deselected elements from the BMEditSelection */
343         BM_select_history_validate(bm);
344
345         recount_totsels(bm);
346 }
347
348
349 /**
350  * mode independent flushing up/down
351  */
352 void BM_mesh_select_flush(BMesh *bm)
353 {
354         BMEdge *e;
355         BMLoop *l_iter;
356         BMLoop *l_first;
357         BMFace *f;
358
359         BMIter eiter;
360         BMIter fiter;
361
362         bool ok;
363
364         BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
365                 if (BM_elem_flag_test(e->v1, BM_ELEM_SELECT) &&
366                     BM_elem_flag_test(e->v2, BM_ELEM_SELECT) &&
367                     !BM_elem_flag_test(e, BM_ELEM_HIDDEN))
368                 {
369                         BM_elem_flag_enable(e, BM_ELEM_SELECT);
370                 }
371         }
372         BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
373                 ok = true;
374                 if (!BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
375                         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
376                         do {
377                                 if (!BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)) {
378                                         ok = false;
379                                         break;
380                                 }
381                         } while ((l_iter = l_iter->next) != l_first);
382                 }
383                 else {
384                         ok = false;
385                 }
386
387                 if (ok) {
388                         BM_elem_flag_enable(f, BM_ELEM_SELECT);
389                 }
390         }
391
392         recount_totsels(bm);
393 }
394
395 /**
396  * \brief Select Vert
397  *
398  * Changes selection state of a single vertex
399  * in a mesh
400  */
401 void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
402 {
403         BLI_assert(v->head.htype == BM_VERT);
404
405         if (BM_elem_flag_test(v, BM_ELEM_HIDDEN)) {
406                 return;
407         }
408
409         if (select) {
410                 if (!BM_elem_flag_test(v, BM_ELEM_SELECT)) {
411                         BM_elem_flag_enable(v, BM_ELEM_SELECT);
412                         bm->totvertsel += 1;
413                 }
414         }
415         else {
416                 if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
417                         bm->totvertsel -= 1;
418                         BM_elem_flag_disable(v, BM_ELEM_SELECT);
419                 }
420         }
421 }
422
423 /**
424  * \brief Select Edge
425  *
426  * Changes selection state of a single edge in a mesh.
427  */
428 void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
429 {
430         BLI_assert(e->head.htype == BM_EDGE);
431
432         if (BM_elem_flag_test(e, BM_ELEM_HIDDEN)) {
433                 return;
434         }
435
436         if (select) {
437                 if (!BM_elem_flag_test(e, BM_ELEM_SELECT)) {
438                         BM_elem_flag_enable(e, BM_ELEM_SELECT);
439                         bm->totedgesel += 1;
440                 }
441                 BM_vert_select_set(bm, e->v1, true);
442                 BM_vert_select_set(bm, e->v2, true);
443         }
444         else {
445                 if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
446                         BM_elem_flag_disable(e, BM_ELEM_SELECT);
447                         bm->totedgesel -= 1;
448                 }
449
450                 if ((bm->selectmode & SCE_SELECT_VERTEX) == 0) {
451                         int i;
452
453                         /* check if the vert is used by a selected edge */
454                         for (i = 0; i < 2; i++) {
455                                 BMVert *v = *((&e->v1) + i);
456                                 if (bm_vert_is_edge_select_any_other(v, e) == false) {
457                                         BM_vert_select_set(bm, v, false);
458                                 }
459                         }
460                 }
461                 else {
462                         BM_vert_select_set(bm, e->v1, false);
463                         BM_vert_select_set(bm, e->v2, false);
464                 }
465
466         }
467 }
468
469 /**
470  * \brief Select Face
471  *
472  * Changes selection state of a single
473  * face in a mesh.
474  */
475 void BM_face_select_set(BMesh *bm, BMFace *f, const bool select)
476 {
477         BMLoop *l_iter;
478         BMLoop *l_first;
479
480         BLI_assert(f->head.htype == BM_FACE);
481
482         if (BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
483                 return;
484         }
485
486         if (select) {
487                 if (!BM_elem_flag_test(f, BM_ELEM_SELECT)) {
488                         BM_elem_flag_enable(f, BM_ELEM_SELECT);
489                         bm->totfacesel += 1;
490                 }
491
492                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
493                 do {
494                         BM_vert_select_set(bm, l_iter->v, true);
495                         BM_edge_select_set(bm, l_iter->e, true);
496                 } while ((l_iter = l_iter->next) != l_first);
497         }
498         else {
499
500                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
501                         BM_elem_flag_disable(f, BM_ELEM_SELECT);
502                         bm->totfacesel -= 1;
503                 }
504                 /**
505                  * \note This allows a temporarily invalid state - where for eg
506                  * an edge bay be de-selected, but an adjacent face remains selected.
507                  *
508                  * Rely on #BM_mesh_select_mode_flush to correct these cases.
509                  *
510                  * \note flushing based on mode, see T46494
511                  */
512                 if (bm->selectmode & SCE_SELECT_VERTEX) {
513                         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
514                         do {
515                                 BM_vert_select_set(bm, l_iter->v, false);
516                                 BM_edge_select_set_noflush(bm, l_iter->e, false);
517                         } while ((l_iter = l_iter->next) != l_first);
518                 }
519                 else {
520                         /**
521                          * \note use #BM_edge_select_set_noflush,
522                          * vertex flushing is handled last.
523                          */
524                         if (bm->selectmode & SCE_SELECT_EDGE) {
525                                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
526                                 do {
527                                         BM_edge_select_set_noflush(bm, l_iter->e, false);
528                                 } while ((l_iter = l_iter->next) != l_first);
529                         }
530                         else {
531                                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
532                                 do {
533                                         if (bm_edge_is_face_select_any_other(l_iter) == false) {
534                                                 BM_edge_select_set_noflush(bm, l_iter->e, false);
535                                         }
536                                 } while ((l_iter = l_iter->next) != l_first);
537                         }
538
539                         /* flush down to verts */
540                         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
541                         do {
542                                 if (bm_vert_is_edge_select_any_other(l_iter->v, l_iter->e) == false) {
543                                         BM_vert_select_set(bm, l_iter->v, false);
544                                 }
545                         } while ((l_iter = l_iter->next) != l_first);
546                 }
547         }
548 }
549
550 /** \name Non flushing versions element selection.
551  * \{ */
552
553 void BM_edge_select_set_noflush(BMesh *bm, BMEdge *e, const bool select)
554 {
555         BLI_assert(e->head.htype == BM_EDGE);
556
557         if (BM_elem_flag_test(e, BM_ELEM_HIDDEN)) {
558                 return;
559         }
560
561         if (select) {
562                 if (!BM_elem_flag_test(e, BM_ELEM_SELECT)) {
563                         BM_elem_flag_enable(e, BM_ELEM_SELECT);
564                         bm->totedgesel += 1;
565                 }
566         }
567         else {
568                 if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
569                         BM_elem_flag_disable(e, BM_ELEM_SELECT);
570                         bm->totedgesel -= 1;
571                 }
572         }
573 }
574
575 void BM_face_select_set_noflush(BMesh *bm, BMFace *f, const bool select)
576 {
577         BLI_assert(f->head.htype == BM_FACE);
578
579         if (BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
580                 return;
581         }
582
583         if (select) {
584                 if (!BM_elem_flag_test(f, BM_ELEM_SELECT)) {
585                         BM_elem_flag_enable(f, BM_ELEM_SELECT);
586                         bm->totfacesel += 1;
587                 }
588         }
589         else {
590                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
591                         BM_elem_flag_disable(f, BM_ELEM_SELECT);
592                         bm->totfacesel -= 1;
593                 }
594         }
595 }
596
597 /** \} */
598
599 /**
600  * Select Mode Set
601  *
602  * Sets the selection mode for the bmesh,
603  * updating the selection state.
604  */
605 void BM_mesh_select_mode_set(BMesh *bm, int selectmode)
606 {
607         BMIter iter;
608         BMElem *ele;
609
610         bm->selectmode = selectmode;
611
612         if (bm->selectmode & SCE_SELECT_VERTEX) {
613                 /* disabled because selection flushing handles these */
614 #if 0
615                 BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
616                         BM_elem_flag_disable(ele, BM_ELEM_SELECT);
617                 }
618                 BM_ITER_MESH (ele, &iter, bm, BM_FACES_OF_MESH) {
619                         BM_elem_flag_disable(ele, BM_ELEM_SELECT);
620                 }
621 #endif
622                 BM_mesh_select_mode_flush(bm);
623         }
624         else if (bm->selectmode & SCE_SELECT_EDGE) {
625                 /* disabled because selection flushing handles these */
626 #if 0
627                 BM_ITER_MESH (ele, &iter, bm, BM_VERTS_OF_MESH) {
628                         BM_elem_flag_disable(ele, BM_ELEM_SELECT);
629                 }
630 #endif
631
632                 BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
633                         if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
634                                 BM_edge_select_set(bm, (BMEdge *)ele, true);
635                         }
636                 }
637                 BM_mesh_select_mode_flush(bm);
638         }
639         else if (bm->selectmode & SCE_SELECT_FACE) {
640                 /* disabled because selection flushing handles these */
641 #if 0
642                 BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
643                         BM_elem_flag_disable(ele, BM_ELEM_SELECT);
644                 }
645 #endif
646                 BM_ITER_MESH (ele, &iter, bm, BM_FACES_OF_MESH) {
647                         if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
648                                 BM_face_select_set(bm, (BMFace *)ele, true);
649                         }
650                 }
651                 BM_mesh_select_mode_flush(bm);
652         }
653 }
654
655 /**
656  * counts number of elements with flag enabled/disabled
657  */
658 static int bm_mesh_flag_count(
659         BMesh *bm, const char htype, const char hflag,
660         const bool respecthide, const bool test_for_enabled)
661 {
662         BMElem *ele;
663         BMIter iter;
664         int tot = 0;
665
666         BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);
667
668         if (htype & BM_VERT) {
669                 BM_ITER_MESH (ele, &iter, bm, BM_VERTS_OF_MESH) {
670                         if (respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN)) continue;
671                         if (BM_elem_flag_test_bool(ele, hflag) == test_for_enabled) tot++;
672                 }
673         }
674         if (htype & BM_EDGE) {
675                 BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
676                         if (respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN)) continue;
677                         if (BM_elem_flag_test_bool(ele, hflag) == test_for_enabled) tot++;
678                 }
679         }
680         if (htype & BM_FACE) {
681                 BM_ITER_MESH (ele, &iter, bm, BM_FACES_OF_MESH) {
682                         if (respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN)) continue;
683                         if (BM_elem_flag_test_bool(ele, hflag) == test_for_enabled) tot++;
684                 }
685         }
686
687         return tot;
688 }
689
690 int BM_mesh_elem_hflag_count_enabled(BMesh *bm, const char htype, const char hflag, const bool respecthide)
691 {
692         return bm_mesh_flag_count(bm, htype, hflag, respecthide, true);
693 }
694
695 int BM_mesh_elem_hflag_count_disabled(BMesh *bm, const char htype, const char hflag, const bool respecthide)
696 {
697         return bm_mesh_flag_count(bm, htype, hflag, respecthide, false);
698 }
699
700 /**
701  * \note use BM_elem_flag_test(ele, BM_ELEM_SELECT) to test selection
702  * \note by design, this will not touch the editselection history stuff
703  */
704 void BM_elem_select_set(BMesh *bm, BMElem *ele, const bool select)
705 {
706         switch (ele->head.htype) {
707                 case BM_VERT:
708                         BM_vert_select_set(bm, (BMVert *)ele, select);
709                         break;
710                 case BM_EDGE:
711                         BM_edge_select_set(bm, (BMEdge *)ele, select);
712                         break;
713                 case BM_FACE:
714                         BM_face_select_set(bm, (BMFace *)ele, select);
715                         break;
716                 default:
717                         BLI_assert(0);
718                         break;
719         }
720 }
721
722 /* this replaces the active flag used in uv/face mode */
723 void BM_mesh_active_face_set(BMesh *bm, BMFace *efa)
724 {
725         bm->act_face = efa;
726 }
727
728 BMFace *BM_mesh_active_face_get(BMesh *bm, const bool is_sloppy, const bool is_selected)
729 {
730         if (bm->act_face && (!is_selected || BM_elem_flag_test(bm->act_face, BM_ELEM_SELECT))) {
731                 return bm->act_face;
732         }
733         else if (is_sloppy) {
734                 BMIter iter;
735                 BMFace *f = NULL;
736                 BMEditSelection *ese;
737
738                 /* Find the latest non-hidden face from the BMEditSelection */
739                 ese = bm->selected.last;
740                 for ( ; ese; ese = ese->prev) {
741                         if (ese->htype == BM_FACE) {
742                                 f = (BMFace *)ese->ele;
743
744                                 if (BM_elem_flag_test(f, BM_ELEM_HIDDEN)) {
745                                         f = NULL;
746                                 }
747                                 else if (is_selected && !BM_elem_flag_test(f, BM_ELEM_SELECT)) {
748                                         f = NULL;
749                                 }
750                                 else {
751                                         break;
752                                 }
753                         }
754                 }
755                 /* Last attempt: try to find any selected face */
756                 if (f == NULL) {
757                         BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
758                                 if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
759                                         break;
760                                 }
761                         }
762                 }
763                 return f; /* can still be null */
764         }
765         return NULL;
766 }
767
768 BMEdge *BM_mesh_active_edge_get(BMesh *bm)
769 {
770         if (bm->selected.last) {
771                 BMEditSelection *ese = bm->selected.last;
772
773                 if (ese && ese->htype == BM_EDGE) {
774                         return (BMEdge *)ese->ele;
775                 }
776         }
777
778         return NULL;
779 }
780
781 BMVert *BM_mesh_active_vert_get(BMesh *bm)
782 {
783         if (bm->selected.last) {
784                 BMEditSelection *ese = bm->selected.last;
785
786                 if (ese && ese->htype == BM_VERT) {
787                         return (BMVert *)ese->ele;
788                 }
789         }
790
791         return NULL;
792 }
793
794 BMElem *BM_mesh_active_elem_get(BMesh *bm)
795 {
796         if (bm->selected.last) {
797                 BMEditSelection *ese = bm->selected.last;
798
799                 if (ese) {
800                         return ese->ele;
801                 }
802         }
803
804         return NULL;
805 }
806
807 /**
808  * Generic way to get data from an EditSelection type
809  * These functions were written to be used by the Modifier widget
810  * when in Rotate about active mode, but can be used anywhere.
811  *
812  * - #BM_editselection_center
813  * - #BM_editselection_normal
814  * - #BM_editselection_plane
815  */
816 void BM_editselection_center(BMEditSelection *ese, float r_center[3])
817 {
818         if (ese->htype == BM_VERT) {
819                 BMVert *eve = (BMVert *)ese->ele;
820                 copy_v3_v3(r_center, eve->co);
821         }
822         else if (ese->htype == BM_EDGE) {
823                 BMEdge *eed = (BMEdge *)ese->ele;
824                 mid_v3_v3v3(r_center, eed->v1->co, eed->v2->co);
825         }
826         else if (ese->htype == BM_FACE) {
827                 BMFace *efa = (BMFace *)ese->ele;
828                 BM_face_calc_center_median(efa, r_center);
829         }
830 }
831
832 void BM_editselection_normal(BMEditSelection *ese, float r_normal[3])
833 {
834         if (ese->htype == BM_VERT) {
835                 BMVert *eve = (BMVert *)ese->ele;
836                 copy_v3_v3(r_normal, eve->no);
837         }
838         else if (ese->htype == BM_EDGE) {
839                 BMEdge *eed = (BMEdge *)ese->ele;
840                 float plane[3]; /* need a plane to correct the normal */
841                 float vec[3]; /* temp vec storage */
842
843                 add_v3_v3v3(r_normal, eed->v1->no, eed->v2->no);
844                 sub_v3_v3v3(plane, eed->v2->co, eed->v1->co);
845
846                 /* the 2 vertex normals will be close but not at rightangles to the edge
847                  * for rotate about edge we want them to be at right angles, so we need to
848                  * do some extra calculation to correct the vert normals,
849                  * we need the plane for this */
850                 cross_v3_v3v3(vec, r_normal, plane);
851                 cross_v3_v3v3(r_normal, plane, vec);
852                 normalize_v3(r_normal);
853
854         }
855         else if (ese->htype == BM_FACE) {
856                 BMFace *efa = (BMFace *)ese->ele;
857                 copy_v3_v3(r_normal, efa->no);
858         }
859 }
860
861 /* Calculate a plane that is rightangles to the edge/vert/faces normal
862  * also make the plane run along an axis that is related to the geometry,
863  * because this is used for the gizmos Y axis. */
864 void BM_editselection_plane(BMEditSelection *ese, float r_plane[3])
865 {
866         if (ese->htype == BM_VERT) {
867                 BMVert *eve = (BMVert *)ese->ele;
868                 float vec[3] = {0.0f, 0.0f, 0.0f};
869
870                 if (ese->prev) { /* use previously selected data to make a useful vertex plane */
871                         BM_editselection_center(ese->prev, vec);
872                         sub_v3_v3v3(r_plane, vec, eve->co);
873                 }
874                 else {
875                         /* make a fake  plane thats at rightangles to the normal
876                          * we cant make a crossvec from a vec thats the same as the vec
877                          * unlikely but possible, so make sure if the normal is (0, 0, 1)
878                          * that vec isn't the same or in the same direction even. */
879                         if      (eve->no[0] < 0.5f) vec[0] = 1.0f;
880                         else if (eve->no[1] < 0.5f) vec[1] = 1.0f;
881                         else                        vec[2] = 1.0f;
882                         cross_v3_v3v3(r_plane, eve->no, vec);
883                 }
884                 normalize_v3(r_plane);
885         }
886         else if (ese->htype == BM_EDGE) {
887                 BMEdge *eed = (BMEdge *)ese->ele;
888
889                 if (BM_edge_is_boundary(eed)) {
890                         sub_v3_v3v3(r_plane, eed->l->v->co, eed->l->next->v->co);
891                 }
892                 else {
893                         /* the plane is simple, it runs along the edge
894                          * however selecting different edges can swap the direction of the y axis.
895                          * this makes it less likely for the y axis of the gizmo
896                          * (running along the edge).. to flip less often.
897                          * at least its more predictable */
898                         if (eed->v2->co[1] > eed->v1->co[1]) {  /* check which to do first */
899                                 sub_v3_v3v3(r_plane, eed->v2->co, eed->v1->co);
900                         }
901                         else {
902                                 sub_v3_v3v3(r_plane, eed->v1->co, eed->v2->co);
903                         }
904                 }
905
906                 normalize_v3(r_plane);
907         }
908         else if (ese->htype == BM_FACE) {
909                 BMFace *efa = (BMFace *)ese->ele;
910                 BM_face_calc_tangent_auto(efa, r_plane);
911         }
912 }
913
914 static BMEditSelection *bm_select_history_create(BMHeader *ele)
915 {
916         BMEditSelection *ese = (BMEditSelection *) MEM_callocN(sizeof(BMEditSelection), "BMEdit Selection");
917         ese->htype = ele->htype;
918         ese->ele = (BMElem *)ele;
919         return ese;
920 }
921
922 /* --- macro wrapped funcs --- */
923 bool _bm_select_history_check(BMesh *bm, const BMHeader *ele)
924 {
925         return (BLI_findptr(&bm->selected, ele, offsetof(BMEditSelection, ele)) != NULL);
926 }
927
928 bool _bm_select_history_remove(BMesh *bm, BMHeader *ele)
929 {
930         BMEditSelection *ese = BLI_findptr(&bm->selected, ele, offsetof(BMEditSelection, ele));
931         if (ese) {
932                 BLI_freelinkN(&bm->selected, ese);
933                 return true;
934         }
935         else {
936                 return false;
937         }
938 }
939
940 void _bm_select_history_store_notest(BMesh *bm, BMHeader *ele)
941 {
942         BMEditSelection *ese = bm_select_history_create(ele);
943         BLI_addtail(&(bm->selected), ese);
944 }
945
946 void _bm_select_history_store_head_notest(BMesh *bm, BMHeader *ele)
947 {
948         BMEditSelection *ese = bm_select_history_create(ele);
949         BLI_addhead(&(bm->selected), ese);
950 }
951
952 void _bm_select_history_store(BMesh *bm, BMHeader *ele)
953 {
954         if (!BM_select_history_check(bm, (BMElem *)ele)) {
955                 BM_select_history_store_notest(bm, (BMElem *)ele);
956         }
957 }
958
959 void _bm_select_history_store_head(BMesh *bm, BMHeader *ele)
960 {
961         if (!BM_select_history_check(bm, (BMElem *)ele)) {
962                 BM_select_history_store_head_notest(bm, (BMElem *)ele);
963         }
964 }
965
966 void _bm_select_history_store_after_notest(BMesh *bm, BMEditSelection *ese_ref, BMHeader *ele)
967 {
968         BMEditSelection *ese = bm_select_history_create(ele);
969         BLI_insertlinkafter(&(bm->selected), ese_ref, ese);
970 }
971
972 void _bm_select_history_store_after(BMesh *bm, BMEditSelection *ese_ref, BMHeader *ele)
973 {
974         if (!BM_select_history_check(bm, (BMElem *)ele)) {
975                 BM_select_history_store_after_notest(bm, ese_ref, (BMElem *)ele);
976         }
977 }
978 /* --- end macro wrapped funcs --- */
979
980
981 void BM_select_history_clear(BMesh *bm)
982 {
983         BLI_freelistN(&bm->selected);
984 }
985
986
987 void BM_select_history_validate(BMesh *bm)
988 {
989         BMEditSelection *ese, *ese_next;
990
991         for (ese = bm->selected.first; ese; ese = ese_next) {
992                 ese_next = ese->next;
993                 if (!BM_elem_flag_test(ese->ele, BM_ELEM_SELECT)) {
994                         BLI_freelinkN(&(bm->selected), ese);
995                 }
996         }
997 }
998
999 /**
1000  * Get the active mesh element (with active-face fallback).
1001  */
1002 bool BM_select_history_active_get(BMesh *bm, BMEditSelection *ese)
1003 {
1004         BMEditSelection *ese_last = bm->selected.last;
1005         BMFace *efa = BM_mesh_active_face_get(bm, false, false);
1006
1007         ese->next = ese->prev = NULL;
1008
1009         if (ese_last) {
1010                 if (ese_last->htype == BM_FACE) { /* if there is an active face, use it over the last selected face */
1011                         if (efa) {
1012                                 ese->ele = (BMElem *)efa;
1013                         }
1014                         else {
1015                                 ese->ele = ese_last->ele;
1016                         }
1017                         ese->htype = BM_FACE;
1018                 }
1019                 else {
1020                         ese->ele =   ese_last->ele;
1021                         ese->htype = ese_last->htype;
1022                 }
1023         }
1024         else if (efa) {
1025                 /* no edit-selection, fallback to active face */
1026                 ese->ele   = (BMElem *)efa;
1027                 ese->htype = BM_FACE;
1028         }
1029         else {
1030                 ese->ele = NULL;
1031                 return false;
1032         }
1033
1034         return true;
1035 }
1036
1037 /**
1038  * Return a map from BMVert/Edge/Face -> BMEditSelection
1039  */
1040 GHash *BM_select_history_map_create(BMesh *bm)
1041 {
1042         BMEditSelection *ese;
1043         GHash *map;
1044
1045         if (BLI_listbase_is_empty(&bm->selected)) {
1046                 return NULL;
1047         }
1048
1049         map = BLI_ghash_ptr_new(__func__);
1050
1051         for (ese = bm->selected.first; ese; ese = ese->next) {
1052                 BLI_ghash_insert(map, ese->ele, ese);
1053         }
1054
1055         return map;
1056 }
1057
1058 /**
1059  * Map arguments may all be the same pointer.
1060  */
1061 void BM_select_history_merge_from_targetmap(
1062         BMesh *bm,
1063         GHash *vert_map,
1064         GHash *edge_map,
1065         GHash *face_map,
1066         const bool use_chain)
1067 {
1068
1069 #ifdef DEBUG
1070         for (BMEditSelection *ese = bm->selected.first; ese; ese = ese->next) {
1071                 BLI_assert(BM_ELEM_API_FLAG_TEST(ese->ele, _FLAG_OVERLAP) == 0);
1072         }
1073 #endif
1074
1075         for (BMEditSelection *ese = bm->selected.first; ese; ese = ese->next) {
1076                 BM_ELEM_API_FLAG_ENABLE(ese->ele, _FLAG_OVERLAP);
1077
1078                 /* Only loop when (use_chain == true). */
1079                 GHash *map = NULL;
1080                 switch (ese->ele->head.htype) {
1081                         case BM_VERT: map = vert_map; break;
1082                         case BM_EDGE: map = edge_map; break;
1083                         case BM_FACE: map = face_map; break;
1084                         default: BMESH_ASSERT(0);     break;
1085                 }
1086                 if (map != NULL) {
1087                         BMElem *ele_dst = ese->ele;
1088                         while (true) {
1089                                 BMElem *ele_dst_next = BLI_ghash_lookup(map, ele_dst);
1090                                 BLI_assert(ele_dst != ele_dst_next);
1091                                 if (ele_dst_next == NULL) {
1092                                         break;
1093                                 }
1094                                 ele_dst = ele_dst_next;
1095                                 /* Break loop on circular reference (should never happen). */
1096                                 if (UNLIKELY(ele_dst == ese->ele)) {
1097                                         BLI_assert(0);
1098                                         break;
1099                                 }
1100                                 if (use_chain == false) {
1101                                         break;
1102                                 }
1103                         }
1104                         ese->ele = ele_dst;
1105                 }
1106         }
1107
1108         /* Remove overlapping duplicates. */
1109         for (BMEditSelection *ese = bm->selected.first, *ese_next; ese; ese = ese_next) {
1110                 ese_next = ese->next;
1111                 if (BM_ELEM_API_FLAG_TEST(ese->ele, _FLAG_OVERLAP)) {
1112                         BM_ELEM_API_FLAG_DISABLE(ese->ele, _FLAG_OVERLAP);
1113                 }
1114                 else {
1115                         BLI_freelinkN(&bm->selected, ese);
1116                 }
1117         }
1118 }
1119
1120 void BM_mesh_elem_hflag_disable_test(
1121         BMesh *bm, const char htype, const char hflag,
1122         const bool respecthide, const bool overwrite, const char hflag_test)
1123 {
1124         const char iter_types[3] = {BM_VERTS_OF_MESH,
1125                                     BM_EDGES_OF_MESH,
1126                                     BM_FACES_OF_MESH};
1127
1128         const char flag_types[3] = {BM_VERT, BM_EDGE, BM_FACE};
1129
1130         const char hflag_nosel = hflag & ~BM_ELEM_SELECT;
1131
1132         int i;
1133
1134         BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);
1135
1136         if (hflag & BM_ELEM_SELECT) {
1137                 BM_select_history_clear(bm);
1138         }
1139
1140         if ((htype == (BM_VERT | BM_EDGE | BM_FACE)) &&
1141             (hflag == BM_ELEM_SELECT) &&
1142             (respecthide == false) &&
1143             (hflag_test == 0))
1144         {
1145                 /* fast path for deselect all, avoid topology loops
1146                  * since we know all will be de-selected anyway. */
1147                 for (i = 0; i < 3; i++) {
1148                         BMIter iter;
1149                         BMElem *ele;
1150
1151                         ele = BM_iter_new(&iter, bm, iter_types[i], NULL);
1152                         for ( ; ele; ele = BM_iter_step(&iter)) {
1153                                 BM_elem_flag_disable(ele, BM_ELEM_SELECT);
1154                         }
1155                 }
1156
1157                 bm->totvertsel = bm->totedgesel = bm->totfacesel = 0;
1158         }
1159         else {
1160                 for (i = 0; i < 3; i++) {
1161                         BMIter iter;
1162                         BMElem *ele;
1163
1164                         if (htype & flag_types[i]) {
1165                                 ele = BM_iter_new(&iter, bm, iter_types[i], NULL);
1166                                 for ( ; ele; ele = BM_iter_step(&iter)) {
1167
1168                                         if (UNLIKELY(respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN))) {
1169                                                 /* pass */
1170                                         }
1171                                         else if (!hflag_test || BM_elem_flag_test(ele, hflag_test)) {
1172                                                 if (hflag & BM_ELEM_SELECT) {
1173                                                         BM_elem_select_set(bm, ele, false);
1174                                                 }
1175                                                 BM_elem_flag_disable(ele, hflag);
1176                                         }
1177                                         else if (overwrite) {
1178                                                 /* no match! */
1179                                                 if (hflag & BM_ELEM_SELECT) {
1180                                                         BM_elem_select_set(bm, ele, true);
1181                                                 }
1182                                                 BM_elem_flag_enable(ele, hflag_nosel);
1183                                         }
1184                                 }
1185                         }
1186                 }
1187         }
1188 }
1189
1190 void BM_mesh_elem_hflag_enable_test(
1191         BMesh *bm, const char htype, const char hflag,
1192         const bool respecthide, const bool overwrite, const char hflag_test)
1193 {
1194         const char iter_types[3] = {BM_VERTS_OF_MESH,
1195                                     BM_EDGES_OF_MESH,
1196                                     BM_FACES_OF_MESH};
1197
1198         const char flag_types[3] = {BM_VERT, BM_EDGE, BM_FACE};
1199
1200         /* use the nosel version when setting so under no
1201          * condition may a hidden face become selected.
1202          * Applying other flags to hidden faces is OK. */
1203         const char hflag_nosel = hflag & ~BM_ELEM_SELECT;
1204
1205         BMIter iter;
1206         BMElem *ele;
1207         int i;
1208
1209         BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);
1210
1211         /* note, better not attempt a fast path for selection as done with de-select
1212          * because hidden geometry and different selection modes can give different results,
1213          * we could of course check for no hidden faces and then use quicker method but its not worth it. */
1214
1215         for (i = 0; i < 3; i++) {
1216                 if (htype & flag_types[i]) {
1217                         ele = BM_iter_new(&iter, bm, iter_types[i], NULL);
1218                         for ( ; ele; ele = BM_iter_step(&iter)) {
1219
1220                                 if (UNLIKELY(respecthide && BM_elem_flag_test(ele, BM_ELEM_HIDDEN))) {
1221                                         /* pass */
1222                                 }
1223                                 else if (!hflag_test || BM_elem_flag_test(ele, hflag_test)) {
1224                                         /* match! */
1225                                         if (hflag & BM_ELEM_SELECT) {
1226                                                 BM_elem_select_set(bm, ele, true);
1227                                         }
1228                                         BM_elem_flag_enable(ele, hflag_nosel);
1229                                 }
1230                                 else if (overwrite) {
1231                                         /* no match! */
1232                                         if (hflag & BM_ELEM_SELECT) {
1233                                                 BM_elem_select_set(bm, ele, false);
1234                                         }
1235                                         BM_elem_flag_disable(ele, hflag);
1236                                 }
1237                         }
1238                 }
1239         }
1240 }
1241
1242 void BM_mesh_elem_hflag_disable_all(
1243         BMesh *bm, const char htype, const char hflag,
1244         const bool respecthide)
1245 {
1246         /* call with 0 hflag_test */
1247         BM_mesh_elem_hflag_disable_test(bm, htype, hflag, respecthide, false, 0);
1248 }
1249
1250 void BM_mesh_elem_hflag_enable_all(
1251         BMesh *bm, const char htype, const char hflag,
1252         const bool respecthide)
1253 {
1254         /* call with 0 hflag_test */
1255         BM_mesh_elem_hflag_enable_test(bm, htype, hflag, respecthide, false, 0);
1256 }
1257
1258 /***************** Mesh Hiding stuff *********** */
1259
1260 /**
1261  * Hide unless any connected elements are visible.
1262  * Run this after hiding a connected edge or face.
1263  */
1264 static void vert_flush_hide_set(BMVert *v)
1265 {
1266         BM_elem_flag_set(v, BM_ELEM_HIDDEN, !bm_vert_is_edge_visible_any(v));
1267 }
1268
1269 /**
1270  * Hide unless any connected elements are visible.
1271  * Run this after hiding a connected face.
1272  */
1273 static void edge_flush_hide_set(BMEdge *e)
1274 {
1275         BM_elem_flag_set(e, BM_ELEM_HIDDEN, !bm_edge_is_face_visible_any(e));
1276 }
1277
1278 void BM_vert_hide_set(BMVert *v, const bool hide)
1279 {
1280         /* vert hiding: vert + surrounding edges and faces */
1281         BLI_assert(v->head.htype == BM_VERT);
1282         if (hide) {
1283                 BLI_assert(!BM_elem_flag_test(v, BM_ELEM_SELECT));
1284         }
1285
1286         BM_elem_flag_set(v, BM_ELEM_HIDDEN, hide);
1287
1288         if (v->e) {
1289                 BMEdge *e_iter, *e_first;
1290                 e_iter = e_first = v->e;
1291                 do {
1292                         BM_elem_flag_set(e_iter, BM_ELEM_HIDDEN, hide);
1293                         if (e_iter->l) {
1294                                 const BMLoop *l_radial_iter, *l_radial_first;
1295                                 l_radial_iter = l_radial_first = e_iter->l;
1296                                 do {
1297                                         BM_elem_flag_set(l_radial_iter->f, BM_ELEM_HIDDEN, hide);
1298                                 } while ((l_radial_iter = l_radial_iter->radial_next) != l_radial_first);
1299                         }
1300                 } while ((e_iter = bmesh_disk_edge_next(e_iter, v)) != e_first);
1301         }
1302 }
1303
1304 void BM_edge_hide_set(BMEdge *e, const bool hide)
1305 {
1306         BLI_assert(e->head.htype == BM_EDGE);
1307         if (hide) {
1308                 BLI_assert(!BM_elem_flag_test(e, BM_ELEM_SELECT));
1309         }
1310
1311         /* edge hiding: faces around the edge */
1312         if (e->l) {
1313                 const BMLoop *l_iter, *l_first;
1314                 l_iter = l_first = e->l;
1315                 do {
1316                         BM_elem_flag_set(l_iter->f, BM_ELEM_HIDDEN, hide);
1317                 } while ((l_iter = l_iter->radial_next) != l_first);
1318         }
1319
1320         BM_elem_flag_set(e, BM_ELEM_HIDDEN, hide);
1321
1322         /* hide vertices if necessary */
1323         if (hide) {
1324                 vert_flush_hide_set(e->v1);
1325                 vert_flush_hide_set(e->v2);
1326         }
1327         else {
1328                 BM_elem_flag_disable(e->v1, BM_ELEM_HIDDEN);
1329                 BM_elem_flag_disable(e->v2, BM_ELEM_HIDDEN);
1330         }
1331 }
1332
1333 void BM_face_hide_set(BMFace *f, const bool hide)
1334 {
1335         BLI_assert(f->head.htype == BM_FACE);
1336         if (hide) {
1337                 BLI_assert(!BM_elem_flag_test(f, BM_ELEM_SELECT));
1338         }
1339
1340         BM_elem_flag_set(f, BM_ELEM_HIDDEN, hide);
1341
1342         if (hide) {
1343                 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
1344                 BMLoop *l_iter;
1345
1346                 l_iter = l_first;
1347                 do {
1348                         edge_flush_hide_set(l_iter->e);
1349                 } while ((l_iter = l_iter->next) != l_first);
1350
1351                 l_iter = l_first;
1352                 do {
1353                         vert_flush_hide_set(l_iter->v);
1354                 } while ((l_iter = l_iter->next) != l_first);
1355         }
1356         else {
1357                 BMLoop *l_first = BM_FACE_FIRST_LOOP(f);
1358                 BMLoop *l_iter;
1359
1360                 l_iter = l_first;
1361                 do {
1362                         BM_elem_flag_disable(l_iter->e, BM_ELEM_HIDDEN);
1363                         BM_elem_flag_disable(l_iter->v, BM_ELEM_HIDDEN);
1364                 } while ((l_iter = l_iter->next) != l_first);
1365         }
1366 }
1367
1368 void _bm_elem_hide_set(BMesh *bm, BMHeader *head, const bool hide)
1369 {
1370         /* Follow convention of always deselecting before
1371          * hiding an element */
1372         switch (head->htype) {
1373                 case BM_VERT:
1374                         if (hide) BM_vert_select_set(bm, (BMVert *)head, false);
1375                         BM_vert_hide_set((BMVert *)head, hide);
1376                         break;
1377                 case BM_EDGE:
1378                         if (hide) BM_edge_select_set(bm, (BMEdge *)head, false);
1379                         BM_edge_hide_set((BMEdge *)head, hide);
1380                         break;
1381                 case BM_FACE:
1382                         if (hide) BM_face_select_set(bm, (BMFace *)head, false);
1383                         BM_face_hide_set((BMFace *)head, hide);
1384                         break;
1385                 default:
1386                         BMESH_ASSERT(0);
1387                         break;
1388         }
1389 }