Skip to content

Commit 7ea97ca

Browse files
authored
Allow float numpy array in BoundingBox.contains (#450)
* Allow float numpy array in BoundingBox.contains * changelog * add test for BoundingBox.contains
1 parent ad0f4b5 commit 7ea97ca

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

webknossos/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ For upgrade instructions, please check the respective *Breaking Changes* section
1515
### Changed
1616
- The `Vec3Int` constructor now asserts that its components are whole numbers also in numpy case. [#434](https://github.com/scalableminds/webknossos-libs/pull/434)
1717
- Updated scikit-image dependency to 0.18.3. [#435](https://github.com/scalableminds/webknossos-libs/pull/435)
18+
- `BoundingBox.contains` now also takes float points in numpy arrays. [#450](https://github.com/scalableminds/webknossos-libs/pull/450)
1819
### Fixed
1920

2021
## [0.8.16](https://github.com/scalableminds/webknossos-cuber/releases/tag/v0.8.16) - 2021-09-22

webknossos/tests/test_bounding_box.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import numpy as np
12
import pytest
23

34
from webknossos.geometry import BoundingBox, Mag
@@ -59,3 +60,17 @@ def test_with_bounds() -> None:
5960
assert BoundingBox((1, 2, 3), (5, 5, 5)).with_bounds_z(
6061
new_size_z=10
6162
) == BoundingBox((1, 2, 3), (5, 5, 10))
63+
64+
65+
def test_contains() -> None:
66+
67+
assert BoundingBox((1, 1, 1), (5, 5, 5)).contains((2, 2, 2))
68+
assert BoundingBox((1, 1, 1), (5, 5, 5)).contains((1, 1, 1))
69+
70+
# top-left is inclusive, bottom-right is exclusive
71+
assert not BoundingBox((1, 1, 1), (5, 5, 5)).contains((6, 6, 6))
72+
assert not BoundingBox((1, 1, 1), (5, 5, 5)).contains((20, 20, 20))
73+
74+
# nd-array may contain float values
75+
assert BoundingBox((1, 1, 1), (5, 5, 5)).contains(np.array([5.5, 5.5, 5.5]))
76+
assert not BoundingBox((1, 1, 1), (5, 5, 5)).contains(np.array([6.0, 6.0, 6.0]))

webknossos/webknossos/geometry/bounding_box.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,16 @@ def align_with_mag(self, mag: Mag, ceil: bool = False) -> "BoundingBox":
300300
bottomright = align(self.bottomright, np.floor)
301301
return BoundingBox(topleft, bottomright - topleft)
302302

303-
def contains(self, coord: Vec3IntLike) -> bool:
304-
305-
coord = Vec3Int(coord).to_np()
303+
def contains(self, coord: Union[Vec3IntLike, np.ndarray]) -> bool:
304+
"""Check whether a point is inside of the bounding box.
305+
Note that the point may have float coordinates in the ndarray case"""
306+
307+
if isinstance(coord, np.ndarray):
308+
assert coord.shape == (
309+
3,
310+
), f"Numpy array BoundingBox.contains must have shape (3,), got {coord.shape}."
311+
else:
312+
coord = Vec3Int(coord).to_np()
306313

307314
return cast(
308315
bool,

0 commit comments

Comments
 (0)