aboutsummaryrefslogtreecommitdiff
path: root/test/vec3_test.c
blob: 886fee31a6f120a21ea122d476c0d82be43267e6 (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
#include <math/vec3.h>

#include <math/float.h>

#include "test.h"

#include <stdio.h>

static const float eps = 1e-7;

static inline void print_vec3(vec3 v) {
  printf("{ %f, %f, %f }\n", v.x, v.y, v.z);
}

/// Slerp between two vectors forming an acute angle.
TEST_CASE(vec3_slerp_acute_angle) {
  const R angle1 = 0;
  const R angle2 = PI / 4;
  const R t      = 0.5;

  const vec3 a = vec3_make(cos(angle1), sin(angle1), 0);
  const vec3 b = vec3_make(cos(angle2), sin(angle2), 0);

  const vec3 result = vec3_slerp(a, b, t);

  const R    angle3   = lerp(angle1, angle2, t);
  const vec3 expected = vec3_make(cos(angle3), sin(angle3), 0.0);
  TEST_TRUE(vec3_eq(result, expected, eps));
}

/// Slerp between two vectors forming an obtuse angle (negative dot product).
///
/// The interpolation must follow the shortest path between both vectors.
TEST_CASE(vec3_slerp_obtuse_angle) {
  const R angle1 = 0;
  const R angle2 = 3 * PI / 4;
  const R t      = 0.5;

  const vec3 a = vec3_make(cos(angle1), sin(angle1), 0);
  const vec3 b = vec3_make(cos(angle2), sin(angle2), 0);

  const vec3 result = vec3_slerp(a, b, t);

  const R    angle3   = lerp(angle1, angle2, t);
  const vec3 expected = vec3_make(cos(angle3), sin(angle3), 0.0);
  TEST_TRUE(vec3_eq(result, expected, eps));
}

/// Slerp between two vectors forming a reflex angle.
///
/// The interpolation must follow the shortest path between both vectors.
TEST_CASE(vec3_slerp_reflex_angle) {
  const R angle1 = 0;
  const R angle2 = 5 * PI / 4;
  const R t      = 0.5;

  const vec3 a = vec3_make(cos(angle1), sin(angle1), 0);
  const vec3 b = vec3_make(cos(angle2), sin(angle2), 0);

  const vec3 result = vec3_slerp(a, b, t);

  // slerp goes from a to b following the shortest path, which is down from a
  // towards b. The resulting angle is therefore +PI of the angle we get from
  // lerping the two input angles.
  const R    angle3   = lerp(angle1, angle2, t) + PI;
  const vec3 expected = vec3_make(cos(angle3), sin(angle3), 0.0);
  TEST_TRUE(vec3_eq(result, expected, 1e-5));
}