[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53475] trunk/blender/source/blender/ editors/uvedit/uvedit_smart_stitch.c: smart stitching:

Antony Riakiotakis kalast at gmail.com
Mon Dec 31 20:38:44 CET 2012


Revision: 53475
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53475
Author:   psy-fi
Date:     2012-12-31 19:38:40 +0000 (Mon, 31 Dec 2012)
Log Message:
-----------
smart stitching:
* Some code cleanup
* Bugfix: rotation of uv islands during stitch on images with aspect
ratio was different for different island rotations. I needed to correct
normal and tangent calculation to take image aspect into account.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/uvedit/uvedit_smart_stitch.c

Modified: trunk/blender/source/blender/editors/uvedit/uvedit_smart_stitch.c
===================================================================
--- trunk/blender/source/blender/editors/uvedit/uvedit_smart_stitch.c	2012-12-31 17:39:01 UTC (rev 53474)
+++ trunk/blender/source/blender/editors/uvedit/uvedit_smart_stitch.c	2012-12-31 19:38:40 UTC (rev 53475)
@@ -468,15 +468,12 @@
 	int index1, index2;
 	float rotation;
 	MLoopUV *luv1, *luv2;
-	BMLoop *l1, *l2;
 
 	element1 = state->uvs[edge->uv1];
 	element2 = state->uvs[edge->uv2];
 
-	l1 = element1->l;
-	luv1 = CustomData_bmesh_get(&state->em->bm->ldata, l1->head.data, CD_MLOOPUV);
-	l2 = element2->l;
-	luv2 = CustomData_bmesh_get(&state->em->bm->ldata, l2->head.data, CD_MLOOPUV);
+	luv1 = CustomData_bmesh_get(&state->em->bm->ldata, element1->l->head.data, CD_MLOOPUV);
+	luv2 = CustomData_bmesh_get(&state->em->bm->ldata, element2->l->head.data, CD_MLOOPUV);
 
 	if (state->mode == STITCH_VERT) {
 		index1 = uvfinal_map[element1 - state->element_map->buf];
@@ -491,14 +488,18 @@
 	uv1[0] = luv2->uv[0] - luv1->uv[0];
 	uv1[1] = luv2->uv[1] - luv1->uv[1];
 
+	uv1[1] /= state->aspect;
+
 	uv2[0] = uv_average[index2].uv[0] - uv_average[index1].uv[0];
 	uv2[1] = uv_average[index2].uv[1] - uv_average[index1].uv[1];
 
+	uv2[1] /= state->aspect;
+
 	normalize_v2(uv1);
 	normalize_v2(uv2);
 
-	edgecos = uv1[0] * uv2[0] + uv1[1] * uv2[1];
-	edgesin = uv1[0] * uv2[1] - uv2[0] * uv1[1];
+	edgecos = dot_v2v2(uv1, uv2);
+	edgesin = cross_v2v2(uv1, uv2);
 
 	rotation = (edgesin > 0.0f) ?
 	            +acosf(max_ff(-1.0f, min_ff(1.0f, edgecos))) :
@@ -543,7 +544,9 @@
 			negate_v2_v2(normal, state->normals + index_tmp2 * 2);
 			edgecos = dot_v2v2(normal, state->normals + index_tmp1 * 2);
 			edgesin = cross_v2v2(normal, state->normals + index_tmp1 * 2);
-			rotation += (edgesin > 0.0f) ? acosf(edgecos) : -acosf(edgecos);
+			rotation += (edgesin > 0.0f) ?
+				+acosf(max_ff(-1.0f, min_ff(1.0f, edgecos))) :
+	            -acosf(max_ff(-1.0f, min_ff(1.0f, edgecos)));
 		}
 	}
 
@@ -844,13 +847,13 @@
 	int previous_island = state->static_island;
 	BMFace *efa;
 	BMIter iter;
-	UVVertAverage *final_position;
+	UVVertAverage *final_position = NULL;
 
 	char stitch_midpoints = state->midpoints;
 	/* used to map uv indices to uvaverage indices for selection */
