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>,
c: Point3<T>) -> Option<Plane3<T>> {
// create two vectors that run parallel to the plane
let v0 = b.as_vec().sub_v(a.as_vec());
let v1 = c.as_vec().sub_v(a.as_vec());
let v0 = (b - a);
let v1 = (c - a);
// find the vector that is perpendicular to v1 and v2
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.z.clone(), other_a.norm.z.clone(), other_b.norm.z.clone());
do mx.inverse().map |m| {
Point3::from_vec(
m.mul_v(&Vec3::new(self.dist.clone(),
Point3::origin() + m.mul_v(&Vec3::new(self.dist.clone(),
other_a.dist.clone(),
other_b.dist.clone()))
)
}
}
}

View file

@ -15,10 +15,13 @@
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
pub trait Point<T, Vec, Rot>: Eq
pub trait Point<T, Vec>: Eq
+ Add<Vec, Self>
+ Sub<Self, Vec>
+ Mul<Vec, Self>
@ -28,7 +31,6 @@ pub trait Point<T, Vec, Rot>: Eq
pub fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec;
pub fn translate(&self, offset: &Vec) -> Self;
pub fn rotate(&self, rotation: &Rot) -> Self;
pub fn scale(&self, factor: &Vec) -> Self;
pub fn distance2(&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)]
pub struct Point2<T> { x: T, y: T }
impl<T> Point2<T> {
impl<T:Num> Point2<T> {
#[inline]
pub fn new(x: T, y: T) -> Point2<T> {
Point2 { x: x, y: y }
@ -49,9 +51,27 @@ impl<T> Point2<T> {
pub fn from_vec(vec: Vec2<T>) -> Point2<T> {
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]
pub fn as_vec<'a>(&'a self) -> &'a Vec2<T> {
unsafe { cast::transmute(self) }
@ -67,12 +87,6 @@ impl<T:Clone + Float> Point<T, Vec2<T>, T> for Point2<T> {
(*self) + (*offset)
}
#[inline]
pub fn rotate(&self, radians: &T) -> Point2<T> {
Point2::new((*self).x.cos() * (*radians),
(*self).y.sin() * (*radians))
}
#[inline]
pub fn scale(&self, factor: &Vec2<T>) -> Point2<T> {
(*self) * (*factor)
@ -153,7 +167,7 @@ mod test_point2 {
#[deriving(Clone, Eq)]
pub struct Point3<T> { x: T, y: T, z: T }
impl<T> Point3<T> {
impl<T:Num> Point3<T> {
#[inline]
pub fn new(x: T, y: T, z: T) -> Point3<T> {
Point3 { x: x, y: y, z: z }
@ -163,9 +177,26 @@ impl<T> Point3<T> {
pub fn from_vec(vec: Vec3<T>) -> Point3<T> {
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]
pub fn as_vec<'a>(&'a self) -> &'a Vec3<T> {
unsafe { cast::transmute(self) }
@ -181,11 +212,6 @@ impl<T:Clone + Float> Point<T, Vec3<T>, Quat<T>> for Point3<T> {
(*self) + (*offset)
}
#[inline]
pub fn rotate(&self, rotation: &Quat<T>) -> Point3<T> {
Point3::from_vec(rotation.mul_v(self.as_vec()))
}
#[inline]
pub fn scale(&self, factor: &Vec3<T>) -> Point3<T> {
(*self) * (*factor)