1b6d64073c0069c500f4cb299fd429c22ff3eb63
[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, **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, "mergedist");
54         int i, ototvert /*, ototedge */;
55         int axis = BMO_slot_int_get(op, "axis");
56         int mirroru = BMO_slot_bool_get(op, "mirror_u");
57         int mirrorv = BMO_slot_bool_get(op, "mirror_v");
58
59         ototvert = bm->totvert;
60         /* ototedge = bm->totedge; */ /* UNUSED */
61         
62         BMO_slot_mat4_get(op, "mat", mtx);
63         invert_m4_m4(imtx, mtx);
64         
65         BMO_op_initf(bm, &dupeop, "dupe geom=%s", op, "geom");
66         BMO_op_exec(bm, &dupeop);
67         
68         BMO_slot_buffer_flag_enable(bm, &dupeop, "newout", BM_ALL, ELE_NEW);
69
70         /* create old -> new mappin */
71         i = 0;
72         v2 = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
73         BMO_ITER(v, &siter, bm, &dupeop, "newout", BM_VERT) {
74                 BLI_array_growone(vmap);
75                 vmap[i] = v;
76                 v2 = BM_iter_step(&iter);
77                 i++;
78         }
79         bm->elem_index_dirty |= BM_VERT;
80
81         /* feed old data to transform bmo */
82         scale[axis] = -1.0f;
83         BMO_op_callf(bm, "transform verts=%fv mat=%m4", ELE_NEW, mtx);
84         BMO_op_callf(bm, "scale verts=%fv vec=%v", ELE_NEW, scale);
85         BMO_op_callf(bm, "transform verts=%fv mat=%m4", ELE_NEW, imtx);
86         
87         BMO_op_init(bm, &weldop, "weldverts");
88
89         v = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
90         for (i = 0; i < ototvert; i++) {
91                 if (ABS(v->co[axis]) <= dist) {
92                         BMO_slot_map_ptr_insert(bm, &weldop, "targetmap", vmap[i], v);
93                 }
94                 v = BM_iter_step(&iter);
95         }
96         
97         if (mirroru || mirrorv) {
98                 BMFace *f;
99                 BMLoop *l;
100                 MLoopUV *luv;
101                 int totlayer;
102                 BMIter liter;
103
104                 BMO_ITER(f, &siter, bm, &dupeop, "newout", BM_FACE) {
105                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
106                                 totlayer = CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV);
107                                 for (i = 0; i < totlayer; i++) {
108                                         luv = CustomData_bmesh_get_n(&bm->ldata, l->head.data, CD_MLOOPUV, i);
109                                         if (mirroru)
110                                                 luv->uv[0] = 1.0f - luv->uv[0];
111                                         if (mirrorv)
112                                                 luv->uv[1] = 1.0f - luv->uv[1];
113                                 }
114                         }
115                 }
116         }
117
118         BMO_op_exec(bm, &weldop);
119         
120         BMO_op_finish(bm, &weldop);
121         BMO_op_finish(bm, &dupeop);
122
123         BMO_slot_buffer_from_enabled_flag(bm, op, "newout", BM_ALL, ELE_NEW);
124
125         BLI_array_free(vmap);
126         BLI_array_free(emap);
127 }