Begin taking advantage of trait inheritance

This commit is contained in:
Brendan Zabarauskas 2012-11-13 11:44:44 +10:00
parent f2e3181c16
commit 7492677366
3 changed files with 29 additions and 22 deletions

View file

@ -41,7 +41,7 @@ pub type dmat3x3 = Mat3<f64>; /// same as a `dmat3`
pub type dmat4x4 = Mat4<f64>; /// same as a `dmat4`
pub trait Matrix<T, ColVec, RowVec> {
pub trait Matrix<T, ColVec, RowVec>: Eq, Index<uint, T>, ToPtr<T> {
pure fn rows() -> uint;
pure fn cols() -> uint;
pure fn is_col_major() -> bool;
@ -51,12 +51,12 @@ pub trait Matrix<T, ColVec, RowVec> {
pure fn row(i: uint) -> RowVec;
}
pub trait NumericMatrix<T, ColVec> {
pub trait NumericMatrix<T, ColVec, RowVec>: Matrix<T, ColVec, RowVec> {
pure fn mul_t(value: T) -> self;
pure fn mul_v(other: &ColVec) -> ColVec;
}
pub trait NumericMatrix_NxN<T> {
pub trait NumericMatrix_NxN<T, Vec>: NumericMatrix<T, Vec, Vec> {
pure fn add_m(other: &self) -> self;
pure fn sub_m(other: &self) -> self;
pure fn mul_m(other: &self) -> self;
@ -73,16 +73,16 @@ pub trait NumericMatrix_NxN<T> {
pure fn is_invertible() -> bool;
}
pub trait Matrix2<T> {
pub trait Matrix2<T>: Matrix<T, Mat2<T>, Mat2<T>> {
pure fn to_Mat3() -> Mat3<T>;
pure fn to_Mat4() -> Mat4<T>;
}
pub trait Matrix3<T> {
pub trait Matrix3<T>: Matrix<T, Mat3<T>, Mat3<T>> {
pure fn to_Mat4() -> Mat4<T>;
}
pub trait Matrix4<T> {
pub trait Matrix4<T>: Matrix<T, Mat4<T>, Mat4<T>> {
}
@ -157,7 +157,7 @@ pub impl<T:Copy> Mat2<T>: Matrix<T, Vec2<T>, Vec2<T>> {
}
}
pub impl<T:Copy Num NumCast> Mat2<T>: NumericMatrix<T, Vec2<T>> {
pub impl<T:Copy Num NumCast> Mat2<T>: NumericMatrix<T, Vec2<T>, Vec2<T>> {
#[inline(always)]
pure fn mul_t(value: T) -> Mat2<T> {
Mat2::from_cols(self[0].mul_t(value),
@ -171,7 +171,7 @@ pub impl<T:Copy Num NumCast> Mat2<T>: NumericMatrix<T, Vec2<T>> {
}
}
pub impl<T:Copy Num NumCast FuzzyEq> Mat2<T>: NumericMatrix_NxN<T> {
pub impl<T:Copy Num NumCast FuzzyEq> Mat2<T>: NumericMatrix_NxN<T, Vec2<T>> {
#[inline(always)]
pure fn add_m(other: &Mat2<T>) -> Mat2<T> {
Mat2::from_cols(self[0].add_v(&other[0]),
@ -300,6 +300,13 @@ pub impl<T:Copy FuzzyEq> Mat2<T>: FuzzyEq {
}
}
pub impl<T:Copy> Mat2<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr() -> *T {
self[0].to_ptr()
}
}
@ -387,7 +394,7 @@ pub impl<T:Copy> Mat3<T>: Matrix<T, Vec3<T>, Vec3<T>> {
}
}
pub impl<T:Copy Num NumCast> Mat3<T>: NumericMatrix<T, Vec3<T>> {
pub impl<T:Copy Num NumCast> Mat3<T>: NumericMatrix<T, Vec3<T>, Vec3<T>> {
#[inline(always)]
pure fn mul_t(value: T) -> Mat3<T> {
Mat3::from_cols(self[0].mul_t(value),
@ -403,7 +410,7 @@ pub impl<T:Copy Num NumCast> Mat3<T>: NumericMatrix<T, Vec3<T>> {
}
}
pub impl<T:Copy Num NumCast FuzzyEq> Mat3<T>: NumericMatrix_NxN<T> {
pub impl<T:Copy Num NumCast FuzzyEq> Mat3<T>: NumericMatrix_NxN<T, Vec3<T>> {
#[inline(always)]
pure fn add_m(other: &Mat3<T>) -> Mat3<T> {
Mat3::from_cols(self[0].add_v(&other[0]),
@ -703,7 +710,7 @@ pub impl<T:Copy> Mat4<T>: Matrix<T, Vec4<T>, Vec4<T>> {
}
pub impl<T:Copy Num NumCast FuzzyEq> Mat4<T>: NumericMatrix<T, Vec4<T>> {
pub impl<T:Copy Num NumCast FuzzyEq> Mat4<T>: NumericMatrix<T, Vec4<T>, Vec4<T>> {
#[inline(always)]
pure fn mul_t(value: T) -> Mat4<T> {
Mat4::from_cols(self[0].mul_t(value),
@ -721,7 +728,7 @@ pub impl<T:Copy Num NumCast FuzzyEq> Mat4<T>: NumericMatrix<T, Vec4<T>> {
}
}
pub impl<T:Copy Num NumCast FuzzyEq Signed Ord> Mat4<T>: NumericMatrix_NxN<T> {
pub impl<T:Copy Num NumCast FuzzyEq Signed Ord> Mat4<T>: NumericMatrix_NxN<T, Vec4<T>> {
#[inline(always)]
pure fn add_m(other: &Mat4<T>) -> Mat4<T> {
Mat4::from_cols(self[0].add_v(&other[0]),

View file

@ -23,7 +23,7 @@ pub type dquat4 = Quat<f64>; /// a double-precision floating-point quate
//
// Quaternion
//
pub trait Quaternion<T> {
pub trait Quaternion<T>: Eq, Index<uint, T>, ToPtr<T> {
pure fn dim() -> uint;
pure fn mul_t(value: T) -> self;

View file

@ -31,11 +31,11 @@ pub type uvec3 = Vec3<u32>; /// a three-component unsigned integer vecto
pub type uvec4 = Vec4<u32>; /// a four-component unsigned integer vector
pub trait Vector {
pub trait Vector<T>: Eq, Index<uint, T>, ToPtr<T> {
static pure fn dim() -> uint;
}
pub trait NumericVector<T> {
pub trait NumericVector<T>: Vector<T> {
pure fn mul_t(value: T) -> self;
pure fn div_t(value: T) -> self;
@ -45,26 +45,26 @@ pub trait NumericVector<T> {
pure fn dot(other: &self) -> T;
}
pub trait GeometricVector<T> {
pub trait GeometricVector<T>: NumericVector<T> {
pure fn length2() -> T;
pure fn length() -> T;
pure fn normalize() -> self;
pure fn lerp(other: &self, amount: T) -> self;
}
pub trait Vector2<T> {
pub trait Vector2<T>: Vector<T> {
// static pure fn new(x: T, y: T) -> self;
// static pure fn from_value(value: T) -> self;
}
pub trait Vector3<T> {
pub trait Vector3<T>: Vector<T> {
// static pure fn new(x: T, y: T, z: T) -> self;
// static pure fn from_value(value: T) -> self;
pure fn cross(other: &self) -> self;
}
pub trait Vector4<T> {
pub trait Vector4<T>: Vector<T> {
// pub static pure fn new(x: T, y: T, z: T, w: T) -> self;
// pub static pure fn from_value(value: T) -> self;
}
@ -117,7 +117,7 @@ pub mod Vec2 {
}
}
pub impl<T> Vec2<T>: Vector {
pub impl<T> Vec2<T>: Vector<T> {
#[inline(always)]
static pure fn dim() -> uint { 2 }
}
@ -297,7 +297,7 @@ pub impl<T:Copy Num> Vec3<T>: Vector3<T> {
}
}
pub impl<T> Vec3<T>: Vector {
pub impl<T> Vec3<T>: Vector<T> {
#[inline(always)]
static pure fn dim() -> uint { 3 }
}
@ -483,7 +483,7 @@ pub mod Vec4 {
}
}
pub impl<T> Vec4<T>: Vector {
pub impl<T> Vec4<T>: Vector<T> {
#[inline(always)]
static pure fn dim() -> uint { 4 }
}