Commit 4edbecaf81ef2bde1a3827ecb64de2bb2539f486

Authored by Georg Hopp
1 parent c5976178

Add a way to merge an iterator of transformation matrices

@@ -29,7 +29,7 @@ use fractional::continuous::Continuous; @@ -29,7 +29,7 @@ use fractional::continuous::Continuous;
29 use fractional::fractional::{Fractional, from_vector}; 29 use fractional::fractional::{Fractional, from_vector};
30 use fractional::trigonometry::Trig; 30 use fractional::trigonometry::Trig;
31 use fractional::vector::{Vector}; 31 use fractional::vector::{Vector};
32 -use fractional::transform::{translate, rotate_x, rotate_y, rotate_z, rotate_v}; 32 +use fractional::transform::{TMatrix, translate, rotate_x, rotate_y, rotate_z, rotate_v};
33 33
34 // Tail recursive Bresenham line with integer incremental error. 34 // Tail recursive Bresenham line with integer incremental error.
35 fn line(a :(u32, u32), b :(u32, u32)) -> Vec<(u32, u32)>{ 35 fn line(a :(u32, u32), b :(u32, u32)) -> Vec<(u32, u32)>{
@@ -228,47 +228,42 @@ fn _transform<T>(v :Vector<T>, v1 :Vector<T>, v2 :Vector<T>, v3 :Vector<T>) @@ -228,47 +228,42 @@ fn _transform<T>(v :Vector<T>, v1 :Vector<T>, v2 :Vector<T>, v3 :Vector<T>)
228 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> 228 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
229 + Mul<Output = T> + Div<Output = T> + Trig 229 + Mul<Output = T> + Div<Output = T> + Trig
230 + From<i32> + Copy + Display { 230 + From<i32> + Copy + Display {
231 - let mt = translate(v);  
232 231
233 println!("{:>14} : {}", "Vector v1", v1); 232 println!("{:>14} : {}", "Vector v1", v1);
234 - println!("{:>14} : {}", "translate v1", mt.apply(&v1)); 233 + println!("{:>14} : {}", "translate v1", translate(v).apply(&v1));
235 println!(); 234 println!();
236 235
237 - println!("{:>14} : {}", "Vector v2", v2);  
238 - for d in [ 30, 45, 60, 90, 120, 135, 150, 180  
239 - , 210, 225, 240, 270, 300, 315, 330 ].iter() {  
240 - let m = rotate_x(*d as i32);  
241 - println!("{:>14} : {}", format!("rot_x {} v2", d), m.apply(&v2)); 236 + fn _rot<T>( o :&str , n :&str , v :&Vector<T>
  237 + , fs :&[&dyn Fn(i32) -> TMatrix<T>] )
  238 + where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
  239 + + Mul<Output = T> + Div<Output = T> + Trig
  240 + + From<i32> + Copy + Display {
  241 +
  242 + for d in [ 30, 45, 60, 90, 120, 135, 150, 180
  243 + , 210, 225, 240, 270, 300, 315, 330 ].iter() {
  244 + let mi = fs.iter().map(|f| f(*d as i32));
  245 + println!( "{:>14} : {}"
  246 + , format!("{} {} {}", o, d, n)
  247 + , TMatrix::combine(mi).apply(v) );
  248 + }
242 } 249 }
243 - println!();  
244 250
245 println!("{:>14} : {}", "Vector v2", v2); 251 println!("{:>14} : {}", "Vector v2", v2);
246 - for d in [ 30, 45, 60, 90, 120, 135, 150, 180  
247 - , 210, 225, 240, 270, 300, 315, 330 ].iter() {  
248 - let m = rotate_y(*d as i32);  
249 - println!("{:>14} : {}", format!("rot_y {} v2", d), m.apply(&v2));  
250 - } 252 + _rot("rot_x", "v2", &v2, &[&rotate_x]);
251 println!(); 253 println!();
252 -  
253 - for d in [ 30, 45, 60, 90, 120, 135, 150, 180  
254 - , 210, 225, 240, 270, 300, 315, 330 ].iter() {  
255 - let m = rotate_x(*d as i32) * rotate_y(*d as i32);  
256 - println!("{:>14} : {}", format!("rot_xy {} v2", d), m.apply(&v2));  
257 - } 254 + _rot("rot_y", "v2", &v2, &[&rotate_y]);
  255 + println!();
  256 + _rot("rot_xy", "v2", &v2, &[&rotate_x, &rotate_y]);
258 println!(); 257 println!();
259 -  
260 println!("{:>14} : {}", "Vector v3", v3); 258 println!("{:>14} : {}", "Vector v3", v3);
261 - for d in [ 30, 45, 60, 90, 120, 135, 150, 180  
262 - , 210, 225, 240, 270, 300, 315, 330 ].iter() {  
263 - let m = rotate_z(*d as i32);  
264 - println!("{:>14} : {}", format!("rot_z {} v3", d), m.apply(&v3));  
265 - } 259 + _rot("rot_z", "v3", &v3, &[&rotate_z]);
266 println!(); 260 println!();
267 261
268 for d in [ 30, 45, 60, 90, 120, 135, 150, 180 262 for d in [ 30, 45, 60, 90, 120, 135, 150, 180
269 , 210, 225, 240, 270, 300, 315, 330 ].iter() { 263 , 210, 225, 240, 270, 300, 315, 330 ].iter() {
270 - let m = rotate_v(&v, *d as i32);  
271 - println!("{:>14} : {}", format!("rot_v {} v2", d), m.apply(&v2)); 264 + println!( "{:>14} : {}"
  265 + , format!("rot_v {} v2", d)
  266 + , rotate_v(&v, *d as i32).apply(&v2));
272 } 267 }
273 } 268 }
274 269
@@ -19,15 +19,25 @@ @@ -19,15 +19,25 @@
19 // along with this program. If not, see <http://www.gnu.org/licenses/>. 19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // 20 //
21 use std::ops::{Add, Sub, Neg, Mul, Div}; 21 use std::ops::{Add, Sub, Neg, Mul, Div};
  22 +
22 use crate::Vector; 23 use crate::Vector;
23 use crate::trigonometry::Trig; 24 use crate::trigonometry::Trig;
24 25
  26 +#[derive(Debug, Clone, Copy)]
25 pub struct TMatrix<T>( (T, T, T, T) 27 pub struct TMatrix<T>( (T, T, T, T)
26 , (T, T, T, T) 28 , (T, T, T, T)
27 , (T, T, T, T) 29 , (T, T, T, T)
28 , (T, T, T, T) ) 30 , (T, T, T, T) )
29 where T: Add + Sub + Neg + Mul + Div + Trig + From<i32> + Copy; 31 where T: Add + Sub + Neg + Mul + Div + Trig + From<i32> + Copy;
30 32
  33 +pub fn unit<T>() -> TMatrix<T>
  34 +where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
  35 + + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy {
  36 + TMatrix( (1.into(), 0.into(), 0.into(), 0.into())
  37 + , (0.into(), 1.into(), 0.into(), 0.into())
  38 + , (0.into(), 0.into(), 1.into(), 0.into())
  39 + , (0.into(), 0.into(), 0.into(), 1.into()) )
  40 +}
31 pub fn translate<T>(v :Vector<T>) -> TMatrix<T> 41 pub fn translate<T>(v :Vector<T>) -> TMatrix<T>
32 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> 42 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
33 + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { 43 + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy {
@@ -115,6 +125,12 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> @@ -115,6 +125,12 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
115 impl<T> TMatrix<T> 125 impl<T> TMatrix<T>
116 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> 126 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
117 + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy { 127 + Mul<Output = T> + Div<Output = T> + Trig + From<i32> + Copy {
  128 + pub fn combine<I>(mi :I) -> TMatrix<T>
  129 + where I: IntoIterator<Item = TMatrix<T>> {
  130 +
  131 + mi.into_iter().fold(unit(), |acc, x| acc * x)
  132 + }
  133 +
118 pub fn apply(&self, v :&Vector<T>) -> Vector<T> { 134 pub fn apply(&self, v :&Vector<T>) -> Vector<T> {
119 let TMatrix( (a11, a12, a13, a14) 135 let TMatrix( (a11, a12, a13, a14)
120 , (a21, a22, a23, a24) 136 , (a21, a22, a23, a24)
Please register or login to post a comment