Skip to content

Commit

Permalink
Demo helpers & examples for Getting Started (rs) (rerun-io#1260)
Browse files Browse the repository at this point in the history
* new helpers for creating an appid

* introducing Session::init

* update all examples

* cranky

* Rust SDK: `save()`/`--save` support (rerun-io#1227)

* implement spawn() support

* api_demo_rs: spawn support

* objectron_rs: spawn support

* raw_mesh_rs: spawn support

* slightly better doc, maybe

* Update crates/re_sdk/src/session.rs

Co-authored-by: Emil Ernerfeldt <[email protected]>

* Update crates/re_sdk/src/session.rs

Co-authored-by: Emil Ernerfeldt <[email protected]>

* addressed PR comments

* backporting arg refactorings from next PR

* some vertical whitespace for emil

* implement Session::save

* api_demo_rs: handle --save

* objectron_rs: handle --save

* raw_mesh_rs: handle --save

* todo

* don't add wake_up_ui_thread_on_each_msg on wasm32 builds (we have no threads!)

* who are you and what do you want from me

* Update crates/re_sdk/src/session.rs

Co-authored-by: Emil Ernerfeldt <[email protected]>

* Update crates/re_sdk/src/session.rs

Co-authored-by: Emil Ernerfeldt <[email protected]>

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>

* Rust SDK: `--serve` support (rerun-io#1228)

* implement spawn() support

* api_demo_rs: spawn support

* objectron_rs: spawn support

* raw_mesh_rs: spawn support

* slightly better doc, maybe

* Update crates/re_sdk/src/session.rs

Co-authored-by: Emil Ernerfeldt <[email protected]>

* Update crates/re_sdk/src/session.rs

Co-authored-by: Emil Ernerfeldt <[email protected]>

* addressed PR comments

* backporting arg refactorings from next PR

* some vertical whitespace for emil

* implement Session::save

* api_demo_rs: handle --save

* objectron_rs: handle --save

* raw_mesh_rs: handle --save

* todo

* api_demo_rs: --serve

* objectron_rs: --serve

* raw_mesh_rs: --serve

* less verbose websocket logs

* serve or save

* don't add wake_up_ui_thread_on_each_msg on wasm32 builds (we have no threads!)

* who are you and what do you want from me

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>

* add color_spiral demo util

* add dna rust example

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
teh-cmc and emilk authored Feb 14, 2023
1 parent 0ce3d83 commit 4b24364
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Cargo.lock

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

36 changes: 36 additions & 0 deletions crates/re_sdk/src/demo_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,39 @@ pub fn grid(from: glam::Vec3, to: glam::Vec3, n: usize) -> impl Iterator<Item =
.flat_map(move |y| linspace(from.x, to.x, n).map(move |x| (x, y, z).into()))
})
}

/// Create a spiral of points with colors along the Z axis.
///
/// * `num_points`: Total number of points.
/// * `radius`: The radius of the spiral.
/// * `angular_step`: The factor applied between each step along the trigonemetric circle.
/// * `angular_offset`: Offsets the starting position on the trigonemetric circle.
/// * `z_step`: The factor applied between between each step along the Z axis.
#[cfg(all(feature = "glam", feature = "re_viewer"))]
pub fn color_spiral(
num_points: usize,
radius: f32,
angular_step: f32,
angular_offset: f32,
z_step: f32,
) -> (Vec<glam::Vec3>, Vec<[u8; 4]>) {
use std::f32::consts::TAU;
let points = (0..num_points)
.map(move |i| {
let angle = i as f32 * angular_step * TAU + angular_offset;
glam::Vec3::new(
angle.sin() * radius,
angle.cos() * radius,
i as f32 * z_step,
)
})
.collect();

let colors = (0..num_points)
.map(move |i| {
re_viewer::color_map::turbo_color_map(i as f32 / num_points as f32).to_array()
})
.collect();

(points, colors)
}
1 change: 1 addition & 0 deletions crates/re_viewer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod misc;
mod remote_viewer_app;
mod ui;

