vgroup_modifiers: coded a way to test whether the dm needs to be copied (i.e. if...
[blender.git] / source / blender / modifiers / intern / MOD_weightvgedit.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) 2011 by Bastien Montagne.
21 * All rights reserved.
22 *
23 * Contributor(s): None yet.
24 *
25 * ***** END GPL LICENSE BLOCK *****
26 *
27 */
28
29 /*
30  * XXX I’d like to make modified weights visible in WeightPaint mode,
31  *     but couldn’t figure a way to do this…
32  *     Maybe this will need changes in mesh_calc_modifiers (DerivedMesh.c)?
33  *     Or the WeightPaint mode code itself?
34  */
35
36 #include "BLI_utildefines.h"
37 #include "BLI_math.h"
38 #include "BLI_string.h"
39
40 #include "DNA_color_types.h"      /* CurveMapping. */
41 #include "DNA_mesh_types.h"
42 #include "DNA_meshdata_types.h"
43 #include "DNA_modifier_types.h"
44 #include "DNA_object_types.h"
45
46 #include "BKE_cdderivedmesh.h"
47 #include "BKE_colortools.h"       /* CurveMapping. */
48 #include "BKE_deform.h"
49 #include "BKE_mesh.h"
50 #include "BKE_modifier.h"
51 #include "BKE_texture.h"          /* Texture masking. */
52
53 #include "depsgraph_private.h"
54 #include "MEM_guardedalloc.h"
55 #include "MOD_util.h"
56 #include "MOD_weightvg_util.h"
57
58 /**************************************
59  * Modifiers functions.               *
60  **************************************/
61 static void initData(ModifierData *md)
62 {
63         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
64         wmd->edit_flags             = MOD_WVG_EDIT_CLAMP;
65         wmd->default_weight         = 0.0f;
66
67         wmd->map_org_min            = 0.0f;
68         wmd->map_org_max            = 1.0f;
69         wmd->map_new_min            = 0.0f;
70         wmd->map_new_max            = 1.0f;
71         wmd->cmap_curve             = curvemapping_add(1, 0.0, 0.0, 1.0, 1.0);
72         curvemapping_initialize(wmd->cmap_curve);
73
74         wmd->clamp_min_weight       = 0.0f;
75         wmd->clamp_max_weight       = 1.0f;
76
77         wmd->add_threshold          = 0.01f;
78         wmd->rem_threshold          = 0.01f;
79
80         wmd->mask_constant          = 1.0f;
81         wmd->mask_tex_use_channel   = MOD_WVG_MASK_TEX_USE_INT; /* Use intensity by default. */
82         wmd->mask_tex_mapping       = MOD_DISP_MAP_LOCAL;
83 }
84
85 static void freeData(ModifierData *md)
86 {
87         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
88         curvemapping_free(wmd->cmap_curve);
89 }
90
91 static void copyData(ModifierData *md, ModifierData *target)
92 {
93         WeightVGEditModifierData *wmd  = (WeightVGEditModifierData*) md;
94         WeightVGEditModifierData *twmd = (WeightVGEditModifierData*) target;
95
96         BLI_strncpy(twmd->defgrp_name, wmd->defgrp_name, sizeof(twmd->defgrp_name));
97
98         twmd->edit_flags             = wmd->edit_flags;
99         twmd->default_weight         = wmd->default_weight;
100
101         twmd->map_org_min            = wmd->map_org_min;
102         twmd->map_org_max            = wmd->map_org_max;
103         twmd->map_new_min            = wmd->map_new_min;
104         twmd->map_new_max            = wmd->map_new_max;
105         twmd->cmap_curve             = curvemapping_copy(wmd->cmap_curve);
106
107         twmd->clamp_min_weight       = wmd->clamp_min_weight;
108         twmd->clamp_max_weight       = wmd->clamp_max_weight;
109
110         twmd->add_threshold          = wmd->add_threshold;
111         twmd->rem_threshold          = wmd->rem_threshold;
112
113         twmd->mask_constant          = wmd->mask_constant;
114         BLI_strncpy(twmd->mask_defgrp_name, wmd->mask_defgrp_name, sizeof(twmd->mask_defgrp_name));
115         twmd->mask_texture           = wmd->mask_texture;
116         twmd->mask_tex_use_channel   = wmd->mask_tex_use_channel;
117         twmd->mask_tex_mapping       = wmd->mask_tex_mapping;
118         twmd->mask_tex_map_obj       = wmd->mask_tex_map_obj;
119         BLI_strncpy(twmd->mask_tex_uvlayer_name, wmd->mask_tex_uvlayer_name,
120                     sizeof(twmd->mask_tex_uvlayer_name));
121 }
122
123 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
124 {
125         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
126         CustomDataMask dataMask = 0;
127
128         /* We need vertex groups! */
129         dataMask |= CD_MASK_MDEFORMVERT;
130
131         /* Ask for UV coordinates if we need them. */
132         if(wmd->mask_tex_mapping == MOD_DISP_MAP_UV)
133                 dataMask |= CD_MASK_MTFACE;
134
135         return dataMask;
136 }
137
138 static int dependsOnTime(ModifierData *md)
139 {
140         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
141
142         if(wmd->mask_texture)
143                 return BKE_texture_dependsOnTime(wmd->mask_texture);
144         return 0;
145 }
146
147 static void foreachObjectLink(ModifierData *md, Object *ob,
148                               void (*walk)(void *userData, Object *ob, Object **obpoin),
149                               void *userData)
150 {
151         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
152         walk(userData, ob, &wmd->mask_tex_map_obj);
153 }
154
155 static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
156 {
157         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
158
159         walk(userData, ob, (ID **)&wmd->mask_texture);
160
161         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
162 }
163
164 static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *UNUSED(scene),
165                            Object *UNUSED(ob), DagNode *obNode)
166 {
167         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
168         DagNode *curNode;
169
170         if(wmd->mask_tex_map_obj && wmd->mask_tex_mapping == MOD_DISP_MAP_OBJECT) {
171                 curNode = dag_get_node(forest, wmd->mask_tex_map_obj);
172
173                 dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA,
174                                  "WeightVGEdit Modifier");
175         }
176
177         if(wmd->mask_tex_mapping == MOD_DISP_MAP_GLOBAL)
178                 dag_add_relation(forest, obNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA,
179                                  "WeightVGEdit Modifier");
180 }
181
182 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
183 {
184         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
185         /* If no vertex group, bypass. */
186         return (wmd->defgrp_name == NULL);
187 }
188
189 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
190                                   int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
191 {
192         WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md;
193         DerivedMesh *dm = derivedData, *ret = NULL;
194         Mesh *ob_m = NULL;
195         MDeformVert *dvert = NULL;
196         float *org_w = NULL; /* Array original weights. */
197         float *new_w = NULL; /* Array new weights. */
198         int numVerts;
199         int defgrp_idx;
200         int i;
201         char rel_ret = 0; /* Boolean, whether we have to release ret dm or not, when not using it! */
202         float *mapf = NULL; /* Cache for mapping factors. */
203         /* Flags. */
204         char do_map   = wmd->edit_flags & MOD_WVG_EDIT_MAP;
205         char do_cmap  = wmd->edit_flags & MOD_WVG_EDIT_CMAP;
206         char do_rev   = wmd->edit_flags & MOD_WVG_EDIT_REVERSE_WEIGHTS;
207         char do_add   = wmd->edit_flags & MOD_WVG_EDIT_ADD2VG;
208         char do_rem   = wmd->edit_flags & MOD_WVG_EDIT_REMFVG;
209         char do_clamp = wmd->edit_flags & MOD_WVG_EDIT_CLAMP;
210
211         /* Get number of verts. */
212         numVerts = dm->getNumVerts(dm);
213
214         /* Check if we can just return the original mesh.
215          * Must have verts and therefore verts assigned to vgroups to do anything useful!
216          */
217         if ((numVerts == 0) || (ob->defbase.first == NULL))
218                 return dm;
219
220         /* Get vgroup idx from its name. */
221         defgrp_idx = defgroup_name_index(ob, wmd->defgrp_name);
222         if (defgrp_idx < 0)
223                 return dm;
224
225         /* XXX All this to avoid copying dm when not needed… However, it nearly doubles compute
226          *     time! See scene 5 of the WeighVG test file…
227          */
228 #if 0
229         /* Get actual dverts (ie vertex group data). */
230         dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
231         /* If no dverts, return unmodified data… */
232         if (dvert == NULL)
233                 return dm;
234
235         /* Get org mesh, only to test whether affected cdata layer has already been copied
236          * somewhere up in the modifiers stack.
237          */
238         ob_m = get_mesh(ob);
239         if (ob_m == NULL)
240                 return dm;
241
242         /* Create a copy of our dmesh, only if our affected cdata layer is the same as org mesh. */
243         if (dvert == CustomData_get_layer(&ob_m->vdata, CD_MDEFORMVERT)) {
244                 /* XXX Seems to create problems with weightpaint mode???
245                  *     I’m missing something here, I guess…
246                  */
247 //              DM_set_only_copy(dm, CD_MASK_MDEFORMVERT); /* Only copy defgroup layer. */
248                 ret = CDDM_copy(dm);
249                 dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
250                 if (dvert == NULL) {
251                         ret->release(ret);
252                         return dm;
253                 }
254                 rel_ret = 1;
255         }
256         else
257                 ret = dm;
258 #else
259         ret = CDDM_copy(dm);
260         rel_ret = 1;
261         dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
262         if (dvert == NULL) {
263                 if (rel_ret)
264                         ret->release(ret);
265                 return dm;
266         }
267 #endif
268
269         /* Get org weights, assuming 0.0 for vertices not in given vgroup. */
270         org_w = MEM_mallocN(sizeof(float) * numVerts, "WeightVGEdit Modifier, org_w");
271         new_w = MEM_mallocN(sizeof(float) * numVerts, "WeightVGEdit Modifier, org_w");
272         for (i = 0; i < numVerts; i++) {
273                 int j;
274                 org_w[i] = new_w[i] = wmd->default_weight;
275                 for (j = 0; j < dvert[i].totweight; j++) {
276                         if(dvert[i].dw[j].def_nr == defgrp_idx) {
277                                 org_w[i] = new_w[i] = dvert[i].dw[j].weight;
278                                 break;
279                         }
280                 }
281                 /* Do mapping. */
282                 if (do_map) {
283                         /* This mapping is a simple func: a*in + b.
284                          * with a = (out_min - out_max)/(in_min - in_max)
285                          * and  b = out_max - a*in_max
286                          * Note a and b are cached!
287                          */
288                         if (mapf == NULL) {
289                                 float denom = wmd->map_org_min - wmd->map_org_max;
290                                 mapf = MEM_mallocN(sizeof(float) * 2, "WeightVGEdit, mapf");
291                                 if (denom > 0.0 && denom < MOD_WVG_ZEROFLOOR)
292                                         denom = MOD_WVG_ZEROFLOOR;
293                                 else if (denom < 0.0 && denom > -MOD_WVG_ZEROFLOOR)
294                                         denom = -MOD_WVG_ZEROFLOOR;
295                                 mapf[0] = (wmd->map_new_min - wmd->map_new_max) / denom;
296                                 mapf[1] = wmd->map_new_max - (mapf[0] * wmd->map_org_max);
297                         }
298                         new_w[i] = (mapf[0] * new_w[i]) + mapf[1];
299                 }
300                 if (do_cmap)
301                         new_w[i] = curvemapping_evaluateF(wmd->cmap_curve, 0, new_w[i]);
302                 if (do_rev)
303                         new_w[i] = (-1.0 * new_w[i]) + 1.0;
304         }
305
306         /* Do masking. */
307         weightvg_do_mask(numVerts, NULL, org_w, new_w, ob, ret, wmd->mask_constant,
308                          wmd->mask_defgrp_name, wmd->mask_texture, wmd->mask_tex_use_channel,
309                          wmd->mask_tex_mapping, wmd->mask_tex_map_obj, wmd->mask_tex_uvlayer_name);
310
311         /* Do clamping. */
312         if (do_clamp) {
313                 for (i = 0; i < numVerts; i++)
314                         CLAMP(org_w[i], wmd->clamp_min_weight, wmd->clamp_max_weight);
315         }
316
317         /* Update/add/remove from vgroup. */
318         weightvg_update_vg(dvert, defgrp_idx, numVerts, NULL, org_w, do_add, wmd->add_threshold,
319                            do_rem, wmd->rem_threshold);
320
321         /* Freeing stuff. */
322         if (org_w)
323                 MEM_freeN(org_w);
324         if (new_w)
325                 MEM_freeN(new_w);
326         if (mapf)
327                 MEM_freeN(mapf);
328
329         /* Return the vgroup-modified mesh. */
330         return ret;
331 }
332
333 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
334                                     struct EditMesh *UNUSED(editData),
335                                     DerivedMesh *derivedData)
336 {
337         return applyModifier(md, ob, derivedData, 0, 1);
338 }
339
340
341 ModifierTypeInfo modifierType_WeightVGEdit = {
342         /* name */              "WeightVGEdit",
343         /* structName */        "WeightVGEditModifierData",
344         /* structSize */        sizeof(WeightVGEditModifierData),
345         /* type */              eModifierTypeType_Nonconstructive,
346         /* flags */             eModifierTypeFlag_AcceptsMesh
347                                |eModifierTypeFlag_SupportsMapping
348                                |eModifierTypeFlag_SupportsEditmode,
349
350         /* copyData */          copyData,
351         /* deformVerts */       NULL,
352         /* deformMatrices */    NULL,
353         /* deformVertsEM */     NULL,
354         /* deformMatricesEM */  NULL,
355         /* applyModifier */     applyModifier,
356         /* applyModifierEM */   applyModifierEM,
357         /* initData */          initData,
358         /* requiredDataMask */  requiredDataMask,
359         /* freeData */          freeData,
360         /* isDisabled */        isDisabled,
361         /* updateDepgraph */    updateDepgraph,
362         /* dependsOnTime */     dependsOnTime,
363         /* dependsOnNormals */  NULL,
364         /* foreachObjectLink */ foreachObjectLink,
365         /* foreachIDLink */     foreachIDLink,
366 };
367