fix [#29044] applying mirror modifier causes crash; something with vertex groups?
authorCampbell Barton <ideasman42@gmail.com>
Thu, 27 Oct 2011 07:54:32 +0000 (07:54 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 27 Oct 2011 07:54:32 +0000 (07:54 +0000)
source/blender/blenkernel/BKE_deform.h
source/blender/blenkernel/intern/deform.c
source/blender/editors/object/object_vgroup.c
source/blender/editors/space_view3d/view3d_buttons.c
source/blender/modifiers/intern/MOD_mirror.c

index 15d3c86..84a6517 100644 (file)
@@ -44,7 +44,7 @@ void                           defgroup_copy_list(struct ListBase *lb1, struct ListBase *lb2);
 struct bDeformGroup *defgroup_duplicate(struct bDeformGroup *ingroup);
 struct bDeformGroup *defgroup_find_name(struct Object *ob, const char *name);
 int                                     defgroup_find_index(struct Object *ob, struct bDeformGroup *dg);
-int                                    *defgroup_flip_map(struct Object *ob, int use_default);
+int                                    *defgroup_flip_map(struct Object *ob, int *flip_map_len, int use_default);
 int                                     defgroup_flip_index(struct Object *ob, int index, int use_default);
 int                                     defgroup_name_index(struct Object *ob, const char *name);
 void                            defgroup_unique_name(struct bDeformGroup *dg, struct Object *ob);
@@ -57,9 +57,9 @@ float  defvert_array_find_weight_safe(const struct MDeformVert *dvert, int index
 
 void defvert_copy(struct MDeformVert *dvert_r, const struct MDeformVert *dvert);
 void defvert_sync(struct MDeformVert *dvert_r, const struct MDeformVert *dvert, int use_verify);
-void defvert_sync_mapped(struct MDeformVert *dvert_r, const struct MDeformVert *dvert, const int *flip_map, int use_verify);
+void defvert_sync_mapped(struct MDeformVert *dvert_r, const struct MDeformVert *dvert, const int *flip_map, const int flip_map_len, int use_verify);
 void defvert_remap (struct MDeformVert *dvert, int *map);
-void defvert_flip(struct MDeformVert *dvert, const int *flip_map);
+void defvert_flip(struct MDeformVert *dvert, const int *flip_map, const int flip_map_len);
 void defvert_normalize(struct MDeformVert *dvert);
 
 /* utility function, note that 32 chars is the maximum string length since its only
index 0f6828c..b8f5d79 100644 (file)
@@ -114,18 +114,20 @@ void defvert_sync (MDeformVert *dvert_r, const MDeformVert *dvert, int use_verif
 }
 
 /* be sure all flip_map values are valid */
-void defvert_sync_mapped (MDeformVert *dvert_r, const MDeformVert *dvert, const int *flip_map, int use_verify)
+void defvert_sync_mapped (MDeformVert *dvert_r, const MDeformVert *dvert, const int *flip_map, const int flip_map_len, const int use_verify)
 {
-       if(dvert->totweight && dvert_r->totweight) {
+       if (dvert->totweight && dvert_r->totweight) {
                int i;
                MDeformWeight *dw;
-               for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++) {
-                       MDeformWeight *dw_r;
-                       if(use_verify)  dw_r= defvert_find_index(dvert_r, flip_map[dw->def_nr]);
-                       else                    dw_r= defvert_verify_index(dvert_r, flip_map[dw->def_nr]);
-
-                       if(dw_r) {
-                               dw_r->weight= dw->weight;
+               for (i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++) {
+                       if (dw->def_nr < flip_map_len) {
+                               MDeformWeight *dw_r;
+                               if(use_verify)  dw_r= defvert_find_index(dvert_r, flip_map[dw->def_nr]);
+                               else                    dw_r= defvert_verify_index(dvert_r, flip_map[dw->def_nr]);
+
+                               if(dw_r) {
+                                       dw_r->weight= dw->weight;
+                               }
                        }
                }
        }
@@ -163,14 +165,16 @@ void defvert_normalize (MDeformVert *dvert)
        }
 }
 
-void defvert_flip (MDeformVert *dvert, const int *flip_map)
+void defvert_flip (MDeformVert *dvert, const int *flip_map, const int flip_map_len)
 {
        MDeformWeight *dw;
        int i;
 
-       for(dw= dvert->dw, i=0; i<dvert->totweight; dw++, i++)
-               if(flip_map[dw->def_nr] >= 0)
+       for(dw= dvert->dw, i=0; i<dvert->totweight; dw++, i++) {
+               if((dw->def_nr < flip_map_len) && (flip_map[dw->def_nr] >= 0)) {
                        dw->def_nr= flip_map[dw->def_nr];
+               }
+       }
 }
 
 
@@ -250,17 +254,17 @@ int defgroup_find_index (Object *ob, bDeformGroup *dg)
 }
 
 /* note, must be freed */
-int *defgroup_flip_map(Object *ob, int use_default)
+int *defgroup_flip_map(Object *ob, int *flip_map_len, int use_default)
 {
        bDeformGroup *dg;
-       int totdg= BLI_countlist(&ob->defbase);
+       int totdg= *flip_map_len= BLI_countlist(&ob->defbase);
 
        if(totdg==0) {
                return NULL;
        }
        else {
                char name[sizeof(dg->name)];
-               int i, flip_num, *map= MEM_mallocN(totdg * sizeof(int), "get_defgroup_flip_map");
+               int i, flip_num, *map= MEM_mallocN(totdg * sizeof(int), __func__);
 
                memset(map, -1, totdg * sizeof(int));
 
index 378e4a4..d4ee68f 100644 (file)
@@ -1527,7 +1527,7 @@ static void vgroup_clean_all(Object *ob, float eul, int keep_single)
 
 static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
                             const char sel, const char sel_mirr,
-                            const int *flip_map,
+                            const int *flip_map, const int flip_map_len,
                             const short mirror_weights, const short flip_vgroups)
 {
        BLI_assert(sel || sel_mirr);
@@ -1537,8 +1537,8 @@ static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
                if(mirror_weights)
                        SWAP(MDeformVert, *dvert, *dvert_mirr);
                if(flip_vgroups) {
-                       defvert_flip(dvert, flip_map);
-                       defvert_flip(dvert_mirr, flip_map);
+                       defvert_flip(dvert, flip_map, flip_map_len);
+                       defvert_flip(dvert_mirr, flip_map, flip_map_len);
                }
        }
        else {
@@ -1550,24 +1550,24 @@ static void dvert_mirror_op(MDeformVert *dvert, MDeformVert *dvert_mirr,
                if(mirror_weights)
                        defvert_copy(dvert, dvert_mirr);
                if(flip_vgroups) {
-                       defvert_flip(dvert, flip_map);
+                       defvert_flip(dvert, flip_map, flip_map_len);
                }
        }
 }
 
 void ED_vgroup_mirror(Object *ob, const short mirror_weights, const short flip_vgroups)
 {
-#define VGROUP_MIRR_OP dvert_mirror_op(dvert, dvert_mirr, sel, sel_mirr, flip_map, mirror_weights, flip_vgroups)
+#define VGROUP_MIRR_OP dvert_mirror_op(dvert, dvert_mirr, sel, sel_mirr, flip_map, flip_map_len, mirror_weights, flip_vgroups)
 
        EditVert *eve, *eve_mirr;
        MDeformVert *dvert, *dvert_mirr;
        short sel, sel_mirr;
-       int     *flip_map;
+       int     *flip_map, flip_map_len;
 
        if(mirror_weights==0 && flip_vgroups==0)
                return;
 
-       flip_map= defgroup_flip_map(ob, 0);
+       flip_map= defgroup_flip_map(ob, &flip_map_len, FALSE);
 
        /* only the active group */
        if(ob->type == OB_MESH) {
index 5e0a32c..63b0f68 100644 (file)
@@ -613,9 +613,9 @@ static void editvert_mirror_update(Object *ob, EditVert *eve, int def_nr, int in
                if(dvert_dst) {
                        if(def_nr == -1) {
                                /* all vgroups, add groups where neded  */
-
-                               int *flip_map= defgroup_flip_map(ob, 1);
-                               defvert_sync_mapped(dvert_dst, dvert_src, flip_map, 1);
+                               int flip_map_len;
+                               int *flip_map= defgroup_flip_map(ob, &flip_map_len, TRUE);
+                               defvert_sync_mapped(dvert_dst, dvert_src, flip_map, flip_map_len, TRUE);
                                MEM_freeN(flip_map);
                        }
                        else {
index f551f0f..b36850d 100644 (file)
@@ -108,7 +108,7 @@ static DerivedMesh *doMirrorOnAxis(MirrorModifierData *mmd,
        int maxVerts = dm->getNumVerts(dm);
        int maxEdges = dm->getNumEdges(dm);
        int maxFaces = dm->getNumFaces(dm);
-       int *flip_map= NULL;
+       int *flip_map= NULL, flip_map_len= 0;
        int do_vgroup_mirr= (mmd->flag & MOD_MIR_VGROUP);
        int (*indexMap)[2];
        float mtx[4][4], imtx[4][4];
@@ -121,7 +121,7 @@ static DerivedMesh *doMirrorOnAxis(MirrorModifierData *mmd,
 
 
        if (do_vgroup_mirr) {
-               flip_map= defgroup_flip_map(ob, 0);
+               flip_map= defgroup_flip_map(ob, &flip_map_len, FALSE);
                if(flip_map == NULL)
                        do_vgroup_mirr= 0;
        }
@@ -187,7 +187,7 @@ static DerivedMesh *doMirrorOnAxis(MirrorModifierData *mmd,
                        if (do_vgroup_mirr) {
                                MDeformVert *dvert= DM_get_vert_data(result, numVerts, CD_MDEFORMVERT);
                                if(dvert) {
-                                       defvert_flip(dvert, flip_map);
+                                       defvert_flip(dvert, flip_map, flip_map_len);
                                }
                        }