[Bf-blender-cvs] [e9986e9] soc-2016-uv_tools: Refactoring of NFP code

Phil Gosch noreply at git.blender.org
Thu Jul 14 12:04:08 CEST 2016


Commit: e9986e9140576fc749f40ee9e744adf52fff5573
Author: Phil Gosch
Date:   Thu Jul 14 12:03:38 2016 +0200
Branches: soc-2016-uv_tools
https://developer.blender.org/rBe9986e9140576fc749f40ee9e744adf52fff5573

Refactoring of NFP code

This fixes some bugs, memory leaks and crashes

Note: Added IFP corners as possible placement positions for now, mainly for testing purposes

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

M	source/blender/editors/uvedit/uvedit_parametrizer.c

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

diff --git a/source/blender/editors/uvedit/uvedit_parametrizer.c b/source/blender/editors/uvedit/uvedit_parametrizer.c
index abf0d1c..2f7c315 100644
--- a/source/blender/editors/uvedit/uvedit_parametrizer.c
+++ b/source/blender/editors/uvedit/uvedit_parametrizer.c
@@ -4786,6 +4786,50 @@ void param_pack(ParamHandle *handle, float margin, bool do_rotate)
 		param_scale(handle, phandle->aspx, phandle->aspy);
 }
 
+/* qsort function - sort largest to smallest */
+static int vert_anglesort(const void *p1, const void *p2)
+{
+	const PVert **v1 = p1, **v2 = p2;
+	const float a1 = (*v1)->edge->u.horizontal_angle;
+	const float a2 = (*v2)->edge->u.horizontal_angle;
+
+	if (a1 > a2) return -1;
+	else if (a1 < a2) return  1;
+	return 0;
+}
+
+/* qsort function - sort largest to smallest */
+static int chart_areasort(const void *p1, const void *p2)
+{
+	const PChart **c1 = p1, **c2 = p2;
+	const float a1 = (*c1)->u.ipack.area;
+	const float a2 = (*c2)->u.ipack.area;
+
+	if (a1 > a2) return -1;
+	else if (a1 < a2) return  1;
+	return 0;
+}
+
+/* ToDo SaphireS: Make this function part of math_vector.c */
+static float p_edge_horizontal_angle(PVert *a, PVert *b)
+{
+	float angle = 0.0f;
+	float hori[2];
+	hori[1] = a->uv[1];
+
+	if (a->uv[1] > b->uv[1] && !(compare_ff(a->uv[1], b->uv[1], FLT_EPSILON))) {
+		hori[0] = a->uv[0] - 1.0f;
+		angle = angle_v2v2v2(b->uv, a->uv, hori);
+		angle += M_PI;
+	}
+	else {
+		hori[0] = a->uv[0] + 1.0f;
+		angle = angle_v2v2v2(b->uv, a->uv, hori);
+	}
+
+	return angle;
+}
+
 /* ToDo SaphireS: Put ConvexHull/NFP stuff in own file*/
 PConvexHull *p_convex_hull_new(PChart *chart)
 {
@@ -4816,6 +4860,11 @@ PConvexHull *p_convex_hull_new(PChart *chart)
 	return conv_hull;
 }
 
+void p_convex_hull_update(PChart *chart)
+{
+	p_chart_uv_bbox(chart, chart->u.ipack.convex_hull->min_v, chart->u.ipack.convex_hull->max_v);
+}
+
 void p_convex_hull_delete(PConvexHull *c_hull)
 {
 	MEM_freeN(c_hull->h_verts);
@@ -4848,54 +4897,10 @@ bool p_convex_hull_intersect(PConvexHull *chull_a, PConvexHull *chull_b)
 	return false;
 }
 
