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