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

BoundingBox: add area/volume attributes #375

Merged
merged 3 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/datasets/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,46 @@ def test_and_no_intersection(
):
bbox1 & bbox2

@pytest.mark.parametrize(
"test_input,expected",
[
# Rectangular prism
((0, 1, 0, 1, 0, 1), 1),
((0, 2, 0, 3, 0, 4), 6),
# Plane
((0, 0, 0, 1, 0, 1), 0),
# Line
((0, 0, 0, 0, 0, 1), 0),
# Point
((0, 0, 0, 0, 0, 0), 0),
],
)
def test_area(
self, test_input: Tuple[float, float, float, float, float, float], expected: int
) -> None:
bbox = BoundingBox(*test_input)
assert bbox.area == expected

@pytest.mark.parametrize(
"test_input,expected",
[
# Rectangular prism
((0, 1, 0, 1, 0, 1), 1),
((0, 2, 0, 3, 0, 4), 24),
# Plane
((0, 0, 0, 1, 0, 1), 0),
# Line
((0, 0, 0, 0, 0, 1), 0),
# Point
((0, 0, 0, 0, 0, 0), 0),
],
)
def test_volume(
self, test_input: Tuple[float, float, float, float, float, float], expected: int
) -> None:
bbox = BoundingBox(*test_input)
assert bbox.volume == expected

@pytest.mark.parametrize(
"test_input,expected",
[
Expand Down
26 changes: 26 additions & 0 deletions torchgeo/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,32 @@ def __and__(self, other: "BoundingBox") -> "BoundingBox":
except ValueError:
raise ValueError(f"Bounding boxes {self} and {other} do not overlap")

@property
def area(self) -> float:
"""Area of bounding box.

Area is defined as spatial area.

Returns:
area

.. versionadded:: 0.3
"""
return (self.maxx - self.minx) * (self.maxy - self.miny)

@property
def volume(self) -> float:
"""Volume of bounding box.

Volume is defined as spatial area times temporal range.

Returns:
volume

.. versionadded:: 0.3
"""
return self.area * (self.maxt - self.mint)

def intersects(self, other: "BoundingBox") -> bool:
"""Whether or not two bounding boxes intersect.

Expand Down