UnionBox now accepts any number of component selections

This commit is contained in:
David Vierra 2016-01-31 20:01:10 -10:00
parent fa1ddb4bb1
commit f82c7a5ea8

View File

@ -163,26 +163,28 @@ class CombinationBox(SelectionBox):
oper = NotImplemented oper = NotImplemented
boundsminoper = NotImplemented boundsminoper = NotImplemented
boundsmaxoper = NotImplemented boundsmaxoper = NotImplemented
box_mask = NotImplemented
def __init__(self, left, right): def __init__(self, *selections):
self.left = left self.selections = selections
self.right = right self.mincx = self.boundsminoper(s.mincx for s in selections)
self.mincx = self.boundsminoper(left.mincx, right.mincx) self.mincy = self.boundsminoper(s.mincy for s in selections)
self.mincy = self.boundsminoper(left.mincy, right.mincy) self.mincz = self.boundsminoper(s.mincz for s in selections)
self.mincz = self.boundsminoper(left.mincz, right.mincz) self.maxcx = self.boundsmaxoper(s.maxcx for s in selections)
self.maxcx = self.boundsmaxoper(left.maxcx, right.maxcx) self.maxcy = self.boundsmaxoper(s.maxcy for s in selections)
self.maxcy = self.boundsmaxoper(left.maxcy, right.maxcy) self.maxcz = self.boundsmaxoper(s.maxcz for s in selections)
self.maxcz = self.boundsmaxoper(left.maxcz, right.maxcz)
def __contains__(self, item): def __contains__(self, item):
return self.oper(item in self.left, item in self.right) return self.oper(item in s for s in self.selections)
def sectionPositions(self, cx, cz): def sectionPositions(self, cx, cz):
left = self.left.sectionPositions(cx, cz) positionLists = [set(s.sectionPositions(cx, cz)) for s in self.selections]
right = self.right.sectionPositions(cx, cz) if len(positionLists) == 0:
left = set(left) return []
right = set(right) if len(positionLists) == 1:
return self.oper(left, right) return positionLists[0]
return reduce(self.oper, positionLists)
class UnionBox(CombinationBox): class UnionBox(CombinationBox):
oper = operator.or_ oper = operator.or_
@ -197,16 +199,17 @@ class UnionBox(CombinationBox):
return self.oper(left, right) return self.oper(left, right)
def box_mask(self, box): def box_mask(self, box):
left = self.left.box_mask(box) masks = [s.box_mask(box) for s in self.selections]
right = self.right.box_mask(box) masks = [m for m in masks if m is not None]
if left is None and right is None: if not len(masks):
return None return None
if left is None:
return right
if right is None:
return left
return left | right m = masks.pop()
while len(masks):
numpy.logical_or(m, masks.pop(), m)
return m
class IntersectionBox(CombinationBox): class IntersectionBox(CombinationBox):
oper = operator.and_ oper = operator.and_