transform-matrix 0.1.1

Easy transformation matrices
Documentation
//! Utility for transformation matrix creation
//!
//! This crate allows the user to
//! create transformation matrices, mostly for the use in
//! OpenGL applications.
//!
//! # Quick Start
//!
//! You can start by creating a new
//! [`Transform`](struct.Transform.html) object, then adding
//! some transformations, and finalizing the transformation
//! with the
//! [`orthographic()`](struct.Transform.html#method.orthographic)
//! projection.
//!
//! ```
//! use transform_matrix::*;
//!
//! let t = Transform::new()
//!     .translate(2.0, 0.0, 0.0)
//!     .scale(3.0, 0.0, 0.0)
//!     .orthographic(10.0, 10.0, 1.0);
//!
//! assert_eq!(t, [
//!     [0.6, 0.0, 0.0, 0.0],
//!     [0.0, 0.0, 0.0, 0.0],
//!     [0.0, 0.0, 0.0, 0.0],
//!     [0.20000005, 1.0, -1.0, 1.0]
//! ]);
//! ```

mod matrix;

use matrix::Matrix;

pub struct Transform {
    matrix: Matrix,
}

impl Transform {
    /// Begins a new transformation
    ///
    /// *Note*: The transformation starts as an identity matrix
    pub fn new() -> Self {
        Self {
            matrix: matrix::identity(),
        }
    }

    /// Adds a translation transformation
    ///
    /// # Arguments
    ///
    /// * `x` - Amount by which to move on the x-axis
    ///
    /// * `y` - Amount by which to move on the y-axis
    ///
    /// * `z` - Amount by which to move on the z-axis
    pub fn translate(mut self, x: f32, y: f32, z: f32) -> Self {
        self.matrix = matrix::multiply(self.matrix, matrix::translate(x, y, z));
        self
    }

    /// Adds a scale transformation
    ///
    /// # Arguments
    ///
    /// * `x` - Amount by which to scale on the x-axis
    ///
    /// * `y` - Amount by which to scale on the y-axis
    ///
    /// * `z` - Amount by which to scale on the z-axis
    pub fn scale(mut self, x: f32, y: f32, z: f32) -> Self {
        self.matrix = matrix::multiply(self.matrix, matrix::scale(x, y, z));
        self
    }

    /// Adds a rotation transformation
    ///
    /// # Arguments
    ///
    /// * `x` - Amount by which to rotate on the x-axis
    ///
    /// * `y` - Amount by which to rotate on the y-axis
    ///
    /// * `z` - Amount by which to rotate on the z-axis
    pub fn rotate(mut self, x: f32, y: f32, z: f32) -> Self {
        self.matrix = matrix::multiply(self.matrix, matrix::rotate_x(x));
        self.matrix = matrix::multiply(self.matrix, matrix::rotate_y(y));
        self.matrix = matrix::multiply(self.matrix, matrix::rotate_z(z));
        self
    }

    /// Adds an orthographic projection transformation
    ///
    /// # Arguments
    ///
    /// * `width` - Size of the view on the x-axis
    ///
    /// * `height` - Size of the view on the y-axis
    ///
    /// * `depth` - Size of the view on the z-axis
    ///
    /// *Note*: This is the last step of the transformation
    /// and returns the matrix
    pub fn orthographic(self, width: f32, height: f32, depth: f32) -> Matrix {
        matrix::multiply(self.matrix, matrix::orthographic(width, height, depth))
    }
}

impl Default for Transform {
    fn default() -> Self {
        Transform::new()
    }
}