Code cleanup: refactor sculpt normal and related functions.

This commit is contained in:
Nicholas Bishop
2012-05-15 04:44:20 +00:00
parent 028e21adb3
commit cb24a9505a

View File

@@ -247,7 +247,13 @@ typedef struct StrokeCache {
int mirror_symmetry_pass; /* the symmetry pass we are currently on between 0 and 7*/
float true_view_normal[3];
float view_normal[3];
float last_area_normal[3];
/* sculpt_normal gets calculated by calc_sculpt_normal(), then the
sculpt_normal_symm gets updated quickly with the usual symmetry
transforms */
float sculpt_normal[3];
float sculpt_normal_symm[3];
float last_center[3];
int radial_symmetry_pass;
float symm_rot_mat[4][4];
@@ -954,56 +960,87 @@ static void calc_area_normal(Sculpt *sd, Object *ob, float an[3], PBVHNode **nod
normalize_v3(an);
}
/* This initializes the faces to be moved for this sculpt for draw/layer/flatten; then it
* finds average normal for all active vertices - note that this is called once for each mirroring direction */
static void calc_sculpt_normal(Sculpt *sd, Object *ob, float an[3], PBVHNode **nodes, int totnode)
/* Calculate primary direction of movement for many brushes */
static void calc_sculpt_normal(Sculpt *sd, Object *ob,
PBVHNode **nodes, int totnode,
float an[3])
{
SculptSession *ss = ob->sculpt;
Brush *brush = paint_brush(&sd->paint);
const Brush *brush = paint_brush(&sd->paint);
const SculptSession *ss = ob->sculpt;
if (ss->cache->mirror_symmetry_pass == 0 &&
ss->cache->radial_symmetry_pass == 0 &&
(ss->cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
switch (brush->sculpt_plane) {
case SCULPT_DISP_DIR_VIEW:
ED_view3d_global_to_vector(ss->cache->vc->rv3d,
ss->cache->vc->rv3d->twmat[3],
an);
break;
case SCULPT_DISP_DIR_X:
an[1] = 0.0;
an[2] = 0.0;
an[0] = 1.0;
break;
case SCULPT_DISP_DIR_Y:
an[0] = 0.0;
an[2] = 0.0;
an[1] = 1.0;
break;
case SCULPT_DISP_DIR_Z:
an[0] = 0.0;
an[1] = 0.0;
an[2] = 1.0;
break;
case SCULPT_DISP_DIR_AREA:
calc_area_normal(sd, ob, an, nodes, totnode);
default:
break;
}
}
static void update_sculpt_normal(Sculpt *sd, Object *ob,
PBVHNode **nodes, int totnode)
{
const Brush *brush = paint_brush(&sd->paint);
StrokeCache *cache = ob->sculpt->cache;
if (cache->mirror_symmetry_pass == 0 &&
cache->radial_symmetry_pass == 0 &&
(cache->first_time || !(brush->flag & BRUSH_ORIGINAL_NORMAL)))
{
switch (brush->sculpt_plane) {
case SCULPT_DISP_DIR_VIEW:
ED_view3d_global_to_vector(ss->cache->vc->rv3d, ss->cache->vc->rv3d->twmat[3], an);
break;
case SCULPT_DISP_DIR_X:
an[1] = 0.0;
an[2] = 0.0;
an[0] = 1.0;
break;
case SCULPT_DISP_DIR_Y:
an[0] = 0.0;
an[2] = 0.0;
an[1] = 1.0;
break;
case SCULPT_DISP_DIR_Z:
an[0] = 0.0;
an[1] = 0.0;
an[2] = 1.0;
break;
case SCULPT_DISP_DIR_AREA:
calc_area_normal(sd, ob, an, nodes, totnode);
default:
break;
}
copy_v3_v3(ss->cache->last_area_normal, an);
calc_sculpt_normal(sd, ob, nodes, totnode, cache->sculpt_normal);
copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal);
}
else {
copy_v3_v3(an, ss->cache->last_area_normal);
flip_v3(an, ss->cache->mirror_symmetry_pass);
mul_m4_v3(ss->cache->symm_rot_mat, an);
copy_v3_v3(cache->sculpt_normal_symm, cache->sculpt_normal);
flip_v3(cache->sculpt_normal_symm, cache->mirror_symmetry_pass);
mul_m4_v3(cache->symm_rot_mat, cache->sculpt_normal_symm);
}
}
/* Test whether the StrokeCache.sculpt_normal needs update in
do_brush_action() */
static int brush_needs_sculpt_normal(const Brush *brush)
{
return ((ELEM(brush->sculpt_tool,
SCULPT_TOOL_GRAB,
SCULPT_TOOL_SNAKE_HOOK) &&
((brush->normal_weight > 0) ||
(brush->flag & BRUSH_FRONTFACE))) ||
ELEM7(brush->sculpt_tool,
SCULPT_TOOL_BLOB,
SCULPT_TOOL_CREASE,
SCULPT_TOOL_DRAW,
SCULPT_TOOL_LAYER,
SCULPT_TOOL_NUDGE,
SCULPT_TOOL_ROTATE,
SCULPT_TOOL_THUMB));
}
/* For the smooth brush, uses the neighboring vertices around vert to calculate
* a smoothed location for vert. Skips corner vertices (used by only one
* polygon.) */
@@ -1366,14 +1403,12 @@ static void do_draw_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
{
SculptSession *ss = ob->sculpt;
Brush *brush = paint_brush(&sd->paint);
float offset[3], area_normal[3];
float offset[3];
float bstrength = ss->cache->bstrength;
int n;
calc_sculpt_normal(sd, ob, area_normal, nodes, totnode);
/* offset with as much as possible factored in already */
mul_v3_v3fl(offset, area_normal, ss->cache->radius);
mul_v3_v3fl(offset, ss->cache->sculpt_normal_symm, ss->cache->radius);
mul_v3_v3(offset, ss->cache->scale);
mul_v3_fl(offset, bstrength);
@@ -1393,7 +1428,8 @@ static void do_draw_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
if (sculpt_brush_test(&test, vd.co)) {
/* offset vertex */
float fade = tex_strength(ss, brush, vd.co, test.dist,
area_normal, vd.no, vd.fno, *vd.mask);
ss->cache->sculpt_normal_symm, vd.no,
vd.fno, *vd.mask);
mul_v3_v3fl(proxy[vd.i], offset, fade);
@@ -1410,16 +1446,14 @@ static void do_crease_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnod
SculptSession *ss = ob->sculpt;
const Scene *scene = ss->cache->vc->scene;
Brush *brush = paint_brush(&sd->paint);
float offset[3], area_normal[3];
float offset[3];
float bstrength = ss->cache->bstrength;
float flippedbstrength, crease_correction;
float brush_alpha;
int n;
calc_sculpt_normal(sd, ob, area_normal, nodes, totnode);
/* offset with as much as possible factored in already */
mul_v3_v3fl(offset, area_normal, ss->cache->radius);
mul_v3_v3fl(offset, ss->cache->sculpt_normal_symm, ss->cache->radius);
mul_v3_v3(offset, ss->cache->scale);
mul_v3_fl(offset, bstrength);
@@ -1450,7 +1484,8 @@ static void do_crease_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnod
if (sculpt_brush_test(&test, vd.co)) {
/* offset vertex */
const float fade = tex_strength(ss, brush, vd.co, test.dist,
area_normal, vd.no, vd.fno, *vd.mask);
ss->cache->sculpt_normal_symm,
vd.no, vd.fno, *vd.mask);
float val1[3];
float val2[3];
@@ -1512,21 +1547,18 @@ static void do_grab_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
SculptSession *ss = ob->sculpt;
Brush *brush = paint_brush(&sd->paint);
float bstrength = ss->cache->bstrength;
float grab_delta[3], an[3];
float grab_delta[3];
int n;
float len;
if (brush->normal_weight > 0 || brush->flag & BRUSH_FRONTFACE)
calc_sculpt_normal(sd, ob, an, nodes, totnode);
copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
len = len_v3(grab_delta);
if (brush->normal_weight > 0) {
mul_v3_fl(an, len * brush->normal_weight);
mul_v3_fl(ss->cache->sculpt_normal_symm, len * brush->normal_weight);
mul_v3_fl(grab_delta, 1.0f - brush->normal_weight);
add_v3_v3(grab_delta, an);
add_v3_v3(grab_delta, ss->cache->sculpt_normal_symm);
}
#pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
@@ -1550,7 +1582,7 @@ static void do_grab_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode)
{
if (sculpt_brush_test(&test, origco[vd.i])) {
const float fade = bstrength * tex_strength(ss, brush, origco[vd.i], test.dist,
an, origno[vd.i], NULL, *vd.mask);
ss->cache->sculpt_normal_symm, origno[vd.i], NULL, *vd.mask);
mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
@@ -1568,16 +1600,13 @@ static void do_nudge_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode
Brush *brush = paint_brush(&sd->paint);
float bstrength = ss->cache->bstrength;
float grab_delta[3];
int n;
float an[3];
float tmp[3], cono[3];
int n;
copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
calc_sculpt_normal(sd, ob, an, nodes, totnode);
cross_v3_v3v3(tmp, an, grab_delta);
cross_v3_v3v3(cono, tmp, an);
cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
#pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
for (n = 0; n < totnode; n++) {
@@ -1593,7 +1622,8 @@ static void do_nudge_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode
{
if (sculpt_brush_test(&test, vd.co)) {
const float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
an, vd.no, vd.fno, *vd.mask);
ss->cache->sculpt_normal_symm,
vd.no, vd.fno, *vd.mask);
mul_v3_v3fl(proxy[vd.i], cono, fade);
@@ -1610,13 +1640,10 @@ static void do_snake_hook_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int to
SculptSession *ss = ob->sculpt;
Brush *brush = paint_brush(&sd->paint);
float bstrength = ss->cache->bstrength;
float grab_delta[3], an[3];
float grab_delta[3];
int n;
float len;
if (brush->normal_weight > 0 || brush->flag & BRUSH_FRONTFACE)
calc_sculpt_normal(sd, ob, an, nodes, totnode);
copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
len = len_v3(grab_delta);
@@ -1625,9 +1652,9 @@ static void do_snake_hook_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int to
negate_v3(grab_delta);
if (brush->normal_weight > 0) {
mul_v3_fl(an, len * brush->normal_weight);
mul_v3_fl(ss->cache->sculpt_normal_symm, len * brush->normal_weight);
mul_v3_fl(grab_delta, 1.0f - brush->normal_weight);
add_v3_v3(grab_delta, an);
add_v3_v3(grab_delta, ss->cache->sculpt_normal_symm);
}
#pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
@@ -1644,7 +1671,8 @@ static void do_snake_hook_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int to
{
if (sculpt_brush_test(&test, vd.co)) {
const float fade = bstrength * tex_strength(ss, brush, vd.co, test.dist,
an, vd.no, vd.fno, *vd.mask);
ss->cache->sculpt_normal_symm,
vd.no, vd.fno, *vd.mask);
mul_v3_v3fl(proxy[vd.i], grab_delta, fade);
@@ -1662,16 +1690,13 @@ static void do_thumb_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode
Brush *brush = paint_brush(&sd->paint);
float bstrength = ss->cache->bstrength;
float grab_delta[3];
int n;
float an[3];
float tmp[3], cono[3];
int n;
copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
calc_sculpt_normal(sd, ob, an, nodes, totnode);
cross_v3_v3v3(tmp, an, grab_delta);
cross_v3_v3v3(cono, tmp, an);
cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
#pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
for (n = 0; n < totnode; n++) {
@@ -1694,7 +1719,8 @@ static void do_thumb_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode
{
if (sculpt_brush_test(&test, origco[vd.i])) {
const float fade = bstrength * tex_strength(ss, brush, origco[vd.i], test.dist,
an, origno[vd.i], NULL, *vd.mask);
ss->cache->sculpt_normal_symm,
origno[vd.i], NULL, *vd.mask);
mul_v3_v3fl(proxy[vd.i], cono, fade);
@@ -1711,20 +1737,17 @@ static void do_rotate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnod
SculptSession *ss = ob->sculpt;
Brush *brush = paint_brush(&sd->paint);
float bstrength = ss->cache->bstrength;
float an[3];
int n;
float m[4][4], rot[4][4], lmat[4][4], ilmat[4][4];
static const int flip[8] = { 1, -1, -1, 1, -1, 1, 1, -1 };
float angle = ss->cache->vertex_rotation * flip[ss->cache->mirror_symmetry_pass];
calc_sculpt_normal(sd, ob, an, nodes, totnode);
unit_m4(m);
unit_m4(lmat);
copy_v3_v3(lmat[3], ss->cache->location);
invert_m4_m4(ilmat, lmat);
axis_angle_to_mat4(rot, an, angle);
axis_angle_to_mat4(rot, ss->cache->sculpt_normal_symm, angle);
mul_serie_m4(m, lmat, rot, ilmat, NULL, NULL, NULL, NULL, NULL);
@@ -1749,7 +1772,8 @@ static void do_rotate_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnod
{
if (sculpt_brush_test(&test, origco[vd.i])) {
const float fade = bstrength * tex_strength(ss, brush, origco[vd.i], test.dist,
an, origno[vd.i], NULL, *vd.mask);
ss->cache->sculpt_normal_symm,
origno[vd.i], NULL, *vd.mask);
mul_v3_m4v3(proxy[vd.i], m, origco[vd.i]);
sub_v3_v3(proxy[vd.i], origco[vd.i]);
@@ -1775,8 +1799,6 @@ static void do_layer_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode
if (bstrength < 0)
lim = -lim;
calc_sculpt_normal(sd, ob, area_normal, nodes, totnode);
mul_v3_v3v3(offset, ss->cache->scale, area_normal);
#pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
@@ -2074,14 +2096,14 @@ static void calc_sculpt_plane(Sculpt *sd, Object *ob, PBVHNode **nodes, int totn
calc_flatten_center(sd, ob, nodes, totnode, fc);
/* for area normal */
copy_v3_v3(ss->cache->last_area_normal, an);
copy_v3_v3(ss->cache->sculpt_normal, an);
/* for flatten center */
copy_v3_v3(ss->cache->last_center, fc);
}
else {
/* for area normal */
copy_v3_v3(an, ss->cache->last_area_normal);
copy_v3_v3(an, ss->cache->sculpt_normal);
/* for flatten center */
copy_v3_v3(fc, ss->cache->last_center);
@@ -2596,6 +2618,9 @@ static void do_brush_action(Sculpt *sd, Object *ob, Brush *brush)
BLI_pbvh_node_mark_update(nodes[n]);
}
if (brush_needs_sculpt_normal(brush))
update_sculpt_normal(sd, ob, nodes, totnode);
/* Apply one type of brush action */
switch (brush->sculpt_tool) {
case SCULPT_TOOL_DRAW: