1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2005 by the Blender Foundation.
* All rights reserved.
*
* Contributor(s): Daniel Dunbar
* Ton Roosendaal,
* Ben Batt,
* Brecht Van Lommel,
* Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*
*/
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "BLI_math.h"
#include "BLI_smallhash.h"
#include "BLI_array.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_mesh.h"
#include "BKE_modifier.h"
#include "BKE_deform.h"
#include "BKE_utildefines.h"
#include "BKE_tessmesh.h"
#include "MEM_guardedalloc.h"
#include "depsgraph_private.h"
static void initData(ModifierData *md)
{
MirrorModifierData *mmd = (MirrorModifierData*) md;
mmd->flag |= (MOD_MIR_AXIS_X | MOD_MIR_VGROUP);
mmd->tolerance = 0.001;
mmd->mirror_ob = NULL;
}
static void copyData(ModifierData *md, ModifierData *target)
{
MirrorModifierData *mmd = (MirrorModifierData*) md;
MirrorModifierData *tmmd = (MirrorModifierData*) target;
tmmd->axis = mmd->axis;
tmmd->flag = mmd->flag;
tmmd->tolerance = mmd->tolerance;
tmmd->mirror_ob = mmd->mirror_ob;;
}
static void foreachObjectLink(
ModifierData *md, Object *ob,
void (*walk)(void *userData, Object *ob, Object **obpoin),
void *userData)
{
MirrorModifierData *mmd = (MirrorModifierData*) md;
if (mmd->mirror_ob)
walk(userData, ob, &mmd->mirror_ob);
}
static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *UNUSED(scene),
Object *UNUSED(ob), DagNode *obNode)
{
MirrorModifierData *mmd = (MirrorModifierData*) md;
if(mmd->mirror_ob) {
DagNode *latNode = dag_get_node(forest, mmd->mirror_ob);
dag_add_relation(forest, latNode, obNode,
DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Mirror Modifier");
}
}
/* Mirror */
#define VERT_NEW 1
void vertgroup_flip_name (char *name, int strip_number);
DerivedMesh *doMirrorOnAxis(MirrorModifierData *mmd,
Object *ob,
DerivedMesh *dm,
int UNUSED(initFlags),
int axis)
{
float tolerance = mmd->tolerance;
DerivedMesh *cddm, *origdm;
bDeformGroup *def, *defb;
bDeformGroup **vector_def = NULL;
MVert *mv;
MEdge *me;
MLoop *ml;
MPoly *mp;
float mtx[4][4], imtx[4][4];
int i, j, *vtargetmap = NULL;
BLI_array_declare(vtargetmap);
int vector_size=0, a, b;
origdm = dm;
if (!CDDM_Check(dm))
dm = CDDM_copy(dm, 0);
if (mmd->flag & MOD_MIR_VGROUP) {
/* calculate the number of deformedGroups */
for(vector_size = 0, def = ob->defbase.first; def;
def = def->next, vector_size++);
/* load the deformedGroups for fast access */
vector_def =
(bDeformGroup **)MEM_mallocN(sizeof(bDeformGroup*) * vector_size,
"group_index");
for(a = 0, def = ob->defbase.first; def; def = def->next, a++) {
vector_def[a] = def;
}
}
if (mmd->mirror_ob) {
float mtx2[4][4];
invert_m4_m4(mtx2, mmd->mirror_ob->obmat);
mul_m4_m4m4(mtx, ob->obmat, mtx2);
} else {
unit_m4(mtx);
}
cddm = CDDM_from_template(dm, dm->numVertData*2, dm->numEdgeData*2, 0, dm->numLoopData*2, dm->numPolyData*2);
/*copy customdata to original geometry*/
CustomData_copy_data(&dm->vertData, &cddm->vertData, 0, 0, dm->numVertData);
CustomData_copy_data(&dm->edgeData, &cddm->edgeData, 0, 0, dm->numEdgeData);
CustomData_copy_data(&dm->loopData, &cddm->loopData, 0, 0, dm->numLoopData);
CustomData_copy_data(&dm->polyData, &cddm->polyData, 0, 0, dm->numPolyData);
/*copy customdata to new geometry*/
CustomData_copy_data(&dm->vertData, &cddm->vertData, 0, dm->numVertData, dm->numVertData);
CustomData_copy_data(&dm->edgeData, &cddm->edgeData, 0, dm->numEdgeData, dm->numEdgeData);
CustomData_copy_data(&dm->polyData, &cddm->polyData, 0, dm->numPolyData, dm->numPolyData);
/*mirror vertex coordinates*/
mv = CDDM_get_verts(cddm) + dm->numVertData;
for (i=0; i<dm->numVertData; i++, mv++) {
mv->co[axis] = -mv->co[axis];
if (fabs(mv->co[axis]) < tolerance) {
BLI_array_append(vtargetmap, i+dm->numVertData);
} else BLI_array_append(vtargetmap, -1);
}
for (i=0; i<dm->numVertData; i++) {
BLI_array_append(vtargetmap, -1);
}
/*adjust mirrored edge vertex indices*/
me = CDDM_get_edges(cddm) + dm->numEdgeData;
for (i=0; i<dm->numEdgeData; i++, me++) {
me->v1 += dm->numVertData;
me->v2 += dm->numVertData;
}
/*adjust mirrored poly loopstart indices, and reverse loop order (normals)*/
mp = CDDM_get_polys(cddm) + dm->numPolyData;
ml = CDDM_get_loops(cddm);
for (i=0; i<dm->numPolyData; i++, mp++) {
MLoop *ml2;
int e;
for (j=0; j<mp->totloop; j++) {
CustomData_copy_data(&dm->loopData, &cddm->loopData, mp->loopstart+j,
mp->loopstart+dm->numLoopData+mp->totloop-j-1, 1);
}
ml2 = ml + mp->loopstart + dm->numLoopData;
e = ml2[0].e;
for (j=0; j<mp->totloop-1; j++) {
ml2[j].e = ml2[j+1].e;
}
ml2[mp->totloop-1].e = e;
mp->loopstart += dm->numLoopData;
}
/*adjust mirrored loop vertex and edge indices*/
ml = CDDM_get_loops(cddm) + dm->numLoopData;
for (i=0; i<dm->numLoopData; i++, ml++) {
ml->v += dm->numVertData;
ml->e += dm->numEdgeData;
}
CDDM_recalc_tesselation(cddm, 1);
/*handle vgroup stuff*/
if ((mmd->flag & MOD_MIR_VGROUP) && CustomData_has_layer(&cddm->vertData, CD_MDEFORMVERT)) {
MDeformVert *dvert = CustomData_get_layer(&cddm->vertData, CD_MDEFORMVERT);
for (i=0; i<dm->numVertData; i++, dvert++) {
for(j = 0; j < dvert->totweight; ++j) {
char tmpname[32];
if(dvert->dw[j].def_nr < 0 ||
dvert->dw[j].def_nr >= vector_size)
continue;
def = vector_def[dvert->dw[j].def_nr];
strcpy(tmpname, def->name);
vertgroup_flip_name(tmpname,0);
for(b = 0, defb = ob->defbase.first; defb;
defb = defb->next, b++)
{
if(!strcmp(defb->name, tmpname))
{
dvert->dw[j].def_nr = b;
break;
}
}
}
}
}
if (!(mmd->flag & MOD_MIR_NO_MERGE))
cddm = CDDM_merge_verts(cddm, vtargetmap);
BLI_array_free(vtargetmap);
if (vector_def) MEM_freeN(vector_def);
if (dm != origdm) {
dm->needsFree = 1;
dm->release(dm);
}
return cddm;
}
static DerivedMesh *mirrorModifier__doMirror(MirrorModifierData *mmd,
Object *ob, DerivedMesh *dm,
int initFlags)
{
DerivedMesh *result = dm;
/* check which axes have been toggled and mirror accordingly */
if(mmd->flag & MOD_MIR_AXIS_X) {
result = doMirrorOnAxis(mmd, ob, result, initFlags, 0);
}
if(mmd->flag & MOD_MIR_AXIS_Y) {
DerivedMesh *tmp = result;
result = doMirrorOnAxis(mmd, ob, result, initFlags, 1);
if(tmp != dm) tmp->release(tmp); /* free intermediate results */
}
if(mmd->flag & MOD_MIR_AXIS_Z) {
DerivedMesh *tmp = result;
result = doMirrorOnAxis(mmd, ob, result, initFlags, 2);
if(tmp != dm) tmp->release(tmp); /* free intermediate results */
}
return result;
}
static DerivedMesh *applyModifier(
ModifierData *md, Object *ob, DerivedMesh *derivedData,
int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
{
DerivedMesh *result;
MirrorModifierData *mmd = (MirrorModifierData*) md;
result = mirrorModifier__doMirror(mmd, ob, derivedData, 0);
if(result != derivedData)
CDDM_calc_normals(result);
return result;
}
static DerivedMesh *applyModifierEM(
ModifierData *md, Object *ob, struct BMEditMesh *UNUSED(editData),
DerivedMesh *derivedData)
{
return applyModifier(md, ob, derivedData, 0, 1);
}
ModifierTypeInfo modifierType_Mirror = {
/* name */ "Mirror",
/* structName */ "MirrorModifierData",
/* structSize */ sizeof(MirrorModifierData),
/* type */ eModifierTypeType_Constructive,
/* flags */ eModifierTypeFlag_AcceptsMesh
| eModifierTypeFlag_SupportsMapping
| eModifierTypeFlag_SupportsEditmode
| eModifierTypeFlag_EnableInEditmode
| eModifierTypeFlag_AcceptsCVs,
/* copyData */ copyData,
/* deformVerts */ NULL,
/* deformMatrices */ NULL,
/* deformVertsEM */ NULL,
/* deformMatricesEM */ NULL,
/* applyModifier */ applyModifier,
/* applyModifierEM */ applyModifierEM,
/* initData */ initData,
/* requiredDataMask */ NULL,
/* freeData */ NULL,
/* isDisabled */ NULL,
/* updateDepgraph */ updateDepgraph,
/* dependsOnTime */ NULL,
/* dependsOnNormals */ NULL,
/* foreachObjectLink */ foreachObjectLink,
/* foreachIDLink */ NULL,
};
|