Skip to content

Commit 175dac6

Browse files
Floored version of align_with_mag() (#217)
* Floored version of align_with_mag() * Format * Update wkcuber/api/bounding_box.py Co-authored-by: Philipp Otto <[email protected]> * Update wkcuber/api/bounding_box.py Co-authored-by: Jonathan Striebel <[email protected]> * Fix lambda
1 parent b5dee90 commit 175dac6

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

tests/test_bounding_box.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@
22
import pytest
33

44

5-
def test_align_with_mag():
5+
def test_align_with_mag_ceiled():
6+
7+
assert BoundingBox((1, 1, 1), (10, 10, 10)).align_with_mag(
8+
Mag(2), ceil=True
9+
) == BoundingBox(topleft=(0, 0, 0), size=(12, 12, 12))
10+
assert BoundingBox((1, 1, 1), (9, 9, 9)).align_with_mag(
11+
Mag(2), ceil=True
12+
) == BoundingBox(topleft=(0, 0, 0), size=(10, 10, 10))
13+
assert BoundingBox((1, 1, 1), (9, 9, 9)).align_with_mag(
14+
Mag(4), ceil=True
15+
) == BoundingBox(topleft=(0, 0, 0), size=(12, 12, 12))
16+
assert BoundingBox((1, 2, 3), (9, 9, 9)).align_with_mag(
17+
Mag(2), ceil=True
18+
) == BoundingBox(topleft=(0, 2, 2), size=(10, 10, 10))
19+
20+
21+
def test_align_with_mag_floored():
622

723
assert BoundingBox((1, 1, 1), (10, 10, 10)).align_with_mag(Mag(2)) == BoundingBox(
8-
topleft=(0, 0, 0), size=(12, 12, 12)
24+
topleft=(2, 2, 2), size=(8, 8, 8)
925
)
1026
assert BoundingBox((1, 1, 1), (9, 9, 9)).align_with_mag(Mag(2)) == BoundingBox(
11-
topleft=(0, 0, 0), size=(10, 10, 10)
27+
topleft=(2, 2, 2), size=(8, 8, 8)
1228
)
1329
assert BoundingBox((1, 1, 1), (9, 9, 9)).align_with_mag(Mag(4)) == BoundingBox(
14-
topleft=(0, 0, 0), size=(12, 12, 12)
30+
topleft=(4, 4, 4), size=(4, 4, 4)
1531
)
1632
assert BoundingBox((1, 2, 3), (9, 9, 9)).align_with_mag(Mag(2)) == BoundingBox(
17-
topleft=(0, 2, 2), size=(10, 10, 10)
33+
topleft=(2, 2, 4), size=(8, 8, 8)
1834
)
1935

2036

wkcuber/api/bounding_box.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,22 @@ def in_mag(self, mag: Mag) -> "BoundingBox":
201201
size=(self.size // np_mag).astype(np.int),
202202
)
203203

204-
def align_with_mag(self, mag: Mag):
205-
"""Rounds the bounding box up, so that both topleft and bottomright are divisible by mag."""
204+
def align_with_mag(self, mag: Mag, ceil=False):
205+
"""Rounds the bounding box, so that both topleft and bottomright are divisible by mag.
206+
207+
:argument ceil: If true, the bounding box is enlarged when necessary. If false, it's shrinked when necessary.
208+
"""
206209

207210
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
211+
212+
align = lambda point, round_fn: round_fn(point / np_mag).astype(np.int) * np_mag
213+
214+
if ceil:
215+
topleft = align(self.topleft, np.floor)
216+
bottomright = align(self.bottomright, np.ceil)
217+
else:
218+
topleft = align(self.topleft, np.ceil)
219+
bottomright = align(self.bottomright, np.floor)
210220
return BoundingBox(topleft, bottomright - topleft)
211221

212222
def contains(self, coord: Shape3D) -> bool:

0 commit comments

Comments
 (0)