From 053586c8e229b675451f0c9218b0425587fa4d7d Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 10 Jul 2013 14:18:44 +1000 Subject: [PATCH] Add point conversion methods for vec2 and vec3 types --- src/core/vec.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/core/vec.rs b/src/core/vec.rs index 575bd34..9f15a71 100644 --- a/src/core/vec.rs +++ b/src/core/vec.rs @@ -13,7 +13,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[cfg(geom)] +use std::cast; + use core::Dimensional; +#[cfg(geom)] +use geom::{Point2, Point3}; #[path = "../num_macros.rs"] mod num_macros; @@ -56,6 +61,24 @@ impl Vec2 { } } +#[cfg(geom)] +impl Vec2 { + #[inline] + pub fn from_point(point: Point2) -> Vec2 { + unsafe { cast::transmute(point) } + } + + #[inline] + pub fn as_point<'a>(&'a self) -> &'a Point2 { + unsafe { cast::transmute(self) } + } + + #[inline] + pub fn as_mut_point<'a>(&'a mut self) -> &'a mut Point2 { + unsafe { cast::transmute(self) } + } +} + impl Vec2 { #[inline] pub fn from_value(value: T) -> Vec2 { @@ -561,6 +584,24 @@ impl Vec3 { } } +#[cfg(geom)] +impl Vec3 { + #[inline] + pub fn from_point(point: Point3) -> Vec3 { + unsafe { cast::transmute(point) } + } + + #[inline] + pub fn as_point<'a>(&'a self) -> &'a Point3 { + unsafe { cast::transmute(self) } + } + + #[inline] + pub fn as_mut_point<'a>(&'a mut self) -> &'a mut Point3 { + unsafe { cast::transmute(self) } + } +} + impl Vec3 { #[inline] pub fn from_value(value: T) -> Vec3 {