skip assigning vars for inline bmesh flag funcs, just cast.
[blender.git] / source / blender / bmesh / operators / mirror.c
1 #include "MEM_guardedalloc.h"
2
3 #include "DNA_listBase.h"
4 #include "DNA_customdata_types.h"
5 #include "DNA_mesh_types.h"
6 #include "DNA_meshdata_types.h"
7 #include "DNA_object_types.h"
8 #include "DNA_scene_types.h"
9
10 #include <string.h>
11
12 #include "BKE_customdata.h" 
13 #include "BKE_mesh.h"
14 #include "BKE_global.h"
15 #include "BKE_DerivedMesh.h"
16 #include "BKE_cdderivedmesh.h"
17 #include "BKE_utildefines.h"
18
19 #include "BLI_editVert.h"
20 #include "mesh_intern.h"
21 #include "ED_mesh.h"
22
23 #include "BLI_math.h"
24 #include "BLI_blenlib.h"
25 #include "BLI_edgehash.h"
26 #include "BLI_array.h"
27 #include "BLI_utildefines.h"
28
29 #include "bmesh.h"
30 #include "bmesh_operators_private.h" /* own include */
31
32 /*
33  * MIRROR.C
34  *
35  * mirror bmop.
36  *
37 */
38
39 #define ELE_NEW         1
40
41 void bmesh_mirror_exec(BMesh *bm, BMOperator *op) {
42         BMOperator dupeop, weldop;
43         BMOIter siter;
44         BMIter iter;
45         BMVert *v, *v2, **vmap = NULL;
46         BLI_array_declare(vmap);
47         BMEdge /*  *e, */ **emap = NULL;
48         BLI_array_declare(emap);
49         float mtx[4][4];
50         float imtx[4][4];
51         float scale[3] = {1.0f, 1.0f, 1.0f};
52         float dist = BMO_Get_Float(op, "mergedist");
53         int i, ototvert, ototedge, axis = BMO_Get_Int(op, "axis");
54         int mirroru = BMO_Get_Int(op, "mirror_u");
55         int mirrorv = BMO_Get_Int(op, "mirror_v");
56
57         ototvert = bm->totvert;
58         ototedge = bm->totedge;
59         
60         BMO_Get_Mat4(op, "mat", mtx);
61         invert_m4_m4(imtx, mtx);
62         
63         BMO_InitOpf(bm, &dupeop, "dupe geom=%s", op, "geom");
64         BMO_Exec_Op(bm, &dupeop);
65         
66         BMO_Flag_Buffer(bm, &dupeop, "newout", ELE_NEW, BM_ALL);
67
68         /*create old -> new mapping*/
69         i = 0;
70         v2 = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
71         BMO_ITER(v, &siter, bm, &dupeop, "newout", BM_VERT) {
72                 BLI_array_growone(vmap);
73                 vmap[i] = v;
74
75                 BM_SetIndex(v2, i);
76                 v2 = BMIter_Step(&iter);
77
78                 i += 1;
79         }
80
81         /*feed old data to transform bmop*/
82         scale[axis] = -1.0f;
83         BMO_CallOpf(bm, "transform verts=%fv mat=%m4", ELE_NEW, mtx);
84         BMO_CallOpf(bm, "scale verts=%fv vec=%v", ELE_NEW, scale);
85         BMO_CallOpf(bm, "transform verts=%fv mat=%m4", ELE_NEW, imtx);
86         
87         BMO_Init_Op(&weldop, "weldverts");
88
89         v = BMIter_New(&iter, bm, BM_VERTS_OF_MESH, NULL);
90         for (i=0; i<ototvert; i++) {
91                 if (ABS(v->co[axis]) <= dist) {
92                         BMO_Insert_MapPointer(bm, &weldop, "targetmap", vmap[i], v);
93                 }
94                 v = BMIter_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.0 - luv->uv[0];
111                                         if (mirrorv)
112                                                 luv->uv[1] = 1.0 - luv->uv[1];
113                                 }
114                         }
115                 }
116         }
117
118         BMO_Exec_Op(bm, &weldop);
119         
120         BMO_Finish_Op(bm, &weldop);
121         BMO_Finish_Op(bm, &dupeop);
122
123         BMO_Flag_To_Slot(bm, op, "newout", ELE_NEW, BM_ALL);
124
125         BLI_array_free(vmap);
126         BLI_array_free(emap);
127 }