Commit: a52fbfa828966d19e8db5995d2332c90aa6515f4
Author: Bastien Montagne
Date:   Sun Nov 16 18:50:23 2014 +0100
Branches: master
https://developer.blender.org/rBa52fbfa828966d19e8db5995d2332c90aa6515f4

ShapeKey: Refactor a bit `BKE_key_convert_from_...`

Thing is, those functions always reallocate the whole keyblock's data mem,
while in some cases we already have right amount of elements, so we can just
copy over. Further more, `BKE_key_convert_from_offset`, despite its name,
was not making any check nor allocation on keyblock's data elements!

So split 'copy' operation itself in `BKE_key_update_from_...`,
where no mem checks/operations are performed (only an assert).

Only useful in sculpt mode currently, but will be used by fix for T35170 too.

===================================================================

M       source/blender/blenkernel/BKE_key.h
M       source/blender/blenkernel/intern/key.c
M       source/blender/editors/sculpt_paint/sculpt.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_key.h 
b/source/blender/blenkernel/BKE_key.h
index 892c42b..c96293f 100644
--- a/source/blender/blenkernel/BKE_key.h
+++ b/source/blender/blenkernel/BKE_key.h
@@ -87,15 +87,24 @@ void BKE_key_evaluate_relative(const int start, int end, 
const int tot, char *ba
                                float **per_keyblock_weights, const int mode);
 
 /* conversion functions */
-void    BKE_key_convert_to_mesh(struct KeyBlock *kb, struct Mesh *me);
-void    BKE_key_convert_from_mesh(struct Mesh *me, struct KeyBlock *kb);
-void    BKE_key_convert_to_lattice(struct KeyBlock *kb, struct Lattice *lt);
+/* Note: 'update_from' versions do not (re)allocate mem in kb, while 
'convert_from' do. */
+void    BKE_key_update_from_lattice(struct Lattice *lt, struct KeyBlock *kb);
 void    BKE_key_convert_from_lattice(struct Lattice *lt, struct KeyBlock *kb);
-void    BKE_key_convert_to_curve(struct KeyBlock *kb, struct Curve  *cu, 
struct ListBase *nurb);
+void    BKE_key_convert_to_lattice(struct KeyBlock *kb, struct Lattice *lt);
+
+void    BKE_key_update_from_curve(struct Curve *cu, struct KeyBlock *kb, 
struct ListBase *nurb);
 void    BKE_key_convert_from_curve(struct Curve *cu, struct KeyBlock *kb, 
struct ListBase *nurb);
-float (*BKE_key_convert_to_vertcos(struct Object *ob, struct KeyBlock *kb))[3];
+void    BKE_key_convert_to_curve(struct KeyBlock *kb, struct Curve  *cu, 
struct ListBase *nurb);
+
+void    BKE_key_update_from_mesh(struct Mesh *me, struct KeyBlock *kb);
+void    BKE_key_convert_from_mesh(struct Mesh *me, struct KeyBlock *kb);
+void    BKE_key_convert_to_mesh(struct KeyBlock *kb, struct Mesh *me);
+
+void    BKE_key_update_from_vertcos(struct Object *ob, struct KeyBlock *kb, 
float (*vertCos)[3]);
 void    BKE_key_convert_from_vertcos(struct Object *ob, struct KeyBlock *kb, 
float (*vertCos)[3]);
-void    BKE_key_convert_from_offset(struct Object *ob, struct KeyBlock *kb, 
float (*ofs)[3]);
+float (*BKE_key_convert_to_vertcos(struct Object *ob, struct KeyBlock *kb))[3];
+
+void    BKE_key_update_from_offset(struct Object *ob, struct KeyBlock *kb, 
float (*ofs)[3]);
 
 /* other management */
 bool    BKE_keyblock_move(struct Object *ob, int org_index, int new_index);
