euler 0.1.0

Mathematics library for 3D computer graphics
Documentation
use cgmath;

use {Quat, Mat4, Vec3};

/// Translation + Rotation + Non-uniform Scale transform in 3D space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Transform {
    /// Translation vector.
    pub translation: Vec3,

    /// Rotation quaternion.
    pub rotation: Quat,

    /// *Non-uniform* scaling.
    pub scale: Vec3,
}

impl Transform {
    /// Returns the identity transform.
    pub fn identity() -> Self {
        Transform {
            translation: vec3!(),
            rotation: Quat::identity(),
            scale: vec3!(1.0),
        }
    }

    /// Returns the equivalent matrix representation for this transform.
    pub fn matrix(&self) -> Mat4 {
        let t = cgmath::Matrix4::from_translation(
            cgmath::Vector3::new(
                self.translation.x,
                self.translation.y,
                self.translation.z,
            ),
        );
        let r = cgmath::Matrix4::from(
            cgmath::Quaternion::new(
                self.rotation.scalar,
                self.rotation.vector.x,
                self.rotation.vector.y,
                self.rotation.vector.z,
            ),
        );
        let s = cgmath::Matrix4::from_nonuniform_scale(
            self.scale.x,
            self.scale.y,
            self.scale.z,
        );
        let m: [[f32; 4]; 4] = (t * r * s).into();
        Mat4::from(m)
    }
}