transform_matrix/
lib.rs

1//! Utility for transformation matrix creation
2//!
3//! This crate allows the user to
4//! create transformation matrices, mostly for the use in
5//! OpenGL applications.
6//!
7//! # Quick Start
8//!
9//! You can start by creating a new
10//! [`Transform`](struct.Transform.html) object, then adding
11//! some transformations, and finalizing the transformation
12//! with the
13//! [`orthographic()`](struct.Transform.html#method.orthographic)
14//! projection.
15//!
16//! ```
17//! use transform_matrix::*;
18//!
19//! let t = Transform::new()
20//!     .translate(2.0, 0.0, 0.0)
21//!     .scale(3.0, 0.0, 0.0)
22//!     .orthographic(10.0, 10.0, 1.0);
23//!
24//! assert_eq!(t, [
25//!     [0.6, 0.0, 0.0, 0.0],
26//!     [0.0, 0.0, 0.0, 0.0],
27//!     [0.0, 0.0, 0.0, 0.0],
28//!     [0.20000005, 1.0, -1.0, 1.0]
29//! ]);
30//! ```
31
32mod matrix;
33
34use matrix::Matrix;
35
36pub struct Transform {
37    matrix: Matrix,
38}
39
40impl Transform {
41    /// Begins a new transformation
42    ///
43    /// *Note*: The transformation starts as an identity matrix
44    pub fn new() -> Self {
45        Self {
46            matrix: matrix::identity(),
47        }
48    }
49
50    /// Adds a translation transformation
51    ///
52    /// # Arguments
53    ///
54    /// * `x` - Amount by which to move on the x-axis
55    ///
56    /// * `y` - Amount by which to move on the y-axis
57    ///
58    /// * `z` - Amount by which to move on the z-axis
59    pub fn translate(mut self, x: f32, y: f32, z: f32) -> Self {
60        self.matrix = matrix::multiply(self.matrix, matrix::translate(x, y, z));
61        self
62    }
63
64    /// Adds a scale transformation
65    ///
66    /// # Arguments
67    ///
68    /// * `x` - Amount by which to scale on the x-axis
69    ///
70    /// * `y` - Amount by which to scale on the y-axis
71    ///
72    /// * `z` - Amount by which to scale on the z-axis
73    pub fn scale(mut self, x: f32, y: f32, z: f32) -> Self {
74        self.matrix = matrix::multiply(self.matrix, matrix::scale(x, y, z));
75        self
76    }
77
78    /// Adds a rotation transformation
79    ///
80    /// # Arguments
81    ///
82    /// * `x` - Amount by which to rotate on the x-axis
83    ///
84    /// * `y` - Amount by which to rotate on the y-axis
85    ///
86    /// * `z` - Amount by which to rotate on the z-axis
87    pub fn rotate(mut self, x: f32, y: f32, z: f32) -> Self {
88        self.matrix = matrix::multiply(self.matrix, matrix::rotate_x(x));
89        self.matrix = matrix::multiply(self.matrix, matrix::rotate_y(y));
90        self.matrix = matrix::multiply(self.matrix, matrix::rotate_z(z));
91        self
92    }
93
94    /// Adds an orthographic projection transformation
95    ///
96    /// # Arguments
97    ///
98    /// * `width` - Size of the view on the x-axis
99    ///
100    /// * `height` - Size of the view on the y-axis
101    ///
102    /// * `depth` - Size of the view on the z-axis
103    ///
104    /// *Note*: This is the last step of the transformation
105    /// and returns the matrix
106    pub fn orthographic(self, width: f32, height: f32, depth: f32) -> Matrix {
107        matrix::multiply(self.matrix, matrix::orthographic(width, height, depth))
108    }
109}
110
111impl Default for Transform {
112    fn default() -> Self {
113        Transform::new()
114    }
115}