updated syntax and added type bounds, scope ownership issues remain

This commit is contained in:
Brian Heylin 2013-03-28 10:45:43 +01:00
parent 7f0de1b42f
commit 8b94369a38
10 changed files with 538 additions and 539 deletions

View file

@ -18,122 +18,122 @@ pub use mat4::{Mat4, mat4, dmat4};
* floating point type and have the same number of dimensions as the
* number of rows and columns in the matrix.
*/
pub trait Matrix<T,V>: Index<uint, V> Eq Neg<Self> {
pub trait Matrix<T,V>: Index<uint, V> + Eq + Neg<Self> {
/**
* # Return value
*
* The column vector at `i`
*/
pure fn col(&self, i: uint) -> V;
/**
* # Return value
*
* The row vector at `i`
*/
pure fn row(&self, i: uint) -> V;
/**
* Construct a diagonal matrix with the major diagonal set to `value`
*/
static pure fn from_value(value: T) -> Self;
/**
* # Return value
*
* The identity matrix
*/
static pure fn identity() -> Self;
/**
* # Return value
*
* A matrix with all elements set to zero
*/
static pure fn zero() -> Self;
/**
* # Return value
*
* The scalar multiplication of this matrix and `value`
*/
pure fn mul_t(&self, value: T) -> Self;
/**
* # Return value
*
* The matrix vector product of the matrix and `vec`
*/
pure fn mul_v(&self, vec: &V) -> V;
/**
* # Return value
*
* The matrix addition of the matrix and `other`
*/
pure fn add_m(&self, other: &Self) -> Self;
/**
* # Return value
*
* The difference between the matrix and `other`
*/
pure fn sub_m(&self, other: &Self) -> Self;
/**
* # Return value
*
* The matrix product of the matrix and `other`
*/
pure fn mul_m(&self, other: &Self) -> Self;
/**
* # Return value
*
* The matrix dot product of the matrix and `other`
*/
pure fn dot(&self, other: &Self) -> T;
/**
* # Return value
*
* The determinant of the matrix
*/
pure fn determinant(&self) -> T;
/**
* # Return value
*
* The sum of the main diagonal of the matrix
*/
pure fn trace(&self) -> T;
/**
* Returns the inverse of the matrix
*
*
* # Return value
*
* * `Some(m)` - if the inversion was successful, where `m` is the inverted matrix
* * `None` - if the inversion was unsuccessful (because the matrix was not invertable)
*/
pure fn inverse(&self) -> Option<Self>;
/**
* # Return value
*
* The transposed matrix
*/
pure fn transpose(&self) -> Self;
/**
* Check to see if the matrix is an identity matrix
*
* # Return value
*
*
* `true` if the matrix is approximately equal to the identity matrix
*/
pure fn is_identity(&self) -> bool;
/**
* Check to see if the matrix is diagonal
*
@ -143,7 +143,7 @@ pub trait Matrix<T,V>: Index<uint, V> Eq Neg<Self> {
* equal to zero.
*/
pure fn is_diagonal(&self) -> bool;
/**
* Check to see if the matrix is rotated
*
@ -152,7 +152,7 @@ pub trait Matrix<T,V>: Index<uint, V> Eq Neg<Self> {
* `true` if the matrix is not approximately equal to the identity matrix.
*/
pure fn is_rotated(&self) -> bool;
/**
* Check to see if the matrix is symmetric
*
@ -161,7 +161,7 @@ pub trait Matrix<T,V>: Index<uint, V> Eq Neg<Self> {
* `true` if the matrix is approximately equal to its transpose).
*/
pure fn is_symmetric(&self) -> bool;
/**
* Check to see if the matrix is invertable
*
@ -170,7 +170,7 @@ pub trait Matrix<T,V>: Index<uint, V> Eq Neg<Self> {
* `true` if the matrix is invertable
*/
pure fn is_invertible(&self) -> bool;
/**
* # Return value
*
@ -185,13 +185,13 @@ pub trait Matrix<T,V>: Index<uint, V> Eq Neg<Self> {
pub trait Matrix2<T,V>: Matrix<T,V> {
static pure fn new(c0r0: T, c0r1: T,
c1r0: T, c1r1: T) -> Self;
static pure fn from_cols(c0: V, c1: V) -> Self;
static pure fn from_angle(radians: T) -> Self;
pure fn to_mat3(&self) -> Mat3<T>;
pure fn to_mat4(&self) -> Mat4<T>;
}
@ -202,25 +202,25 @@ pub trait Matrix3<T,V>: Matrix<T,V> {
static pure fn new(c0r0:T, c0r1:T, c0r2:T,
c1r0:T, c1r1:T, c1r2:T,
c2r0:T, c2r1:T, c2r2:T) -> Self;
static pure fn from_cols(c0: V, c1: V, c2: V) -> Self;
static pure fn from_angle_x(radians: T) -> Self;
static pure fn from_angle_y(radians: T) -> Self;
static pure fn from_angle_z(radians: T) -> Self;
static pure fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Self;
static pure fn from_angle_axis(radians: T, axis: &Vec3<T>) -> Self;
static pure fn from_axes(x: V, y: V, z: V) -> Self;
static pure fn look_at(dir: &Vec3<T>, up: &Vec3<T>) -> Self;
pure fn to_mat4(&self) -> Mat4<T>;
pure fn to_quat(&self) -> Quat<T>;
}
@ -232,7 +232,7 @@ pub trait Matrix4<T,V>: Matrix<T,V> {
c1r0: T, c1r1: T, c1r2: T, c1r3: T,
c2r0: T, c2r1: T, c2r2: T, c2r3: T,
c3r0: T, c3r1: T, c3r2: T, c3r3: T) -> Self;
static pure fn from_cols(c0: V, c1: V, c2: V, c3: V) -> Self;
}
@ -246,27 +246,27 @@ pub trait MutableMatrix<T,V>: Matrix<T,V> {
* A mutable reference to the column at `i`
*/
fn col_mut(&mut self, i: uint) -> &self/mut V;
/**
* Swap two columns of the matrix in place
*/
fn swap_cols(&mut self, a: uint, b: uint);
/**
* Swap two rows of the matrix in place
* Swap two rows of the matrix in place
*/
fn swap_rows(&mut self, a: uint, b: uint);
/**
* Sets the matrix to `other`
* Sets the matrix to `other`
*/
fn set(&mut self, other: &Self);
/**
* Sets the matrix to the identity matrix
*/
fn to_identity(&mut self);
/**
* Sets each element of the matrix to zero
*/
@ -276,27 +276,27 @@ pub trait MutableMatrix<T,V>: Matrix<T,V> {
* Multiplies the matrix by a scalar
*/
fn mul_self_t(&mut self, value: T);
/**
* Add the matrix `other` to `self`
*/
fn add_self_m(&mut self, other: &Self);
/**
* Subtract the matrix `other` from `self`
*/
fn sub_self_m(&mut self, other: &Self);
/**
* Sets the matrix to its inverse
*
*
* # Failure
*
* Fails if the matrix is not invertable. Make sure you check with the
* `is_invertible` method before you attempt this!
*/
fn invert_self(&mut self);
/**
* Sets the matrix to its transpose
*/

View file

@ -46,16 +46,16 @@ use mat::{
#[deriving_eq]
pub struct Mat2<T> { x: Vec2<T>, y: Vec2<T> }
pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec2<T>> for Mat2<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Matrix<T, Vec2<T>> for Mat2<T> {
#[inline(always)]
pure fn col(&self, i: uint) -> Vec2<T> { self[i] }
#[inline(always)]
pure fn row(&self, i: uint) -> Vec2<T> {
Vector2::new(self[0][i],
self[1][i])
}
/**
* Construct a 2 x 2 diagonal matrix with the major diagonal set to `value`
*
@ -77,12 +77,12 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec2<T>> for Mat2<T> {
Matrix2::new(value, zero(),
zero(), value)
}
/**
* Returns the multiplicative identity matrix
* ~~~
* c0 c1
* +----+----+
* +----+----+
* r0 | 1 | 0 |
* +----+----+
* r1 | 0 | 1 |
@ -94,12 +94,12 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec2<T>> for Mat2<T> {
Matrix2::new( one::<T>(), zero::<T>(),
zero::<T>(), one::<T>())
}
/**
* Returns the additive identity matrix
* ~~~
* c0 c1
* +----+----+
* +----+----+
* r0 | 0 | 0 |
* +----+----+
* r1 | 0 | 0 |
@ -111,31 +111,31 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec2<T>> for Mat2<T> {
Matrix2::new(zero::<T>(), zero::<T>(),
zero::<T>(), zero::<T>())
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Mat2<T> {
Matrix2::from_cols(self[0].mul_t(value),
self[1].mul_t(value))
}
#[inline(always)]
pure fn mul_v(&self, vec: &Vec2<T>) -> Vec2<T> {
Vector2::new(self.row(0).dot(vec),
self.row(1).dot(vec))
}
#[inline(always)]
pure fn add_m(&self, other: &Mat2<T>) -> Mat2<T> {
Matrix2::from_cols(self[0].add_v(&other[0]),
self[1].add_v(&other[1]))
}
#[inline(always)]
pure fn sub_m(&self, other: &Mat2<T>) -> Mat2<T> {
Matrix2::from_cols(self[0].sub_v(&other[0]),
self[1].sub_v(&other[1]))
}
#[inline(always)]
pure fn mul_m(&self, other: &Mat2<T>) -> Mat2<T> {
Matrix2::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)),
@ -145,7 +145,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec2<T>> for Mat2<T> {
pure fn dot(&self, other: &Mat2<T>) -> T {
other.transpose().mul_m(self).trace()
}
pure fn determinant(&self) -> T {
self[0][0] * self[1][1] - self[1][0] * self[0][1]
}
@ -164,29 +164,29 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec2<T>> for Mat2<T> {
-self[1][0]/d, self[0][0]/d))
}
}
#[inline(always)]
pure fn transpose(&self) -> Mat2<T> {
Matrix2::new(self[0][0], self[1][0],
self[0][1], self[1][1])
}
#[inline(always)]
pure fn is_identity(&self) -> bool {
self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
pure fn is_diagonal(&self) -> bool {
self[0][1].fuzzy_eq(&zero()) &&
self[1][0].fuzzy_eq(&zero())
}
#[inline(always)]
pure fn is_rotated(&self) -> bool {
!self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
pure fn is_symmetric(&self) -> bool {
self[0][1].fuzzy_eq(&self[1][0]) &&
@ -197,7 +197,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec2<T>> for Mat2<T> {
pure fn is_invertible(&self) -> bool {
!self.determinant().fuzzy_eq(&zero())
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -208,7 +208,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec2<T>> for Mat2<T> {
}
}
pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec2<T>> for Mat2<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableMatrix<T, Vec2<T> > for Mat2<T> {
#[inline(always)]
fn col_mut(&mut self, i: uint) -> &self/mut Vec2<T> {
match i {
@ -217,52 +217,51 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec2<T>> for Mat2<T> {
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i))
}
}
#[inline(always)]
fn swap_cols(&mut self, a: uint, b: uint) {
swap(self.col_mut(a),
self.col_mut(b));
swap(&mut self.x, &mut self.y);
}
#[inline(always)]
fn swap_rows(&mut self, a: uint, b: uint) {
self.x.swap(a, b);
self.y.swap(a, b);
}
#[inline(always)]
fn set(&mut self, other: &Mat2<T>) {
(*self) = (*other);
}
#[inline(always)]
fn to_identity(&mut self) {
(*self) = Matrix::identity();
}
#[inline(always)]
fn to_zero(&mut self) {
(*self) = Matrix::zero();
}
#[inline(always)]
fn mul_self_t(&mut self, value: T) {
self.col_mut(0).mul_self_t(&value);
self.col_mut(1).mul_self_t(&value);
&mut self.x.mul_self_t(&value);
&mut self.y.mul_self_t(&value);
}
#[inline(always)]
fn add_self_m(&mut self, other: &Mat2<T>) {
self.col_mut(0).add_self_v(&other[0]);
self.col_mut(1).add_self_v(&other[1]);
&mut self.x.add_self_v(&other[0]);
&mut self.y.add_self_v(&other[1]);
}
#[inline(always)]
fn sub_self_m(&mut self, other: &Mat2<T>) {
self.col_mut(0).sub_self_v(&other[0]);
self.col_mut(1).sub_self_v(&other[1]);
&mut self.x.sub_self_v(&other[0]);
&mut self.y.sub_self_v(&other[1]);
}
#[inline(always)]
fn invert_self(&mut self) {
match self.inverse() {
@ -270,15 +269,15 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec2<T>> for Mat2<T> {
None => fail!(~"Couldn't invert the matrix!")
}
}
#[inline(always)]
fn transpose_self(&mut self) {
swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0));
swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1));
&mut swap(&mut self.x.index_mut(1), &mut self.y.index_mut(0));
&mut swap(&mut self.y.index_mut(0), &mut self.x.index_mut(1));
}
}
pub impl<T:Copy Float FuzzyEq<T>> Matrix2<T, Vec2<T>> for Mat2<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Matrix2<T, Vec2<T>> for Mat2<T> {
/**
* Construct a 2 x 2 matrix
*
@ -302,7 +301,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix2<T, Vec2<T>> for Mat2<T> {
Matrix2::from_cols(Vector2::new::<T,Vec2<T>>(c0r0, c0r1),
Vector2::new::<T,Vec2<T>>(c1r0, c1r1))
}
/**
* Construct a 2 x 2 matrix from column vectors
*
@ -325,16 +324,16 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix2<T, Vec2<T>> for Mat2<T> {
c1: Vec2<T>) -> Mat2<T> {
Mat2 { x: c0, y: c1 }
}
#[inline(always)]
static pure fn from_angle(radians: T) -> Mat2<T> {
let cos_theta = cos(radians);
let sin_theta = sin(radians);
Matrix2::new(cos_theta, -sin_theta,
sin_theta, cos_theta)
}
/**
* Returns the the matrix with an extra row and column added
* ~~~
@ -354,7 +353,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix2<T, Vec2<T>> for Mat2<T> {
self[1][0], self[1][1], zero(),
zero(), zero(), one())
}
/**
* Returns the the matrix with an extra two rows and columns added
* ~~~
@ -379,7 +378,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix2<T, Vec2<T>> for Mat2<T> {
}
}
pub impl<T:Copy> Index<uint, Vec2<T>> for Mat2<T> {
impl<T:Copy> Index<uint, Vec2<T>> for Mat2<T> {
#[inline(always)]
pure fn index(&self, i: uint) -> Vec2<T> {
unsafe { do buf_as_slice(
@ -389,19 +388,19 @@ pub impl<T:Copy> Index<uint, Vec2<T>> for Mat2<T> {
}
}
pub impl<T:Copy Float FuzzyEq<T>> Neg<Mat2<T>> for Mat2<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Mat2<T>> for Mat2<T> {
#[inline(always)]
pure fn neg(&self) -> Mat2<T> {
Matrix2::from_cols(-self[0], -self[1])
}
}
pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Mat2<T> {
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Mat2<T> {
#[inline(always)]
pure fn fuzzy_eq(&self, other: &Mat2<T>) -> bool {
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
}
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Mat2<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
@ -417,36 +416,36 @@ pub type dmat2 = Mat2<f64>; // a 2×2 double-precision floating-point matrix
// Static method wrappers for GLSL-style types
pub impl mat2 {
impl mat2 {
#[inline(always)] static pure fn new(c0r0: f32, c0r1: f32, c1r0: f32, c1r1: f32)
-> mat2 { Matrix2::new(c0r0, c0r1, c1r0, c1r1) }
#[inline(always)] static pure fn from_cols(c0: vec2, c1: vec2)
-> mat2 { Matrix2::from_cols(c0, c1) }
#[inline(always)] static pure fn from_value(v: f32) -> mat2 { Matrix::from_value(v) }
#[inline(always)] static pure fn identity() -> mat2 { Matrix::identity() }
#[inline(always)] static pure fn zero() -> mat2 { Matrix::zero() }
#[inline(always)] static pure fn from_angle(radians: f32) -> mat2 { Matrix2::from_angle(radians) }
#[inline(always)] static pure fn dim() -> uint { 2 }
#[inline(always)] static pure fn rows() -> uint { 2 }
#[inline(always)] static pure fn cols() -> uint { 2 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<mat2>() }
}
pub impl dmat2 {
impl dmat2 {
#[inline(always)] static pure fn new(c0r0: f64, c0r1: f64, c1r0: f64, c1r1: f64)
-> dmat2 { Matrix2::new(c0r0, c0r1, c1r0, c1r1) }
#[inline(always)] static pure fn from_cols(c0: dvec2, c1: dvec2)
-> dmat2 { Matrix2::from_cols(c0, c1) }
#[inline(always)] static pure fn from_value(v: f64) -> dmat2 { Matrix::from_value(v) }
#[inline(always)] static pure fn identity() -> dmat2 { Matrix::identity() }
#[inline(always)] static pure fn zero() -> dmat2 { Matrix::zero() }
#[inline(always)] static pure fn from_angle(radians: f64) -> dmat2 { Matrix2::from_angle(radians) }
#[inline(always)] static pure fn dim() -> uint { 2 }
#[inline(always)] static pure fn rows() -> uint { 2 }
#[inline(always)] static pure fn cols() -> uint { 2 }

View file

@ -48,17 +48,17 @@ use mat::{
#[deriving_eq]
pub struct Mat3<T> { x: Vec3<T>, y: Vec3<T>, z: Vec3<T> }
pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec3<T>> for Mat3<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Matrix<T, Vec3<T>> for Mat3<T> {
#[inline(always)]
pure fn col(&self, i: uint) -> Vec3<T> { self[i] }
#[inline(always)]
pure fn row(&self, i: uint) -> Vec3<T> {
Vector3::new(self[0][i],
self[1][i],
self[2][i])
}
/**
* Construct a 3 x 3 diagonal matrix with the major diagonal set to `value`
*
@ -83,7 +83,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec3<T>> for Mat3<T> {
zero(), value, zero(),
zero(), zero(), value)
}
/**
* Returns the multiplicative identity matrix
* ~~~
@ -103,7 +103,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec3<T>> for Mat3<T> {
zero::<T>(), one::<T>(), zero::<T>(),
zero::<T>(), zero::<T>(), one::<T>())
}
/**
* Returns the additive identity matrix
* ~~~
@ -123,50 +123,50 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec3<T>> for Mat3<T> {
zero::<T>(), zero::<T>(), zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Mat3<T> {
Matrix3::from_cols(self[0].mul_t(value),
self[1].mul_t(value),
self[2].mul_t(value))
}
#[inline(always)]
pure fn mul_v(&self, vec: &Vec3<T>) -> Vec3<T> {
Vector3::new(self.row(0).dot(vec),
self.row(1).dot(vec),
self.row(2).dot(vec))
}
#[inline(always)]
pure fn add_m(&self, other: &Mat3<T>) -> Mat3<T> {
Matrix3::from_cols(self[0].add_v(&other[0]),
self[1].add_v(&other[1]),
self[2].add_v(&other[2]))
}
#[inline(always)]
pure fn sub_m(&self, other: &Mat3<T>) -> Mat3<T> {
Matrix3::from_cols(self[0].sub_v(&other[0]),
self[1].sub_v(&other[1]),
self[2].sub_v(&other[2]))
}
#[inline(always)]
pure fn mul_m(&self, other: &Mat3<T>) -> Mat3<T> {
Matrix3::new(self.row(0).dot(&other.col(0)),
self.row(1).dot(&other.col(0)),
self.row(2).dot(&other.col(0)),
self.row(0).dot(&other.col(1)),
self.row(1).dot(&other.col(1)),
self.row(2).dot(&other.col(1)),
self.row(0).dot(&other.col(2)),
self.row(1).dot(&other.col(2)),
self.row(2).dot(&other.col(2)))
}
pure fn dot(&self, other: &Mat3<T>) -> T {
other.transpose().mul_m(self).trace()
}
@ -191,44 +191,44 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec3<T>> for Mat3<T> {
Some(m.transpose())
}
}
#[inline(always)]
pure fn transpose(&self) -> Mat3<T> {
Matrix3::new(self[0][0], self[1][0], self[2][0],
self[0][1], self[1][1], self[2][1],
self[0][2], self[1][2], self[2][2])
}
#[inline(always)]
pure fn is_identity(&self) -> bool {
self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
pure fn is_diagonal(&self) -> bool {
self[0][1].fuzzy_eq(&zero()) &&
self[0][2].fuzzy_eq(&zero()) &&
self[1][0].fuzzy_eq(&zero()) &&
self[1][2].fuzzy_eq(&zero()) &&
self[2][0].fuzzy_eq(&zero()) &&
self[2][1].fuzzy_eq(&zero())
}
#[inline(always)]
pure fn is_rotated(&self) -> bool {
!self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
pure fn is_symmetric(&self) -> bool {
self[0][1].fuzzy_eq(&self[1][0]) &&
self[0][2].fuzzy_eq(&self[2][0]) &&
self[1][0].fuzzy_eq(&self[0][1]) &&
self[1][2].fuzzy_eq(&self[2][1]) &&
self[2][0].fuzzy_eq(&self[0][2]) &&
self[2][1].fuzzy_eq(&self[1][2])
}
@ -237,7 +237,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec3<T>> for Mat3<T> {
pure fn is_invertible(&self) -> bool {
!self.determinant().fuzzy_eq(&zero())
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -248,7 +248,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec3<T>> for Mat3<T> {
}
}
pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
/**
* Construct a 3 x 3 matrix
*
@ -277,7 +277,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
Vector3::new::<T,Vec3<T>>(c1r0, c1r1, c1r2),
Vector3::new::<T,Vec3<T>>(c2r0, c2r1, c2r2))
}
/**
* Construct a 3 x 3 matrix from column vectors
*
@ -304,7 +304,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
c2: Vec3<T>) -> Mat3<T> {
Mat3 { x: c0, y: c1, z: c2 }
}
/**
* Construct a matrix from an angular rotation around the `x` axis
*/
@ -313,12 +313,12 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
let cos_theta = cos(radians);
let sin_theta = sin(radians);
Matrix3::new( one(), zero(), zero(),
zero(), cos_theta, sin_theta,
zero(), -sin_theta, cos_theta)
}
/**
* Construct a matrix from an angular rotation around the `y` axis
*/
@ -327,12 +327,12 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
let cos_theta = cos(radians);
let sin_theta = sin(radians);
Matrix3::new(cos_theta, zero(), -sin_theta,
zero(), one(), zero(),
sin_theta, zero(), cos_theta)
}
/**
* Construct a matrix from an angular rotation around the `z` axis
*/
@ -341,12 +341,12 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
let cos_theta = cos(radians);
let sin_theta = sin(radians);
Matrix3::new( cos_theta, sin_theta, zero(),
-sin_theta, cos_theta, zero(),
zero(), zero(), one())
}
/**
* Construct a matrix from Euler angles
*
@ -365,12 +365,12 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
let sy = sin(radians_y);
let cz = cos(radians_z);
let sz = sin(radians_z);
Matrix3::new( cy*cz, cy*sz, -sy,
-cx*sz + sx*sy*cz, cx*cz + sx*sy*sz, sx*cy,
sx*sz + cx*sy*cz, -sx*cz + cx*sy*sz, cx*cy)
}
/**
* Construct a matrix from an axis and an angular rotation
*/
@ -379,30 +379,30 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
let c = cos(radians);
let s = sin(radians);
let _1_c = one::<T>() - c;
let x = axis.x;
let y = axis.y;
let z = axis.z;
Matrix3::new(_1_c*x*x + c, _1_c*x*y + s*z, _1_c*x*z - s*y,
_1_c*x*y - s*z, _1_c*y*y + c, _1_c*y*z + s*x,
_1_c*x*z + s*y, _1_c*y*z - s*x, _1_c*z*z + c)
}
#[inline(always)]
static pure fn from_axes(x: Vec3<T>, y: Vec3<T>, z: Vec3<T>) -> Mat3<T> {
Matrix3::from_cols(x, y, z)
}
#[inline(always)]
static pure fn look_at(dir: &Vec3<T>, up: &Vec3<T>) -> Mat3<T> {
let dir_ = dir.normalize();
let side = dir_.cross(&up.normalize());
let up_ = side.cross(&dir_).normalize();
Matrix3::from_axes(up_, side, dir_)
}
/**
* Returns the the matrix with an extra row and column added
* ~~~
@ -425,7 +425,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
self[2][0], self[2][1], self[2][2], zero(),
zero(), zero(), zero(), one())
}
/**
* Convert the matrix to a quaternion
*/
@ -433,14 +433,14 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
pure fn to_quat(&self) -> Quat<T> {
// Implemented using a mix of ideas from jMonkeyEngine and Ken Shoemake's
// paper on Quaternions: http://www.cs.ucr.edu/~vbz/resources/Quatut.pdf
let mut s;
let w, x, y, z;
let trace = self.trace();
let _1: T = Number::from(1.0);
let half: T = Number::from(0.5);
if trace >= zero() {
s = (_1 + trace).sqrt();
w = half * s;
@ -470,12 +470,12 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix3<T, Vec3<T>> for Mat3<T> {
y = (self[1][2] - self[2][1]) * s;
z = (self[0][1] - self[1][0]) * s;
}
Quat::new(w, x, y, z)
}
}
pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec3<T>> for Mat3<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableMatrix<T, Vec3<T>> for Mat3<T> {
#[inline(always)]
fn col_mut(&mut self, i: uint) -> &self/mut Vec3<T> {
match i {
@ -485,56 +485,56 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec3<T>> for Mat3<T> {
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i))
}
}
#[inline(always)]
fn swap_cols(&mut self, a: uint, b: uint) {
swap(self.col_mut(a),
self.col_mut(b));
}
#[inline(always)]
fn swap_rows(&mut self, a: uint, b: uint) {
self.x.swap(a, b);
self.y.swap(a, b);
self.z.swap(a, b);
}
#[inline(always)]
fn set(&mut self, other: &Mat3<T>) {
(*self) = (*other);
}
#[inline(always)]
fn to_identity(&mut self) {
(*self) = Matrix::identity();
}
#[inline(always)]
fn to_zero(&mut self) {
(*self) = Matrix::zero();
}
#[inline(always)]
fn mul_self_t(&mut self, value: T) {
self.col_mut(0).mul_self_t(&value);
self.col_mut(1).mul_self_t(&value);
self.col_mut(2).mul_self_t(&value);
}
#[inline(always)]
fn add_self_m(&mut self, other: &Mat3<T>) {
self.col_mut(0).add_self_v(&other[0]);
self.col_mut(1).add_self_v(&other[1]);
self.col_mut(2).add_self_v(&other[2]);
}
#[inline(always)]
fn sub_self_m(&mut self, other: &Mat3<T>) {
self.col_mut(0).sub_self_v(&other[0]);
self.col_mut(1).sub_self_v(&other[1]);
self.col_mut(2).sub_self_v(&other[2]);
}
#[inline(always)]
fn invert_self(&mut self) {
match self.inverse() {
@ -542,21 +542,21 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec3<T>> for Mat3<T> {
None => fail!(~"Couldn't invert the matrix!")
}
}
#[inline(always)]
fn transpose_self(&mut self) {
swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0));
swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0));
swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1));
swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1));
swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2));
swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2));
}
}
pub impl<T:Copy> Index<uint, Vec3<T>> for Mat3<T> {
impl<T:Copy> Index<uint, Vec3<T>> for Mat3<T> {
#[inline(always)]
pure fn index(&self, i: uint) -> Vec3<T> {
unsafe { do buf_as_slice(
@ -566,19 +566,19 @@ pub impl<T:Copy> Index<uint, Vec3<T>> for Mat3<T> {
}
}
pub impl<T:Copy Float FuzzyEq<T>> Neg<Mat3<T>> for Mat3<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Mat3<T>> for Mat3<T> {
#[inline(always)]
pure fn neg(&self) -> Mat3<T> {
Matrix3::from_cols(-self[0], -self[1], -self[2])
}
}
pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Mat3<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> FuzzyEq<T> for Mat3<T> {
#[inline(always)]
pure fn fuzzy_eq(&self, other: &Mat3<T>) -> bool {
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
}
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Mat3<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
@ -595,16 +595,16 @@ pub type dmat3 = Mat3<f64>; // a 3×3 double-precision floating-point matrix
// Static method wrappers for GLSL-style types
pub impl mat3 {
impl mat3 {
#[inline(always)] static pure fn new(c0r0: f32, c0r1: f32, c0r2: f32, c1r0: f32, c1r1: f32, c1r2: f32, c2r0: f32, c2r1: f32, c2r2: f32)
-> mat3 { Matrix3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) }
#[inline(always)] static pure fn from_cols(c0: vec3, c1: vec3, c2: vec3)
-> mat3 { Matrix3::from_cols(c0, c1, c2) }
#[inline(always)] static pure fn from_value(v: f32) -> mat3 { Matrix::from_value(v) }
#[inline(always)] static pure fn identity() -> mat3 { Matrix::identity() }
#[inline(always)] static pure fn zero() -> mat3 { Matrix::zero() }
#[inline(always)] static pure fn from_angle_x(radians: f32) -> mat3 { Matrix3::from_angle_x(radians) }
#[inline(always)] static pure fn from_angle_y(radians: f32) -> mat3 { Matrix3::from_angle_y(radians) }
#[inline(always)] static pure fn from_angle_z(radians: f32) -> mat3 { Matrix3::from_angle_z(radians) }
@ -612,7 +612,7 @@ pub impl mat3 {
#[inline(always)] static pure fn from_angle_axis(radians: f32, axis: &vec3) -> mat3 { Matrix3::from_angle_axis(radians, axis) }
#[inline(always)] static pure fn from_axes(x: vec3, y: vec3, z: vec3) -> mat3 { Matrix3::from_axes(x, y, z) }
#[inline(always)] static pure fn look_at(dir: &vec3, up: &vec3) -> mat3 { Matrix3::look_at(dir, up) }
#[inline(always)] static pure fn dim() -> uint { 3 }
#[inline(always)] static pure fn rows() -> uint { 3 }
#[inline(always)] static pure fn cols() -> uint { 3 }
@ -620,16 +620,16 @@ pub impl mat3 {
}
pub impl dmat3 {
impl dmat3 {
#[inline(always)] static pure fn new(c0r0: f64, c0r1: f64, c0r2: f64, c1r0: f64, c1r1: f64, c1r2: f64, c2r0: f64, c2r1: f64, c2r2: f64)
-> dmat3 { Matrix3::new(c0r0, c0r1, c0r2, c1r0, c1r1, c1r2, c2r0, c2r1, c2r2) }
#[inline(always)] static pure fn from_cols(c0: dvec3, c1: dvec3, c2: dvec3)
-> dmat3 { Matrix3::from_cols(c0, c1, c2) }
#[inline(always)] static pure fn from_value(v: f64) -> dmat3 { Matrix::from_value(v) }
#[inline(always)] static pure fn identity() -> dmat3 { Matrix::identity() }
#[inline(always)] static pure fn zero() -> dmat3 { Matrix::zero() }
#[inline(always)] static pure fn dim() -> uint { 3 }
#[inline(always)] static pure fn rows() -> uint { 3 }
#[inline(always)] static pure fn cols() -> uint { 3 }

View file

@ -45,10 +45,10 @@ use mat::{
#[deriving_eq]
pub struct Mat4<T> { x: Vec4<T>, y: Vec4<T>, z: Vec4<T>, w: Vec4<T> }
pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Matrix<T, Vec4<T>> for Mat4<T> {
#[inline(always)]
pure fn col(&self, i: uint) -> Vec4<T> { self[i] }
#[inline(always)]
pure fn row(&self, i: uint) -> Vec4<T> {
Vector4::new(self[0][i],
@ -56,7 +56,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
self[2][i],
self[3][i])
}
/**
* Construct a 4 x 4 diagonal matrix with the major diagonal set to `value`
*
@ -84,7 +84,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
zero(), zero(), value, zero(),
zero(), zero(), zero(), value)
}
/**
* Returns the multiplicative identity matrix
* ~~~
@ -107,7 +107,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
zero::<T>(), zero::<T>(), one::<T>(), zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>(), one::<T>())
}
/**
* Returns the additive identity matrix
* ~~~
@ -130,7 +130,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>(),
zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Mat4<T> {
Matrix4::from_cols(self[0].mul_t(value),
@ -138,7 +138,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
self[2].mul_t(value),
self[3].mul_t(value))
}
#[inline(always)]
pure fn mul_v(&self, vec: &Vec4<T>) -> Vec4<T> {
Vector4::new(self.row(0).dot(vec),
@ -146,7 +146,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
self.row(2).dot(vec),
self.row(3).dot(vec))
}
#[inline(always)]
pure fn add_m(&self, other: &Mat4<T>) -> Mat4<T> {
Matrix4::from_cols(self[0].add_v(&other[0]),
@ -154,7 +154,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
self[2].add_v(&other[2]),
self[3].add_v(&other[3]))
}
#[inline(always)]
pure fn sub_m(&self, other: &Mat4<T>) -> Mat4<T> {
Matrix4::from_cols(self[0].sub_v(&other[0]),
@ -162,31 +162,31 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
self[2].sub_v(&other[2]),
self[3].sub_v(&other[3]))
}
#[inline(always)]
pure fn mul_m(&self, other: &Mat4<T>) -> Mat4<T> {
Matrix4::new(self.row(0).dot(&other.col(0)),
self.row(1).dot(&other.col(0)),
self.row(2).dot(&other.col(0)),
self.row(3).dot(&other.col(0)),
self.row(0).dot(&other.col(1)),
self.row(1).dot(&other.col(1)),
self.row(2).dot(&other.col(1)),
self.row(3).dot(&other.col(1)),
self.row(0).dot(&other.col(2)),
self.row(1).dot(&other.col(2)),
self.row(2).dot(&other.col(2)),
self.row(3).dot(&other.col(2)),
self.row(0).dot(&other.col(3)),
self.row(1).dot(&other.col(3)),
self.row(2).dot(&other.col(3)),
self.row(3).dot(&other.col(3)))
}
pure fn dot(&self, other: &Mat4<T>) -> T {
other.transpose().mul_m(self).trace()
}
@ -204,7 +204,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
let m3: Mat3<T> = Matrix3::new(self[0][1], self[1][1], self[2][1],
self[0][2], self[1][2], self[2][2],
self[0][3], self[1][3], self[2][3]);
self[0][0] * m0.determinant() -
self[1][0] * m1.determinant() +
self[2][0] * m2.determinant() -
@ -220,14 +220,14 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
if d.fuzzy_eq(&zero()) {
None
} else {
// Gauss Jordan Elimination with partial pivoting
// So take this matrix, A, augmented with the identity
// and essentially reduce [A|I]
let mut A = *self;
let mut I: Mat4<T> = Matrix::identity();
for uint::range(0, 4) |j| {
// Find largest element in col j
let mut i1 = j;
@ -236,17 +236,17 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
i1 = i;
}
}
unsafe {
// Swap columns i1 and j in A and I to
// put pivot on diagonal
A.swap_cols(i1, j);
I.swap_cols(i1, j);
// Scale col j to have a unit diagonal
I.col_mut(j).div_self_t(&A[j][j]);
A.col_mut(j).div_self_t(&A[j][j]);
// Eliminate off-diagonal elems in col j of A,
// doing identical ops to I
for uint::range(0, 4) |i| {
@ -260,7 +260,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
Some(I)
}
}
#[inline(always)]
pure fn transpose(&self) -> Mat4<T> {
Matrix4::new(self[0][0], self[1][0], self[2][0], self[3][0],
@ -268,50 +268,50 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
self[0][2], self[1][2], self[2][2], self[3][2],
self[0][3], self[1][3], self[2][3], self[3][3])
}
#[inline(always)]
pure fn is_identity(&self) -> bool {
self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
pure fn is_diagonal(&self) -> bool {
self[0][1].fuzzy_eq(&zero()) &&
self[0][2].fuzzy_eq(&zero()) &&
self[0][3].fuzzy_eq(&zero()) &&
self[1][0].fuzzy_eq(&zero()) &&
self[1][2].fuzzy_eq(&zero()) &&
self[1][3].fuzzy_eq(&zero()) &&
self[2][0].fuzzy_eq(&zero()) &&
self[2][1].fuzzy_eq(&zero()) &&
self[2][3].fuzzy_eq(&zero()) &&
self[3][0].fuzzy_eq(&zero()) &&
self[3][1].fuzzy_eq(&zero()) &&
self[3][2].fuzzy_eq(&zero())
}
#[inline(always)]
pure fn is_rotated(&self) -> bool {
!self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
pure fn is_symmetric(&self) -> bool {
self[0][1].fuzzy_eq(&self[1][0]) &&
self[0][2].fuzzy_eq(&self[2][0]) &&
self[0][3].fuzzy_eq(&self[3][0]) &&
self[1][0].fuzzy_eq(&self[0][1]) &&
self[1][2].fuzzy_eq(&self[2][1]) &&
self[1][3].fuzzy_eq(&self[3][1]) &&
self[2][0].fuzzy_eq(&self[0][2]) &&
self[2][1].fuzzy_eq(&self[1][2]) &&
self[2][3].fuzzy_eq(&self[3][2]) &&
self[3][0].fuzzy_eq(&self[0][3]) &&
self[3][1].fuzzy_eq(&self[1][3]) &&
self[3][2].fuzzy_eq(&self[2][3])
@ -321,7 +321,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
pure fn is_invertible(&self) -> bool {
!self.determinant().fuzzy_eq(&zero())
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -332,7 +332,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix<T, Vec4<T>> for Mat4<T> {
}
}
pub impl<T:Copy Float FuzzyEq<T>> Matrix4<T, Vec4<T>> for Mat4<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Matrix4<T, Vec4<T>> for Mat4<T> {
/**
* Construct a 4 x 4 matrix
*
@ -366,7 +366,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix4<T, Vec4<T>> for Mat4<T> {
Vector4::new::<T,Vec4<T>>(c2r0, c2r1, c2r2, c2r3),
Vector4::new::<T,Vec4<T>>(c3r0, c3r1, c3r2, c3r3))
}
/**
* Construct a 4 x 4 matrix from column vectors
*
@ -399,7 +399,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Matrix4<T, Vec4<T>> for Mat4<T> {
}
}
pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec4<T>> for Mat4<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableMatrix<T, Vec4<T>> for Mat4<T> {
#[inline(always)]
fn col_mut(&mut self, i: uint) -> &self/mut Vec4<T> {
match i {
@ -410,13 +410,13 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec4<T>> for Mat4<T> {
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i))
}
}
#[inline(always)]
fn swap_cols(&mut self, a: uint, b: uint) {
swap(self.col_mut(a),
self.col_mut(b));
}
#[inline(always)]
fn swap_rows(&mut self, a: uint, b: uint) {
self.x.swap(a, b);
@ -424,22 +424,22 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec4<T>> for Mat4<T> {
self.z.swap(a, b);
self.w.swap(a, b);
}
#[inline(always)]
fn set(&mut self, other: &Mat4<T>) {
(*self) = (*other);
}
#[inline(always)]
fn to_identity(&mut self) {
(*self) = Matrix::identity();
}
#[inline(always)]
fn to_zero(&mut self) {
(*self) = Matrix::zero();
}
#[inline(always)]
fn mul_self_t(&mut self, value: T) {
self.col_mut(0).mul_self_t(&value);
@ -447,7 +447,7 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec4<T>> for Mat4<T> {
self.col_mut(2).mul_self_t(&value);
self.col_mut(3).mul_self_t(&value);
}
#[inline(always)]
fn add_self_m(&mut self, other: &Mat4<T>) {
self.col_mut(0).add_self_v(&other[0]);
@ -455,7 +455,7 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec4<T>> for Mat4<T> {
self.col_mut(2).add_self_v(&other[2]);
self.col_mut(3).add_self_v(&other[3]);
}
#[inline(always)]
fn sub_self_m(&mut self, other: &Mat4<T>) {
self.col_mut(0).sub_self_v(&other[0]);
@ -463,7 +463,7 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec4<T>> for Mat4<T> {
self.col_mut(2).sub_self_v(&other[2]);
self.col_mut(3).sub_self_v(&other[3]);
}
#[inline(always)]
fn invert_self(&mut self) {
match self.inverse() {
@ -471,35 +471,35 @@ pub impl<T:Copy Float FuzzyEq<T>> MutableMatrix<T, Vec4<T>> for Mat4<T> {
None => fail!(~"Couldn't invert the matrix!")
}
}
#[inline(always)]
fn transpose_self(&mut self) {
swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0));
swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0));
swap(self.col_mut(0).index_mut(3), self.col_mut(3).index_mut(0));
swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1));
swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1));
swap(self.col_mut(1).index_mut(3), self.col_mut(3).index_mut(1));
swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2));
swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2));
swap(self.col_mut(2).index_mut(3), self.col_mut(3).index_mut(2));
swap(self.col_mut(3).index_mut(0), self.col_mut(0).index_mut(3));
swap(self.col_mut(3).index_mut(1), self.col_mut(1).index_mut(3));
swap(self.col_mut(3).index_mut(2), self.col_mut(2).index_mut(3));
}
}
pub impl<T:Copy Float FuzzyEq<T>> Neg<Mat4<T>> for Mat4<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Mat4<T>> for Mat4<T> {
#[inline(always)]
pure fn neg(&self) -> Mat4<T> {
Matrix4::from_cols(-self[0], -self[1], -self[2], -self[3])
}
}
pub impl<T:Copy> Index<uint, Vec4<T>> for Mat4<T> {
impl<T:Copy> Index<uint, Vec4<T>> for Mat4<T> {
#[inline(always)]
pure fn index(&self, i: uint) -> Vec4<T> {
unsafe { do buf_as_slice(
@ -509,12 +509,12 @@ pub impl<T:Copy> Index<uint, Vec4<T>> for Mat4<T> {
}
}
pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Mat4<T> {
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Mat4<T> {
#[inline(always)]
pure fn fuzzy_eq(&self, other: &Mat4<T>) -> bool {
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
}
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Mat4<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
@ -532,32 +532,32 @@ pub type dmat4 = Mat4<f64>; // a 4×4 double-precision floating-point matrix
// Static method wrappers for GLSL-style types
pub impl mat4 {
impl mat4 {
#[inline(always)] static pure fn new(c0r0: f32, c0r1: f32, c0r2: f32, c0r3: f32, c1r0: f32, c1r1: f32, c1r2: f32, c1r3: f32, c2r0: f32, c2r1: f32, c2r2: f32, c2r3: f32, c3r0: f32, c3r1: f32, c3r2: f32, c3r3: f32)
-> mat4 { Matrix4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3) }
#[inline(always)] static pure fn from_cols(c0: vec4, c1: vec4, c2: vec4, c3: vec4)
-> mat4 { Matrix4::from_cols(c0, c1, c2, c3) }
#[inline(always)] static pure fn from_value(v: f32) -> mat4 { Matrix::from_value(v) }
#[inline(always)] static pure fn identity() -> mat4 { Matrix::identity() }
#[inline(always)] static pure fn zero() -> mat4 { Matrix::zero() }
#[inline(always)] static pure fn dim() -> uint { 4 }
#[inline(always)] static pure fn rows() -> uint { 4 }
#[inline(always)] static pure fn cols() -> uint { 4 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<mat4>() }
}
pub impl dmat4 {
impl dmat4 {
#[inline(always)] static pure fn new(c0r0: f64, c0r1: f64, c0r2: f64, c0r3: f64, c1r0: f64, c1r1: f64, c1r2: f64, c1r3: f64, c2r0: f64, c2r1: f64, c2r2: f64, c2r3: f64, c3r0: f64, c3r1: f64, c3r2: f64, c3r3: f64)
-> dmat4 { Matrix4::new(c0r0, c0r1, c0r2, c0r3, c1r0, c1r1, c1r2, c1r3, c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3) }
#[inline(always)] static pure fn from_cols(c0: dvec4, c1: dvec4, c2: dvec4, c3: dvec4)
-> dmat4 { Matrix4::from_cols(c0, c1, c2, c3) }
#[inline(always)] static pure fn from_value(v: f64) -> dmat4 { Matrix::from_value(v) }
#[inline(always)] static pure fn identity() -> dmat4 { Matrix::identity() }
#[inline(always)] static pure fn zero() -> dmat4 { Matrix::zero() }
#[inline(always)] static pure fn dim() -> uint { 4 }
#[inline(always)] static pure fn rows() -> uint { 4 }
#[inline(always)] static pure fn cols() -> uint { 4 }

View file

@ -12,10 +12,10 @@ use mat::{Mat4, Matrix4};
* can be found [here](http://www.opengl.org/wiki/GluPerspective_code).
*/
#[inline(always)]
pub pure fn perspective<T:Copy Float FuzzyEq<T>>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> {
pub pure fn perspective<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>(fovy: T, aspectRatio: T, near: T, far: T) -> Mat4<T> {
let ymax = near * tan(radians(fovy));
let xmax = ymax * aspectRatio;
frustum(-xmax, xmax, -ymax, ymax, near, far)
}
@ -26,31 +26,31 @@ pub pure fn perspective<T:Copy Float FuzzyEq<T>>(fovy: T, aspectRatio: T, near:
* (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function.
*/
#[inline(always)]
pub pure fn frustum<T:Copy Float FuzzyEq<T>>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> {
pub pure fn frustum<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>(left: T, right: T, bottom: T, top: T, near: T, far: T) -> Mat4<T> {
let _0: T = Number::from(0);
let _1: T = Number::from(1);
let _2: T = Number::from(2);
let c0r0 = (_2 * near) / (right - left);
let c0r1 = _0;
let c0r2 = _0;
let c0r3 = _0;
let c1r0 = _0;
let c1r1 = (_2 * near) / (top - bottom);
let c1r2 = _0;
let c1r3 = _0;
let c2r0 = (right + left) / (right - left);
let c2r1 = (top + bottom) / (top - bottom);
let c2r2 = -(far + near) / (far - near);
let c2r3 = -_1;
let c3r0 = _0;
let c3r1 = _0;
let c3r2 = -(_2 * far * near) / (far - near);
let c3r3 = _0;
Matrix4::new(c0r0, c0r1, c0r2, c0r3,
c1r0, c1r1, c1r2, c1r3,
c2r0, c2r1, c2r2, c2r3,

View file

@ -48,7 +48,7 @@ use vec::{
#[deriving_eq]
pub struct Quat<T> { s: T, v: Vec3<T> }
pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Quat<T> {
/**
* Construct the quaternion from one scalar component and three
* imaginary components
@ -64,7 +64,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
static pure fn new(w: T, xi: T, yj: T, zk: T) -> Quat<T> {
Quat::from_sv(w, Vector3::new(xi, yj, zk))
}
/**
* Construct the quaternion from a scalar and a vector
*
@ -77,7 +77,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
static pure fn from_sv(s: T, v: Vec3<T>) -> Quat<T> {
Quat { s: s, v: v }
}
/**
* # Return value
*
@ -87,7 +87,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
static pure fn identity() -> Quat<T> {
Quat::new(one(), zero(), zero(), zero())
}
/**
* # Return value
*
@ -97,25 +97,25 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
static pure fn zero() -> Quat<T> {
Quat::new(zero(), zero(), zero(), zero())
}
#[inline(always)]
static pure fn from_angle_x(radians: T) -> Quat<T> {
let _2 = Number::from(2);
Quat::new(cos(radians / _2), sin(radians), zero(), zero())
}
#[inline(always)]
static pure fn from_angle_y(radians: T) -> Quat<T> {
let _2 = Number::from(2);
Quat::new(cos(radians / _2), zero(), sin(radians), zero())
}
#[inline(always)]
static pure fn from_angle_z(radians: T) -> Quat<T> {
let _2 = Number::from(2);
Quat::new(cos(radians / _2), zero(), zero(), sin(radians))
}
#[inline(always)]
static pure fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Quat<T> {
// http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion
@ -128,27 +128,27 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
cos(zdiv2) * sin(xdiv2) * cos(ydiv2) + sin(zdiv2) * cos(xdiv2) * sin(ydiv2),
cos(zdiv2) * cos(xdiv2) * sin(ydiv2) - sin(zdiv2) * sin(xdiv2) * cos(ydiv2))
}
#[inline(always)]
static pure fn from_angle_axis(radians: T, axis: &Vec3<T>) -> Quat<T> {
let half = radians / Number::from(2);
Quat::from_sv(cos(half), axis.mul_t(sin(half)))
}
#[inline(always)]
static pure fn from_axes(x: Vec3<T>, y: Vec3<T>, z: Vec3<T>) -> Quat<T> {
let m: Mat3<T> = Matrix3::from_axes(x, y, z); m.to_quat()
}
pure fn get_angle_axis(&self) -> (T, Vec3<T>) {
fail!(~"Not yet implemented.")
}
#[inline(always)]
static pure fn look_at(dir: &Vec3<T>, up: &Vec3<T>) -> Quat<T> {
let m: Mat3<T> = Matrix3::look_at(dir, up); m.to_quat()
}
/**
* # Return value
*
@ -161,7 +161,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
self[2] * value,
self[3] * value)
}
/**
* # Return value
*
@ -174,7 +174,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
self[2] / value,
self[3] / value)
}
/**
* # Return value
*
@ -185,11 +185,11 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
let tmp = self.v.cross(vec).add_v(&vec.mul_t(self.s));
self.v.cross(&tmp).mul_t(Number::from(2)).add_v(vec)
}
/**
* # Return value
*
* The sum of this quaternion and `other`
* The sum of this quaternion and `other`
*/
#[inline(always)]
pure fn add_q(&self, other: &Quat<T>) -> Quat<T> {
@ -198,11 +198,11 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
self[2] + other[2],
self[3] + other[3])
}
/**
* # Return value
*
* The sum of this quaternion and `other`
* The sum of this quaternion and `other`
*/
#[inline(always)]
pure fn sub_q(&self, other: &Quat<T>) -> Quat<T> {
@ -211,7 +211,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
self[2] - other[2],
self[3] - other[3])
}
/**
* # Return value
*
@ -220,11 +220,11 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
#[inline(always)]
pure fn mul_q(&self, other: &Quat<T>) -> Quat<T> {
Quat::new(self.s * other.s - self.v.x * other.v.x - self.v.y * other.v.y - self.v.z * other.v.z,
self.s * other.v.x + self.v.x * other.s + self.v.y * other.v.z - self.v.z * other.v.y,
self.s * other.v.y + self.v.y * other.s + self.v.z * other.v.x - self.v.x * other.v.z,
self.s * other.v.z + self.v.z * other.s + self.v.x * other.v.y - self.v.y * other.v.x)
self.s * other.v.x + self.v.x * other.s + self.v.y * other.v.z - self.v.z * other.v.y,
self.s * other.v.y + self.v.y * other.s + self.v.z * other.v.x - self.v.x * other.v.z,
self.s * other.v.z + self.v.z * other.s + self.v.x * other.v.y - self.v.y * other.v.x)
}
/**
* # Return value
*
@ -234,7 +234,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
pure fn dot(&self, other: &Quat<T>) -> T {
self.s * other.s + self.v.dot(&other.v)
}
/**
* # Return value
*
@ -244,7 +244,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
pure fn conjugate(&self) -> Quat<T> {
Quat::from_sv(self.s, -self.v)
}
/**
* # Return value
*
@ -254,7 +254,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
pure fn inverse(&self) -> Quat<T> {
self.conjugate().div_t(self.magnitude2())
}
/**
* # Return value
*
@ -266,7 +266,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
pure fn magnitude2(&self) -> T {
self.s * self.s + self.v.length2()
}
/**
* # Return value
*
@ -282,7 +282,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
pure fn magnitude(&self) -> T {
self.magnitude2().sqrt()
}
/**
* # Return value
*
@ -292,7 +292,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
pure fn normalize(&self) -> Quat<T> {
self.mul_t(one::<T>()/self.magnitude())
}
/**
* Normalised linear interpolation
*
@ -304,7 +304,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
pure fn nlerp(&self, other: &Quat<T>, amount: T) -> Quat<T> {
self.mul_t(one::<T>() - amount).add_q(&other.mul_t(amount)).normalize()
}
/**
* Spherical Linear Intoperlation
*
@ -329,24 +329,24 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
#[inline(always)]
pure fn slerp(&self, other: &Quat<T>, amount: T) -> Quat<T> {
let dot = self.dot(other);
let dot_threshold = Number::from(0.9995);
if dot > dot_threshold {
return self.nlerp(other, amount); // if quaternions are close together use `nlerp`
} else {
let robust_dot = dot.clamp(-one::<T>(), one()); // stay within the domain of acos()
let theta_0 = acos(robust_dot); // the angle between the quaternions
let theta = theta_0 * amount; // the fraction of theta specified by `amount`
let q = other.sub_q(&self.mul_t(robust_dot))
.normalize();
return self.mul_t(cos(theta)).add_q(&q.mul_t(sin(theta)));
}
}
/**
* # Return value
*
@ -360,7 +360,7 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
)
}
}
/**
* Convert the quaternion to a 3 x 3 rotation matrix
*/
@ -369,28 +369,28 @@ pub impl<T:Copy Float FuzzyEq<T>> Quat<T> {
let x2 = self.v.x + self.v.x;
let y2 = self.v.y + self.v.y;
let z2 = self.v.z + self.v.z;
let xx2 = x2 * self.v.x;
let xy2 = x2 * self.v.y;
let xz2 = x2 * self.v.z;
let yy2 = y2 * self.v.y;
let yz2 = y2 * self.v.z;
let zz2 = z2 * self.v.z;
let sy2 = y2 * self.s;
let sz2 = z2 * self.s;
let sx2 = x2 * self.s;
let _1: T = one();
Matrix3::new(_1 - yy2 - zz2, xy2 + sz2, xz2 - sy2,
xy2 - sz2, _1 - xx2 - zz2, yz2 + sx2,
xz2 + sy2, yz2 - sx2, _1 - xx2 - yy2)
}
}
pub impl<T:Copy> Index<uint, T> for Quat<T> {
impl<T:Copy> Index<uint, T> for Quat<T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(
@ -400,19 +400,19 @@ pub impl<T:Copy> Index<uint, T> for Quat<T> {
}
}
pub impl<T:Copy Float FuzzyEq<T>> Neg<Quat<T>> for Quat<T> {
impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Quat<T>> for Quat<T> {
#[inline(always)]
pure fn neg(&self) -> Quat<T> {
Quat::new(-self[0], -self[1], -self[2], -self[3])
}
}
pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Quat<T> {
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Quat<T> {
#[inline(always)]
pure fn fuzzy_eq(&self, other: &Quat<T>) -> bool {
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
}
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Quat<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
@ -430,12 +430,12 @@ pub type dquat = Quat<f64>; /// a double-precision floating-point qu
// Static method wrappers for GLSL-style types
pub impl quat {
impl quat {
#[inline(always)] static pure fn new(w: f32, xi: f32, yj: f32, zk: f32) -> quat { Quat::new(w, xi, yj, zk) }
#[inline(always)] static pure fn from_sv(s: f32, v: vec3) -> quat { Quat::from_sv(s, v) }
#[inline(always)] static pure fn identity() -> quat { Quat::identity() }
#[inline(always)] static pure fn zero() -> quat { Quat::zero() }
#[inline(always)] static pure fn from_angle_x(radians: f32) -> quat { Quat::from_angle_x(radians) }
#[inline(always)] static pure fn from_angle_y(radians: f32) -> quat { Quat::from_angle_y(radians) }
#[inline(always)] static pure fn from_angle_z(radians: f32) -> quat { Quat::from_angle_z(radians) }
@ -446,12 +446,12 @@ pub impl quat {
#[inline(always)] static pure fn look_at(dir: &vec3, up: &vec3) -> quat { Quat::look_at(dir, up) }
}
pub impl dquat {
impl dquat {
#[inline(always)] static pure fn new(w: f64, xi: f64, yj: f64, zk: f64) -> dquat { Quat::new(w, xi, yj, zk) }
#[inline(always)] static pure fn from_sv(s: f64, v: dvec3) -> dquat { Quat::from_sv(s, v) }
#[inline(always)] static pure fn identity() -> dquat { Quat::identity() }
#[inline(always)] static pure fn zero() -> dquat { Quat::zero() }
#[inline(always)] static pure fn from_angle_x(radians: f64) -> dquat { Quat::from_angle_x(radians) }
#[inline(always)] static pure fn from_angle_y(radians: f64) -> dquat { Quat::from_angle_y(radians) }
#[inline(always)] static pure fn from_angle_z(radians: f64) -> dquat { Quat::from_angle_z(radians) }

View file

@ -17,12 +17,12 @@ pub use vec4::{Vec4, vec4, dvec4, bvec4, ivec4, uvec4};
* * `T` - The type of the components. This is intended to support boolean,
* integer, unsigned integer, and floating point types.
*/
pub trait Vector<T>: Index<uint, T> Eq {
pub trait Vector<T>: Index<uint, T> + Eq {
/**
* Construct the vector from a single value, copying it to each component
*/
static pure fn from_value(value: T) -> Self;
/**
* # Return value
*
@ -36,7 +36,7 @@ pub trait MutableVector<T>: Vector<T> {
* Get a mutable reference to the component at `i`
*/
fn index_mut(&mut self, i: uint) -> &self/mut T;
/**
* Swap two components of the vector in place
*/
@ -67,7 +67,7 @@ pub trait Vector4<T>: Vector<T> {
/**
* A vector with numeric components
*/
pub trait NumericVector<T>: Vector<T> Neg<Self> {
pub trait NumericVector<T>: Vector<T> + Neg<Self> {
/**
* The standard basis vector
*
@ -76,7 +76,7 @@ pub trait NumericVector<T>: Vector<T> Neg<Self> {
* A vector with each component set to one
*/
static pure fn identity() -> Self;
/**
* The null vector
*
@ -85,48 +85,48 @@ pub trait NumericVector<T>: Vector<T> Neg<Self> {
* A vector with each component set to zero
*/
static pure fn zero() -> Self;
/**
* # Return value
*
* True if the vector is equal to zero
*/
pure fn is_zero(&self) -> bool;
/**
* # Return value
*
* The scalar multiplication of the vector and `value`
*/
pure fn mul_t(&self, value: T) -> Self;
/**
* # Return value
*
* The scalar division of the vector and `value`
*/
pure fn div_t(&self, value: T) -> Self;
/**
* Component-wise vector addition
*/
pure fn add_v(&self, other: &Self) -> Self;
/**
* Component-wise vector subtraction
*/
pure fn sub_v(&self, other: &Self) -> Self;
/**
* Component-wise vector multiplication
*/
pure fn mul_v(&self, other: &Self) -> Self;
/**
* Component-wise vector division
*/
pure fn div_v(&self, other: &Self) -> Self;
/**
* # Return value
*
@ -141,7 +141,7 @@ pub trait NumericVector<T>: Vector<T> Neg<Self> {
pub trait NumericVector2<T>: NumericVector<T> {
static pure fn unit_x() -> Self;
static pure fn unit_y() -> Self;
/**
* # Return value
*
@ -157,7 +157,7 @@ pub trait NumericVector3<T>: NumericVector<T> {
static pure fn unit_x() -> Self;
static pure fn unit_y() -> Self;
static pure fn unit_z() -> Self;
/**
* # Return value
*
@ -179,38 +179,38 @@ pub trait NumericVector4<T>: NumericVector<T> {
/**
* A mutable vector with numeric components
*/
pub trait MutableNumericVector<T>: MutableVector<&self/T>
pub trait MutableNumericVector<T>: MutableVector<&self/T> +
NumericVector<T> {
/**
* Negate the vector
*/
fn neg_self(&mut self);
/**
* Multiply the vector by a scalar
*/
fn mul_self_t(&mut self, value: T);
/**
* Divide the vector by a scalar
*/
fn div_self_t(&mut self, value: T);
/**
* Set the vector to the component-wise vector sum
*/
fn add_self_v(&mut self, other: &Self);
/**
* Set the vector to the component-wise vector difference
*/
fn sub_self_v(&mut self, other: &Self);
/**
* Set the vector to the component-wise vector product
*/
fn mul_self_v(&mut self, other: &Self);
/**
* Set the vector to the component-wise vector quotient
*/
@ -249,7 +249,7 @@ pub trait EuclideanVector<T>: NumericVector<T> {
* the exact length does not need to be calculated.
*/
pure fn length2(&self) -> T;
/**
* # Return value
*
@ -262,40 +262,40 @@ pub trait EuclideanVector<T>: NumericVector<T> {
* it is advisable to use the `length2` method instead.
*/
pure fn length(&self) -> T;
/**
* # Return value
*
* The squared distance between the vector and `other`.
*/
pure fn distance2(&self, other: &Self) -> T;
/**
* # Return value
*
* The distance between the vector and `other`
*/
pure fn distance(&self, other: &Self) -> T;
/**
* # Return value
*
* The angle between the vector and `other` in radians
*/
pure fn angle(&self, other: &Self) -> T;
/**
* # Return value
*
* The normalized vector
*/
pure fn normalize(&self) -> Self;
/**
* Set the length of the vector whilst preserving the direction
*/
pure fn normalize_to(&self, length: T) -> Self;
/**
* Linearly intoperlate between the vector and `other`
*
@ -313,18 +313,18 @@ pub trait EuclideanVector<T>: NumericVector<T> {
*
* * `T` - The type of the components. This should be a floating point type.
*/
pub trait MutableEuclideanVector<T>: MutableNumericVector<&self/T>
pub trait MutableEuclideanVector<T>: MutableNumericVector<&self/T> +
EuclideanVector<T> {
/**
* Normalize the vector
*/
fn normalize_self(&mut self);
/**
* Set the vector to a specified length whilst preserving the direction
*/
fn normalize_self_to(&mut self, length: T);
/**
* Linearly intoperlate the vector towards `other`
*/
@ -343,17 +343,17 @@ pub trait OrdinalVector<T, BoolVec>: Vector<T> {
* Component-wise compare of `self < other`
*/
pure fn less_than(&self, other: &Self) -> BoolVec;
/**
* Component-wise compare of `self <= other`
*/
pure fn less_than_equal(&self, other: &Self) -> BoolVec;
/**
* Component-wise compare of `self > other`
*/
pure fn greater_than(&self, other: &Self) -> BoolVec;
/**
* Component-wise compare of `self >= other`
*/
@ -372,7 +372,7 @@ pub trait EquableVector<T, BoolVec>: Vector<T> {
* Component-wise compare of `self == other`
*/
pure fn equal(&self, other: &Self) -> BoolVec;
/**
* Component-wise compare of `self != other`
*/
@ -393,14 +393,14 @@ pub trait BooleanVector: Vector<bool> {
* `true` if of any component is `true`
*/
pure fn any(&self) -> bool;
/**
* # Return value
*
* `true` only if all components are `true`
*/
pure fn all(&self) -> bool;
/**
* # Return value
*
@ -412,18 +412,18 @@ pub trait BooleanVector: Vector<bool> {
pub trait TrigVec<T>: Vector<T> {
pure fn radians(&self) -> Self;
pure fn degrees(&self) -> Self;
// Triganometric functions
pure fn sin(&self) -> Self;
pure fn cos(&self) -> Self;
pure fn tan(&self) -> Self;
// Inverse triganometric functions
pure fn asin(&self) -> Self;
pure fn acos(&self) -> Self;
pure fn atan(&self) -> Self;
pure fn atan2(&self, other: Self) -> Self;
// Hyperbolic triganometric functions
pure fn sinh(&self) -> Self;
pure fn cosh(&self) -> Self;
@ -460,7 +460,7 @@ pub trait SignedVec<T,BV>: Vector<T> {
pure fn is_negative(&self) -> BV;
pure fn is_nonpositive(&self) -> BV;
pure fn is_nonnegative(&self) -> BV;
pure fn abs(&self) -> Self;
pure fn sign(&self) -> Self;
pure fn copysign(&self, other: Self) -> Self;
@ -470,7 +470,7 @@ pub trait ExtentVec<T>: Vector<T> {
pure fn min_v(&self, other: &Self) -> Self;
pure fn max_v(&self, other: &Self) -> Self;
pure fn clamp_v(&self, mn: &Self, mx: &Self) -> Self;
pure fn min_t(&self, other: T) -> Self;
pure fn max_t(&self, other: T) -> Self;
pure fn clamp_t(&self, mn: T, mx: T) -> Self;

View file

@ -43,12 +43,12 @@ use vec::{
#[deriving_eq]
pub struct Vec2<T> { x: T, y: T }
pub impl<T:Copy Eq> Vector<T> for Vec2<T> {
impl<T:Copy + Eq> Vector<T> for Vec2<T> {
#[inline(always)]
static pure fn from_value(value: T) -> Vec2<T> {
Vector2::new(value, value)
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -59,21 +59,21 @@ pub impl<T:Copy Eq> Vector<T> for Vec2<T> {
}
}
pub impl<T> Vector2<T> for Vec2<T> {
impl<T> Vector2<T> for Vec2<T> {
#[inline(always)]
static pure fn new(x: T, y: T ) -> Vec2<T> {
Vec2 { x: x, y: y }
}
}
pub impl<T:Copy Eq> Index<uint, T> for Vec2<T> {
impl<T:Copy + Eq> Index<uint, T> for Vec2<T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } }
}
}
pub impl<T:Copy> MutableVector<T> for Vec2<T> {
impl<T:Copy> MutableVector<T> for Vec2<T> {
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &self/mut T {
match i {
@ -82,67 +82,67 @@ pub impl<T:Copy> MutableVector<T> for Vec2<T> {
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 1, but found %u", i))
}
}
#[inline(always)]
fn swap(&mut self, a: uint, b: uint) {
swap(self.index_mut(a),
self.index_mut(b));
}
}
pub impl<T:Copy Number> NumericVector<T> for Vec2<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> NumericVector<T> for Vec2<T> {
#[inline(always)]
static pure fn identity() -> Vec2<T> {
Vector2::new(one::<T>(), one::<T>())
}
#[inline(always)]
static pure fn zero() -> Vec2<T> {
Vector2::new(zero::<T>(), zero::<T>())
}
#[inline(always)]
pure fn is_zero(&self) -> bool {
self[0] == zero() &&
self[1] == zero()
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Vec2<T> {
Vector2::new(self[0] * value,
self[1] * value)
}
#[inline(always)]
pure fn div_t(&self, value: T) -> Vec2<T> {
Vector2::new(self[0] / value,
self[1] / value)
}
#[inline(always)]
pure fn add_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vector2::new(self[0] + other[0],
self[1] + other[1])
}
#[inline(always)]
pure fn sub_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vector2::new(self[0] - other[0],
self[1] - other[1])
}
#[inline(always)]
pure fn mul_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vector2::new(self[0] * other[0],
self[1] * other[1])
}
#[inline(always)]
pure fn div_v(&self, other: &Vec2<T>) -> Vec2<T> {
Vector2::new(self[0] / other[0],
self[1] / other[1])
}
#[inline(always)]
pure fn dot(&self, other: &Vec2<T>) -> T {
self[0] * other[0] +
@ -150,67 +150,67 @@ pub impl<T:Copy Number> NumericVector<T> for Vec2<T> {
}
}
pub impl<T:Copy Number> Neg<Vec2<T>> for Vec2<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Vec2<T>> for Vec2<T> {
#[inline(always)]
pure fn neg(&self) -> Vec2<T> {
Vector2::new(-self[0], -self[1])
}
}
pub impl<T:Copy Number> NumericVector2<T> for Vec2<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> NumericVector2<T> for Vec2<T> {
#[inline(always)]
static pure fn unit_x() -> Vec2<T> {
Vector2::new(one::<T>(), zero::<T>())
}
#[inline(always)]
static pure fn unit_y() -> Vec2<T> {
Vector2::new(zero::<T>(), one::<T>())
}
#[inline(always)]
pure fn perp_dot(&self, other: &Vec2<T>) ->T {
(self[0] * other[1]) - (self[1] * other[0])
}
}
pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec2<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector<&self/T> for Vec2<T> {
#[inline(always)]
fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index_mut(0);
*self.index_mut(1) = -*self.index_mut(1);
}
#[inline(always)]
fn mul_self_t(&mut self, value: &T) {
*self.index_mut(0) *= (*value);
*self.index_mut(1) *= (*value);
}
#[inline(always)]
fn div_self_t(&mut self, value: &T) {
*self.index_mut(0) /= (*value);
*self.index_mut(1) /= (*value);
}
#[inline(always)]
fn add_self_v(&mut self, other: &Vec2<T>) {
*self.index_mut(0) += other[0];
*self.index_mut(1) += other[1];
}
#[inline(always)]
fn sub_self_v(&mut self, other: &Vec2<T>) {
*self.index_mut(0) -= other[0];
*self.index_mut(1) -= other[1];
}
#[inline(always)]
fn mul_self_v(&mut self, other: &Vec2<T>) {
*self.index_mut(0) *= other[0];
*self.index_mut(1) *= other[1];
}
#[inline(always)]
fn div_self_v(&mut self, other: &Vec2<T>) {
*self.index_mut(0) /= other[0];
@ -218,79 +218,79 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec2<T> {
}
}
pub impl<T:Copy Number> ToHomogeneous<Vec3<T>> for Vec2<T> {
impl<T:Copy + Number> ToHomogeneous<Vec3<T>> for Vec2<T> {
#[inline(always)]
pure fn to_homogeneous(&self) -> Vec3<T> {
Vector3::new(self.x, self.y, zero())
}
}
pub impl<T:Copy Float> EuclideanVector<T> for Vec2<T> {
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> EuclideanVector<T> for Vec2<T> {
#[inline(always)]
pure fn length2(&self) -> T {
self.dot(self)
}
#[inline(always)]
pure fn length(&self) -> T {
self.length2().sqrt()
}
#[inline(always)]
pure fn distance2(&self, other: &Vec2<T>) -> T {
other.sub_v(self).length2()
}
#[inline(always)]
pure fn distance(&self, other: &Vec2<T>) -> T {
other.distance2(self).sqrt()
}
#[inline(always)]
pure fn angle(&self, other: &Vec2<T>) -> T {
atan2(self.perp_dot(other), self.dot(other))
}
#[inline(always)]
pure fn normalize(&self) -> Vec2<T> {
self.mul_t(one::<T>()/self.length())
}
#[inline(always)]
pure fn normalize_to(&self, length: T) -> Vec2<T> {
self.mul_t(length / self.length())
}
#[inline(always)]
pure fn lerp(&self, other: &Vec2<T>, amount: T) -> Vec2<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
}
pub impl<T:Copy Float> MutableEuclideanVector<&self/T> for Vec2<T> {
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableEuclideanVector<&self/T> for Vec2<T> {
#[inline(always)]
fn normalize_self(&mut self) {
let n = one::<T>() / self.length();
self.mul_self_t(&n);
}
#[inline(always)]
fn normalize_self_to(&mut self, length: &T) {
let n = length / self.length();
self.mul_self_t(&n);
}
fn lerp_self(&mut self, other: &Vec2<T>, amount: &T) {
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
}
}
pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Vec2<T> {
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Vec2<T> {
#[inline(always)]
pure fn fuzzy_eq(&self, other: &Vec2<T>) -> bool {
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
}
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Vec2<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
@ -298,25 +298,25 @@ pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Vec2<T> {
}
}
pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec2<bool>> for Vec2<T> {
impl<T:Copy + Ord + Eq> OrdinalVector<T, Vec2<bool>> for Vec2<T> {
#[inline(always)]
pure fn less_than(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] < other[0],
self[1] < other[1])
}
#[inline(always)]
pure fn less_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] <= other[0],
self[1] <= other[1])
}
#[inline(always)]
pure fn greater_than(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] > other[0],
self[1] > other[1])
}
#[inline(always)]
pure fn greater_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] >= other[0],
@ -324,13 +324,13 @@ pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec2<bool>> for Vec2<T> {
}
}
pub impl<T:Copy Eq> EquableVector<T, Vec2<bool>> for Vec2<T> {
impl<T:Copy + Eq> EquableVector<T, Vec2<bool>> for Vec2<T> {
#[inline(always)]
pure fn equal(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] == other[0],
self[1] == other[1])
}
#[inline(always)]
pure fn not_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
Vector2::new(self[0] != other[0],
@ -338,19 +338,19 @@ pub impl<T:Copy Eq> EquableVector<T, Vec2<bool>> for Vec2<T> {
}
}
pub impl BooleanVector for Vec2<bool> {
impl BooleanVector for Vec2<bool> {
#[inline(always)]
pure fn any(&self) -> bool {
self[0] || self[1]
}
#[inline(always)]
pure fn all(&self) -> bool {
self[0] && self[1]
}
#[inline(always)]
pure fn not(&self) -> Vec2<bool> {
pure fn not(&self) -> Vec2<bool> {
Vector2::new(!self[0], !self[1])
}
}
@ -366,62 +366,62 @@ pub type uvec2 = Vec2<u32>; // a two-component unsigned integer vector
// Static method wrappers for GLSL-style types
pub impl vec2 {
impl vec2 {
#[inline(always)] static pure fn new(x: f32, y: f32) -> vec2 { Vector2::new(x, y) }
#[inline(always)] static pure fn from_value(v: f32) -> vec2 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> vec2 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> vec2 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> vec2 { NumericVector2::unit_x() }
#[inline(always)] static pure fn unit_y() -> vec2 { NumericVector2::unit_y() }
#[inline(always)] static pure fn dim() -> uint { 2 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<vec2>() }
}
pub impl dvec2 {
impl dvec2 {
#[inline(always)] static pure fn new(x: f64, y: f64) -> dvec2 { Vector2::new(x, y) }
#[inline(always)] static pure fn from_value(v: f64) -> dvec2 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> dvec2 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> dvec2 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> dvec2 { NumericVector2::unit_x() }
#[inline(always)] static pure fn unit_y() -> dvec2 { NumericVector2::unit_y() }
#[inline(always)] static pure fn dim() -> uint { 2 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<dvec2>() }
}
pub impl bvec2 {
impl bvec2 {
#[inline(always)] static pure fn new(x: bool, y: bool) -> bvec2 { Vector2::new(x, y) }
#[inline(always)] static pure fn from_value(v: bool) -> bvec2 { Vector::from_value(v) }
#[inline(always)] static pure fn dim() -> uint { 2 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<bvec2>() }
}
pub impl ivec2 {
impl ivec2 {
#[inline(always)] static pure fn new(x: i32, y: i32) -> ivec2 { Vector2::new(x, y) }
#[inline(always)] static pure fn from_value(v: i32) -> ivec2 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> ivec2 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> ivec2 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> ivec2 { NumericVector2::unit_x() }
#[inline(always)] static pure fn unit_y() -> ivec2 { NumericVector2::unit_y() }
#[inline(always)] static pure fn dim() -> uint { 2 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<ivec2>() }
}
pub impl uvec2 {
impl uvec2 {
#[inline(always)] static pure fn new(x: u32, y: u32) -> uvec2 { Vector2::new(x, y) }
#[inline(always)] static pure fn from_value(v: u32) -> uvec2 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> uvec2 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> uvec2 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> uvec2 { NumericVector2::unit_x() }
#[inline(always)] static pure fn unit_y() -> uvec2 { NumericVector2::unit_y() }
#[inline(always)] static pure fn dim() -> uint { 2 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<uvec2>() }
}

View file

@ -45,12 +45,12 @@ use vec::{
#[deriving_eq]
pub struct Vec3<T> { x: T, y: T, z: T }
pub impl<T:Copy Eq> Vector<T> for Vec3<T> {
impl<T:Copy + Eq> Vector<T> for Vec3<T> {
#[inline(always)]
static pure fn from_value(value: T) -> Vec3<T> {
Vector3::new(value, value, value)
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -61,21 +61,21 @@ pub impl<T:Copy Eq> Vector<T> for Vec3<T> {
}
}
pub impl<T> Vector3<T> for Vec3<T> {
impl<T> Vector3<T> for Vec3<T> {
#[inline(always)]
static pure fn new(x: T, y: T, z: T) -> Vec3<T> {
Vec3 { x: x, y: y, z: z }
}
}
pub impl<T:Copy Eq> Index<uint, T> for Vec3<T> {
impl<T:Copy + Eq> Index<uint, T> for Vec3<T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } }
}
}
pub impl<T:Copy> MutableVector<T> for Vec3<T> {
impl<T:Copy> MutableVector<T> for Vec3<T> {
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &self/mut T {
match i {
@ -85,7 +85,7 @@ pub impl<T:Copy> MutableVector<T> for Vec3<T> {
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 2, but found %u", i))
}
}
#[inline(always)]
fn swap(&mut self, a: uint, b: uint) {
swap(self.index_mut(a),
@ -93,66 +93,66 @@ pub impl<T:Copy> MutableVector<T> for Vec3<T> {
}
}
pub impl<T:Copy Number> NumericVector<T> for Vec3<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> NumericVector<T> for Vec3<T> {
#[inline(always)]
static pure fn identity() -> Vec3<T> {
Vector3::new(one::<T>(), one::<T>(), one::<T>())
}
#[inline(always)]
static pure fn zero() -> Vec3<T> {
Vector3::new(zero::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
pure fn is_zero(&self) -> bool {
self[0] == zero() &&
self[1] == zero() &&
self[2] == zero()
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Vec3<T> {
Vector3::new(self[0] * value,
self[1] * value,
self[2] * value)
}
#[inline(always)]
pure fn div_t(&self, value: T) -> Vec3<T> {
Vector3::new(self[0] / value,
self[1] / value,
self[2] / value)
}
#[inline(always)]
pure fn add_v(&self, other: &Vec3<T>) -> Vec3<T>{
Vector3::new(self[0] + other[0],
self[1] + other[1],
self[2] + other[2])
}
#[inline(always)]
pure fn sub_v(&self, other: &Vec3<T>) -> Vec3<T>{
Vector3::new(self[0] - other[0],
self[1] - other[1],
self[2] - other[2])
}
#[inline(always)]
pure fn mul_v(&self, other: &Vec3<T>) -> Vec3<T>{
Vector3::new(self[0] * other[0],
self[1] * other[1],
self[2] * other[2])
}
#[inline(always)]
pure fn div_v(&self, other: &Vec3<T>) -> Vec3<T>{
Vector3::new(self[0] / other[0],
self[1] / other[1],
self[2] / other[2])
}
#[inline(always)]
pure fn dot(&self, other: &Vec3<T>) -> T {
self[0] * other[0] +
@ -161,29 +161,29 @@ pub impl<T:Copy Number> NumericVector<T> for Vec3<T> {
}
}
pub impl<T:Copy Number> Neg<Vec3<T>> for Vec3<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Vec3<T>> for Vec3<T> {
#[inline(always)]
pure fn neg(&self) -> Vec3<T> {
Vector3::new(-self[0], -self[1], -self[2])
}
}
pub impl<T:Copy Number> NumericVector3<T> for Vec3<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> NumericVector3<T> for Vec3<T> {
#[inline(always)]
static pure fn unit_x() -> Vec3<T> {
Vector3::new(one::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
static pure fn unit_y() -> Vec3<T> {
Vector3::new(zero::<T>(), one::<T>(), zero::<T>())
}
#[inline(always)]
static pure fn unit_z() -> Vec3<T> {
Vector3::new(zero::<T>(), zero::<T>(), one::<T>())
}
#[inline(always)]
pure fn cross(&self, other: &Vec3<T>) -> Vec3<T> {
Vector3::new((self[1] * other[2]) - (self[2] * other[1]),
@ -192,49 +192,49 @@ pub impl<T:Copy Number> NumericVector3<T> for Vec3<T> {
}
}
pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec3<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector<&self/T> for Vec3<T> {
#[inline(always)]
fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index_mut(0);
*self.index_mut(1) = -*self.index_mut(1);
*self.index_mut(2) = -*self.index_mut(2);
}
#[inline(always)]
fn mul_self_t(&mut self, value: &T) {
*self.index_mut(0) *= (*value);
*self.index_mut(1) *= (*value);
*self.index_mut(2) *= (*value);
}
#[inline(always)]
fn div_self_t(&mut self, value: &T) {
*self.index_mut(0) /= (*value);
*self.index_mut(1) /= (*value);
*self.index_mut(2) /= (*value);
}
#[inline(always)]
fn add_self_v(&mut self, other: &Vec3<T>) {
*self.index_mut(0) += other[0];
*self.index_mut(1) += other[1];
*self.index_mut(2) += other[2];
}
#[inline(always)]
fn sub_self_v(&mut self, other: &Vec3<T>) {
*self.index_mut(0) -= other[0];
*self.index_mut(1) -= other[1];
*self.index_mut(2) -= other[2];
}
#[inline(always)]
fn mul_self_v(&mut self, other: &Vec3<T>) {
*self.index_mut(0) *= other[0];
*self.index_mut(1) *= other[1];
*self.index_mut(2) *= other[2];
}
#[inline(always)]
fn div_self_v(&mut self, other: &Vec3<T>) {
*self.index_mut(0) /= other[0];
@ -243,86 +243,86 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec3<T> {
}
}
pub impl<T:Copy Number> MutableNumericVector3<&self/T> for Vec3<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector3<&self/T> for Vec3<T> {
#[inline(always)]
fn cross_self(&mut self, other: &Vec3<T>) {
*self = self.cross(other);
}
}
pub impl<T:Copy Number> ToHomogeneous<Vec4<T>> for Vec3<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> ToHomogeneous<Vec4<T>> for Vec3<T> {
#[inline(always)]
pure fn to_homogeneous(&self) -> Vec4<T> {
Vector4::new(self.x, self.y, self.z, zero())
}
}
pub impl<T:Copy Float> EuclideanVector<T> for Vec3<T> {
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> EuclideanVector<T> for Vec3<T> {
#[inline(always)]
pure fn length2(&self) -> T {
self.dot(self)
}
#[inline(always)]
pure fn length(&self) -> T {
self.length2().sqrt()
}
#[inline(always)]
pure fn distance2(&self, other: &Vec3<T>) -> T {
other.sub_v(self).length2()
}
#[inline(always)]
pure fn distance(&self, other: &Vec3<T>) -> T {
other.distance2(self).sqrt()
}
#[inline(always)]
pure fn angle(&self, other: &Vec3<T>) -> T {
atan2(self.cross(other).length(), self.dot(other))
}
#[inline(always)]
pure fn normalize(&self) -> Vec3<T> {
self.mul_t(one::<T>()/self.length())
}
#[inline(always)]
pure fn normalize_to(&self, length: T) -> Vec3<T> {
self.mul_t(length / self.length())
}
#[inline(always)]
pure fn lerp(&self, other: &Vec3<T>, amount: T) -> Vec3<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
}
pub impl<T:Copy Float> MutableEuclideanVector<&self/T> for Vec3<T> {
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableEuclideanVector<&self/T> for Vec3<T> {
#[inline(always)]
fn normalize_self(&mut self) {
let n = one::<T>() / self.length();
self.mul_self_t(&n);
}
#[inline(always)]
fn normalize_self_to(&mut self, length: &T) {
let n = length / self.length();
self.mul_self_t(&n);
}
fn lerp_self(&mut self, other: &Vec3<T>, amount: &T) {
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
}
}
pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Vec3<T> {
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Vec3<T> {
#[inline(always)]
pure fn fuzzy_eq(&self, other: &Vec3<T>) -> bool {
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
}
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Vec3<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
@ -331,28 +331,28 @@ pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Vec3<T> {
}
}
pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec3<bool>> for Vec3<T> {
impl<T:Copy + Ord + Eq> OrdinalVector<T, Vec3<bool>> for Vec3<T> {
#[inline(always)]
pure fn less_than(&self, other: &Vec3<T>) -> Vec3<bool> {
Vector3::new(self[0] < other[0],
self[1] < other[1],
self[2] < other[2])
}
#[inline(always)]
pure fn less_than_equal(&self, other: &Vec3<T>) -> Vec3<bool> {
Vector3::new(self[0] <= other[0],
self[1] <= other[1],
self[2] <= other[2])
}
#[inline(always)]
pure fn greater_than(&self, other: &Vec3<T>) -> Vec3<bool> {
Vector3::new(self[0] > other[0],
self[1] > other[1],
self[2] > other[2])
}
#[inline(always)]
pure fn greater_than_equal(&self, other: &Vec3<T>) -> Vec3<bool> {
Vector3::new(self[0] >= other[0],
@ -361,14 +361,14 @@ pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec3<bool>> for Vec3<T> {
}
}
pub impl<T:Copy Eq> EquableVector<T, Vec3<bool>> for Vec3<T> {
impl<T:Copy + Eq> EquableVector<T, Vec3<bool>> for Vec3<T> {
#[inline(always)]
pure fn equal(&self, other: &Vec3<T>) -> Vec3<bool> {
Vector3::new(self[0] == other[0],
self[1] == other[1],
self[2] == other[2])
}
#[inline(always)]
pure fn not_equal(&self, other: &Vec3<T>) -> Vec3<bool> {
Vector3::new(self[0] != other[0],
@ -377,19 +377,19 @@ pub impl<T:Copy Eq> EquableVector<T, Vec3<bool>> for Vec3<T> {
}
}
pub impl BooleanVector for Vec3<bool> {
impl BooleanVector for Vec3<bool> {
#[inline(always)]
pure fn any(&self) -> bool {
self[0] || self[1] || self[2]
}
#[inline(always)]
pure fn all(&self) -> bool {
self[0] && self[1] && self[2]
}
#[inline(always)]
pure fn not(&self) -> Vec3<bool> {
pure fn not(&self) -> Vec3<bool> {
Vector3::new(!self[0], !self[1], !self[2])
}
}
@ -405,66 +405,66 @@ pub type uvec3 = Vec3<u32>; // a three-component unsigned integer vector
// Static method wrappers for GLSL-style types
pub impl vec3 {
impl vec3 {
#[inline(always)] static pure fn new(x: f32, y: f32, z: f32) -> vec3 { Vector3::new(x, y, z) }
#[inline(always)] static pure fn from_value(v: f32) -> vec3 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> vec3 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> vec3 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> vec3 { NumericVector3::unit_x() }
#[inline(always)] static pure fn unit_y() -> vec3 { NumericVector3::unit_y() }
#[inline(always)] static pure fn unit_z() -> vec3 { NumericVector3::unit_z() }
#[inline(always)] static pure fn dim() -> uint { 3 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<vec3>() }
}
pub impl dvec3 {
impl dvec3 {
#[inline(always)] static pure fn new(x: f64, y: f64, z: f64) -> dvec3 { Vector3::new(x, y, z) }
#[inline(always)] static pure fn from_value(v: f64) -> dvec3 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> dvec3 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> dvec3 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> dvec3 { NumericVector3::unit_x() }
#[inline(always)] static pure fn unit_y() -> dvec3 { NumericVector3::unit_y() }
#[inline(always)] static pure fn unit_z() -> dvec3 { NumericVector3::unit_z() }
#[inline(always)] static pure fn dim() -> uint { 3 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<dvec3>() }
}
pub impl bvec3 {
impl bvec3 {
#[inline(always)] static pure fn new(x: bool, y: bool, z: bool) -> bvec3 { Vector3::new(x, y, z) }
#[inline(always)] static pure fn from_value(v: bool) -> bvec3 { Vector::from_value(v) }
#[inline(always)] static pure fn dim() -> uint { 3 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<bvec3>() }
}
pub impl ivec3 {
impl ivec3 {
#[inline(always)] static pure fn new(x: i32, y: i32, z: i32) -> ivec3 { Vector3::new(x, y, z) }
#[inline(always)] static pure fn from_value(v: i32) -> ivec3 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> ivec3 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> ivec3 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> ivec3 { NumericVector3::unit_x() }
#[inline(always)] static pure fn unit_y() -> ivec3 { NumericVector3::unit_y() }
#[inline(always)] static pure fn unit_z() -> ivec3 { NumericVector3::unit_z() }
#[inline(always)] static pure fn dim() -> uint { 3 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<ivec3>() }
}
pub impl uvec3 {
impl uvec3 {
#[inline(always)] static pure fn new(x: u32, y: u32, z: u32) -> uvec3 { Vector3::new(x, y, z) }
#[inline(always)] static pure fn from_value(v: u32) -> uvec3 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> uvec3 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> uvec3 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> uvec3 { NumericVector3::unit_x() }
#[inline(always)] static pure fn unit_y() -> uvec3 { NumericVector3::unit_y() }
#[inline(always)] static pure fn unit_z() -> uvec3 { NumericVector3::unit_z() }
#[inline(always)] static pure fn dim() -> uint { 3 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<uvec3>() }
}

View file

@ -43,12 +43,12 @@ use vec::{
#[deriving_eq]
pub struct Vec4<T> { x: T, y: T, z: T, w: T }
pub impl<T:Copy Eq> Vector<T> for Vec4<T> {
impl<T:Copy + Eq> Vector<T> for Vec4<T> {
#[inline(always)]
static pure fn from_value(value: T) -> Vec4<T> {
Vector4::new(value, value, value, value)
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -59,21 +59,21 @@ pub impl<T:Copy Eq> Vector<T> for Vec4<T> {
}
}
pub impl<T> Vector4<T> for Vec4<T> {
impl<T> Vector4<T> for Vec4<T> {
#[inline(always)]
static pure fn new(x: T, y: T, z: T, w: T) -> Vec4<T> {
Vec4 { x: x, y: y, z: z, w: w }
}
}
pub impl<T:Copy Eq> Index<uint, T> for Vec4<T> {
impl<T:Copy + Eq> Index<uint, T> for Vec4<T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } }
}
}
pub impl<T:Copy> MutableVector<T> for Vec4<T> {
impl<T:Copy> MutableVector<T> for Vec4<T> {
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &self/mut T {
match i {
@ -84,7 +84,7 @@ pub impl<T:Copy> MutableVector<T> for Vec4<T> {
_ => fail!(fmt!("index out of bounds: expected an index from 0 to 3, but found %u", i))
}
}
#[inline(always)]
fn swap(&mut self, a: uint, b: uint) {
swap(self.index_mut(a),
@ -92,17 +92,17 @@ pub impl<T:Copy> MutableVector<T> for Vec4<T> {
}
}
pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> NumericVector<T> for Vec4<T> {
#[inline(always)]
static pure fn identity() -> Vec4<T> {
Vector4::new(one::<T>(), one::<T>(), one::<T>(), one::<T>())
}
#[inline(always)]
static pure fn zero() -> Vec4<T> {
Vector4::new(zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
pure fn is_zero(&self) -> bool {
self[0] == zero() &&
@ -110,7 +110,7 @@ pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
self[2] == zero() &&
self[3] == zero()
}
#[inline(always)]
pure fn mul_t(&self, value: T) -> Vec4<T> {
Vector4::new(self[0] * value,
@ -118,7 +118,7 @@ pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
self[2] * value,
self[3] * value)
}
#[inline(always)]
pure fn div_t(&self, value: T) -> Vec4<T> {
Vector4::new(self[0] / value,
@ -126,7 +126,7 @@ pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
self[2] / value,
self[3] / value)
}
#[inline(always)]
pure fn add_v(&self, other: &Vec4<T>) -> Vec4<T> {
Vector4::new(self[0] + other[0],
@ -134,7 +134,7 @@ pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
self[2] + other[2],
self[3] + other[3])
}
#[inline(always)]
pure fn sub_v(&self, other: &Vec4<T>) -> Vec4<T> {
Vector4::new(self[0] - other[0],
@ -142,7 +142,7 @@ pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
self[2] - other[2],
self[3] - other[3])
}
#[inline(always)]
pure fn mul_v(&self, other: &Vec4<T>) -> Vec4<T> {
Vector4::new(self[0] * other[0],
@ -150,7 +150,7 @@ pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
self[2] * other[2],
self[3] * other[3])
}
#[inline(always)]
pure fn div_v(&self, other: &Vec4<T>) -> Vec4<T> {
Vector4::new(self[0] / other[0],
@ -158,7 +158,7 @@ pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
self[2] / other[2],
self[3] / other[3])
}
#[inline(always)]
pure fn dot(&self, other: &Vec4<T>) -> T {
self[0] * other[0] +
@ -168,36 +168,36 @@ pub impl<T:Copy Number> NumericVector<T> for Vec4<T> {
}
}
pub impl<T:Copy Number> Neg<Vec4<T>> for Vec4<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Neg<Vec4<T>> for Vec4<T> {
#[inline(always)]
pure fn neg(&self) -> Vec4<T> {
Vector4::new(-self[0], -self[1], -self[2], -self[3])
}
}
pub impl<T:Copy Number> NumericVector4<T> for Vec4<T> {
impl<T:Copy + Number> NumericVector4<T> for Vec4<T> {
#[inline(always)]
static pure fn unit_x() -> Vec4<T> {
Vector4::new(one::<T>(), zero::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
static pure fn unit_y() -> Vec4<T> {
Vector4::new(zero::<T>(), one::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
static pure fn unit_z() -> Vec4<T> {
Vector4::new(zero::<T>(), zero::<T>(), one::<T>(), zero::<T>())
}
#[inline(always)]
static pure fn unit_w() -> Vec4<T> {
Vector4::new(zero::<T>(), zero::<T>(), zero::<T>(), one::<T>())
}
}
pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec4<T> {
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector<&self/T> for Vec4<T> {
#[inline(always)]
fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index_mut(0);
@ -205,7 +205,7 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec4<T> {
*self.index_mut(2) = -*self.index_mut(2);
*self.index_mut(3) = -*self.index_mut(3);
}
#[inline(always)]
fn mul_self_t(&mut self, value: &T) {
*self.index_mut(0) *= (*value);
@ -213,7 +213,7 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec4<T> {
*self.index_mut(2) *= (*value);
*self.index_mut(3) *= (*value);
}
#[inline(always)]
fn div_self_t(&mut self, value: &T) {
*self.index_mut(0) /= (*value);
@ -221,7 +221,7 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec4<T> {
*self.index_mut(2) /= (*value);
*self.index_mut(3) /= (*value);
}
#[inline(always)]
fn add_self_v(&mut self, other: &Vec4<T>) {
*self.index_mut(0) += other[0];
@ -229,7 +229,7 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec4<T> {
*self.index_mut(2) += other[2];
*self.index_mut(3) += other[3];
}
#[inline(always)]
fn sub_self_v(&mut self, other: &Vec4<T>) {
*self.index_mut(0) -= other[0];
@ -237,7 +237,7 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec4<T> {
*self.index_mut(2) -= other[2];
*self.index_mut(3) -= other[3];
}
#[inline(always)]
fn mul_self_v(&mut self, other: &Vec4<T>) {
*self.index_mut(0) *= other[0];
@ -245,7 +245,7 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec4<T> {
*self.index_mut(2) *= other[2];
*self.index_mut(3) *= other[3];
}
#[inline(always)]
fn div_self_v(&mut self, other: &Vec4<T>) {
*self.index_mut(0) /= other[0];
@ -255,72 +255,72 @@ pub impl<T:Copy Number> MutableNumericVector<&self/T> for Vec4<T> {
}
}
pub impl<T:Copy Float> EuclideanVector<T> for Vec4<T> {
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> EuclideanVector<T> for Vec4<T> {
#[inline(always)]
pure fn length2(&self) -> T {
self.dot(self)
}
#[inline(always)]
pure fn length(&self) -> T {
self.length2().sqrt()
}
#[inline(always)]
pure fn distance2(&self, other: &Vec4<T>) -> T {
other.sub_v(self).length2()
}
#[inline(always)]
pure fn distance(&self, other: &Vec4<T>) -> T {
other.distance2(self).sqrt()
}
#[inline(always)]
pure fn angle(&self, other: &Vec4<T>) -> T {
acos(self.dot(other) / (self.length() * other.length()))
}
#[inline(always)]
pure fn normalize(&self) -> Vec4<T> {
self.mul_t(one::<T>()/self.length())
}
#[inline(always)]
pure fn normalize_to(&self, length: T) -> Vec4<T> {
self.mul_t(length / self.length())
}
#[inline(always)]
pure fn lerp(&self, other: &Vec4<T>, amount: T) -> Vec4<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
}
pub impl<T:Copy Float> MutableEuclideanVector<&self/T> for Vec4<T> {
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableEuclideanVector<&self/T> for Vec4<T> {
#[inline(always)]
fn normalize_self(&mut self) {
let n = one::<T>() / self.length();
self.mul_self_t(&n);
}
#[inline(always)]
fn normalize_self_to(&mut self, length: &T) {
let n = length / self.length();
self.mul_self_t(&n);
}
fn lerp_self(&mut self, other: &Vec4<T>, amount: &T) {
self.add_self_v(&other.sub_v(&*self).mul_t(*amount));
}
}
pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Vec4<T> {
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Vec4<T> {
#[inline(always)]
pure fn fuzzy_eq(&self, other: &Vec4<T>) -> bool {
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
}
#[inline(always)]
pure fn fuzzy_eq_eps(&self, other: &Vec4<T>, epsilon: &T) -> bool {
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
@ -330,7 +330,7 @@ pub impl<T:Copy Float FuzzyEq<T>> FuzzyEq<T> for Vec4<T> {
}
}
pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec4<bool>> for Vec4<T> {
impl<T:Copy + Ord + Eq> OrdinalVector<T, Vec4<bool>> for Vec4<T> {
#[inline(always)]
pure fn less_than(&self, other: &Vec4<T>) -> Vec4<bool> {
Vector4::new(self[0] < other[0],
@ -338,7 +338,7 @@ pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec4<bool>> for Vec4<T> {
self[2] < other[2],
self[3] < other[3])
}
#[inline(always)]
pure fn less_than_equal(&self, other: &Vec4<T>) -> Vec4<bool> {
Vector4::new(self[0] <= other[0],
@ -346,7 +346,7 @@ pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec4<bool>> for Vec4<T> {
self[2] <= other[2],
self[3] <= other[3])
}
#[inline(always)]
pure fn greater_than(&self, other: &Vec4<T>) -> Vec4<bool> {
Vector4::new(self[0] > other[0],
@ -354,7 +354,7 @@ pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec4<bool>> for Vec4<T> {
self[2] > other[2],
self[3] > other[3])
}
#[inline(always)]
pure fn greater_than_equal(&self, other: &Vec4<T>) -> Vec4<bool> {
Vector4::new(self[0] >= other[0],
@ -364,7 +364,7 @@ pub impl<T:Copy Ord Eq> OrdinalVector<T, Vec4<bool>> for Vec4<T> {
}
}
pub impl<T:Copy Eq> EquableVector<T, Vec4<bool>> for Vec4<T> {
impl<T:Copy + Eq> EquableVector<T, Vec4<bool>> for Vec4<T> {
#[inline(always)]
pure fn equal(&self, other: &Vec4<T>) -> Vec4<bool> {
Vector4::new(self[0] == other[0],
@ -372,7 +372,7 @@ pub impl<T:Copy Eq> EquableVector<T, Vec4<bool>> for Vec4<T> {
self[2] == other[2],
self[3] == other[3])
}
#[inline(always)]
pure fn not_equal(&self, other: &Vec4<T>) -> Vec4<bool> {
Vector4::new(self[0] != other[0],
@ -382,19 +382,19 @@ pub impl<T:Copy Eq> EquableVector<T, Vec4<bool>> for Vec4<T> {
}
}
pub impl BooleanVector for Vec4<bool> {
impl BooleanVector for Vec4<bool> {
#[inline(always)]
pure fn any(&self) -> bool {
self[0] || self[1] || self[2] || self[3]
}
#[inline(always)]
pure fn all(&self) -> bool {
self[0] && self[1] && self[2] && self[3]
}
#[inline(always)]
pure fn not(&self) -> Vec4<bool> {
pure fn not(&self) -> Vec4<bool> {
Vector4::new(!self[0], !self[1], !self[2], !self[3])
}
}
@ -410,71 +410,71 @@ pub type uvec4 = Vec4<u32>; // a four-component unsigned integer vector
// Static method wrappers for GLSL-style types
pub impl vec4 {
impl vec4 {
#[inline(always)] static pure fn new(x: f32, y: f32, z: f32, w: f32) -> vec4 { Vector4::new(x, y, z, w) }
#[inline(always)] static pure fn from_value(v: f32) -> vec4 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> vec4 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> vec4 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> vec4 { NumericVector4::unit_x() }
#[inline(always)] static pure fn unit_y() -> vec4 { NumericVector4::unit_y() }
#[inline(always)] static pure fn unit_z() -> vec4 { NumericVector4::unit_z() }
#[inline(always)] static pure fn unit_w() -> vec4 { NumericVector4::unit_w() }
#[inline(always)] static pure fn dim() -> uint { 4 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<vec4>() }
}
pub impl dvec4 {
impl dvec4 {
#[inline(always)] static pure fn new(x: f64, y: f64, z: f64, w: f64) -> dvec4 { Vector4::new(x, y, z, w) }
#[inline(always)] static pure fn from_value(v: f64) -> dvec4 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> dvec4 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> dvec4 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> dvec4 { NumericVector4::unit_x() }
#[inline(always)] static pure fn unit_y() -> dvec4 { NumericVector4::unit_y() }
#[inline(always)] static pure fn unit_z() -> dvec4 { NumericVector4::unit_z() }
#[inline(always)] static pure fn unit_w() -> dvec4 { NumericVector4::unit_w() }
#[inline(always)] static pure fn dim() -> uint { 4 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<dvec4>() }
}
pub impl bvec4 {
impl bvec4 {
#[inline(always)] static pure fn new(x: bool, y: bool, z: bool, w: bool) -> bvec4 { Vector4::new(x, y, z, w) }
#[inline(always)] static pure fn from_value(v: bool) -> bvec4 { Vector::from_value(v) }
#[inline(always)] static pure fn dim() -> uint { 4 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<bvec4>() }
}
pub impl ivec4 {
impl ivec4 {
#[inline(always)] static pure fn new(x: i32, y: i32, z: i32, w: i32) -> ivec4 { Vector4::new(x, y, z, w) }
#[inline(always)] static pure fn from_value(v: i32) -> ivec4 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> ivec4 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> ivec4 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> ivec4 { NumericVector4::unit_x() }
#[inline(always)] static pure fn unit_y() -> ivec4 { NumericVector4::unit_y() }
#[inline(always)] static pure fn unit_z() -> ivec4 { NumericVector4::unit_z() }
#[inline(always)] static pure fn unit_w() -> ivec4 { NumericVector4::unit_w() }
#[inline(always)] static pure fn dim() -> uint { 4 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<ivec4>() }
}
pub impl uvec4 {
impl uvec4 {
#[inline(always)] static pure fn new(x: u32, y: u32, z: u32, w: u32) -> uvec4 { Vector4::new(x, y, z, w) }
#[inline(always)] static pure fn from_value(v: u32) -> uvec4 { Vector::from_value(v) }
#[inline(always)] static pure fn identity() -> uvec4 { NumericVector::identity() }
#[inline(always)] static pure fn zero() -> uvec4 { NumericVector::zero() }
#[inline(always)] static pure fn unit_x() -> uvec4 { NumericVector4::unit_x() }
#[inline(always)] static pure fn unit_y() -> uvec4 { NumericVector4::unit_y() }
#[inline(always)] static pure fn unit_z() -> uvec4 { NumericVector4::unit_z() }
#[inline(always)] static pure fn unit_w() -> uvec4 { NumericVector4::unit_w() }
#[inline(always)] static pure fn dim() -> uint { 4 }
#[inline(always)] static pure fn size_of() -> uint { size_of::<uvec4>() }
}