Code cleanup for the neighbor_average() sculpt function.

Moved some of the code into a couple new mesh functions for searching
in poly loops to simplify the function, the rest is just cosmetic
changes.
This commit is contained in:
2012-02-28 23:08:40 +00:00
parent d279fb503d
commit 1ebb6e3360
3 changed files with 72 additions and 44 deletions

View File

@@ -99,6 +99,18 @@ void mesh_calc_poly_center(struct MPoly *mpoly, struct MLoop *loopstart,
float mesh_calc_poly_area(struct MPoly *mpoly, struct MLoop *loopstart, float mesh_calc_poly_area(struct MPoly *mpoly, struct MLoop *loopstart,
struct MVert *mvarray, float polynormal[3]); struct MVert *mvarray, float polynormal[3]);
/* Find the index of the loop in 'poly' which references vertex,
returns -1 if not found */
int poly_find_loop_from_vert(const struct MPoly *poly,
const struct MLoop *loopstart,
unsigned vert);
/* Fill 'adj_r' with the loop indices in 'poly' adjacent to the
vertex. Returns the index of the loop matching vertex, or -1 if the
vertex is not in 'poly' */
int poly_get_adj_loops_from_vert(unsigned adj_r[3], const struct MPoly *poly,
const struct MLoop *mloop, unsigned vert);
void unlink_mesh(struct Mesh *me); void unlink_mesh(struct Mesh *me);
void free_mesh(struct Mesh *me, int unlink); void free_mesh(struct Mesh *me, int unlink);
struct Mesh *add_mesh(const char *name); struct Mesh *add_mesh(const char *name);

View File

@@ -2833,6 +2833,42 @@ float mesh_calc_poly_area(MPoly *mpoly, MLoop *loopstart,
} }
} }
/* Find the index of the loop in 'poly' which references vertex,
returns -1 if not found */
int poly_find_loop_from_vert(const MPoly *poly, const MLoop *loopstart,
unsigned vert)
{
int j;
for (j = 0; j < poly->totloop; j++, loopstart++) {
if (loopstart->v == vert)
return j;
}
return -1;
}
/* Fill 'adj_r' with the loop indices in 'poly' adjacent to the
vertex. Returns the index of the loop matching vertex, or -1 if the
vertex is not in 'poly' */
int poly_get_adj_loops_from_vert(unsigned adj_r[3], const MPoly *poly,
const MLoop *mloop, unsigned vert)
{
int corner = poly_find_loop_from_vert(poly,
&mloop[poly->loopstart],
vert);
if(corner != -1) {
const MLoop *ml = &mloop[poly->loopstart + corner];
/* vertex was found */
adj_r[0] = ME_POLY_LOOP_PREV(mloop, poly, corner)->v;
adj_r[1] = ml->v;
adj_r[2] = ME_POLY_LOOP_NEXT(mloop, poly, corner)->v;
}
return corner;
}
/* basic vertex data functions */ /* basic vertex data functions */
int minmax_mesh(Mesh *me, float min[3], float max[3]) int minmax_mesh(Mesh *me, float min[3], float max[3])
{ {

View File

@@ -904,62 +904,42 @@ static void calc_sculpt_normal(Sculpt *sd, Object *ob, float an[3], PBVHNode **n
polygon.) */ polygon.) */
static void neighbor_average(SculptSession *ss, float avg[3], unsigned vert) static void neighbor_average(SculptSession *ss, float avg[3], unsigned vert)
{ {
int i, j, ok, total=0; const int ncount = BLI_countlist(&ss->pmap[vert]);
IndexNode *node= ss->pmap[vert].first; const MVert *mvert = ss->mvert;
char ncount= BLI_countlist(&ss->pmap[vert]); float (*deform_co)[3] = ss->deform_cos;
MPoly *f;
MLoop *ml;
avg[0] = avg[1] = avg[2] = 0; zero_v3(avg);
/* Don't modify corner vertices */ /* Don't modify corner vertices */
if(ncount==1) { if(ncount != 1) {
if(ss->deform_cos) copy_v3_v3(avg, ss->deform_cos[vert]); IndexNode *node;
else copy_v3_v3(avg, ss->mvert[vert].co); int total = 0;
return; for(node = ss->pmap[vert].first; node; node = node->next) {
} const MPoly *p= &ss->mpoly[node->index];
unsigned f_adj_v[3];
while(node){ if(poly_get_adj_loops_from_vert(f_adj_v, p, ss->mloop, vert) != -1) {
f= &ss->mpoly[node->index]; int i;
for (i = 0; i < 3; i++) {
if (ncount != 2 || BLI_countlist(&ss->pmap[f_adj_v[i]]) <= 2) {
add_v3_v3(avg, deform_co ? deform_co[f_adj_v[i]] :
mvert[f_adj_v[i]].co);
/* find the loop in the poly which references this vertex */ total++;
ok = FALSE; }
ml = ss->mloop + f->loopstart;
for (j = 0; j < f->totloop; j++, ml++) {
if (ml->v == vert) {
ok = TRUE;
break;
}
}
if (ok) {
/* vertex was found */
unsigned int f_adj_v[3] = {
ME_POLY_LOOP_PREV(ss->mloop, f, j)->v,
ml->v,
ME_POLY_LOOP_NEXT(ss->mloop, f, j)->v};
for (i=0; i<3; ++i) {
if (ncount!=2 || BLI_countlist(&ss->pmap[f_adj_v[i]]) <= 2) {
if(ss->deform_cos) add_v3_v3(avg, ss->deform_cos[f_adj_v[i]]);
else add_v3_v3(avg, ss->mvert[f_adj_v[i]].co);
++total;
} }
} }
} }
node= node->next; if(total > 0) {
mul_v3_fl(avg, 1.0f / total);
return;
}
} }
if(total>0) copy_v3_v3(avg, deform_co ? deform_co[vert] : mvert[vert].co);
mul_v3_fl(avg, 1.0f / total);
else {
if(ss->deform_cos) copy_v3_v3(avg, ss->deform_cos[vert]);
else copy_v3_v3(avg, ss->mvert[vert].co);
}
} }
static void do_mesh_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node, float bstrength) static void do_mesh_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode *node, float bstrength)