Skip to content

Commit

Permalink
implement legacy and next-gen partial updates snippets for all, ready…
Browse files Browse the repository at this point in the history
… or not
  • Loading branch information
teh-cmc committed Jan 9, 2025
1 parent ffee96a commit 5ba5ce6
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/snippets/INDEX.md

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Demonstrates usage of the new partial updates APIs.

#include <rerun.hpp>

#include <algorithm>
#include <vector>

int main() {
const auto rec = rerun::RecordingStream("rerun_example_points3d_partial_updates");
rec.spawn().exit_on_failure();

std::vector<rerun::Position3D> positions;
for (int i = 0; i < 10; ++i) {
positions.emplace_back(static_cast<float>(i), 0.0f, 0.0f);
}

rec.set_time_sequence("frame", 0);
rec.log("points", rerun::Points3D(positions));

for (int i = 0; i < 10; ++i) {
std::vector<rerun::Color> colors;
for (int n = 0; n < 10; ++n) {
if (n < i) {
colors.emplace_back(rerun::Color(20, 200, 20));
} else {
colors.emplace_back(rerun::Color(200, 20, 20));
}
}

std::vector<rerun::Radius> radii;
for (int n = 0; n < 10; ++n) {
if (n < i) {
radii.emplace_back(rerun::Radius(0.6f));
} else {
radii.emplace_back(rerun::Radius(0.2f));
}
}

rec.set_time_sequence("frame", i);
rec.log("points", colors, radii);
// TODO(cmc): implement new APIs and use them!
// rec.log("points", rerun::Points3D::update_fields().with_radii(radii).with_colors(colors));
}

// TODO(cmc): remove this!
// We cannot replicate the clear_fields() logic!
rec.set_time_sequence("frame", 20);
rec.log("points", rerun::Clear::FLAT);

std::vector<rerun::Radius> radii;
radii.emplace_back(rerun::Radius(0.3));

rec.set_time_sequence("frame", 20);
rec.log("points", positions, radii);

// TODO(cmc): implement new APIs and use them!
// rec.log("points", rerun::Points3D::clear_fields().with_radii(radii).with_colors(colors));
}
30 changes: 30 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Demonstrates usage of the new partial updates APIs."""

import rerun as rr

rr.init("rerun_example_points3d_partial_updates", spawn=True)

positions = [[i, 0, 0] for i in range(0, 10)]

rr.set_time_sequence("frame", 0)
rr.log("points", rr.Points3D(positions))

for i in range(0, 10):
colors = [[20, 200, 20] if n < i else [200, 20, 20] for n in range(0, 10)]
radii = [0.6 if n < i else 0.2 for n in range(0, 10)]

rr.set_time_sequence("frame", i)
rr.log("points", [rr.components.ColorBatch(colors), rr.components.RadiusBatch(radii)])
# TODO(cmc): implement new APIs and use them!
# rr.log("points", rr.Points3D.update_fields(radii=radii, colors=colors))

# TODO(cmc): remove this!
# We cannot replicate the clear_fields() logic!
rr.set_time_sequence("frame", 20)
rr.log("points", rr.Clear(recursive=False))

rr.set_time_sequence("frame", 20)
rr.log("points", [rr.components.Position3DBatch(positions), rr.components.RadiusBatch(0.3)])

# TODO(cmc): implement new APIs and use them!
# rr.log("points", rr.Points3D.clear_fields().update_fields(positions=positions, radii=0.3))
45 changes: 45 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//! Demonstrates usage of the new partial updates APIs.
use rand::{distributions::Uniform, Rng as _};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let dist = Uniform::new(-5., 5.);

let rec =
rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates").spawn()?;

let positions = (0..10).map(|i| (i as f32, 0.0, 0.0)).collect::<Vec<_>>();

rec.set_time_sequence("frame", 0);
rec.log("points", &rerun::Points3D::new(&positions))?;

for i in 0..10 {
let colors = (0..10).map(|n| {
if n < i {
rerun::Color::from_rgb(20, 200, 20)
} else {
rerun::Color::from_rgb(200, 20, 20)
}
});
let radii = (0..10).map(|n| if n < i { 0.6 } else { 0.2 });

rec.set_time_sequence("frame", i);
rec.log(
"points",
&rerun::Points3D::update_fields()
.with_radii(radii)
.with_colors(colors),
)?;
}

rec.set_time_sequence("frame", 20);
rec.log(
"points",
&rerun::Points3D::clear_fields()
.with_positions(positions)
.with_radii([0.3]),
)?;

Ok(())
}
52 changes: 52 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates_legacy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Demonstrates usage of the legacy partial updates APIs.

#include <rerun.hpp>

#include <algorithm>
#include <vector>

int main() {
const auto rec = rerun::RecordingStream("rerun_example_points3d_partial_updates_legacy");
rec.spawn().exit_on_failure();

std::vector<rerun::Position3D> positions;
for (int i = 0; i < 10; ++i) {
positions.emplace_back(static_cast<float>(i), 0.0f, 0.0f);
}

rec.set_time_sequence("frame", 0);
rec.log("points", rerun::Points3D(positions));

for (int i = 0; i < 10; ++i) {
std::vector<rerun::Color> colors;
for (int n = 0; n < 10; ++n) {
if (n < i) {
colors.emplace_back(rerun::Color(20, 200, 20));
} else {
colors.emplace_back(rerun::Color(200, 20, 20));
}
}

std::vector<rerun::Radius> radii;
for (int n = 0; n < 10; ++n) {
if (n < i) {
radii.emplace_back(rerun::Radius(0.6f));
} else {
radii.emplace_back(rerun::Radius(0.2f));
}
}

rec.set_time_sequence("frame", i);
rec.log("points", colors, radii);
}

// We cannot replicate the clear_fields() logic!
rec.set_time_sequence("frame", 20);
rec.log("points", rerun::Clear::FLAT);

std::vector<rerun::Radius> radii;
radii.emplace_back(rerun::Radius(0.3));

rec.set_time_sequence("frame", 20);
rec.log("points", positions, radii);
}
24 changes: 24 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates_legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Demonstrates usage of the new partial updates APIs."""

