glam_ext/f64_ext/
transform3a.rs

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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use core::ops::{Mul, MulAssign};
use glam::{DAffine3, DMat3, DMat4, DQuat, DVec3};

use crate::macros::glam_assert;

#[repr(C)]
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct DTransform3 {
    pub translation: DVec3,
    pub rotation: DQuat,
    pub scale: DVec3,
}

impl DTransform3 {
    /// The degenerate zero transform.
    ///
    /// This transforms any finite vector and point to zero.
    /// The zero transform is non-invertible.
    pub const ZERO: Self = Self {
        translation: DVec3::ZERO,
        rotation: DQuat::IDENTITY,
        scale: DVec3::ONE,
    };

    /// The identity transform.
    ///
    /// Multiplying a vector with this returns the same vector.
    pub const IDENTITY: Self = Self {
        translation: DVec3::ZERO,
        rotation: DQuat::IDENTITY,
        scale: DVec3::ONE,
    };

    /// All NAN:s.
    pub const NAN: Self = Self {
        translation: DVec3::NAN,
        rotation: DQuat::NAN,
        scale: DVec3::NAN,
    };

    /// Creates a new transform.
    #[inline]
    #[must_use]
    pub fn new(translation: DVec3, rotation: DQuat, scale: DVec3) -> Self {
        Self {
            translation,
            rotation,
            scale,
        }
    }

    /// Creates an affine transform that changes scale.
    /// Note that if any scale is zero the transform will be non-invertible.
    #[inline]
    #[must_use]
    pub fn from_scale(scale: DVec3) -> Self {
        Self {
            translation: DVec3::ZERO,
            rotation: DQuat::IDENTITY,
            scale,
        }
    }

    /// Creates a transform transform from the given `rotation` quaternion.
    #[inline]
    #[must_use]
    pub fn from_quat(rotation: DQuat) -> Self {
        Self {
            translation: DVec3::ZERO,
            rotation,
            scale: DVec3::ONE,
        }
    }

    /// Creates a transform transform containing a 3D rotation around a normalized
    /// rotation `axis` of `angle` (in radians).
    #[inline]
    #[must_use]
    pub fn from_axis_angle(axis: DVec3, angle: f64) -> Self {
        Self {
            translation: DVec3::ZERO,
            rotation: DQuat::from_axis_angle(axis, angle),
            scale: DVec3::ONE,
        }
    }

    /// Creates a transform transform containing a 3D rotation around the x axis of
    /// `angle` (in radians).
    #[inline]
    #[must_use]
    pub fn from_rotation_x(angle: f64) -> Self {
        Self {
            translation: DVec3::ZERO,
            rotation: DQuat::from_rotation_x(angle),
            scale: DVec3::ONE,
        }
    }

    /// Creates a transform transform containing a 3D rotation around the y axis of
    /// `angle` (in radians).
    #[inline]
    #[must_use]
    pub fn from_rotation_y(angle: f64) -> Self {
        Self {
            translation: DVec3::ZERO,
            rotation: DQuat::from_rotation_y(angle),
            scale: DVec3::ONE,
        }
    }

    /// Creates a transform transform containing a 3D rotation around the z axis of
    /// `angle` (in radians).
    #[inline]
    #[must_use]
    pub fn from_rotation_z(angle: f64) -> Self {
        Self {
            translation: DVec3::ZERO,
            rotation: DQuat::from_rotation_z(angle),
            scale: DVec3::ONE,
        }
    }

    /// Creates a transform transformation from the given 3D `translation`.
    #[inline]
    #[must_use]
    pub fn from_translation(translation: DVec3) -> Self {
        Self {
            translation,
            rotation: DQuat::IDENTITY,
            scale: DVec3::ONE,
        }
    }

    /// Creates a transform from the given 3D `rotation` and `translation`.
    #[inline]
    #[must_use]
    pub fn from_rotation_translation(rotation: DQuat, translation: DVec3) -> Self {
        Self {
            translation,
            rotation,
            scale: DVec3::ONE,
        }
    }

    /// Creates a transform from the given 3D `scale`, `rotation` and `translation`.
    #[inline]
    #[must_use]
    pub fn from_scale_rotation_translation(scale: DVec3, rotation: DQuat, translation: DVec3) -> Self {
        Self {
            translation,
            rotation,
            scale,
        }
    }

    /// Creates a transform from a 3x3 matrix (expressing scale and rotation)
    ///
    /// Note if the input matrix is non-uniform or shear, the result transform will be ill-defined.
    #[inline]
    #[must_use]
    pub fn from_mat3(mat3: DMat3) -> Self {
        Self::from_mat3_translation(mat3, DVec3::ZERO)
    }

