remove unused initFlags option in mirror modifier
[blender.git] / source / blender / modifiers / intern / MOD_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  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_mirror.c
32  *  \ingroup modifiers
33  */
34
35
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_cdderivedmesh.h"
43 #include "BKE_mesh.h"
44 #include "BKE_modifier.h"
45 #include "BKE_deform.h"
46
47
48 #include "MEM_guardedalloc.h"
49 #include "depsgraph_private.h"
50
51 #include "MOD_util.h"
52
53 static void initData(ModifierData *md)
54 {
55         MirrorModifierData *mmd = (MirrorModifierData*) md;
56
57         mmd->flag |= (MOD_MIR_AXIS_X | MOD_MIR_VGROUP);
58         mmd->tolerance = 0.001;
59         mmd->mirror_ob = NULL;
60 }
61
62 static void copyData(ModifierData *md, ModifierData *target)
63 {
64         MirrorModifierData *mmd = (MirrorModifierData*) md;
65         MirrorModifierData *tmmd = (MirrorModifierData*) target;
66
67         tmmd->flag = mmd->flag;
68         tmmd->tolerance = mmd->tolerance;
69         tmmd->mirror_ob = mmd->mirror_ob;
70 }
71
72 static void foreachObjectLink(
73                                                  ModifierData *md, Object *ob,
74           void (*walk)(void *userData, Object *ob, Object **obpoin),
75                  void *userData)
76 {
77         MirrorModifierData *mmd = (MirrorModifierData*) md;
78
79         walk(userData, ob, &mmd->mirror_ob);
80 }
81
82 static void updateDepgraph(ModifierData *md, DagForest *forest,
83                                                 struct Scene *UNUSED(scene),
84                                                 Object *UNUSED(ob),
85                                                 DagNode *obNode)
86 {
87         MirrorModifierData *mmd = (MirrorModifierData*) md;
88
89         if(mmd->mirror_ob) {
90                 DagNode *latNode = dag_get_node(forest, mmd->mirror_ob);
91
92                 dag_add_relation(forest, latNode, obNode,
93                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Mirror Modifier");
94         }
95 }
96
97 static DerivedMesh *doMirrorOnAxis(MirrorModifierData *mmd,
98                                    Object *ob,
99                                    DerivedMesh *dm,
100                                    int axis)
101 {
102         int i;
103         float tolerance = mmd->tolerance;
104         DerivedMesh *result;
105         int numVerts, numEdges, numFaces;
106         int maxVerts = dm->getNumVerts(dm);
107         int maxEdges = dm->getNumEdges(dm);
108         int maxFaces = dm->getNumFaces(dm);
109         int *flip_map= NULL, flip_map_len= 0;
110         int do_vgroup_mirr= (mmd->flag & MOD_MIR_VGROUP);
111         unsigned int (*indexMap)[2];
112         float mtx[4][4], imtx[4][4];
113
114         numVerts = numEdges = numFaces = 0;
115
116         indexMap = MEM_mallocN(sizeof(*indexMap) * maxVerts, "indexmap");
117
118         result = CDDM_from_template(dm, maxVerts * 2, maxEdges * 2, maxFaces * 2);
119
120
121         if (do_vgroup_mirr) {
122                 flip_map= defgroup_flip_map(ob, &flip_map_len, FALSE);
123                 if(flip_map == NULL)
124                         do_vgroup_mirr= 0;
125         }
126
127         if (mmd->mirror_ob) {
128                 float obinv[4][4];
129                 
130                 invert_m4_m4(obinv, mmd->mirror_ob->obmat);
131                 mult_m4_m4m4(mtx, obinv, ob->obmat);
132                 invert_m4_m4(imtx, mtx);
133         }
134
135         for(i = 0; i < maxVerts; i++) {
136                 MVert inMV;
137                 MVert *mv = CDDM_get_vert(result, numVerts);
138                 int isShared;
139                 float co[3];
140                 
141                 dm->getVert(dm, i, &inMV);
142                 
143                 copy_v3_v3(co, inMV.co);
144                 
145                 if (mmd->mirror_ob) {
146                         mul_m4_v3(mtx, co);
147                 }
148                 
149                 if(mmd->flag & MOD_MIR_NO_MERGE)
150                         isShared = 0;
151                 else
152                         isShared = ABS(co[axis])<=tolerance;
153                 
154                 /* Because the topology result (# of vertices) must be the same if
155                  * the mesh data is overridden by vertex cos, have to calc sharedness
156                  * based on original coordinates. This is why we test before copy.
157                  */
158                 DM_copy_vert_data(dm, result, i, numVerts, 1);
159                 *mv = inMV;
160
161                 indexMap[i][0] = numVerts;
162                 indexMap[i][1] = !isShared;
163
164                 numVerts++;
165
166                 if(isShared ) {
167                         co[axis] = 0.0f;
168                         if (mmd->mirror_ob) {
169                                 mul_m4_v3(imtx, co);
170                         }
171                         copy_v3_v3(mv->co, co);
172                         
173                         mv->flag |= ME_VERT_MERGED;
174                 }
175                 else {
176                         MVert *mv2 = CDDM_get_vert(result, numVerts);
177                         
178                         DM_copy_vert_data(dm, result, i, numVerts, 1);
179                         *mv2 = *mv;
180                         
181                         co[axis] = -co[axis];
182                         if (mmd->mirror_ob) {
183                                 mul_m4_v3(imtx, co);
184                         }
185                         copy_v3_v3(mv2->co, co);
186                         
187                         if (do_vgroup_mirr) {
188                                 MDeformVert *dvert= DM_get_vert_data(result, numVerts, CD_MDEFORMVERT);
189                                 if(dvert) {
190                                         defvert_flip(dvert, flip_map, flip_map_len);
191                                 }
192                         }
193
194                         numVerts++;
195                 }
196         }
197
198         for(i = 0; i < maxEdges; i++) {
199                 MEdge inMED;
200                 MEdge *med = CDDM_get_edge(result, numEdges);
201                 
202                 dm->getEdge(dm, i, &inMED);
203                 
204                 DM_copy_edge_data(dm, result, i, numEdges, 1);
205                 *med = inMED;
206                 numEdges++;
207                 
208                 med->v1 = indexMap[inMED.v1][0];
209                 med->v2 = indexMap[inMED.v2][0];
210                 
211                 if(indexMap[inMED.v1][1] || indexMap[inMED.v2][1]) {
212                         MEdge *med2 = CDDM_get_edge(result, numEdges);
213                         
214                         DM_copy_edge_data(dm, result, i, numEdges, 1);
215                         *med2 = *med;
216                         numEdges++;
217                         
218                         med2->v1 += indexMap[inMED.v1][1];
219                         med2->v2 += indexMap[inMED.v2][1];
220                 }
221         }
222
223         for(i = 0; i < maxFaces; i++) {
224                 MFace inMF;
225                 MFace *mf = CDDM_get_face(result, numFaces);
226                 
227                 dm->getFace(dm, i, &inMF);
228                 
229                 DM_copy_face_data(dm, result, i, numFaces, 1);
230                 *mf = inMF;
231                 numFaces++;
232                 
233                 mf->v1 = indexMap[inMF.v1][0];
234                 mf->v2 = indexMap[inMF.v2][0];
235                 mf->v3 = indexMap[inMF.v3][0];
236                 mf->v4 = indexMap[inMF.v4][0];
237                 
238                 if ( indexMap[inMF.v1][1] ||
239                      indexMap[inMF.v2][1] ||
240                      indexMap[inMF.v3][1] ||
241                      (mf->v4 && indexMap[inMF.v4][1]))
242                 {
243                         MFace *mf2 = CDDM_get_face(result, numFaces);
244                         static int corner_indices[4] = {2, 1, 0, 3};
245                         
246                         DM_copy_face_data(dm, result, i, numFaces, 1);
247                         *mf2 = *mf;
248                         
249                         mf2->v1 += indexMap[inMF.v1][1];
250                         mf2->v2 += indexMap[inMF.v2][1];
251                         mf2->v3 += indexMap[inMF.v3][1];
252                         if(inMF.v4) mf2->v4 += indexMap[inMF.v4][1];
253                         
254                         /* mirror UVs if enabled */
255                         if(mmd->flag & (MOD_MIR_MIRROR_U | MOD_MIR_MIRROR_V)) {
256                                 MTFace *tf = result->getFaceData(result, numFaces, CD_MTFACE);
257                                 if(tf) {
258                                         int j;
259                                         for(j = 0; j < 4; ++j) {
260                                                 if(mmd->flag & MOD_MIR_MIRROR_U)
261                                                         tf->uv[j][0] = 1.0f - tf->uv[j][0];
262                                                 if(mmd->flag & MOD_MIR_MIRROR_V)
263                                                         tf->uv[j][1] = 1.0f - tf->uv[j][1];
264                                         }
265                                 }
266                         }
267                         
268                         /* Flip face normal */
269                         SWAP(unsigned int, mf2->v1, mf2->v3);
270                         DM_swap_face_data(result, numFaces, corner_indices);
271                         
272                         test_index_face(mf2, &result->faceData, numFaces, inMF.v4?4:3);
273                         numFaces++;
274                 }
275         }
276
277         if (flip_map) MEM_freeN(flip_map);
278
279         MEM_freeN(indexMap);
280
281         CDDM_lower_num_verts(result, numVerts);
282         CDDM_lower_num_edges(result, numEdges);
283         CDDM_lower_num_faces(result, numFaces);
284
285         return result;
286 }
287
288 static DerivedMesh *mirrorModifier__doMirror(MirrorModifierData *mmd,
289                                                 Object *ob, DerivedMesh *dm)
290 {
291         DerivedMesh *result = dm;
292
293         /* check which axes have been toggled and mirror accordingly */
294         if(mmd->flag & MOD_MIR_AXIS_X) {
295                 result = doMirrorOnAxis(mmd, ob, result, 0);
296         }
297         if(mmd->flag & MOD_MIR_AXIS_Y) {
298                 DerivedMesh *tmp = result;
299                 result = doMirrorOnAxis(mmd, ob, result, 1);
300                 if(tmp != dm) tmp->release(tmp); /* free intermediate results */
301         }
302         if(mmd->flag & MOD_MIR_AXIS_Z) {
303                 DerivedMesh *tmp = result;
304                 result = doMirrorOnAxis(mmd, ob, result, 2);
305                 if(tmp != dm) tmp->release(tmp); /* free intermediate results */
306         }
307
308         return result;
309 }
310
311 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
312                                                 DerivedMesh *derivedData,
313                                                 int UNUSED(useRenderParams),
314                                                 int UNUSED(isFinalCalc))
315 {
316         DerivedMesh *result;
317         MirrorModifierData *mmd = (MirrorModifierData*) md;
318
319         result = mirrorModifier__doMirror(mmd, ob, derivedData);
320
321         if(result != derivedData)
322                 CDDM_calc_normals(result);
323         
324         return result;
325 }
326
327 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
328                                                 struct EditMesh *UNUSED(editData),
329                                                 DerivedMesh *derivedData)
330 {
331         return applyModifier(md, ob, derivedData, 0, 1);
332 }
333
334
335 ModifierTypeInfo modifierType_Mirror = {
336         /* name */              "Mirror",
337         /* structName */        "MirrorModifierData",
338         /* structSize */        sizeof(MirrorModifierData),
339         /* type */              eModifierTypeType_Constructive,
340         /* flags */             eModifierTypeFlag_AcceptsMesh
341                                                         | eModifierTypeFlag_SupportsMapping
342                                                         | eModifierTypeFlag_SupportsEditmode
343                                                         | eModifierTypeFlag_EnableInEditmode
344                                                         | eModifierTypeFlag_AcceptsCVs,
345
346         /* copyData */          copyData,
347         /* deformVerts */       NULL,
348         /* deformMatrices */    NULL,
349         /* deformVertsEM */     NULL,
350         /* deformMatricesEM */  NULL,
351         /* applyModifier */     applyModifier,
352         /* applyModifierEM */   applyModifierEM,
353         /* initData */          initData,
354         /* requiredDataMask */  NULL,
355         /* freeData */          NULL,
356         /* isDisabled */        NULL,
357         /* updateDepgraph */    updateDepgraph,
358         /* dependsOnTime */     NULL,
359         /* dependsOnNormals */  NULL,
360         /* foreachObjectLink */ foreachObjectLink,
361         /* foreachIDLink */     NULL,
362         /* foreachTexLink */    NULL,
363 };