Merge pull request #4 from kvark/master

Transform is enabled, improvement, and implemented for a struct
This commit is contained in:
Brendan Zabarauskas 2013-11-02 05:19:55 -07:00
commit d02d063cb4
2 changed files with 32 additions and 8 deletions

View file

@ -35,6 +35,7 @@ pub mod plane;
pub mod point; pub mod point;
pub mod ray; pub mod ray;
pub mod rotation; pub mod rotation;
pub mod transform;
pub mod projection; pub mod projection;

View file

@ -13,10 +13,21 @@
// 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.
pub trait Transform<S> { use matrix::Mat4;
fn transform_vec(&self, point: Point3<S>) -> Point3<S>; use point::{Point,Point3};
fn transform_point(&self, point: Point3<S>) -> Point3<S>; use ray::Ray3;
fn transform_ray(&self, ray: Ray3<S>) -> Ray3<S>; use rotation::Rotation3;
use vector::{Vector,Vec3};
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) )
}
} }
/// A homogeneous transformation matrix. /// A homogeneous transformation matrix.
@ -26,15 +37,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 Transform3<S, R> { pub struct Transform3D<S, R> {
rot: R, rot: R,
disp: Vec3<S>, disp: Vec3<S>,
scale: S, scale: S,
} }
impl<S: Float, R: Rotation3<S>> Transform3<S, R> { impl<S: Float, R: Rotation3<S>> Transform3D<S, R> {
#[inline] #[inline]
pub fn new(rot: R, disp: Vec3<S>, scale: S) -> Transform3<S, R> { pub fn new(rot: R, disp: Vec3<S>, scale: S) -> Transform3D<S, R> {
Transform3 { rot: rot, disp: disp, scale: S } Transform3D { rot: rot, disp: disp, scale: scale }
}
}
impl <S: Float, R: Rotation3<S>> Transform3<S> for Transform3D<S,R> {
#[inline]
fn transform_vec3(&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> {
self.rot.rotate_point3( &point.mul_s( self.scale.clone() )).add_v( &self.disp )
} }
} }