Mon, 21 Oct 2024 23:07:01 -0500
Basic face image export
4 | 1 | |
2 | // We use unicode. We would like to use much more of it than Rust allows. | |
3 | // Live with it. Embrace it. | |
4 | #![allow(uncommon_codepoints)] | |
5 | #![allow(mixed_script_confusables)] | |
6 | #![allow(confusable_idents)] | |
1 | 7 | |
7 | 8 | use dist::DistToSquaredDiv2; |
9 | use fb::forward_backward; | |
10 | use manifold::EmbeddedManifoldPoint; | |
11 | 11 | use cube::*; |
12 | use image::{ | |
13 | ImageFormat, | |
14 | ImageBuffer, | |
15 | Rgb | |
16 | }; | |
7 | 17 | |
1 | 18 | mod manifold; |
4 | 19 | mod fb; |
1 | 20 | mod cube; |
5 | 21 | mod dist; |
6
df9628092285
Add a zero function on manifolds
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
22 | mod zero; |
1 | 23 | |
24 | fn main() { | |
7 | 25 | simple_test() |
26 | } | |
1 | 27 | |
7 | 28 | fn simple_test() { |
29 | use alg_tools::loc::Loc; | |
30 | use Face::*; | |
31 | use zero::ZeroFn; | |
11 | 32 | use alg_tools::mapping::{Sum, Apply}; |
7 | 33 | use alg_tools::iterate::{AlgIteratorOptions, Verbose}; |
34 | ||
35 | let points = [ | |
9 | 36 | //OnCube::new(F1, Loc([0.5, 0.5])), |
37 | //OnCube::new(F2, Loc([0.5, 0.5])), | |
38 | //OnCube::new(F4, Loc([0.1, 0.1])), | |
7 | 39 | OnCube::new(F1, Loc([0.5, 0.5])), |
9 | 40 | OnCube::new(F3, Loc([0.5, 0.5])), |
7 | 41 | OnCube::new(F2, Loc([0.5, 0.5])), |
42 | ]; | |
43 | ||
44 | //let x = points[0].clone(); | |
9 | 45 | // OnCube::new(F3, Loc([0.5, 0.5])); goes to opposite side |
46 | let x = OnCube::new(F3, Loc([0.5, 0.4])); | |
7 | 47 | let f = Sum::new(points.into_iter().map(DistToSquaredDiv2)); |
48 | let g = ZeroFn::new(); | |
49 | let τ = 0.1; | |
50 | let iter = AlgIteratorOptions{ | |
51 | max_iter : 100, | |
52 | verbose_iter : Verbose::Every(1), | |
53 | .. Default::default() | |
54 | }; | |
55 | ||
56 | let x̂ = forward_backward(&f, &g, x, τ, iter); | |
57 | println!("result = {}\n{:?}", x̂.embedded_coords(), &x̂); | |
11 | 58 | |
59 | for face in Face::all() { | |
60 | write_face(format!("{face}"), face, 128, |x| f.apply(x) + g.apply(x)) | |
61 | } | |
1 | 62 | } |
11 | 63 | |
64 | fn write_face(filename : String, face : Face, n : usize, mut f : impl FnMut(&OnCube) -> f64) { | |
65 | use alg_tools::lingrid::LinSpace; | |
66 | use alg_tools::loc::Loc; | |
67 | use alg_tools::types::*; | |
68 | ||
69 | let mut img = ImageBuffer::new(n as u32, n as u32); | |
70 | let grid = LinSpace { | |
71 | start : Loc([0.0, 0.0]), | |
72 | end : Loc([1.0, 1.0]), | |
73 | count : [n, n] | |
74 | }; | |
75 | let rawdata : Vec<_> = grid.into_iter() | |
76 | .map(|x| f(&OnCube::new(face, x))) | |
77 | .collect(); | |
78 | let a = rawdata.iter().copied().reduce(f64::max).unwrap(); | |
79 | img.pixels_mut() | |
80 | .zip(rawdata) | |
81 | .for_each(|(p, v)| { | |
82 | let t = v/a; | |
83 | let rgb = [1.0-t, 1.0-t, 1.0]; | |
84 | *p = Rgb(rgb.map(|v| (v*(u8::RANGE_MAX as f64)) as u8)) | |
85 | }); | |
86 | ||
87 | img.save_with_format(format!("{filename}.png"), ImageFormat::Png) | |
88 | .expect("Image save error"); | |
89 | } |