[cairo-commit] 2 commits - build/configure.ac.warnings test/.gitignore test/Makefile.am test/spline-decomposition.c test/spline-decomposition-pdf-ref.png test/spline-decomposition-ps2-ref.png test/spline-decomposition-ps3-ref.png test/spline-decomposition-ref.png test/spline-decomposition-svg11-ref.png test/spline-decomposition-svg12-ref.png

Chris Wilson ickle at kemper.freedesktop.org
Wed Oct 15 15:29:15 PDT 2008


 build/configure.ac.warnings             |    2 
 test/.gitignore                         |    1 
 test/Makefile.am                        |    7 
 test/spline-decomposition-pdf-ref.png   |binary
 test/spline-decomposition-ps2-ref.png   |binary
 test/spline-decomposition-ps3-ref.png   |binary
 test/spline-decomposition-ref.png       |binary
 test/spline-decomposition-svg11-ref.png |binary
 test/spline-decomposition-svg12-ref.png |binary
 test/spline-decomposition.c             |  416 ++++++++++++++++++++++++++++++++
 10 files changed, 425 insertions(+), 1 deletion(-)

New commits:
commit 6afcd821182137e8c3f128f9600e97cff79978b8
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Wed Oct 15 22:41:26 2008 +0100

    [test] Add a spline test.
    
    Test the decomposition of 5 different types of spline curve. Useful test
    for future experiments in improving the decomposition algorithm.
    
    Note: the vector targets all need separate reference images due to their
    lack of support for cairo_set_tolerance(). Also GS strokes the Bezier
    curve differently using offset curves and opposed to transcribing the
    outline of a pen.

diff --git a/test/.gitignore b/test/.gitignore
index e7ec82b..02f5647 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -204,6 +204,7 @@ solid-pattern-cache-stress
 source-clip
 source-clip-scale
 source-surface-scale-paint
+spline-decomposition
 stroke-image
 stroke-ctm-caps
 surface-finish-twice
diff --git a/test/Makefile.am b/test/Makefile.am
index 2a88d53..712a010 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -159,6 +159,7 @@ solid-pattern-cache-stress$(EXEEXT)			\
 source-clip$(EXEEXT)					\
 source-clip-scale$(EXEEXT)				\
 source-surface-scale-paint$(EXEEXT)			\
+spline-decomposition$(EXEEXT)				\
 surface-finish-twice$(EXEEXT)				\
 surface-pattern$(EXEEXT)				\
 surface-pattern-big-scale-down$(EXEEXT)			\
@@ -851,6 +852,12 @@ REFERENCE_IMAGES = \
 	source-clip-scale-pdf-ref.png \
 	source-surface-scale-paint-ref.png	\
 	source-surface-scale-paint-rgb24-ref.png	\
+	spline-decomposition-ref.png \
+	spline-decomposition-ps2-ref.png \
+	spline-decomposition-ps3-ref.png \
+	spline-decomposition-pdf-ref.png \
+	spline-decomposition-svg11-ref.png \
+	spline-decomposition-svg12-ref.png \
 	stroke-ctm-caps-ps2-ref.png \
 	stroke-ctm-caps-ps3-ref.png \
 	surface-pattern-big-scale-down-ref.png	\
