rust_3d/
matrix3.rs

1/*
2Copyright 2017 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Matrix3, a matrix with 3 rows and columns
24
25use std::ops::Mul;
26
27use crate::*;
28
29//------------------------------------------------------------------------------
30
31#[derive(Debug, PartialEq, PartialOrd, Clone)]
32/// Matrix3, a matrix with 3 rows and columns
33pub struct Matrix3 {
34    pub data: [[f64; 3]; 3],
35}
36
37impl Matrix3 {
38    /// Creates a new identity matrix
39    pub fn identity() -> Matrix3 {
40        Matrix3 {
41            data: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
42        }
43    }
44    /// Creates a new matrix which contains only zeroes
45    pub fn zeroes() -> Matrix3 {
46        Matrix3 {
47            data: [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
48        }
49    }
50    /// Creates a new matrix which applies translation
51    pub fn translation(x: f64, y: f64) -> Matrix3 {
52        Matrix3 {
53            data: [[1.0, 0.0, x], [0.0, 1.0, y], [0.0, 0.0, 1.0]],
54        }
55    }
56    /// Creates a new matrix which applies scaling
57    pub fn scale(x: f64, y: f64) -> Matrix3 {
58        Matrix3 {
59            data: [[x, 0.0, 0.0], [0.0, y, 0.0], [0.0, 0.0, 1.0]],
60        }
61    }
62    /// Creates a new matrix which applies rotation
63    pub fn rotation(rad: Rad) -> Matrix3 {
64        Matrix3 {
65            data: [
66                [rad.val.cos(), -rad.val.sin(), 0.0],
67                [rad.val.sin(), rad.val.cos(), 0.0],
68                [0.0, 0.0, 1.0],
69            ],
70        }
71    }
72}
73
74impl Default for Matrix3 {
75    fn default() -> Self {
76        Self::identity()
77    }
78}
79
80impl Mul for Matrix3 {
81    type Output = Self;
82
83    fn mul(self, other: Self) -> Self {
84        let mut result = Matrix3::default();
85        for i in 0..3 {
86            for j in 0..3 {
87                result.data[i][j] = self.data[i][0] * other.data[0][j]
88                    + self.data[i][1] * other.data[1][j]
89                    + self.data[i][2] * other.data[2][j];
90            }
91        }
92        result
93    }
94}
95
96impl Mul<&Matrix3> for Matrix3 {
97    type Output = Self;
98
99    fn mul(self, other: &Self) -> Self {
100        let mut result = Matrix3::default();
101        for i in 0..3 {
102            for j in 0..3 {
103                result.data[i][j] = self.data[i][0] * other.data[0][j]
104                    + self.data[i][1] * other.data[1][j]
105                    + self.data[i][2] * other.data[2][j];
106            }
107        }
108        result
109    }
110}
111
112impl Mul for &Matrix3 {
113    type Output = Matrix3;
114
115    fn mul(self, other: Self) -> Matrix3 {
116        let mut result = Matrix3::default();
117        for i in 0..3 {
118            for j in 0..3 {
119                result.data[i][j] = self.data[i][0] * other.data[0][j]
120                    + self.data[i][1] * other.data[1][j]
121                    + self.data[i][2] * other.data[2][j];
122            }
123        }
124        result
125    }
126}
127
128impl Mul<f64> for Matrix3 {
129    type Output = Self;
130
131    fn mul(self, other: f64) -> Self {
132        let mut result = Matrix3::default();
133        for i in 0..3 {
134            for j in 0..3 {
135                result.data[i][j] = other * self.data[i][j];
136            }
137        }
138        result
139    }
140}
141
142impl Mul<f64> for &Matrix3 {
143    type Output = Matrix3;
144
145    fn mul(self, other: f64) -> Matrix3 {
146        let mut result = Matrix3::default();
147        for i in 0..3 {
148            for j in 0..3 {
149                result.data[i][j] = other * self.data[i][j];
150            }
151        }
152        result
153    }
154}