1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use super::{Scale3D, Transform3D};

use crate::datatypes::{
    Rotation3D, RotationAxisAngle, TranslationAndMat3x3, TranslationRotationScale3D, Vec3D,
};

impl Transform3D {
    pub const IDENTITY: Self = Self::TranslationRotationScale(TranslationRotationScale3D::IDENTITY);

    /// From a translation.
    #[inline]
    pub fn from_translation(translation: impl Into<Vec3D>) -> Self {
        Self::from(TranslationRotationScale3D::from_translation(translation))
    }

    /// From a translation applied after a rotation, known as a rigid transformation.
    #[inline]
    pub fn from_translation_rotation(
        translation: impl Into<Vec3D>,
        rotation: impl Into<Rotation3D>,
    ) -> Self {
        Self::from(TranslationRotationScale3D::from_translation_rotation(
            translation,
            rotation,
        ))
    }

    /// From a translation applied after a 3x3 matrix.
    #[inline]
    pub fn from_translation_mat3x3(
        translation: impl Into<Vec3D>,
        mat3x3: impl Into<crate::datatypes::Mat3x3>,
    ) -> Self {
        Self::from(TranslationAndMat3x3::new(translation, mat3x3))
    }

    /// From a translation, applied after a rotation & scale, known as an affine transformation.
    #[inline]
    pub fn from_translation_rotation_scale(
        translation: impl Into<Vec3D>,
        rotation: impl Into<Rotation3D>,
        scale: impl Into<Scale3D>,
    ) -> Self {
        Self::from(TranslationRotationScale3D::from_translation_rotation_scale(
            translation,
            rotation,
            scale,
        ))
    }

    /// From a rotation & scale
    #[inline]
    pub fn from_rotation_scale(rotation: impl Into<Rotation3D>, scale: impl Into<Scale3D>) -> Self {
        Self::from(TranslationRotationScale3D::from_rotation_scale(
            rotation, scale,
        ))
    }

    /// Indicate that this transform is from parent to child.
    /// This is the oppositve of the default, which is from child to parent.
    #[inline]
    #[allow(clippy::wrong_self_convention)]
    pub fn from_parent(mut self) -> Self {
        match &mut self {
            Self::TranslationAndMat3x3(t) => {
                t.from_parent = true;
            }
            Self::TranslationRotationScale(t) => {
                t.from_parent = true;
            }
        }
        self
    }

    /// Indicates whether this transform is from parent to child.
    /// This is the oppositve of the default, which is from child to parent.
    #[inline]
    #[allow(clippy::wrong_self_convention)]
    pub fn is_from_parent(&self) -> bool {
        match self {
            Self::TranslationAndMat3x3(t) => t.from_parent,
            Self::TranslationRotationScale(t) => t.from_parent,
        }
    }
}

impl Default for Transform3D {
    #[inline]
    fn default() -> Self {
        Self::IDENTITY
    }
}

impl From<TranslationAndMat3x3> for Transform3D {
    #[inline]
    fn from(v: TranslationAndMat3x3) -> Self {
        Self::TranslationAndMat3x3(v)
    }
}

impl From<TranslationRotationScale3D> for Transform3D {
    #[inline]
    fn from(v: TranslationRotationScale3D) -> Self {
        Self::TranslationRotationScale(v)
    }
}

impl From<Vec3D> for Transform3D {
    #[inline]
    fn from(v: Vec3D) -> Self {
        Self::TranslationRotationScale(v.into())
    }
}

#[cfg(feature = "glam")]
impl From<glam::Vec3> for Transform3D {
    #[inline]
    fn from(v: glam::Vec3) -> Self {
        Self::TranslationRotationScale(v.into())
    }
}

impl From<RotationAxisAngle> for Transform3D {
    #[inline]
    fn from(v: RotationAxisAngle) -> Self {
        let rotation = Rotation3D::from(v);
        Self::TranslationRotationScale(rotation.into())
    }
}

#[cfg(feature = "glam")]
impl From<Transform3D> for glam::Affine3A {
    fn from(value: Transform3D) -> Self {
        match value {
            Transform3D::TranslationAndMat3x3(TranslationAndMat3x3 {
                translation,
                mat3x3,
                from_parent: _,
            }) => glam::Affine3A::from_mat3_translation(
                mat3x3.unwrap_or(super::Mat3x3::IDENTITY).into(),
                translation.map_or(glam::Vec3::ZERO, |v| v.into()),
            ),

            Transform3D::TranslationRotationScale(TranslationRotationScale3D {
                translation,
                rotation,
                scale,
                from_parent: _,
            }) => glam::Affine3A::from_scale_rotation_translation(
                scale.map_or(glam::Vec3::ONE, |s| s.into()),
                rotation.map_or(glam::Quat::IDENTITY, |q| q.into()),
                translation.map_or(glam::Vec3::ZERO, |v| v.into()),
            ),
        }
    }
}