File size: 5,195 Bytes
dbf8921 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | # Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import numpy as np
class AlignedBox2d:
"""
An 2D axis aligned box in floating point.
Assumptions:
* The origin is at top-left corner
* `right` and `bottom` are not inclusive in the region, i.e. the width and
height can be simply calculated by `right - left` and `bottom - top`.
"""
def __init__(self, left: float, top: float, right: float, bottom: float):
"""Initializes the bounding box given (left, top, right, bottom)"""
self._left: float = left
self._top: float = top
self._right: float = right
self._bottom: float = bottom
def __repr__(self):
return f"AlignedBox2d(left: {self._left}, top: {self._top}, right: {self._right}, bottom: {self._bottom})"
@property
def left(self) -> float:
"""Left of the aligned box on x-axis."""
return self._left
@property
def top(self) -> float:
"""Top of the aligned box on y-axis."""
return self._top
@property
def right(self) -> float:
"""Right of the aligned box on x-axis."""
return self._right
@property
def bottom(self) -> float:
"""Bottom of the aligned box on y-axis."""
return self._bottom
@property
def width(self) -> float:
"""Width of the aligned box.
Returns:
Width computed by right - left
"""
return self.right - self.left
@property
def height(self) -> float:
"""Height of the aligned box.
Returns:
Height computed by bottom - top
"""
return self.bottom - self.top
def pad(self, width: float, height: float) -> AlignedBox2d:
"""Pads the region by extending `width` and `height` on four sides.
Args:
width (float): length to pad on left and right sides
height (float): length to pad on top and bottom sides
Returns:
a new AlignedBox2d object with padded region
"""
return AlignedBox2d(
self.left - width,
self.top - height,
self.right + width,
self.bottom + height,
)
def array_ltrb(self) -> np.ndarray:
"""Converts the box into a float np.ndarray of shape (4,): (left, top, right, bottom).
Returns:
a float np.ndarray of shape (4,) representing (left, top, right, bottom)
"""
return np.array([self.left, self.top, self.right, self.bottom])
def array_ltwh(self) -> np.ndarray:
"""Converts the box into a float np.ndarray of shape (4,): (left, top, width, height).
Returns:
a float np.ndarray of shape (4,) representing (left, top, width, height)
"""
return np.array([self.left, self.top, self.width, self.height])
def int_array_ltrb(self) -> np.ndarray:
"""Converts the box into an int np.ndarray of shape (4,): (left, top, width, height).
Returns:
an int np.ndarray of shape (4,) representing (left, top, right, bottom)
"""
return self.array_ltrb().astype(int)
def int_array_ltwh(self) -> np.ndarray:
"""Converts the box into an int np.ndarray of shape (4,): (left, top, width, height).
Returns:
an int np.ndarray of shape (4,) representing (left, top, width, height)
"""
return self.array_ltwh().astype(int)
def round(self) -> AlignedBox2d:
"""Rounds the float values to int.
Returns:
a new AlignedBox2d object with rounded values (still float)
"""
return AlignedBox2d(
np.round(self.left),
np.round(self.top),
np.round(self.right),
np.round(self.bottom),
)
def clip(self, boundary: AlignedBox2d) -> AlignedBox2d:
"""Clips the region by the boundary
Args:
boundary (AlignedBox2d): boundary of box to be clipped
(boundary.left: minimum left / right value,
boundary.top: minimum top / bottom value,
boundary.right: maximum left / right value,
boundary.bottom: maximum top / bottom value)
Returns:
a new clipped AlignedBox2d object
"""
return AlignedBox2d(
min(max(self.left, boundary.left), boundary.right),
min(max(self.top, boundary.top), boundary.bottom),
min(max(self.right, boundary.left), boundary.right),
min(max(self.bottom, boundary.top), boundary.bottom),
)
|