feat: argument parsing improvement

* add date random seed parser
* changed --svg to --output-type
* get rid of --output for output file
This commit is contained in:
Ingolf Wagner 2021-04-08 07:13:22 +02:00
parent 81aea71162
commit b30d687963
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
6 changed files with 46 additions and 30 deletions

10
Cargo.lock generated
View file

@ -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"

View file

@ -26,3 +26,4 @@ palette = "0.5"
rand = "0.8"
structopt = "0.3"
svg = "0.9"
iso8601 = "0.4"

View file

@ -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();

View file

@ -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();

View file

@ -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<f64>) {
pub fn draw_multipolygon(&self, multi_polygon: &MultiPolygon<f64>) {
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<dyn RenderContainer> = if opt.svg {
let render_container: Box<dyn RenderContainer> = 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<dyn RenderContainer>
} 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<dyn RenderContainer>
} else {
panic!("output type unknown");
};
render_container.init(width as f64, height as f64, line_size);

View file

@ -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<u64>,
/// 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<iso8601::Date>,
}
impl Opt {