diff --git a/src/mat.rs b/src/mat.rs index 63adabc..ea03488 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -13,1433 +13,35 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One}; -use std::uint; +pub use self::mat2::Mat2; +pub use self::mat3::Mat3; +pub use self::mat4::Mat4; -use vec::*; -use quat::Quat; - -#[deriving(Eq)] -pub struct Mat2 { x: Vec2, y: Vec2 } - -impl Mat2 { - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec2 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec2,..2] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec2,..2] { - unsafe { transmute(self) } - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.col(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.col_mut(i).index_mut(j) - } -} - -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] - pub fn new(c0r0: T, c0r1: T, - c1r0: T, c1r1: T) -> Mat2 { - Mat2::from_cols(Vec2::new(c0r0, c0r1), - Vec2::new(c1r0, c1r1)) - } - - /// Construct a 2 x 2 matrix from column vectors - /// - /// # Arguments - /// - /// - `c0`: the first column vector of the matrix - /// - `c1`: the second column vector of the matrix - /// - /// ~~~ - /// c0 c1 - /// +------+------+ - /// r0 | c0.x | c1.x | - /// +------+------+ - /// r1 | c0.y | c1.y | - /// +------+------+ - /// ~~~ - #[inline] - pub fn from_cols(c0: Vec2, - c1: Vec2) -> Mat2 { - Mat2 { x: c0, y: c1 } - } - - #[inline] - pub fn row(&self, i: uint) -> Vec2 { - Vec2::new(*self.elem(0, i), - *self.elem(1, i)) - } - - #[inline] - pub fn swap_cols(&mut self, a: uint, b: uint) { - let tmp = *self.col(a); - *self.col_mut(a) = *self.col(b); - *self.col_mut(b) = tmp; - } - - #[inline] - pub fn swap_rows(&mut self, a: uint, b: uint) { - self.x.swap(a, b); - self.y.swap(a, b); - } - - #[inline] - pub fn transpose(&self) -> Mat2 { - Mat2::new(*self.elem(0, 0), *self.elem(1, 0), - *self.elem(0, 1), *self.elem(1, 1)) - } - - #[inline] - pub fn transpose_self(&mut self) { - let tmp01 = *self.elem(0, 1); - let tmp10 = *self.elem(1, 0); - - *self.elem_mut(0, 1) = *self.elem(1, 0); - *self.elem_mut(1, 0) = *self.elem(0, 1); - - *self.elem_mut(1, 0) = tmp01; - *self.elem_mut(0, 1) = tmp10; - } -} - -impl Mat2 { - /// Construct a 2 x 2 diagonal matrix with the major diagonal set to `value`. - /// ~~~ - /// c0 c1 - /// +-----+-----+ - /// r0 | val | 0 | - /// +-----+-----+ - /// r1 | 0 | val | - /// +-----+-----+ - /// ~~~ - #[inline] - pub fn from_value(value: T) -> Mat2 { - Mat2::new(value, Zero::zero(), - Zero::zero(), value) - } - - /// Returns the multiplicative identity matrix - /// ~~~ - /// c0 c1 - /// +----+----+ - /// r0 | 1 | 0 | - /// +----+----+ - /// r1 | 0 | 1 | - /// +----+----+ - /// ~~~ - #[inline] - pub fn identity() -> Mat2 { - Mat2::new(One::one::(), Zero::zero::(), - Zero::zero::(), One::one::()) - } - - /// Returns the additive identity matrix - /// ~~~ - /// c0 c1 - /// +----+----+ - /// r0 | 0 | 0 | - /// +----+----+ - /// r1 | 0 | 0 | - /// +----+----+ - /// ~~~ - #[inline] - pub fn zero() -> Mat2 { - Mat2::new(Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Mat2 { - Mat2::from_cols(self.col(0).mul_t(value), - self.col(1).mul_t(value)) - } - - #[inline] - pub fn mul_v(&self, vec: &Vec2) -> Vec2 { - Vec2::new(self.row(0).dot(vec), - self.row(1).dot(vec)) - } - - #[inline] - pub fn add_m(&self, other: &Mat2) -> Mat2 { - Mat2::from_cols(self.col(0).add_v(other.col(0)), - self.col(1).add_v(other.col(1))) - } - - #[inline] - pub fn sub_m(&self, other: &Mat2) -> Mat2 { - Mat2::from_cols(self.col(0).sub_v(other.col(0)), - self.col(1).sub_v(other.col(1))) - } - - #[inline] - pub 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))) - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - self.x.mul_self_t(value); - self.y.mul_self_t(value); - } - - #[inline] - pub fn add_self_m(&mut self, other: &Mat2) { - self.x.add_self_v(other.col(0)); - self.y.add_self_v(other.col(1)); - } - - #[inline] - pub fn sub_self_m(&mut self, other: &Mat2) { - self.x.sub_self_v(other.col(0)); - self.y.sub_self_v(other.col(1)); - } - - pub fn dot(&self, other: &Mat2) -> T { - other.transpose().mul_m(self).trace() - } - - pub fn determinant(&self) -> T { - *self.col(0).index(0) * - *self.col(1).index(1) - - *self.col(1).index(0) * - *self.col(0).index(1) - } - - pub fn trace(&self) -> T { - *self.col(0).index(0) + - *self.col(1).index(1) - } - - #[inline] - pub fn to_identity(&mut self) { - *self = Mat2::identity(); - } - - #[inline] - pub fn to_zero(&mut self) { - *self = Mat2::zero(); - } - - /// Returns the the matrix with an extra row and column added - /// ~~~ - /// c0 c1 c0 c1 c2 - /// +----+----+ +----+----+----+ - /// r0 | a | b | r0 | a | b | 0 | - /// +----+----+ +----+----+----+ - /// r1 | c | d | => r1 | c | d | 0 | - /// +----+----+ +----+----+----+ - /// r2 | 0 | 0 | 1 | - /// +----+----+----+ - /// ~~~ - #[inline] - pub fn to_mat3(&self) -> Mat3 { - Mat3::new(*self.elem(0, 0), *self.elem(0, 1), Zero::zero(), - *self.elem(1, 0), *self.elem(1, 1), Zero::zero(), - Zero::zero(), Zero::zero(), One::one()) - } - - /// Returns the the matrix with an extra two rows and columns added - /// ~~~ - /// c0 c1 c0 c1 c2 c3 - /// +----+----+ +----+----+----+----+ - /// r0 | a | b | r0 | a | b | 0 | 0 | - /// +----+----+ +----+----+----+----+ - /// r1 | c | d | => r1 | c | d | 0 | 0 | - /// +----+----+ +----+----+----+----+ - /// r2 | 0 | 0 | 1 | 0 | - /// +----+----+----+----+ - /// r3 | 0 | 0 | 0 | 1 | - /// +----+----+----+----+ - /// ~~~ - #[inline] - pub fn to_mat4(&self) -> Mat4 { - Mat4::new(*self.elem(0, 0), *self.elem(0, 1), Zero::zero(), Zero::zero(), - *self.elem(1, 0), *self.elem(1, 1), Zero::zero(), Zero::zero(), - Zero::zero(), Zero::zero(), One::one(), Zero::zero(), - Zero::zero(), Zero::zero(), Zero::zero(), One::one()) - } -} - -impl Neg> for Mat2 { - #[inline] - pub fn neg(&self) -> Mat2 { - Mat2::from_cols(-self.col(0), -self.col(1)) - } -} - -impl Mat2 { - #[inline] - pub fn from_angle(radians: T) -> Mat2 { - let cos_theta = radians.cos(); - let sin_theta = radians.sin(); - - Mat2::new(cos_theta, -sin_theta, - sin_theta, cos_theta) - } -} - -impl> Mat2 { - #[inline] - pub fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.approx_eq(&Zero::zero()) { - None - } else { - Some(Mat2::new(self.elem(1, 1) / d, -self.elem(0, 1) / d, - -self.elem(1, 0) / d, self.elem(0, 0) / d)) - } - } - - #[inline] - pub fn invert_self(&mut self) { - *self = self.inverse().expect("Couldn't invert the matrix!"); - } - - #[inline] - pub fn is_identity(&self) -> bool { - self.approx_eq(&Mat2::identity()) - } - - #[inline] - pub fn is_diagonal(&self) -> bool { - self.elem(0, 1).approx_eq(&Zero::zero()) && - self.elem(1, 0).approx_eq(&Zero::zero()) - } - - #[inline] - pub fn is_rotated(&self) -> bool { - !self.approx_eq(&Mat2::identity()) - } - - #[inline] - pub fn is_symmetric(&self) -> bool { - self.elem(0, 1).approx_eq(self.elem(1, 0)) && - self.elem(1, 0).approx_eq(self.elem(0, 1)) - } - - #[inline] - pub fn is_invertible(&self) -> bool { - !self.determinant().approx_eq(&Zero::zero()) - } -} - -impl> ApproxEq for Mat2 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Mat2) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool { - self.col(0).approx_eq_eps(other.col(0), epsilon) && - self.col(1).approx_eq_eps(other.col(1), epsilon) - } -} +pub mod mat2; +pub mod mat3; +pub mod mat4; // GLSL-style type aliases + pub type mat2 = Mat2; pub type dmat2 = Mat2; +pub type mat3 = Mat3; +pub type dmat3 = Mat3; + +pub type mat4 = Mat4; +pub type dmat4 = Mat4; + // Rust-style type aliases + pub type Mat2f = Mat2; pub type Mat2f32 = Mat2; pub type Mat2f64 = Mat2; -#[deriving(Eq)] -pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } - -impl Mat3 { - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec3 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec3,..3] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec3,..3] { - unsafe { transmute(self) } - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.col(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.col_mut(i).index_mut(j) - } -} - -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] - pub 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(c0r0, c0r1, c0r2), - Vec3::new(c1r0, c1r1, c1r2), - Vec3::new(c2r0, c2r1, c2r2)) - } - - /// Construct a 3 x 3 matrix from column vectors - /// - /// # Arguments - /// - /// - `c0`: the first column vector of the matrix - /// - `c1`: the second column vector of the matrix - /// - `c2`: the third column vector of the matrix - /// - /// ~~~ - /// c0 c1 c2 - /// +------+------+------+ - /// r0 | c0.x | c1.x | c2.x | - /// +------+------+------+ - /// r1 | c0.y | c1.y | c2.y | - /// +------+------+------+ - /// r2 | c0.z | c1.z | c2.z | - /// +------+------+------+ - /// ~~~ - #[inline] - pub fn from_cols(c0: Vec3, - c1: Vec3, - c2: Vec3) -> Mat3 { - Mat3 { x: c0, y: c1, z: c2 } - } - - #[inline] - pub fn row(&self, i: uint) -> Vec3 { - Vec3::new(*self.elem(0, i), - *self.elem(1, i), - *self.elem(2, i)) - } - - #[inline] - pub fn swap_cols(&mut self, a: uint, b: uint) { - let tmp = *self.col(a); - *self.col_mut(a) = *self.col(b); - *self.col_mut(b) = tmp; - } - - #[inline] - pub 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] - pub fn transpose(&self) -> Mat3 { - Mat3::new(*self.elem(0, 0), *self.elem(1, 0), *self.elem(2, 0), - *self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), - *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2)) - } - - #[inline] - pub fn transpose_self(&mut self) { - let tmp01 = *self.elem(0, 1); - let tmp02 = *self.elem(0, 2); - let tmp10 = *self.elem(1, 0); - let tmp12 = *self.elem(1, 2); - let tmp20 = *self.elem(2, 0); - let tmp21 = *self.elem(2, 1); - - *self.elem_mut(0, 1) = *self.elem(1, 0); - *self.elem_mut(0, 2) = *self.elem(2, 0); - *self.elem_mut(1, 0) = *self.elem(0, 1); - *self.elem_mut(1, 2) = *self.elem(2, 1); - *self.elem_mut(2, 0) = *self.elem(0, 2); - *self.elem_mut(2, 1) = *self.elem(1, 2); - - *self.elem_mut(1, 0) = tmp01; - *self.elem_mut(2, 0) = tmp02; - *self.elem_mut(0, 1) = tmp10; - *self.elem_mut(2, 1) = tmp12; - *self.elem_mut(0, 2) = tmp20; - *self.elem_mut(1, 2) = tmp21; - } -} - -impl Mat3 { - /// 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] - pub fn from_value(value: T) -> Mat3 { - Mat3::new(value, Zero::zero(), Zero::zero(), - Zero::zero(), value, Zero::zero(), - Zero::zero(), Zero::zero(), value) - } - - /// Returns the multiplicative identity matrix - /// ~~~ - /// c0 c1 c2 - /// +----+----+----+ - /// r0 | 1 | 0 | 0 | - /// +----+----+----+ - /// r1 | 0 | 1 | 0 | - /// +----+----+----+ - /// r2 | 0 | 0 | 1 | - /// +----+----+----+ - /// ~~~ - #[inline] - pub fn identity() -> Mat3 { - Mat3::new(One::one::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), One::one::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), One::one::()) - } - - /// Returns the additive identity matrix - /// ~~~ - /// c0 c1 c2 - /// +----+----+----+ - /// r0 | 0 | 0 | 0 | - /// +----+----+----+ - /// r1 | 0 | 0 | 0 | - /// +----+----+----+ - /// r2 | 0 | 0 | 0 | - /// +----+----+----+ - /// ~~~ - #[inline] - pub fn zero() -> Mat3 { - Mat3::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Mat3 { - Mat3::from_cols(self.col(0).mul_t(value), - self.col(1).mul_t(value), - self.col(2).mul_t(value)) - } - - #[inline] - pub 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] - pub fn add_m(&self, other: &Mat3) -> Mat3 { - Mat3::from_cols(self.col(0).add_v(other.col(0)), - self.col(1).add_v(other.col(1)), - self.col(2).add_v(other.col(2))) - } - - #[inline] - pub fn sub_m(&self, other: &Mat3) -> Mat3 { - Mat3::from_cols(self.col(0).sub_v(other.col(0)), - self.col(1).sub_v(other.col(1)), - self.col(2).sub_v(other.col(2))) - } - - #[inline] - pub 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))) - } - - #[inline] - pub 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] - pub fn add_self_m(&mut self, other: &Mat3) { - self.col_mut(0).add_self_v(other.col(0)); - self.col_mut(1).add_self_v(other.col(1)); - self.col_mut(2).add_self_v(other.col(2)); - } - - #[inline] - pub fn sub_self_m(&mut self, other: &Mat3) { - self.col_mut(0).sub_self_v(other.col(0)); - self.col_mut(1).sub_self_v(other.col(1)); - self.col_mut(2).sub_self_v(other.col(2)); - } - - pub fn dot(&self, other: &Mat3) -> T { - other.transpose().mul_m(self).trace() - } - - pub fn determinant(&self) -> T { - self.col(0).dot(&self.col(1).cross(self.col(2))) - } - - pub fn trace(&self) -> T { - *self.elem(0, 0) + - *self.elem(1, 1) + - *self.elem(2, 2) - } - - #[inline] - pub fn to_identity(&mut self) { - *self = Mat3::identity(); - } - - #[inline] - pub fn to_zero(&mut self) { - *self = Mat3::zero(); - } - - /// Returns the the matrix with an extra row and column added - /// ~~~ - /// c0 c1 c2 c0 c1 c2 c3 - /// +----+----+----+ +----+----+----+----+ - /// r0 | a | b | c | r0 | a | b | c | 0 | - /// +----+----+----+ +----+----+----+----+ - /// r1 | d | e | f | => r1 | d | e | f | 0 | - /// +----+----+----+ +----+----+----+----+ - /// r2 | g | h | i | r2 | g | h | i | 0 | - /// +----+----+----+ +----+----+----+----+ - /// r3 | 0 | 0 | 0 | 1 | - /// +----+----+----+----+ - /// ~~~ - #[inline] - pub fn to_mat4(&self) -> Mat4 { - Mat4::new(*self.elem(0, 0), *self.elem(0, 1), *self.elem(0, 2), Zero::zero(), - *self.elem(1, 0), *self.elem(1, 1), *self.elem(1, 2), Zero::zero(), - *self.elem(2, 0), *self.elem(2, 1), *self.elem(2, 2), Zero::zero(), - Zero::zero(), Zero::zero(), Zero::zero(), One::one()) - } -} - -impl Neg> for Mat3 { - #[inline] - pub fn neg(&self) -> Mat3 { - Mat3::from_cols(-self.col(0), -self.col(1), -self.col(2)) - } -} - -impl Mat3 { - /// Construct a matrix from an angular rotation around the `x` axis - pub fn from_angle_x(radians: T) -> Mat3 { - // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let cos_theta = radians.cos(); - let sin_theta = radians.sin(); - - Mat3::new(One::one(), Zero::zero(), Zero::zero(), - Zero::zero(), cos_theta, sin_theta, - Zero::zero(), -sin_theta, cos_theta) - } - - /// Construct a matrix from an angular rotation around the `y` axis - pub fn from_angle_y(radians: T) -> Mat3 { - // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let cos_theta = radians.cos(); - let sin_theta = radians.sin(); - - Mat3::new(cos_theta, Zero::zero(), -sin_theta, - Zero::zero(), One::one(), Zero::zero(), - sin_theta, Zero::zero(), cos_theta) - } - - /// Construct a matrix from an angular rotation around the `z` axis - pub fn from_angle_z(radians: T) -> Mat3 { - // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations - let cos_theta = radians.cos(); - let sin_theta = radians.sin(); - - Mat3::new(cos_theta, sin_theta, Zero::zero(), - -sin_theta, cos_theta, Zero::zero(), - Zero::zero(), Zero::zero(), One::one()) - } - - /// Construct a matrix from Euler angles - /// - /// # Arguments - /// - /// - `theta_x`: the angular rotation around the `x` axis (pitch) - /// - `theta_y`: the angular rotation around the `y` axis (yaw) - /// - `theta_z`: the angular rotation around the `z` axis (roll) - pub fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Mat3 { - // http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations - let cx = radians_x.cos(); - let sx = radians_x.sin(); - let cy = radians_y.cos(); - let sy = radians_y.sin(); - let cz = radians_z.cos(); - let sz = radians_z.sin(); - - Mat3::new(cy*cz, cy*sz, -sy, - -cx*sz + sx*sy*cz, cx*cz + sx*sy*sz, sx*cy, - sx*sz + cx*sy*cz, -sx*cz + cx*sy*sz, cx*cy) - } - - /// Construct a matrix from an axis and an angular rotation - pub fn from_angle_axis(radians: T, axis: &Vec3) -> Mat3 { - let c = radians.cos(); - let s = radians.sin(); - let _1_c = One::one::() - 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] - pub fn from_axes(x: Vec3, y: Vec3, z: Vec3) -> Mat3 { - Mat3::from_cols(x, y, z) - } - - pub fn look_at(dir: &Vec3, up: &Vec3) -> Mat3 { - let dir_ = dir.normalize(); - let side = dir_.cross(&up.normalize()); - let up_ = side.cross(&dir_).normalize(); - - Mat3::from_axes(up_, side, dir_) - } - - /// Convert the matrix to a quaternion - pub fn to_quat(&self) -> 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; let x; let y; let z; - let trace = self.trace(); - - // FIXME: We don't have any numeric conversions in std yet :P - let half = One::one::() / (One::one::() + One::one::()); - - cond! ( - (trace >= Zero::zero()) { - s = (One::one::() + trace).sqrt(); - w = half * s; - s = half / s; - x = (*self.elem(1, 2) - *self.elem(2, 1)) * s; - y = (*self.elem(2, 0) - *self.elem(0, 2)) * s; - z = (*self.elem(0, 1) - *self.elem(1, 0)) * s; - } - ((*self.elem(0, 0) > *self.elem(1, 1)) - && (*self.elem(0, 0) > *self.elem(2, 2))) { - s = (half + (*self.elem(0, 0) - *self.elem(1, 1) - *self.elem(2, 2))).sqrt(); - w = half * s; - s = half / s; - x = (*self.elem(0, 1) - *self.elem(1, 0)) * s; - y = (*self.elem(2, 0) - *self.elem(0, 2)) * s; - z = (*self.elem(1, 2) - *self.elem(2, 1)) * s; - } - (*self.elem(1, 1) > *self.elem(2, 2)) { - s = (half + (*self.elem(1, 1) - *self.elem(0, 0) - *self.elem(2, 2))).sqrt(); - w = half * s; - s = half / s; - x = (*self.elem(0, 1) - *self.elem(1, 0)) * s; - y = (*self.elem(1, 2) - *self.elem(2, 1)) * s; - z = (*self.elem(2, 0) - *self.elem(0, 2)) * s; - } - _ { - s = (half + (*self.elem(2, 2) - *self.elem(0, 0) - *self.elem(1, 1))).sqrt(); - w = half * s; - s = half / s; - x = (*self.elem(2, 0) - *self.elem(0, 2)) * s; - y = (*self.elem(1, 2) - *self.elem(2, 1)) * s; - z = (*self.elem(0, 1) - *self.elem(1, 0)) * s; - } - ) - Quat::new(w, x, y, z) - } -} - -impl> Mat3 { - pub fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.approx_eq(&Zero::zero()) { - None - } else { - Some(Mat3::from_cols(self.col(1).cross(self.col(2)).div_t(d), - self.col(2).cross(self.col(0)).div_t(d), - self.col(0).cross(self.col(1)).div_t(d)).transpose()) - } - } - - #[inline] - pub fn invert_self(&mut self) { - *self = self.inverse().expect("Couldn't invert the matrix!"); - } - - #[inline] - pub fn is_identity(&self) -> bool { - self.approx_eq(&Mat3::identity()) - } - - #[inline] - pub fn is_diagonal(&self) -> bool { - self.elem(0, 1).approx_eq(&Zero::zero()) && - self.elem(0, 2).approx_eq(&Zero::zero()) && - - self.elem(1, 0).approx_eq(&Zero::zero()) && - self.elem(1, 2).approx_eq(&Zero::zero()) && - - self.elem(2, 0).approx_eq(&Zero::zero()) && - self.elem(2, 1).approx_eq(&Zero::zero()) - } - - #[inline] - pub fn is_rotated(&self) -> bool { - !self.approx_eq(&Mat3::identity()) - } - - #[inline] - pub fn is_symmetric(&self) -> bool { - self.elem(0, 1).approx_eq(self.elem(1, 0)) && - self.elem(0, 2).approx_eq(self.elem(2, 0)) && - - self.elem(1, 0).approx_eq(self.elem(0, 1)) && - self.elem(1, 2).approx_eq(self.elem(2, 1)) && - - self.elem(2, 0).approx_eq(self.elem(0, 2)) && - self.elem(2, 1).approx_eq(self.elem(1, 2)) - } - - #[inline] - pub fn is_invertible(&self) -> bool { - !self.determinant().approx_eq(&Zero::zero()) - } -} - -impl> ApproxEq for Mat3 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Mat3) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool { - self.col(0).approx_eq_eps(other.col(0), epsilon) && - self.col(1).approx_eq_eps(other.col(1), epsilon) && - self.col(2).approx_eq_eps(other.col(2), epsilon) - } -} - -// GLSL-style type aliases -pub type mat3 = Mat3; -pub type dmat3 = Mat3; - -// Rust-style type aliases pub type Mat3f = Mat3; pub type Mat3f32 = Mat3; pub type Mat3f64 = Mat3; -/// A 4 x 4 column major matrix -/// -/// # Type parameters -/// -/// - `T` - The type of the elements of the matrix. Should be a floating point type. -/// -/// # Fields -/// -/// - `x`: the first column vector of the matrix -/// - `y`: the second column vector of the matrix -/// - `z`: the third column vector of the matrix -/// - `w`: the fourth column vector of the matrix -#[deriving(Eq)] -pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } - -impl Mat4 { - #[inline] - pub fn col<'a>(&'a self, i: uint) -> &'a Vec4 { - &'a self.as_slice()[i] - } - - #[inline] - pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4 { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [Vec4,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec4,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { - self.col(i).index(j) - } - - #[inline] - pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { - self.col_mut(i).index_mut(j) - } -} - -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] - pub 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(c0r0, c0r1, c0r2, c0r3), - Vec4::new(c1r0, c1r1, c1r2, c1r3), - Vec4::new(c2r0, c2r1, c2r2, c2r3), - Vec4::new(c3r0, c3r1, c3r2, c3r3)) - } - - /// Construct a 4 x 4 matrix from column vectors - /// - /// # Arguments - /// - /// - `c0`: the first column vector of the matrix - /// - `c1`: the second column vector of the matrix - /// - `c2`: the third column vector of the matrix - /// - `c3`: the fourth column vector of the matrix - /// - /// ~~~ - /// c0 c1 c2 c3 - /// +------+------+------+------+ - /// r0 | c0.x | c1.x | c2.x | c3.x | - /// +------+------+------+------+ - /// r1 | c0.y | c1.y | c2.y | c3.y | - /// +------+------+------+------+ - /// r2 | c0.z | c1.z | c2.z | c3.z | - /// +------+------+------+------+ - /// r3 | c0.w | c1.w | c2.w | c3.w | - /// +------+------+------+------+ - /// ~~~ - #[inline] - pub fn from_cols(c0: Vec4, - c1: Vec4, - c2: Vec4, - c3: Vec4) -> Mat4 { - Mat4 { x: c0, y: c1, z: c2, w: c3 } - } - - #[inline] - pub fn row(&self, i: uint) -> Vec4 { - Vec4::new(*self.elem(0, i), - *self.elem(1, i), - *self.elem(2, i), - *self.elem(3, i)) - } - - #[inline] - pub fn swap_cols(&mut self, a: uint, b: uint) { - let tmp = *self.col(a); - *self.col_mut(a) = *self.col(b); - *self.col_mut(b) = tmp; - } - - #[inline] - pub 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] - pub fn transpose(&self) -> Mat4 { - Mat4::new(*self.elem(0, 0), *self.elem(1, 0), *self.elem(2, 0), *self.elem(3, 0), - *self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), *self.elem(3, 1), - *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2), *self.elem(3, 2), - *self.elem(0, 3), *self.elem(1, 3), *self.elem(2, 3), *self.elem(3, 3)) - } - - #[inline] - pub fn transpose_self(&mut self) { - let tmp01 = *self.elem(0, 1); - let tmp02 = *self.elem(0, 2); - let tmp03 = *self.elem(0, 3); - let tmp10 = *self.elem(1, 0); - let tmp12 = *self.elem(1, 2); - let tmp13 = *self.elem(1, 3); - let tmp20 = *self.elem(2, 0); - let tmp21 = *self.elem(2, 1); - let tmp23 = *self.elem(2, 3); - let tmp30 = *self.elem(3, 0); - let tmp31 = *self.elem(3, 1); - let tmp32 = *self.elem(3, 2); - - *self.elem_mut(0, 1) = *self.elem(1, 0); - *self.elem_mut(0, 2) = *self.elem(2, 0); - *self.elem_mut(0, 3) = *self.elem(3, 0); - *self.elem_mut(1, 0) = *self.elem(0, 1); - *self.elem_mut(1, 2) = *self.elem(2, 1); - *self.elem_mut(1, 3) = *self.elem(3, 1); - *self.elem_mut(2, 0) = *self.elem(0, 2); - *self.elem_mut(2, 1) = *self.elem(1, 2); - *self.elem_mut(2, 3) = *self.elem(3, 2); - *self.elem_mut(3, 0) = *self.elem(0, 3); - *self.elem_mut(3, 1) = *self.elem(1, 3); - *self.elem_mut(3, 2) = *self.elem(2, 3); - - *self.elem_mut(1, 0) = tmp01; - *self.elem_mut(2, 0) = tmp02; - *self.elem_mut(3, 0) = tmp03; - *self.elem_mut(0, 1) = tmp10; - *self.elem_mut(2, 1) = tmp12; - *self.elem_mut(3, 1) = tmp13; - *self.elem_mut(0, 2) = tmp20; - *self.elem_mut(1, 2) = tmp21; - *self.elem_mut(3, 2) = tmp23; - *self.elem_mut(0, 3) = tmp30; - *self.elem_mut(1, 3) = tmp31; - *self.elem_mut(2, 3) = tmp32; - } -} - -impl Mat4 { - /// 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] - pub fn from_value(value: T) -> Mat4 { - Mat4::new(value, Zero::zero(), Zero::zero(), Zero::zero(), - Zero::zero(), value, Zero::zero(), Zero::zero(), - Zero::zero(), Zero::zero(), value, Zero::zero(), - Zero::zero(), Zero::zero(), Zero::zero(), value) - } - - /// Returns the multiplicative identity matrix - /// ~~~ - /// c0 c1 c2 c3 - /// +----+----+----+----+ - /// r0 | 1 | 0 | 0 | 0 | - /// +----+----+----+----+ - /// r1 | 0 | 1 | 0 | 0 | - /// +----+----+----+----+ - /// r2 | 0 | 0 | 1 | 0 | - /// +----+----+----+----+ - /// r3 | 0 | 0 | 0 | 1 | - /// +----+----+----+----+ - /// ~~~ - #[inline] - pub fn identity() -> Mat4 { - Mat4::new(One::one::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), One::one::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), One::one::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), One::one::()) - } - - /// 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] - pub fn zero() -> Mat4 { - Mat4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), - Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Mat4 { - Mat4::from_cols(self.col(0).mul_t(value), - self.col(1).mul_t(value), - self.col(2).mul_t(value), - self.col(3).mul_t(value)) - } - - #[inline] - pub 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] - pub fn add_m(&self, other: &Mat4) -> Mat4 { - Mat4::from_cols(self.col(0).add_v(other.col(0)), - self.col(1).add_v(other.col(1)), - self.col(2).add_v(other.col(2)), - self.col(3).add_v(other.col(3))) - } - - #[inline] - pub fn sub_m(&self, other: &Mat4) -> Mat4 { - Mat4::from_cols(self.col(0).sub_v(other.col(0)), - self.col(1).sub_v(other.col(1)), - self.col(2).sub_v(other.col(2)), - self.col(3).sub_v(other.col(3))) - } - - #[inline] - pub fn mul_m(&self, other: &Mat4) -> Mat4 { - 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))) - } - - #[inline] - pub 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] - pub fn add_self_m(&mut self, other: &Mat4) { - self.col_mut(0).add_self_v(other.col(0)); - self.col_mut(1).add_self_v(other.col(1)); - self.col_mut(2).add_self_v(other.col(2)); - self.col_mut(3).add_self_v(other.col(3)); - } - - #[inline] - pub fn sub_self_m(&mut self, other: &Mat4) { - self.col_mut(0).sub_self_v(other.col(0)); - self.col_mut(1).sub_self_v(other.col(1)); - self.col_mut(2).sub_self_v(other.col(2)); - self.col_mut(3).sub_self_v(other.col(3)); - } - - pub fn dot(&self, other: &Mat4) -> T { - other.transpose().mul_m(self).trace() - } - - pub fn determinant(&self) -> T { - let m0 = Mat3::new(*self.elem(1, 1), *self.elem(2, 1), *self.elem(3, 1), - *self.elem(1, 2), *self.elem(2, 2), *self.elem(3, 2), - *self.elem(1, 3), *self.elem(2, 3), *self.elem(3, 3)); - let m1 = Mat3::new(*self.elem(0, 1), *self.elem(2, 1), *self.elem(3, 1), - *self.elem(0, 2), *self.elem(2, 2), *self.elem(3, 2), - *self.elem(0, 3), *self.elem(2, 3), *self.elem(3, 3)); - let m2 = Mat3::new(*self.elem(0, 1), *self.elem(1, 1), *self.elem(3, 1), - *self.elem(0, 2), *self.elem(1, 2), *self.elem(3, 2), - *self.elem(0, 3), *self.elem(1, 3), *self.elem(3, 3)); - let m3 = Mat3::new(*self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), - *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2), - *self.elem(0, 3), *self.elem(1, 3), *self.elem(2, 3)); - - self.elem(0, 0) * m0.determinant() - - self.elem(1, 0) * m1.determinant() + - self.elem(2, 0) * m2.determinant() - - self.elem(3, 0) * m3.determinant() - } - - pub fn trace(&self) -> T { - *self.elem(0, 0) + - *self.elem(1, 1) + - *self.elem(2, 2) + - *self.elem(3, 3) - } - - #[inline] - pub fn to_identity(&mut self) { - *self = Mat4::identity(); - } - - #[inline] - pub fn to_zero(&mut self) { - *self = Mat4::zero(); - } -} - -impl Neg> for Mat4 { - #[inline] - pub fn neg(&self) -> Mat4 { - Mat4::from_cols(-self.col(0), -self.col(1), -self.col(2), -self.col(3)) - } -} - -impl> Mat4 { - pub fn inverse(&self) -> Option> { - let d = self.determinant(); - if d.approx_eq(&Zero::zero()) { - None - } else { - // Gauss Jordan Elimination with partial pivoting - // So take this matrix, A, augmented with the identity - // and essentially reduce [A|I] - - let mut A = *self; - let mut I = Mat4::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 A.elem(j, i).abs() > A.elem(j, i1).abs() { - i1 = i; - } - } - - // 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 - let ajj = *A.elem(j, j); - I.col_mut(j).div_self_t(ajj); - A.col_mut(j).div_self_t(ajj); - - // Eliminate off-diagonal elems in col j of A, - // doing identical ops to I - for uint::range(0, 4) |i| { - if i != j { - let ij_mul_aij = I.col(j).mul_t(*A.elem(i, j)); - let aj_mul_aij = A.col(j).mul_t(*A.elem(i, j)); - I.col_mut(i).sub_self_v(&ij_mul_aij); - A.col_mut(i).sub_self_v(&aj_mul_aij); - } - } - } - Some(I) - } - } - - #[inline] - pub fn invert_self(&mut self) { - *self = self.inverse().expect("Couldn't invert the matrix!"); - } - - #[inline] - pub fn is_identity(&self) -> bool { - self.approx_eq(&Mat4::identity()) - } - - #[inline] - pub fn is_diagonal(&self) -> bool { - self.elem(0, 1).approx_eq(&Zero::zero()) && - self.elem(0, 2).approx_eq(&Zero::zero()) && - self.elem(0, 3).approx_eq(&Zero::zero()) && - - self.elem(1, 0).approx_eq(&Zero::zero()) && - self.elem(1, 2).approx_eq(&Zero::zero()) && - self.elem(1, 3).approx_eq(&Zero::zero()) && - - self.elem(2, 0).approx_eq(&Zero::zero()) && - self.elem(2, 1).approx_eq(&Zero::zero()) && - self.elem(2, 3).approx_eq(&Zero::zero()) && - - self.elem(3, 0).approx_eq(&Zero::zero()) && - self.elem(3, 1).approx_eq(&Zero::zero()) && - self.elem(3, 2).approx_eq(&Zero::zero()) - } - - #[inline] - pub fn is_rotated(&self) -> bool { - !self.approx_eq(&Mat4::identity()) - } - - #[inline] - pub fn is_symmetric(&self) -> bool { - self.elem(0, 1).approx_eq(self.elem(1, 0)) && - self.elem(0, 2).approx_eq(self.elem(2, 0)) && - self.elem(0, 3).approx_eq(self.elem(3, 0)) && - - self.elem(1, 0).approx_eq(self.elem(0, 1)) && - self.elem(1, 2).approx_eq(self.elem(2, 1)) && - self.elem(1, 3).approx_eq(self.elem(3, 1)) && - - self.elem(2, 0).approx_eq(self.elem(0, 2)) && - self.elem(2, 1).approx_eq(self.elem(1, 2)) && - self.elem(2, 3).approx_eq(self.elem(3, 2)) && - - self.elem(3, 0).approx_eq(self.elem(0, 3)) && - self.elem(3, 1).approx_eq(self.elem(1, 3)) && - self.elem(3, 2).approx_eq(self.elem(2, 3)) - } - - #[inline] - pub fn is_invertible(&self) -> bool { - !self.determinant().approx_eq(&Zero::zero()) - } -} - -impl> ApproxEq for Mat4 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Mat4) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Mat4, epsilon: &T) -> bool { - self.col(0).approx_eq_eps(other.col(0), epsilon) && - self.col(1).approx_eq_eps(other.col(1), epsilon) && - self.col(2).approx_eq_eps(other.col(2), epsilon) && - self.col(3).approx_eq_eps(other.col(3), epsilon) - } -} - -// GLSL-style type aliases, corresponding to Section 4.1.6 of the [GLSL 4.30.6 specification] -// (http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf). - -// a 4×4 single-precision floating-point matrix -pub type mat4 = Mat4; -// a 4×4 double-precision floating-point matrix -pub type dmat4 = Mat4; - -// Rust-style type aliases pub type Mat4f = Mat4; pub type Mat4f32 = Mat4; pub type Mat4f64 = Mat4; diff --git a/src/mat2.rs b/src/mat2.rs new file mode 100644 index 0000000..e513e19 --- /dev/null +++ b/src/mat2.rs @@ -0,0 +1,379 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::cast::transmute; +use std::cmp::ApproxEq; +use std::num::{Zero, One}; + +use vec::*; +use super::{Mat3, Mat4}; + +#[deriving(Eq)] +pub struct Mat2 { x: Vec2, y: Vec2 } + +impl Mat2 { + #[inline] + pub fn col<'a>(&'a self, i: uint) -> &'a Vec2 { + &'a self.as_slice()[i] + } + + #[inline] + pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2 { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [Vec2,..2] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec2,..2] { + unsafe { transmute(self) } + } + + #[inline] + pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { + self.col(i).index(j) + } + + #[inline] + pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { + self.col_mut(i).index_mut(j) + } +} + +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] + pub fn new(c0r0: T, c0r1: T, + c1r0: T, c1r1: T) -> Mat2 { + Mat2::from_cols(Vec2::new(c0r0, c0r1), + Vec2::new(c1r0, c1r1)) + } + + /// Construct a 2 x 2 matrix from column vectors + /// + /// # Arguments + /// + /// - `c0`: the first column vector of the matrix + /// - `c1`: the second column vector of the matrix + /// + /// ~~~ + /// c0 c1 + /// +------+------+ + /// r0 | c0.x | c1.x | + /// +------+------+ + /// r1 | c0.y | c1.y | + /// +------+------+ + /// ~~~ + #[inline] + pub fn from_cols(c0: Vec2, + c1: Vec2) -> Mat2 { + Mat2 { x: c0, y: c1 } + } + + #[inline] + pub fn row(&self, i: uint) -> Vec2 { + Vec2::new(*self.elem(0, i), + *self.elem(1, i)) + } + + #[inline] + pub fn swap_cols(&mut self, a: uint, b: uint) { + let tmp = *self.col(a); + *self.col_mut(a) = *self.col(b); + *self.col_mut(b) = tmp; + } + + #[inline] + pub fn swap_rows(&mut self, a: uint, b: uint) { + self.x.swap(a, b); + self.y.swap(a, b); + } + + #[inline] + pub fn transpose(&self) -> Mat2 { + Mat2::new(*self.elem(0, 0), *self.elem(1, 0), + *self.elem(0, 1), *self.elem(1, 1)) + } + + #[inline] + pub fn transpose_self(&mut self) { + let tmp01 = *self.elem(0, 1); + let tmp10 = *self.elem(1, 0); + + *self.elem_mut(0, 1) = *self.elem(1, 0); + *self.elem_mut(1, 0) = *self.elem(0, 1); + + *self.elem_mut(1, 0) = tmp01; + *self.elem_mut(0, 1) = tmp10; + } +} + +impl Mat2 { + /// Construct a 2 x 2 diagonal matrix with the major diagonal set to `value`. + /// ~~~ + /// c0 c1 + /// +-----+-----+ + /// r0 | val | 0 | + /// +-----+-----+ + /// r1 | 0 | val | + /// +-----+-----+ + /// ~~~ + #[inline] + pub fn from_value(value: T) -> Mat2 { + Mat2::new(value, Zero::zero(), + Zero::zero(), value) + } + + /// Returns the multiplicative identity matrix + /// ~~~ + /// c0 c1 + /// +----+----+ + /// r0 | 1 | 0 | + /// +----+----+ + /// r1 | 0 | 1 | + /// +----+----+ + /// ~~~ + #[inline] + pub fn identity() -> Mat2 { + Mat2::new(One::one::(), Zero::zero::(), + Zero::zero::(), One::one::()) + } + + /// Returns the additive identity matrix + /// ~~~ + /// c0 c1 + /// +----+----+ + /// r0 | 0 | 0 | + /// +----+----+ + /// r1 | 0 | 0 | + /// +----+----+ + /// ~~~ + #[inline] + pub fn zero() -> Mat2 { + Mat2::new(Zero::zero::(), Zero::zero::(), + Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn mul_t(&self, value: T) -> Mat2 { + Mat2::from_cols(self.col(0).mul_t(value), + self.col(1).mul_t(value)) + } + + #[inline] + pub fn mul_v(&self, vec: &Vec2) -> Vec2 { + Vec2::new(self.row(0).dot(vec), + self.row(1).dot(vec)) + } + + #[inline] + pub fn add_m(&self, other: &Mat2) -> Mat2 { + Mat2::from_cols(self.col(0).add_v(other.col(0)), + self.col(1).add_v(other.col(1))) + } + + #[inline] + pub fn sub_m(&self, other: &Mat2) -> Mat2 { + Mat2::from_cols(self.col(0).sub_v(other.col(0)), + self.col(1).sub_v(other.col(1))) + } + + #[inline] + pub 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))) + } + + #[inline] + pub fn mul_self_t(&mut self, value: T) { + self.x.mul_self_t(value); + self.y.mul_self_t(value); + } + + #[inline] + pub fn add_self_m(&mut self, other: &Mat2) { + self.x.add_self_v(other.col(0)); + self.y.add_self_v(other.col(1)); + } + + #[inline] + pub fn sub_self_m(&mut self, other: &Mat2) { + self.x.sub_self_v(other.col(0)); + self.y.sub_self_v(other.col(1)); + } + + pub fn dot(&self, other: &Mat2) -> T { + other.transpose().mul_m(self).trace() + } + + pub fn determinant(&self) -> T { + *self.col(0).index(0) * + *self.col(1).index(1) - + *self.col(1).index(0) * + *self.col(0).index(1) + } + + pub fn trace(&self) -> T { + *self.col(0).index(0) + + *self.col(1).index(1) + } + + #[inline] + pub fn to_identity(&mut self) { + *self = Mat2::identity(); + } + + #[inline] + pub fn to_zero(&mut self) { + *self = Mat2::zero(); + } + + /// Returns the the matrix with an extra row and column added + /// ~~~ + /// c0 c1 c0 c1 c2 + /// +----+----+ +----+----+----+ + /// r0 | a | b | r0 | a | b | 0 | + /// +----+----+ +----+----+----+ + /// r1 | c | d | => r1 | c | d | 0 | + /// +----+----+ +----+----+----+ + /// r2 | 0 | 0 | 1 | + /// +----+----+----+ + /// ~~~ + #[inline] + pub fn to_mat3(&self) -> Mat3 { + Mat3::new(*self.elem(0, 0), *self.elem(0, 1), Zero::zero(), + *self.elem(1, 0), *self.elem(1, 1), Zero::zero(), + Zero::zero(), Zero::zero(), One::one()) + } + + /// Returns the the matrix with an extra two rows and columns added + /// ~~~ + /// c0 c1 c0 c1 c2 c3 + /// +----+----+ +----+----+----+----+ + /// r0 | a | b | r0 | a | b | 0 | 0 | + /// +----+----+ +----+----+----+----+ + /// r1 | c | d | => r1 | c | d | 0 | 0 | + /// +----+----+ +----+----+----+----+ + /// r2 | 0 | 0 | 1 | 0 | + /// +----+----+----+----+ + /// r3 | 0 | 0 | 0 | 1 | + /// +----+----+----+----+ + /// ~~~ + #[inline] + pub fn to_mat4(&self) -> Mat4 { + Mat4::new(*self.elem(0, 0), *self.elem(0, 1), Zero::zero(), Zero::zero(), + *self.elem(1, 0), *self.elem(1, 1), Zero::zero(), Zero::zero(), + Zero::zero(), Zero::zero(), One::one(), Zero::zero(), + Zero::zero(), Zero::zero(), Zero::zero(), One::one()) + } +} + +impl Neg> for Mat2 { + #[inline] + pub fn neg(&self) -> Mat2 { + Mat2::from_cols(-self.col(0), -self.col(1)) + } +} + +impl Mat2 { + #[inline] + pub fn from_angle(radians: T) -> Mat2 { + let cos_theta = radians.cos(); + let sin_theta = radians.sin(); + + Mat2::new(cos_theta, -sin_theta, + sin_theta, cos_theta) + } +} + +impl> Mat2 { + #[inline] + pub fn inverse(&self) -> Option> { + let d = self.determinant(); + if d.approx_eq(&Zero::zero()) { + None + } else { + Some(Mat2::new(self.elem(1, 1) / d, -self.elem(0, 1) / d, + -self.elem(1, 0) / d, self.elem(0, 0) / d)) + } + } + + #[inline] + pub fn invert_self(&mut self) { + *self = self.inverse().expect("Couldn't invert the matrix!"); + } + + #[inline] + pub fn is_identity(&self) -> bool { + self.approx_eq(&Mat2::identity()) + } + + #[inline] + pub fn is_diagonal(&self) -> bool { + self.elem(0, 1).approx_eq(&Zero::zero()) && + self.elem(1, 0).approx_eq(&Zero::zero()) + } + + #[inline] + pub fn is_rotated(&self) -> bool { + !self.approx_eq(&Mat2::identity()) + } + + #[inline] + pub fn is_symmetric(&self) -> bool { + self.elem(0, 1).approx_eq(self.elem(1, 0)) && + self.elem(1, 0).approx_eq(self.elem(0, 1)) + } + + #[inline] + pub fn is_invertible(&self) -> bool { + !self.determinant().approx_eq(&Zero::zero()) + } +} + +impl> ApproxEq for Mat2 { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Mat2) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Mat2, epsilon: &T) -> bool { + self.col(0).approx_eq_eps(other.col(0), epsilon) && + self.col(1).approx_eq_eps(other.col(1), epsilon) + } +} diff --git a/src/mat3.rs b/src/mat3.rs new file mode 100644 index 0000000..030921f --- /dev/null +++ b/src/mat3.rs @@ -0,0 +1,550 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::cast::transmute; +use std::cmp::ApproxEq; +use std::num::{Zero, One}; + +use vec::*; +use quat::Quat; +use super::Mat4; + +#[deriving(Eq)] +pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } + +impl Mat3 { + #[inline] + pub fn col<'a>(&'a self, i: uint) -> &'a Vec3 { + &'a self.as_slice()[i] + } + + #[inline] + pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3 { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [Vec3,..3] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec3,..3] { + unsafe { transmute(self) } + } + + #[inline] + pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { + self.col(i).index(j) + } + + #[inline] + pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { + self.col_mut(i).index_mut(j) + } +} + +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] + pub 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(c0r0, c0r1, c0r2), + Vec3::new(c1r0, c1r1, c1r2), + Vec3::new(c2r0, c2r1, c2r2)) + } + + /// Construct a 3 x 3 matrix from column vectors + /// + /// # Arguments + /// + /// - `c0`: the first column vector of the matrix + /// - `c1`: the second column vector of the matrix + /// - `c2`: the third column vector of the matrix + /// + /// ~~~ + /// c0 c1 c2 + /// +------+------+------+ + /// r0 | c0.x | c1.x | c2.x | + /// +------+------+------+ + /// r1 | c0.y | c1.y | c2.y | + /// +------+------+------+ + /// r2 | c0.z | c1.z | c2.z | + /// +------+------+------+ + /// ~~~ + #[inline] + pub fn from_cols(c0: Vec3, + c1: Vec3, + c2: Vec3) -> Mat3 { + Mat3 { x: c0, y: c1, z: c2 } + } + + #[inline] + pub fn row(&self, i: uint) -> Vec3 { + Vec3::new(*self.elem(0, i), + *self.elem(1, i), + *self.elem(2, i)) + } + + #[inline] + pub fn swap_cols(&mut self, a: uint, b: uint) { + let tmp = *self.col(a); + *self.col_mut(a) = *self.col(b); + *self.col_mut(b) = tmp; + } + + #[inline] + pub 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] + pub fn transpose(&self) -> Mat3 { + Mat3::new(*self.elem(0, 0), *self.elem(1, 0), *self.elem(2, 0), + *self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), + *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2)) + } + + #[inline] + pub fn transpose_self(&mut self) { + let tmp01 = *self.elem(0, 1); + let tmp02 = *self.elem(0, 2); + let tmp10 = *self.elem(1, 0); + let tmp12 = *self.elem(1, 2); + let tmp20 = *self.elem(2, 0); + let tmp21 = *self.elem(2, 1); + + *self.elem_mut(0, 1) = *self.elem(1, 0); + *self.elem_mut(0, 2) = *self.elem(2, 0); + *self.elem_mut(1, 0) = *self.elem(0, 1); + *self.elem_mut(1, 2) = *self.elem(2, 1); + *self.elem_mut(2, 0) = *self.elem(0, 2); + *self.elem_mut(2, 1) = *self.elem(1, 2); + + *self.elem_mut(1, 0) = tmp01; + *self.elem_mut(2, 0) = tmp02; + *self.elem_mut(0, 1) = tmp10; + *self.elem_mut(2, 1) = tmp12; + *self.elem_mut(0, 2) = tmp20; + *self.elem_mut(1, 2) = tmp21; + } +} + +impl Mat3 { + /// 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] + pub fn from_value(value: T) -> Mat3 { + Mat3::new(value, Zero::zero(), Zero::zero(), + Zero::zero(), value, Zero::zero(), + Zero::zero(), Zero::zero(), value) + } + + /// Returns the multiplicative identity matrix + /// ~~~ + /// c0 c1 c2 + /// +----+----+----+ + /// r0 | 1 | 0 | 0 | + /// +----+----+----+ + /// r1 | 0 | 1 | 0 | + /// +----+----+----+ + /// r2 | 0 | 0 | 1 | + /// +----+----+----+ + /// ~~~ + #[inline] + pub fn identity() -> Mat3 { + Mat3::new(One::one::(), Zero::zero::(), Zero::zero::(), + Zero::zero::(), One::one::(), Zero::zero::(), + Zero::zero::(), Zero::zero::(), One::one::()) + } + + /// Returns the additive identity matrix + /// ~~~ + /// c0 c1 c2 + /// +----+----+----+ + /// r0 | 0 | 0 | 0 | + /// +----+----+----+ + /// r1 | 0 | 0 | 0 | + /// +----+----+----+ + /// r2 | 0 | 0 | 0 | + /// +----+----+----+ + /// ~~~ + #[inline] + pub fn zero() -> Mat3 { + Mat3::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), + Zero::zero::(), Zero::zero::(), Zero::zero::(), + Zero::zero::(), Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn mul_t(&self, value: T) -> Mat3 { + Mat3::from_cols(self.col(0).mul_t(value), + self.col(1).mul_t(value), + self.col(2).mul_t(value)) + } + + #[inline] + pub 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] + pub fn add_m(&self, other: &Mat3) -> Mat3 { + Mat3::from_cols(self.col(0).add_v(other.col(0)), + self.col(1).add_v(other.col(1)), + self.col(2).add_v(other.col(2))) + } + + #[inline] + pub fn sub_m(&self, other: &Mat3) -> Mat3 { + Mat3::from_cols(self.col(0).sub_v(other.col(0)), + self.col(1).sub_v(other.col(1)), + self.col(2).sub_v(other.col(2))) + } + + #[inline] + pub 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))) + } + + #[inline] + pub 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] + pub fn add_self_m(&mut self, other: &Mat3) { + self.col_mut(0).add_self_v(other.col(0)); + self.col_mut(1).add_self_v(other.col(1)); + self.col_mut(2).add_self_v(other.col(2)); + } + + #[inline] + pub fn sub_self_m(&mut self, other: &Mat3) { + self.col_mut(0).sub_self_v(other.col(0)); + self.col_mut(1).sub_self_v(other.col(1)); + self.col_mut(2).sub_self_v(other.col(2)); + } + + pub fn dot(&self, other: &Mat3) -> T { + other.transpose().mul_m(self).trace() + } + + pub fn determinant(&self) -> T { + self.col(0).dot(&self.col(1).cross(self.col(2))) + } + + pub fn trace(&self) -> T { + *self.elem(0, 0) + + *self.elem(1, 1) + + *self.elem(2, 2) + } + + #[inline] + pub fn to_identity(&mut self) { + *self = Mat3::identity(); + } + + #[inline] + pub fn to_zero(&mut self) { + *self = Mat3::zero(); + } + + /// Returns the the matrix with an extra row and column added + /// ~~~ + /// c0 c1 c2 c0 c1 c2 c3 + /// +----+----+----+ +----+----+----+----+ + /// r0 | a | b | c | r0 | a | b | c | 0 | + /// +----+----+----+ +----+----+----+----+ + /// r1 | d | e | f | => r1 | d | e | f | 0 | + /// +----+----+----+ +----+----+----+----+ + /// r2 | g | h | i | r2 | g | h | i | 0 | + /// +----+----+----+ +----+----+----+----+ + /// r3 | 0 | 0 | 0 | 1 | + /// +----+----+----+----+ + /// ~~~ + #[inline] + pub fn to_mat4(&self) -> Mat4 { + Mat4::new(*self.elem(0, 0), *self.elem(0, 1), *self.elem(0, 2), Zero::zero(), + *self.elem(1, 0), *self.elem(1, 1), *self.elem(1, 2), Zero::zero(), + *self.elem(2, 0), *self.elem(2, 1), *self.elem(2, 2), Zero::zero(), + Zero::zero(), Zero::zero(), Zero::zero(), One::one()) + } +} + +impl Neg> for Mat3 { + #[inline] + pub fn neg(&self) -> Mat3 { + Mat3::from_cols(-self.col(0), -self.col(1), -self.col(2)) + } +} + +impl Mat3 { + /// Construct a matrix from an angular rotation around the `x` axis + pub fn from_angle_x(radians: T) -> Mat3 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let cos_theta = radians.cos(); + let sin_theta = radians.sin(); + + Mat3::new(One::one(), Zero::zero(), Zero::zero(), + Zero::zero(), cos_theta, sin_theta, + Zero::zero(), -sin_theta, cos_theta) + } + + /// Construct a matrix from an angular rotation around the `y` axis + pub fn from_angle_y(radians: T) -> Mat3 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let cos_theta = radians.cos(); + let sin_theta = radians.sin(); + + Mat3::new(cos_theta, Zero::zero(), -sin_theta, + Zero::zero(), One::one(), Zero::zero(), + sin_theta, Zero::zero(), cos_theta) + } + + /// Construct a matrix from an angular rotation around the `z` axis + pub fn from_angle_z(radians: T) -> Mat3 { + // http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations + let cos_theta = radians.cos(); + let sin_theta = radians.sin(); + + Mat3::new(cos_theta, sin_theta, Zero::zero(), + -sin_theta, cos_theta, Zero::zero(), + Zero::zero(), Zero::zero(), One::one()) + } + + /// Construct a matrix from Euler angles + /// + /// # Arguments + /// + /// - `theta_x`: the angular rotation around the `x` axis (pitch) + /// - `theta_y`: the angular rotation around the `y` axis (yaw) + /// - `theta_z`: the angular rotation around the `z` axis (roll) + pub fn from_angle_xyz(radians_x: T, radians_y: T, radians_z: T) -> Mat3 { + // http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations + let cx = radians_x.cos(); + let sx = radians_x.sin(); + let cy = radians_y.cos(); + let sy = radians_y.sin(); + let cz = radians_z.cos(); + let sz = radians_z.sin(); + + Mat3::new(cy*cz, cy*sz, -sy, + -cx*sz + sx*sy*cz, cx*cz + sx*sy*sz, sx*cy, + sx*sz + cx*sy*cz, -sx*cz + cx*sy*sz, cx*cy) + } + + /// Construct a matrix from an axis and an angular rotation + pub fn from_angle_axis(radians: T, axis: &Vec3) -> Mat3 { + let c = radians.cos(); + let s = radians.sin(); + let _1_c = One::one::() - 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] + pub fn from_axes(x: Vec3, y: Vec3, z: Vec3) -> Mat3 { + Mat3::from_cols(x, y, z) + } + + pub fn look_at(dir: &Vec3, up: &Vec3) -> Mat3 { + let dir_ = dir.normalize(); + let side = dir_.cross(&up.normalize()); + let up_ = side.cross(&dir_).normalize(); + + Mat3::from_axes(up_, side, dir_) + } + + /// Convert the matrix to a quaternion + pub fn to_quat(&self) -> 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; let x; let y; let z; + let trace = self.trace(); + + // FIXME: We don't have any numeric conversions in std yet :P + let half = One::one::() / (One::one::() + One::one::()); + + cond! ( + (trace >= Zero::zero()) { + s = (One::one::() + trace).sqrt(); + w = half * s; + s = half / s; + x = (*self.elem(1, 2) - *self.elem(2, 1)) * s; + y = (*self.elem(2, 0) - *self.elem(0, 2)) * s; + z = (*self.elem(0, 1) - *self.elem(1, 0)) * s; + } + ((*self.elem(0, 0) > *self.elem(1, 1)) + && (*self.elem(0, 0) > *self.elem(2, 2))) { + s = (half + (*self.elem(0, 0) - *self.elem(1, 1) - *self.elem(2, 2))).sqrt(); + w = half * s; + s = half / s; + x = (*self.elem(0, 1) - *self.elem(1, 0)) * s; + y = (*self.elem(2, 0) - *self.elem(0, 2)) * s; + z = (*self.elem(1, 2) - *self.elem(2, 1)) * s; + } + (*self.elem(1, 1) > *self.elem(2, 2)) { + s = (half + (*self.elem(1, 1) - *self.elem(0, 0) - *self.elem(2, 2))).sqrt(); + w = half * s; + s = half / s; + x = (*self.elem(0, 1) - *self.elem(1, 0)) * s; + y = (*self.elem(1, 2) - *self.elem(2, 1)) * s; + z = (*self.elem(2, 0) - *self.elem(0, 2)) * s; + } + _ { + s = (half + (*self.elem(2, 2) - *self.elem(0, 0) - *self.elem(1, 1))).sqrt(); + w = half * s; + s = half / s; + x = (*self.elem(2, 0) - *self.elem(0, 2)) * s; + y = (*self.elem(1, 2) - *self.elem(2, 1)) * s; + z = (*self.elem(0, 1) - *self.elem(1, 0)) * s; + } + ) + Quat::new(w, x, y, z) + } +} + +impl> Mat3 { + pub fn inverse(&self) -> Option> { + let d = self.determinant(); + if d.approx_eq(&Zero::zero()) { + None + } else { + Some(Mat3::from_cols(self.col(1).cross(self.col(2)).div_t(d), + self.col(2).cross(self.col(0)).div_t(d), + self.col(0).cross(self.col(1)).div_t(d)).transpose()) + } + } + + #[inline] + pub fn invert_self(&mut self) { + *self = self.inverse().expect("Couldn't invert the matrix!"); + } + + #[inline] + pub fn is_identity(&self) -> bool { + self.approx_eq(&Mat3::identity()) + } + + #[inline] + pub fn is_diagonal(&self) -> bool { + self.elem(0, 1).approx_eq(&Zero::zero()) && + self.elem(0, 2).approx_eq(&Zero::zero()) && + + self.elem(1, 0).approx_eq(&Zero::zero()) && + self.elem(1, 2).approx_eq(&Zero::zero()) && + + self.elem(2, 0).approx_eq(&Zero::zero()) && + self.elem(2, 1).approx_eq(&Zero::zero()) + } + + #[inline] + pub fn is_rotated(&self) -> bool { + !self.approx_eq(&Mat3::identity()) + } + + #[inline] + pub fn is_symmetric(&self) -> bool { + self.elem(0, 1).approx_eq(self.elem(1, 0)) && + self.elem(0, 2).approx_eq(self.elem(2, 0)) && + + self.elem(1, 0).approx_eq(self.elem(0, 1)) && + self.elem(1, 2).approx_eq(self.elem(2, 1)) && + + self.elem(2, 0).approx_eq(self.elem(0, 2)) && + self.elem(2, 1).approx_eq(self.elem(1, 2)) + } + + #[inline] + pub fn is_invertible(&self) -> bool { + !self.determinant().approx_eq(&Zero::zero()) + } +} + +impl> ApproxEq for Mat3 { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Mat3) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Mat3, epsilon: &T) -> bool { + self.col(0).approx_eq_eps(other.col(0), epsilon) && + self.col(1).approx_eq_eps(other.col(1), epsilon) && + self.col(2).approx_eq_eps(other.col(2), epsilon) + } +} diff --git a/src/mat4.rs b/src/mat4.rs new file mode 100644 index 0000000..f345e20 --- /dev/null +++ b/src/mat4.rs @@ -0,0 +1,528 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::cast::transmute; +use std::cmp::ApproxEq; +use std::num::{Zero, One}; +use std::uint; + +use vec::*; +use super::Mat3; + +/// A 4 x 4 column major matrix +/// +/// # Type parameters +/// +/// - `T` - The type of the elements of the matrix. Should be a floating point type. +/// +/// # Fields +/// +/// - `x`: the first column vector of the matrix +/// - `y`: the second column vector of the matrix +/// - `z`: the third column vector of the matrix +/// - `w`: the fourth column vector of the matrix +#[deriving(Eq)] +pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } + +impl Mat4 { + #[inline] + pub fn col<'a>(&'a self, i: uint) -> &'a Vec4 { + &'a self.as_slice()[i] + } + + #[inline] + pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4 { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [Vec4,..4] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec4,..4] { + unsafe { transmute(self) } + } + + #[inline] + pub fn elem<'a>(&'a self, i: uint, j: uint) -> &'a T { + self.col(i).index(j) + } + + #[inline] + pub fn elem_mut<'a>(&'a mut self, i: uint, j: uint) -> &'a mut T { + self.col_mut(i).index_mut(j) + } +} + +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] + pub 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(c0r0, c0r1, c0r2, c0r3), + Vec4::new(c1r0, c1r1, c1r2, c1r3), + Vec4::new(c2r0, c2r1, c2r2, c2r3), + Vec4::new(c3r0, c3r1, c3r2, c3r3)) + } + + /// Construct a 4 x 4 matrix from column vectors + /// + /// # Arguments + /// + /// - `c0`: the first column vector of the matrix + /// - `c1`: the second column vector of the matrix + /// - `c2`: the third column vector of the matrix + /// - `c3`: the fourth column vector of the matrix + /// + /// ~~~ + /// c0 c1 c2 c3 + /// +------+------+------+------+ + /// r0 | c0.x | c1.x | c2.x | c3.x | + /// +------+------+------+------+ + /// r1 | c0.y | c1.y | c2.y | c3.y | + /// +------+------+------+------+ + /// r2 | c0.z | c1.z | c2.z | c3.z | + /// +------+------+------+------+ + /// r3 | c0.w | c1.w | c2.w | c3.w | + /// +------+------+------+------+ + /// ~~~ + #[inline] + pub fn from_cols(c0: Vec4, + c1: Vec4, + c2: Vec4, + c3: Vec4) -> Mat4 { + Mat4 { x: c0, y: c1, z: c2, w: c3 } + } + + #[inline] + pub fn row(&self, i: uint) -> Vec4 { + Vec4::new(*self.elem(0, i), + *self.elem(1, i), + *self.elem(2, i), + *self.elem(3, i)) + } + + #[inline] + pub fn swap_cols(&mut self, a: uint, b: uint) { + let tmp = *self.col(a); + *self.col_mut(a) = *self.col(b); + *self.col_mut(b) = tmp; + } + + #[inline] + pub 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] + pub fn transpose(&self) -> Mat4 { + Mat4::new(*self.elem(0, 0), *self.elem(1, 0), *self.elem(2, 0), *self.elem(3, 0), + *self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), *self.elem(3, 1), + *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2), *self.elem(3, 2), + *self.elem(0, 3), *self.elem(1, 3), *self.elem(2, 3), *self.elem(3, 3)) + } + + #[inline] + pub fn transpose_self(&mut self) { + let tmp01 = *self.elem(0, 1); + let tmp02 = *self.elem(0, 2); + let tmp03 = *self.elem(0, 3); + let tmp10 = *self.elem(1, 0); + let tmp12 = *self.elem(1, 2); + let tmp13 = *self.elem(1, 3); + let tmp20 = *self.elem(2, 0); + let tmp21 = *self.elem(2, 1); + let tmp23 = *self.elem(2, 3); + let tmp30 = *self.elem(3, 0); + let tmp31 = *self.elem(3, 1); + let tmp32 = *self.elem(3, 2); + + *self.elem_mut(0, 1) = *self.elem(1, 0); + *self.elem_mut(0, 2) = *self.elem(2, 0); + *self.elem_mut(0, 3) = *self.elem(3, 0); + *self.elem_mut(1, 0) = *self.elem(0, 1); + *self.elem_mut(1, 2) = *self.elem(2, 1); + *self.elem_mut(1, 3) = *self.elem(3, 1); + *self.elem_mut(2, 0) = *self.elem(0, 2); + *self.elem_mut(2, 1) = *self.elem(1, 2); + *self.elem_mut(2, 3) = *self.elem(3, 2); + *self.elem_mut(3, 0) = *self.elem(0, 3); + *self.elem_mut(3, 1) = *self.elem(1, 3); + *self.elem_mut(3, 2) = *self.elem(2, 3); + + *self.elem_mut(1, 0) = tmp01; + *self.elem_mut(2, 0) = tmp02; + *self.elem_mut(3, 0) = tmp03; + *self.elem_mut(0, 1) = tmp10; + *self.elem_mut(2, 1) = tmp12; + *self.elem_mut(3, 1) = tmp13; + *self.elem_mut(0, 2) = tmp20; + *self.elem_mut(1, 2) = tmp21; + *self.elem_mut(3, 2) = tmp23; + *self.elem_mut(0, 3) = tmp30; + *self.elem_mut(1, 3) = tmp31; + *self.elem_mut(2, 3) = tmp32; + } +} + +impl Mat4 { + /// 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] + pub fn from_value(value: T) -> Mat4 { + Mat4::new(value, Zero::zero(), Zero::zero(), Zero::zero(), + Zero::zero(), value, Zero::zero(), Zero::zero(), + Zero::zero(), Zero::zero(), value, Zero::zero(), + Zero::zero(), Zero::zero(), Zero::zero(), value) + } + + /// Returns the multiplicative identity matrix + /// ~~~ + /// c0 c1 c2 c3 + /// +----+----+----+----+ + /// r0 | 1 | 0 | 0 | 0 | + /// +----+----+----+----+ + /// r1 | 0 | 1 | 0 | 0 | + /// +----+----+----+----+ + /// r2 | 0 | 0 | 1 | 0 | + /// +----+----+----+----+ + /// r3 | 0 | 0 | 0 | 1 | + /// +----+----+----+----+ + /// ~~~ + #[inline] + pub fn identity() -> Mat4 { + Mat4::new(One::one::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), + Zero::zero::(), One::one::(), Zero::zero::(), Zero::zero::(), + Zero::zero::(), Zero::zero::(), One::one::(), Zero::zero::(), + Zero::zero::(), Zero::zero::(), Zero::zero::(), One::one::()) + } + + /// 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] + pub fn zero() -> Mat4 { + Mat4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), + Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), + Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::(), + Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn mul_t(&self, value: T) -> Mat4 { + Mat4::from_cols(self.col(0).mul_t(value), + self.col(1).mul_t(value), + self.col(2).mul_t(value), + self.col(3).mul_t(value)) + } + + #[inline] + pub 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] + pub fn add_m(&self, other: &Mat4) -> Mat4 { + Mat4::from_cols(self.col(0).add_v(other.col(0)), + self.col(1).add_v(other.col(1)), + self.col(2).add_v(other.col(2)), + self.col(3).add_v(other.col(3))) + } + + #[inline] + pub fn sub_m(&self, other: &Mat4) -> Mat4 { + Mat4::from_cols(self.col(0).sub_v(other.col(0)), + self.col(1).sub_v(other.col(1)), + self.col(2).sub_v(other.col(2)), + self.col(3).sub_v(other.col(3))) + } + + #[inline] + pub fn mul_m(&self, other: &Mat4) -> Mat4 { + 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))) + } + + #[inline] + pub 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] + pub fn add_self_m(&mut self, other: &Mat4) { + self.col_mut(0).add_self_v(other.col(0)); + self.col_mut(1).add_self_v(other.col(1)); + self.col_mut(2).add_self_v(other.col(2)); + self.col_mut(3).add_self_v(other.col(3)); + } + + #[inline] + pub fn sub_self_m(&mut self, other: &Mat4) { + self.col_mut(0).sub_self_v(other.col(0)); + self.col_mut(1).sub_self_v(other.col(1)); + self.col_mut(2).sub_self_v(other.col(2)); + self.col_mut(3).sub_self_v(other.col(3)); + } + + pub fn dot(&self, other: &Mat4) -> T { + other.transpose().mul_m(self).trace() + } + + pub fn determinant(&self) -> T { + let m0 = Mat3::new(*self.elem(1, 1), *self.elem(2, 1), *self.elem(3, 1), + *self.elem(1, 2), *self.elem(2, 2), *self.elem(3, 2), + *self.elem(1, 3), *self.elem(2, 3), *self.elem(3, 3)); + let m1 = Mat3::new(*self.elem(0, 1), *self.elem(2, 1), *self.elem(3, 1), + *self.elem(0, 2), *self.elem(2, 2), *self.elem(3, 2), + *self.elem(0, 3), *self.elem(2, 3), *self.elem(3, 3)); + let m2 = Mat3::new(*self.elem(0, 1), *self.elem(1, 1), *self.elem(3, 1), + *self.elem(0, 2), *self.elem(1, 2), *self.elem(3, 2), + *self.elem(0, 3), *self.elem(1, 3), *self.elem(3, 3)); + let m3 = Mat3::new(*self.elem(0, 1), *self.elem(1, 1), *self.elem(2, 1), + *self.elem(0, 2), *self.elem(1, 2), *self.elem(2, 2), + *self.elem(0, 3), *self.elem(1, 3), *self.elem(2, 3)); + + self.elem(0, 0) * m0.determinant() - + self.elem(1, 0) * m1.determinant() + + self.elem(2, 0) * m2.determinant() - + self.elem(3, 0) * m3.determinant() + } + + pub fn trace(&self) -> T { + *self.elem(0, 0) + + *self.elem(1, 1) + + *self.elem(2, 2) + + *self.elem(3, 3) + } + + #[inline] + pub fn to_identity(&mut self) { + *self = Mat4::identity(); + } + + #[inline] + pub fn to_zero(&mut self) { + *self = Mat4::zero(); + } +} + +impl Neg> for Mat4 { + #[inline] + pub fn neg(&self) -> Mat4 { + Mat4::from_cols(-self.col(0), -self.col(1), -self.col(2), -self.col(3)) + } +} + +impl> Mat4 { + pub fn inverse(&self) -> Option> { + let d = self.determinant(); + if d.approx_eq(&Zero::zero()) { + None + } else { + // Gauss Jordan Elimination with partial pivoting + // So take this matrix, A, augmented with the identity + // and essentially reduce [A|I] + + let mut A = *self; + let mut I = Mat4::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 A.elem(j, i).abs() > A.elem(j, i1).abs() { + i1 = i; + } + } + + // 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 + let ajj = *A.elem(j, j); + I.col_mut(j).div_self_t(ajj); + A.col_mut(j).div_self_t(ajj); + + // Eliminate off-diagonal elems in col j of A, + // doing identical ops to I + for uint::range(0, 4) |i| { + if i != j { + let ij_mul_aij = I.col(j).mul_t(*A.elem(i, j)); + let aj_mul_aij = A.col(j).mul_t(*A.elem(i, j)); + I.col_mut(i).sub_self_v(&ij_mul_aij); + A.col_mut(i).sub_self_v(&aj_mul_aij); + } + } + } + Some(I) + } + } + + #[inline] + pub fn invert_self(&mut self) { + *self = self.inverse().expect("Couldn't invert the matrix!"); + } + + #[inline] + pub fn is_identity(&self) -> bool { + self.approx_eq(&Mat4::identity()) + } + + #[inline] + pub fn is_diagonal(&self) -> bool { + self.elem(0, 1).approx_eq(&Zero::zero()) && + self.elem(0, 2).approx_eq(&Zero::zero()) && + self.elem(0, 3).approx_eq(&Zero::zero()) && + + self.elem(1, 0).approx_eq(&Zero::zero()) && + self.elem(1, 2).approx_eq(&Zero::zero()) && + self.elem(1, 3).approx_eq(&Zero::zero()) && + + self.elem(2, 0).approx_eq(&Zero::zero()) && + self.elem(2, 1).approx_eq(&Zero::zero()) && + self.elem(2, 3).approx_eq(&Zero::zero()) && + + self.elem(3, 0).approx_eq(&Zero::zero()) && + self.elem(3, 1).approx_eq(&Zero::zero()) && + self.elem(3, 2).approx_eq(&Zero::zero()) + } + + #[inline] + pub fn is_rotated(&self) -> bool { + !self.approx_eq(&Mat4::identity()) + } + + #[inline] + pub fn is_symmetric(&self) -> bool { + self.elem(0, 1).approx_eq(self.elem(1, 0)) && + self.elem(0, 2).approx_eq(self.elem(2, 0)) && + self.elem(0, 3).approx_eq(self.elem(3, 0)) && + + self.elem(1, 0).approx_eq(self.elem(0, 1)) && + self.elem(1, 2).approx_eq(self.elem(2, 1)) && + self.elem(1, 3).approx_eq(self.elem(3, 1)) && + + self.elem(2, 0).approx_eq(self.elem(0, 2)) && + self.elem(2, 1).approx_eq(self.elem(1, 2)) && + self.elem(2, 3).approx_eq(self.elem(3, 2)) && + + self.elem(3, 0).approx_eq(self.elem(0, 3)) && + self.elem(3, 1).approx_eq(self.elem(1, 3)) && + self.elem(3, 2).approx_eq(self.elem(2, 3)) + } + + #[inline] + pub fn is_invertible(&self) -> bool { + !self.determinant().approx_eq(&Zero::zero()) + } +} + +impl> ApproxEq for Mat4 { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Mat4) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Mat4, epsilon: &T) -> bool { + self.col(0).approx_eq_eps(other.col(0), epsilon) && + self.col(1).approx_eq_eps(other.col(1), epsilon) && + self.col(2).approx_eq_eps(other.col(2), epsilon) && + self.col(3).approx_eq_eps(other.col(3), epsilon) + } +} diff --git a/src/projection.rs b/src/projection.rs index 293f284..ee966ec 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -16,12 +16,6 @@ use std::num::{Zero, One}; use mat::Mat4; -// FIXME: We can remove this once we have numeric conversions in std -#[inline] -priv fn two() -> T { - One::one::() + One::one::() -} - /// /// Create a perspective projection matrix /// @@ -102,3 +96,9 @@ pub fn ortho(left: T, right: T, bottom: T, top: T, near: T, far: c2r0, c2r1, c2r2, c2r3, c3r0, c3r1, c3r2, c3r3) } + +// FIXME: We can remove this once we have numeric conversions in std +#[inline] +priv fn two() -> T { + One::one::() + One::one::() +} diff --git a/src/quat.rs b/src/quat.rs index 7f4aeb5..753c796 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -20,11 +20,16 @@ use std::num::{Zero, One, cast}; use mat::Mat3; use vec::Vec3; -// FIXME: We can remove this once we have numeric conversions in std -#[inline] -priv fn two() -> T { - One::one::() + One::one::() -} +// GLSL-style type aliases + +pub type quat = Quat; +pub type dquat = Quat; + +// Rust-style type aliases + +pub type Quatf = Quat; +pub type Quatf32 = Quat; +pub type Quatf64 = Quat; /// A quaternion in scalar/vector form /// @@ -379,11 +384,8 @@ impl> ApproxEq for Quat { } } -// GLSL-style type aliases -type quat = Quat; -type dquat = Quat; - -// Rust-style type aliases -type Quatf = Quat; -type Quatf32 = Quat; -type Quatf64 = Quat; +// FIXME: We can remove this once we have numeric conversions in std +#[inline] +priv fn two() -> T { + One::one::() + One::one::() +} diff --git a/src/vec.rs b/src/vec.rs index 6de3389..aec298e 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -13,416 +13,36 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::cast::transmute; -use std::cmp::ApproxEq; -use std::num::{Zero, One}; +pub use self::vec2::Vec2; +pub use self::vec3::Vec3; +pub use self::vec4::Vec4; - #[deriving(Eq)] -pub struct Vec2 { x: T, y: T } - -impl Vec2 { - #[inline] - pub fn index<'a>(&'a self, i: uint) -> &'a T { - &'a self.as_slice()[i] - } - - #[inline] - pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [T,..2] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..2] { - unsafe { transmute(self) } - } -} - -impl Vec2 { - #[inline] - pub fn new(x: T, y: T ) -> Vec2 { - Vec2 { x: x, y: y } - } - - #[inline] - pub fn from_value(value: T) -> Vec2 { - Vec2::new(value, value) - } - - #[inline] - pub fn swap(&mut self, a: uint, b: uint) { - let tmp = *self.index(a); - *self.index_mut(a) = *self.index(b); - *self.index_mut(b) = tmp; - } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec2 { - Vec2::new(f(self.index(0)), - f(self.index(1))) - } -} - -impl Vec2 { - #[inline] - pub fn identity() -> Vec2 { - Vec2::new(One::one::(), One::one::()) - } - - #[inline] - pub fn zero() -> Vec2 { - Vec2::new(Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_x() -> Vec2 { - Vec2::new(One::one::(), Zero::zero::()) - } - - #[inline] - pub fn unit_y() -> Vec2 { - Vec2::new(Zero::zero::(), One::one::()) - } - - #[inline] - pub fn is_zero(&self) -> bool { - *self.index(0) == Zero::zero() && - *self.index(1) == Zero::zero() - } - - #[inline] - pub fn add_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) + value, - *self.index(1) + value) - } - - #[inline] - pub fn sub_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) - value, - *self.index(1) - value) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) * value, - *self.index(1) * value) - } - - #[inline] - pub fn div_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) / value, - *self.index(1) / value) - } - - #[inline] - pub fn rem_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) % value, - *self.index(1) % value) - } - - #[inline] - pub fn add_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) + *other.index(0), - *self.index(1) + *other.index(1)) - } - - #[inline] - pub fn sub_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) - *other.index(0), - *self.index(1) - *other.index(1)) - } - - #[inline] - pub fn mul_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) * *other.index(0), - *self.index(1) * *other.index(1)) - } - - #[inline] - pub fn div_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) / *other.index(0), - *self.index(1) / *other.index(1)) - } - - #[inline] - pub fn rem_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) % *other.index(0), - *self.index(1) % *other.index(1)) - } - - #[inline] - pub fn neg_self(&mut self) { - *self.index_mut(0) = -*self.index(0); - *self.index_mut(1) = -*self.index(1); - } - - #[inline] - pub fn add_self_t(&mut self, value: T) { - *self.index_mut(0) += value; - *self.index_mut(1) += value; - } - - #[inline] - pub fn sub_self_t(&mut self, value: T) { - *self.index_mut(0) -= value; - *self.index_mut(1) -= value; - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - *self.index_mut(0) *= value; - *self.index_mut(1) *= value; - } - - #[inline] - pub fn div_self_t(&mut self, value: T) { - *self.index_mut(0) /= value; - *self.index_mut(1) /= value; - } - - #[inline] - pub fn rem_self_t(&mut self, value: T) { - *self.index_mut(0) %= value; - *self.index_mut(1) %= value; - } - - #[inline] - pub fn add_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) += *other.index(0); - *self.index_mut(1) += *other.index(1); - } - - #[inline] - pub fn sub_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) -= *other.index(0); - *self.index_mut(1) -= *other.index(1); - } - - #[inline] - pub fn mul_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) *= *other.index(0); - *self.index_mut(1) *= *other.index(1); - } - - #[inline] - pub fn div_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - } - - #[inline] - pub fn rem_self_v(&mut self, other: &Vec2) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - } - - #[inline] - pub fn dot(&self, other: &Vec2) -> T { - *self.index(0) * *other.index(0) + - *self.index(1) * *other.index(1) - } - - #[inline] - pub fn perp_dot(&self, other: &Vec2) -> T { - (*self.index(0) * *other.index(1)) - - (*self.index(1) * *other.index(0)) - } - - #[inline] - pub fn to_homogeneous(&self) -> Vec3 { - Vec3::new(self.x, self.y, Zero::zero()) - } -} - -impl Neg> for Vec2 { - #[inline] - pub fn neg(&self) -> Vec2 { - Vec2::new(-self.index(0), -self.index(1)) - } -} - -impl Vec2 { - #[inline] - pub fn length2(&self) -> T { - self.dot(self) - } - - #[inline] - pub fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline] - pub fn distance2(&self, other: &Vec2) -> T { - other.sub_v(self).length2() - } - - #[inline] - pub fn distance(&self, other: &Vec2) -> T { - other.distance2(self).sqrt() - } - - #[inline] - pub fn angle(&self, other: &Vec2) -> T { - self.perp_dot(other).atan2(self.dot(other)) - } - - #[inline] - pub fn normalize(&self) -> Vec2 { - self.mul_t(One::one::()/self.length()) - } - - #[inline] - pub fn normalize_to(&self, length: T) -> Vec2 { - self.mul_t(length / self.length()) - } - - #[inline] - pub fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } - - #[inline] - pub fn normalize_self(&mut self) { - let n = One::one::() / self.length(); - self.mul_self_t(n); - } - - #[inline] - pub fn normalize_self_to(&mut self, length: T) { - let n = length / self.length(); - self.mul_self_t(n); - } - - pub fn lerp_self(&mut self, other: &Vec2, amount: T) { - let v = other.sub_v(self).mul_t(amount); - self.add_self_v(&v); - } -} - -impl> ApproxEq for Vec2 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Vec2) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool { - self.index(0).approx_eq_eps(other.index(0), epsilon) && - self.index(1).approx_eq_eps(other.index(1), epsilon) - } -} - -impl Vec2 { - #[inline] - pub fn lt_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) < value, - *self.index(1) < value) - } - - #[inline] - pub fn le_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) <= value, - *self.index(1) <= value) - } - - #[inline] - pub fn ge_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) >= value, - *self.index(1) >= value) - } - - #[inline] - pub fn gt_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) > value, - *self.index(1) > value) - } - - #[inline] - pub fn lt_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) < *other.index(0), - *self.index(1) < *other.index(1)) - } - - #[inline] - pub fn le_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) <= *other.index(0), - *self.index(1) <= *other.index(1)) - } - - #[inline] - pub fn ge_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) >= *other.index(0), - *self.index(1) >= *other.index(1)) - } - - #[inline] - pub fn gt_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) > *other.index(0), - *self.index(1) > *other.index(1)) - } -} - -impl Vec2 { - #[inline] - pub fn eq_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) == value, - *self.index(1) == value) - } - - #[inline] - pub fn ne_t(&self, value: T) -> Vec2 { - Vec2::new(*self.index(0) != value, - *self.index(1) != value) - } - - #[inline] - pub fn eq_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) == *other.index(0), - *self.index(1) == *other.index(1)) - } - - #[inline] - pub fn ne_v(&self, other: &Vec2) -> Vec2 { - Vec2::new(*self.index(0) != *other.index(0), - *self.index(1) != *other.index(1)) - } -} - -impl Vec2 { - #[inline] - pub fn any(&self) -> bool { - *self.index(0) || *self.index(1) - } - - #[inline] - pub fn all(&self) -> bool { - *self.index(0) && *self.index(1) - } - - #[inline] - pub fn not(&self) -> Vec2 { - Vec2::new(!*self.index(0), !*self.index(1)) - } -} +pub mod vec2; +pub mod vec3; +pub mod vec4; // GLSL-style type aliases + pub type vec2 = Vec2; pub type dvec2 = Vec2; pub type bvec2 = Vec2; pub type ivec2 = Vec2; pub type uvec2 = Vec2; +pub type vec3 = Vec3; +pub type dvec3 = Vec3; +pub type bvec3 = Vec3; +pub type ivec3 = Vec3; +pub type uvec3 = Vec3; + +pub type vec4 = Vec4; +pub type dvec4 = Vec4; +pub type bvec4 = Vec4; +pub type ivec4 = Vec4; +pub type uvec4 = Vec4; + // Rust-style type aliases + pub type Vec2f = Vec2; pub type Vec2f32 = Vec2; pub type Vec2f64 = Vec2; @@ -438,460 +58,6 @@ pub type Vec2u32 = Vec2; pub type Vec2u64 = Vec2; pub type Vec2b = Vec2; -#[deriving(Eq)] -pub struct Vec3 { x: T, y: T, z: T } - -impl Vec3 { - #[inline] - pub fn index<'a>(&'a self, i: uint) -> &'a T { - &'a self.as_slice()[i] - } - - #[inline] - pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [T,..3] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..3] { - unsafe { transmute(self) } - } -} - -impl Vec3 { - #[inline] - pub fn new(x: T, y: T, z: T ) -> Vec3 { - Vec3 { x: x, y: y, z: z } - } - - #[inline] - pub fn from_value(value: T) -> Vec3 { - Vec3::new(value, value, value) - } - - #[inline] - pub fn swap(&mut self, a: uint, b: uint) { - let tmp = *self.index(a); - *self.index_mut(a) = *self.index(b); - *self.index_mut(b) = tmp; - } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec3 { - Vec3::new(f(self.index(0)), - f(self.index(1)), - f(self.index(2))) - } -} - -impl Vec3 { - #[inline] - pub fn identity() -> Vec3 { - Vec3::new(One::one::(), One::one::(), One::one::()) - } - - #[inline] - pub fn zero() -> Vec3 { - Vec3::new(Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_x() -> Vec3 { - Vec3::new(One::one::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_y() -> Vec3 { - Vec3::new(Zero::zero::(), One::one::(), Zero::zero::()) - } - - #[inline] - pub fn unit_z() -> Vec3 { - Vec3::new(Zero::zero::(), Zero::zero::(), One::one::()) - } - - #[inline] - pub fn is_zero(&self) -> bool { - *self.index(0) == Zero::zero() && - *self.index(1) == Zero::zero() && - *self.index(2) == Zero::zero() - } - - #[inline] - pub fn add_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) + value, - *self.index(1) + value, - *self.index(2) + value) - } - - #[inline] - pub fn sub_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) - value, - *self.index(1) - value, - *self.index(2) - value) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) * value, - *self.index(1) * value, - *self.index(2) * value) - } - - #[inline] - pub fn div_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) / value, - *self.index(1) / value, - *self.index(2) / value) - } - - #[inline] - pub fn rem_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) % value, - *self.index(1) % value, - *self.index(2) % value) - } - - #[inline] - pub fn add_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) + *other.index(0), - *self.index(1) + *other.index(1), - *self.index(2) + *other.index(2)) - } - - #[inline] - pub fn sub_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) - *other.index(0), - *self.index(1) - *other.index(1), - *self.index(2) - *other.index(2)) - } - - #[inline] - pub fn mul_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) * *other.index(0), - *self.index(1) * *other.index(1), - *self.index(2) * *other.index(2)) - } - - #[inline] - pub fn div_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) / *other.index(0), - *self.index(1) / *other.index(1), - *self.index(2) / *other.index(2)) - } - - #[inline] - pub fn rem_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) % *other.index(0), - *self.index(1) % *other.index(1), - *self.index(2) % *other.index(2)) - } - - #[inline] - pub fn neg_self(&mut self) { - *self.index_mut(0) = -*self.index(0); - *self.index_mut(1) = -*self.index(1); - *self.index_mut(2) = -*self.index(2); - } - - #[inline] - pub fn add_self_t(&mut self, value: T) { - *self.index_mut(0) += value; - *self.index_mut(1) += value; - *self.index_mut(2) += value; - } - - #[inline] - pub fn sub_self_t(&mut self, value: T) { - *self.index_mut(0) -= value; - *self.index_mut(1) -= value; - *self.index_mut(2) -= value; - } - - #[inline] - pub fn mul_self_t(&mut self, value: T) { - *self.index_mut(0) *= value; - *self.index_mut(1) *= value; - *self.index_mut(2) *= value; - } - - #[inline] - pub fn div_self_t(&mut self, value: T) { - *self.index_mut(0) /= value; - *self.index_mut(1) /= value; - *self.index_mut(2) /= value; - } - - #[inline] - pub fn rem_self_t(&mut self, value: T) { - *self.index_mut(0) %= value; - *self.index_mut(1) %= value; - *self.index_mut(2) %= value; - } - - #[inline] - pub fn add_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) += *other.index(0); - *self.index_mut(1) += *other.index(1); - *self.index_mut(2) += *other.index(2); - } - - #[inline] - pub fn sub_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) -= *other.index(0); - *self.index_mut(1) -= *other.index(1); - *self.index_mut(2) -= *other.index(2); - } - - #[inline] - pub fn mul_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) *= *other.index(0); - *self.index_mut(1) *= *other.index(1); - *self.index_mut(2) *= *other.index(2); - } - - #[inline] - pub fn div_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - *self.index_mut(2) /= *other.index(2); - } - - #[inline] - pub fn rem_self_v(&mut self, other: &Vec3) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - *self.index_mut(2) /= *other.index(2); - } - - #[inline] - pub fn dot(&self, other: &Vec3) -> T { - *self.index(0) * *other.index(0) + - *self.index(1) * *other.index(1) + - *self.index(2) * *other.index(2) - } - - #[inline] - pub fn cross(&self, other: &Vec3) -> Vec3 { - Vec3::new((*self.index(1) * *other.index(2)) - (*self.index(2) * *other.index(1)), - (*self.index(2) * *other.index(0)) - (*self.index(0) * *other.index(2)), - (*self.index(0) * *other.index(1)) - (*self.index(1) * *other.index(0))) - } - - #[inline] - pub fn cross_self(&mut self, other: &Vec3) { - *self = self.cross(other) - } - - #[inline] - pub fn to_homogeneous(&self) -> Vec4 { - Vec4::new(self.x, self.y, self.z, Zero::zero()) - } -} - -impl Neg> for Vec3 { - #[inline] - pub fn neg(&self) -> Vec3 { - Vec3::new(-self.index(0), -self.index(1), -self.index(2)) - } -} - -impl Vec3 { - #[inline] - pub fn length2(&self) -> T { - self.dot(self) - } - - #[inline] - pub fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline] - pub fn distance2(&self, other: &Vec3) -> T { - other.sub_v(self).length2() - } - - #[inline] - pub fn distance(&self, other: &Vec3) -> T { - other.distance2(self).sqrt() - } - - #[inline] - pub fn angle(&self, other: &Vec3) -> T { - self.cross(other).length().atan2(self.dot(other)) - } - - #[inline] - pub fn normalize(&self) -> Vec3 { - self.mul_t(One::one::()/self.length()) - } - - #[inline] - pub fn normalize_to(&self, length: T) -> Vec3 { - self.mul_t(length / self.length()) - } - - #[inline] - pub fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } - - #[inline] - pub fn normalize_self(&mut self) { - let n = One::one::() / self.length(); - self.mul_self_t(n); - } - - #[inline] - pub fn normalize_self_to(&mut self, length: T) { - let n = length / self.length(); - self.mul_self_t(n); - } - - pub fn lerp_self(&mut self, other: &Vec3, amount: T) { - let v = other.sub_v(self).mul_t(amount); - self.add_self_v(&v); - } -} - -impl> ApproxEq for Vec3 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Vec3) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool { - self.index(0).approx_eq_eps(other.index(0), epsilon) && - self.index(1).approx_eq_eps(other.index(1), epsilon) && - self.index(2).approx_eq_eps(other.index(2), epsilon) - } -} - -impl Vec3 { - #[inline] - pub fn lt_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) < value, - *self.index(1) < value, - *self.index(2) < value) - } - - #[inline] - pub fn le_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) <= value, - *self.index(1) <= value, - *self.index(2) <= value) - } - - #[inline] - pub fn ge_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) >= value, - *self.index(1) >= value, - *self.index(2) >= value) - } - - #[inline] - pub fn gt_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) > value, - *self.index(1) > value, - *self.index(2) > value) - } - - #[inline] - pub fn lt_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) < *other.index(0), - *self.index(1) < *other.index(1), - *self.index(2) < *other.index(2)) - } - - #[inline] - pub fn le_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) <= *other.index(0), - *self.index(1) <= *other.index(1), - *self.index(2) <= *other.index(2)) - } - - #[inline] - pub fn ge_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) >= *other.index(0), - *self.index(1) >= *other.index(1), - *self.index(2) >= *other.index(2)) - } - - #[inline] - pub fn gt_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) > *other.index(0), - *self.index(1) > *other.index(1), - *self.index(2) > *other.index(2)) - } -} - -impl Vec3 { - #[inline] - pub fn eq_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) == value, - *self.index(1) == value, - *self.index(2) == value) - } - - #[inline] - pub fn ne_t(&self, value: T) -> Vec3 { - Vec3::new(*self.index(0) != value, - *self.index(1) != value, - *self.index(2) != value) - } - - #[inline] - pub fn eq_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) == *other.index(0), - *self.index(1) == *other.index(1), - *self.index(2) == *other.index(2)) - } - - #[inline] - pub fn ne_v(&self, other: &Vec3) -> Vec3 { - Vec3::new(*self.index(0) != *other.index(0), - *self.index(1) != *other.index(1), - *self.index(2) != *other.index(2)) - } -} - -impl Vec3 { - #[inline] - pub fn any(&self) -> bool { - *self.index(0) || *self.index(1) || *self.index(2) - } - - #[inline] - pub fn all(&self) -> bool { - *self.index(0) && *self.index(1) && *self.index(2) - } - - #[inline] - pub fn not(&self) -> Vec3 { - Vec3::new(!*self.index(0), !*self.index(1), !*self.index(2)) - } -} - -// GLSL-style type aliases -pub type vec3 = Vec3; -pub type dvec3 = Vec3; -pub type bvec3 = Vec3; -pub type ivec3 = Vec3; -pub type uvec3 = Vec3; - -// Rust-style type aliases pub type Vec3f = Vec3; pub type Vec3f32 = Vec3; pub type Vec3f64 = Vec3; @@ -907,485 +73,6 @@ pub type Vec3u32 = Vec3; pub type Vec3u64 = Vec3; pub type Vec3b = Vec3; -#[deriving(Eq)] -pub struct Vec4 { x: T, y: T, z: T, w: T } - -impl Vec4 { - #[inline] - pub fn index<'a>(&'a self, i: uint) -> &'a T { - &'a self.as_slice()[i] - } - - #[inline] - pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { - &'a mut self.as_mut_slice()[i] - } - - #[inline] - pub fn as_slice<'a>(&'a self) -> &'a [T,..4] { - unsafe { transmute(self) } - } - - #[inline] - pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] { - unsafe { transmute(self) } - } -} - -impl Vec4 { - #[inline] - pub fn new(x: T, y: T, z: T, w: T ) -> Vec4 { - Vec4 { x: x, y: y, z: z, w: w } - } - - #[inline] - pub fn from_value(value: T) -> Vec4 { - Vec4::new(value, value, value, value) - } - - #[inline] - pub fn swap(&mut self, a: uint, b: uint) { - let tmp = *self.index(a); - *self.index_mut(a) = *self.index(b); - *self.index_mut(b) = tmp; - } - - #[inline(always)] - pub fn map(&self, f: &fn(&T) -> T) -> Vec4 { - Vec4::new(f(self.index(0)), - f(self.index(1)), - f(self.index(2)), - f(self.index(3))) - } -} - -impl Vec4 { - #[inline] - pub fn identity() -> Vec4 { - Vec4::new(One::one::(), One::one::(), One::one::(), One::one::()) - } - - #[inline] - pub fn zero() -> Vec4 { - Vec4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_x() -> Vec4 { - Vec4::new(One::one::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_y() -> Vec4 { - Vec4::new(Zero::zero::(), One::one::(), Zero::zero::(), Zero::zero::()) - } - - #[inline] - pub fn unit_z() -> Vec4 { - Vec4::new(Zero::zero::(), Zero::zero::(), One::one::(), Zero::zero::()) - } - - #[inline] - pub fn unit_w() -> Vec4 { - Vec4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), One::one::()) - } - - #[inline] - pub fn is_zero(&self) -> bool { - *self.index(0) == Zero::zero() && - *self.index(1) == Zero::zero() && - *self.index(2) == Zero::zero() && - *self.index(3) == Zero::zero() - } - - #[inline] - pub fn add_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) + value, - *self.index(1) + value, - *self.index(2) + value, - *self.index(3) + value) - } - - #[inline] - pub fn sub_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) - value, - *self.index(1) - value, - *self.index(2) - value, - *self.index(3) - value) - } - - #[inline] - pub fn mul_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) * value, - *self.index(1) * value, - *self.index(2) * value, - *self.index(3) * value) - } - - #[inline] - pub fn div_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) / value, - *self.index(1) / value, - *self.index(2) / value, - *self.index(3) / value) - } - - #[inline] - pub fn rem_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) % value, - *self.index(1) % value, - *self.index(2) % value, - *self.index(3) % value) - } - - #[inline] - pub fn add_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) + *other.index(0), - *self.index(1) + *other.index(1), - *self.index(2) + *other.index(2), - *self.index(3) + *other.index(3)) - } - - #[inline] - pub fn sub_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) - *other.index(0), - *self.index(1) - *other.index(1), - *self.index(2) - *other.index(2), - *self.index(3) - *other.index(3)) - } - - #[inline] - pub fn mul_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) * *other.index(0), - *self.index(1) * *other.index(1), - *self.index(2) * *other.index(2), - *self.index(3) * *other.index(3)) - } - - #[inline] - pub fn div_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) / *other.index(0), - *self.index(1) / *other.index(1), - *self.index(2) / *other.index(2), - *self.index(3) / *other.index(3)) - } - - #[inline] - pub fn rem_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) % *other.index(0), - *self.index(1) % *other.index(1), - *self.index(2) % *other.index(2), - *self.index(3) % *other.index(3)) - } - - #[inline] - pub fn neg_self(&mut self) { - *self.index_mut(0) = -*self.index(0); - *self.index_mut(1) = -*self.index(1); - *self.index_mut(2) = -*self.index(2); - *self.index_mut(3) = -*self.index(3); - } - - #[inline] - pub fn add_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] - pub fn sub_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] - pub 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] - pub 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] - pub fn rem_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] - pub fn add_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) += *other.index(0); - *self.index_mut(1) += *other.index(1); - *self.index_mut(2) += *other.index(2); - *self.index_mut(3) += *other.index(3); - } - - #[inline] - pub fn sub_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) -= *other.index(0); - *self.index_mut(1) -= *other.index(1); - *self.index_mut(2) -= *other.index(2); - *self.index_mut(3) -= *other.index(3); - } - - #[inline] - pub fn mul_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) *= *other.index(0); - *self.index_mut(1) *= *other.index(1); - *self.index_mut(2) *= *other.index(2); - *self.index_mut(3) *= *other.index(3); - } - - #[inline] - pub fn div_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - *self.index_mut(2) /= *other.index(2); - *self.index_mut(3) /= *other.index(3); - } - - #[inline] - pub fn rem_self_v(&mut self, other: &Vec4) { - *self.index_mut(0) /= *other.index(0); - *self.index_mut(1) /= *other.index(1); - *self.index_mut(2) /= *other.index(2); - *self.index_mut(3) /= *other.index(3); - } - - #[inline] - pub fn dot(&self, other: &Vec4) -> T { - *self.index(0) * *other.index(0) + - *self.index(1) * *other.index(1) + - *self.index(2) * *other.index(2) + - *self.index(3) * *other.index(3) - } -} - -impl Neg> for Vec4 { - #[inline] - pub fn neg(&self) -> Vec4 { - Vec4::new(-self.index(0), -self.index(1), -self.index(2), -self.index(3)) - } -} - -impl Vec4 { - #[inline] - pub fn length2(&self) -> T { - self.dot(self) - } - - #[inline] - pub fn length(&self) -> T { - self.length2().sqrt() - } - - #[inline] - pub fn distance2(&self, other: &Vec4) -> T { - other.sub_v(self).length2() - } - - #[inline] - pub fn distance(&self, other: &Vec4) -> T { - other.distance2(self).sqrt() - } - - #[inline] - pub fn angle(&self, other: &Vec4) -> T { - (self.dot(other) / (self.length() * other.length())).acos() - } - - #[inline] - pub fn normalize(&self) -> Vec4 { - self.mul_t(One::one::()/self.length()) - } - - #[inline] - pub fn normalize_to(&self, length: T) -> Vec4 { - self.mul_t(length / self.length()) - } - - #[inline] - pub fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { - self.add_v(&other.sub_v(self).mul_t(amount)) - } - - #[inline] - pub fn normalize_self(&mut self) { - let n = One::one::() / self.length(); - self.mul_self_t(n); - } - - #[inline] - pub fn normalize_self_to(&mut self, length: T) { - let n = length / self.length(); - self.mul_self_t(n); - } - - pub fn lerp_self(&mut self, other: &Vec4, amount: T) { - let v = other.sub_v(self).mul_t(amount); - self.add_self_v(&v); - } -} - -impl> ApproxEq for Vec4 { - #[inline] - pub fn approx_epsilon() -> T { - ApproxEq::approx_epsilon::() - } - - #[inline] - pub fn approx_eq(&self, other: &Vec4) -> bool { - self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) - } - - #[inline] - pub fn approx_eq_eps(&self, other: &Vec4, epsilon: &T) -> bool { - self.index(0).approx_eq_eps(other.index(0), epsilon) && - self.index(1).approx_eq_eps(other.index(1), epsilon) && - self.index(2).approx_eq_eps(other.index(2), epsilon) && - self.index(3).approx_eq_eps(other.index(3), epsilon) - } -} - -impl Vec4 { - #[inline] - pub fn lt_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) < value, - *self.index(1) < value, - *self.index(2) < value, - *self.index(3) < value) - } - - #[inline] - pub fn le_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) <= value, - *self.index(1) <= value, - *self.index(2) <= value, - *self.index(3) <= value) - } - - #[inline] - pub fn ge_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) >= value, - *self.index(1) >= value, - *self.index(2) >= value, - *self.index(3) >= value) - } - - #[inline] - pub fn gt_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) > value, - *self.index(1) > value, - *self.index(2) > value, - *self.index(3) > value) - } - - #[inline] - pub fn lt_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) < *other.index(0), - *self.index(1) < *other.index(1), - *self.index(2) < *other.index(2), - *self.index(3) < *other.index(3)) - } - - #[inline] - pub fn le_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) <= *other.index(0), - *self.index(1) <= *other.index(1), - *self.index(2) <= *other.index(2), - *self.index(3) <= *other.index(3)) - } - - #[inline] - pub fn ge_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) >= *other.index(0), - *self.index(1) >= *other.index(1), - *self.index(2) >= *other.index(2), - *self.index(3) >= *other.index(3)) - } - - #[inline] - pub fn gt_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) > *other.index(0), - *self.index(1) > *other.index(1), - *self.index(2) > *other.index(2), - *self.index(3) > *other.index(3)) - } -} - -impl Vec4 { - #[inline] - pub fn eq_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) == value, - *self.index(1) == value, - *self.index(2) == value, - *self.index(3) == value) - } - - #[inline] - pub fn ne_t(&self, value: T) -> Vec4 { - Vec4::new(*self.index(0) != value, - *self.index(1) != value, - *self.index(2) != value, - *self.index(3) != value) - } - - #[inline] - pub fn eq_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) == *other.index(0), - *self.index(1) == *other.index(1), - *self.index(2) == *other.index(2), - *self.index(3) == *other.index(3)) - } - - #[inline] - pub fn ne_v(&self, other: &Vec4) -> Vec4 { - Vec4::new(*self.index(0) != *other.index(0), - *self.index(1) != *other.index(1), - *self.index(2) != *other.index(2), - *self.index(3) != *other.index(3)) - } -} - -impl Vec4 { - #[inline] - pub fn any(&self) -> bool { - *self.index(0) || *self.index(1) || *self.index(2) || *self.index(3) - } - - #[inline] - pub fn all(&self) -> bool { - *self.index(0) && *self.index(1) && *self.index(2) && *self.index(3) - } - - #[inline] - pub fn not(&self) -> Vec4 { - Vec4::new(!*self.index(0), !*self.index(1), !*self.index(2), !*self.index(3)) - } -} - -// GLSL-style type aliases -pub type vec4 = Vec4; -pub type dvec4 = Vec4; -pub type bvec4 = Vec4; -pub type ivec4 = Vec4; -pub type uvec4 = Vec4; - -// Rust-style type aliases pub type Vec4f = Vec4; pub type Vec4f32 = Vec4; pub type Vec4f64 = Vec4; diff --git a/src/vec2.rs b/src/vec2.rs new file mode 100644 index 0000000..1f59095 --- /dev/null +++ b/src/vec2.rs @@ -0,0 +1,418 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::cast::transmute; +use std::cmp::ApproxEq; +use std::num::{Zero, One}; + +use super::Vec3; + +#[deriving(Eq)] +pub struct Vec2 { x: T, y: T } + +impl Vec2 { + #[inline] + pub fn index<'a>(&'a self, i: uint) -> &'a T { + &'a self.as_slice()[i] + } + + #[inline] + pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [T,..2] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..2] { + unsafe { transmute(self) } + } +} + +impl Vec2 { + #[inline] + pub fn new(x: T, y: T ) -> Vec2 { + Vec2 { x: x, y: y } + } + + #[inline] + pub fn from_value(value: T) -> Vec2 { + Vec2::new(value, value) + } + + #[inline] + pub fn swap(&mut self, a: uint, b: uint) { + let tmp = *self.index(a); + *self.index_mut(a) = *self.index(b); + *self.index_mut(b) = tmp; + } + + #[inline(always)] + pub fn map(&self, f: &fn(&T) -> T) -> Vec2 { + Vec2::new(f(self.index(0)), + f(self.index(1))) + } +} + +impl Vec2 { + #[inline] + pub fn identity() -> Vec2 { + Vec2::new(One::one::(), One::one::()) + } + + #[inline] + pub fn zero() -> Vec2 { + Vec2::new(Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn unit_x() -> Vec2 { + Vec2::new(One::one::(), Zero::zero::()) + } + + #[inline] + pub fn unit_y() -> Vec2 { + Vec2::new(Zero::zero::(), One::one::()) + } + + #[inline] + pub fn is_zero(&self) -> bool { + *self.index(0) == Zero::zero() && + *self.index(1) == Zero::zero() + } + + #[inline] + pub fn add_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) + value, + *self.index(1) + value) + } + + #[inline] + pub fn sub_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) - value, + *self.index(1) - value) + } + + #[inline] + pub fn mul_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) * value, + *self.index(1) * value) + } + + #[inline] + pub fn div_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) / value, + *self.index(1) / value) + } + + #[inline] + pub fn rem_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) % value, + *self.index(1) % value) + } + + #[inline] + pub fn add_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) + *other.index(0), + *self.index(1) + *other.index(1)) + } + + #[inline] + pub fn sub_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) - *other.index(0), + *self.index(1) - *other.index(1)) + } + + #[inline] + pub fn mul_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) * *other.index(0), + *self.index(1) * *other.index(1)) + } + + #[inline] + pub fn div_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) / *other.index(0), + *self.index(1) / *other.index(1)) + } + + #[inline] + pub fn rem_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) % *other.index(0), + *self.index(1) % *other.index(1)) + } + + #[inline] + pub fn neg_self(&mut self) { + *self.index_mut(0) = -*self.index(0); + *self.index_mut(1) = -*self.index(1); + } + + #[inline] + pub fn add_self_t(&mut self, value: T) { + *self.index_mut(0) += value; + *self.index_mut(1) += value; + } + + #[inline] + pub fn sub_self_t(&mut self, value: T) { + *self.index_mut(0) -= value; + *self.index_mut(1) -= value; + } + + #[inline] + pub fn mul_self_t(&mut self, value: T) { + *self.index_mut(0) *= value; + *self.index_mut(1) *= value; + } + + #[inline] + pub fn div_self_t(&mut self, value: T) { + *self.index_mut(0) /= value; + *self.index_mut(1) /= value; + } + + #[inline] + pub fn rem_self_t(&mut self, value: T) { + *self.index_mut(0) %= value; + *self.index_mut(1) %= value; + } + + #[inline] + pub fn add_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) += *other.index(0); + *self.index_mut(1) += *other.index(1); + } + + #[inline] + pub fn sub_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) -= *other.index(0); + *self.index_mut(1) -= *other.index(1); + } + + #[inline] + pub fn mul_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) *= *other.index(0); + *self.index_mut(1) *= *other.index(1); + } + + #[inline] + pub fn div_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) /= *other.index(0); + *self.index_mut(1) /= *other.index(1); + } + + #[inline] + pub fn rem_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) /= *other.index(0); + *self.index_mut(1) /= *other.index(1); + } + + #[inline] + pub fn dot(&self, other: &Vec2) -> T { + *self.index(0) * *other.index(0) + + *self.index(1) * *other.index(1) + } + + #[inline] + pub fn perp_dot(&self, other: &Vec2) -> T { + (*self.index(0) * *other.index(1)) - + (*self.index(1) * *other.index(0)) + } + + #[inline] + pub fn to_homogeneous(&self) -> Vec3 { + Vec3::new(self.x, self.y, Zero::zero()) + } +} + +impl Neg> for Vec2 { + #[inline] + pub fn neg(&self) -> Vec2 { + Vec2::new(-self.index(0), -self.index(1)) + } +} + +impl Vec2 { + #[inline] + pub fn length2(&self) -> T { + self.dot(self) + } + + #[inline] + pub fn length(&self) -> T { + self.length2().sqrt() + } + + #[inline] + pub fn distance2(&self, other: &Vec2) -> T { + other.sub_v(self).length2() + } + + #[inline] + pub fn distance(&self, other: &Vec2) -> T { + other.distance2(self).sqrt() + } + + #[inline] + pub fn angle(&self, other: &Vec2) -> T { + self.perp_dot(other).atan2(self.dot(other)) + } + + #[inline] + pub fn normalize(&self) -> Vec2 { + self.mul_t(One::one::()/self.length()) + } + + #[inline] + pub fn normalize_to(&self, length: T) -> Vec2 { + self.mul_t(length / self.length()) + } + + #[inline] + pub fn lerp(&self, other: &Vec2, amount: T) -> Vec2 { + self.add_v(&other.sub_v(self).mul_t(amount)) + } + + #[inline] + pub fn normalize_self(&mut self) { + let n = One::one::() / self.length(); + self.mul_self_t(n); + } + + #[inline] + pub fn normalize_self_to(&mut self, length: T) { + let n = length / self.length(); + self.mul_self_t(n); + } + + pub fn lerp_self(&mut self, other: &Vec2, amount: T) { + let v = other.sub_v(self).mul_t(amount); + self.add_self_v(&v); + } +} + +impl> ApproxEq for Vec2 { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Vec2) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Vec2, epsilon: &T) -> bool { + self.index(0).approx_eq_eps(other.index(0), epsilon) && + self.index(1).approx_eq_eps(other.index(1), epsilon) + } +} + +impl Vec2 { + #[inline] + pub fn lt_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) < value, + *self.index(1) < value) + } + + #[inline] + pub fn le_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) <= value, + *self.index(1) <= value) + } + + #[inline] + pub fn ge_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) >= value, + *self.index(1) >= value) + } + + #[inline] + pub fn gt_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) > value, + *self.index(1) > value) + } + + #[inline] + pub fn lt_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) < *other.index(0), + *self.index(1) < *other.index(1)) + } + + #[inline] + pub fn le_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) <= *other.index(0), + *self.index(1) <= *other.index(1)) + } + + #[inline] + pub fn ge_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) >= *other.index(0), + *self.index(1) >= *other.index(1)) + } + + #[inline] + pub fn gt_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) > *other.index(0), + *self.index(1) > *other.index(1)) + } +} + +impl Vec2 { + #[inline] + pub fn eq_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) == value, + *self.index(1) == value) + } + + #[inline] + pub fn ne_t(&self, value: T) -> Vec2 { + Vec2::new(*self.index(0) != value, + *self.index(1) != value) + } + + #[inline] + pub fn eq_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) == *other.index(0), + *self.index(1) == *other.index(1)) + } + + #[inline] + pub fn ne_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(*self.index(0) != *other.index(0), + *self.index(1) != *other.index(1)) + } +} + +impl Vec2 { + #[inline] + pub fn any(&self) -> bool { + *self.index(0) || *self.index(1) + } + + #[inline] + pub fn all(&self) -> bool { + *self.index(0) && *self.index(1) + } + + #[inline] + pub fn not(&self) -> Vec2 { + Vec2::new(!*self.index(0), !*self.index(1)) + } +} diff --git a/src/vec3.rs b/src/vec3.rs new file mode 100644 index 0000000..89caebb --- /dev/null +++ b/src/vec3.rs @@ -0,0 +1,466 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::cast::transmute; +use std::cmp::ApproxEq; +use std::num::{Zero, One}; + +use super::Vec4; + +#[deriving(Eq)] +pub struct Vec3 { x: T, y: T, z: T } + +impl Vec3 { + #[inline] + pub fn index<'a>(&'a self, i: uint) -> &'a T { + &'a self.as_slice()[i] + } + + #[inline] + pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [T,..3] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..3] { + unsafe { transmute(self) } + } +} + +impl Vec3 { + #[inline] + pub fn new(x: T, y: T, z: T ) -> Vec3 { + Vec3 { x: x, y: y, z: z } + } + + #[inline] + pub fn from_value(value: T) -> Vec3 { + Vec3::new(value, value, value) + } + + #[inline] + pub fn swap(&mut self, a: uint, b: uint) { + let tmp = *self.index(a); + *self.index_mut(a) = *self.index(b); + *self.index_mut(b) = tmp; + } + + #[inline(always)] + pub fn map(&self, f: &fn(&T) -> T) -> Vec3 { + Vec3::new(f(self.index(0)), + f(self.index(1)), + f(self.index(2))) + } +} + +impl Vec3 { + #[inline] + pub fn identity() -> Vec3 { + Vec3::new(One::one::(), One::one::(), One::one::()) + } + + #[inline] + pub fn zero() -> Vec3 { + Vec3::new(Zero::zero::(), Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn unit_x() -> Vec3 { + Vec3::new(One::one::(), Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn unit_y() -> Vec3 { + Vec3::new(Zero::zero::(), One::one::(), Zero::zero::()) + } + + #[inline] + pub fn unit_z() -> Vec3 { + Vec3::new(Zero::zero::(), Zero::zero::(), One::one::()) + } + + #[inline] + pub fn is_zero(&self) -> bool { + *self.index(0) == Zero::zero() && + *self.index(1) == Zero::zero() && + *self.index(2) == Zero::zero() + } + + #[inline] + pub fn add_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) + value, + *self.index(1) + value, + *self.index(2) + value) + } + + #[inline] + pub fn sub_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) - value, + *self.index(1) - value, + *self.index(2) - value) + } + + #[inline] + pub fn mul_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) * value, + *self.index(1) * value, + *self.index(2) * value) + } + + #[inline] + pub fn div_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) / value, + *self.index(1) / value, + *self.index(2) / value) + } + + #[inline] + pub fn rem_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) % value, + *self.index(1) % value, + *self.index(2) % value) + } + + #[inline] + pub fn add_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) + *other.index(0), + *self.index(1) + *other.index(1), + *self.index(2) + *other.index(2)) + } + + #[inline] + pub fn sub_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) - *other.index(0), + *self.index(1) - *other.index(1), + *self.index(2) - *other.index(2)) + } + + #[inline] + pub fn mul_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) * *other.index(0), + *self.index(1) * *other.index(1), + *self.index(2) * *other.index(2)) + } + + #[inline] + pub fn div_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) / *other.index(0), + *self.index(1) / *other.index(1), + *self.index(2) / *other.index(2)) + } + + #[inline] + pub fn rem_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) % *other.index(0), + *self.index(1) % *other.index(1), + *self.index(2) % *other.index(2)) + } + + #[inline] + pub fn neg_self(&mut self) { + *self.index_mut(0) = -*self.index(0); + *self.index_mut(1) = -*self.index(1); + *self.index_mut(2) = -*self.index(2); + } + + #[inline] + pub fn add_self_t(&mut self, value: T) { + *self.index_mut(0) += value; + *self.index_mut(1) += value; + *self.index_mut(2) += value; + } + + #[inline] + pub fn sub_self_t(&mut self, value: T) { + *self.index_mut(0) -= value; + *self.index_mut(1) -= value; + *self.index_mut(2) -= value; + } + + #[inline] + pub fn mul_self_t(&mut self, value: T) { + *self.index_mut(0) *= value; + *self.index_mut(1) *= value; + *self.index_mut(2) *= value; + } + + #[inline] + pub fn div_self_t(&mut self, value: T) { + *self.index_mut(0) /= value; + *self.index_mut(1) /= value; + *self.index_mut(2) /= value; + } + + #[inline] + pub fn rem_self_t(&mut self, value: T) { + *self.index_mut(0) %= value; + *self.index_mut(1) %= value; + *self.index_mut(2) %= value; + } + + #[inline] + pub fn add_self_v(&mut self, other: &Vec3) { + *self.index_mut(0) += *other.index(0); + *self.index_mut(1) += *other.index(1); + *self.index_mut(2) += *other.index(2); + } + + #[inline] + pub fn sub_self_v(&mut self, other: &Vec3) { + *self.index_mut(0) -= *other.index(0); + *self.index_mut(1) -= *other.index(1); + *self.index_mut(2) -= *other.index(2); + } + + #[inline] + pub fn mul_self_v(&mut self, other: &Vec3) { + *self.index_mut(0) *= *other.index(0); + *self.index_mut(1) *= *other.index(1); + *self.index_mut(2) *= *other.index(2); + } + + #[inline] + pub fn div_self_v(&mut self, other: &Vec3) { + *self.index_mut(0) /= *other.index(0); + *self.index_mut(1) /= *other.index(1); + *self.index_mut(2) /= *other.index(2); + } + + #[inline] + pub fn rem_self_v(&mut self, other: &Vec3) { + *self.index_mut(0) /= *other.index(0); + *self.index_mut(1) /= *other.index(1); + *self.index_mut(2) /= *other.index(2); + } + + #[inline] + pub fn dot(&self, other: &Vec3) -> T { + *self.index(0) * *other.index(0) + + *self.index(1) * *other.index(1) + + *self.index(2) * *other.index(2) + } + + #[inline] + pub fn cross(&self, other: &Vec3) -> Vec3 { + Vec3::new((*self.index(1) * *other.index(2)) - (*self.index(2) * *other.index(1)), + (*self.index(2) * *other.index(0)) - (*self.index(0) * *other.index(2)), + (*self.index(0) * *other.index(1)) - (*self.index(1) * *other.index(0))) + } + + #[inline] + pub fn cross_self(&mut self, other: &Vec3) { + *self = self.cross(other) + } + + #[inline] + pub fn to_homogeneous(&self) -> Vec4 { + Vec4::new(self.x, self.y, self.z, Zero::zero()) + } +} + +impl Neg> for Vec3 { + #[inline] + pub fn neg(&self) -> Vec3 { + Vec3::new(-self.index(0), -self.index(1), -self.index(2)) + } +} + +impl Vec3 { + #[inline] + pub fn length2(&self) -> T { + self.dot(self) + } + + #[inline] + pub fn length(&self) -> T { + self.length2().sqrt() + } + + #[inline] + pub fn distance2(&self, other: &Vec3) -> T { + other.sub_v(self).length2() + } + + #[inline] + pub fn distance(&self, other: &Vec3) -> T { + other.distance2(self).sqrt() + } + + #[inline] + pub fn angle(&self, other: &Vec3) -> T { + self.cross(other).length().atan2(self.dot(other)) + } + + #[inline] + pub fn normalize(&self) -> Vec3 { + self.mul_t(One::one::()/self.length()) + } + + #[inline] + pub fn normalize_to(&self, length: T) -> Vec3 { + self.mul_t(length / self.length()) + } + + #[inline] + pub fn lerp(&self, other: &Vec3, amount: T) -> Vec3 { + self.add_v(&other.sub_v(self).mul_t(amount)) + } + + #[inline] + pub fn normalize_self(&mut self) { + let n = One::one::() / self.length(); + self.mul_self_t(n); + } + + #[inline] + pub fn normalize_self_to(&mut self, length: T) { + let n = length / self.length(); + self.mul_self_t(n); + } + + pub fn lerp_self(&mut self, other: &Vec3, amount: T) { + let v = other.sub_v(self).mul_t(amount); + self.add_self_v(&v); + } +} + +impl> ApproxEq for Vec3 { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Vec3) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Vec3, epsilon: &T) -> bool { + self.index(0).approx_eq_eps(other.index(0), epsilon) && + self.index(1).approx_eq_eps(other.index(1), epsilon) && + self.index(2).approx_eq_eps(other.index(2), epsilon) + } +} + +impl Vec3 { + #[inline] + pub fn lt_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) < value, + *self.index(1) < value, + *self.index(2) < value) + } + + #[inline] + pub fn le_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) <= value, + *self.index(1) <= value, + *self.index(2) <= value) + } + + #[inline] + pub fn ge_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) >= value, + *self.index(1) >= value, + *self.index(2) >= value) + } + + #[inline] + pub fn gt_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) > value, + *self.index(1) > value, + *self.index(2) > value) + } + + #[inline] + pub fn lt_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) < *other.index(0), + *self.index(1) < *other.index(1), + *self.index(2) < *other.index(2)) + } + + #[inline] + pub fn le_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) <= *other.index(0), + *self.index(1) <= *other.index(1), + *self.index(2) <= *other.index(2)) + } + + #[inline] + pub fn ge_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) >= *other.index(0), + *self.index(1) >= *other.index(1), + *self.index(2) >= *other.index(2)) + } + + #[inline] + pub fn gt_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) > *other.index(0), + *self.index(1) > *other.index(1), + *self.index(2) > *other.index(2)) + } +} + +impl Vec3 { + #[inline] + pub fn eq_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) == value, + *self.index(1) == value, + *self.index(2) == value) + } + + #[inline] + pub fn ne_t(&self, value: T) -> Vec3 { + Vec3::new(*self.index(0) != value, + *self.index(1) != value, + *self.index(2) != value) + } + + #[inline] + pub fn eq_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) == *other.index(0), + *self.index(1) == *other.index(1), + *self.index(2) == *other.index(2)) + } + + #[inline] + pub fn ne_v(&self, other: &Vec3) -> Vec3 { + Vec3::new(*self.index(0) != *other.index(0), + *self.index(1) != *other.index(1), + *self.index(2) != *other.index(2)) + } +} + +impl Vec3 { + #[inline] + pub fn any(&self) -> bool { + *self.index(0) || *self.index(1) || *self.index(2) + } + + #[inline] + pub fn all(&self) -> bool { + *self.index(0) && *self.index(1) && *self.index(2) + } + + #[inline] + pub fn not(&self) -> Vec3 { + Vec3::new(!*self.index(0), !*self.index(1), !*self.index(2)) + } +} diff --git a/src/vec4.rs b/src/vec4.rs new file mode 100644 index 0000000..6a5c923 --- /dev/null +++ b/src/vec4.rs @@ -0,0 +1,489 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::cast::transmute; +use std::cmp::ApproxEq; +use std::num::{Zero, One}; + +#[deriving(Eq)] +pub struct Vec4 { x: T, y: T, z: T, w: T } + +impl Vec4 { + #[inline] + pub fn index<'a>(&'a self, i: uint) -> &'a T { + &'a self.as_slice()[i] + } + + #[inline] + pub fn index_mut<'a>(&'a mut self, i: uint) -> &'a mut T { + &'a mut self.as_mut_slice()[i] + } + + #[inline] + pub fn as_slice<'a>(&'a self) -> &'a [T,..4] { + unsafe { transmute(self) } + } + + #[inline] + pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] { + unsafe { transmute(self) } + } +} + +impl Vec4 { + #[inline] + pub fn new(x: T, y: T, z: T, w: T ) -> Vec4 { + Vec4 { x: x, y: y, z: z, w: w } + } + + #[inline] + pub fn from_value(value: T) -> Vec4 { + Vec4::new(value, value, value, value) + } + + #[inline] + pub fn swap(&mut self, a: uint, b: uint) { + let tmp = *self.index(a); + *self.index_mut(a) = *self.index(b); + *self.index_mut(b) = tmp; + } + + #[inline(always)] + pub fn map(&self, f: &fn(&T) -> T) -> Vec4 { + Vec4::new(f(self.index(0)), + f(self.index(1)), + f(self.index(2)), + f(self.index(3))) + } +} + +impl Vec4 { + #[inline] + pub fn identity() -> Vec4 { + Vec4::new(One::one::(), One::one::(), One::one::(), One::one::()) + } + + #[inline] + pub fn zero() -> Vec4 { + Vec4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn unit_x() -> Vec4 { + Vec4::new(One::one::(), Zero::zero::(), Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn unit_y() -> Vec4 { + Vec4::new(Zero::zero::(), One::one::(), Zero::zero::(), Zero::zero::()) + } + + #[inline] + pub fn unit_z() -> Vec4 { + Vec4::new(Zero::zero::(), Zero::zero::(), One::one::(), Zero::zero::()) + } + + #[inline] + pub fn unit_w() -> Vec4 { + Vec4::new(Zero::zero::(), Zero::zero::(), Zero::zero::(), One::one::()) + } + + #[inline] + pub fn is_zero(&self) -> bool { + *self.index(0) == Zero::zero() && + *self.index(1) == Zero::zero() && + *self.index(2) == Zero::zero() && + *self.index(3) == Zero::zero() + } + + #[inline] + pub fn add_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) + value, + *self.index(1) + value, + *self.index(2) + value, + *self.index(3) + value) + } + + #[inline] + pub fn sub_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) - value, + *self.index(1) - value, + *self.index(2) - value, + *self.index(3) - value) + } + + #[inline] + pub fn mul_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) * value, + *self.index(1) * value, + *self.index(2) * value, + *self.index(3) * value) + } + + #[inline] + pub fn div_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) / value, + *self.index(1) / value, + *self.index(2) / value, + *self.index(3) / value) + } + + #[inline] + pub fn rem_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) % value, + *self.index(1) % value, + *self.index(2) % value, + *self.index(3) % value) + } + + #[inline] + pub fn add_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) + *other.index(0), + *self.index(1) + *other.index(1), + *self.index(2) + *other.index(2), + *self.index(3) + *other.index(3)) + } + + #[inline] + pub fn sub_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) - *other.index(0), + *self.index(1) - *other.index(1), + *self.index(2) - *other.index(2), + *self.index(3) - *other.index(3)) + } + + #[inline] + pub fn mul_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) * *other.index(0), + *self.index(1) * *other.index(1), + *self.index(2) * *other.index(2), + *self.index(3) * *other.index(3)) + } + + #[inline] + pub fn div_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) / *other.index(0), + *self.index(1) / *other.index(1), + *self.index(2) / *other.index(2), + *self.index(3) / *other.index(3)) + } + + #[inline] + pub fn rem_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) % *other.index(0), + *self.index(1) % *other.index(1), + *self.index(2) % *other.index(2), + *self.index(3) % *other.index(3)) + } + + #[inline] + pub fn neg_self(&mut self) { + *self.index_mut(0) = -*self.index(0); + *self.index_mut(1) = -*self.index(1); + *self.index_mut(2) = -*self.index(2); + *self.index_mut(3) = -*self.index(3); + } + + #[inline] + pub fn add_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] + pub fn sub_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] + pub 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] + pub 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] + pub fn rem_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] + pub fn add_self_v(&mut self, other: &Vec4) { + *self.index_mut(0) += *other.index(0); + *self.index_mut(1) += *other.index(1); + *self.index_mut(2) += *other.index(2); + *self.index_mut(3) += *other.index(3); + } + + #[inline] + pub fn sub_self_v(&mut self, other: &Vec4) { + *self.index_mut(0) -= *other.index(0); + *self.index_mut(1) -= *other.index(1); + *self.index_mut(2) -= *other.index(2); + *self.index_mut(3) -= *other.index(3); + } + + #[inline] + pub fn mul_self_v(&mut self, other: &Vec4) { + *self.index_mut(0) *= *other.index(0); + *self.index_mut(1) *= *other.index(1); + *self.index_mut(2) *= *other.index(2); + *self.index_mut(3) *= *other.index(3); + } + + #[inline] + pub fn div_self_v(&mut self, other: &Vec4) { + *self.index_mut(0) /= *other.index(0); + *self.index_mut(1) /= *other.index(1); + *self.index_mut(2) /= *other.index(2); + *self.index_mut(3) /= *other.index(3); + } + + #[inline] + pub fn rem_self_v(&mut self, other: &Vec4) { + *self.index_mut(0) /= *other.index(0); + *self.index_mut(1) /= *other.index(1); + *self.index_mut(2) /= *other.index(2); + *self.index_mut(3) /= *other.index(3); + } + + #[inline] + pub fn dot(&self, other: &Vec4) -> T { + *self.index(0) * *other.index(0) + + *self.index(1) * *other.index(1) + + *self.index(2) * *other.index(2) + + *self.index(3) * *other.index(3) + } +} + +impl Neg> for Vec4 { + #[inline] + pub fn neg(&self) -> Vec4 { + Vec4::new(-self.index(0), -self.index(1), -self.index(2), -self.index(3)) + } +} + +impl Vec4 { + #[inline] + pub fn length2(&self) -> T { + self.dot(self) + } + + #[inline] + pub fn length(&self) -> T { + self.length2().sqrt() + } + + #[inline] + pub fn distance2(&self, other: &Vec4) -> T { + other.sub_v(self).length2() + } + + #[inline] + pub fn distance(&self, other: &Vec4) -> T { + other.distance2(self).sqrt() + } + + #[inline] + pub fn angle(&self, other: &Vec4) -> T { + (self.dot(other) / (self.length() * other.length())).acos() + } + + #[inline] + pub fn normalize(&self) -> Vec4 { + self.mul_t(One::one::()/self.length()) + } + + #[inline] + pub fn normalize_to(&self, length: T) -> Vec4 { + self.mul_t(length / self.length()) + } + + #[inline] + pub fn lerp(&self, other: &Vec4, amount: T) -> Vec4 { + self.add_v(&other.sub_v(self).mul_t(amount)) + } + + #[inline] + pub fn normalize_self(&mut self) { + let n = One::one::() / self.length(); + self.mul_self_t(n); + } + + #[inline] + pub fn normalize_self_to(&mut self, length: T) { + let n = length / self.length(); + self.mul_self_t(n); + } + + pub fn lerp_self(&mut self, other: &Vec4, amount: T) { + let v = other.sub_v(self).mul_t(amount); + self.add_self_v(&v); + } +} + +impl> ApproxEq for Vec4 { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Vec4) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Vec4, epsilon: &T) -> bool { + self.index(0).approx_eq_eps(other.index(0), epsilon) && + self.index(1).approx_eq_eps(other.index(1), epsilon) && + self.index(2).approx_eq_eps(other.index(2), epsilon) && + self.index(3).approx_eq_eps(other.index(3), epsilon) + } +} + +impl Vec4 { + #[inline] + pub fn lt_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) < value, + *self.index(1) < value, + *self.index(2) < value, + *self.index(3) < value) + } + + #[inline] + pub fn le_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) <= value, + *self.index(1) <= value, + *self.index(2) <= value, + *self.index(3) <= value) + } + + #[inline] + pub fn ge_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) >= value, + *self.index(1) >= value, + *self.index(2) >= value, + *self.index(3) >= value) + } + + #[inline] + pub fn gt_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) > value, + *self.index(1) > value, + *self.index(2) > value, + *self.index(3) > value) + } + + #[inline] + pub fn lt_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) < *other.index(0), + *self.index(1) < *other.index(1), + *self.index(2) < *other.index(2), + *self.index(3) < *other.index(3)) + } + + #[inline] + pub fn le_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) <= *other.index(0), + *self.index(1) <= *other.index(1), + *self.index(2) <= *other.index(2), + *self.index(3) <= *other.index(3)) + } + + #[inline] + pub fn ge_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) >= *other.index(0), + *self.index(1) >= *other.index(1), + *self.index(2) >= *other.index(2), + *self.index(3) >= *other.index(3)) + } + + #[inline] + pub fn gt_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) > *other.index(0), + *self.index(1) > *other.index(1), + *self.index(2) > *other.index(2), + *self.index(3) > *other.index(3)) + } +} + +impl Vec4 { + #[inline] + pub fn eq_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) == value, + *self.index(1) == value, + *self.index(2) == value, + *self.index(3) == value) + } + + #[inline] + pub fn ne_t(&self, value: T) -> Vec4 { + Vec4::new(*self.index(0) != value, + *self.index(1) != value, + *self.index(2) != value, + *self.index(3) != value) + } + + #[inline] + pub fn eq_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) == *other.index(0), + *self.index(1) == *other.index(1), + *self.index(2) == *other.index(2), + *self.index(3) == *other.index(3)) + } + + #[inline] + pub fn ne_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(*self.index(0) != *other.index(0), + *self.index(1) != *other.index(1), + *self.index(2) != *other.index(2), + *self.index(3) != *other.index(3)) + } +} + +impl Vec4 { + #[inline] + pub fn any(&self) -> bool { + *self.index(0) || *self.index(1) || *self.index(2) || *self.index(3) + } + + #[inline] + pub fn all(&self) -> bool { + *self.index(0) && *self.index(1) && *self.index(2) && *self.index(3) + } + + #[inline] + pub fn not(&self) -> Vec4 { + Vec4::new(!*self.index(0), !*self.index(1), !*self.index(2), !*self.index(3)) + } +}