From 2ed5e94710407537deec56dd44d4628e17078a2a Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 12 Jul 2013 14:07:21 +1000 Subject: [PATCH] Add ray_to method to point types --- src/geom/geom.rs | 2 +- src/geom/point.rs | 30 ++++++++++++++++++++++-------- src/geom/ray.rs | 20 ++++++++++++++++++-- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/geom/geom.rs b/src/geom/geom.rs index 9d0f5fc..fbd27c9 100644 --- a/src/geom/geom.rs +++ b/src/geom/geom.rs @@ -17,7 +17,7 @@ pub use self::aabb::{AABB2, AABB3}; pub use self::frustum::{Frustum, FrustumPoints}; pub use self::plane::Plane3; pub use self::point::{Point, Point2, Point3}; -pub use self::ray::Ray3; +pub use self::ray::{Ray2, Ray3}; pub use self::sphere::Sphere; pub mod aabb; diff --git a/src/geom/point.rs b/src/geom/point.rs index abca3ad..b599f4b 100644 --- a/src/geom/point.rs +++ b/src/geom/point.rs @@ -26,19 +26,21 @@ use core::{Mat2, Mat3, Quat}; use core::{Vec2, ToVec2, AsVec2}; use core::{Vec3, ToVec3, AsVec3}; use core::{Vec4, ToVec4}; +use geom::{Ray2, Ray3}; /// A geometric point -pub trait Point: Eq - + Add - + Sub - + Mul - + ApproxEq - + ToStr { +pub trait Point: Eq + + Add + + Sub + + Mul + + ApproxEq + + ToStr { pub fn translate(&self, offset: &Vec) -> Self; pub fn scale(&self, factor: &Vec) -> Self; pub fn distance2(&self, other: &Self) -> T; pub fn distance(&self, other: &Self) -> T; pub fn direction(&self, other: &Self) -> Vec; + pub fn ray_to(&self, other: &Self) -> Ray; } /// A two-dimensional point @@ -107,7 +109,7 @@ impl Point2 { } } -impl Point> for Point2 { +impl Point, Ray2> for Point2 { #[inline] pub fn translate(&self, offset: &Vec2) -> Point2 { (*self) + (*offset) @@ -134,6 +136,12 @@ impl Point> for Point2 { pub fn direction(&self, other: &Point2) -> Vec2 { ((*other) - (*self)).normalize() } + + /// Projects a normalized ray towards the other point + #[inline] + pub fn ray_to(&self, other: &Point2) -> Ray2 { + Ray2::new(self.clone(), self.direction(other)) + } } impl Add, Point2> for Point2 { @@ -241,7 +249,7 @@ impl Point3 { } } -impl Point> for Point3 { +impl Point, Ray3> for Point3 { #[inline] pub fn translate(&self, offset: &Vec3) -> Point3 { (*self) + (*offset) @@ -268,6 +276,12 @@ impl Point> for Point3 { pub fn direction(&self, other: &Point3) -> Vec3 { ((*other) - (*self)).normalize() } + + /// Projects a normalized ray towards the other point + #[inline] + pub fn ray_to(&self, other: &Point3) -> Ray3 { + Ray3::new(self.clone(), self.direction(other)) + } } impl Add, Point3> for Point3 { diff --git a/src/geom/ray.rs b/src/geom/ray.rs index 128f98c..cf5ad12 100644 --- a/src/geom/ray.rs +++ b/src/geom/ray.rs @@ -13,8 +13,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -use core::Vec3; -use geom::Point3; +use core::{Vec2, Vec3}; +use geom::{Point2, Point3}; + +#[deriving(Clone, Eq)] +pub struct Ray2 { + origin: Point2, + direction: Vec2, +} + +impl_approx!(Ray2 { origin, direction }) + +impl Ray2 { + #[inline] + pub fn new(origin: Point2, direction: Vec2) -> Ray2 { + Ray2 { origin: origin, direction: direction } + } +} + #[deriving(Clone, Eq)] pub struct Ray3 {