    /// Creates a transform from a 3x3 matrix (expressing scale and rotation)
    ///
    /// Note if the input matrix is non-uniform or shear, the result transform will be ill-defined.
    #[inline]
    #[must_use]
    pub fn from_mat3_translation(mat3: DMat3, translation: DVec3) -> Self {
        use super::math;
        let det = mat3.determinant();
        glam_assert!(det != 0.0);

        let scale = DVec3::new(
            mat3.x_axis.length() * math::signum(det),
            mat3.y_axis.length(),
            mat3.z_axis.length(),
        );

        glam_assert!(scale.cmpne(DVec3::ZERO).all());

        let inv_scale = scale.recip();

        let rotation = DQuat::from_mat3(&DMat3::from_cols(
            mat3.x_axis * inv_scale.x,
            mat3.y_axis * inv_scale.y,
            mat3.z_axis * inv_scale.z,
        ));
        Self {
            translation,
            rotation,
            scale,
        }
    }

    /// Creates a transform from a 4x4 matrix.
    ///
    /// Note if the input matrix is non-uniform or shear, the result transform will be ill-defined.
    #[inline]
    #[must_use]
    pub fn from_mat4(mat4: DMat4) -> Self {
        let translation = mat4.w_axis.truncate();
        Self::from_mat3_translation(DMat3::from_mat4(mat4), translation)
    }

    /// Extracts `scale`, `rotation` and `translation` from `self`.
    #[inline]
    #[must_use]
    pub fn to_scale_rotation_translation(&self) -> (DVec3, DQuat, DVec3) {
        (self.scale, self.rotation, self.translation)
    }

    /// Transforms the given 3D points, applying scale, rotation and translation.
    #[inline]
    #[must_use]
    pub fn transform_point3(&self, rhs: DVec3) -> DVec3 {
        let scale: DVec3 = self.scale;
        let translation: DVec3 = self.translation;
        self.rotation * (rhs * scale) + translation
    }

    /// Transforms the given 3D vector, applying scale and rotation (but NOT translation).
    #[inline]
    #[must_use]
    pub fn transform_vector3(&self, rhs: DVec3) -> DVec3 {
        let scale: DVec3 = self.scale;
        self.rotation * (rhs * scale)
    }

    /// Returns `true` if, and only if, all elements are finite.
    ///
    /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
    #[inline]
    #[must_use]
    pub fn is_finite(&self) -> bool {
        self.translation.is_finite() && self.rotation.is_finite() && self.scale.is_finite()
    }

    /// Returns `true` if any elements are `NaN`.
    #[inline]
    #[must_use]
    pub fn is_nan(&self) -> bool {
        self.translation.is_nan() && self.rotation.is_nan() && self.scale.is_nan()
    }

    /// Returns true if the absolute difference of all elements between `self` and `rhs`
    /// is less than or equal to `max_abs_diff`.
    #[inline]
    #[must_use]
    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
        self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
            && self.rotation.abs_diff_eq(rhs.rotation, max_abs_diff)
            && self.scale.abs_diff_eq(rhs.scale, max_abs_diff)
    }

    /// Return the inverse of this transform.
    ///
    /// Note that if the transform is not invertible the result will be invalid.
    #[inline]
    #[must_use]
    pub fn inverse(&self) -> Self {
        let rot = DMat3::from_quat(self.rotation);
        let mat_inv = DMat3::from_cols(
            rot.x_axis * self.scale.x,
            rot.y_axis * self.scale.y,
            rot.z_axis * self.scale.z,
        )
        .inverse();
        let translation = -(mat_inv * self.translation);
        DTransform3::from_mat3_translation(mat_inv, translation)
    }
}

impl From<DTransform3> for DMat4 {
    #[inline]
    fn from(t: DTransform3) -> DMat4 {
        let mat3 = DMat3::from_quat(t.rotation);
        DMat4::from_cols(
            (mat3.x_axis * t.scale.x).extend(0.0),
            (mat3.y_axis * t.scale.y).extend(0.0),
            (mat3.z_axis * t.scale.z).extend(0.0),
            t.translation.extend(1.0),
        )
    }
}

impl From<DTransform3> for DAffine3 {
    #[inline]
    fn from(t: DTransform3) -> DAffine3 {
        DAffine3::from_scale_rotation_translation(t.scale, t.rotation, t.translation)
    }
}

impl Mul for DTransform3 {
    type Output = DTransform3;

    #[inline]
    fn mul(self, rhs: Self) -> Self::Output {
        let rot1 = DMat3::from_quat(self.rotation);
        let mat1 = DMat3::from_cols(
            rot1.x_axis * self.scale.x,
            rot1.y_axis * self.scale.y,
            rot1.z_axis * self.scale.z,
        );
        let rot2 = DMat3::from_quat(rhs.rotation);
        let mat2 = DMat3::from_cols(
            rot2.x_axis * rhs.scale.x,
            rot2.y_axis * rhs.scale.y,
            rot2.z_axis * rhs.scale.z,
        );
        let translation = self.rotation * (self.scale * rhs.translation) + self.translation;
        DTransform3::from_mat3_translation(mat1 * mat2, translation)
    }
}

