code cleanup: reduce calling sqrt() when distances are only calculated for comparison...
authorCampbell Barton <ideasman42@gmail.com>
Tue, 14 Aug 2012 08:44:35 +0000 (08:44 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 14 Aug 2012 08:44:35 +0000 (08:44 +0000)
source/blender/blenkernel/intern/dynamicpaint.c
source/blender/editors/mesh/editmesh_rip.c
source/blender/editors/space_clip/tracking_select.c
source/blender/editors/uvedit/uvedit_ops.c

index 8122f9bf617a7bfcd31795ff2b8bb8cdf5315b8f..0daf06c63059bd0ad5bcc5ce12660b78fc82329b 100644 (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; }
 
 
                        /*
index f71ec56ca5f0026e3bd92cc7daf459a879cf7ebe..8325e0a50d4a073330ef3c345f27d40b99955068 100644 (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;
        }
index ea3d12269378b41f3593ce850f7d2fa8a40a35a6..5a79e832ebe24d4ecb711d1abaafc6fbb2093d25 100644 (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])
index 38fd3fe5c68de9978ac38610fbf42d0384c7b5e9..54c5cbfce937d1621e2417010eb29de386adc52a 100644 (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++;