import rerun as rr

rr.init("rerun_example_points3d_partial_updates_legacy", spawn=True)

positions = [[i, 0, 0] for i in range(0, 10)]

rr.set_time_sequence("frame", 0)
rr.log("points", rr.Points3D(positions))

for i in range(0, 10):
colors = [[20, 200, 20] if n < i else [200, 20, 20] for n in range(0, 10)]
radii = [0.6 if n < i else 0.2 for n in range(0, 10)]

rr.set_time_sequence("frame", i)
rr.log("points", [rr.components.ColorBatch(colors), rr.components.RadiusBatch(radii)])

# We cannot replicate the clear_fields() logic!
rr.set_time_sequence("frame", 20)
rr.log("points", rr.Clear(recursive=False))

rr.set_time_sequence("frame", 20)
rr.log("points", [rr.components.Position3DBatch(positions), rr.components.RadiusBatch(0.3)])
54 changes: 54 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates_legacy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Demonstrates usage of the legacy partial updates APIs.
use rand::{distributions::Uniform, Rng as _};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let dist = Uniform::new(-5., 5.);

let rec = rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates_legacy")
.spawn()?;

let positions = (0..10).map(|i| (i as f32, 0.0, 0.0)).collect::<Vec<_>>();

rec.set_time_sequence("frame", 0);
rec.log("points", &rerun::Points3D::new(positions.clone()))?;

for i in 0..10 {
let colors: Vec<rerun::components::Color> = (0..10)
.map(|n| {
if n < i {
rerun::Color::from_rgb(20, 200, 20)
} else {
rerun::Color::from_rgb(200, 20, 20)
}
})
.collect();
let radii: Vec<rerun::components::Radius> = (0..10)
.map(|n| if n < i { 0.6 } else { 0.2 })
.map(Into::into)
.collect();

rec.set_time_sequence("frame", i);
rec.log("points", &[&colors as &dyn rerun::ComponentBatch, &radii])?;
}

// We cannot replicate the clear_fields() logic!
rec.set_time_sequence("frame", 20);
rec.log("points", &rerun::Clear::flat())?;

rec.set_time_sequence("frame", 20);
let positions: Vec<rerun::components::Position3D> = (0..10)
.map(|i| (i as f32, 0.0, 0.0))
.map(Into::into)
.collect();
rec.log(
"points",
&[
&positions as &dyn rerun::ComponentBatch,
&rerun::components::Radius(0.3.into()),
],
)?;

Ok(())
}
4 changes: 4 additions & 0 deletions docs/snippets/snippets.toml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ quick_start = [ # These examples don't have exactly the same implementation.
"py",
"rust",
]
"archetypes/points3d_partial_updates" = [ # TODO(cmc): not supported yet
"py",
"cpp",
]
"archetypes/points3d_random" = [ # TODO(#3206): examples use different RNGs
"cpp",
"py",
Expand Down

0 comments on commit 5ba5ce6

Please sign in to comment.