when comparing lengths no need to sqrt
authorCampbell Barton <ideasman42@gmail.com>
Mon, 28 Nov 2011 01:20:53 +0000 (01:20 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 28 Nov 2011 01:20:53 +0000 (01:20 +0000)
source/blender/bmesh/operators/connectops.c
source/blender/bmesh/operators/createops.c
source/blender/editors/mesh/knifetool.c

index 6c84b45c83b12021965bc59aeff1f3d879163e59..ade4235c4a47a1cb0c0693b0297a98e1acef9089 100644 (file)
@@ -296,9 +296,10 @@ void bmesh_bridge_loops_exec(BMesh *bm, BMOperator *op)
 
                        /* If v1 is a better match for v4 than v3, AND v2 is a better match
                           for v3 than v4, the loops are in opposite directions, so reverse
-                          the order of reads from vv1 */
-                       if (len_v3v3(v1->co, v3->co) > len_v3v3(v1->co, v4->co) &&
-                               len_v3v3(v2->co, v4->co) > len_v3v3(v2->co, v3->co)) {
+                          the order of reads from vv1. We can avoid sqrt for comparison */
+                       if (len_squared_v3v3(v1->co, v3->co) > len_squared_v3v3(v1->co, v4->co) &&
+                               len_squared_v3v3(v2->co, v4->co) > len_squared_v3v3(v2->co, v3->co))
+                       {
                                dir1 = -1;
                                starti = CLAMP_INDEX(-1, lenv1);
                        }
@@ -333,7 +334,8 @@ void bmesh_bridge_loops_exec(BMesh *bm, BMOperator *op)
                        previ = CLAMP_INDEX(starti - 1, lenv1);
                        nexti = CLAMP_INDEX(starti + 1, lenv1);
 
-                       if (len_v3v3(vv1[nexti]->co, vv2[1]->co) > len_v3v3(vv1[previ]->co, vv2[1]->co)) {
+                       /* avoid sqrt for comparison */
+                       if (len_squared_v3v3(vv1[nexti]->co, vv2[1]->co) > len_squared_v3v3(vv1[previ]->co, vv2[1]->co)) {
                                /* reverse direction for reading vv1 (1 is forward, -1 is backward) */
                                dir1 = -1;
                        }
index e592611b935f834e12c07cf9288c8d04be604028..590fb4268113a66f2ae1a7d32f72a36552b8892e 100644 (file)
@@ -1139,8 +1139,10 @@ void bmesh_edgenet_prepare(BMesh *bm, BMOperator *op)
                        else v4 = edges2[i]->v1;
                }
 
-               if (len_v3v3(v1->co, v3->co) + len_v3v3(v2->co, v4->co) >
-                   len_v3v3(v1->co, v4->co) + len_v3v3(v2->co, v3->co)) {
+               /* avoid sqrt for comparison */
+               if (len_squared_v3v3(v1->co, v3->co) + len_squared_v3v3(v2->co, v4->co) >
+                   len_squared_v3v3(v1->co, v4->co) + len_squared_v3v3(v2->co, v3->co))
+               {
                        BMVert *v;
                        v = v3;
                        v3 = v4;
index f9c0b151a6a8ef2c577e492053fe20a26f1ec87e..c55f16c8462b3d6ddcdbe707145e5c090015f1ea 100755 (executable)
@@ -746,11 +746,14 @@ static void knifetool_draw(const bContext *UNUSED(C), ARegion *UNUSED(ar), void
        glEnable(GL_DEPTH_TEST);
 }
 
-static int kfe_vert_in_edge(KnifeEdge *e, KnifeVert *v) {
+/* do we need to keep these functions? - campbell */
+
+static int UNUSED_FUNCTION(kfe_vert_in_edge)(KnifeEdge *e, KnifeVert *v)
+{
        return e->v1 == v || e->v2 == v;
 }
 
-static int point_on_line(float p[3], float v1[3], float v2[3])
+static int UNUSED_FUNCTION(point_on_line)(float p[3], float v1[3], float v2[3])
 {
        float d = dist_to_line_segment_v3(p, v1, v2);
        if (d < 0.01) {
@@ -775,16 +778,17 @@ static BMEdgeHit *knife_edge_tri_isect(knifetool_opdata *kcd, BMBVHTree *bmtree,
        BLI_array_declare(edges);
        BVHTreeOverlap *results, *result;
        BMLoop **ls;
-       float cos[9], uv[3], lambda, depsilon;
+       float cos[9], uv[3], lambda;
        unsigned int tot=0;
        int i, j;
+
+       /* for comparing distances, error of intersection depends on triangle scale */
+       const float depsilon_squared= 50 * FLT_EPSILON * maxf(maxf(len_squared_v3v3(v1, v2), len_squared_v3v3(v1, v3)), len_squared_v3v3(v2, v3));
+       /* const float depsilon= sqrtf(depsilon_squared); */ /* UNUSED */
        
        copy_v3_v3(cos, v1);
        copy_v3_v3(cos+3, v2);
        copy_v3_v3(cos+6, v3);
-       
-       /* for comparing distances, error of intersection depends on triangle scale */
-       depsilon = 50*FLT_EPSILON*MAX3(len_v3v3(v1, v2), len_v3v3(v1, v3), len_v3v3(v2, v3));
 
        BLI_bvhtree_insert(tree2, 0, cos, 3);
        BLI_bvhtree_balance(tree2);
@@ -813,13 +817,16 @@ static BMEdgeHit *knife_edge_tri_isect(knifetool_opdata *kcd, BMBVHTree *bmtree,
                                        
                                        interp_v3_v3v3(p, kfe->v1->cageco, kfe->v2->cageco, lambda);
                                        
-                                       if (kcd->curvert && len_v3v3(kcd->curvert->cageco, p) < depsilon)
+                                       if (kcd->curvert && len_squared_v3v3(kcd->curvert->cageco, p) < depsilon_squared)
                                                continue;
-                                       if (kcd->prevvert && len_v3v3(kcd->prevvert->cageco, p) < depsilon)
+                                       if (kcd->prevvert && len_squared_v3v3(kcd->prevvert->cageco, p) < depsilon_squared)
                                                continue;
-                                       if (len_v3v3(kcd->prevcage, p) < depsilon || len_v3v3(kcd->vertcage, p) < depsilon)
+                                       if ( len_squared_v3v3(kcd->prevcage, p) < depsilon_squared ||
+                                            len_squared_v3v3(kcd->vertcage, p) < depsilon_squared)
+                                       {
                                                continue;
-                                       
+                                       }
+
                                        /*check if this point is visible in the viewport*/
                                        knife_project_v3(kcd, p, sp);
                                        view3d_unproject(mats, view, sp[0], sp[1], 0.0f);
@@ -841,8 +848,11 @@ static BMEdgeHit *knife_edge_tri_isect(knifetool_opdata *kcd, BMBVHTree *bmtree,
                                        if (!hitf && !BLI_smallhash_haskey(ehash, (intptr_t)kfe)) {
                                                BMEdgeHit hit;
                                                
-                                               if (len_v3v3(p, kcd->vertco) < depsilon || len_v3v3(p, kcd->prevco) < depsilon)
+                                               if ( len_squared_v3v3(p, kcd->vertco) < depsilon_squared ||
+                                                    len_squared_v3v3(p, kcd->prevco) < depsilon_squared)
+                                               {
                                                        continue;
+                                               }
                                                
                                                hit.kfe = kfe;
                                                hit.v = NULL;