From 4b30bda722668bef4ba362b3e578f02dede0099b Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 8 Jan 2023 19:36:31 +0100 Subject: add a progress bar --- src/layer.rs | 4 ++++ src/renderer.rs | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/layer.rs b/src/layer.rs index 465953d..1c14df3 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -121,6 +121,10 @@ impl TileLayer

{ pub fn pixels_mut(&mut self) -> impl Iterator { self.enumerate_pixels_mut().map(|x| x.2) } + + pub fn tile_count(&self) -> usize { + self.tiles.len() + } } impl TileLayer> { diff --git a/src/renderer.rs b/src/renderer.rs index c523268..757019c 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -1,6 +1,7 @@ use std::{fs, mem, path::Path}; use color_eyre::eyre::{bail, Result}; +use indicatif::ProgressBar; use image::{ImageBuffer, Luma, Pixel, Rgba, RgbaImage}; use nalgebra::{vector, Vector2}; use num_traits::identities::Zero; @@ -87,6 +88,9 @@ fn render_line( let displacement = normal * thickness as f64; let displacement = displacement.map(|x| x as i32); + if displacement == vector![0, 0] { + return; + } let polygon = [ unsigned_add(start, displacement), unsigned_add(end, displacement), @@ -123,12 +127,12 @@ fn merge_heat_counter(base: &mut HeatCounter, overlay: &HeatCounter) { } fn colorize_tile(tile: &ImageBuffer, Vec>, max: u32) -> RgbaImage { - let gradient = colorgrad::turbo(); + let gradient = colorgrad::yl_or_rd(); let mut result = ImageBuffer::from_pixel(tile.width(), tile.height(), [0, 0, 0, 0].into()); for (x, y, pixel) in tile.enumerate_pixels() { if pixel[0] > 0 { let alpha = pixel[0] as f64 / max as f64; - let color = gradient.at(alpha); + let color = gradient.at(1.0 - alpha); let target = result.get_pixel_mut(x, y); *target = color.to_rgba8().into(); } @@ -159,6 +163,9 @@ pub fn lazy_colorization>(layer: &HeatCounter, base_dir: P) -> Re 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); let folder = base_dir.join(&tile_x.to_string()); @@ -170,7 +177,9 @@ pub fn lazy_colorization>(layer: &HeatCounter, base_dir: P) -> Re } let file = folder.join(&format!("{tile_y}.png")); layer::compress_png(&colorized, file)?; + bar.inc(1); } + bar.finish(); Ok(()) } @@ -178,6 +187,8 @@ pub fn lazy_colorization>(layer: &HeatCounter, base_dir: P) -> Re pub fn render_heatcounter(zoom: u32, tracks: &[Vec]) -> 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 { @@ -189,14 +200,16 @@ pub fn render_heatcounter(zoom: u32, tracks: &[Vec]) -> HeatCounter .collect::>(); for point in points.iter() { - render_circle(&mut layer, *point, 10, [1].into()); + render_circle(&mut layer, *point, (zoom as u64 / 4).max(1), [1].into()); } for (a, b) in points.iter().zip(points.iter().skip(1)) { - render_line(&mut layer, *a, *b, 10, [1].into()); + render_line(&mut layer, *a, *b, (zoom as u64 / 4).max(1), [1].into()); } merge_heat_counter(&mut heatcounter, &layer); + bar.inc(1); } + bar.finish(); heatcounter } -- cgit v1.2.3