diff --git a/Makefile b/Makefile index aaa6bb3..2dee383 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,8 @@ SRC_CRATE = $(TARGET).rs EXTERN_DIR = $(ROOT_DIR)/extern BUILD_DIR = $(ROOT_DIR)/lib +CFG = --cfg=geom --cfg=world + TEST = $(TARGET) TEST_BUILD_DIR = $(ROOT_DIR)/test @@ -30,7 +32,7 @@ TEST_BUILD_DIR = $(ROOT_DIR)/test $(TARGET): @echo "Building $(TARGET)..." @mkdir -p $(BUILD_DIR) - @rustc $(SRC_DIR)/$(SRC_CRATE) --out-dir=$(BUILD_DIR) + @rustc $(CFG) $(SRC_DIR)/$(SRC_CRATE) --out-dir=$(BUILD_DIR) @echo "Success" all: $(TARGET) @@ -38,7 +40,7 @@ all: $(TARGET) test: @echo "Building unit tests for $(TARGET)..." @mkdir -p $(TEST_BUILD_DIR) - @rustc $(SRC_DIR)/$(SRC_CRATE) --test --out-dir=$(TEST_BUILD_DIR) + @rustc $(CFG) $(SRC_DIR)/$(SRC_CRATE) --test --out-dir=$(TEST_BUILD_DIR) @echo "Success" @$(TEST_BUILD_DIR)/$(TARGET) diff --git a/src/core/core.rs b/src/core/core.rs new file mode 100644 index 0000000..b5d8d1e --- /dev/null +++ b/src/core/core.rs @@ -0,0 +1,24 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub use self::dim::Dimensional; +pub use self::mat::{Mat2, ToMat2, Mat3, ToMat3, Mat4, ToMat4}; +pub use self::quat::{Quat, ToQuat}; +pub use self::vec::{Vec2, Vec3, Vec4}; + +pub mod dim; +pub mod mat; +pub mod quat; +pub mod vec; diff --git a/src/dim.rs b/src/core/dim.rs similarity index 100% rename from src/dim.rs rename to src/core/dim.rs diff --git a/src/dim_macros.rs b/src/core/dim_macros.rs similarity index 100% rename from src/dim_macros.rs rename to src/core/dim_macros.rs diff --git a/src/mat.rs b/src/core/mat.rs similarity index 99% rename from src/mat.rs rename to src/core/mat.rs index 3de6718..1210574 100644 --- a/src/mat.rs +++ b/src/core/mat.rs @@ -13,11 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use dim::Dimensional; - -use quat::{Quat, ToQuat}; -use vec::{Vec2, Vec3, Vec4}; +use core::Dimensional; +use core::{Quat, ToQuat}; +use core::{Vec2, Vec3, Vec4}; +#[path = "../num_macros.rs"] mod num_macros; mod dim_macros; @@ -336,8 +336,8 @@ impl> Mat2 { #[cfg(test)] mod mat2_tests{ - use mat::*; - use vec::*; + use core::mat::*; + use core::vec::*; static a: Mat2 = Mat2 { x: Vec2 { x: 1.0, y: 3.0 }, y: Vec2 { x: 2.0, y: 4.0 } }; @@ -900,8 +900,8 @@ impl> Mat3 { #[cfg(test)] mod mat3_tests{ - use mat::*; - use vec::*; + use core::mat::*; + use core::vec::*; static a: Mat3 = Mat3 { x: Vec3 { x: 1.0, y: 4.0, z: 7.0 }, y: Vec3 { x: 2.0, y: 5.0, z: 8.0 }, @@ -1429,8 +1429,8 @@ impl> Mat4 { #[cfg(test)] mod mat4_tests { - use mat::*; - use vec::*; + use core::mat::*; + use core::vec::*; static a: Mat4 = Mat4 { x: Vec4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 }, y: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 }, diff --git a/src/quat.rs b/src/core/quat.rs similarity index 98% rename from src/quat.rs rename to src/core/quat.rs index ab4bb87..5695ddc 100644 --- a/src/quat.rs +++ b/src/core/quat.rs @@ -13,11 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use dim::Dimensional; - -use mat::{Mat3, ToMat3}; -use vec::Vec3; +use core::Dimensional; +use core::{Mat3, ToMat3}; +use core::Vec3; +#[path = "../num_macros.rs"] mod num_macros; mod dim_macros; @@ -310,9 +310,9 @@ impl Quat { #[cfg(test)] mod tests { - use mat::*; - use quat::*; - use vec::*; + use core::mat::*; + use core::quat::*; + use core::vec::*; #[test] fn test_from_angle_axis() { diff --git a/src/vec.rs b/src/core/vec.rs similarity index 99% rename from src/vec.rs rename to src/core/vec.rs index aac6fd1..575bd34 100644 --- a/src/vec.rs +++ b/src/core/vec.rs @@ -13,8 +13,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use dim::Dimensional; +use core::Dimensional; +#[path = "../num_macros.rs"] mod num_macros; mod dim_macros; @@ -392,7 +393,7 @@ impl Vec2 { #[cfg(test)] mod vec2_tests { - use vec::*; + use core::vec::*; #[test] fn test_vec2() { @@ -946,7 +947,7 @@ impl Vec3 { #[cfg(test)] mod vec3_tests{ - use vec::*; + use core::vec::*; #[test] fn test_vec3() { @@ -1546,7 +1547,7 @@ impl Vec4 { #[cfg(test)] mod vec4_tests { - use vec::*; + use core::vec::*; #[test] fn test_vec4() { diff --git a/src/geom/geom.rs b/src/geom/geom.rs new file mode 100644 index 0000000..deae997 --- /dev/null +++ b/src/geom/geom.rs @@ -0,0 +1,22 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub use self::plane::Plane; +pub use self::point::{Point2, Point3}; +pub use self::ray::Ray3; + +pub mod plane; +pub mod point; +pub mod ray; diff --git a/src/plane.rs b/src/geom/plane.rs similarity index 90% rename from src/plane.rs rename to src/geom/plane.rs index 4a9367b..781ce8d 100644 --- a/src/plane.rs +++ b/src/geom/plane.rs @@ -13,14 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use dim::Dimensional; -use mat::Mat3; -use point::Point3; -use ray::Ray3; -use vec::{Vec3, Vec4}; +use core::{Vec3, Vec4, Mat3}; +use geom::{Point3, Ray3}; +#[path = "../num_macros.rs"] mod num_macros; -mod dim_macros; /// A plane formed from the equation: `Ax + Bx + Cx + D = 0` /// @@ -37,10 +34,6 @@ pub struct Plane { dist: T, } -impl_dimensional!(Plane, T, 4) -impl_approx!(Plane, 4) -impl_swap!(Plane) - impl Plane { /// # Arguments /// @@ -150,6 +143,24 @@ impl> Plane { } } +impl> ApproxEq for Plane { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Plane) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Plane, epsilon: &T) -> bool { + self.norm.approx_eq_eps(&other.norm, epsilon) && + self.dist.approx_eq_eps(&other.dist, epsilon) + } +} + impl ToStr for Plane { pub fn to_str(&self) -> ~str { fmt!("%?x + %?y + %?z + %? = 0", self.norm.x, self.norm.y, self.norm.z, self.dist) @@ -158,8 +169,8 @@ impl ToStr for Plane { #[cfg(test)] mod tests { - use plane::*; - use point::*; + use geom::plane::*; + use geom::point::*; #[test] fn test_from_3p() { diff --git a/src/point.rs b/src/geom/point.rs similarity index 70% rename from src/point.rs rename to src/geom/point.rs index fc27dcf..6294dcd 100644 --- a/src/point.rs +++ b/src/geom/point.rs @@ -13,10 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use dim::Dimensional; -use vec::{Vec2, Vec3}; - -mod dim_macros; +use core::{Vec2, Vec3}; /// A geometric point pub trait Point: Eq + ApproxEq + ToStr { @@ -28,9 +25,6 @@ pub trait Point: Eq + ApproxEq + ToStr { #[deriving(Clone, Eq)] pub struct Point2(Vec2); -impl_dimensional!(Point2, T, 2) -impl_approx!(Point2, 2) - impl Point2 { pub fn new(x: T, y: T) -> Point2 { Point2(Vec2::new(x, y)) @@ -47,19 +41,43 @@ impl Point> for Point2 { } } +impl> ApproxEq for Point2 { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Point2) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Point2, epsilon: &T) -> bool { + (**self).approx_eq_eps(&**other, epsilon) + } +} + impl ToStr for Point2 { pub fn to_str(&self) -> ~str { fmt!("[%?, %?]", self.x, self.y) } } +#[cfg(test)] +mod test_point2 { + use geom::point::*; + + #[test] + fn test_to_str() { + assert_eq!(Point2::new(1, 2).to_str(), ~"[1, 2]"); + } +} + /// A three-dimensional point #[deriving(Clone, Eq)] pub struct Point3(Vec3); -impl_dimensional!(Point3, T, 3) -impl_approx!(Point3, 3) - impl Point3 { pub fn new(x: T, y: T, z: T) -> Point3 { Point3(Vec3::new(x, y, z)) @@ -76,25 +94,32 @@ impl Point> for Point3 { } } +impl> ApproxEq for Point3 { + #[inline] + pub fn approx_epsilon() -> T { + ApproxEq::approx_epsilon::() + } + + #[inline] + pub fn approx_eq(&self, other: &Point3) -> bool { + self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) + } + + #[inline] + pub fn approx_eq_eps(&self, other: &Point3, epsilon: &T) -> bool { + (**self).approx_eq_eps(&**other, epsilon) + } +} + impl ToStr for Point3 { pub fn to_str(&self) -> ~str { fmt!("[%?, %?, %?]", self.x, self.y, self.z) } } -#[cfg(test)] -mod test_point2 { - use point::*; - - #[test] - fn test_to_str() { - assert_eq!(Point2::new(1, 2).to_str(), ~"[1, 2]"); - } -} - #[cfg(test)] mod test_point3 { - use point::*; + use geom::point::*; #[test] fn test_to_str() { diff --git a/src/ray.rs b/src/geom/ray.rs similarity index 97% rename from src/ray.rs rename to src/geom/ray.rs index 6ee7770..d58dfa1 100644 --- a/src/ray.rs +++ b/src/geom/ray.rs @@ -13,8 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use vec::Vec3; -use point::Point3; +use core::Vec3; +use geom::Point3; /// A three-dimensional ray /// @@ -44,4 +44,4 @@ impl> ApproxEq for Ray3 { self.pos.approx_eq_eps(&other.pos, epsilon) && self.dir.approx_eq_eps(&other.dir, epsilon) } -} \ No newline at end of file +} diff --git a/src/lmath.rs b/src/lmath.rs index 81b78d8..8fd978f 100644 --- a/src/lmath.rs +++ b/src/lmath.rs @@ -23,14 +23,14 @@ #[license = "ASL2"]; #[crate_type = "lib"]; -pub mod dim; +#[path = "core/core.rs"] +pub mod core; -pub mod mat; -pub mod quat; -pub mod vec; +#[cfg(geom)] +#[path = "geom/geom.rs"] +pub mod geom; + +#[cfg(world)] +#[path = "world/world.rs"] +pub mod world; -pub mod frustum; -pub mod plane; -pub mod point; -pub mod projection; -pub mod ray; diff --git a/src/frustum.rs b/src/world/frustum.rs similarity index 98% rename from src/frustum.rs rename to src/world/frustum.rs index 2ea28f7..26e6ca1 100644 --- a/src/frustum.rs +++ b/src/world/frustum.rs @@ -13,10 +13,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -use mat::Mat4; -use plane::Plane; -use point::Point3; +use core::Mat4; +use geom::{Plane, Point3}; +#[path = "../num_macros.rs"] mod num_macros; #[deriving(Clone, Eq)] diff --git a/src/projection.rs b/src/world/projection.rs similarity index 99% rename from src/projection.rs rename to src/world/projection.rs index b9dfb96..d372324 100644 --- a/src/projection.rs +++ b/src/world/projection.rs @@ -13,10 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -use frustum::Frustum; -use mat::Mat4; -use plane::Plane; +use core::Mat4; +use geom::Plane; +use world::Frustum; +#[path = "../num_macros.rs"] mod num_macros; /// diff --git a/src/world/world.rs b/src/world/world.rs new file mode 100644 index 0000000..2a10437 --- /dev/null +++ b/src/world/world.rs @@ -0,0 +1,20 @@ +// Copyright 2013 The Lmath Developers. For a full listing of the authors, +// refer to the AUTHORS file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub use self::frustum::Frustum; +pub use self::projection::{Projection, Ortho, Perspective, PerspectiveFOV}; + +pub mod frustum; +pub mod projection;