[Bf-blender-cvs] [398980c5c78] PSketch-279: WIP - PSketch Direct support for Bendy Bones - curve_fit_nd wrapper

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


Commit: 398980c5c7848ba40bcf699acea82fd1fa2138ac
Author: Joshua Leung
Date:   Wed Jan 3 15:43:40 2018 +1300
Branches: PSketch-279
https://developer.blender.org/rB398980c5c7848ba40bcf699acea82fd1fa2138ac

WIP - PSketch Direct support for Bendy Bones - curve_fit_nd wrapper

First steps to try and get working pose-sketching support for Bendy Bones.
This commit copies over the core stuff needed to pass the stroke data
over to the Bezier fitting code used by the "Draw Curve" operator
(editcurve_paint.c).  It's not currently working though, as it produces curves
with too many points!

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

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

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

diff --git a/source/blender/editors/armature/CMakeLists.txt b/source/blender/editors/armature/CMakeLists.txt
index ebdf1a778bb..fbaf90bbb49 100644
--- a/source/blender/editors/armature/CMakeLists.txt
+++ b/source/blender/editors/armature/CMakeLists.txt
@@ -30,6 +30,7 @@ set(INC
 	../../../../intern/guardedalloc
 	../../../../intern/eigen
 	../../../../intern/glew-mx
+	../../../../extern/curve_fit_nd
 )
 
 set(INC_SYS
diff --git a/source/blender/editors/armature/pose_sketch.c b/source/blender/editors/armature/pose_sketch.c
index d56b26e6ae0..414352aacf8 100644
--- a/source/blender/editors/armature/pose_sketch.c
+++ b/source/blender/editors/armature/pose_sketch.c
@@ -73,6 +73,8 @@
 #include "WM_api.h"
 #include "WM_types.h"
 
+#include "curve_fit_nd.h"
+
 /* ***************************************************** */
 /* Constants and Defines */
 
@@ -541,6 +543,94 @@ static void psketch_pchan_apply_from_endpoints(PSketchSettings *ps, Scene *scene
 
 /* ---------------------------------------------------------------- */
 
+/* compute bendy bone shape from sketched line */
+static void psketch_pchan_apply_bbone_from_endpoints(
+        PSketchSettings *ps, Scene *scene, Object *ob,
+        bPoseChannel *pchan, const size_t chain_idx,
+        const tGPStrokePosePoint *p1, const tGPStrokePosePoint *p2,
+        const bGPDstroke *stroke)
+{
+	/* Compute transform needed to convert from stroke point coordinates
+	 * to bone-relative coordinates (bone local space)
+	 */
+	float mat[4][4], imat[4][4]; 
+	
+	mul_m4_m4m4(mat, ob->obmat, pchan->bone->arm_mat);
+	invert_m4_m4(imat, mat);
+	
+	/* Convert input stroke data to a coordinates array (for fitting) */
+	bGPDspoint *start_pt = NULL;
+	int stroke_len = 0;
+	float *points = NULL;
+	
+	if (p1->index > p2->index) {
+		/* Reversed bone/stroke - p2 follows p1 */
+		start_pt = &stroke->points[p2->index];
+		stroke_len = (p1->index - p2->index);
+		printf("R stroke_len = %d (%d -> %d)\n", stroke_len, p1->index, p2->index);
+	}
+	else {
+		/* p2 follows p1 in normal (forward) order */
+		start_pt = &stroke->points[p1->index];
+		stroke_len = (p2->index - p1->index);
+		printf("stroke_len = %d (%d -> %d)\n", stroke_len, p1->index, p2->index);	
+	}
+	
+	points = MEM_mallocN(sizeof(*points) * stroke_len * 3, __func__);
+	
+	for (size_t i = 0; i < stroke_len; i++) {
+		bGPDspoint *pt = start_pt + i;
+		float v[3];
+		
+		copy_v3_v3(v, &pt->x);
+		mul_mat3_m4_v3(imat, v);
+		
+		copy_v3_v3(points + (3 * i), v);
+	}
+	
+	
+	/* Try to fit a bezier curve to the points
+	 * NOTE: We use the "cubic_to_points" version and not the
+	 * "handles for single segment" one, as for the latter, we'd
+	 * also need to already know what we want the tangents to be
+	 * (i.e. what we're trying to find out here!)
+	 */
+	const float error_threshold = 0.2f; // XXX: Draw Curve regularly uses 0.15
+	const float corner_angle = DEG2RADF(70.0f);
+	
+	float       *cubic_spline = NULL;
+	unsigned int cubic_spline_len = 0;
+	
+	unsigned int *corners_index = NULL; // XXX: no need for the corners stuff...
+	unsigned int  corners_index_len = 0;
+	
+	unsigned int  calc_flag = CURVE_FIT_CALC_HIGH_QUALIY;
+	
+	int result = curve_fit_cubic_to_points_refit_fl(
+	        points, stroke_len, 3,
+	        error_threshold, calc_flag,
+	        NULL, 0, corner_angle,
+	        &cubic_spline, &cubic_spline_len,
+	        NULL,
+	        &corners_index, &corners_index_len);
+	
+	/* Apply handle settings to curve */
+	if (result == 0) {
+		printf("cubic_spline_len = %d, corners_index_len = %d\n", cubic_spline_len, corners_index_len);
+	}
+	else {
+		printf("couldn't solve bone\n");
+	}
+	
+	/* free data */
+	MEM_freeN(points);
+	
+	if (corners_index) free(corners_index);
+	if (cubic_spline)  free(cubic_spline);
+}
+
+/* ---------------------------------------------------------------- */
+
 /* Perform auto keying on chain of bones */
 /* XXX: This is practically the same as poseAnim_mapping_autoKeyframe() again,
  *      except we use a slightly different way of mapping bones to dsources...
@@ -721,10 +811,19 @@ static int psketch_direct_exec(bContext *C, wmOperator *op)
 		tGPStrokePosePoint *p1 = &spoints[i];
 		tGPStrokePosePoint *p2 = &spoints[i + 1];
 		
+		/* straight-line alignment */
 		psketch_pchan_apply_from_endpoints(ps, scene, ob,
 		                                   pchan, i,
 		                                   p1->co, p2->co,
                                            cfra);
+		
+		/* bendy bone alignment */
+		if ((pchan->bone) && (pchan->bone->segments > 1)) {
+			psketch_pchan_apply_bbone_from_endpoints(ps, scene, ob,
+			                                         pchan, i,
+			                                         p1, p2,
+			                                         stroke);
+		}
 	}



More information about the Bf-blender-cvs mailing list