bmesh operator naming - use clearer names for args eg: (mat -> matrix, use_singleedge...
[blender.git] / source / blender / bmesh / operators / bmo_mirror.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_mirror.c
24  *  \ingroup bmesh
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "DNA_meshdata_types.h"
30
31 #include "BLI_math.h"
32 #include "BLI_array.h"
33
34 #include "BKE_customdata.h"
35
36 #include "bmesh.h"
37 #include "intern/bmesh_operators_private.h" /* own include */
38
39 #define ELE_NEW         1
40
41 void bmo_mirror_exec(BMesh *bm, BMOperator *op)
42 {
43         BMOperator dupeop, weldop;
44         BMOIter siter;
45         BMIter iter;
46         BMVert *v /* , *v2 */ /* UNUSED */, **vmap = NULL;
47         BLI_array_declare(vmap);
48         BMEdge /*  *e, */ **emap = NULL;
49         BLI_array_declare(emap);
50         float mtx[4][4];
51         float imtx[4][4];
52         float scale[3] = {1.0f, 1.0f, 1.0f};
53         float dist = BMO_slot_float_get(op->slots_in, "merge_dist");
54         int i, ototvert /*, ototedge */;
55         int axis = BMO_slot_int_get(op->slots_in, "axis");
56         int mirroru = BMO_slot_bool_get(op->slots_in, "mirror_u");
57         int mirrorv = BMO_slot_bool_get(op->slots_in, "mirror_v");
58         BMOpSlot *slot_targetmap;
59
60         ototvert = bm->totvert;
61         /* ototedge = bm->totedge; */ /* UNUSED */
62         
63         BMO_slot_mat4_get(op->slots_in, "matrix", mtx);
64         invert_m4_m4(imtx, mtx);
65         
66         BMO_op_initf(bm, &dupeop, op->flag, "duplicate geom=%s", op, "geom");
67         BMO_op_exec(bm, &dupeop);
68         
69         BMO_slot_buffer_flag_enable(bm, dupeop.slots_out, "geom.out", BM_ALL_NOLOOP, ELE_NEW);
70
71         /* create old -> new mappin */
72         i = 0;
73         /* v2 = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL); */ /* UNUSED */
74         BMO_ITER (v, &siter, dupeop.slots_out, "geom.out", BM_VERT) {
75                 BLI_array_grow_one(vmap);
76                 vmap[i] = v;
77                 /* v2 = BM_iter_step(&iter); */ /* UNUSED */
78                 i++;
79         }
80         bm->elem_index_dirty |= BM_VERT;
81
82         /* feed old data to transform bmo */
83         scale[axis] = -1.0f;
84         BMO_op_callf(bm, op->flag, "transform verts=%fv matrix=%m4", ELE_NEW, mtx);
85         BMO_op_callf(bm, op->flag, "scale verts=%fv vec=%v", ELE_NEW, scale);
86         BMO_op_callf(bm, op->flag, "transform verts=%fv matrix=%m4", ELE_NEW, imtx);
87         
88         BMO_op_init(bm, &weldop, op->flag, "weld_verts");
89
90         slot_targetmap = BMO_slot_get(weldop.slots_in, "targetmap");
91
92         v = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
93         for (i = 0; i < ototvert; i++) {
94                 if (fabsf(v->co[axis]) <= dist) {
95                         BMO_slot_map_elem_insert(&weldop, slot_targetmap, vmap[i], v);
96                 }
97                 v = BM_iter_step(&iter);
98         }
99         
100         if (mirroru || mirrorv) {
101                 BMFace *f;
102                 BMLoop *l;
103                 MLoopUV *luv;
104                 int totlayer;
105                 BMIter liter;
106
107                 BMO_ITER (f, &siter, dupeop.slots_out, "geom.out", BM_FACE) {
108                         BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
109                                 totlayer = CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV);
110                                 for (i = 0; i < totlayer; i++) {
111                                         luv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
112                                         if (mirroru)
113                                                 luv->uv[0] = 1.0f - luv->uv[0];
114                                         if (mirrorv)
115                                                 luv->uv[1] = 1.0f - luv->uv[1];
116                                 }
117                         }
118                 }
119         }
120
121         BMO_op_exec(bm, &weldop);
122         
123         BMO_op_finish(bm, &weldop);
124         BMO_op_finish(bm, &dupeop);
125
126         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_ALL_NOLOOP, ELE_NEW);
127
128         BLI_array_free(vmap);
129         BLI_array_free(emap);
130 }