Arrange vector methods into traits

This commit is contained in:
Brendan Zabarauskas 2013-07-13 23:37:38 +10:00
parent 0e483bb579
commit 6a9d690c79
12 changed files with 106 additions and 42 deletions

View file

@ -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<T> {

View file

@ -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<T> {

View file

@ -15,7 +15,7 @@
//! Bounding sphere
use geom::Point3;
use geom::*;
#[deriving(Clone, Eq)]
pub struct Sphere<T> {

View file

@ -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};

View file

@ -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};

View file

@ -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<T> { r: T, g: T, b: T }

View file

@ -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<T> { y: T, cb: T, cr: T }

View file

@ -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};

View file

@ -15,6 +15,71 @@
use core::{Dimensional, Swap};
/// Vectors with numeric components
pub trait NumVec<T>: Neg<T> {
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<T>: NumVec<T> + ApproxEq<T> {
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<T,BV> {
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<T,BV>: 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<Self> {
pub fn any(&self) -> bool;
pub fn all(&self) -> bool;
}
#[deriving(Clone, Eq)]
pub struct Vec2<T> { x: T, y: T }
@ -114,7 +179,7 @@ impl<T:Num> Vec2<T> {
}
}
impl<T:Num> Vec2<T> {
impl<T:Num> NumVec<T> for Vec2<T> {
/// Returns a new vector with `value` added to each component.
#[inline]
pub fn add_t(&self, value: T) -> Vec2<T> {
@ -263,7 +328,7 @@ impl<T:Num> Neg<Vec2<T>> for Vec2<T> {
}
}
impl<T:Real> Vec2<T> {
impl<T:Real> RealVec<T> for Vec2<T> {
/// 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<T:Real> Vec2<T> {
}
}
impl<T:Ord> Vec2<T> {
impl<T:Ord> OrdVec<T,Vec2<bool>> for Vec2<T> {
#[inline]
pub fn lt_t(&self, value: T) -> Vec2<bool> {
Vec2::new(*self.index(0) < value,
@ -375,7 +440,7 @@ impl<T:Ord> Vec2<T> {
}
}
impl<T:Eq> Vec2<T> {
impl<T:Eq> EqVec<T,Vec2<bool>> for Vec2<T> {
#[inline]
pub fn eq_t(&self, value: T) -> Vec2<bool> {
Vec2::new(*self.index(0) == value,
@ -401,7 +466,7 @@ impl<T:Eq> Vec2<T> {
}
}
impl Vec2<bool> {
impl BoolVec for Vec2<bool> {
/// Returns `true` if any of the components of the vector are equal to
/// `true`, otherwise `false`.
#[inline]
@ -667,7 +732,7 @@ impl<T:Num> Vec3<T> {
}
}
impl<T:Num> Vec3<T> {
impl<T:Num> NumVec<T> for Vec3<T> {
/// Returns a new vector with `value` added to each component.
#[inline]
pub fn add_t(&self, value: T) -> Vec3<T> {
@ -839,7 +904,7 @@ impl<T:Num> Neg<Vec3<T>> for Vec3<T> {
}
}
impl<T:Real> Vec3<T> {
impl<T:Real> RealVec<T> for Vec3<T> {
/// 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<T:Real> Vec3<T> {
}
}
impl<T:Ord> Vec3<T> {
impl<T:Ord> OrdVec<T,Vec3<bool>> for Vec3<T> {
#[inline]
pub fn lt_t(&self, value: T) -> Vec3<bool> {
Vec3::new(*self.index(0) < value,
@ -959,7 +1024,7 @@ impl<T:Ord> Vec3<T> {
}
}
impl<T:Eq> Vec3<T> {
impl<T:Eq> EqVec<T,Vec3<bool>> for Vec3<T> {
#[inline]
pub fn eq_t(&self, value: T) -> Vec3<bool> {
Vec3::new(*self.index(0) == value,
@ -989,7 +1054,7 @@ impl<T:Eq> Vec3<T> {
}
}
impl Vec3<bool> {
impl BoolVec for Vec3<bool> {
/// Returns `true` if any of the components of the vector are equal to
/// `true`, otherwise `false`.
#[inline]
@ -1248,7 +1313,7 @@ impl<T:Num> Vec4<T> {
}
}
impl<T:Num> Vec4<T> {
impl<T:Num> NumVec<T> for Vec4<T> {
/// Returns a new vector with `value` added to each component.
#[inline]
pub fn add_t(&self, value: T) -> Vec4<T> {
@ -1443,7 +1508,7 @@ impl<T:Num> Neg<Vec4<T>> for Vec4<T> {
}
}
impl<T:Real> Vec4<T> {
impl<T:Real> RealVec<T> for Vec4<T> {
/// 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<T:Real> Vec4<T> {
}
}
impl<T:Ord> Vec4<T> {
impl<T:Ord> OrdVec<T,Vec4<bool>> for Vec4<T> {
#[inline]
pub fn lt_t(&self, value: T) -> Vec4<bool> {
Vec4::new(*self.index(0) < value,
@ -1571,7 +1636,7 @@ impl<T:Ord> Vec4<T> {
}
}
impl<T:Eq> Vec4<T> {
impl<T:Eq> EqVec<T,Vec4<bool>> for Vec4<T> {
#[inline]
pub fn eq_t(&self, value: T) -> Vec4<bool> {
Vec4::new(*self.index(0) == value,
@ -1605,7 +1670,7 @@ impl<T:Eq> Vec4<T> {
}
}
impl Vec4<bool> {
impl BoolVec for Vec4<bool> {
/// Returns `true` if any of the components of the vector are equal to
/// `true`, otherwise `false`.
#[inline]

View file

@ -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`
///

View file

@ -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<T, Vec, Ray>: Eq

View file

@ -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<T> {