Some constructors and functions don't need T:Copy

This commit is contained in:
Brendan Zabarauskas 2013-06-12 11:15:12 +10:00
parent feb4ae2a00
commit b2e7f2e4b1
7 changed files with 194 additions and 173 deletions

View file

@ -21,41 +21,12 @@ use vec::*;
use super::{Mat3, Mat4}; use super::{Mat3, Mat4};
#[deriving(Eq)] #[deriving(Eq)]
pub struct Mat2<T> { x: Vec2<T>, y: Vec2<T> } pub struct Mat2<T> {
x: Vec2<T>,
y: Vec2<T>,
}
impl<T> Mat2<T> { impl<T> Mat2<T> {
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec2<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec2<T>,..2] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec2<T>,..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<T:Copy> Mat2<T> {
/// Construct a 2 x 2 matrix /// Construct a 2 x 2 matrix
/// ///
/// # Arguments /// # Arguments
@ -99,6 +70,44 @@ impl<T:Copy> Mat2<T> {
Mat2 { x: c0, y: c1 } Mat2 { x: c0, y: c1 }
} }
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec2<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec2<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec2<T>,..2] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec2<T>,..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)
}
#[inline(always)]
pub fn map(&self, f: &fn(&Vec2<T>) -> Vec2<T>) -> Mat2<T> {
Mat2::from_cols(f(self.col(0)),
f(self.col(1)))
}
}
impl<T:Copy> Mat2<T> {
#[inline] #[inline]
pub fn row(&self, i: uint) -> Vec2<T> { pub fn row(&self, i: uint) -> Vec2<T> {
Vec2::new(*self.elem(0, i), Vec2::new(*self.elem(0, i),

View file

@ -22,41 +22,13 @@ use quat::Quat;
use super::Mat4; use super::Mat4;
#[deriving(Eq)] #[deriving(Eq)]
pub struct Mat3<T> { x: Vec3<T>, y: Vec3<T>, z: Vec3<T> } pub struct Mat3<T> {
x: Vec3<T>,
y: Vec3<T>,
z: Vec3<T>,
}
impl<T> Mat3<T> { impl<T> Mat3<T> {
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec3<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec3<T>,..3] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec3<T>,..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<T:Copy> Mat3<T> {
/// Construct a 3 x 3 matrix /// Construct a 3 x 3 matrix
/// ///
/// # Arguments /// # Arguments
@ -109,6 +81,45 @@ impl<T:Copy> Mat3<T> {
Mat3 { x: c0, y: c1, z: c2 } Mat3 { x: c0, y: c1, z: c2 }
} }
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec3<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec3<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec3<T>,..3] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec3<T>,..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)
}
#[inline(always)]
pub fn map(&self, f: &fn(&Vec3<T>) -> Vec3<T>) -> Mat3<T> {
Mat3::from_cols(f(self.col(0)),
f(self.col(1)),
f(self.col(2)))
}
}
impl<T:Copy> Mat3<T> {
#[inline] #[inline]
pub fn row(&self, i: uint) -> Vec3<T> { pub fn row(&self, i: uint) -> Vec3<T> {
Vec3::new(*self.elem(0, i), Vec3::new(*self.elem(0, i),

View file

@ -21,54 +21,15 @@ use std::uint;
use vec::*; use vec::*;
use super::Mat3; 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)] #[deriving(Eq)]
pub struct Mat4<T> { x: Vec4<T>, y: Vec4<T>, z: Vec4<T>, w: Vec4<T> } pub struct Mat4<T> {
x: Vec4<T>,
y: Vec4<T>,
z: Vec4<T>,
w: Vec4<T>,
}
impl<T> Mat4<T> { impl<T> Mat4<T> {
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec4<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec4<T>,..4] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec4<T>,..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<T:Copy> Mat4<T> {
/// Construct a 4 x 4 matrix /// Construct a 4 x 4 matrix
/// ///
/// # Arguments /// # Arguments
@ -130,6 +91,46 @@ impl<T:Copy> Mat4<T> {
Mat4 { x: c0, y: c1, z: c2, w: c3 } Mat4 { x: c0, y: c1, z: c2, w: c3 }
} }
#[inline]
pub fn col<'a>(&'a self, i: uint) -> &'a Vec4<T> {
&'a self.as_slice()[i]
}
#[inline]
pub fn col_mut<'a>(&'a mut self, i: uint) -> &'a mut Vec4<T> {
&'a mut self.as_mut_slice()[i]
}
#[inline]
pub fn as_slice<'a>(&'a self) -> &'a [Vec4<T>,..4] {
unsafe { transmute(self) }
}
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [Vec4<T>,..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)
}
#[inline(always)]
pub fn map(&self, f: &fn(&Vec4<T>) -> Vec4<T>) -> Mat4<T> {
Mat4::from_cols(f(self.col(0)),
f(self.col(1)),
f(self.col(2)),
f(self.col(3)))
}
}
impl<T:Copy> Mat4<T> {
#[inline] #[inline]
pub fn row(&self, i: uint) -> Vec4<T> { pub fn row(&self, i: uint) -> Vec4<T> {
Vec4::new(*self.elem(0, i), Vec4::new(*self.elem(0, i),

View file

@ -45,28 +45,6 @@ pub type Quatf64 = Quat<f64>;
pub struct Quat<T> { s: T, v: Vec3<T> } pub struct Quat<T> { s: T, v: Vec3<T> }
impl<T> Quat<T> { impl<T> Quat<T> {
#[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<T:Copy> Quat<T> {
/// Construct the quaternion from one scalar component and three /// Construct the quaternion from one scalar component and three
/// imaginary components /// imaginary components
/// ///
@ -93,10 +71,23 @@ impl<T:Copy> Quat<T> {
} }
#[inline] #[inline]
pub fn swap(&mut self, a: uint, b: uint) { pub fn index<'a>(&'a self, i: uint) -> &'a T {
let tmp = *self.index(a); &'a self.as_slice()[i]
*self.index_mut(a) = *self.index(b); }
*self.index_mut(b) = tmp;
#[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) }
} }
#[inline] #[inline]
@ -108,6 +99,15 @@ impl<T:Copy> Quat<T> {
} }
} }
impl<T:Copy> Quat<T> {
#[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;
}
}
impl<T:Copy + Real> Quat<T> { impl<T:Copy + Real> Quat<T> {
/// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i` /// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i`
#[inline] #[inline]

View file

@ -23,6 +23,11 @@ use super::Vec3;
pub struct Vec2<T> { x: T, y: T } pub struct Vec2<T> { x: T, y: T }
impl<T> Vec2<T> { impl<T> Vec2<T> {
#[inline]
pub fn new(x: T, y: T ) -> Vec2<T> {
Vec2 { x: x, y: y }
}
#[inline] #[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a T { pub fn index<'a>(&'a self, i: uint) -> &'a T {
&'a self.as_slice()[i] &'a self.as_slice()[i]
@ -42,14 +47,15 @@ impl<T> Vec2<T> {
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..2] { pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..2] {
unsafe { transmute(self) } unsafe { transmute(self) }
} }
#[inline(always)]
pub fn map(&self, f: &fn(&T) -> T) -> Vec2<T> {
Vec2::new(f(self.index(0)),
f(self.index(1)))
}
} }
impl<T:Copy> Vec2<T> { impl<T:Copy> Vec2<T> {
#[inline]
pub fn new(x: T, y: T ) -> Vec2<T> {
Vec2 { x: x, y: y }
}
#[inline] #[inline]
pub fn from_value(value: T) -> Vec2<T> { pub fn from_value(value: T) -> Vec2<T> {
Vec2::new(value, value) Vec2::new(value, value)
@ -61,12 +67,6 @@ impl<T:Copy> Vec2<T> {
*self.index_mut(a) = *self.index(b); *self.index_mut(a) = *self.index(b);
*self.index_mut(b) = tmp; *self.index_mut(b) = tmp;
} }
#[inline(always)]
pub fn map(&self, f: &fn(&T) -> T) -> Vec2<T> {
Vec2::new(f(self.index(0)),
f(self.index(1)))
}
} }
impl<T:Copy + Num> Vec2<T> { impl<T:Copy + Num> Vec2<T> {

View file

@ -23,6 +23,11 @@ use super::Vec4;
pub struct Vec3<T> { x: T, y: T, z: T } pub struct Vec3<T> { x: T, y: T, z: T }
impl<T> Vec3<T> { impl<T> Vec3<T> {
#[inline]
pub fn new(x: T, y: T, z: T ) -> Vec3<T> {
Vec3 { x: x, y: y, z: z }
}
#[inline] #[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a T { pub fn index<'a>(&'a self, i: uint) -> &'a T {
&'a self.as_slice()[i] &'a self.as_slice()[i]
@ -42,14 +47,16 @@ impl<T> Vec3<T> {
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..3] { pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..3] {
unsafe { transmute(self) } unsafe { transmute(self) }
} }
#[inline(always)]
pub fn map(&self, f: &fn(&T) -> T) -> Vec3<T> {
Vec3::new(f(self.index(0)),
f(self.index(1)),
f(self.index(2)))
}
} }
impl<T:Copy> Vec3<T> { impl<T:Copy> Vec3<T> {
#[inline]
pub fn new(x: T, y: T, z: T ) -> Vec3<T> {
Vec3 { x: x, y: y, z: z }
}
#[inline] #[inline]
pub fn from_value(value: T) -> Vec3<T> { pub fn from_value(value: T) -> Vec3<T> {
Vec3::new(value, value, value) Vec3::new(value, value, value)
@ -61,13 +68,6 @@ impl<T:Copy> Vec3<T> {
*self.index_mut(a) = *self.index(b); *self.index_mut(a) = *self.index(b);
*self.index_mut(b) = tmp; *self.index_mut(b) = tmp;
} }
#[inline(always)]
pub fn map(&self, f: &fn(&T) -> T) -> Vec3<T> {
Vec3::new(f(self.index(0)),
f(self.index(1)),
f(self.index(2)))
}
} }
impl<T:Copy + Num> Vec3<T> { impl<T:Copy + Num> Vec3<T> {

View file

@ -21,6 +21,11 @@ use std::num::{Zero, One};
pub struct Vec4<T> { x: T, y: T, z: T, w: T } pub struct Vec4<T> { x: T, y: T, z: T, w: T }
impl<T> Vec4<T> { impl<T> Vec4<T> {
#[inline]
pub fn new(x: T, y: T, z: T, w: T ) -> Vec4<T> {
Vec4 { x: x, y: y, z: z, w: w }
}
#[inline] #[inline]
pub fn index<'a>(&'a self, i: uint) -> &'a T { pub fn index<'a>(&'a self, i: uint) -> &'a T {
&'a self.as_slice()[i] &'a self.as_slice()[i]
@ -40,14 +45,17 @@ impl<T> Vec4<T> {
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] { pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T,..4] {
unsafe { transmute(self) } unsafe { transmute(self) }
} }
#[inline(always)]
pub fn map(&self, f: &fn(&T) -> T) -> Vec4<T> {
Vec4::new(f(self.index(0)),
f(self.index(1)),
f(self.index(2)),
f(self.index(3)))
}
} }
impl<T:Copy> Vec4<T> { impl<T:Copy> Vec4<T> {
#[inline]
pub fn new(x: T, y: T, z: T, w: T ) -> Vec4<T> {
Vec4 { x: x, y: y, z: z, w: w }
}
#[inline] #[inline]
pub fn from_value(value: T) -> Vec4<T> { pub fn from_value(value: T) -> Vec4<T> {
Vec4::new(value, value, value, value) Vec4::new(value, value, value, value)
@ -59,14 +67,6 @@ impl<T:Copy> Vec4<T> {
*self.index_mut(a) = *self.index(b); *self.index_mut(a) = *self.index(b);
*self.index_mut(b) = tmp; *self.index_mut(b) = tmp;
} }
#[inline(always)]
pub fn map(&self, f: &fn(&T) -> T) -> Vec4<T> {
Vec4::new(f(self.index(0)),
f(self.index(1)),
f(self.index(2)),
f(self.index(3)))
}
} }
impl<T:Copy + Num> Vec4<T> { impl<T:Copy + Num> Vec4<T> {