code cleanup: reduce calling sqrt() when distances are only calculated for comparison use dist_squared_to_line_segment_v2().

This commit is contained in:
Campbell Barton
2012-08-14 08:44:35 +00:00
parent 8d496b3bf2
commit f2074949e7
4 changed files with 30 additions and 26 deletions

View File

@@ -1997,7 +1997,10 @@ static int dynamicPaint_findNeighbourPixel(PaintUVPoint *tempPoints, DerivedMesh
/* Get closest edge to that subpixel on UV map */
{
float pixel[2], dist, t_dist;
float pixel[2];
/* distances only used for comparison */
float dist_squared, t_dist_squared;
int i, uindex[3], edge1_index, edge2_index,
e1_index, e2_index, target_face;
float closest_point[2], lambda, dir_vec[2];
@@ -2019,17 +2022,17 @@ static int dynamicPaint_findNeighbourPixel(PaintUVPoint *tempPoints, DerivedMesh
/*
* Find closest edge to that pixel
*/
/* Dist to first edge */
/* Dist to first edge */
e1_index = cPoint->v1; e2_index = cPoint->v2; edge1_index = uindex[0]; edge2_index = uindex[1];
dist = dist_to_line_segment_v2(pixel, tface[cPoint->face_index].uv[edge1_index], tface[cPoint->face_index].uv[edge2_index]);
dist_squared = dist_squared_to_line_segment_v2(pixel, tface[cPoint->face_index].uv[edge1_index], tface[cPoint->face_index].uv[edge2_index]);
/* Dist to second edge */
t_dist = dist_to_line_segment_v2(pixel, tface[cPoint->face_index].uv[uindex[1]], tface[cPoint->face_index].uv[uindex[2]]);
if (t_dist < dist) { e1_index = cPoint->v2; e2_index = cPoint->v3; edge1_index = uindex[1]; edge2_index = uindex[2]; dist = t_dist; }
/* Dist to second edge */
t_dist_squared = dist_squared_to_line_segment_v2(pixel, tface[cPoint->face_index].uv[uindex[1]], tface[cPoint->face_index].uv[uindex[2]]);
if (t_dist_squared < dist_squared) { e1_index = cPoint->v2; e2_index = cPoint->v3; edge1_index = uindex[1]; edge2_index = uindex[2]; dist_squared = t_dist_squared; }
/* Dist to third edge */
t_dist = dist_to_line_segment_v2(pixel, tface[cPoint->face_index].uv[uindex[2]], tface[cPoint->face_index].uv[uindex[0]]);
if (t_dist < dist) { e1_index = cPoint->v3; e2_index = cPoint->v1; edge1_index = uindex[2]; edge2_index = uindex[0]; dist = t_dist; }
/* Dist to third edge */
t_dist_squared = dist_squared_to_line_segment_v2(pixel, tface[cPoint->face_index].uv[uindex[2]], tface[cPoint->face_index].uv[uindex[0]]);
if (t_dist_squared < dist_squared) { e1_index = cPoint->v3; e2_index = cPoint->v1; edge1_index = uindex[2]; edge2_index = uindex[0]; dist_squared = t_dist_squared; }
/*

View File

@@ -64,6 +64,7 @@ static float edbm_rip_rip_edgedist(ARegion *ar, float mat[][4],
ED_view3d_project_float_v2(ar, co1, vec1, mat);
ED_view3d_project_float_v2(ar, co2, vec2, mat);
/* TODO: use dist_squared_to_line_segment_v2() looks like we only ever use for comparison */
return dist_to_line_segment_v2(mvalf, vec1, vec2);
}
@@ -111,8 +112,8 @@ static float edbm_rip_edge_side_measure(BMEdge *e, BMLoop *e_l,
score = len_v2v2(e_v1_co, e_v2_co);
if (dist_to_line_segment_v2(fmval_tweak, e_v1_co, e_v2_co) >
dist_to_line_segment_v2(fmval, e_v1_co, e_v2_co))
if (dist_squared_to_line_segment_v2(fmval_tweak, e_v1_co, e_v2_co) >
dist_squared_to_line_segment_v2(fmval, e_v1_co, e_v2_co))
{
return score;
}

View File

@@ -161,12 +161,12 @@ static float dist_to_rect(float co[2], float pos[2], float min[2], float max[2])
float v1[2] = {min[0], min[1]}, v2[2] = {max[0], min[1]};
float v3[2] = {max[0], max[1]}, v4[2] = {min[0], max[1]};
d1 = dist_to_line_segment_v2(p, v1, v2);
d2 = dist_to_line_segment_v2(p, v2, v3);
d3 = dist_to_line_segment_v2(p, v3, v4);
d4 = dist_to_line_segment_v2(p, v4, v1);
d1 = dist_squared_to_line_segment_v2(p, v1, v2);
d2 = dist_squared_to_line_segment_v2(p, v2, v3);
d3 = dist_squared_to_line_segment_v2(p, v3, v4);
d4 = dist_squared_to_line_segment_v2(p, v4, v1);
return MIN4(d1, d2, d3, d4);
return sqrtf(MIN4(d1, d2, d3, d4));
}
static float dist_to_crns(float co[2], float pos[2], float crns[4][2])
@@ -176,12 +176,12 @@ static float dist_to_crns(float co[2], float pos[2], float crns[4][2])
float *v1 = crns[0], *v2 = crns[1];
float *v3 = crns[2], *v4 = crns[3];
d1 = dist_to_line_segment_v2(p, v1, v2);
d2 = dist_to_line_segment_v2(p, v2, v3);
d3 = dist_to_line_segment_v2(p, v3, v4);
d4 = dist_to_line_segment_v2(p, v4, v1);
d1 = dist_squared_to_line_segment_v2(p, v1, v2);
d2 = dist_squared_to_line_segment_v2(p, v2, v3);
d3 = dist_squared_to_line_segment_v2(p, v3, v4);
d4 = dist_squared_to_line_segment_v2(p, v4, v1);
return MIN4(d1, d2, d3, d4);
return sqrtf(MIN4(d1, d2, d3, d4));
}
static MovieTrackingTrack *find_nearest_track(SpaceClip *sc, ListBase *tracksbase, float co[2])

View File

@@ -680,10 +680,10 @@ void uv_find_nearest_edge(Scene *scene, Image *ima, BMEditMesh *em, const float
BMLoop *l;
BMIter iter, liter;
MLoopUV *luv, *nextluv;
float mindist, dist;
float mindist_squared, dist_squared;
int i;
mindist = 1e10f;
mindist_squared = 1e10f;
memset(hit, 0, sizeof(*hit));
BM_mesh_elem_index_ensure(em->bm, BM_VERT);
@@ -698,9 +698,9 @@ 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 = dist_to_line_segment_v2(co, luv->uv, nextluv->uv);
dist_squared = dist_squared_to_line_segment_v2(co, luv->uv, nextluv->uv);
if (dist < mindist) {
if (dist_squared < mindist_squared) {
hit->tf = tf;
hit->efa = efa;
@@ -712,7 +712,7 @@ void uv_find_nearest_edge(Scene *scene, Image *ima, BMEditMesh *em, const float
hit->vert1 = BM_elem_index_get(hit->l->v);
hit->vert2 = BM_elem_index_get(hit->l->next->v);
mindist = dist;
mindist_squared = dist_squared;
}
i++;