rust_3d/
matrix4_pipe.rs

1/*
2Copyright 2016 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//! Matrix4Pipe, 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/// Matrix4Pipe, which makes it easier to pipe different matrices in a defined order
31pub struct Matrix4Pipe {
32    pub mtranslation: Matrix4,
33    pub mrotation: Matrix4,
34    pub mscale: Matrix4,
35    pub mperspective: Matrix4,
36    pub mcamtrans: Matrix4,
37    pub mcamlook: Matrix4,
38}
39
40impl Matrix4Pipe {
41    /// Creates a new matrix as a result of all defined operations set within the pipe
42    pub fn result(&self) -> Matrix4 {
43        &self.mperspective
44            * &self.mcamlook
45            * &self.mcamtrans
46            * &self.mtranslation
47            * &self.mrotation
48            * &self.mscale
49    }
50    /// Adds a translation to the pipe
51    pub fn add_translation(&mut self, x: f64, y: f64, z: f64) {
52        self.mtranslation = Matrix4::translation(x, y, z);
53    }
54    /// Removes any translation from the pipe
55    pub fn remove_translation(&mut self) {
56        self.mtranslation = Matrix4::default();
57    }
58
59    /// Adds a rotation to the pipe
60    pub fn add_rotation(&mut self, x: Rad, y: Rad, z: Rad) {
61        self.mrotation = Matrix4::rotation(x, y, z);
62    }
63    /// Adds a rotation around an axis to the pipe
64    pub fn add_rotation_axis<N>(&mut self, axis: &N, rad: Rad)
65    where
66        N: IsNormalized3D,
67    {
68        self.mrotation = Matrix4::rotation_axis(axis, rad)
69    }
70    /// Removes any rotation from the pipe
71    pub fn remove_rotation(&mut self) {
72        self.mrotation = Matrix4::default();
73    }
74
75    /// Adds scaling to the pipe
76    pub fn add_scale(&mut self, x: f64, y: f64, z: f64) {
77        self.mscale = Matrix4::scale(x, y, z);
78    }
79    /// Removes any scaling from the pipe
80    pub fn remove_scale(&mut self) {
81        self.mscale = Matrix4::default();
82    }
83
84    /// Adds a perspective transformation to the pipe
85    pub fn add_perspective(&mut self, close: f64, away: f64, rad: Rad) {
86        self.mperspective = Matrix4::perspective(close, away, rad);
87    }
88    /// Removes any perspective transformation from the pipe
89    pub fn remove_perspective(&mut self) {
90        self.mperspective = Matrix4::default();
91    }
92
93    /// Adds camera translation to the pipe
94    pub fn add_camera_translation(&mut self, x: f64, y: f64, z: f64) {
95        self.mcamtrans = Matrix4::translation(-x, -y, -z);
96    }
97    /// Removes any camera translation from the pipe
98    pub fn remove_camera_translation(&mut self) {
99        self.mcamtrans = Matrix4::default();
100    }
101
102    /// Adds a look at target to the pipe
103    pub fn add_look_at<P, N>(&mut self, target: &P, up: &N) -> Result<()>
104    where
105        P: IsBuildable3D,
106        N: IsNormalized3D,
107    {
108        self.mcamlook = Matrix4::look_at(target, up)?;
109        Ok(())
110    }
111    /// Removes any look at target from the pipe
112    pub fn remove_look_at(&mut self) {
113        self.mcamlook = Matrix4::default();
114    }
115}