diff --git a/docs/snippets/all/annotation_context_rects.py b/docs/snippets/all/annotation_context_rects.py index 1c430116d223..1a857cc741b8 100644 --- a/docs/snippets/all/annotation_context_rects.py +++ b/docs/snippets/all/annotation_context_rects.py @@ -1,12 +1,11 @@ import rerun as rr +import rerun.blueprint as rrb -rr.init("rerun_example_annotation_context_rects", spawn=True) +blueprint = rrb.Spatial2DView(background=[128, 0, 0], visual_bounds=rrb.VisualBounds(min=[-4.5, -2.5], max=[2.5, 2.5])) +rr.init("rerun_example_annotation_context_rects", spawn=True, default_blueprint=blueprint) # Log an annotation context to assign a label and color to each class rr.log("/", rr.AnnotationContext([(1, "red", (255, 0, 0)), (2, "green", (0, 255, 0))]), static=True) # Log a batch of 2 rectangles with different `class_ids` rr.log("detections", rr.Boxes2D(mins=[[-2, -2], [0, 0]], sizes=[[3, 3], [2, 2]], class_ids=[1, 2])) - -# Log an extra rect to set the view bounds -rr.log("bounds", rr.Boxes2D(half_sizes=[2.5, 2.5])) diff --git a/rerun_py/rerun_sdk/rerun/blueprint/__init__.py b/rerun_py/rerun_sdk/rerun/blueprint/__init__.py index 507e7b007d05..8a2e7f0e2db0 100644 --- a/rerun_py/rerun_sdk/rerun/blueprint/__init__.py +++ b/rerun_py/rerun_sdk/rerun/blueprint/__init__.py @@ -16,6 +16,7 @@ Background, PlotLegend, ScalarAxis, + VisualBounds, ) from .components import ( BackgroundKind, diff --git a/rerun_py/rerun_sdk/rerun/blueprint/archetypes/visual_bounds.py b/rerun_py/rerun_sdk/rerun/blueprint/archetypes/visual_bounds.py index f9037ec6a120..c611cd889a5e 100644 --- a/rerun_py/rerun_sdk/rerun/blueprint/archetypes/visual_bounds.py +++ b/rerun_py/rerun_sdk/rerun/blueprint/archetypes/visual_bounds.py @@ -5,40 +5,20 @@ from __future__ import annotations -from typing import Any - from attrs import define, field -from ... import components, datatypes +from ... import components from ..._baseclasses import Archetype -from ...error_utils import catch_and_log_exceptions +from .visual_bounds_ext import VisualBoundsExt __all__ = ["VisualBounds"] @define(str=False, repr=False, init=False) -class VisualBounds(Archetype): +class VisualBounds(VisualBoundsExt, Archetype): """**Archetype**: Controls the visual bounds of a 2D space view.""" - def __init__(self: Any, *, visual_bounds: datatypes.AABB2DLike | None = None): - """ - Create a new instance of the VisualBounds archetype. - - Parameters - ---------- - visual_bounds: - The visible parts of a 2D space view, in the coordinate space of the scene. - - Everything within these bounds are guaranteed to be visible. - Somethings outside of these bounds may also be visible due to letterboxing. - - """ - - # You can define your own __init__ function as a member of VisualBoundsExt in visual_bounds_ext.py - with catch_and_log_exceptions(context=self.__class__.__name__): - self.__attrs_init__(visual_bounds=visual_bounds) - return - self.__attrs_clear__() + # __init__ can be found in visual_bounds_ext.py def __attrs_clear__(self) -> None: """Convenience method for calling `__attrs_init__` with all `None`s.""" diff --git a/rerun_py/rerun_sdk/rerun/blueprint/archetypes/visual_bounds_ext.py b/rerun_py/rerun_sdk/rerun/blueprint/archetypes/visual_bounds_ext.py new file mode 100644 index 000000000000..9124871c0612 --- /dev/null +++ b/rerun_py/rerun_sdk/rerun/blueprint/archetypes/visual_bounds_ext.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +from typing import Any + +from ... import components, datatypes +from ...error_utils import catch_and_log_exceptions + + +class VisualBoundsExt: + """Extension for [VisualBounds][rerun.blueprint.archetypes.VisualBounds].""" + + def __init__( + self: Any, + *, + min: datatypes.Vec2DLike, + max: datatypes.Vec2DLike, + ): + """ + Create a new instance of the VisualBounds archetype. + + Parameters + ---------- + min: + The minimum point of the visible parts of a 2D space view, in the coordinate space of the scene. + Usually the left-top corner. + max: + The maximum point of the visible parts of a 2D space view, in the coordinate space of the scene. + Usually the right-bottom corner. + + """ + + with catch_and_log_exceptions(context=self.__class__.__name__): + self.__attrs_init__(visual_bounds=components.AABB2D(min=min, max=max)) + return + self.__attrs_clear__() diff --git a/rerun_py/rerun_sdk/rerun/components/aabb2d.py b/rerun_py/rerun_sdk/rerun/components/aabb2d.py index c6ad6c4d6c82..16419767704c 100644 --- a/rerun_py/rerun_sdk/rerun/components/aabb2d.py +++ b/rerun_py/rerun_sdk/rerun/components/aabb2d.py @@ -7,14 +7,15 @@ from .. import datatypes from .._baseclasses import ComponentBatchMixin +from .aabb2d_ext import AABB2DExt __all__ = ["AABB2D", "AABB2DBatch", "AABB2DType"] -class AABB2D(datatypes.AABB2D): +class AABB2D(AABB2DExt, datatypes.AABB2D): """**Component**: An Axis-Aligned Bounding Box in 2D space.""" - # You can define your own __init__ function as a member of AABB2DExt in aabb2d_ext.py + # __init__ can be found in aabb2d_ext.py # Note: there are no fields here because AABB2D delegates to datatypes.AABB2D pass diff --git a/rerun_py/rerun_sdk/rerun/components/aabb2d_ext.py b/rerun_py/rerun_sdk/rerun/components/aabb2d_ext.py new file mode 100644 index 000000000000..75dd962b1dbb --- /dev/null +++ b/rerun_py/rerun_sdk/rerun/components/aabb2d_ext.py @@ -0,0 +1,31 @@ +from __future__ import annotations + +from typing import Any + +from ..datatypes import AABB2D, Vec2DLike + + +class AABB2DExt: + """Extension for [AABB2D][rerun.blueprint.components.AABB2D].""" + + def __init__( + self: Any, + *, + min: Vec2DLike, + max: Vec2DLike, + ): + """ + Create a new instance of the AABB2D component. + + Parameters + ---------- + min: + The minimum point of the visible parts of a 2D space view, in the coordinate space of the scene. + Usually the left-top corner. + max: + The maximum point of the visible parts of a 2D space view, in the coordinate space of the scene. + Usually the right-bottom corner. + + """ + + self.__attrs_init__(AABB2D(min=min, max=max)) diff --git a/rerun_py/rerun_sdk/rerun/datatypes/aabb2d.py b/rerun_py/rerun_sdk/rerun/datatypes/aabb2d.py index 3237db6fbedd..3e5be4dc47be 100644 --- a/rerun_py/rerun_sdk/rerun/datatypes/aabb2d.py +++ b/rerun_py/rerun_sdk/rerun/datatypes/aabb2d.py @@ -5,7 +5,7 @@ from __future__ import annotations -from typing import Any, Sequence, Union +from typing import Sequence, Union import numpy as np import numpy.typing as npt @@ -16,29 +16,16 @@ from .._converters import ( to_np_float64, ) +from .aabb2d_ext import AABB2DExt __all__ = ["AABB2D", "AABB2DArrayLike", "AABB2DBatch", "AABB2DLike", "AABB2DType"] @define(init=False) -class AABB2D: +class AABB2D(AABB2DExt): """**Datatype**: An Axis-Aligned Bounding Box in 2D space, implemented as the minimum and maximum corners.""" - def __init__(self: Any, min_xy: npt.ArrayLike, max_xy: npt.ArrayLike): - """ - Create a new instance of the AABB2D datatype. - - Parameters - ---------- - min_xy: - The minimum bounds; usually left-top corner. - max_xy: - The maximum bounds; usually right-bottom corner. - - """ - - # You can define your own __init__ function as a member of AABB2DExt in aabb2d_ext.py - self.__attrs_init__(min_xy=min_xy, max_xy=max_xy) + # __init__ can be found in aabb2d_ext.py min_xy: npt.NDArray[np.float64] = field(converter=to_np_float64) # The minimum bounds; usually left-top corner. diff --git a/rerun_py/rerun_sdk/rerun/datatypes/aabb2d_ext.py b/rerun_py/rerun_sdk/rerun/datatypes/aabb2d_ext.py new file mode 100644 index 000000000000..1d011248902f --- /dev/null +++ b/rerun_py/rerun_sdk/rerun/datatypes/aabb2d_ext.py @@ -0,0 +1,34 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +import numpy as np + +if TYPE_CHECKING: + from . import Vec2DLike + + +class AABB2DExt: + """Extension for [AABB2D][rerun.blueprint.datatypes.AABB2D].""" + + def __init__( + self: Any, + min: Vec2DLike, + max: Vec2DLike, + ): + """ + Create a new instance of the AABB2D datatype. + + Parameters + ---------- + min: + The minimum point of the visible parts of a 2D space view, in the coordinate space of the scene. + Usually the left-top corner. + max: + The maximum point of the visible parts of a 2D space view, in the coordinate space of the scene. + Usually the right-bottom corner. + + """ + + self.min_xy = np.array(min, np.float64) + self.max_xy = np.array(max, np.float64)