Commit c1655b4efb01e6522771fb337c85eb8dc6a3a784
1 parent
c57ce571
Correct transformation matrix handling
Showing
1 changed file
with
38 additions
and
20 deletions
| ... | ... | @@ -19,6 +19,7 @@ |
| 19 | 19 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | 20 | // |
| 21 | 21 | use std::ops::{Add, Sub, Neg, Mul, Div}; |
| 22 | +use std::fmt::Debug; | |
| 22 | 23 | |
| 23 | 24 | use crate::Vector; |
| 24 | 25 | use crate::trigonometry::Trig; |
| ... | ... | @@ -28,30 +29,34 @@ pub struct TMatrix<T>( (T, T, T, T) |
| 28 | 29 | , (T, T, T, T) |
| 29 | 30 | , (T, T, T, T) |
| 30 | 31 | , (T, T, T, T) ) |
| 31 | - where T: Add + Sub + Neg + Mul + Div + Trig + From<i32> + Copy; | |
| 32 | + where T: Add + Sub + Neg + Mul + Div + Debug + Trig + From<i32> + Copy; | |
| 32 | 33 | |
| 33 | 34 | pub fn unit<T>() -> TMatrix<T> |
| 34 | 35 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 35 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 36 | + + Mul<Output = T> + Div<Output = T> | |
| 37 | + + Debug + Trig + From<i32> + Copy { | |
| 36 | 38 | TMatrix( (1.into(), 0.into(), 0.into(), 0.into()) |
| 37 | 39 | , (0.into(), 1.into(), 0.into(), 0.into()) |
| 38 | 40 | , (0.into(), 0.into(), 1.into(), 0.into()) |
| 39 | 41 | , (0.into(), 0.into(), 0.into(), 1.into()) ) |
| 40 | 42 | } |
| 43 | + | |
| 41 | 44 | pub fn translate<T>(v :Vector<T>) -> TMatrix<T> |
| 42 | 45 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 43 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 44 | - let Vector(x, y, z) = v; | |
| 46 | + + Mul<Output = T> + Div<Output = T> | |
| 47 | + + Debug + Trig + From<i32> + Copy { | |
| 48 | + let Vector(x, y, z) = v; | |
| 45 | 49 | |
| 46 | - TMatrix( (1.into(), 0.into(), 0.into(), 0.into()) | |
| 47 | - , (0.into(), 1.into(), 0.into(), 0.into()) | |
| 48 | - , (0.into(), 0.into(), 1.into(), 0.into()) | |
| 49 | - , ( x, y, z, 1.into()) ) | |
| 50 | + TMatrix( (1.into(), 0.into(), 0.into(), x) | |
| 51 | + , (0.into(), 1.into(), 0.into(), y) | |
| 52 | + , (0.into(), 0.into(), 1.into(), z) | |
| 53 | + , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 50 | 54 | } |
| 51 | 55 | |
| 52 | 56 | pub fn rotate_x<T>(a :i32) -> TMatrix<T> |
| 53 | 57 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 54 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 58 | + + Mul<Output = T> + Div<Output = T> | |
| 59 | + + Debug + Trig + From<i32> + Copy { | |
| 55 | 60 | let sin :T = Trig::sin(a); |
| 56 | 61 | let cos :T = Trig::cos(a); |
| 57 | 62 | |
| ... | ... | @@ -63,7 +68,8 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 63 | 68 | |
| 64 | 69 | pub fn rotate_y<T>(a :i32) -> TMatrix<T> |
| 65 | 70 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 66 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 71 | + + Mul<Output = T> + Div<Output = T> | |
| 72 | + + Debug + Trig + From<i32> + Copy { | |
| 67 | 73 | let sin :T = Trig::sin(a); |
| 68 | 74 | let cos :T = Trig::cos(a); |
| 69 | 75 | |
| ... | ... | @@ -75,7 +81,8 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 75 | 81 | |
| 76 | 82 | pub fn rotate_z<T>(a :i32) -> TMatrix<T> |
| 77 | 83 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 78 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 84 | + + Mul<Output = T> + Div<Output = T> | |
| 85 | + + Debug + Trig + From<i32> + Copy { | |
| 79 | 86 | let sin :T = Trig::sin(a); |
| 80 | 87 | let cos :T = Trig::cos(a); |
| 81 | 88 | |
| ... | ... | @@ -87,7 +94,8 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 87 | 94 | |
| 88 | 95 | pub fn rotate_v<T>(v :&Vector<T>, a :i32) -> TMatrix<T> |
| 89 | 96 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 90 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 97 | + + Mul<Output = T> + Div<Output = T> | |
| 98 | + + Debug + Trig + From<i32> + Copy { | |
| 91 | 99 | let Vector(x, y, z) = *v; |
| 92 | 100 | |
| 93 | 101 | let sin :T = Trig::sin(a); |
| ... | ... | @@ -113,7 +121,8 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 113 | 121 | |
| 114 | 122 | pub fn scale<T>(v :Vector<T>) -> TMatrix<T> |
| 115 | 123 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 116 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 124 | + + Mul<Output = T> + Div<Output = T> | |
| 125 | + + Debug + Trig + From<i32> + Copy { | |
| 117 | 126 | let Vector(x, y, z) = v; |
| 118 | 127 | |
| 119 | 128 | TMatrix( ( x, 0.into(), 0.into(), 0.into()) |
| ... | ... | @@ -124,11 +133,19 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 124 | 133 | |
| 125 | 134 | impl<T> TMatrix<T> |
| 126 | 135 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 127 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 136 | + + Mul<Output = T> + Div<Output = T> | |
| 137 | + + Debug + Trig + From<i32> + Copy { | |
| 138 | + pub fn new( r1 :(T, T, T, T) | |
| 139 | + , r2 :(T, T, T, T) | |
| 140 | + , r3 :(T, T, T, T) | |
| 141 | + , r4 :(T, T, T, T) ) -> TMatrix<T> { | |
| 142 | + TMatrix(r1, r2, r3, r4) | |
| 143 | + } | |
| 144 | + | |
| 128 | 145 | pub fn combine<I>(mi :I) -> TMatrix<T> |
| 129 | 146 | where I: IntoIterator<Item = TMatrix<T>> { |
| 130 | 147 | |
| 131 | - mi.into_iter().fold(unit(), |acc, x| acc * x) | |
| 148 | + mi.into_iter().fold(unit(), |acc, x| x * acc) | |
| 132 | 149 | } |
| 133 | 150 | |
| 134 | 151 | pub fn apply(&self, v :&Vector<T>) -> Vector<T> { |
| ... | ... | @@ -138,10 +155,10 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 138 | 155 | , (a41, a42, a43, a44) ) = *self; |
| 139 | 156 | let Vector(x, y, z) = *v; |
| 140 | 157 | |
| 141 | - let v = Vector( a11 * x + a21 * y + a31 * z + a41 | |
| 142 | - , a12 * x + a22 * y + a32 * z + a42 | |
| 143 | - , a13 * x + a23 * y + a33 * z + a43 ); | |
| 144 | - let w = a14 * x + a24 * y + a34 * z + a44; | |
| 158 | + let v = Vector( a11 * x + a12 * y + a13 * z + a14 | |
| 159 | + , a21 * x + a22 * y + a23 * z + a24 | |
| 160 | + , a31 * x + a32 * y + a33 * z + a34 ); | |
| 161 | + let w = a41 * x + a42 * y + a43 * z + a44; | |
| 145 | 162 | |
| 146 | 163 | v.mul(&w.recip()) |
| 147 | 164 | } |
| ... | ... | @@ -149,7 +166,8 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 149 | 166 | |
| 150 | 167 | impl<T> Mul for TMatrix<T> |
| 151 | 168 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 152 | - + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { | |
| 169 | + + Mul<Output = T> + Div<Output = T> | |
| 170 | + + Debug + Trig + From<i32> + Copy { | |
| 153 | 171 | type Output = Self; |
| 154 | 172 | |
| 155 | 173 | // ATTENTION: This is not commutative, nor assoziative. | ... | ... |
Please
register
or
login
to post a comment