Skip to content

Commit

Permalink
Add new Transform3D partial updates snippet for all languages (#8690)
Browse files Browse the repository at this point in the history
Adds some much needed snippet for partial updates on `Transform3D`.
Much needed because A) `Transform3D` has a long history of custom
partial updatability hacks and B) users are very very likely to use that
one.

* Part of #8580
  • Loading branch information
teh-cmc authored Jan 15, 2025
1 parent efee5ad commit d747b6c
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/snippets/INDEX.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions docs/snippets/all/archetypes/transform3d_partial_updates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Log different transforms with visualized coordinates axes.

#include <rerun.hpp>

float truncated_radians(int deg) {
auto degf = static_cast<float>(deg);
const auto pi = 3.14159265358979323846;
return static_cast<float>(static_cast<int>(degf * pi / 180.0f * 1000.0f)) / 1000.0f;
}

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

// Set up a 3D box.
rec.log(
"box",
rerun::Boxes3D::from_half_sizes({{4.f, 2.f, 1.0f}}).with_fill_mode(rerun::FillMode::Solid),
rerun::Transform3D().with_axis_length(10.0)
);

// Update only the rotation of the box.
for (int deg = 0; deg <= 45; deg++) {
auto rad = truncated_radians(deg * 4);
// TODO(#8583): update_fields
rec.log(
"box",
rerun::Transform3D().with_rotation_axis_angle(
rerun::RotationAxisAngle({0.0f, 1.0f, 0.0f}, rerun::Angle::radians(rad))
)
);
}

// Update only the position of the box.
for (int t = 0; t <= 45; t++) {
rec.log(
"box",
rerun::Transform3D().with_translation({0.0f, 0.0f, static_cast<float>(t) / 10.0f})
);
}

// Update only the rotation of the box.
for (int deg = 0; deg <= 45; deg++) {
auto rad = truncated_radians((deg + 45) * 4);
// TODO(#8583): update_fields
rec.log(
"box",
rerun::Transform3D().with_rotation_axis_angle(
rerun::RotationAxisAngle({0.0f, 1.0f, 0.0f}, rerun::Angle::radians(rad))
)
);
}

// Clear all of the box's attributes, and reset its axis length.
// TODO(#8583): clear_fields
rec.log("box", rerun::Transform3D().with_axis_length(15.0));
}
53 changes: 53 additions & 0 deletions docs/snippets/all/archetypes/transform3d_partial_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Log different transforms with visualized coordinates axes."""

import math

import rerun as rr


def truncated_radians(deg: float) -> float:
return float(int(math.radians(deg) * 1000.0)) / 1000.0


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

rr.log(
"box",
rr.Boxes3D(half_sizes=[4.0, 2.0, 1.0], fill_mode=rr.components.FillMode.Solid),
rr.Transform3D(axis_length=10),
)

for deg in range(46):
rad = truncated_radians(deg * 4)
# TODO(#8582): update_fields
rr.log(
"box",
rr.Transform3D(
# TODO(cmc): we should have access to all the fields of the extended constructor too.
rotation_axis_angle=rr.RotationAxisAngle(axis=[0.0, 1.0, 0.0], radians=rad),
),
)

for t in range(51):
# TODO(#8582): update_fields
rr.log(
"box",
rr.Transform3D(translation=[0, 0, t / 10.0]),
)

for deg in range(46):
rad = truncated_radians((deg + 45) * 4)
# TODO(#8582): update_fields
rr.log(
"box",
rr.Transform3D(
# TODO(cmc): we should have access to all the fields of the extended constructor too.
rotation_axis_angle=rr.RotationAxisAngle(axis=[0.0, 1.0, 0.0], radians=rad),
),
)

# TODO(#8582): update_fields(clear=True)
rr.log(
"box",
rr.Transform3D(axis_length=15),
)
60 changes: 60 additions & 0 deletions docs/snippets/all/archetypes/transform3d_partial_updates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Log different transforms with visualized coordinates axes.
fn main() -> Result<(), Box<dyn std::error::Error>> {
let rec =
rerun::RecordingStreamBuilder::new("rerun_example_transform3d_partial_updates").spawn()?;

// Set up a 3D box.
rec.log(
"box",
&[
&rerun::Boxes3D::from_half_sizes([(4.0, 2.0, 1.0)])
.with_fill_mode(rerun::FillMode::Solid) as &dyn rerun::AsComponents,
&rerun::Transform3D::default().with_axis_length(10.0),
],
)?;

// Update only the rotation of the box.
for deg in 0..=45 {
let rad = truncated_radians((deg * 4) as f32);
rec.log(
"box",
&rerun::Transform3D::update_fields().with_rotation(rerun::RotationAxisAngle::new(
[0.0, 1.0, 0.0],
rerun::Angle::from_radians(rad),
)),
)?;
}

// Update only the position of the box.
for t in 0..=50 {
rec.log(
"box",
&rerun::Transform3D::update_fields().with_translation([0.0, 0.0, t as f32 / 10.0]),
)?;
}

// Update only the rotation of the box.
for deg in 0..=45 {
let rad = truncated_radians(((deg + 45) * 4) as f32);
rec.log(
"box",
&rerun::Transform3D::update_fields().with_rotation(rerun::RotationAxisAngle::new(
[0.0, 1.0, 0.0],
rerun::Angle::from_radians(rad),
)),
)?;
}

// Clear all of the box's attributes, and reset its axis length.
rec.log(
"box",
&rerun::Transform3D::clear_fields().with_axis_length(15.0),
)?;

Ok(())
}

fn truncated_radians(deg: f32) -> f32 {
((deg.to_radians() * 1000.0) as i32) as f32 / 1000.0
}
4 changes: 4 additions & 0 deletions docs/snippets/snippets.toml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ quick_start = [ # These examples don't have exactly the same implementation.
"archetypes/transform3d_hierarchy" = [ # Uses a lot of trigonometry which is surprisingly easy to get the same on Rust & C++, but not on Python/Numpy
"py",
]
"archetypes/transform3d_partial_updates" = [
"cpp", # TODO(#8583): remove once C++ partial updates APIs have shipped
"py", # TODO(#8582): remove once Python partial updates APIs have shipped
]
"archetypes/instance_poses3d_combined" = [ # TODO(#3235): Slight floating point differences in point grid.
"cpp",
"py",
Expand Down
3 changes: 3 additions & 0 deletions lychee.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,7 @@ exclude = [
'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',
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/transform3d_partial_updates.py',
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/transform3d_partial_updates.cpp',
'https://github.com/rerun-io/rerun/blob/main/docs/snippets/all/archetypes/transform3d_partial_updates.rs',
]

0 comments on commit d747b6c

Please sign in to comment.