minor refactor of vertex group functions,
authorCampbell Barton <ideasman42@gmail.com>
Thu, 8 Dec 2011 03:47:45 +0000 (03:47 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 8 Dec 2011 03:47:45 +0000 (03:47 +0000)
- defvert_remove_index's index wasnt used anywhere, rename to defvert_remove_group
- defvert_add_to_group was local in MOD_weightvg_util.c, moved to deform.c and renamed to defvert_add_index_notest

real fix coming next...

source/blender/blenkernel/BKE_deform.h
source/blender/blenkernel/intern/deform.c
source/blender/modifiers/intern/MOD_weightvg_util.c

index ed8ea6911346a28ae0b3a98967ccbb7ab2829358..b08afd9c475cf6d19f6354c18c7f30eec3adfd33 100644 (file)
@@ -52,7 +52,8 @@ void                           defgroup_unique_name(struct bDeformGroup *dg, struct Object *ob);
 
 struct MDeformWeight   *defvert_find_index(const struct MDeformVert *dv, const int defgroup);
 struct MDeformWeight   *defvert_verify_index(struct MDeformVert *dv, const int defgroup);
-void                     defvert_remove_index(struct MDeformVert *dvert, int defgroup, struct MDeformWeight *dw);
+void                     defvert_add_index_notest(struct MDeformVert *dv, int defgroup, const float weight);
+void                     defvert_remove_group(struct MDeformVert *dvert, struct MDeformWeight *dw);
 
 float  defvert_find_weight(const struct MDeformVert *dvert, const int defgroup);
 float  defvert_array_find_weight_safe(const struct MDeformVert *dvert, const int index, const int defgroup);
index 1f09d98d697c359d92ed4641a5bcd6371c03095a..8dfd7e25bfb56663ece7a93275ca56aef6543b0b 100644 (file)
@@ -574,46 +574,61 @@ MDeformWeight *defvert_verify_index(MDeformVert *dvert, const int defgroup)
        return dw_new;
 }
 
-/* Removes the given vertex from the vertex group, specified either by its defgrp_idx,
- * or directly by its MDeformWeight pointer, if dw is not NULL.
- * WARNING: This function frees the given MDeformWeight, do not use it afterward! */
-void defvert_remove_index(MDeformVert *dvert, int defgroup, MDeformWeight *dw)
+/* TODO. merge with code above! */
+
+/* Adds the given vertex to the specified vertex group, with given weight.
+ * warning, this does NOT check for existign, assume caller already knows its not there */
+void defvert_add_index_notest(MDeformVert *dvert, int defgroup, const float weight)
 {
        MDeformWeight *dw_new;
-       int i;
 
-       /* Get index of removed MDeformWeight. */
-       if (dw == NULL) {
-               dw = dvert->dw;
-               for (i = dvert->totweight; i > 0; i--, dw++) {
-                       if (dw->def_nr == defgroup)
-                               break;
-               }
-               i--;
+       /* do this check always, this function is used to check for it */
+       if (!dvert || defgroup < 0)
+               return;
+
+       dw_new = MEM_callocN(sizeof(MDeformWeight)*(dvert->totweight+1), "defvert_add_to group, new deformWeight");
+       if(dvert->dw) {
+               memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*dvert->totweight);
+               MEM_freeN(dvert->dw);
        }
-       else {
-               i = dw - dvert->dw;
+       dvert->dw = dw_new;
+       dw_new += dvert->totweight;
+       dw_new->weight = weight;
+       dw_new->def_nr = defgroup;
+       dvert->totweight++;
+}
+
+
+/* Removes the given vertex from the vertex group.
+ * WARNING: This function frees the given MDeformWeight, do not use it afterward! */
+void defvert_remove_group(MDeformVert *dvert, MDeformWeight *dw)
+{
+       if (dvert && dw) {
+               MDeformWeight *dw_new;
+               int i = dw - dvert->dw;
+
                /* Security check! */
-               if(i < 0 || i >= dvert->totweight)
+               if(i < 0 || i >= dvert->totweight) {
                        return;
-       }
+               }
 
-       dvert->totweight--;
-       /* If there are still other deform weights attached to this vert then remove
-        * this deform weight, and reshuffle the others.
-        */
-       if (dvert->totweight) {
-               dw_new = MEM_mallocN(sizeof(MDeformWeight)*(dvert->totweight), __func__);
-               if (dvert->dw){
-                       memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*i);
-                       memcpy(dw_new+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i));
+               dvert->totweight--;
+               /* If there are still other deform weights attached to this vert then remove
+                * this deform weight, and reshuffle the others.
+                */
+               if (dvert->totweight) {
+                       dw_new = MEM_mallocN(sizeof(MDeformWeight)*(dvert->totweight), __func__);
+                       if (dvert->dw){
+                               memcpy(dw_new, dvert->dw, sizeof(MDeformWeight)*i);
+                               memcpy(dw_new+i, dvert->dw+i+1, sizeof(MDeformWeight)*(dvert->totweight-i));
+                               MEM_freeN(dvert->dw);
+                       }
+                       dvert->dw = dw_new;
+               }
+               else {
+                       /* If there are no other deform weights left then just remove this one. */
                        MEM_freeN(dvert->dw);
+                       dvert->dw = NULL;
                }
-               dvert->dw = dw_new;
-       }
-       else {
-               /* If there are no other deform weights left then just remove this one. */
-               MEM_freeN(dvert->dw);
-               dvert->dw = NULL;
        }
 }
index e206000f41ddbe305a73a4fce873ee655edd2ad3..5631b7e716ad3d0cf5c8f7b90c90bdaa888cda82 100644 (file)
@@ -222,23 +222,7 @@ void weightvg_do_mask(int num, const int *indices, float *org_w, const float *ne
        }
 }
 
-/* Adds the given vertex to the specified vertex group, with given weight. */
-static void defvert_add_to_group(MDeformVert *dv, int defgrp_idx, const float weight)
-{
-       /* TODO, move into deform.c as a generic function. This assumes the vertex
-        * groups have already been checked, so this has to remain low level. */
-       MDeformWeight *newdw;
 
-       newdw = MEM_callocN(sizeof(MDeformWeight)*(dv->totweight+1), "defvert_add_to group, new deformWeight");
-       if(dv->dw) {
-               memcpy(newdw, dv->dw, sizeof(MDeformWeight)*dv->totweight);
-               MEM_freeN(dv->dw);
-       }
-       dv->dw = newdw;
-       dv->dw[dv->totweight].weight = weight;
-       dv->dw[dv->totweight].def_nr = defgrp_idx;
-       dv->totweight++;
-}
 
 
 /* Applies weights to given vgroup (defgroup), and optionnaly add/remove vertices from the group.
@@ -264,7 +248,7 @@ void weightvg_update_vg(MDeformVert *dvert, int defgrp_idx, MDeformWeight **dws,
                /* If the vertex is in this vgroup, remove it if needed, or just update it. */
                if(dw != NULL) {
                        if(do_rem && w < rem_thresh) {
-                               defvert_remove_index(dv, defgrp_idx, dw);
+                               defvert_remove_group(dv, dw);
                        }
                        else {
                                dw->weight = w;
@@ -272,7 +256,7 @@ void weightvg_update_vg(MDeformVert *dvert, int defgrp_idx, MDeformWeight **dws,
                }
                /* Else, add it if needed! */
                else if(do_add && w > add_thresh) {
-                       defvert_add_to_group(dv, defgrp_idx, w);
+                       defvert_add_index_notest(dv, defgrp_idx, w);
                }
        }
 }