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