Skip to content

Commit

Permalink
Update log_mesh and log_meshes docs. (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
John Hughes authored Feb 28, 2023
1 parent e956e2f commit a853700
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions rerun_py/rerun_sdk/rerun/log/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,58 @@

def log_mesh(
entity_path: str,
positions: npt.NDArray[np.float32],
positions: npt.ArrayLike,
*,
indices: Optional[npt.NDArray[np.uint32]] = None,
normals: Optional[npt.NDArray[np.float32]] = None,
albedo_factor: Optional[npt.NDArray[np.float32]] = None,
indices: Optional[npt.ArrayLike] = None,
normals: Optional[npt.ArrayLike] = None,
albedo_factor: Optional[npt.ArrayLike] = None,
timeless: bool = False,
) -> None:
"""
Log a raw 3D mesh by specifying its vertex positions, and optionally indices, normals and albedo factor.
The data is _always_ interpreted as a triangle list:
* `positions` is a flattened array of 3D points, i.e. its length must be divisible by 3.
* `positions` is a (potentially flattened) array of 3D points, i.e. the total number of elements must be divisible
by 3.
* `indices`, if specified, is a flattened array of indices that describe the mesh's faces,
i.e. its length must be divisible by 3.
* `normals`, if specified, is a flattened array of 3D vectors that describe the normal
for each vertex, i.e. its length must be divisible by 3 and more importantly it has to be
equal to the length of `positions`.
* `albedo_factor`, if specified, is either a linear, unmultiplied, normalized RGB (vec3) or
RGBA (vec4) value.
* `normals`, if specified, is a (potentially flattened) array of 3D vectors that describe the normal for each
vertex, i.e. the total number of elements must be divisible by 3 and more importantly, `len(normals)` should be
equal to `len(positions)`.
* `albedo_factor`, if specified, is either a linear, unmultiplied, normalized RGB (vec3) or RGBA (vec4) value.
Example:
-------
```
# A simple red triangle:
positions = np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0])
indices = np.array([0, 1, 2])
normals = np.array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0])
albedo_factor = np.array([1.0, 0.0, 0.0])
rerun.log_mesh(
"world/mesh",
positions = [
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0]
],
indices = [0, 1, 2],
normals = [
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0]
],
albedo_factor = [1.0, 0.0, 0.0],
)
```
Parameters
----------
entity_path:
Path to the mesh in the space hierarchy
positions:
A flattened array of 3D points
An array of 3D points
indices:
Optional flattened array of indices that describe the mesh's faces
Optional array of indices that describe the mesh's faces
normals:
Optional flattened array of 3D vectors that describe the normal of each vertices
Optional array of 3D vectors that describe the normal of each vertices
albedo_factor:
Optional RGB(A) color for the albedo factor of the mesh, aka base color factor.
timeless:
Expand All @@ -64,25 +75,26 @@ def log_mesh(
if not bindings.is_enabled():
return

positions = positions.flatten().astype(np.float32)
positions = np.asarray(positions, dtype=np.float32).flatten()

if indices is not None:
indices = indices.flatten().astype(np.uint32)
indices = np.asarray(indices, dtype=np.uint32).flatten()
if normals is not None:
normals = normals.flatten().astype(np.float32)
normals = np.asarray(normals, dtype=np.float32).flatten()
if albedo_factor is not None:
albedo_factor = albedo_factor.flatten().astype(np.float32)
albedo_factor = np.asarray(albedo_factor, dtype=np.float32).flatten()

# Mesh arrow handling happens inside the python bridge
bindings.log_meshes(entity_path, [positions.flatten()], [indices], [normals], [albedo_factor], timeless)


def log_meshes(
entity_path: str,
position_buffers: Sequence[npt.NDArray[np.float32]],
position_buffers: Sequence[npt.ArrayLike],
*,
index_buffers: Sequence[Optional[npt.NDArray[np.uint32]]],
normal_buffers: Sequence[Optional[npt.NDArray[np.float32]]],
albedo_factors: Sequence[Optional[npt.NDArray[np.float32]]],
index_buffers: Sequence[Optional[npt.ArrayLike]],
normal_buffers: Sequence[Optional[npt.ArrayLike]],
albedo_factors: Sequence[Optional[npt.ArrayLike]],
timeless: bool = False,
) -> None:
"""
Expand Down Expand Up @@ -115,13 +127,13 @@ def log_meshes(
if not bindings.is_enabled():
return

position_buffers = [p.flatten().astype(np.float32) for p in position_buffers]
position_buffers = [np.asarray(p, dtype=np.float32).flatten() for p in position_buffers]
if index_buffers is not None:
index_buffers = [i.flatten().astype(np.uint32) if i else None for i in index_buffers]
index_buffers = [np.asarray(i, dtype=np.uint32).flatten() if i else None for i in index_buffers]
if normal_buffers is not None:
normal_buffers = [n.flatten().astype(np.float32) if n else None for n in normal_buffers]
normal_buffers = [np.asarray(n, dtype=np.float32).flatten() if n else None for n in normal_buffers]
if albedo_factors is not None:
albedo_factors = [af.flatten().astype(np.float32) if af else None for af in albedo_factors]
albedo_factors = [np.asarray(af, dtype=np.float32).flatten() if af else None for af in albedo_factors]

# Mesh arrow handling happens inside the python bridge
bindings.log_meshes(entity_path, position_buffers, index_buffers, normal_buffers, albedo_factors, timeless)

1 comment on commit a853700

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: a853700 Previous: 3710841 Ratio
datastore/insert/batch/rects/insert 556723 ns/iter (± 4287) 544196 ns/iter (± 1940) 1.02
datastore/latest_at/batch/rects/query 1814 ns/iter (± 15) 1822 ns/iter (± 6) 1.00
datastore/latest_at/missing_components/primary 358 ns/iter (± 1) 355 ns/iter (± 0) 1.01
datastore/latest_at/missing_components/secondaries 424 ns/iter (± 2) 425 ns/iter (± 0) 1.00
datastore/range/batch/rects/query 152033 ns/iter (± 1114) 153291 ns/iter (± 977) 0.99
mono_points_arrow/generate_message_bundles 49035734 ns/iter (± 3095751) 47514201 ns/iter (± 1231756) 1.03
mono_points_arrow/generate_messages 137921493 ns/iter (± 1629958) 135717587 ns/iter (± 1177378) 1.02
mono_points_arrow/encode_log_msg 169115165 ns/iter (± 1491936) 165358412 ns/iter (± 694312) 1.02
mono_points_arrow/encode_total 360557002 ns/iter (± 3305610) 351190021 ns/iter (± 1716074) 1.03
mono_points_arrow/decode_log_msg 188099111 ns/iter (± 1943817) 184741148 ns/iter (± 1019315) 1.02
mono_points_arrow/decode_message_bundles 75465592 ns/iter (± 1608330) 71012913 ns/iter (± 1131543) 1.06
mono_points_arrow/decode_total 259745243 ns/iter (± 3196231) 254709773 ns/iter (± 3198040) 1.02
batch_points_arrow/generate_message_bundles 331837 ns/iter (± 2147) 332632 ns/iter (± 1258) 1.00
batch_points_arrow/generate_messages 6232 ns/iter (± 44) 6225 ns/iter (± 22) 1.00
batch_points_arrow/encode_log_msg 357948 ns/iter (± 2556) 354207 ns/iter (± 1494) 1.01
batch_points_arrow/encode_total 719006 ns/iter (± 5624) 714943 ns/iter (± 2382) 1.01
batch_points_arrow/decode_log_msg 347952 ns/iter (± 1501) 346000 ns/iter (± 871) 1.01
batch_points_arrow/decode_message_bundles 2084 ns/iter (± 15) 2081 ns/iter (± 17) 1.00
batch_points_arrow/decode_total 353207 ns/iter (± 1605) 354301 ns/iter (± 685) 1.00
arrow_mono_points/insert 7014697094 ns/iter (± 18838448) 6817562300 ns/iter (± 12906158) 1.03
arrow_mono_points/query 1735165 ns/iter (± 17986) 1739322 ns/iter (± 8158) 1.00
arrow_batch_points/insert 2699253 ns/iter (± 16929) 2641905 ns/iter (± 8333) 1.02
arrow_batch_points/query 17585 ns/iter (± 91) 17677 ns/iter (± 46) 0.99
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 1) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.