Box2D
SUMMARY
An axis-aligned bounding box in 2D space.
python
from telekinesis import datatypes
box2d = datatypes.Box2D([1, 2.5, 3, 3])Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
data | np.ndarray | list | tuple | Required | Box coordinates [cx, cy, width, height] (center point + size, the native CXCYWH format). |
Raises
| Exception | Condition |
|---|---|
TypeError | data can't be converted into a uniform float32 array (e.g. ragged nested lists) |
ValueError | data is not rank-1 (e.g. a (N, 4) batch — use Boxes2D instead) |
ValueError | data does not have exactly 4 elements |
ValueError | Any element is non-finite (NaN/Inf) |
ValueError | width or height (data[2], data[3]) is negative |
A zero-sized width/height is allowed but logs a warning (area will be 0).
Attributes
| Attribute | Type | Description |
|---|---|---|
data | np.ndarray | Defensive copy of the underlying (4,) array [cx, cy, width, height]. Reading it returns a copy; writing re-validates the same way as construction. |
shape | tuple[int, ...] | Always (4,). |
ndim | int | Always 1. |
dtype | np.dtype | Always float32. |
size | int | Always 4. |
shape_spec | tuple[int, ...] | Class-level shape spec (4,). |
center | np.ndarray | [cx, cy] — data[:2] directly. This is the native storage format, so no offset computation is needed to get the center. |
dimensions | np.ndarray | [width, height], data[2:4], non-negative. |
area | np.ndarray (scalar) | width * height. |
Methods
| Method | Type | Description |
|---|---|---|
Box2D.coerce(value) | Box2D | Converts array-like data into a Box2D. If value is already a Box2D, it is returned unchanged; otherwise it goes through the same checks as constructing one directly. |
Box2D.from_xywh(data) | Box2D | Builds a box from [x_min, y_min, width, height], converting it to the native center-based format. |
Box2D.from_xyxy(data) | Box2D | Builds a box from [x_min, y_min, x_max, y_max], converting it to the native center-based format. |
as_xywh() | np.ndarray | Returns this box as [x_min, y_min, width, height] (min corner + size) instead of the native center-based format. |
as_xyxy() | np.ndarray | Returns this box as [x_min, y_min, x_max, y_max] (corner-to-corner) instead of the native center-based format. |
to_numpy(copy=True) | np.ndarray | Returns the box as a plain array. Pass copy=False for a zero-copy view instead — mutating it mutates the Box2D. |
copy() | Box2D | Returns a new, independent Box2D with the same data. |
Operators
| Operation | Behavior |
|---|---|
box == other | True only if other is also a Box2D with element-equal data. False for anything else. |
len(box) | Returns 4 — the length of the coordinate vector, not a box count (a single box is conceptually one item). |
box[i] | Raises TypeError — Box2D defines no __getitem__, so a single box isn't indexable like a batch. Use .data[i] for raw coordinate access instead. |
for x in box | Raises TypeError — with no __getitem__/__iter__, a single Box2D isn't iterable. |
np.asarray(box) | Returns a copy of data as an np.ndarray; NumPy functions accept a Box2D directly. |
Visualization
python
import rerun as rr
# Your code block
# ....
rr.init("box2d_example", spawn=True)
datatypes.visualize(box2d, entity_path="/box2d", label="Box2D")Example
python
"""Demonstrates the Telekinesis Box2D datatype."""
import time
import numpy as np
import rerun as rr
from loguru import logger
from telekinesis import datatypes
def box2d_example():
"""Demonstrate creation, inspection, operations, visualization, and serialization."""
# ======================= Create ============================================
# Box2D format is CXCYWH = [cx, cy, width, height]
coords = [1, 2.5, 3, 3]
box2d = datatypes.Box2D(coords)
logger.info(f"Original Box2D: {box2d}")
xyxy_coords = [1.0, 1.5, 4.0, 4.5]
box2d_from_xyxy = datatypes.Box2D.from_xyxy(xyxy_coords)
logger.info(f"Box2D created from xyxy format: {box2d_from_xyxy}")
xywh_coords = [1.0, 1.5, 3.0, 3.0]
box2d_from_xywh = datatypes.Box2D.from_xywh(xywh_coords)
logger.info(f"Box2D created from xywh format: {box2d_from_xywh}")
# ======================= Inspect ===========================================
logger.info(f"data={box2d.data}")
logger.info(f"dtype={box2d.dtype}")
logger.info(f"ndim={box2d.ndim}")
logger.info(f"shape={box2d.shape}")
logger.info(f"size={box2d.size}")
logger.info(f"dimensions={box2d.dimensions}")
logger.info(f"area={box2d.area}")
logger.info(f"center={box2d.center}")
# ======================= Operations =========================================
updated_coords = [3, 4, 3, 5]
box2d.data = updated_coords
logger.info(f"Updated Box2D: {box2d}")
xyxy_view = box2d.as_xyxy()
logger.info(f"Box2D converted to xyxy format: {xyxy_view}")
xywh_view = box2d.as_xywh()
logger.info(f"Box2D converted to xywh format: {xywh_view}")
box2d_copy = box2d.copy()
logger.info(f"Copied Box2D: {box2d_copy}")
# Returns the internal data as a NumPy array. If copy=True, returns a copy; otherwise, returns a view.
box2d_numpy = box2d.to_numpy(copy=False)
logger.info(f"NumPy Box2D:\n{box2d_numpy}")
# __array__ is implemented, so Box2D works directly with NumPy functions.
logger.info(f"np.asarray(box2d)={np.asarray(box2d)}")
logger.info(f"np.sum(box2d)={np.sum(box2d)}")
# Translate and scale by operating on the underlying NumPy array directly.
translation = [2, 3]
translated_data = box2d.data.copy()
translated_data[:2] += translation
translated_box2d = datatypes.Box2D(translated_data)
logger.info(f"Translated Box2D: {translated_box2d}")
scale_factors = [2, 0.5]
scaled_data = box2d.data.copy()
scaled_data[2:] *= np.asarray(scale_factors, dtype=np.float32)
scaled_box2d = datatypes.Box2D(scaled_data)
logger.info(f"Scaled Box2D: {scaled_box2d}")
# ======================= Visualize =========================================
rr.init("box2d_example", spawn=True)
datatypes.visualize(box2d, entity_path="/box2d/updated", label="Updated Box2D")
datatypes.visualize(
translated_box2d, entity_path="/box2d/translated", label="Translated Box2D"
)
datatypes.visualize(scaled_box2d, entity_path="/box2d/scaled", label="Scaled Box2D")
# ======================= Serialize / Deserialize ===========================
start = time.perf_counter()
serialized = datatypes.serialize(box2d)
serialization_ms = (time.perf_counter() - start) * 1000
start = time.perf_counter()
deserialized = datatypes.deserialize(serialized)["param_0"]
deserialization_ms = (time.perf_counter() - start) * 1000
logger.info(f"Deserialized Box2D: {deserialized}")
logger.info(f"Round-trip successful: {box2d == deserialized}")
logger.info(f"Serialization time: {serialization_ms:.3f} ms")
logger.info(f"Deserialization time: {deserialization_ms:.3f} ms")
if __name__ == "__main__":
box2d_example()
