Debian Codegraph @

debian/missing-sources/glam-0.22.0/src/f64/daffine2.rs

raw symbols SCIP
1 // Generated from affine.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{DMat2, DMat3, DVec2};
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 DAffine2 {
10 pub matrix2: DMat2,
11 pub translation: DVec2,
12 }
13
14 impl DAffine2 {
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: DMat2::ZERO,
21 translation: DVec2::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: DMat2::IDENTITY,
29 translation: DVec2::ZERO,
30 };
31
32 /// All NAN:s.
33 pub const NAN: Self = Self {
34 matrix2: DMat2::NAN,
35 translation: DVec2::NAN,
36 };
37
38 /// Creates an affine transform from three column vectors.
39 #[inline(always)]
40 pub const fn from_cols(x_axis: DVec2, y_axis: DVec2, z_axis: DVec2) -> Self {
41 Self {
42 matrix2: DMat2::from_cols(x_axis, y_axis),
43 translation: z_axis,
44 }
45 }
46
47 /// Creates an affine transform from a `[f64; 6]` array stored in column major order.
48 #[inline]
49 pub fn from_cols_array(m: &[f64; 6]) -> Self {
50 Self {
51 matrix2: DMat2::from_cols_slice(&m[0..4]),
52 translation: DVec2::from_slice(&m[4..6]),
53 }
54 }
55
56 /// Creates a `[f64; 6]` array storing data in column major order.
57 #[inline]
58 pub fn to_cols_array(&self) -> [f64; 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 `[[f64; 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: &[[f64; 2]; 3]) -> Self {
71 Self {
72 matrix2: DMat2::from_cols(m[0].into(), m[1].into()),
73 translation: m[2].into(),
74 }
75 }
76
77 /// Creates a `[[f64; 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) -> [[f64; 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: &[f64]) -> Self {
96 Self {
97 matrix2: DMat2::from_cols_slice(&slice[0..4]),
98 translation: DVec2::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 [f64]) {
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: DVec2) -> Self {
117 Self {
118 matrix2: DMat2::from_diagonal(scale),
119 translation: DVec2::ZERO,
120 }
121 }
122
123 /// Creates an affine transform from the given rotation `angle`.
124 #[inline]
125 pub fn from_angle(angle: f64) -> Self {
126 Self {
127 matrix2: DMat2::from_angle(angle),
128 translation: DVec2::ZERO,
129 }
130 }
131
132 /// Creates an affine transformation from the given 2D `translation`.
133 #[inline]
134 pub fn from_translation(translation: DVec2) -> Self {
135 Self {
136 matrix2: DMat2::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: DMat2) -> Self {
144 Self {
145 matrix2,
146 translation: DVec2::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 /// `DAffine2::from_translation(translation) * DAffine2::from_mat2(mat2)`
155 #[inline]
156 pub fn from_mat2_translation(matrix2: DMat2, translation: DVec2) -> 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 `DAffine2::from_translation(translation) *
167 /// DAffine2::from_angle(angle) * DAffine2::from_scale(scale)`
168 #[inline]
169 pub fn from_scale_angle_translation(scale: DVec2, angle: f64, translation: DVec2) -> Self {
170 let rotation = DMat2::from_angle(angle);
171 Self {
172 matrix2: DMat2::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 `DAffine2::from_translation(translation) * DAffine2::from_angle(angle)`
181 #[inline]
182 pub fn from_angle_translation(angle: f64, translation: DVec2) -> Self {
183 Self {
184 matrix2: DMat2::from_angle(angle),
185 translation,
186 }
187 }
188
189 /// The given `DMat3` must be an affine transform,
190 #[inline]
191 pub fn from_mat3(m: DMat3) -> Self {
192 use crate::swizzles::Vec3Swizzles;
193 Self {
194 matrix2: DMat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
195 translation: m.z_axis.xy(),
196 }
197 }
198
199 /// Transforms the given 2D point, applying shear, scale, rotation and translation.
200 #[inline]
201 pub fn transform_point2(&self, rhs: DVec2) -> DVec2 {
202 self.matrix2 * rhs + self.translation
203 }
204
205 /// Transforms the given 2D vector, applying shear, scale and rotation (but NOT
206 /// translation).
207 ///
208 /// To also apply translation, use [`Self::transform_point2`] instead.
209 #[inline]
210 pub fn transform_vector2(&self, rhs: DVec2) -> DVec2 {
211 self.matrix2 * rhs
212 }
213
214 /// Returns `true` if, and only if, all elements are finite.
215 ///
216 /// If any element is either `NaN`, positive or negative infinity, this will return
217 /// `false`.
218 #[inline]
219 pub fn is_finite(&self) -> bool {
220 self.matrix2.is_finite() && self.translation.is_finite()
221 }
222
223 /// Returns `true` if any elements are `NaN`.
224 #[inline]
225 pub fn is_nan(&self) -> bool {
226 self.matrix2.is_nan() || self.translation.is_nan()
227 }
228
229 /// Returns true if the absolute difference of all elements between `self` and `rhs`
230 /// is less than or equal to `max_abs_diff`.
231 ///
232 /// This can be used to compare if two 3x4 matrices contain similar elements. It works
233 /// best when comparing with a known value. The `max_abs_diff` that should be used used
234 /// depends on the values being compared against.
235 ///
236 /// For more see
237 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
238 #[inline]
239 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
240 self.matrix2.abs_diff_eq(rhs.matrix2, max_abs_diff)
241 && self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
242 }
243
244 /// Return the inverse of this transform.
245 ///
246 /// Note that if the transform is not invertible the result will be invalid.
247 #[must_use]
248 #[inline]
249 pub fn inverse(&self) -> Self {
250 let matrix2 = self.matrix2.inverse();
251 // transform negative translation by the matrix inverse:
252 let translation = -(matrix2 * self.translation);
253
254 Self {
255 matrix2,
256 translation,
257 }
258 }
259 }
260
261 impl Default for DAffine2 {
262 #[inline(always)]
263 fn default() -> Self {
264 Self::IDENTITY
265 }
266 }
267
268 impl Deref for DAffine2 {
269 type Target = crate::deref::Cols3<DVec2>;
270 #[inline(always)]
271 fn deref(&self) -> &Self::Target {
272 unsafe { &*(self as *const Self as *const Self::Target) }
273 }
274 }
275
276 impl DerefMut for DAffine2 {
277 #[inline(always)]
278 fn deref_mut(&mut self) -> &mut Self::Target {
279 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
280 }
281 }
282
283 impl PartialEq for DAffine2 {
284 #[inline]
285 fn eq(&self, rhs: &Self) -> bool {
286 self.matrix2.eq(&rhs.matrix2) && self.translation.eq(&rhs.translation)
287 }
288 }
289
290 #[cfg(not(target_arch = "spirv"))]
291 impl core::fmt::Debug for DAffine2 {
292 fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
293 fmt.debug_struct(stringify!(DAffine2))
294 .field("matrix2", &self.matrix2)
295 .field("translation", &self.translation)
296 .finish()
297 }
298 }
299
300 #[cfg(not(target_arch = "spirv"))]
301 impl core::fmt::Display for DAffine2 {
302 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
303 write!(
304 f,
305 "[{}, {}, {}]",
306 self.matrix2.x_axis, self.matrix2.y_axis, self.translation
307 )
308 }
309 }
310
311 impl<'a> core::iter::Product<&'a Self> for DAffine2 {
312 fn product<I>(iter: I) -> Self
313 where
314 I: Iterator<Item = &'a Self>,
315 {
316 iter.fold(Self::IDENTITY, |a, &b| a * b)
317 }
318 }
319
320 impl Mul for DAffine2 {
321 type Output = DAffine2;
322
323 #[inline]
324 fn mul(self, rhs: DAffine2) -> Self::Output {
325 Self {
326 matrix2: self.matrix2 * rhs.matrix2,
327 translation: self.matrix2 * rhs.translation + self.translation,
328 }
329 }
330 }
331
332 impl From<DAffine2> for DMat3 {
333 #[inline]
334 fn from(m: DAffine2) -> DMat3 {
335 Self::from_cols(
336 m.matrix2.x_axis.extend(0.0),
337 m.matrix2.y_axis.extend(0.0),
338 m.translation.extend(1.0),
339 )
340 }
341 }
342
343 impl Mul<DMat3> for DAffine2 {
344 type Output = DMat3;
345
346 #[inline]
347 fn mul(self, rhs: DMat3) -> Self::Output {
348 DMat3::from(self) * rhs
349 }
350 }
351
352 impl Mul<DAffine2> for DMat3 {
353 type Output = DMat3;
354
355 #[inline]
356 fn mul(self, rhs: DAffine2) -> Self::Output {
357 self * DMat3::from(rhs)
358 }
359 }