bmesh code cleanup
[blender.git] / source / blender / bmesh / operators / bmo_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  * Contributor(s): Joseph Eagar.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 #include "MEM_guardedalloc.h"
24
25 #include "DNA_meshdata_types.h"
26
27 #include "BLI_math.h"
28 #include "BLI_array.h"
29 #include "BLI_heap.h"
30
31 #include "BKE_customdata.h"
32
33 #include "bmesh.h"
34
35 #include "bmesh_operators_private.h" /* own include */
36
37 /*
38  * UTILS.C
39  *
40  * utility bmesh operators, e.g. transform,
41  * translate, rotate, scale, etc.
42  *
43  */
44
45 void bmesh_makevert_exec(BMesh *bm, BMOperator *op)
46 {
47         float vec[3];
48
49         BMO_slot_vec_get(op, "co", vec);
50
51         BMO_elem_flag_enable(bm, BM_vert_create(bm, vec, NULL), 1);
52         BMO_slot_from_flag(bm, op, "newvertout", 1, BM_VERT);
53 }
54
55 void bmesh_transform_exec(BMesh *bm, BMOperator *op)
56 {
57         BMOIter iter;
58         BMVert *v;
59         float mat[4][4];
60
61         BMO_slot_mat4_get(op, "mat", mat);
62
63         BMO_ITER(v, &iter, bm, op, "verts", BM_VERT) {
64                 mul_m4_v3(mat, v->co);
65         }
66 }
67
68 void bmesh_translate_exec(BMesh *bm, BMOperator *op)
69 {
70         float mat[4][4], vec[3];
71         
72         BMO_slot_vec_get(op, "vec", vec);
73
74         unit_m4(mat);
75         copy_v3_v3(mat[3], vec);
76
77         BMO_op_callf(bm, "transform mat=%m4 verts=%s", mat, op, "verts");
78 }
79
80 void bmesh_scale_exec(BMesh *bm, BMOperator *op)
81 {
82         float mat[3][3], vec[3];
83         
84         BMO_slot_vec_get(op, "vec", vec);
85
86         unit_m3(mat);
87         mat[0][0] = vec[0];
88         mat[1][1] = vec[1];
89         mat[2][2] = vec[2];
90
91         BMO_op_callf(bm, "transform mat=%m3 verts=%s", mat, op, "verts");
92 }
93
94 void bmesh_rotate_exec(BMesh *bm, BMOperator *op)
95 {
96         float vec[3];
97         
98         BMO_slot_vec_get(op, "cent", vec);
99         
100         /* there has to be a proper matrix way to do this, but
101          * this is how editmesh did it and I'm too tired to think
102          * through the math right now. */
103         mul_v3_fl(vec, -1.0f);
104         BMO_op_callf(bm, "translate verts=%s vec=%v", op, "verts", vec);
105
106         BMO_op_callf(bm, "transform mat=%s verts=%s", op, "mat", op, "verts");
107
108         mul_v3_fl(vec, -1.0f);
109         BMO_op_callf(bm, "translate verts=%s vec=%v", op, "verts", vec);
110 }
111
112 void bmesh_reversefaces_exec(BMesh *bm, BMOperator *op)
113 {
114         BMOIter siter;
115         BMFace *f;
116
117         BMO_ITER(f, &siter, bm, op, "faces", BM_FACE) {
118                 BM_face_normal_flip(bm, f);
119         }
120 }
121
122 void bmesh_edgerotate_exec(BMesh *bm, BMOperator *op)
123 {
124         BMOIter siter;
125         BMEdge *e, *e2;
126         int ccw = BMO_slot_bool_get(op, "ccw");
127
128         BMO_ITER(e, &siter, bm, op, "edges", BM_EDGE) {
129                 if (!(e2 = BM_edge_rotate(bm, e, ccw))) {
130                         BMO_error_raise(bm, op, BMERR_INVALID_SELECTION, "Could not rotate edge");
131                         return;
132                 }
133
134                 BMO_elem_flag_enable(bm, e2, 1);
135         }
136
137         BMO_slot_from_flag(bm, op, "edgeout", 1, BM_EDGE);
138 }
139
140 #define SEL_FLAG        1
141 #define SEL_ORIG        2
142
143 static void bmesh_regionextend_extend(BMesh *bm, BMOperator *op, int usefaces)
144 {
145         BMVert *v;
146         BMEdge *e;
147         BMIter eiter;
148         BMOIter siter;
149
150         if (!usefaces) {
151                 BMO_ITER(v, &siter, bm, op, "geom", BM_VERT) {
152                         BM_ITER(e, &eiter, bm, BM_EDGES_OF_VERT, v) {
153                                 if (!BMO_elem_flag_test(bm, e, SEL_ORIG))
154                                         break;
155                         }
156
157                         if (e) {
158                                 BM_ITER(e, &eiter, bm, BM_EDGES_OF_VERT, v) {
159                                         BMO_elem_flag_enable(bm, e, SEL_FLAG);
160                                         BMO_elem_flag_enable(bm, BM_edge_other_vert(e, v), SEL_FLAG);
161                                 }
162                         }
163                 }
164         }
165         else {
166                 BMIter liter, fiter;
167                 BMFace *f, *f2;
168                 BMLoop *l;
169
170                 BMO_ITER(f, &siter, bm, op, "geom", BM_FACE) {
171                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
172                                 BM_ITER(f2, &fiter, bm, BM_FACES_OF_EDGE, l->e) {
173                                         if (!BMO_elem_flag_test(bm, f2, SEL_ORIG)) {
174                                                 BMO_elem_flag_enable(bm, f2, SEL_FLAG);
175                                         }
176                                 }
177                         }
178                 }
179         }
180 }
181
182 static void bmesh_regionextend_constrict(BMesh *bm, BMOperator *op, int usefaces)
183 {
184         BMVert *v;
185         BMEdge *e;
186         BMIter eiter;
187         BMOIter siter;
188
189         if (!usefaces) {
190                 BMO_ITER(v, &siter, bm, op, "geom", BM_VERT) {
191                         BM_ITER(e, &eiter, bm, BM_EDGES_OF_VERT, v) {
192                                 if (!BMO_elem_flag_test(bm, e, SEL_ORIG))
193                                         break;
194                         }
195
196                         if (e) {
197                                 BMO_elem_flag_enable(bm, v, SEL_FLAG);
198
199                                 BM_ITER(e, &eiter, bm, BM_EDGES_OF_VERT, v) {
200                                         BMO_elem_flag_enable(bm, e, SEL_FLAG);
201                                 }
202
203                         }
204                 }
205         }
206         else {
207                 BMIter liter, fiter;
208                 BMFace *f, *f2;
209                 BMLoop *l;
210
211                 BMO_ITER(f, &siter, bm, op, "geom", BM_FACE) {
212                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
213                                 BM_ITER(f2, &fiter, bm, BM_FACES_OF_EDGE, l->e) {
214                                         if (!BMO_elem_flag_test(bm, f2, SEL_ORIG)) {
215                                                 BMO_elem_flag_enable(bm, f, SEL_FLAG);
216                                                 break;
217                                         }
218                                 }
219                         }
220                 }
221         }
222 }
223
224 void bmesh_regionextend_exec(BMesh *bm, BMOperator *op)
225 {
226         int use_faces = BMO_slot_bool_get(op, "use_faces");
227         int constrict = BMO_slot_bool_get(op, "constrict");
228
229         BMO_slot_buffer_flag_enable(bm, op, "geom", SEL_ORIG, BM_ALL);
230
231         if (constrict)
232                 bmesh_regionextend_constrict(bm, op, use_faces);
233         else
234                 bmesh_regionextend_extend(bm, op, use_faces);
235
236         BMO_slot_from_flag(bm, op, "geomout", SEL_FLAG, BM_ALL);
237 }
238
239 /********* righthand faces implementation ****** */
240
241 #define FACE_VIS        1
242 #define FACE_FLAG       2
243 #define FACE_MARK       4
244 #define FACE_FLIP       8
245
246 /* NOTE: these are the original righthandfaces comment in editmesh_mods.c,
247  *       copied here for reference. */
248
249 /* based at a select-connected to witness loose objects */
250
251 /* count per edge the amount of faces
252  * find the ultimate left, front, upper face (not manhattan dist!!)
253  * also evaluate both triangle cases in quad, since these can be non-flat
254  *
255  * put normal to the outside, and set the first direction flags in edges
256  *
257  * then check the object, and set directions / direction-flags: but only for edges with 1 or 2 faces
258  * this is in fact the 'select connected'
259  *
260  * in case (selected) faces were not done: start over with 'find the ultimate ...' */
261
262 /* NOTE: this function uses recursion, which is a little unusual for a bmop
263  *       function, but acceptable I think. */
264
265 /* NOTE: BM_ELEM_TAG is used on faces to tell if they are flipped. */
266
267 void bmesh_righthandfaces_exec(BMesh *bm, BMOperator *op)
268 {
269         BMIter liter, liter2;
270         BMOIter siter;
271         BMFace *f, *startf, **fstack = NULL;
272         BLI_array_declare(fstack);
273         BMLoop *l, *l2;
274         float maxx, maxx_test, cent[3];
275         int i, maxi, flagflip = BMO_slot_bool_get(op, "do_flip");
276
277         startf = NULL;
278         maxx = -1.0e10;
279         
280         BMO_slot_buffer_flag_enable(bm, op, "faces", FACE_FLAG, BM_FACE);
281
282         /* find a starting face */
283         BMO_ITER(f, &siter, bm, op, "faces", BM_FACE) {
284
285                 /* clear dirty flag */
286                 BM_elem_flag_disable(f, BM_ELEM_TAG);
287
288                 if (BMO_elem_flag_test(bm, f, FACE_VIS))
289                         continue;
290
291                 if (!startf) startf = f;
292
293                 BM_face_center_bounds_calc(bm, f, cent);
294
295                 if ((maxx_test = dot_v3v3(cent, cent)) > maxx) {
296                         maxx = maxx_test;
297                         startf = f;
298                 }
299         }
300
301         if (!startf) return;
302
303         BM_face_center_bounds_calc(bm, startf, cent);
304
305         /* make sure the starting face has the correct winding */
306         if (dot_v3v3(cent, startf->no) < 0.0f) {
307                 BM_face_normal_flip(bm, startf);
308                 BMO_elem_flag_toggle(bm, startf, FACE_FLIP);
309
310                 if (flagflip)
311                         BM_elem_flag_toggle(startf, BM_ELEM_TAG);
312         }
313         
314         /* now that we've found our starting face, make all connected faces
315          * have the same winding.  this is done recursively, using a manual
316          * stack (if we use simple function recursion, we'd end up overloading
317          * the stack on large meshes). */
318
319         BLI_array_growone(fstack);
320         fstack[0] = startf;
321         BMO_elem_flag_enable(bm, startf, FACE_VIS);
322
323         i = 0;
324         maxi = 1;
325         while (i >= 0) {
326                 f = fstack[i];
327                 i--;
328
329                 BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
330                         BM_ITER(l2, &liter2, bm, BM_LOOPS_OF_LOOP, l) {
331                                 if (!BMO_elem_flag_test(bm, l2->f, FACE_FLAG) || l2 == l)
332                                         continue;
333
334                                 if (!BMO_elem_flag_test(bm, l2->f, FACE_VIS)) {
335                                         BMO_elem_flag_enable(bm, l2->f, FACE_VIS);
336                                         i++;
337                                         
338                                         if (l2->v == l->v) {
339                                                 BM_face_normal_flip(bm, l2->f);
340                                                 
341                                                 BMO_elem_flag_toggle(bm, l2->f, FACE_FLIP);
342                                                 if (flagflip)
343                                                         BM_elem_flag_toggle(l2->f, BM_ELEM_TAG);
344                                         }
345                                         else if (BM_elem_flag_test(l2->f, BM_ELEM_TAG) || BM_elem_flag_test(l->f, BM_ELEM_TAG)) {
346                                                 if (flagflip) {
347                                                         BM_elem_flag_disable(l->f, BM_ELEM_TAG);
348                                                         BM_elem_flag_disable(l2->f, BM_ELEM_TAG);
349                                                 }
350                                         }
351                                         
352                                         if (i == maxi) {
353                                                 BLI_array_growone(fstack);
354                                                 maxi++;
355                                         }
356
357                                         fstack[i] = l2->f;
358                                 }
359                         }
360                 }
361         }
362
363         BLI_array_free(fstack);
364
365         /* check if we have faces yet to do.  if so, recurse */
366         BMO_ITER(f, &siter, bm, op, "faces", BM_FACE) {
367                 if (!BMO_elem_flag_test(bm, f, FACE_VIS)) {
368                         bmesh_righthandfaces_exec(bm, op);
369                         break;
370                 }
371         }
372 }
373
374 void bmesh_vertexsmooth_exec(BMesh *bm, BMOperator *op)
375 {
376         BMOIter siter;
377         BMIter iter;
378         BMVert *v;
379         BMEdge *e;
380         BLI_array_declare(cos);
381         float (*cos)[3] = NULL;
382         float *co, *co2, clipdist = BMO_slot_float_get(op, "clipdist");
383         int i, j, clipx, clipy, clipz;
384         
385         clipx = BMO_slot_bool_get(op, "mirror_clip_x");
386         clipy = BMO_slot_bool_get(op, "mirror_clip_y");
387         clipz = BMO_slot_bool_get(op, "mirror_clip_z");
388
389         i = 0;
390         BMO_ITER(v, &siter, bm, op, "verts", BM_VERT) {
391                 BLI_array_growone(cos);
392                 co = cos[i];
393                 
394                 j  = 0;
395                 BM_ITER(e, &iter, bm, BM_EDGES_OF_VERT, v) {
396                         co2 = BM_edge_other_vert(e, v)->co;
397                         add_v3_v3v3(co, co, co2);
398                         j += 1;
399                 }
400                 
401                 if (!j) {
402                         copy_v3_v3(co, v->co);
403                         i++;
404                         continue;
405                 }
406
407                 mul_v3_fl(co, 1.0f / (float)j);
408                 mid_v3_v3v3(co, co, v->co);
409
410                 if (clipx && fabsf(v->co[0]) <= clipdist)
411                         co[0] = 0.0f;
412                 if (clipy && fabsf(v->co[1]) <= clipdist)
413                         co[1] = 0.0f;
414                 if (clipz && fabsf(v->co[2]) <= clipdist)
415                         co[2] = 0.0f;
416
417                 i++;
418         }
419
420         i = 0;
421         BMO_ITER(v, &siter, bm, op, "verts", BM_VERT) {
422                 copy_v3_v3(v->co, cos[i]);
423                 i++;
424         }
425
426         BLI_array_free(cos);
427 }
428
429 /*
430  * compute the perimeter of an ngon
431  *
432  * NOTE: This should probably go to bmesh_polygon.c
433  */
434 static float ngon_perimeter(BMesh *bm, BMFace *f)
435 {
436         BMIter  liter;
437         BMLoop  *l;
438         int             num_verts = 0;
439         float   v[3], sv[3];
440         float   perimeter = 0.0f;
441
442         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
443                 if (num_verts == 0) {
444                         copy_v3_v3(v, l->v->co);
445                         copy_v3_v3(sv, l->v->co);
446                 }
447                 else {
448                         perimeter += len_v3v3(v, l->v->co);
449                         copy_v3_v3(v, l->v->co);
450                 }
451                 num_verts++;
452         }
453
454         perimeter += len_v3v3(v, sv);
455
456         return perimeter;
457 }
458
459 /*
460  * compute the fake surface of an ngon
461  * This is done by decomposing the ngon into triangles who share the centroid of the ngon
462  * while this method is far from being exact, it should garantee an invariance.
463  *
464  * NOTE: This should probably go to bmesh_polygon.c
465  */
466 static float ngon_fake_area(BMesh *bm, BMFace *f)
467 {
468         BMIter  liter;
469         BMLoop  *l;
470         int             num_verts = 0;
471         float   v[3], sv[3], c[3];
472         float   area = 0.0f;
473
474         BM_face_center_mean_calc(bm, f, c);
475
476         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
477                 if (num_verts == 0) {
478                         copy_v3_v3(v, l->v->co);
479                         copy_v3_v3(sv, l->v->co);
480                         num_verts++;
481                 }
482                 else {
483                         area += area_tri_v3(v, c, l->v->co);
484                         copy_v3_v3(v, l->v->co);
485                         num_verts++;
486                 }
487         }
488
489         area += area_tri_v3(v, c, sv);
490
491         return area;
492 }
493
494 /*
495  * extra face data (computed data)
496  */
497 typedef struct tmp_face_ext {
498         BMFace          *f;                     /* the face */
499         float   c[3];                   /* center */
500         union {
501                 float   area;           /* area */
502                 float   perim;          /* perimeter */
503                 float   d;                      /* 4th component of plane (the first three being the normal) */
504                 struct Image    *t;     /* image pointer */
505         };
506 } tmp_face_ext;
507
508 /*
509  * Select similar faces, the choices are in the enum in source/blender/bmesh/bmesh_operators.h
510  * We select either similar faces based on material, image, area, perimeter, normal, or the coplanar faces
511  */
512 void bmesh_similarfaces_exec(BMesh *bm, BMOperator *op)
513 {
514         BMIter fm_iter;
515         BMFace *fs, *fm;
516         BMOIter fs_iter;
517         int num_sels = 0, num_total = 0, i = 0, idx = 0;
518         float angle = 0.0f;
519         tmp_face_ext *f_ext = NULL;
520         int *indices = NULL;
521         float t_no[3];  /* temporary normal */
522         int type = BMO_slot_int_get(op, "type");
523         float thresh = BMO_slot_float_get(op, "thresh");
524
525         num_total = BM_mesh_elem_count(bm, BM_FACE);
526
527         /*
528         ** The first thing to do is to iterate through all the the selected items and mark them since
529         ** they will be in the selection anyway.
530         ** This will increase performance, (especially when the number of originaly selected faces is high)
531         ** so the overall complexity will be less than $O(mn)$ where is the total number of selected faces,
532         ** and n is the total number of faces
533         */
534         BMO_ITER(fs, &fs_iter, bm, op, "faces", BM_FACE) {
535                 if (!BMO_elem_flag_test(bm, fs, FACE_MARK)) {   /* is this really needed ? */
536                         BMO_elem_flag_enable(bm, fs, FACE_MARK);
537                         num_sels++;
538                 }
539         }
540
541         /* allocate memory for the selected faces indices and for all temporary faces */
542         indices = (int *)MEM_callocN(sizeof(int) * num_sels, "face indices util.c");
543         f_ext = (tmp_face_ext *)MEM_callocN(sizeof(tmp_face_ext) * num_total, "f_ext util.c");
544
545         /* loop through all the faces and fill the faces/indices structure */
546         BM_ITER(fm, &fm_iter, bm, BM_FACES_OF_MESH, NULL) {
547                 f_ext[i].f = fm;
548                 if (BMO_elem_flag_test(bm, fm, FACE_MARK)) {
549                         indices[idx] = i;
550                         idx++;
551                 }
552                 i++;
553         }
554
555         /*
556         ** Save us some computation burden: In case of perimeter/area/coplanar selection we compute
557         ** only once.
558         */
559         if (type == SIMFACE_PERIMETER || type == SIMFACE_AREA || type == SIMFACE_COPLANAR || type == SIMFACE_IMAGE) {
560                 for (i = 0; i < num_total; i++) {
561                         switch (type) {
562                                 case SIMFACE_PERIMETER:
563                                         /* set the perimeter */
564                                         f_ext[i].perim = ngon_perimeter(bm, f_ext[i].f);
565                                         break;
566
567                                 case SIMFACE_COPLANAR:
568                                         /* compute the center of the polygon */
569                                         BM_face_center_mean_calc(bm, f_ext[i].f, f_ext[i].c);
570
571                                         /* normalize the polygon normal */
572                                         copy_v3_v3(t_no, f_ext[i].f->no);
573                                         normalize_v3(t_no);
574
575                                         /* compute the plane distance */
576                                         f_ext[i].d = dot_v3v3(t_no, f_ext[i].c);
577                                         break;
578
579                                 case SIMFACE_AREA:
580                                         f_ext[i].area = ngon_fake_area(bm, f_ext[i].f);
581                                         break;
582
583                                 case SIMFACE_IMAGE:
584                                         f_ext[i].t = NULL;
585                                         if (CustomData_has_layer(&(bm->pdata), CD_MTEXPOLY)) {
586                                                 MTexPoly *mtpoly = CustomData_bmesh_get(&bm->pdata, f_ext[i].f->head.data, CD_MTEXPOLY);
587                                                 f_ext[i].t = mtpoly->tpage;
588                                         }
589                                         break;
590                         }
591                 }
592         }
593
594         /* now select the rest (if any) */
595         for (i = 0; i < num_total; i++) {
596                 fm = f_ext[i].f;
597                 if (!BMO_elem_flag_test(bm, fm, FACE_MARK)  && !BM_elem_flag_test(fm, BM_ELEM_HIDDEN)) {
598                         int cont = TRUE;
599                         for (idx = 0; idx < num_sels && cont == TRUE; idx++) {
600                                 fs = f_ext[indices[idx]].f;
601                                 switch (type) {
602                                         case SIMFACE_MATERIAL:
603                                                 if (fm->mat_nr == fs->mat_nr) {
604                                                         BMO_elem_flag_enable(bm, fm, FACE_MARK);
605                                                         cont = FALSE;
606                                                 }
607                                                 break;
608
609                                         case SIMFACE_IMAGE:
610                                                 if (f_ext[i].t == f_ext[indices[idx]].t) {
611                                                         BMO_elem_flag_enable(bm, fm, FACE_MARK);
612                                                         cont = FALSE;
613                                                 }
614                                                 break;
615
616                                         case SIMFACE_NORMAL:
617                                                 angle = RAD2DEGF(angle_v3v3(fs->no, fm->no));   /* if the angle between the normals -> 0 */
618                                                 if (angle / 180.0f <= thresh) {
619                                                         BMO_elem_flag_enable(bm, fm, FACE_MARK);
620                                                         cont = FALSE;
621                                                 }
622                                                 break;
623
624                                         case SIMFACE_COPLANAR:
625                                                 angle = RAD2DEGF(angle_v3v3(fs->no, fm->no)); /* angle -> 0 */
626                                                 if (angle / 180.0f <= thresh) { /* and dot product difference -> 0 */
627                                                         if (fabsf(f_ext[i].d - f_ext[indices[idx]].d) <= thresh) {
628                                                                 BMO_elem_flag_enable(bm, fm, FACE_MARK);
629                                                                 cont = FALSE;
630                                                         }
631                                                 }
632                                                 break;
633
634                                         case SIMFACE_AREA:
635                                                 if (fabsf(f_ext[i].area - f_ext[indices[idx]].area) <= thresh) {
636                                                         BMO_elem_flag_enable(bm, fm, FACE_MARK);
637                                                         cont = FALSE;
638                                                 }
639                                                 break;
640
641                                         case SIMFACE_PERIMETER:
642                                                 if (fabsf(f_ext[i].perim - f_ext[indices[idx]].perim) <= thresh) {
643                                                         BMO_elem_flag_enable(bm, fm, FACE_MARK);
644                                                         cont = FALSE;
645                                                 }
646                                                 break;
647                                 }
648                         }
649                 }
650         }
651
652         MEM_freeN(f_ext);
653         MEM_freeN(indices);
654
655         /* transfer all marked faces to the output slot */
656         BMO_slot_from_flag(bm, op, "faceout", FACE_MARK, BM_FACE);
657 }
658
659 /******************************************************************************
660 ** Similar Edges
661 **************************************************************************** */
662 #define EDGE_MARK       1
663
664 /*
665  * compute the angle of an edge (i.e. the angle between two faces)
666  */
667 static float edge_angle(BMesh *bm, BMEdge *e)
668 {
669         BMIter  fiter;
670         BMFace  *f, *f_prev = NULL;
671
672         /* first edge faces, dont account for 3+ */
673
674         BM_ITER(f, &fiter, bm, BM_FACES_OF_EDGE, e) {
675                 if (f_prev == NULL) {
676                         f_prev = f;
677                 }
678                 else {
679                         return angle_v3v3(f_prev->no, f->no);
680                 }
681         }
682
683         return 0.0f;
684 }
685 /*
686  * extra edge information
687  */
688 typedef struct tmp_edge_ext {
689         BMEdge          *e;
690         union {
691                 float           dir[3];
692                 float           angle;                  /* angle between the face */
693         };
694
695         union {
696                 float           length;                 /* edge length */
697                 int                     faces;                  /* faces count */
698         };
699 } tmp_edge_ext;
700
701 /*
702  * select similar edges: the choices are in the enum in source/blender/bmesh/bmesh_operators.h
703  * choices are length, direction, face, ...
704  */
705 void bmesh_similaredges_exec(BMesh *bm, BMOperator *op)
706 {
707         BMOIter es_iter;        /* selected edges iterator */
708         BMIter  e_iter;         /* mesh edges iterator */
709         BMEdge  *es;            /* selected edge */
710         BMEdge  *e;             /* mesh edge */
711         int idx = 0, i = 0 /* , f = 0 */;
712         int *indices = NULL;
713         tmp_edge_ext *e_ext = NULL;
714         // float *angles = NULL;
715         float angle;
716
717         int num_sels = 0, num_total = 0;
718         int type = BMO_slot_int_get(op, "type");
719         float thresh = BMO_slot_float_get(op, "thresh");
720
721         num_total = BM_mesh_elem_count(bm, BM_EDGE);
722
723         /* iterate through all selected edges and mark them */
724         BMO_ITER(es, &es_iter, bm, op, "edges", BM_EDGE) {
725                 BMO_elem_flag_enable(bm, es, EDGE_MARK);
726                 num_sels++;
727         }
728
729         /* allocate memory for the selected edges indices and for all temporary edges */
730         indices = (int *)MEM_callocN(sizeof(int) * num_sels, "indices util.c");
731         e_ext = (tmp_edge_ext *)MEM_callocN(sizeof(tmp_edge_ext) * num_total, "e_ext util.c");
732
733         /* loop through all the edges and fill the edges/indices structure */
734         BM_ITER(e, &e_iter, bm, BM_EDGES_OF_MESH, NULL) {
735                 e_ext[i].e = e;
736                 if (BMO_elem_flag_test(bm, e, EDGE_MARK)) {
737                         indices[idx] = i;
738                         idx++;
739                 }
740                 i++;
741         }
742
743         /* save us some computation time by doing heavy computation once */
744         if (type == SIMEDGE_LENGTH || type == SIMEDGE_FACE || type == SIMEDGE_DIR || type == SIMEDGE_FACE_ANGLE) {
745                 for (i = 0; i < num_total; i++) {
746                         switch (type) {
747                                 case SIMEDGE_LENGTH:    /* compute the length of the edge */
748                                         e_ext[i].length = len_v3v3(e_ext[i].e->v1->co, e_ext[i].e->v2->co);
749                                         break;
750
751                                 case SIMEDGE_DIR:               /* compute the direction */
752                                         sub_v3_v3v3(e_ext[i].dir, e_ext[i].e->v1->co, e_ext[i].e->v2->co);
753                                         break;
754
755                                 case SIMEDGE_FACE:              /* count the faces around the edge */
756                                         e_ext[i].faces  = BM_edge_face_count(e_ext[i].e);
757                                         break;
758
759                                 case SIMEDGE_FACE_ANGLE:
760                                         e_ext[i].faces  = BM_edge_face_count(e_ext[i].e);
761                                         if (e_ext[i].faces == 2)
762                                                 e_ext[i].angle = edge_angle(bm, e_ext[i].e);
763                                         break;
764                         }
765                 }
766         }
767
768         /* select the edges if any */
769         for (i = 0; i < num_total; i++) {
770                 e = e_ext[i].e;
771                 if (!BMO_elem_flag_test(bm, e, EDGE_MARK) && !BM_elem_flag_test(e, BM_ELEM_HIDDEN)) {
772                         int cont = TRUE;
773                         for (idx = 0; idx < num_sels && cont == TRUE; idx++) {
774                                 es = e_ext[indices[idx]].e;
775                                 switch (type) {
776                                         case SIMEDGE_LENGTH:
777                                                 if (fabsf(e_ext[i].length - e_ext[indices[idx]].length) <= thresh) {
778                                                         BMO_elem_flag_enable(bm, e, EDGE_MARK);
779                                                         cont = FALSE;
780                                                 }
781                                                 break;
782
783                                         case SIMEDGE_DIR:
784                                                 /* compute the angle between the two edges */
785                                                 angle = RAD2DEGF(angle_v3v3(e_ext[i].dir, e_ext[indices[idx]].dir));
786
787                                                 if (angle > 90.0f) /* use the smallest angle between the edges */
788                                                         angle = fabsf(angle - 180.0f);
789
790                                                 if (angle / 90.0f <= thresh) {
791                                                         BMO_elem_flag_enable(bm, e, EDGE_MARK);
792                                                         cont = FALSE;
793                                                 }
794                                                 break;
795
796                                         case SIMEDGE_FACE:
797                                                 if (e_ext[i].faces == e_ext[indices[idx]].faces) {
798                                                         BMO_elem_flag_enable(bm, e, EDGE_MARK);
799                                                         cont = FALSE;
800                                                 }
801                                                 break;
802
803                                         case SIMEDGE_FACE_ANGLE:
804                                                 if (e_ext[i].faces == 2) {
805                                                         if (e_ext[indices[idx]].faces == 2) {
806                                                                 if (fabsf(e_ext[i].angle - e_ext[indices[idx]].angle) <= thresh) {
807                                                                         BMO_elem_flag_enable(bm, e, EDGE_MARK);
808                                                                         cont = FALSE;
809                                                                 }
810                                                         }
811                                                 }
812                                                 else {
813                                                         cont = FALSE;
814                                                 }
815                                                 break;
816
817                                         case SIMEDGE_CREASE:
818                                                 if (CustomData_has_layer(&bm->edata, CD_CREASE)) {
819                                                         float *c1, *c2;
820
821                                                         c1 = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
822                                                         c2 = CustomData_bmesh_get(&bm->edata, es->head.data, CD_CREASE);
823
824                                                         if (c1 && c2 && fabsf(*c1 - *c2) <= thresh) {
825                                                                 BMO_elem_flag_enable(bm, e, EDGE_MARK);
826                                                                 cont = FALSE;
827                                                         }
828                                                 }
829                                                 break;
830
831                                         case SIMEDGE_SEAM:
832                                                 if (BM_elem_flag_test(e, BM_ELEM_SEAM) == BM_elem_flag_test(es, BM_ELEM_SEAM)) {
833                                                         BMO_elem_flag_enable(bm, e, EDGE_MARK);
834                                                         cont = FALSE;
835                                                 }
836                                                 break;
837
838                                         case SIMEDGE_SHARP:
839                                                 if (BM_elem_flag_test(e, BM_ELEM_SMOOTH) == BM_elem_flag_test(es, BM_ELEM_SMOOTH)) {
840                                                         BMO_elem_flag_enable(bm, e, EDGE_MARK);
841                                                         cont = FALSE;
842                                                 }
843                                                 break;
844                                 }
845                         }
846                 }
847         }
848
849         MEM_freeN(e_ext);
850         MEM_freeN(indices);
851
852         /* transfer all marked edges to the output slot */
853         BMO_slot_from_flag(bm, op, "edgeout", EDGE_MARK, BM_EDGE);
854 }
855
856 /******************************************************************************
857 ** Similar Vertices
858 **************************************************************************** */
859 #define VERT_MARK       1
860
861 typedef struct tmp_vert_ext {
862         BMVert *v;
863         union {
864                 int num_faces; /* adjacent faces */
865                 MDeformVert *dvert; /* deform vertex */
866         };
867 } tmp_vert_ext;
868
869 /*
870  * select similar vertices: the choices are in the enum in source/blender/bmesh/bmesh_operators.h
871  * choices are normal, face, vertex group...
872  */
873 void bmesh_similarverts_exec(BMesh *bm, BMOperator *op)
874 {
875         BMOIter vs_iter;        /* selected verts iterator */
876         BMIter v_iter;          /* mesh verts iterator */
877         BMVert *vs;             /* selected vertex */
878         BMVert *v;                      /* mesh vertex */
879         tmp_vert_ext *v_ext = NULL;
880         int *indices = NULL;
881         int num_total = 0, num_sels = 0, i = 0, idx = 0;
882         int type = BMO_slot_int_get(op, "type");
883         float thresh = BMO_slot_float_get(op, "thresh");
884
885         num_total = BM_mesh_elem_count(bm, BM_VERT);
886
887         /* iterate through all selected edges and mark them */
888         BMO_ITER(vs, &vs_iter, bm, op, "verts", BM_VERT) {
889                 BMO_elem_flag_enable(bm, vs, VERT_MARK);
890                 num_sels++;
891         }
892
893         /* allocate memory for the selected vertices indices and for all temporary vertices */
894         indices = (int *)MEM_mallocN(sizeof(int) * num_sels, "vertex indices");
895         v_ext = (tmp_vert_ext *)MEM_mallocN(sizeof(tmp_vert_ext) * num_total, "vertex extra");
896
897         /* loop through all the vertices and fill the vertices/indices structure */
898         BM_ITER(v, &v_iter, bm, BM_VERTS_OF_MESH, NULL) {
899                 v_ext[i].v = v;
900                 if (BMO_elem_flag_test(bm, v, VERT_MARK)) {
901                         indices[idx] = i;
902                         idx++;
903                 }
904
905                 switch (type) {
906                         case SIMVERT_FACE:
907                                 /* calling BM_vert_face_count every time is time consumming, so call it only once per vertex */
908                                 v_ext[i].num_faces      = BM_vert_face_count(v);
909                                 break;
910
911                         case SIMVERT_VGROUP:
912                                 if (CustomData_has_layer(&(bm->vdata), CD_MDEFORMVERT)) {
913                                         v_ext[i].dvert = CustomData_bmesh_get(&bm->vdata, v_ext[i].v->head.data, CD_MDEFORMVERT);
914                                 }
915                                 else {
916                                         v_ext[i].dvert = NULL;
917                                 }
918                                 break;
919                 }
920
921                 i++;
922         }
923
924         /* select the vertices if any */
925         for (i = 0; i < num_total; i++) {
926                 v = v_ext[i].v;
927                 if (!BMO_elem_flag_test(bm, v, VERT_MARK) && !BM_elem_flag_test(v, BM_ELEM_HIDDEN)) {
928                         int cont = TRUE;
929                         for (idx = 0; idx < num_sels && cont == TRUE; idx++) {
930                                 vs = v_ext[indices[idx]].v;
931                                 switch (type) {
932                                         case SIMVERT_NORMAL:
933                                                 /* compare the angle between the normals */
934                                                 if (RAD2DEGF(angle_v3v3(v->no, vs->no)) / 180.0f <= thresh) {
935                                                         BMO_elem_flag_enable(bm, v, VERT_MARK);
936                                                         cont = FALSE;
937                                                 }
938                                                 break;
939                                         case SIMVERT_FACE:
940                                                 /* number of adjacent faces */
941                                                 if (v_ext[i].num_faces == v_ext[indices[idx]].num_faces) {
942                                                         BMO_elem_flag_enable(bm, v, VERT_MARK);
943                                                         cont = FALSE;
944                                                 }
945                                                 break;
946
947                                         case SIMVERT_VGROUP:
948                                                 if (v_ext[i].dvert != NULL && v_ext[indices[idx]].dvert != NULL) {
949                                                         int v1, v2;
950                                                         for (v1 = 0; v1 < v_ext[i].dvert->totweight && cont == 1; v1++) {
951                                                                 for (v2 = 0; v2 < v_ext[indices[idx]].dvert->totweight; v2++) {
952                                                                         if (v_ext[i].dvert->dw[v1].def_nr == v_ext[indices[idx]].dvert->dw[v2].def_nr) {
953                                                                                 BMO_elem_flag_enable(bm, v, VERT_MARK);
954                                                                                 cont = FALSE;
955                                                                                 break;
956                                                                         }
957                                                                 }
958                                                         }
959                                                 }
960                                                 break;
961                                 }
962                         }
963                 }
964         }
965
966         MEM_freeN(indices);
967         MEM_freeN(v_ext);
968
969         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
970 }
971
972 /******************************************************************************
973 ** Cycle UVs for a face
974 **************************************************************************** */
975
976 void bmesh_rotateuvs_exec(BMesh *bm, BMOperator *op)
977 {
978         BMOIter fs_iter;        /* selected faces iterator */
979         BMFace *fs;     /* current face */
980         BMIter l_iter;  /* iteration loop */
981         // int n;
982
983         int dir = BMO_slot_int_get(op, "dir");
984
985         BMO_ITER(fs, &fs_iter, bm, op, "faces", BM_FACE) {
986                 if (CustomData_has_layer(&(bm->ldata), CD_MLOOPUV)) {
987                         if (dir == DIRECTION_CW) { /* same loops direction */
988                                 BMLoop *lf;     /* current face loops */
989                                 MLoopUV *f_luv; /* first face loop uv */
990                                 float p_uv[2];  /* previous uvs */
991                                 float t_uv[2];  /* tmp uvs */
992
993                                 int n = 0;
994                                 BM_ITER(lf, &l_iter, bm, BM_LOOPS_OF_FACE, fs) {
995                                         /* current loop uv is the previous loop uv */
996                                         MLoopUV *luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPUV);
997                                         if (n == 0) {
998                                                 f_luv = luv;
999                                                 copy_v2_v2(p_uv, luv->uv);
1000                                         }
1001                                         else {
1002                                                 copy_v2_v2(t_uv, luv->uv);
1003                                                 copy_v2_v2(luv->uv, p_uv);
1004                                                 copy_v2_v2(p_uv, t_uv);
1005                                         }
1006                                         n++;
1007                                 }
1008
1009                                 copy_v2_v2(f_luv->uv, p_uv);
1010                         }
1011                         else if (dir == DIRECTION_CCW) { /* counter loop direction */
1012                                 BMLoop *lf;     /* current face loops */
1013                                 MLoopUV *p_luv; /* previous loop uv */
1014                                 MLoopUV *luv;
1015                                 float t_uv[2];  /* current uvs */
1016
1017                                 int n = 0;
1018                                 BM_ITER(lf, &l_iter, bm, BM_LOOPS_OF_FACE, fs) {
1019                                         /* previous loop uv is the current loop uv */
1020                                         luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPUV);
1021                                         if (n == 0) {
1022                                                 p_luv = luv;
1023                                                 copy_v2_v2(t_uv, luv->uv);
1024                                         }
1025                                         else {
1026                                                 copy_v2_v2(p_luv->uv, luv->uv);
1027                                                 p_luv = luv;
1028                                         }
1029                                         n++;
1030                                 }
1031
1032                                 copy_v2_v2(luv->uv, t_uv);
1033                         }
1034                 }
1035         }
1036
1037 }
1038
1039 /******************************************************************************
1040 ** Reverse UVs for a face
1041 **************************************************************************** */
1042
1043 void bmesh_reverseuvs_exec(BMesh *bm, BMOperator *op)
1044 {
1045         BMOIter fs_iter;        /* selected faces iterator */
1046         BMFace *fs;             /* current face */
1047         BMIter l_iter;          /* iteration loop */
1048         BLI_array_declare(uvs);
1049         float (*uvs)[2] = NULL;
1050
1051         BMO_ITER(fs, &fs_iter, bm, op, "faces", BM_FACE) {
1052                 if (CustomData_has_layer(&(bm->ldata), CD_MLOOPUV)) {
1053                         BMLoop *lf;     /* current face loops */
1054                         int i = 0;
1055
1056                         BLI_array_empty(uvs);
1057                         BM_ITER(lf, &l_iter, bm, BM_LOOPS_OF_FACE, fs) {
1058                                 MLoopUV *luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPUV);
1059
1060                                 /* current loop uv is the previous loop uv */
1061                                 BLI_array_growone(uvs);
1062                                 uvs[i][0] = luv->uv[0];
1063                                 uvs[i][1] = luv->uv[1];
1064                                 i++;
1065                         }
1066
1067                         /* now that we have the uvs in the array, reverse! */
1068                         i = 0;
1069                         BM_ITER(lf, &l_iter, bm, BM_LOOPS_OF_FACE, fs) {
1070                                 /* current loop uv is the previous loop uv */
1071                                 MLoopUV *luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPUV);
1072                                 luv->uv[0] = uvs[(fs->len - i - 1)][0];
1073                                 luv->uv[1] = uvs[(fs->len - i - 1)][1];
1074                                 i++;
1075                         }
1076                 }
1077         }
1078
1079         BLI_array_free(uvs);
1080 }
1081
1082 /******************************************************************************
1083 ** Cycle colors for a face
1084 **************************************************************************** */
1085
1086 void bmesh_rotatecolors_exec(BMesh *bm, BMOperator *op)
1087 {
1088         BMOIter fs_iter;        /* selected faces iterator */
1089         BMFace *fs;     /* current face */
1090         BMIter l_iter;  /* iteration loop */
1091         // int n;
1092
1093         int dir = BMO_slot_int_get(op, "dir");
1094
1095         BMO_ITER(fs, &fs_iter, bm, op, "faces", BM_FACE) {
1096                 if (CustomData_has_layer(&(bm->ldata), CD_MLOOPCOL)) {
1097                         if (dir == DIRECTION_CW) { /* same loops direction */
1098                                 BMLoop *lf;     /* current face loops */
1099                                 MLoopCol *f_lcol; /* first face loop color */
1100                                 MLoopCol p_col; /* previous color */
1101                                 MLoopCol t_col; /* tmp color */
1102
1103                                 int n = 0;
1104                                 BM_ITER(lf, &l_iter, bm, BM_LOOPS_OF_FACE, fs) {
1105                                         /* current loop color is the previous loop color */
1106                                         MLoopCol *luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPCOL);
1107                                         if (n == 0) {
1108                                                 f_lcol = luv;
1109                                                 p_col = *luv;
1110                                         }
1111                                         else {
1112                                                 t_col = *luv;
1113                                                 *luv = p_col;
1114                                                 p_col = t_col;
1115                                         }
1116                                         n++;
1117                                 }
1118
1119                                 *f_lcol = p_col;
1120                         }
1121                         else if (dir == DIRECTION_CCW) { /* counter loop direction */
1122                                 BMLoop *lf;     /* current face loops */
1123                                 MLoopCol *p_lcol; /* previous loop color */
1124                                 MLoopCol *lcol;
1125                                 MLoopCol t_col; /* current color */
1126
1127                                 int n = 0;
1128                                 BM_ITER(lf, &l_iter, bm, BM_LOOPS_OF_FACE, fs) {
1129                                         /* previous loop color is the current loop color */
1130                                         lcol = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPCOL);
1131                                         if (n == 0) {
1132                                                 p_lcol = lcol;
1133                                                 t_col = *lcol;
1134                                         }
1135                                         else {
1136                                                 *p_lcol = *lcol;
1137                                                 p_lcol = lcol;
1138                                         }
1139                                         n++;
1140                                 }
1141
1142                                 *lcol = t_col;
1143                         }
1144                 }
1145         }
1146 }
1147
1148 /******************************************************************************
1149 ** Reverse colors for a face
1150 **************************************************************************** */
1151
1152 void bmesh_reversecolors_exec(BMesh *bm, BMOperator *op)
1153 {
1154         BMOIter fs_iter;        /* selected faces iterator */
1155         BMFace *fs;             /* current face */
1156         BMIter l_iter;          /* iteration loop */
1157         BLI_array_declare(cols);
1158         MLoopCol *cols = NULL;
1159
1160         BMO_ITER(fs, &fs_iter, bm, op, "faces", BM_FACE) {
1161                 if (CustomData_has_layer(&(bm->ldata), CD_MLOOPCOL)) {
1162                         BMLoop *lf;     /* current face loops */
1163                         int i = 0;
1164
1165                         BLI_array_empty(cols);
1166                         BM_ITER(lf, &l_iter, bm, BM_LOOPS_OF_FACE, fs) {
1167                                 MLoopCol *lcol = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPCOL);
1168
1169                                 /* current loop uv is the previous loop color */
1170                                 BLI_array_growone(cols);
1171                                 cols[i] = *lcol;
1172                                 i++;
1173                         }
1174
1175                         /* now that we have the uvs in the array, reverse! */
1176                         i = 0;
1177                         BM_ITER(lf, &l_iter, bm, BM_LOOPS_OF_FACE, fs) {
1178                                 /* current loop uv is the previous loop color */
1179                                 MLoopCol *lcol = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPCOL);
1180                                 *lcol = cols[(fs->len - i - 1)];
1181                                 i++;
1182                         }
1183                 }
1184         }
1185
1186         BLI_array_free(cols);
1187 }
1188
1189
1190 /******************************************************************************
1191 ** shortest vertex path select
1192 **************************************************************************** */
1193
1194 typedef struct element_node {
1195         BMVert *v;      /* vertex */
1196         BMVert *parent; /* node parent id */
1197         float weight;   /* node weight */
1198         HeapNode *hn;   /* heap node */
1199 } element_node;
1200
1201 void bmesh_vertexshortestpath_exec(BMesh *bm, BMOperator *op)
1202 {
1203         BMOIter vs_iter /* , vs2_iter */;       /* selected verts iterator */
1204         BMIter v_iter;          /* mesh verts iterator */
1205         BMVert *vs, *sv, *ev;   /* starting vertex, ending vertex */
1206         BMVert *v;              /* mesh vertex */
1207         Heap *h = NULL;
1208
1209         element_node *vert_list = NULL;
1210
1211         int num_total = 0 /*, num_sels = 0 */, i = 0;
1212         int type = BMO_slot_int_get(op, "type");
1213
1214         BMO_ITER(vs, &vs_iter, bm, op, "startv", BM_VERT) {
1215                 sv = vs;
1216         }
1217         BMO_ITER(vs, &vs_iter, bm, op, "endv", BM_VERT) {
1218                 ev = vs;
1219         }
1220
1221         num_total = BM_mesh_elem_count(bm, BM_VERT);
1222
1223         /* allocate memory for the nodes */
1224         vert_list = (element_node *)MEM_mallocN(sizeof(element_node) * num_total, "vertex nodes");
1225
1226         /* iterate through all the mesh vertices */
1227         /* loop through all the vertices and fill the vertices/indices structure */
1228         i = 0;
1229         BM_ITER(v, &v_iter, bm, BM_VERTS_OF_MESH, NULL) {
1230                 vert_list[i].v = v;
1231                 vert_list[i].parent = NULL;
1232                 vert_list[i].weight = FLT_MAX;
1233                 BM_elem_index_set(v, i); /* set_inline */
1234                 i++;
1235         }
1236         bm->elem_index_dirty &= ~BM_VERT;
1237
1238         /*
1239         ** we now have everything we need, start Dijkstra path finding algorithm
1240         */
1241
1242         /* set the distance/weight of the start vertex to 0 */
1243         vert_list[BM_elem_index_get(sv)].weight = 0.0f;
1244
1245         h = BLI_heap_new();
1246
1247         for (i = 0; i < num_total; i++) {
1248                 vert_list[i].hn = BLI_heap_insert(h, vert_list[i].weight, vert_list[i].v);
1249         }
1250
1251         while (!BLI_heap_empty(h)) {
1252                 BMEdge *e;
1253                 BMIter e_i;
1254                 float v_weight;
1255
1256                 /* take the vertex with the lowest weight out of the heap */
1257                 BMVert *v = (BMVert *)BLI_heap_popmin(h);
1258
1259                 if (vert_list[BM_elem_index_get(v)].weight == FLT_MAX) /* this means that there is no path */
1260                         break;
1261
1262                 v_weight = vert_list[BM_elem_index_get(v)].weight;
1263
1264                 BM_ITER(e, &e_i, bm, BM_EDGES_OF_VERT, v) {
1265                         BMVert *u;
1266                         float e_weight = v_weight;
1267
1268                         if (type == VPATH_SELECT_EDGE_LENGTH)
1269                                 e_weight += len_v3v3(e->v1->co, e->v2->co);
1270                         else e_weight += 1.0f;
1271
1272                         u = (e->v1 == v) ? e->v2 : e->v1;
1273
1274                         if (e_weight < vert_list[BM_elem_index_get(u)].weight) { /* is this path shorter ? */
1275                                 /* add it if so */
1276                                 vert_list[BM_elem_index_get(u)].parent = v;
1277                                 vert_list[BM_elem_index_get(u)].weight = e_weight;
1278
1279                                 /* we should do a heap update node function!!! :-/ */
1280                                 BLI_heap_remove(h, vert_list[BM_elem_index_get(u)].hn);
1281                                 BLI_heap_insert(h, e_weight, u);
1282                         }
1283                 }
1284         }
1285
1286         /* now we trace the path (if it exists) */
1287         v = ev;
1288
1289         while (vert_list[BM_elem_index_get(v)].parent != NULL) {
1290                 BMO_elem_flag_enable(bm, v, VERT_MARK);
1291                 v = vert_list[BM_elem_index_get(v)].parent;
1292         }
1293
1294         BLI_heap_free(h, NULL);
1295         MEM_freeN(vert_list);
1296
1297         BMO_slot_from_flag(bm, op, "vertout", VERT_MARK, BM_VERT);
1298 }