From 8dd4a287eb71482ae2c998bb701a4542a5a3566d Mon Sep 17 00:00:00 2001 From: Don Garrett Date: Fri, 28 Jun 2024 17:21:40 -0500 Subject: [PATCH] Add support for generation based on arguments. --- src/mushroom.rs | 10 +++++++-- src/server.rs | 57 +++++++++++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/mushroom.rs b/src/mushroom.rs index 9707cd6..e2a34a9 100644 --- a/src/mushroom.rs +++ b/src/mushroom.rs @@ -1,6 +1,6 @@ use serde_derive::Serialize; -#[derive(clap::ValueEnum, Clone, Default, Debug, Serialize, PartialEq)] +#[derive(clap::ValueEnum, Clone, Default, Debug, Serialize, PartialEq, FromFormField)] pub enum CapStyle { Cylinder, #[default] @@ -8,20 +8,26 @@ pub enum CapStyle { Pointed, } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone, FromForm)] pub struct Mushroom { /// Stalk height in millimeters. Zero for default. + #[field(default = 66.0)] pub stalk_h: f32, /// Stalk diameter in millimeters. Zero for default. + #[field(default = 5.0)] pub stalk_r: f32, /// Cap height in millimeters. Zero for default. + #[field(default = 34.0)] pub cap_h: f32, /// Stalk diameter in millimeters. Zero for default. + #[field(default = 50.0)] pub cap_r: f32, + /// General shape of the cap. + #[field(default = CapStyle::Dome)] pub cap_style: CapStyle, } diff --git a/src/server.rs b/src/server.rs index 246ece2..782cc02 100644 --- a/src/server.rs +++ b/src/server.rs @@ -4,13 +4,12 @@ use rocket::http::ContentType; use rocket::http::Header; use crate::mushroom; -use crate::mushroom::Mushroom; use crate::render; use crate::translate; pub async fn run_server() { rocket::build() - .mount("/", routes![hello, generate_default]) + .mount("/", routes![hello, generate]) .launch() .await .unwrap(); @@ -21,8 +20,22 @@ fn hello() -> &'static str { "Hello, world!" } +// TODO: Wrap this with memoization via https://docs.rs/michie/latest/michie/ + +/// Do work to generate a binary STL for a given mushroom. +fn generate_binary_stl(mushroom: mushroom::Mushroom) -> Vec { + // Generate the binary STL in a byte array. + let layers = translate::mushroom(&mushroom, 0.1); + let mesh = render::render(&layers, 0.1); + + // Generate / return the byte array. + let mut binary_stl = Vec::::new(); + stl_io::write_stl(&mut binary_stl, mesh.iter()).unwrap(); + return binary_stl; +} + #[derive(Responder)] -enum AsyncBinaryStelResponder { +enum AsyncBinaryStlResponder { #[response(status = 500)] Error(&'static str), BinaryStl { @@ -32,23 +45,16 @@ enum AsyncBinaryStelResponder { }, } -async fn generate_helper(mushroom: Mushroom) -> AsyncBinaryStelResponder { +/// Take a mushroom struct, and generate a Responder with error handling, and header info. +async fn generate_helper(mushroom: mushroom::Mushroom) -> AsyncBinaryStlResponder { let result = rocket::tokio::task::spawn_blocking(move || { - // Describe our mushroom - - // Generate the binary STL in a byte array. - let layers = translate::mushroom(&mushroom, 0.1); - let mesh = render::render(&layers, 0.1); - let mut binary_stl = Vec::::new(); - stl_io::write_stl(&mut binary_stl, mesh.iter()).unwrap(); - // Return the byte array. - return binary_stl; + return generate_binary_stl(mushroom); }) .await; return if result.is_ok() { - AsyncBinaryStelResponder::BinaryStl { + AsyncBinaryStlResponder::BinaryStl { inner: result.unwrap(), header: ContentType::parse_flexible("model/x.stl-binary").unwrap(), filename: Header { @@ -57,19 +63,24 @@ async fn generate_helper(mushroom: Mushroom) -> AsyncBinaryStelResponder { }, } } else { - AsyncBinaryStelResponder::Error("Internal JoinError") + AsyncBinaryStlResponder::Error("Internal JoinError") }; } -#[get("/generate")] -async fn generate_default() -> AsyncBinaryStelResponder { - // Default mushroom to generate +#[get("/generate?&&&&")] +async fn generate( + stalk_h: Option, + stalk_r: Option, + cap_h: Option, + cap_r: Option, + cap_style: Option, +) -> AsyncBinaryStlResponder { let mushroom = mushroom::Mushroom { - stalk_h: 75.0, - stalk_r: 10.0, - cap_h: 25.0, - cap_r: 100.0, - cap_style: mushroom::CapStyle::Dome, + stalk_h: stalk_h.unwrap_or(66.0), + stalk_r: stalk_r.unwrap_or(5.0), + cap_h: cap_h.unwrap_or(34.0), + cap_r: cap_r.unwrap_or(50.0), + cap_style: cap_style.unwrap_or(mushroom::CapStyle::Dome), }; return generate_helper(mushroom).await;