4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
23 * Contributor(s): Daniel Dunbar
29 * ***** END GPL LICENSE BLOCK *****
33 #include "DNA_armature_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_modifier_types.h"
37 #include "BLI_ghash.h"
39 #include "BKE_cdderivedmesh.h"
41 #include "BKE_modifier.h"
42 #include "BKE_deform.h"
44 #include "depsgraph_private.h"
47 static void copyData(ModifierData *md, ModifierData *target)
49 MaskModifierData *mmd = (MaskModifierData*) md;
50 MaskModifierData *tmmd = (MaskModifierData*) target;
52 strcpy(tmmd->vgroup, mmd->vgroup);
55 static CustomDataMask requiredDataMask(Object *ob, ModifierData *md)
57 return (1 << CD_MDEFORMVERT);
60 static void foreachObjectLink(
61 ModifierData *md, Object *ob,
62 void (*walk)(void *userData, Object *ob, Object **obpoin),
65 MaskModifierData *mmd = (MaskModifierData *)md;
66 walk(userData, ob, &mmd->ob_arm);
69 static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *scene,
70 Object *ob, DagNode *obNode)
72 MaskModifierData *mmd = (MaskModifierData *)md;
76 DagNode *armNode = dag_get_node(forest, mmd->ob_arm);
78 dag_add_relation(forest, armNode, obNode,
79 DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Mask Modifier");
83 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
84 DerivedMesh *derivedData,
85 int useRenderParams, int isFinalCalc)
87 MaskModifierData *mmd= (MaskModifierData *)md;
88 DerivedMesh *dm= derivedData, *result= NULL;
89 GHash *vertHash=NULL, *edgeHash, *faceHash;
90 GHashIterator *hashIter;
91 MDeformVert *dvert= NULL;
92 int numFaces=0, numEdges=0, numVerts=0;
93 int maxVerts, maxEdges, maxFaces;
96 /* Overview of Method:
97 * 1. Get the vertices that are in the vertexgroup of interest
98 * 2. Filter out unwanted geometry (i.e. not in vertexgroup), by populating mappings with new vs old indices
99 * 3. Make a new mesh containing only the mapping data
102 /* get original number of verts, edges, and faces */
103 maxVerts= dm->getNumVerts(dm);
104 maxEdges= dm->getNumEdges(dm);
105 maxFaces= dm->getNumFaces(dm);
107 /* check if we can just return the original mesh
108 * - must have verts and therefore verts assigned to vgroups to do anything useful
110 if ( !(ELEM(mmd->mode, MOD_MASK_MODE_ARM, MOD_MASK_MODE_VGROUP)) ||
111 (maxVerts == 0) || (ob->defbase.first == NULL) )
116 /* if mode is to use selected armature bones, aggregate the bone groups */
117 if (mmd->mode == MOD_MASK_MODE_ARM) /* --- using selected bones --- */
119 GHash *vgroupHash, *boneHash;
120 Object *oba= mmd->ob_arm;
124 /* check that there is armature object with bones to use, otherwise return original mesh */
125 if (ELEM(NULL, mmd->ob_arm, mmd->ob_arm->pose))
128 /* hashes for finding mapping of:
129 * - vgroups to indicies -> vgroupHash (string, int)
130 * - bones to vgroup indices -> boneHash (index of vgroup, dummy)
132 vgroupHash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp);
133 boneHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
135 /* build mapping of names of vertex groups to indices */
136 for (i = 0, def = ob->defbase.first; def; def = def->next, i++)
137 BLI_ghash_insert(vgroupHash, def->name, SET_INT_IN_POINTER(i));
139 /* get selected-posechannel <-> vertexgroup index mapping */
140 for (pchan= oba->pose->chanbase.first; pchan; pchan= pchan->next)
142 /* check if bone is selected */
143 // TODO: include checks for visibility too?
144 // FIXME: the depsgraph needs extensions to make this work in realtime...
145 if ( (pchan->bone) && (pchan->bone->flag & BONE_SELECTED) )
147 /* check if hash has group for this bone */
148 if (BLI_ghash_haskey(vgroupHash, pchan->name))
150 int defgrp_index= GET_INT_FROM_POINTER(BLI_ghash_lookup(vgroupHash, pchan->name));
152 /* add index to hash (store under key only) */
153 BLI_ghash_insert(boneHash, SET_INT_IN_POINTER(defgrp_index), pchan);
158 /* if no bones selected, free hashes and return original mesh */
159 if (BLI_ghash_size(boneHash) == 0)
161 BLI_ghash_free(vgroupHash, NULL, NULL);
162 BLI_ghash_free(boneHash, NULL, NULL);
167 /* repeat the previous check, but for dverts */
168 dvert= dm->getVertDataArray(dm, CD_MDEFORMVERT);
171 BLI_ghash_free(vgroupHash, NULL, NULL);
172 BLI_ghash_free(boneHash, NULL, NULL);
177 /* hashes for quickly providing a mapping from old to new - use key=oldindex, value=newindex */
178 vertHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
180 /* add vertices which exist in vertexgroups into vertHash for filtering */
181 for (i = 0; i < maxVerts; i++)
183 MDeformWeight *def_weight = NULL;
186 for (j= 0; j < dvert[i].totweight; j++)
188 if (BLI_ghash_haskey(boneHash, SET_INT_IN_POINTER(dvert[i].dw[j].def_nr)))
190 def_weight = &dvert[i].dw[j];
195 /* check if include vert in vertHash */
196 if (mmd->flag & MOD_MASK_INV) {
197 /* if this vert is in the vgroup, don't include it in vertHash */
198 if (def_weight) continue;
201 /* if this vert isn't in the vgroup, don't include it in vertHash */
202 if (!def_weight) continue;
205 /* add to ghash for verts (numVerts acts as counter for mapping) */
206 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numVerts));
210 /* free temp hashes */
211 BLI_ghash_free(vgroupHash, NULL, NULL);
212 BLI_ghash_free(boneHash, NULL, NULL);
214 else /* --- Using Nominated VertexGroup only --- */
216 int defgrp_index = defgroup_name_index(ob, mmd->vgroup);
219 if (defgrp_index >= 0)
220 dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
222 /* if no vgroup (i.e. dverts) found, return the initial mesh */
223 if ((defgrp_index < 0) || (dvert == NULL))
226 /* hashes for quickly providing a mapping from old to new - use key=oldindex, value=newindex */
227 vertHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
229 /* add vertices which exist in vertexgroup into ghash for filtering */
230 for (i = 0; i < maxVerts; i++)
232 MDeformWeight *def_weight = NULL;
235 for (j= 0; j < dvert[i].totweight; j++)
237 if (dvert[i].dw[j].def_nr == defgrp_index)
239 def_weight = &dvert[i].dw[j];
244 /* check if include vert in vertHash */
245 if (mmd->flag & MOD_MASK_INV) {
246 /* if this vert is in the vgroup, don't include it in vertHash */
247 if (def_weight) continue;
250 /* if this vert isn't in the vgroup, don't include it in vertHash */
251 if (!def_weight) continue;
254 /* add to ghash for verts (numVerts acts as counter for mapping) */
255 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numVerts));
260 /* hashes for quickly providing a mapping from old to new - use key=oldindex, value=newindex */
261 edgeHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
262 faceHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
264 /* loop over edges and faces, and do the same thing to
265 * ensure that they only reference existing verts
267 for (i = 0; i < maxEdges; i++)
270 dm->getEdge(dm, i, &me);
272 /* only add if both verts will be in new mesh */
273 if ( BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v1)) &&
274 BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v2)) )
276 BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numEdges));
280 for (i = 0; i < maxFaces; i++)
283 dm->getFace(dm, i, &mf);
285 /* all verts must be available */
286 if ( BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v1)) &&
287 BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v2)) &&
288 BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v3)) &&
289 (mf.v4==0 || BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v4))) )
291 BLI_ghash_insert(faceHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numFaces));
297 /* now we know the number of verts, edges and faces,
298 * we can create the new (reduced) mesh
300 result = CDDM_from_template(dm, numVerts, numEdges, numFaces);
303 /* using ghash-iterators, map data into new mesh */
305 for ( hashIter = BLI_ghashIterator_new(vertHash);
306 !BLI_ghashIterator_isDone(hashIter);
307 BLI_ghashIterator_step(hashIter) )
311 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
312 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
314 dm->getVert(dm, oldIndex, &source);
315 dest = CDDM_get_vert(result, newIndex);
317 DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
320 BLI_ghashIterator_free(hashIter);
323 for ( hashIter = BLI_ghashIterator_new(edgeHash);
324 !BLI_ghashIterator_isDone(hashIter);
325 BLI_ghashIterator_step(hashIter) )
329 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
330 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
332 dm->getEdge(dm, oldIndex, &source);
333 dest = CDDM_get_edge(result, newIndex);
335 source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
336 source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
338 DM_copy_edge_data(dm, result, oldIndex, newIndex, 1);
341 BLI_ghashIterator_free(hashIter);
344 for ( hashIter = BLI_ghashIterator_new(faceHash);
345 !BLI_ghashIterator_isDone(hashIter);
346 BLI_ghashIterator_step(hashIter) )
350 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
351 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
354 dm->getFace(dm, oldIndex, &source);
355 dest = CDDM_get_face(result, newIndex);
359 source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
360 source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
361 source.v3 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v3)));
363 source.v4 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v4)));
365 DM_copy_face_data(dm, result, oldIndex, newIndex, 1);
368 test_index_face(dest, &result->faceData, newIndex, (orig_v4 ? 4 : 3));
370 BLI_ghashIterator_free(hashIter);
372 /* recalculate normals */
373 CDDM_calc_normals(result);
376 BLI_ghash_free(vertHash, NULL, NULL);
377 BLI_ghash_free(edgeHash, NULL, NULL);
378 BLI_ghash_free(faceHash, NULL, NULL);
380 /* return the new mesh */
385 ModifierTypeInfo modifierType_Mask = {
387 /* structName */ "MaskModifierData",
388 /* structSize */ sizeof(MaskModifierData),
389 /* type */ eModifierTypeType_Nonconstructive,
390 /* flags */ eModifierTypeFlag_AcceptsMesh|eModifierTypeFlag_SupportsMapping|eModifierTypeFlag_SupportsEditmode,
392 /* copyData */ copyData,
394 /* deformVertsEM */ 0,
395 /* deformMatricesEM */ 0,
396 /* applyModifier */ applyModifier,
397 /* applyModifierEM */ 0,
399 /* requiredDataMask */ requiredDataMask,
402 /* updateDepgraph */ updateDepgraph,
403 /* dependsOnTime */ 0,
404 /* foreachObjectLink */ foreachObjectLink,
405 /* foreachIDLink */ 0,