Skip to content

Commit d30ab4d

Browse files
Add calculation of a dataset's whole bounding box to Dataset (#975)
* add method to Dataset that calculates the whole dataset's bounding box. * Add test, doc string and changelog entry * format code
1 parent 8a2289a commit d30ab4d

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
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
### Breaking Changes
1616

1717
### Added
18+
- Added a method to the Datasets class that calculates a dataset's bounding box covering all layers. [#975](https://github.com/scalableminds/webknossos-libs/pull/975)
1819

1920
### Changed
2021

webknossos/tests/dataset/test_dataset.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,25 @@ def test_changing_layer_bounding_box(
10521052
assure_exported_properties(ds)
10531053

10541054

1055+
@pytest.mark.parametrize("data_format,output_path", DATA_FORMATS_AND_OUTPUT_PATHS)
1056+
def test_dataset_bounding_box_calculation(
1057+
data_format: DataFormat, output_path: Path
1058+
) -> None:
1059+
ds_path = copy_simple_dataset(data_format, output_path, "change_layer_bounding_box")
1060+
ds = Dataset.open(ds_path)
1061+
layer = ds.get_layer("color")
1062+
# BoundingBox(topleft=(0, 0, 0), size=(24, 24, 24))
1063+
assert layer.bounding_box == ds.calculate_bounding_box(), (
1064+
"The calculated bounding box of the dataset does not "
1065+
+ "match the color layer's bounding box."
1066+
)
1067+
layer.bounding_box = layer.bounding_box.with_size((512, 512, 512))
1068+
assert layer.bounding_box == ds.calculate_bounding_box(), (
1069+
"The calculated bounding box of the dataset does not "
1070+
+ "match the color layer's enlarged bounding box."
1071+
)
1072+
1073+
10551074
def test_get_view() -> None:
10561075
ds_path = prepare_dataset_path(DataFormat.WKW, TESTOUTPUT_DIR, "get_view")
10571076
ds = Dataset(ds_path, voxel_size=(1, 1, 1))

webknossos/webknossos/dataset/dataset.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,19 @@ def add_fs_copy_layer(
14841484
self._export_as_json()
14851485
return self.layers[new_layer_name]
14861486

1487+
def calculate_bounding_box(self) -> BoundingBox:
1488+
"""
1489+
Calculates and returns the enclosing bounding box of all data layers of the dataset.
1490+
"""
1491+
all_layers = list(self.layers.values())
1492+
if len(all_layers) <= 0:
1493+
return BoundingBox.empty()
1494+
dataset_bbox = all_layers[0].bounding_box
1495+
for layer in all_layers[1:]:
1496+
bbox = layer.bounding_box
1497+
dataset_bbox = dataset_bbox.extended_by(bbox)
1498+
return dataset_bbox
1499+
14871500
def copy_dataset(
14881501
self,
14891502
new_dataset_path: Union[str, Path],

0 commit comments

Comments
 (0)