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