From 21aa36f288dfd4537476892b77fdad8b67d6ddd8 Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Mon, 1 Jul 2024 11:51:50 +0200 Subject: [PATCH] Add C++ point3d_ui_radius demo & C++ radius extensions --- .../definitions/rerun/archetypes/points3d.fbs | 1 + crates/re_types/src/archetypes/points3d.rs | 43 +++++++++++++++++- .../reference/types/archetypes/points3d.md | 12 +++++ .../all/archetypes/point3d_ui_radius.cpp | 27 ++++++++++-- .../all/archetypes/point3d_ui_radius.py | 2 +- .../all/archetypes/point3d_ui_radius.rs | 2 +- rerun_cpp/src/rerun/archetypes/points3d.hpp | 37 +++++++++++++++- rerun_cpp/src/rerun/components/radius.hpp | 17 +++++++ rerun_cpp/src/rerun/components/radius_ext.cpp | 30 +++++++++++++ .../rerun_sdk/rerun/archetypes/points3d.py | 44 ++++++++++++++++++- 10 files changed, 205 insertions(+), 10 deletions(-) create mode 100644 rerun_cpp/src/rerun/components/radius_ext.cpp diff --git a/crates/re_types/definitions/rerun/archetypes/points3d.fbs b/crates/re_types/definitions/rerun/archetypes/points3d.fbs index f7034145be4e..8e2ebf124c2c 100644 --- a/crates/re_types/definitions/rerun/archetypes/points3d.fbs +++ b/crates/re_types/definitions/rerun/archetypes/points3d.fbs @@ -11,6 +11,7 @@ namespace rerun.archetypes; /// /// \example archetypes/point3d_simple !api title="Simple 3D points" image="https://static.rerun.io/point3d_simple/32fb3e9b65bea8bd7ffff95ad839f2f8a157a933/1200w.png" /// \example archetypes/point3d_random title="Randomly distributed 3D points with varying color and radius" image="https://static.rerun.io/point3d_random/7e94e1806d2c381943748abbb3bedb68d564de24/1200w.png" +/// \example archetypes/point3d_ui_radius title="Log points with radii given in ui points" image="https://static.rerun.io/point3d_ui_radius/e051a65b4317438bcaea8d0eee016ac9460b5336/1200w.png" table Points3D ( "attr.rust.derive": "PartialEq", "attr.docs.category": "Spatial 3D", diff --git a/crates/re_types/src/archetypes/points3d.rs b/crates/re_types/src/archetypes/points3d.rs index 0a57de6c2b30..eb8c4bf2b6ee 100644 --- a/crates/re_types/src/archetypes/points3d.rs +++ b/crates/re_types/src/archetypes/points3d.rs @@ -24,7 +24,7 @@ use ::re_types_core::{DeserializationError, DeserializationResult}; /// **Archetype**: A 3D point cloud with positions and optional colors, radii, labels, etc. /// -/// ## Example +/// ## Examples /// /// ### Randomly distributed 3D points with varying color and radius /// ```ignore @@ -57,6 +57,47 @@ use ::re_types_core::{DeserializationError, DeserializationResult}; /// /// /// +/// +/// ### Log points with radii given in ui points +/// ```ignore +/// fn main() -> Result<(), Box> { +/// let rec = rerun::RecordingStreamBuilder::new("rerun_example_points3d_ui_radius").spawn()?; +/// +/// // Two blue points with scene unit radii of 0.1 and 0.3. +/// rec.log( +/// "scene_unit_points", +/// &rerun::Points3D::new([(0.0, 1.0, 0.0), (1.0, 1.0, 1.0)]) +/// // By default, radii are interpreted as world-space units. +/// .with_radii([0.1, 0.3]) +/// .with_colors([rerun::Color::from_rgb(0, 0, 255)]), +/// ); +/// +/// // Two red points with ui point radii of 40 and 60. +/// // Ui points are independent of zooming in Views, but are sensitive to the application ui scaling. +/// // For 100% ui scaling, ui points are equal to pixels. +/// rec.log( +/// "ui_points_points", +/// &rerun::Points3D::new([(0.0, 0.0, 0.0), (1.0, 0.0, 1.0)]) +/// // rerun::Radius::new_ui_points produces a radius that the viewer interprets as given in ui points. +/// .with_radii([ +/// rerun::Radius::new_ui_points(40.0), +/// rerun::Radius::new_ui_points(60.0), +/// ]) +/// .with_colors([rerun::Color::from_rgb(255, 0, 0)]), +/// ); +/// +/// Ok(()) +/// } +/// ``` +///
+/// +/// +/// +/// +/// +/// +/// +///
#[derive(Clone, Debug, PartialEq)] pub struct Points3D { /// All the 3D positions at which the point cloud shows points. diff --git a/docs/content/reference/types/archetypes/points3d.md b/docs/content/reference/types/archetypes/points3d.md index fe8866593663..553c81e04178 100644 --- a/docs/content/reference/types/archetypes/points3d.md +++ b/docs/content/reference/types/archetypes/points3d.md @@ -48,3 +48,15 @@ snippet: archetypes/point3d_random +### Log points with radii given in ui points + +snippet: archetypes/point3d_ui_radius + + + + + + + + + diff --git a/docs/snippets/all/archetypes/point3d_ui_radius.cpp b/docs/snippets/all/archetypes/point3d_ui_radius.cpp index 6925abd799ef..2d08de1416c1 100644 --- a/docs/snippets/all/archetypes/point3d_ui_radius.cpp +++ b/docs/snippets/all/archetypes/point3d_ui_radius.cpp @@ -1,4 +1,4 @@ -// Log some very simple points. +// Log some points with ui points & scene unit radii. #include @@ -6,7 +6,26 @@ int main() { const auto rec = rerun::RecordingStream("rerun_example_points3d_ui_radius"); rec.spawn().exit_on_failure(); - rec.log("points", rerun::Points3D({{0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}})); -} + // Two blue points with scene unit radii of 0.1 and 0.3. + rec.log( + "scene_unit_points", + rerun::Points3D({{0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}}) + // By default, radii are interpreted as world-space units. + .with_radii({0.1f, 0.3f}) + .with_colors(rerun::Color(0, 0, 255)) + ); -// TODO: unfinished + // Two red points with ui point radii of 40 and 60. + // Ui points are independent of zooming in Views, but are sensitive to the application ui scaling. + // For 100% ui scaling, ui points are equal to pixels. + rec.log( + "ui_points_points", + rerun::Points3D({{0.0f, 0.0f, 0.0}, {1.0f, 0.0f, 1.0f}}) + // rerun::Radius::ui_points produces radii that the viewer interprets as given in ui points. + .with_radii({ + rerun::Radius::ui_points(40.0f), + rerun::Radius::ui_points(60.0f), + }) + .with_colors(rerun::Color(255, 0, 0)) + ); +} diff --git a/docs/snippets/all/archetypes/point3d_ui_radius.py b/docs/snippets/all/archetypes/point3d_ui_radius.py index ac2d736a796f..13bfbc7c7940 100644 --- a/docs/snippets/all/archetypes/point3d_ui_radius.py +++ b/docs/snippets/all/archetypes/point3d_ui_radius.py @@ -22,7 +22,7 @@ "ui_points_points", rr.Points3D( [[0, 0, 0], [1, 0, 1]], - # rr.Radius.ui_points produces radii that the viewer interprets as given in ui units. + # rr.Radius.ui_points produces radii that the viewer interprets as given in ui points. radii=rr.Radius.ui_points([40.0, 60.0]), colors=[255, 0, 0], ), diff --git a/docs/snippets/all/archetypes/point3d_ui_radius.rs b/docs/snippets/all/archetypes/point3d_ui_radius.rs index 88ecff6d9a8b..7de7e30e79b3 100644 --- a/docs/snippets/all/archetypes/point3d_ui_radius.rs +++ b/docs/snippets/all/archetypes/point3d_ui_radius.rs @@ -18,7 +18,7 @@ fn main() -> Result<(), Box> { rec.log( "ui_points_points", &rerun::Points3D::new([(0.0, 0.0, 0.0), (1.0, 0.0, 1.0)]) - // rr.Radius.ui_points produces radii that the viewer interprets as given in ui units. + // rerun::Radius::new_ui_points produces a radius that the viewer interprets as given in ui points. .with_radii([ rerun::Radius::new_ui_points(40.0), rerun::Radius::new_ui_points(60.0), diff --git a/rerun_cpp/src/rerun/archetypes/points3d.hpp b/rerun_cpp/src/rerun/archetypes/points3d.hpp index f586ebf87bf5..5188eaa2485a 100644 --- a/rerun_cpp/src/rerun/archetypes/points3d.hpp +++ b/rerun_cpp/src/rerun/archetypes/points3d.hpp @@ -23,7 +23,7 @@ namespace rerun::archetypes { /// **Archetype**: A 3D point cloud with positions and optional colors, radii, labels, etc. /// - /// ## Example + /// ## Examples /// /// ### Randomly distributed 3D points with varying color and radius /// ![image](https://static.rerun.io/point3d_random/7e94e1806d2c381943748abbb3bedb68d564de24/full.png) @@ -63,6 +63,41 @@ namespace rerun::archetypes { /// rec.log("random", rerun::Points3D(points3d).with_colors(colors).with_radii(radii)); /// } /// ``` + /// + /// ### Log points with radii given in ui points + /// ![image](https://static.rerun.io/point3d_ui_radius/e051a65b4317438bcaea8d0eee016ac9460b5336/full.png) + /// + /// ```cpp + /// #include + /// + /// int main() { + /// const auto rec = rerun::RecordingStream("rerun_example_points3d_ui_radius"); + /// rec.spawn().exit_on_failure(); + /// + /// // Two blue points with scene unit radii of 0.1 and 0.3. + /// rec.log( + /// "scene_unit_points", + /// rerun::Points3D({{0.0f, 1.0f, 0.0f}, {1.0f, 1.0f, 1.0f}}) + /// // By default, radii are interpreted as world-space units. + /// .with_radii({0.1f, 0.3f}) + /// .with_colors(rerun::Color(0, 0, 255)) + /// ); + /// + /// // Two red points with ui point radii of 40 and 60. + /// // Ui points are independent of zooming in Views, but are sensitive to the application ui scaling. + /// // For 100% ui scaling, ui points are equal to pixels. + /// rec.log( + /// "ui_points_points", + /// rerun::Points3D({{0.0f, 0.0f, 0.0}, {1.0f, 0.0f, 1.0f}}) + /// // rerun::Radius::ui_points produces radii that the viewer interprets as given in ui points. + /// .with_radii([ + /// rerun::Radius::ui_points(40.0f), + /// rerun::Radius::ui_points(60.0f), + /// ]) + /// .with_colors(rerun::Color(255, 0, 0)) + /// ); + /// } + /// ``` struct Points3D { /// All the 3D positions at which the point cloud shows points. Collection positions; diff --git a/rerun_cpp/src/rerun/components/radius.hpp b/rerun_cpp/src/rerun/components/radius.hpp index 2cf69c1a2b3b..b33d51b5d70a 100644 --- a/rerun_cpp/src/rerun/components/radius.hpp +++ b/rerun_cpp/src/rerun/components/radius.hpp @@ -31,6 +31,23 @@ namespace rerun::components { struct Radius { float value; + public: + // Extensions to generated type defined in 'radius_ext.cpp' + + /// Creates a new radius in scene units. + /// + /// Values passed must be finite positive. + static Radius scene_units(float radius_in_scene_units) { + return Radius(radius_in_scene_units); + } + + /// Creates a new radius in ui points. + /// + /// Values passed must be finite positive. + static Radius ui_points(float radius_in_ui_points) { + return Radius(-radius_in_ui_points); + } + public: Radius() = default; diff --git a/rerun_cpp/src/rerun/components/radius_ext.cpp b/rerun_cpp/src/rerun/components/radius_ext.cpp new file mode 100644 index 000000000000..b5c3df6db725 --- /dev/null +++ b/rerun_cpp/src/rerun/components/radius_ext.cpp @@ -0,0 +1,30 @@ +//#define EDIT_EXTENSION +#if EDIT_EXTENSION + +#include "radius.hpp" + +// Uncomment for better auto-complete while editing the extension. + +namespace rerun { + namespace components { + // + + /// Creates a new radius in scene units. + /// + /// Values passed must be finite positive. + static Radius scene_units(float radius_in_scene_units) { + return Radius(radius_in_scene_units); + } + + /// Creates a new radius in ui points. + /// + /// Values passed must be finite positive. + static Radius ui_points(float radius_in_ui_points) { + return Radius(-radius_in_ui_points); + } + + // + } // namespace components +} // namespace rerun + +#endif diff --git a/rerun_py/rerun_sdk/rerun/archetypes/points3d.py b/rerun_py/rerun_sdk/rerun/archetypes/points3d.py index 4324c44a5917..b8a0e96df06a 100644 --- a/rerun_py/rerun_sdk/rerun/archetypes/points3d.py +++ b/rerun_py/rerun_sdk/rerun/archetypes/points3d.py @@ -21,8 +21,8 @@ class Points3D(Points3DExt, Archetype): """ **Archetype**: A 3D point cloud with positions and optional colors, radii, labels, etc. - Example - ------- + Examples + -------- ### Randomly distributed 3D points with varying color and radius: ```python import rerun as rr @@ -47,6 +47,46 @@ class Points3D(Points3DExt, Archetype): + ### Log points with radii given in ui points: + ```python + import rerun as rr + + rr.init("rerun_example_points3d_ui_radius", spawn=True) + + # Two blue points with scene unit radii of 0.1 and 0.3. + rr.log( + "scene_unit_points", + rr.Points3D( + [[0, 1, 0], [1, 1, 1]], + # By default, radii are interpreted as world-space units. + radii=[0.1, 0.3], + colors=[0, 0, 255], + ), + ) + + # Two red points with ui point radii of 40 and 60. + # Ui points are independent of zooming in Views, but are sensitive to the application ui scaling. + # For 100% ui scaling, ui points are equal to pixels. + rr.log( + "ui_points_points", + rr.Points3D( + [[0, 0, 0], [1, 0, 1]], + # rr.Radius.ui_points produces radii that the viewer interprets as given in ui points. + radii=rr.Radius.ui_points([40.0, 60.0]), + colors=[255, 0, 0], + ), + ) + ``` +
+ + + + + + + +
+ """ # __init__ can be found in points3d_ext.py