Debian Codegraph @

debian/missing-sources/glam-0.17.3/src/vec2.rs

raw symbols SCIP
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 impl Index<usize> for $vec2 {
102 type Output = $t;
103 #[inline(always)]
104 fn index(&self, index: usize) -> &Self::Output {
105 match index {
106 0 => &self.x,
107 1 => &self.y,
108 _ => panic!("index out of bounds"),
109 }
110 }
111 }
112
113 impl IndexMut<usize> for $vec2 {
114 #[inline(always)]
115 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
116 match index {
117 0 => &mut self.x,
118 1 => &mut self.y,
119 _ => panic!("index out of bounds"),
120 }
121 }
122 }
123 #[cfg(not(target_arch = "spirv"))]
124 impl fmt::Display for $vec2 {
125 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126 write!(f, "[{}, {}]", self.x, self.y)
127 }
128 }
129
130 #[cfg(not(target_arch = "spirv"))]
131 impl fmt::Debug for $vec2 {
132 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
133 fmt.debug_tuple(stringify!($vec2))
134 .field(&self.x)
135 .field(&self.y)
136 .finish()
137 }
138 }
139
140 impl From<($t, $t)> for $vec2 {
141 #[inline(always)]
142 fn from(t: ($t, $t)) -> Self {
143 Self($inner::from_tuple(t))
144 }
145 }
146
147 impl From<$vec2> for ($t, $t) {
148 #[inline(always)]
149 fn from(v: $vec2) -> Self {
150 v.0.into_tuple()
151 }
152 }
153
154 impl Deref for $vec2 {
155 type Target = XY<$t>;
156 #[inline(always)]
157 fn deref(&self) -> &Self::Target {
158 self.0.as_ref_xy()
159 }
160 }
161
162 impl DerefMut for $vec2 {
163 #[inline(always)]
164 fn deref_mut(&mut self) -> &mut Self::Target {
165 self.0.as_mut_xy()
166 }
167 }
168
169 impl_vecn_common_traits!($t, 2, $vec2, $inner, Vector2);
170 };
171 }
172
173 macro_rules! impl_vec2_unsigned_traits {
174 ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => {
175 impl_vec2_common_traits!($t, $new, $vec2, $vec3, $mask, $inner);
176 };
177 }
178
179 macro_rules! impl_vec2_signed_traits {
180 ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $mask:ident, $inner:ident) => {
181 impl_vec2_common_traits!($t, $new, $vec2, $vec3, $mask, $inner);
182 impl_vecn_signed_traits!($t, 2, $vec2, $inner, SignedVector2);
183 };
184 }
185
186 type XYF32 = XY<f32>;
187
188 /// A 2-dimensional vector.
189 #[derive(Clone, Copy)]
190 #[repr(transparent)]
191 pub struct Vec2(pub(crate) XYF32);
192
193 impl Vec2 {
194 impl_vec2_float_methods!(f32, Vec2, Vec3, BVec2, XYF32);
195 impl_vecn_as_f64!(DVec2, x, y);
196 impl_vecn_as_i32!(IVec2, x, y);
197 impl_vecn_as_u32!(UVec2, x, y);
198 }
199 impl_vec2_signed_traits!(f32, vec2, Vec2, Vec3, BVec2, XYF32);
200
201 type XYF64 = XY<f64>;
202
203 /// A 2-dimensional vector.
204 #[derive(Clone, Copy)]
205 #[repr(transparent)]
206 pub struct DVec2(pub(crate) XYF64);
207
208 impl DVec2 {
209 impl_vec2_float_methods!(f64, DVec2, DVec3, BVec2, XYF64);
210 impl_vecn_as_f32!(Vec2, x, y);
211 impl_vecn_as_i32!(IVec2, x, y);
212 impl_vecn_as_u32!(UVec2, x, y);
213 }
214 impl_vec2_signed_traits!(f64, dvec2, DVec2, DVec3, BVec2, XYF64);
215
216 type XYI32 = XY<i32>;
217
218 /// A 2-dimensional vector.
219 #[derive(Clone, Copy)]
220 #[repr(transparent)]
221 pub struct IVec2(pub(crate) XYI32);
222
223 impl IVec2 {
224 impl_vec2_signed_methods!(i32, IVec2, IVec3, BVec2, XYI32);
225 impl_vecn_as_f32!(Vec2, x, y);
226 impl_vecn_as_f64!(DVec2, x, y);
227 impl_vecn_as_u32!(UVec2, x, y);
228 }
229 impl_vec2_signed_traits!(i32, ivec2, IVec2, IVec3, BVec2, XYI32);
230 impl_vecn_eq_hash_traits!(i32, 2, IVec2);
231
232 type XYU32 = XY<u32>;
233
234 /// A 2-dimensional vector.
235 #[derive(Clone, Copy)]
236 #[repr(transparent)]
237 pub struct UVec2(pub(crate) XYU32);
238
239 impl UVec2 {
240 impl_vec2_common_methods!(u32, UVec2, UVec3, BVec2, XYU32);
241 impl_vecn_as_f32!(Vec2, x, y);
242 impl_vecn_as_f64!(DVec2, x, y);
243 impl_vecn_as_i32!(IVec2, x, y);
244 }
245 impl_vec2_unsigned_traits!(u32, uvec2, UVec2, UVec3, BVec2, XYU32);
246 impl_vecn_eq_hash_traits!(u32, 2, UVec2);
247
248 mod const_test_vec2 {
249 const_assert_eq!(
250 core::mem::align_of::<f32>(),
251 core::mem::align_of::<super::Vec2>()
252 );
253 const_assert_eq!(8, core::mem::size_of::<super::Vec2>());
254 }
255
256 mod const_test_dvec2 {
257 const_assert_eq!(
258 core::mem::align_of::<f64>(),
259 core::mem::align_of::<super::DVec2>()
260 );
261 const_assert_eq!(16, core::mem::size_of::<super::DVec2>());
262 }
263
264 mod const_test_ivec2 {
265 const_assert_eq!(
266 core::mem::align_of::<i32>(),
267 core::mem::align_of::<super::IVec2>()
268 );
269 const_assert_eq!(8, core::mem::size_of::<super::IVec2>());
270 }
271
272 mod const_test_uvec2 {
273 const_assert_eq!(
274 core::mem::align_of::<u32>(),
275 core::mem::align_of::<super::UVec2>()
276 );
277 const_assert_eq!(8, core::mem::size_of::<super::UVec2>());
278 }