Generalized Ray & Transform over dimensions

This commit is contained in:
kvark 2013-11-01 07:09:57 -04:00
parent 53ad086e5e
commit d81157fba5
2 changed files with 39 additions and 37 deletions

View file

@ -13,33 +13,28 @@
// 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 point::{Point2, Point3}; use point::{Point,Point2, Point3};
use vector::{Vec2, Vec3}; use vector::{Vector,Vec2, Vec3};
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct Ray2<S> { pub struct Ray<P,V>
origin: Point2<S>, {
direction: Vec2<S>, origin: P,
direction: V,
} }
impl<S> Ray2<S> { impl
/// Creates a new ray from a position coordinate and a direction vector <
#[inline] S: Primitive,
pub fn new(origin: Point2<S>, direction: Vec2<S>) -> Ray2<S> { Slice,
Ray2 { origin: origin, direction: direction } V: Vector<S,Slice>,
P: Point<S,V,Slice>
> Ray<P,V>
{
pub fn new(origin: P, direction: V) -> Ray<P,V> {
Ray { origin:origin, direction:direction }
} }
} }
#[deriving(Clone, Eq)] pub type Ray2<S> = Ray<Point2<S>,Vec2<S>>;
pub struct Ray3<S> { pub type Ray3<S> = Ray<Point3<S>,Vec3<S>>;
origin: Point3<S>,
direction: Vec3<S>,
}
impl<S> Ray3<S> {
/// Creates a new ray from a position coordinate and a direction vector
#[inline]
pub fn new(origin: Point3<S>, direction: Vec3<S>) -> Ray3<S> {
Ray3 { origin: origin, direction: direction }
}
}

View file

@ -15,18 +15,25 @@
use matrix::Mat4; use matrix::Mat4;
use point::{Point,Point3}; use point::{Point,Point3};
use ray::Ray3; use ray::Ray;
use rotation::Rotation3; use rotation::Rotation3;
use vector::{Vector,Vec3}; 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<S,Slice>,
P: Point<S,V,Slice>
>
{
fn transform_vec(&self, vec: &V) -> V;
fn transform_point(&self, point: &P) -> P;
pub trait Transform3<S> {
fn transform_vec3(&self, vec: &Vec3<S>) -> Vec3<S>;
fn transform_point3(&self, point: &Point3<S>) -> Point3<S>;
#[inline] #[inline]
fn transform_ray3(&self, ray: &Ray3<S>) -> Ray3<S> { fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
Ray3::new( self.transform_point3(&ray.origin), self.transform_vec3(&ray.direction) ) Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) )
} }
} }
@ -37,27 +44,27 @@ pub struct AffineMatrix3<S> {
/// A transformation in three dimensions consisting of a rotation, /// A transformation in three dimensions consisting of a rotation,
/// displacement vector and scale amount. /// displacement vector and scale amount.
pub struct Transform3D<S, R> { pub struct Transform3<S, R> {
rot: R, rot: R,
disp: Vec3<S>, disp: Vec3<S>,
scale: S, scale: S,
} }
impl<S: Float, R: Rotation3<S>> Transform3D<S, R> { impl<S: Float, R: Rotation3<S>> Transform3<S, R> {
#[inline] #[inline]
pub fn new(rot: R, disp: Vec3<S>, scale: S) -> Transform3D<S, R> { pub fn new(rot: R, disp: Vec3<S>, scale: S) -> Transform3<S, R> {
Transform3D { rot: rot, disp: disp, scale: scale } Transform3 { rot: rot, disp: disp, scale: scale }
} }
} }
impl <S: Float, R: Rotation3<S>> Transform3<S> for Transform3D<S,R> { impl <S: Float, R: Rotation3<S>> Transform<S, [S, .. 3], Vec3<S>, Point3<S>> for Transform3<S,R> {
#[inline] #[inline]
fn transform_vec3(&self, vec: &Vec3<S>) -> Vec3<S> { fn transform_vec(&self, vec: &Vec3<S>) -> Vec3<S> {
self.rot.rotate_vec3( &vec.mul_s( self.scale.clone() )) self.rot.rotate_vec3( &vec.mul_s( self.scale.clone() ))
} }
#[inline] #[inline]
fn transform_point3(&self, point: &Point3<S>) -> Point3<S> { fn transform_point(&self, point: &Point3<S>) -> Point3<S> {
self.rot.rotate_point3( &point.mul_s( self.scale.clone() )).add_v( &self.disp ) self.rot.rotate_point3( &point.mul_s( self.scale.clone() )).add_v( &self.disp )
} }
} }