Combine vec*.rs and mat*.rs files into two respective modules
I hate to do this as I find huge long files annoying, but it'll help work around some weird export bugs.
This commit is contained in:
parent
a1f0980688
commit
85e8677c9c
9 changed files with 2838 additions and 2960 deletions
|
@ -12,14 +12,8 @@ extern mod std;
|
|||
extern mod numeric;
|
||||
|
||||
pub mod mat;
|
||||
pub mod mat2;
|
||||
pub mod mat3;
|
||||
pub mod mat4;
|
||||
pub mod quat;
|
||||
pub mod vec;
|
||||
pub mod vec2;
|
||||
pub mod vec3;
|
||||
pub mod vec4;
|
||||
|
||||
pub mod projection;
|
||||
|
||||
|
|
1550
src/mat.rs
1550
src/mat.rs
File diff suppressed because it is too large
Load diff
437
src/mat2.rs
437
src/mat2.rs
|
@ -1,437 +0,0 @@
|
|||
use std::cmp::{FuzzyEq, FUZZY_EPSILON};
|
||||
use numeric::*;
|
||||
use numeric::number::Number;
|
||||
use numeric::number::Number::{zero,one};
|
||||
|
||||
use vec::{
|
||||
Vec2,
|
||||
Vector2,
|
||||
Vector,
|
||||
NumericVector,
|
||||
vec2,
|
||||
dvec2,
|
||||
};
|
||||
|
||||
use mat::{
|
||||
Mat3,
|
||||
Mat4,
|
||||
Matrix,
|
||||
Matrix2,
|
||||
Matrix3,
|
||||
Matrix4,
|
||||
};
|
||||
|
||||
/**
|
||||
* A 2 x 2 column major matrix
|
||||
*
|
||||
* # Type parameters
|
||||
*
|
||||
* * `T` - The type of the elements of the matrix. Should be a floating point type.
|
||||
*
|
||||
* # Fields
|
||||
*
|
||||
* * `x` - the first column vector of the matrix
|
||||
* * `y` - the second column vector of the matrix
|
||||
* * `z` - the third column vector of the matrix
|
||||
*/
|
||||
#[deriving(Eq)]
|
||||
pub struct Mat2<T> { x: Vec2<T>, y: Vec2<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)]
|
||||
fn col(&self, i: uint) -> Vec2<T> { self[i] }
|
||||
|
||||
#[inline(always)]
|
||||
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`
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `value` - the value to set the major diagonal to
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1
|
||||
* +-----+-----+
|
||||
* r0 | val | 0 |
|
||||
* +-----+-----+
|
||||
* r1 | 0 | val |
|
||||
* +-----+-----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_value(value: T) -> Mat2<T> {
|
||||
Matrix2::new(value, zero(),
|
||||
zero(), value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the multiplicative identity matrix
|
||||
* ~~~
|
||||
* c0 c1
|
||||
* +----+----+
|
||||
* r0 | 1 | 0 |
|
||||
* +----+----+
|
||||
* r1 | 0 | 1 |
|
||||
* +----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn identity() -> 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 |
|
||||
* +----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn zero() -> Mat2<T> {
|
||||
Matrix2::new(zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_t(&self, value: T) -> Mat2<T> {
|
||||
Matrix2::from_cols(self[0].mul_t(value),
|
||||
self[1].mul_t(value))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_v(&self, vec: &Vec2<T>) -> Vec2<T> {
|
||||
Vector2::new(self.row(0).dot(vec),
|
||||
self.row(1).dot(vec))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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)]
|
||||
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)]
|
||||
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)),
|
||||
self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)))
|
||||
}
|
||||
|
||||
fn dot(&self, other: &Mat2<T>) -> T {
|
||||
other.transpose().mul_m(self).trace()
|
||||
}
|
||||
|
||||
fn determinant(&self) -> T {
|
||||
self[0][0] * self[1][1] - self[1][0] * self[0][1]
|
||||
}
|
||||
|
||||
fn trace(&self) -> T {
|
||||
self[0][0] + self[1][1]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn inverse(&self) -> Option<Mat2<T>> {
|
||||
let d = self.determinant();
|
||||
if d.fuzzy_eq(&zero()) {
|
||||
None
|
||||
} else {
|
||||
Some(Matrix2::new( self[1][1]/d, -self[0][1]/d,
|
||||
-self[1][0]/d, self[0][0]/d))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn transpose(&self) -> Mat2<T> {
|
||||
Matrix2::new(self[0][0], self[1][0],
|
||||
self[0][1], self[1][1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn col_mut(&mut self, i: uint) -> &'self mut Vec2<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_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);
|
||||
}
|
||||
|
||||
#[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.x.mul_self_t(value);
|
||||
self.y.mul_self_t(value);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn add_self_m(&mut self, other: &Mat2<T>) {
|
||||
self.x.add_self_v(&other[0]);
|
||||
self.y.add_self_v(&other[1]);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn sub_self_m(&mut self, other: &Mat2<T>) {
|
||||
self.x.sub_self_v(&other[0]);
|
||||
self.y.sub_self_v(&other[1]);
|
||||
}
|
||||
|
||||
#[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.x.index_mut(1) <-> *self.y.index_mut(0);
|
||||
*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 { cast::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> {
|
||||
/**
|
||||
* Construct a 2 x 2 matrix
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `c0r0`, `c0r1` - the first column of the matrix
|
||||
* * `c1r0`, `c1r1` - the second column of the matrix
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1
|
||||
* +------+------+
|
||||
* r0 | c0r0 | c1r0 |
|
||||
* +------+------+
|
||||
* r1 | c0r1 | c1r1 |
|
||||
* +------+------+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn new(c0r0: T, c0r1: T,
|
||||
c1r0: T, c1r1: T) -> 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
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `c0` - the first column vector of the matrix
|
||||
* * `c1` - the second column vector of the matrix
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1
|
||||
* +------+------+
|
||||
* r0 | c0.x | c1.x |
|
||||
* +------+------+
|
||||
* r1 | c0.y | c1.y |
|
||||
* +------+------+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_cols(c0: Vec2<T>,
|
||||
c1: Vec2<T>) -> Mat2<T> {
|
||||
Mat2 { x: c0, y: c1 }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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
|
||||
* ~~~
|
||||
* c0 c1 c0 c1 c2
|
||||
* +----+----+ +----+----+----+
|
||||
* r0 | a | b | r0 | a | b | 0 |
|
||||
* +----+----+ +----+----+----+
|
||||
* r1 | c | d | => r1 | c | d | 0 |
|
||||
* +----+----+ +----+----+----+
|
||||
* r2 | 0 | 0 | 1 |
|
||||
* +----+----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn to_mat3(&self) -> Mat3<T> {
|
||||
Matrix3::new(self[0][0], self[0][1], zero(),
|
||||
self[1][0], self[1][1], zero(),
|
||||
zero(), zero(), one())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the the matrix with an extra two rows and columns added
|
||||
* ~~~
|
||||
* c0 c1 c0 c1 c2 c3
|
||||
* +----+----+ +----+----+----+----+
|
||||
* r0 | a | b | r0 | a | b | 0 | 0 |
|
||||
* +----+----+ +----+----+----+----+
|
||||
* r1 | c | d | => r1 | c | d | 0 | 0 |
|
||||
* +----+----+ +----+----+----+----+
|
||||
* r2 | 0 | 0 | 1 | 0 |
|
||||
* +----+----+----+----+
|
||||
* r3 | 0 | 0 | 0 | 1 |
|
||||
* +----+----+----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn to_mat4(&self) -> Mat4<T> {
|
||||
Matrix4::new(self[0][0], self[0][1], zero(), zero(),
|
||||
self[1][0], self[1][1], zero(), zero(),
|
||||
zero(), zero(), one(), zero(),
|
||||
zero(), zero(), zero(), one())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy> Index<uint, Vec2<T>> for Mat2<T> {
|
||||
#[inline(always)]
|
||||
fn index(&self, i: &uint) -> Vec2<T> {
|
||||
unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 2) |slice| { slice[*i] } }
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
fn neg(&self) -> Mat2<T> {
|
||||
Matrix2::from_cols(-self[0], -self[1])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Mat2<T> {
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq(&self, other: &Mat2<T>) -> bool {
|
||||
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq_eps(&self, other: &Mat2<T>, epsilon: &T) -> bool {
|
||||
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
|
||||
self[1].fuzzy_eq_eps(&other[1], epsilon)
|
||||
}
|
||||
}
|
||||
|
||||
// GLSL-style type aliases, corresponding to Section 4.1.6 of the [GLSL 4.30.6 specification]
|
||||
// (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
|
||||
|
||||
/// a 2×2 single-precision floating-point matrix
|
||||
pub type mat2 = Mat2<f32>;
|
||||
/// a 2×2 double-precision floating-point matrix
|
||||
pub type dmat2 = Mat2<f64>;
|
||||
|
||||
// Static method wrappers for GLSL-style types
|
||||
|
||||
pub impl mat2 {
|
||||
#[inline(always)] fn new(c0r0: f32, c0r1: f32, c1r0: f32, c1r1: f32)
|
||||
-> mat2 { Matrix2::new(c0r0, c0r1, c1r0, c1r1) }
|
||||
#[inline(always)] fn from_cols(c0: vec2, c1: vec2)
|
||||
-> mat2 { Matrix2::from_cols(c0, c1) }
|
||||
#[inline(always)] fn from_value(v: f32) -> mat2 { Matrix::from_value(v) }
|
||||
|
||||
#[inline(always)] fn identity() -> mat2 { Matrix::identity() }
|
||||
#[inline(always)] fn zero() -> mat2 { Matrix::zero() }
|
||||
|
||||
#[inline(always)] fn from_angle(radians: f32) -> mat2 { Matrix2::from_angle(radians) }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 2 }
|
||||
#[inline(always)] fn rows() -> uint { 2 }
|
||||
#[inline(always)] fn cols() -> uint { 2 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<mat2>() }
|
||||
}
|
||||
|
||||
pub impl dmat2 {
|
||||
#[inline(always)] fn new(c0r0: f64, c0r1: f64, c1r0: f64, c1r1: f64)
|
||||
-> dmat2 { Matrix2::new(c0r0, c0r1, c1r0, c1r1) }
|
||||
#[inline(always)] fn from_cols(c0: dvec2, c1: dvec2)
|
||||
-> dmat2 { Matrix2::from_cols(c0, c1) }
|
||||
#[inline(always)] fn from_value(v: f64) -> dmat2 { Matrix::from_value(v) }
|
||||
|
||||
#[inline(always)] fn identity() -> dmat2 { Matrix::identity() }
|
||||
#[inline(always)] fn zero() -> dmat2 { Matrix::zero() }
|
||||
|
||||
#[inline(always)] fn from_angle(radians: f64) -> dmat2 { Matrix2::from_angle(radians) }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 2 }
|
||||
#[inline(always)] fn rows() -> uint { 2 }
|
||||
#[inline(always)] fn cols() -> uint { 2 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<dmat2>() }
|
||||
}
|
620
src/mat3.rs
620
src/mat3.rs
|
@ -1,620 +0,0 @@
|
|||
use std::cmp::{FuzzyEq, FUZZY_EPSILON};
|
||||
use numeric::*;
|
||||
use numeric::number::Number;
|
||||
use numeric::number::Number::{zero,one};
|
||||
|
||||
use quat::Quat;
|
||||
|
||||
use vec::{
|
||||
Vec3,
|
||||
Vector3,
|
||||
Vector,
|
||||
NumericVector,
|
||||
NumericVector3,
|
||||
EuclideanVector,
|
||||
vec3,
|
||||
dvec3,
|
||||
};
|
||||
|
||||
use mat::{
|
||||
Mat4,
|
||||
Matrix,
|
||||
Matrix3,
|
||||
Matrix4,
|
||||
};
|
||||
|
||||
/**
|
||||
* A 3 x 3 column major matrix
|
||||
*
|
||||
* # Type parameters
|
||||
*
|
||||
* * `T` - The type of the elements of the matrix. Should be a floating point type.
|
||||
*
|
||||
* # Fields
|
||||
*
|
||||
* * `x` - the first column vector of the matrix
|
||||
* * `y` - the second column vector of the matrix
|
||||
* * `z` - the third column vector of the matrix
|
||||
*/
|
||||
#[deriving(Eq)]
|
||||
pub struct Mat3<T> { x: Vec3<T>, y: Vec3<T>, z: Vec3<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)]
|
||||
fn col(&self, i: uint) -> Vec3<T> { self[i] }
|
||||
|
||||
#[inline(always)]
|
||||
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`
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `value` - the value to set the major diagonal to
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1 c2
|
||||
* +-----+-----+-----+
|
||||
* r0 | val | 0 | 0 |
|
||||
* +-----+-----+-----+
|
||||
* r1 | 0 | val | 0 |
|
||||
* +-----+-----+-----+
|
||||
* r2 | 0 | 0 | val |
|
||||
* +-----+-----+-----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_value(value: T) -> Mat3<T> {
|
||||
Matrix3::new(value, zero(), zero(),
|
||||
zero(), value, zero(),
|
||||
zero(), zero(), value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the multiplicative identity matrix
|
||||
* ~~~
|
||||
* c0 c1 c2
|
||||
* +----+----+----+
|
||||
* r0 | 1 | 0 | 0 |
|
||||
* +----+----+----+
|
||||
* r1 | 0 | 1 | 0 |
|
||||
* +----+----+----+
|
||||
* r2 | 0 | 0 | 1 |
|
||||
* +----+----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn identity() -> Mat3<T> {
|
||||
Matrix3::new( one::<T>(), zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), one::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>(), one::<T>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the additive identity matrix
|
||||
* ~~~
|
||||
* c0 c1 c2
|
||||
* +----+----+----+
|
||||
* r0 | 0 | 0 | 0 |
|
||||
* +----+----+----+
|
||||
* r1 | 0 | 0 | 0 |
|
||||
* +----+----+----+
|
||||
* r2 | 0 | 0 | 0 |
|
||||
* +----+----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn zero() -> Mat3<T> {
|
||||
Matrix3::new(zero::<T>(), zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>(), zero::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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)]
|
||||
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)]
|
||||
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)]
|
||||
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)]
|
||||
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)))
|
||||
}
|
||||
|
||||
fn dot(&self, other: &Mat3<T>) -> T {
|
||||
other.transpose().mul_m(self).trace()
|
||||
}
|
||||
|
||||
fn determinant(&self) -> T {
|
||||
self.col(0).dot(&self.col(1).cross(&self.col(2)))
|
||||
}
|
||||
|
||||
fn trace(&self) -> T {
|
||||
self[0][0] + self[1][1] + self[2][2]
|
||||
}
|
||||
|
||||
// #[inline(always)]
|
||||
fn inverse(&self) -> Option<Mat3<T>> {
|
||||
let d = self.determinant();
|
||||
if d.fuzzy_eq(&zero()) {
|
||||
None
|
||||
} else {
|
||||
let m: Mat3<T> = Matrix3::from_cols(self[1].cross(&self[2]).div_t(d),
|
||||
self[2].cross(&self[0]).div_t(d),
|
||||
self[0].cross(&self[1]).div_t(d));
|
||||
Some(m.transpose())
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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)]
|
||||
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 {
|
||||
self.fuzzy_eq(&Matrix::identity())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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)]
|
||||
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[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])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn is_invertible(&self) -> bool {
|
||||
!self.determinant().fuzzy_eq(&zero())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_ptr(&self) -> *T {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `c0r0`, `c0r1`, `c0r2` - the first column of the matrix
|
||||
* * `c1r0`, `c1r1`, `c1r2` - the second column of the matrix
|
||||
* * `c2r0`, `c2r1`, `c2r2` - the third column of the matrix
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1 c2
|
||||
* +------+------+------+
|
||||
* r0 | c0r0 | c1r0 | c2r0 |
|
||||
* +------+------+------+
|
||||
* r1 | c0r1 | c1r1 | c2r1 |
|
||||
* +------+------+------+
|
||||
* r2 | c0r2 | c1r2 | c2r2 |
|
||||
* +------+------+------+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn new(c0r0:T, c0r1:T, c0r2:T,
|
||||
c1r0:T, c1r1:T, c1r2:T,
|
||||
c2r0:T, c2r1:T, c2r2:T) -> Mat3<T> {
|
||||
Matrix3::from_cols(Vector3::new::<T,Vec3<T>>(c0r0, c0r1, c0r2),
|
||||
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
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `c0` - the first column vector of the matrix
|
||||
* * `c1` - the second column vector of the matrix
|
||||
* * `c2` - the third column vector of the matrix
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1 c2
|
||||
* +------+------+------+
|
||||
* r0 | c0.x | c1.x | c2.x |
|
||||
* +------+------+------+
|
||||
* r1 | c0.y | c1.y | c2.y |
|
||||
* +------+------+------+
|
||||
* r2 | c0.z | c1.z | c2.z |
|
||||
* +------+------+------+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_cols(c0: Vec3<T>,
|
||||
c1: Vec3<T>,
|
||||
c2: Vec3<T>) -> Mat3<T> {
|
||||
Mat3 { x: c0, y: c1, z: c2 }
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a matrix from an angular rotation around the `x` axis
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_angle_x(radians: T) -> 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
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_angle_y(radians: T) -> 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
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_angle_z(radians: T) -> 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
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `theta_x` - the angular rotation around the `x` axis (pitch)
|
||||
* * `theta_y` - the angular rotation around the `y` axis (yaw)
|
||||
* * `theta_z` - the angular rotation around the `z` axis (roll)
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Mat3<T> {
|
||||
// http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations
|
||||
let cx = cos(radians_x);
|
||||
let sx = sin(radians_x);
|
||||
let cy = cos(radians_y);
|
||||
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
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_angle_axis(radians: T, axis: &Vec3<T>) -> 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)]
|
||||
fn from_axes(x: Vec3<T>, y: Vec3<T>, z: Vec3<T>) -> Mat3<T> {
|
||||
Matrix3::from_cols(x, y, z)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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
|
||||
* ~~~
|
||||
* c0 c1 c2 c0 c1 c2 c3
|
||||
* +----+----+----+ +----+----+----+----+
|
||||
* r0 | a | b | c | r0 | a | b | c | 0 |
|
||||
* +----+----+----+ +----+----+----+----+
|
||||
* r1 | d | e | f | => r1 | d | e | f | 0 |
|
||||
* +----+----+----+ +----+----+----+----+
|
||||
* r2 | g | h | i | r2 | g | h | i | 0 |
|
||||
* +----+----+----+ +----+----+----+----+
|
||||
* r3 | 0 | 0 | 0 | 1 |
|
||||
* +----+----+----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn to_mat4(&self) -> Mat4<T> {
|
||||
Matrix4::new(self[0][0], self[0][1], self[0][2], zero(),
|
||||
self[1][0], self[1][1], self[1][2], zero(),
|
||||
self[2][0], self[2][1], self[2][2], zero(),
|
||||
zero(), zero(), zero(), one())
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the matrix to a quaternion
|
||||
*/
|
||||
#[inline(always)]
|
||||
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;
|
||||
s = half / s;
|
||||
x = (self[1][2] - self[2][1]) * s;
|
||||
y = (self[2][0] - self[0][2]) * s;
|
||||
z = (self[0][1] - self[1][0]) * s;
|
||||
} else if (self[0][0] > self[1][1]) && (self[0][0] > self[2][2]) {
|
||||
s = (half + (self[0][0] - self[1][1] - self[2][2])).sqrt();
|
||||
w = half * s;
|
||||
s = half / s;
|
||||
x = (self[0][1] - self[1][0]) * s;
|
||||
y = (self[2][0] - self[0][2]) * s;
|
||||
z = (self[1][2] - self[2][1]) * s;
|
||||
} else if self[1][1] > self[2][2] {
|
||||
s = (half + (self[1][1] - self[0][0] - self[2][2])).sqrt();
|
||||
w = half * s;
|
||||
s = half / s;
|
||||
x = (self[0][1] - self[1][0]) * s;
|
||||
y = (self[1][2] - self[2][1]) * s;
|
||||
z = (self[2][0] - self[0][2]) * s;
|
||||
} else {
|
||||
s = (half + (self[2][2] - self[0][0] - self[1][1])).sqrt();
|
||||
w = half * s;
|
||||
s = half / s;
|
||||
x = (self[2][0] - self[0][2]) * s;
|
||||
y = (self[1][2] - self[2][1]) * s;
|
||||
z = (self[0][1] - self[1][0]) * s;
|
||||
}
|
||||
|
||||
Quat::new(w, x, y, z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy> Index<uint, Vec3<T>> for Mat3<T> {
|
||||
#[inline(always)]
|
||||
fn index(&self, i: &uint) -> Vec3<T> {
|
||||
unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 3) |slice| { slice[*i] } }
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
fn neg(&self) -> Mat3<T> {
|
||||
Matrix3::from_cols(-self[0], -self[1], -self[2])
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
fn fuzzy_eq(&self, other: &Mat3<T>) -> bool {
|
||||
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq_eps(&self, other: &Mat3<T>, epsilon: &T) -> bool {
|
||||
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
|
||||
self[1].fuzzy_eq_eps(&other[1], epsilon) &&
|
||||
self[2].fuzzy_eq_eps(&other[2], epsilon)
|
||||
}
|
||||
}
|
||||
|
||||
// GLSL-style type aliases, corresponding to Section 4.1.6 of the [GLSL 4.30.6 specification]
|
||||
// (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
|
||||
|
||||
/// a 3×3 single-precision floating-point matrix
|
||||
pub type mat3 = Mat3<f32>;
|
||||
/// a 3×3 double-precision floating-point matrix
|
||||
pub type dmat3 = Mat3<f64>;
|
||||
|
||||
// Static method wrappers for GLSL-style types
|
||||
|
||||
pub impl mat3 {
|
||||
#[inline(always)] 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)] fn from_cols(c0: vec3, c1: vec3, c2: vec3)
|
||||
-> mat3 { Matrix3::from_cols(c0, c1, c2) }
|
||||
#[inline(always)] fn from_value(v: f32) -> mat3 { Matrix::from_value(v) }
|
||||
|
||||
#[inline(always)] fn identity() -> mat3 { Matrix::identity() }
|
||||
#[inline(always)] fn zero() -> mat3 { Matrix::zero() }
|
||||
|
||||
#[inline(always)] fn from_angle_x(radians: f32) -> mat3 { Matrix3::from_angle_x(radians) }
|
||||
#[inline(always)] fn from_angle_y(radians: f32) -> mat3 { Matrix3::from_angle_y(radians) }
|
||||
#[inline(always)] fn from_angle_z(radians: f32) -> mat3 { Matrix3::from_angle_z(radians) }
|
||||
#[inline(always)] fn from_angle_xyz(radians_x: f32, radians_y: f32, radians_z: f32) -> mat3 { Matrix3::from_angle_xyz(radians_x, radians_y, radians_z) }
|
||||
#[inline(always)] fn from_angle_axis(radians: f32, axis: &vec3) -> mat3 { Matrix3::from_angle_axis(radians, axis) }
|
||||
#[inline(always)] fn from_axes(x: vec3, y: vec3, z: vec3) -> mat3 { Matrix3::from_axes(x, y, z) }
|
||||
#[inline(always)] fn look_at(dir: &vec3, up: &vec3) -> mat3 { Matrix3::look_at(dir, up) }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 3 }
|
||||
#[inline(always)] fn rows() -> uint { 3 }
|
||||
#[inline(always)] fn cols() -> uint { 3 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<mat3>() }
|
||||
}
|
||||
|
||||
|
||||
pub impl dmat3 {
|
||||
#[inline(always)] 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)] fn from_cols(c0: dvec3, c1: dvec3, c2: dvec3)
|
||||
-> dmat3 { Matrix3::from_cols(c0, c1, c2) }
|
||||
#[inline(always)] fn from_value(v: f64) -> dmat3 { Matrix::from_value(v) }
|
||||
|
||||
#[inline(always)] fn identity() -> dmat3 { Matrix::identity() }
|
||||
#[inline(always)] fn zero() -> dmat3 { Matrix::zero() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 3 }
|
||||
#[inline(always)] fn rows() -> uint { 3 }
|
||||
#[inline(always)] fn cols() -> uint { 3 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<dmat3>() }
|
||||
}
|
548
src/mat4.rs
548
src/mat4.rs
|
@ -1,548 +0,0 @@
|
|||
use std::cmp::{FuzzyEq, FUZZY_EPSILON};
|
||||
use numeric::*;
|
||||
use numeric::number::Number;
|
||||
use numeric::number::Number::{zero,one};
|
||||
|
||||
use vec::{
|
||||
Vec4,
|
||||
Vector4,
|
||||
Vector,
|
||||
NumericVector,
|
||||
vec4,
|
||||
dvec4,
|
||||
};
|
||||
|
||||
use mat::{
|
||||
Mat3,
|
||||
Matrix,
|
||||
Matrix3,
|
||||
Matrix4,
|
||||
};
|
||||
|
||||
/**
|
||||
* A 4 x 4 column major matrix
|
||||
*
|
||||
* # Type parameters
|
||||
*
|
||||
* * `T` - The type of the elements of the matrix. Should be a floating point type.
|
||||
*
|
||||
* # Fields
|
||||
*
|
||||
* * `x` - the first column vector of the matrix
|
||||
* * `y` - the second column vector of the matrix
|
||||
* * `z` - the third column vector of the matrix
|
||||
* * `w` - the fourth column vector of the matrix
|
||||
*/
|
||||
#[deriving(Eq)]
|
||||
pub struct Mat4<T> { x: Vec4<T>, y: Vec4<T>, z: Vec4<T>, w: Vec4<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)]
|
||||
fn col(&self, i: uint) -> Vec4<T> { self[i] }
|
||||
|
||||
#[inline(always)]
|
||||
fn row(&self, i: uint) -> Vec4<T> {
|
||||
Vector4::new(self[0][i],
|
||||
self[1][i],
|
||||
self[2][i],
|
||||
self[3][i])
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a 4 x 4 diagonal matrix with the major diagonal set to `value`
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `value` - the value to set the major diagonal to
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1 c2 c3
|
||||
* +-----+-----+-----+-----+
|
||||
* r0 | val | 0 | 0 | 0 |
|
||||
* +-----+-----+-----+-----+
|
||||
* r1 | 0 | val | 0 | 0 |
|
||||
* +-----+-----+-----+-----+
|
||||
* r2 | 0 | 0 | val | 0 |
|
||||
* +-----+-----+-----+-----+
|
||||
* r3 | 0 | 0 | 0 | val |
|
||||
* +-----+-----+-----+-----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_value(value: T) -> Mat4<T> {
|
||||
Matrix4::new(value, zero(), zero(), zero(),
|
||||
zero(), value, zero(), zero(),
|
||||
zero(), zero(), value, zero(),
|
||||
zero(), zero(), zero(), value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the multiplicative identity matrix
|
||||
* ~~~
|
||||
* c0 c1 c2 c3
|
||||
* +----+----+----+----+
|
||||
* r0 | 1 | 0 | 0 | 0 |
|
||||
* +----+----+----+----+
|
||||
* r1 | 0 | 1 | 0 | 0 |
|
||||
* +----+----+----+----+
|
||||
* r2 | 0 | 0 | 1 | 0 |
|
||||
* +----+----+----+----+
|
||||
* r3 | 0 | 0 | 0 | 1 |
|
||||
* +----+----+----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn identity() -> Mat4<T> {
|
||||
Matrix4::new( one::<T>(), zero::<T>(), zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), one::<T>(), zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>(), one::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>(), zero::<T>(), one::<T>())
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the additive identity matrix
|
||||
* ~~~
|
||||
* c0 c1 c2 c3
|
||||
* +----+----+----+----+
|
||||
* r0 | 0 | 0 | 0 | 0 |
|
||||
* +----+----+----+----+
|
||||
* r1 | 0 | 0 | 0 | 0 |
|
||||
* +----+----+----+----+
|
||||
* r2 | 0 | 0 | 0 | 0 |
|
||||
* +----+----+----+----+
|
||||
* r3 | 0 | 0 | 0 | 0 |
|
||||
* +----+----+----+----+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn zero() -> Mat4<T> {
|
||||
Matrix4::new(zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>(),
|
||||
zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_t(&self, value: T) -> Mat4<T> {
|
||||
Matrix4::from_cols(self[0].mul_t(value),
|
||||
self[1].mul_t(value),
|
||||
self[2].mul_t(value),
|
||||
self[3].mul_t(value))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_v(&self, vec: &Vec4<T>) -> Vec4<T> {
|
||||
Vector4::new(self.row(0).dot(vec),
|
||||
self.row(1).dot(vec),
|
||||
self.row(2).dot(vec),
|
||||
self.row(3).dot(vec))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn add_m(&self, other: &Mat4<T>) -> Mat4<T> {
|
||||
Matrix4::from_cols(self[0].add_v(&other[0]),
|
||||
self[1].add_v(&other[1]),
|
||||
self[2].add_v(&other[2]),
|
||||
self[3].add_v(&other[3]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn sub_m(&self, other: &Mat4<T>) -> Mat4<T> {
|
||||
Matrix4::from_cols(self[0].sub_v(&other[0]),
|
||||
self[1].sub_v(&other[1]),
|
||||
self[2].sub_v(&other[2]),
|
||||
self[3].sub_v(&other[3]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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)))
|
||||
|
||||
}
|
||||
|
||||
fn dot(&self, other: &Mat4<T>) -> T {
|
||||
other.transpose().mul_m(self).trace()
|
||||
}
|
||||
|
||||
fn determinant(&self) -> T {
|
||||
let m0: Mat3<T> = Matrix3::new(self[1][1], self[2][1], self[3][1],
|
||||
self[1][2], self[2][2], self[3][2],
|
||||
self[1][3], self[2][3], self[3][3]);
|
||||
let m1: Mat3<T> = Matrix3::new(self[0][1], self[2][1], self[3][1],
|
||||
self[0][2], self[2][2], self[3][2],
|
||||
self[0][3], self[2][3], self[3][3]);
|
||||
let m2: Mat3<T> = Matrix3::new(self[0][1], self[1][1], self[3][1],
|
||||
self[0][2], self[1][2], self[3][2],
|
||||
self[0][3], self[1][3], self[3][3]);
|
||||
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() -
|
||||
self[3][0] * m3.determinant()
|
||||
}
|
||||
|
||||
fn trace(&self) -> T {
|
||||
self[0][0] + self[1][1] + self[2][2] + self[3][3]
|
||||
}
|
||||
|
||||
fn inverse(&self) -> Option<Mat4<T>> {
|
||||
let d = self.determinant();
|
||||
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;
|
||||
for uint::range(j + 1, 4) |i| {
|
||||
if abs(A[j][i]) > abs(A[j][i1]) {
|
||||
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| {
|
||||
if i != j {
|
||||
I.col_mut(i).sub_self_v(&I[j].mul_t(A[i][j]));
|
||||
A.col_mut(i).sub_self_v(&A[j].mul_t(A[i][j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(I)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn transpose(&self) -> Mat4<T> {
|
||||
Matrix4::new(self[0][0], self[1][0], self[2][0], self[3][0],
|
||||
self[0][1], self[1][1], self[2][1], self[3][1],
|
||||
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 {
|
||||
self.fuzzy_eq(&Matrix::identity())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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)]
|
||||
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[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])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn is_invertible(&self) -> bool {
|
||||
!self.determinant().fuzzy_eq(&zero())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_ptr(&self) -> *T {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `c0r0`, `c0r1`, `c0r2`, `c0r3` - the first column of the matrix
|
||||
* * `c1r0`, `c1r1`, `c1r2`, `c1r3` - the second column of the matrix
|
||||
* * `c2r0`, `c2r1`, `c2r2`, `c2r3` - the third column of the matrix
|
||||
* * `c3r0`, `c3r1`, `c3r2`, `c3r3` - the fourth column of the matrix
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1 c2 c3
|
||||
* +------+------+------+------+
|
||||
* r0 | c0r0 | c1r0 | c2r0 | c3r0 |
|
||||
* +------+------+------+------+
|
||||
* r1 | c0r1 | c1r1 | c2r1 | c3r1 |
|
||||
* +------+------+------+------+
|
||||
* r2 | c0r2 | c1r2 | c2r2 | c3r2 |
|
||||
* +------+------+------+------+
|
||||
* r3 | c0r3 | c1r3 | c2r3 | c3r3 |
|
||||
* +------+------+------+------+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn new(c0r0: T, c0r1: T, c0r2: T, c0r3: T,
|
||||
c1r0: T, c1r1: T, c1r2: T, c1r3: T,
|
||||
c2r0: T, c2r1: T, c2r2: T, c2r3: T,
|
||||
c3r0: T, c3r1: T, c3r2: T, c3r3: T) -> Mat4<T> {
|
||||
Matrix4::from_cols(Vector4::new::<T,Vec4<T>>(c0r0, c0r1, c0r2, c0r3),
|
||||
Vector4::new::<T,Vec4<T>>(c1r0, c1r1, c1r2, c1r3),
|
||||
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
|
||||
*
|
||||
* # Arguments
|
||||
*
|
||||
* * `c0` - the first column vector of the matrix
|
||||
* * `c1` - the second column vector of the matrix
|
||||
* * `c2` - the third column vector of the matrix
|
||||
* * `c3` - the fourth column vector of the matrix
|
||||
*
|
||||
* ~~~
|
||||
* c0 c1 c2 c3
|
||||
* +------+------+------+------+
|
||||
* r0 | c0.x | c1.x | c2.x | c3.x |
|
||||
* +------+------+------+------+
|
||||
* r1 | c0.y | c1.y | c2.y | c3.y |
|
||||
* +------+------+------+------+
|
||||
* r2 | c0.z | c1.z | c2.z | c3.z |
|
||||
* +------+------+------+------+
|
||||
* r3 | c0.w | c1.w | c2.w | c3.w |
|
||||
* +------+------+------+------+
|
||||
* ~~~
|
||||
*/
|
||||
#[inline(always)]
|
||||
fn from_cols(c0: Vec4<T>,
|
||||
c1: Vec4<T>,
|
||||
c2: Vec4<T>,
|
||||
c3: Vec4<T>) -> Mat4<T> {
|
||||
Mat4 { x: c0, y: c1, z: c2, w: c3 }
|
||||
}
|
||||
}
|
||||
|
||||
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> {
|
||||
Matrix4::from_cols(-self[0], -self[1], -self[2], -self[3])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy> Index<uint, Vec4<T>> for Mat4<T> {
|
||||
#[inline(always)]
|
||||
fn index(&self, i: &uint) -> Vec4<T> {
|
||||
unsafe { do vec::raw::buf_as_slice(cast::transmute(self), 4) |slice| { slice[*i] } }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Mat4<T> {
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq(&self, other: &Mat4<T>) -> bool {
|
||||
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq_eps(&self, other: &Mat4<T>, epsilon: &T) -> bool {
|
||||
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
|
||||
self[1].fuzzy_eq_eps(&other[1], epsilon) &&
|
||||
self[2].fuzzy_eq_eps(&other[2], epsilon) &&
|
||||
self[3].fuzzy_eq_eps(&other[3], epsilon)
|
||||
}
|
||||
}
|
||||
|
||||
// GLSL-style type aliases, corresponding to Section 4.1.6 of the [GLSL 4.30.6 specification]
|
||||
// (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
|
||||
|
||||
/// a 4×4 single-precision floating-point matrix
|
||||
pub type mat4 = Mat4<f32>;
|
||||
/// a 4×4 double-precision floating-point matrix
|
||||
pub type dmat4 = Mat4<f64>;
|
||||
|
||||
// Static method wrappers for GLSL-style types
|
||||
|
||||
pub impl mat4 {
|
||||
#[inline(always)] 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)] fn from_cols(c0: vec4, c1: vec4, c2: vec4, c3: vec4)
|
||||
-> mat4 { Matrix4::from_cols(c0, c1, c2, c3) }
|
||||
#[inline(always)] fn from_value(v: f32) -> mat4 { Matrix::from_value(v) }
|
||||
|
||||
#[inline(always)] fn identity() -> mat4 { Matrix::identity() }
|
||||
#[inline(always)] fn zero() -> mat4 { Matrix::zero() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 4 }
|
||||
#[inline(always)] fn rows() -> uint { 4 }
|
||||
#[inline(always)] fn cols() -> uint { 4 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<mat4>() }
|
||||
}
|
||||
|
||||
pub impl dmat4 {
|
||||
#[inline(always)] 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)] fn from_cols(c0: dvec4, c1: dvec4, c2: dvec4, c3: dvec4)
|
||||
-> dmat4 { Matrix4::from_cols(c0, c1, c2, c3) }
|
||||
#[inline(always)] fn from_value(v: f64) -> dmat4 { Matrix::from_value(v) }
|
||||
|
||||
#[inline(always)] fn identity() -> dmat4 { Matrix::identity() }
|
||||
#[inline(always)] fn zero() -> dmat4 { Matrix::zero() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 4 }
|
||||
#[inline(always)] fn rows() -> uint { 4 }
|
||||
#[inline(always)] fn cols() -> uint { 4 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<dmat4>() }
|
||||
}
|
1297
src/vec.rs
1297
src/vec.rs
File diff suppressed because it is too large
Load diff
424
src/vec2.rs
424
src/vec2.rs
|
@ -1,424 +0,0 @@
|
|||
use std::cmp::{FuzzyEq, FUZZY_EPSILON};
|
||||
use numeric::*;
|
||||
use numeric::number::Number;
|
||||
use numeric::number::Number::{zero,one};
|
||||
|
||||
use vec::{
|
||||
Vec3,
|
||||
Vector,
|
||||
Vector2,
|
||||
Vector3,
|
||||
NumericVector,
|
||||
NumericVector2,
|
||||
ToHomogeneous,
|
||||
EuclideanVector,
|
||||
EquableVector,
|
||||
OrdinalVector,
|
||||
BooleanVector,
|
||||
};
|
||||
|
||||
/**
|
||||
* A 2-dimensional vector
|
||||
*
|
||||
* # Type parameters
|
||||
*
|
||||
* * `T` - The type of the components. This is intended to support boolean,
|
||||
* integer, unsigned integer, and floating point types.
|
||||
*
|
||||
* # Fields
|
||||
*
|
||||
* * `x` - the first component of the vector
|
||||
* * `y` - the second component of the vector
|
||||
*/
|
||||
#[deriving(Eq)]
|
||||
pub struct Vec2<T> { x: T, y: T }
|
||||
|
||||
impl<T:Copy + Eq> Vector<T> for Vec2<T> {
|
||||
#[inline(always)]
|
||||
fn from_value(value: T) -> Vec2<T> {
|
||||
Vector2::new(value, value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_ptr(&self) -> *T {
|
||||
unsafe { cast::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> {
|
||||
#[inline(always)]
|
||||
fn new(x: T, y: T ) -> Vec2<T> {
|
||||
Vec2 { x: x, y: y }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Eq> Index<uint, T> for Vec2<T> {
|
||||
#[inline(always)]
|
||||
fn index(&self, i: &uint) -> T {
|
||||
unsafe { do vec::raw::buf_as_slice(self.to_ptr(), 2) |slice| { slice[*i] } }
|
||||
}
|
||||
}
|
||||
|
||||
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> {
|
||||
Vector2::new(one::<T>(), one::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn zero() -> Vec2<T> {
|
||||
Vector2::new(zero::<T>(), zero::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn is_zero(&self) -> bool {
|
||||
self[0] == zero() &&
|
||||
self[1] == zero()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_t(&self, value: T) -> Vec2<T> {
|
||||
Vector2::new(self[0] * value,
|
||||
self[1] * value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_t(&self, value: T) -> Vec2<T> {
|
||||
Vector2::new(self[0] / value,
|
||||
self[1] / value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn add_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
||||
Vector2::new(self[0] + other[0],
|
||||
self[1] + other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn sub_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
||||
Vector2::new(self[0] - other[0],
|
||||
self[1] - other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
||||
Vector2::new(self[0] * other[0],
|
||||
self[1] * other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_v(&self, other: &Vec2<T>) -> Vec2<T> {
|
||||
Vector2::new(self[0] / other[0],
|
||||
self[1] / other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn dot(&self, other: &Vec2<T>) -> T {
|
||||
self[0] * other[0] +
|
||||
self[1] * other[1]
|
||||
}
|
||||
|
||||
#[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];
|
||||
*self.index_mut(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> ToHomogeneous<Vec3<T>> for Vec2<T> {
|
||||
#[inline(always)]
|
||||
fn to_homogeneous(&self) -> Vec3<T> {
|
||||
Vector3::new(self.x, self.y, zero())
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
fn length2(&self) -> T {
|
||||
self.dot(self)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn length(&self) -> T {
|
||||
self.length2().sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn distance2(&self, other: &Vec2<T>) -> T {
|
||||
other.sub_v(self).length2()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn distance(&self, other: &Vec2<T>) -> T {
|
||||
other.distance2(self).sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn angle(&self, other: &Vec2<T>) -> T {
|
||||
atan2(self.perp_dot(other), self.dot(other))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn normalize(&self) -> Vec2<T> {
|
||||
self.mul_t(one::<T>()/self.length())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn normalize_to(&self, length: T) -> Vec2<T> {
|
||||
self.mul_t(length / self.length())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn lerp(&self, other: &Vec2<T>, amount: T) -> Vec2<T> {
|
||||
self.add_v(&other.sub_v(self).mul_t(amount))
|
||||
}
|
||||
|
||||
#[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) {
|
||||
let v = other.sub_v(self).mul_t(amount);
|
||||
self.add_self_v(&v);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Vec2<T> {
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq(&self, other: &Vec2<T>) -> bool {
|
||||
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq_eps(&self, other: &Vec2<T>, epsilon: &T) -> bool {
|
||||
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
|
||||
self[1].fuzzy_eq_eps(&other[1], epsilon)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Ord + Eq> OrdinalVector<T, Vec2<bool>> for Vec2<T> {
|
||||
#[inline(always)]
|
||||
fn less_than(&self, other: &Vec2<T>) -> Vec2<bool> {
|
||||
Vector2::new(self[0] < other[0],
|
||||
self[1] < other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn less_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
||||
Vector2::new(self[0] <= other[0],
|
||||
self[1] <= other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn greater_than(&self, other: &Vec2<T>) -> Vec2<bool> {
|
||||
Vector2::new(self[0] > other[0],
|
||||
self[1] > other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn greater_than_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
||||
Vector2::new(self[0] >= other[0],
|
||||
self[1] >= other[1])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Eq> EquableVector<T, Vec2<bool>> for Vec2<T> {
|
||||
#[inline(always)]
|
||||
fn equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
||||
Vector2::new(self[0] == other[0],
|
||||
self[1] == other[1])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn not_equal(&self, other: &Vec2<T>) -> Vec2<bool> {
|
||||
Vector2::new(self[0] != other[0],
|
||||
self[1] != other[1])
|
||||
}
|
||||
}
|
||||
|
||||
impl BooleanVector for Vec2<bool> {
|
||||
#[inline(always)]
|
||||
fn any(&self) -> bool {
|
||||
self[0] || self[1]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn all(&self) -> bool {
|
||||
self[0] && self[1]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn not(&self) -> Vec2<bool> {
|
||||
Vector2::new(!self[0], !self[1])
|
||||
}
|
||||
}
|
||||
|
||||
// GLSL-style type aliases, corresponding to Section 4.1.5 of the [GLSL 4.30.6 specification]
|
||||
// (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
|
||||
|
||||
/// a two-component single-precision floating-point vector
|
||||
pub type vec2 = Vec2<f32>;
|
||||
/// a two-component double-precision floating-point vector
|
||||
pub type dvec2 = Vec2<f64>;
|
||||
/// a two-component Boolean vector
|
||||
pub type bvec2 = Vec2<bool>;
|
||||
/// a two-component signed integer vector
|
||||
pub type ivec2 = Vec2<i32>;
|
||||
/// a two-component unsigned integer vector
|
||||
pub type uvec2 = Vec2<u32>;
|
||||
|
||||
// Static method wrappers for GLSL-style types
|
||||
|
||||
pub impl vec2 {
|
||||
#[inline(always)] fn new(x: f32, y: f32) -> vec2 { Vector2::new(x, y) }
|
||||
#[inline(always)] fn from_value(v: f32) -> vec2 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> vec2 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> vec2 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> vec2 { NumericVector2::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> vec2 { NumericVector2::unit_y() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 2 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<vec2>() }
|
||||
}
|
||||
|
||||
pub impl dvec2 {
|
||||
#[inline(always)] fn new(x: f64, y: f64) -> dvec2 { Vector2::new(x, y) }
|
||||
#[inline(always)] fn from_value(v: f64) -> dvec2 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> dvec2 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> dvec2 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> dvec2 { NumericVector2::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> dvec2 { NumericVector2::unit_y() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 2 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<dvec2>() }
|
||||
}
|
||||
|
||||
pub impl bvec2 {
|
||||
#[inline(always)] fn new(x: bool, y: bool) -> bvec2 { Vector2::new(x, y) }
|
||||
#[inline(always)] fn from_value(v: bool) -> bvec2 { Vector::from_value(v) }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 2 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<bvec2>() }
|
||||
}
|
||||
|
||||
pub impl ivec2 {
|
||||
#[inline(always)] fn new(x: i32, y: i32) -> ivec2 { Vector2::new(x, y) }
|
||||
#[inline(always)] fn from_value(v: i32) -> ivec2 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> ivec2 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> ivec2 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> ivec2 { NumericVector2::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> ivec2 { NumericVector2::unit_y() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 2 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<ivec2>() }
|
||||
}
|
||||
|
||||
pub impl uvec2 {
|
||||
#[inline(always)] fn new(x: u32, y: u32) -> uvec2 { Vector2::new(x, y) }
|
||||
#[inline(always)] fn from_value(v: u32) -> uvec2 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> uvec2 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> uvec2 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> uvec2 { NumericVector2::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> uvec2 { NumericVector2::unit_y() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 2 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<uvec2>() }
|
||||
}
|
||||
|
||||
// Type aliases named in a more 'Rustic' style
|
||||
|
||||
pub type Vec2f = Vec2<float>;
|
||||
pub type Vec2f32 = Vec2<f32>;
|
||||
pub type Vec2f64 = Vec2<f64>;
|
||||
pub type Vec2i = Vec2<int>;
|
||||
pub type Vec2i8 = Vec2<i8>;
|
||||
pub type Vec2i16 = Vec2<i16>;
|
||||
pub type Vec2i32 = Vec2<i32>;
|
||||
pub type Vec2i64 = Vec2<i64>;
|
||||
pub type Vec2b = Vec2<bool>;
|
452
src/vec3.rs
452
src/vec3.rs
|
@ -1,452 +0,0 @@
|
|||
use std::cmp::{FuzzyEq, FUZZY_EPSILON};
|
||||
use numeric::*;
|
||||
use numeric::number::Number;
|
||||
use numeric::number::Number::{zero,one};
|
||||
|
||||
use vec::{
|
||||
Vec4,
|
||||
Vector,
|
||||
Vector3,
|
||||
Vector4,
|
||||
NumericVector,
|
||||
NumericVector3,
|
||||
ToHomogeneous,
|
||||
EuclideanVector,
|
||||
EquableVector,
|
||||
OrdinalVector,
|
||||
BooleanVector,
|
||||
};
|
||||
|
||||
/**
|
||||
* A 3-dimensional vector
|
||||
*
|
||||
* # Type parameters
|
||||
*
|
||||
* * `T` - The type of the components. This is intended to support boolean,
|
||||
* integer, unsigned integer, and floating point types.
|
||||
*
|
||||
* # Fields
|
||||
*
|
||||
* * `x` - the first component of the vector
|
||||
* * `y` - the second component of the vector
|
||||
* * `z` - the third component of the vector
|
||||
*/
|
||||
#[deriving(Eq)]
|
||||
pub struct Vec3<T> { x: T, y: T, z: T }
|
||||
|
||||
impl<T:Copy + Eq> Vector<T> for Vec3<T> {
|
||||
#[inline(always)]
|
||||
fn from_value(value: T) -> Vec3<T> {
|
||||
Vector3::new(value, value, value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_ptr(&self) -> *T {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn index_mut(&mut self, i: uint) -> &'self mut 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(&mut self, a: uint, b: uint) {
|
||||
*self.index_mut(a) <-> *self.index_mut(b);
|
||||
}
|
||||
}
|
||||
|
||||
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 vec::raw::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> {
|
||||
Vector3::new(one::<T>(), one::<T>(), one::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn zero() -> Vec3<T> {
|
||||
Vector3::new(zero::<T>(), zero::<T>(), zero::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn is_zero(&self) -> bool {
|
||||
self[0] == zero() &&
|
||||
self[1] == zero() &&
|
||||
self[2] == zero()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_t(&self, value: T) -> Vec3<T> {
|
||||
Vector3::new(self[0] * value,
|
||||
self[1] * value,
|
||||
self[2] * value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_t(&self, value: T) -> Vec3<T> {
|
||||
Vector3::new(self[0] / value,
|
||||
self[1] / value,
|
||||
self[2] / value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
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)]
|
||||
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)]
|
||||
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)]
|
||||
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)]
|
||||
fn dot(&self, other: &Vec3<T>) -> T {
|
||||
self[0] * other[0] +
|
||||
self[1] * other[1] +
|
||||
self[2] * other[2]
|
||||
}
|
||||
|
||||
#[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];
|
||||
*self.index_mut(1) /= other[1];
|
||||
*self.index_mut(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]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn cross_self(&mut self, other: &Vec3<T>) {
|
||||
*self = self.cross(other);
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
fn to_homogeneous(&self) -> Vec4<T> {
|
||||
Vector4::new(self.x, self.y, self.z, zero())
|
||||
}
|
||||
}
|
||||
|
||||
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)]
|
||||
fn length2(&self) -> T {
|
||||
self.dot(self)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn length(&self) -> T {
|
||||
self.length2().sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn distance2(&self, other: &Vec3<T>) -> T {
|
||||
other.sub_v(self).length2()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn distance(&self, other: &Vec3<T>) -> T {
|
||||
other.distance2(self).sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn angle(&self, other: &Vec3<T>) -> T {
|
||||
atan2(self.cross(other).length(), self.dot(other))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn normalize(&self) -> Vec3<T> {
|
||||
self.mul_t(one::<T>()/self.length())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn normalize_to(&self, length: T) -> Vec3<T> {
|
||||
self.mul_t(length / self.length())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn lerp(&self, other: &Vec3<T>, amount: T) -> Vec3<T> {
|
||||
self.add_v(&other.sub_v(self).mul_t(amount))
|
||||
}
|
||||
|
||||
#[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) {
|
||||
let v = other.sub_v(self).mul_t(amount);
|
||||
self.add_self_v(&v);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Vec3<T> {
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq(&self, other: &Vec3<T>) -> bool {
|
||||
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq_eps(&self, other: &Vec3<T>, epsilon: &T) -> bool {
|
||||
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
|
||||
self[1].fuzzy_eq_eps(&other[1], epsilon) &&
|
||||
self[2].fuzzy_eq_eps(&other[2], epsilon)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Ord + Eq> OrdinalVector<T, Vec3<bool>> for Vec3<T> {
|
||||
#[inline(always)]
|
||||
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)]
|
||||
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)]
|
||||
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)]
|
||||
fn greater_than_equal(&self, other: &Vec3<T>) -> Vec3<bool> {
|
||||
Vector3::new(self[0] >= other[0],
|
||||
self[1] >= other[1],
|
||||
self[2] >= other[2])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Eq> EquableVector<T, Vec3<bool>> for Vec3<T> {
|
||||
#[inline(always)]
|
||||
fn equal(&self, other: &Vec3<T>) -> Vec3<bool> {
|
||||
Vector3::new(self[0] == other[0],
|
||||
self[1] == other[1],
|
||||
self[2] == other[2])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn not_equal(&self, other: &Vec3<T>) -> Vec3<bool> {
|
||||
Vector3::new(self[0] != other[0],
|
||||
self[1] != other[1],
|
||||
self[2] != other[2])
|
||||
}
|
||||
}
|
||||
|
||||
impl BooleanVector for Vec3<bool> {
|
||||
#[inline(always)]
|
||||
fn any(&self) -> bool {
|
||||
self[0] || self[1] || self[2]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn all(&self) -> bool {
|
||||
self[0] && self[1] && self[2]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn not(&self) -> Vec3<bool> {
|
||||
Vector3::new(!self[0], !self[1], !self[2])
|
||||
}
|
||||
}
|
||||
|
||||
// GLSL-style type aliases, corresponding to Section 4.1.5 of the [GLSL 4.30.6 specification]
|
||||
// (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
|
||||
|
||||
/// a three-component single-precision floating-point vector
|
||||
pub type vec3 = Vec3<f32>;
|
||||
/// a three-component double-precision floating-point vector
|
||||
pub type dvec3 = Vec3<f64>;
|
||||
/// a three-component Boolean vector
|
||||
pub type bvec3 = Vec3<bool>;
|
||||
/// a three-component signed integer vector
|
||||
pub type ivec3 = Vec3<i32>;
|
||||
/// a three-component unsigned integer vector
|
||||
pub type uvec3 = Vec3<u32>;
|
||||
|
||||
// Static method wrappers for GLSL-style types
|
||||
|
||||
pub impl vec3 {
|
||||
#[inline(always)] fn new(x: f32, y: f32, z: f32) -> vec3 { Vector3::new(x, y, z) }
|
||||
#[inline(always)] fn from_value(v: f32) -> vec3 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> vec3 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> vec3 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> vec3 { NumericVector3::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> vec3 { NumericVector3::unit_y() }
|
||||
#[inline(always)] fn unit_z() -> vec3 { NumericVector3::unit_z() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 3 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<vec3>() }
|
||||
}
|
||||
|
||||
pub impl dvec3 {
|
||||
#[inline(always)] fn new(x: f64, y: f64, z: f64) -> dvec3 { Vector3::new(x, y, z) }
|
||||
#[inline(always)] fn from_value(v: f64) -> dvec3 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> dvec3 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> dvec3 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> dvec3 { NumericVector3::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> dvec3 { NumericVector3::unit_y() }
|
||||
#[inline(always)] fn unit_z() -> dvec3 { NumericVector3::unit_z() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 3 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<dvec3>() }
|
||||
}
|
||||
|
||||
pub impl bvec3 {
|
||||
#[inline(always)] fn new(x: bool, y: bool, z: bool) -> bvec3 { Vector3::new(x, y, z) }
|
||||
#[inline(always)] fn from_value(v: bool) -> bvec3 { Vector::from_value(v) }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 3 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<bvec3>() }
|
||||
}
|
||||
|
||||
pub impl ivec3 {
|
||||
#[inline(always)] fn new(x: i32, y: i32, z: i32) -> ivec3 { Vector3::new(x, y, z) }
|
||||
#[inline(always)] fn from_value(v: i32) -> ivec3 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> ivec3 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> ivec3 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> ivec3 { NumericVector3::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> ivec3 { NumericVector3::unit_y() }
|
||||
#[inline(always)] fn unit_z() -> ivec3 { NumericVector3::unit_z() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 3 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<ivec3>() }
|
||||
}
|
||||
|
||||
pub impl uvec3 {
|
||||
#[inline(always)] fn new(x: u32, y: u32, z: u32) -> uvec3 { Vector3::new(x, y, z) }
|
||||
#[inline(always)] fn from_value(v: u32) -> uvec3 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> uvec3 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> uvec3 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> uvec3 { NumericVector3::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> uvec3 { NumericVector3::unit_y() }
|
||||
#[inline(always)] fn unit_z() -> uvec3 { NumericVector3::unit_z() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 3 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<uvec3>() }
|
||||
}
|
464
src/vec4.rs
464
src/vec4.rs
|
@ -1,464 +0,0 @@
|
|||
use std::cmp::{FuzzyEq, FUZZY_EPSILON};
|
||||
use numeric::*;
|
||||
use numeric::number::Number;
|
||||
use numeric::number::Number::{zero,one};
|
||||
|
||||
use vec::{
|
||||
Vector,
|
||||
Vector4,
|
||||
NumericVector,
|
||||
NumericVector4,
|
||||
EuclideanVector,
|
||||
EquableVector,
|
||||
OrdinalVector,
|
||||
BooleanVector,
|
||||
};
|
||||
|
||||
/**
|
||||
* A 4-dimensional vector
|
||||
*
|
||||
* # Type parameters
|
||||
*
|
||||
* * `T` - The type of the components. This is intended to support boolean,
|
||||
* integer, unsigned integer, and floating point types.
|
||||
*
|
||||
* # Fields
|
||||
*
|
||||
* * `x` - the first component of the vector
|
||||
* * `y` - the second component of the vector
|
||||
* * `z` - the third component of the vector
|
||||
* * `w` - the fourth component of the vector
|
||||
*/
|
||||
#[deriving(Eq)]
|
||||
pub struct Vec4<T> { x: T, y: T, z: T, w: T }
|
||||
|
||||
impl<T:Copy + Eq> Vector<T> for Vec4<T> {
|
||||
#[inline(always)]
|
||||
fn from_value(value: T) -> Vec4<T> {
|
||||
Vector4::new(value, value, value, value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn to_ptr(&self) -> *T {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn index_mut(&mut self, i: uint) -> &'self mut 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(&mut self, a: uint, b: uint) {
|
||||
*self.index_mut(a) <-> *self.index_mut(b);
|
||||
}
|
||||
}
|
||||
|
||||
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 vec::raw::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> {
|
||||
Vector4::new(one::<T>(), one::<T>(), one::<T>(), one::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn zero() -> Vec4<T> {
|
||||
Vector4::new(zero::<T>(), zero::<T>(), zero::<T>(), zero::<T>())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn is_zero(&self) -> bool {
|
||||
self[0] == zero() &&
|
||||
self[1] == zero() &&
|
||||
self[2] == zero() &&
|
||||
self[3] == zero()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_t(&self, value: T) -> Vec4<T> {
|
||||
Vector4::new(self[0] * value,
|
||||
self[1] * value,
|
||||
self[2] * value,
|
||||
self[3] * value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_t(&self, value: T) -> Vec4<T> {
|
||||
Vector4::new(self[0] / value,
|
||||
self[1] / value,
|
||||
self[2] / value,
|
||||
self[3] / value)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn add_v(&self, other: &Vec4<T>) -> Vec4<T> {
|
||||
Vector4::new(self[0] + other[0],
|
||||
self[1] + other[1],
|
||||
self[2] + other[2],
|
||||
self[3] + other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn sub_v(&self, other: &Vec4<T>) -> Vec4<T> {
|
||||
Vector4::new(self[0] - other[0],
|
||||
self[1] - other[1],
|
||||
self[2] - other[2],
|
||||
self[3] - other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn mul_v(&self, other: &Vec4<T>) -> Vec4<T> {
|
||||
Vector4::new(self[0] * other[0],
|
||||
self[1] * other[1],
|
||||
self[2] * other[2],
|
||||
self[3] * other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn div_v(&self, other: &Vec4<T>) -> Vec4<T> {
|
||||
Vector4::new(self[0] / other[0],
|
||||
self[1] / other[1],
|
||||
self[2] / other[2],
|
||||
self[3] / other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn dot(&self, other: &Vec4<T>) -> T {
|
||||
self[0] * other[0] +
|
||||
self[1] * other[1] +
|
||||
self[2] * other[2] +
|
||||
self[3] * other[3]
|
||||
}
|
||||
|
||||
#[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);
|
||||
*self.index_mut(3) = -*self.index_mut(3);
|
||||
}
|
||||
|
||||
#[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;
|
||||
*self.index_mut(3) *= 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;
|
||||
*self.index_mut(3) /= value;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn add_self_v(&mut self, other: &Vec4<T>) {
|
||||
*self.index_mut(0) += other[0];
|
||||
*self.index_mut(1) += other[1];
|
||||
*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];
|
||||
*self.index_mut(1) -= other[1];
|
||||
*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];
|
||||
*self.index_mut(1) *= other[1];
|
||||
*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];
|
||||
*self.index_mut(1) /= other[1];
|
||||
*self.index_mut(2) /= other[2];
|
||||
*self.index_mut(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<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 {
|
||||
self.dot(self)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn length(&self) -> T {
|
||||
self.length2().sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn distance2(&self, other: &Vec4<T>) -> T {
|
||||
other.sub_v(self).length2()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn distance(&self, other: &Vec4<T>) -> T {
|
||||
other.distance2(self).sqrt()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn angle(&self, other: &Vec4<T>) -> T {
|
||||
acos(self.dot(other) / (self.length() * other.length()))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn normalize(&self) -> Vec4<T> {
|
||||
self.mul_t(one::<T>()/self.length())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn normalize_to(&self, length: T) -> Vec4<T> {
|
||||
self.mul_t(length / self.length())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn lerp(&self, other: &Vec4<T>, amount: T) -> Vec4<T> {
|
||||
self.add_v(&other.sub_v(self).mul_t(amount))
|
||||
}
|
||||
|
||||
#[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) {
|
||||
let v = other.sub_v(self).mul_t(amount);
|
||||
self.add_self_v(&v);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Float + FuzzyEq<T>> FuzzyEq<T> for Vec4<T> {
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq(&self, other: &Vec4<T>) -> bool {
|
||||
self.fuzzy_eq_eps(other, &Number::from(FUZZY_EPSILON))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn fuzzy_eq_eps(&self, other: &Vec4<T>, epsilon: &T) -> bool {
|
||||
self[0].fuzzy_eq_eps(&other[0], epsilon) &&
|
||||
self[1].fuzzy_eq_eps(&other[1], epsilon) &&
|
||||
self[2].fuzzy_eq_eps(&other[2], epsilon) &&
|
||||
self[3].fuzzy_eq_eps(&other[3], epsilon)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Ord + Eq> OrdinalVector<T, Vec4<bool>> for Vec4<T> {
|
||||
#[inline(always)]
|
||||
fn less_than(&self, other: &Vec4<T>) -> Vec4<bool> {
|
||||
Vector4::new(self[0] < other[0],
|
||||
self[1] < other[1],
|
||||
self[2] < other[2],
|
||||
self[3] < other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn less_than_equal(&self, other: &Vec4<T>) -> Vec4<bool> {
|
||||
Vector4::new(self[0] <= other[0],
|
||||
self[1] <= other[1],
|
||||
self[2] <= other[2],
|
||||
self[3] <= other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn greater_than(&self, other: &Vec4<T>) -> Vec4<bool> {
|
||||
Vector4::new(self[0] > other[0],
|
||||
self[1] > other[1],
|
||||
self[2] > other[2],
|
||||
self[3] > other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn greater_than_equal(&self, other: &Vec4<T>) -> Vec4<bool> {
|
||||
Vector4::new(self[0] >= other[0],
|
||||
self[1] >= other[1],
|
||||
self[2] >= other[2],
|
||||
self[3] >= other[3])
|
||||
}
|
||||
}
|
||||
|
||||
impl<T:Copy + Eq> EquableVector<T, Vec4<bool>> for Vec4<T> {
|
||||
#[inline(always)]
|
||||
fn equal(&self, other: &Vec4<T>) -> Vec4<bool> {
|
||||
Vector4::new(self[0] == other[0],
|
||||
self[1] == other[1],
|
||||
self[2] == other[2],
|
||||
self[3] == other[3])
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn not_equal(&self, other: &Vec4<T>) -> Vec4<bool> {
|
||||
Vector4::new(self[0] != other[0],
|
||||
self[1] != other[1],
|
||||
self[2] != other[2],
|
||||
self[3] != other[3])
|
||||
}
|
||||
}
|
||||
|
||||
impl BooleanVector for Vec4<bool> {
|
||||
#[inline(always)]
|
||||
fn any(&self) -> bool {
|
||||
self[0] || self[1] || self[2] || self[3]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn all(&self) -> bool {
|
||||
self[0] && self[1] && self[2] && self[3]
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn not(&self) -> Vec4<bool> {
|
||||
Vector4::new(!self[0], !self[1], !self[2], !self[3])
|
||||
}
|
||||
}
|
||||
|
||||
// GLSL-style type aliases, corresponding to Section 4.1.5 of the [GLSL 4.30.6 specification]
|
||||
// (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf).
|
||||
|
||||
// a four-component single-precision floating-point vector
|
||||
pub type vec4 = Vec4<f32>;
|
||||
// a four-component double-precision floating-point vector
|
||||
pub type dvec4 = Vec4<f64>;
|
||||
// a four-component Boolean vector
|
||||
pub type bvec4 = Vec4<bool>;
|
||||
// a four-component signed integer vector
|
||||
pub type ivec4 = Vec4<i32>;
|
||||
// a four-component unsigned integer vector
|
||||
pub type uvec4 = Vec4<u32>;
|
||||
|
||||
// Static method wrappers for GLSL-style types
|
||||
|
||||
pub impl vec4 {
|
||||
#[inline(always)] fn new(x: f32, y: f32, z: f32, w: f32) -> vec4 { Vector4::new(x, y, z, w) }
|
||||
#[inline(always)] fn from_value(v: f32) -> vec4 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> vec4 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> vec4 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> vec4 { NumericVector4::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> vec4 { NumericVector4::unit_y() }
|
||||
#[inline(always)] fn unit_z() -> vec4 { NumericVector4::unit_z() }
|
||||
#[inline(always)] fn unit_w() -> vec4 { NumericVector4::unit_w() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 4 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<vec4>() }
|
||||
}
|
||||
|
||||
pub impl dvec4 {
|
||||
#[inline(always)] fn new(x: f64, y: f64, z: f64, w: f64) -> dvec4 { Vector4::new(x, y, z, w) }
|
||||
#[inline(always)] fn from_value(v: f64) -> dvec4 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> dvec4 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> dvec4 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> dvec4 { NumericVector4::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> dvec4 { NumericVector4::unit_y() }
|
||||
#[inline(always)] fn unit_z() -> dvec4 { NumericVector4::unit_z() }
|
||||
#[inline(always)] fn unit_w() -> dvec4 { NumericVector4::unit_w() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 4 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<dvec4>() }
|
||||
}
|
||||
|
||||
|
||||
pub impl bvec4 {
|
||||
#[inline(always)] fn new(x: bool, y: bool, z: bool, w: bool) -> bvec4 { Vector4::new(x, y, z, w) }
|
||||
#[inline(always)] fn from_value(v: bool) -> bvec4 { Vector::from_value(v) }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 4 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<bvec4>() }
|
||||
}
|
||||
|
||||
pub impl ivec4 {
|
||||
#[inline(always)] fn new(x: i32, y: i32, z: i32, w: i32) -> ivec4 { Vector4::new(x, y, z, w) }
|
||||
#[inline(always)] fn from_value(v: i32) -> ivec4 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> ivec4 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> ivec4 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> ivec4 { NumericVector4::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> ivec4 { NumericVector4::unit_y() }
|
||||
#[inline(always)] fn unit_z() -> ivec4 { NumericVector4::unit_z() }
|
||||
#[inline(always)] fn unit_w() -> ivec4 { NumericVector4::unit_w() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 4 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<ivec4>() }
|
||||
}
|
||||
|
||||
pub impl uvec4 {
|
||||
#[inline(always)] fn new(x: u32, y: u32, z: u32, w: u32) -> uvec4 { Vector4::new(x, y, z, w) }
|
||||
#[inline(always)] fn from_value(v: u32) -> uvec4 { Vector::from_value(v) }
|
||||
#[inline(always)] fn identity() -> uvec4 { NumericVector::identity() }
|
||||
#[inline(always)] fn zero() -> uvec4 { NumericVector::zero() }
|
||||
|
||||
#[inline(always)] fn unit_x() -> uvec4 { NumericVector4::unit_x() }
|
||||
#[inline(always)] fn unit_y() -> uvec4 { NumericVector4::unit_y() }
|
||||
#[inline(always)] fn unit_z() -> uvec4 { NumericVector4::unit_z() }
|
||||
#[inline(always)] fn unit_w() -> uvec4 { NumericVector4::unit_w() }
|
||||
|
||||
#[inline(always)] fn dim() -> uint { 4 }
|
||||
#[inline(always)] fn size_of() -> uint { sys::size_of::<uvec4>() }
|
||||
}
|
Loading…
Reference in a new issue