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