Depsgraph: New dependency graph integration commit
[blender.git] / source / blender / modifiers / intern / MOD_weightvgmix.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) 2011 by Bastien Montagne.
19  * All rights reserved.
20  *
21  * Contributor(s): None yet.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  */
26
27 /** \file blender/modifiers/intern/MOD_weightvgmix.c
28  *  \ingroup modifiers
29  */
30
31 #include "BLI_utildefines.h"
32 #include "BLI_math.h"
33 #include "BLI_listbase.h"
34
35 #include "DNA_meshdata_types.h"
36 #include "DNA_modifier_types.h"
37 #include "DNA_object_types.h"
38
39 #include "BKE_cdderivedmesh.h"
40 #include "BKE_deform.h"
41 #include "BKE_library.h"
42 #include "BKE_modifier.h"
43 #include "BKE_texture.h"          /* Texture masking. */
44
45 #include "depsgraph_private.h"
46 #include "DEG_depsgraph_build.h"
47
48 #include "MEM_guardedalloc.h"
49 #include "MOD_weightvg_util.h"
50
51
52 /**
53  * This mixes the old weight with the new weight factor.
54  */
55 static float mix_weight(float weight, float weight2, char mix_mode)
56 {
57 #if 0
58         /*
59          * XXX Don't know why, but the switch version takes many CPU time,
60          *     and produces lag in realtime playback...
61          */
62         switch (mix_mode)
63         {
64                 case MOD_WVG_MIX_ADD:
65                         return (weight + weight2);
66                 case MOD_WVG_MIX_SUB:
67                         return (weight - weight2);
68                 case MOD_WVG_MIX_MUL:
69                         return (weight * weight2);
70                 case MOD_WVG_MIX_DIV:
71                         /* Avoid dividing by zero (or really small values). */
72                         if (0.0 <= weight2 < MOD_WVG_ZEROFLOOR)
73                                 weight2 = MOD_WVG_ZEROFLOOR;
74                         else if (-MOD_WVG_ZEROFLOOR < weight2)
75                                 weight2 = -MOD_WVG_ZEROFLOOR;
76                         return (weight / weight2);
77                 case MOD_WVG_MIX_DIF:
78                         return (weight < weight2 ? weight2 - weight : weight - weight2);
79                 case MOD_WVG_MIX_AVG:
80                         return (weight + weight2) / 2.0;
81                 case MOD_WVG_MIX_SET:
82                 default:
83                         return weight2;
84         }
85 #endif
86         if (mix_mode == MOD_WVG_MIX_SET)
87                 return weight2;
88         else if (mix_mode == MOD_WVG_MIX_ADD)
89                 return (weight + weight2);
90         else if (mix_mode == MOD_WVG_MIX_SUB)
91                 return (weight - weight2);
92         else if (mix_mode == MOD_WVG_MIX_MUL)
93                 return (weight * weight2);
94         else if (mix_mode == MOD_WVG_MIX_DIV) {
95                 /* Avoid dividing by zero (or really small values). */
96                 if (weight2 < 0.0f && weight2 > -MOD_WVG_ZEROFLOOR)
97                         weight2 = -MOD_WVG_ZEROFLOOR;
98                 else if (weight2 >= 0.0f && weight2 < MOD_WVG_ZEROFLOOR)
99                         weight2 = MOD_WVG_ZEROFLOOR;
100                 return (weight / weight2);
101         }
102         else if (mix_mode == MOD_WVG_MIX_DIF)
103                 return (weight < weight2 ? weight2 - weight : weight - weight2);
104         else if (mix_mode == MOD_WVG_MIX_AVG)
105                 return (weight + weight2) * 0.5f;
106         else return weight2;
107 }
108
109 /**************************************
110  * Modifiers functions.               *
111  **************************************/
112 static void initData(ModifierData *md)
113 {
114         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
115
116         wmd->default_weight_a       = 0.0f;
117         wmd->default_weight_b       = 0.0f;
118         wmd->mix_mode               = MOD_WVG_MIX_SET;
119         wmd->mix_set                = MOD_WVG_SET_AND;
120
121         wmd->mask_constant          = 1.0f;
122         wmd->mask_tex_use_channel   = MOD_WVG_MASK_TEX_USE_INT; /* Use intensity by default. */
123         wmd->mask_tex_mapping       = MOD_DISP_MAP_LOCAL;
124 }
125
126 static void freeData(ModifierData *md)
127 {
128         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
129         if (wmd->mask_texture) {
130                 id_us_min(&wmd->mask_texture->id);
131         }
132 }
133
134 static void copyData(ModifierData *md, ModifierData *target)
135 {
136 #if 0
137         WeightVGMixModifierData *wmd  = (WeightVGMixModifierData *) md;
138 #endif
139         WeightVGMixModifierData *twmd = (WeightVGMixModifierData *) target;
140
141         modifier_copyData_generic(md, target);
142
143         if (twmd->mask_texture) {
144                 id_us_plus(&twmd->mask_texture->id);
145         }
146 }
147
148 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
149 {
150         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
151         CustomDataMask dataMask = 0;
152
153         /* We need vertex groups! */
154         dataMask |= CD_MASK_MDEFORMVERT;
155
156         /* Ask for UV coordinates if we need them. */
157         if (wmd->mask_tex_mapping == MOD_DISP_MAP_UV)
158                 dataMask |= CD_MASK_MTFACE;
159
160         /* No need to ask for CD_PREVIEW_MLOOPCOL... */
161
162         return dataMask;
163 }
164
165 static bool dependsOnTime(ModifierData *md)
166 {
167         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
168
169         if (wmd->mask_texture)
170                 return BKE_texture_dependsOnTime(wmd->mask_texture);
171         return false;
172 }
173
174 static void foreachObjectLink(ModifierData *md, Object *ob,
175                               void (*walk)(void *userData, Object *ob, Object **obpoin),
176                               void *userData)
177 {
178         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
179         walk(userData, ob, &wmd->mask_tex_map_obj);
180 }
181
182 static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
183 {
184         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
185
186         walk(userData, ob, (ID **)&wmd->mask_texture);
187
188         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
189 }
190
191 static void foreachTexLink(ModifierData *md, Object *ob, TexWalkFunc walk, void *userData)
192 {
193         walk(userData, ob, md, "mask_texture");
194 }
195
196 static void updateDepgraph(ModifierData *md, DagForest *forest,
197                            struct Main *UNUSED(bmain),
198                            struct Scene *UNUSED(scene),
199                            Object *UNUSED(ob), DagNode *obNode)
200 {
201         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
202         DagNode *curNode;
203
204         if (wmd->mask_tex_map_obj && wmd->mask_tex_mapping == MOD_DISP_MAP_OBJECT) {
205                 curNode = dag_get_node(forest, wmd->mask_tex_map_obj);
206
207                 dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA | DAG_RL_OB_DATA,
208                                  "WeightVGMix Modifier");
209         }
210
211         if (wmd->mask_tex_mapping == MOD_DISP_MAP_GLOBAL)
212                 dag_add_relation(forest, obNode, obNode, DAG_RL_DATA_DATA | DAG_RL_OB_DATA,
213                                  "WeightVGMix Modifier");
214 }
215
216 static void updateDepsgraph(ModifierData *md,
217                             struct Main *UNUSED(bmain),
218                             struct Scene *UNUSED(scene),
219                             Object *ob,
220                             struct DepsNodeHandle *node)
221 {
222         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
223         if (wmd->mask_tex_map_obj != NULL && wmd->mask_tex_mapping == MOD_DISP_MAP_OBJECT) {
224                 DEG_add_object_relation(node, wmd->mask_tex_map_obj, DEG_OB_COMP_TRANSFORM, "WeightVGMix Modifier");
225         }
226         if (wmd->mask_tex_mapping == MOD_DISP_MAP_GLOBAL) {
227                 DEG_add_object_relation(node, ob, DEG_OB_COMP_TRANSFORM, "WeightVGMix Modifier");
228         }
229 }
230
231 static bool isDisabled(ModifierData *md, int UNUSED(useRenderParams))
232 {
233         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
234         /* If no vertex group, bypass. */
235         return (wmd->defgrp_name_a[0] == '\0');
236 }
237
238 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
239                                   ModifierApplyFlag UNUSED(flag))
240 {
241         WeightVGMixModifierData *wmd = (WeightVGMixModifierData *) md;
242         DerivedMesh *dm = derivedData;
243         MDeformVert *dvert = NULL;
244         MDeformWeight **dw1, **tdw1, **dw2, **tdw2;
245         int numVerts;
246         int defgrp_index, defgrp_index_other = -1;
247         float *org_w;
248         float *new_w;
249         int *tidx, *indices = NULL;
250         int numIdx = 0;
251         int i;
252         /* Flags. */
253 #if 0
254         const bool do_prev = (wmd->modifier.mode & eModifierMode_DoWeightPreview) != 0;
255 #endif
256
257         /* Get number of verts. */
258         numVerts = dm->getNumVerts(dm);
259
260         /* Check if we can just return the original mesh.
261          * Must have verts and therefore verts assigned to vgroups to do anything useful!
262          */
263         if ((numVerts == 0) || BLI_listbase_is_empty(&ob->defbase))
264                 return dm;
265
266         /* Get vgroup idx from its name. */
267         defgrp_index = defgroup_name_index(ob, wmd->defgrp_name_a);
268         if (defgrp_index == -1)
269                 return dm;
270         /* Get seconf vgroup idx from its name, if given. */
271         if (wmd->defgrp_name_b[0] != (char)0) {
272                 defgrp_index_other = defgroup_name_index(ob, wmd->defgrp_name_b);
273                 if (defgrp_index_other == -1)
274                         return dm;
275         }
276
277         dvert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MDEFORMVERT, numVerts);
278         /* If no vertices were ever added to an object's vgroup, dvert might be NULL. */
279         if (!dvert) {
280                 /* If not affecting all vertices, just return. */
281                 if (wmd->mix_set != MOD_WVG_SET_ALL)
282                         return dm;
283                 /* Else, add a valid data layer! */
284                 dvert = CustomData_add_layer_named(&dm->vertData, CD_MDEFORMVERT, CD_CALLOC,
285                                                    NULL, numVerts, wmd->defgrp_name_a);
286                 /* Ultimate security check. */
287                 if (!dvert)
288                         return dm;
289         }
290         /* Find out which vertices to work on. */
291         tidx = MEM_mallocN(sizeof(int) * numVerts, "WeightVGMix Modifier, tidx");
292         tdw1 = MEM_mallocN(sizeof(MDeformWeight *) * numVerts, "WeightVGMix Modifier, tdw1");
293         tdw2 = MEM_mallocN(sizeof(MDeformWeight *) * numVerts, "WeightVGMix Modifier, tdw2");
294         switch (wmd->mix_set) {
295                 case MOD_WVG_SET_A:
296                         /* All vertices in first vgroup. */
297                         for (i = 0; i < numVerts; i++) {
298                                 MDeformWeight *dw = defvert_find_index(&dvert[i], defgrp_index);
299                                 if (dw) {
300                                         tdw1[numIdx] = dw;
301                                         tdw2[numIdx] = defvert_find_index(&dvert[i], defgrp_index_other);
302                                         tidx[numIdx++] = i;
303                                 }
304                         }
305                         break;
306                 case MOD_WVG_SET_B:
307                         /* All vertices in second vgroup. */
308                         for (i = 0; i < numVerts; i++) {
309                                 MDeformWeight *dw = defvert_find_index(&dvert[i], defgrp_index_other);
310                                 if (dw) {
311                                         tdw1[numIdx] = defvert_find_index(&dvert[i], defgrp_index);
312                                         tdw2[numIdx] = dw;
313                                         tidx[numIdx++] = i;
314                                 }
315                         }
316                         break;
317                 case MOD_WVG_SET_OR:
318                         /* All vertices in one vgroup or the other. */
319                         for (i = 0; i < numVerts; i++) {
320                                 MDeformWeight *adw = defvert_find_index(&dvert[i], defgrp_index);
321                                 MDeformWeight *bdw = defvert_find_index(&dvert[i], defgrp_index_other);
322                                 if (adw || bdw) {
323                                         tdw1[numIdx] = adw;
324                                         tdw2[numIdx] = bdw;
325                                         tidx[numIdx++] = i;
326                                 }
327                         }
328                         break;
329                 case MOD_WVG_SET_AND:
330                         /* All vertices in both vgroups. */
331                         for (i = 0; i < numVerts; i++) {
332                                 MDeformWeight *adw = defvert_find_index(&dvert[i], defgrp_index);
333                                 MDeformWeight *bdw = defvert_find_index(&dvert[i], defgrp_index_other);
334                                 if (adw && bdw) {
335                                         tdw1[numIdx] = adw;
336                                         tdw2[numIdx] = bdw;
337                                         tidx[numIdx++] = i;
338                                 }
339                         }
340                         break;
341                 case MOD_WVG_SET_ALL:
342                 default:
343                         /* Use all vertices. */
344                         for (i = 0; i < numVerts; i++) {
345                                 tdw1[i] = defvert_find_index(&dvert[i], defgrp_index);
346                                 tdw2[i] = defvert_find_index(&dvert[i], defgrp_index_other);
347                         }
348                         numIdx = -1;
349                         break;
350         }
351         if (numIdx == 0) {
352                 /* Use no vertices! Hence, return org data. */
353                 MEM_freeN(tdw1);
354                 MEM_freeN(tdw2);
355                 MEM_freeN(tidx);
356                 return dm;
357         }
358         if (numIdx != -1) {
359                 indices = MEM_mallocN(sizeof(int) * numIdx, "WeightVGMix Modifier, indices");
360                 memcpy(indices, tidx, sizeof(int) * numIdx);
361                 dw1 = MEM_mallocN(sizeof(MDeformWeight *) * numIdx, "WeightVGMix Modifier, dw1");
362                 memcpy(dw1, tdw1, sizeof(MDeformWeight *) * numIdx);
363                 MEM_freeN(tdw1);
364                 dw2 = MEM_mallocN(sizeof(MDeformWeight *) * numIdx, "WeightVGMix Modifier, dw2");
365                 memcpy(dw2, tdw2, sizeof(MDeformWeight *) * numIdx);
366                 MEM_freeN(tdw2);
367         }
368         else {
369                 /* Use all vertices. */
370                 numIdx = numVerts;
371                 /* Just copy MDeformWeight pointers arrays, they will be freed at the end. */
372                 dw1 = tdw1;
373                 dw2 = tdw2;
374         }
375         MEM_freeN(tidx);
376
377         org_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, org_w");
378         new_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, new_w");
379
380         /* Mix weights. */
381         for (i = 0; i < numIdx; i++) {
382                 float weight2;
383                 org_w[i] = dw1[i] ? dw1[i]->weight : wmd->default_weight_a;
384                 weight2  = dw2[i] ? dw2[i]->weight : wmd->default_weight_b;
385
386                 new_w[i] = mix_weight(org_w[i], weight2, wmd->mix_mode);
387         }
388
389         /* Do masking. */
390         weightvg_do_mask(numIdx, indices, org_w, new_w, ob, dm, wmd->mask_constant,
391                          wmd->mask_defgrp_name, wmd->modifier.scene, wmd->mask_texture,
392                          wmd->mask_tex_use_channel, wmd->mask_tex_mapping,
393                          wmd->mask_tex_map_obj, wmd->mask_tex_uvlayer_name);
394
395         /* Update (add to) vgroup.
396          * XXX Depending on the MOD_WVG_SET_xxx option chosen, we might have to add vertices to vgroup.
397          */
398         weightvg_update_vg(dvert, defgrp_index, dw1, numIdx, indices, org_w, true, -FLT_MAX, false, 0.0f);
399
400         /* If weight preview enabled... */
401 #if 0 /* XXX Currently done in mod stack :/ */
402         if (do_prev)
403                 DM_update_weight_mcol(ob, dm, 0, org_w, numIdx, indices);
404 #endif
405
406         /* Freeing stuff. */
407         MEM_freeN(org_w);
408         MEM_freeN(new_w);
409         MEM_freeN(dw1);
410         MEM_freeN(dw2);
411
412         if (indices)
413                 MEM_freeN(indices);
414
415         /* Return the vgroup-modified mesh. */
416         return dm;
417 }
418
419
420 ModifierTypeInfo modifierType_WeightVGMix = {
421         /* name */              "VertexWeightMix",
422         /* structName */        "WeightVGMixModifierData",
423         /* structSize */        sizeof(WeightVGMixModifierData),
424         /* type */              eModifierTypeType_NonGeometrical,
425         /* flags */             eModifierTypeFlag_AcceptsMesh |
426                                 eModifierTypeFlag_SupportsMapping |
427                                 eModifierTypeFlag_SupportsEditmode |
428                                 eModifierTypeFlag_UsesPreview,
429
430         /* copyData */          copyData,
431         /* deformVerts */       NULL,
432         /* deformMatrices */    NULL,
433         /* deformVertsEM */     NULL,
434         /* deformMatricesEM */  NULL,
435         /* applyModifier */     applyModifier,
436         /* applyModifierEM */   NULL,
437         /* initData */          initData,
438         /* requiredDataMask */  requiredDataMask,
439         /* freeData */          freeData,
440         /* isDisabled */        isDisabled,
441         /* updateDepgraph */    updateDepgraph,
442         /* updateDepsgraph */   updateDepsgraph,
443         /* dependsOnTime */     dependsOnTime,
444         /* dependsOnNormals */  NULL,
445         /* foreachObjectLink */ foreachObjectLink,
446         /* foreachIDLink */     foreachIDLink,
447         /* foreachTexLink */    foreachTexLink,
448 };