Add ray_to method to point types

This commit is contained in:
Brendan Zabarauskas 2013-07-12 14:07:21 +10:00
parent 3d51d83c92
commit 2ed5e94710
3 changed files with 41 additions and 11 deletions

View file

@ -17,7 +17,7 @@ pub use self::aabb::{AABB2, AABB3};
pub use self::frustum::{Frustum, FrustumPoints}; pub use self::frustum::{Frustum, FrustumPoints};
pub use self::plane::Plane3; pub use self::plane::Plane3;
pub use self::point::{Point, Point2, Point3}; pub use self::point::{Point, Point2, Point3};
pub use self::ray::Ray3; pub use self::ray::{Ray2, Ray3};
pub use self::sphere::Sphere; pub use self::sphere::Sphere;
pub mod aabb; pub mod aabb;

View file

@ -26,9 +26,10 @@ use core::{Mat2, Mat3, Quat};
use core::{Vec2, ToVec2, AsVec2}; use core::{Vec2, ToVec2, AsVec2};
use core::{Vec3, ToVec3, AsVec3}; use core::{Vec3, ToVec3, AsVec3};
use core::{Vec4, ToVec4}; use core::{Vec4, ToVec4};
use geom::{Ray2, Ray3};
/// A geometric point /// A geometric point
pub trait Point<T, Vec>: Eq pub trait Point<T, Vec, Ray>: Eq
+ Add<Vec, Self> + Add<Vec, Self>
+ Sub<Self, Vec> + Sub<Self, Vec>
+ Mul<Vec, Self> + Mul<Vec, Self>
@ -39,6 +40,7 @@ pub trait Point<T, Vec>: Eq
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;
pub fn direction(&self, other: &Self) -> Vec; pub fn direction(&self, other: &Self) -> Vec;
pub fn ray_to(&self, other: &Self) -> Ray;
} }
/// A two-dimensional point /// A two-dimensional point
@ -107,7 +109,7 @@ impl<T:Clone + Float> Point2<T> {
} }
} }
impl<T:Clone + Float> Point<T, Vec2<T>> for Point2<T> { impl<T:Clone + Float> Point<T, Vec2<T>, Ray2<T>> for Point2<T> {
#[inline] #[inline]
pub fn translate(&self, offset: &Vec2<T>) -> Point2<T> { pub fn translate(&self, offset: &Vec2<T>) -> Point2<T> {
(*self) + (*offset) (*self) + (*offset)
@ -134,6 +136,12 @@ impl<T:Clone + Float> Point<T, Vec2<T>> for Point2<T> {
pub fn direction(&self, other: &Point2<T>) -> Vec2<T> { pub fn direction(&self, other: &Point2<T>) -> Vec2<T> {
((*other) - (*self)).normalize() ((*other) - (*self)).normalize()
} }
/// Projects a normalized ray towards the other point
#[inline]
pub fn ray_to(&self, other: &Point2<T>) -> Ray2<T> {
Ray2::new(self.clone(), self.direction(other))
}
} }
impl<T:Num> Add<Vec2<T>, Point2<T>> for Point2<T> { impl<T:Num> Add<Vec2<T>, Point2<T>> for Point2<T> {
@ -241,7 +249,7 @@ impl<T:Clone + Float> Point3<T> {
} }
} }
impl<T:Clone + Float> Point<T, Vec3<T>> for Point3<T> { impl<T:Clone + Float> Point<T, Vec3<T>, Ray3<T>> for Point3<T> {
#[inline] #[inline]
pub fn translate(&self, offset: &Vec3<T>) -> Point3<T> { pub fn translate(&self, offset: &Vec3<T>) -> Point3<T> {
(*self) + (*offset) (*self) + (*offset)
@ -268,6 +276,12 @@ impl<T:Clone + Float> Point<T, Vec3<T>> for Point3<T> {
pub fn direction(&self, other: &Point3<T>) -> Vec3<T> { pub fn direction(&self, other: &Point3<T>) -> Vec3<T> {
((*other) - (*self)).normalize() ((*other) - (*self)).normalize()
} }
/// Projects a normalized ray towards the other point
#[inline]
pub fn ray_to(&self, other: &Point3<T>) -> Ray3<T> {
Ray3::new(self.clone(), self.direction(other))
}
} }
impl<T:Num> Add<Vec3<T>, Point3<T>> for Point3<T> { impl<T:Num> Add<Vec3<T>, Point3<T>> for Point3<T> {

View file

@ -13,8 +13,24 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use core::Vec3; use core::{Vec2, Vec3};
use geom::Point3; use geom::{Point2, Point3};
#[deriving(Clone, Eq)]
pub struct Ray2<T> {
origin: Point2<T>,
direction: Vec2<T>,
}
impl_approx!(Ray2 { origin, direction })
impl<T> Ray2<T> {
#[inline]
pub fn new(origin: Point2<T>, direction: Vec2<T>) -> Ray2<T> {
Ray2 { origin: origin, direction: direction }
}
}
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct Ray3<T> { pub struct Ray3<T> {