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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use glam::Affine3A;
use glam::Mat4;
use glam::Vec3A;

use crate::IsoTransform;
use crate::Quat;
use crate::Vec3;
use crate::Vec3Ext;
use crate::Vec4;
use crate::Vec4Swizzles;

/// Represents a transform with translation + rotation + uniform scale.
/// Preserves local angles.
/// Scale and rotation will be applied first, then translation.
#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "with_serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "with_speedy", derive(speedy::Writable, speedy::Readable))]
pub struct Conformal3 {
    translation_and_scale: Vec4,
    rotation: Quat,
}

impl Conformal3 {
    /// The identity transform: doesn't transform at all. Like multiplying with `1`.
    pub const IDENTITY: Self = Self {
        translation_and_scale: Vec4::W,
        rotation: Quat::IDENTITY,
    };

    #[inline]
    /// A transform that first rotates and scales around the origin and then moves all points by a set amount.
    ///
    /// Equivalent to `Conformal3::from_translation(translation) * (Conformal3::from_scale(rotation) * Conformal3::from_quat(scale))`.
    ///
    /// The given rotation should be normalized.
    pub fn from_scale_rotation_translation(scale: f32, rotation: Quat, translation: Vec3) -> Self {
        Self {
            translation_and_scale: translation.extend(scale),
            rotation,
        }
    }

    /// A transform that first rotates around the origin and then moves all points by a set amount.
    ///
    /// Equivalent to `Conformal3::from_translation(translation) * Conformal3::from_quat(rotation)`.
    ///
    /// The given rotation should be normalized.
    #[inline]
    pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self {
        Self {
            translation_and_scale: translation.extend(1.0),
            rotation,
        }
    }

    /// A pure translation without any rotation or scale.
    #[inline]
    pub fn from_translation(translation: Vec3) -> Self {
        Self {
            translation_and_scale: translation.extend(1.0),
            rotation: Quat::IDENTITY,
        }
    }

    /// Returns this transform decomposed into scale, rotation, translation
    #[inline]
    pub fn to_scale_rotation_translation(&self) -> (f32, Quat, Vec3) {
        (self.scale(), self.rotation(), self.translation())
    }

    /// A pure rotation without any translation or scale.
    #[inline]
    pub fn from_quat(rotation: Quat) -> Self {
        Self::from_scale_rotation_translation(1.0, rotation, Vec3::ZERO)
    }

    /// A pure scale without any translation or rotation.
    #[inline]
    pub fn from_scale(scale: f32) -> Self {
        Self::from_scale_rotation_translation(scale, Quat::IDENTITY, Vec3::ZERO)
    }

    /// Returns the inverse of this transform. `my_transform * my_transform.inverse() = Conformal3::IDENITTY`
    #[inline]
    pub fn inverse(&self) -> Self {
        let inv_scale = self.inv_scale();
        let inv_rotation = self.rotation.inverse();
        let inv_translation = inv_scale * (inv_rotation * -self.translation());
        Self::from_scale_rotation_translation(inv_scale, inv_rotation, inv_translation)
    }

    /// Returns self normalized.
    /// You generally don't need to call this unless you've multiplied A LOT of `Conformal3`.
    #[inline]
    #[must_use]
    pub fn normalize(&self) -> Self {
        let scale = self.scale();
        let rotation = self.rotation().normalize();
        let translation = self.translation();
        Self::from_scale_rotation_translation(scale, rotation, translation)
    }

    /// Will attempt to create a `Conformal3` from an `Affine3A`. Assumes no shearing and uniform scaling.
    /// If the affine transform contains shearing or non-uniform scaling it will be lost.
    #[inline]
    pub fn from_affine3a_lossy(transform: &crate::Affine3A) -> Self {
        let (scale, rotation, translation) = transform.to_scale_rotation_translation();
        Self {
            translation_and_scale: translation.extend(scale.mean()),
            rotation: rotation.normalize(),
        }
    }

    /// Returns this transform as an `Affine3A`
    #[inline]
    pub fn to_affine3a(&self) -> Affine3A {
        Affine3A::from_scale_rotation_translation(
            Vec3::splat(self.scale()),
            self.rotation(),
            self.translation(),
        )
    }

    /// Returns this transform as a `Mat4`
    #[inline]
    pub fn to_mat4(self) -> Mat4 {
        Mat4::from_scale_rotation_translation(
            Vec3::splat(self.scale()),
            self.rotation(),
            self.translation(),
        )
    }

    /// Transform a `Vec3` using translation, rotation, scale.
    #[inline]
    pub fn transform_point3(&self, value: Vec3) -> Vec3 {
        self.translation() + self.scale() * (self.rotation() * value)
    }

    /// Transform a `Vec3A` using translation, rotation, scale.
    #[inline]
    pub fn transform_point3a(&self, value: Vec3A) -> Vec3A {
        Vec3A::from(self.translation()) + self.scale() * (self.rotation() * value)
    }

    /// Transform a `Vec3` using only rotation and scale.
    #[inline]
    pub fn transform_vector3(&self, value: Vec3) -> Vec3 {
        self.scale() * (self.rotation() * value)
    }

    /// Transform a `Vec3A` using only rotation and scale.
    #[inline]
    pub fn transform_vector3a(&self, value: Vec3A) -> Vec3A {
        self.scale() * (self.rotation() * value)
    }

    /// Returns the rotation
    #[inline]
    pub fn rotation(&self) -> Quat {
        self.rotation
    }

    /// Sets the rotation
    #[inline]
    pub fn set_rotation(&mut self, rotation: Quat) {
        self.rotation = rotation;
    }

    /// Returns the translation
    #[inline]
    pub fn translation(&self) -> Vec3 {
        self.translation_and_scale.xyz()
    }

