more vertex weight edits,

* replace inline loops with api calls.
* change constraints so verts with 0.0 weight are ignored like they are everywhere else.
This commit is contained in:
Campbell Barton 2011-12-14 22:54:38 +00:00
parent 3d5330f789
commit 40a2c1a292
10 changed files with 109 additions and 133 deletions

@ -924,7 +924,6 @@ void armature_deform_verts(Object *armOb, Object *target, DerivedMesh *dm,
float contrib = 0.0f;
float armature_weight = 1.0f; /* default to 1 if no overall def group */
float prevco_weight = 1.0f; /* weight for optional cached vertexcos */
int j;
if(use_quaternion) {
memset(&sumdq, 0, sizeof(DualQuat));
@ -971,12 +970,14 @@ void armature_deform_verts(Object *armOb, Object *target, DerivedMesh *dm,
mul_m4_v3(premat, co);
if(use_dverts && dvert && dvert->totweight) { // use weight groups ?
MDeformWeight *dw= dvert->dw;
int deformed = 0;
unsigned int j;
for(j = 0; j < dvert->totweight; j++){
int index = dvert->dw[j].def_nr;
for (j= dvert->totweight; j != 0; j--, dw++) {
const int index = dw->def_nr;
if(index < defbase_tot && (pchan= defnrToPC[index])) {
float weight = dvert->dw[j].weight;
float weight = dw->weight;
Bone *bone= pchan->bone;
pdef_info= pdef_info_array + defnrToPCIndex[index];

@ -436,16 +436,15 @@ static void contarget_get_mesh_mat (Object *ob, const char *substring, float mat
float vec[3] = {0.0f, 0.0f, 0.0f};
float normal[3] = {0.0f, 0.0f, 0.0f}, plane[3];
float imat[3][3], tmat[3][3];
int dgroup;
const int defgroup= defgroup_name_index(ob, substring);
short freeDM = 0;
/* initialize target matrix using target matrix */
copy_m4_m4(mat, ob->obmat);
/* get index of vertex group */
dgroup = defgroup_name_index(ob, substring);
if (dgroup < 0) return;
if (defgroup == -1) return;
/* get DerivedMesh */
if (em) {
/* target is in editmode, so get a special derived mesh */
@ -463,28 +462,25 @@ static void contarget_get_mesh_mat (Object *ob, const char *substring, float mat
if (dm) {
MDeformVert *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
int numVerts = dm->getNumVerts(dm);
int i, j, count = 0;
int i, count = 0;
float co[3], nor[3];
/* check that dvert is a valid pointers (just in case) */
if (dvert) {
MDeformVert *dv= dvert;
/* get the average of all verts with that are in the vertex-group */
for (i = 0; i < numVerts; i++) {
for (j = 0; j < dvert[i].totweight; j++) {
/* does this vertex belong to nominated vertex group? */
if (dvert[i].dw[j].def_nr == dgroup) {
dm->getVertCo(dm, i, co);
dm->getVertNo(dm, i, nor);
add_v3_v3(vec, co);
add_v3_v3(normal, nor);
count++;
break;
}
for (i = 0; i < numVerts; i++, dv++) {
MDeformWeight *dw= defvert_find_index(dv, defgroup);
if (dw && dw->weight != 0.0f) {
dm->getVertCo(dm, i, co);
dm->getVertNo(dm, i, nor);
add_v3_v3(vec, co);
add_v3_v3(normal, nor);
count++;
}
}
/* calculate averages of normal and coordinates */
if (count > 0) {
mul_v3_fl(vec, 1.0f / count);
@ -535,43 +531,38 @@ static void contarget_get_lattice_mat (Object *ob, const char *substring, float
float *co = dl?dl->verts:NULL;
BPoint *bp = lt->def;
MDeformVert *dvert = lt->dvert;
MDeformVert *dv = lt->dvert;
int tot_verts= lt->pntsu*lt->pntsv*lt->pntsw;
float vec[3]= {0.0f, 0.0f, 0.0f}, tvec[3];
int dgroup=0, grouped=0;
int grouped=0;
int i, n;
const int defgroup= defgroup_name_index(ob, substring);
/* initialize target matrix using target matrix */
copy_m4_m4(mat, ob->obmat);
/* get index of vertex group */
dgroup = defgroup_name_index(ob, substring);
if (dgroup < 0) return;
if (dvert == NULL) return;
if (defgroup == -1) return;
if (dv == NULL) return;
/* 1. Loop through control-points checking if in nominated vertex-group.
* 2. If it is, add it to vec to find the average point.
*/
for (i=0; i < tot_verts; i++, dvert++) {
for (n= 0; n < dvert->totweight; n++) {
/* found match - vert is in vgroup */
if (dvert->dw[n].def_nr == dgroup) {
for (i=0; i < tot_verts; i++, dv++) {
for (n= 0; n < dv->totweight; n++) {
MDeformWeight *dw= defvert_find_index(dv, defgroup);
if (dw && dw->weight > 0.0f) {
/* copy coordinates of point to temporary vector, then add to find average */
if (co)
memcpy(tvec, co, 3*sizeof(float));
else
memcpy(tvec, bp->vec, 3*sizeof(float));
memcpy(tvec, co ? co : bp->vec, 3 * sizeof(float));
add_v3_v3(vec, tvec);
grouped++;
break;
}
}
/* advance pointer to coordinate data */
if (co) co+= 3;
else bp++;
if (co) co += 3;
else bp++;
}
/* find average location, then multiply by ob->obmat to find world-space location */
@ -1106,10 +1097,10 @@ static void kinematic_new_data (void *cdata)
{
bKinematicConstraint *data= (bKinematicConstraint *)cdata;
data->weight= (float)1.0;
data->orientweight= (float)1.0;
data->weight= 1.0f;
data->orientweight= 1.0f;
data->iterations = 500;
data->dist= (float)1.0;
data->dist= 1.0f;
data->flag= CONSTRAINT_IK_TIP|CONSTRAINT_IK_STRETCH|CONSTRAINT_IK_POS;
}

@ -539,6 +539,12 @@ float defvert_find_weight(const struct MDeformVert *dvert, const int defgroup)
return dw ? dw->weight : 0.0f;
}
/* take care with this the rationale is:
* - if the object has no vertex group. act like vertex group isnt set and return 1.0,
* - if the vertex group exists but the 'defgroup' isnt found on this vertex, _still_ return 0.0
*
* This is a bit confusing, just saves some checks from the caller.
*/
float defvert_array_find_weight_safe(const struct MDeformVert *dvert, const int index, const int defgroup)
{
if (defgroup == -1 || dvert == NULL)

@ -688,57 +688,51 @@ static void vgroup_copy_active_to_sel(Object *ob)
}
}
static void vgroup_copy_active_to_sel_single(Object *ob, int def_nr)
static void vgroup_copy_active_to_sel_single(Object *ob, const int def_nr)
{
EditVert *eve_act;
MDeformVert *dvert_act;
MDeformVert *dv_act;
act_vert_def(ob, &eve_act, &dvert_act);
act_vert_def(ob, &eve_act, &dv_act);
if(dvert_act==NULL) {
if(dv_act==NULL) {
return;
}
else {
Mesh *me= ob->data;
EditMesh *em = BKE_mesh_get_editmesh(me);
EditVert *eve;
MDeformVert *dvert;
MDeformVert *dv;
MDeformWeight *dw;
float act_weight = -1.0f;
int i;
float weight_act;
int index= 0;
for(i=0, dw=dvert_act->dw; i < dvert_act->totweight; i++, dw++) {
if(def_nr == dw->def_nr) {
act_weight= dw->weight;
break;
}
}
dw= defvert_find_index(dv_act, def_nr);
if(act_weight < -0.5f)
if(dw == NULL)
return;
for(eve= em->verts.first; eve; eve= eve->next, index++) {
if(eve->f & SELECT && eve != eve_act) {
dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
if(dvert) {
for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++) {
if(def_nr == dw->def_nr) {
dw->weight= act_weight;
weight_act= dw->weight;
if(me->editflag & ME_EDIT_MIRROR_X)
editvert_mirror_update(ob, eve, -1, index);
for (eve= em->verts.first; eve; eve= eve->next, index++) {
if (eve->f & SELECT && eve != eve_act) {
dv= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
if(dv) {
dw= defvert_find_index(dv, def_nr);
if (dw) {
dw->weight= weight_act;
break;
if (me->editflag & ME_EDIT_MIRROR_X) {
editvert_mirror_update(ob, eve, -1, index);
}
}
}
}
}
if(me->editflag & ME_EDIT_MIRROR_X)
if (me->editflag & ME_EDIT_MIRROR_X) {
editvert_mirror_update(ob, eve_act, -1, -1);
}
}
}
@ -808,14 +802,15 @@ static void view3d_panel_vgroup(const bContext *C, Panel *pa)
Object *ob= OBACT;
EditVert *eve;
MDeformVert *dvert;
MDeformVert *dv;
act_vert_def(ob, &eve, &dvert);
act_vert_def(ob, &eve, &dv);
if(dvert && dvert->totweight) {
if(dv && dv->totweight) {
uiLayout *col;
bDeformGroup *dg;
int i;
MDeformWeight *dw = dv->dw;
unsigned int i;
int yco = 0;
uiBlockSetHandleFunc(block, do_view3d_vgroup_buttons, NULL);
@ -825,11 +820,11 @@ static void view3d_panel_vgroup(const bContext *C, Panel *pa)
uiBlockBeginAlign(block);
for (i=0; i<dvert->totweight; i++){
dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
for (i= dv->totweight; i != 0; i--, dw++) {
dg = BLI_findlink (&ob->defbase, dw->def_nr);
if(dg) {
uiDefButF(block, NUM, B_VGRP_PNL_EDIT_SINGLE + dvert->dw[i].def_nr, dg->name, 0, yco, 180, 20, &dvert->dw[i].weight, 0.0, 1.0, 1, 3, "");
uiDefBut(block, BUT, B_VGRP_PNL_COPY_SINGLE + dvert->dw[i].def_nr, "C", 180,yco,20,20, NULL, 0, 0, 0, 0, "Copy this groups weight to other selected verts");
uiDefButF(block, NUM, B_VGRP_PNL_EDIT_SINGLE + dw->def_nr, dg->name, 0, yco, 180, 20, &dw->weight, 0.0, 1.0, 1, 3, "");
uiDefBut(block, BUT, B_VGRP_PNL_COPY_SINGLE + dw->def_nr, "C", 180,yco,20,20, NULL, 0, 0, 0, 0, "Copy this groups weight to other selected verts");
yco -= 20;
}
}

@ -200,11 +200,11 @@ static void sphere_do(
* with or w/o a vgroup. With lots of if's in the code below,
* further optimizations are possible, if needed */
if (dvert) { /* with a vgroup */
MDeformVert *dv= dvert;
float fac_orig = fac;
for (i = 0; i < numVerts; i++) {
MDeformWeight *dw = NULL;
int j;
for (i = 0; i < numVerts; i++, dv++) {
float tmp_co[3];
float weight;
copy_v3_v3(tmp_co, vertexCos[i]);
if(ctrl_ob) {
@ -224,15 +224,10 @@ static void sphere_do(
if (len_v3(vec) > cmd->radius) continue;
}
for (j = 0; j < dvert[i].totweight; ++j) {
if(dvert[i].dw[j].def_nr == defgrp_index) {
dw = &dvert[i].dw[j];
break;
}
}
if (!dw) continue;
weight= defvert_find_weight(dv, defgrp_index);
if (weight <= 0.0f) continue;
fac = fac_orig * dw->weight;
fac = fac_orig * weight;
facm = 1.0f - fac;
normalize_v3(vec);

@ -188,7 +188,6 @@ static void meshdeformModifier_do(
struct EditMesh *em = (me)? BKE_mesh_get_editmesh(me): NULL;
DerivedMesh *tmpdm, *cagedm;
MDeformVert *dvert = NULL;
MDeformWeight *dw;
MDefInfluence *influences;
int *offsets;
float imat[4][4], cagemat[4][4], iobmat[4][4], icagemat[3][3], cmat[4][4];
@ -293,21 +292,14 @@ static void meshdeformModifier_do(
continue;
if(dvert) {
for(dw=NULL, a=0; a<dvert[b].totweight; a++) {
if(dvert[b].dw[a].def_nr == defgrp_index) {
dw = &dvert[b].dw[a];
break;
}
fac= defvert_find_weight(&dvert[b], defgrp_index);
if (mmd->flag & MOD_MDEF_INVERT_VGROUP) {
fac= 1.0f - fac;
}
if(mmd->flag & MOD_MDEF_INVERT_VGROUP) {
if(!dw) fac= 1.0f;
else if(dw->weight == 1.0f) continue;
else fac=1.0f-dw->weight;
}
else {
if(!dw) continue;
else fac= dw->weight;
if (fac <= 0.0) {
continue;
}
}

@ -154,24 +154,19 @@ static void smoothModifier_do(
}
if (dvert) {
for (i = 0; i < numVerts; i++) {
MDeformWeight *dw = NULL;
MDeformVert *dv= dvert;
for (i = 0; i < numVerts; i++, dv++) {
float f, fm, facw, *fp, *v;
int k;
short flag = smd->flag;
v = vertexCos[i];
fp = &ftmp[i*3];
for (k = 0; k < dvert[i].totweight; ++k) {
if(dvert[i].dw[k].def_nr == defgrp_index) {
dw = &dvert[i].dw[k];
break;
}
}
if (!dw) continue;
f = fac * dw->weight;
f= defvert_find_weight(dv, defgrp_index);
if (f <= 0.0f) continue;
f *= fac;
fm = 1.0f - f;
/* fp is the sum of uctmp[i] verts, so must be averaged */

@ -237,12 +237,12 @@ void BL_SkinDeformer::BGEDeformVerts()
}
}
MDeformVert *dv= dverts;
for (int i=0; i<m_bmesh->totvert; ++i)
for (int i=0; i<m_bmesh->totvert; ++i, dv++)
{
float contrib = 0.f, weight, max_weight=0.f;
bPoseChannel *pchan=NULL;
MDeformVert *dvert;
Eigen::Map<Eigen::Vector3f> norm(m_transnors[i]);
Eigen::Vector4f vec(0, 0, 0, 1);
Eigen::Matrix4f norm_chan_mat;
@ -251,18 +251,18 @@ void BL_SkinDeformer::BGEDeformVerts()
m_transverts[i][2],
1.f);
dvert = dverts+i;
if (!dvert->totweight)
if (!dv->totweight)
continue;
for (int j=0; j<dvert->totweight; ++j)
MDeformWeight *dw= dv->dw;
for (unsigned int j= dv->totweight; j != 0; j--, dw++)
{
int index = dvert->dw[j].def_nr;
const int index = dw->def_nr;
if (index < defbase_tot && (pchan=m_dfnrToPC[index]))
{
weight = dvert->dw[j].weight;
weight = dw->weight;
if (weight)
{

@ -29,6 +29,7 @@ set(INC
../Ketsji
../SceneGraph
../../blender/makesdna
../../blender/blenkernel
../../../intern/container
../../../intern/guardedalloc
../../../intern/moto/include

@ -43,6 +43,10 @@
#include <algorithm>
extern "C" {
# include "BKE_deform.h"
}
/* polygon sorting */
struct RAS_MeshObject::polygonSlot
@ -573,8 +577,8 @@ void RAS_MeshObject::CheckWeightCache(Object* obj)
{
KeyBlock *kb;
int kbindex, defindex;
MDeformVert *dvert= NULL;
int totvert, i, j;
MDeformVert *dv= NULL;
int totvert, i;
float *weights;
if (!m_mesh->key)
@ -598,19 +602,15 @@ void RAS_MeshObject::CheckWeightCache(Object* obj)
kb->weights = NULL;
}
dvert= m_mesh->dvert;
dv= m_mesh->dvert;
totvert= m_mesh->totvert;
weights= (float*)MEM_callocN(totvert*sizeof(float), "weights");
weights= (float*)MEM_mallocN(totvert*sizeof(float), "weights");
for (i=0; i < totvert; i++, dvert++) {
for(j=0; j<dvert->totweight; j++) {
if (dvert->dw[j].def_nr == defindex) {
weights[i]= dvert->dw[j].weight;
break;
}
}
for (i=0; i < totvert; i++, dv++) {
weights[i]= defvert_find_weight(dv, defindex);
}
kb->weights = weights;
m_cacheWeightIndex[kbindex] = defindex;
}