From 9e777546c8325a2aaabb16e6afdb04b4cc1d4e91 Mon Sep 17 00:00:00 2001 From: h3mosphere Date: Fri, 7 Apr 2023 11:39:54 +1000 Subject: [PATCH] Minimal example, with a couple of command line arguments (--num-points-per-axis --radius) * Allows connecting to remote server through rerun's RerunArgs. --- Cargo.lock | 10 ++++ examples/rust/minimal_options/Cargo.toml | 14 +++++ examples/rust/minimal_options/src/main.rs | 66 +++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 examples/rust/minimal_options/Cargo.toml create mode 100644 examples/rust/minimal_options/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index e7986f73779c..d048d60a6b3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2719,6 +2719,16 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "minimal_options" +version = "0.4.0" +dependencies = [ + "anyhow", + "clap 4.1.4", + "glam", + "rerun", +] + [[package]] name = "miniz_oxide" version = "0.5.4" diff --git a/examples/rust/minimal_options/Cargo.toml b/examples/rust/minimal_options/Cargo.toml new file mode 100644 index 000000000000..9f5888dd8d69 --- /dev/null +++ b/examples/rust/minimal_options/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "minimal_options" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +publish = false + +[dependencies] +rerun = { workspace = true, features = ["web_viewer"] } + +anyhow.workspace = true +clap = { workspace = true, features = ["derive"] } +glam.workspace = true diff --git a/examples/rust/minimal_options/src/main.rs b/examples/rust/minimal_options/src/main.rs new file mode 100644 index 000000000000..c6045998e498 --- /dev/null +++ b/examples/rust/minimal_options/src/main.rs @@ -0,0 +1,66 @@ +//! Demonstrates how to accept arguments and connect to running rerun servers. +//! +//! Usage: +//! ``` +//! cargo run -p minimal_options -- --help +//! ``` + +use rerun::components::{ColorRGBA, Point3D}; +use rerun::time::{TimeType, Timeline}; +use rerun::{external::re_log, MsgSender, Session}; + +use rerun::demo_util::grid; + +#[derive(Debug, clap::Parser)] +#[clap(author, version, about)] +struct Args { + #[command(flatten)] + rerun: rerun::clap::RerunArgs, + + #[clap(long, default_value = "10")] + num_points_per_axis: usize, + + #[clap(long, default_value = "5.0")] + radius: f32, +} + +fn run(session: &Session, args: &Args) -> anyhow::Result<()> { + let timeline_keyframe = Timeline::new("keyframe", TimeType::Sequence); + + let points = grid( + glam::Vec3::splat(-args.radius), + glam::Vec3::splat(args.radius), + args.num_points_per_axis, + ) + .map(Point3D::from) + .collect::>(); + let colors = grid( + glam::Vec3::ZERO, + glam::Vec3::splat(255.0), + args.num_points_per_axis, + ) + .map(|v| ColorRGBA::from_rgb(v.x as u8, v.y as u8, v.z as u8)) + .collect::>(); + + MsgSender::new("my_points") + .with_component(&points)? + .with_component(&colors)? + .with_time(timeline_keyframe, 0) + .send(session)?; + + 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("minimal_options", default_enabled, move |session| { + run(&session, &args).unwrap(); + }) +}