Debian Codegraph @

debian/missing-sources/glam/templates/affine.rs.tera

raw
1 {% import "macros.rs.tera" as macros %}
2
3 // Generated from {{template_path}} template. Edit the template, not the generated file.
4
5 {% if scalar_t == "f32" %}
6 {% if dim == 3 %}
7 {% set self_t = "Affine3A" %}
8 {% set col_t = "Vec3A" %}
9 {% set mat_t = "Mat3A" %}
10 {% else %}
11 {% set self_t = "Affine2" %}
12 {% set col_t = "Vec2" %}
13 {% set mat_t = "Mat2" %}
14 {% endif %}
15 {% set quat_t = "Quat" %}
16 {% set vec2_t = "Vec2" %}
17 {% set vec3_t = "Vec3" %}
18 {% set mat3_t = "Mat3" %}
19 {% set mat4_t = "Mat4" %}
20 {% elif scalar_t == "f64" %}
21 {% set self_t = "DAffine" ~ dim %}
22 {% set col_t = "DVec" ~ dim %}
23 {% set mat_t = "DMat" ~ dim %}
24 {% set quat_t = "DQuat" %}
25 {% set vec2_t = "DVec2" %}
26 {% set vec3_t = "DVec3" %}
27 {% set mat3_t = "DMat3" %}
28 {% set mat4_t = "DMat4" %}
29 {% endif %}
30
31 {% if dim == 2 %}
32 {% set size = 6 %}
33 {% set mat_size = 4 %}
34 {% set vec_size = 2 %}
35 {% elif dim == 3 %}
36 {% set size = 12 %}
37 {% set mat_size = 9 %}
38 {% set vec_size = 3 %}
39 {% endif %}
40
41 {% set components = ["x", "y", "z", "w"] | slice(end = dim + 1) %}
42 {% set axes = ["x_axis", "y_axis", "z_axis", "w_axis"] | slice(end = dim + 1) %}
43
44 use crate::{
45 {% if self_t == "Affine2" %}
46 Mat3A, Vec3A,
47 {% elif self_t == "Affine3A" %}
48 Vec3, Mat3,
49 {% endif %}
50 {% if dim == 2 %}
51 {{ mat_t }}, {{ col_t }}, {{ mat3_t }},
52 {% elif dim == 3 %}
53 {{ mat_t }}, {{ col_t}}, {{ mat4_t }}, {{ quat_t }},
54 {% endif %}
55 };
56 use core::ops::{Deref, DerefMut, Mul, MulAssign};
57
58 #[cfg(
59 {% if scalar_t != "f64" %}
60 all(feature = "zerocopy", not(feature = "core-simd"))
61 {% else %}
62 feature = "zerocopy"
63 {% endif %}
64 )]
65 use zerocopy_derive::*;
66
67 /// A {{ dim }}D affine transform, which can represent translation, rotation, scaling and shear.
68 {%- if is_align %}
69 ///
70 /// This type is 16 byte aligned.
71 {%- endif %}
72 #[derive(Copy, Clone)]
73 {%- if self_t == "Affine2" %}
74 #[cfg_attr(all(feature = "bytemuck", not(feature = "scalar-math")), derive(bytemuck::AnyBitPattern))]
75 #[cfg_attr(all(feature = "bytemuck", feature = "scalar-math"), derive(bytemuck::Pod, bytemuck::Zeroable))]
76 #[cfg_attr(
77 {% if scalar_t != "f64" %}
78 all(feature = "zerocopy", not(feature = "core-simd")),
79 {% else %}
80 feature = "zerocopy",
81 {% endif %}
82 derive(FromBytes, Immutable, KnownLayout))
83 ]
84 {%- elif self_t == "Affine3A" and is_scalar %}
85 #[cfg_attr(feature = "bytemuck", derive(bytemuck::AnyBitPattern))]
86 #[cfg_attr(all(feature = "zerocopy", not(feature = "core-simd")), derive(FromBytes, Immutable, KnownLayout))]
87 #[cfg_attr(all(feature = "zerocopy", any(target_arch = "aarch64", target_feature = "sse2", target_feature = "simd128"), not(any(feature = "core-simd", feature = "scalar-math"))), derive(IntoBytes))]
88 {%- else %}
89 #[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
90 #[cfg_attr(
91 {% if scalar_t != "f64" %}
92 all(feature = "zerocopy", not(feature = "core-simd")),
93 {% else %}
94 feature = "zerocopy",
95 {% endif %}
96 derive(FromBytes, Immutable, IntoBytes, KnownLayout))
97 ]
98 {%- endif %}
99 #[repr(C)]
100 pub struct {{ self_t }} {
101 pub matrix{{ dim }}: {{ mat_t }},
102 pub translation: {{ col_t }},
103 }
104
105 impl {{ self_t }} {
106 /// The degenerate zero transform.
107 ///
108 /// This transforms any finite vector and point to zero.
109 /// The zero transform is non-invertible.
110 pub const ZERO: Self = Self {
111 matrix{{ dim }}: {{ mat_t }}::ZERO,
112 translation: {{ col_t }}::ZERO,
113 };
114
115 /// The identity transform.
116 ///
117 /// Multiplying a vector with this returns the same vector.
118 pub const IDENTITY: Self = Self {
119 matrix{{ dim }}: {{ mat_t }}::IDENTITY,
120 translation: {{ col_t }}::ZERO,
121 };
122
123 /// All NAN:s.
124 pub const NAN: Self = Self {
125 matrix{{ dim }}: {{ mat_t }}::NAN,
126 translation: {{ col_t }}::NAN,
127 };
128
129 /// Creates an affine transform from three column vectors.
130 #[inline(always)]
131 #[must_use]
132 pub const fn from_cols(
133 {% for axis in axes %}
134 {{ axis }}: {{ col_t }},
135 {% endfor %}
136 ) -> Self {
137 Self {
138 matrix{{ dim }}: {{ mat_t }}::from_cols(
139 {% for axis in axes | slice(end = dim) %}
140 {{ axis }},
141 {% endfor %}
142 ),
143 translation: {{ axes[dim] }},
144 }
145 }
146
147 /// Creates an affine transform from a `[{{ scalar_t }}; {{ size }}]` array stored in column major order.
148 #[inline]
149 #[must_use]
150 pub fn from_cols_array(m: &[{{ scalar_t }}; {{ size }}]) -> Self {
151 Self {
152 matrix{{ dim }}: {{ mat_t }}::from_cols_array(&[
153 {% for i in range(end = mat_size) %}
154 m[{{ i }}],
155 {%- endfor %}
156 ]),
157 translation: {{ col_t }}::from_array([
158 {% for i in range(start = mat_size, end = mat_size + vec_size) %}
159 m[{{ i }}],
160 {%- endfor %}
161 ]),
162 }
163 }
164
165 /// Creates a `[{{ scalar_t }}; {{ size }}]` array storing data in column major order.
166 #[inline]
167 #[must_use]
168 pub fn to_cols_array(&self) -> [{{ scalar_t }}; {{ size }}] {
169 {% for i in range(end = dim) %}
170 let {{ components[i] }} = &self.matrix{{ dim }}.{{ axes[i] }};
171 {%- endfor %}
172 let {{ components[dim] }} = &self.translation;
173 [
174 {% for i in range(end = dim + 1) %}
175 {% for j in range(end = dim) %}
176 {{ components[i] }}.{{ components[j] }},
177 {% endfor %}
178 {% endfor %}
179 ]
180 }
181
182 /// Creates an affine transform from a `[[{{ scalar_t }}; {{ dim }}]; {{ dim + 1 }}]`
183 /// {{ dim }}D array stored in column major order.
184 /// If your data is in row major order you will need to `transpose` the returned
185 /// matrix.
186 #[inline]
187 #[must_use]
188 pub fn from_cols_array_2d(m: &[[{{ scalar_t }}; {{ dim }}]; {{ dim + 1 }}]) -> Self {
189 Self {
190 matrix{{ dim }}: {{ mat_t }}::from_cols(
191 {% for i in range(end = dim) %}
192 m[{{ i }}].into(),
193 {% endfor %}
194 ),
195 translation: m[{{ dim }}].into(),
196 }
197 }
198
199 /// Creates a `[[{{ scalar_t }}; {{ dim }}]; {{ dim + 1 }}]` {{ dim }}D array storing data in
200 /// column major order.
201 /// If you require data in row major order `transpose` the matrix first.
202 #[inline]
203 #[must_use]
204 pub fn to_cols_array_2d(&self) -> [[{{ scalar_t }}; {{ dim }}]; {{ dim + 1 }}] {
205 [
206 {% for i in range(end = dim) %}
207 self.matrix{{ dim }}.{{ axes[i] }}.into(),
208 {% endfor %}
209 self.translation.into(),
210 ]
211 }
212
213 /// Creates an affine transform from the first {{ size }} values in `slice`.
214 ///
215 /// # Panics
216 ///
217 /// Panics if `slice` is less than {{ size }} elements long.
218 #[inline]
219 #[must_use]
220 pub fn from_cols_slice(slice: &[{{ scalar_t }}]) -> Self {
221 Self {
222 matrix{{ dim }}: {{ mat_t }}::from_cols_slice(&slice[0..{{ dim * dim }}]),
223 translation: {{ col_t }}::from_slice(&slice[{{ dim * dim }}..{{ size }}]),
224 }
225 }
226
227 /// Writes the columns of `self` to the first {{ size }} elements in `slice`.
228 ///
229 /// # Panics
230 ///
231 /// Panics if `slice` is less than {{ size }} elements long.
232 #[inline]
233 pub fn write_cols_to_slice(self, slice: &mut [{{ scalar_t }}]) {
234 self.matrix{{ dim }}.write_cols_to_slice(&mut slice[0..{{ dim * dim }}]);
235 self.translation.write_to_slice(&mut slice[{{ dim * dim }}..{{ size }}]);
236 }
237
238 {% if dim == 2 %}
239 /// Creates an affine transform that changes scale.
240 /// Note that if any scale is zero the transform will be non-invertible.
241 #[inline]
242 #[must_use]
243 pub fn from_scale(scale: {{ vec2_t }}) -> Self {
244 Self {
245 matrix{{ dim }}: {{ mat_t }}::from_diagonal(scale),
246 translation: {{ col_t }}::ZERO,
247 }
248 }
249
250 /// Creates an affine transform from the given rotation `angle`.
251 #[inline]
252 #[must_use]
253 pub fn from_angle(angle: {{ scalar_t }}) -> Self {
254 Self {
255 matrix2: {{ mat_t }}::from_angle(angle),
256 translation: {{ col_t }}::ZERO,
257 }
258 }
259
260 /// Creates an affine transformation from the given 2D `translation`.
261 #[inline]
262 #[must_use]
263 pub fn from_translation(translation: {{ vec2_t }}) -> Self {
264 Self {
265 matrix2: {{ mat_t }}::IDENTITY,
266 translation,
267 }
268 }
269
270 /// Creates an affine transform from a 2x2 matrix (expressing scale, shear and rotation)
271 #[inline]
272 #[must_use]
273 pub fn from_mat2(matrix2: {{ mat_t }}) -> Self {
274 Self {
275 matrix2,
276 translation: {{ col_t }}::ZERO,
277 }
278 }
279
280 /// Creates an affine transform from a 2x2 matrix (expressing scale, shear and rotation) and a
281 /// translation vector.
282 ///
283 /// Equivalent to
284 /// `{{ self_t }}::from_translation(translation) * {{ self_t }}::from_mat{{ dim }}(mat{{ dim }})`
285 #[inline]
286 #[must_use]
287 pub fn from_mat2_translation(matrix2: {{ mat_t }}, translation: {{ vec2_t }}) -> Self {
288 Self {
289 matrix2,
290 translation,
291 }
292 }
293
294 /// Creates an affine transform from the given 2D `scale`, rotation `angle` (in radians) and
295 /// `translation`.
296 ///
297 /// Equivalent to `{{ self_t }}::from_translation(translation) *
298 /// {{ self_t }}::from_angle(angle) * {{ self_t }}::from_scale(scale)`
299 #[inline]
300 #[must_use]
301 pub fn from_scale_angle_translation(
302 scale: {{ vec2_t }},
303 angle: {{ scalar_t }},
304 translation: {{ vec2_t }},
305 ) -> Self {
306 let rotation = {{ mat_t }}::from_angle(angle);
307 Self {
308 matrix2: {{ mat_t }}::from_cols(
309 rotation.x_axis * scale.x,
310 rotation.y_axis * scale.y,
311 ),
312 translation,
313 }
314 }
315
316 /// Creates an affine transform from the given 2D rotation `angle` (in radians) and
317 /// `translation`.
318 ///
319 /// Equivalent to `{{ self_t }}::from_translation(translation) * {{ self_t }}::from_angle(angle)`
320 #[inline]
321 #[must_use]
322 pub fn from_angle_translation(angle: {{ scalar_t }}, translation: {{ vec2_t }}) -> Self {
323 Self {
324 matrix2: {{ mat_t }}::from_angle(angle),
325 translation,
326 }
327 }
328
329 /// The given `{{ mat3_t }}` must be an affine transform,
330 #[inline]
331 #[must_use]
332 pub fn from_mat3(m: {{ mat3_t }}) -> Self {
333 use crate::swizzles::Vec3Swizzles;
334 Self {
335 matrix2: {{ mat_t }}::from_cols(m.x_axis.xy(), m.y_axis.xy()),
336 translation: m.z_axis.xy(),
337 }
338 }
339
340 {% if scalar_t == "f32" %}
341 /// The given [`Mat3A`] must be an affine transform,
342 #[inline]
343 #[must_use]
344 pub fn from_mat3a(m: Mat3A) -> Self {
345 use crate::swizzles::Vec3Swizzles;
346 Self {
347 matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
348 translation: m.z_axis.xy(),
349 }
350 }
351 {% endif %}
352
353 /// Extracts `scale`, `angle` and `translation` from `self`.
354 ///
355 /// The transform is expected to be non-degenerate and without shearing, or the output
356 /// will be invalid.
357 ///
358 /// # Panics
359 ///
360 /// Will panic if the determinant `self.matrix2` is zero or if the resulting scale
361 /// vector contains any zero elements when `glam_assert` is enabled.
362 #[inline]
363 #[must_use]
364 pub fn to_scale_angle_translation(self) -> ({{ vec2_t }}, {{ scalar_t }}, {{ vec2_t }}) {
365 use crate::{{ scalar_t }}::math;
366 let det = self.matrix2.determinant();
367 glam_assert!(det != 0.0);
368
369 let scale = {{ col_t }}::new(
370 self.matrix2.x_axis.length() * math::signum(det),
371 self.matrix2.y_axis.length(),
372 );
373
374 glam_assert!(scale.cmpne({{ col_t }}::ZERO).all());
375
376 let angle = math::atan2(-self.matrix2.y_axis.x, self.matrix2.y_axis.y);
377
378 (scale, angle, self.translation)
379 }
380
381 /// Transforms the given 2D point, applying shear, scale, rotation and translation.
382 #[inline]
383 #[must_use]
384 pub fn transform_point2(&self, rhs: {{ vec2_t }}) -> {{ vec2_t }} {
385 self.matrix2 * rhs + self.translation
386 }
387
388 /// Transforms the given 2D vector, applying shear, scale and rotation (but NOT
389 /// translation).
390 ///
391 /// To also apply translation, use [`Self::transform_point2()`] instead.
392 #[inline]
393 pub fn transform_vector2(&self, rhs: {{ vec2_t }}) -> {{ vec2_t }} {
394 self.matrix2 * rhs
395 }
396
397 {% elif dim == 3 %}
398 /// Creates an affine transform that changes scale.
399 /// Note that if any scale is zero the transform will be non-invertible.
400 #[inline]
401 #[must_use]
402 pub fn from_scale(scale: {{ vec3_t }}) -> Self {
403 Self {
404 matrix3: {{ mat_t }}::from_diagonal(scale),
405 translation: {{ col_t }}::ZERO,
406 }
407 }
408 /// Creates an affine transform from the given `rotation` quaternion.
409 #[inline]
410 #[must_use]
411 pub fn from_quat(rotation: {{ quat_t }}) -> Self {
412 Self {
413 matrix3: {{ mat_t }}::from_quat(rotation),
414 translation: {{ col_t }}::ZERO,
415 }
416 }
417
418 /// Creates an affine transform containing a 3D rotation around a normalized
419 /// rotation `axis` of `angle` (in radians).
420 #[inline]
421 #[must_use]
422 pub fn from_axis_angle(axis: {{ vec3_t }}, angle: {{ scalar_t }}) -> Self {
423 Self {
424 matrix3: {{ mat_t }}::from_axis_angle(axis, angle),
425 translation: {{ col_t }}::ZERO,
426 }
427 }
428
429 /// Creates an affine transform containing a 3D rotation around the x axis of
430 /// `angle` (in radians).
431 #[inline]
432 #[must_use]
433 pub fn from_rotation_x(angle: {{ scalar_t }}) -> Self {
434 Self {
435 matrix3: {{ mat_t }}::from_rotation_x(angle),
436 translation: {{ col_t }}::ZERO,
437 }
438 }
439
440 /// Creates an affine transform containing a 3D rotation around the y axis of
441 /// `angle` (in radians).
442 #[inline]
443 #[must_use]
444 pub fn from_rotation_y(angle: {{ scalar_t }}) -> Self {
445 Self {
446 matrix3: {{ mat_t }}::from_rotation_y(angle),
447 translation: {{ col_t }}::ZERO,
448 }
449 }
450
451 /// Creates an affine transform containing a 3D rotation around the z axis of
452 /// `angle` (in radians).
453 #[inline]
454 #[must_use]
455 pub fn from_rotation_z(angle: {{ scalar_t }}) -> Self {
456 Self {
457 matrix3: {{ mat_t }}::from_rotation_z(angle),
458 translation: {{ col_t }}::ZERO,
459 }
460 }
461
462 /// Creates an affine transformation from the given 3D `translation`.
463 #[inline]
464 #[must_use]
465 pub fn from_translation(translation: {{ vec3_t }}) -> Self {
466 #[allow(clippy::useless_conversion)]
467 Self {
468 matrix3: {{ mat_t }}::IDENTITY,
469 translation: translation.into(),
470 }
471 }
472
473 /// Creates an affine transform from a 3x3 matrix (expressing scale, shear and
474 /// rotation)
475 #[inline]
476 #[must_use]
477 pub fn from_mat3(mat3: {{ mat3_t }}) -> Self {
478 #[allow(clippy::useless_conversion)]
479 Self {
480 matrix3: mat3.into(),
481 translation: {{ col_t }}::ZERO,
482 }
483 }
484
485 /// Creates an affine transform from a 3x3 matrix (expressing scale, shear and rotation)
486 /// and a translation vector.
487 ///
488 /// Equivalent to `{{ self_t }}::from_translation(translation) * {{ self_t }}::from_mat3(mat3)`
489 #[inline]
490 #[must_use]
491 pub fn from_mat3_translation(mat3: {{ mat3_t }}, translation: {{ vec3_t }}) -> Self {
492 #[allow(clippy::useless_conversion)]
493 Self {
494 matrix3: mat3.into(),
495 translation: translation.into(),
496 }
497 }
498
499 /// Creates an affine transform from the given 3D `scale`, `rotation` and
500 /// `translation`.
501 ///
502 /// Equivalent to `{{ self_t }}::from_translation(translation) *
503 /// {{ self_t }}::from_quat(rotation) * {{ self_t }}::from_scale(scale)`
504 #[inline]
505 #[must_use]
506 pub fn from_scale_rotation_translation(
507 scale: {{ vec3_t }},
508 rotation: {{ quat_t }},
509 translation: {{ vec3_t }},
510 ) -> Self {
511 let rotation = {{ mat_t }}::from_quat(rotation);
512 #[allow(clippy::useless_conversion)]
513 Self {
514 matrix3: {{ mat_t }}::from_cols(
515 rotation.x_axis * scale.x,
516 rotation.y_axis * scale.y,
517 rotation.z_axis * scale.z,
518 ),
519 translation: translation.into(),
520 }
521 }
522
523 /// Creates an affine transform from the given 3D `rotation` and `translation`.
524 ///
525 /// Equivalent to `{{ self_t }}::from_translation(translation) * {{ self_t }}::from_quat(rotation)`
526 #[inline]
527 #[must_use]
528 pub fn from_rotation_translation(rotation: {{ quat_t }}, translation: {{ vec3_t }}) -> Self {
529 #[allow(clippy::useless_conversion)]
530 Self {
531 matrix3: {{ mat_t }}::from_quat(rotation),
532 translation: translation.into(),
533 }
534 }
535
536 /// The given `{{ mat4_t }}` must be an affine transform,
537 /// i.e. contain no perspective transform.
538 #[inline]
539 #[must_use]
540 pub fn from_mat4(m: {{ mat4_t }}) -> Self {
541 Self {
542 matrix3: {{ mat_t }}::from_cols(
543 {{ col_t }}::from_vec4(m.x_axis),
544 {{ col_t }}::from_vec4(m.y_axis),
545 {{ col_t }}::from_vec4(m.z_axis),
546 ),
547 translation: {{ col_t }}::from_vec4(m.w_axis),
548 }
549 }
550
551 /// Extracts `scale`, `rotation` and `translation` from `self`.
552 ///
553 /// The transform is expected to be non-degenerate and without shearing, or the output
554 /// will be invalid.
555 ///
556 /// # Panics
557 ///
558 /// Will panic if the determinant `self.matrix3` is zero or if the resulting scale
559 /// vector contains any zero elements when `glam_assert` is enabled.
560 #[inline]
561 #[must_use]
562 pub fn to_scale_rotation_translation(&self) -> ({{ vec3_t }}, {{ quat_t }}, {{ vec3_t }}) {
563 use crate::{{ scalar_t }}::math;
564 let det = self.matrix3.determinant();
565 glam_assert!(det != 0.0);
566
567 let scale = {{ vec3_t }}::new(
568 self.matrix3.x_axis.length() * math::signum(det),
569 self.matrix3.y_axis.length(),
570 self.matrix3.z_axis.length(),
571 );
572
573 glam_assert!(scale.cmpne({{ vec3_t }}::ZERO).all());
574
575 let inv_scale = scale.recip();
576
577 #[allow(clippy::useless_conversion)]
578 let rotation = {{ quat_t }}::from_mat3(&{{ mat3_t }}::from_cols(
579 (self.matrix3.x_axis * inv_scale.x).into(),
580 (self.matrix3.y_axis * inv_scale.y).into(),
581 (self.matrix3.z_axis * inv_scale.z).into(),
582 ));
583
584 #[allow(clippy::useless_conversion)]
585 (scale, rotation, self.translation.into())
586 }
587
588 /// Creates a left-handed view transform using a camera position, an up direction, and a facing
589 /// direction.
590 ///
591 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
592 #[inline]
593 #[must_use]
594 pub fn look_to_lh(eye: {{ vec3_t }}, dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
595 Self::look_to_rh(eye, -dir, up)
596 }
597
598 /// Creates a right-handed view transform using a camera position, an up direction, and a facing
599 /// direction.
600 ///
601 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
602 #[inline]
603 #[must_use]
604 pub fn look_to_rh(eye: {{ vec3_t }}, dir: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
605 let f = dir.normalize();
606 let s = f.cross(up).normalize();
607 let u = s.cross(f);
608
609 Self {
610 matrix3: {{ mat_t }}::from_cols(
611 {{ col_t }}::new(s.x, u.x, -f.x),
612 {{ col_t }}::new(s.y, u.y, -f.y),
613 {{ col_t }}::new(s.z, u.z, -f.z),
614 ),
615 translation: {{ col_t }}::new(-eye.dot(s), -eye.dot(u), eye.dot(f)),
616 }
617 }
618
619 /// Creates a left-handed view transform using a camera position, an up direction, and a focal
620 /// point.
621 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
622 ///
623 /// # Panics
624 ///
625 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
626 #[inline]
627 #[must_use]
628 pub fn look_at_lh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
629 glam_assert!(up.is_normalized());
630 Self::look_to_lh(eye, center - eye, up)
631 }
632
633 /// Creates a right-handed view transform using a camera position, an up direction, and a focal
634 /// point.
635 /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
636 ///
637 /// # Panics
638 ///
639 /// Will panic if `up` is not normalized when `glam_assert` is enabled.
640 #[inline]
641 #[must_use]
642 pub fn look_at_rh(eye: {{ vec3_t }}, center: {{ vec3_t }}, up: {{ vec3_t }}) -> Self {
643 glam_assert!(up.is_normalized());
644 Self::look_to_rh(eye, center - eye, up)
645 }
646
647 /// Transforms the given 3D points, applying shear, scale, rotation and translation.
648 #[inline]
649 pub fn transform_point3(&self, rhs: {{ vec3_t }}) -> {{ vec3_t }} {
650 #[allow(clippy::useless_conversion)]
651 ((self.matrix3.x_axis * rhs.x)
652 + (self.matrix3.y_axis * rhs.y)
653 + (self.matrix3.z_axis * rhs.z)
654 + self.translation)
655 .into()
656 }
657
658 /// Transforms the given 3D vector, applying shear, scale and rotation (but NOT
659 /// translation).
660 ///
661 /// To also apply translation, use [`Self::transform_point3()`] instead.
662 #[inline]
663 #[must_use]
664 pub fn transform_vector3(&self, rhs: {{ vec3_t }}) -> {{ vec3_t }} {
665 #[allow(clippy::useless_conversion)]
666 ((self.matrix3.x_axis * rhs.x)
667 + (self.matrix3.y_axis * rhs.y)
668 + (self.matrix3.z_axis * rhs.z))
669 .into()
670 }
671 {% endif %}
672
673 {% if self_t == "Affine3A" %}
674 /// Transforms the given [`Vec3A`], applying shear, scale, rotation and translation.
675 #[inline]
676 #[must_use]
677 pub fn transform_point3a(&self, rhs: Vec3A) -> Vec3A {
678 self.matrix3 * rhs + self.translation
679 }
680
681 /// Transforms the given [`Vec3A`], applying shear, scale and rotation (but NOT
682 /// translation).
683 ///
684 /// To also apply translation, use [`Self::transform_point3a()`] instead.
685 #[inline]
686 #[must_use]
687 pub fn transform_vector3a(&self, rhs: Vec3A) -> Vec3A {
688 self.matrix3 * rhs
689 }
690 {% endif %}
691
692 /// Returns `true` if, and only if, all elements are finite.
693 ///
694 /// If any element is either `NaN`, positive or negative infinity, this will return
695 /// `false`.
696 #[inline]
697 #[must_use]
698 pub fn is_finite(&self) -> bool {
699 self.matrix{{ dim }}.is_finite() && self.translation.is_finite()
700 }
701
702 /// Returns `true` if any elements are `NaN`.
703 #[inline]
704 #[must_use]
705 pub fn is_nan(&self) -> bool {
706 self.matrix{{ dim }}.is_nan() || self.translation.is_nan()
707 }
708
709 /// Returns true if the absolute difference of all elements between `self` and `rhs`
710 /// is less than or equal to `max_abs_diff`.
711 ///
712 /// This can be used to compare if two 3x4 matrices contain similar elements. It works
713 /// best when comparing with a known value. The `max_abs_diff` that should be used used
714 /// depends on the values being compared against.
715 ///
716 /// For more see
717 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
718 #[inline]
719 #[must_use]
720 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: {{ scalar_t }}) -> bool {
721 self.matrix{{ dim }}.abs_diff_eq(rhs.matrix{{ dim }}, max_abs_diff)
722 && self
723 .translation
724 .abs_diff_eq(rhs.translation, max_abs_diff)
725 }
726
727 /// Return the inverse of this transform.
728 ///
729 /// Note that if the transform is not invertible the result will be invalid.
730 #[inline]
731 #[must_use]
732 pub fn inverse(&self) -> Self {
733 let matrix{{ dim }} = self.matrix{{ dim }}.inverse();
734 // transform negative translation by the matrix inverse:
735 let translation = -(matrix{{ dim }} * self.translation);
736
737 Self {
738 matrix{{ dim }},
739 translation,
740 }
741 }
742
743 {% if scalar_t == "f64" %}
744 {% if dim == 2 %}
745 /// Casts all elements of `self` to `f32`.
746 #[inline]
747 #[must_use]
748 pub fn as_affine2(&self) -> crate::Affine2 {
749 crate::Affine2::from_mat2_translation(self.matrix2.as_mat2(), self.translation.as_vec2())
750 }
751 {% elif dim == 3 %}
752 /// Casts all elements of `self` to `f32`.
753 #[inline]
754 #[must_use]
755 pub fn as_affine3a(&self) -> crate::Affine3A {
756 crate::Affine3A::from_mat3_translation(self.matrix3.as_mat3(), self.translation.as_vec3())
757 }
758 {% endif %}
759 {% endif %}
760
761 {% if scalar_t == "f32" %}
762 {% if dim == 2 %}
763 /// Casts all elements of `self` to `f64`.
764 #[inline]
765 #[must_use]
766 pub fn as_daffine2(&self) -> crate::DAffine2 {
767 crate::DAffine2::from_mat2_translation(self.matrix2.as_dmat2(), self.translation.as_dvec2())
768 }
769 {% elif dim == 3 %}
770 /// Casts all elements of `self` to `f64`.
771 #[inline]
772 #[must_use]
773 pub fn as_daffine3(&self) -> crate::DAffine3 {
774 crate::DAffine3::from_mat3_translation(self.matrix3.as_dmat3(), self.translation.as_dvec3())
775 }
776 {% endif %}
777 {% endif %}
778 }
779
780 impl Default for {{ self_t }} {
781 #[inline(always)]
782 fn default() -> Self {
783 Self::IDENTITY
784 }
785 }
786
787 impl Deref for {{ self_t }} {
788 type Target = crate::deref::Cols{{ dim + 1 }}<{{ col_t }}>;
789 #[inline(always)]
790 fn deref(&self) -> &Self::Target {
791 unsafe { &*(self as *const Self as *const Self::Target) }
792 }
793 }
794
795 impl DerefMut for {{ self_t }} {
796 #[inline(always)]
797 fn deref_mut(&mut self) -> &mut Self::Target {
798 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
799 }
800 }
801
802 impl PartialEq for {{ self_t }} {
803 #[inline]
804 fn eq(&self, rhs: &Self) -> bool {
805 self.matrix{{ dim }}.eq(&rhs.matrix{{ dim }}) && self.translation.eq(&rhs.translation)
806 }
807 }
808
809 impl core::fmt::Debug for {{ self_t }} {
810 fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
811 fmt.debug_struct(stringify!({{ self_t }}))
812 .field("matrix{{ dim }}", &self.matrix{{ dim }})
813 .field("translation", &self.translation)
814 .finish()
815 }
816 }
817
818 impl core::fmt::Display for {{ self_t }} {
819 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
820 if let Some(p) = f.precision() {
821 {% if dim == 2 %}
822 write!(f,
823 "[{:.*}, {:.*}, {:.*}]",
824 p, self.matrix2.x_axis, p, self.matrix2.y_axis, p, self.translation)
825 {% elif dim == 3 %}
826 write!(f,
827 "[{:.*}, {:.*}, {:.*}, {:.*}]",
828 p, self.matrix3.x_axis, p, self.matrix3.y_axis, p, self.matrix3.z_axis, p, self.translation)
829 {% endif %}
830 } else {
831 {% if dim == 2 %}
832 write!(f, "[{}, {}, {}]", self.matrix2.x_axis, self.matrix2.y_axis, self.translation)
833 {% elif dim == 3 %}
834 write!(
835 f,
836 "[{}, {}, {}, {}]",
837 self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
838 )
839 {% endif %}
840 }
841 }
842 }
843
844 impl<'a> core::iter::Product<&'a Self> for {{ self_t }} {
845 fn product<I>(iter: I) -> Self
846 where
847 I: Iterator<Item = &'a Self>,
848 {
849 iter.fold(Self::IDENTITY, |a, &b| a * b)
850 }
851 }
852
853 impl Mul for {{ self_t }} {
854 type Output = Self;
855
856 #[inline]
857 fn mul(self, rhs: Self) -> Self {
858 Self {
859 matrix{{ dim }}: self.matrix{{ dim }} * rhs.matrix{{ dim }},
860 translation: self.matrix{{ dim }} * rhs.translation + self.translation,
861 }
862 }
863 }
864
865 {{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t=self_t, output_t=self_t) }}
866
867 {{ macros::impl_op_assign(imp="Mul", self_t=self_t, rhs_t=self_t) }}
868
869 {% if dim == 2 %}
870 impl From<{{ self_t }}> for {{ mat3_t }} {
871 #[inline]
872 fn from(m: {{ self_t }}) -> Self {
873 Self::from_cols(
874 m.matrix2.x_axis.extend(0.0),
875 m.matrix2.y_axis.extend(0.0),
876 m.translation.extend(1.0),
877 )
878 }
879 }
880
881 impl Mul<{{ mat3_t }}> for {{ self_t }} {
882 type Output = {{ mat3_t }};
883
884 #[inline]
885 fn mul(self, rhs: {{ mat3_t }}) -> Self::Output {
886 {{ mat3_t }}::from(self) * rhs
887 }
888 }
889
890 {{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t=mat3_t, output_t=mat3_t) }}
891
892 impl Mul<{{ self_t }}> for {{ mat3_t }} {
893 type Output = Self;
894
895 #[inline]
896 fn mul(self, rhs: {{ self_t }}) -> Self {
897 self * Self::from(rhs)
898 }
899 }
900
901 {{ macros::impl_ref_binop(imp="Mul", self_t=mat3_t, rhs_t=self_t, output_t=mat3_t) }}
902
903 {{ macros::impl_op_assign(imp="Mul", self_t=mat3_t, rhs_t=self_t) }}
904
905 {% elif dim == 3 %}
906 impl Mul<{{ mat4_t }}> for {{ self_t }} {
907 type Output = {{ mat4_t }};
908
909 #[inline]
910 fn mul(self, rhs: {{ mat4_t }}) -> Self::Output {
911 {{ mat4_t }}::from(self) * rhs
912 }
913 }
914
915 {{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t=mat4_t, output_t=mat4_t) }}
916
917 impl Mul<{{ self_t }}> for {{ mat4_t }} {
918 type Output = Self;
919
920 #[inline]
921 fn mul(self, rhs: {{ self_t }}) -> Self {
922 self * Self::from(rhs)
923 }
924 }
925
926 {{ macros::impl_ref_binop(imp="Mul", self_t=mat4_t, rhs_t=self_t, output_t=mat4_t) }}
927
928 {{ macros::impl_op_assign(imp="Mul", self_t=mat4_t, rhs_t=self_t) }}
929
930
931 impl From<{{ self_t }}> for {{ mat4_t }} {
932 #[inline]
933 fn from(m: {{ self_t }}) -> Self {
934 Self::from_cols(
935 m.matrix3.x_axis.extend(0.0),
936 m.matrix3.y_axis.extend(0.0),
937 m.matrix3.z_axis.extend(0.0),
938 m.translation.extend(1.0),
939 )
940 }
941 }
942
943 {% endif %}
944
945 {% if self_t == "Affine2" %}
946 impl Mul<Mat3A> for Affine2 {
947 type Output = Mat3A;
948
949 #[inline]
950 fn mul(self, rhs: Mat3A) -> Self::Output {
951 Mat3A::from(self) * rhs
952 }
953 }
954
955 {{ macros::impl_ref_binop(imp="Mul", self_t=self_t, rhs_t="Mat3A", output_t="Mat3A") }}
956
957 impl Mul<Affine2> for Mat3A {
958 type Output = Self;
959
960 #[inline]
961 fn mul(self, rhs: Affine2) -> Self {
962 self * Self::from(rhs)
963 }
964 }
965
966 {{ macros::impl_ref_binop(imp="Mul", self_t="Mat3A", rhs_t=self_t, output_t="Mat3A") }}
967
968 {{ macros::impl_op_assign(imp="Mul", self_t="Mat3A", rhs_t=self_t) }}
969
970 impl From<Affine2> for Mat3A {
971 #[inline]
972 fn from(m: Affine2) -> Self {
973 Self::from_cols(
974 Vec3A::from((m.matrix2.x_axis, 0.0)),
975 Vec3A::from((m.matrix2.y_axis, 0.0)),
976 Vec3A::from((m.translation, 1.0)),
977 )
978 }
979 }
980 {% endif %}