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
use cgmath::{
  BaseFloat, BaseNum, InnerSpace, Quaternion, Vector1, Vector2, Vector3, Vector4, VectorSpace,
};

use crate::interpolate::{
  cubic_bezier_def, cubic_hermite_def, quadratic_bezier_def, Additive, Interpolate, Linear, One,
};

macro_rules! impl_interpolate_vec {
  ($($t:tt)*) => {
    impl<T> Linear<T> for $($t)*<T> where T: BaseNum {
      #[inline(always)]
      fn outer_mul(self, t: T) -> Self {
        self * t
      }

      #[inline(always)]
      fn outer_div(self, t: T) -> Self {
        self / t
      }
    }

    impl<T> Interpolate<T> for $($t)*<T>
    where Self: InnerSpace<Scalar = T>, T: Additive + BaseFloat + One {
      #[inline(always)]
      fn lerp(a: Self, b: Self, t: T) -> Self {
        a.lerp(b, t)
      }

      #[inline(always)]
      fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
        cubic_hermite_def(x, a, b, y, t)
      }

      #[inline(always)]
      fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
        quadratic_bezier_def(a, u, b, t)
      }

      #[inline(always)]
      fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
        cubic_bezier_def(a, u, v, b, t)
      }
    }
  }
}

impl_interpolate_vec!(Vector1);
impl_interpolate_vec!(Vector2);
impl_interpolate_vec!(Vector3);
impl_interpolate_vec!(Vector4);

impl<T> Linear<T> for Quaternion<T>
where
  T: BaseFloat,
{
  #[inline(always)]
  fn outer_mul(self, t: T) -> Self {
    self * t
  }

  #[inline(always)]
  fn outer_div(self, t: T) -> Self {
    self / t
  }
}

impl<T> Interpolate<T> for Quaternion<T>
where
  Self: InnerSpace<Scalar = T>,
  T: Additive + BaseFloat + One,
{
  #[inline(always)]
  fn lerp(a: Self, b: Self, t: T) -> Self {
    a.nlerp(b, t)
  }

  #[inline(always)]
  fn cubic_hermite(x: (Self, T), a: (Self, T), b: (Self, T), y: (Self, T), t: T) -> Self {
    cubic_hermite_def(x, a, b, y, t)
  }

  #[inline(always)]
  fn quadratic_bezier(a: Self, u: Self, b: Self, t: T) -> Self {
    quadratic_bezier_def(a, u, b, t)
  }

  #[inline(always)]
  fn cubic_bezier(a: Self, u: Self, v: Self, b: Self, t: T) -> Self {
    cubic_bezier_def(a, u, v, b, t)
  }
}