Debian Codegraph @

debian/missing-sources/glam-0.16.0/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, $mask: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 #[cfg(not(target_arch = "spirv"))]
101 impl fmt::Debug for $vec3 {
102 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
103 fmt.debug_tuple(stringify!($vec3))
104 .field(&self.x)
105 .field(&self.y)
106 .field(&self.z)
107 .finish()
108 }
109 }
110
111 #[cfg(not(target_arch = "spirv"))]
112 impl fmt::Display for $vec3 {
113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
115 }
116 }
117
118 impl From<($vec2, $t)> for $vec3 {
119 #[inline(always)]
120 fn from((v, z): ($vec2, $t)) -> Self {
121 Self::new(v.x, v.y, z)
122 }
123 }
124
125 impl From<($t, $t, $t)> for $vec3 {
126 #[inline(always)]
127 fn from(t: ($t, $t, $t)) -> Self {
128 Self(Vector3::from_tuple(t))
129 }
130 }
131
132 impl From<$vec3> for ($t, $t, $t) {
133 #[inline(always)]
134 fn from(v: $vec3) -> Self {
135 v.into_tuple()
136 }
137 }
138
139 impl From<$vec3> for $vec2 {
140 /// Creates a `Vec2` from the `x` and `y` elements of `self`, discarding `z`.
141 #[inline(always)]
142 fn from(v: $vec3) -> Self {
143 Self(v.into_xy())
144 }
145 }
146
147 impl Deref for $vec3 {
148 type Target = XYZ<$t>;
149 #[inline(always)]
150 fn deref(&self) -> &Self::Target {
151 self.0.as_ref_xyz()
152 }
153 }
154
155 impl DerefMut for $vec3 {
156 #[inline(always)]
157 fn deref_mut(&mut self) -> &mut Self::Target {
158 self.0.as_mut_xyz()
159 }
160 }
161
162 impl_vecn_common_traits!($t, 3, $vec3, $inner, Vector3);
163 };
164 }
165
166 macro_rules! impl_vec3_signed_methods {
167 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
168 impl_vec3_common_methods!($t, $vec2, $vec3, $vec4, $mask, $inner);
169 impl_vecn_signed_methods!($t, $vec3, $mask, $inner, SignedVector3);
170 };
171 }
172
173 macro_rules! impl_vec3_float_methods {
174 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
175 impl_vec3_signed_methods!($t, $vec2, $vec3, $vec4, $mask, $inner);
176 impl_vecn_float_methods!($t, $vec3, $mask, $inner, FloatVector3);
177
178 /// Returns the angle (in radians) between two vectors.
179 ///
180 /// The input vectors do not need to be unit length however they must be non-zero.
181 #[inline(always)]
182 pub fn angle_between(self, other: Self) -> $t {
183 self.0.angle_between(other.0)
184 }
185
186 /// Returns somes vector that is orthogonal to the given one.
187 ///
188 /// The input vector must be finite and non-zero.
189 ///
190 /// The output vector is not necessarily unit-length.
191 /// For that use [`Self::any_orthonormal_vector`] instead.
192 #[inline]
193 #[cfg(feature = "std")]
194 pub fn any_orthogonal_vector(&self) -> Self {
195 // This can probably be optimized
196 if self.x.abs() > self.y.abs() {
197 Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y)
198 } else {
199 Self::new(0.0, self.z, -self.y) // self.cross(Self::X)
200 }
201 }
202
203 /// Returns any unit-length vector that is orthogonal to the given one.
204 /// The input vector must be finite and non-zero.
205 #[inline]
206 #[cfg(feature = "std")]
207 pub fn any_orthonormal_vector(&self) -> Self {
208 glam_assert!(self.is_normalized());
209 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
210 let sign = (1.0 as $t).copysign(self.z);
211 let a = -1.0 / (sign + self.z);
212 let b = self.x * self.y * a;
213 Self::new(b, sign + self.y * self.y * a, -self.y)
214 }
215
216 /// Given a unit-length vector return two other vectors that together form an orthonormal basis.
217 /// That is, all three vectors are orthogonal to each other and are normalized.
218 #[inline]
219 #[cfg(feature = "std")]
220 pub fn any_orthonormal_pair(&self) -> (Self, Self) {
221 glam_assert!(self.is_normalized());
222 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
223 let sign = (1.0 as $t).copysign(self.z);
224 let a = -1.0 / (sign + self.z);
225 let b = self.x * self.y * a;
226 (
227 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
228 Self::new(b, sign + self.y * self.y * a, -self.y),
229 )
230 }
231 };
232 }
233
234 // implements traits that are common between `Vec3`, `Vec3A` and `Vec4` types.
235 macro_rules! impl_vec3_float_traits {
236 ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
237 impl_vec3_common_traits!($t, $new, $vec2, $vec3, $vec4, $mask, $inner);
238 impl_vecn_signed_traits!($t, 3, $vec3, $inner, SignedVector3);
239 };
240 }
241
242 // implements f32 functionality common between `Vec3` and `Vec3A` types.
243 macro_rules! impl_f32_vec3 {
244 ($new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
245 impl $vec3 {
246 impl_vec3_float_methods!(f32, $vec2, $vec3, $vec4, $mask, $inner);
247 impl_vecn_as_f64!(DVec3, x, y, z);
248 impl_vecn_as_i32!(IVec3, x, y, z);
249 impl_vecn_as_u32!(UVec3, x, y, z);
250 }
251 impl_vec3_float_traits!(f32, $new, $vec2, $vec3, $vec4, $mask, $inner);
252 };
253 }
254
255 type XYZF32 = XYZ<f32>;
256
257 /// A 3-dimensional vector without SIMD support.
258 #[derive(Clone, Copy)]
259 #[repr(transparent)]
260 pub struct Vec3(pub(crate) XYZF32);
261 impl_f32_vec3!(vec3, Vec2, Vec3, Vec4, BVec3, XYZF32);
262
263 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
264 type XYZF32A = __m128;
265
266 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
267 type XYZF32A = crate::core::storage::XYZF32A16;
268
269 /// A 3-dimensional vector with SIMD support.
270 ///
271 /// This type is 16 byte aligned. A SIMD vector type is used for storage on supported platforms for
272 /// better performance than the `Vec3` type.
273 ///
274 /// It is possible to convert between `Vec3` and `Vec3A` types using `From` trait implementations.
275 #[derive(Clone, Copy)]
276 #[repr(transparent)]
277 pub struct Vec3A(pub(crate) XYZF32A);
278
279 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
280 impl_f32_vec3!(vec3a, Vec2, Vec3A, Vec4, BVec3A, XYZF32A);
281
282 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
283 impl_f32_vec3!(vec3a, Vec2, Vec3A, Vec4, BVec3, XYZF32A);
284
285 impl From<Vec3> for Vec3A {
286 #[inline(always)]
287 fn from(v: Vec3) -> Self {
288 Self(v.0.into())
289 }
290 }
291
292 impl From<Vec3A> for Vec3 {
293 #[inline(always)]
294 fn from(v: Vec3A) -> Self {
295 Self(v.0.into())
296 }
297 }
298
299 type XYZF64 = XYZ<f64>;
300
301 /// A 3-dimensional vector.
302 #[derive(Clone, Copy)]
303 #[repr(transparent)]
304 pub struct DVec3(pub(crate) XYZF64);
305
306 impl DVec3 {
307 impl_vec3_float_methods!(f64, DVec2, DVec3, DVec4, BVec3, XYZF64);
308 impl_vecn_as_f32!(Vec3, x, y, z);
309 impl_vecn_as_i32!(IVec3, x, y, z);
310 impl_vecn_as_u32!(UVec3, x, y, z);
311 }
312 impl_vec3_float_traits!(f64, dvec3, DVec2, DVec3, DVec4, BVec3, XYZF64);
313
314 type XYZI32 = XYZ<i32>;
315
316 /// A 3-dimensional vector.
317 #[derive(Clone, Copy)]
318 #[repr(transparent)]
319 pub struct IVec3(pub(crate) XYZI32);
320
321 impl IVec3 {
322 impl_vec3_common_methods!(i32, IVec2, IVec3, IVec4, BVec3, XYZI32);
323 impl_vecn_signed_methods!(i32, IVec3, BVec3, XYZI32, SignedVector3);
324 impl_vecn_as_f32!(Vec3, x, y, z);
325 impl_vecn_as_f64!(DVec3, x, y, z);
326 impl_vecn_as_u32!(UVec3, x, y, z);
327 }
328 impl_vec3_common_traits!(i32, ivec3, IVec2, IVec3, IVec4, BVec3, XYZI32);
329 impl_vecn_signed_traits!(i32, 3, IVec3, XYZI32, SignedVector3);
330 impl_vecn_eq_hash_traits!(i32, 3, IVec3);
331
332 type XYZU32 = XYZ<u32>;
333
334 /// A 3-dimensional vector.
335 #[derive(Clone, Copy)]
336 #[repr(transparent)]
337 pub struct UVec3(pub(crate) XYZU32);
338
339 impl UVec3 {
340 impl_vec3_common_methods!(u32, UVec2, UVec3, UVec4, BVec3, XYZU32);
341 impl_vecn_as_f32!(Vec3, x, y, z);
342 impl_vecn_as_f64!(DVec3, x, y, z);
343 impl_vecn_as_i32!(IVec3, x, y, z);
344 }
345 impl_vec3_common_traits!(u32, uvec3, UVec2, UVec3, UVec4, BVec3, XYZU32);
346 impl_vecn_eq_hash_traits!(u32, 3, UVec3);
347
348 #[test]
349 fn test_vec3_private() {
350 assert_eq!(
351 vec3a(1.0, 1.0, 1.0).mul_add(vec3a(0.5, 2.0, -4.0), vec3a(-1.0, -1.0, -1.0)),
352 vec3a(-0.5, 1.0, -5.0)
353 );
354 }
355
356 mod const_test_vec3 {
357 const_assert_eq!(4, core::mem::align_of::<super::Vec3>());
358 const_assert_eq!(12, core::mem::size_of::<super::Vec3>());
359 }
360
361 mod const_test_vec3a {
362 const_assert_eq!(16, core::mem::align_of::<super::Vec3A>());
363 const_assert_eq!(16, core::mem::size_of::<super::Vec3A>());
364 }
365
366 mod const_test_dvec3 {
367 const_assert_eq!(8, core::mem::align_of::<super::DVec3>());
368 const_assert_eq!(24, core::mem::size_of::<super::DVec3>());
369 }
370
371 mod const_test_ivec3 {
372 const_assert_eq!(4, core::mem::align_of::<super::IVec3>());
373 const_assert_eq!(12, core::mem::size_of::<super::IVec3>());
374 }
375
376 mod const_test_uvec3 {
377 const_assert_eq!(4, core::mem::align_of::<super::UVec3>());
378 const_assert_eq!(12, core::mem::size_of::<super::UVec3>());
379 }