Skip to content

Commit

Permalink
Python <-> Rust bridge fixes (#42)
Browse files Browse the repository at this point in the history
* Python SDK: fix log_points without colors

* Python SDK: Fix: copy numpy views -> log_points_rs

* Python example: increase to 40 samples again
  • Loading branch information
nikolausWest authored Aug 19, 2022
1 parent de1aed8 commit ac5f348
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
7 changes: 2 additions & 5 deletions crates/re_sdk_python/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def log_dummy_data(args):
"""Log a few frames of generated dummy data to show how the Rerun SDK is used."""
NUM_FRAMES = 20
NUM_FRAMES = 40

for sample in generate_dummy_data(num_frames=NUM_FRAMES):
# This will assign logged objects a "time source" called `frame_nr`.
Expand All @@ -22,10 +22,7 @@ def log_dummy_data(args):
# The depth image is in millimeters, so we set meter=1000
rerun.log_depth_image("depth", sample.depth_image_mm, meter=1000)

# TODO(nikolausWest): Make setting the color here optional
rgba = [200, 0, 100, 200]
colors = np.array([rgba])
rerun.log_points("depth3D", sample.point_cloud.copy(), colors)
rerun.log_points("depth3D", sample.point_cloud)

rerun.log_image("rgb", sample.rgb_image)

Expand Down
9 changes: 8 additions & 1 deletion crates/re_sdk_python/python/rerun_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def log_points(obj_path, positions, colors=None, space=None):
If no `space` is given, the space name "2D" or "3D" will be used,
depending on the dimensionality of the data.
"""
if colors is not None:
if colors is None:
# An empty array represents no colors.
colors = np.array((), dtype=np.uint8)
else:
# Rust expects colors in 0-255 uint8
if colors.dtype in ['float32', 'float64']:
if np.amax(colors) < 1.1:
Expand All @@ -119,6 +122,10 @@ def log_points(obj_path, positions, colors=None, space=None):

positions.astype('float32')

# Workaround to handle that `rerun_rs` can't handle numpy views correctly.
# TODO(nikolausWest): Remove this extra copy once underlying issue in Rust SDK is fixed.
positions = positions if positions.base is None else positions.copy()

rerun_rs.log_points_rs(obj_path, positions, colors, space)


Expand Down
2 changes: 1 addition & 1 deletion crates/re_sdk_python/src/python_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn log_bbox(
fn log_points_rs(
obj_path: &str,
positions: numpy::PyReadonlyArray2<'_, f64>,
colors: numpy::PyReadonlyArray2<'_, u8>,
colors: numpy::PyReadonlyArrayDyn<'_, u8>,
space: Option<String>,
) -> PyResult<()> {
if positions.is_empty() {
Expand Down

0 comments on commit ac5f348

Please sign in to comment.