First image has been generated!
This commit is contained in:
parent
51f10723ed
commit
5f5d198e04
17
src/grid.rs
17
src/grid.rs
@ -63,6 +63,23 @@ impl Grid {
|
||||
self.blur
|
||||
.run(&mut self.data, &mut self.buf, radius as f32, decay_factor);
|
||||
}
|
||||
|
||||
pub fn quantile(&self, fraction: f32) -> f32 {
|
||||
let index = if fraction == 1.0 {
|
||||
self.data.len() - 1
|
||||
} else {
|
||||
(self.data.len() as f32 * fraction) as usize
|
||||
};
|
||||
let mut sorted = self.data.clone();
|
||||
sorted
|
||||
.as_mut_slice()
|
||||
.select_nth_unstable_by(index, |a, b| a.partial_cmp(b).unwrap());
|
||||
sorted[index]
|
||||
}
|
||||
|
||||
pub fn data(&self) -> &[f32] {
|
||||
&self.data
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -1,10 +1,9 @@
|
||||
use physarum::model;
|
||||
|
||||
fn main() {
|
||||
let mut model = model::Model::new(4, 4, 20, 1);
|
||||
let mut model = model::Model::new(128, 128, 4096, 1);
|
||||
println!("{:#?}", model);
|
||||
model.step();
|
||||
println!("{:#?}", model);
|
||||
model.step();
|
||||
println!("{:#?}", model);
|
||||
model.save_to_image();
|
||||
}
|
||||
|
||||
14
src/model.rs
14
src/model.rs
@ -176,4 +176,18 @@ impl Model {
|
||||
.diffuse(self.diffusivity, self.config.decay_factor);
|
||||
self.iteration += 1;
|
||||
}
|
||||
|
||||
/// Output the current trail layer as a grayscale image.
|
||||
pub fn save_to_image(&self) {
|
||||
let mut img = image::GrayImage::new(self.width as u32, self.height as u32);
|
||||
let max_value = self.grid.quantile(0.999);
|
||||
|
||||
for (i, value) in self.grid.data().iter().enumerate() {
|
||||
let x = (i % self.width) as u32;
|
||||
let y = (i / self.width) as u32;
|
||||
let c = (value / max_value).clamp(0.0, 1.0) * 255.0;
|
||||
img.put_pixel(x, y, image::Luma([c as u8]));
|
||||
}
|
||||
img.save("out.png").unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user