| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
from Metadata import export, constmethod |
| 4 |
from PyObjectBase import PyObjectBase |
| 5 |
from Vector import Vector |
| 6 |
from Matrix import Matrix |
| 7 |
from typing import overload, Any, Final, Tuple, Union |
| 8 |
|
| 9 |
@export( |
| 10 |
TwinPointer="BoundBox3d", |
| 11 |
Constructor=True, |
| 12 |
Delete=True, |
| 13 |
) |
| 14 |
class BoundBox(PyObjectBase): |
| 15 |
""" |
| 16 |
Base.BoundBox class. |
| 17 |
|
| 18 |
This class represents a bounding box. |
| 19 |
A bounding box is a rectangular cuboid which is a way to describe outer |
| 20 |
boundaries and is obtained from a lot of 3D types. |
| 21 |
It is often used to check if a 3D entity lies in the range of another object. |
| 22 |
Checking for bounding interference first can save a lot of computing time! |
| 23 |
An invalid BoundBox is represented by inconsistent values at each direction: |
| 24 |
The maximum float value of the system at the minimum coordinates, and the |
| 25 |
opposite value at the maximum coordinates. |
| 26 |
|
| 27 |
The following constructors are supported: |
| 28 |
|
| 29 |
BoundBox() |
| 30 |
Empty constructor. Returns an invalid BoundBox. |
| 31 |
|
| 32 |
BoundBox(boundBox) |
| 33 |
Copy constructor. |
| 34 |
boundBox : Base.BoundBox |
| 35 |
|
| 36 |
BoundBox(xMin, yMin=0, zMin=0, xMax=0, yMax=0, zMax=0) |
| 37 |
Define from the minimum and maximum values at each direction. |
| 38 |
xMin : float |
| 39 |
Minimum value at x-coordinate. |
| 40 |
yMin : float |
| 41 |
Minimum value at y-coordinate. |
| 42 |
zMin : float |
| 43 |
Minimum value at z-coordinate. |
| 44 |
xMax : float |
| 45 |
Maximum value at x-coordinate. |
| 46 |
yMax : float |
| 47 |
Maximum value at y-coordinate. |
| 48 |
zMax : float |
| 49 |
Maximum value at z-coordinate. |
| 50 |
|
| 51 |
App.BoundBox(min, max) |
| 52 |
Define from two containers representing the minimum and maximum values of the |
| 53 |
coordinates in each direction. |
| 54 |
min : Base.Vector, tuple |
| 55 |
Minimum values of the coordinates. |
| 56 |
max : Base.Vector, tuple |
| 57 |
Maximum values of the coordinates. |
| 58 |
|
| 59 |
Author: Juergen Riegel ([email protected]) |
| 60 |
Licence: LGPL |
| 61 |
""" |
| 62 |
|
| 63 |
Center: Final[Any] = None |
| 64 |
"""Center point of the bounding box.""" |
| 65 |
|
| 66 |
XMax: float = 0.0 |
| 67 |
"""The maximum x boundary position.""" |
| 68 |
|
| 69 |
YMax: float = 0.0 |
| 70 |
"""The maximum y boundary position.""" |
| 71 |
|
| 72 |
ZMax: float = 0.0 |
| 73 |
"""The maximum z boundary position.""" |
| 74 |
|
| 75 |
XMin: float = 0.0 |
| 76 |
"""The minimum x boundary position.""" |
| 77 |
|
| 78 |
YMin: float = 0.0 |
| 79 |
"""The minimum y boundary position.""" |
| 80 |
|
| 81 |
ZMin: float = 0.0 |
| 82 |
"""The minimum z boundary position.""" |
| 83 |
|
| 84 |
XLength: Final[float] = 0.0 |
| 85 |
"""Length of the bounding box in x direction.""" |
| 86 |
|
| 87 |
YLength: Final[float] = 0.0 |
| 88 |
"""Length of the bounding box in y direction.""" |
| 89 |
|
| 90 |
ZLength: Final[float] = 0.0 |
| 91 |
"""Length of the bounding box in z direction.""" |
| 92 |
|
| 93 |
DiagonalLength: Final[float] = 0.0 |
| 94 |
"""Diagonal length of the bounding box.""" |
| 95 |
|
| 96 |
# fmt: off |
| 97 |
@overload |
| 98 |
def __init__(self) -> None: ... |
| 99 |
@overload |
| 100 |
def __init__(self, boundBox: "BoundBox") -> None: ... |
| 101 |
@overload |
| 102 |
def __init__( |
| 103 |
self, |
| 104 |
xMin: float, |
| 105 |
yMin: float = 0, |
| 106 |
zMin: float = 0, |
| 107 |
xMax: float = 0, |
| 108 |
yMax: float = 0, |
| 109 |
zMax: float = 0, |
| 110 |
) -> None: ... |
| 111 |
@overload |
| 112 |
def __init__( |
| 113 |
self, |
| 114 |
min: Union[Vector, Tuple[float, float, float]], |
| 115 |
max: Union[Vector, Tuple[float, float, float]], |
| 116 |
) -> None: ... |
| 117 |
# fmt: on |
| 118 |
|
| 119 |
def setVoid(self) -> None: |
| 120 |
""" |
| 121 |
setVoid() -> None |
| 122 |
|
| 123 |
Invalidate the bounding box. |
| 124 |
""" |
| 125 |
... |
| 126 |
|
| 127 |
@constmethod |
| 128 |
def isValid(self) -> bool: |
| 129 |
""" |
| 130 |
isValid() -> bool |
| 131 |
|
| 132 |
Checks if the bounding box is valid. |
| 133 |
""" |
| 134 |
... |
| 135 |
|
| 136 |
@overload |
| 137 |
def add(self, minMax: Vector) -> None: ... |
| 138 |
@overload |
| 139 |
def add(self, minMax: Tuple[float, float, float]) -> None: ... |
| 140 |
@overload |
| 141 |
def add(self, x: float, y: float, z: float) -> None: ... |
| 142 |
def add(self, *args: Any, **kwargs: Any) -> None: |
| 143 |
""" |
| 144 |
add(minMax) -> None |
| 145 |
add(x, y, z) -> None |
| 146 |
|
| 147 |
Increase the maximum values or decrease the minimum values of this BoundBox by |
| 148 |
replacing the current values with the given values, so the bounding box can grow |
| 149 |
but not shrink. |
| 150 |
|
| 151 |
minMax : Base.Vector, tuple |
| 152 |
Values to enlarge at each direction. |
| 153 |
x : float |
| 154 |
Value to enlarge at x-direction. |
| 155 |
y : float |
| 156 |
Value to enlarge at y-direction. |
| 157 |
z : float |
| 158 |
Value to enlarge at z-direction. |
| 159 |
""" |
| 160 |
... |
| 161 |
|
| 162 |
@constmethod |
| 163 |
def getPoint(self, index: int) -> Vector: |
| 164 |
""" |
| 165 |
getPoint(index) -> Base.Vector |
| 166 |
|
| 167 |
Get the point of the given index. |
| 168 |
The index must be in the range of [0, 7]. |
| 169 |
|
| 170 |
index : int |
| 171 |
""" |
| 172 |
... |
| 173 |
|
| 174 |
@constmethod |
| 175 |
def getEdge(self, index: int) -> Tuple[Vector, ...]: |
| 176 |
""" |
| 177 |
getEdge(index) -> tuple of Base.Vector |
| 178 |
|
| 179 |
Get the edge points of the given index. |
| 180 |
The index must be in the range of [0, 11]. |
| 181 |
|
| 182 |
index : int |
| 183 |
""" |
| 184 |
... |
| 185 |
|
| 186 |
@overload |
| 187 |
def closestPoint(self, point: Vector) -> Vector: ... |
| 188 |
@overload |
| 189 |
def closestPoint(self, x: float, y: float, z: float) -> Vector: ... |
| 190 |
@constmethod |
| 191 |
def closestPoint(self, *args: Any, **kwargs: Any) -> Vector: |
| 192 |
""" |
| 193 |
closestPoint(point) -> Base.Vector |
| 194 |
closestPoint(x, y, z) -> Base.Vector |
| 195 |
|
| 196 |
Get the closest point of the bounding box to the given point. |
| 197 |
|
| 198 |
point : Base.Vector, tuple |
| 199 |
Coordinates of the given point. |
| 200 |
x : float |
| 201 |
X-coordinate of the given point. |
| 202 |
y : float |
| 203 |
Y-coordinate of the given point. |
| 204 |
z : float |
| 205 |
Z-coordinate of the given point. |
| 206 |
""" |
| 207 |
... |
| 208 |
|
| 209 |
@overload |
| 210 |
def intersect(self, boundBox2: "BoundBox") -> bool: ... |
| 211 |
@overload |
| 212 |
def intersect( |
| 213 |
self, |
| 214 |
base: Union[Vector, Tuple[float, float, float]], |
| 215 |
dir: Union[Vector, Tuple[float, float, float]], |
| 216 |
) -> bool: ... |
| 217 |
def intersect(self, *args: Any) -> bool: |
| 218 |
""" |
| 219 |
intersect(boundBox2) -> bool |
| 220 |
intersect(base, dir) -> bool |
| 221 |
|
| 222 |
Checks if the given object intersects with this bounding box. That can be |
| 223 |
another bounding box or a line specified by base and direction. |
| 224 |
|
| 225 |
boundBox2 : Base.BoundBox |
| 226 |
base : Base.Vector, tuple |
| 227 |
dir : Base.Vector, tuple |
| 228 |
""" |
| 229 |
... |
| 230 |
|
| 231 |
def intersected(self, boundBox2: "BoundBox") -> "BoundBox": |
| 232 |
""" |
| 233 |
intersected(boundBox2) -> Base.BoundBox |
| 234 |
|
| 235 |
Returns the intersection of this and the given bounding box. |
| 236 |
|
| 237 |
boundBox2 : Base.BoundBox |
| 238 |
""" |
| 239 |
... |
| 240 |
|
| 241 |
def united(self, boundBox2: "BoundBox") -> "BoundBox": |
| 242 |
""" |
| 243 |
united(boundBox2) -> Base.BoundBox |
| 244 |
|
| 245 |
Returns the union of this and the given bounding box. |
| 246 |
|
| 247 |
boundBox2 : Base.BoundBox |
| 248 |
""" |
| 249 |
... |
| 250 |
|
| 251 |
def enlarge(self, variation: float) -> None: |
| 252 |
""" |
| 253 |
enlarge(variation) -> None |
| 254 |
|
| 255 |
Decrease the minimum values and increase the maximum values by the given value. |
| 256 |
A negative value shrinks the bounding box. |
| 257 |
|
| 258 |
variation : float |
| 259 |
""" |
| 260 |
... |
| 261 |
|
| 262 |
def getIntersectionPoint(self, base: Vector, dir: Vector, epsilon: float = 0.0001) -> Vector: |
| 263 |
""" |
| 264 |
getIntersectionPoint(base, dir, epsilon=0.0001) -> Base.Vector |
| 265 |
|
| 266 |
Calculate the intersection point of a line with the bounding box. |
| 267 |
The base point must lie inside the bounding box, if not an exception is thrown. |
| 268 |
|
| 269 |
base : Base.Vector |
| 270 |
Base point of the line. |
| 271 |
dir : Base.Vector |
| 272 |
Direction of the line. |
| 273 |
epsilon : float |
| 274 |
Bounding box size tolerance. |
| 275 |
""" |
| 276 |
... |
| 277 |
|
| 278 |
@overload |
| 279 |
def move(self, displacement: Vector) -> None: ... |
| 280 |
@overload |
| 281 |
def move(self, displacement: Tuple[float, float, float]) -> None: ... |
| 282 |
@overload |
| 283 |
def move(self, x: float, y: float, z: float) -> None: ... |
| 284 |
def move(self, *args: Any, **kwargs: Any) -> None: |
| 285 |
""" |
| 286 |
move(displacement) -> None |
| 287 |
move(x, y, z) -> None |
| 288 |
|
| 289 |
Move the bounding box by the given values. |
| 290 |
|
| 291 |
displacement : Base.Vector, tuple |
| 292 |
Displacement at each direction. |
| 293 |
x : float |
| 294 |
Displacement at x-direction. |
| 295 |
y : float |
| 296 |
Displacement at y-direction. |
| 297 |
z : float |
| 298 |
Displacement at z-direction. |
| 299 |
""" |
| 300 |
... |
| 301 |
|
| 302 |
@overload |
| 303 |
def scale(self, factor: Vector) -> None: ... |
| 304 |
@overload |
| 305 |
def scale(self, factor: Tuple[float, float, float]) -> None: ... |
| 306 |
@overload |
| 307 |
def scale(self, x: float, y: float, z: float) -> None: ... |
| 308 |
def scale(self, *args: Any, **kwargs: Any) -> None: |
| 309 |
""" |
| 310 |
scale(factor) -> None |
| 311 |
scale(x, y, z) -> None |
| 312 |
|
| 313 |
Scale the bounding box by the given values. |
| 314 |
|
| 315 |
factor : Base.Vector, tuple |
| 316 |
Factor scale at each direction. |
| 317 |
x : float |
| 318 |
Scale at x-direction. |
| 319 |
y : float |
| 320 |
Scale at y-direction. |
| 321 |
z : float |
| 322 |
Scale at z-direction. |
| 323 |
""" |
| 324 |
... |
| 325 |
|
| 326 |
def transformed(self, matrix: Matrix) -> "BoundBox": |
| 327 |
""" |
| 328 |
transformed(matrix) -> Base.BoundBox |
| 329 |
|
| 330 |
Returns a new BoundBox containing the transformed rectangular cuboid |
| 331 |
represented by this BoundBox. |
| 332 |
|
| 333 |
matrix : Base.Matrix |
| 334 |
Transformation matrix. |
| 335 |
""" |
| 336 |
... |
| 337 |
|
| 338 |
def isCutPlane(self, base: Vector, normal: Vector) -> bool: |
| 339 |
""" |
| 340 |
isCutPlane(base, normal) -> bool |
| 341 |
|
| 342 |
Check if the plane specified by base and normal intersects (cuts) this bounding |
| 343 |
box. |
| 344 |
|
| 345 |
base : Base.Vector |
| 346 |
normal : Base.Vector |
| 347 |
""" |
| 348 |
... |
| 349 |
|
| 350 |
@overload |
| 351 |
def isInside(self, object: Vector) -> bool: ... |
| 352 |
@overload |
| 353 |
def isInside(self, object: "BoundBox") -> bool: ... |
| 354 |
@overload |
| 355 |
def isInside(self, x: float, y: float, z: float) -> bool: ... |
| 356 |
def isInside(self, *args: Any) -> bool: |
| 357 |
""" |
| 358 |
isInside(object) -> bool |
| 359 |
isInside(x, y, z) -> bool |
| 360 |
|
| 361 |
Check if a point or a bounding box is inside this bounding box. |
| 362 |
|
| 363 |
object : Base.Vector, Base.BoundBox |
| 364 |
Object to check if it is inside this bounding box. |
| 365 |
x : float |
| 366 |
X-coordinate of the point to check. |
| 367 |
y : float |
| 368 |
Y-coordinate of the point to check. |
| 369 |
z : float |
| 370 |
Z-coordinate of the point to check. |
| 371 |
""" |
| 372 |
... |