[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51068] trunk/blender/source/blender/ editors/mesh/editmesh_knife.c: knife tool: avoid sqrt' s for length comparison, and define KNIFE_FLT_EPS rather than using ( FLT_EPSILON * 80).

Campbell Barton ideasman42 at gmail.com
Fri Oct 5 06:43:47 CEST 2012


Revision: 51068
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51068
Author:   campbellbarton
Date:     2012-10-05 04:43:46 +0000 (Fri, 05 Oct 2012)
Log Message:
-----------
knife tool: avoid sqrt's for length comparison, and define KNIFE_FLT_EPS rather than using (FLT_EPSILON * 80).

Modified Paths:
--------------
    trunk/blender/source/blender/editors/mesh/editmesh_knife.c

Modified: trunk/blender/source/blender/editors/mesh/editmesh_knife.c
===================================================================
--- trunk/blender/source/blender/editors/mesh/editmesh_knife.c	2012-10-05 04:18:52 UTC (rev 51067)
+++ trunk/blender/source/blender/editors/mesh/editmesh_knife.c	2012-10-05 04:43:46 UTC (rev 51068)
@@ -73,6 +73,9 @@
 
 #define KMAXDIST    10  /* max mouse distance from edge before not detecting it */
 
+#define KNIFE_FLT_EPS          0.00001f
+#define KNIFE_FLT_EPS_SQUARED  (KNIFE_FLT_EPS * KNIFE_FLT_EPS)
+
 typedef struct KnifeColors {
 	unsigned char line[3];
 	unsigned char edge[3];
@@ -430,7 +433,7 @@
 	float perc, cageco[3], l12;
 
 	l12 = len_v3v3(kfe->v1->co, kfe->v2->co);
-	if (l12 < FLT_EPSILON * 80) {
+	if (l12 < KNIFE_FLT_EPS) {
 		copy_v3_v3(cageco, kfe->v1->cageco);
 	}
 	else {
@@ -589,7 +592,7 @@
 	 * successor faces connected to the linehits at either end of the range */
 	for (i = 0; i < kcd->totlinehit - 1; i = nexti) {
 		for (j = i + 1; j < kcd->totlinehit; j++) {
-			if (fabsf(kcd->linehits[j].l - kcd->linehits[i].l) > 80 * FLT_EPSILON)
+			if (fabsf(kcd->linehits[j].l - kcd->linehits[i].l) > KNIFE_FLT_EPS)
 				break;
 		}
 		nexti = j;
@@ -797,7 +800,7 @@
 		for (i = 0; i < kcd->totlinehit; i++, (lastlh = lh), lh++) {
 			BMFace *f = lastlh ? lastlh->f : lh->f;
 
-			if (lastlh && len_v3v3(lastlh->hit, lh->hit) == 0.0f) {
+			if (lastlh && len_squared_v3v3(lastlh->hit, lh->hit) == 0.0f) {
 				if (!firstlh)
 					firstlh = lastlh;
 				continue;
@@ -816,13 +819,13 @@
 				lastlh = firstlh = NULL;
 			}
 
-			if (len_v3v3(kcd->prev.cage, lh->realhit) < FLT_EPSILON * 80)
+			if (len_squared_v3v3(kcd->prev.cage, lh->realhit) < KNIFE_FLT_EPS_SQUARED)
 				continue;
-			if (len_v3v3(kcd->curr.cage, lh->realhit) < FLT_EPSILON * 80)
+			if (len_squared_v3v3(kcd->curr.cage, lh->realhit) < KNIFE_FLT_EPS_SQUARED)
 				continue;
 
 			/* first linehit may be down face parallel to view */
-			if (!lastlh && fabsf(lh->l) < FLT_EPSILON * 80)
+			if (!lastlh && fabsf(lh->l) < KNIFE_FLT_EPS)
 				continue;
 
 			if (kcd->prev.is_space) {
@@ -843,7 +846,7 @@
 			copy_v3_v3(kcd->curr.cage, lh->cagehit);
 
 			/* don't draw edges down faces parallel to view */
-			if (lastlh && fabsf(lastlh->l - lh->l) < FLT_EPSILON * 80) {
+			if (lastlh && fabsf(lastlh->l - lh->l) < KNIFE_FLT_EPS) {
 				kcd->prev = kcd->curr;
 				continue;
 			}
@@ -1044,6 +1047,9 @@
 	}
 
 	if (kcd->totlinehit > 0) {
+		const float vthresh4 = kcd->vthresh / 4.0f;
+		const float vthresh4_squared = vthresh4 * vthresh4;
+
 		BMEdgeHit *lh;
 		int i;
 
@@ -1062,12 +1068,12 @@
 			knife_project_v3(kcd, lh->kfe->v2->cageco, sv2);
 			knife_project_v3(kcd, lh->cagehit, lh->schit);
 
-			if (len_v2v2(lh->schit, sv1) < kcd->vthresh / 4.0f) {
+			if (len_squared_v2v2(lh->schit, sv1) < vthresh4_squared) {
 				copy_v3_v3(lh->cagehit, lh->kfe->v1->cageco);
 				glVertex3fv(lh->cagehit);
 				lh->v = lh->kfe->v1;
 			}
-			else if (len_v2v2(lh->schit, sv2) < kcd->vthresh / 4.0f) {
+			else if (len_squared_v2v2(lh->schit, sv2) < vthresh4_squared) {
 				copy_v3_v3(lh->cagehit, lh->kfe->v2->cageco);
 				glVertex3fv(lh->cagehit);
 				lh->v = lh->kfe->v2;
@@ -1136,11 +1142,11 @@
 
 static float len_v3_tri_side_max(const float v1[3], const float v2[3], const float v3[3])
 {
-	const float s1 = len_v3v3(v1, v2);
-	const float s2 = len_v3v3(v2, v3);
-	const float s3 = len_v3v3(v3, v1);
+	const float s1 = len_squared_v3v3(v1, v2);
+	const float s2 = len_squared_v3v3(v2, v3);
+	const float s3 = len_squared_v3v3(v3, v1);
 
-	return MAX3(s1, s2, s3);
+	return sqrtf(MAX3(s1, s2, s3));
 }
 
 static BMEdgeHit *knife_edge_tri_isect(KnifeTool_OpData *kcd, BMBVHTree *bmtree,
@@ -1158,7 +1164,7 @@
 
 	/* for comparing distances, error of intersection depends on triangle scale.
 	 * need to scale down before squaring for accurate comparison */
-	const float depsilon = 50 *FLT_EPSILON * len_v3_tri_side_max(v1, v2, v3);
+	const float depsilon = (KNIFE_FLT_EPS / 2.0f) * len_v3_tri_side_max(v1, v2, v3);
 	const float depsilon_squared = depsilon * depsilon;
 
 	copy_v3_v3(cos + 0, v1);
@@ -1985,7 +1991,7 @@
 		ScanFillFace *sf_tri;
 		ScanFillVert *sf_vert, *sf_vert_last;
 		int j;
-		float rndscale = FLT_EPSILON * 25;
+		float rndscale = (KNIFE_FLT_EPS / 4.0f);
 
 		f = faces[i];
 		BLI_smallhash_init(hash);
@@ -3058,14 +3064,9 @@
 
 static int knifetool_modal(bContext *C, wmOperator *op, wmEvent *event)
 {
-	Object *obedit;
+	Object *obedit = CTX_data_edit_object(C);
 	KnifeTool_OpData *kcd = op->customdata;
 
-	if (!C) {
-		return OPERATOR_FINISHED;
-	}
-
-	obedit = CTX_data_edit_object(C);
 	if (!obedit || obedit->type != OB_MESH || BMEdit_FromObject(obedit) != kcd->em) {
 		knifetool_exit(C, op);
 		ED_area_headerprint(CTX_wm_area(C), NULL);




More information about the Bf-blender-cvs mailing list