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