-/* qsort function - sort largest to smallest */
-static int vert_anglesort(const void *p1, const void *p2)
-{
-	const PVert **v1 = p1, **v2 = p2;
-	const float a1 = (*v1)->edge->u.horizontal_angle;
-	const float a2 = (*v2)->edge->u.horizontal_angle;
-
-	if		(a1 > a2) return -1;
-	else if (a1 < a2) return  1;
-	return 0;
-}
-
-/* qsort function - sort largest to smallest */
-static int chart_areasort(const void *p1, const void *p2)
-{
-	const PChart **c1 = p1, **c2 = p2;
-	const float a1 = (*c1)->u.ipack.area;
-	const float a2 = (*c2)->u.ipack.area;
-
-	if      (a1 > a2) return -1;
-	else if (a1 < a2) return  1;
-	return 0;
-}
-
-/* ToDo SaphireS: Make this function part of math_vector.c */
-static float p_edge_horizontal_angle(PVert *a, PVert *b)
-{
-	float angle = 0.0f;
-	float hori[2]; 
-	hori[1] = a->uv[1];
-
-	if (a->uv[1] > b->uv[1] && !(compare_ff(a->uv[1], b->uv[1], FLT_EPSILON))) {
-		hori[0] = a->uv[0] - 1.0f;
-		angle = angle_v2v2v2(b->uv, a->uv, hori);
-		angle += M_PI;
-	}
-	else {
-		hori[0] = a->uv[0] + 1.0f;
-		angle = angle_v2v2v2(b->uv, a->uv, hori);
-	}
-
-	return angle;
-}
-
 void p_convex_hull_compute_horizontal_angles(PConvexHull *hull)
 {
 	int j;
-	printf("p_convex_hull_compute_horizontal_angles\n");
+	printf("*p_convex_hull_compute_horizontal_angles\n");
 	for (j = 0; j < hull->nverts; j++) {
 		/* DEBUG */
 		//printf("--PVert of convex hull - x: %f, y: %f\n", hull->h_verts[j]->uv[0], hull->h_verts[j]->uv[1]);
@@ -4919,7 +4924,7 @@ void p_convex_hull_compute_horizontal_angles(PConvexHull *hull)
 void p_convex_hull_compute_edge_components(PConvexHull *hull)
 {
 	int j;
-	printf("p_convex_hull_compute_edge_lengths\n");
+	printf("*p_convex_hull_compute_edge_lengths\n");
 	for (j = 0; j < hull->nverts; j++) {
 		/* DEBUG */
 		//printf("--PVert of convex hull - x: %f, y: %f\n", hull->h_verts[j]->uv[0], hull->h_verts[j]->uv[1]);
@@ -4944,10 +4949,6 @@ PConvexHull *p_convex_hull_reverse_vert_order(PConvexHull *hull)
 	conv_hull_inv->nverts = hull->nverts;
 	conv_hull_inv->h_verts = (PVert **)MEM_mallocN(sizeof(PVert *) * conv_hull_inv->nverts, "PConvexHullInversePoints");
 	conv_hull_inv->right = hull->right;
-	conv_hull_inv->min_v[0] = hull->min_v[0];
-	conv_hull_inv->min_v[1] = hull->min_v[1];
-	conv_hull_inv->max_v[0] = hull->max_v[0];
-	conv_hull_inv->max_v[1] = hull->max_v[1];
 	conv_hull_inv->placed = false;
 	int i, j;
 	float miny = 1.0e30f;
@@ -4956,13 +4957,19 @@ PConvexHull *p_convex_hull_reverse_vert_order(PConvexHull *hull)
 	for (j = 0; j < hull->nverts; j++) {
 		conv_hull_inv->h_verts[j] = hull->h_verts[hull->nverts - (j + 1)];
 	}
+	
+	INIT_MINMAX2(conv_hull_inv->min_v, conv_hull_inv->max_v);
 
+	/* reference vertex, for inverse winding direction that's the one with lowest y value */
 	for (i = 0; i < conv_hull_inv->nverts; i++) {
 		if (conv_hull_inv->h_verts[i]->uv[1] < miny) {
 			miny = conv_hull_inv->h_verts[i]->uv[1];
-			printf("--p_convex_hull_reverse_vert_order: min_y = %f\n", miny);
+			printf("--p_convex_hull_reverse_vert_order: min_y : x = %f, y = %f\n", conv_hull_inv->h_verts[i]->uv[0], miny);
 			conv_hull_inv->ref_vert_index = i;
 		}
+
+		/* compute bounds */
+		minmax_v2v2_v2(conv_hull_inv->min_v, conv_hull_inv->max_v, conv_hull_inv->h_verts[i]->uv);
 	}
 
 	printf("--p_convex_hull_reverse_vert_order: FINAL min_y = %f\n", miny);
@@ -5063,7 +5070,6 @@ bool p_point_inside_nfp(PNoFitPolygon *nfp, float p[2])
 
 bool p_temp_cfr_check(PNoFitPolygon **nfps, PNoFitPolygon *ifp, float p[2], int nfp_count, int index)
 {
-	float min[2], max[2];
 	int i;
 
 	/* Make sure point is inside IFP */
@@ -5071,6 +5077,7 @@ bool p_temp_cfr_check(PNoFitPolygon **nfps, PNoFitPolygon *ifp, float p[2], int
 		p[1] < ifp->final_pos[0]->y ||
 		p[0] > ifp->final_pos[2]->x ||
 		p[1] > ifp->final_pos[2]->y) {
+		printf("--end_pos outside IFP!\n");
 		return false;
 	}
 	
@@ -5095,7 +5102,7 @@ PNoFitPolygon *p_no_fit_polygon_create(PConvexHull *item, PConvexHull *fixed)
 	nfp->nverts = item->nverts + fixed->nverts;
 	PVert **points = (PVert **)MEM_mallocN(sizeof(PVert *) * nfp->nverts, "PNFPPoints");
 	nfp->final_pos = (PPointUV **)MEM_callocN(sizeof(*nfp->final_pos) * nfp->nverts, "PNFPFinalPos");
-	int i, j, offset = 0;
+	int i, j, offset_item = 0, offset_fixed = 0;
 
 	/* Assign verts of hulls to NFP */
 	for (i = 0; i < nfp->nverts; i++) {
@@ -5106,11 +5113,11 @@ PNoFitPolygon *p_no_fit_polygon_create(PConvexHull *item, PConvexHull *fixed)
 			points[i] = fixed->h_verts[i - item->nverts];
 		}
 	}
-	printf("Assignment to points done\n");
+	printf("-Assignment to points done\n");
 
 	/* sort edges according to horizontal angle, biggest to smallest */
 	qsort(points, (size_t)nfp->nverts, sizeof(PVert *), vert_anglesort);
-	printf("Sorting done\n");
+	printf("-Sorting done\n");
 
 	for (i = 0; i < nfp->nverts; i++) {
 		printf("-- horizontal angle of points[%i]: %f\n", i, points[i]->edge->u.horizontal_angle);
@@ -5125,28 +5132,48 @@ PNoFitPolygon *p_no_fit_polygon_create(PConvexHull *item, PConvexHull *fixed)
 		if (compare_ff(points[j]->uv[0], item->h_verts[item->ref_vert_index]->uv[0], FLT_EPSILON)
 			&& compare_ff(points[j]->uv[1], item->h_verts[item->ref_vert_index]->uv[1], FLT_EPSILON)) {
 			/* offset */
-			printf("Found item min y, offset = %i\n", j);
-			offset = j;
-			break;
+			printf("-Found item ref vert, offset = %i\n", j);
+			offset_item = j;
+		}
+
+		if (compare_ff(points[j]->uv[0], fixed->h_verts[fixed->ref_vert_index]->uv[0], FLT_EPSILON)
+			&& compare_ff(points[j]->uv[1], fixed->h_verts[fixed->ref_vert_index]->uv[1], FLT_EPSILON)) {
+			/* offset */
+			printf("-Found fixed ref vert, offset = %i\n", j);
+			offset_fixed = j;
 		}
 	}
 
 	/* Minkowski sum computation */
-	printf("PPointUV creation started!\n");
+	printf("-PPointUV creation started!\n");
 	PPointUV *p = (PPointUV *)MEM_callocN(sizeof(*p), "PPointUV");
 	p->x = fixed->h_verts[fixed->ref_vert_index]->uv[0];
 	p->y = fixed->h_verts[fixed->ref_vert_index]->uv[1];
-
 	nfp->final_pos[0] = p;
+
 	for (j = 1; j < nfp->nverts; j++) {
 		PPointUV *p1 = (PPointUV *)MEM_callocN(sizeof(*p1), "PPointUV1");
-		p1->x = nfp->final_pos[j - 1]->x + points[j - 1]->u.delta_edge[0];
+		/*p1->x = nfp->final_pos[j - 1]->x + points[j - 1]->u.delta_edge[0];
 		p1->y = nfp->final_pos[j - 1]->y + points[j - 1]->u.delta_edge[1];
-		nfp->final_pos[j] = p1;
+		nfp->final_pos[j] = p1;*/
+
+		if ((j + offset_fixed) <= nfp->nverts) {
+			p1->x = nfp->final_pos[j - 1]->x + points[j - 1 + offset_fixed]->u.delta_edge[0];
+			p1->y = nfp->final_pos[j - 1]->y + points[j - 1 + offset_fixed]->u.delta_edge[1];
+			nfp->final_pos[j] = p1;
+		}
+		else {
+			/* j - 1 */
+			p1->x = nfp->final_pos[j - 1]->x + points[(j + offset_fixed) - nfp->nverts]->u.delta_edge[0];
+			p1->y = nfp->final_pos[j - 1]->y + points[(j + offset_fixed) - nfp->nverts]->u.delta_edge[1];
+			nfp->final_pos[j] = p1;
+		}
 	}
 
+	/* temp, do this at assignment */
 	for (j = 0; j < nfp->nverts; j++) {
-		printf("-NFP Vert: x: %f, y: %f\n", nfp->final_pos[j]->x, nfp->final_pos[j]->y);
+
+		printf("--NFP Vert: x: %f, y: %f\n", nfp->final_pos[j]->x, nfp->final_pos[j]->y);
 	}
 
 	/* free memory */
@@ -5173,10 +5200,10 @@ void p_place_chart(PChart* item, PConvexHull *ch_item,  PPointUV *pos)
 	cur_pos[0] = ch_item->h_verts[ch_item->ref_vert_index]->uv[0];
 	cur_pos[1] = ch_item->h_verts[ch_item->ref_vert_index]->uv[1];
 
-	printf("ref_vert x: %f\n", cur_pos[0]);
-	printf("ref_vert y: %f\n", cur_pos[1]);
-	printf("end_pos x: %f\n", pos->x);
-	printf("end_pos y: %f\n", pos->y);
+	printf("-ref_vert x: %f\n", cur_pos[0]);
+	printf("-ref_vert y: %f\n", cur_pos[1]);
+	printf("-end_pos x: %f\n", pos->x);
+	printf("-end_pos y: %f\n", pos->y);
 
 	trans[0] = cur_pos[0] - pos->x;
 	trans[1] = cur_pos[1] - pos->y;
@@ -5185,7 +5212,7 @@ void p_place_chart(PChart* item, PConvexHull *ch_item,  PPointUV *pos)
 	trans[1] = -trans[1];
 
 	p_chart_uv_translate(item, trans);
-	printf("translation done!\n");
+	printf("-translation done!\n");
 }
 
 bool p_chart_pack_individual(PHandle *phandle,  PChart *item)
@@ -5224,6 +5251,7 @@ bool p_chart_pack_individual(PHandle *phandle,  PChart *item)
 	/* com

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list