From aedf317af126cebee4d39ece57730b203c04409c Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 11 Jul 2013 11:45:21 +1000 Subject: [PATCH] Add rotate method to Point trait, remove from_vec function and move impl directly on types --- src/geom/plane.rs | 2 +- src/geom/point.rs | 35 +++++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/geom/plane.rs b/src/geom/plane.rs index bdc2734..87c82b9 100644 --- a/src/geom/plane.rs +++ b/src/geom/plane.rs @@ -136,7 +136,7 @@ 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| { - Point::from_vec( + Point3::from_vec( 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 160281f..e9c68d5 100644 --- a/src/geom/point.rs +++ b/src/geom/point.rs @@ -15,15 +15,15 @@ use std::cast; -use core::{Vec2, Vec3}; +use core::{Vec2, Vec3, Quat}; /// A geometric point -pub trait Point: Eq + ApproxEq + ToStr { - pub fn from_vec(vec: V) -> Self; - pub fn as_vec<'a>(&'a self) -> &'a V; - pub fn as_mut_vec<'a>(&'a mut self) -> &'a mut V; +pub trait Point: Eq + 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: &V) -> Self; + pub fn translate(&self, offset: &Vec) -> Self; + pub fn rotate(&self, rotation: &Rot) -> Self; pub fn distance(&self, other: &Self) -> T; } @@ -36,14 +36,14 @@ impl Point2 { pub fn new(x: T, y: T) -> Point2 { Point2 { x: x, y: y } } -} -impl Point> for Point2 { #[inline] pub fn from_vec(vec: Vec2) -> Point2 { unsafe { cast::transmute(vec) } } +} +impl Point,T> for Point2 { #[inline] pub fn as_vec<'a>(&'a self) -> &'a Vec2 { unsafe { cast::transmute(self) } @@ -56,7 +56,13 @@ impl Point> for Point2 { #[inline] pub fn translate(&self, offset: &Vec2) -> Point2 { - Point::from_vec(self.as_vec().add_v(offset)) + Point2::from_vec(self.as_vec().add_v(offset)) + } + + #[inline] + pub fn rotate(&self, radians: &T) -> Point2 { + Point2::new((*radians) * (*self).x.cos(), + (*radians) * (*self).y.sin()) } #[inline] @@ -108,14 +114,14 @@ impl Point3 { pub fn new(x: T, y: T, z: T) -> Point3 { Point3 { x: x, y: y, z: z } } -} -impl Point> for Point3 { #[inline] pub fn from_vec(vec: Vec3) -> Point3 { unsafe { cast::transmute(vec) } } +} +impl Point,Quat> for Point3 { #[inline] pub fn as_vec<'a>(&'a self) -> &'a Vec3 { unsafe { cast::transmute(self) } @@ -128,7 +134,12 @@ impl Point> for Point3 { #[inline] pub fn translate(&self, offset: &Vec3) -> Point3 { - Point::from_vec(self.as_vec().add_v(offset)) + Point3::from_vec(self.as_vec().add_v(offset)) + } + + #[inline] + pub fn rotate(&self, rotation: &Quat) -> Point3 { + Point3::from_vec(rotation.mul_v(self.as_vec())) } #[inline]