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 29 use fractional::fractional::{Fractional, from_vector};
30 30 use fractional::trigonometry::Trig;
31 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 34 // Tail recursive Bresenham line with integer incremental error.
35 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 228 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
229 229 + Mul<Output = T> + Div<Output = T> + Trig
230 230 + From<i32> + Copy + Display {
231   - let mt = translate(v);
232 231
233 232 println!("{:>14} : {}", "Vector v1", v1);
234   - println!("{:>14} : {}", "translate v1", mt.apply(&v1));
  233 + println!("{:>14} : {}", "translate v1", translate(v).apply(&v1));
235 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 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 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 257 println!();
259   -
260 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 260 println!();
267 261
268 262 for d in [ 30, 45, 60, 90, 120, 135, 150, 180
269 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 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 +
22 23 use crate::Vector;
23 24 use crate::trigonometry::Trig;
24 25
  26 +#[derive(Debug, Clone, Copy)]
25 27 pub struct TMatrix<T>( (T, T, T, T)
26 28 , (T, T, T, T)
27 29 , (T, T, T, T)
28 30 , (T, T, T, T) )
29 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 41 pub fn translate<T>(v :Vector<T>) -> TMatrix<T>
32 42 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
33 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 125 impl<T> TMatrix<T>
116 126 where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
117 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 134 pub fn apply(&self, v :&Vector<T>) -> Vector<T> {
119 135 let TMatrix( (a11, a12, a13, a14)
120 136 , (a21, a22, a23, a24)
... ...
Please register or login to post a comment