[Bf-blender-cvs] [ac07046] master: BMesh: optimize quad_coord, avoid duplicate sqrt calls

Campbell Barton noreply at git.blender.org
Fri Mar 28 05:26:05 CET 2014


Commit: ac07046e55e515cbecbb2a528e97c0d57106f552
Author: Campbell Barton
Date:   Fri Mar 28 15:16:15 2014 +1100
https://developer.blender.org/rBac07046e55e515cbecbb2a528e97c0d57106f552

BMesh: optimize quad_coord, avoid duplicate sqrt calls

===================================================================

M	source/blender/blenlib/BLI_utildefines.h
M	source/blender/bmesh/intern/bmesh_interp.c

===================================================================

diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h
index 9f6eabf..bb1b3a3 100644
--- a/source/blender/blenlib/BLI_utildefines.h
+++ b/source/blender/blenlib/BLI_utildefines.h
@@ -276,6 +276,13 @@
 	else if ((a) > (c)) (a) = (c);  \
 } (void)0
 
+#define CLAMP_MAX(a, c)  {          \
+	if ((a) > (c)) (a) = (c);       \
+} (void)0
+
+#define CLAMP_MIN(a, b)  {          \
+	if      ((a) < (b)) (a) = (b);  \
+} (void)0
 
 #define IS_EQ(a, b)  ( \
 	CHECK_TYPE_INLINE(a, double), CHECK_TYPE_INLINE(b, double), \
diff --git a/source/blender/bmesh/intern/bmesh_interp.c b/source/blender/bmesh/intern/bmesh_interp.c
index 2afb9c9..e5921bb 100644
--- a/source/blender/bmesh/intern/bmesh_interp.c
+++ b/source/blender/bmesh/intern/bmesh_interp.c
@@ -257,24 +257,24 @@ static int compute_mdisp_quad(BMLoop *l, float v1[3], float v2[3], float v3[3],
 }
 
 /* funnily enough, I think this is identical to face_to_crn_interp, heh */
-static float quad_coord(float aa[3], float bb[3], float cc[3], float dd[3], int a1, int a2)
+static float quad_coord(const float aa[3], const float bb[3], const float cc[3], const float dd[3], int a1, int a2)
 {
 	float x, y, z, f1;
+	float div;
 	
 	x = aa[a1] * cc[a2] - cc[a1] * aa[a2];
 	y = aa[a1] * dd[a2] + bb[a1] * cc[a2] - cc[a1] * bb[a2] - dd[a1] * aa[a2];
 	z = bb[a1] * dd[a2] - dd[a1] * bb[a2];
-	
-	if (fabsf(2.0f * (x - y + z)) > FLT_EPSILON * 10.0f) {
-		float f2;
 
-		f1 = ( sqrtf(y * y - 4.0f * x * z) - y + 2.0f * z) / (2.0f * (x - y + z));
-		f2 = (-sqrtf(y * y - 4.0f * x * z) - y + 2.0f * z) / (2.0f * (x - y + z));
+	div = 2.0f * (x - y + z);
 
-		f1 = fabsf(f1);
-		f2 = fabsf(f2);
-		f1 = min_ff(f1, f2);
-		CLAMP(f1, 0.0f, 1.0f + FLT_EPSILON);
+	if (fabsf(div) > FLT_EPSILON * 10.0f) {
+		const float f_tmp = sqrtf(y * y - 4.0f * x * z);
+
+		f1 = min_ff(fabsf(( f_tmp - y + 2.0f * z) / div),
+		            fabsf((-f_tmp - y + 2.0f * z) / div));
+
+		CLAMP_MAX(f1, 1.0f + FLT_EPSILON);
 	}
 	else {
 		f1 = -z / (y - 2 * z);




More information about the Bf-blender-cvs mailing list