From 50747703cfd9433f2b96dd69542447e0440a71b9 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 11 Jul 2013 14:49:14 +1000 Subject: [PATCH] Method rejiggering. Add origin method. --- src/geom/plane.rs | 12 ++++---- src/geom/point.rs | 72 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 30 deletions(-) diff --git a/src/geom/plane.rs b/src/geom/plane.rs index 87c82b9..c32d341 100644 --- a/src/geom/plane.rs +++ b/src/geom/plane.rs @@ -85,8 +85,8 @@ impl Plane3 { b: Point3, c: Point3) -> Option> { // 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 Plane3 { 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(), - other_a.dist.clone(), - other_b.dist.clone())) - ) + Point3::origin() + m.mul_v(&Vec3::new(self.dist.clone(), + other_a.dist.clone(), + other_b.dist.clone())) } } } diff --git a/src/geom/point.rs b/src/geom/point.rs index 9d3edfd..41d595d 100644 --- a/src/geom/point.rs +++ b/src/geom/point.rs @@ -15,20 +15,22 @@ 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: Eq - + Add - + Sub - + Mul - + ApproxEq - + ToStr { +pub trait Point: Eq + + Add + + Sub + + Mul + + ApproxEq + + ToStr { pub fn as_vec<'a>(&'a self) -> &'a Vec; 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: Eq #[deriving(Clone, Eq)] pub struct Point2 { x: T, y: T } -impl Point2 { +impl Point2 { #[inline] pub fn new(x: T, y: T) -> Point2 { Point2 { x: x, y: y } @@ -49,9 +51,27 @@ impl Point2 { pub fn from_vec(vec: Vec2) -> Point2 { unsafe { cast::transmute(vec) } } + + #[inline] + pub fn origin() -> Point2 { + Point2::new(zero!(T), zero!(T)) + } } -impl Point, T> for Point2 { +impl Point2 { + #[inline] + pub fn rotate_t(&self, radians: &T) -> Point2 { + Point2::new((*self).x.cos() * (*radians), + (*self).y.sin() * (*radians)) + } + + #[inline] + pub fn rotate_m(&self, mat: &Mat2) -> Point2 { + Point2::from_vec(mat.mul_v(self.as_vec())) + } +} + +impl Point> for Point2 { #[inline] pub fn as_vec<'a>(&'a self) -> &'a Vec2 { unsafe { cast::transmute(self) } @@ -67,12 +87,6 @@ impl Point, T> for Point2 { (*self) + (*offset) } - #[inline] - pub fn rotate(&self, radians: &T) -> Point2 { - Point2::new((*self).x.cos() * (*radians), - (*self).y.sin() * (*radians)) - } - #[inline] pub fn scale(&self, factor: &Vec2) -> Point2 { (*self) * (*factor) @@ -153,7 +167,7 @@ mod test_point2 { #[deriving(Clone, Eq)] pub struct Point3 { x: T, y: T, z: T } -impl Point3 { +impl Point3 { #[inline] pub fn new(x: T, y: T, z: T) -> Point3 { Point3 { x: x, y: y, z: z } @@ -163,9 +177,26 @@ impl Point3 { pub fn from_vec(vec: Vec3) -> Point3 { unsafe { cast::transmute(vec) } } + + #[inline] + pub fn origin() -> Point3 { + Point3::new(zero!(T), zero!(T), zero!(T)) + } } -impl Point, Quat> for Point3 { +impl Point3 { + #[inline] + pub fn rotate_q(&self, quat: &Quat) -> Point3 { + Point3::from_vec(quat.mul_v(self.as_vec())) + } + + #[inline] + pub fn rotate_m(&self, mat: &Mat3) -> Point3 { + Point3::from_vec(mat.mul_v(self.as_vec())) + } +} + +impl Point> for Point3 { #[inline] pub fn as_vec<'a>(&'a self) -> &'a Vec3 { unsafe { cast::transmute(self) } @@ -181,11 +212,6 @@ impl Point, Quat> for Point3 { (*self) + (*offset) } - #[inline] - pub fn rotate(&self, rotation: &Quat) -> Point3 { - Point3::from_vec(rotation.mul_v(self.as_vec())) - } - #[inline] pub fn scale(&self, factor: &Vec3) -> Point3 { (*self) * (*factor)