Skip to main content

windows_numerics/
vector3.rs

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