| 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 | pub fn any_orthogonal_vector(&self) -> Self { |
| 215 | // This can probably be optimized |
| 216 | if self.x.abs() > self.y.abs() { |
| 217 | Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y) |
| 218 | } else { |
| 219 | Self::new(0.0, self.z, -self.y) // self.cross(Self::X) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /// Returns any unit-length vector that is orthogonal to the given one. |
| 224 | /// The input vector must be finite and non-zero. |
| 225 | /// |
| 226 | /// # Panics |
| 227 | /// |
| 228 | /// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 229 | #[inline] |
| 230 | pub fn any_orthonormal_vector(&self) -> Self { |
| 231 | glam_assert!(self.is_normalized()); |
| 232 | // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf |
| 233 | #[cfg(feature = "std")] |
| 234 | let sign = (1.0 as $t).copysign(self.z); |
| 235 | #[cfg(not(feature = "std"))] |
| 236 | let sign = self.z.signum(); |
| 237 | let a = -1.0 / (sign + self.z); |
| 238 | let b = self.x * self.y * a; |
| 239 | Self::new(b, sign + self.y * self.y * a, -self.y) |
| 240 | } |
| 241 | |
| 242 | /// Given a unit-length vector return two other vectors that together form an orthonormal |
| 243 | /// basis. That is, all three vectors are orthogonal to each other and are normalized. |
| 244 | /// |
| 245 | /// # Panics |
| 246 | /// |
| 247 | /// Will panic if `self` is not normalized when `glam_assert` is enabled. |
| 248 | #[inline] |
| 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 | #[cfg(feature = "std")] |
| 253 | let sign = (1.0 as $t).copysign(self.z); |
| 254 | #[cfg(not(feature = "std"))] |
| 255 | let sign = self.z.signum(); |
| 256 | let a = -1.0 / (sign + self.z); |
| 257 | let b = self.x * self.y * a; |
| 258 | ( |
| 259 | Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x), |
| 260 | Self::new(b, sign + self.y * self.y * a, -self.y), |
| 261 | ) |
| 262 | } |
| 263 | }; |
| 264 | } |
| 265 | |
| 266 | // implements traits that are common between `Vec3`, `Vec3A` and `Vec4` types. |
| 267 | macro_rules! impl_vec3_float_traits { |
| 268 | ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $inner:ident) => { |
| 269 | impl_vec3_common_traits!($t, $new, $vec2, $vec3, $vec4, $inner); |
| 270 | impl_vecn_signed_traits!($t, 3, $vec3, $inner, SignedVector3); |
| 271 | }; |
| 272 | } |
| 273 | |
| 274 | // implements f32 functionality common between `Vec3` and `Vec3A` types. |
| 275 | macro_rules! impl_f32_vec3 { |
| 276 | ($new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => { |
| 277 | impl $vec3 { |
| 278 | impl_vec3_float_methods!(f32, $vec2, $vec3, $vec4, $mask, $inner); |
| 279 | impl_as_dvec3!(); |
| 280 | impl_as_ivec3!(); |
| 281 | impl_as_uvec3!(); |
| 282 | } |
| 283 | impl_vec3_float_traits!(f32, $new, $vec2, $vec3, $vec4, $inner); |
| 284 | }; |
| 285 | } |
| 286 | |
| 287 | type XYZF32 = XYZ<f32>; |
| 288 | |
| 289 | /// A 3-dimensional vector without SIMD support. |
| 290 | #[derive(Clone, Copy)] |
| 291 | #[repr(transparent)] |
| 292 | pub struct Vec3(pub(crate) XYZF32); |
| 293 | impl_f32_vec3!(vec3, Vec2, Vec3, Vec4, BVec3, XYZF32); |
| 294 | |
| 295 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 296 | type XYZF32A = __m128; |
| 297 | #[cfg(all(target_feature = "simd128", not(feature = "scalar-math")))] |
| 298 | type XYZF32A = v128; |
| 299 | |
| 300 | #[cfg(any( |
| 301 | not(any(target_feature = "sse2", target_feature = "simd128")), |
| 302 | feature = "scalar-math" |
| 303 | ))] |
| 304 | type XYZF32A = crate::core::storage::XYZF32A16; |
| 305 | |
| 306 | /// A 3-dimensional vector with SIMD support. |
| 307 | /// |
| 308 | /// This type is 16 byte aligned. A SIMD vector type is used for storage on supported platforms for |
| 309 | /// better performance than the `Vec3` type. |
| 310 | /// |
| 311 | /// It is possible to convert between `Vec3` and `Vec3A` types using `From` trait implementations. |
| 312 | #[derive(Clone, Copy)] |
| 313 | #[repr(transparent)] |
| 314 | pub struct Vec3A(pub(crate) XYZF32A); |
| 315 | |
| 316 | #[cfg(all( |
| 317 | any(target_feature = "sse2", target_feature = "simd128"), |
| 318 | not(feature = "scalar-math") |
| 319 | ))] |
| 320 | impl_f32_vec3!(vec3a, Vec2, Vec3A, Vec4, BVec3A, XYZF32A); |
| 321 | |
| 322 | #[cfg(any( |
| 323 | not(any(target_feature = "sse2", target_feature = "simd128")), |
| 324 | feature = "scalar-math" |
| 325 | ))] |
| 326 | impl_f32_vec3!(vec3a, Vec2, Vec3A, Vec4, BVec3, XYZF32A); |
| 327 | |
| 328 | impl From<Vec3> for Vec3A { |
| 329 | #[inline(always)] |
| 330 | fn from(v: Vec3) -> Self { |
| 331 | Self(v.0.into()) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | impl From<Vec3A> for Vec3 { |
| 336 | #[inline(always)] |
| 337 | fn from(v: Vec3A) -> Self { |
| 338 | Self(v.0.into()) |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | type XYZF64 = XYZ<f64>; |
| 343 | |
| 344 | /// A 3-dimensional vector. |
| 345 | #[derive(Clone, Copy)] |
| 346 | #[repr(transparent)] |
| 347 | pub struct DVec3(pub(crate) XYZF64); |
| 348 | |
| 349 | impl DVec3 { |
| 350 | impl_vec3_float_methods!(f64, DVec2, DVec3, DVec4, BVec3, XYZF64); |
| 351 | impl_as_vec3!(); |
| 352 | impl_as_ivec3!(); |
| 353 | impl_as_uvec3!(); |
| 354 | } |
| 355 | impl_vec3_float_traits!(f64, dvec3, DVec2, DVec3, DVec4, XYZF64); |
| 356 | |
| 357 | type XYZI32 = XYZ<i32>; |
| 358 | |
| 359 | /// A 3-dimensional vector. |
| 360 | #[derive(Clone, Copy)] |
| 361 | #[repr(transparent)] |
| 362 | pub struct IVec3(pub(crate) XYZI32); |
| 363 | |
| 364 | impl IVec3 { |
| 365 | impl_vec3_common_methods!(i32, IVec2, IVec3, IVec4, BVec3, XYZI32); |
| 366 | impl_vecn_signed_methods!(i32, IVec3, BVec3, XYZI32, SignedVector3); |
| 367 | impl_as_vec3!(); |
| 368 | impl_as_dvec3!(); |
| 369 | impl_as_uvec3!(); |
| 370 | } |
| 371 | impl_vec3_common_traits!(i32, ivec3, IVec2, IVec3, IVec4, XYZI32); |
| 372 | impl_vecn_signed_traits!(i32, 3, IVec3, XYZI32, SignedVector3); |
| 373 | impl_vecn_eq_hash_traits!(i32, 3, IVec3); |
| 374 | |
| 375 | impl_vecn_scalar_shift_op_traits!(IVec3, i8, XYZI32); |
| 376 | impl_vecn_scalar_shift_op_traits!(IVec3, i16, XYZI32); |
| 377 | impl_vecn_scalar_shift_op_traits!(IVec3, i32, XYZI32); |
| 378 | impl_vecn_scalar_shift_op_traits!(IVec3, u8, XYZI32); |
| 379 | impl_vecn_scalar_shift_op_traits!(IVec3, u16, XYZI32); |
| 380 | impl_vecn_scalar_shift_op_traits!(IVec3, u32, XYZI32); |
| 381 | |
| 382 | impl_vecn_shift_op_traits!(IVec3, IVec3, XYZI32); |
| 383 | impl_vecn_shift_op_traits!(IVec3, UVec3, XYZI32); |
| 384 | |
| 385 | impl_vecn_scalar_bit_op_traits!(IVec3, i32, XYZI32); |
| 386 | |
| 387 | impl_vecn_bit_op_traits!(IVec3, XYZI32); |
| 388 | |
| 389 | type XYZU32 = XYZ<u32>; |
| 390 | |
| 391 | /// A 3-dimensional vector. |
| 392 | #[derive(Clone, Copy)] |
| 393 | #[repr(transparent)] |
| 394 | pub struct UVec3(pub(crate) XYZU32); |
| 395 | |
| 396 | impl UVec3 { |
| 397 | impl_vec3_common_methods!(u32, UVec2, UVec3, UVec4, BVec3, XYZU32); |
| 398 | impl_as_vec3!(); |
| 399 | impl_as_dvec3!(); |
| 400 | impl_as_ivec3!(); |
| 401 | } |
| 402 | impl_vec3_common_traits!(u32, uvec3, UVec2, UVec3, UVec4, XYZU32); |
| 403 | impl_vecn_eq_hash_traits!(u32, 3, UVec3); |
| 404 | |
| 405 | impl_vecn_scalar_shift_op_traits!(UVec3, i8, XYZU32); |
| 406 | impl_vecn_scalar_shift_op_traits!(UVec3, i16, XYZU32); |
| 407 | impl_vecn_scalar_shift_op_traits!(UVec3, i32, XYZU32); |
| 408 | impl_vecn_scalar_shift_op_traits!(UVec3, u8, XYZU32); |
| 409 | impl_vecn_scalar_shift_op_traits!(UVec3, u16, XYZU32); |
| 410 | impl_vecn_scalar_shift_op_traits!(UVec3, u32, XYZU32); |
| 411 | |
| 412 | impl_vecn_shift_op_traits!(UVec3, IVec3, XYZU32); |
| 413 | impl_vecn_shift_op_traits!(UVec3, UVec3, XYZU32); |
| 414 | |
| 415 | impl_vecn_scalar_bit_op_traits!(UVec3, u32, XYZU32); |
| 416 | |
| 417 | impl_vecn_bit_op_traits!(UVec3, XYZU32); |
| 418 | |
| 419 | mod const_test_vec3 { |
| 420 | const_assert_eq!( |
| 421 | core::mem::align_of::<f32>(), |
| 422 | core::mem::align_of::<super::Vec3>() |
| 423 | ); |
| 424 | const_assert_eq!(12, core::mem::size_of::<super::Vec3>()); |
| 425 | } |
| 426 | |
| 427 | mod const_test_vec3a { |
| 428 | const_assert_eq!(16, core::mem::align_of::<super::Vec3A>()); |
| 429 | const_assert_eq!(16, core::mem::size_of::<super::Vec3A>()); |
| 430 | } |
| 431 | |
| 432 | mod const_test_dvec3 { |
| 433 | const_assert_eq!( |
| 434 | core::mem::align_of::<f64>(), |
| 435 | core::mem::align_of::<super::DVec3>() |
| 436 | ); |
| 437 | const_assert_eq!(24, core::mem::size_of::<super::DVec3>()); |
| 438 | } |
| 439 | |
| 440 | mod const_test_ivec3 { |
| 441 | const_assert_eq!( |
| 442 | core::mem::align_of::<i32>(), |
| 443 | core::mem::align_of::<super::IVec3>() |
| 444 | ); |
| 445 | const_assert_eq!(12, core::mem::size_of::<super::IVec3>()); |
| 446 | } |
| 447 | |
| 448 | mod const_test_uvec3 { |
| 449 | const_assert_eq!( |
| 450 | core::mem::align_of::<u32>(), |
| 451 | core::mem::align_of::<super::UVec3>() |
| 452 | ); |
| 453 | const_assert_eq!(12, core::mem::size_of::<super::UVec3>()); |
| 454 | } |