aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/quat_test.c85
-rw-r--r--test/vec3_test.c68
2 files changed, 153 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}
diff --git a/test/vec3_test.c b/test/vec3_test.c
new file mode 100644
index 0000000..886fee3
--- /dev/null
+++ b/test/vec3_test.c
@@ -0,0 +1,68 @@
1#include <math/vec3.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_vec3(vec3 v) {
12 printf("{ %f, %f, %f }\n", v.x, v.y, v.z);
13}
14
15/// Slerp between two vectors forming an acute angle.
16TEST_CASE(vec3_slerp_acute_angle) {
17 const R angle1 = 0;
18 const R angle2 = PI / 4;
19 const R t = 0.5;
20
21 const vec3 a = vec3_make(cos(angle1), sin(angle1), 0);
22 const vec3 b = vec3_make(cos(angle2), sin(angle2), 0);
23
24 const vec3 result = vec3_slerp(a, b, t);
25
26 const R angle3 = lerp(angle1, angle2, t);
27 const vec3 expected = vec3_make(cos(angle3), sin(angle3), 0.0);
28 TEST_TRUE(vec3_eq(result, expected, eps));
29}
30
31/// Slerp between two vectors forming an obtuse angle (negative dot product).
32///
33/// The interpolation must follow the shortest path between both vectors.
34TEST_CASE(vec3_slerp_obtuse_angle) {
35 const R angle1 = 0;
36 const R angle2 = 3 * PI / 4;
37 const R t = 0.5;
38
39 const vec3 a = vec3_make(cos(angle1), sin(angle1), 0);
40 const vec3 b = vec3_make(cos(angle2), sin(angle2), 0);
41
42 const vec3 result = vec3_slerp(a, b, t);
43
44 const R angle3 = lerp(angle1, angle2, t);
45 const vec3 expected = vec3_make(cos(angle3), sin(angle3), 0.0);
46 TEST_TRUE(vec3_eq(result, expected, eps));
47}
48
49/// Slerp between two vectors forming a reflex angle.
50///
51/// The interpolation must follow the shortest path between both vectors.
52TEST_CASE(vec3_slerp_reflex_angle) {
53 const R angle1 = 0;
54 const R angle2 = 5 * PI / 4;
55 const R t = 0.5;
56
57 const vec3 a = vec3_make(cos(angle1), sin(angle1), 0);
58 const vec3 b = vec3_make(cos(angle2), sin(angle2), 0);
59
60 const vec3 result = vec3_slerp(a, b, t);
61
62 // slerp goes from a to b following the shortest path, which is down from a
63 // towards b. The resulting angle is therefore +PI of the angle we get from
64 // lerping the two input angles.
65 const R angle3 = lerp(angle1, angle2, t) + PI;
66 const vec3 expected = vec3_make(cos(angle3), sin(angle3), 0.0);
67 TEST_TRUE(vec3_eq(result, expected, 1e-5));
68}