Debian Codegraph @

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

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