Rework lib structure, no code changes
authorunC0Rr
Fri, 02 Nov 2018 09:45:54 +0100
changeset 14074 abb42ba345b6
parent 14073 859a41b137d4
child 14075 a5be3ef4bbbe
Rework lib structure, no code changes
rust/landgen/src/lib.rs
rust/landgen/src/outline.rs
rust/landgen/src/outline_template.rs
rust/landgen/src/template_based.rs
--- a/rust/landgen/src/lib.rs	Fri Nov 02 01:34:21 2018 +0100
+++ b/rust/landgen/src/lib.rs	Fri Nov 02 09:45:54 2018 +0100
@@ -1,4 +1,6 @@
 pub mod template_based;
+pub mod outline_template;
+mod outline;
 
 extern crate integral_geometry;
 extern crate land2d;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/landgen/src/outline.rs	Fri Nov 02 09:45:54 2018 +0100
@@ -0,0 +1,116 @@
+use itertools::Itertools;
+
+use integral_geometry::{Point, Size};
+use land2d::Land2D;
+
+use outline_template::OutlineTemplate;
+
+pub struct OutlinePoints {
+    pub islands: Vec<Vec<Point>>,
+    pub fill_points: Vec<Point>,
+    pub size: Size,
+}
+
+impl OutlinePoints {
+    pub fn from_outline_template<I: Iterator<Item = u32>>(
+        outline_template: &OutlineTemplate,
+        random_numbers: &mut I,
+    ) -> Self {
+        Self {
+            islands: outline_template
+                .islands
+                .iter()
+                .map(|i| {
+                    i.iter()
+                        .zip(random_numbers.tuples())
+                        .map(|(rect, (rnd_a, rnd_b))| {
+                            Point::new(
+                                rect.x + (rnd_a % rect.width) as i32,
+                                rect.y + (rnd_b % rect.height) as i32,
+                            )
+                        }).collect()
+                }).collect(),
+            fill_points: outline_template.fill_points.clone(),
+            size: outline_template.size,
+        }
+    }
+
+    pub fn total_len(&self) -> usize {
+        self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len()
+    }
+
+    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
+        self.islands
+            .iter_mut()
+            .flat_map(|i| i.iter_mut())
+            .chain(self.fill_points.iter_mut())
+    }
+
+    fn divide_edge<I: Iterator<Item = u32>>(
+        &self,
+        start_point: Point,
+        end_point: Point,
+        random_numbers: &mut I,
+    ) -> Option<Point> {
+        None
+    }
+
+    fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
+        for is in 0..self.islands.len() {
+            let mut i = 0;
+            let mut start_point = Point::zero();
+            let mut end_point = Point::zero();
+
+            loop {
+                {
+                    let island = &self.islands[is];
+                    if i < island.len() {
+                        start_point = island[i];
+                        end_point = if i + 1 < island.len() {
+                            island[i + 1]
+                        } else {
+                            island[0]
+                        };
+                    } else {
+                        break
+                    }
+                }
+
+                if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) {
+                    self.islands[is].insert(i + 1, new_point);
+                    i += 2;
+                } else {
+                    i += 1;
+                }
+            }
+        }
+    }
+
+    pub fn bezierize(&mut self) {
+        unimplemented!()
+    }
+
+    pub fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
+        loop {
+            let old_len = self.total_len();
+            self.divide_edges(random_numbers);
+
+            if self.total_len() != old_len {
+                break;
+            }
+        }
+
+        self.bezierize();
+    }
+
+    pub fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
+        for island in &self.islands {
+            if island.len() > 1 {
+                for i in 0..island.len() - 1 {
+                    land.draw_line(island[i], island[i + 1], value);
+                }
+                land.draw_line(island[island.len() - 1], island[0], value);
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/landgen/src/outline_template.rs	Fri Nov 02 09:45:54 2018 +0100
@@ -0,0 +1,75 @@
+use integral_geometry::{Point, Rect, Size};
+
+
+pub struct OutlineTemplate {
+    pub islands: Vec<Vec<Rect>>,
+    pub fill_points: Vec<Point>,
+    pub size: Size,
+    pub can_flip: bool,
+    pub can_invert: bool,
+    pub can_mirror: bool,
+    pub is_negative: bool,
+}
+
+impl OutlineTemplate {
+    pub fn new(size: Size) -> Self {
+        OutlineTemplate {
+            size,
+            islands: Vec::new(),
+            fill_points: Vec::new(),
+            can_flip: false,
+            can_invert: false,
+            can_mirror: false,
+            is_negative: false,
+        }
+    }
+
+    pub fn flippable(self) -> Self {
+        Self {
+            can_flip: true,
+            ..self
+        }
+    }
+
+    pub fn mirrorable(self) -> Self {
+        Self {
+            can_mirror: true,
+            ..self
+        }
+    }
+
+    pub fn invertable(self) -> Self {
+        Self {
+            can_invert: true,
+            ..self
+        }
+    }
+
+    pub fn negative(self) -> Self {
+        Self {
+            is_negative: true,
+            ..self
+        }
+    }
+
+    pub fn with_fill_points(self, fill_points: Vec<Point>) -> Self {
+        Self {
+            fill_points,
+            ..self
+        }
+    }
+
+    pub fn with_islands(self, islands: Vec<Vec<Rect>>) -> Self {
+        Self { islands, ..self }
+    }
+
+    pub fn add_fill_points(mut self, points: &[Point]) -> Self {
+        self.fill_points.extend_from_slice(points);
+        self
+    }
+
+    pub fn add_island(mut self, island: &[Rect]) -> Self {
+        self.islands.push(island.into());
+        self
+    }
+}
--- a/rust/landgen/src/template_based.rs	Fri Nov 02 01:34:21 2018 +0100
+++ b/rust/landgen/src/template_based.rs	Fri Nov 02 09:45:54 2018 +0100
@@ -1,192 +1,11 @@
-use itertools::Itertools;
-
-use integral_geometry::{Point, Rect, Size};
+use integral_geometry::{Point, Size};
 use land2d::Land2D;
 use LandGenerationParameters;
 use LandGenerator;
 
-struct OutlinePoints {
-    islands: Vec<Vec<Point>>,
-    fill_points: Vec<Point>,
-    size: Size,
-}
-
-impl OutlinePoints {
-    fn from_outline_template<I: Iterator<Item = u32>>(
-        outline_template: &OutlineTemplate,
-        random_numbers: &mut I,
-    ) -> Self {
-        Self {
-            islands: outline_template
-                .islands
-                .iter()
-                .map(|i| {
-                    i.iter()
-                        .zip(random_numbers.tuples())
-                        .map(|(rect, (rnd_a, rnd_b))| {
-                            Point::new(
-                                rect.x + (rnd_a % rect.width) as i32,
-                                rect.y + (rnd_b % rect.height) as i32,
-                            )
-                        }).collect()
-                }).collect(),
-            fill_points: outline_template.fill_points.clone(),
-            size: outline_template.size,
-        }
-    }
-
-    fn total_len(&self) -> usize {
-        self.islands.iter().map(|i| i.len()).sum::<usize>() + self.fill_points.len()
-    }
-
-    fn iter_mut(&mut self) -> impl Iterator<Item = &mut Point> {
-        self.islands
-            .iter_mut()
-            .flat_map(|i| i.iter_mut())
-            .chain(self.fill_points.iter_mut())
-    }
-
-    fn divide_edge<I: Iterator<Item = u32>>(
-        &self,
-        start_point: Point,
-        end_point: Point,
-        random_numbers: &mut I,
-    ) -> Option<Point> {
-        None
-    }
-
-    fn divide_edges<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
-        for is in 0..self.islands.len() {
-            let mut i = 0;
-            let mut start_point = Point::zero();
-            let mut end_point = Point::zero();
-
-            loop {
-                {
-                    let island = &self.islands[is];
-                    if i < island.len() {
-                        start_point = island[i];
-                        end_point = if i + 1 < island.len() {
-                            island[i + 1]
-                        } else {
-                            island[0]
-                        };
-                    } else {
-                        break
-                    }
-                }
-
-                if let Some(new_point) = self.divide_edge(start_point, end_point, random_numbers) {
-                    self.islands[is].insert(i + 1, new_point);
-                    i += 2;
-                } else {
-                    i += 1;
-                }
-            }
-        }
-    }
-
-    fn bezierize(&mut self) {
-        unimplemented!()
-    }
-
-    fn distort<I: Iterator<Item = u32>>(&mut self, random_numbers: &mut I) {
-        loop {
-            let old_len = self.total_len();
-            self.divide_edges(random_numbers);
+use outline::OutlinePoints;
+use outline_template::OutlineTemplate;
 
-            if self.total_len() != old_len {
-                break;
-            }
-        }
-
-        self.bezierize();
-    }
-
-    fn draw<T: Copy + PartialEq>(&self, land: &mut Land2D<T>, value: T) {
-        for island in &self.islands {
-            if island.len() > 1 {
-                for i in 0..island.len() - 1 {
-                    land.draw_line(island[i], island[i + 1], value);
-                }
-                land.draw_line(island[island.len() - 1], island[0], value);
-            }
-        }
-    }
-}
-
-pub struct OutlineTemplate {
-    islands: Vec<Vec<Rect>>,
-    fill_points: Vec<Point>,
-    size: Size,
-    can_flip: bool,
-    can_invert: bool,
-    can_mirror: bool,
-    is_negative: bool,
-}
-
-impl OutlineTemplate {
-    pub fn new(size: Size) -> Self {
-        OutlineTemplate {
-            size,
-            islands: Vec::new(),
-            fill_points: Vec::new(),
-            can_flip: false,
-            can_invert: false,
-            can_mirror: false,
-            is_negative: false,
-        }
-    }
-
-    pub fn flippable(self) -> Self {
-        Self {
-            can_flip: true,
-            ..self
-        }
-    }
-
-    pub fn mirrorable(self) -> Self {
-        Self {
-            can_mirror: true,
-            ..self
-        }
-    }
-
-    pub fn invertable(self) -> Self {
-        Self {
-            can_invert: true,
-            ..self
-        }
-    }
-
-    pub fn negative(self) -> Self {
-        Self {
-            is_negative: true,
-            ..self
-        }
-    }
-
-    pub fn with_fill_points(self, fill_points: Vec<Point>) -> Self {
-        Self {
-            fill_points,
-            ..self
-        }
-    }
-
-    pub fn with_islands(self, islands: Vec<Vec<Rect>>) -> Self {
-        Self { islands, ..self }
-    }
-
-    pub fn add_fill_points(mut self, points: &[Point]) -> Self {
-        self.fill_points.extend_from_slice(points);
-        self
-    }
-
-    pub fn add_island(mut self, island: &[Rect]) -> Self {
-        self.islands.push(island.into());
-        self
-    }
-}
 
 pub struct TemplatedLandGenerator {
     outline_template: OutlineTemplate,