use std::{fs, path::Path}; use color_eyre::eyre::{bail, Result}; use image::{ImageBuffer, Luma, Pixel, RgbaImage}; use nalgebra::{vector, Vector2}; use super::{ gpx::Coordinates, layer::{self, TileLayer}, }; pub type HeatCounter = TileLayer>; fn render_circle(layer: &mut TileLayer

, center: (u64, u64), radius: u64, pixel: P) { let topleft = (center.0 - radius, center.1 - radius); let rad_32: u32 = radius.try_into().unwrap(); let mut circle = ImageBuffer::>::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(), ), radius.try_into().unwrap(), pixel, ); layer.blit_nonzero(topleft.0, topleft.1, &circle); } fn direction_vector(a: (u64, u64), b: (u64, u64)) -> Vector2 { let dx = if b.0 > a.0 { (b.0 - a.0) as f64 } else { -((a.0 - b.0) as f64) }; let dy = if b.1 > a.1 { (b.1 - a.1) as f64 } else { -((a.1 - b.1) as f64) }; vector![dx, dy] } fn render_line( layer: &mut TileLayer

, start: (u64, u64), end: (u64, u64), thickness: u64, pixel: P, ) { use imageproc::point::Point; if start == end { return; } fn unsigned_add(a: Vector2, b: Vector2) -> Vector2 { let x = if b[0] < 0 { a[0] - u64::from(b[0].unsigned_abs()) } else { a[0] + u64::try_from(b[0]).unwrap() }; let y = if b[1] < 0 { a[1] - u64::from(b[1].unsigned_abs()) } else { a[1] + u64::try_from(b[1]).unwrap() }; vector![x, y] } let r = direction_vector(start, end); let normal = vector![r[1], -r[0]].normalize(); let start = vector![start.0, start.1]; let end = vector![end.0, end.1]; 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), unsigned_add(end, -displacement), unsigned_add(start, -displacement), ]; let min_x = polygon.iter().map(|p| p[0]).min().unwrap(); let min_y = polygon.iter().map(|p| p[1]).min().unwrap(); let max_x = polygon.iter().map(|p| p[0]).max().unwrap(); let max_y = polygon.iter().map(|p| p[1]).max().unwrap(); let mut overlay = ImageBuffer::>::new( (max_x - min_x).try_into().unwrap(), (max_y - min_y).try_into().unwrap(), ); let adjusted_poly = polygon .into_iter() .map(|p| Point::new((p[0] - min_x) as i32, (p[1] - min_y) as i32)) .collect::>(); imageproc::drawing::draw_polygon_mut(&mut overlay, &adjusted_poly, pixel); layer.blit_nonzero(min_x, min_y, &overlay); } fn merge_heat_counter(base: &mut HeatCounter, overlay: &HeatCounter) { for (tx, ty, source) in overlay.enumerate_tiles() { let target = base.tile_mut(tx, ty); for (x, y, source) in source.enumerate_pixels() { let target = target.get_pixel_mut(x, y); target[0] += source[0]; } } } fn colorize_tile(tile: &ImageBuffer, Vec>, max: u32) -> RgbaImage { 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(1.0 - alpha); let target = result.get_pixel_mut(x, y); *target = color.to_rgba8().into(); } } result } /// Lazily colorizes a [`HeatCounter`] by colorizing it tile-by-tile and saving a tile before /// rendering the next one. /// /// This has a way lower memory usage than [`colorize_heatcounter`]. pub fn lazy_colorization, 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(()); } 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()); let metadata = folder.metadata(); match metadata { Err(_) => fs::create_dir(&folder)?, Ok(m) if !m.is_dir() => bail!("Output path is not a directory"), _ => {} } let file = folder.join(format!("{tile_y}.png")); layer::compress_png(&colorized, file)?; progress_callback(1); } Ok(()) } /// Renders the heat counter for the given zoom level and track points. pub fn render_heatcounter( zoom: u32, tracks: &[Vec], mut progress_callback: F, ) -> HeatCounter { let mut heatcounter = TileLayer::from_pixel([0].into()); for track in tracks { let mut layer = TileLayer::from_pixel([0].into()); let points = track .iter() .map(|coords| coords.web_mercator(zoom)) .collect::>(); for point in points.iter() { render_circle(&mut layer, *point, (zoom as u64 / 4).max(2) - 1, [1].into()); } for (a, b) in points.iter().zip(points.iter().skip(1)) { render_line(&mut layer, *a, *b, (zoom as u64 / 4).max(1), [1].into()); } merge_heat_counter(&mut heatcounter, &layer); progress_callback(1); } heatcounter }