Method rejiggering. Add origin method.

This commit is contained in:
Brendan Zabarauskas 2013-07-11 14:49:14 +10:00
parent 92ee628c06
commit 50747703cf
2 changed files with 54 additions and 30 deletions

View file

@ -85,8 +85,8 @@ impl<T:Clone + Float> Plane3<T> {
b: Point3<T>, b: Point3<T>,
c: Point3<T>) -> Option<Plane3<T>> { c: Point3<T>) -> Option<Plane3<T>> {
// create two vectors that run parallel to the plane // create two vectors that run parallel to the plane
let v0 = b.as_vec().sub_v(a.as_vec()); let v0 = (b - a);
let v1 = c.as_vec().sub_v(a.as_vec()); let v1 = (c - a);
// find the vector that is perpendicular to v1 and v2 // find the vector that is perpendicular to v1 and v2
let mut norm = v0.cross(&v1); let mut norm = v0.cross(&v1);
@ -136,11 +136,9 @@ impl<T:Clone + Float> Plane3<T> {
self.norm.y.clone(), other_a.norm.y.clone(), other_b.norm.y.clone(), self.norm.y.clone(), other_a.norm.y.clone(), other_b.norm.y.clone(),
self.norm.z.clone(), other_a.norm.z.clone(), other_b.norm.z.clone()); self.norm.z.clone(), other_a.norm.z.clone(), other_b.norm.z.clone());
do mx.inverse().map |m| { do mx.inverse().map |m| {
Point3::from_vec( Point3::origin() + m.mul_v(&Vec3::new(self.dist.clone(),
m.mul_v(&Vec3::new(self.dist.clone(), other_a.dist.clone(),
other_a.dist.clone(), other_b.dist.clone()))
other_b.dist.clone()))
)
} }
} }
} }

View file

