Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New code snippet for Transform3D demonstrating an animated hierarchy #6851

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace rerun.archetypes;
/// A 3D transform.
///
/// \example archetypes/transform3d_simple title="Variety of 3D transforms" image="https://static.rerun.io/transform3d_simple/141368b07360ce3fcb1553079258ae3f42bdb9ac/1200w.png"
/// \example archetypes/transform3d_hierarchy title="Transform hierarchy" image="https://static.rerun.io/transform_hierarchy/cb7be7a5a31fcb2efc02ba38e434849248f87554/1200w.png"
table Transform3D (
"attr.rust.derive": "PartialEq",
"attr.docs.category": "Spatial 3D",
Expand Down
95 changes: 94 additions & 1 deletion crates/store/re_types/src/archetypes/transform3d.rs

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

14 changes: 13 additions & 1 deletion docs/content/reference/types/archetypes/transform3d.md

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

81 changes: 81 additions & 0 deletions docs/snippets/all/archetypes/transform3d_hierarchy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Log different transforms between three arrows.

#include <rerun.hpp>

constexpr float TAU = 6.28318530717958647692528676655900577f;

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

rec.set_time_seconds("sim_time", 0.0);

// Planetary motion is typically in the XY plane.
rec.log_static("/", rerun::ViewCoordinates::RIGHT_HAND_Z_UP);

// Setup points, all are in the center of their own space:
rec.log(
"sun",
rerun::Points3D({{0.0f, 0.0f, 0.0f}})
.with_radii({1.0f})
.with_colors({rerun::Color(255, 200, 10)})
);
rec.log(
"sun/planet",
rerun::Points3D({{0.0f, 0.0f, 0.0f}})
.with_radii({0.4f})
.with_colors({rerun::Color(40, 80, 200)})
);
rec.log(
"sun/planet/moon",
rerun::Points3D({{0.0f, 0.0f, 0.0f}})
.with_radii({0.15f})
.with_colors({rerun::Color(180, 180, 180)})
);

// Draw fixed paths where the planet & moon move.
float d_planet = 6.0f;
float d_moon = 3.0f;
std::vector<std::array<float, 3>> planet_path, moon_path;
for (int i = 0; i <= 100; i++) {
float angle = static_cast<float>(i) * 0.01f * TAU;
float circle_x = std::sin(angle);
float circle_y = std::cos(angle);
planet_path.push_back({circle_x * d_planet, circle_y * d_planet, 0.0f});
moon_path.push_back({circle_x * d_moon, circle_y * d_moon, 0.0f});
}
rec.log("sun/planet_path", rerun::LineStrips3D(rerun::LineStrip3D(planet_path)));
rec.log("sun/planet/moon_path", rerun::LineStrips3D(rerun::LineStrip3D(moon_path)));

// Movement via transforms.
for (int i = 0; i < 6 * 120; i++) {
float time = static_cast<float>(i) / 120.0f;
rec.set_time_seconds("sim_time", time);
float r_moon = time * 5.0f;
float r_planet = time * 2.0f;

rec.log(
"sun/planet",
rerun::Transform3D(
{std::sin(r_planet) * d_planet, std::cos(r_planet) * d_planet, 0.0f},
rerun::RotationAxisAngle{
{1.0, 0.0f, 0.0f},
rerun::Angle::degrees(20.0f),
}
)
);
rec.log(
"sun/planet/moon",
rerun::Transform3D(
{
std::cos(r_moon) * d_moon,
std::sin(r_moon) * d_moon,
0.0f,
},
true
)
);
}

// TODO(#5521): log two space views as in the python example
}
53 changes: 53 additions & 0 deletions docs/snippets/all/archetypes/transform3d_hierarchy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Logs a transforms transform hierarchy."""

import numpy as np
import rerun as rr
import rerun.blueprint as rrb

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

rr.set_time_seconds("sim_time", 0)

# Planetary motion is typically in the XY plane.
rr.log("/", rr.ViewCoordinates.RIGHT_HAND_Z_UP, static=True)

# Setup points, all are in the center of their own space:
# TODO(#1361): Should use spheres instead of points.
rr.log("sun", rr.Points3D([0.0, 0.0, 0.0], radii=1.0, colors=[255, 200, 10]))
rr.log("sun/planet", rr.Points3D([0.0, 0.0, 0.0], radii=0.4, colors=[40, 80, 200]))
rr.log("sun/planet/moon", rr.Points3D([0.0, 0.0, 0.0], radii=0.15, colors=[180, 180, 180]))

# Draw fixed paths where the planet & moon move.
d_planet = 6.0
d_moon = 3.0
angles = np.arange(0.0, 1.01, 0.01) * np.pi * 2
circle = np.array([np.sin(angles), np.cos(angles), angles * 0.0]).transpose()
rr.log("sun/planet_path", rr.LineStrips3D(circle * d_planet))
rr.log("sun/planet/moon_path", rr.LineStrips3D(circle * d_moon))

# Movement via transforms.
for i in range(0, 6 * 120):
time = i / 120.0
rr.set_time_seconds("sim_time", time)
r_moon = time * 5.0
r_planet = time * 2.0

rr.log(
"sun/planet",
rr.Transform3D(
translation=[np.sin(r_planet) * d_planet, np.cos(r_planet) * d_planet, 0.0],
rotation=rr.RotationAxisAngle(axis=(1, 0, 0), degrees=20),
),
)
rr.log(
"sun/planet/moon",
rr.Transform3D(
translation=[np.cos(r_moon) * d_moon, np.sin(r_moon) * d_moon, 0.0],
from_parent=True,
),
)

# One space with the sun in the center, and another one with the planet.
rr.send_blueprint(
rrb.Horizontal(rrb.Spatial3DView(origin="sun"), rrb.Spatial3DView(origin="sun/planet", contents="sun/**"))
)
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
82 changes: 82 additions & 0 deletions docs/snippets/all/archetypes/transform3d_hierarchy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! Log different transforms between three arrows.

fn main() -> Result<(), Box<dyn std::error::Error>> {
let rec = rerun::RecordingStreamBuilder::new("rerun_example_transform3d_hierarchy").spawn()?;

rec.set_time_seconds("sim_time", 0.0);

// Planetary motion is typically in the XY plane.
rec.log_static("/", &rerun::ViewCoordinates::RIGHT_HAND_Z_UP)?;

// Setup points, all are in the center of their own space:
rec.log(
"sun",
&rerun::Points3D::new([[0.0, 0.0, 0.0]])
.with_radii([1.0])
.with_colors([rerun::Color::from_rgb(255, 200, 10)]),
)?;
rec.log(
"sun/planet",
&rerun::Points3D::new([[0.0, 0.0, 0.0]])
.with_radii([0.4])
.with_colors([rerun::Color::from_rgb(40, 80, 200)]),
)?;
rec.log(
"sun/planet/moon",
&rerun::Points3D::new([[0.0, 0.0, 0.0]])
.with_radii([0.15])
.with_colors([rerun::Color::from_rgb(180, 180, 180)]),
)?;

// Draw fixed paths where the planet & moon move.
let d_planet = 6.0;
let d_moon = 3.0;
let angles = (0..=100).map(|i| i as f32 * 0.01 * std::f32::consts::TAU);
let circle: Vec<_> = angles.map(|angle| [angle.sin(), angle.cos()]).collect();
rec.log(
"sun/planet_path",
&rerun::LineStrips3D::new([rerun::LineStrip3D::from_iter(
circle
.iter()
.map(|p| [p[0] * d_planet, p[1] * d_planet, 0.0]),
)]),
)?;
rec.log(
"sun/planet/moon_path",
&rerun::LineStrips3D::new([rerun::LineStrip3D::from_iter(
circle.iter().map(|p| [p[0] * d_moon, p[1] * d_moon, 0.0]),
)]),
)?;

// Movement via transforms.
for i in 0..(6 * 120) {
let time = i as f32 / 120.0;
rec.set_time_seconds("sim_time", time);
let r_moon = time * 5.0;
let r_planet = time * 2.0;

rec.log(
"sun/planet",
&rerun::Transform3D::from_translation_rotation(
[r_planet.sin() * d_planet, r_planet.cos() * d_planet, 0.0],
rerun::RotationAxisAngle {
axis: [1.0, 0.0, 0.0].into(),
angle: rerun::Angle::Degrees(20.0),
},
),
)?;
rec.log(
"sun/planet/moon",
&rerun::Transform3D::from_translation([
r_moon.cos() * d_moon,
r_moon.sin() * d_moon,
0.0,
])
.from_parent(),
)?;
}

// TODO(#5521): log two space views as in the python example

Ok(())
}
2 changes: 1 addition & 1 deletion docs/snippets/compare_snippet_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def main() -> None:
print("----------------------------------------------------------")
print("Build rerun_c & rerun_cpp…")
start_time = time.time()
run(["pixi", "run", "cpp-build-snippets"])
run(["pixi", "run", "-e", "cpp", "cpp-build-snippets"])
elapsed = time.time() - start_time
print(f"rerun-sdk for C++ built in {elapsed:.1f} seconds")
print("")
Expand Down
4 changes: 3 additions & 1 deletion docs/snippets/snippets.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@
"py",
"rust",
]

"archetypes/transform3d_hierarchy" = [ # Uses a lot of trignometry which is suprisingly easy to get the same on Rust & C++, but not on Python/Numpy

Check warning on line 138 in docs/snippets/snippets.toml

View workflow job for this annotation

GitHub Actions / Checks / Spell Check

"trignometry" should be "trigonometry".

Check warning on line 138 in docs/snippets/snippets.toml

View workflow job for this annotation

GitHub Actions / Checks / Spell Check

"suprisingly" should be "surprisingly".
Copy link
Member

Choose a reason for hiding this comment

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

I remember having to deal with these kinds of shenanigans in the past -- iirc it's because numpy defaults to f64 for every computation internally. I don't remember if there's an easy way to change that.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah I tried to tell it to use f32 on its arrays, but it got fiddly - definitely got closer but not to the exact result

"py",
]

# `$config_dir` will be replaced with the absolute path of `docs/snippets`.
[extra_args]
Expand Down
Loading
Loading