From d3978310b451d5d2046f9d0d4d62a8d3a17490b6 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 11 Jun 2019 00:19:37 +0100
Subject: [PATCH] Get it displaying device locations, but they aren't lined up
 yet.

---
 client_src/js/MapManager.mjs             | 12 ++------
 client_src/js/Overlay/VoronoiManager.mjs | 22 ++++++++++++++
 client_src/js/Overlay/VoronoiOverlay.mjs | 37 ++++++++++++++----------
 3 files changed, 45 insertions(+), 26 deletions(-)
 create mode 100644 client_src/js/Overlay/VoronoiManager.mjs

diff --git a/client_src/js/MapManager.mjs b/client_src/js/MapManager.mjs
index b1aeb64..0ee4de3 100644
--- a/client_src/js/MapManager.mjs
+++ b/client_src/js/MapManager.mjs
@@ -7,13 +7,12 @@ import 'leaflet-fullscreen';
 
 import Config from './Config.mjs';
 import LayerDeviceMarkers from './LayerDeviceMarkers.mjs';
-import VoronoiOverlay from './Overlay/VoronoiOverlay.mjs';
+import VoronoiManager from './Overlay/VoronoiManager.mjs';
 // import LayerHeatmap from './LayerHeatmap.mjs';
 // import LayerHeatmapGlue from './LayerHeatmapGlue.mjs';
 import DeviceData from './DeviceData.mjs';
 import UI from './UI.mjs';
 
-import VoronoiCell from './Overlay/VoronoiCell.mjs';
 import Vector2 from './Helpers/Vector2.mjs';
 
 class MapManager {
@@ -70,14 +69,7 @@ class MapManager {
 	}
 	
 	setup_overlay() {
-		this.overlay = new VoronoiOverlay();
-		this.overlay.addCells(
-			new VoronoiCell(new Vector2(53.739429, -0.445607)),
-			new VoronoiCell(new Vector2(53.775879, -0.413569)),
-			new VoronoiCell(new Vector2(53.745144, -0.279373)),
-			new VoronoiCell(new Vector2(53.738222, -0.335983))
-		);
-		this.overlay.add_to(this.map);
+		this.overlay = new VoronoiManager(this.device_data, this.map);
 	}
 	
 	setup_time_dimension() {
diff --git a/client_src/js/Overlay/VoronoiManager.mjs b/client_src/js/Overlay/VoronoiManager.mjs
new file mode 100644
index 0000000..457a931
--- /dev/null
+++ b/client_src/js/Overlay/VoronoiManager.mjs
@@ -0,0 +1,22 @@
+"use strict";
+
+import VoronoiOverlay from './VoronoiOverlay.mjs';
+import VoronoiCell from './VoronoiCell.mjs';
+
+import Vector2 from '../Helpers/Vector2.mjs';
+
+class VoronoiManager {
+	constructor(in_device_data, map) {
+		this.device_data = in_device_data;
+		this.overlay = new VoronoiOverlay();
+		this.overlay.addCells(...this.device_data.devices
+			.filter((device) => typeof device.latitude == "number" &&
+				typeof device.longitude == "number")
+			.map((device) => 
+				new VoronoiCell(new Vector2(device.latitude, device.longitude))
+			));
+		this.overlay.add_to(map);
+	}
+}
+
+export default VoronoiManager;
diff --git a/client_src/js/Overlay/VoronoiOverlay.mjs b/client_src/js/Overlay/VoronoiOverlay.mjs
index 91ab4f4..e3cd003 100644
--- a/client_src/js/Overlay/VoronoiOverlay.mjs
+++ b/client_src/js/Overlay/VoronoiOverlay.mjs
@@ -19,6 +19,10 @@ class VoronoiOverlay {
 		this.cells = [];
 	}
 	
+	/**
+	 * Adds a cell to the voronoi overlay.
+	 * @param {VoronoiCell} cells The cell to add. May be specified as many times as requires to add cells in bulk.
+	 */
 	addCells(...cells) {
 		this.cells.push(...cells);
 	}
@@ -37,14 +41,12 @@ class VoronoiOverlay {
 		}
 		
 		for(let cell of this.cells) {
-			if(!isNaN(cell.point.x)) {
-				if(cell.point.x < result.x_min) result.x_min = cell.point.x;
-				if(cell.point.x > result.x_max) result.x_max = cell.point.x;
-			}
-			if(!isNaN(cell.point.y)) {
-				if(cell.point.y < result.y_min) result.y_min = cell.point.y;
-				if(cell.point.y > result.y_max) result.y_max = cell.point.y;
-			}
+			// TODO: Remove this restriction
+			if(cell.point.x < 40) continue; // Exclude the freetown one for testing 'cause it's miles away
+			if(cell.point.x < result.x_min) result.x_min = cell.point.x;
+			if(cell.point.x > result.x_max) result.x_max = cell.point.x;
+			if(cell.point.y < result.y_min) result.y_min = cell.point.y;
+			if(cell.point.y > result.y_max) result.y_max = cell.point.y;
 		}
 		
 		result.x_min -= this.border.x;
@@ -83,7 +85,7 @@ class VoronoiOverlay {
 		
 		console.log(this.cells);
 		
-		this.svg = new SvgWriter(
+		let svg = new SvgWriter(
 			"100%", "100%",
 			bounding_box,
 			true
@@ -91,15 +93,18 @@ class VoronoiOverlay {
 		
 		// TODO: Render the SVG here
 		for(let cell of this.cells) {
-			this.svg.addPolygon(
-				`hsla(${(Math.random()*360).toFixed(2)}, 50%, 50%, 0.6)`,
-				cell.polygon
-			);
-			this.svg.addCircle(cell.point, 0.005, "red");
+			if(cell.polygon !== null) {
+				svg.addPolygon(
+					`hsla(${(Math.random()*360).toFixed(2)}, 50%, 50%, 0.6)`,
+					cell.polygon
+				);
+			}
+			svg.addCircle(cell.point, 0.005, "red");
 		}
 		
-		this.svg.complete();
-		return this.svg.toString();
+		svg.complete();
+		console.log(svg.toString());
+		return svg.toString();
 	}
 	
 	add_to(map) {
