| 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 | #[inline(always)] |
| 66 | #[cfg_attr( |
| 67 | docsrs, |
| 68 | doc(alias = "wedge"), |
| 69 | doc(alias = "cross"), |
| 70 | doc(alias = "determinant") |
| 71 | )] |
| 72 | pub fn perp_dot(self, other: $vec2) -> $t { |
| 73 | self.0.perp_dot(other.0) |
| 74 | } |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | macro_rules! impl_vec2_float_methods { |
| 79 | ($t:ty, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 80 | impl_vec2_signed_methods!($t, $vec2, $vec3, $mask, $inner); |
| 81 | impl_vecn_float_methods!($t, $vec2, $mask, $inner, FloatVector2); |
| 82 | |
| 83 | /// Returns the angle (in radians) between `self` and `other`. |
| 84 | /// |
| 85 | /// The input vectors do not need to be unit length however they must be non-zero. |
| 86 | #[inline(always)] |
| 87 | pub fn angle_between(self, other: Self) -> $t { |
| 88 | self.0.angle_between(other.0) |
| 89 | } |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | macro_rules! impl_vec2_common_traits { |
| 94 | ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 95 | /// Creates a 2-dimensional vector. |
| 96 | #[inline(always)] |
| 97 | pub fn $new(x: $t, y: $t) -> $vec2 { |
| 98 | $vec2::new(x, y) |
| 99 | } |
| 100 | |
| 101 | #[cfg(not(target_arch = "spirv"))] |
| 102 | impl fmt::Display for $vec2 { |
| 103 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 104 | write!(f, "[{}, {}]", self.x, self.y) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #[cfg(not(target_arch = "spirv"))] |
| 109 | impl fmt::Debug for $vec2 { |
| 110 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 111 | fmt.debug_tuple(stringify!($vec2)) |
| 112 | .field(&self.x) |
| 113 | .field(&self.y) |
| 114 | .finish() |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | impl From<($t, $t)> for $vec2 { |
| 119 | #[inline(always)] |
| 120 | fn from(t: ($t, $t)) -> Self { |
| 121 | Self($inner::from_tuple(t)) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | impl From<$vec2> for ($t, $t) { |
| 126 | #[inline(always)] |
| 127 | fn from(v: $vec2) -> Self { |
| 128 | v.0.into_tuple() |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | impl Deref for $vec2 { |
| 133 | type Target = XY<$t>; |
| 134 | #[inline(always)] |
| 135 | fn deref(&self) -> &Self::Target { |
| 136 | self.0.as_ref_xy() |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | impl DerefMut for $vec2 { |
| 141 | #[inline(always)] |
| 142 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 143 | self.0.as_mut_xy() |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | impl_vecn_common_traits!($t, 2, $vec2, $inner, Vector2); |
| 148 | }; |
| 149 | } |
| 150 | |
| 151 | macro_rules! impl_vec2_unsigned_traits { |
| 152 | ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 153 | impl_vec2_common_traits!($t, $new, $vec2, $vec3, $mask, $inner); |
| 154 | }; |
| 155 | } |
| 156 | |
| 157 | macro_rules! impl_vec2_signed_traits { |
| 158 | ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => { |
| 159 | impl_vec2_common_traits!($t, $new, $vec2, $vec3, $mask, $inner); |
| 160 | impl_vecn_signed_traits!($t, 2, $vec2, $inner, SignedVector2); |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | type XYF32 = XY<f32>; |
| 165 | |
| 166 | /// A 2-dimensional vector. |
| 167 | #[derive(Clone, Copy)] |
| 168 | #[repr(transparent)] |
| 169 | pub struct Vec2(pub(crate) XYF32); |
| 170 | |
| 171 | impl Vec2 { |
| 172 | impl_vec2_float_methods!(f32, Vec2, Vec3, BVec2, XYF32); |
| 173 | impl_vecn_as_f64!(DVec2, x, y); |
| 174 | impl_vecn_as_i32!(IVec2, x, y); |
| 175 | impl_vecn_as_u32!(UVec2, x, y); |
| 176 | } |
| 177 | impl_vec2_signed_traits!(f32, vec2, Vec2, Vec3, BVec2, XYF32); |
| 178 | |
| 179 | type XYF64 = XY<f64>; |
| 180 | |
| 181 | /// A 2-dimensional vector. |
| 182 | #[derive(Clone, Copy)] |
| 183 | #[repr(transparent)] |
| 184 | pub struct DVec2(pub(crate) XYF64); |
| 185 | |
| 186 | impl DVec2 { |
| 187 | impl_vec2_float_methods!(f64, DVec2, DVec3, BVec2, XYF64); |
| 188 | impl_vecn_as_f32!(Vec2, x, y); |
| 189 | impl_vecn_as_i32!(IVec2, x, y); |
| 190 | impl_vecn_as_u32!(UVec2, x, y); |
| 191 | } |
| 192 | impl_vec2_signed_traits!(f64, dvec2, DVec2, DVec3, BVec2, XYF64); |
| 193 | |
| 194 | type XYI32 = XY<i32>; |
| 195 | |
| 196 | /// A 2-dimensional vector. |
| 197 | #[derive(Clone, Copy)] |
| 198 | #[repr(transparent)] |
| 199 | pub struct IVec2(pub(crate) XYI32); |
| 200 | |
| 201 | impl IVec2 { |
| 202 | impl_vec2_signed_methods!(i32, IVec2, IVec3, BVec2, XYI32); |
| 203 | impl_vecn_as_f32!(Vec2, x, y); |
| 204 | impl_vecn_as_f64!(DVec2, x, y); |
| 205 | impl_vecn_as_u32!(UVec2, x, y); |
| 206 | } |
| 207 | impl_vec2_signed_traits!(i32, ivec2, IVec2, IVec3, BVec2, XYI32); |
| 208 | impl_vecn_eq_hash_traits!(i32, 2, IVec2); |
| 209 | |
| 210 | type XYU32 = XY<u32>; |
| 211 | |
| 212 | /// A 2-dimensional vector. |
| 213 | #[derive(Clone, Copy)] |
| 214 | #[repr(transparent)] |
| 215 | pub struct UVec2(pub(crate) XYU32); |
| 216 | |
| 217 | impl UVec2 { |
| 218 | impl_vec2_common_methods!(u32, UVec2, UVec3, BVec2, XYU32); |
| 219 | impl_vecn_as_f32!(Vec2, x, y); |
| 220 | impl_vecn_as_f64!(DVec2, x, y); |
| 221 | impl_vecn_as_i32!(IVec2, x, y); |
| 222 | } |
| 223 | impl_vec2_unsigned_traits!(u32, uvec2, UVec2, UVec3, BVec2, XYU32); |
| 224 | impl_vecn_eq_hash_traits!(u32, 2, UVec2); |
| 225 | |
| 226 | mod const_test_vec2 { |
| 227 | const_assert_eq!(4, core::mem::align_of::<super::Vec2>()); |
| 228 | const_assert_eq!(8, core::mem::size_of::<super::Vec2>()); |
| 229 | } |
| 230 | |
| 231 | mod const_test_dvec2 { |
| 232 | const_assert_eq!(8, core::mem::align_of::<super::DVec2>()); |
| 233 | const_assert_eq!(16, core::mem::size_of::<super::DVec2>()); |
| 234 | } |
| 235 | |
| 236 | mod const_test_ivec2 { |
| 237 | const_assert_eq!(4, core::mem::align_of::<super::IVec2>()); |
| 238 | const_assert_eq!(8, core::mem::size_of::<super::IVec2>()); |
| 239 | } |
| 240 | |
| 241 | mod const_test_uvec2 { |
| 242 | const_assert_eq!(4, core::mem::align_of::<super::UVec2>()); |
| 243 | const_assert_eq!(8, core::mem::size_of::<super::UVec2>()); |
| 244 | } |