From 1b24495c0963e16335765ed676bd50db6f5c530c Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 12 Dec 2012 11:29:35 +1000 Subject: [PATCH] Remove Dimensional and ToPtr traits --- src/color/color.rs | 138 ++++++++++++++++----------------------------- src/dim.rs | 8 --- src/lmath.rc | 1 - src/mat.rs | 94 ++++++++++++------------------ src/quat.rs | 54 +++++++++--------- src/vec.rs | 82 ++++++++++----------------- 6 files changed, 138 insertions(+), 239 deletions(-) delete mode 100644 src/dim.rs diff --git a/src/color/color.rs b/src/color/color.rs index 372d8ae..009fba6 100644 --- a/src/color/color.rs +++ b/src/color/color.rs @@ -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: Dimensional ToPtr Eq { +pub trait Color: Index Eq { /** * # Return value * @@ -103,6 +102,13 @@ pub trait Color: Dimensional ToPtr Eq { * to `1f64`. */ pure fn to_hsv_f64(&self) -> HSV; + + /** + * # Return value + * + * A pointer to the first component of the color + */ + pure fn to_ptr(&self) -> *T; } pub trait MutableColor: Color { @@ -255,34 +261,13 @@ pub impl RGB { } } -pub impl RGB: Dimensional { - #[inline(always)] - static pure fn dim() -> uint { 3 } - - #[inline(always)] - static pure fn size_of() -> uint { - size_of::>() - } -} - -pub impl RGB: Index { +pub impl RGB: Index { #[inline(always)] pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } } } -pub impl RGB: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*RGB, *T>( - to_unsafe_ptr(self) - ) - } - } -} - pub impl RGB: Color { #[inline(always)] pure fn inverse(&self) -> RGB { @@ -335,6 +320,15 @@ pub impl RGB: Color { #[inline(always)] pure fn to_hsv_f32(&self) -> HSV { to_hsv(&self.to_rgb_f32()) } #[inline(always)] pure fn to_hsv_f64(&self) -> HSV { to_hsv(&self.to_rgb_f64()) } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*RGB, *T>( + to_unsafe_ptr(self) + ) + } + } } pub impl RGB: MutableColor { @@ -417,34 +411,13 @@ pub impl RGBA { } } -pub impl RGBA: Dimensional { - #[inline(always)] - static pure fn dim() -> uint { 4 } - - #[inline(always)] - static pure fn size_of() -> uint { - size_of::>() - } -} - -pub impl RGBA: Index { +pub impl RGBA: Index { #[inline(always)] pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } -pub impl RGBA: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*RGBA, *T>( - to_unsafe_ptr(self) - ) - } - } -} - pub impl RGBA: Color { #[inline(always)] pure fn inverse(&self) -> RGBA { @@ -498,6 +471,15 @@ pub impl RGBA: Color { #[inline(always)] pure fn to_hsv_f32(&self) -> HSV { to_hsv(&self.to_rgb_f32()) } #[inline(always)] pure fn to_hsv_f64(&self) -> HSV { to_hsv(&self.to_rgb_f64()) } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*RGBA, *T>( + to_unsafe_ptr(self) + ) + } + } } pub impl RGBA: MutableColor { @@ -575,34 +557,13 @@ pub impl HSV { } } -pub impl HSV: Dimensional { - #[inline(always)] - static pure fn dim() -> uint { 3 } - - #[inline(always)] - static pure fn size_of() -> uint { - size_of::>() - } -} - -pub impl HSV: Index { +pub impl HSV: Index { #[inline(always)] pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } } } -pub impl HSV: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*HSV, *T>( - to_unsafe_ptr(self) - ) - } - } -} - pub impl HSV: Color { #[inline(always)] pure fn inverse(&self) -> HSV { @@ -631,6 +592,15 @@ pub impl HSV: Color { self.s.to_channel_f64(), self.v.to_channel_f64()) } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*HSV, *T>( + to_unsafe_ptr(self) + ) + } + } } pub impl HSV: MutableColor { @@ -713,34 +683,13 @@ pub impl HSVA { } } -pub impl HSVA: Dimensional { - #[inline(always)] - static pure fn dim() -> uint { 4 } - - #[inline(always)] - static pure fn size_of() -> uint { - size_of::>() - } -} - -pub impl HSVA: Index { +pub impl HSVA: Index { #[inline(always)] pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } -pub impl HSVA: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*HSVA, *T>( - to_unsafe_ptr(self) - ) - } - } -} - pub impl HSVA: Color { #[inline(always)] pure fn inverse(&self) -> HSVA { @@ -770,6 +719,15 @@ pub impl HSVA: Color { self.s.to_channel_f64(), self.v.to_channel_f64()) } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*HSVA, *T>( + to_unsafe_ptr(self) + ) + } + } } pub impl HSVA: MutableColor { diff --git a/src/dim.rs b/src/dim.rs deleted file mode 100644 index c1b1dc9..0000000 --- a/src/dim.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub trait Dimensional: Index { - 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 { - pure fn to_ptr(&self) -> *T; -} \ No newline at end of file diff --git a/src/lmath.rc b/src/lmath.rc index 25fbd7c..7f589cc 100644 --- a/src/lmath.rc +++ b/src/lmath.rc @@ -11,7 +11,6 @@ extern mod std; pub mod angle; -pub mod dim; pub mod gltypes; pub mod mat; pub mod quat; diff --git a/src/mat.rs b/src/mat.rs index c82dcec..c9ba72c 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -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: Dimensional ToPtr Eq Neg { +pub trait Matrix: Index Eq Neg { /** * # Return value * @@ -173,6 +172,13 @@ pub trait Matrix: Dimensional ToPtr Eq Neg { * `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 Mat2: Matrix> { // 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>( + to_unsafe_ptr(self) + ) + } + } } pub impl Mat2: MutableMatrix> { @@ -601,14 +616,6 @@ pub impl Mat2: Matrix2> { } } -pub impl Mat2: Dimensional> { - #[inline(always)] - static pure fn dim() -> uint { 2 } - - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - pub impl Mat2: Index> { #[inline(always)] pure fn index(&self, i: uint) -> Vec2 { @@ -619,17 +626,6 @@ pub impl Mat2: Index> { } } -pub impl Mat2: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Mat2, *T>( - to_unsafe_ptr(self) - ) - } - } -} - pub impl Mat2: Neg> { #[inline(always)] pure fn neg(&self) -> Mat2 { @@ -962,6 +958,15 @@ pub impl Mat3: Matrix> { // 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>( + to_unsafe_ptr(self) + ) + } + } } pub impl Mat3: MutableMatrix> { @@ -1117,14 +1122,6 @@ pub impl Mat3: ToQuat { } } -pub impl Mat3: Dimensional> { - #[inline(always)] - static pure fn dim() -> uint { 3 } - - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - pub impl Mat3: Index> { #[inline(always)] pure fn index(&self, i: uint) -> Vec3 { @@ -1135,17 +1132,6 @@ pub impl Mat3: Index> { } } -pub impl Mat3: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Mat3, *T>( - to_unsafe_ptr(self) - ) - } - } -} - pub impl Mat3: Neg> { #[inline(always)] pure fn neg(&self) -> Mat3 { @@ -1574,6 +1560,15 @@ pub impl Mat4: Matrix> { let _0 = cast(0); !self.determinant().fuzzy_eq(&_0) } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*Mat4, *T>( + to_unsafe_ptr(self) + ) + } + } } pub impl Mat4: MutableMatrix> { @@ -1679,14 +1674,6 @@ pub impl Mat4: Neg> { } } -pub impl Mat4: Dimensional> { - #[inline(always)] - static pure fn dim() -> uint { 4 } - - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - pub impl Mat4: Index> { #[inline(always)] pure fn index(&self, i: uint) -> Vec4 { @@ -1697,17 +1684,6 @@ pub impl Mat4: Index> { } } -pub impl Mat4: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { - transmute::<*Mat4, *T>( - to_unsafe_ptr(self) - ) - } - } -} - pub impl Mat4: Eq { #[inline(always)] pure fn eq(&self, other: &Mat4) -> bool { diff --git a/src/quat.rs b/src/quat.rs index 842cbe4..44407d9 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -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: Dimensional ToPtr Eq Neg { - static pure fn from_axis_angle>(axis: &Vec3, theta: A) -> self; - +pub trait Quaternion: Index Eq Neg { /** * # Return value * @@ -177,6 +174,13 @@ pub trait Quaternion: Dimensional ToPtr Eq Neg { * Convert the quaternion to a 4 x 4 transformation matrix */ pure fn to_mat4(&self) -> Mat4; + + /** + * # Return value + * + * A pointer to the first component of the quaternion + */ + pure fn to_ptr(&self) -> *T; } pub trait ToQuat { @@ -204,7 +208,7 @@ pub trait ToQuat { */ pub struct Quat { s: T, v: Vec3 } -pub impl Quat { +pub impl Quat { /** * Construct the quaternion from one scalar component and three * imaginary components @@ -233,41 +237,26 @@ pub impl Quat { static pure fn from_sv(s: T, v: Vec3) -> Quat { Quat { s: move s, v: move v } } -} - -pub impl Quat: Dimensional { - #[inline(always)] - static pure fn dim() -> uint { 4 } #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } + static pure fn from_axis_angle>(axis: &Vec3, theta: A) -> Quat { + // 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 Quat: Index { #[inline(always)] pure fn index(&self, i: uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } - } -} - -pub impl Quat: ToPtr { - #[inline(always)] - pure fn to_ptr(&self) -> *T { - unsafe { + unsafe { do buf_as_slice( transmute::<*Quat, *T>( - to_unsafe_ptr(self) - ) + to_unsafe_ptr(self)), 4) |slice| { slice[i] } } } } pub impl Quat: Quaternion> { - #[inline(always)] - static pure fn from_axis_angle>(axis: &Vec3, theta: A) -> Quat { - 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 { Quat::new(Number::from(1), @@ -436,9 +425,18 @@ pub impl Quat: Quaternion> { pure fn to_mat4(&self) -> Mat4 { self.to_mat3().to_mat4() } + + #[inline(always)] + pure fn to_ptr(&self) -> *T { + unsafe { + transmute::<*Quat, *T>( + to_unsafe_ptr(self) + ) + } + } } -pub impl Quat: Neg> { +pub impl Quat: Neg> { #[inline(always)] pure fn neg(&self) -> Quat { Quat::new(-self[0], -self[1], -self[2], -self[3]) diff --git a/src/vec.rs b/src/vec.rs index 40f2eba..42eb77d 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -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: Dimensional ToPtr Eq { +pub trait Vector: Index 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: Vector { @@ -301,24 +307,7 @@ pub impl Vec2: Vector { static pure fn from_value(value: T) -> Vec2 { Vec2::new(value, value) } -} - -pub impl Vec2: Dimensional { - #[inline(always)] - static pure fn dim() -> uint { 2 } - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - -pub impl Vec2: Index { - #[inline(always)] - pure fn index(&self, i: uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } } - } -} - -pub impl Vec2: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -329,6 +318,13 @@ pub impl Vec2: ToPtr { } } +pub impl Vec2: Index { + #[inline(always)] + pure fn index(&self, i: uint) -> T { + unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } } + } +} + pub impl Vec2: MutableVector { #[inline(always)] fn index_mut(&mut self, i: uint) -> &self/mut T { @@ -530,24 +526,7 @@ pub impl Vec3: Vector { static pure fn from_value(value: T) -> Vec3 { Vec3::new(value, value, value) } -} - -pub impl Vec3: Dimensional { - #[inline(always)] - static pure fn dim() -> uint { 3 } - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - -pub impl Vec3: Index { - #[inline(always)] - pure fn index(&self, i: uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } - } -} - -pub impl Vec3: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -558,6 +537,13 @@ pub impl Vec3: ToPtr { } } +pub impl Vec3: Index { + #[inline(always)] + pure fn index(&self, i: uint) -> T { + unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } + } +} + pub impl Vec3: MutableVector { #[inline(always)] fn index_mut(&mut self, i: uint) -> &self/mut T { @@ -791,24 +777,7 @@ pub impl Vec4: Vector { static pure fn from_value(value: T) -> Vec4 { Vec4::new(value, value, value, value) } -} - -pub impl Vec4: Dimensional { - #[inline(always)] - static pure fn dim() -> uint { 4 } - #[inline(always)] - static pure fn size_of() -> uint { size_of::>() } -} - -pub impl Vec4: Index { - #[inline(always)] - pure fn index(&self, i: uint) -> T { - unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } - } -} - -pub impl Vec4: ToPtr { #[inline(always)] pure fn to_ptr(&self) -> *T { unsafe { @@ -819,6 +788,13 @@ pub impl Vec4: ToPtr { } } +pub impl Vec4: Index { + #[inline(always)] + pure fn index(&self, i: uint) -> T { + unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } + } +} + pub impl Vec4: MutableVector { #[inline(always)] fn index_mut(&mut self, i: uint) -> &self/mut T {