svn merge ^/trunk/blender -r42761:42776
[blender-staging.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
255         /* Find out which vertices to work on. */
256         tidx = MEM_mallocN(sizeof(int) * numVerts, "WeightVGMix Modifier, tidx");
257         tdw1 = MEM_mallocN(sizeof(MDeformWeight*) * numVerts, "WeightVGMix Modifier, tdw1");
258         tdw2 = MEM_mallocN(sizeof(MDeformWeight*) * numVerts, "WeightVGMix Modifier, tdw2");
259         switch (wmd->mix_set) {
260         case MOD_WVG_SET_A:
261                 /* All vertices in first vgroup. */
262                 for (i = 0; i < numVerts; i++) {
263                         MDeformWeight *dw = defvert_find_index(&dvert[i], defgrp_idx);
264                         if(dw) {
265                                 tdw1[numIdx] = dw;
266                                 tdw2[numIdx] = defvert_find_index(&dvert[i], defgrp_idx2);
267                                 tidx[numIdx++] = i;
268                         }
269                 }
270                 break;
271         case MOD_WVG_SET_B:
272                 /* All vertices in second vgroup. */
273                 for (i = 0; i < numVerts; i++) {
274                         MDeformWeight *dw = defvert_find_index(&dvert[i], defgrp_idx2);
275                         if(dw) {
276                                 tdw1[numIdx] = defvert_find_index(&dvert[i], defgrp_idx);
277                                 tdw2[numIdx] = dw;
278                                 tidx[numIdx++] = i;
279                         }
280                 }
281                 break;
282         case MOD_WVG_SET_OR:
283                 /* All vertices in one vgroup or the other. */
284                 for (i = 0; i < numVerts; i++) {
285                         MDeformWeight *adw = defvert_find_index(&dvert[i], defgrp_idx);
286                         MDeformWeight *bdw = defvert_find_index(&dvert[i], defgrp_idx2);
287                         if(adw || bdw) {
288                                 tdw1[numIdx] = adw;
289                                 tdw2[numIdx] = bdw;
290                                 tidx[numIdx++] = i;
291                         }
292                 }
293                 break;
294         case MOD_WVG_SET_AND:
295                 /* All vertices in both vgroups. */
296                 for (i = 0; i < numVerts; i++) {
297                         MDeformWeight *adw = defvert_find_index(&dvert[i], defgrp_idx);
298                         MDeformWeight *bdw = defvert_find_index(&dvert[i], defgrp_idx2);
299                         if(adw && bdw) {
300                                 tdw1[numIdx] = adw;
301                                 tdw2[numIdx] = bdw;
302                                 tidx[numIdx++] = i;
303                         }
304                 }
305                 break;
306         case MOD_WVG_SET_ALL:
307         default:
308                 /* Use all vertices. */
309                 for (i = 0; i < numVerts; i++) {
310                         tdw1[i] = defvert_find_index(&dvert[i], defgrp_idx);
311                         tdw2[i] = defvert_find_index(&dvert[i], defgrp_idx2);
312                 }
313                 numIdx = -1;
314                 break;
315         }
316         if(numIdx == 0) {
317                 /* Use no vertices! Hence, return org data. */
318                 MEM_freeN(tdw1);
319                 MEM_freeN(tdw2);
320                 MEM_freeN(tidx);
321                 return dm;
322         }
323         if (numIdx != -1) {
324                 indices = MEM_mallocN(sizeof(int) * numIdx, "WeightVGMix Modifier, indices");
325                 memcpy(indices, tidx, sizeof(int) * numIdx);
326                 dw1 = MEM_mallocN(sizeof(MDeformWeight*) * numIdx, "WeightVGMix Modifier, dw1");
327                 memcpy(dw1, tdw1, sizeof(MDeformWeight*) * numIdx);
328                 MEM_freeN(tdw1);
329                 dw2 = MEM_mallocN(sizeof(MDeformWeight*) * numIdx, "WeightVGMix Modifier, dw2");
330                 memcpy(dw2, tdw2, sizeof(MDeformWeight*) * numIdx);
331                 MEM_freeN(tdw2);
332         }
333         else {
334                 /* Use all vertices. */
335                 numIdx = numVerts;
336                 /* Just copy MDeformWeight pointers arrays, they will be freed at the end. */
337                 dw1 = tdw1;
338                 dw2 = tdw2;
339         }
340         MEM_freeN(tidx);
341
342         org_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, org_w");
343         new_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, new_w");
344
345         /* Mix weights. */
346         for (i = 0; i < numIdx; i++) {
347                 float weight2 = 0.0;
348                 org_w[i] = dw1[i] ? dw1[i]->weight : wmd->default_weight_a;
349                 weight2  = dw2[i] ? dw2[i]->weight : wmd->default_weight_b;
350
351                 new_w[i] = mix_weight(org_w[i], weight2, wmd->mix_mode);
352         }
353
354         /* Do masking. */
355         weightvg_do_mask(numIdx, indices, org_w, new_w, ob, dm, wmd->mask_constant,
356                          wmd->mask_defgrp_name, wmd->mask_texture, wmd->mask_tex_use_channel,
357                          wmd->mask_tex_mapping, wmd->mask_tex_map_obj, wmd->mask_tex_uvlayer_name);
358
359         /* Update (add to) vgroup.
360          * XXX Depending on the MOD_WVG_SET_xxx option chosen, we might have to add vertices to vgroup.
361          */
362         weightvg_update_vg(dvert, defgrp_idx, dw1, numIdx, indices, org_w, TRUE, -FLT_MAX, FALSE, 0.0f);
363
364         /* Freeing stuff. */
365         MEM_freeN(org_w);
366         MEM_freeN(new_w);
367         MEM_freeN(dw1);
368         MEM_freeN(dw2);
369
370         if (indices)
371                 MEM_freeN(indices);
372
373         /* Return the vgroup-modified mesh. */
374         return dm;
375 }
376
377 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
378                                     struct BMEditMesh *UNUSED(editData),
379                                     DerivedMesh *derivedData)
380 {
381         return applyModifier(md, ob, derivedData, 0, 1);
382 }
383
384
385 ModifierTypeInfo modifierType_WeightVGMix = {
386         /* name */              "VertexWeightMix",
387         /* structName */        "WeightVGMixModifierData",
388         /* structSize */        sizeof(WeightVGMixModifierData),
389         /* type */              eModifierTypeType_NonGeometrical,
390         /* flags */             eModifierTypeFlag_AcceptsMesh
391 /*                             |eModifierTypeFlag_SupportsMapping*/
392                                |eModifierTypeFlag_SupportsEditmode,
393
394         /* copyData */          copyData,
395         /* deformVerts */       NULL,
396         /* deformMatrices */    NULL,
397         /* deformVertsEM */     NULL,
398         /* deformMatricesEM */  NULL,
399         /* applyModifier */     applyModifier,
400         /* applyModifierEM */   applyModifierEM,
401         /* initData */          initData,
402         /* requiredDataMask */  requiredDataMask,
403         /* freeData */          NULL,
404         /* isDisabled */        isDisabled,
405         /* updateDepgraph */    updateDepgraph,
406         /* dependsOnTime */     dependsOnTime,
407         /* dependsOnNormals */  NULL,
408         /* foreachObjectLink */ foreachObjectLink,
409         /* foreachIDLink */     foreachIDLink,
410         /* foreachTexLink */    foreachTexLink,
411 };