From b30d687963b0c95dd27c8a37eae9c9f1a4fff1f6 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Thu, 8 Apr 2021 07:13:22 +0200 Subject: [PATCH] feat: argument parsing improvement * add date random seed parser * changed --svg to --output-type * get rid of --output for output file --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + examples/clipping.rs | 8 ++++---- examples/svg.rs | 2 +- src/context.rs | 35 ++++++++++++++++------------------- src/context/commandline.rs | 20 ++++++++++++++------ 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index af903c9..6ecd8ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -536,6 +536,14 @@ dependencies = [ "quick-error 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "iso8601" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nom 5.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itertools" version = "0.8.2" @@ -706,6 +714,7 @@ dependencies = [ "geo-clipper 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "geo-svg-io 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "geo-types 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "iso8601 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "palette 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1196,6 +1205,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772" "checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +"checksum iso8601 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cee08a007a59a8adfc96f69738ddf59e374888dfd84b49c4b916543067644d58" "checksum itertools 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum lazycell 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" diff --git a/Cargo.toml b/Cargo.toml index 5ff0ff2..acb5b65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,3 +26,4 @@ palette = "0.5" rand = "0.8" structopt = "0.3" svg = "0.9" +iso8601 = "0.4" diff --git a/examples/clipping.rs b/examples/clipping.rs index 5e0b1d2..8d02833 100644 --- a/examples/clipping.rs +++ b/examples/clipping.rs @@ -34,23 +34,23 @@ fn main() { // union context.translate(250.0, 0.); let union = &polygon_a.union(&polygon_b, 10.0); - context.draw_multipolgon(union); + context.draw_multipolygon(union); // intersection context.translate(0.0, 250.); let intersection = &polygon_a.intersection(&polygon_b, 10.0); - context.draw_multipolgon(intersection); + context.draw_multipolygon(intersection); context.restore(); // difference A context.translate(250.0, 0.); let difference = &polygon_a.difference(&polygon_b, 10.0); - context.draw_multipolgon(difference); + context.draw_multipolygon(difference); // difference B context.translate(0., 250.); let difference = &polygon_b.difference(&polygon_a, 10.0); - context.draw_multipolgon(difference); + context.draw_multipolygon(difference); context.restore(); context.render(); diff --git a/examples/svg.rs b/examples/svg.rs index 4d4e5f3..456f5f1 100644 --- a/examples/svg.rs +++ b/examples/svg.rs @@ -40,7 +40,7 @@ fn main() { (index % items_per_line) as f64 * item_space, (index / items_per_line) as f64 * item_space, ); - renderer.draw_multipolgon(&svg); + renderer.draw_multipolygon(&svg); } renderer.restore(); diff --git a/src/context.rs b/src/context.rs index 76e278e..ab115d7 100644 --- a/src/context.rs +++ b/src/context.rs @@ -7,6 +7,7 @@ use rand::{Rng, SeedableRng}; use crate::context::commandline::Opt; use crate::context::palette::Palette; use crate::context::renderer::{PngRenderContainer, RenderContainer, SvgRenderContainer}; +use iso8601::Date::*; pub mod commandline; pub mod palette; @@ -21,7 +22,7 @@ pub struct Context { } impl Context { - pub fn draw_multipolgon(&self, multi_polygon: &MultiPolygon) { + pub fn draw_multipolygon(&self, multi_polygon: &MultiPolygon) { for polygon in multi_polygon.iter() { self.draw_polygon(polygon); } @@ -76,21 +77,6 @@ impl Context { context.translate(tx, ty); } - /// plot a path - // pub fn plot_object(&self, object: &Object) { - // self.render_container.plot_object(object); - // } - - /// plot a path - // pub fn plot_polygon(&self, polygon: &OldPolynom) { - // self.plot_path(&polygon.path, polygon.filled); - // } - - /// plot a path - // pub fn plot_path(&self, path: &Vec<[f64; 2]>, fill: bool) { - // self.render_container.plot_path(path, fill); - // } - pub fn render(self) { self.render_container.render(); } @@ -100,10 +86,19 @@ impl Context { let height = f64::from(opt.height.clone()); let width = f64::from(opt.width.clone()); let line_size = opt.line_size.clone(); + let random_seed = match opt.random_seed { Some(random_seed) => random_seed, - None => rand::random(), + None => match opt.random_seed_date { + Some(YMD { year, month, day }) => { + ((10000 + year) as u32 * 365 + month * 12 + day) as u64 + } + Some(Week { year, ww, d }) => ((10000 + year) as u32 * 365 + ww * 7 + d) as u64, + Some(Ordinal { year, ddd }) => ((10000 + year) as u32 * 365 + ddd) as u64, + None => rand::random(), + }, }; + println!("random seed {}", random_seed); let mut rng = StdRng::seed_from_u64(random_seed); @@ -114,7 +109,7 @@ impl Context { let value: f32 = f32::max(rng.gen(), 0.6); let palette = Palette::dark_on_bright(Palette::color(hue, saturation, value)); - let render_container: Box = if opt.svg { + let render_container: Box = if opt.output_type == "svg" { let svg_surface = SvgSurface::new( f64::from(width), f64::from(height), @@ -122,7 +117,7 @@ impl Context { ) .expect("Can't svg surface"); Box::new(SvgRenderContainer::new(svg_surface, palette)) as Box - } else { + } else if opt.output_type == "png" { let png_surface = ImageSurface::create(Format::Rgb24, opt.width.clone(), opt.height.clone()) .expect("Can't create png surface"); @@ -131,6 +126,8 @@ impl Context { png_surface, palette, )) as Box + } else { + panic!("output type unknown"); }; render_container.init(width as f64, height as f64, line_size); diff --git a/src/context/commandline.rs b/src/context/commandline.rs index 2857ce4..f019227 100644 --- a/src/context/commandline.rs +++ b/src/context/commandline.rs @@ -4,9 +4,12 @@ use structopt::StructOpt; #[derive(StructOpt)] #[structopt()] /// Polygon Art command line interface +/// +/// A Framework to draw images as background or to plot them +/// with your favorite plotter pub struct Opt { - /// Output file (will be a png if you don't use the --svg flag) - #[structopt(short, long, parse(from_os_str))] + /// Output file + #[structopt(parse(from_os_str))] pub output: PathBuf, /// define width in pixels @@ -22,12 +25,17 @@ pub struct Opt { pub line_size: f64, /// output file format should be svg instead of png - #[structopt(long)] - pub svg: bool, + #[structopt(long="type", possible_values=&["svg", "png"], default_value="png")] + pub output_type: String, - /// seed for randomizer - #[structopt(long)] + /// seed for the randomizer + #[structopt(long = "random")] pub random_seed: Option, + + /// seed for the randomizer based on date. + /// (must be ISO8601) + #[structopt(long = "random-date", conflicts_with = "random-seed", parse(try_from_str = iso8601::date))] + pub random_seed_date: Option, } impl Opt {