svn merge ^/trunk/blender -r43564:43609
[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_mesh.h"
43 #include "BKE_modifier.h"
44 #include "BKE_texture.h"          /* Texture masking. */
45
46 #include "depsgraph_private.h"
47 #include "MEM_guardedalloc.h"
48 #include "MOD_util.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 copyData(ModifierData *md, ModifierData *target)
127 {
128         WeightVGMixModifierData *wmd  = (WeightVGMixModifierData*) md;
129         WeightVGMixModifierData *twmd = (WeightVGMixModifierData*) target;
130
131         BLI_strncpy(twmd->defgrp_name_a, wmd->defgrp_name_a, sizeof(twmd->defgrp_name_a));
132         BLI_strncpy(twmd->defgrp_name_b, wmd->defgrp_name_b, sizeof(twmd->defgrp_name_b));
133         twmd->default_weight_a       = wmd->default_weight_a;
134         twmd->default_weight_b       = wmd->default_weight_b;
135         twmd->mix_mode               = wmd->mix_mode;
136         twmd->mix_set                = wmd->mix_set;
137
138         twmd->mask_constant          = wmd->mask_constant;
139         BLI_strncpy(twmd->mask_defgrp_name, wmd->mask_defgrp_name, sizeof(twmd->mask_defgrp_name));
140         twmd->mask_texture           = wmd->mask_texture;
141         twmd->mask_tex_use_channel   = wmd->mask_tex_use_channel;
142         twmd->mask_tex_mapping       = wmd->mask_tex_mapping;
143         twmd->mask_tex_map_obj       = wmd->mask_tex_map_obj;
144         BLI_strncpy(twmd->mask_tex_uvlayer_name, wmd->mask_tex_uvlayer_name, sizeof(twmd->mask_tex_uvlayer_name));
145 }
146
147 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
148 {
149         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
150         CustomDataMask dataMask = 0;
151
152         /* We need vertex groups! */
153         dataMask |= CD_MASK_MDEFORMVERT;
154
155         /* Ask for UV coordinates if we need them. */
156         if(wmd->mask_tex_mapping == MOD_DISP_MAP_UV)
157                 dataMask |= CD_MASK_MTFACE;
158
159         return dataMask;
160 }
161
162 static int dependsOnTime(ModifierData *md)
163 {
164         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
165
166         if(wmd->mask_texture)
167                 return BKE_texture_dependsOnTime(wmd->mask_texture);
168         return 0;
169 }
170
171 static void foreachObjectLink(ModifierData *md, Object *ob,
172                               void (*walk)(void *userData, Object *ob, Object **obpoin),
173                               void *userData)
174 {
175         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
176         walk(userData, ob, &wmd->mask_tex_map_obj);
177 }
178
179 static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
180 {
181         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
182
183         walk(userData, ob, (ID **)&wmd->mask_texture);
184
185         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
186 }
187
188 static void foreachTexLink(ModifierData *md, Object *ob, TexWalkFunc walk, void *userData)
189 {
190         walk(userData, ob, md, "mask_texture");
191 }
192
193 static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *UNUSED(scene),
194                            Object *UNUSED(ob), DagNode *obNode)
195 {
196         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
197         DagNode *curNode;
198
199         if(wmd->mask_tex_map_obj && wmd->mask_tex_mapping == MOD_DISP_MAP_OBJECT) {
200                 curNode = dag_get_node(forest, wmd->mask_tex_map_obj);
201
202                 dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA,
203                                  "WeightVGMix Modifier");
204         }
205
206         if(wmd->mask_tex_mapping == MOD_DISP_MAP_GLOBAL)
207                 dag_add_relation(forest, obNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA,
208                                  "WeightVGMix Modifier");
209 }
210
211 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
212 {
213         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
214         /* If no vertex group, bypass. */
215         return (wmd->defgrp_name_a[0] == '\0');
216 }
217
218 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
219                                   int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
220 {
221         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
222         DerivedMesh *dm = derivedData;
223         MDeformVert *dvert = NULL;
224         MDeformWeight **dw1, **tdw1, **dw2, **tdw2;
225         int numVerts;
226         int defgrp_idx, defgrp_idx2 = -1;
227         float *org_w;
228         float *new_w;
229         int *tidx, *indices = NULL;
230         int numIdx = 0;
231         int i;
232
233         /* Get number of verts. */
234         numVerts = dm->getNumVerts(dm);
235
236         /* Check if we can just return the original mesh.
237          * Must have verts and therefore verts assigned to vgroups to do anything useful!
238          */
239         if ((numVerts == 0) || (ob->defbase.first == NULL))
240                 return dm;
241
242         /* Get vgroup idx from its name. */
243         defgrp_idx = defgroup_name_index(ob, wmd->defgrp_name_a);
244         if (defgrp_idx < 0)
245                 return dm;
246         /* Get seconf vgroup idx from its name, if given. */
247         if (wmd->defgrp_name_b[0] != (char)0) {
248                 defgrp_idx2 = defgroup_name_index(ob, wmd->defgrp_name_b);
249                 if (defgrp_idx2 < 0)
250                         return dm;
251         }
252
253         dvert = CustomData_duplicate_referenced_layer(&dm->vertData, CD_MDEFORMVERT, numVerts);
254         /* If no vertices were ever added to an object's vgroup, dvert might be NULL. */
255         if(!dvert)
256                 /* If not affecting all vertices, just return. */
257                 if(wmd->mix_set != MOD_WVG_SET_ALL)
258                         return dm;
259                 /* Else, add a valid data layer! */
260                 dvert = CustomData_add_layer_named(&dm->vertData, CD_MDEFORMVERT, CD_CALLOC,
261                                                    NULL, numVerts, wmd->defgrp_name_a);
262                 /* Ultimate security check. */
263                 if(!dvert)
264                         return dm;
265
266         /* Find out which vertices to work on. */
267         tidx = MEM_mallocN(sizeof(int) * numVerts, "WeightVGMix Modifier, tidx");
268         tdw1 = MEM_mallocN(sizeof(MDeformWeight*) * numVerts, "WeightVGMix Modifier, tdw1");
269         tdw2 = MEM_mallocN(sizeof(MDeformWeight*) * numVerts, "WeightVGMix Modifier, tdw2");
270         switch (wmd->mix_set) {
271         case MOD_WVG_SET_A:
272                 /* All vertices in first vgroup. */
273                 for (i = 0; i < numVerts; i++) {
274                         MDeformWeight *dw = defvert_find_index(&dvert[i], defgrp_idx);
275                         if(dw) {
276                                 tdw1[numIdx] = dw;
277                                 tdw2[numIdx] = defvert_find_index(&dvert[i], defgrp_idx2);
278                                 tidx[numIdx++] = i;
279                         }
280                 }
281                 break;
282         case MOD_WVG_SET_B:
283                 /* All vertices in second vgroup. */
284                 for (i = 0; i < numVerts; i++) {
285                         MDeformWeight *dw = defvert_find_index(&dvert[i], defgrp_idx2);
286                         if(dw) {
287                                 tdw1[numIdx] = defvert_find_index(&dvert[i], defgrp_idx);
288                                 tdw2[numIdx] = dw;
289                                 tidx[numIdx++] = i;
290                         }
291                 }
292                 break;
293         case MOD_WVG_SET_OR:
294                 /* All vertices in one vgroup or the other. */
295                 for (i = 0; i < numVerts; i++) {
296                         MDeformWeight *adw = defvert_find_index(&dvert[i], defgrp_idx);
297                         MDeformWeight *bdw = defvert_find_index(&dvert[i], defgrp_idx2);
298                         if(adw || bdw) {
299                                 tdw1[numIdx] = adw;
300                                 tdw2[numIdx] = bdw;
301                                 tidx[numIdx++] = i;
302                         }
303                 }
304                 break;
305         case MOD_WVG_SET_AND:
306                 /* All vertices in both vgroups. */
307                 for (i = 0; i < numVerts; i++) {
308                         MDeformWeight *adw = defvert_find_index(&dvert[i], defgrp_idx);
309                         MDeformWeight *bdw = defvert_find_index(&dvert[i], defgrp_idx2);
310                         if(adw && bdw) {
311                                 tdw1[numIdx] = adw;
312                                 tdw2[numIdx] = bdw;
313                                 tidx[numIdx++] = i;
314                         }
315                 }
316                 break;
317         case MOD_WVG_SET_ALL:
318         default:
319                 /* Use all vertices. */
320                 for (i = 0; i < numVerts; i++) {
321                         tdw1[i] = defvert_find_index(&dvert[i], defgrp_idx);
322                         tdw2[i] = defvert_find_index(&dvert[i], defgrp_idx2);
323                 }
324                 numIdx = -1;
325                 break;
326         }
327         if(numIdx == 0) {
328                 /* Use no vertices! Hence, return org data. */
329                 MEM_freeN(tdw1);
330                 MEM_freeN(tdw2);
331                 MEM_freeN(tidx);
332                 return dm;
333         }
334         if (numIdx != -1) {
335                 indices = MEM_mallocN(sizeof(int) * numIdx, "WeightVGMix Modifier, indices");
336                 memcpy(indices, tidx, sizeof(int) * numIdx);
337                 dw1 = MEM_mallocN(sizeof(MDeformWeight*) * numIdx, "WeightVGMix Modifier, dw1");
338                 memcpy(dw1, tdw1, sizeof(MDeformWeight*) * numIdx);
339                 MEM_freeN(tdw1);
340                 dw2 = MEM_mallocN(sizeof(MDeformWeight*) * numIdx, "WeightVGMix Modifier, dw2");
341                 memcpy(dw2, tdw2, sizeof(MDeformWeight*) * numIdx);
342                 MEM_freeN(tdw2);
343         }
344         else {
345                 /* Use all vertices. */
346                 numIdx = numVerts;
347                 /* Just copy MDeformWeight pointers arrays, they will be freed at the end. */
348                 dw1 = tdw1;
349                 dw2 = tdw2;
350         }
351         MEM_freeN(tidx);
352
353         org_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, org_w");
354         new_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, new_w");
355
356         /* Mix weights. */
357         for (i = 0; i < numIdx; i++) {
358                 float weight2 = 0.0;
359                 org_w[i] = dw1[i] ? dw1[i]->weight : wmd->default_weight_a;
360                 weight2  = dw2[i] ? dw2[i]->weight : wmd->default_weight_b;
361
362                 new_w[i] = mix_weight(org_w[i], weight2, wmd->mix_mode);
363         }
364
365         /* Do masking. */
366         weightvg_do_mask(numIdx, indices, org_w, new_w, ob, dm, wmd->mask_constant,
367                          wmd->mask_defgrp_name, wmd->mask_texture, wmd->mask_tex_use_channel,
368                          wmd->mask_tex_mapping, wmd->mask_tex_map_obj, wmd->mask_tex_uvlayer_name);
369
370         /* Update (add to) vgroup.
371          * XXX Depending on the MOD_WVG_SET_xxx option chosen, we might have to add vertices to vgroup.
372          */
373         weightvg_update_vg(dvert, defgrp_idx, dw1, numIdx, indices, org_w, TRUE, -FLT_MAX, FALSE, 0.0f);
374
375         /* Freeing stuff. */
376         MEM_freeN(org_w);
377         MEM_freeN(new_w);
378         MEM_freeN(dw1);
379         MEM_freeN(dw2);
380
381         if (indices)
382                 MEM_freeN(indices);
383
384         /* Return the vgroup-modified mesh. */
385         return dm;
386 }
387
388 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
389                                     struct BMEditMesh *UNUSED(editData),
390                                     DerivedMesh *derivedData)
391 {
392         return applyModifier(md, ob, derivedData, 0, 1);
393 }
394
395
396 ModifierTypeInfo modifierType_WeightVGMix = {
397         /* name */              "VertexWeightMix",
398         /* structName */        "WeightVGMixModifierData",
399         /* structSize */        sizeof(WeightVGMixModifierData),
400         /* type */              eModifierTypeType_NonGeometrical,
401         /* flags */             eModifierTypeFlag_AcceptsMesh
402 /*                             |eModifierTypeFlag_SupportsMapping*/
403                                |eModifierTypeFlag_SupportsEditmode,
404
405         /* copyData */          copyData,
406         /* deformVerts */       NULL,
407         /* deformMatrices */    NULL,
408         /* deformVertsEM */     NULL,
409         /* deformMatricesEM */  NULL,
410         /* applyModifier */     applyModifier,
411         /* applyModifierEM */   applyModifierEM,
412         /* initData */          initData,
413         /* requiredDataMask */  requiredDataMask,
414         /* freeData */          NULL,
415         /* isDisabled */        isDisabled,
416         /* updateDepgraph */    updateDepgraph,
417         /* dependsOnTime */     dependsOnTime,
418         /* dependsOnNormals */  NULL,
419         /* foreachObjectLink */ foreachObjectLink,
420         /* foreachIDLink */     foreachIDLink,
421         /* foreachTexLink */    foreachTexLink,
422 };