pub use self::misc::color_map;
pub(crate) use misc::{mesh_loader, Item, TimeControl, TimeView, ViewerContext};
pub(crate) use ui::{event_log_view, memory_panel, selection_panel, time_panel, UiVerbosity};

Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/misc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod app_options;
pub mod caches;
pub(crate) mod color_map;
pub mod color_map;
mod item;
pub(crate) mod mesh_loader;
mod selection_state;
Expand Down
13 changes: 13 additions & 0 deletions examples/rust/dna/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "dna"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
publish = false

[dependencies]
rerun = { workspace = true, features = ["glam"] }

itertools = "0.10"
rand = "0.8"
100 changes: 100 additions & 0 deletions examples/rust/dna/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! The example from our Getting Started page.
use std::f32::consts::TAU;

use itertools::Itertools as _;

use rerun::components::{
ColorRGBA, LineStrip3D, Point3D, Quaternion, Radius, Rigid3, Transform, Vec3D,
};
use rerun::demo_util::{bounce_lerp, color_spiral};
use rerun::external::glam;
use rerun::time::{Time, TimeType, Timeline};
use rerun::{MsgSender, Session};

const NUM_POINTS: usize = 100;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut session = Session::init("DNA Abacus", true);

let stable_time = Timeline::new("stable_time", TimeType::Time);

let (points1, colors1) = color_spiral(NUM_POINTS, 2.0, 0.02, 0.0, 0.1);
let (points2, colors2) = color_spiral(NUM_POINTS, 2.0, 0.02, TAU * 0.5, 0.1);

MsgSender::new("dna/structure/left")
.with_time(stable_time, 0)
.with_component(&points1.iter().copied().map(Point3D::from).collect_vec())?
.with_component(&colors1.iter().copied().map(ColorRGBA::from).collect_vec())?
.with_splat(Radius(0.08))?
.send(&mut session)?;

MsgSender::new("dna/structure/right")
.with_time(stable_time, 0)
.with_component(&points2.iter().copied().map(Point3D::from).collect_vec())?
.with_component(&colors2.iter().copied().map(ColorRGBA::from).collect_vec())?
.with_splat(Radius(0.08))?
.send(&mut session)?;

let scaffolding = points1
.iter()
.interleave(points2.iter())
.copied()
.map(Vec3D::from)
.chunks(2)
.into_iter()
.map(|positions| LineStrip3D(positions.collect_vec()))
.collect_vec();
MsgSender::new("dna/structure/scaffolding")
.with_time(stable_time, 0)
.with_component(&scaffolding)?
.with_splat(ColorRGBA::from([128, 128, 128, 255]))?
.send(&mut session)?;

use rand::Rng as _;
let mut rng = rand::thread_rng();
let offsets = (0..NUM_POINTS).map(|_| rng.gen::<f32>()).collect_vec();

for i in 0..400 {
let time = i as f32 * 0.01;

let times = offsets.iter().map(|offset| time + offset).collect_vec();
let (beads, colors): (Vec<_>, Vec<_>) = points1
.iter()
.interleave(points2.iter())
.copied()
.chunks(2)
.into_iter()
.enumerate()
.map(|(n, mut points)| {
let (p1, p2) = (points.next().unwrap(), points.next().unwrap());
let c = bounce_lerp(80.0, 230.0, times[n] * 2.0) as u8;
(
Point3D::from(bounce_lerp(p1, p2, times[n])),
ColorRGBA::from_rgb(c, c, c),
)
})
.unzip();
MsgSender::new("dna/structure/scaffolding/beads")
.with_time(stable_time, Time::from_seconds_since_epoch(time as _))
.with_component(&beads)?
.with_component(&colors)?
.with_splat(Radius(0.06))?
.send(&mut session)?;

MsgSender::new("dna/structure")
.with_time(stable_time, Time::from_seconds_since_epoch(time as _))
.with_component(&[Transform::Rigid3(Rigid3 {
rotation: Quaternion::from(glam::Quat::from_axis_angle(
glam::Vec3::Z,
time / 4.0 * TAU,
)),
..Default::default()
})])?
.send(&mut session)?;
}

session.show()?;

Ok(())
}

0 comments on commit 4b24364

Please sign in to comment.