impl MulAssign for DTransform3 {
    #[inline]
    fn mul_assign(&mut self, rhs: DTransform3) {
        *self = self.mul(rhs);
    }
}

impl Mul<DMat4> for DTransform3 {
    type Output = DMat4;

    #[inline]
    fn mul(self, rhs: DMat4) -> Self::Output {
        DMat4::from(self) * rhs
    }
}

impl Mul<DTransform3> for DMat4 {
    type Output = DMat4;

    #[inline]
    fn mul(self, rhs: DTransform3) -> Self::Output {
        self * DMat4::from(rhs)
    }
}

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

    #[test]
    fn test_from_mat4() {
        let scale = DVec3::new(0.5, 1.0, 2.0);
        let rot = DQuat::from_rotation_y(-0.6);
        let pos = DVec3::new(1.0, -2.0, 3.0);
        let mat = DMat4::from_scale_rotation_translation(scale, rot, pos);
        let tran = DTransform3::from_mat4(mat);
        assert!(DVec3::abs_diff_eq(tran.scale, scale, 1e-6));
        assert!(DQuat::abs_diff_eq(tran.rotation, rot, 1e-6));
        assert!(DVec3::abs_diff_eq(tran.translation, pos, 1e-6));
    }

    #[test]
    fn test_transform_point3() {
        let scale = DVec3::new(1.0, 0.7, 0.5);
        let rot = DQuat::from_rotation_x(0.41);
        let pos = DVec3::new(1.1, 2.1, -3.1);
        let mat = DMat4::from_scale_rotation_translation(scale, rot, pos);
        let tran = DTransform3::from_mat4(mat);

        let point = DVec3::new(5.0, -5.0, 5.0);
        let p1 = mat.project_point3(point);
        let p2 = tran.transform_point3(point);
        assert!(DVec3::abs_diff_eq(p1, p2, 1e-6));
    }

    #[test]
    fn test_transform_vec3() {
        let scale = DVec3::new(2.0, 2.0, 0.35);
        let rot = DQuat::from_rotation_z(-0.2);
        let pos = DVec3::new(-1.5, 2.5, 4.5);
        let mat = DMat4::from_scale_rotation_translation(scale, rot, pos);
        let tran = DTransform3::from_mat4(mat);

        let vec = DVec3::new(1.0, 0.0, 0.7);
        let v1 = mat.transform_vector3(vec);
        let v2 = tran.transform_vector3(vec);
        assert!(DVec3::abs_diff_eq(v1, v2, 1e-6));
    }

    #[test]
    fn test_inverse() {
        let scale = DVec3::new(2.0, 1.7, 0.35);
        let rot = DQuat::from_rotation_z(1.5) * DQuat::from_rotation_x(1.0);
        let pos = DVec3::new(1.99, 0.77, -1.55);
        let mat = DMat4::from_scale_rotation_translation(scale, rot, pos);
        let mat_inv = mat.inverse();
        let tran1 = DTransform3::from_mat4(mat).inverse();
        let tran2 = DTransform3::from_mat4(mat_inv);
        assert!(DTransform3::abs_diff_eq(tran1, tran2, 1e-6));
    }

    #[test]
    fn test_mat4_from() {
        let scale = DVec3::new(3.1, 0.7, 1.11);
        let rot = DQuat::from_rotation_y(-2.0);
        let pos = DVec3::new(3.0, 3.3, 3.33);
        let mat = DMat4::from_scale_rotation_translation(scale, rot, pos);
        let is = DTransform3::from_mat4(mat);
        let mat2 = DMat4::from(is);
        assert!(DMat4::abs_diff_eq(&mat, mat2, 1e-6));
    }

    #[test]
    fn test_transform_mul() {
        let scale1 = DVec3::new(1.1, 1.4, 1.7);
        let rot1 = DQuat::from_rotation_x(0.77);
        let pos1 = DVec3::new(5.5, -6.6, 3.3);
        let mat1 = DMat4::from_scale_rotation_translation(scale1, rot1, pos1);
        let tran1 = DTransform3::from_scale_rotation_translation(scale1, rot1, pos1);

        let scale2 = DVec3::new(0.3, 1.0, 0.7);
        let rot2 = DQuat::from_rotation_y(-0.44);
        let pos2 = DVec3::new(-4.4, -2.2, -3.3);
        let mat2 = DMat4::from_scale_rotation_translation(scale2, rot2, pos2);
        let tran2 = DTransform3::from_scale_rotation_translation(scale2, rot2, pos2);

        let mat = mat1 * mat2;
        let tran = tran1 * tran2;
        let tran_mat = DTransform3::from_mat4(mat);
        assert!(DTransform3::abs_diff_eq(tran, tran_mat, 1e-6));
    }
}