Skip to content

Commit

Permalink
Merge pull request #271 from ebassi/issue-250
Browse files Browse the repository at this point in the history
Add identity matrix decomposition
  • Loading branch information
ebassi authored Aug 12, 2024
2 parents 0885352 + fcf96e5 commit a4fa180
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/graphene-matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,15 @@ graphene_matrix_decompose (const graphene_matrix_t *m,
graphene_vec3_t *shear,
graphene_vec4_t *perspective)
{
if (graphene_matrix_is_2d (m))
if (graphene_matrix_is_identity (m))
{
translate->value = graphene_simd4f_init_zero ();
scale->value = graphene_simd4f_init (1.f, 1.f, 1.f, 0.f);
graphene_quaternion_init_from_angles (rotate, 0.f, 0.f, 0.f);
shear->value = graphene_simd4f_init_zero ();
perspective->value = graphene_simd4f_init_zero ();
}
else if (graphene_matrix_is_2d (m))
{
graphene_vec2_t translate_res;
graphene_vec2_t scale_res;
Expand Down
35 changes: 33 additions & 2 deletions tests/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ matrix_invert (void)
graphene_matrix_init_identity (&identity);

graphene_matrix_init_identity (&m);

graphene_matrix_inverse (&m , &inv);
graphene_matrix_multiply (&m, &inv, &res);
mutest_expect ("inverting an identity to return an identity",
mutest_pointer (&res),
graphene_test_matrix_near, mutest_pointer (&identity),
NULL);

graphene_matrix_scale (&m, 1.0f, 2.0f, 3.0f);
graphene_matrix_inverse (&m , &inv);
graphene_matrix_multiply (&m, &inv, &res);
Expand Down Expand Up @@ -748,6 +748,36 @@ matrix_3d_transform_point (void)
NULL);
}

static void
matrix_decompose_3d (void)
{
graphene_matrix_t m;
graphene_vec3_t t;
graphene_vec3_t s;
graphene_quaternion_t r;
graphene_vec3_t sh;
graphene_vec4_t p;

graphene_matrix_init_identity (&m);
mutest_expect ("decompose identity matrix",
mutest_bool_value (graphene_matrix_decompose (&m, &t, &s, &r, &sh, &p)),
mutest_to_be_true,
NULL);

mutest_expect ("translation is (0, 0, 0)",
mutest_bool_value (graphene_vec3_equal (&t, graphene_vec3_zero ())),
mutest_to_be_true,
NULL);
mutest_expect ("issue 250: scale is (1, 1, 1)",
mutest_bool_value (graphene_vec3_equal (&s, graphene_vec3_one ())),
mutest_to_be_true,
NULL);
mutest_expect ("shear is (0, 0, 0)",
mutest_bool_value (graphene_vec3_equal (&sh, graphene_vec3_zero ())),
mutest_to_be_true,
NULL);
}

static void
matrix_suite (void)
{
Expand All @@ -770,6 +800,7 @@ matrix_suite (void)
mutest_it ("can interpolate 2D transformations", matrix_2d_interpolate);
mutest_it ("can transform 2D bounds", matrix_2d_transform_bound);
mutest_it ("can transform 3D points", matrix_3d_transform_point);
mutest_it ("can decompose a 3D matrix", matrix_decompose_3d);
}

MUTEST_MAIN (
Expand Down

0 comments on commit a4fa180

Please sign in to comment.