| 1 | use crate::core::traits::vector::*; |
| 2 | use crate::{BVec2, DVec3, IVec3, UVec3, Vec3, XY}; |
| 3 | #[cfg(not(target_arch = "spirv"))] |
| 4 | use core::fmt; |
| 5 | use core::iter::{Product, Sum}; |
| 6 | use core::{f32, ops::*}; |
| 7 | |
| 8 | #[cfg(not(feature = "std"))] |
| 9 | use num_traits::Float; |
| 10 | |
| 11 | macro_rules! impl_vec2_common_methods { |
| 12 | ($t:ty, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 13 | /// All zeroes. |
| 14 | pub const ZERO: Self = Self($inner::ZERO); |
| 15 | |
| 16 | /// All ones. |
| 17 | pub const ONE: Self = Self($inner::ONE); |
| 18 | |
| 19 | /// `[1, 0]`: a unit-length vector pointing along the positive X axis. |
| 20 | pub const X: Self = Self($inner::X); |
| 21 | |
| 22 | /// `[0, 1]`: a unit-length vector pointing along the positive Y axis. |
| 23 | pub const Y: Self = Self($inner::Y); |
| 24 | |
| 25 | /// The unit axes. |
| 26 | pub const AXES: [Self; 2] = [Self::X, Self::Y]; |
| 27 | |
| 28 | /// Creates a new vector. |
| 29 | #[inline(always)] |
| 30 | pub fn new(x: $t, y: $t) -> $vec2 { |
| 31 | Self(Vector2::new(x, y)) |
| 32 | } |
| 33 | |
| 34 | /// Creates a 3D vector from `self` and the given `z` value. |
| 35 | #[inline(always)] |
| 36 | pub fn extend(self, z: $t) -> $vec3 { |
| 37 | $vec3::new(self.x, self.y, z) |
| 38 | } |
| 39 | |
| 40 | /// `[x, y]` |
| 41 | #[inline(always)] |
| 42 | pub fn to_array(&self) -> [$t; 2] { |
| 43 | [self.x, self.y] |
| 44 | } |
| 45 | |
| 46 | impl_vecn_common_methods!($t, $vec2, $mask, $inner, Vector2); |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | macro_rules! impl_vec2_signed_methods { |
| 51 | ($t:ty, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 52 | impl_vec2_common_methods!($t, $vec2, $vec3, $mask, $inner); |
| 53 | impl_vecn_signed_methods!($t, $vec2, $mask, $inner, SignedVector2); |
| 54 | |
| 55 | /// Returns a vector that is equal to `self` rotated by 90 degrees. |
| 56 | #[inline(always)] |
| 57 | pub fn perp(self) -> Self { |
| 58 | Self(self.0.perp()) |
| 59 | } |
| 60 | |
| 61 | /// The perpendicular dot product of `self` and `other`. |
| 62 | /// Also known as the wedge product, 2d cross product, and determinant. |
| 63 | #[doc(alias = "wedge")] |
| 64 | #[doc(alias = "cross")] |
| 65 | #[doc(alias = "determinant")] |
| 66 | #[inline(always)] |
| 67 | pub fn perp_dot(self, other: $vec2) -> $t { |
| 68 | self.0.perp_dot(other.0) |
| 69 | } |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | macro_rules! impl_vec2_float_methods { |
| 74 | ($t:ty, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 75 | impl_vec2_signed_methods!($t, $vec2, $vec3, $mask, $inner); |
| 76 | impl_vecn_float_methods!($t, $vec2, $mask, $inner, FloatVector2); |
| 77 | |
| 78 | /// Returns the angle (in radians) between `self` and `other`. |
| 79 | /// |
| 80 | /// The input vectors do not need to be unit length however they must be non-zero. |
| 81 | #[inline(always)] |
| 82 | pub fn angle_between(self, other: Self) -> $t { |
| 83 | self.0.angle_between(other.0) |
| 84 | } |
| 85 | }; |
| 86 | } |
| 87 | |
| 88 | macro_rules! impl_vec2_common_traits { |
| 89 | ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 90 | /// Creates a 2-dimensional vector. |
| 91 | #[inline(always)] |
| 92 | pub fn $new(x: $t, y: $t) -> $vec2 { |
| 93 | $vec2::new(x, y) |
| 94 | } |
| 95 | |
| 96 | impl Index<usize> for $vec2 { |
| 97 | type Output = $t; |
| 98 | #[inline(always)] |
| 99 | fn index(&self, index: usize) -> &Self::Output { |
| 100 | match index { |
| 101 | 0 => &self.x, |
| 102 | 1 => &self.y, |
| 103 | _ => panic!("index out of bounds"), |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | impl IndexMut<usize> for $vec2 { |
| 109 | #[inline(always)] |
| 110 | fn index_mut(&mut self, index: usize) -> &mut Self::Output { |
| 111 | match index { |
| 112 | 0 => &mut self.x, |
| 113 | 1 => &mut self.y, |
| 114 | _ => panic!("index out of bounds"), |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | #[cfg(not(target_arch = "spirv"))] |
| 119 | impl fmt::Display for $vec2 { |
| 120 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 121 | write!(f, "[{}, {}]", self.x, self.y) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[cfg(not(target_arch = "spirv"))] |
| 126 | impl fmt::Debug for $vec2 { |
| 127 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 128 | fmt.debug_tuple(stringify!($vec2)) |
| 129 | .field(&self.x) |
| 130 | .field(&self.y) |
| 131 | .finish() |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | impl From<($t, $t)> for $vec2 { |
| 136 | #[inline(always)] |
| 137 | fn from(t: ($t, $t)) -> Self { |
| 138 | Self($inner::from_tuple(t)) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl From<$vec2> for ($t, $t) { |
| 143 | #[inline(always)] |
| 144 | fn from(v: $vec2) -> Self { |
| 145 | v.0.into_tuple() |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | impl Deref for $vec2 { |
| 150 | type Target = XY<$t>; |
| 151 | #[inline(always)] |
| 152 | fn deref(&self) -> &Self::Target { |
| 153 | self.0.as_ref_xy() |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | impl DerefMut for $vec2 { |
| 158 | #[inline(always)] |
| 159 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 160 | self.0.as_mut_xy() |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | impl_vecn_common_traits!($t, 2, $vec2, $inner, Vector2); |
| 165 | }; |
| 166 | } |
| 167 | |
| 168 | macro_rules! impl_vec2_unsigned_traits { |
| 169 | ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 170 | impl_vec2_common_traits!($t, $new, $vec2, $vec3, $mask, $inner); |
| 171 | }; |
| 172 | } |
| 173 | |
| 174 | macro_rules! impl_vec2_signed_traits { |
| 175 | ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 176 | impl_vec2_common_traits!($t, $new, $vec2, $vec3, $mask, $inner); |
| 177 | impl_vecn_signed_traits!($t, 2, $vec2, $inner, SignedVector2); |
| 178 | }; |
| 179 | } |
| 180 | |
| 181 | type XYF32 = XY<f32>; |
| 182 | |
| 183 | /// A 2-dimensional vector. |
| 184 | #[derive(Clone, Copy)] |
| 185 | #[cfg_attr(feature = "cuda", repr(C, align(8)))] |
| 186 | #[cfg_attr(not(feature = "cuda"), repr(transparent))] |
| 187 | pub struct Vec2(pub(crate) XYF32); |
| 188 | |
| 189 | impl Vec2 { |
| 190 | impl_vec2_float_methods!(f32, Vec2, Vec3, BVec2, XYF32); |
| 191 | impl_as_dvec2!(); |
| 192 | impl_as_ivec2!(); |
| 193 | impl_as_uvec2!(); |
| 194 | } |
| 195 | impl_vec2_signed_traits!(f32, vec2, Vec2, Vec3, BVec2, XYF32); |
| 196 | |
| 197 | type XYF64 = XY<f64>; |
| 198 | |
| 199 | /// A 2-dimensional vector. |
| 200 | #[derive(Clone, Copy)] |
| 201 | #[cfg_attr(feature = "cuda", repr(C, align(16)))] |
| 202 | #[cfg_attr(not(feature = "cuda"), repr(transparent))] |
| 203 | pub struct DVec2(pub(crate) XYF64); |
| 204 | |
| 205 | impl DVec2 { |
| 206 | impl_vec2_float_methods!(f64, DVec2, DVec3, BVec2, XYF64); |
| 207 | impl_as_vec2!(); |
| 208 | impl_as_ivec2!(); |
| 209 | impl_as_uvec2!(); |
| 210 | } |
| 211 | impl_vec2_signed_traits!(f64, dvec2, DVec2, DVec3, BVec2, XYF64); |
| 212 | |
| 213 | type XYI32 = XY<i32>; |
| 214 | |
| 215 | /// A 2-dimensional vector. |
| 216 | #[derive(Clone, Copy)] |
| 217 | #[cfg_attr(feature = "cuda", repr(C, align(8)))] |
| 218 | #[cfg_attr(not(feature = "cuda"), repr(transparent))] |
| 219 | pub struct IVec2(pub(crate) XYI32); |
| 220 | |
| 221 | impl IVec2 { |
| 222 | impl_vec2_signed_methods!(i32, IVec2, IVec3, BVec2, XYI32); |
| 223 | impl_as_vec2!(); |
| 224 | impl_as_dvec2!(); |
| 225 | impl_as_uvec2!(); |
| 226 | } |
| 227 | impl_vec2_signed_traits!(i32, ivec2, IVec2, IVec3, BVec2, XYI32); |
| 228 | impl_vecn_eq_hash_traits!(i32, 2, IVec2); |
| 229 | |
| 230 | impl_vecn_scalar_shift_op_traits!(IVec2, i8, XYI32); |
| 231 | impl_vecn_scalar_shift_op_traits!(IVec2, i16, XYI32); |
| 232 | impl_vecn_scalar_shift_op_traits!(IVec2, i32, XYI32); |
| 233 | impl_vecn_scalar_shift_op_traits!(IVec2, u8, XYI32); |
| 234 | impl_vecn_scalar_shift_op_traits!(IVec2, u16, XYI32); |
| 235 | impl_vecn_scalar_shift_op_traits!(IVec2, u32, XYI32); |
| 236 | |
| 237 | impl_vecn_shift_op_traits!(IVec2, IVec2, XYI32); |
| 238 | impl_vecn_shift_op_traits!(IVec2, UVec2, XYI32); |
| 239 | |
| 240 | impl_vecn_scalar_bit_op_traits!(IVec2, i32, XYI32); |
| 241 | |
| 242 | impl_vecn_bit_op_traits!(IVec2, XYI32); |
| 243 | |
| 244 | type XYU32 = XY<u32>; |
| 245 | |
| 246 | /// A 2-dimensional vector. |
| 247 | #[derive(Clone, Copy)] |
| 248 | #[cfg_attr(feature = "cuda", repr(C, align(8)))] |
| 249 | #[cfg_attr(not(feature = "cuda"), repr(transparent))] |
| 250 | pub struct UVec2(pub(crate) XYU32); |
| 251 | |
| 252 | impl UVec2 { |
| 253 | impl_vec2_common_methods!(u32, UVec2, UVec3, BVec2, XYU32); |
| 254 | impl_as_vec2!(); |
| 255 | impl_as_dvec2!(); |
| 256 | impl_as_ivec2!(); |
| 257 | } |
| 258 | impl_vec2_unsigned_traits!(u32, uvec2, UVec2, UVec3, BVec2, XYU32); |
| 259 | impl_vecn_eq_hash_traits!(u32, 2, UVec2); |
| 260 | |
| 261 | impl_vecn_scalar_shift_op_traits!(UVec2, i8, XYU32); |
| 262 | impl_vecn_scalar_shift_op_traits!(UVec2, i16, XYU32); |
| 263 | impl_vecn_scalar_shift_op_traits!(UVec2, i32, XYU32); |
| 264 | impl_vecn_scalar_shift_op_traits!(UVec2, u8, XYU32); |
| 265 | impl_vecn_scalar_shift_op_traits!(UVec2, u16, XYU32); |
| 266 | impl_vecn_scalar_shift_op_traits!(UVec2, u32, XYU32); |
| 267 | |
| 268 | impl_vecn_shift_op_traits!(UVec2, IVec2, XYU32); |
| 269 | impl_vecn_shift_op_traits!(UVec2, UVec2, XYU32); |
| 270 | |
| 271 | impl_vecn_scalar_bit_op_traits!(UVec2, u32, XYU32); |
| 272 | |
| 273 | impl_vecn_bit_op_traits!(UVec2, XYU32); |
| 274 | |
| 275 | mod const_test_vec2 { |
| 276 | #[cfg(not(feature = "cuda"))] |
| 277 | const_assert_eq!( |
| 278 | core::mem::align_of::<f32>(), |
| 279 | core::mem::align_of::<super::Vec2>() |
| 280 | ); |
| 281 | #[cfg(feature = "cuda")] |
| 282 | const_assert_eq!(8, core::mem::align_of::<super::Vec2>()); |
| 283 | const_assert_eq!(8, core::mem::size_of::<super::Vec2>()); |
| 284 | } |
| 285 | |
| 286 | mod const_test_dvec2 { |
| 287 | #[cfg(not(feature = "cuda"))] |
| 288 | const_assert_eq!( |
| 289 | core::mem::align_of::<f64>(), |
| 290 | core::mem::align_of::<super::DVec2>() |
| 291 | ); |
| 292 | #[cfg(feature = "cuda")] |
| 293 | const_assert_eq!(16, core::mem::align_of::<super::DVec2>()); |
| 294 | const_assert_eq!(16, core::mem::size_of::<super::DVec2>()); |
| 295 | } |
| 296 | |
| 297 | mod const_test_ivec2 { |
| 298 | #[cfg(not(feature = "cuda"))] |
| 299 | const_assert_eq!( |
| 300 | core::mem::align_of::<i32>(), |
| 301 | core::mem::align_of::<super::IVec2>() |
| 302 | ); |
| 303 | #[cfg(feature = "cuda")] |
| 304 | const_assert_eq!(8, core::mem::align_of::<super::IVec2>()); |
| 305 | const_assert_eq!(8, core::mem::size_of::<super::IVec2>()); |
| 306 | } |
| 307 | |
| 308 | mod const_test_uvec2 { |
| 309 | #[cfg(not(feature = "cuda"))] |
| 310 | const_assert_eq!( |
| 311 | core::mem::align_of::<u32>(), |
| 312 | core::mem::align_of::<super::UVec2>() |
| 313 | ); |
| 314 | #[cfg(feature = "cuda")] |
| 315 | const_assert_eq!(8, core::mem::align_of::<super::UVec2>()); |
| 316 | const_assert_eq!(8, core::mem::size_of::<super::UVec2>()); |
| 317 | } |