curve: Add tests for length

Add some tests for gsk_curve_get_length.
This commit is contained in:
Matthias Clasen 2023-08-23 22:02:08 -04:00
parent 04e6fc3f74
commit 787b1a661e
2 changed files with 82 additions and 28 deletions

View File

@ -165,6 +165,8 @@ test_circle (void)
gsk_curve_get_end_tangent (&c, &tangent);
g_assert_true (graphene_vec2_equal (&tangent, graphene_vec2_init (&tangent2, -1, 0)));
g_assert_cmpfloat_with_epsilon (gsk_curve_get_length (&c), M_PI_2, 0.001);
for (int i = 1; i < 10; i++)
{
float t = i / 10.f;
@ -180,6 +182,25 @@ test_circle (void)
}
}
static void
test_curve_length (void)
{
GskCurve c, c1, c2;
float l, l1, l2, l1a;
parse_curve (&c, "M 1462.632080 -1593.118896 C 751.533630 -74.179169 -914.280090 956.537720 -83.091866 207.213776");
gsk_curve_split (&c, 0.5, &c1, &c2);
l = gsk_curve_get_length (&c);
l1a = gsk_curve_get_length_to (&c, 0.5);
l1 = gsk_curve_get_length (&c1);
l2 = gsk_curve_get_length (&c2);
g_assert_cmpfloat_with_epsilon (l1, l1a, 0.1);
g_assert_cmpfloat_with_epsilon (l, l1 + l2, 0.5);
}
int
main (int argc,
char *argv[])
@ -190,6 +211,7 @@ main (int argc,
g_test_add_func ("/curve/special/degenerate-tangents", test_curve_degenerate_tangents);
g_test_add_func ("/curve/special/crossing", test_curve_crossing);
g_test_add_func ("/curve/special/circle", test_circle);
g_test_add_func ("/curve/special/length", test_curve_length);
return g_test_run ();
}

View File

@ -300,42 +300,53 @@ test_curve_decompose_into_cubic (void)
static void
test_curve_split (void)
{
for (int i = 0; i < 100; i++)
for (int i = 0; i < 20; i++)
{
GskCurve c;
GskCurve c1, c2;
graphene_point_t p;
graphene_vec2_t t, t1, t2;
init_random_curve (&c);
gsk_curve_split (&c, 0.5, &c1, &c2);
for (int j = 0; j < 20; j++)
{
GskCurve c1, c2;
graphene_point_t p;
graphene_vec2_t t, t1, t2;
float split;
g_assert_true (c1.op == c.op);
g_assert_true (c2.op == c.op);
split = g_test_rand_double_range (0, 1);
g_assert_true (graphene_point_near (gsk_curve_get_start_point (&c),
gsk_curve_get_start_point (&c1), 0.005));
g_assert_true (graphene_point_near (gsk_curve_get_end_point (&c1),
gsk_curve_get_start_point (&c2), 0.005));
g_assert_true (graphene_point_near (gsk_curve_get_end_point (&c),
gsk_curve_get_end_point (&c2), 0.005));
gsk_curve_get_point (&c, 0.5, &p);
gsk_curve_get_tangent (&c, 0.5, &t);
g_assert_true (graphene_point_near (gsk_curve_get_end_point (&c1), &p, 0.005));
g_assert_true (graphene_point_near (gsk_curve_get_start_point (&c2), &p, 0.005));
gsk_curve_split (&c, split, &c1, &c2);
gsk_curve_get_start_tangent (&c, &t1);
gsk_curve_get_start_tangent (&c1, &t2);
g_assert_true (graphene_vec2_near (&t1, &t2, 0.005));
gsk_curve_get_end_tangent (&c1, &t1);
gsk_curve_get_start_tangent (&c2, &t2);
g_assert_true (graphene_vec2_near (&t1, &t2, 0.005));
g_assert_true (graphene_vec2_near (&t, &t1, 0.005));
g_assert_true (graphene_vec2_near (&t, &t2, 0.005));
gsk_curve_get_end_tangent (&c, &t1);
gsk_curve_get_end_tangent (&c2, &t2);
g_assert_true (graphene_vec2_near (&t1, &t2, 0.005));
g_assert_true (c1.op == c.op);
g_assert_true (c2.op == c.op);
g_assert_true (graphene_point_near (gsk_curve_get_start_point (&c),
gsk_curve_get_start_point (&c1), 0.005));
g_assert_true (graphene_point_near (gsk_curve_get_end_point (&c1),
gsk_curve_get_start_point (&c2), 0.005));
g_assert_true (graphene_point_near (gsk_curve_get_end_point (&c),
gsk_curve_get_end_point (&c2), 0.005));
gsk_curve_get_point (&c, split, &p);
gsk_curve_get_tangent (&c, split, &t);
g_assert_true (graphene_point_near (gsk_curve_get_end_point (&c1), &p, 0.005));
g_assert_true (graphene_point_near (gsk_curve_get_start_point (&c2), &p, 0.005));
gsk_curve_get_start_tangent (&c, &t1);
gsk_curve_get_start_tangent (&c1, &t2);
g_assert_true (graphene_vec2_near (&t1, &t2, 0.005));
gsk_curve_get_end_tangent (&c1, &t1);
gsk_curve_get_start_tangent (&c2, &t2);
g_assert_true (graphene_vec2_near (&t1, &t2, 0.005));
g_assert_true (graphene_vec2_near (&t, &t1, 0.005));
g_assert_true (graphene_vec2_near (&t, &t2, 0.005));
gsk_curve_get_end_tangent (&c, &t1);
gsk_curve_get_end_tangent (&c2, &t2);
g_assert_true (graphene_vec2_near (&t1, &t2, 0.005));
g_assert_cmpfloat_with_epsilon (gsk_curve_get_length (&c),
gsk_curve_get_length (&c1) + gsk_curve_get_length (&c2),
1);
}
}
}
@ -363,6 +374,26 @@ test_curve_derivative (void)
}
}
static void
test_curve_length (void)
{
GskCurve c;
float l, l0;
for (int i = 0; i < 1000; i++)
{
init_random_curve (&c);
l = gsk_curve_get_length (&c);
l0 = graphene_point_distance (gsk_curve_get_start_point (&c),
gsk_curve_get_end_point (&c),
NULL, NULL);
g_assert_true (l >= l0);
if (c.op == GSK_PATH_LINE)
g_assert_true (l == l0);
}
}
int
main (int argc, char *argv[])
{
@ -376,6 +407,7 @@ main (int argc, char *argv[])
g_test_add_func ("/curve/decompose-cubic", test_curve_decompose_into_cubic);
g_test_add_func ("/curve/split", test_curve_split);
g_test_add_func ("/curve/derivative", test_curve_derivative);
g_test_add_func ("/curve/length", test_curve_length);
return g_test_run ();
}