diff --git a/src/cgmath/ray.rs b/src/cgmath/ray.rs index ec6c06a..3b1096c 100644 --- a/src/cgmath/ray.rs +++ b/src/cgmath/ray.rs @@ -13,33 +13,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -use point::{Point2, Point3}; -use vector::{Vec2, Vec3}; +use point::{Point,Point2, Point3}; +use vector::{Vector,Vec2, Vec3}; #[deriving(Clone, Eq)] -pub struct Ray2 { - origin: Point2, - direction: Vec2, +pub struct Ray +{ + origin: P, + direction: V, } -impl Ray2 { - /// Creates a new ray from a position coordinate and a direction vector - #[inline] - pub fn new(origin: Point2, direction: Vec2) -> Ray2 { - Ray2 { origin: origin, direction: direction } +impl +< + S: Primitive, + Slice, + V: Vector, + P: Point +> Ray +{ + pub fn new(origin: P, direction: V) -> Ray { + Ray { origin:origin, direction:direction } } } -#[deriving(Clone, Eq)] -pub struct Ray3 { - origin: Point3, - direction: Vec3, -} - -impl Ray3 { - /// Creates a new ray from a position coordinate and a direction vector - #[inline] - pub fn new(origin: Point3, direction: Vec3) -> Ray3 { - Ray3 { origin: origin, direction: direction } - } -} +pub type Ray2 = Ray,Vec2>; +pub type Ray3 = Ray,Vec3>; diff --git a/src/cgmath/transform.rs b/src/cgmath/transform.rs index bb60df7..2eefb44 100644 --- a/src/cgmath/transform.rs +++ b/src/cgmath/transform.rs @@ -15,18 +15,25 @@ use matrix::Mat4; use point::{Point,Point3}; -use ray::Ray3; +use ray::Ray; use rotation::Rotation3; use vector::{Vector,Vec3}; +/// A trait of affine transformation, that can be applied to points or vectors +pub trait Transform +< + S: Primitive, + Slice, + V: Vector, + P: Point +> +{ + fn transform_vec(&self, vec: &V) -> V; + fn transform_point(&self, point: &P) -> P; -pub trait Transform3 { - fn transform_vec3(&self, vec: &Vec3) -> Vec3; - fn transform_point3(&self, point: &Point3) -> Point3; - #[inline] - fn transform_ray3(&self, ray: &Ray3) -> Ray3 { - Ray3::new( self.transform_point3(&ray.origin), self.transform_vec3(&ray.direction) ) + fn transform_ray(&self, ray: &Ray) -> Ray { + Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) ) } } @@ -37,27 +44,27 @@ pub struct AffineMatrix3 { /// A transformation in three dimensions consisting of a rotation, /// displacement vector and scale amount. -pub struct Transform3D { +pub struct Transform3 { rot: R, disp: Vec3, scale: S, } -impl> Transform3D { +impl> Transform3 { #[inline] - pub fn new(rot: R, disp: Vec3, scale: S) -> Transform3D { - Transform3D { rot: rot, disp: disp, scale: scale } + pub fn new(rot: R, disp: Vec3, scale: S) -> Transform3 { + Transform3 { rot: rot, disp: disp, scale: scale } } } -impl > Transform3 for Transform3D { +impl > Transform, Point3> for Transform3 { #[inline] - fn transform_vec3(&self, vec: &Vec3) -> Vec3 { + fn transform_vec(&self, vec: &Vec3) -> Vec3 { self.rot.rotate_vec3( &vec.mul_s( self.scale.clone() )) } #[inline] - fn transform_point3(&self, point: &Point3) -> Point3 { + fn transform_point(&self, point: &Point3) -> Point3 { self.rot.rotate_point3( &point.mul_s( self.scale.clone() )).add_v( &self.disp ) } }