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