From e5209c205974b03f1090bf73414a82072a6a0d5b Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 5 Sep 2011 05:28:32 +0000 Subject: - vertex group modifiers isDisabled functions were incorrect, need to check if the string is set: == NULL will never be true. - was doing NULL checks on freeing memory in cases where the values were already accessed (blender would have crashed anyway), so remove the NULL checks. - use deform.c api weight functions to replace inline weight lookups in some cases. - change if checks in weightvg_do_mask() so its more obvious whats going on. --- .../blender/modifiers/intern/MOD_weightvg_util.c | 50 +++++++++++----------- source/blender/modifiers/intern/MOD_weightvgedit.c | 35 +++++++-------- source/blender/modifiers/intern/MOD_weightvgmix.c | 15 +++---- .../modifiers/intern/MOD_weightvgproximity.c | 14 +++--- 4 files changed, 51 insertions(+), 63 deletions(-) diff --git a/source/blender/modifiers/intern/MOD_weightvg_util.c b/source/blender/modifiers/intern/MOD_weightvg_util.c index dc6ec638b4c..8952d70d323 100644 --- a/source/blender/modifiers/intern/MOD_weightvg_util.c +++ b/source/blender/modifiers/intern/MOD_weightvg_util.c @@ -66,7 +66,6 @@ void weightvg_do_mask(int num, int *indices, float *org_w, float *new_w, Object const char *tex_uvlayer_name) { int ref_didx; - MDeformVert *dvert = NULL; int i; /* If influence factor is null, nothing to do! */ @@ -139,16 +138,16 @@ void weightvg_do_mask(int num, int *indices, float *org_w, float *new_w, Object } MEM_freeN(tex_co); - return; } + else if ((ref_didx = defgroup_name_index(ob, defgrp_name)) != -1) { + MDeformVert *dvert = NULL; - /* Check whether we want to set vgroup weights from a constant weight factor or a vertex - * group. - */ - /* Get vgroup idx from its name. */ - ref_didx = defgroup_name_index(ob, defgrp_name); - /* Proceed only if vgroup is valid, else use constant factor. */ - if (ref_didx >= 0) { + /* Check whether we want to set vgroup weights from a constant weight factor or a vertex + * group. + */ + /* Get vgroup idx from its name. */ + + /* Proceed only if vgroup is valid, else use constant factor. */ /* Get actual dverts (ie vertex group data). */ dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT); /* Proceed only if vgroup is valid, else assume factor = O. */ @@ -157,23 +156,18 @@ void weightvg_do_mask(int num, int *indices, float *org_w, float *new_w, Object /* For each weight (vertex), make the mix between org and new weights. */ for (i = 0; i < num; i++) { int idx = indices ? indices[i] : i; - int j; - for (j = 0; j < dvert[idx].totweight; j++) { - if(dvert[idx].dw[j].def_nr == ref_didx) { - float f = dvert[idx].dw[j].weight * fact; - org_w[i] = (new_w[i] * f) + (org_w[i] * (1.0-f)); - break; - } - } + const float f= defvert_find_weight(&dvert[idx], ref_didx) * fact; + org_w[i] = (new_w[i] * f) + (org_w[i] * (1.0f-f)); /* If that vertex is not in ref vgroup, assume null factor, and hence do nothing! */ } - return; } - - /* Default "influence" behavior. */ - /* For each weight (vertex), make the mix between org and new weights. */ - for (i = 0; i < num; i++) { - org_w[i] = (new_w[i] * fact) + (org_w[i] * (1.0-fact)); + else { + /* Default "influence" behavior. */ + /* For each weight (vertex), make the mix between org and new weights. */ + const float ifact= 1.0-fact; + for (i = 0; i < num; i++) { + org_w[i] = (new_w[i] * fact) + (org_w[i] * ifact); + } } } @@ -189,7 +183,7 @@ void weightvg_update_vg(MDeformVert *dvert, int defgrp_idx, int num, for (i = 0; i < num; i++) { int j; - char add2vg = do_add; + int add2vg = do_add; float w = weights[i]; MDeformVert *dv = &dvert[indices ? indices[i] : i]; MDeformWeight *newdw; @@ -207,6 +201,8 @@ void weightvg_update_vg(MDeformVert *dvert, int defgrp_idx, int num, if (dv->dw[j].def_nr == defgrp_idx) { /* Remove the vertex from this vgroup if needed. */ if (do_rem && w < rem_thresh) { + /* TODO, move this into deform.c to make into a generic function */ + dv->totweight--; /* If there are still other deform weights attached to this vert then remove * this deform weight, and reshuffle the others. @@ -230,14 +226,16 @@ void weightvg_update_vg(MDeformVert *dvert, int defgrp_idx, int num, else { dv->dw[j].weight = w; } - add2vg = 0; + add2vg = FALSE; break; } } /* If the vert wasn't in the deform group, add it if needed! */ - if (add2vg && w > add_thresh) { + if ((add2vg == TRUE) && w > add_thresh) { + /* TODO, mvoe into deform.c and make into a generic function, this assumes the vertex + * groups have already been checked, so this has to remain low level */ newdw = MEM_callocN(sizeof(MDeformWeight)*(dv->totweight+1), "WeightVGEdit Modifier, deformWeight"); if(dv->dw) { memcpy(newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight); diff --git a/source/blender/modifiers/intern/MOD_weightvgedit.c b/source/blender/modifiers/intern/MOD_weightvgedit.c index 55f8716e0b0..87747f255fd 100644 --- a/source/blender/modifiers/intern/MOD_weightvgedit.c +++ b/source/blender/modifiers/intern/MOD_weightvgedit.c @@ -173,7 +173,7 @@ static int isDisabled(ModifierData *md, int UNUSED(useRenderParams)) { WeightVGEditModifierData *wmd = (WeightVGEditModifierData*) md; /* If no vertex group, bypass. */ - return (wmd->defgrp_name == NULL); + return (wmd->defgrp_name[0] == '\0'); } static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData, @@ -185,17 +185,16 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der Mesh *ob_m = NULL; #endif MDeformVert *dvert = NULL; - float *org_w = NULL; /* Array original weights. */ - float *new_w = NULL; /* Array new weights. */ + float *org_w; /* Array original weights. */ + float *new_w; /* Array new weights. */ int numVerts; int defgrp_idx; int i; char rel_ret = 0; /* Boolean, whether we have to release ret dm or not, when not using it! */ - float *mapf = NULL; /* Cache for mapping factors. */ /* Flags. */ - char do_map = wmd->edit_flags & MOD_WVG_EDIT_CMAP; - char do_add = wmd->edit_flags & MOD_WVG_EDIT_ADD2VG; - char do_rem = wmd->edit_flags & MOD_WVG_EDIT_REMFVG; + int do_map = (wmd->edit_flags & MOD_WVG_EDIT_CMAP) != 0; + int do_add = (wmd->edit_flags & MOD_WVG_EDIT_ADD2VG) != 0; + int do_rem = (wmd->edit_flags & MOD_WVG_EDIT_REMFVG) != 0; /* Get number of verts. */ numVerts = dm->getNumVerts(dm); @@ -259,17 +258,17 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der org_w = MEM_mallocN(sizeof(float) * numVerts, "WeightVGEdit Modifier, org_w"); new_w = MEM_mallocN(sizeof(float) * numVerts, "WeightVGEdit Modifier, org_w"); for (i = 0; i < numVerts; i++) { - int j; + MDeformWeight *dw= defvert_find_index(&dvert[i], defgrp_idx); org_w[i] = new_w[i] = wmd->default_weight; - for (j = 0; j < dvert[i].totweight; j++) { - if(dvert[i].dw[j].def_nr == defgrp_idx) { - org_w[i] = new_w[i] = dvert[i].dw[j].weight; - break; - } + + if(dw) { + org_w[i] = new_w[i] = dw->weight; } + /* Do mapping. */ - if (do_map) + if (do_map) { new_w[i] = curvemapping_evaluateF(wmd->cmap_curve, 0, new_w[i]); + } } /* Do masking. */ @@ -282,12 +281,8 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der do_rem, wmd->rem_threshold); /* Freeing stuff. */ - if (org_w) - MEM_freeN(org_w); - if (new_w) - MEM_freeN(new_w); - if (mapf) - MEM_freeN(mapf); + MEM_freeN(org_w); + MEM_freeN(new_w); /* Return the vgroup-modified mesh. */ return ret; diff --git a/source/blender/modifiers/intern/MOD_weightvgmix.c b/source/blender/modifiers/intern/MOD_weightvgmix.c index ad70e8ab55c..a30afcb230f 100644 --- a/source/blender/modifiers/intern/MOD_weightvgmix.c +++ b/source/blender/modifiers/intern/MOD_weightvgmix.c @@ -217,7 +217,7 @@ static int isDisabled(ModifierData *md, int UNUSED(useRenderParams)) { WeightVGMixModifierData *wmd = (WeightVGMixModifierData*) md; /* If no vertex group, bypass. */ - return (wmd->defgrp_name_a == NULL); + return (wmd->defgrp_name_a[0] == '\0'); } static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData, @@ -231,8 +231,8 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der MDeformVert *dvert = NULL; int numVerts; int defgrp_idx, defgrp_idx2 = -1; - float *org_w = NULL; - float *new_w = NULL; + float *org_w; + float *new_w; int *tidx, *indices = NULL; int numIdx = 0; int i, j; @@ -416,13 +416,12 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der /* Update (add to) vgroup. * XXX Depending on the MOD_WVG_SET_xxx option chosen, we might have to add vertices to vgroup. */ - weightvg_update_vg(dvert, defgrp_idx, numIdx, indices, org_w, 1, -FLT_MAX, 0, 0.0f); + weightvg_update_vg(dvert, defgrp_idx, numIdx, indices, org_w, TRUE, -FLT_MAX, 0, 0.0f); /* Freeing stuff. */ - if (org_w) - MEM_freeN(org_w); - if (new_w) - MEM_freeN(new_w); + MEM_freeN(org_w); + MEM_freeN(new_w); + if (indices) MEM_freeN(indices); diff --git a/source/blender/modifiers/intern/MOD_weightvgproximity.c b/source/blender/modifiers/intern/MOD_weightvgproximity.c index 36f749ab506..513ba9c815f 100644 --- a/source/blender/modifiers/intern/MOD_weightvgproximity.c +++ b/source/blender/modifiers/intern/MOD_weightvgproximity.c @@ -327,7 +327,7 @@ static int isDisabled(ModifierData *md, int UNUSED(useRenderParams)) { WeightVGProximityModifierData *wmd = (WeightVGProximityModifierData*) md; /* If no vertex group, bypass. */ - if (wmd->defgrp_name == NULL) return 1; + if (wmd->defgrp_name[0] == '\0') return 1; /* If no target object, bypass. */ return (wmd->proximity_ob_target == NULL); } @@ -505,14 +505,10 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *der weightvg_update_vg(dvert, defgrp_idx, numIdx, indices, org_w, 0, 0.0f, 0, 0.0f); /* Freeing stuff. */ - if (org_w) - MEM_freeN(org_w); - if (new_w) - MEM_freeN(new_w); - if (indices) - MEM_freeN(indices); - if (v_cos) - MEM_freeN(v_cos); + MEM_freeN(org_w); + MEM_freeN(new_w); + MEM_freeN(indices); + MEM_freeN(v_cos); /* Return the vgroup-modified mesh. */ return ret; -- cgit v1.2.3