Skip to main content

ty_math/
ty_transform.rs

1use crate::{TyQuaternion, TyVector3};
2
3/// A node transform generic over its component type `T`, composing as
4/// `Translation * Rotation * Scale`.
5///
6/// See `TyTransformF32` and `TyTransformF64` for the common instantiations.
7#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct TyTransform<T> {
9    /// The translation, which may be fractional.
10    pub position: TyVector3<T>,
11
12    /// The rotation, a unit quaternion.
13    pub rotation: TyQuaternion<T>,
14
15    /// The per-axis scale.
16    pub scale: TyVector3<T>,
17}
18
19impl<T> TyTransform<T> {
20    /// Creates a new transform from a `position`, `rotation`, and `scale`.
21    pub fn new(position: TyVector3<T>, rotation: TyQuaternion<T>, scale: TyVector3<T>) -> Self {
22        Self {
23            position,
24            rotation,
25            scale,
26        }
27    }
28}
29
30/// Implements the float-only transform [`Default`] (zero position, identity
31/// rotation, unit scale) for a concrete floating-point component type.
32macro_rules! impl_ty_transform_float {
33    ($t:ty) => {
34        impl Default for TyTransform<$t> {
35            fn default() -> Self {
36                Self {
37                    position: TyVector3::new(0.0, 0.0, 0.0),
38                    rotation: TyQuaternion::<$t>::identity(),
39                    scale: TyVector3::new(1.0, 1.0, 1.0),
40                }
41            }
42        }
43    };
44}
45
46impl_ty_transform_float!(f32);
47impl_ty_transform_float!(f64);