1 | use super::*; |
2 | |
3 | impl Vector4 { |
4 | pub fn new(X: f32, Y: f32, Z: f32, W: f32) -> Self { |
5 | Self { X, Y, Z, W } |
6 | } |
7 | pub fn zero() -> Self { |
8 | Self { |
9 | X: 0f32, |
10 | Y: 0f32, |
11 | Z: 0f32, |
12 | W: 0f32, |
13 | } |
14 | } |
15 | pub fn one() -> Self { |
16 | Self { |
17 | X: 1f32, |
18 | Y: 1f32, |
19 | Z: 1f32, |
20 | W: 1f32, |
21 | } |
22 | } |
23 | pub fn unit_x() -> Self { |
24 | Self { |
25 | X: 1.0, |
26 | Y: 0.0, |
27 | Z: 0.0, |
28 | W: 0.0, |
29 | } |
30 | } |
31 | pub fn unit_y() -> Self { |
32 | Self { |
33 | X: 0.0, |
34 | Y: 1.0, |
35 | Z: 0.0, |
36 | W: 0.0, |
37 | } |
38 | } |
39 | pub fn unit_z() -> Self { |
40 | Self { |
41 | X: 0.0, |
42 | Y: 0.0, |
43 | Z: 1.0, |
44 | W: 0.0, |
45 | } |
46 | } |
47 | pub fn unit_w() -> Self { |
48 | Self { |
49 | X: 0.0, |
50 | Y: 0.0, |
51 | Z: 0.0, |
52 | W: 1.0, |
53 | } |
54 | } |
55 | pub fn dot(&self, rhs: &Self) -> f32 { |
56 | self.X * rhs.X + self.Y * rhs.Y + self.Z * rhs.Z + self.W * rhs.W |
57 | } |
58 | pub fn length_squared(&self) -> f32 { |
59 | self.dot(self) |
60 | } |
61 | #[cfg (feature = "std" )] |
62 | pub fn length(&self) -> f32 { |
63 | self.length_squared().sqrt() |
64 | } |
65 | #[cfg (feature = "std" )] |
66 | pub fn distance(&self, value: &Self) -> f32 { |
67 | (self - value).length() |
68 | } |
69 | pub fn distance_squared(&self, value: &Self) -> f32 { |
70 | (self - value).length_squared() |
71 | } |
72 | #[cfg (feature = "std" )] |
73 | pub fn normalize(&self) -> Self { |
74 | self / self.length() |
75 | } |
76 | |
77 | fn impl_add(&self, rhs: &Self) -> Self { |
78 | Self { |
79 | X: self.X + rhs.X, |
80 | Y: self.Y + rhs.Y, |
81 | Z: self.Z + rhs.Z, |
82 | W: self.W + rhs.W, |
83 | } |
84 | } |
85 | fn impl_sub(&self, rhs: &Self) -> Self { |
86 | Self { |
87 | X: self.X - rhs.X, |
88 | Y: self.Y - rhs.Y, |
89 | Z: self.Z - rhs.Z, |
90 | W: self.W - rhs.W, |
91 | } |
92 | } |
93 | fn impl_div(&self, rhs: &Self) -> Self { |
94 | Self { |
95 | X: self.X / rhs.X, |
96 | Y: self.Y / rhs.Y, |
97 | Z: self.Z / rhs.Z, |
98 | W: self.W / rhs.W, |
99 | } |
100 | } |
101 | fn impl_div_f32(&self, rhs: f32) -> Self { |
102 | Self { |
103 | X: self.X / rhs, |
104 | Y: self.Y / rhs, |
105 | Z: self.Z / rhs, |
106 | W: self.W / rhs, |
107 | } |
108 | } |
109 | fn impl_mul(&self, rhs: &Self) -> Self { |
110 | Self { |
111 | X: self.X * rhs.X, |
112 | Y: self.Y * rhs.Y, |
113 | Z: self.Z * rhs.Z, |
114 | W: self.W * rhs.W, |
115 | } |
116 | } |
117 | fn impl_mul_f32(&self, rhs: f32) -> Self { |
118 | Self { |
119 | X: self.X * rhs, |
120 | Y: self.Y * rhs, |
121 | Z: self.Z * rhs, |
122 | W: self.W * rhs, |
123 | } |
124 | } |
125 | } |
126 | |
127 | impl core::ops::Add<Vector4> for Vector4 { |
128 | type Output = Vector4; |
129 | fn add(self, rhs: Vector4) -> Vector4 { |
130 | self.impl_add(&rhs) |
131 | } |
132 | } |
133 | impl core::ops::Add<&Vector4> for Vector4 { |
134 | type Output = Vector4; |
135 | fn add(self, rhs: &Vector4) -> Vector4 { |
136 | self.impl_add(rhs) |
137 | } |
138 | } |
139 | impl core::ops::Add<Vector4> for &Vector4 { |
140 | type Output = Vector4; |
141 | fn add(self, rhs: Vector4) -> Vector4 { |
142 | self.impl_add(&rhs) |
143 | } |
144 | } |
145 | impl core::ops::Add<&Vector4> for &Vector4 { |
146 | type Output = Vector4; |
147 | fn add(self, rhs: &Vector4) -> Vector4 { |
148 | self.impl_add(rhs) |
149 | } |
150 | } |
151 | impl core::ops::Sub<Vector4> for Vector4 { |
152 | type Output = Vector4; |
153 | fn sub(self, rhs: Vector4) -> Vector4 { |
154 | self.impl_sub(&rhs) |
155 | } |
156 | } |
157 | impl core::ops::Sub<&Vector4> for Vector4 { |
158 | type Output = Vector4; |
159 | fn sub(self, rhs: &Vector4) -> Vector4 { |
160 | self.impl_sub(rhs) |
161 | } |
162 | } |
163 | impl core::ops::Sub<Vector4> for &Vector4 { |
164 | type Output = Vector4; |
165 | fn sub(self, rhs: Vector4) -> Vector4 { |
166 | self.impl_sub(&rhs) |
167 | } |
168 | } |
169 | impl core::ops::Sub<&Vector4> for &Vector4 { |
170 | type Output = Vector4; |
171 | fn sub(self, rhs: &Vector4) -> Vector4 { |
172 | self.impl_sub(rhs) |
173 | } |
174 | } |
175 | impl core::ops::Div<Vector4> for Vector4 { |
176 | type Output = Vector4; |
177 | fn div(self, rhs: Vector4) -> Vector4 { |
178 | self.impl_div(&rhs) |
179 | } |
180 | } |
181 | impl core::ops::Div<&Vector4> for Vector4 { |
182 | type Output = Vector4; |
183 | fn div(self, rhs: &Vector4) -> Vector4 { |
184 | self.impl_div(rhs) |
185 | } |
186 | } |
187 | impl core::ops::Div<Vector4> for &Vector4 { |
188 | type Output = Vector4; |
189 | fn div(self, rhs: Vector4) -> Vector4 { |
190 | self.impl_div(&rhs) |
191 | } |
192 | } |
193 | impl core::ops::Div<&Vector4> for &Vector4 { |
194 | type Output = Vector4; |
195 | fn div(self, rhs: &Vector4) -> Vector4 { |
196 | self.impl_div(rhs) |
197 | } |
198 | } |
199 | impl core::ops::Div<f32> for Vector4 { |
200 | type Output = Vector4; |
201 | fn div(self, rhs: f32) -> Vector4 { |
202 | self.impl_div_f32(rhs) |
203 | } |
204 | } |
205 | impl core::ops::Div<f32> for &Vector4 { |
206 | type Output = Vector4; |
207 | fn div(self, rhs: f32) -> Vector4 { |
208 | self.impl_div_f32(rhs) |
209 | } |
210 | } |
211 | impl core::ops::Mul<Vector4> for Vector4 { |
212 | type Output = Vector4; |
213 | fn mul(self, rhs: Vector4) -> Vector4 { |
214 | self.impl_mul(&rhs) |
215 | } |
216 | } |
217 | impl core::ops::Mul<&Vector4> for Vector4 { |
218 | type Output = Vector4; |
219 | fn mul(self, rhs: &Vector4) -> Vector4 { |
220 | self.impl_mul(rhs) |
221 | } |
222 | } |
223 | impl core::ops::Mul<Vector4> for &Vector4 { |
224 | type Output = Vector4; |
225 | fn mul(self, rhs: Vector4) -> Vector4 { |
226 | self.impl_mul(&rhs) |
227 | } |
228 | } |
229 | impl core::ops::Mul<&Vector4> for &Vector4 { |
230 | type Output = Vector4; |
231 | fn mul(self, rhs: &Vector4) -> Vector4 { |
232 | self.impl_mul(rhs) |
233 | } |
234 | } |
235 | impl core::ops::Mul<f32> for Vector4 { |
236 | type Output = Vector4; |
237 | fn mul(self, rhs: f32) -> Vector4 { |
238 | self.impl_mul_f32(rhs) |
239 | } |
240 | } |
241 | impl core::ops::Mul<f32> for &Vector4 { |
242 | type Output = Vector4; |
243 | fn mul(self, rhs: f32) -> Vector4 { |
244 | self.impl_mul_f32(rhs) |
245 | } |
246 | } |
247 | |