summaryrefslogtreecommitdiff
path: root/libs/cglm/test/src/test_affine_mat.h
blob: e928f8e10324f84d6e70baea629619127a02c38a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (c), Recep Aslantas.
 *
 * MIT License (MIT), http://opensource.org/licenses/MIT
 * Full license can be found in the LICENSE file
 */

#include "test_common.h"

#ifndef glm_affine_mat_test_guard
#define glm_affine_mat_test_guard
CGLM_INLINE
void
glm_inv_tr_raw(mat4 mat) {
  CGLM_ALIGN_MAT mat3 r;
  CGLM_ALIGN(8)  vec3 t;

  /* rotate */
  glm_mat4_pick3t(mat, r);
  glm_mat4_ins3(r, mat);

  /* translate */
  glm_mat3_mulv(r, mat[3], t);
  glm_vec3_negate(t);
  glm_vec3_copy(t, mat[3]);
}
#endif

TEST_IMPL(GLM_PREFIX, mul) {
  mat4 m1 = GLM_MAT4_IDENTITY_INIT;
  mat4 m2 = GLM_MAT4_IDENTITY_INIT;
  mat4 m3;
  mat4 m4 = GLM_MAT4_ZERO_INIT;
  int  i, j, k;

  test_rand_mat4(m1);
  test_rand_mat4(m2);

  GLM(mul)(m1, m2, m3);
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      for (k = 0; k < 4; k++)
        /* column-major */
        m4[i][j] += m1[k][j] * m2[i][k];
    }
  }

  ASSERTIFY(test_assert_mat4_eq(m3, m4))

  /* test pre compiled */
  GLM(mul)(m1, m2, m3);
  ASSERTIFY(test_assert_mat4_eq(m3, m4))

  TEST_SUCCESS
}

TEST_IMPL(GLM_PREFIX, mul_rot) {
  mat4 m1 = GLM_MAT4_IDENTITY_INIT;
  mat4 m2 = GLM_MAT4_IDENTITY_INIT;
  mat4 m3;
  mat4 m4 = GLM_MAT4_ZERO_INIT;
  int  i, j, k;

  glm_rotate(m1, drand48(), (vec3){drand48(), drand48(), drand48()});
  glm_rotate(m2, drand48(), (vec3){drand48(), drand48(), drand48()});

  GLM(mul_rot)(m1, m2, m3);
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      for (k = 0; k < 4; k++)
        /* column-major */
        m4[i][j] += m1[k][j] * m2[i][k];
    }
  }

  ASSERTIFY(test_assert_mat4_eq(m3, m4))

  /* test pre compiled */
  GLM(mul_rot)(m1, m2, m3);
  ASSERTIFY(test_assert_mat4_eq(m3, m4))

  TEST_SUCCESS
}

TEST_IMPL(GLM_PREFIX, inv_tr) {
  mat4 m1, m2;
  int  i;

  for (i = 0; i < 10000; i++) {
    test_rand_mat4(m1);

    glm_mat4_copy(m1, m2);

    /* test inverse precise */
    GLM(inv_tr)(m1);
    GLM(inv_tr)(m1);
    ASSERTIFY(test_assert_mat4_eq(m1, m2))

    /* test inverse precise */
    GLM(mat4_inv)(m1, m2);
    GLM(inv_tr)(m2);
    ASSERTIFY(test_assert_mat4_eq(m1, m2))
    
    /* test with raw */
    glm_mat4_copy(m1, m2);
    glm_inv_tr_raw(m2);
    GLM(inv_tr)(m1);
    ASSERTIFY(test_assert_mat4_eq(m1, m2))
  }

  TEST_SUCCESS
}