Index: source/blender/editors/uvedit/uvedit_intern.h =================================================================== --- source/blender/editors/uvedit/uvedit_intern.h (revision 51718) +++ source/blender/editors/uvedit/uvedit_intern.h (working copy) @@ -55,20 +55,18 @@ void uv_poly_copy_aspect(float uv_orig [][2], float uv[][2], float aspx, float void uv_poly_center(struct BMEditMesh *em, struct BMFace *f, float r_cent[2]); /* find nearest */ - typedef struct NearestHit { struct BMFace *efa; struct MTexPoly *tf; struct BMLoop *l, *nextl; struct MLoopUV *luv, *nextluv; - int lindex; //index of loop within face - int vert1, vert2; //index in mesh of edge vertices + int lindex; /* index of loop within face */ + int vert1, vert2; /* index in mesh of edge vertices */ + float dist; /* distance to hit, as in dx + dy */ } NearestHit; void uv_find_nearest_vert(struct Scene *scene, struct Image *ima, struct BMEditMesh *em, const float co[2], const float penalty[2], struct NearestHit *hit); -void uv_find_nearest_edge(struct Scene *scene, struct Image *ima, struct BMEditMesh *em, - const float co[2], struct NearestHit *hit); /* utility tool functions */ Index: source/blender/editors/uvedit/uvedit_ops.c =================================================================== --- source/blender/editors/uvedit/uvedit_ops.c (revision 51718) +++ source/blender/editors/uvedit/uvedit_ops.c (working copy) @@ -573,8 +573,7 @@ void uv_poly_center(BMEditMesh *em, BMFace *f, float r_cent[2]) float uv_poly_area(float uv[][2], int len) { - //BMESH_TODO: make this not suck - //maybe use scanfill? I dunno. + /* BMESH_TODO: make this not suck, maybe use scanfill? I dunno. */ if (len >= 4) return area_tri_v2(uv[0], uv[1], uv[2]) + area_tri_v2(uv[0], uv[2], uv[3]); @@ -675,17 +674,18 @@ static int uvedit_center(Scene *scene, Image *ima, Object *obedit, float cent[2] /************************** find nearest ****************************/ -void uv_find_nearest_edge(Scene *scene, Image *ima, BMEditMesh *em, const float co[2], NearestHit *hit) +static void uv_find_nearest_edge(Scene *scene, Image *ima, BMEditMesh *em, const float co[2], NearestHit *hit) { MTexPoly *tf; BMFace *efa; BMLoop *l; BMIter iter, liter; MLoopUV *luv, *nextluv; - float mindist_squared, dist_squared; + float mindist, dist; + float closest_point[2]; int i; - mindist_squared = 1e10f; + mindist = 1e10f; memset(hit, 0, sizeof(*hit)); BM_mesh_elem_index_ensure(em->bm, BM_VERT); @@ -700,9 +700,12 @@ void uv_find_nearest_edge(Scene *scene, Image *ima, BMEditMesh *em, const float luv = CustomData_bmesh_get(&em->bm->ldata, l->head.data, CD_MLOOPUV); nextluv = CustomData_bmesh_get(&em->bm->ldata, l->next->head.data, CD_MLOOPUV); - dist_squared = dist_squared_to_line_segment_v2(co, luv->uv, nextluv->uv); + /* get point on uv closest to co, so we can use dx + dy + * for dist, like other find_nearest functions */ + closest_to_line_segment_v2(closest_point, co, luv->uv, nextluv->uv); + dist = fabs(co[0] - closest_point[0]) + fabs(co[1] - closest_point[1]); - if (dist_squared < mindist_squared) { + if (dist < mindist) { hit->tf = tf; hit->efa = efa; @@ -713,8 +716,9 @@ void uv_find_nearest_edge(Scene *scene, Image *ima, BMEditMesh *em, const float hit->lindex = i; hit->vert1 = BM_elem_index_get(hit->l->v); hit->vert2 = BM_elem_index_get(hit->l->next->v); + hit->dist = dist; - mindist_squared = dist_squared; + mindist = dist; } i++; @@ -722,7 +726,7 @@ void uv_find_nearest_edge(Scene *scene, Image *ima, BMEditMesh *em, const float } } -static void find_nearest_uv_face(Scene *scene, Image *ima, BMEditMesh *em, const float co[2], NearestHit *hit) +static void uv_find_nearest_face(Scene *scene, Image *ima, BMEditMesh *em, const float co[2], NearestHit *hit) { MTexPoly *tf; BMFace *efa; @@ -749,6 +753,7 @@ static void find_nearest_uv_face(Scene *scene, Image *ima, BMEditMesh *em, const if (dist < mindist) { hit->tf = tf; hit->efa = efa; + hit->dist = dist; mindist = dist; } } @@ -853,6 +858,7 @@ void uv_find_nearest_vert(Scene *scene, Image *ima, BMEditMesh *em, hit->efa = efa; hit->lindex = i; hit->vert1 = BM_elem_index_get(hit->l->v); + hit->dist = dist; } i++; @@ -860,6 +866,29 @@ void uv_find_nearest_vert(Scene *scene, Image *ima, BMEditMesh *em, } } +/* find nearest vert or edge */ +static void uv_find_nearest_island(Scene *scene, Image *ima, BMEditMesh *em, const float co[2], NearestHit *hit) +{ + NearestHit temp_hit; + + memset(hit, 0, sizeof(*hit)); + + uv_find_nearest_vert(scene, ima, em, co, NULL, &temp_hit); + if(temp_hit.efa != NULL) + *hit = temp_hit; + + memset(&temp_hit, 0, sizeof(temp_hit)); + uv_find_nearest_edge(scene, ima, em, co, &temp_hit); + if (temp_hit.efa != NULL) { + if (hit) { + if (temp_hit.dist <= hit->dist) + *hit = temp_hit; + } + else + *hit = temp_hit; + } +} + int ED_uvedit_nearest_uv(Scene *scene, Object *obedit, Image *ima, const float co[2], float r_uv[2]) { BMEditMesh *em = BMEdit_FromObject(obedit); @@ -1811,7 +1840,7 @@ static int mouse_select(bContext *C, const float co[2], int extend, int loop) } else if (selectmode == UV_SELECT_FACE) { /* find face */ - find_nearest_uv_face(scene, ima, em, co, &hit); + uv_find_nearest_face(scene, ima, em, co, &hit); if (hit.efa == NULL) { BLI_array_free(hitv); BLI_array_free(hituv); @@ -1836,7 +1865,7 @@ static int mouse_select(bContext *C, const float co[2], int extend, int loop) hitlen = hit.efa->len; } else if (selectmode == UV_SELECT_ISLAND) { - uv_find_nearest_vert(scene, ima, em, co, NULL, &hit); + uv_find_nearest_island(scene, ima, em, co, &hit); if (hit.efa == NULL) { BLI_array_free(hitv); @@ -2436,8 +2465,8 @@ static void uv_faces_do_sticky(SpaceImage *sima, Scene *scene, Object *obedit, s struct UvVertMap *vmap; float limit[2]; unsigned int efa_index; - //BMVert *eve; /* removed vert counting for now */ - //int a; + /* BMVert *eve; */ /* removed vert counting for now */ + /* int a; */ uvedit_pixel_to_float(sima, limit, 0.05);