From 5ce765367a8abd3192f38073245661d6c002ad50 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 13 Dec 2012 23:01:42 +1000 Subject: [PATCH] Split up vec and mat modules --- src/mat.rs | 1441 +-------------------------------------------------- src/mat2.rs | 381 ++++++++++++++ src/mat3.rs | 504 ++++++++++++++++++ src/mat4.rs | 551 ++++++++++++++++++++ src/vec.rs | 794 +--------------------------- src/vec2.rs | 248 +++++++++ src/vec3.rs | 273 ++++++++++ src/vec4.rs | 273 ++++++++++ 8 files changed, 2255 insertions(+), 2210 deletions(-) create mode 100644 src/mat2.rs create mode 100644 src/mat3.rs create mode 100644 src/mat4.rs create mode 100644 src/vec2.rs create mode 100644 src/vec3.rs create mode 100644 src/vec4.rs diff --git a/src/mat.rs b/src/mat.rs index ca9d79d..fb38945 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -1,18 +1,17 @@ -use core::cast::transmute; -use core::cmp::{Eq, Ord}; -use core::ptr::to_unsafe_ptr; -use core::sys::size_of; -use core::vec::raw::buf_as_slice; +use core::cmp::Eq; use std::cmp::FuzzyEq; use angle::Angle; -use funs::common::*; -use funs::exponential::*; -use funs::triganomic::{sin, cos}; -use num::types::{Float, Number}; -use quat::{Quat, ToQuat}; -use vec::{NumericVector, Vec2, Vec3, Vec4}; +use quat::ToQuat; + +pub mod mat2; +pub mod mat3; +pub mod mat4; + +pub use mat2::Mat2; +pub use mat3::Mat3; +pub use mat4::Mat4; /** * The base square matrix trait @@ -258,8 +257,8 @@ pub trait Matrix2: Matrix { /** * A 3 x 3 matrix */ -pub trait Matrix3: Matrix { - static pure fn from_axis_angle>(axis: &Vec3, theta: A) -> Mat3; +pub trait Matrix3: Matrix ToQuat { + static pure fn from_axis_angle>(axis: &V, theta: A) -> Mat3; pure fn to_mat4(&self) -> Mat4; } @@ -267,1418 +266,4 @@ pub trait Matrix3: Matrix { * A 4 x 4 matrix */ pub trait Matrix4: Matrix { -} - - - - - - -/** - * 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 - */ -pub struct Mat2 { x: Vec2, y: Vec2 } - -pub impl Mat2 { - /** - * 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)] - static pure fn new(c0r0: T, c0r1: T, - c1r0: T, c1r1: T) -> Mat2 { - Mat2::from_cols(Vec2::new(move c0r0, move c0r1), - Vec2::new(move c1r0, move 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)] - static pure fn from_cols(c0: Vec2, c1: Vec2) -> Mat2 { - Mat2 { x: move c0, - y: move c1 } - } - - /** - * 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)] - static pure fn from_value(value: T) -> Mat2 { - let _0 = Number::from(0); - Mat2::new(value, _0, - _0, value) - } - - // FIXME: An interim solution to the issues with static functions - #[inline(always)] - static pure fn identity() -> Mat2 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat2::new(_1, _0, - _0, _1) - } - - // FIXME: An interim solution to the issues with static functions - #[inline(always)] - static pure fn zero() -> Mat2 { - let _0 = Number::from(0); - Mat2::new(_0, _0, - _0, _0) - } -} - -pub impl Mat2: Matrix> { - #[inline(always)] - pure fn col(&self, i: uint) -> Vec2 { self[i] } - - #[inline(always)] - pure fn row(&self, i: uint) -> Vec2 { - Vec2::new(self[0][i], - self[1][i]) - } - - /** - * Returns the multiplicative identity matrix - * ~~~ - * c0 c1 - * +----+----+ - * r0 | 1 | 0 | - * +----+----+ - * r1 | 0 | 1 | - * +----+----+ - * ~~~ - */ - #[inline(always)] - static pure fn identity() -> Mat2 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat2::new(_1, _0, - _0, _1) - } - - /** - * Returns the additive identity matrix - * ~~~ - * c0 c1 - * +----+----+ - * r0 | 0 | 0 | - * +----+----+ - * r1 | 0 | 0 | - * +----+----+ - * ~~~ - */ - #[inline(always)] - static pure fn zero() -> Mat2 { - let _0 = Number::from(0); - Mat2::new(_0, _0, - _0, _0) - } - - #[inline(always)] - pure fn mul_t(&self, value: T) -> Mat2 { - Mat2::from_cols(self[0].mul_t(value), - self[1].mul_t(value)) - } - - #[inline(always)] - pure fn mul_v(&self, vec: &Vec2) -> Vec2 { - Vec2::new(self.row(0).dot(vec), - self.row(1).dot(vec)) - } - - #[inline(always)] - pure fn add_m(&self, other: &Mat2) -> Mat2 { - Mat2::from_cols(self[0].add_v(&other[0]), - self[1].add_v(&other[1])) - } - - #[inline(always)] - pure fn sub_m(&self, other: &Mat2) -> Mat2 { - Mat2::from_cols(self[0].sub_v(&other[0]), - self[1].sub_v(&other[1])) - } - - #[inline(always)] - pure fn mul_m(&self, other: &Mat2) -> Mat2 { - Mat2::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))) - } - - pure fn dot(&self, other: &Mat2) -> T { - other.transpose().mul_m(self).trace() - } - - pure fn determinant(&self) -> T { - self[0][0] * self[1][1] - self[1][0] * self[0][1] - } - - pure fn trace(&self) -> T { - self[0][0] + self[1][1] - } - - #[inline(always)] - pure fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.fuzzy_eq(&Number::from(0)) { - None - } else { - Some(Mat2::new( self[1][1]/d, -self[0][1]/d, - -self[1][0]/d, self[0][0]/d)) - } - } - - #[inline(always)] - pure fn transpose(&self) -> Mat2 { - Mat2::new(self[0][0], self[1][0], - self[0][1], self[1][1]) - } - - #[inline(always)] - pure fn is_identity(&self) -> bool { - // self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! - self.fuzzy_eq(&Mat2::identity()) - } - - #[inline(always)] - pure fn is_diagonal(&self) -> bool { - let _0 = Number::from(0); - self[0][1].fuzzy_eq(&_0) && - self[1][0].fuzzy_eq(&_0) - } - - #[inline(always)] - pure fn is_rotated(&self) -> bool { - // !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! - !self.fuzzy_eq(&Mat2::identity()) - } - - #[inline(always)] - pure fn is_symmetric(&self) -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[1][0].fuzzy_eq(&self[0][1]) - } - - #[inline(always)] - pure fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&Number::from(0)) - } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Mat2, *T>( - to_unsafe_ptr(self) - ) - } - } -} - -pub impl Mat2: MutableMatrix> { - #[inline(always)] - fn col_mut(&mut self, i: uint) -> &self/mut Vec2 { - 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) { - util::swap(self.col_mut(a), - self.col_mut(b)); - } - - #[inline(always)] - fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - } - - #[inline(always)] - fn set(&mut self, other: &Mat2) { - (*self) = (*other); - } - - #[inline(always)] - fn to_identity(&mut self) { - (*self) = Mat2::identity(); - } - - #[inline(always)] - fn to_zero(&mut self) { - (*self) = Mat2::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); - } - - #[inline(always)] - fn add_self_m(&mut self, other: &Mat2) { - self.col_mut(0).add_self_v(&other[0]); - self.col_mut(1).add_self_v(&other[1]); - } - - #[inline(always)] - fn sub_self_m(&mut self, other: &Mat2) { - self.col_mut(0).sub_self_v(&other[0]); - self.col_mut(1).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) { - util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); - util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); - } -} - -pub impl Mat2: Matrix2> { - #[inline(always)] - pure fn to_mat3(&self) -> Mat3 { - Mat3::from_Mat2(self) - } - - #[inline(always)] - pure fn to_mat4(&self) -> Mat4 { - Mat4::from_Mat2(self) - } -} - -pub impl Mat2: Index> { - #[inline(always)] - pure fn index(&self, i: uint) -> Vec2 { - unsafe { do buf_as_slice( - transmute::<*Mat2, *Vec2>( - to_unsafe_ptr(self)), 2) |slice| { slice[i] } - } - } -} - -pub impl Mat2: Neg> { - #[inline(always)] - pure fn neg(&self) -> Mat2 { - Mat2::from_cols(-self[0], -self[1]) - } -} - -pub impl Mat2: Eq { - #[inline(always)] - pure fn eq(&self, other: &Mat2) -> bool { - self[0] == other[0] && - self[1] == other[1] - } - - #[inline(always)] - pure fn ne(&self, other: &Mat2) -> bool { - !(self == other) - } -} - -pub impl Mat2: FuzzyEq { - #[inline(always)] - pure fn fuzzy_eq(other: &Mat2) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) - } -} - - - - - - -/** - * 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 - */ -pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } - -pub impl Mat3 { - /** - * 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)] - static pure fn new(c0r0:T, c0r1:T, c0r2:T, - c1r0:T, c1r1:T, c1r2:T, - c2r0:T, c2r1:T, c2r2:T) -> Mat3 { - Mat3::from_cols(Vec3::new(move c0r0, move c0r1, move c0r2), - Vec3::new(move c1r0, move c1r1, move c1r2), - Vec3::new(move c2r0, move c2r1, move 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.y | c2.z | - * +------+------+------+ - * r1 | c0.x | c1.y | c2.z | - * +------+------+------+ - * r2 | c0.x | c1.y | c2.z | - * +------+------+------+ - * ~~~ - */ - #[inline(always)] - static pure fn from_cols(c0: Vec3, c1: Vec3, c2: Vec3) -> Mat3 { - Mat3 { x: move c0, - y: move c1, - z: move c2 } - } - - /** - * 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)] - static pure fn from_value(value: T) -> Mat3 { - let _0 = Number::from(0); - Mat3::new(value, _0, _0, - _0, value, _0, - _0, _0, value) - } - - #[inline(always)] - static pure fn from_Mat2(m: &Mat2) -> Mat3 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat3::new(m[0][0], m[0][1], _0, - m[1][0], m[1][1], _0, - _0, _0, _1) - } - - // FIXME: An interim solution to the issues with static functions - #[inline(always)] - static pure fn identity() -> Mat3 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat3::new(_1, _0, _0, - _0, _1, _0, - _0, _0, _1) - } - - // FIXME: An interim solution to the issues with static functions - #[inline(always)] - static pure fn zero() -> Mat3 { - let _0 = Number::from(0); - Mat3::new(_0, _0, _0, - _0, _0, _0, - _0, _0, _0) - } -} - -pub impl Mat3: Matrix> { - #[inline(always)] - pure fn col(&self, i: uint) -> Vec3 { self[i] } - - #[inline(always)] - pure fn row(&self, i: uint) -> Vec3 { - Vec3::new(self[0][i], - self[1][i], - self[2][i]) - } - - /** - * Returns the multiplicative identity matrix - * ~~~ - * c0 c1 c2 - * +----+----+----+ - * r0 | 1 | 0 | 0 | - * +----+----+----+ - * r1 | 0 | 1 | 0 | - * +----+----+----+ - * r2 | 0 | 0 | 1 | - * +----+----+----+ - * ~~~ - */ - #[inline(always)] - static pure fn identity() -> Mat3 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat3::new(_1, _0, _0, - _0, _1, _0, - _0, _0, _1) - } - - /** - * Returns the additive identity matrix - * ~~~ - * c0 c1 c2 - * +----+----+----+ - * r0 | 0 | 0 | 0 | - * +----+----+----+ - * r1 | 0 | 0 | 0 | - * +----+----+----+ - * r2 | 0 | 0 | 0 | - * +----+----+----+ - * ~~~ - */ - #[inline(always)] - static pure fn zero() -> Mat3 { - let _0 = Number::from(0); - Mat3::new(_0, _0, _0, - _0, _0, _0, - _0, _0, _0) - } - - #[inline(always)] - pure fn mul_t(&self, value: T) -> Mat3 { - Mat3::from_cols(self[0].mul_t(value), - self[1].mul_t(value), - self[2].mul_t(value)) - } - - #[inline(always)] - pure fn mul_v(&self, vec: &Vec3) -> Vec3 { - Vec3::new(self.row(0).dot(vec), - self.row(1).dot(vec), - self.row(2).dot(vec)) - } - - #[inline(always)] - pure fn add_m(&self, other: &Mat3) -> Mat3 { - Mat3::from_cols(self[0].add_v(&other[0]), - self[1].add_v(&other[1]), - self[2].add_v(&other[2])) - } - - #[inline(always)] - pure fn sub_m(&self, other: &Mat3) -> Mat3 { - Mat3::from_cols(self[0].sub_v(&other[0]), - self[1].sub_v(&other[1]), - self[2].sub_v(&other[2])) - } - - #[inline(always)] - pure fn mul_m(&self, other: &Mat3) -> Mat3 { - Mat3::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)), - self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)), self.row(2).dot(&other.col(1)), - self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2))) - } - - pure fn dot(&self, other: &Mat3) -> T { - other.transpose().mul_m(self).trace() - } - - pure fn determinant(&self) -> T { - self.col(0).dot(&self.col(1).cross(&self.col(2))) - } - - pure fn trace(&self) -> T { - self[0][0] + self[1][1] + self[2][2] - } - - // #[inline(always)] - pure fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.fuzzy_eq(&Number::from(0)) { - None - } else { - Some(Mat3::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)).transpose()) - } - } - - #[inline(always)] - pure fn transpose(&self) -> Mat3 { - Mat3::new(self[0][0], self[1][0], self[2][0], - self[0][1], self[1][1], self[2][1], - self[0][2], self[1][2], self[2][2]) - } - - #[inline(always)] - pure fn is_identity(&self) -> bool { - // self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! - self.fuzzy_eq(&Mat3::identity()) - } - - #[inline(always)] - pure fn is_diagonal(&self) -> bool { - let _0 = Number::from(0); - self[0][1].fuzzy_eq(&_0) && - self[0][2].fuzzy_eq(&_0) && - - self[1][0].fuzzy_eq(&_0) && - self[1][2].fuzzy_eq(&_0) && - - self[2][0].fuzzy_eq(&_0) && - self[2][1].fuzzy_eq(&_0) - } - - #[inline(always)] - pure fn is_rotated(&self) -> bool { - // !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! - !self.fuzzy_eq(&Mat3::identity()) - } - - #[inline(always)] - pure fn is_symmetric(&self) -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[0][2].fuzzy_eq(&self[2][0]) && - - self[1][0].fuzzy_eq(&self[0][1]) && - self[1][2].fuzzy_eq(&self[2][1]) && - - self[2][0].fuzzy_eq(&self[0][2]) && - self[2][1].fuzzy_eq(&self[1][2]) - } - - #[inline(always)] - pure fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&Number::zero()) - } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Mat3, *T>( - to_unsafe_ptr(self) - ) - } - } -} - -pub impl Mat3: MutableMatrix> { - #[inline(always)] - fn col_mut(&mut self, i: uint) -> &self/mut Vec3 { - 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) { - util::swap(self.col_mut(a), - self.col_mut(b)); - } - - #[inline(always)] - fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - self.z.swap(a, b); - } - - #[inline(always)] - fn set(&mut self, other: &Mat3) { - (*self) = (*other); - } - - #[inline(always)] - fn to_identity(&mut self) { - (*self) = Mat3::identity(); - } - - #[inline(always)] - fn to_zero(&mut self) { - (*self) = Mat3::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) { - 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) { - 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) { - util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); - util::swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0)); - - util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); - util::swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1)); - - util::swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2)); - util::swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2)); - } -} - -pub impl Mat3: Matrix3> { - #[inline(always)] - static pure fn from_axis_angle>(axis: &Vec3, theta: A) -> Mat3 { - let c: T = cos(&theta.to_radians()); - let s: T = sin(&theta.to_radians()); - let _0: T = Number::from(0); - let _1: T = Number::from(1); - let _1_c: T = _1 - c; - - let x = axis.x; - let y = axis.y; - let z = axis.z; - - Mat3::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)] - pure fn to_mat4(&self) -> Mat4 { - Mat4::from_Mat3(self) - } -} - -pub impl Mat3: ToQuat { - pure fn to_Quat() -> Quat { - // 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 >= Number::from(0) { - 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) - } -} - -pub impl Mat3: Index> { - #[inline(always)] - pure fn index(&self, i: uint) -> Vec3 { - unsafe { do buf_as_slice( - transmute::<*Mat3, *Vec3>( - to_unsafe_ptr(self)), 3) |slice| { slice[i] } - } - } -} - -pub impl Mat3: Neg> { - #[inline(always)] - pure fn neg(&self) -> Mat3 { - Mat3::from_cols(-self[0], -self[1], -self[2]) - } -} - -pub impl Mat3: Eq { - #[inline(always)] - pure fn eq(&self, other: &Mat3) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] - } - - #[inline(always)] - pure fn ne(&self, other: &Mat3) -> bool { - !(self == other) - } -} - -pub impl Mat3: FuzzyEq { - #[inline(always)] - pure fn fuzzy_eq(other: &Mat3) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) - } -} - - - - - - -/** - * 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 - */ -pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } - -pub impl Mat4 { - /** - * 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)] - static pure 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 { - Mat4::from_cols(Vec4::new(move c0r0, move c0r1, move c0r2, move c0r3), - Vec4::new(move c1r0, move c1r1, move c1r2, move c1r3), - Vec4::new(move c2r0, move c2r1, move c2r2, move c2r3), - Vec4::new(move c3r0, move c3r1, move c3r2, move 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)] - static pure fn from_cols(c0: Vec4, c1: Vec4, c2: Vec4, c3: Vec4) -> Mat4 { - Mat4 { x: move c0, - y: move c1, - z: move c2, - w: move c3 } - } - - /** - * 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)] - static pure fn from_value(value: T) -> Mat4 { - let _0 = Number::from(0); - Mat4::new(value, _0, _0, _0, - _0, value, _0, _0, - _0, _0, value, _0, - _0, _0, _0, value) - } - - #[inline(always)] - static pure fn from_Mat2(m: &Mat2) -> Mat4 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat4::new(m[0][0], m[0][1], _0, _0, - m[1][0], m[1][1], _0, _0, - _0, _0, _1, _0, - _0, _0, _0, _1) - } - - #[inline(always)] - static pure fn from_Mat3(m: &Mat3) -> Mat4 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat4::new(m[0][0], m[0][1], m[0][2], _0, - m[1][0], m[1][1], m[1][2], _0, - m[2][0], m[2][1], m[2][2], _0, - _0, _0, _0, _1) - } - - // FIXME: An interim solution to the issues with static functions - #[inline(always)] - static pure fn identity() -> Mat4 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat4::new(_1, _0, _0, _0, - _0, _1, _0, _0, - _0, _0, _1, _0, - _0, _0, _0, _1) - } - - // FIXME: An interim solution to the issues with static functions - #[inline(always)] - static pure fn zero() -> Mat4 { - let _0 = Number::from(0); - Mat4::new(_0, _0, _0, _0, - _0, _0, _0, _0, - _0, _0, _0, _0, - _0, _0, _0, _0) - } -} - -pub impl Mat4: Matrix> { - #[inline(always)] - pure fn col(&self, i: uint) -> Vec4 { self[i] } - - #[inline(always)] - pure fn row(&self, i: uint) -> Vec4 { - Vec4::new(self[0][i], - self[1][i], - self[2][i], - self[3][i]) - } - - /** - * 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)] - static pure fn identity() -> Mat4 { - let _0 = Number::from(0); - let _1 = Number::from(1); - Mat4::new(_1, _0, _0, _0, - _0, _1, _0, _0, - _0, _0, _1, _0, - _0, _0, _0, _1) - } - - /** - * 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)] - static pure fn zero() -> Mat4 { - let _0 = Number::from(0); - Mat4::new(_0, _0, _0, _0, - _0, _0, _0, _0, - _0, _0, _0, _0, - _0, _0, _0, _0) - } - - #[inline(always)] - pure fn mul_t(&self, value: T) -> Mat4 { - Mat4::from_cols(self[0].mul_t(value), - self[1].mul_t(value), - self[2].mul_t(value), - self[3].mul_t(value)) - } - - #[inline(always)] - pure fn mul_v(&self, vec: &Vec4) -> Vec4 { - Vec4::new(self.row(0).dot(vec), - self.row(1).dot(vec), - self.row(2).dot(vec), - self.row(3).dot(vec)) - } - - #[inline(always)] - pure fn add_m(&self, other: &Mat4) -> Mat4 { - Mat4::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)] - pure fn sub_m(&self, other: &Mat4) -> Mat4 { - Mat4::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)] - pure fn mul_m(&self, other: &Mat4) -> Mat4 { - // Surprisingly when building with optimisation turned on this is actually - // faster than writing out the matrix multiplication in expanded form. - // If you don't believe me, see ./test/performance/matrix_mul.rs - Mat4::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)), self.row(3).dot(&other.col(0)), - self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)), self.row(2).dot(&other.col(1)), self.row(3).dot(&other.col(1)), - self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2)), self.row(3).dot(&other.col(2)), - self.row(0).dot(&other.col(3)), self.row(1).dot(&other.col(3)), self.row(2).dot(&other.col(3)), self.row(3).dot(&other.col(3))) - } - - pure fn dot(&self, other: &Mat4) -> T { - other.transpose().mul_m(self).trace() - } - - pure fn determinant(&self) -> T { - self[0][0]*Mat3::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]).determinant() - - self[1][0]*Mat3::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]).determinant() + - self[2][0]*Mat3::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]).determinant() - - self[3][0]*Mat3::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]).determinant() - } - - pure fn trace(&self) -> T { - self[0][0] + self[1][1] + self[2][2] + self[3][3] - } - - pure fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.fuzzy_eq(&Number::from(0)) { - 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 = Matrix::identity(); // FIXME: there's something wrong with static functions here! - let mut I = Mat4::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)] - pure fn transpose(&self) -> Mat4 { - Mat4::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)] - pure fn is_identity(&self) -> bool { - // self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! - self.fuzzy_eq(&Mat4::identity()) - } - - #[inline(always)] - pure fn is_diagonal(&self) -> bool { - let _0 = Number::from(0); - self[0][1].fuzzy_eq(&_0) && - self[0][2].fuzzy_eq(&_0) && - self[0][3].fuzzy_eq(&_0) && - - self[1][0].fuzzy_eq(&_0) && - self[1][2].fuzzy_eq(&_0) && - self[1][3].fuzzy_eq(&_0) && - - self[2][0].fuzzy_eq(&_0) && - self[2][1].fuzzy_eq(&_0) && - self[2][3].fuzzy_eq(&_0) && - - self[3][0].fuzzy_eq(&_0) && - self[3][1].fuzzy_eq(&_0) && - self[3][2].fuzzy_eq(&_0) - } - - #[inline(always)] - pure fn is_rotated(&self) -> bool { - // !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! - !self.fuzzy_eq(&Mat4::identity()) - } - - #[inline(always)] - pure fn is_symmetric(&self) -> bool { - self[0][1].fuzzy_eq(&self[1][0]) && - self[0][2].fuzzy_eq(&self[2][0]) && - self[0][3].fuzzy_eq(&self[3][0]) && - - self[1][0].fuzzy_eq(&self[0][1]) && - self[1][2].fuzzy_eq(&self[2][1]) && - self[1][3].fuzzy_eq(&self[3][1]) && - - self[2][0].fuzzy_eq(&self[0][2]) && - self[2][1].fuzzy_eq(&self[1][2]) && - self[2][3].fuzzy_eq(&self[3][2]) && - - self[3][0].fuzzy_eq(&self[0][3]) && - self[3][1].fuzzy_eq(&self[1][3]) && - self[3][2].fuzzy_eq(&self[2][3]) - } - - #[inline(always)] - pure fn is_invertible(&self) -> bool { - !self.determinant().fuzzy_eq(&Number::zero()) - } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Mat4, *T>( - to_unsafe_ptr(self) - ) - } - } -} - -pub impl Mat4: MutableMatrix> { - #[inline(always)] - fn col_mut(&mut self, i: uint) -> &self/mut Vec4 { - 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) { - util::swap(self.col_mut(a), - self.col_mut(b)); - } - - #[inline(always)] - fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - self.z.swap(a, b); - self.w.swap(a, b); - } - - #[inline(always)] - fn set(&mut self, other: &Mat4) { - (*self) = (*other); - } - - #[inline(always)] - fn to_identity(&mut self) { - (*self) = Mat4::identity(); - } - - #[inline(always)] - fn to_zero(&mut self) { - (*self) = Mat4::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) { - 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) { - 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) { - util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); - util::swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0)); - util::swap(self.col_mut(0).index_mut(3), self.col_mut(3).index_mut(0)); - - util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); - util::swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1)); - util::swap(self.col_mut(1).index_mut(3), self.col_mut(3).index_mut(1)); - - util::swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2)); - util::swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2)); - util::swap(self.col_mut(2).index_mut(3), self.col_mut(3).index_mut(2)); - - util::swap(self.col_mut(3).index_mut(0), self.col_mut(0).index_mut(3)); - util::swap(self.col_mut(3).index_mut(1), self.col_mut(1).index_mut(3)); - util::swap(self.col_mut(3).index_mut(2), self.col_mut(2).index_mut(3)); - } -} - -pub impl Mat4: Matrix4> { -} - -pub impl Mat4: Neg> { - #[inline(always)] - pure fn neg(&self) -> Mat4 { - Mat4::from_cols(-self[0], -self[1], -self[2], -self[3]) - } -} - -pub impl Mat4: Index> { - #[inline(always)] - pure fn index(&self, i: uint) -> Vec4 { - unsafe { do buf_as_slice( - transmute::<*Mat4, *Vec4>( - to_unsafe_ptr(self)), 4) |slice| { slice[i] } - } - } -} - -pub impl Mat4: Eq { - #[inline(always)] - pure fn eq(&self, other: &Mat4) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] && - self[3] == other[3] - } - - #[inline(always)] - pure fn ne(&self, other: &Mat4) -> bool { - !(self == other) - } -} - -pub impl Mat4: FuzzyEq { - #[inline(always)] - pure fn fuzzy_eq(other: &Mat4) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) && - self[3].fuzzy_eq(&other[3]) - } -} +} \ No newline at end of file diff --git a/src/mat2.rs b/src/mat2.rs new file mode 100644 index 0000000..6a7732d --- /dev/null +++ b/src/mat2.rs @@ -0,0 +1,381 @@ +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::to_unsafe_ptr; +use core::vec::raw::buf_as_slice; + +use std::cmp::FuzzyEq; + +use funs::common::*; +use funs::exponential::*; +use num::types::{Float, Number}; +use vec::Vec2; + +/** + * 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 + */ +pub struct Mat2 { x: Vec2, y: Vec2 } + +pub impl Mat2 { + /** + * 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)] + static pure fn new(c0r0: T, c0r1: T, + c1r0: T, c1r1: T) -> Mat2 { + Mat2::from_cols(Vec2::new(move c0r0, move c0r1), + Vec2::new(move c1r0, move 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)] + static pure fn from_cols(c0: Vec2, c1: Vec2) -> Mat2 { + Mat2 { x: move c0, + y: move c1 } + } + + /** + * 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)] + static pure fn from_value(value: T) -> Mat2 { + let _0 = Number::from(0); + Mat2::new(value, _0, + _0, value) + } + + // FIXME: An interim solution to the issues with static functions + #[inline(always)] + static pure fn identity() -> Mat2 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat2::new(_1, _0, + _0, _1) + } + + // FIXME: An interim solution to the issues with static functions + #[inline(always)] + static pure fn zero() -> Mat2 { + let _0 = Number::from(0); + Mat2::new(_0, _0, + _0, _0) + } +} + +pub impl Mat2: Matrix> { + #[inline(always)] + pure fn col(&self, i: uint) -> Vec2 { self[i] } + + #[inline(always)] + pure fn row(&self, i: uint) -> Vec2 { + Vec2::new(self[0][i], + self[1][i]) + } + + /** + * Returns the multiplicative identity matrix + * ~~~ + * c0 c1 + * +----+----+ + * r0 | 1 | 0 | + * +----+----+ + * r1 | 0 | 1 | + * +----+----+ + * ~~~ + */ + #[inline(always)] + static pure fn identity() -> Mat2 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat2::new(_1, _0, + _0, _1) + } + + /** + * Returns the additive identity matrix + * ~~~ + * c0 c1 + * +----+----+ + * r0 | 0 | 0 | + * +----+----+ + * r1 | 0 | 0 | + * +----+----+ + * ~~~ + */ + #[inline(always)] + static pure fn zero() -> Mat2 { + let _0 = Number::from(0); + Mat2::new(_0, _0, + _0, _0) + } + + #[inline(always)] + pure fn mul_t(&self, value: T) -> Mat2 { + Mat2::from_cols(self[0].mul_t(value), + self[1].mul_t(value)) + } + + #[inline(always)] + pure fn mul_v(&self, vec: &Vec2) -> Vec2 { + Vec2::new(self.row(0).dot(vec), + self.row(1).dot(vec)) + } + + #[inline(always)] + pure fn add_m(&self, other: &Mat2) -> Mat2 { + Mat2::from_cols(self[0].add_v(&other[0]), + self[1].add_v(&other[1])) + } + + #[inline(always)] + pure fn sub_m(&self, other: &Mat2) -> Mat2 { + Mat2::from_cols(self[0].sub_v(&other[0]), + self[1].sub_v(&other[1])) + } + + #[inline(always)] + pure fn mul_m(&self, other: &Mat2) -> Mat2 { + Mat2::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))) + } + + pure fn dot(&self, other: &Mat2) -> T { + other.transpose().mul_m(self).trace() + } + + pure fn determinant(&self) -> T { + self[0][0] * self[1][1] - self[1][0] * self[0][1] + } + + pure fn trace(&self) -> T { + self[0][0] + self[1][1] + } + + #[inline(always)] + pure fn inverse(&self) -> Option> { + let d = self.determinant(); + if d.fuzzy_eq(&Number::from(0)) { + None + } else { + Some(Mat2::new( self[1][1]/d, -self[0][1]/d, + -self[1][0]/d, self[0][0]/d)) + } + } + + #[inline(always)] + pure fn transpose(&self) -> Mat2 { + Mat2::new(self[0][0], self[1][0], + self[0][1], self[1][1]) + } + + #[inline(always)] + pure fn is_identity(&self) -> bool { + // self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! + self.fuzzy_eq(&Mat2::identity()) + } + + #[inline(always)] + pure fn is_diagonal(&self) -> bool { + let _0 = Number::from(0); + self[0][1].fuzzy_eq(&_0) && + self[1][0].fuzzy_eq(&_0) + } + + #[inline(always)] + pure fn is_rotated(&self) -> bool { + // !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! + !self.fuzzy_eq(&Mat2::identity()) + } + + #[inline(always)] + pure fn is_symmetric(&self) -> bool { + self[0][1].fuzzy_eq(&self[1][0]) && + self[1][0].fuzzy_eq(&self[0][1]) + } + + #[inline(always)] + pure fn is_invertible(&self) -> bool { + !self.determinant().fuzzy_eq(&Number::from(0)) + } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*Mat2, *T>( + to_unsafe_ptr(self) + ) + } + } +} + +pub impl Mat2: MutableMatrix> { + #[inline(always)] + fn col_mut(&mut self, i: uint) -> &self/mut Vec2 { + 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) { + util::swap(self.col_mut(a), + self.col_mut(b)); + } + + #[inline(always)] + fn swap_rows(&mut self, a: uint, b: uint) { + self.x.swap(a, b); + self.y.swap(a, b); + } + + #[inline(always)] + fn set(&mut self, other: &Mat2) { + (*self) = (*other); + } + + #[inline(always)] + fn to_identity(&mut self) { + (*self) = Mat2::identity(); + } + + #[inline(always)] + fn to_zero(&mut self) { + (*self) = Mat2::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); + } + + #[inline(always)] + fn add_self_m(&mut self, other: &Mat2) { + self.col_mut(0).add_self_v(&other[0]); + self.col_mut(1).add_self_v(&other[1]); + } + + #[inline(always)] + fn sub_self_m(&mut self, other: &Mat2) { + self.col_mut(0).sub_self_v(&other[0]); + self.col_mut(1).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) { + util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); + util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); + } +} + +pub impl Mat2: Matrix2> { + #[inline(always)] + pure fn to_mat3(&self) -> Mat3 { + Mat3::from_Mat2(self) + } + + #[inline(always)] + pure fn to_mat4(&self) -> Mat4 { + Mat4::from_Mat2(self) + } +} + +pub impl Mat2: Index> { + #[inline(always)] + pure fn index(&self, i: uint) -> Vec2 { + unsafe { do buf_as_slice( + transmute::<*Mat2, *Vec2>( + to_unsafe_ptr(self)), 2) |slice| { slice[i] } + } + } +} + +pub impl Mat2: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat2 { + Mat2::from_cols(-self[0], -self[1]) + } +} + +pub impl Mat2: Eq { + #[inline(always)] + pure fn eq(&self, other: &Mat2) -> bool { + self[0] == other[0] && + self[1] == other[1] + } + + #[inline(always)] + pure fn ne(&self, other: &Mat2) -> bool { + !(self == other) + } +} + +pub impl Mat2: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &Mat2) -> bool { + self[0].fuzzy_eq(&other[0]) && + self[1].fuzzy_eq(&other[1]) + } +} \ No newline at end of file diff --git a/src/mat3.rs b/src/mat3.rs new file mode 100644 index 0000000..d9ad04e --- /dev/null +++ b/src/mat3.rs @@ -0,0 +1,504 @@ +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::to_unsafe_ptr; +use core::vec::raw::buf_as_slice; + +use std::cmp::FuzzyEq; + +use angle::Angle; +use funs::common::*; +use funs::exponential::*; +use funs::triganomic::{sin, cos}; +use num::types::{Float, Number}; +use quat::{Quat, ToQuat}; +use vec::Vec3; + +/** + * 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 + */ +pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } + +pub impl Mat3 { + /** + * 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)] + static pure fn new(c0r0:T, c0r1:T, c0r2:T, + c1r0:T, c1r1:T, c1r2:T, + c2r0:T, c2r1:T, c2r2:T) -> Mat3 { + Mat3::from_cols(Vec3::new(move c0r0, move c0r1, move c0r2), + Vec3::new(move c1r0, move c1r1, move c1r2), + Vec3::new(move c2r0, move c2r1, move 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.y | c2.z | + * +------+------+------+ + * r1 | c0.x | c1.y | c2.z | + * +------+------+------+ + * r2 | c0.x | c1.y | c2.z | + * +------+------+------+ + * ~~~ + */ + #[inline(always)] + static pure fn from_cols(c0: Vec3, c1: Vec3, c2: Vec3) -> Mat3 { + Mat3 { x: move c0, + y: move c1, + z: move c2 } + } + + /** + * 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)] + static pure fn from_value(value: T) -> Mat3 { + let _0 = Number::from(0); + Mat3::new(value, _0, _0, + _0, value, _0, + _0, _0, value) + } + + #[inline(always)] + static pure fn from_Mat2(m: &Mat2) -> Mat3 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat3::new(m[0][0], m[0][1], _0, + m[1][0], m[1][1], _0, + _0, _0, _1) + } + + // FIXME: An interim solution to the issues with static functions + #[inline(always)] + static pure fn identity() -> Mat3 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat3::new(_1, _0, _0, + _0, _1, _0, + _0, _0, _1) + } + + // FIXME: An interim solution to the issues with static functions + #[inline(always)] + static pure fn zero() -> Mat3 { + let _0 = Number::from(0); + Mat3::new(_0, _0, _0, + _0, _0, _0, + _0, _0, _0) + } +} + +pub impl Mat3: Matrix> { + #[inline(always)] + pure fn col(&self, i: uint) -> Vec3 { self[i] } + + #[inline(always)] + pure fn row(&self, i: uint) -> Vec3 { + Vec3::new(self[0][i], + self[1][i], + self[2][i]) + } + + /** + * Returns the multiplicative identity matrix + * ~~~ + * c0 c1 c2 + * +----+----+----+ + * r0 | 1 | 0 | 0 | + * +----+----+----+ + * r1 | 0 | 1 | 0 | + * +----+----+----+ + * r2 | 0 | 0 | 1 | + * +----+----+----+ + * ~~~ + */ + #[inline(always)] + static pure fn identity() -> Mat3 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat3::new(_1, _0, _0, + _0, _1, _0, + _0, _0, _1) + } + + /** + * Returns the additive identity matrix + * ~~~ + * c0 c1 c2 + * +----+----+----+ + * r0 | 0 | 0 | 0 | + * +----+----+----+ + * r1 | 0 | 0 | 0 | + * +----+----+----+ + * r2 | 0 | 0 | 0 | + * +----+----+----+ + * ~~~ + */ + #[inline(always)] + static pure fn zero() -> Mat3 { + let _0 = Number::from(0); + Mat3::new(_0, _0, _0, + _0, _0, _0, + _0, _0, _0) + } + + #[inline(always)] + pure fn mul_t(&self, value: T) -> Mat3 { + Mat3::from_cols(self[0].mul_t(value), + self[1].mul_t(value), + self[2].mul_t(value)) + } + + #[inline(always)] + pure fn mul_v(&self, vec: &Vec3) -> Vec3 { + Vec3::new(self.row(0).dot(vec), + self.row(1).dot(vec), + self.row(2).dot(vec)) + } + + #[inline(always)] + pure fn add_m(&self, other: &Mat3) -> Mat3 { + Mat3::from_cols(self[0].add_v(&other[0]), + self[1].add_v(&other[1]), + self[2].add_v(&other[2])) + } + + #[inline(always)] + pure fn sub_m(&self, other: &Mat3) -> Mat3 { + Mat3::from_cols(self[0].sub_v(&other[0]), + self[1].sub_v(&other[1]), + self[2].sub_v(&other[2])) + } + + #[inline(always)] + pure fn mul_m(&self, other: &Mat3) -> Mat3 { + Mat3::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)), + self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)), self.row(2).dot(&other.col(1)), + self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2))) + } + + pure fn dot(&self, other: &Mat3) -> T { + other.transpose().mul_m(self).trace() + } + + pure fn determinant(&self) -> T { + self.col(0).dot(&self.col(1).cross(&self.col(2))) + } + + pure fn trace(&self) -> T { + self[0][0] + self[1][1] + self[2][2] + } + + // #[inline(always)] + pure fn inverse(&self) -> Option> { + let d = self.determinant(); + if d.fuzzy_eq(&Number::from(0)) { + None + } else { + Some(Mat3::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)).transpose()) + } + } + + #[inline(always)] + pure fn transpose(&self) -> Mat3 { + Mat3::new(self[0][0], self[1][0], self[2][0], + self[0][1], self[1][1], self[2][1], + self[0][2], self[1][2], self[2][2]) + } + + #[inline(always)] + pure fn is_identity(&self) -> bool { + // self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! + self.fuzzy_eq(&Mat3::identity()) + } + + #[inline(always)] + pure fn is_diagonal(&self) -> bool { + let _0 = Number::from(0); + self[0][1].fuzzy_eq(&_0) && + self[0][2].fuzzy_eq(&_0) && + + self[1][0].fuzzy_eq(&_0) && + self[1][2].fuzzy_eq(&_0) && + + self[2][0].fuzzy_eq(&_0) && + self[2][1].fuzzy_eq(&_0) + } + + #[inline(always)] + pure fn is_rotated(&self) -> bool { + // !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! + !self.fuzzy_eq(&Mat3::identity()) + } + + #[inline(always)] + pure fn is_symmetric(&self) -> bool { + self[0][1].fuzzy_eq(&self[1][0]) && + self[0][2].fuzzy_eq(&self[2][0]) && + + self[1][0].fuzzy_eq(&self[0][1]) && + self[1][2].fuzzy_eq(&self[2][1]) && + + self[2][0].fuzzy_eq(&self[0][2]) && + self[2][1].fuzzy_eq(&self[1][2]) + } + + #[inline(always)] + pure fn is_invertible(&self) -> bool { + !self.determinant().fuzzy_eq(&Number::zero()) + } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*Mat3, *T>( + to_unsafe_ptr(self) + ) + } + } +} + +pub impl Mat3: MutableMatrix> { + #[inline(always)] + fn col_mut(&mut self, i: uint) -> &self/mut Vec3 { + 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) { + util::swap(self.col_mut(a), + self.col_mut(b)); + } + + #[inline(always)] + fn swap_rows(&mut self, a: uint, b: uint) { + self.x.swap(a, b); + self.y.swap(a, b); + self.z.swap(a, b); + } + + #[inline(always)] + fn set(&mut self, other: &Mat3) { + (*self) = (*other); + } + + #[inline(always)] + fn to_identity(&mut self) { + (*self) = Mat3::identity(); + } + + #[inline(always)] + fn to_zero(&mut self) { + (*self) = Mat3::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) { + 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) { + 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) { + util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); + util::swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0)); + + util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); + util::swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1)); + + util::swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2)); + util::swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2)); + } +} + +pub impl Mat3: Matrix3> { + #[inline(always)] + static pure fn from_axis_angle>(axis: &Vec3, theta: A) -> Mat3 { + let c: T = cos(&theta.to_radians()); + let s: T = sin(&theta.to_radians()); + let _0: T = Number::from(0); + let _1: T = Number::from(1); + let _1_c: T = _1 - c; + + let x = axis.x; + let y = axis.y; + let z = axis.z; + + Mat3::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)] + pure fn to_mat4(&self) -> Mat4 { + Mat4::from_Mat3(self) + } +} + +pub impl Mat3: ToQuat { + pure fn to_Quat() -> Quat { + // 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 >= Number::from(0) { + 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) + } +} + +pub impl Mat3: Index> { + #[inline(always)] + pure fn index(&self, i: uint) -> Vec3 { + unsafe { do buf_as_slice( + transmute::<*Mat3, *Vec3>( + to_unsafe_ptr(self)), 3) |slice| { slice[i] } + } + } +} + +pub impl Mat3: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat3 { + Mat3::from_cols(-self[0], -self[1], -self[2]) + } +} + +pub impl Mat3: Eq { + #[inline(always)] + pure fn eq(&self, other: &Mat3) -> bool { + self[0] == other[0] && + self[1] == other[1] && + self[2] == other[2] + } + + #[inline(always)] + pure fn ne(&self, other: &Mat3) -> bool { + !(self == other) + } +} + +pub impl Mat3: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &Mat3) -> bool { + self[0].fuzzy_eq(&other[0]) && + self[1].fuzzy_eq(&other[1]) && + self[2].fuzzy_eq(&other[2]) + } +} \ No newline at end of file diff --git a/src/mat4.rs b/src/mat4.rs new file mode 100644 index 0000000..151ec96 --- /dev/null +++ b/src/mat4.rs @@ -0,0 +1,551 @@ +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::to_unsafe_ptr; +use core::vec::raw::buf_as_slice; + +use std::cmp::FuzzyEq; + +use angle::Angle; +use funs::common::*; +use funs::exponential::*; +use num::types::{Float, Number}; +use vec::Vec4; + +/** + * 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 + */ +pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } + +pub impl Mat4 { + /** + * 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)] + static pure 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 { + Mat4::from_cols(Vec4::new(move c0r0, move c0r1, move c0r2, move c0r3), + Vec4::new(move c1r0, move c1r1, move c1r2, move c1r3), + Vec4::new(move c2r0, move c2r1, move c2r2, move c2r3), + Vec4::new(move c3r0, move c3r1, move c3r2, move 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)] + static pure fn from_cols(c0: Vec4, c1: Vec4, c2: Vec4, c3: Vec4) -> Mat4 { + Mat4 { x: move c0, + y: move c1, + z: move c2, + w: move c3 } + } + + /** + * 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)] + static pure fn from_value(value: T) -> Mat4 { + let _0 = Number::from(0); + Mat4::new(value, _0, _0, _0, + _0, value, _0, _0, + _0, _0, value, _0, + _0, _0, _0, value) + } + + #[inline(always)] + static pure fn from_Mat2(m: &Mat2) -> Mat4 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat4::new(m[0][0], m[0][1], _0, _0, + m[1][0], m[1][1], _0, _0, + _0, _0, _1, _0, + _0, _0, _0, _1) + } + + #[inline(always)] + static pure fn from_Mat3(m: &Mat3) -> Mat4 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat4::new(m[0][0], m[0][1], m[0][2], _0, + m[1][0], m[1][1], m[1][2], _0, + m[2][0], m[2][1], m[2][2], _0, + _0, _0, _0, _1) + } + + // FIXME: An interim solution to the issues with static functions + #[inline(always)] + static pure fn identity() -> Mat4 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat4::new(_1, _0, _0, _0, + _0, _1, _0, _0, + _0, _0, _1, _0, + _0, _0, _0, _1) + } + + // FIXME: An interim solution to the issues with static functions + #[inline(always)] + static pure fn zero() -> Mat4 { + let _0 = Number::from(0); + Mat4::new(_0, _0, _0, _0, + _0, _0, _0, _0, + _0, _0, _0, _0, + _0, _0, _0, _0) + } +} + +pub impl Mat4: Matrix> { + #[inline(always)] + pure fn col(&self, i: uint) -> Vec4 { self[i] } + + #[inline(always)] + pure fn row(&self, i: uint) -> Vec4 { + Vec4::new(self[0][i], + self[1][i], + self[2][i], + self[3][i]) + } + + /** + * 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)] + static pure fn identity() -> Mat4 { + let _0 = Number::from(0); + let _1 = Number::from(1); + Mat4::new(_1, _0, _0, _0, + _0, _1, _0, _0, + _0, _0, _1, _0, + _0, _0, _0, _1) + } + + /** + * 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)] + static pure fn zero() -> Mat4 { + let _0 = Number::from(0); + Mat4::new(_0, _0, _0, _0, + _0, _0, _0, _0, + _0, _0, _0, _0, + _0, _0, _0, _0) + } + + #[inline(always)] + pure fn mul_t(&self, value: T) -> Mat4 { + Mat4::from_cols(self[0].mul_t(value), + self[1].mul_t(value), + self[2].mul_t(value), + self[3].mul_t(value)) + } + + #[inline(always)] + pure fn mul_v(&self, vec: &Vec4) -> Vec4 { + Vec4::new(self.row(0).dot(vec), + self.row(1).dot(vec), + self.row(2).dot(vec), + self.row(3).dot(vec)) + } + + #[inline(always)] + pure fn add_m(&self, other: &Mat4) -> Mat4 { + Mat4::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)] + pure fn sub_m(&self, other: &Mat4) -> Mat4 { + Mat4::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)] + pure fn mul_m(&self, other: &Mat4) -> Mat4 { + // Surprisingly when building with optimisation turned on this is actually + // faster than writing out the matrix multiplication in expanded form. + // If you don't believe me, see ./test/performance/matrix_mul.rs + Mat4::new(self.row(0).dot(&other.col(0)), self.row(1).dot(&other.col(0)), self.row(2).dot(&other.col(0)), self.row(3).dot(&other.col(0)), + self.row(0).dot(&other.col(1)), self.row(1).dot(&other.col(1)), self.row(2).dot(&other.col(1)), self.row(3).dot(&other.col(1)), + self.row(0).dot(&other.col(2)), self.row(1).dot(&other.col(2)), self.row(2).dot(&other.col(2)), self.row(3).dot(&other.col(2)), + self.row(0).dot(&other.col(3)), self.row(1).dot(&other.col(3)), self.row(2).dot(&other.col(3)), self.row(3).dot(&other.col(3))) + } + + pure fn dot(&self, other: &Mat4) -> T { + other.transpose().mul_m(self).trace() + } + + pure fn determinant(&self) -> T { + self[0][0]*Mat3::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]).determinant() - + self[1][0]*Mat3::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]).determinant() + + self[2][0]*Mat3::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]).determinant() - + self[3][0]*Mat3::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]).determinant() + } + + pure fn trace(&self) -> T { + self[0][0] + self[1][1] + self[2][2] + self[3][3] + } + + pure fn inverse(&self) -> Option> { + let d = self.determinant(); + if d.fuzzy_eq(&Number::from(0)) { + 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 = Matrix::identity(); // FIXME: there's something wrong with static functions here! + let mut I = Mat4::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)] + pure fn transpose(&self) -> Mat4 { + Mat4::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)] + pure fn is_identity(&self) -> bool { + // self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! + self.fuzzy_eq(&Mat4::identity()) + } + + #[inline(always)] + pure fn is_diagonal(&self) -> bool { + let _0 = Number::from(0); + self[0][1].fuzzy_eq(&_0) && + self[0][2].fuzzy_eq(&_0) && + self[0][3].fuzzy_eq(&_0) && + + self[1][0].fuzzy_eq(&_0) && + self[1][2].fuzzy_eq(&_0) && + self[1][3].fuzzy_eq(&_0) && + + self[2][0].fuzzy_eq(&_0) && + self[2][1].fuzzy_eq(&_0) && + self[2][3].fuzzy_eq(&_0) && + + self[3][0].fuzzy_eq(&_0) && + self[3][1].fuzzy_eq(&_0) && + self[3][2].fuzzy_eq(&_0) + } + + #[inline(always)] + pure fn is_rotated(&self) -> bool { + // !self.fuzzy_eq(&Matrix::identity()) // FIXME: there's something wrong with static functions here! + !self.fuzzy_eq(&Mat4::identity()) + } + + #[inline(always)] + pure fn is_symmetric(&self) -> bool { + self[0][1].fuzzy_eq(&self[1][0]) && + self[0][2].fuzzy_eq(&self[2][0]) && + self[0][3].fuzzy_eq(&self[3][0]) && + + self[1][0].fuzzy_eq(&self[0][1]) && + self[1][2].fuzzy_eq(&self[2][1]) && + self[1][3].fuzzy_eq(&self[3][1]) && + + self[2][0].fuzzy_eq(&self[0][2]) && + self[2][1].fuzzy_eq(&self[1][2]) && + self[2][3].fuzzy_eq(&self[3][2]) && + + self[3][0].fuzzy_eq(&self[0][3]) && + self[3][1].fuzzy_eq(&self[1][3]) && + self[3][2].fuzzy_eq(&self[2][3]) + } + + #[inline(always)] + pure fn is_invertible(&self) -> bool { + !self.determinant().fuzzy_eq(&Number::zero()) + } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*Mat4, *T>( + to_unsafe_ptr(self) + ) + } + } +} + +pub impl Mat4: MutableMatrix> { + #[inline(always)] + fn col_mut(&mut self, i: uint) -> &self/mut Vec4 { + 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) { + util::swap(self.col_mut(a), + self.col_mut(b)); + } + + #[inline(always)] + fn swap_rows(&mut self, a: uint, b: uint) { + self.x.swap(a, b); + self.y.swap(a, b); + self.z.swap(a, b); + self.w.swap(a, b); + } + + #[inline(always)] + fn set(&mut self, other: &Mat4) { + (*self) = (*other); + } + + #[inline(always)] + fn to_identity(&mut self) { + (*self) = Mat4::identity(); + } + + #[inline(always)] + fn to_zero(&mut self) { + (*self) = Mat4::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) { + 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) { + 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) { + util::swap(self.col_mut(0).index_mut(1), self.col_mut(1).index_mut(0)); + util::swap(self.col_mut(0).index_mut(2), self.col_mut(2).index_mut(0)); + util::swap(self.col_mut(0).index_mut(3), self.col_mut(3).index_mut(0)); + + util::swap(self.col_mut(1).index_mut(0), self.col_mut(0).index_mut(1)); + util::swap(self.col_mut(1).index_mut(2), self.col_mut(2).index_mut(1)); + util::swap(self.col_mut(1).index_mut(3), self.col_mut(3).index_mut(1)); + + util::swap(self.col_mut(2).index_mut(0), self.col_mut(0).index_mut(2)); + util::swap(self.col_mut(2).index_mut(1), self.col_mut(1).index_mut(2)); + util::swap(self.col_mut(2).index_mut(3), self.col_mut(3).index_mut(2)); + + util::swap(self.col_mut(3).index_mut(0), self.col_mut(0).index_mut(3)); + util::swap(self.col_mut(3).index_mut(1), self.col_mut(1).index_mut(3)); + util::swap(self.col_mut(3).index_mut(2), self.col_mut(2).index_mut(3)); + } +} + +pub impl Mat4: Matrix4> { +} + +pub impl Mat4: Neg> { + #[inline(always)] + pure fn neg(&self) -> Mat4 { + Mat4::from_cols(-self[0], -self[1], -self[2], -self[3]) + } +} + +pub impl Mat4: Index> { + #[inline(always)] + pure fn index(&self, i: uint) -> Vec4 { + unsafe { do buf_as_slice( + transmute::<*Mat4, *Vec4>( + to_unsafe_ptr(self)), 4) |slice| { slice[i] } + } + } +} + +pub impl Mat4: Eq { + #[inline(always)] + pure fn eq(&self, other: &Mat4) -> bool { + self[0] == other[0] && + self[1] == other[1] && + self[2] == other[2] && + self[3] == other[3] + } + + #[inline(always)] + pure fn ne(&self, other: &Mat4) -> bool { + !(self == other) + } +} + +pub impl Mat4: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &Mat4) -> bool { + self[0].fuzzy_eq(&other[0]) && + self[1].fuzzy_eq(&other[1]) && + self[2].fuzzy_eq(&other[2]) && + self[3].fuzzy_eq(&other[3]) + } +} \ No newline at end of file diff --git a/src/vec.rs b/src/vec.rs index 37f8ba6..9b51c05 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -1,16 +1,20 @@ -use core::cast::transmute; use core::cmp::Eq; -use core::ptr::to_unsafe_ptr; -use core::sys::size_of; -use core::vec::raw::buf_as_slice; use std::cmp::FuzzyEq; use angle::Radians; -use funs::exponential::Exp; -use funs::triganomic::{InvTrig, acos, atan2}; use num::types::Number; + +pub mod vec2; +pub mod vec3; +pub mod vec4; + +pub use vec2::Vec2; +pub use vec3::Vec3; +pub use vec4::Vec4; + + /** * The base generic vector trait. * @@ -127,7 +131,8 @@ pub trait NumericVector: Vector Neg { /** * A mutable vector with numeric components */ -pub trait MutableNumericVector: MutableVector<&self/T> NumericVector { +pub trait MutableNumericVector: MutableVector<&self/T> + NumericVector { /** * Negate the vector */ @@ -300,779 +305,4 @@ pub trait MutableEuclideanVector: MutableNumericVector<&self/T> * Linearly intoperlate the vector towards `other` */ fn lerp_self(&mut self, other: &self, amount: T); -} - - - - - -/** - * 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 - */ -pub struct Vec2 { x: T, y: T } - -pub impl Vec2/*: Vector2*/ { - #[inline(always)] - static pure fn new(x: T, y: T ) -> Vec2 { - Vec2 { x: move x, y: move y } - } -} - -pub impl Vec2: Vector { - #[inline(always)] - static pure fn from_value(value: T) -> Vec2 { - Vec2::new(value, value) - } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Vec2, *T>( - to_unsafe_ptr(self) - ) - } - } -} - -pub impl Vec2: Index { - #[inline(always)] - pure fn index(&self, i: uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } } - } -} - -pub impl Vec2: MutableVector { - #[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) { - util::swap(self.index_mut(a), - self.index_mut(b)); - } -} - -pub impl Vec2: NumericVector { - #[inline(always)] - static pure fn identity() -> Vec2 { - Vec2::new(Number::one(), - Number::one()) - } - - #[inline(always)] - static pure fn zero() -> Vec2 { - Vec2::new(Number::zero(), - Number::zero()) - } - - #[inline(always)] - pure fn mul_t(&self, value: T) -> Vec2 { - Vec2::new(self[0] * value, - self[1] * value) - } - - #[inline(always)] - pure fn div_t(&self, value: T) -> Vec2 { - Vec2::new(self[0] / value, - self[1] / value) - } - - #[inline(always)] - pure fn add_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] + other[0], - self[1] + other[1]) - } - - #[inline(always)] - pure fn sub_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(self[0] - other[0], - self[1] - other[1]) - } - - #[inline(always)] - pure fn dot(&self, other: &Vec2) -> T { - self[0] * other[0] + - self[1] * other[1] - } -} - -pub impl Vec2: Neg> { - #[inline(always)] - pure fn neg(&self) -> Vec2 { - Vec2::new(-self[0], -self[1]) - } -} - -pub impl Vec2: MutableNumericVector<&self/T> { - #[inline(always)] - fn neg_self(&mut self) { - *self.index_mut(0) = -*self.index_mut(0); - *self.index_mut(1) = -*self.index_mut(1); - } - - #[inline(always)] - fn mul_self_t(&mut self, value: &T) { - *self.index_mut(0) *= (*value); - *self.index_mut(1) *= (*value); - } - - #[inline(always)] - fn div_self_t(&mut self, value: &T) { - *self.index_mut(0) /= (*value); - *self.index_mut(1) /= (*value); - } - - #[inline(always)] - fn add_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) += other[0]; - *self.index_mut(1) += other[1]; - } - - #[inline(always)] - fn sub_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) -= other[0]; - *self.index_mut(1) -= other[1]; - } -} - -pub impl Vec2: NumericVector2 { - #[inline(always)] - pure fn perp_dot(&self, other: &Vec2) ->T { - (self[0] * other[1]) - (self[1] * other[0]) - } -} - -pub impl Vec2: EuclideanVector { - #[inline(always)] - pure fn length2(&self) -> T { - self.dot(self) - } - - #[inline(always)] - pure fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline(always)] - pure fn distance2(&self, other: &Vec2) -> T { - other.sub_v(self).length2() - } - - #[inline(always)] - pure fn distance(&self, other: &Vec2) -> T { - other.distance2(self).sqrt() - } - - #[inline(always)] - pure fn angle(&self, other: &Vec2) -> Radians { - atan2(&self.perp_dot(other), &self.dot(other)) - } - - #[inline(always)] - pure fn normalize(&self) -> Vec2 { - let mut n: T = Number::from(1); - n /= self.length(); - return self.mul_t(n); - } - - #[inline(always)] - pure fn normalize_to(&self, length: T) -> Vec2 { - let mut n: T = length / self.length(); - return self.mul_t(n); - } - - #[inline(always)] - pure fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } -} - -pub impl Vec2: MutableEuclideanVector<&self/T> { - #[inline(always)] - fn normalize_self(&mut self) { - let mut n: T = Number::from(1); - n /= self.length(); - self.mul_self_t(&n); - } - - #[inline(always)] - fn normalize_self_to(&mut self, length: &T) { - let mut n: T = length / self.length(); - self.mul_self_t(&n); - } - - fn lerp_self(&mut self, other: &Vec2, amount: &T) { - self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); - } -} - -pub impl Vec2: Eq { - #[inline(always)] - pure fn eq(&self, other: &Vec2) -> bool { - self[0] == other[0] && - self[1] == other[1] - } - - #[inline(always)] - pure fn ne(&self, other: &Vec2) -> bool { - !(self == other) - } -} - -pub impl Vec2: FuzzyEq { - #[inline(always)] - pure fn fuzzy_eq(other: &Vec2) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) - } -} - - - - - - -/** - * 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 - */ -pub struct Vec3 { x: T, y: T, z: T } - -pub impl Vec3/*: Vector3*/ { - #[inline(always)] - static pure fn new(x: T, y: T, z: T) -> Vec3 { - Vec3 { x: move x, y: move y, z: move z } - } -} - -pub impl Vec3: Vector { - #[inline(always)] - static pure fn from_value(value: T) -> Vec3 { - Vec3::new(value, value, value) - } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Vec3, *T>( - to_unsafe_ptr(self) - ) - } - } -} - -pub impl Vec3: Index { - #[inline(always)] - pure fn index(&self, i: uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } - } -} - -pub impl Vec3: MutableVector { - #[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) { - util::swap(self.index_mut(a), - self.index_mut(b)); - } -} - -pub impl Vec3: NumericVector { - #[inline(always)] - static pure fn identity() -> Vec3 { - Vec3::new(Number::one(), - Number::one(), - Number::one()) - } - - #[inline(always)] - static pure fn zero() -> Vec3 { - Vec3::new(Number::zero(), - Number::zero(), - Number::zero()) - } - - #[inline(always)] - pure fn mul_t(&self, value: T) -> Vec3 { - Vec3::new(self[0] * value, - self[1] * value, - self[2] * value) - } - - #[inline(always)] - pure fn div_t(&self, value: T) -> Vec3 { - Vec3::new(self[0] / value, - self[1] / value, - self[2] / value) - } - - #[inline(always)] - pure fn add_v(&self, other: &Vec3) -> Vec3{ - Vec3::new(self[0] + other[0], - self[1] + other[1], - self[2] + other[2]) - } - - #[inline(always)] - pure fn sub_v(&self, other: &Vec3) -> Vec3{ - Vec3::new(self[0] - other[0], - self[1] - other[1], - self[2] - other[2]) - } - - #[inline(always)] - pure fn dot(&self, other: &Vec3) -> T { - self[0] * other[0] + - self[1] * other[1] + - self[2] * other[2] - } -} - -pub impl Vec3: Neg> { - #[inline(always)] - pure fn neg(&self) -> Vec3 { - Vec3::new(-self[0], -self[1], -self[2]) - } -} - -pub impl Vec3: MutableNumericVector<&self/T> { - #[inline(always)] - fn neg_self(&mut self) { - *self.index_mut(0) = -*self.index_mut(0); - *self.index_mut(1) = -*self.index_mut(1); - *self.index_mut(2) = -*self.index_mut(2); - } - - #[inline(always)] - fn mul_self_t(&mut self, value: &T) { - *self.index_mut(0) *= (*value); - *self.index_mut(1) *= (*value); - *self.index_mut(2) *= (*value); - } - - #[inline(always)] - fn div_self_t(&mut self, value: &T) { - *self.index_mut(0) /= (*value); - *self.index_mut(1) /= (*value); - *self.index_mut(2) /= (*value); - } - - #[inline(always)] - fn add_self_v(&mut self, other: &Vec3) { - *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) { - *self.index_mut(0) -= other[0]; - *self.index_mut(1) -= other[1]; - *self.index_mut(2) -= other[2]; - } -} - -pub impl Vec3: NumericVector3 { - #[inline(always)] - pure fn cross(&self, other: &Vec3) -> Vec3 { - Vec3::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])) - } -} - -pub impl Vec3: MutableNumericVector3<&self/T> { - #[inline(always)] - fn cross_self(&mut self, other: &Vec3) { - *self = self.cross(other); - } -} - -pub impl Vec3: EuclideanVector { - #[inline(always)] - pure fn length2(&self) -> T { - self.dot(self) - } - - #[inline(always)] - pure fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline(always)] - pure fn distance2(&self, other: &Vec3) -> T { - other.sub_v(self).length2() - } - - #[inline(always)] - pure fn distance(&self, other: &Vec3) -> T { - other.distance2(self).sqrt() - } - - #[inline(always)] - pure fn angle(&self, other: &Vec3) -> Radians { - atan2(&self.cross(other).length(), &self.dot(other)) - } - - #[inline(always)] - pure fn normalize(&self) -> Vec3 { - let mut n: T = Number::from(1); - n /= self.length(); - return self.mul_t(n); - } - - #[inline(always)] - pure fn normalize_to(&self, length: T) -> Vec3 { - let mut n: T = length / self.length(); - return self.mul_t(n); - } - - #[inline(always)] - pure fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } -} - -pub impl Vec3: MutableEuclideanVector<&self/T> { - #[inline(always)] - fn normalize_self(&mut self) { - let mut n: T = Number::from(1); - n /= self.length(); - self.mul_self_t(&n); - } - - #[inline(always)] - fn normalize_self_to(&mut self, length: &T) { - let mut n: T = length / self.length(); - self.mul_self_t(&n); - } - - fn lerp_self(&mut self, other: &Vec3, amount: &T) { - self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); - } -} - -pub impl Vec3: Eq { - #[inline(always)] - pure fn eq(&self, other: &Vec3) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] - } - - #[inline(always)] - pure fn ne(&self, other: &Vec3) -> bool { - !(self == other) - } -} - -pub impl Vec3: FuzzyEq { - #[inline(always)] - pure fn fuzzy_eq(other: &Vec3) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) - } -} - - - - - - -/** - * 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 - */ -pub struct Vec4 { x: T, y: T, z: T, w: T } - -pub impl Vec4/*: Vector4*/ { - #[inline(always)] - static pure fn new(x: T, y: T, z: T, w: T) -> Vec4 { - Vec4 { x: move x, y: move y, z: move z, w: move w } - } -} - -pub impl Vec4: Vector { - #[inline(always)] - static pure fn from_value(value: T) -> Vec4 { - Vec4::new(value, value, value, value) - } - - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Vec4, *T>( - to_unsafe_ptr(self) - ) - } - } -} - -pub impl Vec4: Index { - #[inline(always)] - pure fn index(&self, i: uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } - } -} - -pub impl Vec4: MutableVector { - #[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) { - util::swap(self.index_mut(a), - self.index_mut(b)); - } -} - -pub impl Vec4: NumericVector { - #[inline(always)] - static pure fn identity() -> Vec4 { - Vec4::new(Number::one(), - Number::one(), - Number::one(), - Number::one()) - } - - #[inline(always)] - static pure fn zero() -> Vec4 { - Vec4::new(Number::zero(), - Number::zero(), - Number::zero(), - Number::zero()) - } - - #[inline(always)] - pure fn mul_t(&self, value: T) -> Vec4 { - Vec4::new(self[0] * value, - self[1] * value, - self[2] * value, - self[3] * value) - } - - #[inline(always)] - pure fn div_t(&self, value: T) -> Vec4 { - Vec4::new(self[0] / value, - self[1] / value, - self[2] / value, - self[3] / value) - } - - #[inline(always)] - pure fn add_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] + other[0], - self[1] + other[1], - self[2] + other[2], - self[3] + other[3]) - } - - #[inline(always)] - pure fn sub_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(self[0] - other[0], - self[1] - other[1], - self[2] - other[2], - self[3] - other[3]) - } - - #[inline(always)] - pure fn dot(&self, other: &Vec4) -> T { - self[0] * other[0] + - self[1] * other[1] + - self[2] * other[2] + - self[3] * other[3] - } -} - -pub impl Vec4: Neg> { - #[inline(always)] - pure fn neg(&self) -> Vec4 { - Vec4::new(-self[0], -self[1], -self[2], -self[3]) - } -} - -pub impl Vec4: MutableNumericVector<&self/T> { - #[inline(always)] - fn neg_self(&mut self) { - *self.index_mut(0) = -*self.index_mut(0); - *self.index_mut(1) = -*self.index_mut(1); - *self.index_mut(2) = -*self.index_mut(2); - *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) { - *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) { - *self.index_mut(0) -= other[0]; - *self.index_mut(1) -= other[1]; - *self.index_mut(2) -= other[2]; - *self.index_mut(3) -= other[3]; - } -} - -pub impl Vec4: EuclideanVector { - #[inline(always)] - pure fn length2(&self) -> T { - self.dot(self) - } - - #[inline(always)] - pure fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline(always)] - pure fn distance2(&self, other: &Vec4) -> T { - other.sub_v(self).length2() - } - - #[inline(always)] - pure fn distance(&self, other: &Vec4) -> T { - other.distance2(self).sqrt() - } - - #[inline(always)] - pure fn angle(&self, other: &Vec4) -> Radians { - acos(&(self.dot(other) / (self.length() * other.length()))) - } - - #[inline(always)] - pure fn normalize(&self) -> Vec4 { - let mut n: T = Number::from(1); - n /= self.length(); - return self.mul_t(n); - } - - #[inline(always)] - pure fn normalize_to(&self, length: T) -> Vec4 { - let mut n: T = length / self.length(); - return self.mul_t(n); - } - - #[inline(always)] - pure fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } -} - -pub impl Vec4: MutableEuclideanVector<&self/T> { - #[inline(always)] - fn normalize_self(&mut self) { - let mut n: T = Number::from(1); - n /= self.length(); - self.mul_self_t(&n); - } - - #[inline(always)] - fn normalize_self_to(&mut self, length: &T) { - let mut n: T = length / self.length(); - self.mul_self_t(&n); - } - - fn lerp_self(&mut self, other: &Vec4, amount: &T) { - self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); - } -} - -pub impl Vec4: Eq { - #[inline(always)] - pure fn eq(&self, other: &Vec4) -> bool { - self[0] == other[0] && - self[1] == other[1] && - self[2] == other[2] && - self[3] == other[3] - } - - #[inline(always)] - pure fn ne(&self, other: &Vec4) -> bool { - !(self == other) - } -} - -pub impl Vec4: FuzzyEq { - #[inline(always)] - pure fn fuzzy_eq(other: &Vec4) -> bool { - self[0].fuzzy_eq(&other[0]) && - self[1].fuzzy_eq(&other[1]) && - self[2].fuzzy_eq(&other[2]) && - self[3].fuzzy_eq(&other[3]) - } } \ No newline at end of file diff --git a/src/vec2.rs b/src/vec2.rs new file mode 100644 index 0000000..3d7a94c --- /dev/null +++ b/src/vec2.rs @@ -0,0 +1,248 @@ +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::to_unsafe_ptr; +use core::vec::raw::buf_as_slice; + +use std::cmp::FuzzyEq; + +use angle::Radians; +use funs::exponential::Exp; +use funs::triganomic::{InvTrig, atan2}; +use num::types::Number; + +/** + * 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 + */ +pub struct Vec2 { x: T, y: T } + +pub impl Vec2/*: Vector2*/ { + #[inline(always)] + static pure fn new(x: T, y: T ) -> Vec2 { + Vec2 { x: move x, y: move y } + } +} + +pub impl Vec2: Vector { + #[inline(always)] + static pure fn from_value(value: T) -> Vec2 { + Vec2::new(value, value) + } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*Vec2, *T>( + to_unsafe_ptr(self) + ) + } + } +} + +pub impl Vec2: Index { + #[inline(always)] + pure fn index(&self, i: uint) -> T { + unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } } + } +} + +pub impl Vec2: MutableVector { + #[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) { + util::swap(self.index_mut(a), + self.index_mut(b)); + } +} + +pub impl Vec2: NumericVector { + #[inline(always)] + static pure fn identity() -> Vec2 { + Vec2::new(Number::one(), + Number::one()) + } + + #[inline(always)] + static pure fn zero() -> Vec2 { + Vec2::new(Number::zero(), + Number::zero()) + } + + #[inline(always)] + pure fn mul_t(&self, value: T) -> Vec2 { + Vec2::new(self[0] * value, + self[1] * value) + } + + #[inline(always)] + pure fn div_t(&self, value: T) -> Vec2 { + Vec2::new(self[0] / value, + self[1] / value) + } + + #[inline(always)] + pure fn add_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] + other[0], + self[1] + other[1]) + } + + #[inline(always)] + pure fn sub_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] - other[0], + self[1] - other[1]) + } + + #[inline(always)] + pure fn dot(&self, other: &Vec2) -> T { + self[0] * other[0] + + self[1] * other[1] + } +} + +pub impl Vec2: Neg> { + #[inline(always)] + pure fn neg(&self) -> Vec2 { + Vec2::new(-self[0], -self[1]) + } +} + +pub impl Vec2: MutableNumericVector<&self/T> { + #[inline(always)] + fn neg_self(&mut self) { + *self.index_mut(0) = -*self.index_mut(0); + *self.index_mut(1) = -*self.index_mut(1); + } + + #[inline(always)] + fn mul_self_t(&mut self, value: &T) { + *self.index_mut(0) *= (*value); + *self.index_mut(1) *= (*value); + } + + #[inline(always)] + fn div_self_t(&mut self, value: &T) { + *self.index_mut(0) /= (*value); + *self.index_mut(1) /= (*value); + } + + #[inline(always)] + fn add_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) += other[0]; + *self.index_mut(1) += other[1]; + } + + #[inline(always)] + fn sub_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) -= other[0]; + *self.index_mut(1) -= other[1]; + } +} + +pub impl Vec2: NumericVector2 { + #[inline(always)] + pure fn perp_dot(&self, other: &Vec2) ->T { + (self[0] * other[1]) - (self[1] * other[0]) + } +} + +pub impl Vec2: EuclideanVector { + #[inline(always)] + pure fn length2(&self) -> T { + self.dot(self) + } + + #[inline(always)] + pure fn length(&self) -> T { + self.length2().sqrt() + } + + #[inline(always)] + pure fn distance2(&self, other: &Vec2) -> T { + other.sub_v(self).length2() + } + + #[inline(always)] + pure fn distance(&self, other: &Vec2) -> T { + other.distance2(self).sqrt() + } + + #[inline(always)] + pure fn angle(&self, other: &Vec2) -> Radians { + atan2(&self.perp_dot(other), &self.dot(other)) + } + + #[inline(always)] + pure fn normalize(&self) -> Vec2 { + let mut n: T = Number::from(1); + n /= self.length(); + return self.mul_t(n); + } + + #[inline(always)] + pure fn normalize_to(&self, length: T) -> Vec2 { + let mut n: T = length / self.length(); + return self.mul_t(n); + } + + #[inline(always)] + pure fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { + self.add_v(&other.sub_v(self).mul_t(amount)) + } +} + +pub impl Vec2: MutableEuclideanVector<&self/T> { + #[inline(always)] + fn normalize_self(&mut self) { + let mut n: T = Number::from(1); + n /= self.length(); + self.mul_self_t(&n); + } + + #[inline(always)] + fn normalize_self_to(&mut self, length: &T) { + let mut n: T = length / self.length(); + self.mul_self_t(&n); + } + + fn lerp_self(&mut self, other: &Vec2, amount: &T) { + self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); + } +} + +pub impl Vec2: Eq { + #[inline(always)] + pure fn eq(&self, other: &Vec2) -> bool { + self[0] == other[0] && + self[1] == other[1] + } + + #[inline(always)] + pure fn ne(&self, other: &Vec2) -> bool { + !(self == other) + } +} + +pub impl Vec2: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &Vec2) -> bool { + self[0].fuzzy_eq(&other[0]) && + self[1].fuzzy_eq(&other[1]) + } +} \ No newline at end of file diff --git a/src/vec3.rs b/src/vec3.rs new file mode 100644 index 0000000..2f1f243 --- /dev/null +++ b/src/vec3.rs @@ -0,0 +1,273 @@ +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::to_unsafe_ptr; +use core::vec::raw::buf_as_slice; + +use std::cmp::FuzzyEq; + +use angle::Radians; +use funs::exponential::Exp; +use funs::triganomic::{InvTrig, atan2}; +use num::types::Number; + +/** + * 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 + */ +pub struct Vec3 { x: T, y: T, z: T } + +pub impl Vec3/*: Vector3*/ { + #[inline(always)] + static pure fn new(x: T, y: T, z: T) -> Vec3 { + Vec3 { x: move x, y: move y, z: move z } + } +} + +pub impl Vec3: Vector { + #[inline(always)] + static pure fn from_value(value: T) -> Vec3 { + Vec3::new(value, value, value) + } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*Vec3, *T>( + to_unsafe_ptr(self) + ) + } + } +} + +pub impl Vec3: Index { + #[inline(always)] + pure fn index(&self, i: uint) -> T { + unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } + } +} + +pub impl Vec3: MutableVector { + #[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) { + util::swap(self.index_mut(a), + self.index_mut(b)); + } +} + +pub impl Vec3: NumericVector { + #[inline(always)] + static pure fn identity() -> Vec3 { + Vec3::new(Number::one(), + Number::one(), + Number::one()) + } + + #[inline(always)] + static pure fn zero() -> Vec3 { + Vec3::new(Number::zero(), + Number::zero(), + Number::zero()) + } + + #[inline(always)] + pure fn mul_t(&self, value: T) -> Vec3 { + Vec3::new(self[0] * value, + self[1] * value, + self[2] * value) + } + + #[inline(always)] + pure fn div_t(&self, value: T) -> Vec3 { + Vec3::new(self[0] / value, + self[1] / value, + self[2] / value) + } + + #[inline(always)] + pure fn add_v(&self, other: &Vec3) -> Vec3{ + Vec3::new(self[0] + other[0], + self[1] + other[1], + self[2] + other[2]) + } + + #[inline(always)] + pure fn sub_v(&self, other: &Vec3) -> Vec3{ + Vec3::new(self[0] - other[0], + self[1] - other[1], + self[2] - other[2]) + } + + #[inline(always)] + pure fn dot(&self, other: &Vec3) -> T { + self[0] * other[0] + + self[1] * other[1] + + self[2] * other[2] + } +} + +pub impl Vec3: Neg> { + #[inline(always)] + pure fn neg(&self) -> Vec3 { + Vec3::new(-self[0], -self[1], -self[2]) + } +} + +pub impl Vec3: MutableNumericVector<&self/T> { + #[inline(always)] + fn neg_self(&mut self) { + *self.index_mut(0) = -*self.index_mut(0); + *self.index_mut(1) = -*self.index_mut(1); + *self.index_mut(2) = -*self.index_mut(2); + } + + #[inline(always)] + fn mul_self_t(&mut self, value: &T) { + *self.index_mut(0) *= (*value); + *self.index_mut(1) *= (*value); + *self.index_mut(2) *= (*value); + } + + #[inline(always)] + fn div_self_t(&mut self, value: &T) { + *self.index_mut(0) /= (*value); + *self.index_mut(1) /= (*value); + *self.index_mut(2) /= (*value); + } + + #[inline(always)] + fn add_self_v(&mut self, other: &Vec3) { + *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) { + *self.index_mut(0) -= other[0]; + *self.index_mut(1) -= other[1]; + *self.index_mut(2) -= other[2]; + } +} + +pub impl Vec3: NumericVector3 { + #[inline(always)] + pure fn cross(&self, other: &Vec3) -> Vec3 { + Vec3::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])) + } +} + +pub impl Vec3: MutableNumericVector3<&self/T> { + #[inline(always)] + fn cross_self(&mut self, other: &Vec3) { + *self = self.cross(other); + } +} + +pub impl Vec3: EuclideanVector { + #[inline(always)] + pure fn length2(&self) -> T { + self.dot(self) + } + + #[inline(always)] + pure fn length(&self) -> T { + self.length2().sqrt() + } + + #[inline(always)] + pure fn distance2(&self, other: &Vec3) -> T { + other.sub_v(self).length2() + } + + #[inline(always)] + pure fn distance(&self, other: &Vec3) -> T { + other.distance2(self).sqrt() + } + + #[inline(always)] + pure fn angle(&self, other: &Vec3) -> Radians { + atan2(&self.cross(other).length(), &self.dot(other)) + } + + #[inline(always)] + pure fn normalize(&self) -> Vec3 { + let mut n: T = Number::from(1); + n /= self.length(); + return self.mul_t(n); + } + + #[inline(always)] + pure fn normalize_to(&self, length: T) -> Vec3 { + let mut n: T = length / self.length(); + return self.mul_t(n); + } + + #[inline(always)] + pure fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { + self.add_v(&other.sub_v(self).mul_t(amount)) + } +} + +pub impl Vec3: MutableEuclideanVector<&self/T> { + #[inline(always)] + fn normalize_self(&mut self) { + let mut n: T = Number::from(1); + n /= self.length(); + self.mul_self_t(&n); + } + + #[inline(always)] + fn normalize_self_to(&mut self, length: &T) { + let mut n: T = length / self.length(); + self.mul_self_t(&n); + } + + fn lerp_self(&mut self, other: &Vec3, amount: &T) { + self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); + } +} + +pub impl Vec3: Eq { + #[inline(always)] + pure fn eq(&self, other: &Vec3) -> bool { + self[0] == other[0] && + self[1] == other[1] && + self[2] == other[2] + } + + #[inline(always)] + pure fn ne(&self, other: &Vec3) -> bool { + !(self == other) + } +} + +pub impl Vec3: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &Vec3) -> bool { + self[0].fuzzy_eq(&other[0]) && + self[1].fuzzy_eq(&other[1]) && + self[2].fuzzy_eq(&other[2]) + } +} \ No newline at end of file diff --git a/src/vec4.rs b/src/vec4.rs new file mode 100644 index 0000000..2290d8c --- /dev/null +++ b/src/vec4.rs @@ -0,0 +1,273 @@ +use core::cast::transmute; +use core::cmp::Eq; +use core::ptr::to_unsafe_ptr; +use core::vec::raw::buf_as_slice; + +use std::cmp::FuzzyEq; + +use angle::Radians; +use funs::exponential::Exp; +use funs::triganomic::{InvTrig, acos}; +use num::types::Number; + +/** + * 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 + */ +pub struct Vec4 { x: T, y: T, z: T, w: T } + +pub impl Vec4/*: Vector4*/ { + #[inline(always)] + static pure fn new(x: T, y: T, z: T, w: T) -> Vec4 { + Vec4 { x: move x, y: move y, z: move z, w: move w } + } +} + +pub impl Vec4: Vector { + #[inline(always)] + static pure fn from_value(value: T) -> Vec4 { + Vec4::new(value, value, value, value) + } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*Vec4, *T>( + to_unsafe_ptr(self) + ) + } + } +} + +pub impl Vec4: Index { + #[inline(always)] + pure fn index(&self, i: uint) -> T { + unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } + } +} + +pub impl Vec4: MutableVector { + #[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) { + util::swap(self.index_mut(a), + self.index_mut(b)); + } +} + +pub impl Vec4: NumericVector { + #[inline(always)] + static pure fn identity() -> Vec4 { + Vec4::new(Number::one(), + Number::one(), + Number::one(), + Number::one()) + } + + #[inline(always)] + static pure fn zero() -> Vec4 { + Vec4::new(Number::zero(), + Number::zero(), + Number::zero(), + Number::zero()) + } + + #[inline(always)] + pure fn mul_t(&self, value: T) -> Vec4 { + Vec4::new(self[0] * value, + self[1] * value, + self[2] * value, + self[3] * value) + } + + #[inline(always)] + pure fn div_t(&self, value: T) -> Vec4 { + Vec4::new(self[0] / value, + self[1] / value, + self[2] / value, + self[3] / value) + } + + #[inline(always)] + pure fn add_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] + other[0], + self[1] + other[1], + self[2] + other[2], + self[3] + other[3]) + } + + #[inline(always)] + pure fn sub_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] - other[0], + self[1] - other[1], + self[2] - other[2], + self[3] - other[3]) + } + + #[inline(always)] + pure fn dot(&self, other: &Vec4) -> T { + self[0] * other[0] + + self[1] * other[1] + + self[2] * other[2] + + self[3] * other[3] + } +} + +pub impl Vec4: Neg> { + #[inline(always)] + pure fn neg(&self) -> Vec4 { + Vec4::new(-self[0], -self[1], -self[2], -self[3]) + } +} + +pub impl Vec4: MutableNumericVector<&self/T> { + #[inline(always)] + fn neg_self(&mut self) { + *self.index_mut(0) = -*self.index_mut(0); + *self.index_mut(1) = -*self.index_mut(1); + *self.index_mut(2) = -*self.index_mut(2); + *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) { + *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) { + *self.index_mut(0) -= other[0]; + *self.index_mut(1) -= other[1]; + *self.index_mut(2) -= other[2]; + *self.index_mut(3) -= other[3]; + } +} + +pub impl Vec4: EuclideanVector { + #[inline(always)] + pure fn length2(&self) -> T { + self.dot(self) + } + + #[inline(always)] + pure fn length(&self) -> T { + self.length2().sqrt() + } + + #[inline(always)] + pure fn distance2(&self, other: &Vec4) -> T { + other.sub_v(self).length2() + } + + #[inline(always)] + pure fn distance(&self, other: &Vec4) -> T { + other.distance2(self).sqrt() + } + + #[inline(always)] + pure fn angle(&self, other: &Vec4) -> Radians { + acos(&(self.dot(other) / (self.length() * other.length()))) + } + + #[inline(always)] + pure fn normalize(&self) -> Vec4 { + let mut n: T = Number::from(1); + n /= self.length(); + return self.mul_t(n); + } + + #[inline(always)] + pure fn normalize_to(&self, length: T) -> Vec4 { + let mut n: T = length / self.length(); + return self.mul_t(n); + } + + #[inline(always)] + pure fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { + self.add_v(&other.sub_v(self).mul_t(amount)) + } +} + +pub impl Vec4: MutableEuclideanVector<&self/T> { + #[inline(always)] + fn normalize_self(&mut self) { + let mut n: T = Number::from(1); + n /= self.length(); + self.mul_self_t(&n); + } + + #[inline(always)] + fn normalize_self_to(&mut self, length: &T) { + let mut n: T = length / self.length(); + self.mul_self_t(&n); + } + + fn lerp_self(&mut self, other: &Vec4, amount: &T) { + self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); + } +} + +pub impl Vec4: Eq { + #[inline(always)] + pure fn eq(&self, other: &Vec4) -> bool { + self[0] == other[0] && + self[1] == other[1] && + self[2] == other[2] && + self[3] == other[3] + } + + #[inline(always)] + pure fn ne(&self, other: &Vec4) -> bool { + !(self == other) + } +} + +pub impl Vec4: FuzzyEq { + #[inline(always)] + pure fn fuzzy_eq(other: &Vec4) -> bool { + self[0].fuzzy_eq(&other[0]) && + self[1].fuzzy_eq(&other[1]) && + self[2].fuzzy_eq(&other[2]) && + self[3].fuzzy_eq(&other[3]) + } +} \ No newline at end of file