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