diff --git a/test/spline-decomposition-pdf-ref.png b/test/spline-decomposition-pdf-ref.png
new file mode 100644
index 0000000..4fd25a6
Binary files /dev/null and b/test/spline-decomposition-pdf-ref.png differ
diff --git a/test/spline-decomposition-ps2-ref.png b/test/spline-decomposition-ps2-ref.png
new file mode 100644
index 0000000..ca1fbfd
Binary files /dev/null and b/test/spline-decomposition-ps2-ref.png differ
diff --git a/test/spline-decomposition-ps3-ref.png b/test/spline-decomposition-ps3-ref.png
new file mode 100644
index 0000000..ca1fbfd
Binary files /dev/null and b/test/spline-decomposition-ps3-ref.png differ
diff --git a/test/spline-decomposition-ref.png b/test/spline-decomposition-ref.png
new file mode 100644
index 0000000..4e1b344
Binary files /dev/null and b/test/spline-decomposition-ref.png differ
diff --git a/test/spline-decomposition-svg11-ref.png b/test/spline-decomposition-svg11-ref.png
new file mode 100644
index 0000000..4fd25a6
Binary files /dev/null and b/test/spline-decomposition-svg11-ref.png differ
diff --git a/test/spline-decomposition-svg12-ref.png b/test/spline-decomposition-svg12-ref.png
new file mode 100644
index 0000000..4fd25a6
Binary files /dev/null and b/test/spline-decomposition-svg12-ref.png differ
diff --git a/test/spline-decomposition.c b/test/spline-decomposition.c
new file mode 100644
index 0000000..ca6b007
--- /dev/null
+++ b/test/spline-decomposition.c
@@ -0,0 +1,416 @@
+/*
+ * Copyright 2008 Chris Wilson
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Chris Wilson not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Chris Wilson makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Chris Wilson <chris at chris-wilson.co.uk>
+ */
+
+#include "cairo-test.h"
+
+static cairo_test_draw_function_t draw;
+
+static const cairo_test_t test = {
+    "spline-decomposition",
+    "Tests splines with various inflection points",
+    390, 260,
+    draw
+};
+
+typedef struct _point {
+    double x,y;
+} point_t;
+
+typedef struct _knots {
+    point_t a,b,c,d;
+} knots_t;
+
+static knots_t knots[5] = {
+    { {0, 0}, {0, 100}, {100, 100}, {100, 0} },
+    { {0, 0}, {75, 100}, {25, 100}, {100, 0} },
+    { {0, 0}, {100, 100}, {0, 100}, {100, 0} },
+    { {0, 0}, {150, 100}, {-50, 100}, {100, 0} },
+    { {0, 0}, {100, 200}, {0, -100}, {100, 100} },
+};
+
+#ifdef REFERENCE
+static void
+_lerp_half (const point_t *a, const point_t *b, point_t *result)
+{
+    result->x = .5 * (a->x + b->x);
+    result->y = .5 * (a->y + b->y);
+}
+
+static void
+_de_casteljau (knots_t *k1, knots_t *k2)
+{
+    point_t ab, bc, cd;
+    point_t abbc, bccd;
+    point_t final;
+
+    _lerp_half (&k1->a, &k1->b, &ab);
+    _lerp_half (&k1->b, &k1->c, &bc);
+    _lerp_half (&k1->c, &k1->d, &cd);
+    _lerp_half (&ab, &bc, &abbc);
+    _lerp_half (&bc, &cd, &bccd);
+    _lerp_half (&abbc, &bccd, &final);
+
+    k2->a = final;
+    k2->b = bccd;
+    k2->c = cd;
+    k2->d = k1->d;
+
+    k1->b = ab;
+    k1->c = abbc;
+    k1->d = final;
+}
+
+static double
+_spline_error_squared (const knots_t *knots)
+{
+    double bdx, bdy, berr;
+    double cdx, cdy, cerr;
+    double dx, dy, v;
+
+    /* Intersection point (px):
+     *	    px = p1 + u(p2 - p1)
+     *	    (p - px) ∙ (p2 - p1) = 0
+     * Thus:
+     *	    u = ((p - p1) ∙ (p2 - p1)) / ∥p2 - p1∥²;
+     */
+    bdx = knots->b.x - knots->a.x;
+    bdy = knots->b.y - knots->a.y;
+
+    cdx = knots->c.x - knots->a.x;
+    cdy = knots->c.y - knots->a.y;
+
+    dx = knots->d.x - knots->a.x;
+    dy = knots->d.y - knots->a.y;
+    v = dx * dx + dy * dy;
+    if (v != 0.) {
+	double u;
+
+	u = bdx * dx + bdy * dy;
+	if (u <= 0) {
+	    /* bdx -= 0;
+	     * bdy -= 0;
+	     */
+	} else if (u >= v) {
+	    bdx -= dx;
+	    bdy -= dy;
+	} else {
+	    bdx -= u/v * dx;
+	    bdy -= u/v * dy;
+	}
+
+	u = cdx * dx + cdy * dy;
+	if (u <= 0) {
+	    /* cdx -= 0;
+	     * cdy -= 0;
+	     */
+	} else if (u >= v) {
+	    cdx -= dx;
+	    cdy -= dy;
+	} else {
+	    cdx -= u/v * dx;
+	    cdy -= u/v * dy;
+	}
+    }
+
+    berr = bdx * bdx + bdy * bdy;
+    cerr = cdx * cdx + cdy * cdy;
+    if (berr > cerr)
+	return berr * v;
+    else
+	return cerr * v;
+}
+
+static void
+_offset_line_to (cairo_t *cr,
+		 const point_t *p0,
+		 const point_t *p1,
+		 const point_t *p2,
+		 const point_t *p3,
+		 double offset)
+{
+    double dx, dy, v;
+
+    dx = p1->x - p0->x;
+    dy = p1->y - p0->y;
+     v = hypot (dx, dy);
+     if (v == 0) {
+	 dx = p2->x - p0->x;
+	 dy = p2->y - p0->y;
+	 v = hypot (dx, dy);
+	 if (v == 0) {
+	     dx = p3->x - p0->x;
+	     dy = p3->y - p0->y;
+	     v = hypot (dx, dy);
+	 }
+     }
+
+     if (v == 0) {
+	 cairo_line_to (cr, p0->x, p0->y);
+     } else
+	 cairo_line_to (cr, p0->x - offset * dy / v, p0->y + offset * dx / v);
+}
+
+static void
+_spline_decompose_into (knots_t *k1,
+			double tolerance_squared,
+			double offset,
+			cairo_t *cr)
+{
+    knots_t k2;
+
+    if (_spline_error_squared (k1) < tolerance_squared) {
+	_offset_line_to (cr, &k1->a, &k1->b, &k1->c, &k1->d, offset);
+	return;
+    }
+
+    _de_casteljau (k1, &k2);
+
+    _spline_decompose_into (k1, tolerance_squared, offset, cr);
+    _spline_decompose_into (&k2, tolerance_squared, offset, cr);
+}
+
+static void
+_spline_decompose (const knots_t *knots,
+		   double tolerance, double offset,
+		   cairo_t *cr)
+{
+    knots_t k;
+
+    k = *knots;
+    _spline_decompose_into (&k, tolerance * tolerance, offset, cr);
+
+    _offset_line_to (cr, &knots->d, &knots->c, &knots->b, &knots->a, -offset);
+}
+
+static void
+_knots_reverse (knots_t *knots)
+{
+    point_t tmp;
+
+    tmp = knots->a;
+    knots->a = knots->d;
+    knots->d = tmp;
+
+    tmp = knots->b;
+    knots->b = knots->c;
+    knots->c = tmp;
+}
+
+static void
+thick_splines (cairo_t *cr, double offset)
+{
+    knots_t k;
+
+    cairo_save (cr);
+    cairo_translate (cr, 15, 15);
+
+    k = knots[0];
+
+    cairo_new_path (cr);
+    _spline_decompose (&k, .1, offset, cr);
+    _knots_reverse (&k);
+    _spline_decompose (&k, .1, offset, cr);
+    cairo_close_path (cr);
+    cairo_fill (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    k = knots[1];
+
+    cairo_new_path (cr);
+    _spline_decompose (&k, .1, offset, cr);
+    _knots_reverse (&k);
+    _spline_decompose (&k, .1, offset, cr);
+    cairo_close_path (cr);
+    cairo_fill (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    k = knots[2];
+
+    cairo_new_path (cr);
+    _spline_decompose (&k, .1, offset, cr);
+    _knots_reverse (&k);
+    _spline_decompose (&k, .1, offset, cr);
+    cairo_close_path (cr);
+    cairo_fill (cr);
+
+    cairo_translate (cr, -130 - 65, 130);
+
+    k = knots[3];
+
+    cairo_new_path (cr);
+    _spline_decompose (&k, .1, offset, cr);
+    _knots_reverse (&k);
+    _spline_decompose (&k, .1, offset, cr);
+    cairo_close_path (cr);
+    cairo_fill (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    k = knots[4];
+
+    cairo_new_path (cr);
+    _spline_decompose (&k, .1, offset, cr);
+    _knots_reverse (&k);
+    _spline_decompose (&k, .1, offset, cr);
+    cairo_close_path (cr);
+    cairo_fill (cr);
+    cairo_restore (cr);
+}
+
+static void
+thin_splines (cairo_t *cr)
+{
+    cairo_save (cr);
+    cairo_translate (cr, 15, 15);
+
+    cairo_new_path (cr);
+    _spline_decompose (&knots[0], .1, 0, cr);
+    cairo_stroke (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    cairo_new_path (cr);
+    _spline_decompose (&knots[1], .1, 0, cr);
+    cairo_stroke (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    cairo_new_path (cr);
+    _spline_decompose (&knots[2], .1, 0, cr);
+    cairo_stroke (cr);
+
+    cairo_translate (cr, -130 - 65, 130);
+
+    cairo_new_path (cr);
+    _spline_decompose (&knots[3], .1, 0, cr);
+    cairo_stroke (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    cairo_new_path (cr);
+    _spline_decompose (&knots[4], .1, 0, cr);
+    cairo_stroke (cr);
+    cairo_restore (cr);
+}
+#endif
+
+static void
+stroke_splines (cairo_t *cr)
+{
+    cairo_save (cr);
+    cairo_translate (cr, 15, 15);
+
+    cairo_new_path (cr);
+    cairo_move_to (cr,
+		   knots[0].a.x, knots[0].a.y);
+    cairo_curve_to (cr,
+		    knots[0].b.x, knots[0].b.y,
+		    knots[0].c.x, knots[0].c.y,
+		    knots[0].d.x, knots[0].d.y);
+    cairo_stroke (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    cairo_new_path (cr);
+    cairo_move_to (cr,
+		   knots[1].a.x, knots[1].a.y);
+    cairo_curve_to (cr,
+		    knots[1].b.x, knots[1].b.y,
+		    knots[1].c.x, knots[1].c.y,
+		    knots[1].d.x, knots[1].d.y);
+    cairo_stroke (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    cairo_new_path (cr);
+    cairo_move_to (cr,
+		   knots[2].a.x, knots[2].a.y);
+    cairo_curve_to (cr,
+		    knots[2].b.x, knots[2].b.y,
+		    knots[2].c.x, knots[2].c.y,
+		    knots[2].d.x, knots[2].d.y);
+    cairo_stroke (cr);
+
+    cairo_translate (cr, -130 - 65, 130);
+
+    cairo_new_path (cr);
+    cairo_move_to (cr,
+		   knots[3].a.x, knots[3].a.y);
+    cairo_curve_to (cr,
+		    knots[3].b.x, knots[3].b.y,
+		    knots[3].c.x, knots[3].c.y,
+		    knots[3].d.x, knots[3].d.y);
+    cairo_stroke (cr);
+
+    cairo_translate (cr, 130, 0);
+
+    cairo_new_path (cr);
+    cairo_move_to (cr,
+		   knots[4].a.x, knots[4].a.y);
+    cairo_curve_to (cr,
+		    knots[4].b.x, knots[4].b.y,
+		    knots[4].c.x, knots[4].c.y,
+		    knots[4].d.x, knots[4].d.y);
+    cairo_stroke (cr);
+    cairo_restore (cr);
+}
+
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height)
+{
+    cairo_set_source_rgb (cr, 1, 1, 1);
+    cairo_paint (cr);
+
+#ifdef REFERENCE
+    cairo_set_source_rgb (cr, 0, 0, 0);
+    thick_splines (cr, 5);
+
+    cairo_set_source_rgb (cr, 1, 1, 1);
+    thin_splines (cr);
+#endif
+
+    /*
+     * Use a high tolerance to reduce dependence upon algorithm used for
+     * spline decomposition.
+     */
+    cairo_set_tolerance (cr, 0.001);
+
+    cairo_set_line_width (cr, 10);
+    cairo_set_source_rgb (cr, 0, 0, 0);
+    stroke_splines (cr);
+    cairo_set_line_width (cr, 2);
+    cairo_set_source_rgb (cr, 1, 1, 1);
+    stroke_splines (cr);
+
+    return CAIRO_TEST_SUCCESS;
+}
+
+int
+main (void)
+{
+    return cairo_test (&test);
+}
commit 91b0dc92c8ede189d942188f92068f189907e239
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Wed Oct 15 22:27:53 2008 +0100

    [build] Add -fno-common.
    
    Generate a warning if two files define a global variable with the same
    name, instead of silently merging them to reference the same location.

diff --git a/build/configure.ac.warnings b/build/configure.ac.warnings
index 3756439..2d111a0 100644
--- a/build/configure.ac.warnings
+++ b/build/configure.ac.warnings
@@ -17,7 +17,7 @@ MAYBE_WARN="-Wall -Wextra \
 
 dnl We also abuse the warning-flag facility to enable other compiler
 dnl options.  Namely, the following:
-MAYBE_WARN="$MAYBE_WARN -fno-strict-aliasing"
+MAYBE_WARN="$MAYBE_WARN -fno-strict-aliasing -fno-common"
 
 dnl Also to turn various gcc/glibc-specific preprocessor checks
 MAYBE_WARN="$MAYBE_WARN -Wp,-D_FORTIFY_SOURCE=2"


More information about the cairo-commit mailing list