1use std::ops::{Add, Mul, Neg, Sub};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq)]
7pub struct Vec3 {
8 pub x: f32,
10 pub y: f32,
12 pub z: f32,
14}
15
16impl Vec3 {
17 pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
19
20 #[inline]
22 pub const fn new(x: f32, y: f32, z: f32) -> Self {
23 Self { x, y, z }
24 }
25
26 #[inline]
28 pub const fn to_array(self) -> [f32; 3] {
29 [self.x, self.y, self.z]
30 }
31
32 #[inline]
34 pub const fn from_array([x, y, z]: [f32; 3]) -> Self {
35 Self { x, y, z }
36 }
37
38 #[inline]
40 pub fn dot(self, other: Self) -> f32 {
41 self.x * other.x + self.y * other.y + self.z * other.z
42 }
43
44 #[inline]
46 pub fn cross(self, other: Self) -> Self {
47 Self::new(
48 self.y * other.z - self.z * other.y,
49 self.z * other.x - self.x * other.z,
50 self.x * other.y - self.y * other.x,
51 )
52 }
53
54 #[inline]
56 pub fn length(self) -> f32 {
57 self.dot(self).sqrt()
58 }
59
60 #[inline]
62 pub fn normalized(self) -> Option<Self> {
63 let length = self.length();
64 (length.is_normal()).then(|| self * (1.0 / length))
65 }
66
67 #[inline]
69 pub fn lerp(self, other: Self, t: f32) -> Self {
70 self + (other - self) * t
71 }
72
73 #[inline]
75 pub fn min(self, other: Self) -> Self {
76 Self::new(
77 self.x.min(other.x),
78 self.y.min(other.y),
79 self.z.min(other.z),
80 )
81 }
82
83 #[inline]
85 pub fn max(self, other: Self) -> Self {
86 Self::new(
87 self.x.max(other.x),
88 self.y.max(other.y),
89 self.z.max(other.z),
90 )
91 }
92}
93
94impl Add for Vec3 {
95 type Output = Self;
96
97 #[inline]
98 fn add(self, other: Self) -> Self {
99 Self::new(self.x + other.x, self.y + other.y, self.z + other.z)
100 }
101}
102
103impl Sub for Vec3 {
104 type Output = Self;
105
106 #[inline]
107 fn sub(self, other: Self) -> Self {
108 Self::new(self.x - other.x, self.y - other.y, self.z - other.z)
109 }
110}
111
112impl Mul<f32> for Vec3 {
113 type Output = Self;
114
115 #[inline]
116 fn mul(self, factor: f32) -> Self {
117 Self::new(self.x * factor, self.y * factor, self.z * factor)
118 }
119}
120
121impl Neg for Vec3 {
122 type Output = Self;
123
124 #[inline]
125 fn neg(self) -> Self {
126 Self::new(-self.x, -self.y, -self.z)
127 }
128}
129
130#[derive(Clone, Copy, Debug, PartialEq)]
133pub struct Mat4 {
134 rows: [[f32; 4]; 4],
135}
136
137impl Default for Mat4 {
138 fn default() -> Self {
139 Self::IDENTITY
140 }
141}
142
143impl Mat4 {
144 pub const IDENTITY: Self = Self::from_rows([
146 [1.0, 0.0, 0.0, 0.0],
147 [0.0, 1.0, 0.0, 0.0],
148 [0.0, 0.0, 1.0, 0.0],
149 [0.0, 0.0, 0.0, 1.0],
150 ]);
151
152 pub const fn from_rows(rows: [[f32; 4]; 4]) -> Self {
154 Self { rows }
155 }
156
157 pub const fn rows(&self) -> [[f32; 4]; 4] {
159 self.rows
160 }
161
162 pub const fn from_axes(x: Vec3, y: Vec3, z: Vec3, origin: Vec3) -> Self {
165 Self::from_rows([
166 [x.x, y.x, z.x, origin.x],
167 [x.y, y.y, z.y, origin.y],
168 [x.z, y.z, z.z, origin.z],
169 [0.0, 0.0, 0.0, 1.0],
170 ])
171 }
172
173 pub const fn translation(offset: Vec3) -> Self {
175 Self::from_axes(
176 Vec3::new(1.0, 0.0, 0.0),
177 Vec3::new(0.0, 1.0, 0.0),
178 Vec3::new(0.0, 0.0, 1.0),
179 offset,
180 )
181 }
182
183 pub const fn scaling(factors: Vec3) -> Self {
185 Self::from_axes(
186 Vec3::new(factors.x, 0.0, 0.0),
187 Vec3::new(0.0, factors.y, 0.0),
188 Vec3::new(0.0, 0.0, factors.z),
189 Vec3::ZERO,
190 )
191 }
192
193 pub fn rotation_x(radians: f32) -> Self {
196 let (sin, cos) = radians.sin_cos();
197 Self::from_axes(
198 Vec3::new(1.0, 0.0, 0.0),
199 Vec3::new(0.0, cos, sin),
200 Vec3::new(0.0, -sin, cos),
201 Vec3::ZERO,
202 )
203 }
204
205 pub fn rotation_y(radians: f32) -> Self {
208 let (sin, cos) = radians.sin_cos();
209 Self::from_axes(
210 Vec3::new(cos, 0.0, sin),
211 Vec3::new(0.0, 1.0, 0.0),
212 Vec3::new(-sin, 0.0, cos),
213 Vec3::ZERO,
214 )
215 }
216
217 pub fn rotation_z(radians: f32) -> Self {
220 let (sin, cos) = radians.sin_cos();
221 Self::from_axes(
222 Vec3::new(cos, sin, 0.0),
223 Vec3::new(-sin, cos, 0.0),
224 Vec3::new(0.0, 0.0, 1.0),
225 Vec3::ZERO,
226 )
227 }
228
229 pub fn then(&self, next: &Self) -> Self {
231 let (a, b) = (next.rows, self.rows);
232 Self::from_rows(std::array::from_fn(|row| {
233 std::array::from_fn(|column| (0..4).map(|k| a[row][k] * b[k][column]).sum())
234 }))
235 }
236
237 pub fn transform_point(&self, point: Vec3) -> Vec3 {
239 let [x, y, z, w] = self.transform_homogeneous(point);
240 if w == 1.0 || w == 0.0 {
241 Vec3::new(x, y, z)
242 } else {
243 Vec3::new(x / w, y / w, z / w)
244 }
245 }
246
247 pub fn transform_homogeneous(&self, point: Vec3) -> [f32; 4] {
250 self.rows
251 .map(|row| row[0] * point.x + row[1] * point.y + row[2] * point.z + row[3])
252 }
253
254 pub fn transform_vector(&self, vector: Vec3) -> Vec3 {
256 let [x, y, z, _] = self
257 .rows
258 .map(|row| row[0] * vector.x + row[1] * vector.y + row[2] * vector.z);
259 Vec3::new(x, y, z)
260 }
261
262 pub fn length_scale(&self) -> f32 {
265 let axis = |axis: Vec3| self.transform_vector(axis).length();
266 (axis(Vec3::new(1.0, 0.0, 0.0))
267 * axis(Vec3::new(0.0, 1.0, 0.0))
268 * axis(Vec3::new(0.0, 0.0, 1.0)))
269 .cbrt()
270 }
271
272 pub fn transpose(&self) -> Self {
274 Self::from_rows(std::array::from_fn(|row| {
275 std::array::from_fn(|column| self.rows[column][row])
276 }))
277 }
278
279 pub fn inverse(&self) -> Option<Self> {
281 let m = self.rows;
282 let cofactor = |row: usize, column: usize| {
283 let pick = |skip: usize| -> [usize; 3] {
284 let mut picked = [0; 3];
285 let mut next = 0;
286 for index in 0..4 {
287 if index != skip {
288 picked[next] = index;
289 next += 1;
290 }
291 }
292 picked
293 };
294 let [r0, r1, r2] = pick(row);
295 let [c0, c1, c2] = pick(column);
296 let minor = m[r0][c0] * (m[r1][c1] * m[r2][c2] - m[r1][c2] * m[r2][c1])
297 - m[r0][c1] * (m[r1][c0] * m[r2][c2] - m[r1][c2] * m[r2][c0])
298 + m[r0][c2] * (m[r1][c0] * m[r2][c1] - m[r1][c1] * m[r2][c0]);
299 if (row + column).is_multiple_of(2) {
300 minor
301 } else {
302 -minor
303 }
304 };
305 let cofactors: [[f32; 4]; 4] =
306 std::array::from_fn(|row| std::array::from_fn(|column| cofactor(row, column)));
307 let determinant: f32 = (0..4)
308 .map(|column| m[0][column] * cofactors[0][column])
309 .sum();
310 if !determinant.is_normal() {
311 return None;
312 }
313 let scale = 1.0 / determinant;
314 Some(Self::from_rows(std::array::from_fn(|row| {
315 std::array::from_fn(|column| cofactors[column][row] * scale)
316 })))
317 }
318}