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.
This commit is contained in:
Bastien Montagne 2014-11-16 18:50:23 +01:00
parent b505ecaa4e
commit a52fbfa828
3 changed files with 172 additions and 114 deletions

View File

@ -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);

View File

@ -1669,20 +1669,17 @@ 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;
BLI_assert(kb->totelem == lt->pntsu * lt->pntsv * lt->pntsw);
tot = kb->totelem;
if (tot == 0) return;
if (kb->data) MEM_freeN(kb->data);
kb->data = MEM_mallocN(lt->key->elemsize * tot, "kb->data");
kb->totelem = tot;
bp = lt->def;
fp = kb->data;
for (a = 0; a < kb->totelem; a++, fp += 3, bp++) {
@ -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);
BLI_assert(BKE_nurbList_verts_count(nurb) == kb->totelem);
tot = kb->totelem;
if (tot == 0) return;
if (kb->data) MEM_freeN(kb->data);
kb->data = MEM_mallocN(cu->key->elemsize * tot, "kb->data");
kb->totelem = tot;
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;
int a, tot;
if (me->totvert == 0) return;
BLI_assert(me->totvert == kb->totelem);
if (kb->data) MEM_freeN(kb->data);
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,7 +1883,99 @@ void BKE_key_convert_to_mesh(KeyBlock *kb, Mesh *me)
}
}
/************************* vert coords ************************/
/************************* raw coords ************************/
void BKE_key_update_from_vertcos(Object *ob, KeyBlock *kb, float (*vertCos)[3])
{
float *co = (float *)vertCos;
float *fp = kb->data;
int tot, a;
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);
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(fp, co);
}
}
else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
Curve *cu = (Curve *)ob->data;
Nurb *nu = cu->nurb.first;
BezTriple *bezt;
BPoint *bp;
while (nu) {
if (nu->bezt) {
int i;
bezt = nu->bezt;
a = nu->pntsu;
while (a--) {
for (i = 0; i < 3; i++) {
copy_v3_v3(fp, co);
fp += 3; co += 3;
}
fp += 3; /* skip alphas */
bezt++;
}
}
else {
bp = nu->bp;
a = nu->pntsu * nu->pntsv;
while (a--) {
copy_v3_v3(fp, co);
fp += 4;
co += 3;
bp++;
}
}
nu = nu->next;
}
}
}
void BKE_key_convert_from_vertcos(Object *ob, KeyBlock *kb, float (*vertCos)[3])
{
int tot = 0, elemsize;
MEM_SAFE_FREE(kb->data);
/* Count of vertex coords in array */
if (ob->type == OB_MESH) {
Mesh *me = (Mesh *)ob->data;
tot = me->totvert;
elemsize = me->key->elemsize;
}
else if (ob->type == OB_LATTICE) {
Lattice *lt = (Lattice *)ob->data;
tot = lt->pntsu * lt->pntsv * lt->pntsw;
elemsize = lt->key->elemsize;
}
else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
Curve *cu = (Curve *)ob->data;
elemsize = cu->key->elemsize;
tot = BKE_nurbList_verts_count(&cu->nurb);
}
if (tot == 0) return;
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;
@ -1924,87 +2052,8 @@ float (*BKE_key_convert_to_vertcos(Object *ob, KeyBlock *kb))[3]
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;
if (kb->data) MEM_freeN(kb->data);
/* Count of vertex coords in array */
if (ob->type == OB_MESH) {
Mesh *me = (Mesh *)ob->data;
tot = me->totvert;
elemsize = me->key->elemsize;
}
else if (ob->type == OB_LATTICE) {
Lattice *lt = (Lattice *)ob->data;
tot = lt->pntsu * lt->pntsv * lt->pntsw;
elemsize = lt->key->elemsize;
}
else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
Curve *cu = (Curve *)ob->data;
elemsize = cu->key->elemsize;
tot = BKE_nurbList_verts_count(&cu->nurb);
}
if (tot == 0) {
kb->data = NULL;
return;
}
fp = kb->data = MEM_mallocN(tot * elemsize, "BKE_key_convert_to_vertcos vertCos");
/* 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(fp, co);
}
}
else if (ELEM(ob->type, OB_CURVE, OB_SURF)) {
Curve *cu = (Curve *)ob->data;
Nurb *nu = cu->nurb.first;
BezTriple *bezt;
BPoint *bp;
while (nu) {
if (nu->bezt) {
int i;
bezt = nu->bezt;
a = nu->pntsu;
while (a--) {
for (i = 0; i < 3; i++) {
copy_v3_v3(fp, co);
fp += 3; co += 3;
}
fp += 3; /* skip alphas */
bezt++;
}
}
else {
bp = nu->bp;
a = nu->pntsu * nu->pntsv;
while (a--) {
copy_v3_v3(fp, co);
fp += 4;
co += 3;
bp++;
}
}
nu = nu->next;
}
}
}
void BKE_key_convert_from_offset(Object *ob, KeyBlock *kb, float (*ofs)[3])
/************************* raw coord offsets ************************/
void BKE_key_update_from_offset(Object *ob, KeyBlock *kb, float (*ofs)[3])
{
int a;
float *co = (float *)ofs, *fp = kb->data;

View File

@ -2942,7 +2942,7 @@ void sculpt_vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
int apply_offset = ((currkey != kb) && (ob->shapenr - 1 == currkey->relative));
if (apply_offset)
BKE_key_convert_from_offset(ob, currkey, ofs);
BKE_key_update_from_offset(ob, currkey, ofs);
currkey = currkey->next;
}
@ -2960,8 +2960,8 @@ void sculpt_vertcos_to_key(Object *ob, KeyBlock *kb, float (*vertCos)[3])
BKE_mesh_calc_normals(me);
}
/* apply new coords on active key block */
BKE_key_convert_from_vertcos(ob, kb, vertCos);
/* apply new coords on active key block, no need to re-allocate kb->data here! */
BKE_key_update_from_vertcos(ob, kb, vertCos);
}
/* Note: we do the topology update before any brush actions to avoid