alloy/math/
interpolate.rs

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! This module provides interpolation logic for the core systems.

use crate::ops::Lerp;
use std::ops::{Add, Mul};

/// Linearly interpolates between two values.
///
/// # Parameters
///
/// * `start` - The starting state.
/// * `end` - The ending state.
/// * `alpha` - The interpolation factor.
pub fn linear<T, U>(start: &T, end: &U, alpha: f32) -> T::Output
where
  T: Lerp<U>,
{
  start.lerp(end, alpha)
}

/// Quadratically interpolates between three values.
///
/// # Parameters
///
/// * `start` - The starting state.
/// * `intermediate` - The intermediate value.
/// * `end` - The end state.
/// * `alpha` - The interpolation factor.
pub fn quadratic<T>(start: &T, intermediate: &T, end: &T, alpha: f32) -> T
where
  T: Lerp<Output = T> + Mul<f32, Output = T> + Add<T, Output = T>,
{
  let alpha_sq = alpha * alpha;
  let alpha_inv = 1.0 - alpha;
  let alpha_inv_sq = alpha_inv * alpha_inv;

  start.lerp(intermediate, 2.0 * alpha * alpha_inv) * alpha_inv_sq
    + intermediate.lerp(end, 2.0 * alpha * alpha_inv) * alpha_sq
}

/// Interpolates between two values using a circular interpolation.
///
/// # Parameters
///
/// * `start` - The starting state.
/// * `end` - The ending state.
/// * `alpha` - The interpolation factor.
pub fn circular<T, U>(start: &T, end: &U, alpha: f32) -> T::Output
where
  T: Lerp<U>,
{
  start.lerp(end, 1.0 - (1.0 - (alpha * alpha)).sqrt())
}

/// Interpolates between four values using a bilinear interpolation.
///
/// # Parameters
///
/// * `v00` - The value at the top-left corner.
/// * `v10` - The value at the top-right corner.
/// * `v01` - The value at the bottom-left corner.
/// * `v11` - The value at the bottom-right corner.
/// * `tx` - The interpolation factor along the x-axis.
/// * `ty` - The interpolation factor along the y-axis.
pub fn bilinear<T0, T1, T2, T3, R>(v00: &T0, v10: &T1, v01: &T2, v11: &T3, tx: f32, ty: f32) -> R
where
  T0: Lerp<T1>,
  T2: Lerp<T3>,
  T0::Output: Lerp<T2::Output, Output = R>,
{
  linear(&linear(v00, v10, tx), &linear(v01, v11, tx), ty)
}

/// Interpolates between two values using a sine interpolation.
///
/// # Parameters
///
/// * `start` - The starting state.
/// * `end` - The ending state.
/// * `alpha` - The interpolation factor.
pub fn sine<T, U>(start: &T, end: &U, alpha: f32) -> T::Output
where
  T: Lerp<U>,
{
  let alpha = (alpha * std::f32::consts::PI).sin() * 0.5 + 0.5;
  start.lerp(end, alpha)
}

/// Interpolates between two values using a cosine interpolation.
///
/// # Parameters
///
/// * `start` - The starting state.
/// * `end` - The ending state.
/// * `alpha` - The interpolation factor.
pub fn cosine<T, U>(start: &T, end: &U, alpha: f32) -> T::Output
where
  T: Lerp<U>,
{
  let alpha = (alpha * std::f32::consts::PI).cos() * 0.5 + 0.5;
  start.lerp(end, alpha)
}

/// Interpolates between two values using a smoothstep interpolation.
///
/// # Parameters
///
/// * `start` - The starting state.
/// * `end` - The ending state.
/// * `alpha` - The interpolation factor.
pub fn smoothstep<T, U>(start: &T, end: &U, alpha: f32) -> T::Output
where
  T: Lerp<U>,
{
  let alpha = alpha * alpha * (3.0 - 2.0 * alpha);
  start.lerp(end, alpha)
}