knife tool alpha values where being set to zero (intersection points weren't visible...
[blender.git] / source / blender / bmesh / intern / bmesh_core.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Joseph Eagar, Geoffrey Bantle, Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/intern/bmesh_core.c
24  *  \ingroup bmesh
25  *
26  */
27
28 #include "MEM_guardedalloc.h"
29
30 #include "BLI_math_vector.h"
31
32 #include "BKE_DerivedMesh.h"
33
34 #include "BLI_listbase.h"
35 #include "BLI_array.h"
36
37 #include "bmesh.h"
38 #include "intern/bmesh_private.h"
39
40 /* use so valgrinds memcheck alerts us when undefined index is used.
41  * TESTING ONLY! */
42 // #define USE_DEBUG_INDEX_MEMCHECK
43
44 #ifdef USE_DEBUG_INDEX_MEMCHECK
45 #define DEBUG_MEMCHECK_INDEX_INVALIDATE(ele)               \
46         {                                                      \
47                 int undef_idx;                                     \
48                 BM_elem_index_set(ele, undef_idx); /* set_ok_invalid */  \
49         }                                                      \
50
51 #endif
52
53 BMVert *BM_vert_create(BMesh *bm, const float co[3], const BMVert *example)
54 {
55         BMVert *v = BLI_mempool_calloc(bm->vpool);
56
57 #ifdef USE_DEBUG_INDEX_MEMCHECK
58         DEBUG_MEMCHECK_INDEX_INVALIDATE(v)
59 #else
60         BM_elem_index_set(v, -1); /* set_ok_invalid */
61 #endif
62
63         bm->elem_index_dirty |= BM_VERT; /* may add to middle of the pool */
64
65         bm->totvert++;
66
67         v->head.htype = BM_VERT;
68
69         /* 'v->no' is handled by BM_elem_attrs_copy */
70         if (co) {
71                 copy_v3_v3(v->co, co);
72         }
73
74         /* allocate flag */
75         v->oflags = BLI_mempool_calloc(bm->toolflagpool);
76
77         CustomData_bmesh_set_default(&bm->vdata, &v->head.data);
78         
79         if (example) {
80                 BM_elem_attrs_copy(bm, bm, example, v);
81         }
82
83         BM_CHECK_ELEMENT(v);
84
85         return v;
86 }
87
88 BMEdge *BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *example, int nodouble)
89 {
90         BMEdge *e;
91         
92         if (nodouble && (e = BM_edge_exists(v1, v2)))
93                 return e;
94         
95         e = BLI_mempool_calloc(bm->epool);
96
97 #ifdef USE_DEBUG_INDEX_MEMCHECK
98         DEBUG_MEMCHECK_INDEX_INVALIDATE(e)
99 #else
100         BM_elem_index_set(e, -1); /* set_ok_invalid */
101 #endif
102
103         bm->elem_index_dirty |= BM_EDGE; /* may add to middle of the pool */
104
105         bm->totedge++;
106
107         e->head.htype = BM_EDGE;
108         
109         /* allocate flag */
110         e->oflags = BLI_mempool_calloc(bm->toolflagpool);
111
112         e->v1 = v1;
113         e->v2 = v2;
114         
115         BM_elem_flag_enable(e, BM_ELEM_SMOOTH);
116         
117         CustomData_bmesh_set_default(&bm->edata, &e->head.data);
118         
119         bmesh_disk_edge_append(e, e->v1);
120         bmesh_disk_edge_append(e, e->v2);
121         
122         if (example)
123                 BM_elem_attrs_copy(bm, bm, example, e);
124         
125         BM_CHECK_ELEMENT(e);
126
127         return e;
128 }
129
130 static BMLoop *bm_loop_create(BMesh *bm, BMVert *v, BMEdge *e, BMFace *f, const BMLoop *example)
131 {
132         BMLoop *l = NULL;
133
134         l = BLI_mempool_calloc(bm->lpool);
135         l->next = l->prev = NULL;
136         l->v = v;
137         l->e = e;
138         l->f = f;
139         l->radial_next = l->radial_prev = NULL;
140         l->head.data = NULL;
141         l->head.htype = BM_LOOP;
142
143         bm->totloop++;
144
145         if (example) {
146                 CustomData_bmesh_copy_data(&bm->ldata, &bm->ldata, example->head.data, &l->head.data);
147         }
148         else {
149                 CustomData_bmesh_set_default(&bm->ldata, &l->head.data);
150         }
151
152         return l;
153 }
154
155 static BMLoop *bm_face_boundary_add(BMesh *bm, BMFace *f, BMVert *startv, BMEdge *starte)
156 {
157 #ifdef USE_BMESH_HOLES
158         BMLoopList *lst = BLI_mempool_calloc(bm->looplistpool);
159 #endif
160         BMLoop *l = bm_loop_create(bm, startv, starte, f, NULL);
161         
162         bmesh_radial_append(starte, l);
163
164 #ifdef USE_BMESH_HOLES
165         lst->first = lst->last = l;
166         BLI_addtail(&f->loops, lst);
167 #else
168         f->l_first = l;
169 #endif
170
171         l->f = f;
172         
173         return l;
174 }
175
176 BMFace *BM_face_copy(BMesh *bm, BMFace *f, const short copyverts, const short copyedges)
177 {
178         BMEdge **edges = NULL;
179         BMVert **verts = NULL;
180         BLI_array_staticdeclare(edges, BM_NGON_STACK_SIZE);
181         BLI_array_staticdeclare(verts, BM_NGON_STACK_SIZE);
182         BMLoop *l_iter;
183         BMLoop *l_first;
184         BMLoop *l2;
185         BMFace *f2;
186         int i;
187
188         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
189         do {
190                 if (copyverts) {
191                         BMVert *v = BM_vert_create(bm, l_iter->v->co, l_iter->v);
192                         BLI_array_append(verts, v);
193                 }
194                 else {
195                         BLI_array_append(verts, l_iter->v);
196                 }
197         } while ((l_iter = l_iter->next) != l_first);
198
199         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
200         i = 0;
201         do {
202                 if (copyedges) {
203                         BMEdge *e;
204                         BMVert *v1, *v2;
205                         
206                         if (l_iter->e->v1 == verts[i]) {
207                                 v1 = verts[i];
208                                 v2 = verts[(i + 1) % f->len];
209                         }
210                         else {
211                                 v2 = verts[i];
212                                 v1 = verts[(i + 1) % f->len];
213                         }
214                         
215                         e = BM_edge_create(bm,  v1, v2, l_iter->e, FALSE);
216                         BLI_array_append(edges, e);
217                 }
218                 else {
219                         BLI_array_append(edges, l_iter->e);
220                 }
221                 
222                 i++;
223         } while ((l_iter = l_iter->next) != l_first);
224         
225         f2 = BM_face_create(bm, verts, edges, f->len, FALSE);
226         
227         BM_elem_attrs_copy(bm, bm, f, f2);
228         
229         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
230         l2 = BM_FACE_FIRST_LOOP(f2);
231         do {
232                 BM_elem_attrs_copy(bm, bm, l_iter, l2);
233                 l2 = l2->next;
234         } while ((l_iter = l_iter->next) != l_first);
235         
236         return f2;
237 }
238
239 /**
240  * only create the face, since this calloc's the length is initialized to 0,
241  * leave adding loops to the caller.
242  */
243 BLI_INLINE BMFace *bm_face_create__internal(BMesh *bm)
244 {
245         BMFace *f;
246
247         f = BLI_mempool_calloc(bm->fpool);
248
249 #ifdef USE_DEBUG_INDEX_MEMCHECK
250         DEBUG_MEMCHECK_INDEX_INVALIDATE(f)
251 #else
252         BM_elem_index_set(f, -1); /* set_ok_invalid */
253 #endif
254
255         bm->elem_index_dirty |= BM_FACE; /* may add to middle of the pool */
256
257         bm->totface++;
258
259         f->head.htype = BM_FACE;
260
261         /* allocate flag */
262         f->oflags = BLI_mempool_calloc(bm->toolflagpool);
263
264         CustomData_bmesh_set_default(&bm->pdata, &f->head.data);
265
266 #ifdef USE_BMESH_HOLES
267         f->totbounds = 0;
268 #endif
269
270         return f;
271 }
272
273 BMFace *BM_face_create(BMesh *bm, BMVert **verts, BMEdge **edges, const int len, int nodouble)
274 {
275         BMFace *f = NULL;
276         BMLoop *l, *startl, *lastl;
277         int i, overlap;
278         
279         if (len == 0) {
280                 /* just return NULL for no */
281                 return NULL;
282         }
283
284         if (nodouble) {
285                 /* Check if face already exists */
286                 overlap = BM_face_exists(bm, verts, len, &f);
287                 if (overlap) {
288                         return f;
289                 }
290                 else {
291                         BLI_assert(f == NULL);
292                 }
293         }
294
295         f = bm_face_create__internal(bm);
296
297         startl = lastl = bm_face_boundary_add(bm, f, verts[0], edges[0]);
298         
299         startl->v = verts[0];
300         startl->e = edges[0];
301         for (i = 1; i < len; i++) {
302                 l = bm_loop_create(bm, verts[i], edges[i], f, edges[i]->l);
303                 
304                 l->f = f;
305                 bmesh_radial_append(edges[i], l);
306
307                 l->prev = lastl;
308                 lastl->next = l;
309                 lastl = l;
310         }
311         
312         startl->prev = lastl;
313         lastl->next = startl;
314         
315         f->len = len;
316         
317         BM_CHECK_ELEMENT(f);
318
319         return f;
320 }
321
322 int bmesh_elem_check(void *element, const char htype)
323 {
324         BMHeader *head = element;
325         int err = 0;
326
327         if (!element)
328                 return 1;
329
330         if (head->htype != htype)
331                 return 2;
332         
333         switch (htype) {
334                 case BM_VERT: {
335                         BMVert *v = element;
336                         if (v->e && v->e->head.htype != BM_EDGE) {
337                                 err |= 4;
338                         }
339                         break;
340                 }
341                 case BM_EDGE: {
342                         BMEdge *e = element;
343                         if (e->l && e->l->head.htype != BM_LOOP)
344                                 err |= 8;
345                         if (e->l && e->l->f->head.htype != BM_FACE)
346                                 err |= 16;
347                         if (e->v1_disk_link.prev == NULL ||
348                             e->v2_disk_link.prev == NULL ||
349                             e->v1_disk_link.next == NULL ||
350                             e->v2_disk_link.next == NULL)
351                         {
352                                 err |= 32;
353                         }
354                         if (e->l && (e->l->radial_next == NULL || e->l->radial_prev == NULL))
355                                 err |= 64;
356                         if (e->l && e->l->f->len <= 0)
357                                 err |= 128;
358                         break;
359                 }
360                 case BM_LOOP: {
361                         BMLoop *l = element, *l2;
362                         int i;
363
364                         if (l->f->head.htype != BM_FACE)
365                                 err |= 256;
366                         if (l->e->head.htype != BM_EDGE)
367                                 err |= 512;
368                         if (l->v->head.htype !=  BM_VERT)
369                                 err |= 1024;
370                         if (!BM_vert_in_edge(l->e, l->v)) {
371                                 fprintf(stderr, "%s: fatal bmesh error (vert not in edge)! (bmesh internal error)\n", __func__);
372                                 err |= 2048;
373                         }
374
375                         if (l->radial_next == NULL || l->radial_prev == NULL)
376                                 err |= (1 << 12);
377                         if (l->f->len <= 0)
378                                 err |= (1 << 13);
379
380                         /* validate boundary loop -- invalid for hole loops, of course,
381                          * but we won't be allowing those for a while yet */
382                         l2 = l;
383                         i = 0;
384                         do {
385                                 if (i >= BM_NGON_MAX) {
386                                         break;
387                                 }
388
389                                 i++;
390                         } while ((l2 = l2->next) != l);
391
392                         if (i != l->f->len || l2 != l)
393                                 err |= (1 << 14);
394
395                         if (!bmesh_radial_validate(bmesh_radial_length(l), l))
396                                 err |= (1 << 15);
397
398                         break;
399                 }
400                 case BM_FACE: {
401                         BMFace *f = element;
402                         BMLoop *l_iter;
403                         BMLoop *l_first;
404                         int len = 0;
405
406 #ifdef USE_BMESH_HOLES
407                         if (!f->loops.first)
408 #else
409                         if (!f->l_first)
410 #endif
411                         {
412                                 err |= (1 << 16);
413                         }
414                         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
415                         do {
416                                 if (l_iter->f != f) {
417                                         fprintf(stderr, "%s: loop inside one face points to another! (bmesh internal error)\n", __func__);
418                                         err |= (1 << 17);
419                                 }
420
421                                 if (!l_iter->e)
422                                         err |= (1 << 18);
423                                 if (!l_iter->v)
424                                         err |= (1 << 19);
425                                 if (!BM_vert_in_edge(l_iter->e, l_iter->v) || !BM_vert_in_edge(l_iter->e, l_iter->next->v)) {
426                                         err |= (1 << 20);
427                                 }
428
429                                 if (!bmesh_radial_validate(bmesh_radial_length(l_iter), l_iter))
430                                         err |= (1 << 21);
431
432                                 if (!bmesh_disk_count(l_iter->v) || !bmesh_disk_count(l_iter->next->v))
433                                         err |= (1 << 22);
434
435                                 len++;
436                         } while ((l_iter = l_iter->next) != l_first);
437
438                         if (len != f->len)
439                                 err |= (1 << 23);
440                 }
441         }
442
443         BMESH_ASSERT(err == 0);
444
445         return err;
446 }
447
448 /**
449  * low level function, only free's,
450  * does not change adjust surrounding geometry */
451 static void bm_kill_only_vert(BMesh *bm, BMVert *v)
452 {
453         bm->totvert--;
454         bm->elem_index_dirty |= BM_VERT;
455
456         BM_select_history_remove(bm, (BMElem *)v);
457         if (v->head.data)
458                 CustomData_bmesh_free_block(&bm->vdata, &v->head.data);
459
460         BLI_mempool_free(bm->toolflagpool, v->oflags);
461         BLI_mempool_free(bm->vpool, v);
462 }
463
464 static void bm_kill_only_edge(BMesh *bm, BMEdge *e)
465 {
466         bm->totedge--;
467         bm->elem_index_dirty |= BM_EDGE;
468
469         BM_select_history_remove(bm, (BMElem *)e);
470
471         if (e->head.data)
472                 CustomData_bmesh_free_block(&bm->edata, &e->head.data);
473
474         BLI_mempool_free(bm->toolflagpool, e->oflags);
475         BLI_mempool_free(bm->epool, e);
476 }
477
478 static void bm_kill_only_face(BMesh *bm, BMFace *f)
479 {
480         if (bm->act_face == f)
481                 bm->act_face = NULL;
482
483         bm->totface--;
484         bm->elem_index_dirty |= BM_FACE;
485
486         BM_select_history_remove(bm, (BMElem *)f);
487
488         if (f->head.data)
489                 CustomData_bmesh_free_block(&bm->pdata, &f->head.data);
490
491         BLI_mempool_free(bm->toolflagpool, f->oflags);
492         BLI_mempool_free(bm->fpool, f);
493 }
494
495 static void bm_kill_only_loop(BMesh *bm, BMLoop *l)
496 {
497         bm->totloop--;
498         if (l->head.data)
499                 CustomData_bmesh_free_block(&bm->ldata, &l->head.data);
500
501         BLI_mempool_free(bm->lpool, l);
502 }
503
504 /**
505  * kills all edges associated with f, along with any other faces containing
506  * those edges
507  */
508 void BM_face_edges_kill(BMesh *bm, BMFace *f)
509 {
510         BMEdge **edges = NULL;
511         BLI_array_staticdeclare(edges, BM_NGON_STACK_SIZE);
512         BMLoop *l_iter;
513         BMLoop *l_first;
514         int i;
515         
516         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
517         do {
518                 BLI_array_append(edges, l_iter->e);
519         } while ((l_iter = l_iter->next) != l_first);
520         
521         for (i = 0; i < BLI_array_count(edges); i++) {
522                 BM_edge_kill(bm, edges[i]);
523         }
524         
525         BLI_array_free(edges);
526 }
527
528 /**
529  * kills all verts associated with f, along with any other faces containing
530  * those vertices
531  */
532 void BM_face_verts_kill(BMesh *bm, BMFace *f)
533 {
534         BMVert **verts = NULL;
535         BLI_array_staticdeclare(verts, BM_NGON_STACK_SIZE);
536         BMLoop *l_iter;
537         BMLoop *l_first;
538         int i;
539         
540         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
541         do {
542                 BLI_array_append(verts, l_iter->v);
543         } while ((l_iter = l_iter->next) != l_first);
544         
545         for (i = 0; i < BLI_array_count(verts); i++) {
546                 BM_vert_kill(bm, verts[i]);
547         }
548         
549         BLI_array_free(verts);
550 }
551
552 void BM_face_kill(BMesh *bm, BMFace *f)
553 {
554 #ifdef USE_BMESH_HOLES
555         BMLoopList *ls, *ls_next;
556 #endif
557
558         BM_CHECK_ELEMENT(f);
559
560 #ifdef USE_BMESH_HOLES
561         for (ls = f->loops.first; ls; ls = ls_next)
562 #else
563         if (f->l_first)
564 #endif
565         {
566                 BMLoop *l_iter, *l_next, *l_first;
567
568 #ifdef USE_BMESH_HOLES
569                 ls_next = ls->next;
570                 l_iter = l_first = ls->first;
571 #else
572                 l_iter = l_first = f->l_first;
573 #endif
574
575                 do {
576                         l_next = l_iter->next;
577
578                         bmesh_radial_loop_remove(l_iter, l_iter->e);
579                         bm_kill_only_loop(bm, l_iter);
580
581                 } while ((l_iter = l_next) != l_first);
582
583 #ifdef USE_BMESH_HOLES
584                 BLI_mempool_free(bm->looplistpool, ls);
585 #endif
586         }
587
588         bm_kill_only_face(bm, f);
589 }
590
591 void BM_edge_kill(BMesh *bm, BMEdge *e)
592 {
593
594         bmesh_disk_edge_remove(e, e->v1);
595         bmesh_disk_edge_remove(e, e->v2);
596
597         if (e->l) {
598                 BMLoop *l = e->l, *lnext, *startl = e->l;
599
600                 do {
601                         lnext = l->radial_next;
602                         if (lnext->f == l->f) {
603                                 BM_face_kill(bm, l->f);
604                                 break;
605                         }
606                         
607                         BM_face_kill(bm, l->f);
608
609                         if (l == lnext)
610                                 break;
611                         l = lnext;
612                 } while (l != startl);
613         }
614         
615         bm_kill_only_edge(bm, e);
616 }
617
618 void BM_vert_kill(BMesh *bm, BMVert *v)
619 {
620         if (v->e) {
621                 BMEdge *e, *nexte;
622                 
623                 e = v->e;
624                 while (v->e) {
625                         nexte = bmesh_disk_edge_next(e, v);
626                         BM_edge_kill(bm, e);
627                         e = nexte;
628                 }
629         }
630
631         bm_kill_only_vert(bm, v);
632 }
633
634 /********** private disk and radial cycle functions ********** */
635
636 static int bm_loop_length(BMLoop *l)
637 {
638         BMLoop *l_first = l;
639         int i = 0;
640
641         do {
642                 i++;
643         } while ((l = l->next) != l_first);
644
645         return i;
646 }
647
648 /**
649  * \brief Loop Reverse
650  *
651  * Changes the winding order of a face from CW to CCW or vice versa.
652  * This euler is a bit peculiar in comparison to others as it is its
653  * own inverse.
654  *
655  * BMESH_TODO: reinsert validation code.
656  *
657  * \return Success
658  */
659 static int bm_loop_reverse_loop(BMesh *bm, BMFace *f
660 #ifdef USE_BMESH_HOLES
661                                 , BMLoopList *lst
662 #endif
663                                 )
664 {
665
666 #ifdef USE_BMESH_HOLES
667         BMLoop *l_first = lst->first;
668 #else
669         BMLoop *l_first = f->l_first;
670 #endif
671
672         BMLoop *l_iter, *oldprev, *oldnext;
673         BMEdge **edar = NULL;
674         MDisps *md;
675         BLI_array_staticdeclare(edar, BM_NGON_STACK_SIZE);
676         int i, j, edok, len = 0, do_disps = CustomData_has_layer(&bm->ldata, CD_MDISPS);
677
678         len = bm_loop_length(l_first);
679
680         for (i = 0, l_iter = l_first; i < len; i++, l_iter = l_iter->next) {
681                 BMEdge *curedge = l_iter->e;
682                 bmesh_radial_loop_remove(l_iter, curedge);
683                 BLI_array_append(edar, curedge);
684         }
685
686         /* actually reverse the loop */
687         for (i = 0, l_iter = l_first; i < len; i++) {
688                 oldnext = l_iter->next;
689                 oldprev = l_iter->prev;
690                 l_iter->next = oldprev;
691                 l_iter->prev = oldnext;
692                 l_iter = oldnext;
693                 
694                 if (do_disps) {
695                         float (*co)[3];
696                         int x, y, sides;
697                         
698                         md = CustomData_bmesh_get(&bm->ldata, l_iter->head.data, CD_MDISPS);
699                         if (!md->totdisp || !md->disps)
700                                 continue;
701
702                         sides = (int)sqrt(md->totdisp);
703                         co = md->disps;
704                         
705                         for (x = 0; x < sides; x++) {
706                                 for (y = 0; y < x; y++) {
707                                         swap_v3_v3(co[y * sides + x], co[sides * x + y]);
708                                 }
709                         }
710                 }
711         }
712
713         if (len == 2) { /* two edged face */
714                 /* do some verification here! */
715                 l_first->e = edar[1];
716                 l_first->next->e = edar[0];
717         }
718         else {
719                 for (i = 0, l_iter = l_first; i < len; i++, l_iter = l_iter->next) {
720                         edok = 0;
721                         for (j = 0; j < len; j++) {
722                                 edok = bmesh_verts_in_edge(l_iter->v, l_iter->next->v, edar[j]);
723                                 if (edok) {
724                                         l_iter->e = edar[j];
725                                         break;
726                                 }
727                         }
728                 }
729         }
730         /* rebuild radia */
731         for (i = 0, l_iter = l_first; i < len; i++, l_iter = l_iter->next)
732                 bmesh_radial_append(l_iter->e, l_iter);
733
734         /* validate radia */
735         for (i = 0, l_iter = l_first; i < len; i++, l_iter = l_iter->next) {
736                 BM_CHECK_ELEMENT(l_iter);
737                 BM_CHECK_ELEMENT(l_iter->e);
738                 BM_CHECK_ELEMENT(l_iter->v);
739                 BM_CHECK_ELEMENT(l_iter->f);
740         }
741
742         BLI_array_free(edar);
743
744         BM_CHECK_ELEMENT(f);
745
746         return 1;
747 }
748
749 int bmesh_loop_reverse(BMesh *bm, BMFace *f)
750 {
751 #ifdef USE_BMESH_HOLES
752         return bmesh_loop_reverse_loop(bm, f, f->loops.first);
753 #else
754         return bm_loop_reverse_loop(bm, f);
755 #endif
756 }
757
758 static void bm_elements_systag_enable(void *veles, int tot, int flag)
759 {
760         BMHeader **eles = veles;
761         int i;
762
763         for (i = 0; i < tot; i++) {
764                 BM_ELEM_API_FLAG_ENABLE((BMElemF *)eles[i], flag);
765         }
766 }
767
768 static void bm_elements_systag_disable(void *veles, int tot, int flag)
769 {
770         BMHeader **eles = veles;
771         int i;
772
773         for (i = 0; i < tot; i++) {
774                 BM_ELEM_API_FLAG_DISABLE((BMElemF *)eles[i], flag);
775         }
776 }
777
778 #define FACE_MARK  (1 << 10)
779
780 static int count_flagged_radial(BMesh *bm, BMLoop *l, int flag)
781 {
782         BMLoop *l2 = l;
783         int i = 0, c = 0;
784
785         do {
786                 if (UNLIKELY(!l2)) {
787                         BMESH_ASSERT(0);
788                         goto error;
789                 }
790                 
791                 i += BM_ELEM_API_FLAG_TEST(l2->f, flag) ? 1 : 0;
792                 l2 = l2->radial_next;
793                 if (UNLIKELY(c >= BM_LOOP_RADIAL_MAX)) {
794                         BMESH_ASSERT(0);
795                         goto error;
796                 }
797                 c++;
798         } while (l2 != l);
799
800         return i;
801
802 error:
803         BMO_error_raise(bm, bm->currentop, BMERR_MESH_ERROR, NULL);
804         return 0;
805 }
806
807 static int UNUSED_FUNCTION(count_flagged_disk)(BMVert *v, int flag)
808 {
809         BMEdge *e = v->e;
810         int i = 0;
811
812         if (!e)
813                 return 0;
814
815         do {
816                 i += BM_ELEM_API_FLAG_TEST(e, flag) ? 1 : 0;
817         } while ((e = bmesh_disk_edge_next(e, v)) != v->e);
818
819         return i;
820 }
821
822 static int disk_is_flagged(BMVert *v, int flag)
823 {
824         BMEdge *e = v->e;
825
826         if (!e)
827                 return FALSE;
828
829         do {
830                 BMLoop *l = e->l;
831
832                 if (!l) {
833                         return FALSE;
834                 }
835                 
836                 if (bmesh_radial_length(l) == 1)
837                         return FALSE;
838                 
839                 do {
840                         if (!BM_ELEM_API_FLAG_TEST(l->f, flag))
841                                 return FALSE;
842
843                         l = l->radial_next;
844                 } while (l != e->l);
845
846                 e = bmesh_disk_edge_next(e, v);
847         } while (e != v->e);
848
849         return TRUE;
850 }
851
852 /* Mid-level Topology Manipulation Functions */
853
854 /**
855  * \brief Join Connected Faces
856  *
857  * Joins a collected group of faces into one. Only restriction on
858  * the input data is that the faces must be connected to each other.
859  *
860  * \return The newly created combine BMFace.
861  *
862  * \note If a pair of faces share multiple edges,
863  * the pair of faces will be joined at every edge.
864  *
865  * \note this is a generic, flexible join faces function,
866  * almost everything uses this, including #BM_faces_join_pair
867  */
868 BMFace *BM_faces_join(BMesh *bm, BMFace **faces, int totface, const short do_del)
869 {
870         BMFace *f, *newf;
871 #ifdef USE_BMESH_HOLES
872         BMLoopList *lst;
873         ListBase holes = {NULL, NULL};
874 #endif
875         BMLoop *l_iter;
876         BMLoop *l_first;
877         BMEdge **edges = NULL;
878         BMEdge **deledges = NULL;
879         BMVert **delverts = NULL;
880         BLI_array_staticdeclare(edges,    BM_NGON_STACK_SIZE);
881         BLI_array_staticdeclare(deledges, BM_NGON_STACK_SIZE);
882         BLI_array_staticdeclare(delverts, BM_NGON_STACK_SIZE);
883         BMVert *v1 = NULL, *v2 = NULL;
884         const char *err = NULL;
885         int i, tote = 0;
886
887         if (UNLIKELY(!totface)) {
888                 BMESH_ASSERT(0);
889                 return NULL;
890         }
891
892         if (totface == 1)
893                 return faces[0];
894
895         bm_elements_systag_enable(faces, totface, _FLAG_JF);
896
897         for (i = 0; i < totface; i++) {
898                 f = faces[i];
899                 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
900                 do {
901                         int rlen = count_flagged_radial(bm, l_iter, _FLAG_JF);
902
903                         if (rlen > 2) {
904                                 err = "Input faces do not form a contiguous manifold region";
905                                 goto error;
906                         }
907                         else if (rlen == 1) {
908                                 BLI_array_append(edges, l_iter->e);
909
910                                 if (!v1) {
911                                         v1 = l_iter->v;
912                                         v2 = BM_edge_other_vert(l_iter->e, l_iter->v);
913                                 }
914                                 tote++;
915                         }
916                         else if (rlen == 2) {
917                                 int d1, d2;
918
919                                 d1 = disk_is_flagged(l_iter->e->v1, _FLAG_JF);
920                                 d2 = disk_is_flagged(l_iter->e->v2, _FLAG_JF);
921
922                                 if (!d1 && !d2 && !BM_ELEM_API_FLAG_TEST(l_iter->e, _FLAG_JF)) {
923                                         /* don't remove an edge it makes up the side of another face
924                                          * else this will remove the face as well - campbell */
925                                         if (BM_edge_face_count(l_iter->e) <= 2) {
926                                                 if (do_del) {
927                                                         BLI_array_append(deledges, l_iter->e);
928                                                 }
929                                                 BM_ELEM_API_FLAG_ENABLE(l_iter->e, _FLAG_JF);
930                                         }
931                                 }
932                                 else {
933                                         if (d1 && !BM_ELEM_API_FLAG_TEST(l_iter->e->v1, _FLAG_JF)) {
934                                                 if (do_del) {
935                                                         BLI_array_append(delverts, l_iter->e->v1);
936                                                 }
937                                                 BM_ELEM_API_FLAG_ENABLE(l_iter->e->v1, _FLAG_JF);
938                                         }
939
940                                         if (d2 && !BM_ELEM_API_FLAG_TEST(l_iter->e->v2, _FLAG_JF)) {
941                                                 if (do_del) {
942                                                         BLI_array_append(delverts, l_iter->e->v2);
943                                                 }
944                                                 BM_ELEM_API_FLAG_ENABLE(l_iter->e->v2, _FLAG_JF);
945                                         }
946                                 }
947                         }
948                 } while ((l_iter = l_iter->next) != l_first);
949
950 #ifdef USE_BMESH_HOLES
951                 for (lst = f->loops.first; lst; lst = lst->next) {
952                         if (lst == f->loops.first) {
953                                 continue;
954                         }
955
956                         BLI_remlink(&f->loops, lst);
957                         BLI_addtail(&holes, lst);
958                 }
959 #endif
960
961         }
962
963         /* create region face */
964         newf = BM_face_create_ngon(bm, v1, v2, edges, tote, FALSE);
965         if (!newf || BMO_error_occurred(bm)) {
966                 if (!BMO_error_occurred(bm))
967                         err = "Invalid boundary region to join faces";
968                 goto error;
969         }
970
971         /* copy over loop data */
972         l_iter = l_first = BM_FACE_FIRST_LOOP(newf);
973         do {
974                 BMLoop *l2 = l_iter->radial_next;
975
976                 do {
977                         if (BM_ELEM_API_FLAG_TEST(l2->f, _FLAG_JF))
978                                 break;
979                         l2 = l2->radial_next;
980                 } while (l2 != l_iter);
981
982                 if (l2 != l_iter) {
983                         /* I think this is correct */
984                         if (l2->v != l_iter->v) {
985                                 l2 = l2->next;
986                         }
987
988                         BM_elem_attrs_copy(bm, bm, l2, l_iter);
989                 }
990         } while ((l_iter = l_iter->next) != l_first);
991         
992         BM_elem_attrs_copy(bm, bm, faces[0], newf);
993
994 #ifdef USE_BMESH_HOLES
995         /* add hole */
996         BLI_movelisttolist(&newf->loops, &holes);
997 #endif
998
999         /* update loop face pointer */
1000 #ifdef USE_BMESH_HOLES
1001         for (lst = newf->loops.first; lst; lst = lst->next)
1002 #endif
1003         {
1004 #ifdef USE_BMESH_HOLES
1005                 l_iter = l_first = lst->first;
1006 #else
1007                 l_iter = l_first = BM_FACE_FIRST_LOOP(newf);
1008 #endif
1009                 do {
1010                         l_iter->f = newf;
1011                 } while ((l_iter = l_iter->next) != l_first);
1012         }
1013
1014         bm_elements_systag_disable(faces, totface, _FLAG_JF);
1015         BM_ELEM_API_FLAG_DISABLE(newf, _FLAG_JF);
1016
1017         /* handle multi-res data */
1018         if (CustomData_has_layer(&bm->ldata, CD_MDISPS)) {
1019                 l_iter = l_first = BM_FACE_FIRST_LOOP(newf);
1020                 do {
1021                         for (i = 0; i < totface; i++) {
1022                                 BM_loop_interp_multires(bm, l_iter, faces[i]);
1023                         }
1024                 } while ((l_iter = l_iter->next) != l_first);
1025         }
1026
1027         /* delete old geometry */
1028         if (do_del) {
1029                 for (i = 0; i < BLI_array_count(deledges); i++) {
1030                         BM_edge_kill(bm, deledges[i]);
1031                 }
1032
1033                 for (i = 0; i < BLI_array_count(delverts); i++) {
1034                         BM_vert_kill(bm, delverts[i]);
1035                 }
1036         }
1037         else {
1038                 /* otherwise we get both old and new faces */
1039                 for (i = 0; i < totface; i++) {
1040                         BM_face_kill(bm, faces[i]);
1041                 }
1042         }
1043         
1044         BLI_array_free(edges);
1045         BLI_array_free(deledges);
1046         BLI_array_free(delverts);
1047
1048         BM_CHECK_ELEMENT(newf);
1049         return newf;
1050
1051 error:
1052         bm_elements_systag_disable(faces, totface, _FLAG_JF);
1053         BLI_array_free(edges);
1054         BLI_array_free(deledges);
1055         BLI_array_free(delverts);
1056
1057         if (err) {
1058                 BMO_error_raise(bm, bm->currentop, BMERR_DISSOLVEFACES_FAILED, err);
1059         }
1060         return NULL;
1061 }
1062
1063 static BMFace *bm_face_create__sfme(BMesh *bm, BMFace *UNUSED(example))
1064 {
1065         BMFace *f;
1066 #ifdef USE_BMESH_HOLES
1067         BMLoopList *lst;
1068 #endif
1069
1070         f = bm_face_create__internal(bm);
1071
1072 #ifdef USE_BMESH_HOLES
1073         lst = BLI_mempool_calloc(bm->looplistpool);
1074         BLI_addtail(&f->loops, lst);
1075 #endif
1076
1077 #ifdef USE_BMESH_HOLES
1078         f->totbounds = 1;
1079 #endif
1080
1081         return f;
1082 }
1083
1084 /**
1085  * \brief Split Face Make Edge (SFME)
1086  *
1087  * Takes as input two vertices in a single face. An edge is created which divides the original face
1088  * into two distinct regions. One of the regions is assigned to the original face and it is closed off.
1089  * The second region has a new face assigned to it.
1090  *
1091  * \par Examples:
1092  *
1093  *     Before:               After:
1094  *      +--------+           +--------+
1095  *      |        |           |        |
1096  *      |        |           |   f1   |
1097  *     v1   f1   v2          v1======v2
1098  *      |        |           |   f2   |
1099  *      |        |           |        |
1100  *      +--------+           +--------+
1101  *
1102  * \note the input vertices can be part of the same edge. This will
1103  * result in a two edged face. This is desirable for advanced construction
1104  * tools and particularly essential for edge bevel. Because of this it is
1105  * up to the caller to decide what to do with the extra edge.
1106  *
1107  * \note If \a holes is NULL, then both faces will lose
1108  * all holes from the original face.  Also, you cannot split between
1109  * a hole vert and a boundary vert; that case is handled by higher-
1110  * level wrapping functions (when holes are fully implemented, anyway).
1111  *
1112  * \note that holes represents which holes goes to the new face, and of
1113  * course this requires removing them from the existing face first, since
1114  * you cannot have linked list links inside multiple lists.
1115  *
1116  * \return A BMFace pointer
1117  */
1118 BMFace *bmesh_sfme(BMesh *bm, BMFace *f, BMVert *v1, BMVert *v2,
1119                    BMLoop **r_l,
1120 #ifdef USE_BMESH_HOLES
1121                    ListBase *holes,
1122 #endif
1123                    BMEdge *example,
1124                    const short nodouble
1125                    )
1126 {
1127 #ifdef USE_BMESH_HOLES
1128         BMLoopList *lst, *lst2;
1129 #endif
1130
1131         BMFace *f2;
1132         BMLoop *l_iter, *l_first;
1133         BMLoop *v1loop = NULL, *v2loop = NULL, *f1loop = NULL, *f2loop = NULL;
1134         BMEdge *e;
1135         int i, len, f1len, f2len, first_loop_f1;
1136
1137         /* verify that v1 and v2 are in face */
1138         len = f->len;
1139         for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f); i < len; i++, l_iter = l_iter->next) {
1140                 if (l_iter->v == v1) v1loop = l_iter;
1141                 else if (l_iter->v == v2) v2loop = l_iter;
1142         }
1143
1144         if (!v1loop || !v2loop) {
1145                 return NULL;
1146         }
1147
1148         /* allocate new edge between v1 and v2 */
1149         e = BM_edge_create(bm, v1, v2, example, nodouble);
1150
1151         f2 = bm_face_create__sfme(bm, f);
1152         f1loop = bm_loop_create(bm, v2, e, f, v2loop);
1153         f2loop = bm_loop_create(bm, v1, e, f2, v1loop);
1154
1155         f1loop->prev = v2loop->prev;
1156         f2loop->prev = v1loop->prev;
1157         v2loop->prev->next = f1loop;
1158         v1loop->prev->next = f2loop;
1159
1160         f1loop->next = v1loop;
1161         f2loop->next = v2loop;
1162         v1loop->prev = f1loop;
1163         v2loop->prev = f2loop;
1164
1165 #ifdef USE_BMESH_HOLES
1166         lst = f->loops.first;
1167         lst2 = f2->loops.first;
1168
1169         lst2->first = lst2->last = f2loop;
1170         lst->first = lst->last = f1loop;
1171 #else
1172         /* find which of the faces the original first loop is in */
1173         l_iter = l_first = f1loop;
1174         first_loop_f1 = 0;
1175         do {
1176                 if (l_iter == f->l_first)
1177                         first_loop_f1 = 1;
1178         } while ((l_iter = l_iter->next) != l_first);
1179
1180         if (first_loop_f1) {
1181                 /* original first loop was in f1, find a suitable first loop for f2
1182                    which is as similar as possible to f1. the order matters for tools
1183                    such as duplifaces. */
1184                 if (f->l_first->prev == f1loop)
1185                         f2->l_first = f2loop->prev;
1186                 else if (f->l_first->next == f1loop)
1187                         f2->l_first = f2loop->next;
1188                 else
1189                         f2->l_first = f2loop;
1190         }
1191         else {
1192                 /* original first loop was in f2, further do same as above */
1193                 f2->l_first = f->l_first;
1194
1195                 if (f->l_first->prev == f2loop)
1196                         f->l_first = f1loop->prev;
1197                 else if (f->l_first->next == f2loop)
1198                         f->l_first = f1loop->next;
1199                 else
1200                         f->l_first = f1loop;
1201         }
1202 #endif
1203
1204         /* validate both loop */
1205         /* I don't know how many loops are supposed to be in each face at this point! FIXME */
1206
1207         /* go through all of f2's loops and make sure they point to it properly */
1208         l_iter = l_first = BM_FACE_FIRST_LOOP(f2);
1209         f2len = 0;
1210         do {
1211                 l_iter->f = f2;
1212                 f2len++;
1213         } while ((l_iter = l_iter->next) != l_first);
1214
1215         /* link up the new loops into the new edges radial */
1216         bmesh_radial_append(e, f1loop);
1217         bmesh_radial_append(e, f2loop);
1218
1219         f2->len = f2len;
1220
1221         f1len = 0;
1222         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1223         do {
1224                 f1len++;
1225         } while ((l_iter = l_iter->next) != l_first);
1226
1227         f->len = f1len;
1228
1229         if (r_l) *r_l = f2loop;
1230
1231 #ifdef USE_BMESH_HOLES
1232         if (holes) {
1233                 BLI_movelisttolist(&f2->loops, holes);
1234         }
1235         else {
1236                 /* this code is not significant until holes actually work */
1237                 //printf("warning: call to split face euler without holes argument; holes will be tossed.\n");
1238                 for (lst = f->loops.last; lst != f->loops.first; lst = lst2) {
1239                         lst2 = lst->prev;
1240                         BLI_mempool_free(bm->looplistpool, lst);
1241                 }
1242         }
1243 #endif
1244
1245         BM_CHECK_ELEMENT(e);
1246         BM_CHECK_ELEMENT(f);
1247         BM_CHECK_ELEMENT(f2);
1248         
1249         return f2;
1250 }
1251
1252 /**
1253  * \brief Split Edge Make Vert (SEMV)
1254  *
1255  * Takes \a e edge and splits it into two, creating a new vert.
1256  * \a tv should be one end of \a e : the newly created edge
1257  * will be attached to that end and is returned in \a r_e.
1258  *
1259  * \par Examples:
1260  *
1261  *                     E
1262  *     Before: OV-------------TV
1263  *
1264  *                 E       RE
1265  *     After:  OV------NV-----TV
1266  *
1267  * \return The newly created BMVert pointer.
1268  */
1269 BMVert *bmesh_semv(BMesh *bm, BMVert *tv, BMEdge *e, BMEdge **r_e)
1270 {
1271         BMLoop *nextl;
1272         BMEdge *ne;
1273         BMVert *nv, *ov;
1274         int i, edok, valence1 = 0, valence2 = 0;
1275
1276         BLI_assert(bmesh_vert_in_edge(e, tv) != FALSE);
1277
1278         ov = bmesh_edge_other_vert_get(e, tv);
1279
1280         valence1 = bmesh_disk_count(ov);
1281
1282         valence2 = bmesh_disk_count(tv);
1283
1284         nv = BM_vert_create(bm, tv->co, tv);
1285         ne = BM_edge_create(bm, nv, tv, e, FALSE);
1286
1287         bmesh_disk_edge_remove(ne, tv);
1288         bmesh_disk_edge_remove(ne, nv);
1289
1290         /* remove e from tv's disk cycle */
1291         bmesh_disk_edge_remove(e, tv);
1292
1293         /* swap out tv for nv in e */
1294         bmesh_edge_swapverts(e, tv, nv);
1295
1296         /* add e to nv's disk cycle */
1297         bmesh_disk_edge_append(e, nv);
1298
1299         /* add ne to nv's disk cycle */
1300         bmesh_disk_edge_append(ne, nv);
1301
1302         /* add ne to tv's disk cycle */
1303         bmesh_disk_edge_append(ne, tv);
1304
1305         /* verify disk cycle */
1306         edok = bmesh_disk_validate(valence1, ov->e, ov);
1307         BMESH_ASSERT(edok != FALSE);
1308         edok = bmesh_disk_validate(valence2, tv->e, tv);
1309         BMESH_ASSERT(edok != FALSE);
1310         edok = bmesh_disk_validate(2, nv->e, nv);
1311         BMESH_ASSERT(edok != FALSE);
1312
1313         /* Split the radial cycle if present */
1314         nextl = e->l;
1315         e->l = NULL;
1316         if (nextl) {
1317                 BMLoop *nl, *l;
1318                 int radlen = bmesh_radial_length(nextl);
1319                 int first1 = 0, first2 = 0;
1320
1321                 /* Take the next loop. Remove it from radial. Split it. Append to appropriate radials */
1322                 while (nextl) {
1323                         l = nextl;
1324                         l->f->len++;
1325                         nextl = nextl != nextl->radial_next ? nextl->radial_next : NULL;
1326                         bmesh_radial_loop_remove(l, NULL);
1327
1328                         nl = bm_loop_create(bm, NULL, NULL, l->f, l);
1329                         nl->prev = l;
1330                         nl->next = (l->next);
1331                         nl->prev->next = nl;
1332                         nl->next->prev = nl;
1333                         nl->v = nv;
1334
1335                         /* assign the correct edge to the correct loop */
1336                         if (bmesh_verts_in_edge(nl->v, nl->next->v, e)) {
1337                                 nl->e = e;
1338                                 l->e = ne;
1339
1340                                 /* append l into ne's rad cycle */
1341                                 if (!first1) {
1342                                         first1 = 1;
1343                                         l->radial_next = l->radial_prev = NULL;
1344                                 }
1345
1346                                 if (!first2) {
1347                                         first2 = 1;
1348                                         l->radial_next = l->radial_prev = NULL;
1349                                 }
1350                                 
1351                                 bmesh_radial_append(nl->e, nl);
1352                                 bmesh_radial_append(l->e, l);
1353                         }
1354                         else if (bmesh_verts_in_edge(nl->v, nl->next->v, ne)) {
1355                                 nl->e = ne;
1356                                 l->e = e;
1357
1358                                 /* append l into ne's rad cycle */
1359                                 if (!first1) {
1360                                         first1 = 1;
1361                                         l->radial_next = l->radial_prev = NULL;
1362                                 }
1363
1364                                 if (!first2) {
1365                                         first2 = 1;
1366                                         l->radial_next = l->radial_prev = NULL;
1367                                 }
1368
1369                                 bmesh_radial_append(nl->e, nl);
1370                                 bmesh_radial_append(l->e, l);
1371                         }
1372
1373                 }
1374
1375                 /* verify length of radial cycle */
1376                 edok = bmesh_radial_validate(radlen, e->l);
1377                 BMESH_ASSERT(edok != FALSE);
1378                 edok = bmesh_radial_validate(radlen, ne->l);
1379                 BMESH_ASSERT(edok != FALSE);
1380
1381                 /* verify loop->v and loop->next->v pointers for e */
1382                 for (i = 0, l = e->l; i < radlen; i++, l = l->radial_next) {
1383                         BMESH_ASSERT(l->e == e);
1384                         //BMESH_ASSERT(l->radial_next == l);
1385                         BMESH_ASSERT(!(l->prev->e != ne && l->next->e != ne));
1386
1387                         edok = bmesh_verts_in_edge(l->v, l->next->v, e);
1388                         BMESH_ASSERT(edok != FALSE);
1389                         BMESH_ASSERT(l->v != l->next->v);
1390                         BMESH_ASSERT(l->e != l->next->e);
1391
1392                         /* verify loop cycle for kloop-> */
1393                         BM_CHECK_ELEMENT(l);
1394                         BM_CHECK_ELEMENT(l->v);
1395                         BM_CHECK_ELEMENT(l->e);
1396                         BM_CHECK_ELEMENT(l->f);
1397                 }
1398                 /* verify loop->v and loop->next->v pointers for ne */
1399                 for (i = 0, l = ne->l; i < radlen; i++, l = l->radial_next) {
1400                         BMESH_ASSERT(l->e == ne);
1401                         // BMESH_ASSERT(l->radial_next == l);
1402                         BMESH_ASSERT(!(l->prev->e != e && l->next->e != e));
1403                         edok = bmesh_verts_in_edge(l->v, l->next->v, ne);
1404                         BMESH_ASSERT(edok != FALSE);
1405                         BMESH_ASSERT(l->v != l->next->v);
1406                         BMESH_ASSERT(l->e != l->next->e);
1407
1408                         BM_CHECK_ELEMENT(l);
1409                         BM_CHECK_ELEMENT(l->v);
1410                         BM_CHECK_ELEMENT(l->e);
1411                         BM_CHECK_ELEMENT(l->f);
1412                 }
1413         }
1414
1415         BM_CHECK_ELEMENT(ne);
1416         BM_CHECK_ELEMENT(nv);
1417         BM_CHECK_ELEMENT(ov);
1418         BM_CHECK_ELEMENT(e);
1419         BM_CHECK_ELEMENT(tv);
1420
1421         if (r_e) *r_e = ne;
1422         return nv;
1423 }
1424
1425 /**
1426  * \brief Join Edge Kill Vert (JEKV)
1427  *
1428  * Takes an edge \a ke and pointer to one of its vertices \a kv
1429  * and collapses the edge on that vertex.
1430  *
1431  * \par Examples:
1432  *
1433  *     Before:         OE      KE
1434  *                   ------- -------
1435  *                   |     ||      |
1436  *                  OV     KV      TV
1437  *
1438  *
1439  *     After:              OE
1440  *                   ---------------
1441  *                   |             |
1442  *                  OV             TV
1443  *
1444  * \par Restrictions:
1445  * KV is a vertex that must have a valance of exactly two. Furthermore
1446  * both edges in KV's disk cycle (OE and KE) must be unique (no double edges).
1447  *
1448  * \return The resulting edge, NULL for failure.
1449  *
1450  * \note This euler has the possibility of creating
1451  * faces with just 2 edges. It is up to the caller to decide what to do with
1452  * these faces.
1453  */
1454 BMEdge *bmesh_jekv(BMesh *bm, BMEdge *ke, BMVert *kv, const short check_edge_double)
1455 {
1456         BMEdge *oe;
1457         BMVert *ov, *tv;
1458         BMLoop *killoop, *l;
1459         int len, radlen = 0, halt = 0, i, valence1, valence2, edok;
1460
1461         if (bmesh_vert_in_edge(ke, kv) == 0) {
1462                 return NULL;
1463         }
1464
1465         len = bmesh_disk_count(kv);
1466         
1467         if (len == 2) {
1468                 oe = bmesh_disk_edge_next(ke, kv);
1469                 tv = bmesh_edge_other_vert_get(ke, kv);
1470                 ov = bmesh_edge_other_vert_get(oe, kv);
1471                 halt = bmesh_verts_in_edge(kv, tv, oe); /* check for double edge */
1472                 
1473                 if (halt) {
1474                         return NULL;
1475                 }
1476                 else {
1477                         BMEdge *e_splice;
1478
1479                         /* For verification later, count valence of ov and t */
1480                         valence1 = bmesh_disk_count(ov);
1481                         valence2 = bmesh_disk_count(tv);
1482
1483                         if (check_edge_double) {
1484                                 e_splice = BM_edge_exists(tv, ov);
1485                         }
1486
1487                         /* remove oe from kv's disk cycle */
1488                         bmesh_disk_edge_remove(oe, kv);
1489                         /* relink oe->kv to be oe->tv */
1490                         bmesh_edge_swapverts(oe, kv, tv);
1491                         /* append oe to tv's disk cycle */
1492                         bmesh_disk_edge_append(oe, tv);
1493                         /* remove ke from tv's disk cycle */
1494                         bmesh_disk_edge_remove(ke, tv);
1495
1496                         /* deal with radial cycle of ke */
1497                         radlen = bmesh_radial_length(ke->l);
1498                         if (ke->l) {
1499                                 /* first step, fix the neighboring loops of all loops in ke's radial cycle */
1500                                 for (i = 0, killoop = ke->l; i < radlen; i++, killoop = killoop->radial_next) {
1501                                         /* relink loops and fix vertex pointer */
1502                                         if (killoop->next->v == kv) {
1503                                                 killoop->next->v = tv;
1504                                         }
1505
1506                                         killoop->next->prev = killoop->prev;
1507                                         killoop->prev->next = killoop->next;
1508                                         if (BM_FACE_FIRST_LOOP(killoop->f) == killoop) {
1509                                                 BM_FACE_FIRST_LOOP(killoop->f) = killoop->next;
1510                                         }
1511                                         killoop->next = NULL;
1512                                         killoop->prev = NULL;
1513
1514                                         /* fix len attribute of face */
1515                                         killoop->f->len--;
1516                                 }
1517                                 /* second step, remove all the hanging loops attached to ke */
1518                                 radlen = bmesh_radial_length(ke->l);
1519
1520                                 if (LIKELY(radlen)) {
1521                                         BMLoop **loops = NULL;
1522                                         BLI_array_fixedstack_declare(loops, BM_NGON_STACK_SIZE, radlen, __func__);
1523
1524                                         killoop = ke->l;
1525
1526                                         /* this should be wrapped into a bme_free_radial function to be used by bmesh_KF as well... */
1527                                         for (i = 0; i < radlen; i++) {
1528                                                 loops[i] = killoop;
1529                                                 killoop = killoop->radial_next;
1530                                         }
1531                                         for (i = 0; i < radlen; i++) {
1532                                                 bm->totloop--;
1533                                                 BLI_mempool_free(bm->lpool, loops[i]);
1534                                         }
1535                                         BLI_array_fixedstack_free(loops);
1536                                 }
1537
1538                                 /* Validate radial cycle of oe */
1539                                 edok = bmesh_radial_validate(radlen, oe->l);
1540                                 BMESH_ASSERT(edok != FALSE);
1541                         }
1542
1543                         /* deallocate edg */
1544                         bm_kill_only_edge(bm, ke);
1545
1546                         /* deallocate verte */
1547                         bm_kill_only_vert(bm, kv);
1548
1549                         /* Validate disk cycle lengths of ov, tv are unchanged */
1550                         edok = bmesh_disk_validate(valence1, ov->e, ov);
1551                         BMESH_ASSERT(edok != FALSE);
1552                         edok = bmesh_disk_validate(valence2, tv->e, tv);
1553                         BMESH_ASSERT(edok != FALSE);
1554
1555                         /* Validate loop cycle of all faces attached to oe */
1556                         for (i = 0, l = oe->l; i < radlen; i++, l = l->radial_next) {
1557                                 BMESH_ASSERT(l->e == oe);
1558                                 edok = bmesh_verts_in_edge(l->v, l->next->v, oe);
1559                                 BMESH_ASSERT(edok != FALSE);
1560                                 edok = bmesh_loop_validate(l->f);
1561                                 BMESH_ASSERT(edok != FALSE);
1562
1563                                 BM_CHECK_ELEMENT(l);
1564                                 BM_CHECK_ELEMENT(l->v);
1565                                 BM_CHECK_ELEMENT(l->e);
1566                                 BM_CHECK_ELEMENT(l->f);
1567                         }
1568
1569                         if (check_edge_double) {
1570                                 if (e_splice) {
1571                                         /* removes e_splice */
1572                                         BM_edge_splice(bm, e_splice, oe);
1573                                 }
1574                         }
1575
1576                         BM_CHECK_ELEMENT(ov);
1577                         BM_CHECK_ELEMENT(tv);
1578                         BM_CHECK_ELEMENT(oe);
1579
1580                         return oe;
1581                 }
1582         }
1583         return NULL;
1584 }
1585
1586 /**
1587  * \brief Join Face Kill Edge (JFKE)
1588  *
1589  * Takes two faces joined by a single 2-manifold edge and fuses them together.
1590  * The edge shared by the faces must not be connected to any other edges which have
1591  * Both faces in its radial cycle
1592  *
1593  * \par Examples:
1594  *
1595  *           A                   B
1596  *      +--------+           +--------+
1597  *      |        |           |        |
1598  *      |   f1   |           |   f1   |
1599  *     v1========v2 = Ok!    v1==V2==v3 == Wrong!
1600  *      |   f2   |           |   f2   |
1601  *      |        |           |        |
1602  *      +--------+           +--------+
1603  *
1604  * In the example A, faces \a f1 and \a f2 are joined by a single edge,
1605  * and the euler can safely be used.
1606  * In example B however, \a f1 and \a f2 are joined by multiple edges and will produce an error.
1607  * The caller in this case should call #bmesh_jekv on the extra edges
1608  * before attempting to fuse \a f1 and \a f2.
1609  *
1610  * \note The order of arguments decides whether or not certain per-face attributes are present
1611  * in the resultant face. For instance vertex winding, material index, smooth flags, etc are inherited
1612  * from \a f1, not \a f2.
1613  *
1614  * \return A BMFace pointer
1615  */
1616 BMFace *bmesh_jfke(BMesh *bm, BMFace *f1, BMFace *f2, BMEdge *e)
1617 {
1618         BMLoop *l_iter, *f1loop = NULL, *f2loop = NULL;
1619         int newlen = 0, i, f1len = 0, f2len = 0, edok;
1620
1621         /* can't join a face to itself */
1622         if (f1 == f2) {
1623                 return NULL;
1624         }
1625
1626         /* validate that edge is 2-manifold edge */
1627         if (!BM_edge_is_manifold(e)) {
1628                 return NULL;
1629         }
1630
1631         /* verify that e is in both f1 and f2 */
1632         f1len = f1->len;
1633         f2len = f2->len;
1634
1635         if (!((f1loop = BM_face_edge_share_loop(f1, e)) &&
1636               (f2loop = BM_face_edge_share_loop(f2, e))))
1637         {
1638                 return NULL;
1639         }
1640
1641         /* validate direction of f2's loop cycle is compatible */
1642         if (f1loop->v == f2loop->v) {
1643                 return NULL;
1644         }
1645
1646         /* validate that for each face, each vertex has another edge in its disk cycle that is
1647          * not e, and not shared. */
1648         if (bmesh_radial_face_find(f1loop->next->e, f2) ||
1649             bmesh_radial_face_find(f1loop->prev->e, f2) ||
1650             bmesh_radial_face_find(f2loop->next->e, f1) ||
1651             bmesh_radial_face_find(f2loop->prev->e, f1) )
1652         {
1653                 return NULL;
1654         }
1655
1656         /* validate only one shared edge */
1657         if (BM_face_share_edge_count(f1, f2) > 1) {
1658                 return NULL;
1659         }
1660
1661         /* validate no internal join */
1662         for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f1); i < f1len; i++, l_iter = l_iter->next) {
1663                 BM_elem_flag_disable(l_iter->v, BM_ELEM_INTERNAL_TAG);
1664         }
1665         for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f2); i < f2len; i++, l_iter = l_iter->next) {
1666                 BM_elem_flag_disable(l_iter->v, BM_ELEM_INTERNAL_TAG);
1667         }
1668
1669         for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f1); i < f1len; i++, l_iter = l_iter->next) {
1670                 if (l_iter != f1loop) {
1671                         BM_elem_flag_enable(l_iter->v, BM_ELEM_INTERNAL_TAG);
1672                 }
1673         }
1674         for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f2); i < f2len; i++, l_iter = l_iter->next) {
1675                 if (l_iter != f2loop) {
1676                         /* as soon as a duplicate is found, bail out */
1677                         if (BM_elem_flag_test(l_iter->v, BM_ELEM_INTERNAL_TAG)) {
1678                                 return NULL;
1679                         }
1680                 }
1681         }
1682
1683         /* join the two loop */
1684         f1loop->prev->next = f2loop->next;
1685         f2loop->next->prev = f1loop->prev;
1686         
1687         f1loop->next->prev = f2loop->prev;
1688         f2loop->prev->next = f1loop->next;
1689         
1690         /* if f1loop was baseloop, make f1loop->next the base. */
1691         if (BM_FACE_FIRST_LOOP(f1) == f1loop)
1692                 BM_FACE_FIRST_LOOP(f1) = f1loop->next;
1693
1694         /* increase length of f1 */
1695         f1->len += (f2->len - 2);
1696
1697         /* make sure each loop points to the proper face */
1698         newlen = f1->len;
1699         for (i = 0, l_iter = BM_FACE_FIRST_LOOP(f1); i < newlen; i++, l_iter = l_iter->next)
1700                 l_iter->f = f1;
1701         
1702         /* remove edge from the disk cycle of its two vertices */
1703         bmesh_disk_edge_remove(f1loop->e, f1loop->e->v1);
1704         bmesh_disk_edge_remove(f1loop->e, f1loop->e->v2);
1705         
1706         /* deallocate edge and its two loops as well as f2 */
1707         BLI_mempool_free(bm->toolflagpool, f1loop->e->oflags);
1708         BLI_mempool_free(bm->epool, f1loop->e);
1709         bm->totedge--;
1710         BLI_mempool_free(bm->lpool, f1loop);
1711         bm->totloop--;
1712         BLI_mempool_free(bm->lpool, f2loop);
1713         bm->totloop--;
1714         BLI_mempool_free(bm->toolflagpool, f2->oflags);
1715         BLI_mempool_free(bm->fpool, f2);
1716         bm->totface--;
1717         /* account for both above */
1718         bm->elem_index_dirty |= BM_EDGE | BM_FACE;
1719
1720         BM_CHECK_ELEMENT(f1);
1721
1722         /* validate the new loop cycle */
1723         edok = bmesh_loop_validate(f1);
1724         BMESH_ASSERT(edok != FALSE);
1725         
1726         return f1;
1727 }
1728
1729 /**
1730  * \brief Splice Vert
1731  *
1732  * Merges two verts into one (\a v into \a vtarget).
1733  *
1734  * \return Success
1735  */
1736 int BM_vert_splice(BMesh *bm, BMVert *v, BMVert *vtarget)
1737 {
1738         BMEdge *e;
1739         BMLoop *l;
1740         BMIter liter;
1741
1742         /* verts already spliced */
1743         if (v == vtarget) {
1744                 return FALSE;
1745         }
1746
1747         /* retarget all the loops of v to vtarget */
1748         BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1749                 l->v = vtarget;
1750         }
1751
1752         /* move all the edges from v's disk to vtarget's disk */
1753         while ((e = v->e)) {
1754                 bmesh_disk_edge_remove(e, v);
1755                 bmesh_edge_swapverts(e, v, vtarget);
1756                 bmesh_disk_edge_append(e, vtarget);
1757         }
1758
1759         BM_CHECK_ELEMENT(v);
1760         BM_CHECK_ELEMENT(vtarget);
1761
1762         /* v is unused now, and can be killed */
1763         BM_vert_kill(bm, v);
1764
1765         return TRUE;
1766 }
1767
1768 /**
1769  * \brief Separate Vert
1770  *
1771  * Separates all disjoint fans that meet at a vertex, making a unique
1772  * vertex for each region. returns an array of all resulting vertices.
1773  *
1774  * \note this is a low level function, bm_edge_separate needs to run on edges first
1775  * or, the faces sharing verts must not be sharing edges for them to split at least.
1776  *
1777  * \return Success
1778  */
1779 int bmesh_vert_separate(BMesh *bm, BMVert *v, BMVert ***r_vout, int *r_vout_len)
1780 {
1781         BMEdge **stack = NULL;
1782         BLI_array_declare(stack);
1783         BMVert **verts = NULL;
1784         GHash *visithash;
1785         BMIter eiter, liter;
1786         BMLoop *l;
1787         BMEdge *e;
1788         int i, maxindex;
1789         BMLoop *nl;
1790
1791         visithash = BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp, __func__);
1792
1793         maxindex = 0;
1794         BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1795                 if (BLI_ghash_haskey(visithash, e)) {
1796                         continue;
1797                 }
1798
1799                 /* Prime the stack with this unvisited edge */
1800                 BLI_array_append(stack, e);
1801
1802                 /* Considering only edges and faces incident on vertex v, walk
1803                  * the edges & faces and assign an index to each connected set */
1804                 while ((e = BLI_array_pop(stack))) {
1805                         BLI_ghash_insert(visithash, e, SET_INT_IN_POINTER(maxindex));
1806
1807                         BM_ITER_ELEM (l, &liter, e, BM_LOOPS_OF_EDGE) {
1808                                 nl = (l->v == v) ? l->prev : l->next;
1809                                 if (!BLI_ghash_haskey(visithash, nl->e)) {
1810                                         BLI_array_append(stack, nl->e);
1811                                 }
1812                         }
1813                 }
1814
1815                 maxindex++;
1816         }
1817
1818         /* Make enough verts to split v for each group */
1819         verts = MEM_callocN(sizeof(BMVert *) * maxindex, __func__);
1820         verts[0] = v;
1821         for (i = 1; i < maxindex; i++) {
1822                 verts[i] = BM_vert_create(bm, v->co, v);
1823         }
1824
1825         /* Replace v with the new verts in each group */
1826 #if 0
1827         BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1828                 /* call first since its faster then a hash lookup */
1829                 if (l->v != v) {
1830                         continue;
1831                 }
1832                 i = GET_INT_FROM_POINTER(BLI_ghash_lookup(visithash, l->e));
1833                 if (i == 0) {
1834                         continue;
1835                 }
1836
1837                 /* Loops here should always refer to an edge that has v as an
1838                  * endpoint. For each appearance of this vert in a face, there
1839                  * will actually be two iterations: one for the loop heading
1840                  * towards vertex v, and another for the loop heading out from
1841                  * vertex v. Only need to swap the vertex on one of those times,
1842                  * on the outgoing loop. */
1843
1844                 /* XXX - because this clobbers the iterator, this *whole* block is commented, see below */
1845                 l->v = verts[i];
1846         }
1847 #else
1848         /* note: this is the same as the commented code above *except* that it doesn't break iterator
1849          * by modifying data it loops over [#30632], this re-uses the 'stack' variable which is a bit
1850          * bad practice but save alloc'ing a new array - note, the comment above is useful, keep it
1851          * if you are tidying up code - campbell */
1852         BLI_array_empty(stack);
1853         BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1854                 if (l->v == v) {
1855                         BLI_array_append(stack, (BMEdge *)l);
1856                 }
1857         }
1858         while ((l = (BMLoop *)(BLI_array_pop(stack)))) {
1859                 if ((i = GET_INT_FROM_POINTER(BLI_ghash_lookup(visithash, l->e)))) {
1860                         l->v = verts[i];
1861                 }
1862         }
1863 #endif
1864
1865         BLI_array_free(stack);
1866
1867         BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
1868                 i = GET_INT_FROM_POINTER(BLI_ghash_lookup(visithash, e));
1869                 if (i == 0) {
1870                         continue;
1871                 }
1872
1873                 BLI_assert(e->v1 == v || e->v2 == v);
1874                 bmesh_disk_edge_remove(e, v);
1875                 bmesh_edge_swapverts(e, v, verts[i]);
1876                 bmesh_disk_edge_append(e, verts[i]);
1877         }
1878
1879         BLI_ghash_free(visithash, NULL, NULL);
1880
1881         for (i = 0; i < maxindex; i++) {
1882                 BM_CHECK_ELEMENT(verts[i]);
1883         }
1884
1885         if (r_vout_len != NULL) {
1886                 *r_vout_len = maxindex;
1887         }
1888
1889         if (r_vout != NULL) {
1890                 *r_vout = verts;
1891         }
1892         else {
1893                 MEM_freeN(verts);
1894         }
1895
1896         return TRUE;
1897 }
1898
1899 /**
1900  * High level function which wraps both #bm_vert_separate and #bm_edge_separate
1901  */
1902 int BM_vert_separate(BMesh *bm, BMVert *v, BMVert ***r_vout, int *r_vout_len,
1903                      BMEdge **e_in, int e_in_len)
1904 {
1905         int i;
1906
1907         for (i = 0; i < e_in_len; i++) {
1908                 BMEdge *e = e_in[i];
1909                 if (e->l && BM_vert_in_edge(e, v)) {
1910                         bmesh_edge_separate(bm, e, e->l);
1911                 }
1912         }
1913
1914         return bmesh_vert_separate(bm, v, r_vout, r_vout_len);
1915 }
1916
1917 /**
1918  * \brief Splice Edge
1919  *
1920  * Splice two unique edges which share the same two vertices into one edge.
1921  *
1922  * \return Success
1923  *
1924  * \note Edges must already have the same vertices.
1925  */
1926 int BM_edge_splice(BMesh *bm, BMEdge *e, BMEdge *etarget)
1927 {
1928         BMLoop *l;
1929
1930         if (!BM_vert_in_edge(e, etarget->v1) || !BM_vert_in_edge(e, etarget->v2)) {
1931                 /* not the same vertices can't splice */
1932                 return FALSE;
1933         }
1934
1935         while (e->l) {
1936                 l = e->l;
1937                 BLI_assert(BM_vert_in_edge(etarget, l->v));
1938                 BLI_assert(BM_vert_in_edge(etarget, l->next->v));
1939                 bmesh_radial_loop_remove(l, e);
1940                 bmesh_radial_append(etarget, l);
1941         }
1942
1943         BLI_assert(bmesh_radial_length(e->l) == 0);
1944
1945         BM_CHECK_ELEMENT(e);
1946         BM_CHECK_ELEMENT(etarget);
1947
1948         /* removes from disks too */
1949         BM_edge_kill(bm, e);
1950
1951         return TRUE;
1952 }
1953
1954 /**
1955  * \brief Separate Edge
1956  *
1957  * Separates a single edge into two edge: the original edge and
1958  * a new edge that has only \a l_sep in its radial.
1959  *
1960  * \return Success
1961  *
1962  * \note Does nothing if \a l_sep is already the only loop in the
1963  * edge radial.
1964  */
1965 int bmesh_edge_separate(BMesh *bm, BMEdge *e, BMLoop *l_sep)
1966 {
1967         BMEdge *ne;
1968         int radlen;
1969
1970         BLI_assert(l_sep->e == e);
1971         BLI_assert(e->l);
1972         
1973         radlen = bmesh_radial_length(e->l);
1974         if (radlen < 2) {
1975                 /* no cut required */
1976                 return TRUE;
1977         }
1978
1979         if (l_sep == e->l) {
1980                 e->l = l_sep->radial_next;
1981         }
1982
1983         ne = BM_edge_create(bm, e->v1, e->v2, e, FALSE);
1984         bmesh_radial_loop_remove(l_sep, e);
1985         bmesh_radial_append(ne, l_sep);
1986         l_sep->e = ne;
1987
1988         BLI_assert(bmesh_radial_length(e->l) == radlen - 1);
1989         BLI_assert(bmesh_radial_length(ne->l) == 1);
1990
1991         BM_CHECK_ELEMENT(ne);
1992         BM_CHECK_ELEMENT(e);
1993
1994         return TRUE;
1995 }
1996
1997 /**
1998  * \brief Unglue Region Make Vert (URMV)
1999  *
2000  * Disconnects a face from its vertex fan at loop \a sl
2001  *
2002  * \return The newly created BMVert
2003  */
2004 BMVert *bmesh_urmv_loop(BMesh *bm, BMLoop *sl)
2005 {
2006         BMVert **vtar;
2007         int len, i;
2008         BMVert *nv = NULL;
2009         BMVert *sv = sl->v;
2010
2011         /* peel the face from the edge radials on both sides of the
2012          * loop vert, disconnecting the face from its fan */
2013         bmesh_edge_separate(bm, sl->e, sl);
2014         bmesh_edge_separate(bm, sl->prev->e, sl->prev);
2015
2016         if (bmesh_disk_count(sv) == 2) {
2017                 /* If there are still only two edges out of sv, then
2018                  * this whole URMV was just a no-op, so exit now. */
2019                 return sv;
2020         }
2021
2022         /* Update the disk start, so that v->e points to an edge
2023          * not touching the split loop. This is so that BM_vert_split
2024          * will leave the original sv on some *other* fan (not the
2025          * one-face fan that holds the unglue face). */
2026         while (sv->e == sl->e || sv->e == sl->prev->e) {
2027                 sv->e = bmesh_disk_edge_next(sv->e, sv);
2028         }
2029
2030         /* Split all fans connected to the vert, duplicating it for
2031          * each fans. */
2032         bmesh_vert_separate(bm, sv, &vtar, &len);
2033
2034         /* There should have been at least two fans cut apart here,
2035          * otherwise the early exit would have kicked in. */
2036         BLI_assert(len >= 2);
2037
2038         nv = sl->v;
2039
2040         /* Desired result here is that a new vert should always be
2041          * created for the unglue face. This is so we can glue any
2042          * extras back into the original vert. */
2043         BLI_assert(nv != sv);
2044         BLI_assert(sv == vtar[0]);
2045
2046         /* If there are more than two verts as a result, glue together
2047          * all the verts except the one this URMV intended to create */
2048         if (len > 2) {
2049                 for (i = 0; i < len; i++) {
2050                         if (vtar[i] == nv) {
2051                                 break;
2052                         }
2053                 }
2054
2055                 if (i != len) {
2056                         /* Swap the single vert that was needed for the
2057                          * unglue into the last array slot */
2058                         SWAP(BMVert *, vtar[i], vtar[len - 1]);
2059
2060                         /* And then glue the rest back together */
2061                         for (i = 1; i < len - 1; i++) {
2062                                 BM_vert_splice(bm, vtar[i], vtar[0]);
2063                         }
2064                 }
2065         }
2066
2067         MEM_freeN(vtar);
2068
2069         return nv;
2070 }
2071
2072 /**
2073  * \brief Unglue Region Make Vert (URMV)
2074  *
2075  * Disconnects sf from the vertex fan at \a sv
2076  *
2077  * \return The newly created BMVert
2078  */
2079 BMVert *bmesh_urmv(BMesh *bm, BMFace *sf, BMVert *sv)
2080 {
2081         BMLoop *l = BM_face_vert_share_loop(sf, sv);
2082         return bmesh_urmv_loop(bm, l);
2083 }