diff --git a/source/blender/blenkernel/intern/key.c 
b/source/blender/blenkernel/intern/key.c
index 28a77b7..f42ed5d 100644
--- a/source/blender/blenkernel/intern/key.c
+++ b/source/blender/blenkernel/intern/key.c
@@ -1669,19 +1669,16 @@ char *BKE_keyblock_curval_rnapath_get(Key *key, 
KeyBlock *kb)
 /* conversion functions */
 
 /************************* Lattice ************************/
-void BKE_key_convert_from_lattice(Lattice *lt, KeyBlock *kb)
+void BKE_key_update_from_lattice(Lattice *lt, KeyBlock *kb)
 {
        BPoint *bp;
        float *fp;
        int a, tot;
 
-       tot = lt->pntsu * lt->pntsv * lt->pntsw;
-       if (tot == 0) return;
-
-       if (kb->data) MEM_freeN(kb->data);
+       BLI_assert(kb->totelem == lt->pntsu * lt->pntsv * lt->pntsw);
 
-       kb->data = MEM_mallocN(lt->key->elemsize * tot, "kb->data");
-       kb->totelem = tot;
+       tot = kb->totelem;
+       if (tot == 0) return;
 
        bp = lt->def;
        fp = kb->data;
@@ -1690,6 +1687,21 @@ void BKE_key_convert_from_lattice(Lattice *lt, KeyBlock 
*kb)
        }
 }
 
+void BKE_key_convert_from_lattice(Lattice *lt, KeyBlock *kb)
+{
+       int tot;
+
+       tot = lt->pntsu * lt->pntsv * lt->pntsw;
+       if (tot == 0) return;
+
+       MEM_SAFE_FREE(kb->data);
+
+       kb->data = MEM_mallocN(lt->key->elemsize * tot, __func__);
+       kb->totelem = tot;
+
+       BKE_key_update_from_lattice(lt, kb);
+}
+
 void BKE_key_convert_to_lattice(KeyBlock *kb, Lattice *lt)
 {
        BPoint *bp;
@@ -1708,7 +1720,7 @@ void BKE_key_convert_to_lattice(KeyBlock *kb, Lattice *lt)
 }
 
 /************************* Curve ************************/
