est_render/math/
vector.rs

1use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};
2
3use bytemuck::{Pod, Zeroable};
4use num_traits::ToPrimitive;
5
6#[repr(C)]
7#[derive(Clone, Copy, Default, Pod, Zeroable, Debug)]
8pub struct Vector2 {
9    pub x: f32,
10    pub y: f32,
11}
12
13impl Vector2 {
14    pub fn new<T: ToPrimitive>(x: T, y: T) -> Self {
15        Self {
16            x: x.to_f32().unwrap_or(0.0),
17            y: y.to_f32().unwrap_or(0.0),
18        }
19    }
20
21    pub fn length(&self) -> f32 {
22        (self.x * self.x + self.y * self.y).sqrt()
23    }
24
25    pub fn normalize(&self) -> Self {
26        let length = self.length();
27        Self {
28            x: self.x / length,
29            y: self.y / length,
30        }
31    }
32
33    pub fn dot(&self, other: &Self) -> f32 {
34        self.x * other.x + self.y * other.y
35    }
36
37    pub fn angle(&self, other: &Self) -> f32 {
38        let dot = self.dot(other);
39        let len1 = self.length();
40        let len2 = other.length();
41        (dot / (len1 * len2)).acos()
42    }
43
44    pub fn into_vector3(&self) -> Vector3 {
45        Vector3 {
46            x: self.x,
47            y: self.y,
48            z: 0.0,
49        }
50    }
51
52    pub fn min(&self, other: &Self) -> Self {
53        Self {
54            x: self.x.min(other.x),
55            y: self.y.min(other.y),
56        }
57    }
58
59    pub fn max(&self, other: &Self) -> Self {
60        Self {
61            x: self.x.max(other.x),
62            y: self.y.max(other.y),
63        }
64    }
65
66    pub const ZERO: Self = Self { x: 0.0, y: 0.0 };
67    pub const ONE: Self = Self { x: 1.0, y: 1.0 };
68    pub const UP: Self = Self { x: 0.0, y: 1.0 };
69    pub const DOWN: Self = Self { x: 0.0, y: -1.0 };
70    pub const LEFT: Self = Self { x: -1.0, y: 0.0 };
71    pub const RIGHT: Self = Self { x: 1.0, y: 0.0 };
72}
73
74impl From<[f32; 2]> for Vector2 {
75    fn from(array: [f32; 2]) -> Self {
76        Self {
77            x: array[0],
78            y: array[1],
79        }
80    }
81}
82
83impl From<(f32, f32)> for Vector2 {
84    fn from(tuple: (f32, f32)) -> Self {
85        Self {
86            x: tuple.0,
87            y: tuple.1,
88        }
89    }
90}
91
92impl From<Vector3> for Vector2 {
93    fn from(vector: Vector3) -> Self {
94        Self {
95            x: vector.x,
96            y: vector.y,
97        }
98    }
99}
100
101impl From<(f32, f32, f32)> for Vector2 {
102    fn from(tuple: (f32, f32, f32)) -> Self {
103        Self {
104            x: tuple.0,
105            y: tuple.1,
106        }
107    }
108}
109
110impl From<(u32, u32)> for Vector2 {
111    fn from(tuple: (u32, u32)) -> Self {
112        Self {
113            x: tuple.0 as f32,
114            y: tuple.1 as f32,
115        }
116    }
117}
118
119impl From<(i32, i32)> for Vector2 {
120    fn from(tuple: (i32, i32)) -> Self {
121        Self {
122            x: tuple.0 as f32,
123            y: tuple.1 as f32,
124        }
125    }
126}
127
128impl Add for Vector2 {
129    type Output = Self;
130
131    fn add(self, other: Self) -> Self {
132        Self {
133            x: self.x + other.x,
134            y: self.y + other.y,
135        }
136    }
137}
138
139impl Sub for Vector2 {
140    type Output = Self;
141
142    fn sub(self, other: Self) -> Self {
143        Self {
144            x: self.x - other.x,
145            y: self.y - other.y,
146        }
147    }
148}
149
150impl Sub<f32> for Vector2 {
151    type Output = Self;
152
153    fn sub(self, scalar: f32) -> Self {
154        Self {
155            x: self.x - scalar,
156            y: self.y - scalar,
157        }
158    }
159}
160
161impl Mul<Vector2> for f32 {
162    type Output = Vector2;
163
164    fn mul(self, vector: Vector2) -> Vector2 {
165        Vector2 {
166            x: self * vector.x,
167            y: self * vector.y,
168        }
169    }
170}
171
172impl Div<Vector2> for f32 {
173    type Output = Vector2;
174
175    fn div(self, vector: Vector2) -> Vector2 {
176        Vector2 {
177            x: self / vector.x,
178            y: self / vector.y,
179        }
180    }
181}
182
183impl Mul<f32> for Vector2 {
184    type Output = Self;
185
186    fn mul(self, scalar: f32) -> Self {
187        Self {
188            x: self.x * scalar,
189            y: self.y * scalar,
190        }
191    }
192}
193
194impl Div<f32> for Vector2 {
195    type Output = Self;
196
197    fn div(self, scalar: f32) -> Self {
198        Self {
199            x: self.x / scalar,
200            y: self.y / scalar,
201        }
202    }
203}
204
205impl Div<Vector2> for Vector2 {
206    type Output = Self;
207
208    fn div(self, other: Self) -> Self {
209        Self {
210            x: self.x / other.x,
211            y: self.y / other.y,
212        }
213    }
214}
215
216impl AddAssign for Vector2 {
217    fn add_assign(&mut self, other: Self) {
218        *self = *self + other;
219    }
220}
221
222impl SubAssign for Vector2 {
223    fn sub_assign(&mut self, other: Self) {
224        *self = *self - other;
225    }
226}
227
228impl PartialEq for Vector2 {
229    fn eq(&self, other: &Self) -> bool {
230        self.x == other.x && self.y == other.y
231    }
232}
233
234impl Eq for Vector2 {}
235
236#[repr(C)]
237#[derive(Debug, Clone, Copy, Pod, Zeroable)]
238pub struct Vector3 {
239    pub x: f32,
240    pub y: f32,
241    pub z: f32,
242}
243
244impl Vector3 {
245    pub fn new<T: ToPrimitive>(x: T, y: T, z: T) -> Self {
246        Self {
247            x: x.to_f32().unwrap_or(0.0),
248            y: y.to_f32().unwrap_or(0.0),
249            z: z.to_f32().unwrap_or(0.0),
250        }
251    }
252
253    pub fn cross(&self, other: &Self) -> Self {
254        Self {
255            x: self.y * other.z - self.z * other.y,
256            y: self.z * other.x - self.x * other.z,
257            z: self.x * other.y - self.y * other.x,
258        }
259    }
260
261    pub fn dot(&self, other: &Self) -> f32 {
262        self.x * other.x + self.y * other.y + self.z * other.z
263    }
264
265    pub fn length(&self) -> f32 {
266        self.dot(self).sqrt()
267    }
268
269    pub fn normalize(&self) -> Self {
270        let length = self.length();
271        Self {
272            x: self.x / length,
273            y: self.y / length,
274            z: self.z / length,
275        }
276    }
277
278    pub fn angle(&self, other: &Self) -> f32 {
279        let dot = self.dot(other);
280        let len1 = self.length();
281        let len2 = other.length();
282        (dot / (len1 * len2)).acos()
283    }
284
285    pub fn min(&self, other: &Self) -> Self {
286        Self {
287            x: self.x.min(other.x),
288            y: self.y.min(other.y),
289            z: self.z.min(other.z),
290        }
291    }
292
293    pub fn max(&self, other: &Self) -> Self {
294        Self {
295            x: self.x.max(other.x),
296            y: self.y.max(other.y),
297            z: self.z.max(other.z),
298        }
299    }
300
301    pub const ZERO: Self = Self {
302        x: 0.0,
303        y: 0.0,
304        z: 0.0,
305    };
306
307    pub const ONE: Self = Self {
308        x: 1.0,
309        y: 1.0,
310        z: 1.0,
311    };
312
313    pub const UP: Self = Self {
314        x: 0.0,
315        y: 1.0,
316        z: 0.0,
317    };
318
319    pub const DOWN: Self = Self {
320        x: 0.0,
321        y: -1.0,
322        z: 0.0,
323    };
324
325    pub const LEFT: Self = Self {
326        x: -1.0,
327        y: 0.0,
328        z: 0.0,
329    };
330
331    pub const RIGHT: Self = Self {
332        x: 1.0,
333        y: 0.0,
334        z: 0.0,
335    };
336
337    pub const FORWARD: Self = Self {
338        x: 0.0,
339        y: 0.0,
340        z: 1.0,
341    };
342}
343
344impl Default for Vector3 {
345    fn default() -> Self {
346        Self {
347            x: 0.0,
348            y: 0.0,
349            z: 0.0,
350        }
351    }
352}
353
354impl From<[f32; 3]> for Vector3 {
355    fn from(array: [f32; 3]) -> Self {
356        Self {
357            x: array[0],
358            y: array[1],
359            z: array[2],
360        }
361    }
362}
363
364impl From<(f32, f32, f32)> for Vector3 {
365    fn from(tuple: (f32, f32, f32)) -> Self {
366        Self {
367            x: tuple.0,
368            y: tuple.1,
369            z: tuple.2,
370        }
371    }
372}
373
374impl From<(u32, u32, u32)> for Vector3 {
375    fn from(tuple: (u32, u32, u32)) -> Self {
376        Self {
377            x: tuple.0 as f32,
378            y: tuple.1 as f32,
379            z: tuple.2 as f32,
380        }
381    }
382}
383
384impl From<(i32, i32, i32)> for Vector3 {
385    fn from(tuple: (i32, i32, i32)) -> Self {
386        Self {
387            x: tuple.0 as f32,
388            y: tuple.1 as f32,
389            z: tuple.2 as f32,
390        }
391    }
392}
393
394impl From<Vector2> for Vector3 {
395    fn from(vector: Vector2) -> Self {
396        Self {
397            x: vector.x,
398            y: vector.y,
399            z: 0.0,
400        }
401    }
402}
403
404impl From<(f32, f32)> for Vector3 {
405    fn from(tuple: (f32, f32)) -> Self {
406        Self {
407            x: tuple.0,
408            y: tuple.1,
409            z: 0.0,
410        }
411    }
412}
413
414impl Add for Vector3 {
415    type Output = Self;
416
417    fn add(self, other: Self) -> Self {
418        Self {
419            x: self.x + other.x,
420            y: self.y + other.y,
421            z: self.z + other.z,
422        }
423    }
424}
425
426impl Sub for Vector3 {
427    type Output = Self;
428
429    fn sub(self, other: Self) -> Self {
430        Self {
431            x: self.x - other.x,
432            y: self.y - other.y,
433            z: self.z - other.z,
434        }
435    }
436}
437
438impl Mul<f32> for Vector3 {
439    type Output = Self;
440
441    fn mul(self, scalar: f32) -> Self {
442        Self {
443            x: self.x * scalar,
444            y: self.y * scalar,
445            z: self.z * scalar,
446        }
447    }
448}
449
450impl Div<f32> for Vector3 {
451    type Output = Self;
452
453    fn div(self, scalar: f32) -> Self {
454        Self {
455            x: self.x / scalar,
456            y: self.y / scalar,
457            z: self.z / scalar,
458        }
459    }
460}
461
462impl PartialEq for Vector3 {
463    fn eq(&self, other: &Self) -> bool {
464        self.x == other.x && self.y == other.y && self.z == other.z
465    }
466}
467
468impl Eq for Vector3 {}
469
470impl AddAssign for Vector3 {
471    fn add_assign(&mut self, other: Self) {
472        *self = *self + other;
473    }
474}
475
476impl SubAssign for Vector3 {
477    fn sub_assign(&mut self, other: Self) {
478        *self = *self - other;
479    }
480}
481
482#[repr(C)]
483#[derive(Debug, Clone, Copy, Pod, Zeroable)]
484pub struct Vector4 {
485    pub x: f32,
486    pub y: f32,
487    pub z: f32,
488    pub w: f32,
489}
490
491impl Vector4 {
492    pub fn new<T: ToPrimitive>(x: T, y: T, z: T, w: T) -> Self {
493        Self {
494            x: x.to_f32().unwrap_or(0.0),
495            y: y.to_f32().unwrap_or(0.0),
496            z: z.to_f32().unwrap_or(0.0),
497            w: w.to_f32().unwrap_or(1.0), // Default w to 1.0
498        }
499    }
500
501    pub fn length(&self) -> f32 {
502        (self.x * self.x + self.y * self.y + self.z * self.z + self.w * self.w).sqrt()
503    }
504
505    pub fn normalize(&self) -> Self {
506        let length = self.length();
507        Self {
508            x: self.x / length,
509            y: self.y / length,
510            z: self.z / length,
511            w: self.w / length,
512        }
513    }
514
515    pub fn dot(&self, other: &Self) -> f32 {
516        self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
517    }
518
519    pub fn cross(&self, other: &Self) -> Self {
520        Self {
521            x: self.y * other.z - self.z * other.y,
522            y: self.z * other.x - self.x * other.z,
523            z: self.x * other.y - self.y * other.x,
524            w: 0.0, // Cross product in 4D space is not well-defined, set w to 0
525        }
526    }
527
528    pub const ZERO: Self = Self {
529        x: 0.0,
530        y: 0.0,
531        z: 0.0,
532        w: 1.0, // Default w to 1.0
533    };
534
535    pub const ONE: Self = Self {
536        x: 1.0,
537        y: 1.0,
538        z: 1.0,
539        w: 1.0, // Default w to 1.0
540    };
541
542    pub const UP: Self = Self {
543        x: 0.0,
544        y: 1.0,
545        z: 0.0,
546        w: 1.0, // Default w to 1.0
547    };
548
549    pub const DOWN: Self = Self {
550        x: 0.0,
551        y: -1.0,
552        z: 0.0,
553        w: 1.0, // Default w to 1.0
554    };
555}
556
557impl Default for Vector4 {
558    fn default() -> Self {
559        Self {
560            x: 0.0,
561            y: 0.0,
562            z: 0.0,
563            w: 1.0, // Default w to 1.0
564        }
565    }
566}
567
568impl PartialEq for Vector4 {
569    fn eq(&self, other: &Self) -> bool {
570        self.x == other.x && self.y == other.y && self.z == other.z && self.w == other.w
571    }
572}
573
574impl Eq for Vector4 {}
575
576#[repr(C)]
577#[derive(Clone, Copy, Pod, Zeroable)]
578pub struct Vector2I {
579    pub x: i32,
580    pub y: i32,
581}
582
583#[allow(dead_code)]
584impl Vector2I {
585    pub fn new<T: ToPrimitive>(x: T, y: T) -> Self {
586        Self {
587            x: x.to_i32().unwrap_or(0),
588            y: y.to_i32().unwrap_or(0),
589        }
590    }
591
592    pub const ZERO: Self = Self { x: 0, y: 0 };
593    pub const ONE: Self = Self { x: 1, y: 1 };
594    pub const UP: Self = Self { x: 0, y: 1 };
595    pub const DOWN: Self = Self { x: 0, y: -1 };
596    pub const LEFT: Self = Self { x: -1, y: 0 };
597    pub const RIGHT: Self = Self { x: 1, y: 0 };
598    pub const FORWARD: Self = Self { x: 0, y: 1 };
599}
600
601#[allow(dead_code)]
602impl Vector2I {
603    pub fn length(&self) -> f32 {
604        ((self.x * self.x + self.y * self.y) as f32).sqrt()
605    }
606
607    pub fn normalize(&self) -> Self {
608        let length = self.length();
609        Self {
610            x: (self.x as f32 / length) as i32,
611            y: (self.y as f32 / length) as i32,
612        }
613    }
614}
615
616impl From<(i32, i32)> for Vector2I {
617    fn from(tuple: (i32, i32)) -> Self {
618        Self {
619            x: tuple.0,
620            y: tuple.1,
621        }
622    }
623}
624
625impl From<(u32, u32)> for Vector2I {
626    fn from(tuple: (u32, u32)) -> Self {
627        Self {
628            x: tuple.0 as i32,
629            y: tuple.1 as i32,
630        }
631    }
632}
633
634impl From<(f32, f32)> for Vector2I {
635    fn from(tuple: (f32, f32)) -> Self {
636        Self {
637            x: tuple.0 as i32,
638            y: tuple.1 as i32,
639        }
640    }
641}
642
643#[repr(C)]
644#[derive(Clone, Copy, Pod, Zeroable)]
645pub struct Vector3I {
646    pub x: i32,
647    pub y: i32,
648    pub z: i32,
649}
650
651#[allow(dead_code)]
652impl Vector3I {
653    pub fn new<T: ToPrimitive>(x: T, y: T, z: T) -> Self {
654        Self {
655            x: x.to_i32().unwrap_or(0),
656            y: y.to_i32().unwrap_or(0),
657            z: z.to_i32().unwrap_or(0),
658        }
659    }
660
661    pub const ZERO: Self = Self { x: 0, y: 0, z: 0 };
662    pub const ONE: Self = Self { x: 1, y: 1, z: 1 };
663    pub const UP: Self = Self { x: 0, y: 1, z: 0 };
664    pub const DOWN: Self = Self { x: 0, y: -1, z: 0 };
665    pub const LEFT: Self = Self { x: -1, y: 0, z: 0 };
666    pub const RIGHT: Self = Self { x: 1, y: 0, z: 0 };
667}
668
669#[allow(dead_code)]
670impl Vector3I {
671    pub fn length(&self) -> f32 {
672        ((self.x * self.x + self.y * self.y + self.z * self.z) as f32).sqrt()
673    }
674
675    pub fn normalize(&self) -> Self {
676        let length = self.length();
677        Self {
678            x: (self.x as f32 / length) as i32,
679            y: (self.y as f32 / length) as i32,
680            z: (self.z as f32 / length) as i32,
681        }
682    }
683}
684
685impl From<(i32, i32, i32)> for Vector3I {
686    fn from(tuple: (i32, i32, i32)) -> Self {
687        Self {
688            x: tuple.0,
689            y: tuple.1,
690            z: tuple.2,
691        }
692    }
693}
694
695impl From<(u32, u32, u32)> for Vector3I {
696    fn from(tuple: (u32, u32, u32)) -> Self {
697        Self {
698            x: tuple.0 as i32,
699            y: tuple.1 as i32,
700            z: tuple.2 as i32,
701        }
702    }
703}
704
705impl From<(f32, f32, f32)> for Vector3I {
706    fn from(tuple: (f32, f32, f32)) -> Self {
707        Self {
708            x: tuple.0 as i32,
709            y: tuple.1 as i32,
710            z: tuple.2 as i32,
711        }
712    }
713}