From 2787c78175eb7f12e48afe65098779444ff83720 Mon Sep 17 00:00:00 2001 From: Antony Riakiotakis Date: Mon, 31 Dec 2012 19:38:40 +0000 Subject: [PATCH] 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. --- .../editors/uvedit/uvedit_smart_stitch.c | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/source/blender/editors/uvedit/uvedit_smart_stitch.c b/source/blender/editors/uvedit/uvedit_smart_stitch.c index 96e43dafa2f..dc9f5fbf97c 100644 --- a/source/blender/editors/uvedit/uvedit_smart_stitch.c +++ b/source/blender/editors/uvedit/uvedit_smart_stitch.c @@ -468,15 +468,12 @@ static void stitch_island_calculate_edge_rotation(UvEdge *edge, StitchState *sta 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 @@ static void stitch_island_calculate_edge_rotation(UvEdge *edge, StitchState *sta 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 @@ static void stitch_island_calculate_vert_rotation(UvElement *element, StitchStat 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 @@ static int stitch_process_data(StitchState *state, Scene *scene, int final) 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 @@ static void stitch_switch_selection_mode(StitchState *state) 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 @@ static void stitch_draw(const bContext *UNUSED(C), ARegion *UNUSED(ar), void *ar 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 @@ static void stitch_draw(const bContext *UNUSED(C), ARegion *UNUSED(ar), void *ar 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 @@ static int stitch_init(bContext *C, wmOperator *op) * 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); } }