Skip to content

Commit

Permalink
better plane init in python
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Dec 6, 2024
1 parent 7f67a28 commit 3cdbed5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/snippets/all/views/spatial3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
visible=True, # The grid is enabled by default, but you can hide it with this property.
spacing=0.1, # Makes the grid more fine-grained.
# By default, the plane is inferred from view coordinates setup, but you can set arbitrary planes.
plane=rr.components.Plane3D((0, 0, 1), -5.0),
plane=rr.components.Plane3D.XY.with_distance(-5.0),
stroke_width=2.0, # Makes the grid lines twice as thick as usual.
color=[255, 255, 255, 128], # Colors the grid a half-transparent white.
),
Expand Down
17 changes: 16 additions & 1 deletion rerun_py/rerun_sdk/rerun/datatypes/plane3d_ext.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, Union
from typing import TYPE_CHECKING, Any, Union, cast

import numpy as np
import numpy.typing as npt
import pyarrow as pa

from .._validators import flat_np_float32_array_from_array_like
Expand Down Expand Up @@ -53,6 +54,20 @@ def __init__(self: Any, normal: Vec3DLike, distance: Union[float, int, None] = N

self.__attrs_init__(xyzd=np.concatenate((normal_np, distance_np)))

def normal(self: Any) -> npt.NDArray[np.float32]:
"""Returns the normal vector of the plane."""
return cast(npt.NDArray[np.float32], self.xyzd[:3])

def distance(self: Any) -> float:
"""Returns the distance of the plane from the origin."""
return cast(float, self.xyzd[3])

def with_distance(self: Any, distance: float) -> Plane3D:
"""Returns a new plane with the same normal but with the distance set to the given amount."""
from . import Plane3D

return Plane3D(self.normal(), distance)

@staticmethod
def native_to_pa_array_override(data: Plane3DArrayLike, data_type: pa.DataType) -> pa.Array:
planes = flat_np_float32_array_from_array_like(data, 4)
Expand Down

0 comments on commit 3cdbed5

Please sign in to comment.