Debian Codegraph @

debian/missing-sources/glam-0.17.3/src/vec3.rs

raw symbols SCIP
1 use crate::core::traits::vector::*;
2 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
3 use crate::BVec3A;
4 use crate::{BVec3, DVec2, DVec4, IVec2, IVec4, UVec2, UVec4, Vec2, Vec4, XYZ};
5 #[cfg(not(target_arch = "spirv"))]
6 use core::fmt;
7 use core::{f32, ops::*};
8
9 #[cfg(not(feature = "std"))]
10 use num_traits::Float;
11
12 #[cfg(all(
13 target_arch = "x86",
14 target_feature = "sse2",
15 not(feature = "scalar-math")
16 ))]
17 use core::arch::x86::*;
18 #[cfg(all(
19 target_arch = "x86_64",
20 target_feature = "sse2",
21 not(feature = "scalar-math")
22 ))]
23 use core::arch::x86_64::*;
24
25 #[cfg(feature = "std")]
26 use std::iter::{Product, Sum};
27
28 macro_rules! impl_vec3_common_methods {
29 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
30 /// All zeroes.
31 pub const ZERO: Self = Self(VectorConst::ZERO);
32
33 /// All ones.
34 pub const ONE: Self = Self(VectorConst::ONE);
35
36 /// `[1, 0, 0]`: a unit-length vector pointing along the positive X axis.
37 pub const X: Self = Self(Vector3Const::X);
38
39 /// `[0, 1, 0]`: a unit-length vector pointing along the positive Y axis.
40 pub const Y: Self = Self(Vector3Const::Y);
41
42 /// `[0, 0, 1]`: a unit-length vector pointing along the positive Z axis.
43 pub const Z: Self = Self(Vector3Const::Z);
44
45 /// The unit axes.
46 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
47
48 /// Creates a new 3D vector.
49 #[inline(always)]
50 pub fn new(x: $t, y: $t, z: $t) -> Self {
51 Self(Vector3::new(x, y, z))
52 }
53
54 /// Creates a 4D vector from `self` and the given `w` value.
55 #[inline(always)]
56 pub fn extend(self, w: $t) -> $vec4 {
57 // TODO: Optimize?
58 $vec4(Vector4::new(self.x, self.y, self.z, w))
59 }
60
61 /// Creates a `Vec2` from the `x` and `y` elements of `self`, discarding `z`.
62 ///
63 /// Truncation may also be performed by using `self.xy()` or `Vec2::from()`.
64 #[inline(always)]
65 pub fn truncate(self) -> $vec2 {
66 $vec2(Vector3::into_xy(self.0))
67 }
68
69 /// Returns the dot product result in all elements of the vector
70 #[inline(always)]
71 #[allow(dead_code)]
72 pub(crate) fn dot_as_vec3(self, other: Self) -> Self {
73 Self(Vector3::dot_into_vec(self.0, other.0))
74 }
75
76 /// Computes the cross product of `self` and `other`.
77 #[inline(always)]
78 pub fn cross(self, other: Self) -> Self {
79 Self(self.0.cross(other.0))
80 }
81
82 /// `[x, y, z]`
83 #[inline(always)]
84 pub fn to_array(&self) -> [$t; 3] {
85 [self.x, self.y, self.z]
86 }
87
88 impl_vecn_common_methods!($t, $vec3, $mask, $inner, Vector3);
89 };
90 }
91
92 macro_rules! impl_vec3_common_traits {
93 ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $inner:ident) => {
94 /// Creates a 3-dimensional vector.
95 #[inline(always)]
96 pub fn $new(x: $t, y: $t, z: $t) -> $vec3 {
97 $vec3::new(x, y, z)
98 }
99
100 impl Index<usize> for $vec3 {
101 type Output = $t;
102 #[inline(always)]
103 fn index(&self, index: usize) -> &Self::Output {
104 match index {
105 0 => &self.x,
106 1 => &self.y,
107 2 => &self.z,
108 _ => panic!("index out of bounds"),
109 }
110 }
111 }
112
113 impl IndexMut<usize> for $vec3 {
114 #[inline(always)]
115 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
116 match index {
117 0 => &mut self.x,
118 1 => &mut self.y,
119 2 => &mut self.z,
120 _ => panic!("index out of bounds"),
121 }
122 }
123 }
124
125 #[cfg(not(target_arch = "spirv"))]
126 impl fmt::Debug for $vec3 {
127 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
128 fmt.debug_tuple(stringify!($vec3))
129 .field(&self.x)
130 .field(&self.y)
131 .field(&self.z)
132 .finish()
133 }
134 }
135
136 #[cfg(not(target_arch = "spirv"))]
137 impl fmt::Display for $vec3 {
138 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
140 }
141 }
142
143 impl From<($vec2, $t)> for $vec3 {
144 #[inline(always)]
145 fn from((v, z): ($vec2, $t)) -> Self {
146 Self::new(v.x, v.y, z)
147 }
148 }
149
150 impl From<($t, $t, $t)> for $vec3 {
151 #[inline(always)]
152 fn from(t: ($t, $t, $t)) -> Self {
153 Self(Vector3::from_tuple(t))
154 }
155 }
156
157 impl From<$vec3> for ($t, $t, $t) {
158 #[inline(always)]
159 fn from(v: $vec3) -> Self {
160 v.into_tuple()
161 }
162 }
163
164 impl From<$vec3> for $vec2 {
165 /// Creates a `Vec2` from the `x` and `y` elements of `self`, discarding `z`.
166 #[inline(always)]
167 fn from(v: $vec3) -> Self {
168 Self(v.into_xy())
169 }
170 }
171
172 impl Deref for $vec3 {
173 type Target = XYZ<$t>;
174 #[inline(always)]
175 fn deref(&self) -> &Self::Target {
176 self.0.as_ref_xyz()
177 }
178 }
179
180 impl DerefMut for $vec3 {
181 #[inline(always)]
182 fn deref_mut(&mut self) -> &mut Self::Target {
183 self.0.as_mut_xyz()
184 }
185 }
186
187 impl_vecn_common_traits!($t, 3, $vec3, $inner, Vector3);
188 };
189 }
190
191 macro_rules! impl_vec3_signed_methods {
192 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
193 impl_vec3_common_methods!($t, $vec2, $vec3, $vec4, $mask, $inner);
194 impl_vecn_signed_methods!($t, $vec3, $mask, $inner, SignedVector3);
195 };
196 }
197
198 macro_rules! impl_vec3_float_methods {
199 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
200 impl_vec3_signed_methods!($t, $vec2, $vec3, $vec4, $mask, $inner);
201 impl_vecn_float_methods!($t, $vec3, $mask, $inner, FloatVector3);
202
203 /// Returns the angle (in radians) between two vectors.
204 ///
205 /// The input vectors do not need to be unit length however they must be non-zero.
206 #[inline(always)]
207 pub fn angle_between(self, other: Self) -> $t {
208 self.0.angle_between(other.0)
209 }
210
211 /// Returns somes vector that is orthogonal to the given one.
212 ///
213 /// The input vector must be finite and non-zero.
214 ///
215 /// The output vector is not necessarily unit-length.
216 /// For that use [`Self::any_orthonormal_vector`] instead.
217 #[inline]
218 #[cfg(feature = "std")]
219 pub fn any_orthogonal_vector(&self) -> Self {
220 // This can probably be optimized
221 if self.x.abs() > self.y.abs() {
222 Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y)
223 } else {
224 Self::new(0.0, self.z, -self.y) // self.cross(Self::X)
225 }
226 }
227
228 /// Returns any unit-length vector that is orthogonal to the given one.
229 /// The input vector must be finite and non-zero.
230 ///
231 /// # Panics
232 ///
233 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
234 #[inline]
235 #[cfg(feature = "std")]
236 pub fn any_orthonormal_vector(&self) -> Self {
237 glam_assert!(self.is_normalized());
238 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
239 let sign = (1.0 as $t).copysign(self.z);
240 let a = -1.0 / (sign + self.z);
241 let b = self.x * self.y * a;
242 Self::new(b, sign + self.y * self.y * a, -self.y)
243 }
244
245 /// Given a unit-length vector return two other vectors that together form an orthonormal
246 /// basis. That is, all three vectors are orthogonal to each other and are normalized.
247 ///
248 /// # Panics
249 ///
250 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
251 #[inline]
252 #[cfg(feature = "std")]
253 pub fn any_orthonormal_pair(&self) -> (Self, Self) {
254 glam_assert!(self.is_normalized());
255 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
256 let sign = (1.0 as $t).copysign(self.z);
257 let a = -1.0 / (sign + self.z);
258 let b = self.x * self.y * a;
259 (
260 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
261 Self::new(b, sign + self.y * self.y * a, -self.y),
262 )
263 }
264 };
265 }
266
267 // implements traits that are common between `Vec3`, `Vec3A` and `Vec4` types.
268 macro_rules! impl_vec3_float_traits {
269 ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $inner:ident) => {
270 impl_vec3_common_traits!($t, $new, $vec2, $vec3, $vec4, $inner);
271 impl_vecn_signed_traits!($t, 3, $vec3, $inner, SignedVector3);
272 };
273 }
274
275 // implements f32 functionality common between `Vec3` and `Vec3A` types.
276 macro_rules! impl_f32_vec3 {
277 ($new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
278 impl $vec3 {
279 impl_vec3_float_methods!(f32, $vec2, $vec3, $vec4, $mask, $inner);
280 impl_vecn_as_f64!(DVec3, x, y, z);
281 impl_vecn_as_i32!(IVec3, x, y, z);
282 impl_vecn_as_u32!(UVec3, x, y, z);
283 }
284 impl_vec3_float_traits!(f32, $new, $vec2, $vec3, $vec4, $inner);
285 };
286 }
287
288 type XYZF32 = XYZ<f32>;
289
290 /// A 3-dimensional vector without SIMD support.
291 #[derive(Clone, Copy)]
292 #[repr(transparent)]
293 pub struct Vec3(pub(crate) XYZF32);
294 impl_f32_vec3!(vec3, Vec2, Vec3, Vec4, BVec3, XYZF32);
295
296 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
297 type XYZF32A = __m128;
298
299 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
300 type XYZF32A = crate::core::storage::XYZF32A16;
301
302 /// A 3-dimensional vector with SIMD support.
303 ///
304 /// This type is 16 byte aligned. A SIMD vector type is used for storage on supported platforms for
305 /// better performance than the `Vec3` type.
306 ///
307 /// It is possible to convert between `Vec3` and `Vec3A` types using `From` trait implementations.
308 #[derive(Clone, Copy)]
309 #[repr(transparent)]
310 pub struct Vec3A(pub(crate) XYZF32A);
311
312 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
313 impl_f32_vec3!(vec3a, Vec2, Vec3A, Vec4, BVec3A, XYZF32A);
314
315 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
316 impl_f32_vec3!(vec3a, Vec2, Vec3A, Vec4, BVec3, XYZF32A);
317
318 impl From<Vec3> for Vec3A {
319 #[inline(always)]
320 fn from(v: Vec3) -> Self {
321 Self(v.0.into())
322 }
323 }
324
325 impl From<Vec3A> for Vec3 {
326 #[inline(always)]
327 fn from(v: Vec3A) -> Self {
328 Self(v.0.into())
329 }
330 }
331
332 type XYZF64 = XYZ<f64>;
333
334 /// A 3-dimensional vector.
335 #[derive(Clone, Copy)]
336 #[repr(transparent)]
337 pub struct DVec3(pub(crate) XYZF64);
338
339 impl DVec3 {
340 impl_vec3_float_methods!(f64, DVec2, DVec3, DVec4, BVec3, XYZF64);
341 impl_vecn_as_f32!(Vec3, x, y, z);
342 impl_vecn_as_i32!(IVec3, x, y, z);
343 impl_vecn_as_u32!(UVec3, x, y, z);
344 }
345 impl_vec3_float_traits!(f64, dvec3, DVec2, DVec3, DVec4, XYZF64);
346
347 type XYZI32 = XYZ<i32>;
348
349 /// A 3-dimensional vector.
350 #[derive(Clone, Copy)]
351 #[repr(transparent)]
352 pub struct IVec3(pub(crate) XYZI32);
353
354 impl IVec3 {
355 impl_vec3_common_methods!(i32, IVec2, IVec3, IVec4, BVec3, XYZI32);
356 impl_vecn_signed_methods!(i32, IVec3, BVec3, XYZI32, SignedVector3);
357 impl_vecn_as_f32!(Vec3, x, y, z);
358 impl_vecn_as_f64!(DVec3, x, y, z);
359 impl_vecn_as_u32!(UVec3, x, y, z);
360 }
361 impl_vec3_common_traits!(i32, ivec3, IVec2, IVec3, IVec4, XYZI32);
362 impl_vecn_signed_traits!(i32, 3, IVec3, XYZI32, SignedVector3);
363 impl_vecn_eq_hash_traits!(i32, 3, IVec3);
364
365 type XYZU32 = XYZ<u32>;
366
367 /// A 3-dimensional vector.
368 #[derive(Clone, Copy)]
369 #[repr(transparent)]
370 pub struct UVec3(pub(crate) XYZU32);
371
372 impl UVec3 {
373 impl_vec3_common_methods!(u32, UVec2, UVec3, UVec4, BVec3, XYZU32);
374 impl_vecn_as_f32!(Vec3, x, y, z);
375 impl_vecn_as_f64!(DVec3, x, y, z);
376 impl_vecn_as_i32!(IVec3, x, y, z);
377 }
378 impl_vec3_common_traits!(u32, uvec3, UVec2, UVec3, UVec4, XYZU32);
379 impl_vecn_eq_hash_traits!(u32, 3, UVec3);
380
381 #[test]
382 fn test_vec3_private() {
383 assert_eq!(
384 vec3a(1.0, 1.0, 1.0).mul_add(vec3a(0.5, 2.0, -4.0), vec3a(-1.0, -1.0, -1.0)),
385 vec3a(-0.5, 1.0, -5.0)
386 );
387 }
388
389 mod const_test_vec3 {
390 const_assert_eq!(
391 core::mem::align_of::<f32>(),
392 core::mem::align_of::<super::Vec3>()
393 );
394 const_assert_eq!(12, core::mem::size_of::<super::Vec3>());
395 }
396
397 mod const_test_vec3a {
398 const_assert_eq!(16, core::mem::align_of::<super::Vec3A>());
399 const_assert_eq!(16, core::mem::size_of::<super::Vec3A>());
400 }
401
402 mod const_test_dvec3 {
403 const_assert_eq!(
404 core::mem::align_of::<f64>(),
405 core::mem::align_of::<super::DVec3>()
406 );
407 const_assert_eq!(24, core::mem::size_of::<super::DVec3>());
408 }
409
410 mod const_test_ivec3 {
411 const_assert_eq!(
412 core::mem::align_of::<i32>(),
413 core::mem::align_of::<super::IVec3>()
414 );
415 const_assert_eq!(12, core::mem::size_of::<super::IVec3>());
416 }
417
418 mod const_test_uvec3 {
419 const_assert_eq!(
420 core::mem::align_of::<u32>(),
421 core::mem::align_of::<super::UVec3>()
422 );
423 const_assert_eq!(12, core::mem::size_of::<super::UVec3>());
424 }