rust_3d/matrix3_pipe.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//! Matrix3Pipe, which makes it easier to pipe different matrices in a defined order
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29#[derive(Default, Debug, PartialEq, PartialOrd, Clone)]
30/// Matrix3Pipe, which makes it easier to pipe different matrices in a defined order
31pub struct Matrix3Pipe {
32 pub mtranslation: Matrix3,
33 pub mrotation: Matrix3,
34 pub mscale: Matrix3,
35}
36
37impl Matrix3Pipe {
38 /// Creates a new matrix as a result of all defined operations set within the pipe
39 pub fn result(&self) -> Matrix3 {
40 &self.mtranslation * &self.mrotation * &self.mscale
41 }
42 /// Adds a translation to the pipe
43 pub fn add_translation(&mut self, x: f64, y: f64) {
44 self.mtranslation = Matrix3::translation(x, y);
45 }
46 /// Removes any translation from the pipe
47 pub fn remove_translation(&mut self) {
48 self.mtranslation = Matrix3::default();
49 }
50
51 /// Adds a rotation to the pipe
52 pub fn add_rotation(&mut self, rad: Rad) {
53 self.mrotation = Matrix3::rotation(rad);
54 }
55 /// Removes any rotation from the pipe
56 pub fn remove_rotation(&mut self) {
57 self.mrotation = Matrix3::default();
58 }
59
60 /// Adds scaling to the pipe
61 pub fn add_scale(&mut self, x: f64, y: f64) {
62 self.mscale = Matrix3::scale(x, y);
63 }
64 /// Removes any scaling from the pipe
65 pub fn remove_scale(&mut self) {
66 self.mscale = Matrix3::default();
67 }
68}