Skip to content

Commit

Permalink
Add legacy and next-gen partial updates snippets for all languages (#…
Browse files Browse the repository at this point in the history
…8649)

This adds a partial update example for `Points3D`, in all languages,
using both the legacy and new APIs (if they already exist).

This acts as a before/after comparison, a piece of documentation and a
way to make sure that the legacy and new APIs behave similarly.

* DNM: requires #8648 
* Part of #8583 
* Part of #8581 
* Part of #8582
  • Loading branch information
teh-cmc authored Jan 13, 2025
1 parent 822a192 commit e905439
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/snippets/INDEX.md

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! 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", rerun::Points3D::IndicatorComponent(), colors, radii);
// TODO(cmc): implement new APIs and use them!
// rec.log("points", rerun::Points3D::update_fields().with_radii(radii).with_colors(colors));
}

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

rec.set_time_sequence("frame", 20);
rec.log(
"points",
rerun::Points3D::IndicatorComponent(),
positions,
radii,
std::vector<rerun::components::Color>(),
std::vector<rerun::components::Text>(),
std::vector<rerun::components::ShowLabels>(),
std::vector<rerun::components::ClassId>(),
std::vector<rerun::components::KeypointId>()
);
// TODO(cmc): implement new APIs and use them!
// rec.log("points", rerun::Points3D::clear_fields().with_radii(radii).with_colors(colors));
}
36 changes: 36 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""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.Points3D.indicator(), 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))

rr.set_time_sequence("frame", 20)
rr.log(
"points",
[
rr.Points3D.indicator(),
rr.components.Position3DBatch(positions),
rr.components.RadiusBatch(0.3),
rr.components.ColorBatch([]),
rr.components.TextBatch([]),
rr.components.ShowLabelsBatch([]),
rr.components.ClassIdBatch([]),
rr.components.KeypointIdBatch([]),
],
)
# TODO(cmc): implement new APIs and use them!
# rr.log("points", rr.Points3D.clear_fields().update_fields(positions=positions, radii=0.3))
40 changes: 40 additions & 0 deletions docs/snippets/all/archetypes/points3d_partial_updates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Demonstrates usage of the new partial updates APIs.
fn main() -> Result<(), Box<dyn std::error::Error>> {
let rec =
rerun::RecordingStreamBuilder::new("rerun_example_points3d_partial_updates").spawn()?;

let positions = || (0..10).map(|i| (i as f32, 0.0, 0.0));

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(())
}
58 changes: 58 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,58 @@
// 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);
}

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

rec.set_time_sequence("frame", 20);
rec.log(
"points",
rerun::Points3D::IndicatorComponent(),
positions,
radii,
std::vector<rerun::components::Color>(),
std::vector<rerun::components::Text>(),
std::vector<rerun::components::ShowLabels>(),
std::vector<rerun::components::ClassId>(),
std::vector<rerun::components::KeypointId>()
);
}
32 changes: 32 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,32 @@
"""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)])

rr.set_time_sequence("frame", 20)
rr.log(
"points",
[
rr.Points3D.indicator(),
rr.components.Position3DBatch(positions),
rr.components.RadiusBatch(0.3),
rr.components.ColorBatch([]),
rr.components.TextBatch([]),
rr.components.ShowLabelsBatch([]),
rr.components.ClassIdBatch([]),
rr.components.KeypointIdBatch([]),
],
)
51 changes: 51 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,51 @@
//! Demonstrates usage of the legacy partial updates APIs.
fn main() -> Result<(), Box<dyn std::error::Error>> {
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));

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

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])?;
}

use rerun::Archetype as _;
let positions: Vec<rerun::components::Position3D> =
(0..10).map(|i| (i as f32, 0.0, 0.0).into()).collect();

rec.set_time_sequence("frame", 20);
rec.log(
"points",
&[
&rerun::Points3D::indicator() as &dyn rerun::ComponentBatch,
&positions,
&vec![rerun::components::Radius(0.3.into())],
&Vec::<rerun::components::Color>::new(),
&Vec::<rerun::components::Text>::new(),
&Vec::<rerun::components::ShowLabels>::new(),
&Vec::<rerun::components::ClassId>::new(),
&Vec::<rerun::components::KeypointId>::new(),
],
)?;

Ok(())
}
10 changes: 9 additions & 1 deletion lychee.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,13 @@ exclude = [
'https://openaccess.thecvf.com/content/CVPR2023/html/Du_Learning_To_Render_Novel_Views_From_Wide-Baseline_Stereo_Pairs_CVPR_2023_paper.html',
'https://anaconda.org/conda-forge/arrow-cpp',

#'^file:///', # Ignore local file links. They need to be tested, but it's useful for external links we have to ping.
# '^file:///', # Ignore local file links. They need to be tested, but it's useful for external links we have to ping.

# Snippets that haven't been released yet.
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/points3d_partial_updates.py',
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/points3d_partial_updates_legacy.py',
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/points3d_partial_updates.cpp',
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/points3d_partial_updates_legacy.cpp',
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/points3d_partial_updates.rs',
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/points3d_partial_updates_legacy.rs',
]

0 comments on commit e905439

Please sign in to comment.