From 45c6fd630d71a8da8036199201bb7d3e924e298e Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sun, 1 Mar 2015 01:29:47 -0500 Subject: [PATCH] Cleaned up public exports. Implemented ToComponents for Matrix3. Added tests. --- src/cgmath.rs | 43 ++++++++++++++----------------------------- src/transform.rs | 38 +++++++++++++++++++++++++++++++++++--- tests/transform.rs | 12 ++++++++++++ 3 files changed, 61 insertions(+), 32 deletions(-) diff --git a/src/cgmath.rs b/src/cgmath.rs index 300e4bf..b1dfea7 100644 --- a/src/cgmath.rs +++ b/src/cgmath.rs @@ -37,45 +37,30 @@ extern crate rand; // Re-exports -pub use array::{Array1, Array2, FixedArray}; +pub use array::*; +pub use matrix::*; +pub use quaternion::*; +pub use vector::*; -pub use matrix::Matrix; -pub use matrix::{Matrix2, Matrix3, Matrix4}; -pub use matrix::{ToMatrix2, ToMatrix3, ToMatrix4}; -pub use quaternion::{Quaternion, ToQuaternion}; -pub use vector::{Vector, EuclideanVector}; -pub use vector::{Vector2, Vector3, Vector4}; -pub use vector::{dot, vec2, vec3, vec4}; - -pub use angle::{rad, deg}; -pub use angle::{Angle, Rad, Deg}; -pub use angle::{ToRad, ToDeg}; -pub use angle::bisect; -pub use angle::{sin, cos, tan, sin_cos}; -pub use angle::{cot, sec, csc}; -pub use angle::{acos, asin, atan, atan2}; +pub use angle::*; pub use plane::Plane; -pub use point::{Point, Point2, Point3}; -pub use line::{Line, Line2, Line3}; -pub use ray::{Ray, Ray2, Ray3}; -pub use rotation::{Rotation, Rotation2, Rotation3}; -pub use rotation::{Basis3, Basis2}; -pub use rotation::{ToBasis2, ToBasis3}; -pub use transform::{Transform, Transform3}; -pub use transform::{Decomposed, AffineMatrix3}; +pub use point::*; +pub use line::*; +pub use ray::*; +pub use rotation::*; +pub use transform::*; -pub use projection::{perspective, frustum, ortho}; -pub use projection::{Projection, PerspectiveFov, Perspective, Ortho}; +pub use projection::*; -pub use aabb::{Aabb, Aabb2, Aabb3}; +pub use aabb::*; pub use cylinder::Cylinder; pub use frustum::{Frustum, FrustumPoints}; pub use intersect::Intersect; -pub use obb::{Obb2, Obb3}; +pub use obb::*; pub use sphere::Sphere; pub use approx::ApproxEq; -pub use num::{PartialOrd, BaseNum, BaseInt, BaseFloat, One, one, Zero, zero}; +pub use num::*; // Modules diff --git a/src/transform.rs b/src/transform.rs index 6d2fdac..de3d33a 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -16,9 +16,10 @@ use std::fmt; use approx::ApproxEq; -use matrix::{Matrix, ToMatrix3, Matrix4, ToMatrix4}; +use matrix::*; use num::{BaseNum, BaseFloat, zero, one}; use point::{Point, Point3}; +use quaternion::*; use ray::Ray; use rotation::{Rotation, Rotation3}; use std::marker::PhantomFn; @@ -230,6 +231,9 @@ pub trait ToComponents, P: Point> { fn to_scale(&self) -> V; } +pub trait ToComponents3: ToComponents, Point3> + where Self::Rotation: ToMatrix3 {} + impl< S: BaseFloat, V: Vector + Clone, @@ -251,5 +255,33 @@ impl< } } -pub trait ToComponents3: ToComponents, Point3> - where Self::Rotation: ToMatrix3 {} \ No newline at end of file +impl< + S: BaseFloat, + R: Rotation, Point3> + Clone + ToMatrix3, +> ToComponents3 for Decomposed, R> {} + +impl< + S: BaseFloat + 'static, +> ToComponents, Point3> for AffineMatrix3 { + type Rotation = Quaternion; + + fn to_translation(&self) -> Vector3 { + Vector3::new(self.mat.w.x, self.mat.w.y, self.mat.w.z) + } + + fn to_rotation(&self) -> Quaternion { + Matrix3::new( + self.mat.x.x, self.mat.x.y, self.mat.x.z, + self.mat.y.x, self.mat.y.y, self.mat.y.z, + self.mat.z.x, self.mat.z.y, self.mat.z.z, + ).to_quaternion() + } + + fn to_scale(&self) -> Vector3 { + Vector3::new(self.mat.x.x, self.mat.y.y, self.mat.z.z) + } +} + +impl< + S: BaseFloat + 'static, +> ToComponents3 for AffineMatrix3 {} diff --git a/tests/transform.rs b/tests/transform.rs index 25d68f9..3ec7b1a 100644 --- a/tests/transform.rs +++ b/tests/transform.rs @@ -41,3 +41,15 @@ fn test_look_at() { let view_point = Point3::new(0.0f64, 1.0f64, 5.0f64); assert!( t.transform_point(&point).approx_eq(&view_point) ); } + +#[test] +fn test_components() { + let t = Decomposed { + scale: 1.5f64, + rot: Quaternion::new(0.5f64,0.5,0.5,0.5), + disp: Vector3::new(6.0f64,-7.0f64,8.0) + }; + assert_eq!(t.to_translation(), t.disp); + assert_eq!(t.to_rotation(), t.rot); + assert_eq!(t.to_scale(), Vector::from_value(t.scale)); +}