Merge branch 'blender2.7'
[blender.git] / source / blender / editors / mesh / editmesh_utils.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  * The Original Code is Copyright (C) 2004 by Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joseph Eagar
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/mesh/editmesh_utils.c
29  *  \ingroup edmesh
30  */
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_mesh_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_key_types.h"
37
38 #include "BLI_math.h"
39 #include "BLI_alloca.h"
40 #include "BLI_buffer.h"
41 #include "BLI_kdtree.h"
42 #include "BLI_listbase.h"
43
44 #include "BKE_DerivedMesh.h"
45 #include "BKE_context.h"
46 #include "BKE_main.h"
47 #include "BKE_mesh.h"
48 #include "BKE_mesh_mapping.h"
49 #include "BKE_report.h"
50 #include "BKE_editmesh.h"
51 #include "BKE_editmesh_bvh.h"
52
53 #include "DEG_depsgraph.h"
54
55 #include "BKE_object.h"  /* XXX. only for EDBM_mesh_load(). */
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "ED_mesh.h"
61 #include "ED_screen.h"
62 #include "ED_view3d.h"
63
64 #include "mesh_intern.h"  /* own include */
65
66 /* -------------------------------------------------------------------- */
67 /** \name Redo API
68  * \{ */
69
70 /* Mesh backup implementation.
71  * This would greatly benefit from some sort of binary diffing
72  * just as the undo stack would.
73  * So leaving this as an interface for further work */
74
75 BMBackup EDBM_redo_state_store(BMEditMesh *em)
76 {
77         BMBackup backup;
78         backup.bmcopy = BM_mesh_copy(em->bm);
79         return backup;
80 }
81
82 void EDBM_redo_state_restore(BMBackup backup, BMEditMesh *em, int recalctess)
83 {
84         BMesh *tmpbm;
85         if (!em || !backup.bmcopy) {
86                 return;
87         }
88
89         BM_mesh_data_free(em->bm);
90         tmpbm = BM_mesh_copy(backup.bmcopy);
91         *em->bm = *tmpbm;
92         MEM_freeN(tmpbm);
93         tmpbm = NULL;
94
95         if (recalctess) {
96                 BKE_editmesh_tessface_calc(em);
97         }
98 }
99
100 void EDBM_redo_state_free(BMBackup *backup, BMEditMesh *em, int recalctess)
101 {
102         if (em && backup->bmcopy) {
103                 BM_mesh_data_free(em->bm);
104                 *em->bm = *backup->bmcopy;
105         }
106         else if (backup->bmcopy) {
107                 BM_mesh_data_free(backup->bmcopy);
108         }
109
110         if (backup->bmcopy) {
111                 MEM_freeN(backup->bmcopy);
112         }
113         backup->bmcopy = NULL;
114
115         if (recalctess && em) {
116                 BKE_editmesh_tessface_calc(em);
117         }
118 }
119
120 /** \} */
121
122 /* -------------------------------------------------------------------- */
123 /** \name BMesh Operator (BMO) API Wrapper
124  * \{ */
125
126 bool EDBM_op_init(BMEditMesh *em, BMOperator *bmop, wmOperator *op, const char *fmt, ...)
127 {
128         BMesh *bm = em->bm;
129         va_list list;
130
131         va_start(list, fmt);
132
133         if (!BMO_op_vinitf(bm, bmop, BMO_FLAG_DEFAULTS, fmt, list)) {
134                 BKE_reportf(op->reports, RPT_ERROR, "Parse error in %s", __func__);
135                 va_end(list);
136                 return false;
137         }
138
139         if (!em->emcopy) {
140                 em->emcopy = BKE_editmesh_copy(em);
141         }
142         em->emcopyusers++;
143
144         va_end(list);
145
146         return true;
147 }
148
149 /* returns 0 on error, 1 on success.  executes and finishes a bmesh operator */
150 bool EDBM_op_finish(BMEditMesh *em, BMOperator *bmop, wmOperator *op, const bool do_report)
151 {
152         const char *errmsg;
153
154         BMO_op_finish(em->bm, bmop);
155
156         if (BMO_error_get(em->bm, &errmsg, NULL)) {
157                 BMEditMesh *emcopy = em->emcopy;
158
159                 if (do_report) {
160                         BKE_report(op->reports, RPT_ERROR, errmsg);
161                 }
162
163                 EDBM_mesh_free(em);
164                 *em = *emcopy;
165
166                 MEM_freeN(emcopy);
167                 em->emcopyusers = 0;
168                 em->emcopy = NULL;
169
170                 /* when copying, tessellation isn't to for faster copying,
171                  * but means we need to re-tessellate here */
172                 if (em->looptris == NULL) {
173                         BKE_editmesh_tessface_calc(em);
174                 }
175
176                 if (em->ob) {
177                         DEG_id_tag_update(&((Mesh *)em->ob->data)->id, ID_RECALC_COPY_ON_WRITE);
178                 }
179
180                 return false;
181         }
182         else {
183                 em->emcopyusers--;
184                 if (em->emcopyusers < 0) {
185                         printf("warning: em->emcopyusers was less than zero.\n");
186                 }
187
188                 if (em->emcopyusers <= 0) {
189                         BKE_editmesh_free(em->emcopy);
190                         MEM_freeN(em->emcopy);
191                         em->emcopy = NULL;
192                 }
193
194                 return true;
195         }
196 }
197
198 bool EDBM_op_callf(BMEditMesh *em, wmOperator *op, const char *fmt, ...)
199 {
200         BMesh *bm = em->bm;
201         BMOperator bmop;
202         va_list list;
203
204         va_start(list, fmt);
205
206         if (!BMO_op_vinitf(bm, &bmop, BMO_FLAG_DEFAULTS, fmt, list)) {
207                 BKE_reportf(op->reports, RPT_ERROR, "Parse error in %s", __func__);
208                 va_end(list);
209                 return false;
210         }
211
212         if (!em->emcopy) {
213                 em->emcopy = BKE_editmesh_copy(em);
214         }
215         em->emcopyusers++;
216
217         BMO_op_exec(bm, &bmop);
218
219         va_end(list);
220         return EDBM_op_finish(em, &bmop, op, true);
221 }
222
223 bool EDBM_op_call_and_selectf(
224         BMEditMesh *em, wmOperator *op,
225         const char *select_slot_out, const bool select_extend,
226         const char *fmt, ...)
227 {
228         BMOpSlot *slot_select_out;
229         BMesh *bm = em->bm;
230         BMOperator bmop;
231         va_list list;
232         char hflag;
233
234         va_start(list, fmt);
235
236         if (!BMO_op_vinitf(bm, &bmop, BMO_FLAG_DEFAULTS, fmt, list)) {
237                 BKE_reportf(op->reports, RPT_ERROR, "Parse error in %s", __func__);
238                 va_end(list);
239                 return false;
240         }
241
242         if (!em->emcopy) {
243                 em->emcopy = BKE_editmesh_copy(em);
244         }
245         em->emcopyusers++;
246
247         BMO_op_exec(bm, &bmop);
248
249         slot_select_out = BMO_slot_get(bmop.slots_out, select_slot_out);
250         hflag = slot_select_out->slot_subtype.elem & BM_ALL_NOLOOP;
251         BLI_assert(hflag != 0);
252
253         if (select_extend == false) {
254                 BM_mesh_elem_hflag_disable_all(em->bm, BM_VERT | BM_EDGE | BM_FACE, BM_ELEM_SELECT, false);
255         }
256
257         BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, select_slot_out, hflag, BM_ELEM_SELECT, true);
258
259         va_end(list);
260         return EDBM_op_finish(em, &bmop, op, true);
261 }
262
263 bool EDBM_op_call_silentf(BMEditMesh *em, const char *fmt, ...)
264 {
265         BMesh *bm = em->bm;
266         BMOperator bmop;
267         va_list list;
268
269         va_start(list, fmt);
270
271         if (!BMO_op_vinitf(bm, &bmop, BMO_FLAG_DEFAULTS, fmt, list)) {
272                 va_end(list);
273                 return false;
274         }
275
276         if (!em->emcopy) {
277                 em->emcopy = BKE_editmesh_copy(em);
278         }
279         em->emcopyusers++;
280
281         BMO_op_exec(bm, &bmop);
282
283         va_end(list);
284         return EDBM_op_finish(em, &bmop, NULL, false);
285 }
286
287 /** \} */
288
289 /* -------------------------------------------------------------------- */
290 /** \name Edit BMesh API
291  *
292  * Make/Clear/Free functions.
293  * \{ */
294
295 void EDBM_mesh_make(Object *ob, const int select_mode, const bool add_key_index)
296 {
297         Mesh *me = ob->data;
298         BMesh *bm;
299
300         if (UNLIKELY(!me->mpoly && me->totface)) {
301                 BKE_mesh_convert_mfaces_to_mpolys(me);
302         }
303
304         bm = BKE_mesh_to_bmesh(
305                 me, ob, add_key_index,
306                 &((struct BMeshCreateParams){.use_toolflags = true,}));
307
308         if (me->edit_btmesh) {
309                 /* this happens when switching shape keys */
310                 EDBM_mesh_free(me->edit_btmesh);
311                 MEM_freeN(me->edit_btmesh);
312         }
313
314         /* currently executing operators re-tessellates, so we can avoid doing here
315          * but at some point it may need to be added back. */
316 #if 0
317         me->edit_btmesh = BKE_editmesh_create(bm, true);
318 #else
319         me->edit_btmesh = BKE_editmesh_create(bm, false);
320 #endif
321
322         me->edit_btmesh->selectmode = me->edit_btmesh->bm->selectmode = select_mode;
323         me->edit_btmesh->mat_nr = (ob->actcol > 0) ? ob->actcol - 1 : 0;
324         me->edit_btmesh->ob = ob;
325
326         /* we need to flush selection because the mode may have changed from when last in editmode */
327         EDBM_selectmode_flush(me->edit_btmesh);
328 }
329
330 /**
331  * \warning This can invalidate the #Mesh runtime cache of other objects (for linked duplicates).
332  * Most callers should run #DEG_id_tag_update on \a ob->data, see: T46738, T46913
333  */
334 void EDBM_mesh_load(Main *bmain, Object *ob)
335 {
336         Mesh *me = ob->data;
337         BMesh *bm = me->edit_btmesh->bm;
338
339         /* Workaround for T42360, 'ob->shapenr' should be 1 in this case.
340          * however this isn't synchronized between objects at the moment. */
341         if (UNLIKELY((ob->shapenr == 0) && (me->key && !BLI_listbase_is_empty(&me->key->block)))) {
342                 bm->shapenr = 1;
343         }
344
345         BM_mesh_bm_to_me(
346                 bmain, bm, me, (&(struct BMeshToMeshParams){
347                     .calc_object_remap = true,
348                 }));
349
350 #ifdef USE_TESSFACE_DEFAULT
351         BKE_mesh_tessface_calc(me);
352 #endif
353
354         /* Free derived mesh. usually this would happen through depsgraph but there
355          * are exceptions like file save that will not cause this, and we want to
356          * avoid ending up with an invalid derived mesh then.
357          *
358          * Do it for all objects which shares the same mesh datablock, since their
359          * derived meshes might also be referencing data which was just freed,
360          *
361          * Annoying enough, but currently seems most efficient way to avoid access
362          * of freed data on scene update, especially in cases when there are dependency
363          * cycles.
364          */
365 #if 0
366         for (Object *other_object = bmain->object.first;
367              other_object != NULL;
368              other_object = other_object->id.next)
369         {
370                 if (other_object->data == ob->data) {
371                         BKE_object_free_derived_caches(other_object);
372                 }
373         }
374 #endif
375 }
376
377 void EDBM_mesh_clear(BMEditMesh *em)
378 {
379         /* clear bmesh */
380         BM_mesh_clear(em->bm);
381
382         /* free derived meshes */
383         BKE_editmesh_free_derivedmesh(em);
384
385         /* free tessellation data */
386         em->tottri = 0;
387         if (em->looptris) {
388                 MEM_freeN(em->looptris);
389                 em->looptris = NULL;
390         }
391 }
392
393 /**
394  * Should only be called on the active editmesh, otherwise call #BKE_editmesh_free
395  */
396 void EDBM_mesh_free(BMEditMesh *em)
397 {
398         /* These tables aren't used yet, so it's not strictly necessary
399          * to 'end' them (with 'e' param) but if someone tries to start
400          * using them, having these in place will save a lot of pain */
401         ED_mesh_mirror_spatial_table(NULL, NULL, NULL, NULL, 'e');
402         ED_mesh_mirror_topo_table(NULL, NULL, 'e');
403
404         BKE_editmesh_free(em);
405 }
406
407 /** \} */
408
409 /* -------------------------------------------------------------------- */
410 /** \name Selection Utilities
411  * \{ */
412
413 void EDBM_selectmode_to_scene(bContext *C)
414 {
415         Scene *scene = CTX_data_scene(C);
416         Object *obedit = CTX_data_edit_object(C);
417         BMEditMesh *em = BKE_editmesh_from_object(obedit);
418
419         if (!em) {
420                 return;
421         }
422
423         scene->toolsettings->selectmode = em->selectmode;
424
425         /* Request redraw of header buttons (to show new select mode) */
426         WM_event_add_notifier(C, NC_SCENE | ND_TOOLSETTINGS, scene);
427 }
428
429 void EDBM_selectmode_flush_ex(BMEditMesh *em, const short selectmode)
430 {
431         BM_mesh_select_mode_flush_ex(em->bm, selectmode);
432 }
433
434 void EDBM_selectmode_flush(BMEditMesh *em)
435 {
436         EDBM_selectmode_flush_ex(em, em->selectmode);
437 }
438
439 void EDBM_deselect_flush(BMEditMesh *em)
440 {
441         /* function below doesn't use. just do this to keep the values in sync */
442         em->bm->selectmode = em->selectmode;
443         BM_mesh_deselect_flush(em->bm);
444 }
445
446 void EDBM_select_flush(BMEditMesh *em)
447 {
448         /* function below doesn't use. just do this to keep the values in sync */
449         em->bm->selectmode = em->selectmode;
450         BM_mesh_select_flush(em->bm);
451 }
452
453 void EDBM_select_more(BMEditMesh *em, const bool use_face_step)
454 {
455         BMOperator bmop;
456         const bool use_faces = (em->selectmode == SCE_SELECT_FACE);
457
458         BMO_op_initf(
459                 em->bm, &bmop, BMO_FLAG_DEFAULTS,
460                 "region_extend geom=%hvef use_contract=%b use_faces=%b use_face_step=%b",
461                 BM_ELEM_SELECT, false, use_faces, use_face_step);
462         BMO_op_exec(em->bm, &bmop);
463         /* don't flush selection in edge/vertex mode  */
464         BMO_slot_buffer_hflag_enable(em->bm, bmop.slots_out, "geom.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, use_faces ? true : false);
465         BMO_op_finish(em->bm, &bmop);
466
467         EDBM_selectmode_flush(em);
468 }
469
470 void EDBM_select_less(BMEditMesh *em, const bool use_face_step)
471 {
472         BMOperator bmop;
473         const bool use_faces = (em->selectmode == SCE_SELECT_FACE);
474
475         BMO_op_initf(
476                 em->bm, &bmop, BMO_FLAG_DEFAULTS,
477                 "region_extend geom=%hvef use_contract=%b use_faces=%b use_face_step=%b",
478                 BM_ELEM_SELECT, true, use_faces, use_face_step);
479         BMO_op_exec(em->bm, &bmop);
480         /* don't flush selection in edge/vertex mode  */
481         BMO_slot_buffer_hflag_disable(em->bm, bmop.slots_out, "geom.out", BM_ALL_NOLOOP, BM_ELEM_SELECT, use_faces ? true : false);
482         BMO_op_finish(em->bm, &bmop);
483
484         EDBM_selectmode_flush(em);
485
486         /* only needed for select less, ensure we don't have isolated elements remaining */
487         BM_mesh_select_mode_clean(em->bm);
488 }
489
490 void EDBM_flag_disable_all(BMEditMesh *em, const char hflag)
491 {
492         BM_mesh_elem_hflag_disable_all(em->bm, BM_VERT | BM_EDGE | BM_FACE, hflag, false);
493 }
494
495 void EDBM_flag_enable_all(BMEditMesh *em, const char hflag)
496 {
497         BM_mesh_elem_hflag_enable_all(em->bm, BM_VERT | BM_EDGE | BM_FACE, hflag, true);
498 }
499
500 /** \} */
501
502 /* -------------------------------------------------------------------- */
503 /** \name UV Vertex Map API
504  * \{ */
505
506 /**
507  * Return a new UVVertMap from the editmesh
508  */
509 UvVertMap *BM_uv_vert_map_create(
510         BMesh *bm,
511         const float limit[2], const bool use_select, const bool use_winding)
512 {
513         BMVert *ev;
514         BMFace *efa;
515         BMLoop *l;
516         BMIter iter, liter;
517         /* vars from original func */
518         UvVertMap *vmap;
519         UvMapVert *buf;
520         MLoopUV *luv;
521         unsigned int a;
522         int totverts, i, totuv, totfaces;
523         const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
524         bool *winding = NULL;
525         BLI_buffer_declare_static(vec2f, tf_uv_buf, BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
526
527         BM_mesh_elem_index_ensure(bm, BM_VERT | BM_FACE);
528
529         totfaces = bm->totface;
530         totverts = bm->totvert;
531         totuv = 0;
532
533         /* generate UvMapVert array */
534         BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
535                 if ((use_select == false) || BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
536                         totuv += efa->len;
537                 }
538         }
539
540         if (totuv == 0) {
541                 return NULL;
542         }
543         vmap = (UvVertMap *)MEM_callocN(sizeof(*vmap), "UvVertMap");
544         if (!vmap) {
545                 return NULL;
546         }
547
548         vmap->vert = (UvMapVert **)MEM_callocN(sizeof(*vmap->vert) * totverts, "UvMapVert_pt");
549         buf = vmap->buf = (UvMapVert *)MEM_callocN(sizeof(*vmap->buf) * totuv, "UvMapVert");
550         if (use_winding) {
551                 winding = MEM_callocN(sizeof(*winding) * totfaces, "winding");
552         }
553
554         if (!vmap->vert || !vmap->buf) {
555                 BKE_mesh_uv_vert_map_free(vmap);
556                 return NULL;
557         }
558
559         BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, a) {
560                 if ((use_select == false) || BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
561                         float (*tf_uv)[2] = NULL;
562
563                         if (use_winding) {
564                                 tf_uv = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa->len);
565                         }
566
567                         BM_ITER_ELEM_INDEX(l, &liter, efa, BM_LOOPS_OF_FACE, i) {
568                                 buf->loop_of_poly_index = i;
569                                 buf->poly_index = a;
570                                 buf->separate = 0;
571
572                                 buf->next = vmap->vert[BM_elem_index_get(l->v)];
573                                 vmap->vert[BM_elem_index_get(l->v)] = buf;
574                                 buf++;
575
576                                 if (use_winding) {
577                                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
578                                         copy_v2_v2(tf_uv[i], luv->uv);
579                                 }
580                         }
581
582                         if (use_winding) {
583                                 winding[a] = cross_poly_v2(tf_uv, efa->len) > 0;
584                         }
585                 }
586         }
587
588         /* sort individual uvs for each vert */
589         BM_ITER_MESH_INDEX (ev, &iter, bm, BM_VERTS_OF_MESH, a) {
590                 UvMapVert *newvlist = NULL, *vlist = vmap->vert[a];
591                 UvMapVert *iterv, *v, *lastv, *next;
592                 float *uv, *uv2, uvdiff[2];
593
594                 while (vlist) {
595                         v = vlist;
596                         vlist = vlist->next;
597                         v->next = newvlist;
598                         newvlist = v;
599
600                         efa = BM_face_at_index(bm, v->poly_index);
601
602                         l = BM_iter_at_index(bm, BM_LOOPS_OF_FACE, efa, v->loop_of_poly_index);
603                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
604                         uv = luv->uv;
605
606                         lastv = NULL;
607                         iterv = vlist;
608
609                         while (iterv) {
610                                 next = iterv->next;
611                                 efa = BM_face_at_index(bm, iterv->poly_index);
612                                 l = BM_iter_at_index(bm, BM_LOOPS_OF_FACE, efa, iterv->loop_of_poly_index);
613                                 luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
614                                 uv2 = luv->uv;
615
616                                 sub_v2_v2v2(uvdiff, uv2, uv);
617
618                                 if (fabsf(uvdiff[0]) < limit[0] && fabsf(uvdiff[1]) < limit[1] &&
619                                     (!use_winding || winding[iterv->poly_index] == winding[v->poly_index]))
620                                 {
621                                         if (lastv) lastv->next = next;
622                                         else vlist = next;
623                                         iterv->next = newvlist;
624                                         newvlist = iterv;
625                                 }
626                                 else {
627                                         lastv = iterv;
628                                 }
629
630                                 iterv = next;
631                         }
632
633                         newvlist->separate = 1;
634                 }
635
636                 vmap->vert[a] = newvlist;
637         }
638
639         if (use_winding) {
640                 MEM_freeN(winding);
641         }
642
643         BLI_buffer_free(&tf_uv_buf);
644
645         return vmap;
646 }
647
648 UvMapVert *BM_uv_vert_map_at_index(UvVertMap *vmap, unsigned int v)
649 {
650         return vmap->vert[v];
651 }
652
653 /* A specialized vert map used by stitch operator */
654 UvElementMap *BM_uv_element_map_create(
655         BMesh *bm,
656         const bool selected, const bool use_winding, const bool do_islands)
657 {
658         BMVert *ev;
659         BMFace *efa;
660         BMLoop *l;
661         BMIter iter, liter;
662         /* vars from original func */
663         UvElementMap *element_map;
664         UvElement *buf;
665         bool *winding = NULL;
666         BLI_buffer_declare_static(vec2f, tf_uv_buf, BLI_BUFFER_NOP, BM_DEFAULT_NGON_STACK_SIZE);
667
668         MLoopUV *luv;
669         int totverts, totfaces, i, totuv, j;
670
671         const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
672
673         BM_mesh_elem_index_ensure(bm, BM_VERT | BM_FACE);
674
675         totfaces = bm->totface;
676         totverts = bm->totvert;
677         totuv = 0;
678
679         /* generate UvElement array */
680         BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
681                 if (!selected || BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
682                         totuv += efa->len;
683                 }
684         }
685
686         if (totuv == 0) {
687                 return NULL;
688         }
689
690         element_map = (UvElementMap *)MEM_callocN(sizeof(*element_map), "UvElementMap");
691         element_map->totalUVs = totuv;
692         element_map->vert = (UvElement **)MEM_callocN(sizeof(*element_map->vert) * totverts, "UvElementVerts");
693         buf = element_map->buf = (UvElement *)MEM_callocN(sizeof(*element_map->buf) * totuv, "UvElement");
694
695         if (use_winding) {
696                 winding = MEM_mallocN(sizeof(*winding) * totfaces, "winding");
697         }
698
699         BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, j) {
700
701                 if (use_winding) {
702                         winding[j] = false;
703                 }
704
705                 if (!selected || BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
706                         float (*tf_uv)[2] = NULL;
707
708                         if (use_winding) {
709                                 tf_uv = (float (*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, efa->len);
710                         }
711
712                         BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
713                                 buf->l = l;
714                                 buf->separate = 0;
715                                 buf->island = INVALID_ISLAND;
716                                 buf->loop_of_poly_index = i;
717
718                                 buf->next = element_map->vert[BM_elem_index_get(l->v)];
719                                 element_map->vert[BM_elem_index_get(l->v)] = buf;
720
721                                 if (use_winding) {
722                                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
723                                         copy_v2_v2(tf_uv[i], luv->uv);
724                                 }
725
726                                 buf++;
727                         }
728
729                         if (use_winding) {
730                                 winding[j] = cross_poly_v2(tf_uv, efa->len) > 0;
731                         }
732                 }
733         }
734
735         /* sort individual uvs for each vert */
736         BM_ITER_MESH_INDEX (ev, &iter, bm, BM_VERTS_OF_MESH, i) {
737                 UvElement *newvlist = NULL, *vlist = element_map->vert[i];
738                 UvElement *iterv, *v, *lastv, *next;
739                 float *uv, *uv2, uvdiff[2];
740
741                 while (vlist) {
742                         v = vlist;
743                         vlist = vlist->next;
744                         v->next = newvlist;
745                         newvlist = v;
746
747                         l = v->l;
748                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
749                         uv = luv->uv;
750
751                         lastv = NULL;
752                         iterv = vlist;
753
754                         while (iterv) {
755                                 next = iterv->next;
756
757                                 l = iterv->l;
758                                 luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
759                                 uv2 = luv->uv;
760
761                                 sub_v2_v2v2(uvdiff, uv2, uv);
762
763                                 if (fabsf(uvdiff[0]) < STD_UV_CONNECT_LIMIT && fabsf(uvdiff[1]) < STD_UV_CONNECT_LIMIT &&
764                                     (!use_winding || winding[BM_elem_index_get(iterv->l->f)] == winding[BM_elem_index_get(v->l->f)]))
765                                 {
766                                         if (lastv) lastv->next = next;
767                                         else vlist = next;
768                                         iterv->next = newvlist;
769                                         newvlist = iterv;
770                                 }
771                                 else {
772                                         lastv = iterv;
773                                 }
774
775                                 iterv = next;
776                         }
777
778                         newvlist->separate = 1;
779                 }
780
781                 element_map->vert[i] = newvlist;
782         }
783
784         if (use_winding) {
785                 MEM_freeN(winding);
786         }
787
788         if (do_islands) {
789                 unsigned int *map;
790                 BMFace **stack;
791                 int stacksize = 0;
792                 UvElement *islandbuf;
793                 /* island number for faces */
794                 int *island_number = NULL;
795
796                 int nislands = 0, islandbufsize = 0;
797
798                 /* map holds the map from current vmap->buf to the new, sorted map */
799                 map = MEM_mallocN(sizeof(*map) * totuv, "uvelement_remap");
800                 stack = MEM_mallocN(sizeof(*stack) * bm->totface, "uv_island_face_stack");
801                 islandbuf = MEM_callocN(sizeof(*islandbuf) * totuv, "uvelement_island_buffer");
802                 island_number = MEM_mallocN(sizeof(*island_number) * totfaces, "uv_island_number_face");
803                 copy_vn_i(island_number, totfaces, INVALID_ISLAND);
804
805                 /* at this point, every UvElement in vert points to a UvElement sharing the same vertex.
806                  * Now we should sort uv's in islands. */
807                 for (i = 0; i < totuv; i++) {
808                         if (element_map->buf[i].island == INVALID_ISLAND) {
809                                 element_map->buf[i].island = nislands;
810                                 stack[0] = element_map->buf[i].l->f;
811                                 island_number[BM_elem_index_get(stack[0])] = nislands;
812                                 stacksize = 1;
813
814                                 while (stacksize > 0) {
815                                         efa = stack[--stacksize];
816
817                                         BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
818                                                 UvElement *element, *initelement = element_map->vert[BM_elem_index_get(l->v)];
819
820                                                 for (element = initelement; element; element = element->next) {
821                                                         if (element->separate)
822                                                                 initelement = element;
823
824                                                         if (element->l->f == efa) {
825                                                                 /* found the uv corresponding to our face and vertex.
826                                                                  * Now fill it to the buffer */
827                                                                 element->island = nislands;
828                                                                 map[element - element_map->buf] = islandbufsize;
829                                                                 islandbuf[islandbufsize].l = element->l;
830                                                                 islandbuf[islandbufsize].separate = element->separate;
831                                                                 islandbuf[islandbufsize].loop_of_poly_index = element->loop_of_poly_index;
832                                                                 islandbuf[islandbufsize].island =  nislands;
833                                                                 islandbufsize++;
834
835                                                                 for (element = initelement; element; element = element->next) {
836                                                                         if (element->separate && element != initelement)
837                                                                                 break;
838
839                                                                         if (island_number[BM_elem_index_get(element->l->f)] == INVALID_ISLAND) {
840                                                                                 stack[stacksize++] = element->l->f;
841                                                                                 island_number[BM_elem_index_get(element->l->f)] = nislands;
842                                                                         }
843                                                                 }
844                                                                 break;
845                                                         }
846                                                 }
847                                         }
848                                 }
849
850                                 nislands++;
851                         }
852                 }
853
854                 MEM_freeN(island_number);
855
856                 /* remap */
857                 for (i = 0; i < bm->totvert; i++) {
858                         /* important since we may do selection only. Some of these may be NULL */
859                         if (element_map->vert[i])
860                                 element_map->vert[i] = &islandbuf[map[element_map->vert[i] - element_map->buf]];
861                 }
862
863                 element_map->islandIndices = MEM_callocN(sizeof(*element_map->islandIndices) * nislands, "UvElementMap_island_indices");
864                 j = 0;
865                 for (i = 0; i < totuv; i++) {
866                         UvElement *element = element_map->buf[i].next;
867                         if (element == NULL)
868                                 islandbuf[map[i]].next = NULL;
869                         else
870                                 islandbuf[map[i]].next = &islandbuf[map[element - element_map->buf]];
871
872                         if (islandbuf[i].island != j) {
873                                 j++;
874                                 element_map->islandIndices[j] = i;
875                         }
876                 }
877
878                 MEM_freeN(element_map->buf);
879
880                 element_map->buf = islandbuf;
881                 element_map->totalIslands = nislands;
882                 MEM_freeN(stack);
883                 MEM_freeN(map);
884         }
885
886         BLI_buffer_free(&tf_uv_buf);
887
888         return element_map;
889 }
890
891 void BM_uv_vert_map_free(UvVertMap *vmap)
892 {
893         if (vmap) {
894                 if (vmap->vert) MEM_freeN(vmap->vert);
895                 if (vmap->buf) MEM_freeN(vmap->buf);
896                 MEM_freeN(vmap);
897         }
898 }
899
900 void BM_uv_element_map_free(UvElementMap *element_map)
901 {
902         if (element_map) {
903                 if (element_map->vert) MEM_freeN(element_map->vert);
904                 if (element_map->buf) MEM_freeN(element_map->buf);
905                 if (element_map->islandIndices) MEM_freeN(element_map->islandIndices);
906                 MEM_freeN(element_map);
907         }
908 }
909
910 UvElement *BM_uv_element_get(UvElementMap *map, BMFace *efa, BMLoop *l)
911 {
912         for (UvElement *element = map->vert[BM_elem_index_get(l->v)];
913              element;
914              element = element->next)
915         {
916                 if (element->l->f == efa) {
917                         return element;
918                 }
919         }
920
921         return NULL;
922 }
923
924 /** \} */
925
926 /* -------------------------------------------------------------------- */
927 /** \name Data Layer Checks
928  * \{ */
929
930 /* last_sel, use em->act_face otherwise get the last selected face in the editselections
931  * at the moment, last_sel is mainly useful for making sure the space image dosnt flicker */
932 BMFace *EDBM_uv_active_face_get(BMEditMesh *em, const bool sloppy, const bool selected)
933 {
934         BMFace *efa = NULL;
935
936         if (!EDBM_uv_check(em)) {
937                 return NULL;
938         }
939
940         efa = BM_mesh_active_face_get(em->bm, sloppy, selected);
941
942         if (efa) {
943                 return efa;
944         }
945
946         return NULL;
947 }
948
949 /* can we edit UV's for this mesh?*/
950 bool EDBM_uv_check(BMEditMesh *em)
951 {
952         /* some of these checks could be a touch overkill */
953         return em && em->bm->totface &&
954                CustomData_has_layer(&em->bm->ldata, CD_MLOOPUV);
955 }
956
957 bool EDBM_vert_color_check(BMEditMesh *em)
958 {
959         /* some of these checks could be a touch overkill */
960         return em && em->bm->totface && CustomData_has_layer(&em->bm->ldata, CD_MLOOPCOL);
961 }
962
963 /** \} */
964
965 /* -------------------------------------------------------------------- */
966 /** \name Mirror Cache API
967  * \{ */
968
969 static BMVert *cache_mirr_intptr_as_bmvert(intptr_t *index_lookup, int index)
970 {
971         intptr_t eve_i = index_lookup[index];
972         return (eve_i == -1) ? NULL : (BMVert *)eve_i;
973 }
974
975 /**
976  * Mirror editing API, usage:
977  *
978  * \code{.c}
979  * EDBM_verts_mirror_cache_begin(em, ...);
980  *
981  * BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
982  *     v_mirror = EDBM_verts_mirror_get(em, v);
983  *     e_mirror = EDBM_verts_mirror_get_edge(em, e);
984  *     f_mirror = EDBM_verts_mirror_get_face(em, f);
985  * }
986  *
987  * EDBM_verts_mirror_cache_end(em);
988  * \endcode
989  */
990
991 /* BM_SEARCH_MAXDIST is too big, copied from 2.6x MOC_THRESH, should become a
992  * preference */
993 #define BM_SEARCH_MAXDIST_MIRR 0.00002f
994 #define BM_CD_LAYER_ID "__mirror_index"
995 /**
996  * \param em: Editmesh.
997  * \param use_self: Allow a vertex to point to its self (middle verts).
998  * \param use_select: Restrict to selected verts.
999  * \param use_topology: Use topology mirror.
1000  * \param maxdist: Distance for close point test.
1001  * \param r_index: Optional array to write into, as an alternative to a customdata layer (length of total verts).
1002  */
1003 void EDBM_verts_mirror_cache_begin_ex(
1004         BMEditMesh *em, const int axis, const bool use_self, const bool use_select,
1005         /* extra args */
1006         const bool use_topology, float maxdist, int *r_index)
1007 {
1008         Mesh *me = (Mesh *)em->ob->data;
1009         BMesh *bm = em->bm;
1010         BMIter iter;
1011         BMVert *v;
1012         int cd_vmirr_offset = 0;
1013         int i;
1014         const float maxdist_sq = SQUARE(maxdist);
1015
1016         /* one or the other is used depending if topo is enabled */
1017         KDTree *tree = NULL;
1018         MirrTopoStore_t mesh_topo_store = {NULL, -1, -1, -1};
1019
1020         BM_mesh_elem_table_ensure(bm, BM_VERT);
1021
1022         if (r_index == NULL) {
1023                 const char *layer_id = BM_CD_LAYER_ID;
1024                 em->mirror_cdlayer = CustomData_get_named_layer_index(&bm->vdata, CD_PROP_INT, layer_id);
1025                 if (em->mirror_cdlayer == -1) {
1026                         BM_data_layer_add_named(bm, &bm->vdata, CD_PROP_INT, layer_id);
1027                         em->mirror_cdlayer = CustomData_get_named_layer_index(&bm->vdata, CD_PROP_INT, layer_id);
1028                 }
1029
1030                 cd_vmirr_offset = CustomData_get_n_offset(
1031                         &bm->vdata, CD_PROP_INT,
1032                         em->mirror_cdlayer - CustomData_get_layer_index(&bm->vdata, CD_PROP_INT));
1033
1034                 bm->vdata.layers[em->mirror_cdlayer].flag |= CD_FLAG_TEMPORARY;
1035         }
1036
1037         BM_mesh_elem_index_ensure(bm, BM_VERT);
1038
1039         if (use_topology) {
1040                 ED_mesh_mirrtopo_init(me, NULL, &mesh_topo_store, true);
1041         }
1042         else {
1043                 tree = BLI_kdtree_new(bm->totvert);
1044                 BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
1045                         BLI_kdtree_insert(tree, i, v->co);
1046                 }
1047                 BLI_kdtree_balance(tree);
1048         }
1049
1050 #define VERT_INTPTR(_v, _i) r_index ? &r_index[_i] : BM_ELEM_CD_GET_VOID_P(_v, cd_vmirr_offset);
1051
1052         BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
1053                 BLI_assert(BM_elem_index_get(v) == i);
1054
1055                 /* temporary for testing, check for selection */
1056                 if (use_select && !BM_elem_flag_test(v, BM_ELEM_SELECT)) {
1057                         /* do nothing */
1058                 }
1059                 else {
1060                         BMVert *v_mirr;
1061                         int *idx = VERT_INTPTR(v, i);
1062
1063                         if (use_topology) {
1064                                 v_mirr = cache_mirr_intptr_as_bmvert(mesh_topo_store.index_lookup, i);
1065                         }
1066                         else {
1067                                 int i_mirr;
1068                                 float co[3];
1069                                 copy_v3_v3(co, v->co);
1070                                 co[axis] *= -1.0f;
1071
1072                                 v_mirr = NULL;
1073                                 i_mirr = BLI_kdtree_find_nearest(tree, co, NULL);
1074                                 if (i_mirr != -1) {
1075                                         BMVert *v_test = BM_vert_at_index(bm, i_mirr);
1076                                         if (len_squared_v3v3(co, v_test->co) < maxdist_sq) {
1077                                                 v_mirr = v_test;
1078                                         }
1079                                 }
1080                         }
1081
1082                         if (v_mirr && (use_self || (v_mirr != v))) {
1083                                 const int i_mirr = BM_elem_index_get(v_mirr);
1084                                 *idx = i_mirr;
1085                                 idx = VERT_INTPTR(v_mirr, i_mirr);
1086                                 *idx = i;
1087                         }
1088                         else {
1089                                 *idx = -1;
1090                         }
1091                 }
1092
1093         }
1094
1095 #undef VERT_INTPTR
1096
1097         if (use_topology) {
1098                 ED_mesh_mirrtopo_free(&mesh_topo_store);
1099         }
1100         else {
1101                 BLI_kdtree_free(tree);
1102         }
1103 }
1104
1105 void EDBM_verts_mirror_cache_begin(
1106         BMEditMesh *em, const int axis,
1107         const bool use_self, const bool use_select,
1108         const bool use_topology)
1109 {
1110         EDBM_verts_mirror_cache_begin_ex(
1111                 em, axis,
1112                 use_self, use_select,
1113                 /* extra args */
1114                 use_topology, BM_SEARCH_MAXDIST_MIRR, NULL);
1115 }
1116
1117 BMVert *EDBM_verts_mirror_get(BMEditMesh *em, BMVert *v)
1118 {
1119         const int *mirr = CustomData_bmesh_get_layer_n(&em->bm->vdata, v->head.data, em->mirror_cdlayer);
1120
1121         BLI_assert(em->mirror_cdlayer != -1); /* invalid use */
1122
1123         if (mirr && *mirr >= 0 && *mirr < em->bm->totvert) {
1124                 if (!em->bm->vtable) {
1125                         printf("err: should only be called between "
1126                                "EDBM_verts_mirror_cache_begin and EDBM_verts_mirror_cache_end");
1127                         return NULL;
1128                 }
1129
1130                 return em->bm->vtable[*mirr];
1131         }
1132
1133         return NULL;
1134 }
1135
1136 BMEdge *EDBM_verts_mirror_get_edge(BMEditMesh *em, BMEdge *e)
1137 {
1138         BMVert *v1_mirr = EDBM_verts_mirror_get(em, e->v1);
1139         if (v1_mirr) {
1140                 BMVert *v2_mirr = EDBM_verts_mirror_get(em, e->v2);
1141                 if (v2_mirr) {
1142                         return BM_edge_exists(v1_mirr, v2_mirr);
1143                 }
1144         }
1145
1146         return NULL;
1147 }
1148
1149 BMFace *EDBM_verts_mirror_get_face(BMEditMesh *em, BMFace *f)
1150 {
1151         BMVert **v_mirr_arr = BLI_array_alloca(v_mirr_arr, f->len);
1152
1153         BMLoop *l_iter, *l_first;
1154         unsigned int i = 0;
1155
1156         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1157         do {
1158                 if ((v_mirr_arr[i++] = EDBM_verts_mirror_get(em, l_iter->v)) == NULL) {
1159                         return NULL;
1160                 }
1161         } while ((l_iter = l_iter->next) != l_first);
1162
1163         return BM_face_exists(v_mirr_arr, f->len);
1164 }
1165
1166 void EDBM_verts_mirror_cache_clear(BMEditMesh *em, BMVert *v)
1167 {
1168         int *mirr = CustomData_bmesh_get_layer_n(&em->bm->vdata, v->head.data, em->mirror_cdlayer);
1169
1170         BLI_assert(em->mirror_cdlayer != -1); /* invalid use */
1171
1172         if (mirr) {
1173                 *mirr = -1;
1174         }
1175 }
1176
1177 void EDBM_verts_mirror_cache_end(BMEditMesh *em)
1178 {
1179         em->mirror_cdlayer = -1;
1180 }
1181
1182 void EDBM_verts_mirror_apply(BMEditMesh *em, const int sel_from, const int sel_to)
1183 {
1184         BMIter iter;
1185         BMVert *v;
1186
1187         BLI_assert((em->bm->vtable != NULL) && ((em->bm->elem_table_dirty & BM_VERT) == 0));
1188
1189         BM_ITER_MESH (v, &iter, em->bm, BM_VERTS_OF_MESH) {
1190                 if (BM_elem_flag_test(v, BM_ELEM_SELECT) == sel_from) {
1191                         BMVert *mirr = EDBM_verts_mirror_get(em, v);
1192                         if (mirr) {
1193                                 if (BM_elem_flag_test(mirr, BM_ELEM_SELECT) == sel_to) {
1194                                         copy_v3_v3(mirr->co, v->co);
1195                                         mirr->co[0] *= -1.0f;
1196                                 }
1197                         }
1198                 }
1199         }
1200 }
1201
1202 /** \} */
1203
1204 /* -------------------------------------------------------------------- */
1205 /** \name Hide/Reveal API
1206  * \{ */
1207
1208 /* swap is 0 or 1, if 1 it hides not selected */
1209 void EDBM_mesh_hide(BMEditMesh *em, bool swap)
1210 {
1211         BMIter iter;
1212         BMElem *ele;
1213         int itermode;
1214         char hflag_swap = swap ? BM_ELEM_SELECT : 0;
1215
1216         if (em == NULL) return;
1217
1218         if (em->selectmode & SCE_SELECT_VERTEX)
1219                 itermode = BM_VERTS_OF_MESH;
1220         else if (em->selectmode & SCE_SELECT_EDGE)
1221                 itermode = BM_EDGES_OF_MESH;
1222         else
1223                 itermode = BM_FACES_OF_MESH;
1224
1225         BM_ITER_MESH (ele, &iter, em->bm, itermode) {
1226                 if (BM_elem_flag_test(ele, BM_ELEM_SELECT) ^ hflag_swap)
1227                         BM_elem_hide_set(em->bm, ele, true);
1228         }
1229
1230         EDBM_selectmode_flush(em);
1231
1232         /* original hide flushing comment (OUTDATED):
1233          * hide happens on least dominant select mode, and flushes up, not down!
1234          * (helps preventing errors in subsurf) */
1235         /* - vertex hidden, always means edge is hidden too
1236          * - edge hidden, always means face is hidden too
1237          * - face hidden, only set face hide
1238          * - then only flush back down what's absolute hidden
1239          */
1240 }
1241
1242 void EDBM_mesh_reveal(BMEditMesh *em, bool select)
1243 {
1244         const char iter_types[3] = {
1245                 BM_VERTS_OF_MESH,
1246                 BM_EDGES_OF_MESH,
1247                 BM_FACES_OF_MESH,
1248         };
1249
1250         const bool sels[3] = {
1251                 (em->selectmode & SCE_SELECT_VERTEX) != 0,
1252                 (em->selectmode & SCE_SELECT_EDGE) != 0,
1253                 (em->selectmode & SCE_SELECT_FACE) != 0,
1254         };
1255         int i;
1256
1257         /* Use tag flag to remember what was hidden before all is revealed.
1258          * BM_ELEM_HIDDEN --> BM_ELEM_TAG */
1259         for (i = 0; i < 3; i++) {
1260                 BMIter iter;
1261                 BMElem *ele;
1262
1263                 BM_ITER_MESH (ele, &iter, em->bm, iter_types[i]) {
1264                         BM_elem_flag_set(ele, BM_ELEM_TAG, BM_elem_flag_test(ele, BM_ELEM_HIDDEN));
1265                 }
1266         }
1267
1268         /* Reveal everything */
1269         EDBM_flag_disable_all(em, BM_ELEM_HIDDEN);
1270
1271         /* Select relevant just-revealed elements */
1272         for (i = 0; i < 3; i++) {
1273                 BMIter iter;
1274                 BMElem *ele;
1275
1276                 if (!sels[i]) {
1277                         continue;
1278                 }
1279
1280                 BM_ITER_MESH (ele, &iter, em->bm, iter_types[i]) {
1281                         if (BM_elem_flag_test(ele, BM_ELEM_TAG)) {
1282                                 BM_elem_select_set(em->bm, ele, select);
1283                         }
1284                 }
1285         }
1286
1287         EDBM_selectmode_flush(em);
1288
1289         /* hidden faces can have invalid normals */
1290         EDBM_mesh_normals_update(em);
1291 }
1292
1293 /** \} */
1294
1295 /* -------------------------------------------------------------------- */
1296 /** \name Update API
1297  * \{ */
1298
1299 void EDBM_mesh_normals_update(BMEditMesh *em)
1300 {
1301         BM_mesh_normals_update(em->bm);
1302 }
1303
1304 void EDBM_stats_update(BMEditMesh *em)
1305 {
1306         const char iter_types[3] = {
1307                 BM_VERTS_OF_MESH,
1308                 BM_EDGES_OF_MESH,
1309                 BM_FACES_OF_MESH,
1310         };
1311
1312         BMIter iter;
1313         BMElem *ele;
1314         int *tots[3];
1315         int i;
1316
1317         tots[0] = &em->bm->totvertsel;
1318         tots[1] = &em->bm->totedgesel;
1319         tots[2] = &em->bm->totfacesel;
1320
1321         em->bm->totvertsel = em->bm->totedgesel = em->bm->totfacesel = 0;
1322
1323         for (i = 0; i < 3; i++) {
1324                 ele = BM_iter_new(&iter, em->bm, iter_types[i], NULL);
1325                 for ( ; ele; ele = BM_iter_step(&iter)) {
1326                         if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
1327                                 (*tots[i])++;
1328                         }
1329                 }
1330         }
1331 }
1332
1333 /* so many tools call these that we better make it a generic function.
1334  */
1335 void EDBM_update_generic(BMEditMesh *em, const bool do_tessface, const bool is_destructive)
1336 {
1337         Object *ob = em->ob;
1338         /* order of calling isn't important */
1339         DEG_id_tag_update(ob->data, ID_RECALC_GEOMETRY);
1340         WM_main_add_notifier(NC_GEOM | ND_DATA, ob->data);
1341
1342         if (do_tessface) {
1343                 BKE_editmesh_tessface_calc(em);
1344         }
1345
1346         if (is_destructive) {
1347                 /* TODO. we may be able to remove this now! - Campbell */
1348                 // BM_mesh_elem_table_free(em->bm, BM_ALL_NOLOOP);
1349         }
1350         else {
1351                 /* in debug mode double check we didn't need to recalculate */
1352                 BLI_assert(BM_mesh_elem_table_check(em->bm) == true);
1353         }
1354         if (em->bm->spacearr_dirty & BM_SPACEARR_BMO_SET) {
1355                 BM_lnorspace_invalidate(em->bm, false);
1356                 em->bm->spacearr_dirty &= ~BM_SPACEARR_BMO_SET;
1357         }
1358         /* don't keep stale derivedMesh data around, see: [#38872] */
1359         BKE_editmesh_free_derivedmesh(em);
1360
1361 #ifdef DEBUG
1362         {
1363                 BMEditSelection *ese;
1364                 for (ese = em->bm->selected.first; ese; ese = ese->next) {
1365                         BLI_assert(BM_elem_flag_test(ese->ele, BM_ELEM_SELECT));
1366                 }
1367         }
1368 #endif
1369 }
1370
1371 /** \} */
1372
1373 /* -------------------------------------------------------------------- */
1374 /** \name Operator Helpers
1375  * \{ */
1376
1377 /* poll call for mesh operators requiring a view3d context */
1378 bool EDBM_view3d_poll(bContext *C)
1379 {
1380         if (ED_operator_editmesh(C) && ED_operator_view3d_active(C)) {
1381                 return 1;
1382         }
1383
1384         return 0;
1385 }
1386
1387 /** \} */
1388
1389 /* -------------------------------------------------------------------- */
1390 /** \name BMesh Element API
1391  * \{ */
1392
1393 BMElem *EDBM_elem_from_selectmode(BMEditMesh *em, BMVert *eve, BMEdge *eed, BMFace *efa)
1394 {
1395         BMElem *ele = NULL;
1396
1397         if ((em->selectmode & SCE_SELECT_VERTEX) && eve) {
1398                 ele = (BMElem *)eve;
1399         }
1400         else if ((em->selectmode & SCE_SELECT_EDGE) && eed) {
1401                 ele = (BMElem *)eed;
1402         }
1403         else if ((em->selectmode & SCE_SELECT_FACE) && efa) {
1404                 ele = (BMElem *)efa;
1405         }
1406
1407         return ele;
1408 }
1409
1410 /**
1411  * Used when we want to store a single index for any vert/edge/face.
1412  *
1413  * Intended for use with operators.
1414  */
1415 int EDBM_elem_to_index_any(BMEditMesh *em, BMElem *ele)
1416 {
1417         BMesh *bm = em->bm;
1418         int index = BM_elem_index_get(ele);
1419
1420         if (ele->head.htype == BM_VERT) {
1421                 BLI_assert(!(bm->elem_index_dirty & BM_VERT));
1422         }
1423         else if (ele->head.htype == BM_EDGE) {
1424                 BLI_assert(!(bm->elem_index_dirty & BM_EDGE));
1425                 index += bm->totvert;
1426         }
1427         else if (ele->head.htype == BM_FACE) {
1428                 BLI_assert(!(bm->elem_index_dirty & BM_FACE));
1429                 index += bm->totvert + bm->totedge;
1430         }
1431         else {
1432                 BLI_assert(0);
1433         }
1434
1435         return index;
1436 }
1437
1438 BMElem *EDBM_elem_from_index_any(BMEditMesh *em, int index)
1439 {
1440         BMesh *bm = em->bm;
1441
1442         if (index < bm->totvert) {
1443                 return (BMElem *)BM_vert_at_index_find_or_table(bm, index);
1444         }
1445         index -= bm->totvert;
1446         if (index < bm->totedge) {
1447                 return (BMElem *)BM_edge_at_index_find_or_table(bm, index);
1448         }
1449         index -= bm->totedge;
1450         if (index < bm->totface) {
1451                 return (BMElem *)BM_face_at_index_find_or_table(bm, index);
1452         }
1453
1454         return NULL;
1455 }
1456
1457 /** \} */
1458
1459 /* -------------------------------------------------------------------- */
1460 /** \name BMesh BVH API
1461  * \{ */
1462
1463 static BMFace *edge_ray_cast(struct BMBVHTree *tree, const float co[3], const float dir[3], float *r_hitout, BMEdge *e)
1464 {
1465         BMFace *f = BKE_bmbvh_ray_cast(tree, co, dir, 0.0f, NULL, r_hitout, NULL);
1466
1467         if (f && BM_edge_in_face(e, f)) {
1468                 return NULL;
1469         }
1470
1471         return f;
1472 }
1473
1474 static void scale_point(float c1[3], const float p[3], const float s)
1475 {
1476         sub_v3_v3(c1, p);
1477         mul_v3_fl(c1, s);
1478         add_v3_v3(c1, p);
1479 }
1480
1481 bool BMBVH_EdgeVisible(struct BMBVHTree *tree, BMEdge *e,
1482                        struct Depsgraph *depsgraph,
1483                        ARegion *ar, View3D *v3d, Object *obedit)
1484 {
1485         BMFace *f;
1486         float co1[3], co2[3], co3[3], dir1[3], dir2[3], dir3[3];
1487         float origin[3], invmat[4][4];
1488         float epsilon = 0.01f;
1489         float end[3];
1490         const float mval_f[2] = {
1491                 ar->winx / 2.0f,
1492                 ar->winy / 2.0f,
1493         };
1494
1495         ED_view3d_win_to_segment_clipped(depsgraph, ar, v3d, mval_f, origin, end, false);
1496
1497         invert_m4_m4(invmat, obedit->obmat);
1498         mul_m4_v3(invmat, origin);
1499
1500         copy_v3_v3(co1, e->v1->co);
1501         mid_v3_v3v3(co2, e->v1->co, e->v2->co);
1502         copy_v3_v3(co3, e->v2->co);
1503
1504         scale_point(co1, co2, 0.99);
1505         scale_point(co3, co2, 0.99);
1506
1507         /* ok, idea is to generate rays going from the camera origin to the
1508          * three points on the edge (v1, mid, v2)*/
1509         sub_v3_v3v3(dir1, origin, co1);
1510         sub_v3_v3v3(dir2, origin, co2);
1511         sub_v3_v3v3(dir3, origin, co3);
1512
1513         normalize_v3_length(dir1, epsilon);
1514         normalize_v3_length(dir2, epsilon);
1515         normalize_v3_length(dir3, epsilon);
1516
1517         /* offset coordinates slightly along view vectors, to avoid
1518          * hitting the faces that own the edge.*/
1519         add_v3_v3v3(co1, co1, dir1);
1520         add_v3_v3v3(co2, co2, dir2);
1521         add_v3_v3v3(co3, co3, dir3);
1522
1523         normalize_v3(dir1);
1524         normalize_v3(dir2);
1525         normalize_v3(dir3);
1526
1527         /* do three samplings: left, middle, right */
1528         f = edge_ray_cast(tree, co1, dir1, NULL, e);
1529         if (f && !edge_ray_cast(tree, co2, dir2, NULL, e)) {
1530                 return true;
1531         }
1532         else if (f && !edge_ray_cast(tree, co3, dir3, NULL, e)) {
1533                 return true;
1534         }
1535         else if (!f) {
1536                 return true;
1537         }
1538
1539         return false;
1540 }
1541
1542 /** \} */