-	unsigned int *uvfinal_map;
+	unsigned int *uvfinal_map = NULL;
 	/* per face preview position in preview buffer */
-	PreviewPosition *preview_position;
+	PreviewPosition *preview_position = NULL;
 
 	/* cleanup previous preview */
 	stitch_preview_delete(state->stitch_preview);
@@ -1429,18 +1432,19 @@
 	MEM_freeN(old_selection_stack);
 }
 
-static void stitch_calculate_edge_normal(BMEditMesh *em, UvEdge *edge, float *normal)
+static void stitch_calculate_edge_normal(BMEditMesh *em, UvEdge *edge, float *normal, float aspect)
 {
 	BMLoop *l1 = edge->element->l;
-	BMLoop *l2 = l1->next;
 	MLoopUV *luv1, *luv2;
 	float tangent[2];
 
 	luv1 = CustomData_bmesh_get(&em->bm->ldata, l1->head.data, CD_MLOOPUV);
-	luv2 = CustomData_bmesh_get(&em->bm->ldata, l2->head.data, CD_MLOOPUV);
+	luv2 = CustomData_bmesh_get(&em->bm->ldata, l1->next->head.data, CD_MLOOPUV);
 
 	sub_v2_v2v2(tangent, luv2->uv,  luv1->uv);
 
+	tangent[1] /= aspect;
+
 	normal[0] = tangent[1];
 	normal[1] = -tangent[0];
 
@@ -1457,6 +1461,8 @@
 	glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
 	glEnableClientState(GL_VERTEX_ARRAY);
 
+	glPointSize(pointsize * 2.0f);
+
 	glEnable(GL_BLEND);
 
 	UI_ThemeColor4(TH_STITCH_PREVIEW_ACTIVE);
@@ -1472,19 +1478,18 @@
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 		UI_ThemeColor4(TH_STITCH_PREVIEW_EDGE);
 		glDrawArrays(GL_POLYGON, index, stitch_preview->uvs_per_polygon[i]);
+		#if 0
+		glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
+		UI_ThemeColor4(TH_STITCH_PREVIEW_VERT);
+		glDrawArrays(GL_POLYGON, index, stitch_preview->uvs_per_polygon[i]);
+		#endif
 
 		index += stitch_preview->uvs_per_polygon[i];
 	}
-	glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
-#if 0
-	UI_ThemeColor4(TH_STITCH_PREVIEW_VERT);
-	glDrawArrays(GL_TRIANGLES, 0, stitch_preview->num_tris * 3);
-#endif
 	glDisable(GL_BLEND);
 
 	/* draw vert preview */
 	if (state->mode == STITCH_VERT) {
-		glPointSize(pointsize * 2.0f);
 		UI_ThemeColor4(TH_STITCH_PREVIEW_STITCHABLE);
 		glVertexPointer(2, GL_FLOAT, 0, stitch_preview->preview_stitchable);
 		glDrawArrays(GL_POINTS, 0, stitch_preview->num_stitchable);
@@ -1733,15 +1738,16 @@
 	 * the winding of the polygon (assuming counter-clockwise flow). */
 
 	for (i = 0; i < total_edges; i++) {
+		UvEdge *edge = edges + i;
 		float normal[2];
-		if (edges[i].flag & STITCH_BOUNDARY) {
-			stitch_calculate_edge_normal(em, edges + i, normal);
+		if (edge->flag & STITCH_BOUNDARY) {
+			stitch_calculate_edge_normal(em, edge, normal, state->aspect);
 
-			add_v2_v2(state->normals + edges[i].uv1 * 2, normal);
-			add_v2_v2(state->normals + edges[i].uv2 * 2, normal);
+			add_v2_v2(state->normals + edge->uv1 * 2, normal);
+			add_v2_v2(state->normals + edge->uv2 * 2, normal);
 
-			normalize_v2(state->normals + edges[i].uv1 * 2);
-			normalize_v2(state->normals + edges[i].uv2 * 2);
+			normalize_v2(state->normals + edge->uv1 * 2);
+			normalize_v2(state->normals + edge->uv2 * 2);
 		}
 	}
 




More information about the Bf-blender-cvs mailing list