diff options
Diffstat (limited to 'test/vec3_test.c')
-rw-r--r-- | test/vec3_test.c | 68 |
1 files changed, 68 insertions, 0 deletions
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 | |||
9 | static const float eps = 1e-7; | ||
10 | |||
11 | static 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. | ||
16 | TEST_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. | ||
34 | TEST_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. | ||
52 | TEST_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 | } | ||