    /// Returns the translation and scale as a `Vec4`
    #[inline]
    pub fn translation_and_scale(&self) -> Vec4 {
        self.translation_and_scale
    }

    /// Sets the translation
    #[inline]
    pub fn set_translation(&mut self, translation: Vec3) {
        let scale = self.scale();
        self.translation_and_scale = translation.extend(scale);
    }

    /// Returns the scale
    #[inline]
    pub fn scale(&self) -> f32 {
        self.translation_and_scale.w
    }

    /// Returns the scale inverse
    #[inline]
    pub fn inv_scale(&self) -> f32 {
        if self.scale() == 0.0 {
            f32::INFINITY
        } else {
            1.0 / self.scale()
        }
    }

    /// Sets the scale
    #[inline]
    pub fn set_scale(&mut self, scale: f32) {
        self.translation_and_scale.w = scale;
    }

    /// Builds a `Conformal3` from an `IsoTransform` (rotation, translation).
    #[inline]
    pub fn from_iso_transform(t: IsoTransform) -> Self {
        Self::from_rotation_translation(t.rotation(), t.translation())
    }

    /// Creates a right-handed view transform using a camera position,
    /// a point to look at, and an up direction.
    ///
    /// The result transforms from world coordinates to view coordinates.
    ///
    /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
    ///
    /// Will return [`None`] if any argument is zero, non-finite, or if forward and up are colinear.
    #[cfg(not(target_arch = "spirv"))] // TODO: large Options in rust-gpu
    #[inline]
    pub fn look_at_rh(eye: Vec3, target: Vec3, up: Vec3) -> Option<Self> {
        IsoTransform::look_at_rh(eye, target, up).map(Self::from_iso_transform)
    }

    /// Returns `true` if, and only if, all components are finite.
    ///
    /// If any component is either `NaN`, positive or negative infinity, this will return `false`.
    pub fn is_finite(&self) -> bool {
        self.translation_and_scale.is_finite() && self.rotation.is_finite()
    }
}

impl core::ops::Mul for &Conformal3 {
    type Output = Conformal3;

    #[inline]
    fn mul(self, rhs: &Conformal3) -> Conformal3 {
        let translation = self.transform_point3(rhs.translation());
        let rotation = self.rotation() * rhs.rotation();
        let scale = self.scale() * rhs.scale();
        Conformal3::from_scale_rotation_translation(scale, rotation, translation)
    }
}

impl core::ops::Mul<Conformal3> for &Conformal3 {
    type Output = Conformal3;

    #[inline]
    fn mul(self, rhs: Conformal3) -> Conformal3 {
        self.mul(&rhs)
    }
}

impl core::ops::Mul for Conformal3 {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: Self) -> Self {
        (&self).mul(&rhs)
    }
}

impl core::ops::Mul<Conformal3> for IsoTransform {
    type Output = Conformal3;

    #[inline]
    fn mul(self, rhs: Conformal3) -> Conformal3 {
        Conformal3::from_iso_transform(self).mul(rhs)
    }
}

impl core::ops::Mul<IsoTransform> for Conformal3 {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: IsoTransform) -> Self {
        self.mul(Self::from_iso_transform(rhs))
    }
}

/// Identity transform
impl Default for Conformal3 {
    /// Identity transform
    #[inline]
    fn default() -> Self {
        Self::IDENTITY
    }
}

impl From<Conformal3> for Mat4 {
    #[inline]
    fn from(c: Conformal3) -> Self {
        c.to_mat4()
    }
}

impl From<Conformal3> for crate::Affine3A {
    #[inline]
    fn from(c: Conformal3) -> Self {
        c.to_affine3a()
    }
}

impl From<IsoTransform> for Conformal3 {
    #[inline]
    fn from(c: IsoTransform) -> Self {
        Self::from_iso_transform(c)
    }
}

#[cfg(feature = "std")]
impl core::fmt::Debug for Conformal3 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let (axis, angle) = self.rotation().to_axis_angle();
        let translation = self.translation();
        let scale = self.scale();
        f.debug_struct("Conformal3")
            .field(
                "translation",
                &format!("[{} {} {}]", translation[0], translation[1], translation[2]),
            )
            .field(
                "rotation",
                &format!(
                    "{:.1}° around [{} {} {}]",
                    angle.to_degrees(),
                    axis[0],
                    axis[1],
                    axis[2],
                ),
            )
            .field("scale", &format!("{}", scale))
            .finish()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn approx_eq_transform(a: Conformal3, b: Conformal3) -> bool {
        let max_abs_diff = 1e-6;
        a.translation().abs_diff_eq(b.translation(), max_abs_diff)
            && a.rotation().abs_diff_eq(b.rotation(), max_abs_diff)
            && ((a.scale() - b.scale()).abs() < max_abs_diff)
    }

    macro_rules! assert_approx_eq_transform {
        ($a: expr, $b: expr) => {
            assert!(approx_eq_transform($a, $b), "{:#?} != {:#?}", $a, $b,);
        };
    }

    #[test]
    fn test_inverse() {
        use crate::Conformal3;

        let transform = Conformal3::from_scale_rotation_translation(
            2.0,
            Quat::from_rotation_y(std::f32::consts::PI),
            Vec3::ONE,
        );
        let identity = transform * transform.inverse();
        assert_approx_eq_transform!(identity, Conformal3::IDENTITY);

        let transform = Conformal3::from_scale_rotation_translation(
            10.0,
            Quat::from_axis_angle(Vec3::ONE.normalize(), 1.234),
            Vec3::new(1.0, 2.0, 3.0),
        );
        let identity = transform * transform.inverse();
        assert_approx_eq_transform!(identity, Conformal3::IDENTITY);
    }
}