Remove Dimensional and ToPtr traits

This commit is contained in:
Brendan Zabarauskas 2012-12-12 11:29:35 +10:00
parent 3abc1df753
commit 1b24495c09
6 changed files with 138 additions and 239 deletions

View file

@ -6,14 +6,13 @@ use core::vec::raw::buf_as_slice;
use angle::Degrees;
use channel::Channel;
use dim::{Dimensional, ToPtr};
use funs::common::Sign;
use num::types::{Float, Number};
/**
* A generic color trait.
*/
pub trait Color<T>: Dimensional<T> ToPtr<T> Eq {
pub trait Color<T>: Index<uint, T> Eq {
/**
* # Return value
*
@ -103,6 +102,13 @@ pub trait Color<T>: Dimensional<T> ToPtr<T> Eq {
* to `1f64`.
*/
pure fn to_hsv_f64(&self) -> HSV<f64>;
/**
* # Return value
*
* A pointer to the first component of the color
*/
pure fn to_ptr(&self) -> *T;
}
pub trait MutableColor<T>: Color<T> {
@ -255,34 +261,13 @@ pub impl<T:Copy> RGB<T> {
}
}
pub impl<T> RGB<T>: Dimensional<T> {
#[inline(always)]
static pure fn dim() -> uint { 3 }
#[inline(always)]
static pure fn size_of() -> uint {
size_of::<RGB<T>>()
}
}
pub impl<T:Copy> RGB<T>: Index<uint, T> {
pub impl<T:Copy Number Channel> RGB<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } }
}
}
pub impl<T:Copy> RGB<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*RGB<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Number Channel> RGB<T>: Color<T> {
#[inline(always)]
pure fn inverse(&self) -> RGB<T> {
@ -335,6 +320,15 @@ pub impl<T:Copy Number Channel> RGB<T>: Color<T> {
#[inline(always)] pure fn to_hsv_f32(&self) -> HSV<f32> { to_hsv(&self.to_rgb_f32()) }
#[inline(always)] pure fn to_hsv_f64(&self) -> HSV<f64> { to_hsv(&self.to_rgb_f64()) }
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*RGB<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Channel> RGB<T>: MutableColor<T> {
@ -417,34 +411,13 @@ pub impl<T:Copy> RGBA<T> {
}
}
pub impl<T> RGBA<T>: Dimensional<T> {
#[inline(always)]
static pure fn dim() -> uint { 4 }
#[inline(always)]
static pure fn size_of() -> uint {
size_of::<RGBA<T>>()
}
}
pub impl<T:Copy> RGBA<T>: Index<uint, T> {
pub impl<T:Copy Number Channel> RGBA<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } }
}
}
pub impl<T:Copy> RGBA<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*RGBA<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Number Channel> RGBA<T>: Color<T> {
#[inline(always)]
pure fn inverse(&self) -> RGBA<T> {
@ -498,6 +471,15 @@ pub impl<T:Copy Number Channel> RGBA<T>: Color<T> {
#[inline(always)] pure fn to_hsv_f32(&self) -> HSV<f32> { to_hsv(&self.to_rgb_f32()) }
#[inline(always)] pure fn to_hsv_f64(&self) -> HSV<f64> { to_hsv(&self.to_rgb_f64()) }
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*RGBA<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Channel> RGBA<T>: MutableColor<T> {
@ -575,34 +557,13 @@ pub impl<T:Copy> HSV<T> {
}
}
pub impl<T> HSV<T>: Dimensional<T> {
#[inline(always)]
static pure fn dim() -> uint { 3 }
#[inline(always)]
static pure fn size_of() -> uint {
size_of::<HSV<T>>()
}
}
pub impl<T:Copy> HSV<T>: Index<uint, T> {
pub impl<T:Copy Float Channel> HSV<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } }
}
}
pub impl<T:Copy> HSV<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*HSV<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Float Channel> HSV<T>: Color<T> {
#[inline(always)]
pure fn inverse(&self) -> HSV<T> {
@ -631,6 +592,15 @@ pub impl<T:Copy Float Channel> HSV<T>: Color<T> {
self.s.to_channel_f64(),
self.v.to_channel_f64())
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*HSV<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Channel Float> HSV<T>: MutableColor<T> {
@ -713,34 +683,13 @@ pub impl<T:Copy> HSVA<T> {
}
}
pub impl<T> HSVA<T>: Dimensional<T> {
#[inline(always)]
static pure fn dim() -> uint { 4 }
#[inline(always)]
static pure fn size_of() -> uint {
size_of::<HSVA<T>>()
}
}
pub impl<T:Copy> HSVA<T>: Index<uint, T> {
pub impl<T:Copy Float Channel> HSVA<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } }
}
}
pub impl<T:Copy> HSVA<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*HSVA<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Float Channel> HSVA<T>: Color<T> {
#[inline(always)]
pure fn inverse(&self) -> HSVA<T> {
@ -770,6 +719,15 @@ pub impl<T:Copy Float Channel> HSVA<T>: Color<T> {
self.s.to_channel_f64(),
self.v.to_channel_f64())
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*HSVA<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Channel Float> HSVA<T>: MutableColor<T> {

View file

@ -1,8 +0,0 @@
pub trait Dimensional<T>: Index<uint, T> {
static pure fn dim() -> uint; // dim and size_of are pretty useless at the moment due to
static pure fn size_of() -> uint; // how Rust's static methods are currently implemented
}
pub trait ToPtr<T> {
pure fn to_ptr(&self) -> *T;
}

View file

@ -11,7 +11,6 @@
extern mod std;
pub mod angle;
pub mod dim;
pub mod gltypes;
pub mod mat;
pub mod quat;

View file

@ -7,7 +7,6 @@ use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq;
use angle::Angle;
use dim::{Dimensional, ToPtr};
use funs::common::*;
use funs::exponential::*;
use funs::triganomic::{sin, cos};
@ -26,7 +25,7 @@ use vec::{NumericVector, Vec2, Vec3, Vec4};
* floating point type and have the same number of dimensions as the
* number of rows and columns in the matrix.
*/
pub trait Matrix<T,V>: Dimensional<V> ToPtr<T> Eq Neg<self> {
pub trait Matrix<T,V>: Index<uint, V> Eq Neg<self> {
/**
* # Return value
*
@ -173,6 +172,13 @@ pub trait Matrix<T,V>: Dimensional<V> ToPtr<T> Eq Neg<self> {
* `true` if the matrix is invertable
*/
pure fn is_invertible(&self) -> bool;
/**
* # Return value
*
* A pointer to the first element of the matrix
*/
pure fn to_ptr(&self) -> *T;
}
/**
@ -517,6 +523,15 @@ pub impl<T:Copy Float> Mat2<T>: Matrix<T, Vec2<T>> {
// let _0 = Number::from(0); // FIXME: causes ICE
!self.determinant().fuzzy_eq(&_0)
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*Mat2<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Float Sign> Mat2<T>: MutableMatrix<T, Vec2<T>> {
@ -601,14 +616,6 @@ pub impl<T:Copy Float> Mat2<T>: Matrix2<T, Vec2<T>> {
}
}
pub impl<T:Copy> Mat2<T>: Dimensional<Vec2<T>> {
#[inline(always)]
static pure fn dim() -> uint { 2 }
#[inline(always)]
static pure fn size_of() -> uint { size_of::<Mat2<T>>() }
}
pub impl<T:Copy> Mat2<T>: Index<uint, Vec2<T>> {
#[inline(always)]
pure fn index(&self, i: uint) -> Vec2<T> {
@ -619,17 +626,6 @@ pub impl<T:Copy> Mat2<T>: Index<uint, Vec2<T>> {
}
}
pub impl<T:Copy> Mat2<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*Mat2<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Float> Mat2<T>: Neg<Mat2<T>> {
#[inline(always)]
pure fn neg(&self) -> Mat2<T> {
@ -962,6 +958,15 @@ pub impl<T:Copy Float> Mat3<T>: Matrix<T, Vec3<T>> {
// let _0 = Number::from(0); // FIXME: causes ICE
!self.determinant().fuzzy_eq(&_0)
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*Mat3<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Float Sign> Mat3<T>: MutableMatrix<T, Vec3<T>> {
@ -1117,14 +1122,6 @@ pub impl<T:Copy Float Exp> Mat3<T>: ToQuat<T> {
}
}
pub impl<T:Copy> Mat3<T>: Dimensional<Vec3<T>> {
#[inline(always)]
static pure fn dim() -> uint { 3 }
#[inline(always)]
static pure fn size_of() -> uint { size_of::<Mat3<T>>() }
}
pub impl<T:Copy> Mat3<T>: Index<uint, Vec3<T>> {
#[inline(always)]
pure fn index(&self, i: uint) -> Vec3<T> {
@ -1135,17 +1132,6 @@ pub impl<T:Copy> Mat3<T>: Index<uint, Vec3<T>> {
}
}
pub impl<T:Copy> Mat3<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*Mat3<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Float> Mat3<T>: Neg<Mat3<T>> {
#[inline(always)]
pure fn neg(&self) -> Mat3<T> {
@ -1574,6 +1560,15 @@ pub impl<T:Copy Float Sign> Mat4<T>: Matrix<T, Vec4<T>> {
let _0 = cast(0);
!self.determinant().fuzzy_eq(&_0)
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*Mat4<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Float Sign> Mat4<T>: MutableMatrix<T, Vec4<T>> {
@ -1679,14 +1674,6 @@ pub impl<T:Copy Float> Mat4<T>: Neg<Mat4<T>> {
}
}
pub impl<T> Mat4<T>: Dimensional<Vec4<T>> {
#[inline(always)]
static pure fn dim() -> uint { 4 }
#[inline(always)]
static pure fn size_of() -> uint { size_of::<Mat4<T>>() }
}
pub impl<T:Copy> Mat4<T>: Index<uint, Vec4<T>> {
#[inline(always)]
pure fn index(&self, i: uint) -> Vec4<T> {
@ -1697,17 +1684,6 @@ pub impl<T:Copy> Mat4<T>: Index<uint, Vec4<T>> {
}
}
pub impl<T:Copy> Mat4<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*Mat4<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Float> Mat4<T>: Eq {
#[inline(always)]
pure fn eq(&self, other: &Mat4<T>) -> bool {

View file

@ -16,7 +16,6 @@ use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq;
use angle::Angle;
use dim::{Dimensional, ToPtr};
use funs::common::*;
use funs::exponential::*;
use funs::triganomic::*;
@ -35,9 +34,7 @@ use vec::Vec3;
* * `V3` - The 3-dimensional vector type that will containin the imaginary
* components of the quaternion.
*/
pub trait Quaternion<T,V3>: Dimensional<T> ToPtr<T> Eq Neg<self> {
static pure fn from_axis_angle<A:Angle<T>>(axis: &Vec3<T>, theta: A) -> self;
pub trait Quaternion<T,V3>: Index<uint, T> Eq Neg<self> {
/**
* # Return value
*
@ -177,6 +174,13 @@ pub trait Quaternion<T,V3>: Dimensional<T> ToPtr<T> Eq Neg<self> {
* Convert the quaternion to a 4 x 4 transformation matrix
*/
pure fn to_mat4(&self) -> Mat4<T>;
/**
* # Return value
*
* A pointer to the first component of the quaternion
*/
pure fn to_ptr(&self) -> *T;
}
pub trait ToQuat<T> {
@ -204,7 +208,7 @@ pub trait ToQuat<T> {
*/
pub struct Quat<T> { s: T, v: Vec3<T> }
pub impl<T> Quat<T> {
pub impl<T:Copy Float> Quat<T> {
/**
* Construct the quaternion from one scalar component and three
* imaginary components
@ -233,41 +237,26 @@ pub impl<T> Quat<T> {
static pure fn from_sv(s: T, v: Vec3<T>) -> Quat<T> {
Quat { s: move s, v: move v }
}
}
pub impl<T> Quat<T>: Dimensional<T> {
#[inline(always)]
static pure fn dim() -> uint { 4 }
#[inline(always)]
static pure fn size_of() -> uint { size_of::<Quat<T>>() }
static pure fn from_axis_angle<A:Angle<T>>(axis: &Vec3<T>, theta: A) -> Quat<T> {
// let half = theta.to_radians() / Number::from(2);
let half = theta.to_radians() / cast(2);
Quat::from_sv(cos(&half), axis.mul_t(sin(&half)))
}
}
pub impl<T:Copy> Quat<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } }
}
}
pub impl<T:Copy> Quat<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
unsafe { do buf_as_slice(
transmute::<*Quat<T>, *T>(
to_unsafe_ptr(self)
)
to_unsafe_ptr(self)), 4) |slice| { slice[i] }
}
}
}
pub impl<T:Copy Float Exp Extent InvTrig> Quat<T>: Quaternion<T, Vec3<T>> {
#[inline(always)]
static pure fn from_axis_angle<A:Angle<T>>(axis: &Vec3<T>, theta: A) -> Quat<T> {
let half = theta.to_radians() / Number::from(2);
Quat::from_sv(cos(&half), axis.mul_t(sin(&half)))
}
#[inline(always)]
static pure fn identity() -> Quat<T> {
Quat::new(Number::from(1),
@ -436,9 +425,18 @@ pub impl<T:Copy Float Exp Extent InvTrig> Quat<T>: Quaternion<T, Vec3<T>> {
pure fn to_mat4(&self) -> Mat4<T> {
self.to_mat3().to_mat4()
}
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
transmute::<*Quat<T>, *T>(
to_unsafe_ptr(self)
)
}
}
}
pub impl<T:Copy Num> Quat<T>: Neg<Quat<T>> {
pub impl<T:Copy Float> Quat<T>: Neg<Quat<T>> {
#[inline(always)]
pure fn neg(&self) -> Quat<T> {
Quat::new(-self[0], -self[1], -self[2], -self[3])

View file

@ -6,7 +6,6 @@ use core::vec::raw::buf_as_slice;
use std::cmp::FuzzyEq;
use dim::{Dimensional, ToPtr};
use funs::exponential::Exp;
use num::types::Number;
@ -18,11 +17,18 @@ use num::types::Number;
* * `T` - The type of the components. This is intended to support boolean,
* integer, unsigned integer, and floating point types.
*/
pub trait Vector<T>: Dimensional<T> ToPtr<T> Eq {
pub trait Vector<T>: Index<uint, T> Eq {
/**
* Construct the vector from a single value, copying it to each component
*/
static pure fn from_value(value: T) -> self;
/**
* # Return value
*
* A pointer to the first component of the vector
*/
pure fn to_ptr(&self) -> *T;
}
pub trait MutableVector<T>: Vector<T> {
@ -301,24 +307,7 @@ pub impl<T:Copy> Vec2<T>: Vector<T> {
static pure fn from_value(value: T) -> Vec2<T> {
Vec2::new(value, value)
}
}
pub impl<T> Vec2<T>: Dimensional<T> {
#[inline(always)]
static pure fn dim() -> uint { 2 }
#[inline(always)]
static pure fn size_of() -> uint { size_of::<Vec2<T>>() }
}
pub impl<T:Copy> Vec2<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } }
}
}
pub impl<T:Copy> Vec2<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -329,6 +318,13 @@ pub impl<T:Copy> Vec2<T>: ToPtr<T> {
}
}
pub impl<T:Copy> Vec2<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } }
}
}
pub impl<T:Copy> Vec2<T>: MutableVector<T> {
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &self/mut T {
@ -530,24 +526,7 @@ pub impl<T:Copy> Vec3<T>: Vector<T> {
static pure fn from_value(value: T) -> Vec3<T> {
Vec3::new(value, value, value)
}
}
pub impl<T> Vec3<T>: Dimensional<T> {
#[inline(always)]
static pure fn dim() -> uint { 3 }
#[inline(always)]
static pure fn size_of() -> uint { size_of::<Vec3<T>>() }
}
pub impl<T:Copy> Vec3<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } }
}
}
pub impl<T:Copy> Vec3<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -558,6 +537,13 @@ pub impl<T:Copy> Vec3<T>: ToPtr<T> {
}
}
pub impl<T:Copy> Vec3<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } }
}
}
pub impl<T:Copy> Vec3<T>: MutableVector<T> {
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &self/mut T {
@ -791,24 +777,7 @@ pub impl<T:Copy> Vec4<T>: Vector<T> {
static pure fn from_value(value: T) -> Vec4<T> {
Vec4::new(value, value, value, value)
}
}
pub impl<T> Vec4<T>: Dimensional<T> {
#[inline(always)]
static pure fn dim() -> uint { 4 }
#[inline(always)]
static pure fn size_of() -> uint { size_of::<Vec4<T>>() }
}
pub impl<T:Copy> Vec4<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } }
}
}
pub impl<T:Copy> Vec4<T>: ToPtr<T> {
#[inline(always)]
pure fn to_ptr(&self) -> *T {
unsafe {
@ -819,6 +788,13 @@ pub impl<T:Copy> Vec4<T>: ToPtr<T> {
}
}
pub impl<T:Copy> Vec4<T>: Index<uint, T> {
#[inline(always)]
pure fn index(&self, i: uint) -> T {
unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } }
}
}
pub impl<T:Copy> Vec4<T>: MutableVector<T> {
#[inline(always)]
fn index_mut(&mut self, i: uint) -> &self/mut T {