Basic face image export

Mon, 21 Oct 2024 23:07:01 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 21 Oct 2024 23:07:01 -0500
changeset 11
933242e0f3b8
parent 10
cd6f6e7defd2
child 12
3b05a8b45b95

Basic face image export

Cargo.toml file | annotate | diff | comparison | revisions
src/cube.rs file | annotate | diff | comparison | revisions
src/main.rs file | annotate | diff | comparison | revisions
--- a/Cargo.toml	Mon Oct 21 15:07:41 2024 -0500
+++ b/Cargo.toml	Mon Oct 21 23:07:01 2024 -0500
@@ -21,3 +21,4 @@
 serde = { version = "1.0", features = ["derive"] }
 alg_tools = { version = "~0.3.0-dev", path = "../alg_tools", default-features = false }
 colored = "~2.0.0"
+image = "~0.24.3"
--- a/src/cube.rs	Mon Oct 21 15:07:41 2024 -0500
+++ b/src/cube.rs	Mon Oct 21 23:07:01 2024 -0500
@@ -52,8 +52,26 @@
     }
 }
 
+impl std::fmt::Display for Face {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let s = match *self {
+            F1 => "F1",
+            F2 => "F2",
+            F3 => "F3",
+            F4 => "F4",
+            F5 => "F5",
+            F6 => "F6",
+        };
+        write!(f, "{}", s)
+    }
+}
 
 impl Face {
+    /// Return an aray of all faces
+    pub fn all() -> [Face; 6] {
+        [F1, F2, F3, F4, F5, F6]
+    }
+
     /// Returns an array of the four faces adjacent to `self` in the
     /// order [left, right, down, up] in the `self`-relative unfolding.
     pub fn adjacent_faces(&self) -> AdjacentFaces {
--- a/src/main.rs	Mon Oct 21 15:07:41 2024 -0500
+++ b/src/main.rs	Mon Oct 21 23:07:01 2024 -0500
@@ -8,6 +8,12 @@
 use dist::DistToSquaredDiv2;
 use fb::forward_backward;
 use manifold::EmbeddedManifoldPoint;
+use cube::*;
+use image::{
+    ImageFormat,
+    ImageBuffer,
+    Rgb
+};
 
 mod manifold;
 mod fb;
@@ -20,11 +26,10 @@
 }
 
 fn simple_test() {
-    use cube::*;
     use alg_tools::loc::Loc;
     use Face::*;
     use zero::ZeroFn;
-    use alg_tools::mapping::Sum;
+    use alg_tools::mapping::{Sum, Apply};
     use alg_tools::iterate::{AlgIteratorOptions, Verbose};
     
     let points = [
@@ -50,4 +55,35 @@
 
     let x̂ = forward_backward(&f, &g, x, τ, iter);
     println!("result = {}\n{:?}", x̂.embedded_coords(), &x̂);
+
+    for face in Face::all() {
+        write_face(format!("{face}"), face, 128, |x| f.apply(x) + g.apply(x))
+    }
 }
+
+fn write_face(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) {
+    use alg_tools::lingrid::LinSpace;
+    use alg_tools::loc::Loc;
+    use alg_tools::types::*;
+
+    let mut img = ImageBuffer::new(n as u32, n as u32);
+    let grid = LinSpace {
+        start : Loc([0.0, 0.0]),
+        end : Loc([1.0, 1.0]),
+        count : [n, n]
+    };
+    let rawdata : Vec<_> = grid.into_iter()
+                               .map(|x| f(&OnCube::new(face, x)))
+                               .collect();
+    let a = rawdata.iter().copied().reduce(f64::max).unwrap();
+    img.pixels_mut()
+        .zip(rawdata)
+        .for_each(|(p, v)| {
+            let t = v/a;
+            let rgb = [1.0-t, 1.0-t, 1.0];
+            *p = Rgb(rgb.map(|v| (v*(u8::RANGE_MAX as f64)) as u8))
+        });
+
+    img.save_with_format(format!("{filename}.png"), ImageFormat::Png)
+        .expect("Image save error");
+}

mercurial