Skip to content

Commit

Permalink
Never direct users towards using `RecordingStream::log_component_batc…
Browse files Browse the repository at this point in the history
…hes` (#8149)

`RecordingStream::log_component_batches` is a clunky implementation
detail, at best. Users should be directed towards
`RecordingStream::log{_static}`, always.

To do so, we implement `AsComponents` for basic collections of `dyn
ComponentBatch`es, and update all examples accordingly.
  • Loading branch information
teh-cmc authored Nov 15, 2024
1 parent 015888f commit 9e46657
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
34 changes: 34 additions & 0 deletions crates/store/re_types_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,40 @@ impl<C: Component> AsComponents for C {
}
}

impl AsComponents for dyn ComponentBatch {
#[inline]
fn as_component_batches(&self) -> Vec<MaybeOwnedComponentBatch<'_>> {
vec![MaybeOwnedComponentBatch::Ref(self)]
}
}

impl<const N: usize> AsComponents for [&dyn ComponentBatch; N] {
#[inline]
fn as_component_batches(&self) -> Vec<MaybeOwnedComponentBatch<'_>> {
self.iter()
.map(|batch| MaybeOwnedComponentBatch::Ref(*batch))
.collect()
}
}

impl AsComponents for &[&dyn ComponentBatch] {
#[inline]
fn as_component_batches(&self) -> Vec<MaybeOwnedComponentBatch<'_>> {
self.iter()
.map(|batch| MaybeOwnedComponentBatch::Ref(*batch))
.collect()
}
}

impl AsComponents for Vec<&dyn ComponentBatch> {
#[inline]
fn as_component_batches(&self) -> Vec<MaybeOwnedComponentBatch<'_>> {
self.iter()
.map(|batch| MaybeOwnedComponentBatch::Ref(*batch))
.collect()
}
}

// ---

mod archetype;
Expand Down
6 changes: 1 addition & 5 deletions docs/snippets/all/archetypes/image_send_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Log the ImageFormat and indicator once, as static.
let format = rerun::components::ImageFormat::rgb8([width as _, height as _]);
rec.log_component_batches(
"images",
true,
[&format as _, &rerun::Image::indicator() as _],
)?;
rec.log_static("images", &[&format as _, &rerun::Image::indicator() as _])?;

// Split up the image data into several components referencing the underlying data.
let image_size_in_bytes = width * height * 3;
Expand Down
5 changes: 2 additions & 3 deletions docs/snippets/all/archetypes/manual_indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Specify both a Mesh3D and a Points3D indicator component so that the data is shown as both a
// 3D mesh _and_ a point cloud by default.
rec.log_component_batches(
rec.log(
"points_and_mesh",
false,
[
&[
rerun::Points3D::indicator().as_ref() as &dyn rerun::ComponentBatch,
rerun::Mesh3D::indicator().as_ref(),
&[[0.0, 0.0, 0.0], [10.0, 0.0, 0.0], [0.0, 10.0, 0.0]].map(rerun::Position3D::from),
Expand Down
2 changes: 1 addition & 1 deletion docs/snippets/all/archetypes/mesh3d_partial_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
(glam::Vec3::from(vertex_positions[1]) * factor).into(),
(glam::Vec3::from(vertex_positions[2]) * factor).into(),
];
rec.log_component_batches("triangle", false, [&vertex_positions as _])?;
rec.log("triangle", &[&vertex_positions as _])?;
}

Ok(())
Expand Down
10 changes: 4 additions & 6 deletions docs/snippets/all/concepts/different_data_per_timeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Log a red color on one timeline.
rec.reset_time(); // Clears all set timeline info.
rec.set_time_seconds("red timeline", 1.0);
rec.log_component_batches(
rec.log(
"points",
false,
[&rerun::components::Color::from_u32(0xFF0000FF) as &dyn rerun::ComponentBatch],
&[&rerun::components::Color::from_u32(0xFF0000FF) as &dyn rerun::ComponentBatch],
)?;

// And a blue color on the other.
rec.reset_time(); // Clears all set timeline info.
rec.set_time_sequence("blue timeline", 1);
rec.log_component_batches(
rec.log(
"points",
false,
[&rerun::components::Color::from_u32(0x0000FFFF) as &dyn rerun::ComponentBatch],
&[&rerun::components::Color::from_u32(0x0000FFFF) as &dyn rerun::ComponentBatch],
)?;

// TODO(#5521): log VisualBounds2D
Expand Down
14 changes: 3 additions & 11 deletions examples/rust/incremental_logging/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ let radii = [rerun::Radius(0.1); 10];
// Only log colors and radii once.
rec.set_time_sequence("frame_nr", 0);
rec.log_component_batches("points", false /* static */, [&colors as &dyn rerun::ComponentBatch, &radii])?;
rec.log("points", &[&colors as &dyn rerun::ComponentBatch, &radii])?;
let mut rng = rand::thread_rng();
let dist = Uniform::new(-5., 5.);
Expand All @@ -63,17 +63,9 @@ fn run(rec: &rerun::RecordingStream) -> anyhow::Result<()> {

// Only log colors and radii once.
rec.set_time_sequence("frame_nr", 0);
rec.log_component_batches(
"points",
false, /* static */
[&colors as &dyn rerun::ComponentBatch, &radii],
)?;
rec.log("points", &[&colors as &dyn rerun::ComponentBatch, &radii])?;
// Logging statically would also work.
// rec.log_component_batches(
// "points",
// true, /* static */
// [&colors as &dyn rerun::ComponentBatch, &radii],
// )?;
// rec.log_static("points", &[&colors as &dyn rerun::ComponentBatch, &radii])?;

let mut rng = rand::thread_rng();
let dist = Uniform::new(-5., 5.);
Expand Down

0 comments on commit 9e46657

Please sign in to comment.