diff --git a/src/adaptors/rotmat.rs b/src/adaptors/rotmat.rs index c5fa4e7b..8ff29df5 100644 --- a/src/adaptors/rotmat.rs +++ b/src/adaptors/rotmat.rs @@ -24,7 +24,7 @@ impl Rotmat { self.submat } } -pub fn rotmat2>(angle: T) -> Rotmat> +pub fn rotmat2>(angle: N) -> Rotmat> { let coa = angle.cos(); let sia = angle.sin(); @@ -33,11 +33,11 @@ pub fn rotmat2>(angle: T) -> Rotmat> { submat: Mat2::new(coa, -sia, sia, coa) } } -pub fn rotmat3 + One + Sub + Add + - Mul> -(axis: &Vec3, angle: T) -> Rotmat> +pub fn rotmat3 + One + Sub + Add + + Mul> +(axis: &Vec3, angle: N) -> Rotmat> { - let _1 = One::one::(); + let _1 = One::one::(); let ux = axis.x; let uy = axis.y; let uz = axis.z; @@ -64,44 +64,44 @@ pub fn rotmat3 + One + Sub + Add + } } -impl + Trigonometric + Neg + Mul + Add + Copy> -Rotation> for Rotmat> +impl + Trigonometric + Neg + Mul + Add + Copy> +Rotation> for Rotmat> { - fn rotation(&self) -> Vec1 + fn rotation(&self) -> Vec1 { Vec1::new(-(self.submat.m12 / self.submat.m11).atan()) } - fn rotated(&self, rot: &Vec1) -> Rotmat> + fn rotated(&self, rot: &Vec1) -> Rotmat> { rotmat2(rot.x) * *self } - fn rotate(&mut self, rot: &Vec1) + fn rotate(&mut self, rot: &Vec1) { *self = self.rotated(rot) } } -impl + Trigonometric + Neg + Mul + Add + Copy + - One + Sub> -Rotation<(Vec3, T)> for Rotmat> +impl + Trigonometric + Neg + Mul + Add + Copy + + One + Sub> +Rotation<(Vec3, N)> for Rotmat> { - fn rotation(&self) -> (Vec3, T) + fn rotation(&self) -> (Vec3, N) { fail!("Not yet implemented.") } - fn rotated(&self, &(axis, angle): &(Vec3, T)) -> Rotmat> + fn rotated(&self, &(axis, angle): &(Vec3, N)) -> Rotmat> { rotmat3(&axis, angle) * *self } - fn rotate(&mut self, rot: &(Vec3, T)) + fn rotate(&mut self, rot: &(Vec3, N)) { *self = self.rotated(rot) } } -impl> Rand for Rotmat> +impl> Rand for Rotmat> { - fn rand(rng: &mut R) -> Rotmat> + fn rand(rng: &mut R) -> Rotmat> { rotmat2(rng.gen()) } } -impl + One + Sub + Add + - Mul> -Rand for Rotmat> +impl + One + Sub + Add + + Mul> +Rand for Rotmat> { - fn rand(rng: &mut R) -> Rotmat> + fn rand(rng: &mut R) -> Rotmat> { rotmat3(&rng.gen(), rng.gen()) } } @@ -160,14 +160,14 @@ Transpose for Rotmat { self.submat.transpose() } } -impl, M: ApproxEq> ApproxEq for Rotmat +impl, M: ApproxEq> ApproxEq for Rotmat { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } fn approx_eq(&self, other: &Rotmat) -> bool { self.submat.approx_eq(&other.submat) } - fn approx_eq_eps(&self, other: &Rotmat, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &Rotmat, epsilon: &N) -> bool { self.submat.approx_eq_eps(&other.submat, epsilon) } } diff --git a/src/adaptors/transform.rs b/src/adaptors/transform.rs index 6004e555..b0811522 100644 --- a/src/adaptors/transform.rs +++ b/src/adaptors/transform.rs @@ -123,11 +123,11 @@ Inv for Transform } } -impl, M:ApproxEq, V:ApproxEq> -ApproxEq for Transform +impl, M:ApproxEq, V:ApproxEq> +ApproxEq for Transform { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } fn approx_eq(&self, other: &Transform) -> bool { @@ -135,7 +135,7 @@ ApproxEq for Transform self.subtrans.approx_eq(&other.subtrans) } - fn approx_eq_eps(&self, other: &Transform, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &Transform, epsilon: &N) -> bool { self.submat.approx_eq_eps(&other.submat, epsilon) && self.subtrans.approx_eq_eps(&other.subtrans, epsilon) diff --git a/src/dim1/mat1.rs b/src/dim1/mat1.rs index 6d22e5dd..788a1ac7 100644 --- a/src/dim1/mat1.rs +++ b/src/dim1/mat1.rs @@ -8,63 +8,63 @@ use traits::workarounds::rlmul::{RMul, LMul}; use dim1::vec1::Vec1; #[deriving(Eq, ToStr)] -pub struct Mat1 -{ m11: T } +pub struct Mat1 +{ m11: N } -impl Mat1 +impl Mat1 { - pub fn new(m11: T) -> Mat1 + pub fn new(m11: N) -> Mat1 { Mat1 { m11: m11 } } } -impl Dim for Mat1 +impl Dim for Mat1 { fn dim() -> uint { 1 } } -impl One for Mat1 +impl One for Mat1 { - fn one() -> Mat1 + fn one() -> Mat1 { return Mat1::new(One::one()) } } -impl Zero for Mat1 +impl Zero for Mat1 { - fn zero() -> Mat1 + fn zero() -> Mat1 { Mat1::new(Zero::zero()) } fn is_zero(&self) -> bool { self.m11.is_zero() } } -impl + Add> Mul, Mat1> for Mat1 +impl + Add> Mul, Mat1> for Mat1 { - fn mul(&self, other: &Mat1) -> Mat1 + fn mul(&self, other: &Mat1) -> Mat1 { Mat1::new(self.m11 * other.m11) } } -impl + Mul> RMul> for Mat1 +impl + Mul> RMul> for Mat1 { - fn rmul(&self, other: &Vec1) -> Vec1 + fn rmul(&self, other: &Vec1) -> Vec1 { Vec1::new(self.m11 * other.x) } } -impl + Mul> LMul> for Mat1 +impl + Mul> LMul> for Mat1 { - fn lmul(&self, other: &Vec1) -> Vec1 + fn lmul(&self, other: &Vec1) -> Vec1 { Vec1::new(self.m11 * other.x) } } -impl + Div + Sub + Neg + Zero + One> -Inv for Mat1 +impl + Div + Sub + Neg + Zero + One> +Inv for Mat1 { - fn inverse(&self) -> Mat1 + fn inverse(&self) -> Mat1 { - let mut res : Mat1 = *self; + let mut res : Mat1 = *self; res.invert(); @@ -75,33 +75,33 @@ Inv for Mat1 { assert!(!self.m11.is_zero()); - self.m11 = One::one::() / self.m11 + self.m11 = One::one::() / self.m11 } } -impl Transpose for Mat1 +impl Transpose for Mat1 { - fn transposed(&self) -> Mat1 + fn transposed(&self) -> Mat1 { *self } fn transpose(&mut self) { } } -impl> ApproxEq for Mat1 +impl> ApproxEq for Mat1 { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &Mat1) -> bool + fn approx_eq(&self, other: &Mat1) -> bool { self.m11.approx_eq(&other.m11) } - fn approx_eq_eps(&self, other: &Mat1, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &Mat1, epsilon: &N) -> bool { self.m11.approx_eq_eps(&other.m11, epsilon) } } -impl Rand for Mat1 +impl Rand for Mat1 { - fn rand(rng: &mut R) -> Mat1 + fn rand(rng: &mut R) -> Mat1 { Mat1::new(rng.gen()) } } diff --git a/src/dim1/vec1.rs b/src/dim1/vec1.rs index 6e1419a5..daa3e12a 100644 --- a/src/dim1/vec1.rs +++ b/src/dim1/vec1.rs @@ -10,112 +10,112 @@ use traits::sub_dot::SubDot; use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; #[deriving(Eq, ToStr)] -pub struct Vec1 -{ x : T } +pub struct Vec1 +{ x : N } -impl Vec1 +impl Vec1 { - pub fn new(x: T) -> Vec1 + pub fn new(x: N) -> Vec1 { Vec1 {x: x} } } -impl Dim for Vec1 +impl Dim for Vec1 { fn dim() -> uint { 1 } } -impl> Add, Vec1> for Vec1 +impl> Add, Vec1> for Vec1 { - fn add(&self, other: &Vec1) -> Vec1 + fn add(&self, other: &Vec1) -> Vec1 { Vec1::new(self.x + other.x) } } -impl> Sub, Vec1> for Vec1 +impl> Sub, Vec1> for Vec1 { - fn sub(&self, other: &Vec1) -> Vec1 + fn sub(&self, other: &Vec1) -> Vec1 { Vec1::new(self.x - other.x) } } -impl> -ScalarMul for Vec1 +impl> +ScalarMul for Vec1 { - fn scalar_mul(&self, s: &T) -> Vec1 + fn scalar_mul(&self, s: &N) -> Vec1 { Vec1 { x: self.x * *s } } - fn scalar_mul_inplace(&mut self, s: &T) + fn scalar_mul_inplace(&mut self, s: &N) { self.x *= *s; } } -impl> -ScalarDiv for Vec1 +impl> +ScalarDiv for Vec1 { - fn scalar_div(&self, s: &T) -> Vec1 + fn scalar_div(&self, s: &N) -> Vec1 { Vec1 { x: self.x / *s } } - fn scalar_div_inplace(&mut self, s: &T) + fn scalar_div_inplace(&mut self, s: &N) { self.x /= *s; } } -impl> -ScalarAdd for Vec1 +impl> +ScalarAdd for Vec1 { - fn scalar_add(&self, s: &T) -> Vec1 + fn scalar_add(&self, s: &N) -> Vec1 { Vec1 { x: self.x + *s } } - fn scalar_add_inplace(&mut self, s: &T) + fn scalar_add_inplace(&mut self, s: &N) { self.x += *s; } } -impl> -ScalarSub for Vec1 +impl> +ScalarSub for Vec1 { - fn scalar_sub(&self, s: &T) -> Vec1 + fn scalar_sub(&self, s: &N) -> Vec1 { Vec1 { x: self.x - *s } } - fn scalar_sub_inplace(&mut self, s: &T) + fn scalar_sub_inplace(&mut self, s: &N) { self.x -= *s; } } -impl> Translation> for Vec1 +impl> Translation> for Vec1 { - fn translation(&self) -> Vec1 + fn translation(&self) -> Vec1 { *self } - fn translated(&self, t: &Vec1) -> Vec1 + fn translated(&self, t: &Vec1) -> Vec1 { self + *t } - fn translate(&mut self, t: &Vec1) + fn translate(&mut self, t: &Vec1) { *self += *t } } -impl> Dot for Vec1 +impl> Dot for Vec1 { - fn dot(&self, other : &Vec1) -> T + fn dot(&self, other : &Vec1) -> N { self.x * other.x } } -impl + Sub> SubDot for Vec1 +impl + Sub> SubDot for Vec1 { - fn sub_dot(&self, a: &Vec1, b: &Vec1) -> T + fn sub_dot(&self, a: &Vec1, b: &Vec1) -> N { (self.x - a.x) * b.x } } -impl + Add + Div + Algebraic> -Norm for Vec1 +impl + Add + Div + Algebraic> +Norm for Vec1 { - fn sqnorm(&self) -> T + fn sqnorm(&self) -> N { self.dot(self) } - fn norm(&self) -> T + fn norm(&self) -> N { self.sqnorm().sqrt() } - fn normalized(&self) -> Vec1 + fn normalized(&self) -> Vec1 { Vec1::new(self.x / self.norm()) } - fn normalize(&mut self) -> T + fn normalize(&mut self) -> N { let l = self.norm(); @@ -125,15 +125,15 @@ Norm for Vec1 } } -impl> Neg> for Vec1 +impl> Neg> for Vec1 { - fn neg(&self) -> Vec1 + fn neg(&self) -> Vec1 { Vec1::new(-self.x) } } -impl Zero for Vec1 +impl Zero for Vec1 { - fn zero() -> Vec1 + fn zero() -> Vec1 { let _0 = Zero::zero(); Vec1::new(_0) @@ -143,29 +143,29 @@ impl Zero for Vec1 { self.x.is_zero() } } -impl Basis for Vec1 +impl Basis for Vec1 { - fn canonical_basis() -> ~[Vec1] + fn canonical_basis() -> ~[Vec1] { ~[ Vec1::new(One::one()) ] } // FIXME: this should be static - fn orthogonal_subspace_basis(&self) -> ~[Vec1] + fn orthogonal_subspace_basis(&self) -> ~[Vec1] { ~[] } } -impl> ApproxEq for Vec1 +impl> ApproxEq for Vec1 { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &Vec1) -> bool + fn approx_eq(&self, other: &Vec1) -> bool { self.x.approx_eq(&other.x) } - fn approx_eq_eps(&self, other: &Vec1, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &Vec1, epsilon: &N) -> bool { self.x.approx_eq_eps(&other.x, epsilon) } } -impl Rand for Vec1 +impl Rand for Vec1 { - fn rand(rng: &mut R) -> Vec1 + fn rand(rng: &mut R) -> Vec1 { Vec1::new(rng.gen()) } } diff --git a/src/dim2/mat2.rs b/src/dim2/mat2.rs index 9f78c317..49e690a7 100644 --- a/src/dim2/mat2.rs +++ b/src/dim2/mat2.rs @@ -9,15 +9,15 @@ use traits::workarounds::rlmul::{RMul, LMul}; use dim2::vec2::Vec2; #[deriving(Eq, ToStr)] -pub struct Mat2 +pub struct Mat2 { - m11: T, m12: T, - m21: T, m22: T + m11: N, m12: N, + m21: N, m22: N } -impl Mat2 +impl Mat2 { - pub fn new(m11: T, m12: T, m21: T, m22: T) -> Mat2 + pub fn new(m11: N, m12: N, m21: N, m22: N) -> Mat2 { Mat2 { @@ -27,15 +27,15 @@ impl Mat2 } } -impl Dim for Mat2 +impl Dim for Mat2 { fn dim() -> uint { 2 } } -impl One for Mat2 +impl One for Mat2 { - fn one() -> Mat2 + fn one() -> Mat2 { let (_0, _1) = (Zero::zero(), One::one()); return Mat2::new(_1, _0, @@ -43,9 +43,9 @@ impl One for Mat2 } } -impl Zero for Mat2 +impl Zero for Mat2 { - fn zero() -> Mat2 + fn zero() -> Mat2 { let _0 = Zero::zero(); return Mat2::new(_0, _0, @@ -59,9 +59,9 @@ impl Zero for Mat2 } } -impl + Add> Mul, Mat2> for Mat2 +impl + Add> Mul, Mat2> for Mat2 { - fn mul(&self, other: &Mat2) -> Mat2 + fn mul(&self, other: &Mat2) -> Mat2 { Mat2::new( self.m11 * other.m11 + self.m12 * other.m21, @@ -72,9 +72,9 @@ impl + Add> Mul, Mat2> for Mat2 } } -impl + Mul> RMul> for Mat2 +impl + Mul> RMul> for Mat2 { - fn rmul(&self, other: &Vec2) -> Vec2 + fn rmul(&self, other: &Vec2) -> Vec2 { Vec2::new( self.m11 * other.x + self.m12 * other.y, @@ -83,9 +83,9 @@ impl + Mul> RMul> for Mat2 } } -impl + Mul> LMul> for Mat2 +impl + Mul> LMul> for Mat2 { - fn lmul(&self, other: &Vec2) -> Vec2 + fn lmul(&self, other: &Vec2) -> Vec2 { Vec2::new( self.m11 * other.x + self.m21 * other.y, @@ -94,12 +94,12 @@ impl + Mul> LMul> for Mat2 } } -impl + Div + Sub + Neg + Zero> -Inv for Mat2 +impl + Div + Sub + Neg + Zero> +Inv for Mat2 { - fn inverse(&self) -> Mat2 + fn inverse(&self) -> Mat2 { - let mut res : Mat2 = *self; + let mut res : Mat2 = *self; res.invert(); @@ -117,9 +117,9 @@ Inv for Mat2 } } -impl Transpose for Mat2 +impl Transpose for Mat2 { - fn transposed(&self) -> Mat2 + fn transposed(&self) -> Mat2 { Mat2::new(self.m11, self.m21, self.m12, self.m22) @@ -129,12 +129,12 @@ impl Transpose for Mat2 { swap(&mut self.m21, &mut self.m12); } } -impl> ApproxEq for Mat2 +impl> ApproxEq for Mat2 { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &Mat2) -> bool + fn approx_eq(&self, other: &Mat2) -> bool { self.m11.approx_eq(&other.m11) && self.m12.approx_eq(&other.m12) && @@ -143,7 +143,7 @@ impl> ApproxEq for Mat2 self.m22.approx_eq(&other.m22) } - fn approx_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &Mat2, epsilon: &N) -> bool { self.m11.approx_eq_eps(&other.m11, epsilon) && self.m12.approx_eq_eps(&other.m12, epsilon) && @@ -153,8 +153,8 @@ impl> ApproxEq for Mat2 } } -impl Rand for Mat2 +impl Rand for Mat2 { - fn rand(rng: &mut R) -> Mat2 + fn rand(rng: &mut R) -> Mat2 { Mat2::new(rng.gen(), rng.gen(), rng.gen(), rng.gen()) } } diff --git a/src/dim2/vec2.rs b/src/dim2/vec2.rs index fcb6b4fe..68d97609 100644 --- a/src/dim2/vec2.rs +++ b/src/dim2/vec2.rs @@ -12,43 +12,43 @@ use traits::translation::Translation; use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; #[deriving(Eq, ToStr)] -pub struct Vec2 +pub struct Vec2 { - x : T, - y : T + x : N, + y : N } -impl Vec2 +impl Vec2 { - pub fn new(x: T, y: T) -> Vec2 + pub fn new(x: N, y: N) -> Vec2 { Vec2 {x: x, y: y} } } -impl Dim for Vec2 +impl Dim for Vec2 { fn dim() -> uint { 2 } } -impl> Add, Vec2> for Vec2 +impl> Add, Vec2> for Vec2 { - fn add(&self, other: &Vec2) -> Vec2 + fn add(&self, other: &Vec2) -> Vec2 { Vec2::new(self.x + other.x, self.y + other.y) } } -impl> Sub, Vec2> for Vec2 +impl> Sub, Vec2> for Vec2 { - fn sub(&self, other: &Vec2) -> Vec2 + fn sub(&self, other: &Vec2) -> Vec2 { Vec2::new(self.x - other.x, self.y - other.y) } } -impl> -ScalarMul for Vec2 +impl> +ScalarMul for Vec2 { - fn scalar_mul(&self, s: &T) -> Vec2 + fn scalar_mul(&self, s: &N) -> Vec2 { Vec2 { x: self.x * *s, y: self.y * *s } } - fn scalar_mul_inplace(&mut self, s: &T) + fn scalar_mul_inplace(&mut self, s: &N) { self.x *= *s; self.y *= *s; @@ -56,86 +56,86 @@ ScalarMul for Vec2 } -impl> -ScalarDiv for Vec2 +impl> +ScalarDiv for Vec2 { - fn scalar_div(&self, s: &T) -> Vec2 + fn scalar_div(&self, s: &N) -> Vec2 { Vec2 { x: self.x / *s, y: self.y / *s } } - fn scalar_div_inplace(&mut self, s: &T) + fn scalar_div_inplace(&mut self, s: &N) { self.x /= *s; self.y /= *s; } } -impl> -ScalarAdd for Vec2 +impl> +ScalarAdd for Vec2 { - fn scalar_add(&self, s: &T) -> Vec2 + fn scalar_add(&self, s: &N) -> Vec2 { Vec2 { x: self.x + *s, y: self.y + *s } } - fn scalar_add_inplace(&mut self, s: &T) + fn scalar_add_inplace(&mut self, s: &N) { self.x += *s; self.y += *s; } } -impl> -ScalarSub for Vec2 +impl> +ScalarSub for Vec2 { - fn scalar_sub(&self, s: &T) -> Vec2 + fn scalar_sub(&self, s: &N) -> Vec2 { Vec2 { x: self.x - *s, y: self.y - *s } } - fn scalar_sub_inplace(&mut self, s: &T) + fn scalar_sub_inplace(&mut self, s: &N) { self.x -= *s; self.y -= *s; } } -impl> Translation> for Vec2 +impl> Translation> for Vec2 { - fn translation(&self) -> Vec2 + fn translation(&self) -> Vec2 { *self } - fn translated(&self, t: &Vec2) -> Vec2 + fn translated(&self, t: &Vec2) -> Vec2 { self + *t } - fn translate(&mut self, t: &Vec2) + fn translate(&mut self, t: &Vec2) { *self += *t; } } -impl + Add> Dot for Vec2 +impl + Add> Dot for Vec2 { - fn dot(&self, other : &Vec2) -> T + fn dot(&self, other : &Vec2) -> N { self.x * other.x + self.y * other.y } } -impl + Add + Sub> SubDot for Vec2 +impl + Add + Sub> SubDot for Vec2 { - fn sub_dot(&self, a: &Vec2, b: &Vec2) -> T + fn sub_dot(&self, a: &Vec2, b: &Vec2) -> N { (self.x - a.x) * b.x + (self.y - a.y) * b.y } } -impl + Add + Div + Algebraic> -Norm for Vec2 +impl + Add + Div + Algebraic> +Norm for Vec2 { - fn sqnorm(&self) -> T + fn sqnorm(&self) -> N { self.dot(self) } - fn norm(&self) -> T + fn norm(&self) -> N { self.sqnorm().sqrt() } - fn normalized(&self) -> Vec2 + fn normalized(&self) -> Vec2 { let l = self.norm(); Vec2::new(self.x / l, self.y / l) } - fn normalize(&mut self) -> T + fn normalize(&mut self) -> N { let l = self.norm(); @@ -146,21 +146,21 @@ Norm for Vec2 } } -impl + Sub> Cross> for Vec2 +impl + Sub> Cross> for Vec2 { - fn cross(&self, other : &Vec2) -> Vec1 + fn cross(&self, other : &Vec2) -> Vec1 { Vec1::new(self.x * other.y - self.y * other.x) } } -impl> Neg> for Vec2 +impl> Neg> for Vec2 { - fn neg(&self) -> Vec2 + fn neg(&self) -> Vec2 { Vec2::new(-self.x, -self.y) } } -impl Zero for Vec2 +impl Zero for Vec2 { - fn zero() -> Vec2 + fn zero() -> Vec2 { let _0 = Zero::zero(); Vec2::new(_0, _0) @@ -170,36 +170,36 @@ impl Zero for Vec2 { self.x.is_zero() && self.y.is_zero() } } -impl> Basis for Vec2 +impl> Basis for Vec2 { - fn canonical_basis() -> ~[Vec2] + fn canonical_basis() -> ~[Vec2] { // FIXME: this should be static ~[ Vec2::new(One::one(), Zero::zero()), Vec2::new(Zero::zero(), One::one()) ] } - fn orthogonal_subspace_basis(&self) -> ~[Vec2] + fn orthogonal_subspace_basis(&self) -> ~[Vec2] { ~[ Vec2::new(-self.y, self.x) ] } } -impl> ApproxEq for Vec2 +impl> ApproxEq for Vec2 { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &Vec2) -> bool + fn approx_eq(&self, other: &Vec2) -> bool { self.x.approx_eq(&other.x) && self.y.approx_eq(&other.y) } - fn approx_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &Vec2, epsilon: &N) -> bool { self.x.approx_eq_eps(&other.x, epsilon) && self.y.approx_eq_eps(&other.y, epsilon) } } -impl Rand for Vec2 +impl Rand for Vec2 { - fn rand(rng: &mut R) -> Vec2 + fn rand(rng: &mut R) -> Vec2 { Vec2::new(rng.gen(), rng.gen()) } } diff --git a/src/dim3/mat3.rs b/src/dim3/mat3.rs index 25389ad9..8fdad125 100644 --- a/src/dim3/mat3.rs +++ b/src/dim3/mat3.rs @@ -9,18 +9,18 @@ use traits::workarounds::rlmul::{RMul, LMul}; use dim3::vec3::Vec3; #[deriving(Eq, ToStr)] -pub struct Mat3 +pub struct Mat3 { - m11: T, m12: T, m13: T, - m21: T, m22: T, m23: T, - m31: T, m32: T, m33: T + m11: N, m12: N, m13: N, + m21: N, m22: N, m23: N, + m31: N, m32: N, m33: N } -impl Mat3 +impl Mat3 { - pub fn new(m11: T, m12: T, m13: T, - m21: T, m22: T, m23: T, - m31: T, m32: T, m33: T) -> Mat3 + pub fn new(m11: N, m12: N, m13: N, + m21: N, m22: N, m23: N, + m31: N, m32: N, m33: N) -> Mat3 { Mat3 { @@ -31,15 +31,15 @@ impl Mat3 } } -impl Dim for Mat3 +impl Dim for Mat3 { fn dim() -> uint { 3 } } -impl One for Mat3 +impl One for Mat3 { - fn one() -> Mat3 + fn one() -> Mat3 { let (_0, _1) = (Zero::zero(), One::one()); return Mat3::new(_1, _0, _0, @@ -48,9 +48,9 @@ impl One for Mat3 } } -impl Zero for Mat3 +impl Zero for Mat3 { - fn zero() -> Mat3 + fn zero() -> Mat3 { let _0 = Zero::zero(); return Mat3::new(_0, _0, _0, @@ -66,9 +66,9 @@ impl Zero for Mat3 } } -impl + Add> Mul, Mat3> for Mat3 +impl + Add> Mul, Mat3> for Mat3 { - fn mul(&self, other: &Mat3) -> Mat3 + fn mul(&self, other: &Mat3) -> Mat3 { Mat3::new( self.m11 * other.m11 + self.m12 * other.m21 + self.m13 * other.m31, @@ -86,9 +86,9 @@ impl + Add> Mul, Mat3> for Mat3 } } -impl + Mul> RMul> for Mat3 +impl + Mul> RMul> for Mat3 { - fn rmul(&self, other: &Vec3) -> Vec3 + fn rmul(&self, other: &Vec3) -> Vec3 { Vec3::new( self.m11 * other.x + self.m12 * other.y + self.m13 * other.z, @@ -98,9 +98,9 @@ impl + Mul> RMul> for Mat3 } } -impl + Mul> LMul> for Mat3 +impl + Mul> LMul> for Mat3 { - fn lmul(&self, other: &Vec3) -> Vec3 + fn lmul(&self, other: &Vec3) -> Vec3 { Vec3::new( self.m11 * other.x + self.m21 * other.y + self.m31 * other.z, @@ -110,10 +110,10 @@ impl + Mul> LMul> for Mat3 } } -impl + Div + Sub + Add + Neg + Zero> -Inv for Mat3 +impl + Div + Sub + Add + Neg + Zero> +Inv for Mat3 { - fn inverse(&self) -> Mat3 + fn inverse(&self) -> Mat3 { let mut res = *self; @@ -150,9 +150,9 @@ Inv for Mat3 } } -impl Transpose for Mat3 +impl Transpose for Mat3 { - fn transposed(&self) -> Mat3 + fn transposed(&self) -> Mat3 { Mat3::new(self.m11, self.m21, self.m31, self.m12, self.m22, self.m32, @@ -167,12 +167,12 @@ impl Transpose for Mat3 } } -impl> ApproxEq for Mat3 +impl> ApproxEq for Mat3 { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &Mat3) -> bool + fn approx_eq(&self, other: &Mat3) -> bool { self.m11.approx_eq(&other.m11) && self.m12.approx_eq(&other.m12) && @@ -187,7 +187,7 @@ impl> ApproxEq for Mat3 self.m33.approx_eq(&other.m33) } - fn approx_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &Mat3, epsilon: &N) -> bool { self.m11.approx_eq_eps(&other.m11, epsilon) && self.m12.approx_eq_eps(&other.m12, epsilon) && @@ -203,9 +203,9 @@ impl> ApproxEq for Mat3 } } -impl Rand for Mat3 +impl Rand for Mat3 { - fn rand(rng: &mut R) -> Mat3 + fn rand(rng: &mut R) -> Mat3 { Mat3::new(rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen(), rng.gen(), diff --git a/src/dim3/vec3.rs b/src/dim3/vec3.rs index 219256af..70842146 100644 --- a/src/dim3/vec3.rs +++ b/src/dim3/vec3.rs @@ -11,44 +11,44 @@ use traits::translation::Translation; use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; #[deriving(Eq, ToStr)] -pub struct Vec3 +pub struct Vec3 { - x : T, - y : T, - z : T + x : N, + y : N, + z : N } -impl Vec3 +impl Vec3 { - pub fn new(x: T, y: T, z: T) -> Vec3 + pub fn new(x: N, y: N, z: N) -> Vec3 { Vec3 {x: x, y: y, z: z} } } -impl Dim for Vec3 +impl Dim for Vec3 { fn dim() -> uint { 3 } } -impl> Add, Vec3> for Vec3 +impl> Add, Vec3> for Vec3 { - fn add(&self, other: &Vec3) -> Vec3 + fn add(&self, other: &Vec3) -> Vec3 { Vec3::new(self.x + other.x, self.y + other.y, self.z + other.z) } } -impl> Sub, Vec3> for Vec3 +impl> Sub, Vec3> for Vec3 { - fn sub(&self, other: &Vec3) -> Vec3 + fn sub(&self, other: &Vec3) -> Vec3 { Vec3::new(self.x - other.x, self.y - other.y, self.z - other.z) } } -impl> -ScalarMul for Vec3 +impl> +ScalarMul for Vec3 { - fn scalar_mul(&self, s: &T) -> Vec3 + fn scalar_mul(&self, s: &N) -> Vec3 { Vec3 { x: self.x * *s, y: self.y * *s, z: self.z * *s } } - fn scalar_mul_inplace(&mut self, s: &T) + fn scalar_mul_inplace(&mut self, s: &N) { self.x *= *s; self.y *= *s; @@ -57,13 +57,13 @@ ScalarMul for Vec3 } -impl> -ScalarDiv for Vec3 +impl> +ScalarDiv for Vec3 { - fn scalar_div(&self, s: &T) -> Vec3 + fn scalar_div(&self, s: &N) -> Vec3 { Vec3 { x: self.x / *s, y: self.y / *s, z: self.z / *s } } - fn scalar_div_inplace(&mut self, s: &T) + fn scalar_div_inplace(&mut self, s: &N) { self.x /= *s; self.y /= *s; @@ -71,13 +71,13 @@ ScalarDiv for Vec3 } } -impl> -ScalarAdd for Vec3 +impl> +ScalarAdd for Vec3 { - fn scalar_add(&self, s: &T) -> Vec3 + fn scalar_add(&self, s: &N) -> Vec3 { Vec3 { x: self.x + *s, y: self.y + *s, z: self.z + *s } } - fn scalar_add_inplace(&mut self, s: &T) + fn scalar_add_inplace(&mut self, s: &N) { self.x += *s; self.y += *s; @@ -85,13 +85,13 @@ ScalarAdd for Vec3 } } -impl> -ScalarSub for Vec3 +impl> +ScalarSub for Vec3 { - fn scalar_sub(&self, s: &T) -> Vec3 + fn scalar_sub(&self, s: &N) -> Vec3 { Vec3 { x: self.x - *s, y: self.y - *s, z: self.z - *s } } - fn scalar_sub_inplace(&mut self, s: &T) + fn scalar_sub_inplace(&mut self, s: &N) { self.x -= *s; self.y -= *s; @@ -99,55 +99,55 @@ ScalarSub for Vec3 } } -impl> Translation> for Vec3 +impl> Translation> for Vec3 { - fn translation(&self) -> Vec3 + fn translation(&self) -> Vec3 { *self } - fn translated(&self, t: &Vec3) -> Vec3 + fn translated(&self, t: &Vec3) -> Vec3 { self + *t } - fn translate(&mut self, t: &Vec3) + fn translate(&mut self, t: &Vec3) { *self += *t; } } -impl> Neg> for Vec3 +impl> Neg> for Vec3 { - fn neg(&self) -> Vec3 + fn neg(&self) -> Vec3 { Vec3::new(-self.x, -self.y, -self.z) } } -impl + Add> Dot for Vec3 +impl + Add> Dot for Vec3 { - fn dot(&self, other : &Vec3) -> T + fn dot(&self, other : &Vec3) -> N { self.x * other.x + self.y * other.y + self.z * other.z } } -impl + Add + Sub> SubDot for Vec3 +impl + Add + Sub> SubDot for Vec3 { - fn sub_dot(&self, a: &Vec3, b: &Vec3) -> T + fn sub_dot(&self, a: &Vec3, b: &Vec3) -> N { (self.x - a.x) * b.x + (self.y - a.y) * b.y + (self.z - a.z) * b.z } } -impl + Add + Div + Algebraic> -Norm for Vec3 +impl + Add + Div + Algebraic> +Norm for Vec3 { - fn sqnorm(&self) -> T + fn sqnorm(&self) -> N { self.dot(self) } - fn norm(&self) -> T + fn norm(&self) -> N { self.sqnorm().sqrt() } - fn normalized(&self) -> Vec3 + fn normalized(&self) -> Vec3 { let l = self.norm(); Vec3::new(self.x / l, self.y / l, self.z / l) } - fn normalize(&mut self) -> T + fn normalize(&mut self) -> N { let l = self.norm(); @@ -159,9 +159,9 @@ Norm for Vec3 } } -impl + Sub> Cross> for Vec3 +impl + Sub> Cross> for Vec3 { - fn cross(&self, other : &Vec3) -> Vec3 + fn cross(&self, other : &Vec3) -> Vec3 { Vec3::new( self.y * other.z - self.z * other.y, @@ -171,9 +171,9 @@ impl + Sub> Cross> for Vec3 } } -impl Zero for Vec3 +impl Zero for Vec3 { - fn zero() -> Vec3 + fn zero() -> Vec3 { let _0 = Zero::zero(); Vec3::new(_0, _0, _0) @@ -183,11 +183,11 @@ impl Zero for Vec3 { self.x.is_zero() && self.y.is_zero() && self.z.is_zero() } } -impl + Ord + Mul + Sub + Add + - Div + Algebraic> -Basis for Vec3 +impl + Ord + Mul + Sub + Add + + Div + Algebraic> +Basis for Vec3 { - fn canonical_basis() -> ~[Vec3] + fn canonical_basis() -> ~[Vec3] { // FIXME: this should be static ~[ Vec3::new(One::one(), Zero::zero(), Zero::zero()), @@ -195,7 +195,7 @@ Basis for Vec3 Vec3::new(Zero::zero(), Zero::zero(), One::one()) ] } - fn orthogonal_subspace_basis(&self) -> ~[Vec3] + fn orthogonal_subspace_basis(&self) -> ~[Vec3] { let a = if (abs(self.x) > abs(self.y)) @@ -207,19 +207,19 @@ Basis for Vec3 } } -impl> ApproxEq for Vec3 +impl> ApproxEq for Vec3 { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &Vec3) -> bool + fn approx_eq(&self, other: &Vec3) -> bool { self.x.approx_eq(&other.x) && self.y.approx_eq(&other.y) && self.z.approx_eq(&other.z) } - fn approx_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &Vec3, epsilon: &N) -> bool { self.x.approx_eq_eps(&other.x, epsilon) && self.y.approx_eq_eps(&other.y, epsilon) && @@ -227,8 +227,8 @@ impl> ApproxEq for Vec3 } } -impl Rand for Vec3 +impl Rand for Vec3 { - fn rand(rng: &mut R) -> Vec3 + fn rand(rng: &mut R) -> Vec3 { Vec3::new(rng.gen(), rng.gen(), rng.gen()) } } diff --git a/src/ndim/dmat.rs b/src/ndim/dmat.rs index 71262076..b46b41d0 100644 --- a/src/ndim/dmat.rs +++ b/src/ndim/dmat.rs @@ -10,22 +10,22 @@ use traits::workarounds::rlmul::{RMul, LMul}; use ndim::dvec::{DVec, zero_vec_with_dim}; #[deriving(Eq, ToStr, Clone)] -pub struct DMat +pub struct DMat { dim: uint, // FIXME: handle more than just square matrices - mij: ~[T] + mij: ~[N] } -pub fn zero_mat_with_dim(dim: uint) -> DMat +pub fn zero_mat_with_dim(dim: uint) -> DMat { DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } } -pub fn is_zero_mat(mat: &DMat) -> bool +pub fn is_zero_mat(mat: &DMat) -> bool { mat.mij.all(|e| e.is_zero()) } -pub fn one_mat_with_dim(dim: uint) -> DMat +pub fn one_mat_with_dim(dim: uint) -> DMat { let mut res = zero_mat_with_dim(dim); - let _1 = One::one::(); + let _1 = One::one::(); for iterate(0u, dim) |i| { res.set(i, i, &_1); } @@ -33,19 +33,19 @@ pub fn one_mat_with_dim(dim: uint) -> DMat res } -impl DMat +impl DMat { pub fn offset(&self, i: uint, j: uint) -> uint { i * self.dim + j } - pub fn set(&mut self, i: uint, j: uint, t: &T) + pub fn set(&mut self, i: uint, j: uint, t: &N) { assert!(i < self.dim); assert!(j < self.dim); self.mij[self.offset(i, j)] = *t } - pub fn at(&self, i: uint, j: uint) -> T + pub fn at(&self, i: uint, j: uint) -> N { assert!(i < self.dim); assert!(j < self.dim); @@ -53,16 +53,16 @@ impl DMat } } -impl Index<(uint, uint), T> for DMat +impl Index<(uint, uint), N> for DMat { - fn index(&self, &(i, j): &(uint, uint)) -> T + fn index(&self, &(i, j): &(uint, uint)) -> N { self.at(i, j) } } -impl + Add + Zero> -Mul, DMat> for DMat +impl + Add + Zero> +Mul, DMat> for DMat { - fn mul(&self, other: &DMat) -> DMat + fn mul(&self, other: &DMat) -> DMat { assert!(self.dim == other.dim); @@ -73,7 +73,7 @@ Mul, DMat> for DMat { for iterate(0u, dim) |j| { - let mut acc = Zero::zero::(); + let mut acc = Zero::zero::(); for iterate(0u, dim) |k| { acc += self.at(i, k) * other.at(k, j); } @@ -86,15 +86,15 @@ Mul, DMat> for DMat } } -impl + Mul + Zero> -RMul> for DMat +impl + Mul + Zero> +RMul> for DMat { - fn rmul(&self, other: &DVec) -> DVec + fn rmul(&self, other: &DVec) -> DVec { assert!(self.dim == other.at.len()); let dim = self.dim; - let mut res : DVec = zero_vec_with_dim(dim); + let mut res : DVec = zero_vec_with_dim(dim); for iterate(0u, dim) |i| { @@ -106,15 +106,15 @@ RMul> for DMat } } -impl + Mul + Zero> -LMul> for DMat +impl + Mul + Zero> +LMul> for DMat { - fn lmul(&self, other: &DVec) -> DVec + fn lmul(&self, other: &DVec) -> DVec { assert!(self.dim == other.at.len()); let dim = self.dim; - let mut res : DVec = zero_vec_with_dim(dim); + let mut res : DVec = zero_vec_with_dim(dim); for iterate(0u, dim) |i| { @@ -126,12 +126,12 @@ LMul> for DMat } } -impl -Inv for DMat +impl +Inv for DMat { - fn inverse(&self) -> DMat + fn inverse(&self) -> DMat { - let mut res : DMat = self.clone(); + let mut res : DMat = self.clone(); res.invert(); @@ -141,8 +141,8 @@ Inv for DMat fn invert(&mut self) { let dim = self.dim; - let mut res = one_mat_with_dim::(dim); - let _0T = Zero::zero::(); + let mut res = one_mat_with_dim::(dim); + let _0T = Zero::zero::(); // inversion using Gauss-Jordan elimination for iterate(0u, dim) |k| @@ -215,9 +215,9 @@ Inv for DMat } } -impl Transpose for DMat +impl Transpose for DMat { - fn transposed(&self) -> DMat + fn transposed(&self) -> DMat { let mut res = copy *self; @@ -243,19 +243,19 @@ impl Transpose for DMat } } -impl> ApproxEq for DMat +impl> ApproxEq for DMat { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &DMat) -> bool + fn approx_eq(&self, other: &DMat) -> bool { let mut zip = self.mij.iter().zip(other.mij.iter()); do zip.all |(a, b)| { a.approx_eq(b) } } - fn approx_eq_eps(&self, other: &DMat, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &DMat, epsilon: &N) -> bool { let mut zip = self.mij.iter().zip(other.mij.iter()); diff --git a/src/ndim/dvec.rs b/src/ndim/dvec.rs index 5654fd49..eb060151 100644 --- a/src/ndim/dvec.rs +++ b/src/ndim/dvec.rs @@ -12,27 +12,27 @@ use traits::translation::Translation; use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; #[deriving(Eq, ToStr, Clone)] -pub struct DVec +pub struct DVec { - at: ~[T] + at: ~[N] } -pub fn zero_vec_with_dim(dim: uint) -> DVec -{ DVec { at: from_elem(dim, Zero::zero::()) } } +pub fn zero_vec_with_dim(dim: uint) -> DVec +{ DVec { at: from_elem(dim, Zero::zero::()) } } -pub fn is_zero_vec(vec: &DVec) -> bool +pub fn is_zero_vec(vec: &DVec) -> bool { vec.at.all(|e| e.is_zero()) } // FIXME: is Clone needed? -impl> DVec +impl> DVec { - pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec] + pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec] { - let mut res : ~[DVec] = ~[]; + let mut res : ~[DVec] = ~[]; for iterate(0u, dim) |i| { - let mut basis_element : DVec = zero_vec_with_dim(dim); + let mut basis_element : DVec = zero_vec_with_dim(dim); basis_element.at[i] = One::one(); @@ -42,16 +42,16 @@ impl> DVec res } - pub fn orthogonal_subspace_basis(&self) -> ~[DVec] + pub fn orthogonal_subspace_basis(&self) -> ~[DVec] { // compute the basis of the orthogonal subspace using Gram-Schmidt // orthogonalization algorithm let dim = self.at.len(); - let mut res : ~[DVec] = ~[]; + let mut res : ~[DVec] = ~[]; for iterate(0u, dim) |i| { - let mut basis_element : DVec = zero_vec_with_dim(self.at.len()); + let mut basis_element : DVec = zero_vec_with_dim(self.at.len()); basis_element.at[i] = One::one(); @@ -75,38 +75,38 @@ impl> DVec } } -impl> Add, DVec> for DVec +impl> Add, DVec> for DVec { - fn add(&self, other: &DVec) -> DVec + fn add(&self, other: &DVec) -> DVec { assert!(self.at.len() == other.at.len()); DVec { at: map_zip(self.at, other.at, | a, b | { *a + *b }) } } } -impl> Sub, DVec> for DVec +impl> Sub, DVec> for DVec { - fn sub(&self, other: &DVec) -> DVec + fn sub(&self, other: &DVec) -> DVec { assert!(self.at.len() == other.at.len()); DVec { at: map_zip(self.at, other.at, | a, b | *a - *b) } } } -impl> Neg> for DVec +impl> Neg> for DVec { - fn neg(&self) -> DVec + fn neg(&self) -> DVec { DVec { at: map(self.at, |a| -a) } } } -impl -Dot for DVec +impl +Dot for DVec { - fn dot(&self, other: &DVec) -> T + fn dot(&self, other: &DVec) -> N { assert!(self.at.len() == other.at.len()); - let mut res = Zero::zero::(); + let mut res = Zero::zero::(); for iterate(0u, self.at.len()) |i| { res += self.at[i] * other.at[i]; } @@ -115,11 +115,11 @@ Dot for DVec } } -impl SubDot for DVec +impl SubDot for DVec { - fn sub_dot(&self, a: &DVec, b: &DVec) -> T + fn sub_dot(&self, a: &DVec, b: &DVec) -> N { - let mut res = Zero::zero::(); + let mut res = Zero::zero::(); for iterate(0u, self.at.len()) |i| { res += (self.at[i] - a.at[i]) * b.at[i]; } @@ -128,13 +128,13 @@ impl SubDot for DVec } } -impl> -ScalarMul for DVec +impl> +ScalarMul for DVec { - fn scalar_mul(&self, s: &T) -> DVec + fn scalar_mul(&self, s: &N) -> DVec { DVec { at: map(self.at, |a| a * *s) } } - fn scalar_mul_inplace(&mut self, s: &T) + fn scalar_mul_inplace(&mut self, s: &N) { for iterate(0u, self.at.len()) |i| { self.at[i] *= *s; } @@ -142,76 +142,76 @@ ScalarMul for DVec } -impl> -ScalarDiv for DVec +impl> +ScalarDiv for DVec { - fn scalar_div(&self, s: &T) -> DVec + fn scalar_div(&self, s: &N) -> DVec { DVec { at: map(self.at, |a| a / *s) } } - fn scalar_div_inplace(&mut self, s: &T) + fn scalar_div_inplace(&mut self, s: &N) { for iterate(0u, self.at.len()) |i| { self.at[i] /= *s; } } } -impl> -ScalarAdd for DVec +impl> +ScalarAdd for DVec { - fn scalar_add(&self, s: &T) -> DVec + fn scalar_add(&self, s: &N) -> DVec { DVec { at: map(self.at, |a| a + *s) } } - fn scalar_add_inplace(&mut self, s: &T) + fn scalar_add_inplace(&mut self, s: &N) { for iterate(0u, self.at.len()) |i| { self.at[i] += *s; } } } -impl> -ScalarSub for DVec +impl> +ScalarSub for DVec { - fn scalar_sub(&self, s: &T) -> DVec + fn scalar_sub(&self, s: &N) -> DVec { DVec { at: map(self.at, |a| a - *s) } } - fn scalar_sub_inplace(&mut self, s: &T) + fn scalar_sub_inplace(&mut self, s: &N) { for iterate(0u, self.at.len()) |i| { self.at[i] -= *s; } } } -impl> Translation> for DVec +impl> Translation> for DVec { - fn translation(&self) -> DVec + fn translation(&self) -> DVec { self.clone() } - fn translated(&self, t: &DVec) -> DVec + fn translated(&self, t: &DVec) -> DVec { self + *t } - fn translate(&mut self, t: &DVec) + fn translate(&mut self, t: &DVec) { *self = *self + *t; } } -impl -Norm for DVec +impl +Norm for DVec { - fn sqnorm(&self) -> T + fn sqnorm(&self) -> N { self.dot(self) } - fn norm(&self) -> T + fn norm(&self) -> N { self.sqnorm().sqrt() } - fn normalized(&self) -> DVec + fn normalized(&self) -> DVec { - let mut res : DVec = self.clone(); + let mut res : DVec = self.clone(); res.normalize(); res } - fn normalize(&mut self) -> T + fn normalize(&mut self) -> N { let l = self.norm(); @@ -222,19 +222,19 @@ Norm for DVec } } -impl> ApproxEq for DVec +impl> ApproxEq for DVec { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &DVec) -> bool + fn approx_eq(&self, other: &DVec) -> bool { let mut zip = self.at.iter().zip(other.at.iter()); do zip.all |(a, b)| { a.approx_eq(b) } } - fn approx_eq_eps(&self, other: &DVec, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &DVec, epsilon: &N) -> bool { let mut zip = self.at.iter().zip(other.at.iter()); diff --git a/src/ndim/nmat.rs b/src/ndim/nmat.rs index c16b12cb..84fcd704 100644 --- a/src/ndim/nmat.rs +++ b/src/ndim/nmat.rs @@ -15,58 +15,58 @@ use ndim::nvec::NVec; // It can be anything implementing the Dim trait. However, to avoid confusion, // using d0, d1, d2, d3 and d4 tokens are prefered. #[deriving(Eq, ToStr)] -pub struct NMat -{ mij: DMat } +pub struct NMat +{ mij: DMat } -impl NMat +impl NMat { fn offset(i: uint, j: uint) -> uint { i * Dim::dim::() + j } - fn set(&mut self, i: uint, j: uint, t: &T) + fn set(&mut self, i: uint, j: uint, t: &N) { self.mij.set(i, j, t) } } -impl Dim for NMat +impl Dim for NMat { fn dim() -> uint { Dim::dim::() } } -impl Index<(uint, uint), T> for NMat +impl Index<(uint, uint), N> for NMat { - fn index(&self, &idx: &(uint, uint)) -> T + fn index(&self, &idx: &(uint, uint)) -> N { self.mij[idx] } } -impl One for NMat +impl One for NMat { - fn one() -> NMat + fn one() -> NMat { NMat { mij: one_mat_with_dim(Dim::dim::()) } } } -impl Zero for NMat +impl Zero for NMat { - fn zero() -> NMat + fn zero() -> NMat { NMat { mij: zero_mat_with_dim(Dim::dim::()) } } fn is_zero(&self) -> bool { is_zero_mat(&self.mij) } } -impl + Add + Zero> -Mul, NMat> for NMat +impl + Add + Zero> +Mul, NMat> for NMat { - fn mul(&self, other: &NMat) -> NMat + fn mul(&self, other: &NMat) -> NMat { let dim = Dim::dim::(); - let mut res = Zero::zero::>(); + let mut res = Zero::zero::>(); for iterate(0u, dim) |i| { for iterate(0u, dim) |j| { - let mut acc: T = Zero::zero(); + let mut acc: N = Zero::zero(); for iterate(0u, dim) |k| { acc += self[(i, k)] * other[(k, j)]; } @@ -79,13 +79,13 @@ Mul, NMat> for NMat } } -impl + Mul + Zero> -RMul> for NMat +impl + Mul + Zero> +RMul> for NMat { - fn rmul(&self, other: &NVec) -> NVec + fn rmul(&self, other: &NVec) -> NVec { let dim = Dim::dim::(); - let mut res : NVec = Zero::zero(); + let mut res : NVec = Zero::zero(); for iterate(0u, dim) |i| { @@ -97,13 +97,13 @@ RMul> for NMat } } -impl + Mul + Zero> -LMul> for NMat +impl + Mul + Zero> +LMul> for NMat { - fn lmul(&self, other: &NVec) -> NVec + fn lmul(&self, other: &NVec) -> NVec { let dim = Dim::dim::(); - let mut res : NVec = Zero::zero(); + let mut res : NVec = Zero::zero(); for iterate(0u, dim) |i| { @@ -115,19 +115,19 @@ LMul> for NMat } } -impl -Inv for NMat +impl +Inv for NMat { - fn inverse(&self) -> NMat + fn inverse(&self) -> NMat { NMat { mij: self.mij.inverse() } } fn invert(&mut self) { self.mij.invert() } } -impl Transpose for NMat +impl Transpose for NMat { - fn transposed(&self) -> NMat + fn transposed(&self) -> NMat { let mut res = copy *self; @@ -140,24 +140,24 @@ impl Transpose for NMat { self.mij.transpose() } } -impl> ApproxEq for NMat +impl> ApproxEq for NMat { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &NMat) -> bool + fn approx_eq(&self, other: &NMat) -> bool { self.mij.approx_eq(&other.mij) } - fn approx_eq_eps(&self, other: &NMat, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &NMat, epsilon: &N) -> bool { self.mij.approx_eq_eps(&other.mij, epsilon) } } -impl Rand for NMat +impl Rand for NMat { - fn rand(rng: &mut R) -> NMat + fn rand(rng: &mut R) -> NMat { let dim = Dim::dim::(); - let mut res : NMat = Zero::zero(); + let mut res : NMat = Zero::zero(); for iterate(0u, dim) |i| { diff --git a/src/ndim/nvec.rs b/src/ndim/nvec.rs index adeb37f9..fe0e262c 100644 --- a/src/ndim/nvec.rs +++ b/src/ndim/nvec.rs @@ -21,174 +21,174 @@ use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub} // FIXME: it might be possible to implement type-level integers and use them // here? #[deriving(Eq, ToStr)] -pub struct NVec -{ at: DVec } +pub struct NVec +{ at: DVec } -impl Dim for NVec +impl Dim for NVec { fn dim() -> uint { Dim::dim::() } } -impl Clone for NVec +impl Clone for NVec { - fn clone(&self) -> NVec + fn clone(&self) -> NVec { NVec{ at: self.at.clone() } } } -impl> Add, NVec> for NVec +impl> Add, NVec> for NVec { - fn add(&self, other: &NVec) -> NVec + fn add(&self, other: &NVec) -> NVec { NVec { at: self.at + other.at } } } -impl> Sub, NVec> for NVec +impl> Sub, NVec> for NVec { - fn sub(&self, other: &NVec) -> NVec + fn sub(&self, other: &NVec) -> NVec { NVec { at: self.at - other.at } } } -impl> Neg> for NVec +impl> Neg> for NVec { - fn neg(&self) -> NVec + fn neg(&self) -> NVec { NVec { at: -self.at } } } -impl -Dot for NVec +impl +Dot for NVec { - fn dot(&self, other: &NVec) -> T + fn dot(&self, other: &NVec) -> N { self.at.dot(&other.at) } } -impl SubDot for NVec +impl SubDot for NVec { - fn sub_dot(&self, a: &NVec, b: &NVec) -> T + fn sub_dot(&self, a: &NVec, b: &NVec) -> N { self.at.sub_dot(&a.at, &b.at) } } -impl> -ScalarMul for NVec +impl> +ScalarMul for NVec { - fn scalar_mul(&self, s: &T) -> NVec + fn scalar_mul(&self, s: &N) -> NVec { NVec { at: self.at.scalar_mul(s) } } - fn scalar_mul_inplace(&mut self, s: &T) + fn scalar_mul_inplace(&mut self, s: &N) { self.at.scalar_mul_inplace(s) } } -impl> -ScalarDiv for NVec +impl> +ScalarDiv for NVec { - fn scalar_div(&self, s: &T) -> NVec + fn scalar_div(&self, s: &N) -> NVec { NVec { at: self.at.scalar_div(s) } } - fn scalar_div_inplace(&mut self, s: &T) + fn scalar_div_inplace(&mut self, s: &N) { self.at.scalar_div_inplace(s) } } -impl> -ScalarAdd for NVec +impl> +ScalarAdd for NVec { - fn scalar_add(&self, s: &T) -> NVec + fn scalar_add(&self, s: &N) -> NVec { NVec { at: self.at.scalar_add(s) } } - fn scalar_add_inplace(&mut self, s: &T) + fn scalar_add_inplace(&mut self, s: &N) { self.at.scalar_add_inplace(s) } } -impl> -ScalarSub for NVec +impl> +ScalarSub for NVec { - fn scalar_sub(&self, s: &T) -> NVec + fn scalar_sub(&self, s: &N) -> NVec { NVec { at: self.at.scalar_sub(s) } } - fn scalar_sub_inplace(&mut self, s: &T) + fn scalar_sub_inplace(&mut self, s: &N) { self.scalar_sub_inplace(s) } } -impl> Translation> for NVec +impl> Translation> for NVec { - fn translation(&self) -> NVec + fn translation(&self) -> NVec { self.clone() } - fn translated(&self, t: &NVec) -> NVec + fn translated(&self, t: &NVec) -> NVec { self + *t } - fn translate(&mut self, t: &NVec) + fn translate(&mut self, t: &NVec) { *self = *self + *t; } } -impl -Norm for NVec +impl +Norm for NVec { - fn sqnorm(&self) -> T + fn sqnorm(&self) -> N { self.dot(self) } - fn norm(&self) -> T + fn norm(&self) -> N { self.sqnorm().sqrt() } - fn normalized(&self) -> NVec + fn normalized(&self) -> NVec { - let mut res : NVec = self.clone(); + let mut res : NVec = self.clone(); res.normalize(); res } - fn normalize(&mut self) -> T + fn normalize(&mut self) -> N { self.at.normalize() } } impl> -Basis for NVec + N: Copy + DivisionRing + Algebraic + Clone + ApproxEq> +Basis for NVec { - fn canonical_basis() -> ~[NVec] + fn canonical_basis() -> ~[NVec] { map(DVec::canonical_basis_with_dim(Dim::dim::()), |&e| NVec { at: e }) } - fn orthogonal_subspace_basis(&self) -> ~[NVec] + fn orthogonal_subspace_basis(&self) -> ~[NVec] { map(self.at.orthogonal_subspace_basis(), |&e| NVec { at: e }) } } // FIXME: I dont really know how te generalize the cross product int // n-dimensions… -// impl + Sub> Cross for NVec +// impl + Sub> Cross for NVec // { -// fn cross(&self, other: &NVec) -> T +// fn cross(&self, other: &NVec) -> N // { self.x * other.y - self.y * other.x } // } -impl Zero for NVec +impl Zero for NVec { - fn zero() -> NVec + fn zero() -> NVec { NVec { at: zero_vec_with_dim(Dim::dim::()) } } fn is_zero(&self) -> bool { is_zero_vec(&self.at) } } -impl> ApproxEq for NVec +impl> ApproxEq for NVec { - fn approx_epsilon() -> T - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N + { ApproxEq::approx_epsilon::() } - fn approx_eq(&self, other: &NVec) -> bool + fn approx_eq(&self, other: &NVec) -> bool { self.at.approx_eq(&other.at) } - fn approx_eq_eps(&self, other: &NVec, epsilon: &T) -> bool + fn approx_eq_eps(&self, other: &NVec, epsilon: &N) -> bool { self.at.approx_eq_eps(&other.at, epsilon) } } -impl Rand for NVec +impl Rand for NVec { - fn rand(rng: &mut R) -> NVec + fn rand(rng: &mut R) -> NVec { let dim = Dim::dim::(); - let mut res : NVec = Zero::zero(); + let mut res : NVec = Zero::zero(); for iterate(0u, dim) |i| { res.at.at[i] = rng.gen() } diff --git a/src/traits/dim.rs b/src/traits/dim.rs index 489b57d2..f35e14af 100644 --- a/src/traits/dim.rs +++ b/src/traits/dim.rs @@ -1,5 +1,5 @@ /** - * Trait of objects having a dimension (in term of spacial dimension). + * Trait of objects having a spacial dimension. */ pub trait Dim { /// The dimension of the object. diff --git a/src/traits/division_ring.rs b/src/traits/division_ring.rs index cf50f689..0fc33343 100644 --- a/src/traits/division_ring.rs +++ b/src/traits/division_ring.rs @@ -7,4 +7,4 @@ use traits::ring::Ring; pub trait DivisionRing : Ring + Div { } -impl> DivisionRing for T; +impl> DivisionRing for N; diff --git a/src/traits/dot.rs b/src/traits/dot.rs index 256aed98..37ce51a8 100644 --- a/src/traits/dot.rs +++ b/src/traits/dot.rs @@ -1,8 +1,8 @@ /** * Trait of objects having a dot product (also called inner product). */ -pub trait Dot +pub trait Dot { /// Computes the dot (inner) product of two objects. - fn dot(&self, &Self) -> T; + fn dot(&self, &Self) -> N; } diff --git a/src/traits/norm.rs b/src/traits/norm.rs index 4eba307f..e2bdece0 100644 --- a/src/traits/norm.rs +++ b/src/traits/norm.rs @@ -1,20 +1,20 @@ /** * Trait of objects having a L² norm and which can be normalized. */ -pub trait Norm +pub trait Norm { /// Computes the norm a an object. - fn norm(&self) -> T; + fn norm(&self) -> N; /** * Computes the squared norm of an object. Usually faster than computing the * norm itself. */ - fn sqnorm(&self) -> T; + fn sqnorm(&self) -> N; /// Gets the normalized version of the argument. fn normalized(&self) -> Self; /// In-place version of `normalized`. - fn normalize(&mut self) -> T; + fn normalize(&mut self) -> N; } diff --git a/src/traits/ring.rs b/src/traits/ring.rs index 7f77afea..1364d3f3 100644 --- a/src/traits/ring.rs +++ b/src/traits/ring.rs @@ -10,5 +10,5 @@ pub trait Ring : Sub + Add + Neg + Mul + One + Zero { } -impl + Add + Neg + Mul + One + Zero> -Ring for T; +impl + Add + Neg + Mul + One + Zero> +Ring for N; diff --git a/src/traits/sub_dot.rs b/src/traits/sub_dot.rs index e0b8b42c..884d68f1 100644 --- a/src/traits/sub_dot.rs +++ b/src/traits/sub_dot.rs @@ -1,4 +1,4 @@ -pub trait SubDot +pub trait SubDot { /** * Short-cut to compute the projecton of a point on a vector, but without @@ -8,5 +8,5 @@ pub trait SubDot * (a - b).dot(c) * */ - fn sub_dot(&self, b: &Self, c: &Self) -> T; + fn sub_dot(&self, b: &Self, c: &Self) -> N; } diff --git a/src/traits/vector_space.rs b/src/traits/vector_space.rs index 6221edec..85c1d2c0 100644 --- a/src/traits/vector_space.rs +++ b/src/traits/vector_space.rs @@ -6,7 +6,7 @@ use traits::workarounds::scalar_op::{ScalarMul, ScalarDiv}; /// structure, the elements of which have addition, substraction, negation, /// scalar multiplication (the scalar being a element of a `DivisionRing`), and /// has a distinct element (`Zero`) neutral wrt the addition. -pub trait VectorSpace +pub trait VectorSpace : Sub + Add + Neg + Zero + - ScalarMul + ScalarDiv + ScalarMul + ScalarDiv { } diff --git a/src/traits/workarounds/scalar_op.rs b/src/traits/workarounds/scalar_op.rs index 720a910b..c2556726 100644 --- a/src/traits/workarounds/scalar_op.rs +++ b/src/traits/workarounds/scalar_op.rs @@ -5,17 +5,17 @@ * exemple does not compile (end with an ICE): * * ~~~ - * trait Mul for T - * trait Mul for T + * trait Mul for N + * trait Mul for N * ~~~ */ -pub trait ScalarMul +pub trait ScalarMul { /// Gets the result of a multiplication by a scalar. - fn scalar_mul(&self, &T) -> Self; + fn scalar_mul(&self, &N) -> Self; /// In-place version of `scalar_mul`. - fn scalar_mul_inplace(&mut self, &T); + fn scalar_mul_inplace(&mut self, &N); } /** @@ -25,17 +25,17 @@ pub trait ScalarMul * exemple does not compile (end with an ICE): * * ~~~ - * trait Div for T - * trait Div for T + * trait Div for N + * trait Div for N * ~~~ */ -pub trait ScalarDiv +pub trait ScalarDiv { /// Gets the result of a division by a scalar. - fn scalar_div(&self, &T) -> Self; + fn scalar_div(&self, &N) -> Self; /// In-place version of `scalar_div`. - fn scalar_div_inplace(&mut self, &T); + fn scalar_div_inplace(&mut self, &N); } /** @@ -45,17 +45,17 @@ pub trait ScalarDiv * exemple does not compile (end with an ICE): * * ~~~ - * trait Add for T - * trait Add for T + * trait Add for N + * trait Add for N * ~~~ */ -pub trait ScalarAdd +pub trait ScalarAdd { /// Gets the result of an addition by a scalar. - fn scalar_add(&self, &T) -> Self; + fn scalar_add(&self, &N) -> Self; /// In-place version of `scalar_add`. - fn scalar_add_inplace(&mut self, &T); + fn scalar_add_inplace(&mut self, &N); } /** @@ -65,15 +65,15 @@ pub trait ScalarAdd * exemple does not compile (end with an ICE): * * ~~~ - * trait Sub for T - * trait Sub for T + * trait Sub for N + * trait Sub for N * ~~~ */ -pub trait ScalarSub +pub trait ScalarSub { /// Gets the result of a subtraction by a scalar. - fn scalar_sub(&self, &T) -> Self; + fn scalar_sub(&self, &N) -> Self; /// In-place version of `scalar_sub`. - fn scalar_sub_inplace(&mut self, &T); + fn scalar_sub_inplace(&mut self, &N); }