aboutsummaryrefslogtreecommitdiff
path: root/test/quat_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/quat_test.c')
-rw-r--r--test/quat_test.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/test/quat_test.c b/test/quat_test.c
new file mode 100644
index 0000000..83519c3
--- /dev/null
+++ b/test/quat_test.c
@@ -0,0 +1,85 @@
1#include <math/quat.h>
2
3#include <math/float.h>
4
5#include "test.h"
6
7#include <stdio.h>
8
9static const float eps = 1e-7;
10
11static inline void print_quat(quat q) {
12 printf("{ %f, %f, %f, %f }\n", q.x, q.y, q.z, q.w);
13}
14
15static inline void print_vec3(vec3 v) {
16 printf("{ %f, %f, %f }\n", v.x, v.y, v.z);
17}
18
19/// Slerp between two vectors forming an acute angle.
20TEST_CASE(quat_slerp_acute_angle) {
21 const R angle1 = 0;
22 const R angle2 = PI / 4;
23 const R t = 0.5;
24
25 const quat a = qmake_rot(angle1, 0, 0, 1);
26 const quat b = qmake_rot(angle2, 0, 0, 1);
27
28 const quat c = qslerp(a, b, t);
29 const vec3 result = qrot(c, vec3_make(1, 0, 0));
30
31 const R angle3 = lerp(angle1, angle2, t);
32 const vec3 expected = vec3_make(cos(angle3), sin(angle3), 0.0);
33 TEST_TRUE(vec3_eq(result, expected, eps));
34}
35
36/// Slerp between two vectors forming an obtuse angle (negative dot product).
37///
38/// The interpolation must follow the shortest path between both vectors.
39TEST_CASE(quat_slerp_obtuse_angle) {
40 const R angle1 = 0;
41 const R angle2 = 3 * PI / 4;
42 const R t = 0.5;
43
44 const quat a = qmake_rot(angle1, 0, 0, 1);
45 const quat b = qmake_rot(angle2, 0, 0, 1);
46
47 const quat c = qslerp(a, b, t);
48 const vec3 result = qrot(c, vec3_make(1, 0, 0));
49
50 const R angle3 = lerp(angle1, angle2, t);
51 const vec3 expected = vec3_make(cos(angle3), sin(angle3), 0.0);
52 TEST_TRUE(vec3_eq(result, expected, eps));
53}
54
55/// Slerp between two vectors forming a reflex angle.
56///
57/// The interpolation must follow the shortest path between both vectors.
58TEST_CASE(quat_slerp_reflex_angle) {
59 const R angle1 = 0;
60 const R angle2 = 5 * PI / 4;
61 const R t = 0.5;
62
63 const quat a = qmake_rot(angle1, 0, 0, 1);
64 const quat b = qmake_rot(angle2, 0, 0, 1);
65
66 const quat c = qslerp(a, b, t);
67 const vec3 result = qrot(c, vec3_make(1, 0, 0));
68
69 // Because it's a reflex angle, we expect the rotation to follow the short
70 // path from 'a' down clockwise to 'b'. Could add +PI to the result of lerp(),
71 // but that adds more error than negating cos and sin.
72 const R angle3 = lerp(angle1, angle2, t);
73 const vec3 expected = vec3_make(-cos(angle3), -sin(angle3), 0.0);
74 TEST_TRUE(vec3_eq(result, expected, eps));
75}
76
77TEST_CASE(quat_mat4_from_quat) {
78 const R angle = PI / 8;
79 const quat q = qmake_rot(angle, 0, 0, 1);
80
81 const mat4 m = mat4_from_quat(q);
82 const vec3 p = mat4_mul_vec3(m, vec3_make(1, 0, 0), /*w=*/1);
83
84 TEST_TRUE(vec3_eq(p, vec3_make(cos(angle), sin(angle), 0), eps));
85}