Skip to content

Commit dcadb9b

Browse files
authored
added option for unique/non-unique returns (#16)
1 parent b07ee9e commit dcadb9b

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

aabbtree.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def does_overlap(self, aabb, method='DFS', closed=False):
473473

474474
return len(_overlap_pairs(self, aabb, method, True, closed)) > 0
475475

476-
def overlap_aabbs(self, aabb, method='DFS', closed=False):
476+
def overlap_aabbs(self, aabb, method='DFS', closed=False, unique=True):
477477
"""Get overlapping AABBs
478478
479479
This function gets each overlapping AABB.
@@ -491,17 +491,19 @@ def overlap_aabbs(self, aabb, method='DFS', closed=False):
491491
closed (bool): Option to specify closed or open box intersection.
492492
If open, there must be a non-zero amount of overlap. If closed,
493493
boxes can be touching.
494+
unique (bool): Return only unique pairs. Defaults to True.
494495
495496
Returns:
496497
list: AABB objects in AABBTree that overlap with the input.
497498
"""
498-
pairs = _overlap_pairs(self, aabb, method, closed=closed)
499+
pairs = _overlap_pairs(self, aabb, method, closed=closed,
500+
unique=unique)
499501
if len(pairs) == 0:
500502
return []
501503
boxes, _ = zip(*pairs)
502504
return list(boxes)
503505

504-
def overlap_values(self, aabb, method='DFS', closed=False):
506+
def overlap_values(self, aabb, method='DFS', closed=False, unique=True):
505507
"""Get values of overlapping AABBs
506508
507509
This function gets the value field of each overlapping AABB.
@@ -519,11 +521,13 @@ def overlap_values(self, aabb, method='DFS', closed=False):
519521
closed (bool): Option to specify closed or open box intersection.
520522
If open, there must be a non-zero amount of overlap. If closed,
521523
boxes can be touching.
524+
unique (bool): Return only unique pairs. Defaults to True.
522525
523526
Returns:
524527
list: Value fields of each node that overlaps.
525528
"""
526-
pairs = _overlap_pairs(self, aabb, method, closed=closed)
529+
pairs = _overlap_pairs(self, aabb, method, closed=closed,
530+
unique=unique)
527531
if len(pairs) == 0:
528532
return []
529533
_, values = zip(*pairs)
@@ -537,7 +541,8 @@ def _merge(lims1, lims2):
537541
return (lower, upper)
538542

539543

540-
def _overlap_pairs(in_tree, aabb, method='DFS', halt=False, closed=False):
544+
def _overlap_pairs(in_tree, aabb, method='DFS', halt=False, closed=False,
545+
unique=True):
541546
"""Get overlapping AABBs and values in (AABB, value) pairs
542547
543548
*New in version 2.6.0*
@@ -553,6 +558,7 @@ def _overlap_pairs(in_tree, aabb, method='DFS', halt=False, closed=False):
553558
halt (bool): Return the list immediately once a pair has been
554559
added.
555560
closed (bool): Check for closed box intersection. Defaults to False.
561+
unique (bool): Return only unique pairs. Defaults to True.
556562
557563
Returns:
558564
list: (AABB, value) pairs in AABBTree that overlap with the input.
@@ -571,7 +577,7 @@ def _overlap_pairs(in_tree, aabb, method='DFS', halt=False, closed=False):
571577
e_str = "method should be 'DFS' or 'BFS', not " + str(method)
572578
raise ValueError(e_str)
573579

574-
if len(pairs) < 2:
580+
if len(pairs) < 2 or not unique:
575581
return pairs
576582
return _unique_pairs(pairs)
577583

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def read(fname):
1515

1616
setup(
1717
name='aabbtree',
18-
version='2.7.0',
18+
version='2.8.0',
1919
license='MIT',
2020
description='Pure Python implementation of d-dimensional AABB tree.',
2121
long_description=read('README.rst'),

tests/test_aabbtree.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,22 @@ def test_depth():
262262
assert standard_tree().depth == 2
263263

264264

265+
def test_unique():
266+
tree = AABBTree()
267+
aabb1 = AABB([(0, 1)])
268+
aabb2 = AABB([(0, 1)])
269+
aabb3 = AABB([(0, 1)])
270+
tree.add(aabb1, 'box 1')
271+
tree.add(aabb2, 'box 2')
272+
vals = tree.overlap_values(aabb3, unique=True)
273+
assert len(vals) == 1
274+
275+
vals = tree.overlap_values(aabb3, unique=False)
276+
assert len(vals) == 2
277+
assert 'box 1' in vals
278+
assert 'box 2' in vals
279+
280+
265281
def standard_aabbs():
266282
aabb1 = AABB([(0, 1), (0, 1)])
267283
aabb2 = AABB([(3, 4), (0, 1)])

0 commit comments

Comments
 (0)