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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::ops::{Add, Div, Mul, Sub};

use glam::{Vec2, Vec3, Vec4};
use serde::{Deserialize, Serialize};

pub trait Saturate {
    // Clamps a value between 0 and 1
    fn saturate(&self) -> Self;
}
impl Saturate for f32 {
    fn saturate(&self) -> Self {
        self.clamp(0., 1.)
    }
}
impl Saturate for Vec2 {
    fn saturate(&self) -> Self {
        self.clamp(Vec2::ZERO, Vec2::ONE)
    }
}
impl Saturate for Vec3 {
    fn saturate(&self) -> Self {
        self.clamp(Vec3::ZERO, Vec3::ONE)
    }
}
impl Saturate for Vec4 {
    fn saturate(&self) -> Self {
        self.clamp(Vec4::ZERO, Vec4::ONE)
    }
}

pub fn mix<X: Clone + Copy, Y: Clone + Copy>(a: Y, b: Y, p: X) -> Y
where
    Y: Sub<Y, Output = Y>,
    Y: Mul<X, Output = Y>,
    Y: Add<Y, Output = Y>,
{
    a + (b - a) * p
}

pub fn interpolate<X: Clone + Copy, Y: Clone + Copy>(x: X, x0: X, x1: X, y0: Y, y1: Y) -> Y
where
    X: Sub<X, Output = X>,
    X: Div<X, Output = X>,
    Y: Sub<Y, Output = Y>,
    Y: Add<Y, Output = Y>,
    X: Mul<Y, Output = Y>,
{
    let p = (x - x0) / (x1 - x0);
    y0 + p * (y1 - y0)
}
pub fn interpolate_clamped<X: Clone + Copy, Y: Clone + Copy>(x: X, x0: X, x1: X, y0: Y, y1: Y) -> Y
where
    X: Sub<X, Output = X>,
    X: Div<X, Output = X>,
    Y: Sub<Y, Output = Y>,
    Y: Add<Y, Output = Y>,
    X: Mul<Y, Output = Y>,
    X: Saturate,
{
    let p = (x - x0) / (x1 - x0);
    y0 + p.saturate() * (y1 - y0)
}

#[test]
fn test_interpolate() {
    assert_eq!(interpolate(-1., 0., 1., 0., 1.), -1.);
}

pub fn smoothstep(edge0: f32, edge1: f32, x: f32) -> f32 {
    let x = ((x - edge0) / (edge1 - edge0)).clamp(0.0, 1.0);
    x * x * (3. - 2. * x)
}

pub fn angle_shortest_dist(a0: f32, a1: f32) -> f32 {
    let max = std::f32::consts::PI * 2.;
    let da = (a1 - a0) % max;
    ((2. * da) % max) - da
}

pub fn angle_lerp(a0: f32, a1: f32, t: f32) -> f32 {
    a0 + angle_shortest_dist(a0, a1) * t
}

pub fn angle_to_position(
    self_position: glam::Vec2,
    self_forward: glam::Vec2,
    other_position: glam::Vec2,
) -> f32 {
    let delta = other_position - self_position;
    if delta.length() == 0.0 {
        0.0
    } else {
        self_forward.angle_between(delta)
    }
}

#[derive(Debug, Clone, Copy)]
pub struct SphericalCoords {
    pub theta: f32,
    pub phi: f32,
    pub radius: f32,
}
impl SphericalCoords {
    pub fn new(theta: f32, phi: f32, radius: f32) -> Self {
        Self { theta, phi, radius }
    }
}
impl Default for SphericalCoords {
    fn default() -> Self {
        Self::new(0., 0., 1.)
    }
}
impl From<SphericalCoords> for glam::Vec3 {
    fn from(coords: SphericalCoords) -> glam::Vec3 {
        glam::vec3(
            coords.theta.sin() * coords.phi.cos() * coords.radius,
            coords.theta.sin() * coords.phi.sin() * coords.radius,
            coords.theta.cos() * coords.radius,
        )
    }
}

pub fn cdf_sample<T>(vector: &[T], weight: fn(usize, &T) -> f32) -> usize {
    let total_weight = vector
        .iter()
        .enumerate()
        .fold(0., |p, (i, x)| p + weight(i, x));
    let mut culm = 0.;
    let x = rand::random::<f32>();
    for (i, v) in vector.iter().enumerate() {
        culm += weight(i, v) / total_weight;
        if x <= culm {
            return i;
        }
    }
    unreachable!();
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Line(pub Vec3, pub Vec3);

pub trait Round100 {
    fn round100(&self) -> Self;
}
impl Round100 for f32 {
    fn round100(&self) -> f32 {
        (*self * 100.).round() / 100.
    }
}
impl Round100 for Vec2 {
    fn round100(&self) -> Vec2 {
        (*self * 100.).round() / 100.
    }
}
impl Round100 for Vec3 {
    fn round100(&self) -> Vec3 {
        (*self * 100.).round() / 100.
    }
}
impl Round100 for Vec4 {
    fn round100(&self) -> Vec4 {
        (*self * 100.).round() / 100.
    }
}