[cairo-commit] 3 commits - src/cairo-spline.c

Behdad Esfahbod behdad at kemper.freedesktop.org
Sat Dec 27 22:59:41 PST 2008


 src/cairo-spline.c |   66 ++++++++++++++++++++++++++++-------------------------
 1 file changed, 35 insertions(+), 31 deletions(-)

New commits:
commit 3292f9906b3637564c37863dde5d214b0fee2885
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sun Dec 28 01:59:12 2008 -0500

    [spline] Do some checks to avoid calling sqrt() if no feasible solution exists

diff --git a/src/cairo-spline.c b/src/cairo-spline.c
index 9d8c200..1b6f4cf 100644
--- a/src/cairo-spline.c
+++ b/src/cairo-spline.c
@@ -271,15 +271,28 @@ _cairo_spline_bound (cairo_spline_add_point_func_t add_point_func,
 
 #define FIND_EXTREMES(a,b,c) \
     { \
-	double delta = b * b - a * c; \
 	if (a == 0) { \
 	    ADD (-c / (2*b)); \
-	} else if (delta > 0) { \
-	    double sqrt_delta = sqrt (delta); \
-	    ADD ((-b - sqrt_delta) / a); \
-	    ADD ((-b + sqrt_delta) / a); \
-	} else if (delta == 0) { \
-	    ADD (-b / a); \
+	} else { \
+	    double b2 = b * b; \
+	    double delta = b2 - a * c; \
+	    if (delta > 0) { \
+		double a2 = a * a; \
+		double ab = a * b; \
+		/* We are only interested in solutions t that satisfy 0<t<1 \
+		 * here.  We do some checks to avoid sqrt if the solutions \
+		 * are not in that range.  The checks can be derived from: \
+		 * \
+		 *   0 < (-b±√delta)/a < 1 \
+		 */ \
+		if (delta > (ab >= 0 ? b2 : a2 + b2 + 2*ab)) { \
+		    double sqrt_delta = sqrt (delta); \
+		    ADD ((-b - sqrt_delta) / a); \
+		    ADD ((-b + sqrt_delta) / a); \
+		} \
+	    } else if (delta == 0) { \
+		ADD (-b / a); \
+	    } \
 	} \
     }
 
commit efe4d2ce99e65f9841951d3ff3e0536973bfb12c
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sun Dec 28 01:22:40 2008 -0500

    [spline] Simplify code

diff --git a/src/cairo-spline.c b/src/cairo-spline.c
index f8c0267..9d8c200 100644
--- a/src/cairo-spline.c
+++ b/src/cairo-spline.c
@@ -263,24 +263,23 @@ _cairo_spline_bound (cairo_spline_add_point_func_t add_point_func,
      */
 
 #define ADD(t0) \
-	if (0 < (t0) && (t0) < 1) \
-	    t[t_num++] = (t0);
+    { \
+	double _t0 = (t0); \
+	if (0 < _t0 && _t0 < 1) \
+	    t[t_num++] = _t0; \
+    }
 
 #define FIND_EXTREMES(a,b,c) \
     { \
 	double delta = b * b - a * c; \
 	if (a == 0) { \
-	    double t0 = -c / (2*b); \
-	    ADD (t0); \
+	    ADD (-c / (2*b)); \
 	} else if (delta > 0) { \
 	    double sqrt_delta = sqrt (delta); \
-	    double t1 = (-b - sqrt_delta) / a; \
-	    double t2 = (-b + sqrt_delta) / a; \
-	    ADD (t1); \
-	    ADD (t2); \
+	    ADD ((-b - sqrt_delta) / a); \
+	    ADD ((-b + sqrt_delta) / a); \
 	} else if (delta == 0) { \
-	    double t0 = -b / a; \
-	    ADD (t0); \
+	    ADD (-b / a); \
 	} \
     }
 
commit 8672178bf6c7f3a38e11e224f1d484b1f0c7fd7b
Author: Behdad Esfahbod <behdad at behdad.org>
Date:   Sun Dec 28 01:20:37 2008 -0500

    [spline] Remove duplicated code by using a macro

diff --git a/src/cairo-spline.c b/src/cairo-spline.c
index 9ae15f9..f8c0267 100644
--- a/src/cairo-spline.c
+++ b/src/cairo-spline.c
@@ -217,7 +217,7 @@ _cairo_spline_bound (cairo_spline_add_point_func_t add_point_func,
 {
     double x0, x1, x2, x3;
     double y0, y1, y2, y3;
-    double a, b, c, delta;
+    double a, b, c;
     double t[4];
     int t_num = 0, i;
 
@@ -266,43 +266,35 @@ _cairo_spline_bound (cairo_spline_add_point_func_t add_point_func,
 	if (0 < (t0) && (t0) < 1) \
 	    t[t_num++] = (t0);
 
+#define FIND_EXTREMES(a,b,c) \
+    { \
+	double delta = b * b - a * c; \
+	if (a == 0) { \
+	    double t0 = -c / (2*b); \
+	    ADD (t0); \
+	} else if (delta > 0) { \
+	    double sqrt_delta = sqrt (delta); \
+	    double t1 = (-b - sqrt_delta) / a; \
+	    double t2 = (-b + sqrt_delta) / a; \
+	    ADD (t1); \
+	    ADD (t2); \
+	} else if (delta == 0) { \
+	    double t0 = -b / a; \
+	    ADD (t0); \
+	} \
+    }
+
     /* Find X extremes */
     a = -x0 + 3*x1 - 3*x2 + x3;
     b =  x0 - 2*x1 + x2;
     c = -x0 + x1;
-    delta = b * b - a * c;
-    if (a == 0) {
-	double t0 = -c / (2*b);
-	ADD (t0);
-    } else if (delta > 0) {
-	double sqrt_delta = sqrt (delta);
-	double t1 = (-b - sqrt_delta) / a;
-	double t2 = (-b + sqrt_delta) / a;
-	ADD (t1);
-	ADD (t2);
-    } else if (delta == 0) {
-	double t0 = -b / a;
-	ADD (t0);
-    }
+    FIND_EXTREMES (a, b, c);
 
     /* Find Y extremes */
     a = -y0 + 3*y1 - 3*y2 + y3;
     b =  y0 - 2*y1 + y2;
     c = -y0 + y1;
-    delta = b * b - a * c;
-    if (a == 0) {
-	double t0 = -c / (2*b);
-	ADD (t0);
-    } else if (delta > 0) {
-	double sqrt_delta = sqrt (delta);
-	double t1 = (-b - sqrt_delta) / a;
-	double t2 = (-b + sqrt_delta) / a;
-	ADD (t1);
-	ADD (t2);
-    } else if (delta == 0) {
-	double t0 = -b / a;
-	ADD (t0);
-    }
+    FIND_EXTREMES (a, b, c);
 
     add_point_func (closure, p0);
     for (i = 0; i < t_num; i++) {


More information about the cairo-commit mailing list