-void BKE_key_convert_from_curve(Curve *cu, KeyBlock *kb, ListBase *nurb)
+void BKE_key_update_from_curve(Curve *UNUSED(cu), KeyBlock *kb, ListBase *nurb)
 {
        Nurb *nu;
        BezTriple *bezt;
@@ -1717,18 +1729,14 @@ void BKE_key_convert_from_curve(Curve *cu, KeyBlock 
*kb, ListBase *nurb)
        int a, tot;
 
        /* count */
-       tot = BKE_nurbList_verts_count(nurb);
-       if (tot == 0) return;
-
-       if (kb->data) MEM_freeN(kb->data);
+       BLI_assert(BKE_nurbList_verts_count(nurb) == kb->totelem);
 
-       kb->data = MEM_mallocN(cu->key->elemsize * tot, "kb->data");
-       kb->totelem = tot;
+       tot = kb->totelem;
+       if (tot == 0) return;
 
        nu = nurb->first;
        fp = kb->data;
        while (nu) {
-
                if (nu->bezt) {
                        bezt = nu->bezt;
                        a = nu->pntsu;
@@ -1759,6 +1767,22 @@ void BKE_key_convert_from_curve(Curve *cu, KeyBlock *kb, 
ListBase *nurb)
        }
 }
 
+void BKE_key_convert_from_curve(Curve *cu, KeyBlock *kb, ListBase *nurb)
+{
+       int tot;
+
+       /* count */
+       tot = BKE_nurbList_verts_count(nurb);
+       if (tot == 0) return;
+
+       MEM_SAFE_FREE(kb->data);
+
+       kb->data = MEM_mallocN(cu->key->elemsize * tot, __func__);
+       kb->totelem = tot;
+
+       BKE_key_update_from_curve(cu, kb, nurb);
+}
+
 void BKE_key_convert_to_curve(KeyBlock *kb, Curve *UNUSED(cu), ListBase *nurb)
 {
        Nurb *nu;
@@ -1810,27 +1834,39 @@ void BKE_key_convert_to_curve(KeyBlock *kb, Curve 
*UNUSED(cu), ListBase *nurb)
 }
 
 /************************* Mesh ************************/
-void BKE_key_convert_from_mesh(Mesh *me, KeyBlock *kb)
+void BKE_key_update_from_mesh(Mesh *me, KeyBlock *kb)
 {
        MVert *mvert;
        float *fp;
-       int a;
-
-       if (me->totvert == 0) return;
+       int a, tot;
 
-       if (kb->data) MEM_freeN(kb->data);
+       BLI_assert(me->totvert == kb->totelem);
 
-       kb->data = MEM_mallocN(me->key->elemsize * me->totvert, "kb->data");
-       kb->totelem = me->totvert;
+       tot = me->totvert;
+       if (tot == 0) return;
 
        mvert = me->mvert;
        fp = kb->data;
-       for (a = 0; a < kb->totelem; a++, fp += 3, mvert++) {
+       for (a = 0; a < tot; a++, fp += 3, mvert++) {
                copy_v3_v3(fp, mvert->co);
 
        }
 }
 
+void BKE_key_convert_from_mesh(Mesh *me, KeyBlock *kb)
+{
+       int tot = me->totvert;
+
+       if (me->totvert == 0) return;
+
+       MEM_SAFE_FREE(kb->data);
+
+       kb->data = MEM_mallocN(me->key->elemsize * tot, __func__);
+       kb->totelem = tot;
+
+       BKE_key_update_from_mesh(me, kb);
+}
+
 void BKE_key_convert_to_mesh(KeyBlock *kb, Mesh *me)
 {
        MVert *mvert;
@@ -1847,37 +1883,24 @@ void BKE_key_convert_to_mesh(KeyBlock *kb, Mesh *me)
        }
 }
 
-/************************* vert coords ************************/
-float (*BKE_key_convert_to_vertcos(Object *ob, KeyBlock *kb))[3]
+/************************* raw coords ************************/
+void BKE_key_update_from_vertcos(Object *ob, KeyBlock *kb, float (*vertCos)[3])
 {
-       float (*vertCos)[3], *co;
-       const float *fp = kb->data;
-       int tot = 0, a;
+       float *co = (float *)vertCos;
+       float *fp = kb->data;
+       int tot, a;
 
-       /* Count of vertex coords in array */
-       if (ob->type == OB_MESH) {
-               Mesh *me = (Mesh *)ob->data;
-               tot = me->totvert;
-       }
-       else if (ob->type == OB_LATTICE) {
-               Lattice *lt = (Lattice *)ob->data;
-               tot = lt->pntsu * lt->pntsv * lt->pntsw;
-       }
-       else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
-               Curve *cu = (Curve *)ob->data;
-               tot = BKE_nurbList_verts_count(&cu->nurb);
-       }
+       BLI_assert(((ob->type == OB_MESH) ? me->totvert :
+                   (ob->type == OB_LATTICE) ? lt->pntsu * lt->pntsv * 
lt->pntsw :
+                   ELEM(ob->type, OB_CURVE, OB_SURF) ? 
BKE_nurbList_verts_count(&cu->nurb) : 0) == kb->totelem);
 
-       if (tot == 0) return NULL;
-
-       vertCos = MEM_mallocN(tot * sizeof(*vertCos), 
"BKE_key_convert_to_vertcos vertCos");
-
-       /* Copy coords to array */
-       co = (float *)vertCos;
+       tot = kb->totelem;
+       if (tot == 0) return;
 
+       /* Copy coords to keyblock */
        if (ELEM(ob->type, OB_MESH, OB_LATTICE)) {
                for (a = 0; a < tot; a++, fp += 3, co += 3) {
-                       copy_v3_v3(co, fp);
+                       copy_v3_v3(fp, co);
                }
        }
        else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
@@ -1894,7 +1917,7 @@ float (*BKE_key_convert_to_vertcos(Object *ob, KeyBlock 
*kb))[3]
 
                                while (a--) {
                                        for (i = 0; i < 3; i++) {
-                                               copy_v3_v3(co, fp);
+                                               copy_v3_v3(fp, co);
                                                fp += 3; co += 3;
                                        }
 
@@ -1908,7 +1931,7 @@ float (*BKE_key_convert_to_vertcos(Object *ob, KeyBlock 
*kb))[3]
                                a = nu->pntsu * nu->pntsv;
 
                                while (a--) {
-                                       copy_v3_v3(co, fp);
+                                       copy_v3_v3(fp, co);
 
                                        fp += 4;
                                        co += 3;
@@ -1920,16 +1943,13 @@ float (*BKE_key_convert_to_vertcos(Object *ob, KeyBlock 
*kb))[3]
                        nu = nu->next;
                }
        }
-
-       return vertCos;
 }
 
 void BKE_key_convert_from_vertcos(Object *ob, KeyBlock *kb, float 
(*vertCos)[3])
 {
-       float *co = (float *)vertCos, *fp;
-       int tot = 0, a, elemsize;
+       int tot = 0, elemsize;
 
-       if (kb->data) MEM_freeN(kb->data);
+       MEM_SAFE_FREE(kb->data);
 
        /* Count of vertex coords in array */
        if (ob->type == OB_MESH) {
@@ -1948,18 +1968,44 @@ void BKE_key_convert_from_vertcos(Object *ob, KeyBlock 
*kb, float (*vertCos)[3])
                tot = BKE_nurbList_verts_count(&cu->nurb);
        }
 
-       if (tot == 0) {
-               kb->data = NULL;
-               return;
-       }
+       if (tot == 0) return;
 
-       fp = kb->data = MEM_mallocN(tot * elemsize, "BKE_key_convert_to_vertcos 
vertCos");
+       kb->data = MEM_mallocN(tot * elemsize, __func__);
 
        /* Copy coords to keyblock */
+       BKE_key_update_from_vertcos(ob, kb, vertCos);
+}
+
+float (*BKE_key_convert_to_vertcos(Object *ob, KeyBlock *kb))[3]
+{
+       float (*vertCos)[3], *co;
+       const float *fp = kb->data;
+       int tot = 0, a;
+
+       /* Count of vertex coords in array */
+       if (ob->type == OB_MESH) {
+               Mesh *me = (Mesh *)ob->data;
+               tot = me->totvert;
+       }
+       else if (ob->type == OB_LATTICE) {
+               Lattice *lt = (Lattice *)ob->data;
+               tot = lt->pntsu * lt->pntsv * lt->pntsw;
+       }
+       else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
+               Curve *cu = (Curve *)ob->data;
+               tot = BKE_nurbList_verts_count(&cu->nurb);
+       }
+
+       if (tot == 0) return NULL;
+
+       vertCos = MEM_mallocN(tot * sizeof(*vertCos), 
"BKE_key_convert_to_vertcos vertCos");
+
+       /* Copy coords to array */
+       co = (float *)vertCos;
 
        if (ELEM(ob->type, OB_MESH, OB_LATTICE)) {
                for (a = 0; a < tot; a++, fp += 3, co += 3) {
-                       copy_v3_v3(fp, co);
+                       copy_v3_v3(co, fp);
                }
        }
        else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
@@ -1976,7 +2022,7 @@ void BKE_key_convert_from_vertcos(Object *ob, KeyBlock 
*kb, float (*vertCos)[3])
 
                                while (a--) {
                                        for (i = 0; i < 3; i++) {
-                                               copy_v3_v3(fp, co);
+                                               copy_v3_v3(co, fp);
                                                fp += 3; co += 3;
                                        }

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to