@ -15,20 +15,22 @@
use std::cast; use std::cast;
use core::{Vec2, Vec3, Quat}; use core::{Mat2, Mat3, Quat, Vec2, Vec3};
#[path = "../num_macros.rs"]
mod num_macros;
/// A geometric point /// A geometric point
pub trait Point<T, Vec, Rot>: Eq pub trait Point<T, Vec>: Eq
+ Add<Vec, Self> + Add<Vec, Self>
+ Sub<Self, Vec> + Sub<Self, Vec>
+ Mul<Vec, Self> + Mul<Vec, Self>
+ ApproxEq<T> + ApproxEq<T>
+ ToStr { + ToStr {
pub fn as_vec<'a>(&'a self) -> &'a Vec; pub fn as_vec<'a>(&'a self) -> &'a Vec;
pub fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec; pub fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec;
pub fn translate(&self, offset: &Vec) -> Self; pub fn translate(&self, offset: &Vec) -> Self;
pub fn rotate(&self, rotation: &Rot) -> Self;
pub fn scale(&self, factor: &Vec) -> Self; pub fn scale(&self, factor: &Vec) -> Self;
pub fn distance2(&self, other: &Self) -> T; pub fn distance2(&self, other: &Self) -> T;
pub fn distance(&self, other: &Self) -> T; pub fn distance(&self, other: &Self) -> T;
@ -39,7 +41,7 @@ pub trait Point<T, Vec, Rot>: Eq
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct Point2<T> { x: T, y: T } pub struct Point2<T> { x: T, y: T }
impl<T> Point2<T> { impl<T:Num> Point2<T> {
#[inline] #[inline]
pub fn new(x: T, y: T) -> Point2<T> { pub fn new(x: T, y: T) -> Point2<T> {
Point2 { x: x, y: y } Point2 { x: x, y: y }
@ -49,9 +51,27 @@ impl<T> Point2<T> {
pub fn from_vec(vec: Vec2<T>) -> Point2<T> { pub fn from_vec(vec: Vec2<T>) -> Point2<T> {
unsafe { cast::transmute(vec) } unsafe { cast::transmute(vec) }
} }
#[inline]
pub fn origin() -> Point2<T> {
Point2::new(zero!(T), zero!(T))
}
} }
impl<T:Clone + Float> Point<T, Vec2<T>, T> for Point2<T> { impl<T:Clone + Float> Point2<T> {
#[inline]
pub fn rotate_t(&self, radians: &T) -> Point2<T> {
Point2::new((*self).x.cos() * (*radians),
(*self).y.sin() * (*radians))
}
#[inline]
pub fn rotate_m(&self, mat: &Mat2<T>) -> Point2<T> {
Point2::from_vec(mat.mul_v(self.as_vec()))
}
}
impl<T:Clone + Float> Point<T, Vec2<T>> for Point2<T> {
#[inline] #[inline]
pub fn as_vec<'a>(&'a self) -> &'a Vec2<T> { pub fn as_vec<'a>(&'a self) -> &'a Vec2<T> {
unsafe { cast::transmute(self) } unsafe { cast::transmute(self) }
@ -67,12 +87,6 @@ impl<T:Clone + Float> Point<T, Vec2<T>, T> for Point2<T> {
(*self) + (*offset) (*self) + (*offset)
} }
#[inline]
pub fn rotate(&self, radians: &T) -> Point2<T> {
Point2::new((*self).x.cos() * (*radians),
(*self).y.sin() * (*radians))
}
#[inline] #[inline]
pub fn scale(&self, factor: &Vec2<T>) -> Point2<T> { pub fn scale(&self, factor: &Vec2<T>) -> Point2<T> {
(*self) * (*factor) (*self) * (*factor)
@ -153,7 +167,7 @@ mod test_point2 {
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct Point3<T> { x: T, y: T, z: T } pub struct Point3<T> { x: T, y: T, z: T }
impl<T> Point3<T> { impl<T:Num> Point3<T> {
#[inline] #[inline]
pub fn new(x: T, y: T, z: T) -> Point3<T> { pub fn new(x: T, y: T, z: T) -> Point3<T> {
Point3 { x: x, y: y, z: z } Point3 { x: x, y: y, z: z }
@ -163,9 +177,26 @@ impl<T> Point3<T> {
pub fn from_vec(vec: Vec3<T>) -> Point3<T> { pub fn from_vec(vec: Vec3<T>) -> Point3<T> {
unsafe { cast::transmute(vec) } unsafe { cast::transmute(vec) }
} }
#[inline]
pub fn origin() -> Point3<T> {
Point3::new(zero!(T), zero!(T), zero!(T))
}
} }
impl<T:Clone + Float> Point<T, Vec3<T>, Quat<T>> for Point3<T> { impl<T:Clone + Float> Point3<T> {
#[inline]
pub fn rotate_q(&self, quat: &Quat<T>) -> Point3<T> {
Point3::from_vec(quat.mul_v(self.as_vec()))
}
#[inline]
pub fn rotate_m(&self, mat: &Mat3<T>) -> Point3<T> {
Point3::from_vec(mat.mul_v(self.as_vec()))
}
}
impl<T:Clone + Float> Point<T, Vec3<T>> for Point3<T> {
#[inline] #[inline]
pub fn as_vec<'a>(&'a self) -> &'a Vec3<T> { pub fn as_vec<'a>(&'a self) -> &'a Vec3<T> {
unsafe { cast::transmute(self) } unsafe { cast::transmute(self) }
@ -181,11 +212,6 @@ impl<T:Clone + Float> Point<T, Vec3<T>, Quat<T>> for Point3<T> {
(*self) + (*offset) (*self) + (*offset)
} }
#[inline]
pub fn rotate(&self, rotation: &Quat<T>) -> Point3<T> {
Point3::from_vec(rotation.mul_v(self.as_vec()))
}
#[inline] #[inline]
pub fn scale(&self, factor: &Vec3<T>) -> Point3<T> { pub fn scale(&self, factor: &Vec3<T>) -> Point3<T> {
(*self) * (*factor) (*self) * (*factor)