From 475d525c75a055b15e6a7c8937dba9a8da7eb984 Mon Sep 17 00:00:00 2001 From: David Vierra Date: Fri, 17 Dec 2010 08:21:59 -1000 Subject: [PATCH] added bounding box intersection func, to find the intersecting region of two boxes --- box.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/box.py b/box.py index 01df695..82389da 100644 --- a/box.py +++ b/box.py @@ -104,6 +104,27 @@ class BoundingBox (object): def isChunkAligned(self): return (self.origin[0] & 0xf == 0) and (self.origin[2] & 0xf == 0) + def intersect(self, box): + """ return a box containing the area self and box have in common""" + newbox = BoundingBox() + + if self.minx > box.maxx or self.maxx < box.minx: return BoundingBox() + newbox.minx = max(self.minx, box.minx) + newbox.maxx = min(self.maxx, box.maxx) + + if self.miny > box.maxy or self.maxy < box.miny: return BoundingBox() + newbox.miny = max(self.miny, box.miny) + newbox.maxy = min(self.maxy, box.maxy) + if self.minz > box.maxz or self.maxz < box.minz: return BoundingBox() + newbox.minz = max(self.minz, box.minz) + newbox.maxz = min(self.maxz, box.maxz) + + + + print "Intersect of {0} and {1}: {2}".format(self, box, newbox) + return newbox + + def __contains__(self, pos): x,y,z = pos; if x=self.maxx: return False