Merge each mutable trait with its respective immutable trait

This commit is contained in:
Brendan Zabarauskas 2013-03-29 22:51:34 +11:00
parent fdb8a73555
commit c6c92dc3e3
8 changed files with 441 additions and 511 deletions

View file

@ -123,6 +123,68 @@ pub trait Matrix<T,V>: Index<uint, V> + Eq + Neg<Self> {
* The transposed matrix
*/
fn transpose(&self) -> Self;
/**
* # Return value
*
* 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
*/
fn swap_rows(&mut self, a: uint, b: uint);
/**
* 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
*/
fn to_zero(&mut self);
/**
* 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
*/
fn transpose_self(&mut self);
/**
* Check to see if the matrix is an identity matrix
@ -234,70 +296,3 @@ pub trait Matrix4<T,V>: Matrix<T,V> {
fn from_cols(c0: V, c1: V, c2: V, c3: V) -> Self;
}
/**
* A mutable matrix
*/
pub trait MutableMatrix<T,V>: Matrix<T,V> {
/**
* # Return value
*
* 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
*/
fn swap_rows(&mut self, a: uint, b: uint);
/**
* 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
*/
fn to_zero(&mut self);
/**
* 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
*/
fn transpose_self(&mut self);
}

View file

@ -12,9 +12,8 @@ use numeric::number::Number::{zero,one};
use vec::{
Vec2,
Vector2,
MutableVector,
Vector,
NumericVector,
MutableNumericVector,
vec2,
dvec2,
};
@ -26,7 +25,6 @@ use mat::{
Matrix2,
Matrix3,
Matrix4,
MutableMatrix,
};
/**
@ -169,41 +167,7 @@ impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + N
Matrix2::new(self[0][0], self[1][0],
self[0][1], self[1][1])
}
#[inline(always)]
fn is_identity(&self) -> bool {
self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
fn is_diagonal(&self) -> bool {
self[0][1].fuzzy_eq(&zero()) &&
self[1][0].fuzzy_eq(&zero())
}
#[inline(always)]
fn is_rotated(&self) -> bool {
!self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
fn is_symmetric(&self) -> bool {
self[0][1].fuzzy_eq(&self[1][0]) &&
self[1][0].fuzzy_eq(&self[0][1])
}
#[inline(always)]
fn is_invertible(&self) -> bool {
!self.determinant().fuzzy_eq(&zero())
}
#[inline(always)]
fn to_ptr(&self) -> *T {
unsafe { transmute(self) }
}
}
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 {
@ -270,6 +234,38 @@ impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + N
swap(self.x.index_mut(1), self.y.index_mut(0));
swap(self.y.index_mut(0), self.x.index_mut(1));
}
#[inline(always)]
fn is_identity(&self) -> bool {
self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
fn is_diagonal(&self) -> bool {
self[0][1].fuzzy_eq(&zero()) &&
self[1][0].fuzzy_eq(&zero())
}
#[inline(always)]
fn is_rotated(&self) -> bool {
!self.fuzzy_eq(&Matrix::identity())
}
#[inline(always)]
fn is_symmetric(&self) -> bool {
self[0][1].fuzzy_eq(&self[1][0]) &&
self[1][0].fuzzy_eq(&self[0][1])
}
#[inline(always)]
fn is_invertible(&self) -> bool {
!self.determinant().fuzzy_eq(&zero())
}
#[inline(always)]
fn to_ptr(&self) -> *T {
unsafe { transmute(self) }
}
}
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> {

View file

@ -13,10 +13,9 @@ use quat::Quat;
use vec::{
Vec3,
Vector3,
MutableVector,
Vector,
NumericVector,
NumericVector3,
MutableNumericVector,
EuclideanVector,
vec3,
dvec3,
@ -27,7 +26,6 @@ use mat::{
Matrix,
Matrix3,
Matrix4,
MutableMatrix,
};
/**
@ -196,6 +194,84 @@ impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + N
self[0][1], self[1][1], self[2][1],
self[0][2], self[1][2], self[2][2])
}
#[inline(always)]
fn col_mut(&mut self, i: uint) -> &'self mut Vec3<T> {
match i {
0 => &mut self.x,
1 => &mut self.y,
2 => &mut self.z,
_ => 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) {
*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() {
Some(m) => (*self) = m,
None => fail!(~"Couldn't invert the matrix!")
}
}
#[inline(always)]
fn transpose_self(&mut self) {
*self.col_mut(0).index_mut(1) <-> *self.col_mut(1).index_mut(0);
*self.col_mut(0).index_mut(2) <-> *self.col_mut(2).index_mut(0);
*self.col_mut(1).index_mut(0) <-> *self.col_mut(0).index_mut(1);
*self.col_mut(1).index_mut(2) <-> *self.col_mut(2).index_mut(1);
*self.col_mut(2).index_mut(0) <-> *self.col_mut(0).index_mut(2);
*self.col_mut(2).index_mut(1) <-> *self.col_mut(1).index_mut(2);
}
#[inline(always)]
fn is_identity(&self) -> bool {
@ -469,86 +545,6 @@ impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + N
}
}
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 {
0 => &mut self.x,
1 => &mut self.y,
2 => &mut self.z,
_ => 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) {
*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() {
Some(m) => (*self) = m,
None => fail!(~"Couldn't invert the matrix!")
}
}
#[inline(always)]
fn transpose_self(&mut self) {
*self.col_mut(0).index_mut(1) <-> *self.col_mut(1).index_mut(0);
*self.col_mut(0).index_mut(2) <-> *self.col_mut(2).index_mut(0);
*self.col_mut(1).index_mut(0) <-> *self.col_mut(0).index_mut(1);
*self.col_mut(1).index_mut(2) <-> *self.col_mut(2).index_mut(1);
*self.col_mut(2).index_mut(0) <-> *self.col_mut(0).index_mut(2);
*self.col_mut(2).index_mut(1) <-> *self.col_mut(1).index_mut(2);
}
}
impl<T:Copy> Index<uint, Vec3<T>> for Mat3<T> {
#[inline(always)]
fn index(&self, i: &uint) -> Vec3<T> {

View file

@ -11,9 +11,8 @@ use numeric::number::Number::{zero,one};
use vec::{
Vec4,
Vector4,
MutableVector,
Vector,
NumericVector,
MutableNumericVector,
vec4,
dvec4,
};
@ -23,7 +22,6 @@ use mat::{
Matrix,
Matrix3,
Matrix4,
MutableMatrix,
};
/**
@ -266,6 +264,96 @@ impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + N
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)]
fn col_mut(&mut self, i: uint) -> &'self mut Vec4<T> {
match i {
0 => &mut self.x,
1 => &mut self.y,
2 => &mut self.z,
3 => &mut self.w,
_ => 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) {
*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);
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);
self.col_mut(1).mul_self_t(value);
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]);
self.col_mut(1).add_self_v(&other[1]);
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]);
self.col_mut(1).sub_self_v(&other[1]);
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() {
Some(m) => (*self) = m,
None => fail!(~"Couldn't invert the matrix!")
}
}
#[inline(always)]
fn transpose_self(&mut self) {
*self.col_mut(0).index_mut(1) <-> *self.col_mut(1).index_mut(0);
*self.col_mut(0).index_mut(2) <-> *self.col_mut(2).index_mut(0);
*self.col_mut(0).index_mut(3) <-> *self.col_mut(3).index_mut(0);
*self.col_mut(1).index_mut(0) <-> *self.col_mut(0).index_mut(1);
*self.col_mut(1).index_mut(2) <-> *self.col_mut(2).index_mut(1);
*self.col_mut(1).index_mut(3) <-> *self.col_mut(3).index_mut(1);
*self.col_mut(2).index_mut(0) <-> *self.col_mut(0).index_mut(2);
*self.col_mut(2).index_mut(1) <-> *self.col_mut(1).index_mut(2);
*self.col_mut(2).index_mut(3) <-> *self.col_mut(3).index_mut(2);
*self.col_mut(3).index_mut(0) <-> *self.col_mut(0).index_mut(3);
*self.col_mut(3).index_mut(1) <-> *self.col_mut(1).index_mut(3);
*self.col_mut(3).index_mut(2) <-> *self.col_mut(2).index_mut(3);
}
#[inline(always)]
fn is_identity(&self) -> bool {
@ -393,98 +481,6 @@ impl<T:Copy + Float + FuzzyEq<T> + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + N
}
}
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 {
0 => &mut self.x,
1 => &mut self.y,
2 => &mut self.z,
3 => &mut self.w,
_ => 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) {
*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);
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);
self.col_mut(1).mul_self_t(value);
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]);
self.col_mut(1).add_self_v(&other[1]);
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]);
self.col_mut(1).sub_self_v(&other[1]);
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() {
Some(m) => (*self) = m,
None => fail!(~"Couldn't invert the matrix!")
}
}
#[inline(always)]
fn transpose_self(&mut self) {
*self.col_mut(0).index_mut(1) <-> *self.col_mut(1).index_mut(0);
*self.col_mut(0).index_mut(2) <-> *self.col_mut(2).index_mut(0);
*self.col_mut(0).index_mut(3) <-> *self.col_mut(3).index_mut(0);
*self.col_mut(1).index_mut(0) <-> *self.col_mut(0).index_mut(1);
*self.col_mut(1).index_mut(2) <-> *self.col_mut(2).index_mut(1);
*self.col_mut(1).index_mut(3) <-> *self.col_mut(3).index_mut(1);
*self.col_mut(2).index_mut(0) <-> *self.col_mut(0).index_mut(2);
*self.col_mut(2).index_mut(1) <-> *self.col_mut(1).index_mut(2);
*self.col_mut(2).index_mut(3) <-> *self.col_mut(3).index_mut(2);
*self.col_mut(3).index_mut(0) <-> *self.col_mut(0).index_mut(3);
*self.col_mut(3).index_mut(1) <-> *self.col_mut(1).index_mut(3);
*self.col_mut(3).index_mut(2) <-> *self.col_mut(2).index_mut(3);
}
}
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)]
fn neg(&self) -> Mat4<T> {

View file

@ -13,7 +13,7 @@ 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
*/
@ -25,9 +25,7 @@ pub trait Vector<T>: Index<uint, T> + Eq {
* A pointer to the first component of the vector
*/
fn to_ptr(&self) -> *T;
}
pub trait MutableVector<'self, T>: Vector<T> {
/**
* Get a mutable reference to the component at `i`
*/
@ -129,54 +127,7 @@ pub trait NumericVector<T>: Vector<T> + Neg<Self> {
* The dot product of the vector and `other`
*/
fn dot(&self, other: &Self) -> T;
}
/**
* A 2-dimensional vector with numeric components
*/
pub trait NumericVector2<T>: NumericVector<T> {
fn unit_x() -> Self;
fn unit_y() -> Self;
/**
* # Return value
*
* The perp dot product of the vector and `other`
*/
fn perp_dot(&self, other: &Self) -> T;
}
/**
* A 3-dimensional vector with numeric components
*/
pub trait NumericVector3<T>: NumericVector<T> {
fn unit_x() -> Self;
fn unit_y() -> Self;
fn unit_z() -> Self;
/**
* # Return value
*
* The cross product of the vector and `other`
*/
fn cross(&self, other: &Self) -> Self;
}
/**
* A 4-dimensional vector with numeric components
*/
pub trait NumericVector4<T>: NumericVector<T> {
fn unit_x() -> Self;
fn unit_y() -> Self;
fn unit_z() -> Self;
fn unit_w() -> Self;
}
/**
* A mutable vector with numeric components
*/
pub trait MutableNumericVector<'self, T>: MutableVector<T> +
NumericVector<T> {
/**
* Negate the vector
*/
@ -214,15 +165,51 @@ pub trait MutableNumericVector<'self, T>: MutableVector<T> +
}
/**
* A mutable 3-dimensional vector with numeric components
* A 2-dimensional vector with numeric components
*/
pub trait MutableNumericVector3<'self, T>: MutableNumericVector<T> {
pub trait NumericVector2<T>: NumericVector<T> {
fn unit_x() -> Self;
fn unit_y() -> Self;
/**
* # Return value
*
* The perp dot product of the vector and `other`
*/
fn perp_dot(&self, other: &Self) -> T;
}
/**
* A 3-dimensional vector with numeric components
*/
pub trait NumericVector3<T>: NumericVector<T> {
fn unit_x() -> Self;
fn unit_y() -> Self;
fn unit_z() -> Self;
/**
* # Return value
*
* The cross product of the vector and `other`
*/
fn cross(&self, other: &Self) -> Self;
/**
* Set to the cross product of the vector and `other`
*/
fn cross_self(&mut self, other: &Self);
}
/**
* A 4-dimensional vector with numeric components
*/
pub trait NumericVector4<T>: NumericVector<T> {
fn unit_x() -> Self;
fn unit_y() -> Self;
fn unit_z() -> Self;
fn unit_w() -> Self;
}
pub trait ToHomogeneous<H> {
/**
* Convert to a homogenous coordinate
@ -300,17 +287,7 @@ pub trait EuclideanVector<T>: NumericVector<T> {
* The intoperlated vector
*/
fn lerp(&self, other: &Self, amount: T) -> Self;
}
/**
* A mutable Euclidean (or Affine) vector
*
* # Type parameters
*
* * `T` - The type of the components. This should be a floating point type.
*/
pub trait MutableEuclideanVector<'self, T>: MutableNumericVector<T> +
EuclideanVector<T> {
/**
* Normalize the vector
*/

View file

@ -13,13 +13,10 @@ use vec::{
Vector,
Vector2,
Vector3,
MutableVector,
NumericVector,
NumericVector2,
MutableNumericVector,
ToHomogeneous,
EuclideanVector,
MutableEuclideanVector,
EquableVector,
OrdinalVector,
BooleanVector,
@ -51,6 +48,20 @@ impl<T:Copy + Eq> Vector<T> for Vec2<T> {
fn to_ptr(&self) -> *T {
unsafe { transmute(self) }
}
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &'self mut T {
match i {
0 => &mut self.x,
1 => &mut self.y,
_ => 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) {
*self.index_mut(a) <-> *self.index_mut(b);
}
}
impl<T> Vector2<T> for Vec2<T> {
@ -67,22 +78,6 @@ impl<T:Copy + Eq> Index<uint, 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 {
0 => &mut self.x,
1 => &mut self.y,
_ => 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) {
*self.index_mut(a) <-> *self.index_mut(b);
}
}
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)]
fn identity() -> Vec2<T> {
@ -141,33 +136,7 @@ impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Numer
self[0] * other[0] +
self[1] * other[1]
}
}
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)]
fn neg(&self) -> Vec2<T> {
Vector2::new(-self[0], -self[1])
}
}
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)]
fn unit_x() -> Vec2<T> {
Vector2::new(one::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_y() -> Vec2<T> {
Vector2::new(zero::<T>(), one::<T>())
}
#[inline(always)]
fn perp_dot(&self, other: &Vec2<T>) ->T {
(self[0] * other[1]) - (self[1] * other[0])
}
}
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector<T> for Vec2<T> {
#[inline(always)]
fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index_mut(0);
@ -211,6 +180,30 @@ impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Mutab
}
}
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)]
fn neg(&self) -> Vec2<T> {
Vector2::new(-self[0], -self[1])
}
}
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)]
fn unit_x() -> Vec2<T> {
Vector2::new(one::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_y() -> Vec2<T> {
Vector2::new(zero::<T>(), one::<T>())
}
#[inline(always)]
fn perp_dot(&self, other: &Vec2<T>) ->T {
(self[0] * other[1]) - (self[1] * other[0])
}
}
impl<T:Copy + Number> ToHomogeneous<Vec3<T>> for Vec2<T> {
#[inline(always)]
fn to_homogeneous(&self) -> Vec3<T> {
@ -258,9 +251,7 @@ impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Euclid
fn lerp(&self, other: &Vec2<T>, amount: T) -> Vec2<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
}
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableEuclideanVector<T> for Vec2<T> {
#[inline(always)]
fn normalize_self(&mut self) {
let n = one::<T>() / self.length();

View file

@ -13,14 +13,10 @@ use vec::{
Vector,
Vector3,
Vector4,
MutableVector,
NumericVector,
NumericVector3,
MutableNumericVector,
MutableNumericVector3,
ToHomogeneous,
EuclideanVector,
MutableEuclideanVector,
EquableVector,
OrdinalVector,
BooleanVector,
@ -53,23 +49,7 @@ impl<T:Copy + Eq> Vector<T> for Vec3<T> {
fn to_ptr(&self) -> *T {
unsafe { transmute(self) }
}
}
impl<T> Vector3<T> for Vec3<T> {
#[inline(always)]
fn new(x: T, y: T, z: T) -> Vec3<T> {
Vec3 { x: x, y: y, z: z }
}
}
impl<T:Copy + Eq> Index<uint, T> for Vec3<T> {
#[inline(always)]
fn index(&self, i: &uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[*i] } }
}
}
impl<T:Copy> MutableVector<T> for Vec3<T> {
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &'self mut T {
match i {
@ -86,6 +66,20 @@ impl<T:Copy> MutableVector<T> for Vec3<T> {
}
}
impl<T> Vector3<T> for Vec3<T> {
#[inline(always)]
fn new(x: T, y: T, z: T) -> Vec3<T> {
Vec3 { x: x, y: y, z: z }
}
}
impl<T:Copy + Eq> Index<uint, T> for Vec3<T> {
#[inline(always)]
fn index(&self, i: &uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[*i] } }
}
}
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)]
fn identity() -> Vec3<T> {
@ -152,40 +146,7 @@ impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Numer
self[1] * other[1] +
self[2] * other[2]
}
}
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)]
fn neg(&self) -> Vec3<T> {
Vector3::new(-self[0], -self[1], -self[2])
}
}
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)]
fn unit_x() -> Vec3<T> {
Vector3::new(one::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_y() -> Vec3<T> {
Vector3::new(zero::<T>(), one::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_z() -> Vec3<T> {
Vector3::new(zero::<T>(), zero::<T>(), one::<T>())
}
#[inline(always)]
fn cross(&self, other: &Vec3<T>) -> Vec3<T> {
Vector3::new((self[1] * other[2]) - (self[2] * other[1]),
(self[2] * other[0]) - (self[0] * other[2]),
(self[0] * other[1]) - (self[1] * other[0]))
}
}
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector<T> for Vec3<T> {
#[inline(always)]
fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index_mut(0);
@ -236,7 +197,36 @@ impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Mutab
}
}
impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector3<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)]
fn neg(&self) -> Vec3<T> {
Vector3::new(-self[0], -self[1], -self[2])
}
}
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)]
fn unit_x() -> Vec3<T> {
Vector3::new(one::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_y() -> Vec3<T> {
Vector3::new(zero::<T>(), one::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_z() -> Vec3<T> {
Vector3::new(zero::<T>(), zero::<T>(), one::<T>())
}
#[inline(always)]
fn cross(&self, other: &Vec3<T>) -> Vec3<T> {
Vector3::new((self[1] * other[2]) - (self[2] * other[1]),
(self[2] * other[0]) - (self[0] * other[2]),
(self[0] * other[1]) - (self[1] * other[0]))
}
#[inline(always)]
fn cross_self(&mut self, other: &Vec3<T>) {
*self = self.cross(other);
@ -290,9 +280,7 @@ impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Euclid
fn lerp(&self, other: &Vec3<T>, amount: T) -> Vec3<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
}
impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableEuclideanVector<T> for Vec3<T> {
#[inline(always)]
fn normalize_self(&mut self) {
let n = one::<T>() / self.length();

View file

@ -11,13 +11,10 @@ use numeric::number::Number::{zero,one};
use vec::{
Vector,
Vector4,
MutableVector,
NumericVector,
NumericVector4,
MutableNumericVector,
ToHomogeneous,
EuclideanVector,
MutableEuclideanVector,
EquableVector,
OrdinalVector,
BooleanVector,
@ -51,23 +48,7 @@ impl<T:Copy + Eq> Vector<T> for Vec4<T> {
fn to_ptr(&self) -> *T {
unsafe { transmute(self) }
}
}
impl<T> Vector4<T> for Vec4<T> {
#[inline(always)]
fn new(x: T, y: T, z: T, w: T) -> Vec4<T> {
Vec4 { x: x, y: y, z: z, w: w }
}
}
impl<T:Copy + Eq> Index<uint, T> for Vec4<T> {
#[inline(always)]
fn index(&self, i: &uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[*i] } }
}
}
impl<T:Copy> MutableVector<T> for Vec4<T> {
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &'self mut T {
match i {
@ -85,6 +66,20 @@ impl<T:Copy> MutableVector<T> for Vec4<T> {
}
}
impl<T> Vector4<T> for Vec4<T> {
#[inline(always)]
fn new(x: T, y: T, z: T, w: T) -> Vec4<T> {
Vec4 { x: x, y: y, z: z, w: w }
}
}
impl<T:Copy + Eq> Index<uint, T> for Vec4<T> {
#[inline(always)]
fn index(&self, i: &uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[*i] } }
}
}
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)]
fn identity() -> Vec4<T> {
@ -159,38 +154,7 @@ impl<T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Numer
self[2] * other[2] +
self[3] * other[3]
}
}
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)]
fn neg(&self) -> Vec4<T> {
Vector4::new(-self[0], -self[1], -self[2], -self[3])
}
}
impl<T:Copy + Number> NumericVector4<T> for Vec4<T> {
#[inline(always)]
fn unit_x() -> Vec4<T> {
Vector4::new(one::<T>(), zero::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_y() -> Vec4<T> {
Vector4::new(zero::<T>(), one::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_z() -> Vec4<T> {
Vector4::new(zero::<T>(), zero::<T>(), one::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_w() -> Vec4<T> {
Vector4::new(zero::<T>(), zero::<T>(), zero::<T>(), one::<T>())
}
}
impl<'self,T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableNumericVector<T> for Vec4<T> {
#[inline(always)]
fn neg_self(&mut self) {
*self.index_mut(0) = -*self.index_mut(0);
@ -248,6 +212,35 @@ impl<'self,T:Copy + Number + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<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)]
fn neg(&self) -> Vec4<T> {
Vector4::new(-self[0], -self[1], -self[2], -self[3])
}
}
impl<T:Copy + Number> NumericVector4<T> for Vec4<T> {
#[inline(always)]
fn unit_x() -> Vec4<T> {
Vector4::new(one::<T>(), zero::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_y() -> Vec4<T> {
Vector4::new(zero::<T>(), one::<T>(), zero::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_z() -> Vec4<T> {
Vector4::new(zero::<T>(), zero::<T>(), one::<T>(), zero::<T>())
}
#[inline(always)]
fn unit_w() -> Vec4<T> {
Vector4::new(zero::<T>(), zero::<T>(), zero::<T>(), one::<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)]
fn length2(&self) -> T {
@ -288,9 +281,7 @@ impl<T:Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> Euclid
fn lerp(&self, other: &Vec4<T>, amount: T) -> Vec4<T> {
self.add_v(&other.sub_v(self).mul_t(amount))
}
}
impl<T: Copy + Float + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>> MutableEuclideanVector<T> for Vec4<T> {
#[inline(always)]
fn normalize_self(&mut self) {
let n = one::<T>() / self.length();