feo_math/linear_algebra/
vector4.rs

1use std::{fmt, ops::{Add, Div, Mul, Neg, Rem, Sub}};
2
3use crate::{Construct, F32Fmt, One, SignOps, /*Sum,*/ Two, Zero};
4
5use super::{Vector, vector3::Vector3};
6
7/// A 4D vector
8#[derive(PartialEq, Clone, Copy)]
9pub struct Vector4<T>(pub T, pub T, pub T, pub T);
10
11impl<T> Vector4<T> where T: Copy{
12    /// Returns a new Vector4
13    /// # Arguments
14    /// * `x` - The x direction scalar .0
15    /// * `y` - The y direction scalar .1
16    /// * `z` - The z direction scalar .2
17    /// * `w` - The w direction scalar .3
18    pub fn new(x: T, y: T, z: T, w: T) -> Self {
19        Vector4(x, y, z, w)
20    }
21
22    /// Returns a new Vector4 created by adding a w value to a Vector3
23    /// # Arguments
24    /// * `vec3` - The 3D Vector that the w direction scaler is added onto
25    /// * `w` - The w direction scalar .3
26    pub fn from_vector3(vec3: Vector3<T>, w: T) -> Self {
27        Vector4(vec3.0, vec3.1, vec3.2, w)
28    }
29
30    pub fn empty(zero: T) -> Self {
31        Vector4(zero, zero, zero, zero)
32    }
33
34    pub fn normalize(&self, length: Option<T>) -> Vector4<T>
35    where T: Mul<T, Output = T> + Div<T, Output = T> + Add<T, Output = T> + F32Fmt {
36        let size: T = self.norm();
37        match length{
38            Some(len) => Vector4((self.0 / size) * len, (self.1 / size) * len, (self.2 / size) * len, (self.3 / size) * len),
39            None => Vector4(self.0 / size, self.1 / size, self.2 / size, self.3 / size),
40        }
41    }
42
43    /// Returns the magnitude or length of the Vector4 denoted `||q||`
44    ///
45    pub fn norm(&self) -> T
46    where T: Add<T, Output = T> + Mul<T, Output = T> + F32Fmt + Copy {
47        (self.0 * self.0 + self.1 * self.1 + self.2 * self.2 + self.3 * self.3).sqrt()
48    }
49}
50
51impl<T> Construct<T> for Vector4<T> where T: Construct<T> {}
52impl<T> Vector<T> for Vector4<T> where T: Construct<T> {/* none */}
53
54impl<T> fmt::Debug for Vector4<T>
55where T: fmt::Debug + Copy {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        write!(f, "({:?}, {:?}, {:?}, {:?})", self.0, self.1, self.2, self.3)
58    }
59}
60
61impl<T> Add for Vector4<T> 
62where T: Add<T, Output = T> + Copy {
63    type Output = Self;
64
65    fn add (self, rhs: Self) -> Self::Output {
66        Vector4(self.0 + rhs.0, self.1 + rhs.1, self.2 + rhs.2, self.3 + rhs.3)
67    }
68}
69
70impl<T> Add<T> for Vector4<T> 
71where T: Add<T, Output = T> + Copy {
72    type Output = Self;
73
74    fn add (self, rhs: T) -> Self::Output {
75        Vector4(self.0 + rhs, self.1 + rhs, self.2 + rhs, self.3 + rhs)
76    }
77}
78
79impl<T> Sub for Vector4<T> 
80where T: Sub<T, Output = T> + Copy {
81    type Output = Self;
82
83    fn sub (self, rhs: Self) -> Self::Output {
84        Vector4(self.0 - rhs.0, self.1 - rhs.1, self.2 - rhs.2, self.3 - rhs.3)
85    }
86}
87
88impl<T> Sub<T> for Vector4<T> 
89where T: Sub<T, Output = T> + Copy {
90    type Output = Self;
91
92    fn sub (self, rhs: T) -> Self::Output {
93        Vector4(self.0 - rhs, self.1 - rhs, self.2 - rhs, self.3 - rhs)
94    }
95}
96
97impl<T> Mul for Vector4<T> where T: Mul<T, Output = T> + Copy{
98    type Output = Self;
99
100    fn mul (self, rhs: Self) -> Self::Output{
101        Vector4(self.0 * rhs.0, self.1 * rhs.1, self.2 * rhs.2, self.3 * rhs.3)
102    }
103}
104
105impl<T> Mul<T> for Vector4<T> 
106where T: Mul<T, Output = T> + Copy {
107    type Output = Self;
108
109    fn mul (self, rhs: T) -> Self::Output{
110        Vector4(self.0 * rhs, self.1 * rhs, self.2 * rhs, self.3 * rhs)
111    }
112}
113
114impl<T> Div for Vector4<T>
115where T: Div<T, Output = T> + Copy {
116    type Output = Self;
117
118    fn div (self, rhs: Self) -> Self::Output{
119        Vector4(self.0 / rhs.0, self.1 / rhs.1, self.2 / rhs.2, self.3 / rhs.3)
120    }
121}
122
123impl<T> Div<T> for Vector4<T> 
124where T: Div<T, Output = T> + Copy {
125    type Output = Self;
126
127    fn div (self, rhs: T) -> Self::Output{
128        Vector4(self.0 / rhs, self.1 / rhs, self.2 / rhs, self.3 / rhs)
129    }
130}
131
132impl<T> Rem for Vector4<T>
133where T: Rem<T, Output = T> + Copy {
134    type Output = Self;
135
136    /// Vector4<T> % Vector4<T>
137    /// = Vector4<T>
138    /// # Examples
139    /// ```
140    /// use feo_math::linear_algebra::vector4::Vector4;
141    /// 
142    /// ```
143    fn rem (self, rhs: Self) -> Self::Output{
144        Vector4(self.0 % rhs.0, self.1 % rhs.1, self.2 % rhs.2, self.3 % rhs.3)
145    }
146}
147
148impl<T> Rem<T> for Vector4<T> 
149where T: Rem<T, Output = T> + Copy {
150    type Output = Self;
151
152    fn rem (self, rhs: T) -> Self::Output{
153        Vector4(self.0 % rhs, self.1 % rhs, self.2 % rhs, self.3 % rhs)
154    }
155}
156
157impl<T> Neg for Vector4<T> 
158where T: Neg<Output = T> + Copy {
159    type Output = Self;
160
161    fn neg(self) -> Self::Output{
162        Vector4(-self.0, -self.1, -self.2, -self.3)
163    }
164}
165
166impl<T> Zero for Vector4<T> 
167where T: Zero {
168    const ZERO: Self = Vector4(T::ZERO, T::ZERO, T::ZERO, T::ZERO);
169}
170impl<T> One for Vector4<T> 
171where T: One {
172    const ONE: Self = Vector4(T::ONE, T::ONE, T::ONE, T::ONE);
173}
174impl<T> Two for Vector4<T> 
175where T: Two {
176    const TWO: Self = Vector4(T::TWO, T::TWO, T::TWO, T::TWO);
177}
178
179impl<T> From<Vector4<T>> for [T; 4]{
180    fn from(other: Vector4<T>) -> [T; 4]{
181        [other.0, other.1, other.2, other.3]
182    }
183}
184
185impl<T> From<Vector4<T>> for Vector3<T> where T: Copy{
186    fn from(other: Vector4<T>) -> Vector3<T>{
187        Vector3(other.0, other.1, other.2)
188    }
189}
190
191impl<T> From<[T; 4]> for Vector4<T> 
192where T: Copy{
193    fn from(other: [T; 4]) -> Self {
194        Vector4(other[0], other[1], other[2], other[3])
195    }
196}
197
198impl<T: F32Fmt> F32Fmt for Vector4<T> 
199where T: Mul<T, Output = T> { 
200    type F32Fmt = Vector4<T::F32Fmt>;
201    #[inline]
202    fn intoF32Fmt(self) -> Self::F32Fmt {
203        Vector4(self.0.intoF32Fmt(), self.1.intoF32Fmt(), self.2.intoF32Fmt(), self.3.intoF32Fmt())
204    } 
205    #[inline]
206    fn fromF32Fmt(f32_fmt: Self::F32Fmt) -> Self {
207        let vec = &f32_fmt;
208        Vector4(T::fromF32Fmt(vec.0), T::fromF32Fmt(vec.1), T::fromF32Fmt(f32_fmt.2), T::fromF32Fmt(f32_fmt.3))
209    }
210
211    fn sqrt(self) -> Self {
212        Vector4(self.0.sqrt(), self.1.sqrt(), self.2.sqrt(), self.3.sqrt())
213    }
214
215    fn cbrt(self) -> Self {
216        Vector4(self.0.cbrt(), self.1.cbrt(), self.2.cbrt(), self.3.cbrt())
217    }
218
219    fn f32_const_mul(self, constant: f32) -> Self {
220        Vector4(self.0.f32_const_mul(constant), self.1.f32_const_mul(constant), self.2.f32_const_mul(constant), self.3.f32_const_mul(constant))
221    }
222
223    fn sin_mul(self, mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
224        Vector4(self.0.sin_mul(mul_by.0), self.1.sin_mul(mul_by.1), self.2.sin_mul(mul_by.2), self.3.sin_mul(mul_by.3))
225    }
226
227    fn cos_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
228        todo!()
229    }
230
231    fn tan_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
232        todo!()
233    }
234
235    fn asin_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
236        todo!()
237    }
238
239    fn acos_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
240        todo!()
241    }
242
243    fn atan_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
244        todo!()
245    }
246
247    fn atan2_mul(self, _other: Self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
248        todo!()
249    }
250
251    fn sinh_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
252        todo!()
253    }
254
255    fn cosh_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
256        todo!()
257    }
258
259    fn tanh_mul(self, _mul_by: Self) -> Self where Self: Mul<Self, Output = Self> + Sized {
260        todo!()
261    }
262}
263impl<T> SignOps for Vector4<T> {
264    fn ptcopysign(self, _sign: Self) -> Self {
265        todo!()
266    }
267
268    fn ptsignum(self) -> i8 {
269        todo!()
270    }
271
272    fn abs(self) -> Self {
273        todo!()
274    }
275}