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

View file

@ -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<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]
fn transform_ray3(&self, ray: &Ray3<S>) -> Ray3<S> {
Ray3::new( self.transform_point3(&ray.origin), self.transform_vec3(&ray.direction) )
fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
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,
/// displacement vector and scale amount.
pub struct Transform3D<S, R> {
pub struct Transform3<S, R> {
rot: R,
disp: Vec3<S>,
scale: S,
}
impl<S: Float, R: Rotation3<S>> Transform3D<S, R> {
impl<S: Float, R: Rotation3<S>> Transform3<S, R> {
#[inline]
pub fn new(rot: R, disp: Vec3<S>, scale: S) -> Transform3D<S, R> {
Transform3D { rot: rot, disp: disp, scale: scale }
pub fn new(rot: R, disp: Vec3<S>, scale: S) -> Transform3<S, R> {
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]
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() ))
}
#[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 )
}
}