[Bf-blender-cvs] [baff2c72ce1] PSketch-279: PSketch BBone Fit WIP: Repeat the fitting process multiple times to get a better fit

Joshua Leung noreply at git.blender.org
Tue May 8 18:04:03 CEST 2018


Commit: baff2c72ce18fb12fd21f9baacf25c1e1d8de96d
Author: Joshua Leung
Date:   Wed Jan 3 20:27:07 2018 +1300
Branches: PSketch-279
https://developer.blender.org/rBbaff2c72ce18fb12fd21f9baacf25c1e1d8de96d

PSketch BBone Fit WIP: Repeat the fitting process multiple times to get a better fit

>From watching the traces, it was found that the fitting process could
be improved sometimes by repeating it a few times, especially if the
initial and final states were quite different. This is probably because
the initial handle estimates differed too much from what was needed,
meaning that we need to make it closer before it can work.

===================================================================

M	source/blender/editors/armature/pose_sketch.c

===================================================================

diff --git a/source/blender/editors/armature/pose_sketch.c b/source/blender/editors/armature/pose_sketch.c
index 38e595ca2eb..f46ea129fb8 100644
--- a/source/blender/editors/armature/pose_sketch.c
+++ b/source/blender/editors/armature/pose_sketch.c
@@ -601,44 +601,51 @@ static void psketch_pchan_apply_bbone_from_endpoints(
 	}
 	
 	
-	
-	/* Generate an estimate for the unit "proto" handles the handles
-	 * should be aligned to. As an initial guess, let's use the BBone
-	 * handles, and then invert the "right" (tail) one to face the same
-	 * direction as the bone.
+	/* Repeat the curve fitting process multiple times until the error drops
+	 * to an acceptable level (or we've tried too many times, and it oscillates)
 	 */
-	// TODO: Do we need to include the rest values too?
-	float tan_l[3] = {pchan->curveInX, 0, pchan->curveInY};
-	float tan_r[3] = {pchan->curveOutX, 0, pchan->curveOutY};
-	
-	normalize_v3(tan_l);
-	normalize_v3(tan_r);
-	
-	/* Fit a single segment */
-	float in_handle[3], out_handle[3];
+	const int max_iters = 5;
+	int cur_iter = 0;
 	
-	const float error_threshold = 0.1f; // XXX: Draw Curve regularly uses 0.15
+	const float error_threshold = 0.3f; // XXX: Draw Curve regularly uses 0.15
 	float r_err_squared = 0.0f;
 	unsigned int r_error_index = 0;
 	
-	int result = curve_fit_cubic_to_points_single_fl(
-			points, stroke_len, NULL,
-			3, error_threshold,
-			tan_l, tan_r,
-			in_handle, out_handle,
-			&r_err_squared, &r_error_index);
-	
-	printf("Curve Fit: result = %d, err = %f (%d)\n", result, r_err_squared, r_error_index);
-	if (result == 0) {
-		pchan->curveInX = in_handle[0];
-		pchan->curveInY = in_handle[2];
-		
-		pchan->curveOutX = out_handle[0];
-		pchan->curveOutY = out_handle[2];
-		
-		//pchan->ease1 = in_handle[1];		
-		//pchan->ease2 = out_handle[1];
-	}
+	do {
+		/* Generate an estimate for the unit "proto" handles the handles
+		 * should be aligned to. As an initial guess, let's use the BBone
+		 * handles, and then invert the "right" (tail) one to face the same
+		 * direction as the bone.
+		 */
+		// TODO: Do we need to include the rest values too?
+		float tan_l[3] = {pchan->curveInX, 0, pchan->curveInY};
+		float tan_r[3] = {pchan->curveOutX, 0, pchan->curveOutY};
+		
+		normalize_v3(tan_l);
+		normalize_v3(tan_r);
+		
+		/* Fit a single segment */
+		float in_handle[3], out_handle[3];
+		
+		int result = curve_fit_cubic_to_points_single_fl(
+				points, stroke_len, NULL,
+				3, error_threshold,
+				tan_l, tan_r,
+				in_handle, out_handle,
+				&r_err_squared, &r_error_index);
+		
+		printf("Curve Fit (i=%d): result = %d, err = %f (%d)\n", cur_iter, result, r_err_squared, r_error_index);
+		if (result == 0) {
+			pchan->curveInX = in_handle[0];
+			pchan->curveInY = in_handle[2];
+			
+			pchan->curveOutX = out_handle[0];
+			pchan->curveOutY = out_handle[2];
+			
+			//pchan->ease1 = in_handle[1];		
+			//pchan->ease2 = out_handle[1];
+		}
+	} while ((r_err_squared > 1.0f) && (cur_iter++ < max_iters));
 	
 	/* free data */
 	MEM_freeN(points);



More information about the Bf-blender-cvs mailing list