diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/renderer.rs | 28 |
3 files changed, 31 insertions, 14 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..06f8f31 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,4 @@ +//! This is a stub library for `hittekaart` to expose the functions for benchmarking. +pub mod gpx; +pub mod layer; +pub mod renderer; diff --git a/src/main.rs b/src/main.rs index b5bbdb3..e066a95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::{env, fs, path::PathBuf}; use color_eyre::eyre::Result; +use indicatif::ProgressBar; mod gpx; mod layer; @@ -21,10 +22,18 @@ fn main() -> Result<()> { for zoom in 0..=19 { println!("Doing level {zoom}"); - let counter = renderer::render_heatcounter(zoom, &tracks); + + let bar = ProgressBar::new(tracks.len().try_into().unwrap()); + let counter = + renderer::render_heatcounter(zoom, &tracks, |x| bar.inc(x.try_into().unwrap())); + bar.finish(); + let target = ["tiles", &zoom.to_string()].iter().collect::<PathBuf>(); fs::create_dir(&target)?; - renderer::lazy_colorization(&counter, &target)?; + + let bar = ProgressBar::new(counter.tile_count().try_into().unwrap()); + renderer::lazy_colorization(&counter, &target, |x| bar.inc(x.try_into().unwrap()))?; + bar.finish(); } Ok(()) diff --git a/src/renderer.rs b/src/renderer.rs index 1346ed1..6e17304 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -2,7 +2,6 @@ use std::{fs, mem, path::Path}; use color_eyre::eyre::{bail, Result}; use image::{ImageBuffer, Luma, Pixel, Rgba, RgbaImage}; -use indicatif::ProgressBar; use nalgebra::{vector, Vector2}; use num_traits::identities::Zero; @@ -21,7 +20,10 @@ fn render_circle<P: Pixel>(layer: &mut TileLayer<P>, center: (u64, u64), radius: let mut circle = ImageBuffer::<P, Vec<P::Subpixel>>::new(rad_32 * 2 + 1, rad_32 * 2 + 1); imageproc::drawing::draw_filled_circle_mut( &mut circle, - (i32::try_from(radius).unwrap(), i32::try_from(radius).unwrap()), + ( + i32::try_from(radius).unwrap(), + i32::try_from(radius).unwrap(), + ), radius.try_into().unwrap(), pixel, ); @@ -145,15 +147,17 @@ pub fn colorize_heatcounter(layer: &HeatCounter) -> HeatMap { /// rendering the next one. /// /// This has a way lower memory usage than [`colorize_heatcounter`]. -pub fn lazy_colorization<P: AsRef<Path>>(layer: &HeatCounter, base_dir: P) -> Result<()> { +pub fn lazy_colorization<P: AsRef<Path>, F: FnMut(usize)>( + layer: &HeatCounter, + base_dir: P, + mut progress_callback: F, +) -> Result<()> { let base_dir = base_dir.as_ref(); let max = layer.pixels().map(|l| l.0[0]).max().unwrap_or_default(); if max == 0 { return Ok(()); } - let bar = ProgressBar::new(layer.tile_count().try_into().unwrap()); - for (tile_x, tile_y, tile) in layer.enumerate_tiles() { let colorized = colorize_tile(&tile, max.into()); let folder = base_dir.join(&tile_x.to_string()); @@ -165,18 +169,19 @@ pub fn lazy_colorization<P: AsRef<Path>>(layer: &HeatCounter, base_dir: P) -> Re } let file = folder.join(&format!("{tile_y}.png")); layer::compress_png(&colorized, file)?; - bar.inc(1); + progress_callback(1); } - bar.finish(); Ok(()) } /// Renders the heat counter for the given zoom level and track points. -pub fn render_heatcounter(zoom: u32, tracks: &[Vec<Coordinates>]) -> HeatCounter { +pub fn render_heatcounter<F: FnMut(usize)>( + zoom: u32, + tracks: &[Vec<Coordinates>], + mut progress_callback: F, +) -> HeatCounter { let size = 256 * 2u64.pow(zoom); - let bar = ProgressBar::new(tracks.len().try_into().unwrap()); - let mut heatcounter = TileLayer::from_pixel(size, size, [0].into()); for track in tracks { @@ -196,8 +201,7 @@ pub fn render_heatcounter(zoom: u32, tracks: &[Vec<Coordinates>]) -> HeatCounter } merge_heat_counter(&mut heatcounter, &layer); - bar.inc(1); + progress_callback(1); } - bar.finish(); heatcounter } |