Debian Codegraph @

debian/missing-sources/glam-0.23.0/src/f32/affine2.rs

raw symbols SCIP
1 // Generated from affine.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{Mat2, Mat3, Mat3A, Vec2, Vec3A};
4 use core::ops::{Deref, DerefMut, Mul};
5
6 /// A 2D affine transform, which can represent translation, rotation, scaling and shear.
7 #[derive(Copy, Clone)]
8 #[repr(C)]
9 pub struct Affine2 {
10 pub matrix2: Mat2,
11 pub translation: Vec2,
12 }
13
14 impl Affine2 {
15 /// The degenerate zero transform.
16 ///
17 /// This transforms any finite vector and point to zero.
18 /// The zero transform is non-invertible.
19 pub const ZERO: Self = Self {
20 matrix2: Mat2::ZERO,
21 translation: Vec2::ZERO,
22 };
23
24 /// The identity transform.
25 ///
26 /// Multiplying a vector with this returns the same vector.
27 pub const IDENTITY: Self = Self {
28 matrix2: Mat2::IDENTITY,
29 translation: Vec2::ZERO,
30 };
31
32 /// All NAN:s.
33 pub const NAN: Self = Self {
34 matrix2: Mat2::NAN,
35 translation: Vec2::NAN,
36 };
37
38 /// Creates an affine transform from three column vectors.
39 #[inline(always)]
40 pub const fn from_cols(x_axis: Vec2, y_axis: Vec2, z_axis: Vec2) -> Self {
41 Self {
42 matrix2: Mat2::from_cols(x_axis, y_axis),
43 translation: z_axis,
44 }
45 }
46
47 /// Creates an affine transform from a `[f32; 6]` array stored in column major order.
48 #[inline]
49 pub fn from_cols_array(m: &[f32; 6]) -> Self {
50 Self {
51 matrix2: Mat2::from_cols_slice(&m[0..4]),
52 translation: Vec2::from_slice(&m[4..6]),
53 }
54 }
55
56 /// Creates a `[f32; 6]` array storing data in column major order.
57 #[inline]
58 pub fn to_cols_array(&self) -> [f32; 6] {
59 let x = &self.matrix2.x_axis;
60 let y = &self.matrix2.y_axis;
61 let z = &self.translation;
62 [x.x, x.y, y.x, y.y, z.x, z.y]
63 }
64
65 /// Creates an affine transform from a `[[f32; 2]; 3]`
66 /// 2D array stored in column major order.
67 /// If your data is in row major order you will need to `transpose` the returned
68 /// matrix.
69 #[inline]
70 pub fn from_cols_array_2d(m: &[[f32; 2]; 3]) -> Self {
71 Self {
72 matrix2: Mat2::from_cols(m[0].into(), m[1].into()),
73 translation: m[2].into(),
74 }
75 }
76
77 /// Creates a `[[f32; 2]; 3]` 2D array storing data in
78 /// column major order.
79 /// If you require data in row major order `transpose` the matrix first.
80 #[inline]
81 pub fn to_cols_array_2d(&self) -> [[f32; 2]; 3] {
82 [
83 self.matrix2.x_axis.into(),
84 self.matrix2.y_axis.into(),
85 self.translation.into(),
86 ]
87 }
88
89 /// Creates an affine transform from the first 6 values in `slice`.
90 ///
91 /// # Panics
92 ///
93 /// Panics if `slice` is less than 6 elements long.
94 #[inline]
95 pub fn from_cols_slice(slice: &[f32]) -> Self {
96 Self {
97 matrix2: Mat2::from_cols_slice(&slice[0..4]),
98 translation: Vec2::from_slice(&slice[4..6]),
99 }
100 }
101
102 /// Writes the columns of `self` to the first 6 elements in `slice`.
103 ///
104 /// # Panics
105 ///
106 /// Panics if `slice` is less than 6 elements long.
107 #[inline]
108 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
109 self.matrix2.write_cols_to_slice(&mut slice[0..4]);
110 self.translation.write_to_slice(&mut slice[4..6]);
111 }
112
113 /// Creates an affine transform that changes scale.
114 /// Note that if any scale is zero the transform will be non-invertible.
115 #[inline]
116 pub fn from_scale(scale: Vec2) -> Self {
117 Self {
118 matrix2: Mat2::from_diagonal(scale),
119 translation: Vec2::ZERO,
120 }
121 }
122
123 /// Creates an affine transform from the given rotation `angle`.
124 #[inline]
125 pub fn from_angle(angle: f32) -> Self {
126 Self {
127 matrix2: Mat2::from_angle(angle),
128 translation: Vec2::ZERO,
129 }
130 }
131
132 /// Creates an affine transformation from the given 2D `translation`.
133 #[inline]
134 pub fn from_translation(translation: Vec2) -> Self {
135 Self {
136 matrix2: Mat2::IDENTITY,
137 translation,
138 }
139 }
140
141 /// Creates an affine transform from a 2x2 matrix (expressing scale, shear and rotation)
142 #[inline]
143 pub fn from_mat2(matrix2: Mat2) -> Self {
144 Self {
145 matrix2,
146 translation: Vec2::ZERO,
147 }
148 }
149
150 /// Creates an affine transform from a 2x2 matrix (expressing scale, shear and rotation) and a
151 /// translation vector.
152 ///
153 /// Equivalent to
154 /// `Affine2::from_translation(translation) * Affine2::from_mat2(mat2)`
155 #[inline]
156 pub fn from_mat2_translation(matrix2: Mat2, translation: Vec2) -> Self {
157 Self {
158 matrix2,
159 translation,
160 }
161 }
162
163 /// Creates an affine transform from the given 2D `scale`, rotation `angle` (in radians) and
164 /// `translation`.
165 ///
166 /// Equivalent to `Affine2::from_translation(translation) *
167 /// Affine2::from_angle(angle) * Affine2::from_scale(scale)`
168 #[inline]
169 pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
170 let rotation = Mat2::from_angle(angle);
171 Self {
172 matrix2: Mat2::from_cols(rotation.x_axis * scale.x, rotation.y_axis * scale.y),
173 translation,
174 }
175 }
176
177 /// Creates an affine transform from the given 2D rotation `angle` (in radians) and
178 /// `translation`.
179 ///
180 /// Equivalent to `Affine2::from_translation(translation) * Affine2::from_angle(angle)`
181 #[inline]
182 pub fn from_angle_translation(angle: f32, translation: Vec2) -> Self {
183 Self {
184 matrix2: Mat2::from_angle(angle),
185 translation,
186 }
187 }
188
189 /// The given `Mat3` must be an affine transform,
190 #[inline]
191 pub fn from_mat3(m: Mat3) -> Self {
192 use crate::swizzles::Vec3Swizzles;
193 Self {
194 matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
195 translation: m.z_axis.xy(),
196 }
197 }
198
199 /// The given `Mat3A` must be an affine transform,
200 #[inline]
201 pub fn from_mat3a(m: Mat3A) -> Self {
202 use crate::swizzles::Vec3Swizzles;
203 Self {
204 matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
205 translation: m.z_axis.xy(),
206 }
207 }
208
209 /// Transforms the given 2D point, applying shear, scale, rotation and translation.
210 #[inline]
211 pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
212 self.matrix2 * rhs + self.translation
213 }
214
215 /// Transforms the given 2D vector, applying shear, scale and rotation (but NOT
216 /// translation).
217 ///
218 /// To also apply translation, use [`Self::transform_point2`] instead.
219 #[inline]
220 pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 {
221 self.matrix2 * rhs
222 }
223
224 /// Returns `true` if, and only if, all elements are finite.
225 ///
226 /// If any element is either `NaN`, positive or negative infinity, this will return
227 /// `false`.
228 #[inline]
229 pub fn is_finite(&self) -> bool {
230 self.matrix2.is_finite() && self.translation.is_finite()
231 }
232
233 /// Returns `true` if any elements are `NaN`.
234 #[inline]
235 pub fn is_nan(&self) -> bool {
236 self.matrix2.is_nan() || self.translation.is_nan()
237 }
238
239 /// Returns true if the absolute difference of all elements between `self` and `rhs`
240 /// is less than or equal to `max_abs_diff`.
241 ///
242 /// This can be used to compare if two 3x4 matrices contain similar elements. It works
243 /// best when comparing with a known value. The `max_abs_diff` that should be used used
244 /// depends on the values being compared against.
245 ///
246 /// For more see
247 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
248 #[inline]
249 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
250 self.matrix2.abs_diff_eq(rhs.matrix2, max_abs_diff)
251 && self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
252 }
253
254 /// Return the inverse of this transform.
255 ///
256 /// Note that if the transform is not invertible the result will be invalid.
257 #[must_use]
258 #[inline]
259 pub fn inverse(&self) -> Self {
260 let matrix2 = self.matrix2.inverse();
261 // transform negative translation by the matrix inverse:
262 let translation = -(matrix2 * self.translation);
263
264 Self {
265 matrix2,
266 translation,
267 }
268 }
269 }
270
271 impl Default for Affine2 {
272 #[inline(always)]
273 fn default() -> Self {
274 Self::IDENTITY
275 }
276 }
277
278 impl Deref for Affine2 {
279 type Target = crate::deref::Cols3<Vec2>;
280 #[inline(always)]
281 fn deref(&self) -> &Self::Target {
282 unsafe { &*(self as *const Self as *const Self::Target) }
283 }
284 }
285
286 impl DerefMut for Affine2 {
287 #[inline(always)]
288 fn deref_mut(&mut self) -> &mut Self::Target {
289 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
290 }
291 }
292
293 impl PartialEq for Affine2 {
294 #[inline]
295 fn eq(&self, rhs: &Self) -> bool {
296 self.matrix2.eq(&rhs.matrix2) && self.translation.eq(&rhs.translation)
297 }
298 }
299
300 #[cfg(not(target_arch = "spirv"))]
301 impl core::fmt::Debug for Affine2 {
302 fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
303 fmt.debug_struct(stringify!(Affine2))
304 .field("matrix2", &self.matrix2)
305 .field("translation", &self.translation)
306 .finish()
307 }
308 }
309
310 #[cfg(not(target_arch = "spirv"))]
311 impl core::fmt::Display for Affine2 {
312 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
313 write!(
314 f,
315 "[{}, {}, {}]",
316 self.matrix2.x_axis, self.matrix2.y_axis, self.translation
317 )
318 }
319 }
320
321 impl<'a> core::iter::Product<&'a Self> for Affine2 {
322 fn product<I>(iter: I) -> Self
323 where
324 I: Iterator<Item = &'a Self>,
325 {
326 iter.fold(Self::IDENTITY, |a, &b| a * b)
327 }
328 }
329
330 impl Mul for Affine2 {
331 type Output = Affine2;
332
333 #[inline]
334 fn mul(self, rhs: Affine2) -> Self::Output {
335 Self {
336 matrix2: self.matrix2 * rhs.matrix2,
337 translation: self.matrix2 * rhs.translation + self.translation,
338 }
339 }
340 }
341
342 impl From<Affine2> for Mat3 {
343 #[inline]
344 fn from(m: Affine2) -> Mat3 {
345 Self::from_cols(
346 m.matrix2.x_axis.extend(0.0),
347 m.matrix2.y_axis.extend(0.0),
348 m.translation.extend(1.0),
349 )
350 }
351 }
352
353 impl Mul<Mat3> for Affine2 {
354 type Output = Mat3;
355
356 #[inline]
357 fn mul(self, rhs: Mat3) -> Self::Output {
358 Mat3::from(self) * rhs
359 }
360 }
361
362 impl Mul<Affine2> for Mat3 {
363 type Output = Mat3;
364
365 #[inline]
366 fn mul(self, rhs: Affine2) -> Self::Output {
367 self * Mat3::from(rhs)
368 }
369 }
370
371 impl From<Affine2> for Mat3A {
372 #[inline]
373 fn from(m: Affine2) -> Mat3A {
374 Self::from_cols(
375 Vec3A::from((m.matrix2.x_axis, 0.0)),
376 Vec3A::from((m.matrix2.y_axis, 0.0)),
377 Vec3A::from((m.translation, 1.0)),
378 )
379 }
380 }
381
382 impl Mul<Mat3A> for Affine2 {
383 type Output = Mat3A;
384
385 #[inline]
386 fn mul(self, rhs: Mat3A) -> Self::Output {
387 Mat3A::from(self) * rhs
388 }
389 }
390
391 impl Mul<Affine2> for Mat3A {
392 type Output = Mat3A;
393
394 #[inline]
395 fn mul(self, rhs: Affine2) -> Self::Output {
396 self * Mat3A::from(rhs)
397 }
398 }