Browse Source

Make TMatrix constructors part of the implementation

master
Georg Hopp 6 years ago
parent
commit
6fd0ac65e9
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 46
      fractional/src/main.rs
  2. 67
      fractional/src/transform.rs

46
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<T>(v :Vector<T>, v1 :Vector<T>, v2 :Vector<T>, v3 :Vector<T>)
+ Debug + From<i32> + 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<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>)
}
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<Fractional> = rotate_z(20);
let rot :TMatrix<Fractional> = 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<f64> = rotate_z(20);
let rot :TMatrix<f64> = 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<T>( xcb :&XcbEasel
let step = Duration::from_millis(25);
let mut last = Instant::now();
let t :TMatrix<T> = translate(Vector(0.into(), 0.into(), 150.into()));
let t :TMatrix<T> = TMatrix::translate(Vector( 0.into()
, 0.into()
, 150.into() ));
// We do not need this here… it is used within projection…
// let p :TMatrix<T> = camera.get_projection();
loop {
let deg = ((start.elapsed() / 25).as_millis() % 360) as i32;
let rz :TMatrix<T> = rotate_z(deg);
let rz :TMatrix<T> = TMatrix::rotate_z(deg);
let rx :TMatrix<T> = TMatrix::rotate_x(-deg*2);
let ry :TMatrix<T> = 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 {

67
fractional/src/transform.rs

@ -31,71 +31,64 @@ pub struct TMatrix<T>( (T, T, T, T)
, (T, T, T, T) )
where T: Add + Sub + Neg + Mul + Div + Debug + Trig + From<i32> + Copy;
pub fn unit<T>() -> TMatrix<T>
impl<T> TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Trig + From<i32> + Copy {
TMatrix( (1.into(), 0.into(), 0.into(), 0.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 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()) )
}
pub fn translate<T>(v :Vector<T>) -> TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Trig + From<i32> + Copy {
pub fn translate(v :Vector<T>) -> Self {
let Vector(x, y, z) = v;
TMatrix( (1.into(), 0.into(), 0.into(), x)
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()) )
}
pub fn rotate_x<T>(a :i32) -> TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Trig + From<i32> + Copy {
pub fn rotate_x(a :i32) -> Self {
let sin :T = Trig::sin(a);
let cos :T = Trig::cos(a);
TMatrix( (1.into(), 0.into(), 0.into(), 0.into())
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()) )
}
pub fn rotate_y<T>(a :i32) -> TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Trig + From<i32> + Copy {
pub fn rotate_y(a :i32) -> Self {
let sin :T = Trig::sin(a);
let cos :T = Trig::cos(a);
TMatrix( (cos , 0.into(), sin , 0.into())
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()) )
}
pub fn rotate_z<T>(a :i32) -> TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Trig + From<i32> + Copy {
pub fn rotate_z(a :i32) -> Self {
let sin :T = Trig::sin(a);
let cos :T = Trig::cos(a);
TMatrix( (cos , -sin , 0.into(), 0.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 rotate_v<T>(v :&Vector<T>, a :i32) -> TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Trig + From<i32> + Copy {
pub fn rotate_v(v :&Vector<T>, a :i32) -> Self {
let Vector(x, y, z) = *v;
let sin :T = Trig::sin(a);
@ -104,7 +97,7 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
let zero :T = 0.into();
let one :T = 1.into();
TMatrix( ( (one - cos) * x * x + cos
Self::new( ( (one - cos) * x * x + cos
, (one - cos) * x * y - sin * z
, (one - cos) * x * z + sin * y
, zero )
@ -119,33 +112,19 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
, (0.into(), 0.into(), 0.into(), 1.into()) )
}
pub fn scale<T>(v :Vector<T>) -> TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Trig + From<i32> + Copy {
pub fn scale(v :Vector<T>) -> Self {
let Vector(x, y, z) = v;
TMatrix( ( x, 0.into(), 0.into(), 0.into())
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()) )
}
impl<T> TMatrix<T>
where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
+ Mul<Output = T> + Div<Output = T>
+ Debug + Trig + From<i32> + 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<T> {
TMatrix(r1, r2, r3, r4)
}
pub fn combine<I>(mi :I) -> TMatrix<T>
where I: IntoIterator<Item = TMatrix<T>> {
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<T>) -> Vector<T> {

Loading…
Cancel
Save