diff --git a/fractional/src/main.rs b/fractional/src/main.rs index 1a24aef..139e69a 100644 --- a/fractional/src/main.rs +++ b/fractional/src/main.rs @@ -33,8 +33,8 @@ use fractional::easel::{ Coordinate, Coordinates, Drawable, Line, Polyline , Polygon, Rectangle}; use fractional::fractional::{Fractional, from_vector}; use fractional::trigonometry::Trig; -use fractional::vector::{Vector}; -use fractional::transform::{TMatrix, translate, rotate_x, rotate_y, rotate_z, rotate_v}; +use fractional::vector::Vector; +use fractional::transform::TMatrix; use fractional::xcb::XcbEasel; use fractional::easel::Canvas; @@ -204,7 +204,7 @@ fn _transform(v :Vector, v1 :Vector, v2 :Vector, v3 :Vector) + Debug + From + Copy + Display { println!("{:>14} : {}", "Vector v1", v1); - println!("{:>14} : {}", "translate v1", translate(v).apply(&v1)); + println!("{:>14} : {}", "translate v1", TMatrix::translate(v).apply(&v1)); println!(); fn _rot( o :&str , n :&str , v :&Vector @@ -223,21 +223,21 @@ fn _transform(v :Vector, v1 :Vector, v2 :Vector, v3 :Vector) } println!("{:>14} : {}", "Vector v2", v2); - _rot("rot_x", "v2", &v2, &[&rotate_x]); + _rot("rot_x", "v2", &v2, &[&TMatrix::rotate_x]); println!(); - _rot("rot_y", "v2", &v2, &[&rotate_y]); + _rot("rot_y", "v2", &v2, &[&TMatrix::rotate_y]); println!(); - _rot("rot_xy", "v2", &v2, &[&rotate_x, &rotate_y]); + _rot("rot_xy", "v2", &v2, &[&TMatrix::rotate_x, &TMatrix::rotate_y]); println!(); println!("{:>14} : {}", "Vector v3", v3); - _rot("rot_z", "v3", &v3, &[&rotate_z]); + _rot("rot_z", "v3", &v3, &[&TMatrix::rotate_z]); println!(); for d in [ 30, 45, 60, 90, 120, 135, 150, 180 , 210, 225, 240, 270, 300, 315, 330 ].iter() { println!( "{:>14} : {}" , format!("rot_v {} v2", d) - , rotate_v(&v, *d as i32).apply(&v2)); + , TMatrix::rotate_v(&v, *d as i32).apply(&v2)); } } @@ -272,7 +272,7 @@ fn _line() { let j = Vector(Fractional( 30,1), Fractional( 30,1), Fractional(0,1)); let k = Vector(Fractional(-30,1), Fractional( 30,1), Fractional(0,1)); - let rot :TMatrix = rotate_z(20); + let rot :TMatrix = TMatrix::rotate_z(20); let Vector(ix, iy, _) = rot.apply(&i); let Vector(jx, jy, _) = rot.apply(&j); let Vector(kx, ky, _) = rot.apply(&k); @@ -293,7 +293,7 @@ fn _line() { let j = Vector( 30.0, 30.0, 0.0); let k = Vector(-30.0, 30.0, 0.0); - let rot :TMatrix = rotate_z(20); + let rot :TMatrix = TMatrix::rotate_z(20); let Vector(ix, iy, _) = rot.apply(&i); let Vector(jx, jy, _) = rot.apply(&j); let Vector(kx, ky, _) = rot.apply(&k); @@ -334,22 +334,26 @@ fn _democanvas( xcb :&XcbEasel let step = Duration::from_millis(25); let mut last = Instant::now(); - let t :TMatrix = translate(Vector(0.into(), 0.into(), 150.into())); + let t :TMatrix = TMatrix::translate(Vector( 0.into() + , 0.into() + , 150.into() )); // We do not need this here… it is used within projection… // let p :TMatrix = camera.get_projection(); loop { let deg = ((start.elapsed() / 25).as_millis() % 360) as i32; - let rz :TMatrix = rotate_z(deg); + let rz :TMatrix = TMatrix::rotate_z(deg); + let rx :TMatrix = TMatrix::rotate_x(-deg*2); + let ry :TMatrix = TMatrix::rotate_y(-deg*2); // I can not apply the projection in one turn, as I generate the // normals always… and this is no longer possible after the // projection… - // let rot1 = TMatrix::combine(vec!(rz, rotate_x(-deg*2), t, p)); - // let rot2 = TMatrix::combine(vec!(rz, rotate_y(-deg*2), t, p)); - let rot1 = TMatrix::combine(vec!(rz, rotate_x(-deg*2), t)); - let rot2 = TMatrix::combine(vec!(rz, rotate_y(-deg*2), t)); + // let rot1 = TMatrix::combine(vec!(rz, rx, t, p)); + // let rot2 = TMatrix::combine(vec!(rz, ry, t, p)); + let rot1 = TMatrix::combine(vec!(rz, rx, t)); + let rot2 = TMatrix::combine(vec!(rz, ry, t)); let objects = vec!( (tetrahedron.transform(&rot1), 0xFFFF00) , ( cube.transform(&rot2), 0x0000FF) ); @@ -410,16 +414,18 @@ fn main() { _democanvas( &xcb, "Something...(f64)", tx.clone() , Polyeder::triangle(60.0) - , Polyeder::tetrahedron(60.0) - , Polyeder::cube(60.0) + , Polyeder::tetrahedron(80.0) + , Polyeder::cube(55.0) , DirectLight::new(Vector(0.0, 0.0, 1.0)) ); + /* _democanvas( &xcb, "Something...(Fractional)", tx.clone() , Polyeder::triangle(Fractional(60,1)) - , Polyeder::tetrahedron(Fractional(60,1)) - , Polyeder::cube(Fractional(60,1)) + , Polyeder::tetrahedron(Fractional(80,1)) + , Polyeder::cube(Fractional(55,1)) , DirectLight::new(Vector( Fractional(0,1) , Fractional(0,1) , Fractional(1,1) )) ); + */ for x in rx { match x { diff --git a/fractional/src/transform.rs b/fractional/src/transform.rs index 5bfd28e..db67249 100644 --- a/fractional/src/transform.rs +++ b/fractional/src/transform.rs @@ -31,121 +31,100 @@ pub struct TMatrix( (T, T, T, T) , (T, T, T, T) ) where T: Add + Sub + Neg + Mul + Div + Debug + Trig + From + Copy; -pub fn unit() -> TMatrix +impl TMatrix where T: Add + Sub + Neg + Mul + Div + Debug + Trig + From + Copy { - TMatrix( (1.into(), 0.into(), 0.into(), 0.into()) - , (0.into(), 1.into(), 0.into(), 0.into()) - , (0.into(), 0.into(), 1.into(), 0.into()) - , (0.into(), 0.into(), 0.into(), 1.into()) ) -} + pub fn new( r1 :(T, T, T, T) + , r2 :(T, T, T, T) + , r3 :(T, T, T, T) + , r4 :(T, T, T, T) ) -> Self { + TMatrix(r1, r2, r3, r4) + } -pub fn translate(v :Vector) -> TMatrix -where T: Add + Sub + Neg - + Mul + Div - + Debug + Trig + From + Copy { - let Vector(x, y, z) = v; + pub fn unit() -> Self { + Self::new( (1.into(), 0.into(), 0.into(), 0.into()) + , (0.into(), 1.into(), 0.into(), 0.into()) + , (0.into(), 0.into(), 1.into(), 0.into()) + , (0.into(), 0.into(), 0.into(), 1.into()) ) + } - TMatrix( (1.into(), 0.into(), 0.into(), x) - , (0.into(), 1.into(), 0.into(), y) - , (0.into(), 0.into(), 1.into(), z) - , (0.into(), 0.into(), 0.into(), 1.into()) ) -} + pub fn translate(v :Vector) -> Self { + let Vector(x, y, z) = v; -pub fn rotate_x(a :i32) -> TMatrix -where T: Add + Sub + Neg - + Mul + Div - + Debug + Trig + From + Copy { - let sin :T = Trig::sin(a); - let cos :T = Trig::cos(a); + Self::new( (1.into(), 0.into(), 0.into(), x) + , (0.into(), 1.into(), 0.into(), y) + , (0.into(), 0.into(), 1.into(), z) + , (0.into(), 0.into(), 0.into(), 1.into()) ) + } - TMatrix( (1.into(), 0.into(), 0.into(), 0.into()) - , (0.into(), cos , -sin , 0.into()) - , (0.into(), sin , cos , 0.into()) - , (0.into(), 0.into(), 0.into(), 1.into()) ) -} + pub fn rotate_x(a :i32) -> Self { + let sin :T = Trig::sin(a); + let cos :T = Trig::cos(a); -pub fn rotate_y(a :i32) -> TMatrix -where T: Add + Sub + Neg - + Mul + Div - + Debug + Trig + From + Copy { - let sin :T = Trig::sin(a); - let cos :T = Trig::cos(a); + Self::new( (1.into(), 0.into(), 0.into(), 0.into()) + , (0.into(), cos , -sin , 0.into()) + , (0.into(), sin , cos , 0.into()) + , (0.into(), 0.into(), 0.into(), 1.into()) ) + } - TMatrix( (cos , 0.into(), sin , 0.into()) - , (0.into(), 1.into(), 0.into(), 0.into()) - , (-sin , 0.into(), cos , 0.into()) - , (0.into(), 0.into(), 0.into(), 1.into()) ) -} + pub fn rotate_y(a :i32) -> Self { + let sin :T = Trig::sin(a); + let cos :T = Trig::cos(a); -pub fn rotate_z(a :i32) -> TMatrix -where T: Add + Sub + Neg - + Mul + Div - + Debug + Trig + From + Copy { - let sin :T = Trig::sin(a); - let cos :T = Trig::cos(a); + Self::new( (cos , 0.into(), sin , 0.into()) + , (0.into(), 1.into(), 0.into(), 0.into()) + , (-sin , 0.into(), cos , 0.into()) + , (0.into(), 0.into(), 0.into(), 1.into()) ) + } - TMatrix( (cos , -sin , 0.into(), 0.into()) - , (sin , cos , 0.into(), 0.into()) - , (0.into(), 0.into(), 1.into(), 0.into()) - , (0.into(), 0.into(), 0.into(), 1.into()) ) -} + pub fn rotate_z(a :i32) -> Self { + let sin :T = Trig::sin(a); + let cos :T = Trig::cos(a); -pub fn rotate_v(v :&Vector, a :i32) -> TMatrix -where T: Add + Sub + Neg - + Mul + Div - + Debug + Trig + From + Copy { - let Vector(x, y, z) = *v; - - let sin :T = Trig::sin(a); - let cos :T = Trig::cos(a); - - let zero :T = 0.into(); - let one :T = 1.into(); - - TMatrix( ( (one - cos) * x * x + cos - , (one - cos) * x * y - sin * z - , (one - cos) * x * z + sin * y - , zero ) - , ( (one - cos) * x * y + sin * z - , (one - cos) * y * y + cos - , (one - cos) * y * z - sin * x - , zero ) - , ( (one - cos) * x * z - sin * y - , (one - cos) * y * z + sin * x - , (one - cos) * z * z + cos - , zero ) - , (0.into(), 0.into(), 0.into(), 1.into()) ) -} + Self::new( (cos , -sin , 0.into(), 0.into()) + , (sin , cos , 0.into(), 0.into()) + , (0.into(), 0.into(), 1.into(), 0.into()) + , (0.into(), 0.into(), 0.into(), 1.into()) ) + } -pub fn scale(v :Vector) -> TMatrix -where T: Add + Sub + Neg - + Mul + Div - + Debug + Trig + From + Copy { - let Vector(x, y, z) = v; + pub fn rotate_v(v :&Vector, a :i32) -> Self { + let Vector(x, y, z) = *v; - TMatrix( ( x, 0.into(), 0.into(), 0.into()) - , (0.into(), y, 0.into(), 0.into()) - , (0.into(), 0.into(), z, 0.into()) - , (0.into(), 0.into(), 0.into(), 1.into()) ) -} + let sin :T = Trig::sin(a); + let cos :T = Trig::cos(a); + + let zero :T = 0.into(); + let one :T = 1.into(); + + Self::new( ( (one - cos) * x * x + cos + , (one - cos) * x * y - sin * z + , (one - cos) * x * z + sin * y + , zero ) + , ( (one - cos) * x * y + sin * z + , (one - cos) * y * y + cos + , (one - cos) * y * z - sin * x + , zero ) + , ( (one - cos) * x * z - sin * y + , (one - cos) * y * z + sin * x + , (one - cos) * z * z + cos + , zero ) + , (0.into(), 0.into(), 0.into(), 1.into()) ) + } -impl TMatrix -where T: Add + Sub + Neg - + Mul + Div - + Debug + Trig + From + Copy { - pub fn new( r1 :(T, T, T, T) - , r2 :(T, T, T, T) - , r3 :(T, T, T, T) - , r4 :(T, T, T, T) ) -> TMatrix { - TMatrix(r1, r2, r3, r4) + pub fn scale(v :Vector) -> Self { + let Vector(x, y, z) = v; + + Self::new( ( x, 0.into(), 0.into(), 0.into()) + , (0.into(), y, 0.into(), 0.into()) + , (0.into(), 0.into(), z, 0.into()) + , (0.into(), 0.into(), 0.into(), 1.into()) ) } pub fn combine(mi :I) -> TMatrix where I: IntoIterator> { - mi.into_iter().fold(unit(), |acc, x| x * acc) + mi.into_iter().fold(Self::unit(), |acc, x| x * acc) } pub fn apply(&self, v :&Vector) -> Vector {