Debian Codegraph @

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