Add conversions from points to homogenous coordinates

This commit is contained in:
Brendan Zabarauskas 2013-07-11 15:14:32 +10:00
parent de649c3b50
commit 290861f952

View file

@ -22,7 +22,7 @@
use std::cast; use std::cast;
use core::{Mat2, Mat3, Quat, Vec2, Vec3}; use core::{Mat2, Mat3, Quat, Vec2, Vec3, Vec4};
#[path = "../num_macros.rs"] #[path = "../num_macros.rs"]
mod num_macros; mod num_macros;
@ -65,6 +65,17 @@ impl<T:Num> Point2<T> {
} }
} }
impl<T:Clone + Num> Point2<T> {
/// Converts the point to a three-dimensional homogeneous vector:
/// `[x, y] -> [x, y, 1]`
#[inline]
pub fn to_vec3(&self) -> Vec3<T> {
Vec3::new((*self).x.clone(),
(*self).y.clone(),
one!(T))
}
}
impl<T:Clone + Float> Point2<T> { impl<T:Clone + Float> Point2<T> {
#[inline] #[inline]
pub fn rotate_t(&self, radians: &T) -> Point2<T> { pub fn rotate_t(&self, radians: &T) -> Point2<T> {
@ -191,6 +202,18 @@ impl<T:Num> Point3<T> {
} }
} }
impl<T:Clone + Num> Point3<T> {
/// Converts the point to a four-dimensional homogeneous vector:
/// `[x, y, z] -> [x, y, z, 1]`
#[inline]
pub fn to_vec4(&self) -> Vec4<T> {
Vec4::new((*self).x.clone(),
(*self).y.clone(),
(*self).z.clone(),
one!(T))
}
}
impl<T:Clone + Float> Point3<T> { impl<T:Clone + Float> Point3<T> {
#[inline] #[inline]
pub fn rotate_q(&self, quat: &Quat<T>) -> Point3<T> { pub fn rotate_q(&self, quat: &Quat<T>) -> Point3<T> {