[gtk/path-stroke: 7/27] Add curve intersection tests




commit 3ceee6d3ff6b931df6fdd4ffb97661601cd42f53
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Dec 7 11:14:33 2020 -0500

    Add curve intersection tests
    
    These tests check that gsk_curve_intersect finds
    the intersections we want.

 testsuite/gsk/curve.c     | 99 +++++++++++++++++++++++++++++++++++++++++++++++
 testsuite/gsk/meson.build |  2 +-
 2 files changed, 100 insertions(+), 1 deletion(-)
---
diff --git a/testsuite/gsk/curve.c b/testsuite/gsk/curve.c
index 649149f360..20619874be 100644
--- a/testsuite/gsk/curve.c
+++ b/testsuite/gsk/curve.c
@@ -191,6 +191,102 @@ test_curve_decompose (void)
     }
 }
 
+static void
+test_line_line_intersection (void)
+{
+  GskCurve c1, c2;
+  graphene_point_t p1[2], p2[2];
+  float t1, t2;
+  graphene_point_t p;
+  int n;
+
+  graphene_point_init (&p1[0], 10, 0);
+  graphene_point_init (&p1[1], 10, 100);
+  graphene_point_init (&p2[0], 0, 10);
+  graphene_point_init (&p2[1], 100, 10);
+
+  gsk_curve_init (&c1, gsk_pathop_encode (GSK_PATH_LINE, p1));
+  gsk_curve_init (&c2, gsk_pathop_encode (GSK_PATH_LINE, p2));
+
+  n = gsk_curve_intersect (&c1, &c2, &t1, &t2, &p, 1);
+
+  g_assert_cmpint (n, ==, 1);
+  g_assert_cmpfloat_with_epsilon (t1, 0.1, 0.0001);
+  g_assert_cmpfloat_with_epsilon (t2, 0.1, 0.0001);
+  g_assert_true (graphene_point_near (&p, &GRAPHENE_POINT_INIT (10, 10), 0.0001));
+}
+
+static void
+test_line_curve_intersection (void)
+{
+  GskCurve c1, c2;
+  graphene_point_t p1[4], p2[2];
+  float t1[9], t2[9];
+  graphene_point_t p[9];
+  int n;
+  graphene_rect_t b;
+
+  graphene_point_init (&p1[0], 0, 100);
+  graphene_point_init (&p1[1], 50, 100);
+  graphene_point_init (&p1[2], 50, 0);
+  graphene_point_init (&p1[3], 100, 0);
+  graphene_point_init (&p2[0], 0, 0);
+  graphene_point_init (&p2[1], 100, 100);
+
+  gsk_curve_init (&c1, gsk_pathop_encode (GSK_PATH_CURVE, p1));
+  gsk_curve_init (&c2, gsk_pathop_encode (GSK_PATH_LINE, p2));
+
+  n = gsk_curve_intersect (&c1, &c2, t1, t2, p, 1);
+
+  g_assert_cmpint (n, ==, 1);
+  g_assert_cmpfloat_with_epsilon (t1[0], 0.5, 0.0001);
+  g_assert_cmpfloat_with_epsilon (t2[0], 0.5, 0.0001);
+  g_assert_true (graphene_point_near (&p[0], &GRAPHENE_POINT_INIT (50, 50), 0.0001));
+
+  gsk_curve_get_tight_bounds (&c1, &b);
+  graphene_rect_contains_point (&b, &p[0]);
+
+  gsk_curve_get_tight_bounds (&c2, &b);
+  graphene_rect_contains_point (&b, &p[0]);
+}
+
+static void
+test_curve_curve_intersection (void)
+{
+  GskCurve c1, c2;
+  graphene_point_t p1[4], p2[4];
+  float t1[9], t2[9];
+  graphene_point_t p[9];
+  int n;
+  graphene_rect_t b;
+
+  graphene_point_init (&p1[0], 0, 0);
+  graphene_point_init (&p1[1], 33.333, 100);
+  graphene_point_init (&p1[2], 66.667, 0);
+  graphene_point_init (&p1[3], 100, 100);
+  graphene_point_init (&p2[0], 0, 50);
+  graphene_point_init (&p2[1], 100, 0);
+  graphene_point_init (&p2[2], 20, 0); // weight 20
+  graphene_point_init (&p2[3], 50, 100);
+
+  gsk_curve_init (&c1, gsk_pathop_encode (GSK_PATH_CURVE, p1));
+  gsk_curve_init (&c2, gsk_pathop_encode (GSK_PATH_CONIC, p2));
+
+  n = gsk_curve_intersect (&c1, &c2, t1, t2, p, 9);
+
+  g_assert_cmpint (n, ==, 2);
+  g_assert_cmpfloat (t1[0], <, 0.5);
+  g_assert_cmpfloat (t1[1], >, 0.5);
+  g_assert_cmpfloat (t2[0], <, 0.5);
+  g_assert_cmpfloat (t2[1], >, 0.5);
+
+  gsk_curve_get_tight_bounds (&c1, &b);
+  graphene_rect_contains_point (&b, &p[0]);
+
+  gsk_curve_get_tight_bounds (&c2, &b);
+  graphene_rect_contains_point (&b, &p[0]);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -199,6 +295,9 @@ main (int argc, char *argv[])
   g_test_add_func ("/curve/points", test_curve_points);
   g_test_add_func ("/curve/tangents", test_curve_tangents);
   g_test_add_func ("/curve/decompose", test_curve_decompose);
+  g_test_add_func ("/curve/intersection/line-line", test_line_line_intersection);
+  g_test_add_func ("/curve/intersection/line-curve", test_line_curve_intersection);
+  g_test_add_func ("/curve/intersection/curve-curve", test_curve_curve_intersection);
 
   return g_test_run ();
 }
diff --git a/testsuite/gsk/meson.build b/testsuite/gsk/meson.build
index 44f30495fc..c2fd5b848b 100644
--- a/testsuite/gsk/meson.build
+++ b/testsuite/gsk/meson.build
@@ -193,7 +193,7 @@ foreach test : node_parser_tests
 endforeach
 
 tests = [
-  ['curve', ['../../gsk/gskcurve.c'], ['-DGTK_COMPILATION']],
+  ['curve', ['../../gsk/gskcurve.c', '../../gsk/gskcurveintersect.c'], ['-DGTK_COMPILATION']],
   ['curve-special-cases', ['../../gsk/gskcurve.c'], ['-DGTK_COMPILATION']],
   ['dash'],
   ['path'],


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]