From 6a9d690c792c433db8ff19b40bc59792b7f61cf5 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sat, 13 Jul 2013 23:37:38 +1000 Subject: [PATCH] Arrange vector methods into traits --- src/bounds/aabb.rs | 4 +- src/bounds/frustum.rs | 5 +-- src/bounds/sphere.rs | 2 +- src/color/hsv.rs | 5 +-- src/color/rgb.rs | 5 +-- src/color/srgb.rs | 4 +- src/color/ycbcr.rs | 3 +- src/core/core.rs | 2 + src/core/vec.rs | 95 ++++++++++++++++++++++++++++++++++++------- src/geom/plane.rs | 7 +++- src/geom/point.rs | 10 ++--- src/geom/ray.rs | 6 ++- 12 files changed, 106 insertions(+), 42 deletions(-) diff --git a/src/bounds/aabb.rs b/src/bounds/aabb.rs index 155e8ca..c063ff8 100644 --- a/src/bounds/aabb.rs +++ b/src/bounds/aabb.rs @@ -15,8 +15,8 @@ //! Axis-aligned bounding boxes -use core::{Vec2, AsVec2, Vec3, AsVec3}; -use geom::{Point2, Point3}; +use core::*; +use geom::*; #[deriving(Clone, Eq)] pub struct AABB2 { diff --git a/src/bounds/frustum.rs b/src/bounds/frustum.rs index f13de2d..58ed7c2 100644 --- a/src/bounds/frustum.rs +++ b/src/bounds/frustum.rs @@ -13,9 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use core::Dimensional; -use core::Mat4; -use geom::{Plane3, Point3}; +use core::*; +use geom::*; #[deriving(Clone, Eq)] pub struct Frustum { diff --git a/src/bounds/sphere.rs b/src/bounds/sphere.rs index f8c7759..7355e72 100644 --- a/src/bounds/sphere.rs +++ b/src/bounds/sphere.rs @@ -15,7 +15,7 @@ //! Bounding sphere -use geom::Point3; +use geom::*; #[deriving(Clone, Eq)] pub struct Sphere { diff --git a/src/color/hsv.rs b/src/color/hsv.rs index e816c2e..2412bff 100644 --- a/src/color/hsv.rs +++ b/src/color/hsv.rs @@ -16,9 +16,8 @@ use std::num; use std::cast; -use core::{Dimensional, Swap}; -use core::{Vec3, ToVec3, AsVec3}; -use core::{Vec4, ToVec4, AsVec4}; +use core::*; + use color::{Color, FloatColor}; use color::{Channel, FloatChannel}; use color::{RGB, ToRGB, RGBA, ToRGBA}; diff --git a/src/color/rgb.rs b/src/color/rgb.rs index 326c864..559df86 100644 --- a/src/color/rgb.rs +++ b/src/color/rgb.rs @@ -16,9 +16,8 @@ use std::num; use std::cast; -use core::{Dimensional, Swap}; -use core::{Vec3, ToVec3, AsVec3}; -use core::{Vec4, ToVec4, AsVec4}; +use core::*; + use color::{Color, FloatColor}; use color::{Channel, FloatChannel}; use color::{HSV, ToHSV, HSVA, ToHSVA}; diff --git a/src/color/srgb.rs b/src/color/srgb.rs index 6e062f0..f222851 100644 --- a/src/color/srgb.rs +++ b/src/color/srgb.rs @@ -13,9 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use core::{Dimensional, Swap}; -use core::{Vec3, ToVec3, AsVec3}; -use core::{Vec4, ToVec4, AsVec4}; +use core::*; #[deriving(Clone, Eq)] pub struct SRGB { r: T, g: T, b: T } diff --git a/src/color/ycbcr.rs b/src/color/ycbcr.rs index 34a2e42..ebfc3a3 100644 --- a/src/color/ycbcr.rs +++ b/src/color/ycbcr.rs @@ -15,8 +15,7 @@ // http://en.wikipedia.org/wiki/YCbCr -use core::{Dimensional, Swap}; -use core::{Vec3, ToVec3, AsVec3}; +use core::*; #[deriving(Clone, Eq)] pub struct YCbCr { y: T, cb: T, cr: T } diff --git a/src/core/core.rs b/src/core/core.rs index 8638cca..b414d81 100644 --- a/src/core/core.rs +++ b/src/core/core.rs @@ -19,6 +19,8 @@ pub use self::mat::{Mat2, ToMat2}; pub use self::mat::{Mat3, ToMat3}; pub use self::mat::{Mat4, ToMat4}; pub use self::quat::{Quat, ToQuat}; +pub use self::vec::{NumVec, RealVec}; +pub use self::vec::{OrdVec, EqVec, BoolVec}; pub use self::vec::{Vec2, ToVec2, AsVec2}; pub use self::vec::{Vec3, ToVec3, AsVec3}; pub use self::vec::{Vec4, ToVec4, AsVec4}; diff --git a/src/core/vec.rs b/src/core/vec.rs index b264f88..2f99ec9 100644 --- a/src/core/vec.rs +++ b/src/core/vec.rs @@ -15,6 +15,71 @@ use core::{Dimensional, Swap}; +/// Vectors with numeric components +pub trait NumVec: Neg { + pub fn add_t(&self, value: T) -> Self; + pub fn sub_t(&self, value: T) -> Self; + pub fn mul_t(&self, value: T) -> Self; + pub fn div_t(&self, value: T) -> Self; + pub fn rem_t(&self, value: T) -> Self; + pub fn add_v(&self, other: &Self) -> Self; + pub fn sub_v(&self, other: &Self) -> Self; + pub fn mul_v(&self, other: &Self) -> Self; + pub fn div_v(&self, other: &Self) -> Self; + pub fn rem_v(&self, other: &Self) -> Self; + pub fn neg_self(&mut self); + pub fn add_self_t(&mut self, value: T); + pub fn sub_self_t(&mut self, value: T); + pub fn mul_self_t(&mut self, value: T); + pub fn div_self_t(&mut self, value: T); + pub fn rem_self_t(&mut self, value: T); + pub fn add_self_v(&mut self, other: &Self); + pub fn sub_self_v(&mut self, other: &Self); + pub fn mul_self_v(&mut self, other: &Self); + pub fn div_self_v(&mut self, other: &Self); + pub fn rem_self_v(&mut self, other: &Self); + pub fn dot(&self, other: &Self) -> T; +} + +/// Vectors with real components +pub trait RealVec: NumVec + ApproxEq { + pub fn magnitude2(&self) -> T; + pub fn magnitude(&self) -> T; + pub fn angle(&self, other: &Self) -> T; + pub fn normalize(&self) -> Self; + pub fn normalize_to(&self, magnitude: T) -> Self; + pub fn lerp(&self, other: &Self, amount: T) -> Self; + pub fn normalize_self(&mut self); + pub fn normalize_self_to(&mut self, magnitude: T); + pub fn lerp_self(&mut self, other: &Self, amount: T); +} + +/// Vectors with orderable components +pub trait OrdVec { + pub fn lt_t(&self, value: T) -> BV; + pub fn le_t(&self, value: T) -> BV; + pub fn ge_t(&self, value: T) -> BV; + pub fn gt_t(&self, value: T) -> BV; + pub fn lt_v(&self, other: &Self) -> BV; + pub fn le_v(&self, other: &Self) -> BV; + pub fn ge_v(&self, other: &Self) -> BV; + pub fn gt_v(&self, other: &Self) -> BV; +} + +/// Vectors with components that can be tested for equality +pub trait EqVec: Eq { + pub fn eq_t(&self, value: T) -> BV; + pub fn ne_t(&self, value: T) -> BV; + pub fn eq_v(&self, other: &Self) -> BV; + pub fn ne_v(&self, other: &Self) -> BV; +} + +/// Vectors with boolean components +pub trait BoolVec: Not { + pub fn any(&self) -> bool; + pub fn all(&self) -> bool; +} + #[deriving(Clone, Eq)] pub struct Vec2 { x: T, y: T } @@ -114,7 +179,7 @@ impl Vec2 { } } -impl Vec2 { +impl NumVec for Vec2 { /// Returns a new vector with `value` added to each component. #[inline] pub fn add_t(&self, value: T) -> Vec2 { @@ -263,7 +328,7 @@ impl Neg> for Vec2 { } } -impl Vec2 { +impl RealVec for Vec2 { /// Returns the squared magnitude of the vector. This does not perform a /// square root operation like in the `magnitude` method and can therefore /// be more efficient for comparing the magnitudes of two vectors. @@ -325,7 +390,7 @@ impl Vec2 { } } -impl Vec2 { +impl OrdVec> for Vec2 { #[inline] pub fn lt_t(&self, value: T) -> Vec2 { Vec2::new(*self.index(0) < value, @@ -375,7 +440,7 @@ impl Vec2 { } } -impl Vec2 { +impl EqVec> for Vec2 { #[inline] pub fn eq_t(&self, value: T) -> Vec2 { Vec2::new(*self.index(0) == value, @@ -401,7 +466,7 @@ impl Vec2 { } } -impl Vec2 { +impl BoolVec for Vec2 { /// Returns `true` if any of the components of the vector are equal to /// `true`, otherwise `false`. #[inline] @@ -667,7 +732,7 @@ impl Vec3 { } } -impl Vec3 { +impl NumVec for Vec3 { /// Returns a new vector with `value` added to each component. #[inline] pub fn add_t(&self, value: T) -> Vec3 { @@ -839,7 +904,7 @@ impl Neg> for Vec3 { } } -impl Vec3 { +impl RealVec for Vec3 { /// Returns the squared magnitude of the vector. This does not perform a /// square root operation like in the `magnitude` method and can therefore /// be more efficient for comparing the magnitudes of two vectors. @@ -901,7 +966,7 @@ impl Vec3 { } } -impl Vec3 { +impl OrdVec> for Vec3 { #[inline] pub fn lt_t(&self, value: T) -> Vec3 { Vec3::new(*self.index(0) < value, @@ -959,7 +1024,7 @@ impl Vec3 { } } -impl Vec3 { +impl EqVec> for Vec3 { #[inline] pub fn eq_t(&self, value: T) -> Vec3 { Vec3::new(*self.index(0) == value, @@ -989,7 +1054,7 @@ impl Vec3 { } } -impl Vec3 { +impl BoolVec for Vec3 { /// Returns `true` if any of the components of the vector are equal to /// `true`, otherwise `false`. #[inline] @@ -1248,7 +1313,7 @@ impl Vec4 { } } -impl Vec4 { +impl NumVec for Vec4 { /// Returns a new vector with `value` added to each component. #[inline] pub fn add_t(&self, value: T) -> Vec4 { @@ -1443,7 +1508,7 @@ impl Neg> for Vec4 { } } -impl Vec4 { +impl RealVec for Vec4 { /// Returns the squared magnitude of the vector. This does not perform a /// square root operation like in the `magnitude` method and can therefore /// be more efficient for comparing the magnitudes of two vectors. @@ -1505,7 +1570,7 @@ impl Vec4 { } } -impl Vec4 { +impl OrdVec> for Vec4 { #[inline] pub fn lt_t(&self, value: T) -> Vec4 { Vec4::new(*self.index(0) < value, @@ -1571,7 +1636,7 @@ impl Vec4 { } } -impl Vec4 { +impl EqVec> for Vec4 { #[inline] pub fn eq_t(&self, value: T) -> Vec4 { Vec4::new(*self.index(0) == value, @@ -1605,7 +1670,7 @@ impl Vec4 { } } -impl Vec4 { +impl BoolVec for Vec4 { /// Returns `true` if any of the components of the vector are equal to /// `true`, otherwise `false`. #[inline] diff --git a/src/geom/plane.rs b/src/geom/plane.rs index 6405121..1b95c18 100644 --- a/src/geom/plane.rs +++ b/src/geom/plane.rs @@ -13,8 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -use core::{Vec3, AsVec3, Vec4, Mat3}; -use geom::{Point, Point3, Ray3}; +use core::*; + +use geom::Point; +use geom::Point3; +use geom::Ray3; /// A plane formed from the equation: `Ax + Bx + Cx + D = 0` /// diff --git a/src/geom/point.rs b/src/geom/point.rs index d00cad1..9786324 100644 --- a/src/geom/point.rs +++ b/src/geom/point.rs @@ -22,12 +22,10 @@ use std::cast; -use core::{Dimensional, Swap}; -use core::{Mat2, Mat3, Quat}; -use core::{Vec2, ToVec2, AsVec2}; -use core::{Vec3, ToVec3, AsVec3}; -use core::{Vec4, ToVec4}; -use geom::{Ray2, Ray3}; +use core::*; + +use geom::Ray2; +use geom::Ray3; /// A geometric point pub trait Point: Eq diff --git a/src/geom/ray.rs b/src/geom/ray.rs index b04d151..d793d30 100644 --- a/src/geom/ray.rs +++ b/src/geom/ray.rs @@ -13,8 +13,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -use core::{Vec2, Vec3}; -use geom::{Point2, Point3}; +use core::*; + +use geom::Point2; +use geom::Point3; #[deriving(Clone, Eq)] pub struct Ray2 {