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

SDK batching/revamp 2.1: clock example for Rust #2000

Merged
merged 38 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
68d8b0c
version crossbeam at the workspace level
teh-cmc Apr 17, 2023
cb74038
more DataRow size helpers
teh-cmc Apr 26, 2023
a0d9d39
DataTableBatcher
teh-cmc Apr 26, 2023
f46ac72
lints
teh-cmc Apr 26, 2023
5440f76
lints
teh-cmc Apr 26, 2023
c1088c5
self review
teh-cmc Apr 26, 2023
cbf17be
don't expose shutdown to make errors impossible
teh-cmc Apr 26, 2023
e7b42bf
doc
teh-cmc Apr 26, 2023
573de98
backport
teh-cmc Apr 26, 2023
67dc616
backport
teh-cmc Apr 27, 2023
14130c5
introduce RecordingStream
teh-cmc Apr 27, 2023
71a31bd
clean up old stuff from the before time
teh-cmc Apr 27, 2023
649bbe8
self-review
teh-cmc Apr 28, 2023
2b74c3b
ordered data columns in data tables
teh-cmc Apr 28, 2023
34be0a7
tests
teh-cmc Apr 28, 2023
72685fa
even more tests
teh-cmc Apr 28, 2023
067168f
rogue todo
teh-cmc Apr 28, 2023
b8e0065
batching is now a reality
teh-cmc Apr 28, 2023
0e69707
some extra peace of mind
teh-cmc Apr 28, 2023
232bf8d
added Rust clock example
teh-cmc Apr 28, 2023
a7f84c8
revert
teh-cmc Apr 28, 2023
ead5883
Merge branch 'main' into cmc/sdk_revamp/1_batcher
teh-cmc Apr 28, 2023
3f0ec73
Merge branch 'cmc/sdk_revamp/1_batcher' into cmc/sdk_revamp/2_rust_re…
teh-cmc Apr 28, 2023
4bb95ea
Merge branch 'cmc/sdk_revamp/2_rust_revamp' into cmc/sdk_revamp/21_clock
teh-cmc Apr 28, 2023
6e348db
lock shenanigans
teh-cmc Apr 28, 2023
80557c3
Merge branch 'cmc/sdk_revamp/2_rust_revamp' into cmc/sdk_revamp/21_clock
teh-cmc Apr 28, 2023
5e71844
lock shenanigans
teh-cmc Apr 28, 2023
a31285b
Merge branch 'main' into cmc/sdk_revamp/1_batcher
teh-cmc May 3, 2023
ecb7ce5
Merge branch 'cmc/sdk_revamp/1_batcher' into cmc/sdk_revamp/2_rust_re…
teh-cmc May 3, 2023
f64a4dc
Merge branch 'cmc/sdk_revamp/2_rust_revamp' into cmc/sdk_revamp/21_clock
teh-cmc May 3, 2023
8580773
Merge remote-tracking branch 'origin/main' into cmc/sdk_revamp/2_rust…
teh-cmc May 3, 2023
4af3342
merge shenanigans
teh-cmc May 3, 2023
d1e5c19
address PR comments
teh-cmc May 3, 2023
180cdf1
Merge branch 'cmc/sdk_revamp/2_rust_revamp' into cmc/sdk_revamp/21_clock
teh-cmc May 3, 2023
dbab901
fix python example
teh-cmc May 4, 2023
cb4fd66
lock shenanigans
teh-cmc May 4, 2023
ce676d1
Merge remote-tracking branch 'origin/main' into cmc/sdk_revamp/21_clock
teh-cmc May 4, 2023
c2e39a2
self-review
teh-cmc May 4, 2023
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
10 changes: 10 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions examples/python/clock/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
LENGTH_H: Final = 4.0

WIDTH_S: Final = 0.25
WIDTH_M: Final = 0.5
WIDTH_H: Final = 0.7
WIDTH_M: Final = 0.4
WIDTH_H: Final = 0.6


def log_clock(steps: int) -> None:
Expand Down Expand Up @@ -61,7 +61,7 @@ def rotate(angle: float, len: float) -> Tuple[float, float, float]:
point_h = np.array(rotate(math.tau * scaled_h, LENGTH_H))
color_h = (int(255 - (scaled_h * 255)), int(scaled_h * 255), 255, 255)
rr.log_point("world/hours_pt", position=point_h, color=color_h)
rr.log_arrow("world/hours_hand", origin=[0.0, 0.0, 0.0], vector=point_h, color=color_h, width_scale=WIDTH_M)
rr.log_arrow("world/hours_hand", origin=[0.0, 0.0, 0.0], vector=point_h, color=color_h, width_scale=WIDTH_H)


