code cleanup: make bmesh operator names more consistant since python has access to...
[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->slots_in, "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, op->slots_out, "vert.out", BM_VERT, 1);
52 }
53
54 void bmo_transform_exec(BMesh *UNUSED(bm), BMOperator *op)
55 {
56         BMOIter iter;
57         BMVert *v;
58         float mat[4][4];
59
60         BMO_slot_mat4_get(op->slots_in, "mat", mat);
61
62         BMO_ITER (v, &iter, op->slots_in, "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->slots_in, "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->slots_in, "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->slots_in, "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, op->slots_in, "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         const int use_ccw = BMO_slot_bool_get(op->slots_in, "use_ccw");
126         const int is_single = BMO_slot_buffer_count(op->slots_in, "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, op->slots_in, "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, use_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, op->slots_out, "edges.out", 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, op->slots_in, "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, op->slots_in, "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, op->slots_in, "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, op->slots_in, "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->slots_in, "use_faces");
269         int constrict = BMO_slot_bool_get(op->slots_in, "use_constrict");
270
271         BMO_slot_buffer_flag_enable(bm, op->slots_in, "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, op->slots_out, "geom.out", 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  /* UNUSED */
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->slots_in, "use_flip");
318
319         startf = NULL;
320         maxx = -1.0e10;
321         
322         BMO_slot_buffer_flag_enable(bm, op->slots_in, "faces", BM_FACE, FACE_FLAG);
323
324         /* find a starting face */
325         BMO_ITER (f, &siter, op->slots_in, "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, op->slots_in, "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 *UNUSED(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, clip_dist = BMO_slot_float_get(op->slots_in, "clip_dist");
425         int i, j, clipx, clipy, clipz;
426         int xaxis, yaxis, zaxis;
427         
428         clipx = BMO_slot_bool_get(op->slots_in, "mirror_clip_x");
429         clipy = BMO_slot_bool_get(op->slots_in, "mirror_clip_y");
430         clipz = BMO_slot_bool_get(op->slots_in, "mirror_clip_z");
431
432         xaxis = BMO_slot_bool_get(op->slots_in, "use_axis_x");
433         yaxis = BMO_slot_bool_get(op->slots_in, "use_axis_y");
434         zaxis = BMO_slot_bool_get(op->slots_in, "use_axis_z");
435
436         i = 0;
437         BMO_ITER (v, &siter, op->slots_in, "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]) <= clip_dist)
458                         co[0] = 0.0f;
459                 if (clipy && fabsf(v->co[1]) <= clip_dist)
460                         co[1] = 0.0f;
461                 if (clipz && fabsf(v->co[2]) <= clip_dist)
462                         co[2] = 0.0f;
463
464                 i++;
465         }
466
467         i = 0;
468         BMO_ITER (v, &siter, op->slots_in, "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  * Cycle UVs for a face
484  **************************************************************************** */
485
486 void bmo_rotate_uvs_exec(BMesh *bm, BMOperator *op)
487 {
488         BMOIter fs_iter;  /* selected faces iterator */
489         BMFace *fs;       /* current face */
490         BMIter l_iter;    /* iteration loop */
491
492         const int use_ccw = BMO_slot_bool_get(op->slots_in, "use_ccw");
493
494         BMO_ITER (fs, &fs_iter, op->slots_in, "faces", BM_FACE) {
495                 if (CustomData_has_layer(&(bm->ldata), CD_MLOOPUV)) {
496                         if (use_ccw == FALSE) {  /* same loops direction */
497                                 BMLoop *lf;     /* current face loops */
498                                 MLoopUV *f_luv; /* first face loop uv */
499                                 float p_uv[2];  /* previous uvs */
500                                 float t_uv[2];  /* tmp uvs */
501
502                                 int n = 0;
503                                 BM_ITER_ELEM (lf, &l_iter, fs, BM_LOOPS_OF_FACE) {
504                                         /* current loop uv is the previous loop uv */
505                                         MLoopUV *luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPUV);
506                                         if (n == 0) {
507                                                 f_luv = luv;
508                                                 copy_v2_v2(p_uv, luv->uv);
509                                         }
510                                         else {
511                                                 copy_v2_v2(t_uv, luv->uv);
512                                                 copy_v2_v2(luv->uv, p_uv);
513                                                 copy_v2_v2(p_uv, t_uv);
514                                         }
515                                         n++;
516                                 }
517
518                                 copy_v2_v2(f_luv->uv, p_uv);
519                         }
520                         else { /* counter loop direction */
521                                 BMLoop *lf;     /* current face loops */
522                                 MLoopUV *p_luv; /* previous loop uv */
523                                 MLoopUV *luv;
524                                 float t_uv[2];  /* current uvs */
525
526                                 int n = 0;
527                                 BM_ITER_ELEM (lf, &l_iter, fs, BM_LOOPS_OF_FACE) {
528                                         /* previous loop uv is the current loop uv */
529                                         luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPUV);
530                                         if (n == 0) {
531                                                 p_luv = luv;
532                                                 copy_v2_v2(t_uv, luv->uv);
533                                         }
534                                         else {
535                                                 copy_v2_v2(p_luv->uv, luv->uv);
536                                                 p_luv = luv;
537                                         }
538                                         n++;
539                                 }
540
541                                 copy_v2_v2(luv->uv, t_uv);
542                         }
543                 }
544         }
545 }
546
547 /**************************************************************************** *
548  * Reverse UVs for a face
549  **************************************************************************** */
550
551 void bmo_reverse_uvs_exec(BMesh *bm, BMOperator *op)
552 {
553         BMOIter fs_iter;        /* selected faces iterator */
554         BMFace *fs;             /* current face */
555         BMIter l_iter;          /* iteration loop */
556         BLI_array_declare(uvs);
557         float (*uvs)[2] = NULL;
558
559         BMO_ITER (fs, &fs_iter, op->slots_in, "faces", BM_FACE) {
560                 if (CustomData_has_layer(&(bm->ldata), CD_MLOOPUV)) {
561                         BMLoop *lf;     /* current face loops */
562                         int i;
563
564                         BLI_array_empty(uvs);
565                         BLI_array_grow_items(uvs, fs->len);
566
567                         BM_ITER_ELEM_INDEX (lf, &l_iter, fs, BM_LOOPS_OF_FACE, i) {
568                                 MLoopUV *luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPUV);
569
570                                 /* current loop uv is the previous loop uv */
571                                 copy_v2_v2(uvs[i], luv->uv);
572                         }
573
574                         /* now that we have the uvs in the array, reverse! */
575                         i = 0;
576                         BM_ITER_ELEM_INDEX (lf, &l_iter, fs, BM_LOOPS_OF_FACE, i) {
577                                 /* current loop uv is the previous loop uv */
578                                 MLoopUV *luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPUV);
579                                 copy_v2_v2(luv->uv, uvs[(fs->len - i - 1)]);
580                         }
581                 }
582         }
583
584         BLI_array_free(uvs);
585 }
586
587 /**************************************************************************** *
588  * Cycle colors for a face
589  **************************************************************************** */
590
591 void bmo_rotate_colors_exec(BMesh *bm, BMOperator *op)
592 {
593         BMOIter fs_iter;  /* selected faces iterator */
594         BMFace *fs;       /* current face */
595         BMIter l_iter;    /* iteration loop */
596
597         const int use_ccw = BMO_slot_bool_get(op->slots_in, "use_ccw");
598
599         BMO_ITER (fs, &fs_iter, op->slots_in, "faces", BM_FACE) {
600                 if (CustomData_has_layer(&(bm->ldata), CD_MLOOPCOL)) {
601                         if (use_ccw == FALSE) {  /* same loops direction */
602                                 BMLoop *lf;     /* current face loops */
603                                 MLoopCol *f_lcol; /* first face loop color */
604                                 MLoopCol p_col; /* previous color */
605                                 MLoopCol t_col; /* tmp color */
606
607                                 int n = 0;
608                                 BM_ITER_ELEM (lf, &l_iter, fs, BM_LOOPS_OF_FACE) {
609                                         /* current loop color is the previous loop color */
610                                         MLoopCol *luv = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPCOL);
611                                         if (n == 0) {
612                                                 f_lcol = luv;
613                                                 p_col = *luv;
614                                         }
615                                         else {
616                                                 t_col = *luv;
617                                                 *luv = p_col;
618                                                 p_col = t_col;
619                                         }
620                                         n++;
621                                 }
622
623                                 *f_lcol = p_col;
624                         }
625                         else {  /* counter loop direction */
626                                 BMLoop *lf;     /* current face loops */
627                                 MLoopCol *p_lcol; /* previous loop color */
628                                 MLoopCol *lcol;
629                                 MLoopCol t_col; /* current color */
630
631                                 int n = 0;
632                                 BM_ITER_ELEM (lf, &l_iter, fs, BM_LOOPS_OF_FACE) {
633                                         /* previous loop color is the current loop color */
634                                         lcol = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPCOL);
635                                         if (n == 0) {
636                                                 p_lcol = lcol;
637                                                 t_col = *lcol;
638                                         }
639                                         else {
640                                                 *p_lcol = *lcol;
641                                                 p_lcol = lcol;
642                                         }
643                                         n++;
644                                 }
645
646                                 *lcol = t_col;
647                         }
648                 }
649         }
650 }
651
652 /*************************************************************************** *
653  * Reverse colors for a face
654  *************************************************************************** */
655
656 void bmo_reverse_colors_exec(BMesh *bm, BMOperator *op)
657 {
658         BMOIter fs_iter;        /* selected faces iterator */
659         BMFace *fs;             /* current face */
660         BMIter l_iter;          /* iteration loop */
661         BLI_array_declare(cols);
662         MLoopCol *cols = NULL;
663
664         BMO_ITER (fs, &fs_iter, op->slots_in, "faces", BM_FACE) {
665                 if (CustomData_has_layer(&(bm->ldata), CD_MLOOPCOL)) {
666                         BMLoop *lf;     /* current face loops */
667                         int i;
668
669                         BLI_array_empty(cols);
670                         BLI_array_grow_items(cols, fs->len);
671
672                         BM_ITER_ELEM_INDEX (lf, &l_iter, fs, BM_LOOPS_OF_FACE, i) {
673                                 cols[i] = *((MLoopCol *)CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPCOL));
674                         }
675
676                         /* now that we have the uvs in the array, reverse! */
677                         BM_ITER_ELEM_INDEX (lf, &l_iter, fs, BM_LOOPS_OF_FACE, i) {
678                                 /* current loop uv is the previous loop color */
679                                 MLoopCol *lcol = CustomData_bmesh_get(&bm->ldata, lf->head.data, CD_MLOOPCOL);
680                                 *lcol = cols[(fs->len - i - 1)];
681                         }
682                 }
683         }
684
685         BLI_array_free(cols);
686 }
687
688
689 /*************************************************************************** *
690  * shortest vertex path select
691  *************************************************************************** */
692
693 typedef struct ElemNode {
694         BMVert *v;      /* vertex */
695         BMVert *parent; /* node parent id */
696         float weight;   /* node weight */
697         HeapNode *hn;   /* heap node */
698 } ElemNode;
699
700 #define VERT_MARK       1
701
702 void bmo_shortest_path_exec(BMesh *bm, BMOperator *op)
703 {
704         BMOIter vs_iter /* , vs2_iter */;       /* selected verts iterator */
705         BMIter v_iter;          /* mesh verts iterator */
706         BMVert *vs, *sv, *ev;   /* starting vertex, ending vertex */
707         BMVert *v;              /* mesh vertex */
708         Heap *h = NULL;
709
710         ElemNode *vert_list = NULL;
711
712         int num_total = 0 /*, num_sels = 0 */, i = 0;
713         const int type = BMO_slot_int_get(op->slots_in, "type");
714
715         /* BMESH_TODO use BMO_slot_buffer_elem_first here? */
716         BMO_ITER (vs, &vs_iter, op->slots_in, "startv", BM_VERT) {
717                 sv = vs;
718         }
719         BMO_ITER (vs, &vs_iter, op->slots_in, "endv", BM_VERT) {
720                 ev = vs;
721         }
722
723         num_total = BM_mesh_elem_count(bm, BM_VERT);
724
725         /* allocate memory for the nodes */
726         vert_list = (ElemNode *)MEM_mallocN(sizeof(ElemNode) * num_total, "vertex nodes");
727
728         /* iterate through all the mesh vertices */
729         /* loop through all the vertices and fill the vertices/indices structure */
730         i = 0;
731         BM_ITER_MESH (v, &v_iter, bm, BM_VERTS_OF_MESH) {
732                 vert_list[i].v = v;
733                 vert_list[i].parent = NULL;
734                 vert_list[i].weight = FLT_MAX;
735                 BM_elem_index_set(v, i); /* set_inline */
736                 i++;
737         }
738         bm->elem_index_dirty &= ~BM_VERT;
739
740         /*
741          * we now have everything we need, start Dijkstra path finding algorithm
742          */
743
744         /* set the distance/weight of the start vertex to 0 */
745         vert_list[BM_elem_index_get(sv)].weight = 0.0f;
746
747         h = BLI_heap_new();
748
749         for (i = 0; i < num_total; i++) {
750                 vert_list[i].hn = BLI_heap_insert(h, vert_list[i].weight, vert_list[i].v);
751         }
752
753         while (!BLI_heap_is_empty(h)) {
754                 BMEdge *e;
755                 BMIter e_i;
756                 float v_weight;
757
758                 /* take the vertex with the lowest weight out of the heap */
759                 BMVert *v = (BMVert *)BLI_heap_popmin(h);
760
761                 if (vert_list[BM_elem_index_get(v)].weight == FLT_MAX) /* this means that there is no path */
762                         break;
763
764                 v_weight = vert_list[BM_elem_index_get(v)].weight;
765
766                 BM_ITER_ELEM (e, &e_i, v, BM_EDGES_OF_VERT) {
767                         BMVert *u;
768                         float e_weight = v_weight;
769
770                         if (type == VPATH_SELECT_EDGE_LENGTH)
771                                 e_weight += len_v3v3(e->v1->co, e->v2->co);
772                         else e_weight += 1.0f;
773
774                         u = (e->v1 == v) ? e->v2 : e->v1;
775
776                         if (e_weight < vert_list[BM_elem_index_get(u)].weight) { /* is this path shorter ? */
777                                 /* add it if so */
778                                 vert_list[BM_elem_index_get(u)].parent = v;
779                                 vert_list[BM_elem_index_get(u)].weight = e_weight;
780
781                                 /* we should do a heap update node function!!! :-/ */
782                                 BLI_heap_remove(h, vert_list[BM_elem_index_get(u)].hn);
783                                 BLI_heap_insert(h, e_weight, u);
784                         }
785                 }
786         }
787
788         /* now we trace the path (if it exists) */
789         v = ev;
790
791         while (vert_list[BM_elem_index_get(v)].parent != NULL) {
792                 BMO_elem_flag_enable(bm, v, VERT_MARK);
793                 v = vert_list[BM_elem_index_get(v)].parent;
794         }
795
796         BLI_heap_free(h, NULL);
797         MEM_freeN(vert_list);
798
799         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "verts.out", BM_VERT, VERT_MARK);
800 }