mask modifier properly works in weightpaint and edit modes now. note that modifiers...
[blender.git] / source / blender / modifiers / intern / MOD_mask.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
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.
10 *
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.
15 *
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.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 #include "DNA_armature_types.h"
34 #include "DNA_meshdata_types.h"
35 #include "DNA_modifier_types.h"
36
37 #include "BLI_ghash.h"
38
39 #include "BKE_cdderivedmesh.h"
40 #include "BKE_mesh.h"
41 #include "BKE_modifier.h"
42 #include "BKE_deform.h"
43
44 #include "depsgraph_private.h"
45
46
47 static void copyData(ModifierData *md, ModifierData *target)
48 {
49         MaskModifierData *mmd = (MaskModifierData*) md;
50         MaskModifierData *tmmd = (MaskModifierData*) target;
51         
52         strcpy(tmmd->vgroup, mmd->vgroup);
53 }
54
55 static CustomDataMask requiredDataMask(Object *ob, ModifierData *md)
56 {
57         return (1 << CD_MDEFORMVERT);
58 }
59
60 static void foreachObjectLink(
61                                                   ModifierData *md, Object *ob,
62            void (*walk)(void *userData, Object *ob, Object **obpoin),
63                   void *userData)
64 {
65         MaskModifierData *mmd = (MaskModifierData *)md;
66         walk(userData, ob, &mmd->ob_arm);
67 }
68
69 static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *scene,
70                                            Object *ob, DagNode *obNode)
71 {
72         MaskModifierData *mmd = (MaskModifierData *)md;
73
74         if (mmd->ob_arm) 
75         {
76                 DagNode *armNode = dag_get_node(forest, mmd->ob_arm);
77                 
78                 dag_add_relation(forest, armNode, obNode,
79                                 DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Mask Modifier");
80         }
81 }
82
83 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
84                 DerivedMesh *derivedData,
85   int useRenderParams, int isFinalCalc)
86 {
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;
94         int i;
95         
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
100          */
101         
102         /* get original number of verts, edges, and faces */
103         maxVerts= dm->getNumVerts(dm);
104         maxEdges= dm->getNumEdges(dm);
105         maxFaces= dm->getNumFaces(dm);
106         
107         /* check if we can just return the original mesh 
108          *      - must have verts and therefore verts assigned to vgroups to do anything useful
109          */
110         if ( !(ELEM(mmd->mode, MOD_MASK_MODE_ARM, MOD_MASK_MODE_VGROUP)) ||
111                  (maxVerts == 0) || (ob->defbase.first == NULL) )
112         {
113                 return derivedData;
114         }
115         
116         /* if mode is to use selected armature bones, aggregate the bone groups */
117         if (mmd->mode == MOD_MASK_MODE_ARM) /* --- using selected bones --- */
118         {
119                 GHash *vgroupHash, *boneHash;
120                 Object *oba= mmd->ob_arm;
121                 bPoseChannel *pchan;
122                 bDeformGroup *def;
123                 
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))
126                         return derivedData;             
127                 
128                 /* hashes for finding mapping of:
129                  *      - vgroups to indicies -> vgroupHash  (string, int)
130                  *      - bones to vgroup indices -> boneHash (index of vgroup, dummy)
131                  */
132                 vgroupHash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp);
133                 boneHash= BLI_ghash_new(BLI_ghashutil_inthash, BLI_ghashutil_intcmp);
134                 
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));
138                 
139                 /* get selected-posechannel <-> vertexgroup index mapping */
140                 for (pchan= oba->pose->chanbase.first; pchan; pchan= pchan->next) 
141                 {
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) ) 
146                         {
147                                 /* check if hash has group for this bone */
148                                 if (BLI_ghash_haskey(vgroupHash, pchan->name)) 
149                                 {
150                                         int defgrp_index= GET_INT_FROM_POINTER(BLI_ghash_lookup(vgroupHash, pchan->name));
151                                         
152                                         /* add index to hash (store under key only) */
153                                         BLI_ghash_insert(boneHash, SET_INT_IN_POINTER(defgrp_index), pchan);
154                                 }
155                         }
156                 }
157                 
158                 /* if no bones selected, free hashes and return original mesh */
159                 if (BLI_ghash_size(boneHash) == 0)
160                 {
161                         BLI_ghash_free(vgroupHash, NULL, NULL);
162                         BLI_ghash_free(boneHash, NULL, NULL);
163                         
164                         return derivedData;
165                 }
166                 
167                 /* repeat the previous check, but for dverts */
168                 dvert= dm->getVertDataArray(dm, CD_MDEFORMVERT);
169                 if (dvert == NULL)
170                 {
171                         BLI_ghash_free(vgroupHash, NULL, NULL);
172                         BLI_ghash_free(boneHash, NULL, NULL);
173                         
174                         return derivedData;
175                 }
176                 
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);
179                 
180                 /* add vertices which exist in vertexgroups into vertHash for filtering */
181                 for (i = 0; i < maxVerts; i++) 
182                 {
183                         MDeformWeight *def_weight = NULL;
184                         int j;
185                         
186                         for (j= 0; j < dvert[i].totweight; j++) 
187                         {
188                                 if (BLI_ghash_haskey(boneHash, SET_INT_IN_POINTER(dvert[i].dw[j].def_nr))) 
189                                 {
190                                         def_weight = &dvert[i].dw[j];
191                                         break;
192                                 }
193                         }
194                         
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;
199                         }
200                         else {
201                                 /* if this vert isn't in the vgroup, don't include it in vertHash */
202                                 if (!def_weight) continue;
203                         }
204                         
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));
207                         numVerts++;
208                 }
209                 
210                 /* free temp hashes */
211                 BLI_ghash_free(vgroupHash, NULL, NULL);
212                 BLI_ghash_free(boneHash, NULL, NULL);
213         }
214         else            /* --- Using Nominated VertexGroup only --- */ 
215         {
216                 int defgrp_index = defgroup_name_index(ob, mmd->vgroup);
217                 
218                 /* get dverts */
219                 if (defgrp_index >= 0)
220                         dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
221                         
222                 /* if no vgroup (i.e. dverts) found, return the initial mesh */
223                 if ((defgrp_index < 0) || (dvert == NULL))
224                         return dm;
225                         
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);
228                 
229                 /* add vertices which exist in vertexgroup into ghash for filtering */
230                 for (i = 0; i < maxVerts; i++) 
231                 {
232                         MDeformWeight *def_weight = NULL;
233                         int j;
234                         
235                         for (j= 0; j < dvert[i].totweight; j++) 
236                         {
237                                 if (dvert[i].dw[j].def_nr == defgrp_index) 
238                                 {
239                                         def_weight = &dvert[i].dw[j];
240                                         break;
241                                 }
242                         }
243                         
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;
248                         }
249                         else {
250                                 /* if this vert isn't in the vgroup, don't include it in vertHash */
251                                 if (!def_weight) continue;
252                         }
253                         
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));
256                         numVerts++;
257                 }
258         }
259         
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);
263         
264         /* loop over edges and faces, and do the same thing to 
265          * ensure that they only reference existing verts 
266          */
267         for (i = 0; i < maxEdges; i++) 
268         {
269                 MEdge me;
270                 dm->getEdge(dm, i, &me);
271                 
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)) )
275                 {
276                         BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numEdges));
277                         numEdges++;
278                 }
279         }
280         for (i = 0; i < maxFaces; i++) 
281         {
282                 MFace mf;
283                 dm->getFace(dm, i, &mf);
284                 
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))) )
290                 {
291                         BLI_ghash_insert(faceHash, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(numFaces));
292                         numFaces++;
293                 }
294         }
295         
296         
297         /* now we know the number of verts, edges and faces, 
298          * we can create the new (reduced) mesh
299          */
300         result = CDDM_from_template(dm, numVerts, numEdges, numFaces);
301         
302         
303         /* using ghash-iterators, map data into new mesh */
304                 /* vertices */
305         for ( hashIter = BLI_ghashIterator_new(vertHash);
306                   !BLI_ghashIterator_isDone(hashIter);
307                   BLI_ghashIterator_step(hashIter) ) 
308         {
309                 MVert source;
310                 MVert *dest;
311                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
312                 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
313                 
314                 dm->getVert(dm, oldIndex, &source);
315                 dest = CDDM_get_vert(result, newIndex);
316                 
317                 DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
318                 *dest = source;
319         }
320         BLI_ghashIterator_free(hashIter);
321                 
322                 /* edges */
323         for ( hashIter = BLI_ghashIterator_new(edgeHash);
324                   !BLI_ghashIterator_isDone(hashIter);
325                   BLI_ghashIterator_step(hashIter) ) 
326         {
327                 MEdge source;
328                 MEdge *dest;
329                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
330                 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
331                 
332                 dm->getEdge(dm, oldIndex, &source);
333                 dest = CDDM_get_edge(result, newIndex);
334                 
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)));
337                 
338                 DM_copy_edge_data(dm, result, oldIndex, newIndex, 1);
339                 *dest = source;
340         }
341         BLI_ghashIterator_free(hashIter);
342         
343                 /* faces */
344         for ( hashIter = BLI_ghashIterator_new(faceHash);
345                   !BLI_ghashIterator_isDone(hashIter);
346                   BLI_ghashIterator_step(hashIter) ) 
347         {
348                 MFace source;
349                 MFace *dest;
350                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
351                 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
352                 int orig_v4;
353                 
354                 dm->getFace(dm, oldIndex, &source);
355                 dest = CDDM_get_face(result, newIndex);
356                 
357                 orig_v4 = source.v4;
358                 
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)));
362                 if (source.v4)
363                    source.v4 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v4)));
364                 
365                 DM_copy_face_data(dm, result, oldIndex, newIndex, 1);
366                 *dest = source;
367                 
368                 test_index_face(dest, &result->faceData, newIndex, (orig_v4 ? 4 : 3));
369         }
370         BLI_ghashIterator_free(hashIter);
371         
372         /* recalculate normals */
373         CDDM_calc_normals(result);
374         
375         /* free hashes */
376         BLI_ghash_free(vertHash, NULL, NULL);
377         BLI_ghash_free(edgeHash, NULL, NULL);
378         BLI_ghash_free(faceHash, NULL, NULL);
379         
380         /* return the new mesh */
381         return result;
382 }
383
384
385 ModifierTypeInfo modifierType_Mask = {
386         /* name */              "Mask",
387         /* structName */        "MaskModifierData",
388         /* structSize */        sizeof(MaskModifierData),
389         /* type */              eModifierTypeType_Nonconstructive,
390         /* flags */             eModifierTypeFlag_AcceptsMesh|eModifierTypeFlag_SupportsMapping|eModifierTypeFlag_SupportsEditmode,
391
392         /* copyData */          copyData,
393         /* deformVerts */       0,
394         /* deformVertsEM */     0,
395         /* deformMatricesEM */  0,
396         /* applyModifier */     applyModifier,
397         /* applyModifierEM */   0,
398         /* initData */          0,
399         /* requiredDataMask */  requiredDataMask,
400         /* freeData */          0,
401         /* isDisabled */        0,
402         /* updateDepgraph */    updateDepgraph,
403         /* dependsOnTime */     0,
404         /* foreachObjectLink */ foreachObjectLink,
405         /* foreachIDLink */     0,
406 };