Skip to content

Commit b5dee90

Browse files
authored
Assert that bounding box and mag are aligned (#211)
* Assert that bounding box and mag are aligned * Add tests * Remove unused imports * Run black * Merge branch 'master' into fix-in-mag
1 parent 263b452 commit b5dee90

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

tests/test_bounding_box.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from wkcuber.api.bounding_box import BoundingBox, Mag
2+
import pytest
3+
4+
5+
def test_align_with_mag():
6+
7+
assert BoundingBox((1, 1, 1), (10, 10, 10)).align_with_mag(Mag(2)) == BoundingBox(
8+
topleft=(0, 0, 0), size=(12, 12, 12)
9+
)
10+
assert BoundingBox((1, 1, 1), (9, 9, 9)).align_with_mag(Mag(2)) == BoundingBox(
11+
topleft=(0, 0, 0), size=(10, 10, 10)
12+
)
13+
assert BoundingBox((1, 1, 1), (9, 9, 9)).align_with_mag(Mag(4)) == BoundingBox(
14+
topleft=(0, 0, 0), size=(12, 12, 12)
15+
)
16+
assert BoundingBox((1, 2, 3), (9, 9, 9)).align_with_mag(Mag(2)) == BoundingBox(
17+
topleft=(0, 2, 2), size=(10, 10, 10)
18+
)
19+
20+
21+
def test_in_mag():
22+
23+
with pytest.raises(AssertionError):
24+
BoundingBox((1, 2, 3), (9, 9, 9)).in_mag(Mag(2))
25+
26+
with pytest.raises(AssertionError):
27+
BoundingBox((2, 2, 2), (9, 9, 9)).in_mag(Mag(2))
28+
29+
assert BoundingBox((2, 2, 2), (10, 10, 10)).in_mag(Mag(2)) == BoundingBox(
30+
topleft=(1, 1, 1), size=(5, 5, 5)
31+
)

wkcuber/api/bounding_box.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,30 @@ def is_empty(self) -> bool:
185185

186186
return not all(self.size > 0)
187187

188-
def in_mag(self, mag: Mag, ceil: bool = False) -> "BoundingBox":
188+
def in_mag(self, mag: Mag) -> "BoundingBox":
189189

190190
np_mag = np.array(mag.to_array())
191191

192-
def ceil_maybe(array: np.ndarray) -> np.ndarray:
193-
if ceil:
194-
return np.ceil(array)
195-
return array
192+
assert (
193+
np.count_nonzero(self.topleft % np_mag) == 0
194+
), f"topleft {self.topleft} is not aligned with the mag {mag}. Use BoundingBox.align_with_mag()."
195+
assert (
196+
np.count_nonzero(self.bottomright % np_mag) == 0
197+
), f"bottomright {self.bottomright} is not aligned with the mag {mag}. Use BoundingBox.align_with_mag()."
196198

197199
return BoundingBox(
198-
topleft=ceil_maybe(self.topleft / np_mag).astype(np.int),
199-
size=ceil_maybe(self.size / np_mag).astype(np.int),
200+
topleft=(self.topleft // np_mag).astype(np.int),
201+
size=(self.size // np_mag).astype(np.int),
200202
)
201203

204+
def align_with_mag(self, mag: Mag):
205+
"""Rounds the bounding box up, so that both topleft and bottomright are divisible by mag."""
206+
207+
np_mag = np.array(mag.to_array())
208+
topleft = (self.topleft // np_mag).astype(np.int) * np_mag
209+
bottomright = np.ceil(self.bottomright / np_mag).astype(np.int) * np_mag
210+
return BoundingBox(topleft, bottomright - topleft)
211+
202212
def contains(self, coord: Shape3D) -> bool:
203213

204214
coord = np.array(coord)

0 commit comments

Comments
 (0)