Skip to main content

trackball/
rigid.rs

1use core::ops::{Mul, MulAssign};
2use glam::{Mat4, Quat, Vec3};
3
4/// Rigid transformation as rotation followed by translation, i.e., a direct isometry.
5///
6/// Implements [`Default`] and can be created with `Rigid::default()` returning [`Self::IDENTITY`].
7#[derive(Debug, Clone, Copy, PartialEq, Default)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(
10    feature = "rkyv-08",
11    derive(rkyv_08::Archive, rkyv_08::Serialize, rkyv_08::Deserialize),
12    rkyv(crate = rkyv_08)
13)]
14#[repr(C)]
15pub struct Rigid {
16    /// Rotational part.
17    pub rotation: Quat,
18    /// Translational part.
19    pub translation: Vec3,
20}
21
22impl Rigid {
23    /// Identity transformation.
24    pub const IDENTITY: Self = Self {
25        rotation: Quat::IDENTITY,
26        translation: Vec3::ZERO,
27    };
28
29    /// Rigid transformation from `rotation` followed by `translation`.
30    #[must_use]
31    pub const fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self {
32        Self {
33            rotation,
34            translation,
35        }
36    }
37    /// Inverse transformation.
38    #[must_use]
39    pub fn inverse(&self) -> Self {
40        let rotation = self.rotation.inverse();
41        Self {
42            rotation,
43            translation: rotation * -self.translation,
44        }
45    }
46    /// Transforms point.
47    #[must_use]
48    pub fn transform_point(&self, point: Vec3) -> Vec3 {
49        self.rotation * point + self.translation
50    }
51    /// Transforms vector, i.e., rotation only.
52    #[must_use]
53    pub fn transform_vector(&self, vector: Vec3) -> Vec3 {
54        self.rotation * vector
55    }
56    /// Homogeneous transformation matrix.
57    #[must_use]
58    pub fn to_mat4(&self) -> Mat4 {
59        Mat4::from_rotation_translation(self.rotation, self.translation)
60    }
61    /// Whether all components are approximately equal within absolute difference `max_abs_diff`.
62    #[must_use]
63    pub fn abs_diff_eq(&self, other: Self, max_abs_diff: f32) -> bool {
64        self.rotation.abs_diff_eq(other.rotation, max_abs_diff)
65            && self
66                .translation
67                .abs_diff_eq(other.translation, max_abs_diff)
68    }
69}
70
71impl From<Rigid> for Mat4 {
72    fn from(rigid: Rigid) -> Self {
73        rigid.to_mat4()
74    }
75}
76
77impl Mul for Rigid {
78    type Output = Self;
79
80    fn mul(self, rhs: Self) -> Self {
81        Self {
82            rotation: self.rotation * rhs.rotation,
83            translation: self.transform_point(rhs.translation),
84        }
85    }
86}
87
88impl MulAssign for Rigid {
89    fn mul_assign(&mut self, rhs: Self) {
90        *self = *self * rhs;
91    }
92}
93
94impl Mul<Vec3> for Rigid {
95    type Output = Vec3;
96
97    /// Transforms point, see [`Rigid::transform_point()`].
98    fn mul(self, rhs: Vec3) -> Vec3 {
99        self.transform_point(rhs)
100    }
101}