summary refs log tree commit diff
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2017-08-27 12:32:08 +0300
committerGitHub <noreply@github.com>2017-08-27 12:32:08 +0300
commit21b63ad2d37be7a558a5984a52fd2e3d4ad3536c (patch)
tree9e560d1cde6bd2597ef3e4595492a8d28ac0daf7
parent(no commit message) (diff)
downloadheatmap-21b63ad2d37be7a558a5984a52fd2e3d4ad3536c.tar.gz
heatmap-21b63ad2d37be7a558a5984a52fd2e3d4ad3536c.zip
-rw-r--r--ascii-town-heatmap.rs64
1 files changed, 35 insertions, 29 deletions
diff --git a/ascii-town-heatmap.rs b/ascii-town-heatmap.rs
index 23b80da..38ff338 100644
--- a/ascii-town-heatmap.rs
+++ b/ascii-town-heatmap.rs
@@ -1,7 +1,8 @@
 // cargo-deps: hsl, image, csv = "1.0.0-beta.4", serde, serde_derive
+
+extern crate csv;
 extern crate hsl;
 extern crate image;
-extern crate csv;
 extern crate serde;
 #[macro_use]
 extern crate serde_derive;
@@ -18,51 +19,55 @@ struct Tile {
     modify_count: u64,
     modify_time: u64,
     access_count: u64,
-    access_time: u64
+    access_time: u64,
 }
 
-// Torus size.
-const W: u32 = 512;
+// Torus size (in tiles).
+const TORUS_SZ: u32 = 512;
 
-// Tile width/height in pixels.
-const TW: u32 = 4;
-const TH: u32 = 1;
+// Tile width/height (in pixels).
+const TILE_W: u32 = 4;
+const TILE_H: u32 = 1;
 
 fn main() {
     // Read the `torus.csv` into a 2D array of `(access, modify)`.
     // Also track the values we see in `BTreeSet` (for ordering).
-    let mut tiles = [[(0, 0); W as usize]; W as usize];
-    let mut sa = BTreeSet::new();
-    let mut sm = BTreeSet::new();
-    let mut rdr = csv::Reader::from_reader(io::stdin());
-    for tile in rdr.deserialize().map(Result::<Tile, _>::unwrap) {
-        let a = tile.access_count;
-        let m = tile.modify_count;
+    let mut tiles = [[(0, 0); TORUS_SZ as usize]; TORUS_SZ as usize];
+    let mut ord_a = BTreeSet::new();
+    let mut ord_m = BTreeSet::new();
+    for result in csv::Reader::from_reader(io::stdin()).deserialize() {
+        let Tile {
+            access_count: a,
+            modify_count: m,
+            tile_x: x,
+            tile_y: y,
+            ..
+        } = result.unwrap();
 
         // HACK(eddyb) ignore 0,0 for analysis - too busy.
-        if (tile.tile_x, tile.tile_y) != (0, 0) {
-            sa.insert(a);
-            sm.insert(m);
+        if (x, y) != (0, 0) {
+            ord_a.insert(a);
+            ord_m.insert(m);
         }
 
-        tiles[tile.tile_y][tile.tile_x] = (a, m);
+        tiles[y][x] = (a, m);
     }
 
     // Extract the maximum values.
     // TODO(eddyb) chunk values for better representation.
-    let ma = sa.iter().next_back().cloned().unwrap_or(0) as f64;
-    let mm = sm.iter().next_back().cloned().unwrap_or(0) as f64;
+    let max_a = ord_a.iter().next_back().cloned().unwrap_or(0) as f64;
+    let max_m = ord_m.iter().next_back().cloned().unwrap_or(0) as f64;
 
     // Compose the heatmap image in-memory from the 2D array.
-    let mut heatmap = image::ImageBuffer::new(W * TW, W * TH);
+    let mut heatmap = image::ImageBuffer::new(TORUS_SZ * TILE_W, TORUS_SZ * TILE_H);
     let red = hsl::HSL::from_rgb(&[255, 0, 0]).h;
     // let green = hsl::HSL::from_rgb(&[0, 255, 0]).h;
     // let blue = hsl::HSL::from_rgb(&[0, 0, 255]).h;
-    for y in 0..W {
-        for x in 0..W {
+    for y in 0..TORUS_SZ {
+        for x in 0..TORUS_SZ {
             let (a, m) = tiles[y as usize][x as usize];
-            let a = (a as f64 / ma).min(1.0).powf(0.1);
-            let m = (m as f64 / mm).min(1.0).powf(0.1);
+            let a = (a as f64 / max_a).min(1.0).powf(0.1);
+            let m = (m as f64 / max_m).min(1.0).powf(0.1);
 
             // access => luminosity, modify => hue (green -> red)
             // let h = green * (1.0 - m) + red * m;
@@ -77,10 +82,11 @@ fn main() {
             let (r, g, b) = hsl::HSL { h, s, l }.to_rgb();
             let rgb = image::Rgb([r, g, b]);
 
-            for dy in 0..TH {
-                let y = ((y * 2 + W + 1) * TH / 2 + dy) % (W * TH);
-                for dx in 0..TW {
-                    let x = ((x * 2 + W + 1) * TW / 2 + dx) % (W * TW);
+            let coord = |x, dx, px| ((x * 2 + TORUS_SZ + 1) * px / 2 + dx) % (TORUS_SZ * px);
+            for dy in 0..TILE_H {
+                let y = coord(y, dy, TILE_H);
+                for dx in 0..TILE_W {
+                    let x = coord(x, dx, TILE_W);
                     heatmap.put_pixel(x, y, rgb);
                 }
             }