1use std::ops::Rem;
2
3use crate::{F32Fmt, One, SignOps, Two, Zero};
4
5use {
6 super::Vector,
7 crate::Construct,
8 std::{
9 fmt,
10 ops::{Add, Div, Sub, Mul, Neg}
11 }
12};
13
14#[derive(PartialEq, Clone, Copy)]
16pub struct Vector3<T>(pub T, pub T, pub T);
17
18impl<T> fmt::Debug for Vector3<T>
19where T: fmt::Debug + Copy {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 write!(f, "({:?}, {:?}, {:?})", self.0, self.1, self.2)
22 }
23}
24
25impl<T> Vector3<T> {
26 pub fn new(x: T, y: T, z: T) -> Self
32 where T: Construct<T> {
33 Vector3(x, y, z)
34 }
35
36 pub fn normalize(&self, length: Option<T>) -> Vector3<T>
47 where T: Mul<T, Output = T> + Div<T, Output = T> + Add<T, Output = T> + F32Fmt + Copy {
48 let s = self.norm();
49 match length {
50 Some(len) => Vector3(self.0 / s * len, self.1 / s * len, self.2 / s * len),
51 None => Vector3(self.0 / s, self.1 / s, self.2 / s),
52 }
53 }
54
55 #[inline]
63 pub fn norm(&self) -> T
64 where T: Add<T, Output = T> + Mul<T, Output = T> + F32Fmt + Copy {
65 (self.0 * self.0 + self.1 * self.1 + self.2 * self.2).sqrt()
66 }
67
68 #[inline]
77 pub fn unit_vector(&self) -> Self
78 where Self: Div<T, Output = Self>, T: Add<T, Output = T> + Mul<T, Output = T> + F32Fmt + Copy {
79 *self / self.norm()
80 }
81
82 pub fn cross_product(a: Self, b: Self) -> Self
111 where T: Mul<T, Output = T> + Sub<T, Output = T> + Copy {
112 Vector3(
113 (a.1 * b.2) - (a.2 * b.1),
114 (a.2 * b.0) - (a.0 * b.2),
115 (a.0 * b.1) - (a.1 * b.0)
116 )
117 }
118
119 pub fn dot_product(a: Self, b: Self) -> T
131 where T: Mul<T, Output = T> + Add<T, Output = T> + Copy {
132 (a.0 * b.0) + (a.1 * b.1) + (a.2 * b.2)
133 }
134}
135
136impl<T> Construct<T> for Vector3<T> where T: Construct<T> {}
137impl<T> Vector<T> for Vector3<T> where T: Construct<T> {}
138
139impl<T> Add for Vector3<T>
140where T: Add<T, Output = T> + Copy {
141 type Output = Self;
142
143 fn add (self, rhs: Self) -> Self::Output{
152 Vector3(self.0 + rhs.0, self.1 + rhs.1, self.2 + rhs.2)
153 }
154}
155
156impl<T> Add<T> for Vector3<T>
157where T: Add<T, Output = T> + Copy {
158 type Output = Self;
159
160 fn add (self, rhs: T) -> Self::Output{
167 Vector3(self.0 + rhs, self.1 + rhs, self.2 + rhs)
168 }
169}
170
171impl<T> Sub for Vector3<T>
172where T: Sub<T, Output = T> + Copy {
173 type Output = Self;
174
175 fn sub (self, rhs: Self) -> Self::Output{
176 Vector3(self.0 - rhs.0, self.1 - rhs.1, self.2 - rhs.2)
177 }
178}
179
180impl<T> Sub<T> for Vector3<T>
181where T: Sub<T, Output = T> + Copy {
182 type Output = Self;
183
184 fn sub (self, rhs: T) -> Self::Output{
185 Vector3(self.0 - rhs, self.1 - rhs, self.2 - rhs)
186 }
187}
188
189impl<T> Mul for Vector3<T>
190where T: Mul<T, Output = T> + Copy {
191 type Output = Self;
192
193 fn mul (self, rhs: Self) -> Self::Output{
194 Vector3(self.0 * rhs.0, self.1 * rhs.1, self.2 * rhs.2)
195 }
196}
197
198impl<T> Mul<T> for Vector3<T>
199where T: Mul<T, Output = T> + Copy {
200 type Output = Self;
201
202 fn mul (self, rhs: T) -> Self::Output{
203 Vector3(self.0 * rhs, self.1 * rhs, self.2 * rhs)
204 }
205}
206
207impl<T> Div for Vector3<T>
208where T: Div<T, Output = T> + Copy {
209 type Output = Self;
210
211 fn div (self, rhs: Self) -> Self::Output{
212 Vector3(self.0 / rhs.0, self.1 / rhs.1, self.2 / rhs.2)
213 }
214}
215
216impl<T> Div<T> for Vector3<T>
217where T: Div<T, Output = T> + Copy {
218 type Output = Self;
219
220 fn div (self, rhs: T) -> Self::Output {
221 Vector3(self.0 / rhs, self.1 / rhs, self.2 / rhs)
222 }
223}
224
225impl<T> Rem for Vector3<T>
226where T: Rem<T, Output = T> + Copy {
227 type Output = Self;
228
229 fn rem (self, rhs: Self) -> Self::Output {
230 Vector3(self.0 % rhs.0, self.1 % rhs.1, self.2 % rhs.2)
231 }
232}
233
234impl<T> Rem<T> for Vector3<T>
235where T: Rem<T, Output = T> + Copy {
236 type Output = Self;
237
238 fn rem (self, rhs: T) -> Self::Output {
243 Vector3(self.0 % rhs, self.1 % rhs, self.2 % rhs)
244 }
245}
246
247impl<T> Neg for Vector3<T>
248where T: Neg<Output = T> + Copy {
249 type Output = Self;
250
251 fn neg(self) -> Self::Output {
252 Vector3(-self.0, -self.1, -self.2)
253 }
254}
255
256impl<T> Zero for Vector3<T> where T: Zero { const ZERO: Self = Vector3(T::ZERO, T::ZERO, T::ZERO); }
257impl<T> One for Vector3<T> where T: One { const ONE: Self = Vector3(T::ONE, T::ONE, T::ONE); }
258impl<T> Two for Vector3<T> where T: Two { const TWO: Self = Vector3(T::TWO, T::TWO, T::TWO); }
259
260impl<T> From<Vector3<T>> for [T; 3]
261where T: Copy {
262 fn from(other: Vector3<T>) -> [T; 3]{
263 [other.0, other.1, other.2]
264 }
265}
266
267impl<T> From<[T; 3]> for Vector3<T>
268where T: Copy{
269 fn from(other: [T; 3]) -> Self {
270 Vector3(other[0], other[1], other[2])
271 }
272}
273
274impl<T> F32Fmt for Vector3<T>
275where T: F32Fmt + Copy {
276 type F32Fmt = Vector3<T::F32Fmt>;
277 #[inline]
278 fn intoF32Fmt(self) -> Self::F32Fmt {
279 Vector3(self.0.intoF32Fmt(), self.1.intoF32Fmt(), self.2.intoF32Fmt())
280 }
281 #[inline]
282 fn fromF32Fmt(f32_fmt: Self::F32Fmt) -> Self {
283 let vec = &f32_fmt;
284 Vector3(T::fromF32Fmt(vec.0), T::fromF32Fmt(vec.1), T::fromF32Fmt(f32_fmt.2))
285 }
286
287 fn sqrt(self) -> Self {
289 Vector3(self.0.sqrt(), self.1.sqrt(), self.2.sqrt())
290 }
291
292 fn cbrt(self) -> Self {
293 Vector3(self.0.cbrt(), self.1.cbrt(), self.2.cbrt())
294 }
295
296 fn f32_const_mul(self, constant: f32) -> Self {
297 Vector3(self.0.f32_const_mul(constant), self.1.f32_const_mul(constant), self.2.f32_const_mul(constant))
298 }
299
300 fn sin_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
301 todo!()
302 }
303
304 fn cos_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
305 todo!()
306 }
307
308 fn tan_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
309 todo!()
310 }
311
312 fn asin_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
313 todo!()
314 }
315
316 fn acos_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
317 todo!()
318 }
319
320 fn atan_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
321 todo!()
322 }
323
324 fn atan2_mul(self, _other: Self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
325 todo!()
326 }
327
328 fn sinh_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
329 todo!()
330 }
331
332 fn cosh_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
333 todo!()
334 }
335
336 fn tanh_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
337 todo!()
338 }
339}
340impl<T> SignOps for Vector3<T> {
341 fn ptcopysign(self, _sign: Self) -> Self {
342 todo!()
343 }
344
345 fn ptsignum(self) -> i8 {
346 todo!()
347 }
348
349 fn abs(self) -> Self {
350 todo!()
351 }
352}