Skip to content

Commit

Permalink
BoundingBox: add area/volume attributes (#375)
Browse files Browse the repository at this point in the history
* BoundingBox: add volume attribute

* Style fixes

* BoundingBox: add area attribute
  • Loading branch information
adamjstewart authored Feb 10, 2022
1 parent fa6e4e1 commit 9e0e30a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
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

0 comments on commit 9e0e30a

Please sign in to comment.