src/main.rs

changeset 16
a6efe0fafd90
parent 13
f67949050a32
child 18
84923812d220
equal deleted inserted replaced
15:2f4af30af476 16:a6efe0fafd90
7 #![allow(uncommon_codepoints)] 7 #![allow(uncommon_codepoints)]
8 #![allow(mixed_script_confusables)] 8 #![allow(mixed_script_confusables)]
9 #![allow(confusable_idents)] 9 #![allow(confusable_idents)]
10 10
11 use serde::Serialize; 11 use serde::Serialize;
12 use alg_tools::logger::Logger;
13 use alg_tools::tabledump::{TableDump, write_csv};
14 use alg_tools::error::DynError;
15 use alg_tools::lingrid::LinSpace;
16 use alg_tools::loc::Loc;
17 use alg_tools::types::*;
18 use alg_tools::mapping::{Sum, Apply};
19 use alg_tools::iterate::{AlgIteratorOptions, AlgIteratorFactory, Verbose};
20 use image::{ImageFormat, ImageBuffer, Rgb};
21
12 use dist::DistToSquaredDiv2; 22 use dist::DistToSquaredDiv2;
13 use fb::{forward_backward, IterInfo}; 23 use fb::{forward_backward, IterInfo};
14 use manifold::EmbeddedManifoldPoint; 24 use manifold::EmbeddedManifoldPoint;
15 use alg_tools::logger::Logger;
16 use alg_tools::tabledump::TableDump;
17 use alg_tools::error::DynError;
18 use cube::*; 25 use cube::*;
19 use image::{ 26 use Face::*;
20 ImageFormat, 27 use zero::ZeroFn;
21 ImageBuffer,
22 Rgb
23 };
24 28
25 mod manifold; 29 mod manifold;
26 mod fb; 30 mod fb;
27 mod cube; 31 mod cube;
28 mod dist; 32 mod dist;
47 /// Location for saving results 51 /// Location for saving results
48 static PREFIX : &str = "res"; 52 static PREFIX : &str = "res";
49 53
50 /// A simple test on the cube 54 /// A simple test on the cube
51 fn simple_cube_test() -> DynError { 55 fn simple_cube_test() -> DynError {
52 use alg_tools::loc::Loc;
53 use Face::*;
54 use zero::ZeroFn;
55 use alg_tools::mapping::{Sum, Apply};
56 use alg_tools::iterate::{AlgIteratorOptions, AlgIteratorFactory, Verbose};
57 56
58 let points = [ 57 let points = [
59 //OnCube::new(F1, Loc([0.5, 0.5])), 58 //OnCube::new(F1, Loc([0.5, 0.5])),
60 //OnCube::new(F2, Loc([0.5, 0.5])), 59 //OnCube::new(F2, Loc([0.5, 0.5])),
61 //OnCube::new(F4, Loc([0.1, 0.1])), 60 //OnCube::new(F4, Loc([0.1, 0.1])),
90 std::fs::create_dir_all(PREFIX)?; 89 std::fs::create_dir_all(PREFIX)?;
91 90
92 logger.write_csv(format!("{PREFIX}/log.txt"))?; 91 logger.write_csv(format!("{PREFIX}/log.txt"))?;
93 92
94 for face in Face::all() { 93 for face in Face::all() {
95 write_face(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?; 94 write_face_csv(format!("{PREFIX}/{face}"), face, 64, |x| f.apply(x) + g.apply(x))?;
95 write_face_img(format!("{PREFIX}/{face}"), face, 128, |x| f.apply(x) + g.apply(x))?;
96 } 96 }
97 97
98 Ok(()) 98 Ok(())
99 } 99 }
100 100
101 /// Writes the values of `f` on `face` of a [`OnCube`] into a PNG file 101 /// Writes the values of `f` on `face` of a [`OnCube`] into a PNG file
102 /// with resolution `n × n`. 102 /// with resolution `n × n`.
103 fn write_face(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError { 103 fn write_face_img(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError {
104 use alg_tools::lingrid::LinSpace;
105 use alg_tools::loc::Loc;
106 use alg_tools::types::*;
107
108 let mut img = ImageBuffer::new(n as u32, n as u32); 104 let mut img = ImageBuffer::new(n as u32, n as u32);
109 let grid = LinSpace { 105 let grid = LinSpace {
110 start : Loc([0.0, 0.0]), 106 start : Loc([0.0, 0.0]),
111 end : Loc([1.0, 1.0]), 107 end : Loc([1.0, 1.0]),
112 count : [n, n] 108 count : [n, n]
125 121
126 img.save_with_format(format!("{filename}.png"), ImageFormat::Png)?; 122 img.save_with_format(format!("{filename}.png"), ImageFormat::Png)?;
127 123
128 Ok(()) 124 Ok(())
129 } 125 }
126
127 /// Writes the values of `f` on `face` of a [`OnCube`] into a CSV file
128 /// with resolution `n × n`.
129 fn write_face_csv(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) -> DynError {
130
131 #[derive(Serialize)]
132 struct CSVFace { u : f64, v : f64, value : f64 }
133
134 let grid = LinSpace {
135 start : Loc([0.0, 0.0]),
136 end : Loc([1.0, 1.0]),
137 count : [n, n]
138 };
139
140 let data = grid.into_iter()
141 .map(|p@Loc([u,v])| CSVFace{ u, v, value : f(&OnCube::new(face, p)) });
142
143 write_csv(data, format!("{filename}.csv"))?;
144
145 Ok(())
146 }

mercurial