land_dump app for testing templated landgen
authorunc0rr
Sat, 03 Nov 2018 18:29:58 +0100
changeset 14126 69db1d2e4cec
parent 14125 7bb0e27910aa
child 14127 c27461e6a9eb
land_dump app for testing templated landgen
rust/land2d/src/lib.rs
rust/land_dump/Cargo.toml
rust/land_dump/src/main.rs
rust/landgen/src/lib.rs
rust/landgen/src/outline.rs
rust/landgen/src/template_based.rs
rust/vec2d/src/lib.rs
--- a/rust/land2d/src/lib.rs	Sat Nov 03 17:40:21 2018 +0100
+++ b/rust/land2d/src/lib.rs	Sat Nov 03 18:29:58 2018 +0100
@@ -29,6 +29,10 @@
         }
     }
 
+    pub fn raw_pixels(&self) -> &[T] {
+        &self.pixels.raw_data()
+    }
+
     #[inline]
     pub fn width(&self) -> usize {
         self.pixels.width()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/land_dump/Cargo.toml	Sat Nov 03 18:29:58 2018 +0100
@@ -0,0 +1,13 @@
+[package]
+name = "land_dump"
+version = "0.1.0"
+authors = ["Andrei Korotaev <andrey.korotaev@aptomar.com>"]
+edition = "2018"
+
+[dependencies]
+land2d = { path = "../land2d" }
+landgen = { path = "../landgen" }
+lfprng = { path = "../lfprng" }
+integral-geometry = { path = "../integral-geometry" }
+png = "0.13"
+structopt ="0.2.13"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/land_dump/src/main.rs	Sat Nov 03 18:29:58 2018 +0100
@@ -0,0 +1,99 @@
+extern crate integral_geometry;
+extern crate land2d;
+extern crate landgen;
+extern crate lfprng;
+extern crate png;
+extern crate structopt;
+
+use png::HasParameters;
+use std::fs::File;
+use std::io::BufWriter;
+use std::path::Path;
+use structopt::StructOpt;
+
+use integral_geometry::{Point, Rect, Size};
+use landgen::outline_template::OutlineTemplate;
+use landgen::template_based::TemplatedLandGenerator;
+use landgen::LandGenerationParameters;
+use landgen::LandGenerator;
+use lfprng::LaggedFibonacciPRNG;
+
+#[derive(StructOpt, Debug)]
+#[structopt(name = "basic")]
+struct Opt {
+    #[structopt(short = "s", long = "seed", default_value = "TEST_SEED")]
+    seed: String,
+    #[structopt(short = "d", long = "dump-before-distort")]
+    dump_before_distort: bool,
+    #[structopt(short = "b", long = "dump-before-bezierize")]
+    dump_before_bezierize: bool,
+}
+
+fn template() -> OutlineTemplate {
+    let mut template = OutlineTemplate::new(Size::new(4096, 2048));
+    template.islands = vec![vec![
+        Rect::new(100, 2050, 1, 1),
+        Rect::new(100, 500, 400, 1200),
+        Rect::new(3600, 500, 400, 1200),
+        Rect::new(3900, 2050, 1, 1),
+    ]];
+    template.fill_points = vec![Point::new(2047, 2047)];
+
+    template
+}
+
+fn dump(
+    seed: &[u8],
+    skip_distort: bool,
+    skip_bezier: bool,
+    file_name: &Path,
+) -> std::io::Result<()> {
+    let params = LandGenerationParameters::new(0 as u8, 255, 100, skip_distort, skip_bezier);
+    let landgen = TemplatedLandGenerator::new(template());
+    let mut prng = LaggedFibonacciPRNG::new(seed);
+    let land = landgen.generate_land(&params, &mut prng);
+
+    let file = File::create(file_name)?;
+    let ref mut w = BufWriter::new(file);
+
+    let mut encoder = png::Encoder::new(w, land.width() as u32, land.height() as u32); // Width is 2 pixels and height is 1.
+    encoder
+        .set(png::ColorType::Grayscale)
+        .set(png::BitDepth::Eight);
+    let mut writer = encoder.write_header()?;
+
+    writer.write_image_data(land.raw_pixels()).unwrap();
+
+    Ok(())
+}
+
+fn main() {
+    let opt = Opt::from_args();
+    println!("{:?}", opt);
+
+    if opt.dump_before_distort {
+        dump(
+            opt.seed.as_str().as_bytes(),
+            true,
+            true,
+            Path::new("out.before_distort.png"),
+        )
+        .unwrap();
+    }
+    if opt.dump_before_bezierize {
+        dump(
+            opt.seed.as_str().as_bytes(),
+            false,
+            true,
+            Path::new("out.before_bezier.png"),
+        )
+        .unwrap();
+    }
+    dump(
+        opt.seed.as_str().as_bytes(),
+        false,
+        true,
+        Path::new("out.full.png"),
+    )
+    .unwrap();
+}
--- a/rust/landgen/src/lib.rs	Sat Nov 03 17:40:21 2018 +0100
+++ b/rust/landgen/src/lib.rs	Sat Nov 03 18:29:58 2018 +0100
@@ -10,14 +10,18 @@
     zero: T,
     basic: T,
     distance_divisor: u32,
+    skip_distort: bool,
+    skip_bezier: bool,
 }
 
 impl<T: Copy + PartialEq> LandGenerationParameters<T> {
-    pub fn new(zero: T, basic: T) -> Self {
+    pub fn new(zero: T, basic: T, distance_divisor: u32, skip_distort: bool, skip_bezier: bool) -> Self {
         Self {
             zero,
             basic,
-            distance_divisor: 1,
+            distance_divisor,
+            skip_distort,
+            skip_bezier,
         }
     }
 }
@@ -25,7 +29,7 @@
 pub trait LandGenerator {
     fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>(
         &self,
-        parameters: LandGenerationParameters<T>,
+        parameters: &LandGenerationParameters<T>,
         random_numbers: &mut I,
     ) -> land2d::Land2D<T>;
 }
--- a/rust/landgen/src/outline.rs	Sat Nov 03 17:40:21 2018 +0100
+++ b/rust/landgen/src/outline.rs	Sat Nov 03 18:29:58 2018 +0100
@@ -270,8 +270,6 @@
                 break;
             }
         }
