diff --git a/src/adaptors/rotmat.rs b/src/adaptors/rotmat.rs index f5e19be6..d484bb76 100644 --- a/src/adaptors/rotmat.rs +++ b/src/adaptors/rotmat.rs @@ -20,40 +20,39 @@ use vec::Vec3; /// the type-level) that it will always represent a rotation. Rotation matrices have some /// properties useful for performances, like the fact that the inversion is simply a transposition. #[deriving(Eq, ToStr, Clone)] -pub struct Rotmat -{ priv submat: M } - -impl Rotmat -{ - /// Gets a copy of the internal representation of the rotation. - pub fn submat(&self) -> M - { self.submat.clone() } +pub struct Rotmat { + priv submat: M } -impl> Rotmat> -{ - /// Builds a 2 dimensional rotation matrix from an angle in radian. - pub fn from_angle(angle: N) -> Rotmat> - { - let (sia, coa) = angle.sin_cos(); - - Rotmat { submat: Mat2::new(coa.clone(), -sia, sia.clone(), coa) } +impl Rotmat { + /// Gets a copy of the internal representation of the rotation. + pub fn submat(&self) -> M { + self.submat.clone() } } -impl Rotmat> -{ +impl> Rotmat> { + /// Builds a 2 dimensional rotation matrix from an angle in radian. + pub fn from_angle(angle: N) -> Rotmat> { + let (sia, coa) = angle.sin_cos(); + + Rotmat { + submat: Mat2::new(coa.clone(), -sia, sia.clone(), coa) + } + } +} + +impl Rotmat> { /// Builds a 3 dimensional rotation matrix from an axis and an angle. /// /// # Arguments /// * `axisangle` - A vector representing the rotation. Its magnitude is the amount of rotation /// in radian. Its direction is the axis of rotation. - pub fn from_axis_angle(axisangle: Vec3) -> Rotmat> - { - if axisangle.sqnorm().is_zero() - { One::one() } - else - { + pub fn from_axis_angle(axisangle: Vec3) -> Rotmat> { + if axisangle.sqnorm().is_zero() { + One::one() + } + else { let mut axis = axisangle; let angle = axis.normalize(); let _1 = One::one::(); @@ -84,8 +83,7 @@ impl Rotmat> } } -impl Rotmat> -{ +impl Rotmat> { /// Reorient this matrix such that its local `x` axis points to a given point. Note that the /// usually known `look_at` function does the same thing but with the `z` axis. See `look_at_z` /// for that. @@ -95,8 +93,7 @@ impl Rotmat> /// with /// * up - Vector pointing `up`. The only requirement of this parameter is to not be colinear /// with `at`. Non-colinearity is not checked. - pub fn look_at(&mut self, at: &Vec3, up: &Vec3) - { + pub fn look_at(&mut self, at: &Vec3, up: &Vec3) { let xaxis = at.normalized(); let zaxis = up.cross(&xaxis).normalized(); let yaxis = zaxis.cross(&xaxis); @@ -113,8 +110,7 @@ impl Rotmat> /// with /// * up - Vector pointing `up`. The only requirement of this parameter is to not be colinear /// with `at`. Non-colinearity is not checked. - pub fn look_at_z(&mut self, at: &Vec3, up: &Vec3) - { + pub fn look_at_z(&mut self, at: &Vec3, up: &Vec3) { let zaxis = at.normalized(); let xaxis = up.cross(&zaxis).normalized(); let yaxis = zaxis.cross(&xaxis); @@ -126,171 +122,179 @@ impl Rotmat> } impl -Rotation> for Rotmat> -{ +Rotation> for Rotmat> { #[inline] - fn rotation(&self) -> Vec1 - { Vec1::new((-self.submat.at((0, 1))).atan2(&self.submat.at((0, 0)))) } + fn rotation(&self) -> Vec1 { + Vec1::new((-self.submat.at((0, 1))).atan2(&self.submat.at((0, 0)))) + } #[inline] - fn inv_rotation(&self) -> Vec1 - { -self.rotation() } + fn inv_rotation(&self) -> Vec1 { + -self.rotation() + } #[inline] - fn rotate_by(&mut self, rot: &Vec1) - { *self = self.rotated(rot) } + fn rotate_by(&mut self, rot: &Vec1) { + *self = self.rotated(rot) + } } impl -Rotatable, Rotmat>> for Rotmat> -{ +Rotatable, Rotmat>> for Rotmat> { #[inline] - fn rotated(&self, rot: &Vec1) -> Rotmat> - { Rotmat::from_angle(rot.x.clone()) * *self } + fn rotated(&self, rot: &Vec1) -> Rotmat> { + Rotmat::from_angle(rot.x.clone()) * *self + } } impl -Rotation> for Rotmat> -{ - #[inline] - fn rotation(&self) -> Vec3 - { fail!("Not yet implemented.") } +Rotation> for Rotmat> { #[inline] + fn rotation(&self) -> Vec3 { + fail!("Not yet implemented.") + } - fn inv_rotation(&self) -> Vec3 - { fail!("Not yet implemented.") } + #[inline] + fn inv_rotation(&self) -> Vec3 { + fail!("Not yet implemented.") + } #[inline] - fn rotate_by(&mut self, rot: &Vec3) - { *self = self.rotated(rot) } + fn rotate_by(&mut self, rot: &Vec3) { + *self = self.rotated(rot) + } } impl -Rotatable, Rotmat>> for Rotmat> -{ +Rotatable, Rotmat>> for Rotmat> { #[inline] - fn rotated(&self, axisangle: &Vec3) -> Rotmat> - { Rotmat::from_axis_angle(axisangle.clone()) * *self } + fn rotated(&self, axisangle: &Vec3) -> Rotmat> { + Rotmat::from_axis_angle(axisangle.clone()) * *self + } } -impl> Rand for Rotmat> -{ +impl> Rand for Rotmat> { #[inline] - fn rand(rng: &mut R) -> Rotmat> - { Rotmat::from_angle(rng.gen()) } + fn rand(rng: &mut R) -> Rotmat> { + Rotmat::from_angle(rng.gen()) + } } -impl + LMul, V> Rotate for Rotmat -{ +impl + LMul, V> Rotate for Rotmat { #[inline] - fn rotate(&self, v: &V) -> V - { self.rmul(v) } + fn rotate(&self, v: &V) -> V { + self.rmul(v) + } #[inline] - fn inv_rotate(&self, v: &V) -> V - { self.lmul(v) } + fn inv_rotate(&self, v: &V) -> V { + self.lmul(v) + } } -impl + LMul, V> Transform for Rotmat -{ +impl + LMul, V> Transform for Rotmat { #[inline] - fn transform_vec(&self, v: &V) -> V - { self.rotate(v) } + fn transform_vec(&self, v: &V) -> V { + self.rotate(v) + } #[inline] - fn inv_transform(&self, v: &V) -> V - { self.inv_rotate(v) } + fn inv_transform(&self, v: &V) -> V { + self.inv_rotate(v) + } } impl -Rand for Rotmat> -{ +Rand for Rotmat> { #[inline] - fn rand(rng: &mut R) -> Rotmat> - { Rotmat::from_axis_angle(rng.gen()) } + fn rand(rng: &mut R) -> Rotmat> { + Rotmat::from_axis_angle(rng.gen()) + } } -impl Dim for Rotmat -{ +impl Dim for Rotmat { #[inline] - fn dim() -> uint - { Dim::dim::() } + fn dim() -> uint { + Dim::dim::() + } } -impl One for Rotmat -{ +impl One for Rotmat { #[inline] - fn one() -> Rotmat - { Rotmat { submat: One::one() } } + fn one() -> Rotmat { + Rotmat { submat: One::one() } + } } -impl> Mul, Rotmat> for Rotmat -{ +impl> Mul, Rotmat> for Rotmat { #[inline] - fn mul(&self, other: &Rotmat) -> Rotmat - { Rotmat { submat: self.submat.mul(&other.submat) } } + fn mul(&self, other: &Rotmat) -> Rotmat { + Rotmat { submat: self.submat.mul(&other.submat) } + } } -impl> RMul for Rotmat -{ +impl> RMul for Rotmat { #[inline] - fn rmul(&self, other: &V) -> V - { self.submat.rmul(other) } + fn rmul(&self, other: &V) -> V { + self.submat.rmul(other) + } } -impl> LMul for Rotmat -{ +impl> LMul for Rotmat { #[inline] - fn lmul(&self, other: &V) -> V - { self.submat.lmul(other) } + fn lmul(&self, other: &V) -> V { + self.submat.lmul(other) + } } -impl Inv for Rotmat -{ +impl Inv for Rotmat { #[inline] - fn inplace_inverse(&mut self) -> bool - { + fn inplace_inverse(&mut self) -> bool { self.transpose(); true } #[inline] - fn inverse(&self) -> Option> - { Some(self.transposed()) } + fn inverse(&self) -> Option> { + Some(self.transposed()) + } } impl -Transpose for Rotmat -{ +Transpose for Rotmat { #[inline] - fn transposed(&self) -> Rotmat - { Rotmat { submat: self.submat.transposed() } } + fn transposed(&self) -> Rotmat { + Rotmat { submat: self.submat.transposed() } + } #[inline] - fn transpose(&mut self) - { self.submat.transpose() } + fn transpose(&mut self) { + self.submat.transpose() + } } // we loose the info that we are a rotation matrix -impl, M2> ToHomogeneous for Rotmat -{ - fn to_homogeneous(&self) -> M2 - { self.submat.to_homogeneous() } +impl, M2> ToHomogeneous for Rotmat { + fn to_homogeneous(&self) -> M2 { + self.submat.to_homogeneous() + } } -impl, M: ApproxEq> ApproxEq for Rotmat -{ +impl, M: ApproxEq> ApproxEq for Rotmat { #[inline] - fn approx_epsilon() -> N - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N { + ApproxEq::approx_epsilon::() + } #[inline] - fn approx_eq(&self, other: &Rotmat) -> bool - { self.submat.approx_eq(&other.submat) } + fn approx_eq(&self, other: &Rotmat) -> bool { + self.submat.approx_eq(&other.submat) + } #[inline] - fn approx_eq_eps(&self, other: &Rotmat, epsilon: &N) -> bool - { self.submat.approx_eq_eps(&other.submat, epsilon) } + 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 0901729b..c1e72869 100644 --- a/src/adaptors/transform.rs +++ b/src/adaptors/transform.rs @@ -23,35 +23,37 @@ use mat::Mat3; /// underlying transform is a rotation (see `Rotmat`): this makes inversion much faster than /// inverting the homogeneous matrix itself. #[deriving(Eq, ToStr, Clone)] -pub struct Transform -{ +pub struct Transform { priv submat : M, priv subtrans : V } -impl Transform -{ +impl Transform { /// Builds a new transform from a matrix and a vector. #[inline] - pub fn new(mat: M, trans: V) -> Transform - { Transform { submat: mat, subtrans: trans } } + pub fn new(mat: M, trans: V) -> Transform { + Transform { + submat: mat, + subtrans: trans + } + } } -impl Transform -{ +impl Transform { /// Gets a copy of the internal matrix. #[inline] - pub fn submat(&self) -> M - { self.submat.clone() } + pub fn submat(&self) -> M { + self.submat.clone() + } /// Gets a copy of the internal translation. #[inline] - pub fn subtrans(&self) -> V - { self.subtrans.clone() } + pub fn subtrans(&self) -> V { + self.subtrans.clone() + } } -impl Transform>, Vec3> -{ +impl Transform>, Vec3> { /// Reorient and translate this transformation such that its local `x` axis points to a given /// direction. Note that the usually known `look_at` function does the same thing but with the /// `z` axis. See `look_at_z` for that. @@ -62,8 +64,7 @@ impl Transform>, Vec3> /// aligned with /// * up - Vector pointing `up`. The only requirement of this parameter is to not be colinear /// with `at`. Non-colinearity is not checked. - pub fn look_at(&mut self, eye: &Vec3, at: &Vec3, up: &Vec3) - { + pub fn look_at(&mut self, eye: &Vec3, at: &Vec3, up: &Vec3) { self.submat.look_at(&(*at - *eye), up); self.subtrans = eye.clone(); } @@ -77,112 +78,119 @@ impl Transform>, Vec3> /// aligned with /// * up - Vector pointing `up`. The only requirement of this parameter is to not be colinear /// with `at`. Non-colinearity is not checked. - pub fn look_at_z(&mut self, eye: &Vec3, at: &Vec3, up: &Vec3) - { + pub fn look_at_z(&mut self, eye: &Vec3, at: &Vec3, up: &Vec3) { self.submat.look_at_z(&(*at - *eye), up); self.subtrans = eye.clone(); } } -impl Dim for Transform -{ +impl Dim for Transform { #[inline] - fn dim() -> uint - { Dim::dim::() } -} - -impl One for Transform -{ - #[inline] - fn one() -> Transform - { Transform { submat: One::one(), subtrans: Zero::zero() } } -} - -impl Zero for Transform -{ - #[inline] - fn zero() -> Transform - { Transform { submat: Zero::zero(), subtrans: Zero::zero() } } - - #[inline] - fn is_zero(&self) -> bool - { self.submat.is_zero() && self.subtrans.is_zero() } -} - -impl + Mul, V: Add> -Mul, Transform> for Transform -{ - #[inline] - fn mul(&self, other: &Transform) -> Transform - { - Transform { submat: self.submat * other.submat, - subtrans: self.subtrans + self.submat.rmul(&other.subtrans) } + fn dim() -> uint { + Dim::dim::() } } -impl, V: Add> RMul for Transform -{ +impl One for Transform { #[inline] - fn rmul(&self, other: &V) -> V - { self.submat.rmul(other) + self.subtrans } + fn one() -> Transform { + Transform { + submat: One::one(), subtrans: Zero::zero() + } + } } -impl, V: Add> LMul for Transform -{ +impl Zero for Transform { #[inline] - fn lmul(&self, other: &V) -> V - { self.submat.lmul(other) + self.subtrans } + fn zero() -> Transform { + Transform { + submat: Zero::zero(), subtrans: Zero::zero() + } + } + + #[inline] + fn is_zero(&self) -> bool { + self.submat.is_zero() && self.subtrans.is_zero() + } } -impl> Translation for Transform -{ +impl + Mul, V: Add> +Mul, Transform> for Transform { #[inline] - fn translation(&self) -> V - { self.subtrans.translation() } - - #[inline] - fn inv_translation(&self) -> V - { self.subtrans.inv_translation() } - - #[inline] - fn translate_by(&mut self, t: &V) - { self.subtrans.translate_by(t) } + fn mul(&self, other: &Transform) -> Transform { + Transform { + submat: self.submat * other.submat, + subtrans: self.subtrans + self.submat.rmul(&other.subtrans) + } + } } -impl, V, _0> Translate for Transform -{ +impl, V: Add> RMul for Transform { #[inline] - fn translate(&self, v: &V) -> V - { self.submat.translate(v) } + fn rmul(&self, other: &V) -> V { + self.submat.rmul(other) + self.subtrans + } +} + +impl, V: Add> LMul for Transform { + #[inline] + fn lmul(&self, other: &V) -> V { + self.submat.lmul(other) + self.subtrans + } +} + +impl> Translation for Transform { + #[inline] + fn translation(&self) -> V { + self.subtrans.translation() + } #[inline] - fn inv_translate(&self, v: &V) -> V - { self.submat.inv_translate(v) } + fn inv_translation(&self) -> V { + self.subtrans.inv_translation() + } + + #[inline] + fn translate_by(&mut self, t: &V) { + self.subtrans.translate_by(t) + } +} + +impl, V, _0> Translate for Transform { + #[inline] + fn translate(&self, v: &V) -> V { + self.submat.translate(v) + } + + #[inline] + fn inv_translate(&self, v: &V) -> V { + self.submat.inv_translate(v) + } } impl + Translation> -Translatable> for Transform -{ +Translatable> for Transform { #[inline] - fn translated(&self, t: &V) -> Transform - { Transform::new(self.submat.clone(), self.subtrans.translated(t)) } + fn translated(&self, t: &V) -> Transform { + Transform::new(self.submat.clone(), self.subtrans.translated(t)) + } } impl + RMul + One, V, AV> -Rotation for Transform -{ +Rotation for Transform { #[inline] - fn rotation(&self) -> AV - { self.submat.rotation() } + fn rotation(&self) -> AV { + self.submat.rotation() + } #[inline] - fn inv_rotation(&self) -> AV - { self.submat.inv_rotation() } + fn inv_rotation(&self) -> AV { + self.submat.inv_rotation() + } #[inline] - fn rotate_by(&mut self, rot: &AV) - { + fn rotate_by(&mut self, rot: &AV) { // FIXME: this does not seem opitmal let mut delta = One::one::(); delta.rotate_by(rot); @@ -191,23 +199,22 @@ Rotation for Transform } } -impl, V, _0> Rotate for Transform -{ +impl, V, _0> Rotate for Transform { #[inline] - fn rotate(&self, v: &V) -> V - { self.submat.rotate(v) } + fn rotate(&self, v: &V) -> V { + self.submat.rotate(v) + } #[inline] - fn inv_rotate(&self, v: &V) -> V - { self.submat.inv_rotate(v) } + fn inv_rotate(&self, v: &V) -> V { + self.submat.inv_rotate(v) + } } impl + One, Res: Rotation + RMul + One, V, AV> -Rotatable> for Transform -{ +Rotatable> for Transform { #[inline] - fn rotated(&self, rot: &AV) -> Transform - { + fn rotated(&self, rot: &AV) -> Transform { // FIXME: this does not seem opitmal let delta = One::one::().rotated(rot); @@ -216,79 +223,76 @@ Rotatable> for Transform } impl + Mul + Clone, V: Add + Neg + Clone> -Transformation> for Transform -{ - fn transformation(&self) -> Transform - { self.clone() } +Transformation> for Transform { + fn transformation(&self) -> Transform { + self.clone() + } - fn inv_transformation(&self) -> Transform - { + fn inv_transformation(&self) -> Transform { // FIXME: fail or return a Some> ? - match self.inverse() - { + match self.inverse() { Some(t) => t, None => fail!("This transformation was not inversible.") } } - fn transform_by(&mut self, other: &Transform) - { *self = other * *self; } + fn transform_by(&mut self, other: &Transform) { + *self = other * *self + } } impl, V: Add + Sub> -Ts for Transform -{ +Ts for Transform { #[inline] - fn transform_vec(&self, v: &V) -> V - { self.submat.transform_vec(v) + self.subtrans } + fn transform_vec(&self, v: &V) -> V { + self.submat.transform_vec(v) + self.subtrans + } #[inline] - fn inv_transform(&self, v: &V) -> V - { self.submat.inv_transform(&(v - self.subtrans)) } + fn inv_transform(&self, v: &V) -> V { + self.submat.inv_transform(&(v - self.subtrans)) + } } // FIXME: constraints are too restrictive. // Should be: Transformable ... impl + Mul + Inv + Clone, V: Add + Neg + Clone> -Transformable, Transform> for Transform -{ - fn transformed(&self, t: &Transform) -> Transform - { t * *self } +Transformable, Transform> for Transform { + fn transformed(&self, t: &Transform) -> Transform { + t * *self + } } impl + Clone, V: Neg + Clone> -Inv for Transform -{ +Inv for Transform { #[inline] - fn inplace_inverse(&mut self) -> bool - { - if !self.submat.inplace_inverse() - { false } - else - { + fn inplace_inverse(&mut self) -> bool { + if !self.submat.inplace_inverse() { + false + } + else { self.subtrans = self.submat.rmul(&-self.subtrans); true } } #[inline] - fn inverse(&self) -> Option> - { + fn inverse(&self) -> Option> { let mut res = self.clone(); - if res.inplace_inverse() - { Some(res) } - else - { None } + if res.inplace_inverse() { + Some(res) + } + else { + None + } } } impl, M2: Dim + Column, V: Clone> -ToHomogeneous for Transform -{ - fn to_homogeneous(&self) -> M2 - { +ToHomogeneous for Transform { + fn to_homogeneous(&self) -> M2 { let mut res = self.submat.to_homogeneous(); // copy the translation @@ -301,39 +305,35 @@ ToHomogeneous for Transform } impl + Dim, M2: FromHomogeneous, V> -FromHomogeneous for Transform -{ - fn from(m: &M) -> Transform - { +FromHomogeneous for Transform { + fn from(m: &M) -> Transform { Transform::new(FromHomogeneous::from(m), m.column(Dim::dim::() - 1)) } } impl, M:ApproxEq, V:ApproxEq> -ApproxEq for Transform -{ +ApproxEq for Transform { #[inline] - fn approx_epsilon() -> N - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N { + ApproxEq::approx_epsilon::() + } #[inline] - fn approx_eq(&self, other: &Transform) -> bool - { + fn approx_eq(&self, other: &Transform) -> bool { self.submat.approx_eq(&other.submat) && self.subtrans.approx_eq(&other.subtrans) } #[inline] - fn approx_eq_eps(&self, other: &Transform, epsilon: &N) -> 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) } } -impl Rand for Transform -{ +impl Rand for Transform { #[inline] - fn rand(rng: &mut R) -> Transform - { Transform::new(rng.gen(), rng.gen()) } + fn rand(rng: &mut R) -> Transform { + Transform::new(rng.gen(), rng.gen()) + } } diff --git a/src/dmat.rs b/src/dmat.rs index 2cdbbb74..619bae73 100644 --- a/src/dmat.rs +++ b/src/dmat.rs @@ -10,8 +10,7 @@ use dvec::{DVec, zero_vec_with_dim}; /// Square matrix with a dimension unknown at compile-time. #[deriving(Eq, ToStr, Clone)] -pub struct DMat -{ +pub struct DMat { priv dim: uint, // FIXME: handle more than just square matrices priv mij: ~[N] } @@ -22,13 +21,15 @@ pub struct DMat /// * `dim` - The dimension of the matrix. A `dim`-dimensional matrix contains `dim * dim` /// components. #[inline] -pub fn zero_mat_with_dim(dim: uint) -> DMat -{ DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } } +pub fn zero_mat_with_dim(dim: uint) -> DMat { + DMat { dim: dim, mij: from_elem(dim * dim, Zero::zero()) } +} /// Tests if all components of the matrix are zeroes. #[inline] -pub fn is_zero_mat(mat: &DMat) -> bool -{ mat.mij.iter().all(|e| e.is_zero()) } +pub fn is_zero_mat(mat: &DMat) -> bool { + mat.mij.iter().all(|e| e.is_zero()) +} /// Builds an identity matrix. /// @@ -36,22 +37,22 @@ pub fn is_zero_mat(mat: &DMat) -> bool /// * `dim` - The dimension of the matrix. A `dim`-dimensional matrix contains `dim * dim` /// components. #[inline] -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::(); - for i in range(0u, dim) - { res.set(i, i, &_1); } + for i in range(0u, dim) { + res.set(i, i, &_1); + } res } -impl DMat -{ +impl DMat { #[inline] - fn offset(&self, i: uint, j: uint) -> uint - { i * self.dim + j } + fn offset(&self, i: uint, j: uint) -> uint { + i * self.dim + j + } /// Changes the value of a component of the matrix. /// @@ -59,8 +60,7 @@ impl DMat /// * `i` - 0-based index of the line to be changed /// * `j` - 0-based index of the column to be changed #[inline] - pub fn set(&mut self, i: uint, j: uint, t: &N) - { + 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.clone() @@ -72,39 +72,35 @@ impl DMat /// * `i` - 0-based index of the line to be read /// * `j` - 0-based index of the column to be read #[inline] - pub fn at(&self, i: uint, j: uint) -> N - { + pub fn at(&self, i: uint, j: uint) -> N { assert!(i < self.dim); assert!(j < self.dim); self.mij[self.offset(i, j)].clone() } } -impl Index<(uint, uint), N> for DMat -{ +impl Index<(uint, uint), N> for DMat { #[inline] - fn index(&self, &(i, j): &(uint, uint)) -> N - { self.at(i, j) } + fn index(&self, &(i, j): &(uint, uint)) -> N { + self.at(i, j) + } } impl + Add + Zero> -Mul, DMat> for DMat -{ - fn mul(&self, other: &DMat) -> DMat - { +Mul, DMat> for DMat { + fn mul(&self, other: &DMat) -> DMat { assert!(self.dim == other.dim); let dim = self.dim; let mut res = zero_mat_with_dim(dim); - for i in range(0u, dim) - { - for j in range(0u, dim) - { + for i in range(0u, dim) { + for j in range(0u, dim) { let mut acc = Zero::zero::(); - for k in range(0u, dim) - { acc = acc + self.at(i, k) * other.at(k, j); } + for k in range(0u, dim) { + acc = acc + self.at(i, k) * other.at(k, j); + } res.set(i, j, &acc); } @@ -115,19 +111,17 @@ Mul, DMat> for DMat } impl + Mul + Zero> -RMul> for DMat -{ - fn rmul(&self, other: &DVec) -> DVec - { +RMul> for DMat { + 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); - for i in range(0u, dim) - { - for j in range(0u, dim) - { res.at[i] = res.at[i] + other.at[j] * self.at(i, j); } + for i in range(0u, dim) { + for j in range(0u, dim) { + res.at[i] = res.at[i] + other.at[j] * self.at(i, j); + } } res @@ -135,19 +129,17 @@ RMul> for DMat } impl + Mul + Zero> -LMul> for DMat -{ - fn lmul(&self, other: &DVec) -> DVec - { +LMul> for DMat { + 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); - for i in range(0u, dim) - { - for j in range(0u, dim) - { res.at[i] = res.at[i] + other.at[j] * self.at(j, i); } + for i in range(0u, dim) { + for j in range(0u, dim) { + res.at[i] = res.at[i] + other.at[j] * self.at(j, i); + } } res @@ -155,50 +147,47 @@ LMul> for DMat } impl -Inv for DMat -{ +Inv for DMat { #[inline] - fn inverse(&self) -> Option> - { + fn inverse(&self) -> Option> { let mut res : DMat = self.clone(); - if res.inplace_inverse() - { Some(res) } - else - { None } + if res.inplace_inverse() { + Some(res) + } + else { + None + } } - fn inplace_inverse(&mut self) -> bool - { + fn inplace_inverse(&mut self) -> bool { let dim = self.dim; let mut res = one_mat_with_dim::(dim); let _0T = Zero::zero::(); // inversion using Gauss-Jordan elimination - for k in range(0u, dim) - { + for k in range(0u, dim) { // search a non-zero value on the k-th column // FIXME: would it be worth it to spend some more time searching for the // max instead? let mut n0 = k; // index of a non-zero entry - while (n0 != dim) - { - if self.at(n0, k) != _0T - { break; } + while (n0 != dim) { + if self.at(n0, k) != _0T { + break; + } n0 = n0 + 1; } - if n0 == dim - { return false } + if n0 == dim { + return false + } // swap pivot line - if n0 != k - { - for j in range(0u, dim) - { + if n0 != k { + for j in range(0u, dim) { let off_n0_j = self.offset(n0, j); let off_k_j = self.offset(k, j); @@ -209,32 +198,26 @@ Inv for DMat let pivot = self.at(k, k); - for j in range(k, dim) - { + for j in range(k, dim) { let selfval = &(self.at(k, j) / pivot); self.set(k, j, selfval); } - for j in range(0u, dim) - { + for j in range(0u, dim) { let resval = &(res.at(k, j) / pivot); res.set(k, j, resval); } - for l in range(0u, dim) - { - if l != k - { + for l in range(0u, dim) { + if l != k { let normalizer = self.at(l, k); - for j in range(k, dim) - { + for j in range(k, dim) { let selfval = &(self.at(l, j) - self.at(k, j) * normalizer); self.set(l, j, selfval); } - for j in range(0u, dim) - { + for j in range(0u, dim) { let resval = &(res.at(l, j) - res.at(k, j) * normalizer); res.set(l, j, resval); } @@ -248,11 +231,9 @@ Inv for DMat } } -impl Transpose for DMat -{ +impl Transpose for DMat { #[inline] - fn transposed(&self) -> DMat - { + fn transposed(&self) -> DMat { let mut res = self.clone(); res.transpose(); @@ -260,14 +241,11 @@ impl Transpose for DMat res } - fn transpose(&mut self) - { + fn transpose(&mut self) { let dim = self.dim; - for i in range(1u, dim) - { - for j in range(0u, dim - 1) - { + for i in range(1u, dim) { + for j in range(0u, dim - 1) { let off_i_j = self.offset(i, j); let off_j_i = self.offset(j, i); @@ -277,25 +255,27 @@ impl Transpose for DMat } } -impl> ApproxEq for DMat -{ +impl> ApproxEq for DMat { #[inline] - fn approx_epsilon() -> N - { ApproxEq::approx_epsilon::() } - - #[inline] - 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_epsilon() -> N { + ApproxEq::approx_epsilon::() } #[inline] - fn approx_eq_eps(&self, other: &DMat, epsilon: &N) -> 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_eps(b, epsilon) } + do zip.all |(a, b)| { + a.approx_eq(b) + } + } + + #[inline] + fn approx_eq_eps(&self, other: &DMat, epsilon: &N) -> bool { + let mut zip = self.mij.iter().zip(other.mij.iter()); + + do zip.all |(a, b)| { + a.approx_eq_eps(b, epsilon) + } } } diff --git a/src/dvec.rs b/src/dvec.rs index 95d241f6..0afed924 100644 --- a/src/dvec.rs +++ b/src/dvec.rs @@ -14,8 +14,7 @@ use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; /// Vector with a dimension unknown at compile-time. #[deriving(Eq, Ord, ToStr, Clone)] -pub struct DVec -{ +pub struct DVec { /// Components of the vector. Contains as much elements as the vector dimension. at: ~[N] } @@ -25,50 +24,48 @@ pub struct DVec /// # Arguments /// * `dim` - The dimension of the vector. #[inline] -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::()) } +} /// Tests if all components of the vector are zeroes. #[inline] -pub fn is_zero_vec(vec: &DVec) -> bool -{ vec.at.iter().all(|e| e.is_zero()) } - -impl Iterable for DVec -{ - fn iter<'l>(&'l self) -> VecIterator<'l, N> - { self.at.iter() } +pub fn is_zero_vec(vec: &DVec) -> bool { + vec.at.iter().all(|e| e.is_zero()) } -impl IterableMut for DVec -{ - fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> - { self.at.mut_iter() } +impl Iterable for DVec { + fn iter<'l>(&'l self) -> VecIterator<'l, N> { + self.at.iter() + } } -impl> FromIterator for DVec -{ - fn from_iterator(mut param: &mut Iter) -> DVec - { +impl IterableMut for DVec { + fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> { + self.at.mut_iter() + } +} + +impl> FromIterator for DVec { + fn from_iterator(mut param: &mut Iter) -> DVec { let mut res = DVec { at: ~[] }; - for e in param - { res.at.push(e) } + for e in param { + res.at.push(e) + } res } } -impl> DVec -{ +impl> DVec { /// Computes the canonical basis for the given dimension. A canonical basis is a set of /// vectors, mutually orthogonal, with all its component equal to 0.0 exept one which is equal /// to 1.0. - pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec] - { + pub fn canonical_basis_with_dim(dim: uint) -> ~[DVec] { let mut res : ~[DVec] = ~[]; - for i in range(0u, dim) - { + for i in range(0u, dim) { let mut basis_element : DVec = zero_vec_with_dim(dim); basis_element.at[i] = One::one(); @@ -81,31 +78,32 @@ impl> DVec /// Computes a basis of the space orthogonal to the vector. If the input vector is of dimension /// `n`, this will return `n - 1` vectors. - 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] = ~[]; - for i in range(0u, dim) - { + for i in range(0u, dim) { let mut basis_element : DVec = zero_vec_with_dim(self.at.len()); basis_element.at[i] = One::one(); - if res.len() == dim - 1 - { break; } + if res.len() == dim - 1 { + break; + } let mut elt = basis_element.clone(); elt = elt - self.scalar_mul(&basis_element.dot(self)); - for v in res.iter() - { elt = elt - v.scalar_mul(&elt.dot(v)) }; + for v in res.iter() { + elt = elt - v.scalar_mul(&elt.dot(v)) + }; - if !elt.sqnorm().approx_eq(&Zero::zero()) - { res.push(elt.normalized()); } + if !elt.sqnorm().approx_eq(&Zero::zero()) { + res.push(elt.normalized()); + } } assert!(res.len() == dim - 1); @@ -114,11 +112,9 @@ impl> DVec } } -impl> Add, DVec> for DVec -{ +impl> Add, DVec> for DVec { #[inline] - fn add(&self, other: &DVec) -> DVec - { + fn add(&self, other: &DVec) -> DVec { assert!(self.at.len() == other.at.len()); DVec { at: self.at.iter().zip(other.at.iter()).transform(|(a, b)| *a + *b).collect() @@ -126,11 +122,9 @@ impl> Add, DVec> for DVec } } -impl> Sub, DVec> for DVec -{ +impl> Sub, DVec> for DVec { #[inline] - fn sub(&self, other: &DVec) -> DVec - { + fn sub(&self, other: &DVec) -> DVec { assert!(self.at.len() == other.at.len()); DVec { at: self.at.iter().zip(other.at.iter()).transform(|(a, b)| *a - *b).collect() @@ -138,135 +132,135 @@ impl> Sub, DVec> for DVec } } -impl> Neg> for DVec -{ +impl> Neg> for DVec { #[inline] - fn neg(&self) -> DVec - { DVec { at: self.at.iter().transform(|a| -a).collect() } } + fn neg(&self) -> DVec { + DVec { at: self.at.iter().transform(|a| -a).collect() } + } } -impl Dot for DVec -{ +impl Dot for DVec { #[inline] - fn dot(&self, other: &DVec) -> N - { + fn dot(&self, other: &DVec) -> N { assert!(self.at.len() == other.at.len()); let mut res = Zero::zero::(); - for i in range(0u, self.at.len()) - { res = res + self.at[i] * other.at[i]; } + for i in range(0u, self.at.len()) { + res = res + self.at[i] * other.at[i]; + } res } } -impl SubDot for DVec -{ +impl SubDot for DVec { #[inline] - fn sub_dot(&self, a: &DVec, b: &DVec) -> N - { + fn sub_dot(&self, a: &DVec, b: &DVec) -> N { let mut res = Zero::zero::(); - for i in range(0u, self.at.len()) - { res = res + (self.at[i] - a.at[i]) * b.at[i]; } + for i in range(0u, self.at.len()) { + res = res + (self.at[i] - a.at[i]) * b.at[i]; + } res } } -impl> ScalarMul for DVec -{ +impl> ScalarMul for DVec { #[inline] - fn scalar_mul(&self, s: &N) -> DVec - { DVec { at: self.at.iter().transform(|a| a * *s).collect() } } + fn scalar_mul(&self, s: &N) -> DVec { + DVec { at: self.at.iter().transform(|a| a * *s).collect() } + } #[inline] - fn scalar_mul_inplace(&mut self, s: &N) - { - for i in range(0u, self.at.len()) - { self.at[i] = self.at[i] * *s; } + fn scalar_mul_inplace(&mut self, s: &N) { + for i in range(0u, self.at.len()) { + self.at[i] = self.at[i] * *s; + } } } -impl> ScalarDiv for DVec -{ +impl> ScalarDiv for DVec { #[inline] - fn scalar_div(&self, s: &N) -> DVec - { DVec { at: self.at.iter().transform(|a| a / *s).collect() } } + fn scalar_div(&self, s: &N) -> DVec { + DVec { at: self.at.iter().transform(|a| a / *s).collect() } + } #[inline] - fn scalar_div_inplace(&mut self, s: &N) - { - for i in range(0u, self.at.len()) - { self.at[i] = self.at[i] / *s; } + fn scalar_div_inplace(&mut self, s: &N) { + for i in range(0u, self.at.len()) { + self.at[i] = self.at[i] / *s; + } } } -impl> ScalarAdd for DVec -{ +impl> ScalarAdd for DVec { #[inline] - fn scalar_add(&self, s: &N) -> DVec - { DVec { at: self.at.iter().transform(|a| a + *s).collect() } } + fn scalar_add(&self, s: &N) -> DVec { + DVec { at: self.at.iter().transform(|a| a + *s).collect() } + } #[inline] - fn scalar_add_inplace(&mut self, s: &N) - { - for i in range(0u, self.at.len()) - { self.at[i] = self.at[i] + *s; } + fn scalar_add_inplace(&mut self, s: &N) { + for i in range(0u, self.at.len()) { + self.at[i] = self.at[i] + *s; + } } } -impl> ScalarSub for DVec -{ +impl> ScalarSub for DVec { #[inline] - fn scalar_sub(&self, s: &N) -> DVec - { DVec { at: self.at.iter().transform(|a| a - *s).collect() } } + fn scalar_sub(&self, s: &N) -> DVec { + DVec { at: self.at.iter().transform(|a| a - *s).collect() } + } #[inline] - fn scalar_sub_inplace(&mut self, s: &N) - { - for i in range(0u, self.at.len()) - { self.at[i] = self.at[i] - *s; } + fn scalar_sub_inplace(&mut self, s: &N) { + for i in range(0u, self.at.len()) { + self.at[i] = self.at[i] - *s; + } } } -impl + Neg + Clone> Translation> for DVec -{ +impl + Neg + Clone> Translation> for DVec { #[inline] - fn translation(&self) -> DVec - { self.clone() } + fn translation(&self) -> DVec { + self.clone() + } #[inline] - fn inv_translation(&self) -> DVec - { -self } + fn inv_translation(&self) -> DVec { + -self + } #[inline] - fn translate_by(&mut self, t: &DVec) - { *self = *self + *t; } + fn translate_by(&mut self, t: &DVec) { + *self = *self + *t; + } } -impl + Neg + Clone> Translatable, DVec> for DVec -{ +impl + Neg + Clone> Translatable, DVec> for DVec { #[inline] - fn translated(&self, t: &DVec) -> DVec - { self + *t } + fn translated(&self, t: &DVec) -> DVec { + self + *t + } } -impl Norm for DVec -{ +impl Norm for DVec { #[inline] - fn sqnorm(&self) -> N - { self.dot(self) } + fn sqnorm(&self) -> N { + self.dot(self) + } #[inline] - fn norm(&self) -> N - { self.sqnorm().sqrt() } + fn norm(&self) -> N { + self.sqnorm().sqrt() + } #[inline] - fn normalized(&self) -> DVec - { + fn normalized(&self) -> DVec { let mut res : DVec = self.clone(); res.normalize(); @@ -275,36 +269,38 @@ impl Norm for DVec } #[inline] - fn normalize(&mut self) -> N - { + fn normalize(&mut self) -> N { let l = self.norm(); - for i in range(0u, self.at.len()) - { self.at[i] = self.at[i] / l; } + for i in range(0u, self.at.len()) { + self.at[i] = self.at[i] / l; + } l } } -impl> ApproxEq for DVec -{ +impl> ApproxEq for DVec { #[inline] - fn approx_epsilon() -> N - { ApproxEq::approx_epsilon::() } - - #[inline] - 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_epsilon() -> N { + ApproxEq::approx_epsilon::() } #[inline] - fn approx_eq_eps(&self, other: &DVec, epsilon: &N) -> 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_eps(b, epsilon) } + do zip.all |(a, b)| { + a.approx_eq(b) + } + } + + #[inline] + fn approx_eq_eps(&self, other: &DVec, epsilon: &N) -> bool { + let mut zip = self.at.iter().zip(other.at.iter()); + + do zip.all |(a, b)| { + a.approx_eq_eps(b, epsilon) + } } } diff --git a/src/mat.rs b/src/mat.rs index 4a5aa5e3..45b30593 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -31,8 +31,7 @@ mod mat_macros; /// Square matrix of dimension 1. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Mat1 -{ +pub struct Mat1 { m11: N } @@ -56,8 +55,7 @@ from_homogeneous_impl!(Mat1, Mat2, 1, 2) /// Square matrix of dimension 2. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Mat2 -{ +pub struct Mat2 { m11: N, m12: N, m21: N, m22: N } @@ -85,8 +83,7 @@ from_homogeneous_impl!(Mat2, Mat3, 2, 3) /// Square matrix of dimension 3. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Mat3 -{ +pub struct Mat3 { m11: N, m12: N, m13: N, m21: N, m22: N, m23: N, m31: N, m32: N, m33: N @@ -118,8 +115,7 @@ from_homogeneous_impl!(Mat3, Mat4, 3, 4) /// Square matrix of dimension 4. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Mat4 -{ +pub struct Mat4 { m11: N, m12: N, m13: N, m14: N, m21: N, m22: N, m23: N, m24: N, m31: N, m32: N, m33: N, m34: N, @@ -159,8 +155,7 @@ from_homogeneous_impl!(Mat4, Mat5, 4, 5) /// Square matrix of dimension 5. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Mat5 -{ +pub struct Mat5 { m11: N, m12: N, m13: N, m14: N, m15: N, m21: N, m22: N, m23: N, m24: N, m25: N, m31: N, m32: N, m33: N, m34: N, m35: N, @@ -206,8 +201,7 @@ from_homogeneous_impl!(Mat5, Mat6, 5, 6) /// Square matrix of dimension 6. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Mat6 -{ +pub struct Mat6 { m11: N, m12: N, m13: N, m14: N, m15: N, m16: N, m21: N, m22: N, m23: N, m24: N, m25: N, m26: N, m31: N, m32: N, m33: N, m34: N, m35: N, m36: N, diff --git a/src/mat_macros.rs b/src/mat_macros.rs index 36225030..79a25be9 100644 --- a/src/mat_macros.rs +++ b/src/mat_macros.rs @@ -2,11 +2,9 @@ macro_rules! mat_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl $t - { + impl $t { #[inline] - pub fn new($comp0: N $(, $compN: N )*) -> $t - { + pub fn new($comp0: N $(, $compN: N )*) -> $t { $t { $comp0: $comp0 $(, $compN: $compN )* @@ -18,42 +16,44 @@ macro_rules! mat_impl( macro_rules! mat_cast_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl MatCast<$t> for $t - { + impl MatCast<$t> for $t { #[inline] - pub fn from(m: $t) -> $t - { $t::new(NumCast::from(m.$comp0.clone()) $(, NumCast::from(m.$compN.clone()) )*) } + pub fn from(m: $t) -> $t { + $t::new(NumCast::from(m.$comp0.clone()) $(, NumCast::from(m.$compN.clone()) )*) + } } ) ) macro_rules! iterable_impl( ($t: ident, $dim: expr) => ( - impl Iterable for $t - { - fn iter<'l>(&'l self) -> VecIterator<'l, N> - { unsafe { cast::transmute::<&'l $t, &'l [N, ..$dim * $dim]>(self).iter() } } + impl Iterable for $t { + fn iter<'l>(&'l self) -> VecIterator<'l, N> { + unsafe { + cast::transmute::<&'l $t, &'l [N, ..$dim * $dim]>(self).iter() + } + } } ) ) macro_rules! iterable_mut_impl( ($t: ident, $dim: expr) => ( - impl IterableMut for $t - { - fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> - { unsafe { cast::transmute::<&'l mut $t, &'l mut [N, ..$dim * $dim]>(self).mut_iter() } } + impl IterableMut for $t { + fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> { + unsafe { + cast::transmute::<&'l mut $t, &'l mut [N, ..$dim * $dim]>(self).mut_iter() + } + } } ) ) macro_rules! one_impl( ($t: ident, $value0: ident $(, $valueN: ident)* ) => ( - impl One for $t - { + impl One for $t { #[inline] - fn one() -> $t - { + fn one() -> $t { let (_0, _1) = (Zero::zero::(), One::one::()); return $t::new($value0.clone() $(, $valueN.clone() )*) } @@ -63,30 +63,34 @@ macro_rules! one_impl( macro_rules! dim_impl( ($t: ident, $dim: expr) => ( - impl Dim for $t - { + impl Dim for $t { #[inline] - fn dim() -> uint - { $dim } + fn dim() -> uint { + $dim + } } ) ) macro_rules! indexable_impl( ($t: ident, $dim: expr) => ( - impl Indexable<(uint, uint), N> for $t - { + impl Indexable<(uint, uint), N> for $t { #[inline] - pub fn at(&self, (i, j): (uint, uint)) -> N - { unsafe { cast::transmute::<&$t, &[N, ..$dim * $dim]>(self)[i * $dim + j].clone() } } + pub fn at(&self, (i, j): (uint, uint)) -> N { + unsafe { + cast::transmute::<&$t, &[N, ..$dim * $dim]>(self)[i * $dim + j].clone() + } + } #[inline] - pub fn set(&mut self, (i, j): (uint, uint), val: N) - { unsafe { cast::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self)[i * $dim + j] = val } } + pub fn set(&mut self, (i, j): (uint, uint), val: N) { + unsafe { + cast::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self)[i * $dim + j] = val + } + } #[inline] - pub fn swap(&mut self, (i1, j1): (uint, uint), (i2, j2): (uint, uint)) - { + pub fn swap(&mut self, (i1, j1): (uint, uint), (i2, j2): (uint, uint)) { unsafe { cast::transmute::<&mut $t, &mut [N, ..$dim * $dim]>(self) .swap(i1 * $dim + j1, i2 * $dim + j2) @@ -98,27 +102,24 @@ macro_rules! indexable_impl( macro_rules! column_impl( ($t: ident, $dim: expr) => ( - impl + IterableMut> Column for $t - { - fn set_column(&mut self, col: uint, v: V) - { - for (i, e) in v.iter().enumerate() - { - if i == Dim::dim::<$t>() - { break } + impl + IterableMut> Column for $t { + fn set_column(&mut self, col: uint, v: V) { + for (i, e) in v.iter().enumerate() { + if i == Dim::dim::<$t>() { + break + } self.set((i, col), e.clone()); } } - fn column(&self, col: uint) -> V - { + fn column(&self, col: uint) -> V { let mut res = Zero::zero::(); - for (i, e) in res.mut_iter().enumerate() - { - if i >= Dim::dim::<$t>() - { break } + for (i, e) in res.mut_iter().enumerate() { + if i >= Dim::dim::<$t>() { + break + } *e = self.at((i, col)); } @@ -131,20 +132,17 @@ macro_rules! column_impl( macro_rules! mul_impl( ($t: ident, $dim: expr) => ( - impl Mul<$t, $t> for $t - { - fn mul(&self, other: &$t) -> $t - { + impl Mul<$t, $t> for $t { + fn mul(&self, other: &$t) -> $t { let mut res: $t = Zero::zero(); - for i in range(0u, $dim) - { - for j in range(0u, $dim) - { + for i in range(0u, $dim) { + for j in range(0u, $dim) { let mut acc = Zero::zero::(); - for k in range(0u, $dim) - { acc = acc + self.at((i, k)) * other.at((k, j)); } + for k in range(0u, $dim) { + acc = acc + self.at((i, k)) * other.at((k, j)); + } res.set((i, j), acc); } @@ -158,16 +156,12 @@ macro_rules! mul_impl( macro_rules! rmul_impl( ($t: ident, $v: ident, $dim: expr) => ( - impl RMul<$v> for $t - { - fn rmul(&self, other: &$v) -> $v - { + impl RMul<$v> for $t { + fn rmul(&self, other: &$v) -> $v { let mut res : $v = Zero::zero(); - for i in range(0u, $dim) - { - for j in range(0u, $dim) - { + for i in range(0u, $dim) { + for j in range(0u, $dim) { let val = res.at(i) + other.at(j) * self.at((i, j)); res.set(i, val) } @@ -181,16 +175,12 @@ macro_rules! rmul_impl( macro_rules! lmul_impl( ($t: ident, $v: ident, $dim: expr) => ( - impl LMul<$v> for $t - { - fn lmul(&self, other: &$v) -> $v - { + impl LMul<$v> for $t { + fn lmul(&self, other: &$v) -> $v { let mut res : $v = Zero::zero(); - for i in range(0u, $dim) - { - for j in range(0u, $dim) - { + for i in range(0u, $dim) { + for j in range(0u, $dim) { let val = res.at(i) + other.at(j) * self.at((j, i)); res.set(i, val) } @@ -205,17 +195,15 @@ macro_rules! lmul_impl( macro_rules! transform_impl( ($t: ident, $v: ident) => ( impl - Transform<$v> for $t - { + Transform<$v> for $t { #[inline] - fn transform_vec(&self, v: &$v) -> $v - { self.rmul(v) } + fn transform_vec(&self, v: &$v) -> $v { + self.rmul(v) + } #[inline] - fn inv_transform(&self, v: &$v) -> $v - { - match self.inverse() - { + fn inv_transform(&self, v: &$v) -> $v { + match self.inverse() { Some(t) => t.transform_vec(v), None => fail!("Cannot use inv_transform on a non-inversible matrix.") } @@ -227,49 +215,46 @@ macro_rules! transform_impl( macro_rules! inv_impl( ($t: ident, $dim: expr) => ( impl - Inv for $t - { + Inv for $t { #[inline] - fn inverse(&self) -> Option<$t> - { + fn inverse(&self) -> Option<$t> { let mut res : $t = self.clone(); - if res.inplace_inverse() - { Some(res) } - else - { None } + if res.inplace_inverse() { + Some(res) + } + else { + None + } } - fn inplace_inverse(&mut self) -> bool - { + fn inplace_inverse(&mut self) -> bool { let mut res: $t = One::one(); let _0N: N = Zero::zero(); // inversion using Gauss-Jordan elimination - for k in range(0u, $dim) - { + for k in range(0u, $dim) { // search a non-zero value on the k-th column // FIXME: would it be worth it to spend some more time searching for the // max instead? let mut n0 = k; // index of a non-zero entry - while (n0 != $dim) - { - if self.at((n0, k)) != _0N - { break; } + while (n0 != $dim) { + if self.at((n0, k)) != _0N { + break; + } n0 = n0 + 1; } - if n0 == $dim - { return false } + if n0 == $dim { + return false + } // swap pivot line - if n0 != k - { - for j in range(0u, $dim) - { + if n0 != k { + for j in range(0u, $dim) { self.swap((n0, j), (k, j)); res.swap((n0, j), (k, j)); } @@ -277,32 +262,26 @@ macro_rules! inv_impl( let pivot = self.at((k, k)); - for j in range(k, $dim) - { + for j in range(k, $dim) { let selfval = self.at((k, j)) / pivot; self.set((k, j), selfval); } - for j in range(0u, $dim) - { + for j in range(0u, $dim) { let resval = res.at((k, j)) / pivot; res.set((k, j), resval); } - for l in range(0u, $dim) - { - if l != k - { + for l in range(0u, $dim) { + if l != k { let normalizer = self.at((l, k)); - for j in range(k, $dim) - { + for j in range(k, $dim) { let selfval = self.at((l, j)) - self.at((k, j)) * normalizer; self.set((l, j), selfval); } - for j in range(0u, $dim) - { + for j in range(0u, $dim) { let resval = res.at((l, j)) - res.at((k, j)) * normalizer; res.set((l, j), resval); } @@ -320,11 +299,9 @@ macro_rules! inv_impl( macro_rules! transpose_impl( ($t: ident, $dim: expr) => ( - impl Transpose for $t - { + impl Transpose for $t { #[inline] - fn transposed(&self) -> $t - { + fn transposed(&self) -> $t { let mut res = self.clone(); res.transpose(); @@ -332,12 +309,11 @@ macro_rules! transpose_impl( res } - fn transpose(&mut self) - { - for i in range(1u, $dim) - { - for j in range(0u, i) - { self.swap((i, j), (j, i)) } + fn transpose(&mut self) { + for i in range(1u, $dim) { + for j in range(0u, i) { + self.swap((i, j), (j, i)) + } } } } @@ -346,26 +322,28 @@ macro_rules! transpose_impl( macro_rules! approx_eq_impl( ($t: ident) => ( - impl> ApproxEq for $t - { + impl> ApproxEq for $t { #[inline] - fn approx_epsilon() -> N - { ApproxEq::approx_epsilon::() } - - #[inline] - fn approx_eq(&self, other: &$t) -> bool - { - let mut zip = self.iter().zip(other.iter()); - - do zip.all |(a, b)| { a.approx_eq(b) } + fn approx_epsilon() -> N { + ApproxEq::approx_epsilon::() } #[inline] - fn approx_eq_eps(&self, other: &$t, epsilon: &N) -> bool - { + fn approx_eq(&self, other: &$t) -> bool { let mut zip = self.iter().zip(other.iter()); - do zip.all |(a, b)| { a.approx_eq_eps(b, epsilon) } + do zip.all |(a, b)| { + a.approx_eq(b) + } + } + + #[inline] + fn approx_eq_eps(&self, other: &$t, epsilon: &N) -> bool { + let mut zip = self.iter().zip(other.iter()); + + do zip.all |(a, b)| { + a.approx_eq_eps(b, epsilon) + } } } ) @@ -373,16 +351,14 @@ macro_rules! approx_eq_impl( macro_rules! to_homogeneous_impl( ($t: ident, $t2: ident, $dim: expr, $dim2: expr) => ( - impl ToHomogeneous<$t2> for $t - { - fn to_homogeneous(&self) -> $t2 - { + impl ToHomogeneous<$t2> for $t { + fn to_homogeneous(&self) -> $t2 { let mut res: $t2 = One::one(); - for i in range(0u, $dim) - { - for j in range(0u, $dim) - { res.set((i, j), self.at((i, j))) } + for i in range(0u, $dim) { + for j in range(0u, $dim) { + res.set((i, j), self.at((i, j))) + } } res @@ -393,16 +369,14 @@ macro_rules! to_homogeneous_impl( macro_rules! from_homogeneous_impl( ($t: ident, $t2: ident, $dim: expr, $dim2: expr) => ( - impl FromHomogeneous<$t2> for $t - { - fn from(m: &$t2) -> $t - { + impl FromHomogeneous<$t2> for $t { + fn from(m: &$t2) -> $t { let mut res: $t = One::one(); - for i in range(0u, $dim2) - { - for j in range(0u, $dim2) - { res.set((i, j), m.at((i, j))) } + for i in range(0u, $dim2) { + for j in range(0u, $dim2) { + res.set((i, j), m.at((i, j))) + } } // FIXME: do we have to deal the lost components diff --git a/src/mat_spec.rs b/src/mat_spec.rs index 298389d7..bc74ddff 100644 --- a/src/mat_spec.rs +++ b/src/mat_spec.rs @@ -5,26 +5,25 @@ use traits::inv::Inv; // some specializations: impl -Inv for Mat1 -{ +Inv for Mat1 { #[inline] - fn inverse(&self) -> Option> - { + fn inverse(&self) -> Option> { let mut res : Mat1 = self.clone(); - if res.inplace_inverse() - { Some(res) } - else - { None } + if res.inplace_inverse() { + Some(res) + } + else { + None + } } #[inline] - fn inplace_inverse(&mut self) -> bool - { - if self.m11.is_zero() - { false } - else - { + fn inplace_inverse(&mut self) -> bool { + if self.m11.is_zero() { + false + } + else { self.m11 = One::one::() / self.m11; true } @@ -32,28 +31,27 @@ Inv for Mat1 } impl -Inv for Mat2 -{ +Inv for Mat2 { #[inline] - fn inverse(&self) -> Option> - { + fn inverse(&self) -> Option> { let mut res : Mat2 = self.clone(); - if res.inplace_inverse() - { Some(res) } - else - { None } + if res.inplace_inverse() { + Some(res) + } + else { + None + } } #[inline] - fn inplace_inverse(&mut self) -> bool - { + fn inplace_inverse(&mut self) -> bool { let det = self.m11 * self.m22 - self.m21 * self.m12; - if det.is_zero() - { false } - else - { + if det.is_zero() { + false + } + else { *self = Mat2::new(self.m22 / det , -self.m12 / det, -self.m21 / det, self.m11 / det); @@ -63,22 +61,21 @@ Inv for Mat2 } impl -Inv for Mat3 -{ +Inv for Mat3 { #[inline] - fn inverse(&self) -> Option> - { + fn inverse(&self) -> Option> { let mut res = self.clone(); - if res.inplace_inverse() - { Some(res) } - else - { None } + if res.inplace_inverse() { + Some(res) + } + else { + None + } } #[inline] - fn inplace_inverse(&mut self) -> bool - { + fn inplace_inverse(&mut self) -> bool { let minor_m12_m23 = self.m22 * self.m33 - self.m32 * self.m23; let minor_m11_m23 = self.m21 * self.m33 - self.m31 * self.m23; let minor_m11_m22 = self.m21 * self.m32 - self.m31 * self.m22; @@ -87,10 +84,10 @@ Inv for Mat3 - self.m12 * minor_m11_m23 + self.m13 * minor_m11_m22; - if det.is_zero() - { false } - else - { + if det.is_zero() { + false + } + else { *self = Mat3::new( (minor_m12_m23 / det), ((self.m13 * self.m32 - self.m33 * self.m12) / det), diff --git a/src/tests/mat.rs b/src/tests/mat.rs index fa5d5648..7cdb6dcd 100644 --- a/src/tests/mat.rs +++ b/src/tests/mat.rs @@ -25,8 +25,7 @@ use adaptors::rotmat::Rotmat; macro_rules! test_inv_mat_impl( ($t: ty) => ( - do 10000.times - { + do 10000.times { let randmat : $t = random(); assert!((randmat.inverse().unwrap() * randmat).approx_eq(&One::one())); @@ -36,8 +35,7 @@ macro_rules! test_inv_mat_impl( macro_rules! test_transpose_mat_impl( ($t: ty) => ( - do 10000.times - { + do 10000.times { let randmat : $t = random(); assert!(randmat.transposed().transposed().eq(&randmat)); @@ -46,58 +44,68 @@ macro_rules! test_transpose_mat_impl( ) #[test] -fn test_transpose_mat1() -{ test_transpose_mat_impl!(Mat1); } +fn test_transpose_mat1() { + test_transpose_mat_impl!(Mat1); +} #[test] -fn test_transpose_mat2() -{ test_transpose_mat_impl!(Mat2); } +fn test_transpose_mat2() { + test_transpose_mat_impl!(Mat2); +} #[test] -fn test_transpose_mat3() -{ test_transpose_mat_impl!(Mat3); } +fn test_transpose_mat3() { + test_transpose_mat_impl!(Mat3); +} #[test] -fn test_transpose_mat4() -{ test_transpose_mat_impl!(Mat4); } +fn test_transpose_mat4() { + test_transpose_mat_impl!(Mat4); +} #[test] -fn test_transpose_mat5() -{ test_transpose_mat_impl!(Mat5); } +fn test_transpose_mat5() { + test_transpose_mat_impl!(Mat5); +} #[test] -fn test_transpose_mat6() -{ test_transpose_mat_impl!(Mat6); } +fn test_transpose_mat6() { + test_transpose_mat_impl!(Mat6); +} #[test] -fn test_inv_mat1() -{ test_inv_mat_impl!(Mat1); } +fn test_inv_mat1() { + test_inv_mat_impl!(Mat1); +} #[test] -fn test_inv_mat2() -{ test_inv_mat_impl!(Mat2); } +fn test_inv_mat2() { + test_inv_mat_impl!(Mat2); +} #[test] -fn test_inv_mat3() -{ test_inv_mat_impl!(Mat3); } +fn test_inv_mat3() { + test_inv_mat_impl!(Mat3); +} #[test] -fn test_inv_mat4() -{ test_inv_mat_impl!(Mat4); } +fn test_inv_mat4() { + test_inv_mat_impl!(Mat4); +} #[test] -fn test_inv_mat5() -{ test_inv_mat_impl!(Mat5); } +fn test_inv_mat5() { + test_inv_mat_impl!(Mat5); +} #[test] -fn test_inv_mat6() -{ test_inv_mat_impl!(Mat6); } +fn test_inv_mat6() { + test_inv_mat_impl!(Mat6); +} #[test] -fn test_rotation2() -{ - do 10000.times - { +fn test_rotation2() { + do 10000.times { let randmat = One::one::>>(); let ang = &Vec1::new(abs::(random()) % Real::pi()); @@ -106,18 +114,15 @@ fn test_rotation2() } #[test] -fn test_index_mat2() -{ +fn test_index_mat2() { let mat: Mat2 = random(); assert!(mat.at((0, 1)) == mat.transposed().at((1, 0))); } #[test] -fn test_inv_rotation3() -{ - do 10000.times - { +fn test_inv_rotation3() { + do 10000.times { let randmat = One::one::>>(); let dir: Vec3 = random(); let ang = &dir.normalized().scalar_mul(&(abs::(random()) % Real::pi())); diff --git a/src/tests/vec.rs b/src/tests/vec.rs index 46075591..a5759fc6 100644 --- a/src/tests/vec.rs +++ b/src/tests/vec.rs @@ -22,252 +22,280 @@ use traits::iterable::{Iterable, IterableMut}; use traits::scalar_op::{ScalarMul, ScalarDiv, ScalarAdd, ScalarSub}; macro_rules! test_iterator_impl( - ($t: ty, $n: ty) => ( - do 10000.times - { - let v: $t = random(); - let mut mv: $t = v.clone(); - let n: $n = random(); + ($t: ty, $n: ty) => ( + do 10000.times { + let v: $t = random(); + let mut mv: $t = v.clone(); + let n: $n = random(); - let nv: $t = v.iter().transform(|e| e * n).collect(); + let nv: $t = v.iter().transform(|e| e * n).collect(); - for e in mv.mut_iter() - { *e = *e * n } + for e in mv.mut_iter() { + *e = *e * n + } - assert!(nv == mv && nv == v.scalar_mul(&n)); - } - ) + assert!(nv == mv && nv == v.scalar_mul(&n)); + } + ) ) macro_rules! test_commut_dot_impl( - ($t: ty) => ( - do 10000.times - { - let v1 : $t = random(); - let v2 : $t = random(); - - assert!(v1.dot(&v2).approx_eq(&v2.dot(&v1))); - } - ); + ($t: ty) => ( + do 10000.times { + let v1 : $t = random(); + let v2 : $t = random(); + + assert!(v1.dot(&v2).approx_eq(&v2.dot(&v1))); + } + ); ) macro_rules! test_scalar_op_impl( - ($t: ty, $n: ty) => ( - do 10000.times - { - let v1 : $t = random(); - let n : $n = random(); - - assert!(v1.scalar_mul(&n).scalar_div(&n).approx_eq(&v1)); - assert!(v1.scalar_div(&n).scalar_mul(&n).approx_eq(&v1)); - assert!(v1.scalar_sub(&n).scalar_add(&n).approx_eq(&v1)); - assert!(v1.scalar_add(&n).scalar_sub(&n).approx_eq(&v1)); + ($t: ty, $n: ty) => ( + do 10000.times { + let v1 : $t = random(); + let n : $n = random(); + + assert!(v1.scalar_mul(&n).scalar_div(&n).approx_eq(&v1)); + assert!(v1.scalar_div(&n).scalar_mul(&n).approx_eq(&v1)); + assert!(v1.scalar_sub(&n).scalar_add(&n).approx_eq(&v1)); + assert!(v1.scalar_add(&n).scalar_sub(&n).approx_eq(&v1)); - let mut v1 : $t = random(); - let v0 : $t = v1.clone(); - let n : $n = random(); + let mut v1 : $t = random(); + let v0 : $t = v1.clone(); + let n : $n = random(); - v1.scalar_mul_inplace(&n); - v1.scalar_div_inplace(&n); - - assert!(v1.approx_eq(&v0)); - } - ); + v1.scalar_mul_inplace(&n); + v1.scalar_div_inplace(&n); + + assert!(v1.approx_eq(&v0)); + } + ); ) macro_rules! test_basis_impl( - ($t: ty) => ( - do 10000.times - { - do Basis::canonical_basis::<$t> |e1| - { - do Basis::canonical_basis::<$t> |e2| - { assert!(e1 == e2 || e1.dot(&e2).approx_eq(&Zero::zero())) } + ($t: ty) => ( + do 10000.times { + do Basis::canonical_basis::<$t> |e1| { + do Basis::canonical_basis::<$t> |e2| { + assert!(e1 == e2 || e1.dot(&e2).approx_eq(&Zero::zero())) + } - assert!(e1.norm().approx_eq(&One::one())); - } - } - ); + assert!(e1.norm().approx_eq(&One::one())); + } + } + ); ) macro_rules! test_subspace_basis_impl( - ($t: ty) => ( - do 10000.times - { - let v : $t = random(); - let v1 = v.normalized(); + ($t: ty) => ( + do 10000.times { + let v : $t = random(); + let v1 = v.normalized(); - do v1.orthonormal_subspace_basis() |e1| - { - // check vectors are orthogonal to v1 - assert!(v1.dot(&e1).approx_eq(&Zero::zero())); - // check vectors form an orthonormal basis - assert!(e1.norm().approx_eq(&One::one())); - // check vectors form an ortogonal basis - do v1.orthonormal_subspace_basis() |e2| - { assert!(e1 == e2 || e1.dot(&e2).approx_eq(&Zero::zero())) } - } - } - ); + do v1.orthonormal_subspace_basis() |e1| { + // check vectors are orthogonal to v1 + assert!(v1.dot(&e1).approx_eq(&Zero::zero())); + // check vectors form an orthonormal basis + assert!(e1.norm().approx_eq(&One::one())); + // check vectors form an ortogonal basis + do v1.orthonormal_subspace_basis() |e2| { + assert!(e1 == e2 || e1.dot(&e2).approx_eq(&Zero::zero())) + } + } + } + ); ) #[test] -fn test_cross_vec3() -{ - do 10000.times - { - let v1 : Vec3 = random(); - let v2 : Vec3 = random(); - let v3 : Vec3 = v1.cross(&v2); +fn test_cross_vec3() { + do 10000.times { + let v1 : Vec3 = random(); + let v2 : Vec3 = random(); + let v3 : Vec3 = v1.cross(&v2); - assert!(v3.dot(&v2).approx_eq(&Zero::zero())); - assert!(v3.dot(&v1).approx_eq(&Zero::zero())); - } + assert!(v3.dot(&v2).approx_eq(&Zero::zero())); + assert!(v3.dot(&v1).approx_eq(&Zero::zero())); + } } #[test] -fn test_commut_dot_vec0() -{ test_commut_dot_impl!(Vec0); } +fn test_commut_dot_vec0() { + test_commut_dot_impl!(Vec0); +} #[test] -fn test_commut_dot_vec1() -{ test_commut_dot_impl!(Vec1); } +fn test_commut_dot_vec1() { + test_commut_dot_impl!(Vec1); +} #[test] -fn test_commut_dot_vec2() -{ test_commut_dot_impl!(Vec2); } +fn test_commut_dot_vec2() { + test_commut_dot_impl!(Vec2); +} #[test] -fn test_commut_dot_vec3() -{ test_commut_dot_impl!(Vec3); } +fn test_commut_dot_vec3() { + test_commut_dot_impl!(Vec3); +} #[test] -fn test_commut_dot_vec4() -{ test_commut_dot_impl!(Vec4); } +fn test_commut_dot_vec4() { + test_commut_dot_impl!(Vec4); +} #[test] -fn test_commut_dot_vec5() -{ test_commut_dot_impl!(Vec5); } +fn test_commut_dot_vec5() { + test_commut_dot_impl!(Vec5); +} #[test] -fn test_commut_dot_vec6() -{ test_commut_dot_impl!(Vec6); } +fn test_commut_dot_vec6() { + test_commut_dot_impl!(Vec6); +} #[test] -fn test_basis_vec0() -{ test_basis_impl!(Vec0); } +fn test_basis_vec0() { + test_basis_impl!(Vec0); +} #[test] -fn test_basis_vec1() -{ test_basis_impl!(Vec1); } +fn test_basis_vec1() { + test_basis_impl!(Vec1); +} #[test] -fn test_basis_vec2() -{ test_basis_impl!(Vec2); } +fn test_basis_vec2() { + test_basis_impl!(Vec2); +} #[test] -fn test_basis_vec3() -{ test_basis_impl!(Vec3); } +fn test_basis_vec3() { + test_basis_impl!(Vec3); +} #[test] -fn test_basis_vec4() -{ test_basis_impl!(Vec4); } +fn test_basis_vec4() { + test_basis_impl!(Vec4); +} #[test] -fn test_basis_vec5() -{ test_basis_impl!(Vec5); } +fn test_basis_vec5() { + test_basis_impl!(Vec5); +} #[test] -fn test_basis_vec6() -{ test_basis_impl!(Vec6); } +fn test_basis_vec6() { + test_basis_impl!(Vec6); +} #[test] -fn test_subspace_basis_vec0() -{ test_subspace_basis_impl!(Vec0); } +fn test_subspace_basis_vec0() { + test_subspace_basis_impl!(Vec0); +} #[test] -fn test_subspace_basis_vec1() -{ test_subspace_basis_impl!(Vec1); } +fn test_subspace_basis_vec1() { + test_subspace_basis_impl!(Vec1); +} #[test] -fn test_subspace_basis_vec2() -{ test_subspace_basis_impl!(Vec2); } +fn test_subspace_basis_vec2() { + test_subspace_basis_impl!(Vec2); +} #[test] -fn test_subspace_basis_vec3() -{ test_subspace_basis_impl!(Vec3); } +fn test_subspace_basis_vec3() { + test_subspace_basis_impl!(Vec3); +} #[test] -fn test_subspace_basis_vec4() -{ test_subspace_basis_impl!(Vec4); } +fn test_subspace_basis_vec4() { + test_subspace_basis_impl!(Vec4); +} #[test] -fn test_subspace_basis_vec5() -{ test_subspace_basis_impl!(Vec5); } +fn test_subspace_basis_vec5() { + test_subspace_basis_impl!(Vec5); +} #[test] -fn test_subspace_basis_vec6() -{ test_subspace_basis_impl!(Vec6); } +fn test_subspace_basis_vec6() { + test_subspace_basis_impl!(Vec6); +} #[test] -fn test_scalar_op_vec0() -{ test_scalar_op_impl!(Vec0, f64); } +fn test_scalar_op_vec0() { + test_scalar_op_impl!(Vec0, f64); +} #[test] -fn test_scalar_op_vec1() -{ test_scalar_op_impl!(Vec1, f64); } +fn test_scalar_op_vec1() { + test_scalar_op_impl!(Vec1, f64); +} #[test] -fn test_scalar_op_vec2() -{ test_scalar_op_impl!(Vec2, f64); } +fn test_scalar_op_vec2() { + test_scalar_op_impl!(Vec2, f64); +} #[test] -fn test_scalar_op_vec3() -{ test_scalar_op_impl!(Vec3, f64); } +fn test_scalar_op_vec3() { + test_scalar_op_impl!(Vec3, f64); +} #[test] -fn test_scalar_op_vec4() -{ test_scalar_op_impl!(Vec4, f64); } +fn test_scalar_op_vec4() { + test_scalar_op_impl!(Vec4, f64); +} #[test] -fn test_scalar_op_vec5() -{ test_scalar_op_impl!(Vec5, f64); } +fn test_scalar_op_vec5() { + test_scalar_op_impl!(Vec5, f64); +} #[test] -fn test_scalar_op_vec6() -{ test_scalar_op_impl!(Vec6, f64); } +fn test_scalar_op_vec6() { + test_scalar_op_impl!(Vec6, f64); +} #[test] -fn test_iterator_vec0() -{ test_iterator_impl!(Vec0, f64); } +fn test_iterator_vec0() { + test_iterator_impl!(Vec0, f64); +} #[test] -fn test_iterator_vec1() -{ test_iterator_impl!(Vec1, f64); } +fn test_iterator_vec1() { + test_iterator_impl!(Vec1, f64); +} #[test] -fn test_iterator_vec2() -{ test_iterator_impl!(Vec2, f64); } +fn test_iterator_vec2() { + test_iterator_impl!(Vec2, f64); +} #[test] -fn test_iterator_vec3() -{ test_iterator_impl!(Vec3, f64); } +fn test_iterator_vec3() { + test_iterator_impl!(Vec3, f64); +} #[test] -fn test_iterator_vec4() -{ test_iterator_impl!(Vec4, f64); } +fn test_iterator_vec4() { + test_iterator_impl!(Vec4, f64); +} #[test] -fn test_iterator_vec5() -{ test_iterator_impl!(Vec5, f64); } +fn test_iterator_vec5() { + test_iterator_impl!(Vec5, f64); +} #[test] -fn test_iterator_vec6() -{ test_iterator_impl!(Vec6, f64); } +fn test_iterator_vec6() { + test_iterator_impl!(Vec6, f64); +} #[test] -fn test_ord_vec3() -{ +fn test_ord_vec3() { // equality assert!(Vec3::new(0.5, 0.5, 0.5) == Vec3::new(0.5, 0.5, 0.5)); assert!(!(Vec3::new(1.5, 0.5, 0.5) == Vec3::new(0.5, 0.5, 0.5))); @@ -287,8 +315,7 @@ fn test_ord_vec3() } #[test] -fn test_min_max_vec3() -{ +fn test_min_max_vec3() { assert_eq!(Vec3::new(1, 2, 3).max(&Vec3::new(3, 2, 1)), Vec3::new(3, 2, 3)); assert_eq!(Vec3::new(1, 2, 3).min(&Vec3::new(3, 2, 1)), Vec3::new(1, 2, 1)); assert_eq!( diff --git a/src/traits/basis.rs b/src/traits/basis.rs index 60ac27c8..fe60d99c 100644 --- a/src/traits/basis.rs +++ b/src/traits/basis.rs @@ -1,6 +1,5 @@ /// Traits of objecs which can form a basis. -pub trait Basis -{ +pub trait Basis { /// Iterate through the canonical basis of the space in which this object lives. fn canonical_basis(&fn(Self)); @@ -8,23 +7,23 @@ pub trait Basis fn orthonormal_subspace_basis(&self, &fn(Self)); /// Creates the canonical basis of the space in which this object lives. - fn canonical_basis_list() -> ~[Self] - { + fn canonical_basis_list() -> ~[Self] { let mut res = ~[]; - do Basis::canonical_basis:: |elem| - { res.push(elem) } + do Basis::canonical_basis:: |elem| { + res.push(elem) + } res } /// Creates a basis of the subspace orthogonal to `self`. - fn orthonormal_subspace_basis_list(&self) -> ~[Self] - { + fn orthonormal_subspace_basis_list(&self) -> ~[Self] { let mut res = ~[]; - do self.orthonormal_subspace_basis |elem| - { res.push(elem) } + do self.orthonormal_subspace_basis |elem| { + res.push(elem) + } res } diff --git a/src/traits/column.rs b/src/traits/column.rs index 37aaf77b..aff97047 100644 --- a/src/traits/column.rs +++ b/src/traits/column.rs @@ -1,6 +1,5 @@ /// Traits to access columns of a matrix. -pub trait Column -{ +pub trait Column { /// Reads the `i`-th column of `self`. fn column(&self, i: uint) -> C; /// Writes the `i`-th column of `self`. diff --git a/src/traits/homogeneous.rs b/src/traits/homogeneous.rs index 8f0390c5..72fcbff8 100644 --- a/src/traits/homogeneous.rs +++ b/src/traits/homogeneous.rs @@ -1,13 +1,11 @@ /// Traits of objects which can be put in homogeneous coordinates. -pub trait ToHomogeneous -{ +pub trait ToHomogeneous { /// Gets the homogeneous coordinates version of this object. fn to_homogeneous(&self) -> U; } /// Traits of objects which can be build from an homogeneous coordinate representation. -pub trait FromHomogeneous -{ +pub trait FromHomogeneous { /// Builds an object with its homogeneous coordinate version. Note this it is not required for /// `from` to be the iverse of `to_homogeneous`. Typically, `from` will remove some informations /// unrecoverable by `to_homogeneous`. diff --git a/src/traits/indexable.rs b/src/traits/indexable.rs index f545a785..ff4f6dab 100644 --- a/src/traits/indexable.rs +++ b/src/traits/indexable.rs @@ -7,8 +7,7 @@ /// It exists because the `Index` trait cannot be used to express write access. /// Thus, this is the same as the `Index` trait but without the syntactic sugar and with a method /// to write to a specific index. -pub trait Indexable -{ +pub trait Indexable { /// Reads the `i`-th element of `self`. fn at(&self, i: Index) -> Res; /// Writes to the `i`-th element of `self`. diff --git a/src/traits/inv.rs b/src/traits/inv.rs index e4340d1b..4fed280a 100644 --- a/src/traits/inv.rs +++ b/src/traits/inv.rs @@ -1,8 +1,7 @@ /** * Trait of inversible objects. Typically used to implement matrix inverse. */ -pub trait Inv -{ +pub trait Inv { /// Returns the inverse of an element. fn inverse(&self) -> Option; /// Inplace version of `inverse`. diff --git a/src/traits/iterable.rs b/src/traits/iterable.rs index 8ef70cf4..1e14e652 100644 --- a/src/traits/iterable.rs +++ b/src/traits/iterable.rs @@ -1,15 +1,13 @@ use std::vec; /// Traits of objects which can be iterated through like a vector. -pub trait Iterable -{ +pub trait Iterable { /// Gets a vector-like read-only iterator. fn iter<'l>(&'l self) -> vec::VecIterator<'l, N>; } /// Traits of mutable objects which can be iterated through like a vector. -pub trait IterableMut -{ +pub trait IterableMut { /// Gets a vector-like read-write iterator. fn mut_iter<'l>(&'l mut self) -> vec::VecMutIterator<'l, N>; } @@ -18,13 +16,11 @@ pub trait IterableMut * FIXME: the prevous traits are only workarounds. * It should be something like: - pub trait Iterable<'self, N, I: Iterator> - { + pub trait Iterable<'self, N, I: Iterator> { fn iter(&'self self) -> I; } - pub trait IterableMut<'self, N, I: Iterator> - { + pub trait IterableMut<'self, N, I: Iterator> { fn mut_iter(&'self self) -> I; } diff --git a/src/traits/mat_cast.rs b/src/traits/mat_cast.rs index 1bf14a51..5e6ec162 100644 --- a/src/traits/mat_cast.rs +++ b/src/traits/mat_cast.rs @@ -1,7 +1,6 @@ /// Trait of matrices which can be converted to another matrix. Used to change the type of a matrix /// components. -pub trait MatCast -{ +pub trait MatCast { /// Converts `m` to have the type `M`. fn from(m: Self) -> M; } diff --git a/src/traits/norm.rs b/src/traits/norm.rs index 7fe10adb..e7c47f80 100644 --- a/src/traits/norm.rs +++ b/src/traits/norm.rs @@ -1,8 +1,7 @@ /** * 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) -> N; diff --git a/src/traits/ring.rs b/src/traits/ring.rs index 1364d3f3..87f2f271 100644 --- a/src/traits/ring.rs +++ b/src/traits/ring.rs @@ -7,8 +7,7 @@ use std::num::{One, Zero}; * `Zero`) respectively neutral and absorbant wrt the multiplication. */ pub trait Ring : -Sub + Add + Neg + Mul + One + Zero -{ } +Sub + Add + Neg + Mul + One + Zero { } impl + Add + Neg + Mul + One + Zero> Ring for N; diff --git a/src/traits/rlmul.rs b/src/traits/rlmul.rs index bcf2bd07..f85624e0 100644 --- a/src/traits/rlmul.rs +++ b/src/traits/rlmul.rs @@ -1,8 +1,7 @@ /** * Trait of objects having a right multiplication with another element. */ -pub trait RMul -{ +pub trait RMul { /// Computes self * v fn rmul(&self, v : &V) -> V; } @@ -10,8 +9,7 @@ pub trait RMul /** * Trait of objects having a left multiplication with another element. */ -pub trait LMul -{ +pub trait LMul { /// Computes v * self fn lmul(&self, &V) -> V; } diff --git a/src/traits/rotation.rs b/src/traits/rotation.rs index 06c1373a..a3e8d79c 100644 --- a/src/traits/rotation.rs +++ b/src/traits/rotation.rs @@ -3,8 +3,7 @@ use traits::translation::{Translation, Translatable}; /// Trait of object which represent a rotation, and to wich new rotations can /// be appended. A rotation is assumed to be an isomitry without translation /// and without reflexion. -pub trait Rotation -{ +pub trait Rotation { /// Gets the rotation associated with this object. fn rotation(&self) -> V; @@ -18,8 +17,7 @@ pub trait Rotation /// Trait of objects which can be put on an alternate form which represent a rotation. This is /// typically implemented by structures requiring an internal restructuration to be able to /// represent a rotation. -pub trait Rotatable> -{ +pub trait Rotatable> { /// Appends a rotation from an alternative representation. Such /// representation has the same format as the one returned by `rotation`. fn rotated(&self, &V) -> Res; @@ -27,8 +25,7 @@ pub trait Rotatable> /// Trait of objects able to rotate other objects. This is typically implemented by matrices which /// rotate vectors. -pub trait Rotate -{ +pub trait Rotate { /// Apply a rotation to an object. fn rotate(&self, &V) -> V; /// Apply an inverse rotation to an object. @@ -50,8 +47,7 @@ pub fn rotated_wrt_point, m: &M, ammount: &AV, center: &LV) - -> M2 -{ + -> M2 { let mut res = m.translated(&-center); res.rotate_by(ammount); @@ -72,8 +68,7 @@ pub fn rotate_wrt_point + Translation, AV>( m: &mut M, ammount: &AV, - center: &LV) -{ + center: &LV) { m.translate_by(&-center); m.rotate_by(ammount); m.translate_by(center); @@ -93,8 +88,9 @@ pub fn rotated_wrt_center + Translation, AV>( m: &M, ammount: &AV) - -> M2 -{ rotated_wrt_point(m, ammount, &m.translation()) } + -> M2 { + rotated_wrt_point(m, ammount, &m.translation()) +} /** * Applies a rotation centered on the input translation. @@ -108,8 +104,7 @@ pub fn rotate_wrt_center + Translation + Rotation LV: Neg, AV>( m: &mut M, - ammount: &AV) -{ + ammount: &AV) { let t = m.translation(); rotate_wrt_point(m, ammount, &t) diff --git a/src/traits/sample.rs b/src/traits/sample.rs index 50bcf621..8cad2871 100644 --- a/src/traits/sample.rs +++ b/src/traits/sample.rs @@ -1,17 +1,16 @@ /// Traits of vectors able to sample a sphere. The number of sample must be sufficient to /// approximate a sphere using support mapping functions. -pub trait UniformSphereSample -{ +pub trait UniformSphereSample { /// Iterate throught the samples. pub fn sample(&fn(&'static Self)); /// Gets the list of all samples. - pub fn sample_list() -> ~[&'static Self] - { + pub fn sample_list() -> ~[&'static Self] { let mut res = ~[]; - do UniformSphereSample::sample:: |s| - { res.push(s) } + do UniformSphereSample::sample:: |s| { + res.push(s) + } res } diff --git a/src/traits/scalar_op.rs b/src/traits/scalar_op.rs index 856a3a4c..f2f2f9a5 100644 --- a/src/traits/scalar_op.rs +++ b/src/traits/scalar_op.rs @@ -1,8 +1,7 @@ /** * Trait of objects having a multiplication with a scalar. */ -pub trait ScalarMul -{ +pub trait ScalarMul { /// Gets the result of a multiplication by a scalar. fn scalar_mul(&self, &N) -> Self; @@ -13,8 +12,7 @@ pub trait ScalarMul /** * Trait of objects having a division with a scalar. */ -pub trait ScalarDiv -{ +pub trait ScalarDiv { /// Gets the result of a division by a scalar. fn scalar_div(&self, &N) -> Self; @@ -25,8 +23,7 @@ pub trait ScalarDiv /** * Trait of objects having an addition with a scalar. */ -pub trait ScalarAdd -{ +pub trait ScalarAdd { /// Gets the result of an addition by a scalar. fn scalar_add(&self, &N) -> Self; @@ -37,8 +34,7 @@ pub trait ScalarAdd /** * Trait of objects having a subtraction with a scalar. */ -pub trait ScalarSub -{ +pub trait ScalarSub { /// Gets the result of a subtraction by a scalar. fn scalar_sub(&self, &N) -> Self; diff --git a/src/traits/sub_dot.rs b/src/traits/sub_dot.rs index eb00c194..1452addc 100644 --- a/src/traits/sub_dot.rs +++ b/src/traits/sub_dot.rs @@ -1,8 +1,7 @@ use traits::dot::Dot; /// Traits of objects with a subtract and a dot product. Exists only for optimization purpose. -pub trait SubDot : Sub + Dot -{ +pub trait SubDot : Sub + Dot { /** * Short-cut to compute the projection of a point on a vector, but without * computing intermediate vectors. diff --git a/src/traits/transformation.rs b/src/traits/transformation.rs index 7774b409..cb228b90 100644 --- a/src/traits/transformation.rs +++ b/src/traits/transformation.rs @@ -1,8 +1,7 @@ /// Trait of object which represent a transformation, and to wich new transformations can /// be appended. A transformation is assumed to be an isomitry without translation /// and without reflexion. -pub trait Transformation -{ +pub trait Transformation { /// Gets the transformation associated with this object. fn transformation(&self) -> M; @@ -15,8 +14,7 @@ pub trait Transformation /// Trait of objects able to transform other objects. This is typically implemented by matrices which /// transform vectors. -pub trait Transform -{ +pub trait Transform { // XXX: sadly we cannot call this `transform` as it conflicts with the // iterators' `transform` function (which seems always exist). /// Apply a transformation to an object. @@ -28,8 +26,7 @@ pub trait Transform /// Trait of objects which can be put on an alternate form which represent a transformation. This is /// typically implemented by structures requiring an internal restructuration to be able to /// represent a transformation. -pub trait Transformable> -{ +pub trait Transformable> { /// Appends a transformation from an alternative representation. Such /// representation has the same format as the one returned by `transformation`. fn transformed(&self, &M) -> Res; diff --git a/src/traits/translation.rs b/src/traits/translation.rs index ebf22ff7..450daa2b 100644 --- a/src/traits/translation.rs +++ b/src/traits/translation.rs @@ -1,7 +1,6 @@ /// Trait of object which represent a translation, and to wich new translation /// can be appended. -pub trait Translation -{ +pub trait Translation { // FIXME: add a "from translation: translantion(V) -> Self ? /// Gets the translation associated with this object. fn translation(&self) -> V; @@ -15,8 +14,7 @@ pub trait Translation /// Trait of objects able to rotate other objects. This is typically implemented by matrices which /// rotate vectors. -pub trait Translate -{ +pub trait Translate { /// Apply a translation to an object. fn translate(&self, &V) -> V; /// Apply an inverse translation to an object. @@ -26,8 +24,7 @@ pub trait Translate /// Trait of objects which can be put on an alternate form which represent a translation. This is /// typically implemented by structures requiring an internal restructuration to be able to /// represent a translation. -pub trait Translatable> -{ +pub trait Translatable> { /// Appends a translation from an alternative representation. Such /// representation has the same format as the one returned by `translation`. fn translated(&self, &V) -> Res; diff --git a/src/traits/transpose.rs b/src/traits/transpose.rs index 4d504ade..eb312d1d 100644 --- a/src/traits/transpose.rs +++ b/src/traits/transpose.rs @@ -2,8 +2,7 @@ /// Trait of objects which can be transposed. Note that, for the moment, this /// does not allow the implementation by non-square matrix (or anything which /// is not stable by transposition). -pub trait Transpose -{ +pub trait Transpose { /// Computes the transpose of a matrix. fn transposed(&self) -> Self; diff --git a/src/traits/vec_cast.rs b/src/traits/vec_cast.rs index 517dfb92..bfa9ec30 100644 --- a/src/traits/vec_cast.rs +++ b/src/traits/vec_cast.rs @@ -1,7 +1,6 @@ /// Trait of vectors which can be converted to another matrix. Used to change the type of a vector /// components. -pub trait VecCast -{ +pub trait VecCast { /// Converts `v` to have the type `V`. fn from(v: Self) -> V; } diff --git a/src/traits/vector_space.rs b/src/traits/vector_space.rs index 50aa4a1f..2b15fbf0 100644 --- a/src/traits/vector_space.rs +++ b/src/traits/vector_space.rs @@ -8,8 +8,7 @@ use traits::scalar_op::{ScalarMul, ScalarDiv}; /// has a distinct element (`Zero`) neutral wrt the addition. pub trait VectorSpace : Sub + Add + Neg + Zero + - ScalarMul + ScalarDiv -{ } + ScalarMul + ScalarDiv { } impl + Add + Neg + Zero + ScalarMul + ScalarDiv, N: DivisionRing> VectorSpace for V; diff --git a/src/vec.rs b/src/vec.rs index 19794b17..ed8f3e1b 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -32,8 +32,7 @@ pub struct Vec0; /// Vector of dimension 1. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Vec1 -{ +pub struct Vec1 { /// First component of the vector. x: N } @@ -70,8 +69,7 @@ from_homogeneous_impl!(Vec1, Vec2, y, x) /// Vector of dimension 2. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Vec2 -{ +pub struct Vec2 { /// First component of the vector. x: N, /// Second component of the vector. @@ -110,8 +108,7 @@ from_homogeneous_impl!(Vec2, Vec3, z, x, y) /// Vector of dimension 3. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Vec3 -{ +pub struct Vec3 { /// First component of the vector. x: N, /// Second component of the vector. @@ -152,8 +149,7 @@ from_homogeneous_impl!(Vec3, Vec4, w, x, y, z) /// Vector of dimension 4. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Vec4 -{ +pub struct Vec4 { /// First component of the vector. x: N, /// Second component of the vector. @@ -196,8 +192,7 @@ from_homogeneous_impl!(Vec4, Vec5, a, x, y, z, w) /// Vector of dimension 5. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Vec5 -{ +pub struct Vec5 { /// First component of the vector. x: N, /// Second component of the vector. @@ -242,8 +237,7 @@ from_homogeneous_impl!(Vec5, Vec6, b, x, y, z, w, a) /// Vector of dimension 6. #[deriving(Eq, Encodable, Decodable, Clone, DeepClone, IterBytes, Rand, Zero, ToStr)] -pub struct Vec6 -{ +pub struct Vec6 { /// First component of the vector. x: N, /// Second component of the vector. diff --git a/src/vec0_spec.rs b/src/vec0_spec.rs index 88d22086..1a389842 100644 --- a/src/vec0_spec.rs +++ b/src/vec0_spec.rs @@ -16,179 +16,176 @@ use traits::division_ring::DivisionRing; use traits::indexable::Indexable; use vec; -impl vec::Vec0 -{ +impl vec::Vec0 { /// Creates a new vector. #[inline] - pub fn new() -> vec::Vec0 - { vec::Vec0 } + pub fn new() -> vec::Vec0 { + vec::Vec0 + } } -impl Indexable for vec::Vec0 -{ +impl Indexable for vec::Vec0 { #[inline] - pub fn at(&self, _: uint) -> N - { fail!("Cannot index a Vec0.") } + pub fn at(&self, _: uint) -> N { + fail!("Cannot index a Vec0.") + } #[inline] - pub fn set(&mut self, _: uint, _: N) - { } + pub fn set(&mut self, _: uint, _: N) { + + } #[inline] - pub fn swap(&mut self, _: uint, _: uint) - { } + pub fn swap(&mut self, _: uint, _: uint) { + + } } -impl vec::Vec0 -{ +impl vec::Vec0 { /// Creates a new vector. The parameter is not taken in account. #[inline] - pub fn new_repeat(_: N) -> vec::Vec0 - { vec::Vec0 } + pub fn new_repeat(_: N) -> vec::Vec0 { + vec::Vec0 + } } -impl Iterable for vec::Vec0 -{ - fn iter<'l>(&'l self) -> VecIterator<'l, N> - { unsafe { cast::transmute::<&'l vec::Vec0, &'l [N, ..0]>(self).iter() } } +impl Iterable for vec::Vec0 { + fn iter<'l>(&'l self) -> VecIterator<'l, N> { + unsafe { cast::transmute::<&'l vec::Vec0, &'l [N, ..0]>(self).iter() } + } } -impl IterableMut for vec::Vec0 -{ - fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> - { unsafe { cast::transmute::<&'l mut vec::Vec0, &'l mut [N, ..0]>(self).mut_iter() } } +impl IterableMut for vec::Vec0 { + fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> { + unsafe { cast::transmute::<&'l mut vec::Vec0, &'l mut [N, ..0]>(self).mut_iter() } + } } -impl Dim for vec::Vec0 -{ +impl Dim for vec::Vec0 { #[inline] - fn dim() -> uint - { 0 } + fn dim() -> uint { + 0 + } } -impl> Basis for vec::Vec0 -{ - pub fn canonical_basis(_: &fn(vec::Vec0)) - { } +impl> Basis for vec::Vec0 { + pub fn canonical_basis(_: &fn(vec::Vec0)) { } - pub fn orthonormal_subspace_basis(&self, _: &fn(vec::Vec0)) - { } + pub fn orthonormal_subspace_basis(&self, _: &fn(vec::Vec0)) { } } -impl> Add, vec::Vec0> for vec::Vec0 -{ +impl> Add, vec::Vec0> for vec::Vec0 { #[inline] - fn add(&self, _: &vec::Vec0) -> vec::Vec0 - { vec::Vec0 } + fn add(&self, _: &vec::Vec0) -> vec::Vec0 { + vec::Vec0 + } } -impl> Sub, vec::Vec0> for vec::Vec0 -{ +impl> Sub, vec::Vec0> for vec::Vec0 { #[inline] - fn sub(&self, _: &vec::Vec0) -> vec::Vec0 - { vec::Vec0 } + fn sub(&self, _: &vec::Vec0) -> vec::Vec0 { + vec::Vec0 + } } -impl> Neg> for vec::Vec0 -{ +impl> Neg> for vec::Vec0 { #[inline] - fn neg(&self) -> vec::Vec0 - { vec::Vec0 } + fn neg(&self) -> vec::Vec0 { + vec::Vec0 + } } -impl Dot for vec::Vec0 -{ +impl Dot for vec::Vec0 { #[inline] - fn dot(&self, _: &vec::Vec0) -> N - { Zero::zero() } + fn dot(&self, _: &vec::Vec0) -> N { + Zero::zero() + } } -impl SubDot for vec::Vec0 -{ +impl SubDot for vec::Vec0 { #[inline] - fn sub_dot(&self, _: &vec::Vec0, _: &vec::Vec0) -> N - { Zero::zero() } + fn sub_dot(&self, _: &vec::Vec0, _: &vec::Vec0) -> N { + Zero::zero() + } } -impl> ScalarMul for vec::Vec0 -{ +impl> ScalarMul for vec::Vec0 { #[inline] - fn scalar_mul(&self, _: &N) -> vec::Vec0 - { vec::Vec0 } + fn scalar_mul(&self, _: &N) -> vec::Vec0 { + vec::Vec0 + } #[inline] - fn scalar_mul_inplace(&mut self, _: &N) - { } + fn scalar_mul_inplace(&mut self, _: &N) { } } -impl> ScalarDiv for vec::Vec0 -{ +impl> ScalarDiv for vec::Vec0 { #[inline] - fn scalar_div(&self, _: &N) -> vec::Vec0 - { vec::Vec0 } + fn scalar_div(&self, _: &N) -> vec::Vec0 { + vec::Vec0 + } #[inline] - fn scalar_div_inplace(&mut self, _: &N) - { } + fn scalar_div_inplace(&mut self, _: &N) { } } -impl> ScalarAdd for vec::Vec0 -{ +impl> ScalarAdd for vec::Vec0 { #[inline] - fn scalar_add(&self, _: &N) -> vec::Vec0 - { vec::Vec0 } + fn scalar_add(&self, _: &N) -> vec::Vec0 { + vec::Vec0 + } #[inline] - fn scalar_add_inplace(&mut self, _: &N) - { } + fn scalar_add_inplace(&mut self, _: &N) { } } -impl> ScalarSub for vec::Vec0 -{ +impl> ScalarSub for vec::Vec0 { #[inline] - fn scalar_sub(&self, _: &N) -> vec::Vec0 - { vec::Vec0 } + fn scalar_sub(&self, _: &N) -> vec::Vec0 { + vec::Vec0 + } #[inline] - fn scalar_sub_inplace(&mut self, _: &N) - { } + fn scalar_sub_inplace(&mut self, _: &N) { } } -impl + Neg> Translation> for vec::Vec0 -{ +impl + Neg> Translation> for vec::Vec0 { #[inline] - fn translation(&self) -> vec::Vec0 - { self.clone() } + fn translation(&self) -> vec::Vec0 { + self.clone() + } #[inline] - fn inv_translation(&self) -> vec::Vec0 - { -self } + fn inv_translation(&self) -> vec::Vec0 { + -self + } #[inline] - fn translate_by(&mut self, t: &vec::Vec0) - { *self = *self + *t; } + fn translate_by(&mut self, t: &vec::Vec0) { + *self = *self + *t; + } } -impl + Neg + Clone> Translatable, vec::Vec0> for vec::Vec0 -{ +impl + Neg + Clone> Translatable, vec::Vec0> for vec::Vec0 { #[inline] - fn translated(&self, t: &vec::Vec0) -> vec::Vec0 - { self + *t } + fn translated(&self, t: &vec::Vec0) -> vec::Vec0 { + self + *t + } } -impl Norm for vec::Vec0 -{ +impl Norm for vec::Vec0 { #[inline] - fn sqnorm(&self) -> N - { self.dot(self) } + fn sqnorm(&self) -> N { + self.dot(self) + } #[inline] - fn norm(&self) -> N - { self.sqnorm().sqrt() } + fn norm(&self) -> N { + self.sqnorm().sqrt() + } #[inline] - fn normalized(&self) -> vec::Vec0 - { + fn normalized(&self) -> vec::Vec0 { let mut res : vec::Vec0 = self.clone(); res.normalize(); @@ -197,8 +194,7 @@ impl Norm for vec::Vec0 } #[inline] - fn normalize(&mut self) -> N - { + fn normalize(&mut self) -> N { let l = self.norm(); self.scalar_div_inplace(&l); @@ -207,41 +203,44 @@ impl Norm for vec::Vec0 } } -impl> ApproxEq for vec::Vec0 -{ +impl> ApproxEq for vec::Vec0 { #[inline] - fn approx_epsilon() -> N - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N { + ApproxEq::approx_epsilon::() + } #[inline] - fn approx_eq(&self, _: &vec::Vec0) -> bool - { true } + fn approx_eq(&self, _: &vec::Vec0) -> bool { + true + } #[inline] - fn approx_eq_eps(&self, _: &vec::Vec0, _: &N) -> bool - { true } + fn approx_eq_eps(&self, _: &vec::Vec0, _: &N) -> bool { + true + } } -impl One for vec::Vec0 -{ +impl One for vec::Vec0 { #[inline] - fn one() -> vec::Vec0 - { vec::Vec0 } + fn one() -> vec::Vec0 { + vec::Vec0 + } } -impl> FromIterator for vec::Vec0 -{ - fn from_iterator(_: &mut Iter) -> vec::Vec0 - { vec::Vec0 } +impl> FromIterator for vec::Vec0 { + fn from_iterator(_: &mut Iter) -> vec::Vec0 { + vec::Vec0 + } } -impl Bounded for vec::Vec0 -{ +impl Bounded for vec::Vec0 { #[inline] - fn max_value() -> vec::Vec0 - { vec::Vec0 } + fn max_value() -> vec::Vec0 { + vec::Vec0 + } #[inline] - fn min_value() -> vec::Vec0 - { vec::Vec0 } + fn min_value() -> vec::Vec0 { + vec::Vec0 + } } diff --git a/src/vec_macros.rs b/src/vec_macros.rs index ed9bfbc5..c404a8c1 100644 --- a/src/vec_macros.rs +++ b/src/vec_macros.rs @@ -2,12 +2,10 @@ macro_rules! new_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl $t - { + impl $t { /// Creates a new vector. #[inline] - pub fn new($comp0: N $( , $compN: N )*) -> $t - { + pub fn new($comp0: N $( , $compN: N )*) -> $t { $t { $comp0: $comp0 $(, $compN: $compN )* @@ -19,42 +17,45 @@ macro_rules! new_impl( macro_rules! ord_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl Ord for $t - { + impl Ord for $t { #[inline] - fn lt(&self, other: &$t) -> bool - { self.$comp0 < other.$comp0 $(&& self.$compN < other.$compN)* } + fn lt(&self, other: &$t) -> bool { + self.$comp0 < other.$comp0 $(&& self.$compN < other.$compN)* + } #[inline] - fn le(&self, other: &$t) -> bool - { self.$comp0 <= other.$comp0 $(&& self.$compN <= other.$compN)* } + fn le(&self, other: &$t) -> bool { + self.$comp0 <= other.$comp0 $(&& self.$compN <= other.$compN)* + } #[inline] - fn gt(&self, other: &$t) -> bool - { self.$comp0 > other.$comp0 $(&& self.$compN > other.$compN)* } + fn gt(&self, other: &$t) -> bool { + self.$comp0 > other.$comp0 $(&& self.$compN > other.$compN)* + } #[inline] - fn ge(&self, other: &$t) -> bool - { self.$comp0 >= other.$comp0 $(&& self.$compN >= other.$compN)* } + fn ge(&self, other: &$t) -> bool { + self.$comp0 >= other.$comp0 $(&& self.$compN >= other.$compN)* + } } ) ) macro_rules! orderable_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl Orderable for $t - { + impl Orderable for $t { #[inline] - fn max(&self, other: &$t) -> $t - { $t::new(self.$comp0.max(&other.$comp0) $(, self.$compN.max(&other.$compN))*) } + fn max(&self, other: &$t) -> $t { + $t::new(self.$comp0.max(&other.$comp0) $(, self.$compN.max(&other.$compN))*) + } #[inline] - fn min(&self, other: &$t) -> $t - { $t::new(self.$comp0.min(&other.$comp0) $(, self.$compN.min(&other.$compN))*) } + fn min(&self, other: &$t) -> $t { + $t::new(self.$comp0.min(&other.$comp0) $(, self.$compN.min(&other.$compN))*) + } #[inline] - fn clamp(&self, min: &$t, max: &$t) -> $t - { + fn clamp(&self, min: &$t, max: &$t) -> $t { $t::new(self.$comp0.clamp(&min.$comp0, &max.$comp0) $(, self.$compN.clamp(&min.$comp0, &max.$comp0))*) } @@ -64,12 +65,10 @@ macro_rules! orderable_impl( macro_rules! vec_axis_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl $t - { + impl $t { /// Create a unit vector with its `$comp0` component equal to 1.0. #[inline] - pub fn $comp0() -> $t - { + pub fn $comp0() -> $t { let mut res: $t = Zero::zero(); res.$comp0 = One::one(); @@ -80,8 +79,7 @@ macro_rules! vec_axis_impl( $( /// Create a unit vector with its `$compN` component equal to 1.0. #[inline] - pub fn $compN() -> $t - { + pub fn $compN() -> $t { let mut res: $t = Zero::zero(); res.$compN = One::one(); @@ -95,42 +93,48 @@ macro_rules! vec_axis_impl( macro_rules! vec_cast_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl VecCast<$t> for $t - { + impl VecCast<$t> for $t { #[inline] - pub fn from(v: $t) -> $t - { $t::new(NumCast::from(v.$comp0.clone()) $(, NumCast::from(v.$compN.clone()))*) } + pub fn from(v: $t) -> $t { + $t::new(NumCast::from(v.$comp0.clone()) $(, NumCast::from(v.$compN.clone()))*) + } } ) ) macro_rules! indexable_impl( ($t: ident, $dim: expr) => ( - impl Indexable for $t - { + impl Indexable for $t { #[inline] - pub fn at(&self, i: uint) -> N - { unsafe { cast::transmute::<&$t, &[N, ..$dim]>(self)[i].clone() } } + pub fn at(&self, i: uint) -> N { + unsafe { + cast::transmute::<&$t, &[N, ..$dim]>(self)[i].clone() + } + } #[inline] - pub fn set(&mut self, i: uint, val: N) - { unsafe { cast::transmute::<&mut $t, &mut [N, ..$dim]>(self)[i] = val } } + pub fn set(&mut self, i: uint, val: N) { + unsafe { + cast::transmute::<&mut $t, &mut [N, ..$dim]>(self)[i] = val + } + } #[inline] - pub fn swap(&mut self, i1: uint, i2: uint) - { unsafe { cast::transmute::<&mut $t, &mut [N, ..$dim]>(self).swap(i1, i2) } } + pub fn swap(&mut self, i1: uint, i2: uint) { + unsafe { + cast::transmute::<&mut $t, &mut [N, ..$dim]>(self).swap(i1, i2) + } + } } ) ) macro_rules! new_repeat_impl( ($t: ident, $param: ident, $comp0: ident $(,$compN: ident)*) => ( - impl $t - { + impl $t { /// Creates a new vector with all its components equal to a given value. #[inline] - pub fn new_repeat($param: N) -> $t - { + pub fn new_repeat($param: N) -> $t { $t{ $comp0: $param.clone() $(, $compN: $param.clone() )* @@ -142,43 +146,44 @@ macro_rules! new_repeat_impl( macro_rules! iterable_impl( ($t: ident, $dim: expr) => ( - impl Iterable for $t - { - fn iter<'l>(&'l self) -> VecIterator<'l, N> - { unsafe { cast::transmute::<&'l $t, &'l [N, ..$dim]>(self).iter() } } + impl Iterable for $t { + fn iter<'l>(&'l self) -> VecIterator<'l, N> { + unsafe { + cast::transmute::<&'l $t, &'l [N, ..$dim]>(self).iter() + } + } } ) ) macro_rules! iterable_mut_impl( ($t: ident, $dim: expr) => ( - impl IterableMut for $t - { - fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> - { unsafe { cast::transmute::<&'l mut $t, &'l mut [N, ..$dim]>(self).mut_iter() } } + impl IterableMut for $t { + fn mut_iter<'l>(&'l mut self) -> VecMutIterator<'l, N> { + unsafe { + cast::transmute::<&'l mut $t, &'l mut [N, ..$dim]>(self).mut_iter() + } + } } ) ) macro_rules! dim_impl( ($t: ident, $dim: expr) => ( - impl Dim for $t - { + impl Dim for $t { #[inline] - fn dim() -> uint - { $dim } + fn dim() -> uint { + $dim + } } ) ) macro_rules! basis_impl( ($t: ident, $dim: expr) => ( - impl> Basis for $t - { - pub fn canonical_basis(f: &fn($t)) - { - for i in range(0u, $dim) - { + impl> Basis for $t { + pub fn canonical_basis(f: &fn($t)) { + for i in range(0u, $dim) { let mut basis_element : $t = Zero::zero(); basis_element.set(i, One::one()); @@ -187,30 +192,29 @@ macro_rules! basis_impl( } } - pub fn orthonormal_subspace_basis(&self, f: &fn($t)) - { + pub fn orthonormal_subspace_basis(&self, f: &fn($t)) { // compute the basis of the orthogonal subspace using Gram-Schmidt // orthogonalization algorithm let mut basis: ~[$t] = ~[]; - for i in range(0u, $dim) - { + for i in range(0u, $dim) { let mut basis_element : $t = Zero::zero(); basis_element.set(i, One::one()); - if basis.len() == $dim - 1 - { break; } + if basis.len() == $dim - 1 { + break; + } let mut elt = basis_element.clone(); elt = elt - self.scalar_mul(&basis_element.dot(self)); - for v in basis.iter() - { elt = elt - v.scalar_mul(&elt.dot(v)) }; + for v in basis.iter() { + elt = elt - v.scalar_mul(&elt.dot(v)) + }; - if !elt.sqnorm().approx_eq(&Zero::zero()) - { + if !elt.sqnorm().approx_eq(&Zero::zero()) { let new_element = elt.normalized(); f(new_element.clone()); @@ -225,70 +229,69 @@ macro_rules! basis_impl( macro_rules! add_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> Add<$t, $t> for $t - { + impl> Add<$t, $t> for $t { #[inline] - fn add(&self, other: &$t) -> $t - { $t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN)*) } + fn add(&self, other: &$t) -> $t { + $t::new(self.$comp0 + other.$comp0 $(, self.$compN + other.$compN)*) + } } ) ) macro_rules! sub_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> Sub<$t, $t> for $t - { + impl> Sub<$t, $t> for $t { #[inline] - fn sub(&self, other: &$t) -> $t - { $t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN)*) } + fn sub(&self, other: &$t) -> $t { + $t::new(self.$comp0 - other.$comp0 $(, self.$compN - other.$compN)*) + } } ) ) macro_rules! neg_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> Neg<$t> for $t - { + impl> Neg<$t> for $t { #[inline] - fn neg(&self) -> $t - { $t::new(-self.$comp0 $(, -self.$compN )*) } + fn neg(&self) -> $t { + $t::new(-self.$comp0 $(, -self.$compN )*) + } } ) ) macro_rules! dot_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl Dot for $t - { + impl Dot for $t { #[inline] - fn dot(&self, other: &$t) -> N - { self.$comp0 * other.$comp0 $(+ self.$compN * other.$compN )* } + fn dot(&self, other: &$t) -> N { + self.$comp0 * other.$comp0 $(+ self.$compN * other.$compN )* + } } ) ) macro_rules! sub_dot_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl SubDot for $t - { + impl SubDot for $t { #[inline] - fn sub_dot(&self, a: &$t, b: &$t) -> N - { (self.$comp0 - a.$comp0) * b.$comp0 $(+ (self.$compN - a.$compN) * b.$compN )* } + fn sub_dot(&self, a: &$t, b: &$t) -> N { + (self.$comp0 - a.$comp0) * b.$comp0 $(+ (self.$compN - a.$compN) * b.$compN )* + } } ) ) macro_rules! scalar_mul_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> ScalarMul for $t - { + impl> ScalarMul for $t { #[inline] - fn scalar_mul(&self, s: &N) -> $t - { $t::new(self.$comp0 * *s $(, self.$compN * *s)*) } + fn scalar_mul(&self, s: &N) -> $t { + $t::new(self.$comp0 * *s $(, self.$compN * *s)*) + } #[inline] - fn scalar_mul_inplace(&mut self, s: &N) - { + fn scalar_mul_inplace(&mut self, s: &N) { self.$comp0 = self.$comp0 * *s; $(self.$compN = self.$compN * *s;)* } @@ -298,15 +301,14 @@ macro_rules! scalar_mul_impl( macro_rules! scalar_div_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> ScalarDiv for $t - { + impl> ScalarDiv for $t { #[inline] - fn scalar_div(&self, s: &N) -> $t - { $t::new(self.$comp0 / *s $(, self.$compN / *s)*) } + fn scalar_div(&self, s: &N) -> $t { + $t::new(self.$comp0 / *s $(, self.$compN / *s)*) + } #[inline] - fn scalar_div_inplace(&mut self, s: &N) - { + fn scalar_div_inplace(&mut self, s: &N) { self.$comp0 = self.$comp0 / *s; $(self.$compN = self.$compN / *s;)* } @@ -316,15 +318,14 @@ macro_rules! scalar_div_impl( macro_rules! scalar_add_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> ScalarAdd for $t - { + impl> ScalarAdd for $t { #[inline] - fn scalar_add(&self, s: &N) -> $t - { $t::new(self.$comp0 + *s $(, self.$compN + *s)*) } + fn scalar_add(&self, s: &N) -> $t { + $t::new(self.$comp0 + *s $(, self.$compN + *s)*) + } #[inline] - fn scalar_add_inplace(&mut self, s: &N) - { + fn scalar_add_inplace(&mut self, s: &N) { self.$comp0 = self.$comp0 + *s; $(self.$compN = self.$compN + *s;)* } @@ -334,15 +335,14 @@ macro_rules! scalar_add_impl( macro_rules! scalar_sub_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> ScalarSub for $t - { + impl> ScalarSub for $t { #[inline] - fn scalar_sub(&self, s: &N) -> $t - { $t::new(self.$comp0 - *s $(, self.$compN - *s)*) } + fn scalar_sub(&self, s: &N) -> $t { + $t::new(self.$comp0 - *s $(, self.$compN - *s)*) + } #[inline] - fn scalar_sub_inplace(&mut self, s: &N) - { + fn scalar_sub_inplace(&mut self, s: &N) { self.$comp0 = self.$comp0 - *s; $(self.$compN = self.$compN - *s;)* } @@ -352,49 +352,51 @@ macro_rules! scalar_sub_impl( macro_rules! translation_impl( ($t: ident) => ( - impl + Neg> Translation<$t> for $t - { + impl + Neg> Translation<$t> for $t { #[inline] - fn translation(&self) -> $t - { self.clone() } + fn translation(&self) -> $t { + self.clone() + } #[inline] - fn inv_translation(&self) -> $t - { -self } + fn inv_translation(&self) -> $t { + -self + } #[inline] - fn translate_by(&mut self, t: &$t) - { *self = *self + *t; } + fn translate_by(&mut self, t: &$t) { + *self = *self + *t; + } } ) ) macro_rules! translatable_impl( ($t: ident) => ( - impl + Neg + Clone> Translatable<$t, $t> for $t - { + impl + Neg + Clone> Translatable<$t, $t> for $t { #[inline] - fn translated(&self, t: &$t) -> $t - { self + *t } + fn translated(&self, t: &$t) -> $t { + self + *t + } } ) ) macro_rules! norm_impl( ($t: ident) => ( - impl Norm for $t - { + impl Norm for $t { #[inline] - fn sqnorm(&self) -> N - { self.dot(self) } + fn sqnorm(&self) -> N { + self.dot(self) + } #[inline] - fn norm(&self) -> N - { self.sqnorm().sqrt() } + fn norm(&self) -> N { + self.sqnorm().sqrt() + } #[inline] - fn normalized(&self) -> $t - { + fn normalized(&self) -> $t { let mut res : $t = self.clone(); res.normalize(); @@ -403,8 +405,7 @@ macro_rules! norm_impl( } #[inline] - fn normalize(&mut self) -> N - { + fn normalize(&mut self) -> N { let l = self.norm(); self.scalar_div_inplace(&l); @@ -417,65 +418,66 @@ macro_rules! norm_impl( macro_rules! approx_eq_impl( ($t: ident, $comp0: ident $(,$compN: ident)*) => ( - impl> ApproxEq for $t - { + impl> ApproxEq for $t { #[inline] - fn approx_epsilon() -> N - { ApproxEq::approx_epsilon::() } + fn approx_epsilon() -> N { + ApproxEq::approx_epsilon::() + } #[inline] - fn approx_eq(&self, other: &$t) -> bool - { self.$comp0.approx_eq(&other.$comp0) $(&& self.$compN.approx_eq(&other.$compN))* } + fn approx_eq(&self, other: &$t) -> bool { + self.$comp0.approx_eq(&other.$comp0) $(&& self.$compN.approx_eq(&other.$compN))* + } #[inline] - fn approx_eq_eps(&self, other: &$t, eps: &N) -> bool - { self.$comp0.approx_eq_eps(&other.$comp0, eps) $(&& self.$compN.approx_eq_eps(&other.$compN, eps))* } + fn approx_eq_eps(&self, other: &$t, eps: &N) -> bool { + self.$comp0.approx_eq_eps(&other.$comp0, eps) $(&& self.$compN.approx_eq_eps(&other.$compN, eps))* + } } ) ) macro_rules! one_impl( ($t: ident) => ( - impl One for $t - { + impl One for $t { #[inline] - fn one() -> $t - { $t::new_repeat(One::one()) } + fn one() -> $t { + $t::new_repeat(One::one()) + } } ) ) macro_rules! from_iterator_impl( ($t: ident, $param0: ident $(, $paramN: ident)*) => ( - impl> FromIterator for $t - { - fn from_iterator($param0: &mut Iter) -> $t - { $t::new($param0.next().unwrap() $(, $paramN.next().unwrap())*) } + impl> FromIterator for $t { + fn from_iterator($param0: &mut Iter) -> $t { + $t::new($param0.next().unwrap() $(, $paramN.next().unwrap())*) + } } ) ) macro_rules! bounded_impl( ($t: ident) => ( - impl Bounded for $t - { + impl Bounded for $t { #[inline] - fn max_value() -> $t - { $t::new_repeat(Bounded::max_value()) } + fn max_value() -> $t { + $t::new_repeat(Bounded::max_value()) + } #[inline] - fn min_value() -> $t - { $t::new_repeat(Bounded::min_value()) } + fn min_value() -> $t { + $t::new_repeat(Bounded::min_value()) + } } ) ) macro_rules! to_homogeneous_impl( ($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => ( - impl ToHomogeneous<$t2> for $t - { - fn to_homogeneous(&self) -> $t2 - { + impl ToHomogeneous<$t2> for $t { + fn to_homogeneous(&self) -> $t2 { let mut res: $t2 = One::one(); res.$comp0 = self.$comp0.clone(); @@ -489,10 +491,8 @@ macro_rules! to_homogeneous_impl( macro_rules! from_homogeneous_impl( ($t: ident, $t2: ident, $extra: ident, $comp0: ident $(,$compN: ident)*) => ( - impl + One + Zero> FromHomogeneous<$t2> for $t - { - fn from(v: &$t2) -> $t - { + impl + One + Zero> FromHomogeneous<$t2> for $t { + fn from(v: &$t2) -> $t { let mut res: $t = Zero::zero(); res.$comp0 = v.$comp0.clone(); diff --git a/src/vec_spec.rs b/src/vec_spec.rs index 52772030..f18282a4 100644 --- a/src/vec_spec.rs +++ b/src/vec_spec.rs @@ -6,18 +6,16 @@ use traits::norm::Norm; use traits::sample::UniformSphereSample; use vec::{Vec1, Vec2, Vec3}; -impl + Sub> Cross> for Vec2 -{ +impl + Sub> Cross> for Vec2 { #[inline] - fn cross(&self, other : &Vec2) -> Vec1 - { Vec1::new(self.x * other.y - self.y * other.x) } + fn cross(&self, other : &Vec2) -> Vec1 { + Vec1::new(self.x * other.y - self.y * other.x) + } } -impl + Sub> Cross> for Vec3 -{ +impl + Sub> Cross> for Vec3 { #[inline] - fn cross(&self, other : &Vec3) -> Vec3 - { + fn cross(&self, other : &Vec3) -> Vec3 { Vec3::new( self.y * other.z - self.z * other.y, self.z * other.x - self.x * other.z, @@ -26,50 +24,47 @@ impl + Sub> Cross> for Vec3 } } -impl Basis for Vec1 -{ +impl Basis for Vec1 { #[inline(always)] - fn canonical_basis(f: &fn(Vec1)) - { f(Vec1::new(One::one())) } + fn canonical_basis(f: &fn(Vec1)) { + f(Vec1::new(One::one())) + } #[inline(always)] - fn orthonormal_subspace_basis(&self, _: &fn(Vec1)) - { } + fn orthonormal_subspace_basis(&self, _: &fn(Vec1)) { } } -impl> Basis for Vec2 -{ +impl> Basis for Vec2 { #[inline] - fn canonical_basis(f: &fn(Vec2)) - { + fn canonical_basis(f: &fn(Vec2)) { f(Vec2::new(One::one(), Zero::zero())); f(Vec2::new(Zero::zero(), One::one())); } #[inline] - fn orthonormal_subspace_basis(&self, f: &fn(Vec2)) - { f(Vec2::new(-self.y, self.x.clone())) } + fn orthonormal_subspace_basis(&self, f: &fn(Vec2)) { + f(Vec2::new(-self.y, self.x.clone())) + } } impl -Basis for Vec3 -{ +Basis for Vec3 { #[inline(always)] - fn canonical_basis(f: &fn(Vec3)) - { + fn canonical_basis(f: &fn(Vec3)) { f(Vec3::new(One::one(), Zero::zero(), Zero::zero())); f(Vec3::new(Zero::zero(), One::one(), Zero::zero())); f(Vec3::new(Zero::zero(), Zero::zero(), One::one())); } #[inline(always)] - fn orthonormal_subspace_basis(&self, f: &fn(Vec3)) - { + fn orthonormal_subspace_basis(&self, f: &fn(Vec3)) { let a = - if self.x.clone().abs() > self.y.clone().abs() - { Vec3::new(self.z.clone(), Zero::zero(), -self.x).normalized() } - else - { Vec3::new(Zero::zero(), -self.z, self.y.clone()).normalized() }; + if self.x.clone().abs() > self.y.clone().abs() { + Vec3::new(self.z.clone(), Zero::zero(), -self.x).normalized() + } + else { + Vec3::new(Zero::zero(), -self.z, self.y.clone()).normalized() + }; f(a.cross(self)); f(a); @@ -147,20 +142,18 @@ static SAMPLES_3_F64: [Vec3, ..42] = [ Vec3 { x: 0.162456 , y: 0.499995 , z: 0.850654 } ]; -impl UniformSphereSample for Vec2 -{ - pub fn sample(f: &fn(&'static Vec2)) - { - for sample in SAMPLES_2_F64.iter() - { f(sample) } +impl UniformSphereSample for Vec2 { + pub fn sample(f: &fn(&'static Vec2)) { + for sample in SAMPLES_2_F64.iter() { + f(sample) + } } } -impl UniformSphereSample for Vec3 -{ - pub fn sample(f: &fn(&'static Vec3)) - { - for sample in SAMPLES_3_F64.iter() - { f(sample) } +impl UniformSphereSample for Vec3 { + pub fn sample(f: &fn(&'static Vec3)) { + for sample in SAMPLES_3_F64.iter() { + f(sample) + } } }