Swap the type parameters of `Transform`.

This commit is contained in:
Sébastien Crozet 2013-09-06 15:17:53 +02:00
parent 2ccd62d558
commit 0be1132452
2 changed files with 66 additions and 66 deletions

View File

@ -22,16 +22,16 @@ 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<M, V> {
pub struct Transform<V, M> {
priv submat : M,
priv subtrans : V
}
// FIXME: this should be Trasform<V, M>
impl<M, V> Transform<M, V> {
impl<V, M> Transform<V, M> {
/// Builds a new transform from a matrix and a vector.
#[inline]
pub fn new(mat: M, trans: V) -> Transform<M, V> {
pub fn new(trans: V, mat: M) -> Transform<V, M> {
Transform {
submat: mat,
subtrans: trans
@ -39,7 +39,7 @@ impl<M, V> Transform<M, V> {
}
}
impl<M: Clone, V: Clone> Transform<M, V> {
impl<V: Clone, M: Clone> Transform<V, M> {
/// Gets a copy of the internal matrix.
#[inline]
pub fn submat(&self) -> M {
@ -53,7 +53,7 @@ impl<M: Clone, V: Clone> Transform<M, V> {
}
}
impl<N: Clone + Num + Algebraic> Transform<Rotmat<Mat3<N>>, Vec3<N>> {
impl<N: Clone + Num + Algebraic> Transform<Vec3<N>, Rotmat<Mat3<N>>> {
/// 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.
@ -84,25 +84,25 @@ impl<N: Clone + Num + Algebraic> Transform<Rotmat<Mat3<N>>, Vec3<N>> {
}
}
impl<M: Dim, V> Dim for Transform<M, V> {
impl<M: Dim, V> Dim for Transform<V, M> {
#[inline]
fn dim(_: Option<Transform<M, V>>) -> uint {
fn dim(_: Option<Transform<V, M>>) -> uint {
Dim::dim(None::<M>)
}
}
impl<M: One, V: Zero> One for Transform<M, V> {
impl<M: One, V: Zero> One for Transform<V, M> {
#[inline]
fn one() -> Transform<M, V> {
fn one() -> Transform<V, M> {
Transform {
submat: One::one(), subtrans: Zero::zero()
}
}
}
impl<M: Zero, V: Zero> Zero for Transform<M, V> {
impl<M: Zero, V: Zero> Zero for Transform<V, M> {
#[inline]
fn zero() -> Transform<M, V> {
fn zero() -> Transform<V, M> {
Transform {
submat: Zero::zero(), subtrans: Zero::zero()
}
@ -115,9 +115,9 @@ impl<M: Zero, V: Zero> Zero for Transform<M, V> {
}
impl<M: RMul<V> + Mul<M, M>, V: Add<V, V>>
Mul<Transform<M, V>, Transform<M, V>> for Transform<M, V> {
Mul<Transform<V, M>, Transform<V, M>> for Transform<V, M> {
#[inline]
fn mul(&self, other: &Transform<M, V>) -> Transform<M, V> {
fn mul(&self, other: &Transform<V, M>) -> Transform<V, M> {
Transform {
submat: self.submat * other.submat,
subtrans: self.subtrans + self.submat.rmul(&other.subtrans)
@ -125,21 +125,21 @@ Mul<Transform<M, V>, Transform<M, V>> for Transform<M, V> {
}
}
impl<M: RMul<V>, V: Add<V, V>> RMul<V> for Transform<M, V> {
impl<M: RMul<V>, V: Add<V, V>> RMul<V> for Transform<V, M> {
#[inline]
fn rmul(&self, other: &V) -> V {
self.submat.rmul(other) + self.subtrans
}
}
impl<M: LMul<V>, V: Add<V, V>> LMul<V> for Transform<M, V> {
impl<M: LMul<V>, V: Add<V, V>> LMul<V> for Transform<V, M> {
#[inline]
fn lmul(&self, other: &V) -> V {
self.submat.lmul(other) + self.subtrans
}
}
impl<M: Clone, V: Translation<V>> Translation<V> for Transform<M, V> {
impl<M: Clone, V: Translation<V>> Translation<V> for Transform<V, M> {
#[inline]
fn translation(&self) -> V {
self.subtrans.translation()
@ -156,8 +156,8 @@ impl<M: Clone, V: Translation<V>> Translation<V> for Transform<M, V> {
}
#[inline]
fn translated(&self, t: &V) -> Transform<M, V> {
Transform::new(self.submat.clone(), self.subtrans.translated(t))
fn translated(&self, t: &V) -> Transform<V, M> {
Transform::new(self.subtrans.translated(t), self.submat.clone())
}
#[inline]
@ -166,7 +166,7 @@ impl<M: Clone, V: Translation<V>> Translation<V> for Transform<M, V> {
}
}
impl<M: Translate<V>, V, _0> Translate<V> for Transform<M, _0> {
impl<V, M: Translate<V>, _0> Translate<V> for Transform<_0, M> {
#[inline]
fn translate(&self, v: &V) -> V {
self.submat.translate(v)
@ -179,7 +179,7 @@ impl<M: Translate<V>, V, _0> Translate<V> for Transform<M, _0> {
}
impl<M: Rotation<AV> + RMul<V> + One, V, AV>
Rotation<AV> for Transform<M, V> {
Rotation<AV> for Transform<V, M> {
#[inline]
fn rotation(&self) -> AV {
self.submat.rotation()
@ -201,12 +201,12 @@ Rotation<AV> for Transform<M, V> {
}
#[inline]
fn rotated(&self, rot: &AV) -> Transform<M, V> {
fn rotated(&self, rot: &AV) -> Transform<V, M> {
// FIXME: this does not seem opitmal
let _1: M = One::one();
let delta = _1.rotated(rot);
Transform::new(self.submat.rotated(rot), delta.rmul(&self.subtrans))
Transform::new(delta.rmul(&self.subtrans), self.submat.rotated(rot))
}
#[inline]
@ -216,7 +216,7 @@ Rotation<AV> for Transform<M, V> {
}
}
impl<M: Rotate<V>, V, _0> Rotate<V> for Transform<M, _0> {
impl<V, M: Rotate<V>, _0> Rotate<V> for Transform<_0, M> {
#[inline]
fn rotate(&self, v: &V) -> V {
self.submat.rotate(v)
@ -229,34 +229,34 @@ impl<M: Rotate<V>, V, _0> Rotate<V> for Transform<M, _0> {
}
impl<M: Inv + RMul<V> + Mul<M, M> + Clone, V: Add<V, V> + Neg<V> + Clone>
Transformation<Transform<M, V>> for Transform<M, V> {
fn transformation(&self) -> Transform<M, V> {
Transformation<Transform<V, M>> for Transform<V, M> {
fn transformation(&self) -> Transform<V, M> {
self.clone()
}
fn inv_transformation(&self) -> Transform<M, V> {
// FIXME: fail or return a Some<Transform<M, V>> ?
fn inv_transformation(&self) -> Transform<V, M> {
// FIXME: fail or return a Some<Transform<V, M>> ?
match self.inverse() {
Some(t) => t,
None => fail!("This transformation was not inversible.")
}
}
fn transform_by(&mut self, other: &Transform<M, V>) {
fn transform_by(&mut self, other: &Transform<V, M>) {
*self = other * *self
}
fn transformed(&self, t: &Transform<M, V>) -> Transform<M, V> {
fn transformed(&self, t: &Transform<V, M>) -> Transform<V, M> {
t * *self
}
fn set_transformation(&mut self, t: Transform<M, V>) {
fn set_transformation(&mut self, t: Transform<V, M>) {
*self = t
}
}
impl<M: Ts<V>, V: Add<V, V> + Sub<V, V>>
Ts<V> for Transform<M, V> {
Ts<V> for Transform<V, M> {
#[inline]
fn transform(&self, v: &V) -> V {
self.submat.transform(v) + self.subtrans
@ -269,7 +269,7 @@ Ts<V> for Transform<M, V> {
}
impl<M: Inv + RMul<V> + Clone, V: Neg<V> + Clone>
Inv for Transform<M, V> {
Inv for Transform<V, M> {
#[inline]
fn inplace_inverse(&mut self) -> bool {
if !self.submat.inplace_inverse() {
@ -282,7 +282,7 @@ Inv for Transform<M, V> {
}
#[inline]
fn inverse(&self) -> Option<Transform<M, V>> {
fn inverse(&self) -> Option<Transform<V, M>> {
let mut res = self.clone();
if res.inplace_inverse() {
@ -295,7 +295,7 @@ Inv for Transform<M, V> {
}
impl<M: ToHomogeneous<M2>, M2: Dim + Column<V2>, V: ToHomogeneous<V2> + Clone, V2>
ToHomogeneous<M2> for Transform<M, V> {
ToHomogeneous<M2> for Transform<V, M> {
fn to_homogeneous(&self) -> M2 {
let mut res = self.submat.to_homogeneous();
@ -309,14 +309,14 @@ ToHomogeneous<M2> for Transform<M, V> {
}
impl<M: Column<V> + Dim, M2: FromHomogeneous<M>, V>
FromHomogeneous<M> for Transform<M2, V> {
fn from(m: &M) -> Transform<M2, V> {
Transform::new(FromHomogeneous::from(m), m.column(Dim::dim(None::<M>) - 1))
FromHomogeneous<M> for Transform<V, M2> {
fn from(m: &M) -> Transform<V, M2> {
Transform::new(m.column(Dim::dim(None::<M>) - 1), FromHomogeneous::from(m))
}
}
impl<N: ApproxEq<N>, M:ApproxEq<N>, V:ApproxEq<N>>
ApproxEq<N> for Transform<M, V> {
ApproxEq<N> for Transform<V, M> {
#[inline]
fn approx_epsilon() -> N {
fail!("approx_epsilon is broken since rust revision 8693943676487c01fa09f5f3daf0df6a1f71e24d.")
@ -324,21 +324,21 @@ ApproxEq<N> for Transform<M, V> {
}
#[inline]
fn approx_eq(&self, other: &Transform<M, V>) -> bool {
fn approx_eq(&self, other: &Transform<V, M>) -> bool {
self.submat.approx_eq(&other.submat) &&
self.subtrans.approx_eq(&other.subtrans)
}
#[inline]
fn approx_eq_eps(&self, other: &Transform<M, V>, epsilon: &N) -> bool {
fn approx_eq_eps(&self, other: &Transform<V, M>, epsilon: &N) -> bool {
self.submat.approx_eq_eps(&other.submat, epsilon) &&
self.subtrans.approx_eq_eps(&other.subtrans, epsilon)
}
}
impl<M: Rand, V: Rand> Rand for Transform<M, V> {
impl<M: Rand, V: Rand> Rand for Transform<V, M> {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> Transform<M, V> {
fn rand<R: Rng>(rng: &mut R) -> Transform<V, M> {
Transform::new(rng.gen(), rng.gen())
}
}

View File

@ -33,11 +33,11 @@ pub type Mat1flt = Mat1<float>;
// pub type Iso1flt = Transform<Rot1flt, Vec1flt>;
/// 1-dimensional `f64`-valued general transform.
pub type Aff1f64 = Transform<Mat1f64, Vec1f64>;
pub type Aff1f64 = Transform<Vec1f64, Mat1f64>;
/// 1-dimensional `f32`-valued general transform.
pub type Aff1f32 = Transform<Mat1f32, Vec1f32>;
pub type Aff1f32 = Transform<Vec1f32, Mat1f32>;
/// 1-dimensional `float`-valued general transform.
pub type Aff1flt = Transform<Mat1flt, Vec1flt>;
pub type Aff1flt = Transform<Vec1flt, Mat1flt>;
// 2D
/// 2-dimensional `f64`-valued vector.
@ -62,18 +62,18 @@ pub type Rot2f32 = Rotmat<Mat2<f32>>;
pub type Rot2flt = Rotmat<Mat2<float>>;
/// 2-dimensional `f64`-valued isometric transform.
pub type Iso2f64 = Transform<Rot2f64, Vec2f64>;
pub type Iso2f64 = Transform<Vec2f64, Rot2f64>;
/// 2-dimensional `f32`-valued isometric transform.
pub type Iso2f32 = Transform<Rot2f32, Vec2f32>;
pub type Iso2f32 = Transform<Vec2f32, Rot2f32>;
/// 2-dimensional `float`-valued isometric transform.
pub type Iso2flt = Transform<Rot2flt, Vec2flt>;
pub type Iso2flt = Transform<Vec2flt, Rot2flt>;
/// 2-dimensional `f64`-valued general transform.
pub type Aff2f64 = Transform<Mat2f64, Vec2f64>;
pub type Aff2f64 = Transform<Vec2f64, Mat2f64>;
/// 2-dimensional `f32`-valued general transform.
pub type Aff2f32 = Transform<Mat2f32, Vec2f32>;
pub type Aff2f32 = Transform<Vec2f32, Mat2f32>;
/// 2-dimensional `float`-valued general transform.
pub type Aff2flt = Transform<Mat2flt, Vec2flt>;
pub type Aff2flt = Transform<Vec2flt, Mat2flt>;
// 3D
/// 3-dimensional `f64`-valued vector.
@ -98,18 +98,18 @@ pub type Rot3f32 = Rotmat<Mat3<f32>>;
pub type Rot3flt = Rotmat<Mat3<float>>;
/// 3-dimensional `f64`-valued isometric transform.
pub type Iso3f64 = Transform<Rot3f64, Vec3f64>;
pub type Iso3f64 = Transform<Vec3f64, Rot3f64>;
/// 3-dimensional `f32`-valued isometric transform.
pub type Iso3f32 = Transform<Rot3f32, Vec3f32>;
pub type Iso3f32 = Transform<Vec3f32, Rot3f32>;
/// 3-dimensional `float`-valued isometric transform.
pub type Iso3flt = Transform<Rot3flt, Vec3flt>;
pub type Iso3flt = Transform<Vec3flt, Rot3flt>;
/// 3-dimensional `f64`-valued general transform.
pub type Aff3f64 = Transform<Mat3f64, Vec3f64>;
pub type Aff3f64 = Transform<Vec3f64, Mat3f64>;
/// 3-dimensional `f32`-valued general transform.
pub type Aff3f32 = Transform<Mat3f32, Vec3f32>;
pub type Aff3f32 = Transform<Vec3f32, Mat3f32>;
/// 3-dimensional `float`-valued general transform.
pub type Aff3flt = Transform<Mat3flt, Vec3flt>;
pub type Aff3flt = Transform<Vec3flt, Mat3flt>;
// 4D
/// 4-dimensional `f64`-valued vector.
@ -141,11 +141,11 @@ pub type Mat4flt = Mat4<float>;
// pub type Iso4flt = Transform<Rot4flt, Vec4flt>;
/// 4-dimensional `f64`-valued general transform.
pub type Aff4f64 = Transform<Mat4f64, Vec4f64>;
pub type Aff4f64 = Transform<Vec4f64, Mat4f64>;
/// 4-dimensional `f32`-valued general transform.
pub type Aff4f32 = Transform<Mat4f32, Vec4f32>;
pub type Aff4f32 = Transform<Vec4f32, Mat4f32>;
/// 4-dimensional `float`-valued general transform.
pub type Aff4flt = Transform<Mat4flt, Vec4flt>;
pub type Aff4flt = Transform<Vec4flt, Mat4flt>;
// 5D
/// 5-dimensional `f64`-valued vector.
@ -177,11 +177,11 @@ pub type Mat5flt = Mat5<float>;
// pub type Iso5flt = Transform<Rot5flt, Vec5flt>;
/// 5-dimensional `f64`-valued general transform.
pub type Aff5f64 = Transform<Mat5f64, Vec5f64>;
pub type Aff5f64 = Transform<Vec5f64, Mat5f64>;
/// 5-dimensional `f32`-valued general transform.
pub type Aff5f32 = Transform<Mat5f32, Vec5f32>;
pub type Aff5f32 = Transform<Vec5f32, Mat5f32>;
/// 5-dimensional `float`-valued general transform.
pub type Aff5flt = Transform<Mat5flt, Vec5flt>;
pub type Aff5flt = Transform<Vec5flt, Mat5flt>;
// 6D
/// 6-dimensional `f64`-valued vector.
@ -213,8 +213,8 @@ pub type Mat6flt = Mat6<float>;
// pub type Iso6flt = Transform<Rot6flt, Vec6flt>;
/// 6-dimensional `f64`-valued general transform.
pub type Aff6f64 = Transform<Mat6f64, Vec6f64>;
pub type Aff6f64 = Transform<Vec6f64, Mat6f64>;
/// 6-dimensional `f32`-valued general transform.
pub type Aff6f32 = Transform<Mat6f32, Vec6f32>;
pub type Aff6f32 = Transform<Vec6f32, Mat6f32>;
/// 6-dimensional `float`-valued general transform.
pub type Aff6flt = Transform<Mat6flt, Vec6flt>;
pub type Aff6flt = Transform<Vec6flt, Mat6flt>;