Merging r39652 through r39842 from trunk into vgroup_modifiers.
[blender.git] / source / blender / modifiers / intern / MOD_weightvgmix.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_mesh_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_modifier_types.h"
43 #include "DNA_object_types.h"
44
45 #include "BKE_cdderivedmesh.h"
46 #include "BKE_deform.h"
47 #include "BKE_mesh.h"
48 #include "BKE_modifier.h"
49 #include "BKE_texture.h"          /* Texture masking. */
50
51 #include "depsgraph_private.h"
52 #include "MEM_guardedalloc.h"
53 #include "MOD_util.h"
54 #include "MOD_weightvg_util.h"
55
56
57 /**
58  * This mixes the old weight with the new weight factor.
59  */
60 static float mix_weight(float weight, float weight2, char mix_mode)
61 {
62 #if 0
63         /*
64          * XXX Don’t know why, but the switch version takes many CPU time,
65          *     and produces lag in realtime playback…
66          */
67         switch (mix_mode)
68         {
69         case MOD_WVG_MIX_ADD:
70                 return (weight + weight2);
71         case MOD_WVG_MIX_SUB:
72                 return (weight - weight2);
73         case MOD_WVG_MIX_MUL:
74                 return (weight * weight2);
75         case MOD_WVG_MIX_DIV:
76                 /* Avoid dividing by zero (or really small values). */
77                 if (0.0 <= weight2 < MOD_WVG_ZEROFLOOR)
78                         weight2 = MOD_WVG_ZEROFLOOR;
79                 else if (-MOD_WVG_ZEROFLOOR < weight2)
80                         weight2 = -MOD_WVG_ZEROFLOOR;
81                 return (weight / weight2);
82         case MOD_WVG_MIX_DIF:
83                 return (weight < weight2 ? weight2 - weight : weight - weight2);
84         case MOD_WVG_MIX_AVG:
85                 return (weight + weight2) / 2.0;
86         case MOD_WVG_MIX_SET:
87         default:
88                 return weight2;
89         }
90 #endif
91         if (mix_mode == MOD_WVG_MIX_SET)
92                 return weight2;
93         else if (mix_mode == MOD_WVG_MIX_ADD)
94                 return (weight + weight2);
95         else if (mix_mode == MOD_WVG_MIX_SUB)
96                 return (weight - weight2);
97         else if (mix_mode == MOD_WVG_MIX_MUL)
98                 return (weight * weight2);
99         else if (mix_mode == MOD_WVG_MIX_DIV) {
100                 /* Avoid dividing by zero (or really small values). */
101                 if (weight2 < 0.0 && weight2 > -MOD_WVG_ZEROFLOOR)
102                         weight2 = -MOD_WVG_ZEROFLOOR;
103                 else if (weight2 >= 0.0 && weight2 < MOD_WVG_ZEROFLOOR)
104                         weight2 = MOD_WVG_ZEROFLOOR;
105                 return (weight / weight2);
106         }
107         else if (mix_mode == MOD_WVG_MIX_DIF)
108                 return (weight < weight2 ? weight2 - weight : weight - weight2);
109         else if (mix_mode == MOD_WVG_MIX_AVG)
110                 return (weight + weight2) / 2.0;
111         else return weight2;
112 }
113
114 /**************************************
115  * Modifiers functions.               *
116  **************************************/
117 static void initData(ModifierData *md)
118 {
119         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
120
121         wmd->default_weight         = 0.0;
122         wmd->default_weight2        = 0.0;
123         wmd->mix_mode               = MOD_WVG_MIX_SET;
124         wmd->mix_set                = MOD_WVG_SET_INTER;
125
126         wmd->mask_constant          = 1.0f;
127         wmd->mask_tex_use_channel   = MOD_WVG_MASK_TEX_USE_INT; /* Use intensity by default. */
128         wmd->mask_tex_mapping       = MOD_DISP_MAP_LOCAL;
129 }
130
131 static void copyData(ModifierData *md, ModifierData *target)
132 {
133         WeightVGMixModifierData *wmd  = (WeightVGMixModifierData*) md;
134         WeightVGMixModifierData *twmd = (WeightVGMixModifierData*) target;
135
136         BLI_strncpy(twmd->defgrp_name, wmd->defgrp_name, sizeof(twmd->defgrp_name));
137         BLI_strncpy(twmd->defgrp_name2, wmd->defgrp_name2, sizeof(twmd->defgrp_name2));
138         twmd->default_weight         = wmd->default_weight;
139         twmd->default_weight2        = wmd->default_weight2;
140         twmd->mix_mode               = wmd->mix_mode;
141         twmd->mix_set                = wmd->mix_set;
142
143         twmd->mask_constant          = wmd->mask_constant;
144         BLI_strncpy(twmd->mask_defgrp_name, wmd->mask_defgrp_name, sizeof(twmd->mask_defgrp_name));
145         twmd->mask_texture           = wmd->mask_texture;
146         twmd->mask_tex_use_channel   = wmd->mask_tex_use_channel;
147         twmd->mask_tex_mapping       = wmd->mask_tex_mapping;
148         twmd->mask_tex_map_obj       = wmd->mask_tex_map_obj;
149         BLI_strncpy(twmd->mask_tex_uvlayer_name, wmd->mask_tex_uvlayer_name,
150                     sizeof(twmd->mask_tex_uvlayer_name));
151 }
152
153 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
154 {
155         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
156         CustomDataMask dataMask = 0;
157
158         /* We need vertex groups! */
159         dataMask |= CD_MASK_MDEFORMVERT;
160
161         /* Ask for UV coordinates if we need them. */
162         if(wmd->mask_tex_mapping == MOD_DISP_MAP_UV)
163                 dataMask |= CD_MASK_MTFACE;
164
165         return dataMask;
166 }
167
168 static int dependsOnTime(ModifierData *md)
169 {
170         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
171
172         if(wmd->mask_texture)
173                 return BKE_texture_dependsOnTime(wmd->mask_texture);
174         return 0;
175 }
176
177 static void foreachObjectLink(ModifierData *md, Object *ob,
178                               void (*walk)(void *userData, Object *ob, Object **obpoin),
179                               void *userData)
180 {
181         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
182         walk(userData, ob, &wmd->mask_tex_map_obj);
183 }
184
185 static void foreachIDLink(ModifierData *md, Object *ob, IDWalkFunc walk, void *userData)
186 {
187         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
188
189         walk(userData, ob, (ID **)&wmd->mask_texture);
190
191         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
192 }
193
194 static void foreachTexLink(ModifierData *md, Object *ob, TexWalkFunc walk, void *userData)
195 {
196         walk(userData, ob, md, "mask_texture");
197 }
198
199 static void updateDepgraph(ModifierData *md, DagForest *forest, struct Scene *UNUSED(scene),
200                            Object *UNUSED(ob), DagNode *obNode)
201 {
202         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
203         DagNode *curNode;
204
205         if(wmd->mask_tex_map_obj && wmd->mask_tex_mapping == MOD_DISP_MAP_OBJECT) {
206                 curNode = dag_get_node(forest, wmd->mask_tex_map_obj);
207
208                 dag_add_relation(forest, curNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA,
209                                  "WeightVGMix Modifier");
210         }
211
212         if(wmd->mask_tex_mapping == MOD_DISP_MAP_GLOBAL)
213                 dag_add_relation(forest, obNode, obNode, DAG_RL_DATA_DATA|DAG_RL_OB_DATA,
214                                  "WeightVGMix Modifier");
215 }
216
217 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
218 {
219         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
220         /* If no vertex group, bypass. */
221         return (wmd->defgrp_name == NULL);
222 }
223
224 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
225                                   int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
226 {
227         WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md;
228         DerivedMesh *dm = derivedData, *ret = NULL;
229 #if 0
230         Mesh *ob_m = NULL;
231 #endif
232         MDeformVert *dvert = NULL;
233         int numVerts;
234         int defgrp_idx, defgrp_idx2 = -1;
235         float *org_w = NULL;
236         float *new_w = NULL;
237         int *tidx, *indices = NULL;
238         int numIdx = 0;
239         int i, j;
240         char rel_ret = 0; /* Boolean, whether we have to release ret dm or not, when not using it! */
241
242         /* Get number of verts. */
243         numVerts = dm->getNumVerts(dm);
244
245         /* Check if we can just return the original mesh.
246          * Must have verts and therefore verts assigned to vgroups to do anything useful!
247          */
248         if ((numVerts == 0) || (ob->defbase.first == NULL))
249                 return dm;
250
251         /* Get vgroup idx from its name. */
252         defgrp_idx = defgroup_name_index(ob, wmd->defgrp_name);
253         if (defgrp_idx < 0)
254                 return dm;
255         /* Get seconf vgroup idx from its name, if given. */
256         if (wmd->defgrp_name2[0] != (char)0) {
257                 defgrp_idx2 = defgroup_name_index(ob, wmd->defgrp_name2);
258                 if (defgrp_idx2 < 0)
259                         return dm;
260         }
261
262         /* XXX All this to avoid copying dm when not needed… However, it nearly doubles compute
263          *     time! See scene 5 of the WeighVG test file…
264          */
265 #if 0
266         /* Get actual dverts (ie vertex group data). */
267         dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
268         /* If no dverts, return unmodified data… */
269         if (dvert == NULL)
270                 return dm;
271
272         /* Get org mesh, only to test whether affected cdata layer has already been copied
273          * somewhere up in the modifiers stack.
274          */
275         ob_m = get_mesh(ob);
276         if (ob_m == NULL)
277                 return dm;
278
279         /* Create a copy of our dmesh, only if our affected cdata layer is the same as org mesh. */
280         if (dvert == CustomData_get_layer(&ob_m->vdata, CD_MDEFORMVERT)) {
281                 /* XXX Seems to create problems with weightpaint mode???
282                  *     I’m missing something here, I guess…
283                  */
284 //              DM_set_only_copy(dm, CD_MASK_MDEFORMVERT); /* Only copy defgroup layer. */
285                 ret = CDDM_copy(dm);
286                 dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
287                 if (dvert == NULL) {
288                         ret->release(ret);
289                         return dm;
290                 }
291                 rel_ret = 1;
292         }
293         else
294                 ret = dm;
295 #else
296         ret = CDDM_copy(dm);
297         rel_ret = 1;
298         dvert = ret->getVertDataArray(ret, CD_MDEFORMVERT);
299         if (dvert == NULL) {
300                 if (rel_ret)
301                         ret->release(ret);
302                 return dm;
303         }
304 #endif
305
306         /* Find out which vertices to work on. */
307         tidx = MEM_mallocN(sizeof(int) * numVerts, "WeightVGMix Modifier, tidx");
308         switch (wmd->mix_set) {
309         case MOD_WVG_SET_ORG:
310                 /* All vertices in first vgroup. */
311                 for (i = 0; i < numVerts; i++) {
312                         for (j = 0; j < dvert[i].totweight; j++) {
313                                 if(dvert[i].dw[j].def_nr == defgrp_idx) {
314                                         tidx[numIdx++] = i;
315                                         break;
316                                 }
317                         }
318                 }
319                 break;
320         case MOD_WVG_SET_NEW:
321                 /* All vertices in second vgroup. */
322                 for (i = 0; i < numVerts; i++) {
323                         for (j = 0; j < dvert[i].totweight; j++) {
324                                 if(dvert[i].dw[j].def_nr == defgrp_idx2) {
325                                         tidx[numIdx++] = i;
326                                         break;
327                                 }
328                         }
329                 }
330                 break;
331         case MOD_WVG_SET_UNION:
332                 /* All vertices in one vgroup or the other. */
333                 for (i = 0; i < numVerts; i++) {
334                         for (j = 0; j < dvert[i].totweight; j++) {
335                                 if(dvert[i].dw[j].def_nr == defgrp_idx || dvert[i].dw[j].def_nr == defgrp_idx2) {
336                                         tidx[numIdx++] = i;
337                                         break;
338                                 }
339                         }
340                 }
341                 break;
342         case MOD_WVG_SET_INTER:
343                 /* All vertices in both vgroups. */
344                 for (i = 0; i < numVerts; i++) {
345                         char idx1 = 0;
346                         char idx2 = 0;
347                         for (j = 0; j < dvert[i].totweight; j++) {
348                                 if(dvert[i].dw[j].def_nr == defgrp_idx) {
349                                         if (idx2) {
350                                                 tidx[numIdx++] = i;
351                                                 break;
352                                         }
353                                         else
354                                                 idx1 = 1;
355                                 }
356                                 else if(dvert[i].dw[j].def_nr == defgrp_idx2) {
357                                         if (idx1) {
358                                                 tidx[numIdx++] = i;
359                                                 break;
360                                         }
361                                         else
362                                                 idx2 = 1;
363                                 }
364                         }
365                 }
366                 break;
367         case MOD_WVG_SET_ALL:
368         default:
369                 /* Use all vertices, no need to do anything here. */
370                 break;
371         }
372         if (numIdx) {
373                 indices = MEM_mallocN(sizeof(int) * numIdx, "WeightVGMix Modifier, indices");
374                 memcpy(indices, tidx, sizeof(int) * numIdx);
375         }
376         else
377                 numIdx = numVerts;
378         MEM_freeN(tidx);
379
380         org_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, org_w");
381         new_w = MEM_mallocN(sizeof(float) * numIdx, "WeightVGMix Modifier, new_w");
382
383         /* Mix weights. */
384         for (i = 0; i < numIdx; i++) {
385                 float weight2 = 0.0;
386                 char w1 = 0;
387                 char w2 = 0;
388                 int idx = indices ? indices[i] : i;
389                 for (j = 0; j < dvert[idx].totweight; j++) {
390                         if(dvert[idx].dw[j].def_nr == defgrp_idx) {
391                                 org_w[i] = dvert[idx].dw[j].weight;
392                                 w1 = 1;
393                                 if (w2)
394                                         break;
395                         }
396                         else if(dvert[idx].dw[j].def_nr == defgrp_idx2) {
397                                 weight2 = dvert[idx].dw[j].weight;
398                                 w2 = 1;
399                                 if (w1)
400                                         break;
401                         }
402                 }
403                 if (w1 == 0)
404                         org_w[i] = wmd->default_weight;
405                 if (w2 == 0)
406                         weight2 = wmd->default_weight2;
407                 new_w[i] = mix_weight(org_w[i], weight2, wmd->mix_mode);
408         }
409
410         /* Do masking. */
411         weightvg_do_mask(numIdx, indices, org_w, new_w, ob, ret, wmd->mask_constant,
412                          wmd->mask_defgrp_name, wmd->mask_texture, wmd->mask_tex_use_channel,
413                          wmd->mask_tex_mapping, wmd->mask_tex_map_obj, wmd->mask_tex_uvlayer_name);
414
415         /* Update (add to) vgroup.
416          * XXX Depending on the MOD_WVG_SET_xxx option chosen, we might have to add vertices to vgroup.
417          */
418         weightvg_update_vg(dvert, defgrp_idx, numIdx, indices, org_w, 1, -FLT_MAX, 0, 0.0f);
419
420         /* Freeing stuff. */
421         if (org_w)
422                 MEM_freeN(org_w);
423         if (new_w)
424                 MEM_freeN(new_w);
425         if (indices)
426                 MEM_freeN(indices);
427
428         /* Return the vgroup-modified mesh. */
429         return ret;
430 }
431
432 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
433                                     struct EditMesh *UNUSED(editData),
434                                     DerivedMesh *derivedData)
435 {
436         return applyModifier(md, ob, derivedData, 0, 1);
437 }
438
439
440 ModifierTypeInfo modifierType_WeightVGMix = {
441         /* name */              "WeightVGMix",
442         /* structName */        "WeightVGMixModifierData",
443         /* structSize */        sizeof(WeightVGMixModifierData),
444         /* type */              eModifierTypeType_Nonconstructive,
445         /* flags */             eModifierTypeFlag_AcceptsMesh
446 /*                             |eModifierTypeFlag_SupportsMapping*/
447                                |eModifierTypeFlag_SupportsEditmode,
448
449         /* copyData */          copyData,
450         /* deformVerts */       NULL,
451         /* deformMatrices */    NULL,
452         /* deformVertsEM */     NULL,
453         /* deformMatricesEM */  NULL,
454         /* applyModifier */     applyModifier,
455         /* applyModifierEM */   applyModifierEM,
456         /* initData */          initData,
457         /* requiredDataMask */  requiredDataMask,
458         /* freeData */          NULL,
459         /* isDisabled */        isDisabled,
460         /* updateDepgraph */    updateDepgraph,
461         /* dependsOnTime */     dependsOnTime,
462         /* dependsOnNormals */  NULL,
463         /* foreachObjectLink */ foreachObjectLink,
464         /* foreachIDLink */     foreachIDLink,
465         /* foreachTexLink */    foreachTexLink,
466 };
467