Merge pull request #38 from rsaarelm/master
Axis-aligned boxes use top and bottom corners
This commit is contained in:
commit
0c903a7ec5
3 changed files with 40 additions and 6 deletions
|
@ -16,16 +16,37 @@
|
||||||
//! Axis-aligned bounding boxes
|
//! Axis-aligned bounding boxes
|
||||||
|
|
||||||
use point::{Point2, Point3};
|
use point::{Point2, Point3};
|
||||||
use vector::{Vec2, Vec3};
|
|
||||||
|
|
||||||
#[deriving(Clone, Eq)]
|
#[deriving(Clone, Eq)]
|
||||||
pub struct Aabb2<S> {
|
pub struct Aabb2<S> {
|
||||||
center: Point2<S>,
|
min: Point2<S>,
|
||||||
size: Vec2<S>,
|
max: Point2<S>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: Num + Orderable> Aabb2<S> {
|
||||||
|
/// Construct a new axis-aligned bounding box from two points.
|
||||||
|
#[inline]
|
||||||
|
pub fn new(p1: &Point2<S>, p2: &Point2<S>) -> Aabb2<S> {
|
||||||
|
Aabb2 {
|
||||||
|
min: Point2::new(p1.x.min(&p2.x), p1.y.min(&p2.y)),
|
||||||
|
max: Point2::new(p1.x.max(&p2.x), p1.y.max(&p2.y)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[deriving(Clone, Eq)]
|
#[deriving(Clone, Eq)]
|
||||||
pub struct Aabb3<S> {
|
pub struct Aabb3<S> {
|
||||||
center: Point3<S>,
|
min: Point3<S>,
|
||||||
size: Vec3<S>,
|
max: Point3<S>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: Num + Orderable> Aabb3<S> {
|
||||||
|
/// Construct a new axis-aligned bounding box from two points.
|
||||||
|
#[inline]
|
||||||
|
pub fn new(p1: &Point3<S>, p2: &Point3<S>) -> Aabb3<S> {
|
||||||
|
Aabb3 {
|
||||||
|
min: Point3::new(p1.x.min(&p2.x), p1.y.min(&p2.y), p1.z.min(&p2.z)),
|
||||||
|
max: Point3::new(p1.x.max(&p2.x), p1.y.max(&p2.y), p1.z.max(&p2.z)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
13
src/test/aabb.rs
Normal file
13
src/test/aabb.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
use cgmath::aabb::{Aabb2, Aabb3};
|
||||||
|
use cgmath::point::{Point2, Point3};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_aabb() {
|
||||||
|
let aabb = Aabb2::new(&Point2::new(-20f64, 30f64), &Point2::new(10f64, -10f64));
|
||||||
|
assert_eq!(aabb.min, Point2::new(-20f64, -10f64));
|
||||||
|
assert_eq!(aabb.max, Point2::new(10f64, 30f64));
|
||||||
|
|
||||||
|
let aabb = Aabb3::new(&Point3::new(-20f64, 30f64, 0f64), &Point3::new(10f64, -10f64, -5f64));
|
||||||
|
assert_eq!(aabb.min, Point3::new(-20f64, -10f64, -5f64));
|
||||||
|
assert_eq!(aabb.max, Point3::new(10f64, 30f64, 0f64));
|
||||||
|
}
|
|
@ -32,7 +32,7 @@ pub mod transform;
|
||||||
|
|
||||||
// pub mod projection;
|
// pub mod projection;
|
||||||
|
|
||||||
// pub mod aabb;
|
pub mod aabb;
|
||||||
// pub mod cylinder;
|
// pub mod cylinder;
|
||||||
// pub mod frustum;
|
// pub mod frustum;
|
||||||
// pub mod intersect;
|
// pub mod intersect;
|
||||||
|
|
Loading…
Reference in a new issue