-
-        self.bezierize();
     }
 
     pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
--- a/rust/landgen/src/template_based.rs	Sat Nov 03 17:40:21 2018 +0100
+++ b/rust/landgen/src/template_based.rs	Sat Nov 03 18:29:58 2018 +0100
@@ -19,7 +19,7 @@
 impl LandGenerator for TemplatedLandGenerator {
     fn generate_land<T: Copy + PartialEq, I: Iterator<Item = u32>>(
         &self,
-        parameters: LandGenerationParameters<T>,
+        parameters: &LandGenerationParameters<T>,
         random_numbers: &mut I,
     ) -> Land2D<T> {
         let mut land = Land2D::new(self.outline_template.size, parameters.basic);
@@ -49,7 +49,13 @@
             }
         }
 
-        points.distort(parameters.distance_divisor, random_numbers);
+        if !parameters.skip_distort {
+            points.distort(parameters.distance_divisor, random_numbers);
+        }
+
+        if !parameters.skip_bezier {
+            points.bezierize();
+        }
 
         points.draw(&mut land, parameters.zero);
 
--- a/rust/vec2d/src/lib.rs	Sat Nov 03 17:40:21 2018 +0100
+++ b/rust/vec2d/src/lib.rs	Sat Nov 03 18:29:58 2018 +0100
@@ -55,6 +55,10 @@
         Self { size, data: vec![value; size.area()] }
     }
 
+    pub fn raw_data(&self) -> &[T] {
+        &self.data
+    }
+
     #[inline]
     pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> {
         self.data.get(row * self.width() + column)