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