Commit 6fd0ac65e9152e40742b14f8240d350cb43ea621
1 parent
18da9a08
Make TMatrix constructors part of the implementation
Showing
2 changed files
with
100 additions
and
115 deletions
| ... | ... | @@ -33,8 +33,8 @@ use fractional::easel::{ Coordinate, Coordinates, Drawable, Line, Polyline |
| 33 | 33 | , Polygon, Rectangle}; |
| 34 | 34 | use fractional::fractional::{Fractional, from_vector}; |
| 35 | 35 | use fractional::trigonometry::Trig; |
| 36 | -use fractional::vector::{Vector}; | |
| 37 | -use fractional::transform::{TMatrix, translate, rotate_x, rotate_y, rotate_z, rotate_v}; | |
| 36 | +use fractional::vector::Vector; | |
| 37 | +use fractional::transform::TMatrix; | |
| 38 | 38 | |
| 39 | 39 | use fractional::xcb::XcbEasel; |
| 40 | 40 | use fractional::easel::Canvas; |
| ... | ... | @@ -204,7 +204,7 @@ fn _transform<T>(v :Vector<T>, v1 :Vector<T>, v2 :Vector<T>, v3 :Vector<T>) |
| 204 | 204 | + Debug + From<i32> + Copy + Display { |
| 205 | 205 | |
| 206 | 206 | println!("{:>14} : {}", "Vector v1", v1); |
| 207 | - println!("{:>14} : {}", "translate v1", translate(v).apply(&v1)); | |
| 207 | + println!("{:>14} : {}", "translate v1", TMatrix::translate(v).apply(&v1)); | |
| 208 | 208 | println!(); |
| 209 | 209 | |
| 210 | 210 | fn _rot<T>( o :&str , n :&str , v :&Vector<T> |
| ... | ... | @@ -223,21 +223,21 @@ fn _transform<T>(v :Vector<T>, v1 :Vector<T>, v2 :Vector<T>, v3 :Vector<T>) |
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | println!("{:>14} : {}", "Vector v2", v2); |
| 226 | - _rot("rot_x", "v2", &v2, &[&rotate_x]); | |
| 226 | + _rot("rot_x", "v2", &v2, &[&TMatrix::rotate_x]); | |
| 227 | 227 | println!(); |
| 228 | - _rot("rot_y", "v2", &v2, &[&rotate_y]); | |
| 228 | + _rot("rot_y", "v2", &v2, &[&TMatrix::rotate_y]); | |
| 229 | 229 | println!(); |
| 230 | - _rot("rot_xy", "v2", &v2, &[&rotate_x, &rotate_y]); | |
| 230 | + _rot("rot_xy", "v2", &v2, &[&TMatrix::rotate_x, &TMatrix::rotate_y]); | |
| 231 | 231 | println!(); |
| 232 | 232 | println!("{:>14} : {}", "Vector v3", v3); |
| 233 | - _rot("rot_z", "v3", &v3, &[&rotate_z]); | |
| 233 | + _rot("rot_z", "v3", &v3, &[&TMatrix::rotate_z]); | |
| 234 | 234 | println!(); |
| 235 | 235 | |
| 236 | 236 | for d in [ 30, 45, 60, 90, 120, 135, 150, 180 |
| 237 | 237 | , 210, 225, 240, 270, 300, 315, 330 ].iter() { |
| 238 | 238 | println!( "{:>14} : {}" |
| 239 | 239 | , format!("rot_v {} v2", d) |
| 240 | - , rotate_v(&v, *d as i32).apply(&v2)); | |
| 240 | + , TMatrix::rotate_v(&v, *d as i32).apply(&v2)); | |
| 241 | 241 | } |
| 242 | 242 | } |
| 243 | 243 | |
| ... | ... | @@ -272,7 +272,7 @@ fn _line() { |
| 272 | 272 | let j = Vector(Fractional( 30,1), Fractional( 30,1), Fractional(0,1)); |
| 273 | 273 | let k = Vector(Fractional(-30,1), Fractional( 30,1), Fractional(0,1)); |
| 274 | 274 | |
| 275 | - let rot :TMatrix<Fractional> = rotate_z(20); | |
| 275 | + let rot :TMatrix<Fractional> = TMatrix::rotate_z(20); | |
| 276 | 276 | let Vector(ix, iy, _) = rot.apply(&i); |
| 277 | 277 | let Vector(jx, jy, _) = rot.apply(&j); |
| 278 | 278 | let Vector(kx, ky, _) = rot.apply(&k); |
| ... | ... | @@ -293,7 +293,7 @@ fn _line() { |
| 293 | 293 | let j = Vector( 30.0, 30.0, 0.0); |
| 294 | 294 | let k = Vector(-30.0, 30.0, 0.0); |
| 295 | 295 | |
| 296 | - let rot :TMatrix<f64> = rotate_z(20); | |
| 296 | + let rot :TMatrix<f64> = TMatrix::rotate_z(20); | |
| 297 | 297 | let Vector(ix, iy, _) = rot.apply(&i); |
| 298 | 298 | let Vector(jx, jy, _) = rot.apply(&j); |
| 299 | 299 | let Vector(kx, ky, _) = rot.apply(&k); |
| ... | ... | @@ -334,22 +334,26 @@ fn _democanvas<T>( xcb :&XcbEasel |
| 334 | 334 | let step = Duration::from_millis(25); |
| 335 | 335 | let mut last = Instant::now(); |
| 336 | 336 | |
| 337 | - let t :TMatrix<T> = translate(Vector(0.into(), 0.into(), 150.into())); | |
| 337 | + let t :TMatrix<T> = TMatrix::translate(Vector( 0.into() | |
| 338 | + , 0.into() | |
| 339 | + , 150.into() )); | |
| 338 | 340 | // We do not need this here… it is used within projection… |
| 339 | 341 | // let p :TMatrix<T> = camera.get_projection(); |
| 340 | 342 | |
| 341 | 343 | loop { |
| 342 | 344 | let deg = ((start.elapsed() / 25).as_millis() % 360) as i32; |
| 343 | 345 | |
| 344 | - let rz :TMatrix<T> = rotate_z(deg); | |
| 346 | + let rz :TMatrix<T> = TMatrix::rotate_z(deg); | |
| 347 | + let rx :TMatrix<T> = TMatrix::rotate_x(-deg*2); | |
| 348 | + let ry :TMatrix<T> = TMatrix::rotate_y(-deg*2); | |
| 345 | 349 | |
| 346 | 350 | // I can not apply the projection in one turn, as I generate the |
| 347 | 351 | // normals always… and this is no longer possible after the |
| 348 | 352 | // projection… |
| 349 | - // let rot1 = TMatrix::combine(vec!(rz, rotate_x(-deg*2), t, p)); | |
| 350 | - // let rot2 = TMatrix::combine(vec!(rz, rotate_y(-deg*2), t, p)); | |
| 351 | - let rot1 = TMatrix::combine(vec!(rz, rotate_x(-deg*2), t)); | |
| 352 | - let rot2 = TMatrix::combine(vec!(rz, rotate_y(-deg*2), t)); | |
| 353 | + // let rot1 = TMatrix::combine(vec!(rz, rx, t, p)); | |
| 354 | + // let rot2 = TMatrix::combine(vec!(rz, ry, t, p)); | |
| 355 | + let rot1 = TMatrix::combine(vec!(rz, rx, t)); | |
| 356 | + let rot2 = TMatrix::combine(vec!(rz, ry, t)); | |
| 353 | 357 | |
| 354 | 358 | let objects = vec!( (tetrahedron.transform(&rot1), 0xFFFF00) |
| 355 | 359 | , ( cube.transform(&rot2), 0x0000FF) ); |
| ... | ... | @@ -410,16 +414,18 @@ fn main() { |
| 410 | 414 | |
| 411 | 415 | _democanvas( &xcb, "Something...(f64)", tx.clone() |
| 412 | 416 | , Polyeder::triangle(60.0) |
| 413 | - , Polyeder::tetrahedron(60.0) | |
| 414 | - , Polyeder::cube(60.0) | |
| 417 | + , Polyeder::tetrahedron(80.0) | |
| 418 | + , Polyeder::cube(55.0) | |
| 415 | 419 | , DirectLight::new(Vector(0.0, 0.0, 1.0)) ); |
| 420 | + /* | |
| 416 | 421 | _democanvas( &xcb, "Something...(Fractional)", tx.clone() |
| 417 | 422 | , Polyeder::triangle(Fractional(60,1)) |
| 418 | - , Polyeder::tetrahedron(Fractional(60,1)) | |
| 419 | - , Polyeder::cube(Fractional(60,1)) | |
| 423 | + , Polyeder::tetrahedron(Fractional(80,1)) | |
| 424 | + , Polyeder::cube(Fractional(55,1)) | |
| 420 | 425 | , DirectLight::new(Vector( Fractional(0,1) |
| 421 | 426 | , Fractional(0,1) |
| 422 | 427 | , Fractional(1,1) )) ); |
| 428 | + */ | |
| 423 | 429 | |
| 424 | 430 | for x in rx { |
| 425 | 431 | match x { | ... | ... |
| ... | ... | @@ -31,121 +31,100 @@ pub struct TMatrix<T>( (T, T, T, T) |
| 31 | 31 | , (T, T, T, T) ) |
| 32 | 32 | where T: Add + Sub + Neg + Mul + Div + Debug + Trig + From<i32> + Copy; |
| 33 | 33 | |
| 34 | -pub fn unit<T>() -> TMatrix<T> | |
| 34 | +impl<T> TMatrix<T> | |
| 35 | 35 | where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> |
| 36 | 36 | + Mul<Output = T> + Div<Output = T> |
| 37 | 37 | + Debug + Trig + From<i32> + Copy { |
| 38 | - TMatrix( (1.into(), 0.into(), 0.into(), 0.into()) | |
| 39 | - , (0.into(), 1.into(), 0.into(), 0.into()) | |
| 40 | - , (0.into(), 0.into(), 1.into(), 0.into()) | |
| 41 | - , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 42 | -} | |
| 38 | + pub fn new( r1 :(T, T, T, T) | |
| 39 | + , r2 :(T, T, T, T) | |
| 40 | + , r3 :(T, T, T, T) | |
| 41 | + , r4 :(T, T, T, T) ) -> Self { | |
| 42 | + TMatrix(r1, r2, r3, r4) | |
| 43 | + } | |
| 43 | 44 | |
| 44 | -pub fn translate<T>(v :Vector<T>) -> TMatrix<T> | |
| 45 | -where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | |
| 46 | - + Mul<Output = T> + Div<Output = T> | |
| 47 | - + Debug + Trig + From<i32> + Copy { | |
| 48 | - let Vector(x, y, z) = v; | |
| 45 | + pub fn unit() -> Self { | |
| 46 | + Self::new( (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 | + , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 50 | + } | |
| 49 | 51 | |
| 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()) ) | |
| 54 | -} | |
| 52 | + pub fn translate(v :Vector<T>) -> Self { | |
| 53 | + let Vector(x, y, z) = v; | |
| 55 | 54 | |
| 56 | -pub fn rotate_x<T>(a :i32) -> TMatrix<T> | |
| 57 | -where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | |
| 58 | - + Mul<Output = T> + Div<Output = T> | |
| 59 | - + Debug + Trig + From<i32> + Copy { | |
| 60 | - let sin :T = Trig::sin(a); | |
| 61 | - let cos :T = Trig::cos(a); | |
| 55 | + Self::new( (1.into(), 0.into(), 0.into(), x) | |
| 56 | + , (0.into(), 1.into(), 0.into(), y) | |
| 57 | + , (0.into(), 0.into(), 1.into(), z) | |
| 58 | + , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 59 | + } | |
| 62 | 60 | |
| 63 | - TMatrix( (1.into(), 0.into(), 0.into(), 0.into()) | |
| 64 | - , (0.into(), cos , -sin , 0.into()) | |
| 65 | - , (0.into(), sin , cos , 0.into()) | |
| 66 | - , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 67 | -} | |
| 61 | + pub fn rotate_x(a :i32) -> Self { | |
| 62 | + let sin :T = Trig::sin(a); | |
| 63 | + let cos :T = Trig::cos(a); | |
| 68 | 64 | |
| 69 | -pub fn rotate_y<T>(a :i32) -> TMatrix<T> | |
| 70 | -where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | |
| 71 | - + Mul<Output = T> + Div<Output = T> | |
| 72 | - + Debug + Trig + From<i32> + Copy { | |
| 73 | - let sin :T = Trig::sin(a); | |
| 74 | - let cos :T = Trig::cos(a); | |
| 65 | + Self::new( (1.into(), 0.into(), 0.into(), 0.into()) | |
| 66 | + , (0.into(), cos , -sin , 0.into()) | |
| 67 | + , (0.into(), sin , cos , 0.into()) | |
| 68 | + , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 69 | + } | |
| 75 | 70 | |
| 76 | - TMatrix( (cos , 0.into(), sin , 0.into()) | |
| 77 | - , (0.into(), 1.into(), 0.into(), 0.into()) | |
| 78 | - , (-sin , 0.into(), cos , 0.into()) | |
| 79 | - , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 80 | -} | |
| 71 | + pub fn rotate_y(a :i32) -> Self { | |
| 72 | + let sin :T = Trig::sin(a); | |
| 73 | + let cos :T = Trig::cos(a); | |
| 81 | 74 | |
| 82 | -pub fn rotate_z<T>(a :i32) -> TMatrix<T> | |
| 83 | -where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | |
| 84 | - + Mul<Output = T> + Div<Output = T> | |
| 85 | - + Debug + Trig + From<i32> + Copy { | |
| 86 | - let sin :T = Trig::sin(a); | |
| 87 | - let cos :T = Trig::cos(a); | |
| 75 | + Self::new( (cos , 0.into(), sin , 0.into()) | |
| 76 | + , (0.into(), 1.into(), 0.into(), 0.into()) | |
| 77 | + , (-sin , 0.into(), cos , 0.into()) | |
| 78 | + , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 79 | + } | |
| 88 | 80 | |
| 89 | - TMatrix( (cos , -sin , 0.into(), 0.into()) | |
| 90 | - , (sin , cos , 0.into(), 0.into()) | |
| 91 | - , (0.into(), 0.into(), 1.into(), 0.into()) | |
| 92 | - , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 93 | -} | |
| 81 | + pub fn rotate_z(a :i32) -> Self { | |
| 82 | + let sin :T = Trig::sin(a); | |
| 83 | + let cos :T = Trig::cos(a); | |
| 94 | 84 | |
| 95 | -pub fn rotate_v<T>(v :&Vector<T>, a :i32) -> TMatrix<T> | |
| 96 | -where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | |
| 97 | - + Mul<Output = T> + Div<Output = T> | |
| 98 | - + Debug + Trig + From<i32> + Copy { | |
| 99 | - let Vector(x, y, z) = *v; | |
| 100 | - | |
| 101 | - let sin :T = Trig::sin(a); | |
| 102 | - let cos :T = Trig::cos(a); | |
| 103 | - | |
| 104 | - let zero :T = 0.into(); | |
| 105 | - let one :T = 1.into(); | |
| 106 | - | |
| 107 | - TMatrix( ( (one - cos) * x * x + cos | |
| 108 | - , (one - cos) * x * y - sin * z | |
| 109 | - , (one - cos) * x * z + sin * y | |
| 110 | - , zero ) | |
| 111 | - , ( (one - cos) * x * y + sin * z | |
| 112 | - , (one - cos) * y * y + cos | |
| 113 | - , (one - cos) * y * z - sin * x | |
| 114 | - , zero ) | |
| 115 | - , ( (one - cos) * x * z - sin * y | |
| 116 | - , (one - cos) * y * z + sin * x | |
| 117 | - , (one - cos) * z * z + cos | |
| 118 | - , zero ) | |
| 119 | - , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 120 | -} | |
| 85 | + Self::new( (cos , -sin , 0.into(), 0.into()) | |
| 86 | + , (sin , cos , 0.into(), 0.into()) | |
| 87 | + , (0.into(), 0.into(), 1.into(), 0.into()) | |
| 88 | + , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 89 | + } | |
| 121 | 90 | |
| 122 | -pub fn scale<T>(v :Vector<T>) -> TMatrix<T> | |
| 123 | -where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | |
| 124 | - + Mul<Output = T> + Div<Output = T> | |
| 125 | - + Debug + Trig + From<i32> + Copy { | |
| 126 | - let Vector(x, y, z) = v; | |
| 91 | + pub fn rotate_v(v :&Vector<T>, a :i32) -> Self { | |
| 92 | + let Vector(x, y, z) = *v; | |
| 127 | 93 | |
| 128 | - TMatrix( ( x, 0.into(), 0.into(), 0.into()) | |
| 129 | - , (0.into(), y, 0.into(), 0.into()) | |
| 130 | - , (0.into(), 0.into(), z, 0.into()) | |
| 131 | - , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 132 | -} | |
| 94 | + let sin :T = Trig::sin(a); | |
| 95 | + let cos :T = Trig::cos(a); | |
| 96 | + | |
| 97 | + let zero :T = 0.into(); | |
| 98 | + let one :T = 1.into(); | |
| 99 | + | |
| 100 | + Self::new( ( (one - cos) * x * x + cos | |
| 101 | + , (one - cos) * x * y - sin * z | |
| 102 | + , (one - cos) * x * z + sin * y | |
| 103 | + , zero ) | |
| 104 | + , ( (one - cos) * x * y + sin * z | |
| 105 | + , (one - cos) * y * y + cos | |
| 106 | + , (one - cos) * y * z - sin * x | |
| 107 | + , zero ) | |
| 108 | + , ( (one - cos) * x * z - sin * y | |
| 109 | + , (one - cos) * y * z + sin * x | |
| 110 | + , (one - cos) * z * z + cos | |
| 111 | + , zero ) | |
| 112 | + , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 113 | + } | |
| 133 | 114 | |
| 134 | -impl<T> TMatrix<T> | |
| 135 | -where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | |
| 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) | |
| 115 | + pub fn scale(v :Vector<T>) -> Self { | |
| 116 | + let Vector(x, y, z) = v; | |
| 117 | + | |
| 118 | + Self::new( ( x, 0.into(), 0.into(), 0.into()) | |
| 119 | + , (0.into(), y, 0.into(), 0.into()) | |
| 120 | + , (0.into(), 0.into(), z, 0.into()) | |
| 121 | + , (0.into(), 0.into(), 0.into(), 1.into()) ) | |
| 143 | 122 | } |
| 144 | 123 | |
| 145 | 124 | pub fn combine<I>(mi :I) -> TMatrix<T> |
| 146 | 125 | where I: IntoIterator<Item = TMatrix<T>> { |
| 147 | 126 | |
| 148 | - mi.into_iter().fold(unit(), |acc, x| x * acc) | |
| 127 | + mi.into_iter().fold(Self::unit(), |acc, x| x * acc) | |
| 149 | 128 | } |
| 150 | 129 | |
| 151 | 130 | pub fn apply(&self, v :&Vector<T>) -> Vector<T> { | ... | ... |
Please
register
or
login
to post a comment