From 5c3197a7fc993ac3b043b8229799c2dec2f328fb Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 9 Jul 2013 16:43:16 +1000 Subject: [PATCH] Rename Plane to Plane3 for consistency with other types --- src/geom/geom.rs | 2 +- src/geom/plane.rs | 56 ++++++++++++++++++++--------------------- src/world/frustum.rs | 46 ++++++++++++++++----------------- src/world/projection.rs | 26 +++++++++---------- 4 files changed, 65 insertions(+), 65 deletions(-) diff --git a/src/geom/geom.rs b/src/geom/geom.rs index ed9db33..9e32c17 100644 --- a/src/geom/geom.rs +++ b/src/geom/geom.rs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub use self::plane::Plane; +pub use self::plane::Plane3; pub use self::point::{Point, Point2, Point3}; pub use self::ray::Ray3; diff --git a/src/geom/plane.rs b/src/geom/plane.rs index 2691f44..fa78cf9 100644 --- a/src/geom/plane.rs +++ b/src/geom/plane.rs @@ -29,33 +29,33 @@ mod num_macros; /// - `n.z`: corresponds to `C` in the plane equation /// - `d`: the distance value, corresponding to `D` in the plane equation #[deriving(Clone, Eq)] -pub struct Plane { +pub struct Plane3 { norm: Vec3, dist: T, } -impl Plane { +impl Plane3 { /// # Arguments /// /// - `a`: the `x` component of the normal /// - `b`: the `y` component of the normal /// - `c`: the `z` component of the normal /// - `d`: the plane's distance value - pub fn from_abcd(a: T, b: T, c: T, d: T) -> Plane { - Plane { + pub fn from_abcd(a: T, b: T, c: T, d: T) -> Plane3 { + Plane3 { norm: Vec3::new(a, b, c), dist: d, } } /// Construct a plane from a normal vector `n` and a distance `d` - pub fn from_nd(norm: Vec3, dist: T) -> Plane { - Plane { norm: norm, dist: dist } + pub fn from_nd(norm: Vec3, dist: T) -> Plane3 { + Plane3 { norm: norm, dist: dist } } /// Construct a plane from the components of a four-dimensional vector - pub fn from_vec4(vec: Vec4) -> Plane { - Plane::from_abcd(vec.x.clone(), vec.y.clone(), vec.z.clone(), vec.w.clone()) + pub fn from_vec4(vec: Vec4) -> Plane3 { + Plane3::from_abcd(vec.x.clone(), vec.y.clone(), vec.z.clone(), vec.w.clone()) } /// Compute the distance from the plane to the point @@ -79,11 +79,11 @@ impl Plane { } } -impl Plane { +impl Plane3 { /// Constructs a plane that passes through the the three points `a`, `b` and `c` pub fn from_3p(a: Point3, b: Point3, - c: Point3) -> Option> { + c: Point3) -> Option> { // create two vectors that run parallel to the plane let v0 = b.as_vec().sub_v(a.as_vec()); let v1 = c.as_vec().sub_v(a.as_vec()); @@ -97,7 +97,7 @@ impl Plane { norm.normalize_self(); let dist = -a.as_vec().dot(&norm); - Some(Plane::from_nd(norm, dist)) + Some(Plane3::from_nd(norm, dist)) } } @@ -107,7 +107,7 @@ impl Plane { /// /// - `Some(r)`: The ray `r` where the planes intersect. /// - `None`: No valid intersection was found. The planes are probably parallel. - pub fn intersection_2pl(&self, other: &Plane) -> Option> { + pub fn intersection_2pl(&self, other: &Plane3) -> Option> { let ray_dir = self.norm.cross(&other.norm); if ray_dir.approx_eq(&Vec3::zero::()) { @@ -115,7 +115,7 @@ impl Plane { } else { // The end-point of the ray is at the three-plane intersection between // `self`, `other`, and a tempory plane positioned at the origin - do Plane::from_nd(ray_dir.clone(), zero!(T)).intersection_3pl(self, other).map |&ray_pos| { + do Plane3::from_nd(ray_dir.clone(), zero!(T)).intersection_3pl(self, other).map |&ray_pos| { Ray3 { pos: ray_pos.clone(), dir: ray_dir.clone(), @@ -131,7 +131,7 @@ impl Plane { /// - `Some(p)`: The position vector `p` where the planes intersect. /// - `None`: No valid intersection was found. The normals of the three /// planes are probably coplanar. - pub fn intersection_3pl(&self, other_a: &Plane, other_b: &Plane) -> Option> { + pub fn intersection_3pl(&self, other_a: &Plane3, other_b: &Plane3) -> Option> { let mx = Mat3::new(self.norm.x.clone(), other_a.norm.x.clone(), other_b.norm.x.clone(), self.norm.y.clone(), other_a.norm.y.clone(), other_b.norm.y.clone(), self.norm.z.clone(), other_a.norm.z.clone(), other_b.norm.z.clone()); @@ -145,25 +145,25 @@ impl Plane { } } -impl> ApproxEq for Plane { +impl> ApproxEq for Plane3 { #[inline] pub fn approx_epsilon() -> T { ApproxEq::approx_epsilon::() } #[inline] - pub fn approx_eq(&self, other: &Plane) -> bool { + pub fn approx_eq(&self, other: &Plane3) -> bool { self.approx_eq_eps(other, &ApproxEq::approx_epsilon::()) } #[inline] - pub fn approx_eq_eps(&self, other: &Plane, epsilon: &T) -> bool { + pub fn approx_eq_eps(&self, other: &Plane3, epsilon: &T) -> bool { self.norm.approx_eq_eps(&other.norm, epsilon) && self.dist.approx_eq_eps(&other.dist, epsilon) } } -impl ToStr for Plane { +impl ToStr for Plane3 { pub fn to_str(&self) -> ~str { fmt!("%?x + %?y + %?z + %? = 0", self.norm.x, self.norm.y, self.norm.z, self.dist) } @@ -176,26 +176,26 @@ mod tests { #[test] fn test_from_3p() { - assert_eq!(Plane::from_3p(Point3::new(5f, 0f, 5f), - Point3::new(5f, 5f, 5f), - Point3::new(5f, 0f, -1f)), Some(Plane::from_abcd(-1f, 0f, 0f, 5f))); + assert_eq!(Plane3::from_3p(Point3::new(5f, 0f, 5f), + Point3::new(5f, 5f, 5f), + Point3::new(5f, 0f, -1f)), Some(Plane3::from_abcd(-1f, 0f, 0f, 5f))); - assert_eq!(Plane::from_3p(Point3::new(0f, 5f, -5f), - Point3::new(0f, 5f, 0f), - Point3::new(0f, 5f, 5f)), None); // The points are parallel + assert_eq!(Plane3::from_3p(Point3::new(0f, 5f, -5f), + Point3::new(0f, 5f, 0f), + Point3::new(0f, 5f, 5f)), None); // The points are parallel } #[test] fn test_plane_intersection_3pl() { - let p0 = Plane::from_abcd(1.0, 0.0, 0.0, 1.0); - let p1 = Plane::from_abcd(0.0, -1.0, 0.0, 2.0); - let p2 = Plane::from_abcd(0.0, 0.0, 1.0, 1.0); + let p0 = Plane3::from_abcd(1.0, 0.0, 0.0, 1.0); + let p1 = Plane3::from_abcd(0.0, -1.0, 0.0, 2.0); + let p2 = Plane3::from_abcd(0.0, 0.0, 1.0, 1.0); assert_eq!(p0.intersection_3pl(&p1, &p2), Some(Point3::new(1.0, -2.0, 1.0))); } #[test] fn test_to_str() { - assert_eq!(Plane::from_abcd(1.0, 2.0, 3.0, 4.0).to_str(), ~"1x + 2y + 3z + 4 = 0"); + assert_eq!(Plane3::from_abcd(1.0, 2.0, 3.0, 4.0).to_str(), ~"1x + 2y + 3z + 4 = 0"); } } diff --git a/src/world/frustum.rs b/src/world/frustum.rs index 2c1e4ef..d316bef 100644 --- a/src/world/frustum.rs +++ b/src/world/frustum.rs @@ -14,19 +14,19 @@ // limitations under the License. use core::Mat4; -use geom::{Plane, Point3}; +use geom::{Plane3, Point3}; #[path = "../num_macros.rs"] mod num_macros; #[deriving(Clone, Eq)] pub struct Frustum { - left: Plane, - right: Plane, - bottom: Plane, - top: Plane, - near: Plane, - far: Plane, + left: Plane3, + right: Plane3, + bottom: Plane3, + top: Plane3, + near: Plane3, + far: Plane3, } #[deriving(Clone, Eq)] @@ -43,9 +43,9 @@ pub struct FrustumPoints { impl Frustum { /// Constructs a frustum - pub fn from_planes(left: Plane, right: Plane, - bottom: Plane, top: Plane, - near: Plane, far: Plane) -> Frustum { + pub fn from_planes(left: Plane3, right: Plane3, + bottom: Plane3, top: Plane3, + near: Plane3, far: Plane3) -> Frustum { Frustum { left: left, right: right, @@ -59,23 +59,23 @@ impl Frustum { /// Extracts frustum planes from a projection matrix pub fn from_matrix(mat: Mat4) -> Frustum { Frustum { - left: Plane::from_vec4(mat.row(3).add_v(&mat.row(0)).normalize()), - right: Plane::from_vec4(mat.row(3).sub_v(&mat.row(0)).normalize()), - bottom: Plane::from_vec4(mat.row(3).add_v(&mat.row(1)).normalize()), - top: Plane::from_vec4(mat.row(3).sub_v(&mat.row(1)).normalize()), - near: Plane::from_vec4(mat.row(3).add_v(&mat.row(2)).normalize()), - far: Plane::from_vec4(mat.row(3).sub_v(&mat.row(2)).normalize()), + left: Plane3::from_vec4(mat.row(3).add_v(&mat.row(0)).normalize()), + right: Plane3::from_vec4(mat.row(3).sub_v(&mat.row(0)).normalize()), + bottom: Plane3::from_vec4(mat.row(3).add_v(&mat.row(1)).normalize()), + top: Plane3::from_vec4(mat.row(3).sub_v(&mat.row(1)).normalize()), + near: Plane3::from_vec4(mat.row(3).add_v(&mat.row(2)).normalize()), + far: Plane3::from_vec4(mat.row(3).sub_v(&mat.row(2)).normalize()), } } pub fn base() -> Frustum { Frustum { - left: Plane::from_abcd( one!(T), zero!(T), zero!(T), one!(T)), - right: Plane::from_abcd(-one!(T), zero!(T), zero!(T), one!(T)), - bottom: Plane::from_abcd( zero!(T), one!(T), zero!(T), one!(T)), - top: Plane::from_abcd( zero!(T), -one!(T), zero!(T), one!(T)), - near: Plane::from_abcd( zero!(T), zero!(T), -one!(T), one!(T)), - far: Plane::from_abcd( zero!(T), zero!(T), one!(T), one!(T)), + left: Plane3::from_abcd( one!(T), zero!(T), zero!(T), one!(T)), + right: Plane3::from_abcd(-one!(T), zero!(T), zero!(T), one!(T)), + bottom: Plane3::from_abcd( zero!(T), one!(T), zero!(T), one!(T)), + top: Plane3::from_abcd( zero!(T), -one!(T), zero!(T), one!(T)), + near: Plane3::from_abcd( zero!(T), zero!(T), -one!(T), one!(T)), + far: Plane3::from_abcd( zero!(T), zero!(T), one!(T), one!(T)), } } } @@ -117,4 +117,4 @@ impl> ApproxEq for Frustum { self.near.approx_eq_eps(&other.near, epsilon) && self.far.approx_eq_eps(&other.far, epsilon) } -} \ No newline at end of file +} diff --git a/src/world/projection.rs b/src/world/projection.rs index c0f692f..effd90a 100644 --- a/src/world/projection.rs +++ b/src/world/projection.rs @@ -14,7 +14,7 @@ // limitations under the License. use core::Mat4; -use geom::Plane; +use geom::Plane3; use world::Frustum; #[path = "../num_macros.rs"] @@ -253,12 +253,12 @@ impl Projection for Perspective { let theta_t = (self.top / self.far).atan(); Frustum { - left: Plane::from_abcd(theta_l.cos(), zero!(T), theta_l.sin(), zero!(T)), - right: Plane::from_abcd(theta_r.cos(), zero!(T), theta_r.sin(), zero!(T)), - bottom: Plane::from_abcd(zero!(T), theta_b.cos(), theta_b.sin(), zero!(T)), - top: Plane::from_abcd(zero!(T), theta_t.cos(), theta_t.sin(), zero!(T)), - near: Plane::from_abcd(zero!(T), zero!(T), -one!(T), -self.near.clone()), - far: Plane::from_abcd(zero!(T), zero!(T), one!(T), self.far.clone()), + left: Plane3::from_abcd(theta_l.cos(), zero!(T), theta_l.sin(), zero!(T)), + right: Plane3::from_abcd(theta_r.cos(), zero!(T), theta_r.sin(), zero!(T)), + bottom: Plane3::from_abcd(zero!(T), theta_b.cos(), theta_b.sin(), zero!(T)), + top: Plane3::from_abcd(zero!(T), theta_t.cos(), theta_t.sin(), zero!(T)), + near: Plane3::from_abcd(zero!(T), zero!(T), -one!(T), -self.near.clone()), + far: Plane3::from_abcd(zero!(T), zero!(T), one!(T), self.far.clone()), } } } @@ -317,12 +317,12 @@ impl Projection for Ortho { pub fn to_frustum(&self) -> Result, ~str> { do self.if_valid { Frustum { - left: Plane::from_abcd(one!(T), zero!(T), zero!(T), self.left.clone()), - right: Plane::from_abcd(-one!(T), zero!(T), zero!(T), self.right.clone()), - bottom: Plane::from_abcd(zero!(T), one!(T), zero!(T), self.bottom.clone()), - top: Plane::from_abcd(zero!(T), -one!(T), zero!(T), self.top.clone()), - near: Plane::from_abcd(zero!(T), zero!(T), -one!(T), self.near.clone()), - far: Plane::from_abcd(zero!(T), zero!(T), one!(T),self.far.clone()), + left: Plane3::from_abcd(one!(T), zero!(T), zero!(T), self.left.clone()), + right: Plane3::from_abcd(-one!(T), zero!(T), zero!(T), self.right.clone()), + bottom: Plane3::from_abcd(zero!(T), one!(T), zero!(T), self.bottom.clone()), + top: Plane3::from_abcd(zero!(T), -one!(T), zero!(T), self.top.clone()), + near: Plane3::from_abcd(zero!(T), zero!(T), -one!(T), self.near.clone()), + far: Plane3::from_abcd(zero!(T), zero!(T), one!(T),self.far.clone()), } } }