Debian Codegraph @

debian/missing-sources/glam-0.19.0/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::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 #[repr(transparent)]
186 pub struct Vec2(pub(crate) XYF32);
187
188 impl Vec2 {
189 impl_vec2_float_methods!(f32, Vec2, Vec3, BVec2, XYF32);
190 impl_as_dvec2!();
191 impl_as_ivec2!();
192 impl_as_uvec2!();
193 }
194 impl_vec2_signed_traits!(f32, vec2, Vec2, Vec3, BVec2, XYF32);
195
196 type XYF64 = XY<f64>;
197
198 /// A 2-dimensional vector.
199 #[derive(Clone, Copy)]
200 #[repr(transparent)]
201 pub struct DVec2(pub(crate) XYF64);
202
203 impl DVec2 {
204 impl_vec2_float_methods!(f64, DVec2, DVec3, BVec2, XYF64);
205 impl_as_vec2!();
206 impl_as_ivec2!();
207 impl_as_uvec2!();
208 }
209 impl_vec2_signed_traits!(f64, dvec2, DVec2, DVec3, BVec2, XYF64);
210
211 type XYI32 = XY<i32>;
212
213 /// A 2-dimensional vector.
214 #[derive(Clone, Copy)]
215 #[repr(transparent)]
216 pub struct IVec2(pub(crate) XYI32);
217
218 impl IVec2 {
219 impl_vec2_signed_methods!(i32, IVec2, IVec3, BVec2, XYI32);
220 impl_as_vec2!();
221 impl_as_dvec2!();
222 impl_as_uvec2!();
223 }
224 impl_vec2_signed_traits!(i32, ivec2, IVec2, IVec3, BVec2, XYI32);
225 impl_vecn_eq_hash_traits!(i32, 2, IVec2);
226
227 impl_vecn_scalar_shift_op_traits!(IVec2, i8, XYI32);
228 impl_vecn_scalar_shift_op_traits!(IVec2, i16, XYI32);
229 impl_vecn_scalar_shift_op_traits!(IVec2, i32, XYI32);
230 impl_vecn_scalar_shift_op_traits!(IVec2, u8, XYI32);
231 impl_vecn_scalar_shift_op_traits!(IVec2, u16, XYI32);
232 impl_vecn_scalar_shift_op_traits!(IVec2, u32, XYI32);
233
234 impl_vecn_shift_op_traits!(IVec2, IVec2, XYI32);
235 impl_vecn_shift_op_traits!(IVec2, UVec2, XYI32);
236
237 impl_vecn_scalar_bit_op_traits!(IVec2, i32, XYI32);
238
239 impl_vecn_bit_op_traits!(IVec2, XYI32);
240
241 type XYU32 = XY<u32>;
242
243 /// A 2-dimensional vector.
244 #[derive(Clone, Copy)]
245 #[repr(transparent)]
246 pub struct UVec2(pub(crate) XYU32);
247
248 impl UVec2 {
249 impl_vec2_common_methods!(u32, UVec2, UVec3, BVec2, XYU32);
250 impl_as_vec2!();
251 impl_as_dvec2!();
252 impl_as_ivec2!();
253 }
254 impl_vec2_unsigned_traits!(u32, uvec2, UVec2, UVec3, BVec2, XYU32);
255 impl_vecn_eq_hash_traits!(u32, 2, UVec2);
256
257 impl_vecn_scalar_shift_op_traits!(UVec2, i8, XYU32);
258 impl_vecn_scalar_shift_op_traits!(UVec2, i16, XYU32);
259 impl_vecn_scalar_shift_op_traits!(UVec2, i32, XYU32);
260 impl_vecn_scalar_shift_op_traits!(UVec2, u8, XYU32);
261 impl_vecn_scalar_shift_op_traits!(UVec2, u16, XYU32);
262 impl_vecn_scalar_shift_op_traits!(UVec2, u32, XYU32);
263
264 impl_vecn_shift_op_traits!(UVec2, IVec2, XYU32);
265 impl_vecn_shift_op_traits!(UVec2, UVec2, XYU32);
266
267 impl_vecn_scalar_bit_op_traits!(UVec2, u32, XYU32);
268
269 impl_vecn_bit_op_traits!(UVec2, XYU32);
270
271 mod const_test_vec2 {
272 const_assert_eq!(
273 core::mem::align_of::<f32>(),
274 core::mem::align_of::<super::Vec2>()
275 );
276 const_assert_eq!(8, core::mem::size_of::<super::Vec2>());
277 }
278
279 mod const_test_dvec2 {
280 const_assert_eq!(
281 core::mem::align_of::<f64>(),
282 core::mem::align_of::<super::DVec2>()
283 );
284 const_assert_eq!(16, core::mem::size_of::<super::DVec2>());
285 }
286
287 mod const_test_ivec2 {
288 const_assert_eq!(
289 core::mem::align_of::<i32>(),
290 core::mem::align_of::<super::IVec2>()
291 );
292 const_assert_eq!(8, core::mem::size_of::<super::IVec2>());
293 }
294
295 mod const_test_uvec2 {
296 const_assert_eq!(
297 core::mem::align_of::<u32>(),
298 core::mem::align_of::<super::UVec2>()
299 );
300 const_assert_eq!(8, core::mem::size_of::<super::UVec2>());
301 }