[gtk/curve-ops: 3/5] Add curve intersection tests




commit 8ad057fbaa4f948bccf09abde05fdf8462caa93b
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Dec 7 00:07:14 2020 -0500

    Add curve intersection tests
    
    Add some basic tests for gsk_curve_intersect.

 gsk/gskpathopprivate.h    |  2 +-
 testsuite/gsk/curve.c     | 96 +++++++++++++++++++++++++++++++++++++++++++++++
 testsuite/gsk/meson.build |  1 +
 3 files changed, 98 insertions(+), 1 deletion(-)
---
diff --git a/gsk/gskpathopprivate.h b/gsk/gskpathopprivate.h
index 2b2e7aec2a..cc80fb9423 100644
--- a/gsk/gskpathopprivate.h
+++ b/gsk/gskpathopprivate.h
@@ -21,7 +21,7 @@
 #ifndef __GSK_PATHOP_PRIVATE_H__
 #define __GSK_PATHOP_PRIVATE_H__
 
-#include <gskpath.h>
+#include <gsk/gskpath.h>
 
 G_BEGIN_DECLS
 
diff --git a/testsuite/gsk/curve.c b/testsuite/gsk/curve.c
new file mode 100644
index 0000000000..a58a1fb061
--- /dev/null
+++ b/testsuite/gsk/curve.c
@@ -0,0 +1,96 @@
+#include <gtk/gtk.h>
+#include "gsk/gskcurveprivate.h"
+
+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, t2;
+  graphene_point_t p;
+  int n;
+
+  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.5, 0.0001);
+  g_assert_cmpfloat_with_epsilon (t2, 0.5, 0.0001);
+  g_assert_true (graphene_point_near (&p, &GRAPHENE_POINT_INIT (50, 50), 0.0001));
+}
+
+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_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);
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+
+  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 b6930d07fc..f46a89f14f 100644
--- a/testsuite/gsk/meson.build
+++ b/testsuite/gsk/meson.build
@@ -188,6 +188,7 @@ foreach test : node_parser_tests
 endforeach
 
 tests = [
+  ['curve', ['../../gsk/gskcurve.c'], ['-DGTK_COMPILATION']],
   ['path'],
   ['path-special-cases'],
   ['rounded-rect'],


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