[Bf-blender-cvs] [1829199] soc-2016-uv_tools: Refactor of code, taking into account margin and storing the convex hull

Phil Gosch noreply at git.blender.org
Fri Jul 29 14:46:00 CEST 2016


Commit: 1829199887d2e4f406d47487aafabbd820b0c551
Author: Phil Gosch
Date:   Fri Jul 29 14:45:46 2016 +0200
Branches: soc-2016-uv_tools
https://developer.blender.org/rB1829199887d2e4f406d47487aafabbd820b0c551

Refactor of code, taking into account margin and storing the convex hull

Warning: Intermediate commit, this currently breaks correct packing solution computation

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

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

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

diff --git a/source/blender/editors/uvedit/uvedit_parametrizer.c b/source/blender/editors/uvedit/uvedit_parametrizer.c
index ba4f2aa..b6bac0c 100644
--- a/source/blender/editors/uvedit/uvedit_parametrizer.c
+++ b/source/blender/editors/uvedit/uvedit_parametrizer.c
@@ -159,6 +159,8 @@ typedef struct PFace {
 typedef struct PPointUV{
 	float x;
 	float y;
+	float angle;			/* irregular packing */
+	float delta_edge[2];	/* irregular packing */
 } PPointUV;
 
 typedef struct PConvexHull {
@@ -4841,6 +4843,18 @@ static int vert_anglesort(const void *p1, const void *p2)
 }
 
 /* qsort function - sort largest to smallest */
+static int point_anglesort(const void *p1, const void *p2)
+{
+	const PPointUV **v1 = p1, **v2 = p2;
+	const float a1 = (*v1)->angle;
+	const float a2 = (*v2)->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;
@@ -4872,6 +4886,28 @@ static float p_edge_horizontal_angle(PVert *a, PVert *b)
 	return angle;
 }
 
+static float p_edge_horizontal_angle_ppointuv(PPointUV *a, PPointUV *b)
+{
+	float angle = 0.0f;
+	float hori[2], pa[2], pb[2];
+	hori[1] = a->y;
+	pa[0] = a->x;
+	pa[1] = a->y;
+	pb[0] = b->x;
+	pb[1] = b->y;
+	if (a->y > b->y && !(compare_ff(a->y, b->y, FLT_EPSILON))) {
+		hori[0] = a->x - 1.0f;
+		angle = angle_v2v2v2(pb, pa, hori);
+		angle += M_PI;
+	}
+	else {
+		hori[0] = a->x + 1.0f;
+		angle = angle_v2v2v2(pb, pa, hori);
+	}
+
+	return angle;
+}
+
 /* ToDo SaphireS: Put ConvexHull/NFP stuff in own file*/
 PConvexHull *p_convex_hull_new(PChart *chart)
 {
@@ -4901,16 +4937,16 @@ PConvexHull *p_convex_hull_new(PChart *chart)
 		conv_hull->verts[i] = p;
 
 		/* Note: FLT_EPSILON is to exact and produces wrong results in this context, use custom max_diff instead */
-		if (compare_ff(maxy, conv_hull->h_verts[i]->uv[1], 0.00001f)) {
+		if (compare_ff(maxy, conv_hull->verts[i]->y, 0.00001f)) {
 			/* same y value, only take if x value is lower than for current ref vert */
-			if (conv_hull->h_verts[i]->uv[0] < conv_hull->h_verts[conv_hull->ref_vert_index]->uv[0]) {
-				maxy = conv_hull->h_verts[i]->uv[1];
+			if (conv_hull->verts[i]->x < conv_hull->verts[conv_hull->ref_vert_index]->x) {
+				maxy = conv_hull->verts[i]->y;
 				conv_hull->ref_vert_index = i;
 			}
 		}
-		else if (conv_hull->h_verts[i]->uv[1] > maxy) {
+		else if (conv_hull->verts[i]->y > maxy) {
 				/* higher y value */
-				maxy = conv_hull->h_verts[i]->uv[1];
+				maxy = conv_hull->verts[i]->y;
 				conv_hull->ref_vert_index = i;
 		}
 	}
@@ -4923,25 +4959,27 @@ void p_convex_hull_update(PChart *chart)
 {
 	PConvexHull *conv_hull = chart->u.ipack.convex_hull;
 	int i;
-	float maxy = -1.0e30f;
+	float maxy = -1.0e30f, p[2];
 	conv_hull->ref_vert_index = 0;
 
-	/* Update bounds */
-	p_chart_uv_bbox(chart, chart->u.ipack.convex_hull->min_v, chart->u.ipack.convex_hull->max_v);
-
-	/* get reference vertex */
 	for (i = 0; i < conv_hull->nverts; i++) {
+		/* Update bounds */
+		p[0] = conv_hull->verts[i]->x;
+		p[1] = conv_hull->verts[i]->y;
+		minmax_v2v2_v2(conv_hull->min_v, conv_hull->max_v, p);
+
+		/* get reference vertex */
 		/* Note: FLT_EPSILON is to exact and produces wrong results in this context, use custom max_diff instead */
-		if (compare_ff(maxy, conv_hull->h_verts[i]->uv[1], 0.00001f)) {
+		if (compare_ff(maxy, conv_hull->verts[i]->y, 0.00001f)) {
 			/* same y value, only take if x value is lower than for current ref vert */
-			if (conv_hull->h_verts[i]->uv[0] < conv_hull->h_verts[conv_hull->ref_vert_index]->uv[0]) {
-				maxy = conv_hull->h_verts[i]->uv[1];
+			if (conv_hull->verts[i]->x < conv_hull->verts[conv_hull->ref_vert_index]->x) {
+				maxy = conv_hull->verts[i]->y;
 				conv_hull->ref_vert_index = i;
 			}
 		}
-		else if (conv_hull->h_verts[i]->uv[1] > maxy) {
+		else if (conv_hull->verts[i]->y > maxy) {
 			/* higher y value */
-			maxy = conv_hull->h_verts[i]->uv[1];
+			maxy = conv_hull->verts[i]->x;
 			conv_hull->ref_vert_index = i;
 		}
 	}
@@ -4974,6 +5012,7 @@ bool p_convex_hull_intersect(PConvexHull *chull_a, PConvexHull *chull_b)
 	/* Check edges for intersctions */
 	for (i = 0; i < chull_a->nverts; i++) {
 		for (j = 0; j < chull_b->nverts; j++) {
+			/* ToDo SaphireS: Use chull_a->verts instead */
 			if (p_intersect_line_segments_2d(chull_a->h_verts[i]->uv,
 									chull_a->h_verts[i+1]->uv,
 									chull_b->h_verts[j]->uv,
@@ -4993,11 +5032,14 @@ void p_convex_hull_compute_horizontal_angles(PConvexHull *hull)
 	for (j = 0; j < hull->nverts; j++) {
 
 		/* Compute horizontal angle for each edge of hull (Needed for NFP) */
+		/* ToDo SaphireS: Get rid of h_verts */
 		if (j == (hull->nverts - 1)) {
 			hull->h_verts[j]->edge->u.horizontal_angle = p_edge_horizontal_angle(hull->h_verts[j], hull->h_verts[0]);
+			hull->verts[j]->angle = p_edge_horizontal_angle_ppointuv(hull->verts[j], hull->verts[0]);
 		}
 		else {
 			hull->h_verts[j]->edge->u.horizontal_angle = p_edge_horizontal_angle(hull->h_verts[j], hull->h_verts[j + 1]);
+			hull->verts[j]->angle = p_edge_horizontal_angle_ppointuv(hull->verts[j], hull->verts[j + 1]);
 		}
 
 		/* Note: FLT_EPSILON is to exact and produces wrong results in this context, use custom max_diff instead */
@@ -5005,6 +5047,9 @@ void p_convex_hull_compute_horizontal_angles(PConvexHull *hull)
 		if (compare_ff(hull->h_verts[j]->edge->u.horizontal_angle, 0.0f, 0.00001f)) {
 			hull->h_verts[j]->edge->u.horizontal_angle += 2 * M_PI;
 		}
+		if (compare_ff(hull->verts[j]->angle, 0.0f, 0.00001f)) {
+			hull->verts[j]->angle += 2 * M_PI;
+		}
 
 		printf("---horizontal angle of edge [%i]: %f\n", j, hull->h_verts[j]->edge->u.horizontal_angle);
 	}
@@ -5017,6 +5062,7 @@ void p_convex_hull_compute_edge_components(PConvexHull *hull)
 	for (j = 0; j < hull->nverts; j++) {
 
 		/* Compute edge components for each edge of hull (Needed for NFP) */
+		/* ToDo SaphireS: Get rid of h_verts */
 		if (j == (hull->nverts - 1)) {
 			hull->h_verts[j]->u.delta_edge[0] = hull->h_verts[0]->uv[0] - hull->h_verts[j]->uv[0];
 			hull->h_verts[j]->u.delta_edge[1] = hull->h_verts[0]->uv[1] - hull->h_verts[j]->uv[1];
@@ -5025,6 +5071,15 @@ void p_convex_hull_compute_edge_components(PConvexHull *hull)
 			hull->h_verts[j]->u.delta_edge[0] = hull->h_verts[j + 1]->uv[0] - hull->h_verts[j]->uv[0];
 			hull->h_verts[j]->u.delta_edge[1] = hull->h_verts[j + 1]->uv[1] - hull->h_verts[j]->uv[1];
 		}
+		/* verts */
+		if (j == (hull->nverts - 1)) {
+			hull->verts[j]->delta_edge[0] = hull->verts[0]->x - hull->verts[j]->x;
+			hull->verts[j]->delta_edge[1] = hull->verts[0]->y - hull->verts[j]->y;
+		}
+		else {
+			hull->verts[j]->delta_edge[0] = hull->verts[j + 1]->x - hull->verts[j]->x;
+			hull->verts[j]->delta_edge[1] = hull->verts[j + 1]->y - hull->verts[j]->y;
+		}
 
 		//printf("--- edge [%i]: x: %f, y: %f\n", j, hull->h_verts[j]->u.delta_edge[0], hull->h_verts[j]->u.delta_edge[1]);
 	}
@@ -5038,44 +5093,46 @@ PConvexHull *p_convex_hull_reverse_vert_order(PConvexHull *hull)
 	conv_hull_inv->verts = (PPointUV **)MEM_callocN(sizeof(*conv_hull_inv->verts) * conv_hull_inv->nverts, "PConvexHullVerts");
 	conv_hull_inv->right = hull->right;
 	conv_hull_inv->placed = false;
-	int i, j;
+	int i, j, p[2];
 	float miny = 1.0e30f;
 	
 	/* reverse vert order */
 	for (j = 0; j < hull->nverts; j++) {
 		conv_hull_inv->h_verts[j] = hull->h_verts[hull->nverts - (j + 1)];
+		PPointUV *p = (PPointUV *)MEM_callocN(sizeof(*p), "PPointUV");
+		p->x = hull->verts[hull->nverts - (j + 1)]->x;
+		p->y = hull->verts[hull->nverts - (j + 1)]->y;
+		conv_hull_inv->verts[j] = p;
 	}
 	
 	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++) {
-		PPointUV *p = (PPointUV *)MEM_callocN(sizeof(*p), "PPointUV");
-		p->x = conv_hull_inv->h_verts[i]->uv[0];
-		p->y = conv_hull_inv->h_verts[i]->uv[1];
-		conv_hull_inv->verts[i] = p;
 
 		/* Note: FLT_EPSILON is to exact and produces wrong results in this context, use custom max_diff instead */
-		if (compare_ff(miny, conv_hull_inv->h_verts[i]->uv[1], 0.00001f)) {
+		if (compare_ff(miny, conv_hull_inv->verts[i]->y, 0.00001f)) {
 			/* same y value, only take if x value is higher than for current ref vert */
-			if (conv_hull_inv->h_verts[i]->uv[0] > conv_hull_inv->h_verts[conv_hull_inv->ref_vert_index]->uv[0]) {
-				miny = conv_hull_inv->h_verts[i]->uv[1];
+			if (conv_hull_inv->verts[i]->x > conv_hull_inv->verts[conv_hull_inv->ref_vert_index]->x) {
+				miny = conv_hull_inv->verts[i]->y;
 				conv_hull_inv->ref_vert_index = i;
-				printf("--p_convex_hull_reverse_vert_order: EQUAL min_y : x = %f, y = %f\n", conv_hull_inv->h_verts[i]->uv[0], miny);
+				printf("--p_convex_hull_reverse_vert_order: EQUAL min_y : x = %f, y = %f\n", conv_hull_inv->verts[i]->x, miny);
 			}
 		}
-		else if (conv_hull_inv->h_verts[i]->uv[1] < miny) {
+		else if (conv_hull_inv->verts[i]->y < miny) {
 			/* lower y value */
-			miny = conv_hull_inv->h_verts[i]->uv[1];
+			miny = conv_hull_inv->verts[i]->y;
 			conv_hull_inv->ref_vert_index = i;
-			printf("--p_convex_hull_reverse_vert_order: SMALLER min_y : x = %f, y = %f\n", conv_hull_inv->h_verts[i]->uv[0], miny);
+			printf("--p_convex_hull_reverse_vert_order: SMALLER min_y : x = %f, y = %f\n", conv_hull_inv->verts[i]->x, miny);
 		}
 
 		/* compute bounds */
-		minmax_v2v2_v2(conv_hull_inv->min_v, conv_hull_inv->max_v, conv_hull_inv->h_verts[i]->uv);
+		p[0] = conv_hull_inv->verts[i]->x;
+		p[1] = conv_hull_inv->verts[i]->y;
+		minmax_v2v2_v2(conv_hull_inv->min_v, conv_hull_inv->max_v, p);
 	}
 
-	printf("--p_convex_hull_reverse_vert_order: FINAL min_y: x: %f, y: %f\n", conv_hull_inv->h_verts[conv_hull_inv->ref_vert_index]->uv[0], miny);
+	printf("--p_convex_hull_reverse_vert_order: FINAL min_y: x: %f, y: %f\n", conv_hull_inv->verts[conv_hull_inv->ref_vert_index]->x, miny);
 
 	return conv_hull_inv;
 }
@@ -5086,7 +5143,9 @@ PConvexHull *p_convex_hull_reverse_direction(PConvexHull *item)
 	printf("inversing direction start\n");
 	/* ref_vert_index now conta

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list