if __name__ == "__main__":
Expand Down
14 changes: 14 additions & 0 deletions examples/rust/clock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "clock"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
publish = false

[dependencies]
rerun = { path = "../../../crates/rerun", features = ["web_viewer"] }

anyhow = "1.0"
clap = { version = "4.0", features = ["derive"] }
glam = "0.22"
117 changes: 117 additions & 0 deletions examples/rust/clock/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! Builds an analog clock using Rerun's `Arrow3D` primitive.
//!
//! This is a great benchmark for many small events.
//!
//! Usage:
//! ```
//! cargo run -p clock -- --help
//! ```

use std::f32::consts::TAU;

use rerun::components::{Arrow3D, Box3D, ColorRGBA, Radius, Vec3D, ViewCoordinates};
use rerun::coordinates::SignedAxis3;
use rerun::time::{Time, TimePoint, TimeType, Timeline};
use rerun::{external::re_log, MsgSender, RecordingStream};

#[derive(Debug, clap::Parser)]
#[clap(author, version, about)]
struct Args {
#[command(flatten)]
rerun: rerun::clap::RerunArgs,

#[clap(long, default_value = "10000")]
steps: usize,
}

fn run(rec_stream: &RecordingStream, args: &Args) -> anyhow::Result<()> {
const LENGTH_S: f32 = 20.0;
const LENGTH_M: f32 = 10.0;
const LENGTH_H: f32 = 4.0;
const WIDTH_S: f32 = 0.25;
const WIDTH_M: f32 = 0.4;
const WIDTH_H: f32 = 0.6;

let view_coords = ViewCoordinates::from_up_and_handedness(
SignedAxis3::POSITIVE_Y,
rerun::coordinates::Handedness::Right,
);
MsgSender::new("world")
.with_timeless(true)
.with_component(&[view_coords])?
.send(rec_stream)?;

MsgSender::new("world/frame")
.with_timeless(true)
.with_component(&[Box3D::new(LENGTH_S, LENGTH_S, 1.0)])?
.send(rec_stream)?;

fn sim_time(at: f64) -> TimePoint {
let timeline_sim_time = Timeline::new("sim_time", TimeType::Time);
let time = Time::from_seconds_since_epoch(at);
[(timeline_sim_time, time.into())].into()
}

fn pos(angle: f32, length: f32) -> Vec3D {
Vec3D::new(length * angle.sin(), length * angle.cos(), 0.0)
}

fn color(angle: f32, blue: u8) -> ColorRGBA {
let c = (angle * 255.0) as u8;
ColorRGBA::from_unmultiplied_rgba(255 - c, c, blue, u8::max(128, blue))
}

fn log_hand(
rec_stream: &RecordingStream,
name: &str,
step: usize,
angle: f32,
length: f32,
width: f32,
blue: u8,
) -> anyhow::Result<()> {
let point = pos(angle * TAU, length);
let color = color(angle, blue);
MsgSender::new(format!("world/{name}_pt"))
.with_timepoint(sim_time(step as _))
.with_component(&[point])?
.with_component(&[color])?
.send(rec_stream)?;
MsgSender::new(format!("world/{name}_hand"))
.with_timepoint(sim_time(step as _))
.with_component(&[Arrow3D {
origin: glam::Vec3::ZERO.into(),
vector: point,
}])?
.with_component(&[color])?
.with_component(&[Radius(width * 0.5)])?
.send(rec_stream)?;

Ok(())
}

for step in 0..args.steps {
#[rustfmt::skip]
log_hand(rec_stream, "seconds", step, (step % 60) as f32 / 60.0, LENGTH_S, WIDTH_S, 0)?;
#[rustfmt::skip]
log_hand(rec_stream, "minutes", step, (step % 3600) as f32 / 3600.0, LENGTH_M, WIDTH_M, 128)?;
#[rustfmt::skip]
log_hand(rec_stream, "hours", step, (step % 43200) as f32 / 43200.0, LENGTH_H, WIDTH_H, 255)?;
Comment on lines +94 to +99
Copy link
Member

Choose a reason for hiding this comment

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

#[rustfmt::skip] doesn't work on a block?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not on expressions, it's been nightly-only since the dawn of time... 😒

}

Ok(())
}

fn main() -> anyhow::Result<()> {
re_log::setup_native_logging();

use clap::Parser as _;
let args = Args::parse();

let default_enabled = true;
args.rerun
.clone()
.run("clock", default_enabled, move |rec_stream| {
run(&rec_stream, &args).unwrap();
})
}