[Bf-blender-cvs] [4d54b49] temp-curve-draw: Add orig-index argument to map the output curve back to the input data.

Campbell Barton noreply at git.blender.org
Wed Apr 13 21:20:07 CEST 2016


Commit: 4d54b4950de6fc78ac6f8bc3f13520772076bc39
Author: Campbell Barton
Date:   Thu Apr 14 05:19:33 2016 +1000
Branches: temp-curve-draw
https://developer.blender.org/rB4d54b4950de6fc78ac6f8bc3f13520772076bc39

Add orig-index argument to map the output curve back to the input data.

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

M	extern/curve_fit_nd/curve_fit_nd.h
M	extern/curve_fit_nd/intern/curve_fit_cubic.c
M	source/blender/editors/curve/editcurve_paint.c

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

diff --git a/extern/curve_fit_nd/curve_fit_nd.h b/extern/curve_fit_nd/curve_fit_nd.h
index 3e39ac1..ad1588b 100644
--- a/extern/curve_fit_nd/curve_fit_nd.h
+++ b/extern/curve_fit_nd/curve_fit_nd.h
@@ -64,6 +64,7 @@ int spline_fit_cubic_to_points_db(
         const unsigned int  corners_len,
 
         double **r_cubic_array, unsigned int *r_cubic_array_len,
+        uint  **r_cubic_orig_index,
         unsigned int **r_corner_index_array, unsigned int *r_corner_index_len);
 
 int spline_fit_cubic_to_points_fl(
@@ -75,6 +76,7 @@ int spline_fit_cubic_to_points_fl(
         const unsigned int  corners_len,
 
         float **r_cubic_array, unsigned int *r_cubic_array_len,
+        uint  **r_cubic_orig_index,
         unsigned int **r_corners_index_array, unsigned int *r_corners_index_len);
 
 
diff --git a/extern/curve_fit_nd/intern/curve_fit_cubic.c b/extern/curve_fit_nd/intern/curve_fit_cubic.c
index 2989bb6..8590d7f 100644
--- a/extern/curve_fit_nd/intern/curve_fit_cubic.c
+++ b/extern/curve_fit_nd/intern/curve_fit_cubic.c
@@ -42,6 +42,10 @@
 /* avoid re-calculating lengths multiple times */
 #define USE_LENGTH_CACHE
 
+/* store the indices in the cubic data so we can return the original indices,
+ * useful when the caller has data assosiated with the curve. */
+#define USE_ORIG_INDEX_DATA
+
 typedef unsigned int uint;
 
 #include "curve_fit_inline.h"
@@ -76,6 +80,9 @@ typedef unsigned int uint;
 typedef struct Cubic {
 	/* single linked lists */
 	struct Cubic *next;
+#ifdef USE_ORIG_INDEX_DATA
+	uint orig_span;
+#endif
 	/* 0: point_0, 1: handle_0, 2: handle_1, 3: point_1,
 	 * each one is offset by 'dims' */
 	double pt_data[0];
@@ -140,15 +147,27 @@ static void cubic_list_prepend(CubicList *clist, Cubic *cubic)
 	clist->len++;
 }
 
-static double *cubic_list_as_array(const CubicList *clist)
+static double *cubic_list_as_array(
+        const CubicList *clist
+#ifdef USE_ORIG_INDEX_DATA
+        ,
+        const uint index_last,
+        uint *r_orig_index
+#endif
+        )
 {
 	const uint dims = clist->dims;
 	const uint array_flat_len = (clist->len + 1) * 3 * dims;
 
 	double *array = malloc(sizeof(double) * array_flat_len);
-
 	const double *handle_prev = &((Cubic *)clist->items)->pt_data[dims];
 
+#ifdef USE_ORIG_INDEX_DATA
+	uint orig_index_value = index_last;
+	uint orig_index_index = clist->len;
+	bool use_orig_index = (r_orig_index != NULL);
+#endif
+
 	/* fill the array backwards */
 	const size_t array_chunk = 3 * dims;
 	double *array_iter = array + array_flat_len;
@@ -157,8 +176,24 @@ static double *cubic_list_as_array(const CubicList *clist)
 		memcpy(array_iter, &citer->pt_data[2 * dims], sizeof(double) * 2 * dims);
 		memcpy(&array_iter[2 * dims], &handle_prev[dims], sizeof(double) * dims);
 		handle_prev = citer->pt_data;
+
+#ifdef USE_ORIG_INDEX_DATA
+		if (use_orig_index) {
+			r_orig_index[orig_index_index--] = orig_index_value;
+			orig_index_value -= citer->orig_span;
+		}
+#endif
 	}
 
+#ifdef USE_ORIG_INDEX_DATA
+	if (use_orig_index) {
+		assert(orig_index_index == 0);
+		assert(orig_index_value == 0 || index_last == 0);
+		r_orig_index[orig_index_index] = index_last ? orig_index_value : 0;
+
+	}
+#endif
+
 	/* flip tangent for first and last (we could leave at zero, but set to something useful) */
 
 	/* first */
@@ -437,6 +472,9 @@ static void cubic_from_points(
 	copy_vnvn(CUBIC_PT(r_cubic, 0, dims), p0, dims);
 	copy_vnvn(CUBIC_PT(r_cubic, 3, dims), p3, dims);
 
+#ifdef USE_ORIG_INDEX_DATA
+	r_cubic->orig_span = (points_offset_len - 1);
+#endif
 
 	/* p1 = p0 - (tan_l * alpha_l);
 	 * p2 = p3 + (tan_r * alpha_r);
@@ -675,6 +713,10 @@ static void fit_cubic_to_points(
 		msub_vn_vnvn_fl(p1, p0, tan_l, dist, dims);
 		madd_vn_vnvn_fl(p2, p3, tan_r, dist, dims);
 
+#ifdef USE_ORIG_INDEX_DATA
+		cubic->orig_span = 1;
+#endif
+
 		cubic_list_prepend(clist, cubic);
 		return;
 	}
@@ -802,6 +844,7 @@ int spline_fit_cubic_to_points_db(
         uint          corners_len,
 
         double **r_cubic_array, uint *r_cubic_array_len,
+        uint **r_cubic_orig_index,
         uint **r_corner_index_array, uint *r_corner_index_len)
 {
 	uint corners_buf[2];
@@ -894,12 +937,31 @@ int spline_fit_cubic_to_points_db(
 	}
 #endif
 
+#ifdef USE_ORIG_INDEX_DATA
+	uint *cubic_orig_index = NULL;
+	if (r_cubic_orig_index) {
+		cubic_orig_index = malloc(sizeof(uint) * (clist.len + 1));
+	}
+#else
+	*r_cubic_orig_index = NULL;
+#endif
+
 	/* allocate a contiguous array and free the linked list */
-	*r_cubic_array = cubic_list_as_array(&clist);
+	*r_cubic_array = cubic_list_as_array(
+	        &clist
+#ifdef USE_ORIG_INDEX_DATA
+	        , corners[corners_len - 1], cubic_orig_index
+#endif
+	        );
 	*r_cubic_array_len = clist.len + 1;
 
 	cubic_list_clear(&clist);
 
+#ifdef USE_ORIG_INDEX_DATA
+	if (cubic_orig_index) {
+		*r_cubic_orig_index = cubic_orig_index;
+	}
+#endif
 
 	if (corner_index_array) {
 		assert(corner_index == corners_len);
@@ -922,6 +984,7 @@ int spline_fit_cubic_to_points_fl(
         const uint    corners_len,
 
         float **r_cubic_array, uint *r_cubic_array_len,
+        uint **r_cubic_orig_index,
         uint **r_corner_index_array, uint *r_corner_index_len)
 {
 	const uint points_flat_len = points_len * dims;
@@ -938,6 +1001,7 @@ int spline_fit_cubic_to_points_fl(
 	int result = spline_fit_cubic_to_points_db(
 	        points_db, points_len, dims, error_threshold, corners, corners_len,
 	        &cubic_array_db, &cubic_array_len,
+	        r_cubic_orig_index,
 	        r_corner_index_array, r_corner_index_len);
 	free(points_db);
 
diff --git a/source/blender/editors/curve/editcurve_paint.c b/source/blender/editors/curve/editcurve_paint.c
index cb2f22e..a24a04a 100644
--- a/source/blender/editors/curve/editcurve_paint.c
+++ b/source/blender/editors/curve/editcurve_paint.c
@@ -429,6 +429,8 @@ static int curve_draw_exec(bContext *C, wmOperator *op)
 			        &corners, &corners_len);
 		}
 
+		unsigned int *cubic_orig_index = NULL;
+
 		unsigned int *corners_index = NULL;
 		unsigned int  corners_index_len = 0;
 
@@ -436,6 +438,7 @@ static int curve_draw_exec(bContext *C, wmOperator *op)
 		        (const float *)coords, stroke_len, DIMS, error_threshold,
 		        corners, corners_len,
 		        &cubic_spline, &cubic_spline_len,
+		        &cubic_orig_index,
 		        &corners_index, &corners_index_len);
 		MEM_freeN(coords);
 		if (corners) {
@@ -477,6 +480,10 @@ static int curve_draw_exec(bContext *C, wmOperator *op)
 			}
 		}
 
+		if (cubic_orig_index) {
+			free(cubic_orig_index);
+		}
+
 		if (corners_index) {
 			free(corners_index);
 		}




More information about the Bf-blender-cvs mailing list