From 896c357ea8e0e023b1a38426604da30b7ff83db3 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 13 Mar 2015 23:49:22 +0300 Subject: [PATCH] Implemented Aabb to_corners() --- src/aabb.rs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/aabb.rs b/src/aabb.rs index aab695a..cf47d80 100644 --- a/src/aabb.rs +++ b/src/aabb.rs @@ -103,6 +103,15 @@ impl Aabb2 { p1.y.partial_max(p2.y)), } } + + /// Compute corners. + #[inline] + pub fn to_corners(&self) -> [Point2; 4] { + [self.min, + Point2::new(self.max.x, self.min.y), + Point2::new(self.min.x, self.max.y), + self.max] + } } impl Aabb, Point2> for Aabb2 { @@ -150,6 +159,19 @@ impl Aabb3 { p1.z.partial_max(p2.z)), } } + + /// Compute corners. + #[inline] + pub fn to_corners(&self) -> [Point3; 8] { + [self.min, + Point3::new(self.max.x, self.min.y, self.min.z), + Point3::new(self.min.x, self.max.y, self.min.z), + Point3::new(self.max.x, self.max.y, self.min.z), + Point3::new(self.min.x, self.min.y, self.max.z), + Point3::new(self.max.x, self.min.y, self.max.z), + Point3::new(self.min.x, self.max.y, self.max.z), + self.max] + } } impl Aabb, Point3> for Aabb3 { @@ -222,11 +244,13 @@ impl Intersect>> for (Ray2, Aabb2) { impl Bound for Aabb3 { fn relate(&self, plane: &Plane) -> Relation { - //TODO: match all 8 corners - match (self.min.relate(plane), self.max.relate(plane)) { - (Relation::In, Relation::In) => Relation::In, - (Relation::Out, Relation::Out) => Relation::Out, - (_, _) => Relation::Cross, + let corners = self.to_corners(); + let first = corners[0].relate(plane); + for p in corners[1..].iter() { + if p.relate(plane) != first { + return Relation::Cross; + } } + first } }