1use crate::BinarySerializable;
4use glam::{Vec2, Vec3, Vec4};
5
6pub type Vector = Vec3;
9
10pub type Vector2D = Vec2;
12
13pub type Vector4 = Vec4;
15
16pub type Quaternion = glam::Quat;
18
19pub type Matrix3 = glam::Mat3;
21
22pub type Matrix4 = glam::Mat4;
24
25impl BinarySerializable for Vector {}
27impl BinarySerializable for Vector2D {}
28impl BinarySerializable for Vector4 {}
29impl BinarySerializable for Quaternion {}
30impl BinarySerializable for Matrix3 {}
31impl BinarySerializable for Matrix4 {}
32
33pub struct VectorConstants;
35
36impl VectorConstants {
37 pub const FORWARD: Vector = Vec3::X;
39
40 pub const RIGHT: Vector = Vec3::Y;
42
43 pub const UP: Vector = Vec3::Z;
45
46 pub const ZERO: Vector = Vec3::ZERO;
48
49 pub const ONE: Vector = Vec3::ONE;
51}
52
53pub trait VectorExt {
55 fn size(self) -> f32;
57
58 fn size_squared(self) -> f32;
60
61 fn is_nearly_zero(self, tolerance: f32) -> bool;
63
64 fn is_normalized(self) -> bool;
66
67 fn get_safe_normal(self, tolerance: f32) -> Vector;
69}
70
71impl VectorExt for Vector {
72 fn size(self) -> f32 {
73 self.length()
74 }
75
76 fn size_squared(self) -> f32 {
77 self.length_squared()
78 }
79
80 fn is_nearly_zero(self, tolerance: f32) -> bool {
81 self.length_squared() <= tolerance * tolerance
82 }
83
84 fn is_normalized(self) -> bool {
85 (self.length_squared() - 1.0).abs() < 0.01
86 }
87
88 fn get_safe_normal(self, tolerance: f32) -> Vector {
89 let square_sum = self.length_squared();
90 if square_sum == 1.0 {
91 return self;
92 } else if square_sum < tolerance * tolerance {
93 return Vector::ZERO;
94 }
95 self.normalize()
96 }
97}
98
99pub trait Vector2DExt {
101 fn size(self) -> f32;
103
104 fn size_squared(self) -> f32;
106
107 fn is_nearly_zero(self, tolerance: f32) -> bool;
109}
110
111impl Vector2DExt for Vector2D {
112 fn size(self) -> f32 {
113 self.length()
114 }
115
116 fn size_squared(self) -> f32 {
117 self.length_squared()
118 }
119
120 fn is_nearly_zero(self, tolerance: f32) -> bool {
121 self.length_squared() <= tolerance * tolerance
122 }
123}
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128
129 #[test]
130 fn test_vector_constants() {
131 assert_eq!(VectorConstants::FORWARD, Vec3::new(1.0, 0.0, 0.0));
132 assert_eq!(VectorConstants::RIGHT, Vec3::new(0.0, 1.0, 0.0));
133 assert_eq!(VectorConstants::UP, Vec3::new(0.0, 0.0, 1.0));
134 }
135
136 #[test]
137 fn test_vector_ext() {
138 let v = Vector::new(3.0, 4.0, 0.0);
139 assert_eq!(v.size(), 5.0);
140 assert_eq!(v.size_squared(), 25.0);
141 assert!(!v.is_nearly_zero(0.1));
142
143 let normalized = v.get_safe_normal(0.001);
144 assert!(normalized.is_normalized());
145 }
146}