Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented contains for BoundingBox containing other BoundingBox #2906

Merged
merged 8 commits into from
Mar 21, 2024
22 changes: 19 additions & 3 deletions openmc/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,25 @@ def __or__(self, other: BoundingBox) -> BoundingBox:
new |= other
return new

def __contains__(self, point):
"""Check whether or not a point is in the bounding box"""
return all(point > self.lower_left) and all(point < self.upper_right)
def __contains__(self, other):
"""Check whether or not a point or another bounding box is in the bounding box.

For another bounding box to be in the parent it must lie fully inside of it,
with no overlap.
MicahGale marked this conversation as resolved.
Show resolved Hide resolved

"""
# test for a single point
if isinstance(other, (tuple, list, np.ndarray)):
point = other
check_length("Point", point, 3, 3)
return all(point > self.lower_left) and all(point < self.upper_right)
elif isinstance(other, BoundingBox):
return all([p in self for p in [other.lower_left, other.upper_right]])
else:
raise TypeError(
f"Unable to test if {other} is in the bounding box."
MicahGale marked this conversation as resolved.
Show resolved Hide resolved
f" Expected a tuple or a bounding box, but {type(other)} given"
)

@property
def center(self) -> np.ndarray:
Expand Down
26 changes: 25 additions & 1 deletion tests/unit_tests/test_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_bounding_upper_right(bb, expected):


@pytest.mark.parametrize(
"bb, expected",
"bb, expected",
MicahGale marked this conversation as resolved.
Show resolved Hide resolved
[
(test_bb_1, np.array([-4.5, -9.0, -13.5])),
(test_bb_2, np.array([6.0, 12.0, 18.0])),
Expand Down Expand Up @@ -156,3 +156,27 @@ def test_bounding_box_methods():

assert all(test_bb[0] == [-50.1, -50.1, -12.1])
assert all(test_bb[1] == [50.1, 14.1, 50.1])

@pytest.mark.parametrize(
"bb, other, expected",
[
(test_bb_1, (0, 0, 0), True),
(test_bb_2, (3, 3, 3), False),
(test_bb_1, test_bb_2, False),
(test_bb_1, openmc.BoundingBox((-9, -19, -29), (0, 0, 0)), True)
],
)
def test_bounding_box_contains(bb, other, expected):
assert (other in bb) == expected

@pytest.mark.parametrize(
"invalid, ex",
[
((1,0), ValueError),
MicahGale marked this conversation as resolved.
Show resolved Hide resolved
((1,2,3,4), ValueError),
MicahGale marked this conversation as resolved.
Show resolved Hide resolved
("foo", TypeError),
]
)
def test_bounding_box_contains_checking(invalid, ex):
with pytest.raises(ex):
invalid in test_bb_1
Loading