Transform: no need to store distance to snap point
authorCampbell Barton <ideasman42@gmail.com>
Mon, 18 Jan 2016 02:15:38 +0000 (13:15 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 18 Jan 2016 03:01:20 +0000 (14:01 +1100)
Compare squared distance to snap target since the value is only ever used for comparison.

source/blender/editors/transform/transform.h
source/blender/editors/transform/transform_snap.c

index 39b137fb5f3499d84ac792af6f113c6c2b937a39..f6b4d7da57fc44aae8cefb1150068280a56c0a22 100644 (file)
@@ -90,12 +90,16 @@ typedef struct TransSnap {
        char    snapNodeBorder;
        ListBase points;
        TransSnapPoint  *selectedPoint;
-       float   dist; // Distance from snapPoint to snapTarget
        double  last;
        void  (*applySnap)(struct TransInfo *, float *);
        void  (*calcSnap)(struct TransInfo *, float *);
        void  (*targetSnap)(struct TransInfo *);
-       /* Get the transform distance between two points (used by Closest snap) */
+       /**
+        * Get the transform distance between two points (used by Closest snap)
+        *
+        * \note Return value can be anything,
+        * where the smallest absolute value defines whats closest.
+        */
        float  (*distance)(struct TransInfo *, const float p1[3], const float p2[3]);
 } TransSnap;
 
index ea07193b85bfc663f6f0a908e6e203ae6ee39f9e..ed48478246f2b2bf0444368ccdbccd961617bb03 100644 (file)
@@ -823,7 +823,7 @@ static void ApplySnapResize(TransInfo *t, float vec[3])
 
 static float TranslationBetween(TransInfo *UNUSED(t), const float p1[3], const float p2[3])
 {
-       return len_v3v3(p1, p2);
+       return len_squared_v3v3(p1, p2);
 }
 
 static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3])
@@ -1157,6 +1157,7 @@ static void TargetSnapClosest(TransInfo *t)
 {
        // Only valid if a snap point has been selected
        if (t->tsnap.status & POINT_INIT) {
+               float dist_closest = 0.0f;
                TransData *closest = NULL, *td = NULL;
                
                /* Object mode */
@@ -1179,11 +1180,11 @@ static void TargetSnapClosest(TransInfo *t)
                                                dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
 
                                                if ((dist != TRANSFORM_DIST_INVALID) &&
-                                                   (closest == NULL || fabsf(dist) < fabsf(t->tsnap.dist)))
+                                                   (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
                                                {
                                                        copy_v3_v3(t->tsnap.snapTarget, loc);
                                                        closest = td;
-                                                       t->tsnap.dist = dist; 
+                                                       dist_closest = dist;
                                                }
                                        }
                                }
@@ -1197,11 +1198,10 @@ static void TargetSnapClosest(TransInfo *t)
                                        dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
 
                                        if ((dist != TRANSFORM_DIST_INVALID) &&
-                                           (closest == NULL || fabsf(dist) < fabsf(t->tsnap.dist)))
+                                           (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
                                        {
                                                copy_v3_v3(t->tsnap.snapTarget, loc);
                                                closest = td;
-                                               t->tsnap.dist = dist; 
                                        }
                                }
                        }
@@ -1222,11 +1222,11 @@ static void TargetSnapClosest(TransInfo *t)
                                dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
                                
                                if ((dist != TRANSFORM_DIST_INVALID) &&
-                                   (closest == NULL || fabsf(dist) < fabsf(t->tsnap.dist)))
+                                   (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
                                {
                                        copy_v3_v3(t->tsnap.snapTarget, loc);
                                        closest = td;
-                                       t->tsnap.dist = dist; 
+                                       dist_closest = dist;
                                }
                        }
                }