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