Add support for generation based on arguments.
This commit is contained in:
1 parent
8fa25de7bb
commit
8dd4a287eb
2 files changed
+42
-25
No files matched your search
+8
-2
@@ -1,6 +1,6 @@
|
|||||||
use serde_derive::Serialize;
|
use serde_derive::Serialize;
|
||||||
|
|
||||||
#[derive(clap::ValueEnum, Clone, Default, Debug, Serialize, PartialEq)]
|
#[derive(clap::ValueEnum, Clone, Default, Debug, Serialize, PartialEq, FromFormField)]
|
||||||
pub enum CapStyle {
|
pub enum CapStyle {
|
||||||
Cylinder,
|
Cylinder,
|
||||||
#[default]
|
#[default]
|
||||||
@@ -8,20 +8,26 @@ pub enum CapStyle {
|
|||||||
Pointed,
|
Pointed,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Clone, FromForm)]
|
||||||
pub struct Mushroom {
|
pub struct Mushroom {
|
||||||
/// Stalk height in millimeters. Zero for default.
|
/// Stalk height in millimeters. Zero for default.
|
||||||
|
#[field(default = 66.0)]
|
||||||
pub stalk_h: f32,
|
pub stalk_h: f32,
|
||||||
|
|
||||||
/// Stalk diameter in millimeters. Zero for default.
|
/// Stalk diameter in millimeters. Zero for default.
|
||||||
|
#[field(default = 5.0)]
|
||||||
pub stalk_r: f32,
|
pub stalk_r: f32,
|
||||||
|
|
||||||
/// Cap height in millimeters. Zero for default.
|
/// Cap height in millimeters. Zero for default.
|
||||||
|
#[field(default = 34.0)]
|
||||||
pub cap_h: f32,
|
pub cap_h: f32,
|
||||||
|
|
||||||
/// Stalk diameter in millimeters. Zero for default.
|
/// Stalk diameter in millimeters. Zero for default.
|
||||||
|
#[field(default = 50.0)]
|
||||||
pub cap_r: f32,
|
pub cap_r: f32,
|
||||||
|
|
||||||
|
/// General shape of the cap.
|
||||||
|
#[field(default = CapStyle::Dome)]
|
||||||
pub cap_style: CapStyle,
|
pub cap_style: CapStyle,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+34
-23
@@ -4,13 +4,12 @@ use rocket::http::ContentType;
|
|||||||
use rocket::http::Header;
|
use rocket::http::Header;
|
||||||
|
|
||||||
use crate::mushroom;
|
use crate::mushroom;
|
||||||
use crate::mushroom::Mushroom;
|
|
||||||
use crate::render;
|
use crate::render;
|
||||||
use crate::translate;
|
use crate::translate;
|
||||||
|
|
||||||
pub async fn run_server() {
|
pub async fn run_server() {
|
||||||
rocket::build()
|
rocket::build()
|
||||||
.mount("/", routes![hello, generate_default])
|
.mount("/", routes![hello, generate])
|
||||||
.launch()
|
.launch()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -21,8 +20,22 @@ fn hello() -> &'static str {
|
|||||||
"Hello, world!"
|
"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<u8> {
|
||||||
|
// 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::<u8>::new();
|
||||||
|
stl_io::write_stl(&mut binary_stl, mesh.iter()).unwrap();
|
||||||
|
return binary_stl;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Responder)]
|
#[derive(Responder)]
|
||||||
enum AsyncBinaryStelResponder {
|
enum AsyncBinaryStlResponder {
|
||||||
#[response(status = 500)]
|
#[response(status = 500)]
|
||||||
Error(&'static str),
|
Error(&'static str),
|
||||||
BinaryStl {
|
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 || {
|
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::<u8>::new();
|
|
||||||
stl_io::write_stl(&mut binary_stl, mesh.iter()).unwrap();
|
|
||||||
|
|
||||||
// Return the byte array.
|
// Return the byte array.
|
||||||
return binary_stl;
|
return generate_binary_stl(mushroom);
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
return if result.is_ok() {
|
return if result.is_ok() {
|
||||||
AsyncBinaryStelResponder::BinaryStl {
|
AsyncBinaryStlResponder::BinaryStl {
|
||||||
inner: result.unwrap(),
|
inner: result.unwrap(),
|
||||||
header: ContentType::parse_flexible("model/x.stl-binary").unwrap(),
|
header: ContentType::parse_flexible("model/x.stl-binary").unwrap(),
|
||||||
filename: Header {
|
filename: Header {
|
||||||
@@ -57,19 +63,24 @@ async fn generate_helper(mushroom: Mushroom) -> AsyncBinaryStelResponder {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
AsyncBinaryStelResponder::Error("Internal JoinError")
|
AsyncBinaryStlResponder::Error("Internal JoinError")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/generate")]
|
#[get("/generate?<stalk_h>&<stalk_r>&<cap_h>&<cap_r>&<cap_style>")]
|
||||||
async fn generate_default() -> AsyncBinaryStelResponder {
|
async fn generate(
|
||||||
// Default mushroom to generate
|
stalk_h: Option<f32>,
|
||||||
|
stalk_r: Option<f32>,
|
||||||
|
cap_h: Option<f32>,
|
||||||
|
cap_r: Option<f32>,
|
||||||
|
cap_style: Option<mushroom::CapStyle>,
|
||||||
|
) -> AsyncBinaryStlResponder {
|
||||||
let mushroom = mushroom::Mushroom {
|
let mushroom = mushroom::Mushroom {
|
||||||
stalk_h: 75.0,
|
stalk_h: stalk_h.unwrap_or(66.0),
|
||||||
stalk_r: 10.0,
|
stalk_r: stalk_r.unwrap_or(5.0),
|
||||||
cap_h: 25.0,
|
cap_h: cap_h.unwrap_or(34.0),
|
||||||
cap_r: 100.0,
|
cap_r: cap_r.unwrap_or(50.0),
|
||||||
cap_style: mushroom::CapStyle::Dome,
|
cap_style: cap_style.unwrap_or(mushroom::CapStyle::Dome),
|
||||||
};
|
};
|
||||||
|
|
||||||
return generate_helper(mushroom).await;
|
return generate_helper(mushroom).await;
|
||||||
|
|||||||
Reference in new issue
Block a user