[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [46649] trunk/blender/source/blender/ editors/sculpt_paint/sculpt.c: Code cleanup: refactor sculpt normal and related functions.

Nicholas Bishop nicholasbishop at gmail.com
Tue May 15 06:44:20 CEST 2012


Revision: 46649
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=46649
Author:   nicholasbishop
Date:     2012-05-15 04:44:20 +0000 (Tue, 15 May 2012)
Log Message:
-----------
Code cleanup: refactor sculpt normal and related functions.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/sculpt_paint/sculpt.c

Modified: trunk/blender/source/blender/editors/sculpt_paint/sculpt.c
===================================================================
--- trunk/blender/source/blender/editors/sculpt_paint/sculpt.c	2012-05-15 04:44:13 UTC (rev 46648)
+++ trunk/blender/source/blender/editors/sculpt_paint/sculpt.c	2012-05-15 04:44:20 UTC (rev 46649)
@@ -247,7 +247,13 @@
 	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 @@
 	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;
+	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_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_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_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);
+		case SCULPT_DISP_DIR_AREA:
+			calc_area_normal(sd, ob, an, nodes, totnode);
 
-			default:
-				break;
-		}
+		default:
+			break;
+	}
+}
 
-		copy_v3_v3(ss->cache->last_area_normal, an);
+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)))
+	{
+		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 @@
 {
 	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 @@
 			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 @@
 	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 @@
 			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 @@
 	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 @@
 		{
 			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,17 +1600,14 @@
 	Brush *brush = paint_brush(&sd->paint);
 	float bstrength = ss->cache->bstrength;
 	float grab_delta[3];
+	float tmp[3], cono[3];
 	int n;
-	float an[3];
-	float tmp[3], cono[3];
 
 	copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
 
-	calc_sculpt_normal(sd, ob, an, nodes, totnode);
+	cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
+	cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
 
-	cross_v3_v3v3(tmp, an, grab_delta);
-	cross_v3_v3v3(cono, tmp, an);
-
 	#pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
 	for (n = 0; n < totnode; n++) {
 		PBVHVertexIter vd;
@@ -1593,7 +1622,8 @@
 		{
 			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 @@
 	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 @@
 		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 @@
 		{
 			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,17 +1690,14 @@
 	Brush *brush = paint_brush(&sd->paint);
 	float bstrength = ss->cache->bstrength;
 	float grab_delta[3];
+	float tmp[3], cono[3];
 	int n;
-	float an[3];
-	float tmp[3], cono[3];
 
 	copy_v3_v3(grab_delta, ss->cache->grab_delta_symmetry);
 
-	calc_sculpt_normal(sd, ob, an, nodes, totnode);
+	cross_v3_v3v3(tmp, ss->cache->sculpt_normal_symm, grab_delta);
+	cross_v3_v3v3(cono, tmp, ss->cache->sculpt_normal_symm);
 
-	cross_v3_v3v3(tmp, an, grab_delta);
-	cross_v3_v3v3(cono, tmp, an);
-
 	#pragma omp parallel for schedule(guided) if (sd->flags & SCULPT_USE_OPENMP)
 	for (n = 0; n < totnode; n++) {
 		PBVHVertexIter vd;
@@ -1694,7 +1719,8 @@

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list