Rename Plane to Plane3 for consistency with other types

This commit is contained in:
Brendan Zabarauskas 2013-07-09 16:43:16 +10:00
parent 2ca90cb750
commit 5c3197a7fc
4 changed files with 65 additions and 65 deletions

View file

@ -13,7 +13,7 @@
// 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 use self::plane::Plane; pub use self::plane::Plane3;
pub use self::point::{Point, Point2, Point3}; pub use self::point::{Point, Point2, Point3};
pub use self::ray::Ray3; pub use self::ray::Ray3;

View file

@ -29,33 +29,33 @@ mod num_macros;
/// - `n.z`: corresponds to `C` in the plane equation /// - `n.z`: corresponds to `C` in the plane equation
/// - `d`: the distance value, corresponding to `D` in the plane equation /// - `d`: the distance value, corresponding to `D` in the plane equation
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct Plane<T> { pub struct Plane3<T> {
norm: Vec3<T>, norm: Vec3<T>,
dist: T, dist: T,
} }
impl<T:Clone + Float> Plane<T> { impl<T:Clone + Float> Plane3<T> {
/// # Arguments /// # Arguments
/// ///
/// - `a`: the `x` component of the normal /// - `a`: the `x` component of the normal
/// - `b`: the `y` component of the normal /// - `b`: the `y` component of the normal
/// - `c`: the `z` component of the normal /// - `c`: the `z` component of the normal
/// - `d`: the plane's distance value /// - `d`: the plane's distance value
pub fn from_abcd(a: T, b: T, c: T, d: T) -> Plane<T> { pub fn from_abcd(a: T, b: T, c: T, d: T) -> Plane3<T> {
Plane { Plane3 {
norm: Vec3::new(a, b, c), norm: Vec3::new(a, b, c),
dist: d, dist: d,
} }
} }
/// Construct a plane from a normal vector `n` and a distance `d` /// Construct a plane from a normal vector `n` and a distance `d`
pub fn from_nd(norm: Vec3<T>, dist: T) -> Plane<T> { pub fn from_nd(norm: Vec3<T>, dist: T) -> Plane3<T> {
Plane { norm: norm, dist: dist } Plane3 { norm: norm, dist: dist }
} }
/// Construct a plane from the components of a four-dimensional vector /// Construct a plane from the components of a four-dimensional vector
pub fn from_vec4(vec: Vec4<T>) -> Plane<T> { pub fn from_vec4(vec: Vec4<T>) -> Plane3<T> {
Plane::from_abcd(vec.x.clone(), vec.y.clone(), vec.z.clone(), vec.w.clone()) Plane3::from_abcd(vec.x.clone(), vec.y.clone(), vec.z.clone(), vec.w.clone())
} }
/// Compute the distance from the plane to the point /// Compute the distance from the plane to the point
@ -79,11 +79,11 @@ impl<T:Clone + Float> Plane<T> {
} }
} }
impl<T:Clone + Float> Plane<T> { impl<T:Clone + Float> Plane3<T> {
/// Constructs a plane that passes through the the three points `a`, `b` and `c` /// Constructs a plane that passes through the the three points `a`, `b` and `c`
pub fn from_3p(a: Point3<T>, pub fn from_3p(a: Point3<T>,
b: Point3<T>, b: Point3<T>,
c: Point3<T>) -> Option<Plane<T>> { c: Point3<T>) -> Option<Plane3<T>> {
// create two vectors that run parallel to the plane // create two vectors that run parallel to the plane
let v0 = b.as_vec().sub_v(a.as_vec()); let v0 = b.as_vec().sub_v(a.as_vec());
let v1 = c.as_vec().sub_v(a.as_vec()); let v1 = c.as_vec().sub_v(a.as_vec());
@ -97,7 +97,7 @@ impl<T:Clone + Float> Plane<T> {
norm.normalize_self(); norm.normalize_self();
let dist = -a.as_vec().dot(&norm); let dist = -a.as_vec().dot(&norm);
Some(Plane::from_nd(norm, dist)) Some(Plane3::from_nd(norm, dist))
} }
} }
@ -107,7 +107,7 @@ impl<T:Clone + Float> Plane<T> {
/// ///
/// - `Some(r)`: The ray `r` where the planes intersect. /// - `Some(r)`: The ray `r` where the planes intersect.
/// - `None`: No valid intersection was found. The planes are probably parallel. /// - `None`: No valid intersection was found. The planes are probably parallel.
pub fn intersection_2pl(&self, other: &Plane<T>) -> Option<Ray3<T>> { pub fn intersection_2pl(&self, other: &Plane3<T>) -> Option<Ray3<T>> {
let ray_dir = self.norm.cross(&other.norm); let ray_dir = self.norm.cross(&other.norm);
if ray_dir.approx_eq(&Vec3::zero::<T>()) { if ray_dir.approx_eq(&Vec3::zero::<T>()) {
@ -115,7 +115,7 @@ impl<T:Clone + Float> Plane<T> {
} else { } else {
// The end-point of the ray is at the three-plane intersection between // The end-point of the ray is at the three-plane intersection between
// `self`, `other`, and a tempory plane positioned at the origin // `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 { Ray3 {
pos: ray_pos.clone(), pos: ray_pos.clone(),
dir: ray_dir.clone(), dir: ray_dir.clone(),
@ -131,7 +131,7 @@ impl<T:Clone + Float> Plane<T> {
/// - `Some(p)`: The position vector `p` where the planes intersect. /// - `Some(p)`: The position vector `p` where the planes intersect.
/// - `None`: No valid intersection was found. The normals of the three /// - `None`: No valid intersection was found. The normals of the three
/// planes are probably coplanar. /// planes are probably coplanar.
pub fn intersection_3pl(&self, other_a: &Plane<T>, other_b: &Plane<T>) -> Option<Point3<T>> { pub fn intersection_3pl(&self, other_a: &Plane3<T>, other_b: &Plane3<T>) -> Option<Point3<T>> {
let mx = Mat3::new(self.norm.x.clone(), other_a.norm.x.clone(), other_b.norm.x.clone(), 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.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()); self.norm.z.clone(), other_a.norm.z.clone(), other_b.norm.z.clone());
@ -145,25 +145,25 @@ impl<T:Clone + Float> Plane<T> {
} }
} }
impl<T:Clone + Eq + ApproxEq<T>> ApproxEq<T> for Plane<T> { impl<T:Clone + Eq + ApproxEq<T>> ApproxEq<T> for Plane3<T> {
#[inline] #[inline]
pub fn approx_epsilon() -> T { pub fn approx_epsilon() -> T {
ApproxEq::approx_epsilon::<T,T>() ApproxEq::approx_epsilon::<T,T>()
} }
#[inline] #[inline]
pub fn approx_eq(&self, other: &Plane<T>) -> bool { pub fn approx_eq(&self, other: &Plane3<T>) -> bool {
self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>()) self.approx_eq_eps(other, &ApproxEq::approx_epsilon::<T,T>())
} }
#[inline] #[inline]
pub fn approx_eq_eps(&self, other: &Plane<T>, epsilon: &T) -> bool { pub fn approx_eq_eps(&self, other: &Plane3<T>, epsilon: &T) -> bool {
self.norm.approx_eq_eps(&other.norm, epsilon) && self.norm.approx_eq_eps(&other.norm, epsilon) &&
self.dist.approx_eq_eps(&other.dist, epsilon) self.dist.approx_eq_eps(&other.dist, epsilon)
} }
} }
impl<T> ToStr for Plane<T> { impl<T> ToStr for Plane3<T> {
pub fn to_str(&self) -> ~str { pub fn to_str(&self) -> ~str {
fmt!("%?x + %?y + %?z + %? = 0", self.norm.x, self.norm.y, self.norm.z, self.dist) fmt!("%?x + %?y + %?z + %? = 0", self.norm.x, self.norm.y, self.norm.z, self.dist)
} }
@ -176,26 +176,26 @@ mod tests {
#[test] #[test]
fn test_from_3p() { fn test_from_3p() {
assert_eq!(Plane::from_3p(Point3::new(5f, 0f, 5f), assert_eq!(Plane3::from_3p(Point3::new(5f, 0f, 5f),
Point3::new(5f, 5f, 5f), Point3::new(5f, 5f, 5f),
Point3::new(5f, 0f, -1f)), Some(Plane::from_abcd(-1f, 0f, 0f, 5f))); Point3::new(5f, 0f, -1f)), Some(Plane3::from_abcd(-1f, 0f, 0f, 5f)));
assert_eq!(Plane::from_3p(Point3::new(0f, 5f, -5f), assert_eq!(Plane3::from_3p(Point3::new(0f, 5f, -5f),
Point3::new(0f, 5f, 0f), Point3::new(0f, 5f, 0f),
Point3::new(0f, 5f, 5f)), None); // The points are parallel Point3::new(0f, 5f, 5f)), None); // The points are parallel
} }
#[test] #[test]
fn test_plane_intersection_3pl() { fn test_plane_intersection_3pl() {
let p0 = Plane::from_abcd(1.0, 0.0, 0.0, 1.0); let p0 = Plane3::from_abcd(1.0, 0.0, 0.0, 1.0);
let p1 = Plane::from_abcd(0.0, -1.0, 0.0, 2.0); let p1 = Plane3::from_abcd(0.0, -1.0, 0.0, 2.0);
let p2 = Plane::from_abcd(0.0, 0.0, 1.0, 1.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))); assert_eq!(p0.intersection_3pl(&p1, &p2), Some(Point3::new(1.0, -2.0, 1.0)));
} }
#[test] #[test]
fn test_to_str() { 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");
} }
} }

View file

@ -14,19 +14,19 @@
// limitations under the License. // limitations under the License.
use core::Mat4; use core::Mat4;
use geom::{Plane, Point3}; use geom::{Plane3, Point3};
#[path = "../num_macros.rs"] #[path = "../num_macros.rs"]
mod num_macros; mod num_macros;
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
pub struct Frustum<T> { pub struct Frustum<T> {
left: Plane<T>, left: Plane3<T>,
right: Plane<T>, right: Plane3<T>,
bottom: Plane<T>, bottom: Plane3<T>,
top: Plane<T>, top: Plane3<T>,
near: Plane<T>, near: Plane3<T>,
far: Plane<T>, far: Plane3<T>,
} }
#[deriving(Clone, Eq)] #[deriving(Clone, Eq)]
@ -43,9 +43,9 @@ pub struct FrustumPoints<T> {
impl<T:Clone + Float> Frustum<T> { impl<T:Clone + Float> Frustum<T> {
/// Constructs a frustum /// Constructs a frustum
pub fn from_planes(left: Plane<T>, right: Plane<T>, pub fn from_planes(left: Plane3<T>, right: Plane3<T>,
bottom: Plane<T>, top: Plane<T>, bottom: Plane3<T>, top: Plane3<T>,
near: Plane<T>, far: Plane<T>) -> Frustum<T> { near: Plane3<T>, far: Plane3<T>) -> Frustum<T> {
Frustum { Frustum {
left: left, left: left,
right: right, right: right,
@ -59,23 +59,23 @@ impl<T:Clone + Float> Frustum<T> {
/// Extracts frustum planes from a projection matrix /// Extracts frustum planes from a projection matrix
pub fn from_matrix(mat: Mat4<T>) -> Frustum<T> { pub fn from_matrix(mat: Mat4<T>) -> Frustum<T> {
Frustum { Frustum {
left: Plane::from_vec4(mat.row(3).add_v(&mat.row(0)).normalize()), left: Plane3::from_vec4(mat.row(3).add_v(&mat.row(0)).normalize()),
right: Plane::from_vec4(mat.row(3).sub_v(&mat.row(0)).normalize()), right: Plane3::from_vec4(mat.row(3).sub_v(&mat.row(0)).normalize()),
bottom: Plane::from_vec4(mat.row(3).add_v(&mat.row(1)).normalize()), bottom: Plane3::from_vec4(mat.row(3).add_v(&mat.row(1)).normalize()),
top: Plane::from_vec4(mat.row(3).sub_v(&mat.row(1)).normalize()), top: Plane3::from_vec4(mat.row(3).sub_v(&mat.row(1)).normalize()),
near: Plane::from_vec4(mat.row(3).add_v(&mat.row(2)).normalize()), near: Plane3::from_vec4(mat.row(3).add_v(&mat.row(2)).normalize()),
far: Plane::from_vec4(mat.row(3).sub_v(&mat.row(2)).normalize()), far: Plane3::from_vec4(mat.row(3).sub_v(&mat.row(2)).normalize()),
} }
} }
pub fn base() -> Frustum<T> { pub fn base() -> Frustum<T> {
Frustum { Frustum {
left: Plane::from_abcd( one!(T), zero!(T), zero!(T), one!(T)), left: Plane3::from_abcd( one!(T), zero!(T), zero!(T), one!(T)),
right: Plane::from_abcd(-one!(T), zero!(T), zero!(T), one!(T)), right: Plane3::from_abcd(-one!(T), zero!(T), zero!(T), one!(T)),
bottom: Plane::from_abcd( zero!(T), one!(T), zero!(T), one!(T)), bottom: Plane3::from_abcd( zero!(T), one!(T), zero!(T), one!(T)),
top: Plane::from_abcd( zero!(T), -one!(T), zero!(T), one!(T)), top: Plane3::from_abcd( zero!(T), -one!(T), zero!(T), one!(T)),
near: Plane::from_abcd( zero!(T), zero!(T), -one!(T), one!(T)), near: Plane3::from_abcd( zero!(T), zero!(T), -one!(T), one!(T)),
far: Plane::from_abcd( zero!(T), zero!(T), one!(T), one!(T)), far: Plane3::from_abcd( zero!(T), zero!(T), one!(T), one!(T)),
} }
} }
} }

View file

@ -14,7 +14,7 @@
// limitations under the License. // limitations under the License.
use core::Mat4; use core::Mat4;
use geom::Plane; use geom::Plane3;
use world::Frustum; use world::Frustum;
#[path = "../num_macros.rs"] #[path = "../num_macros.rs"]
@ -253,12 +253,12 @@ impl<T:Clone + Float> Projection<T> for Perspective<T> {
let theta_t = (self.top / self.far).atan(); let theta_t = (self.top / self.far).atan();
Frustum { Frustum {
left: Plane::from_abcd(theta_l.cos(), zero!(T), theta_l.sin(), zero!(T)), left: Plane3::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)), right: Plane3::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)), bottom: Plane3::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)), top: Plane3::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()), near: Plane3::from_abcd(zero!(T), zero!(T), -one!(T), -self.near.clone()),
far: Plane::from_abcd(zero!(T), zero!(T), one!(T), self.far.clone()), far: Plane3::from_abcd(zero!(T), zero!(T), one!(T), self.far.clone()),
} }
} }
} }
@ -317,12 +317,12 @@ impl<T:Clone + Float> Projection<T> for Ortho<T> {
pub fn to_frustum(&self) -> Result<Frustum<T>, ~str> { pub fn to_frustum(&self) -> Result<Frustum<T>, ~str> {
do self.if_valid { do self.if_valid {
Frustum { Frustum {
left: Plane::from_abcd(one!(T), zero!(T), zero!(T), self.left.clone()), left: Plane3::from_abcd(one!(T), zero!(T), zero!(T), self.left.clone()),
right: Plane::from_abcd(-one!(T), zero!(T), zero!(T), self.right.clone()), right: Plane3::from_abcd(-one!(T), zero!(T), zero!(T), self.right.clone()),
bottom: Plane::from_abcd(zero!(T), one!(T), zero!(T), self.bottom.clone()), bottom: Plane3::from_abcd(zero!(T), one!(T), zero!(T), self.bottom.clone()),
top: Plane::from_abcd(zero!(T), -one!(T), zero!(T), self.top.clone()), top: Plane3::from_abcd(zero!(T), -one!(T), zero!(T), self.top.clone()),
near: Plane::from_abcd(zero!(T), zero!(T), -one!(T), self.near.clone()), near: Plane3::from_abcd(zero!(T), zero!(T), -one!(T), self.near.clone()),
far: Plane::from_abcd(zero!(T), zero!(T), one!(T),self.far.clone()), far: Plane3::from_abcd(zero!(T), zero!(T), one!(T),self.far.clone()),
} }
} }
} }