2019-05-29 19:21:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
import L from 'leaflet';
|
|
|
|
import { Delaunay } from 'd3-delaunay';
|
2019-05-29 20:06:00 +00:00
|
|
|
|
|
|
|
import Vector2 from '../Helpers/Vector2.mjs';
|
|
|
|
import Rectangle from '../Helpers/Rectangle.mjs';
|
|
|
|
|
2019-06-10 23:32:01 +00:00
|
|
|
import SvgWriter from '../Helpers/SvgWriter.mjs';
|
2019-05-29 20:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates and manages a single voronoi SVGOverlay layer.
|
|
|
|
*/
|
2019-05-29 19:21:03 +00:00
|
|
|
class VoronoiOverlay {
|
|
|
|
constructor() {
|
2019-05-29 20:06:00 +00:00
|
|
|
this.border = new Vector2(0.1, 0.1); // lat / long
|
|
|
|
|
|
|
|
this.cells = [];
|
|
|
|
}
|
|
|
|
|
2019-06-10 23:19:37 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-05-29 20:06:00 +00:00
|
|
|
addCells(...cells) {
|
|
|
|
this.cells.push(...cells);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes the bounding box of all the currently registered points.
|
|
|
|
* Includes a border, which is defined by this.border.
|
|
|
|
* @return {Rectangle} The bounding box of the currently registered points.
|
|
|
|
*/
|
|
|
|
computeBoundingBox() {
|
2019-06-10 22:24:28 +00:00
|
|
|
let result = {
|
|
|
|
x_min: Infinity,
|
|
|
|
x_max: -Infinity,
|
|
|
|
y_min: Infinity,
|
|
|
|
y_max: -Infinity
|
|
|
|
}
|
2019-05-29 20:06:00 +00:00
|
|
|
|
|
|
|
for(let cell of this.cells) {
|
2019-06-10 23:19:37 +00:00
|
|
|
// TODO: Remove this restriction
|
2019-06-10 23:32:01 +00:00
|
|
|
if(cell.point.y < 40) continue; // Exclude the freetown one for testing 'cause it's miles away
|
2019-06-10 23:19:37 +00:00
|
|
|
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;
|
2019-05-29 20:06:00 +00:00
|
|
|
}
|
2019-05-29 19:21:03 +00:00
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
result.x_min -= this.border.x;
|
|
|
|
result.y_min -= this.border.y;
|
|
|
|
result.x_max += this.border.x;
|
|
|
|
result.y_max += this.border.y;
|
2019-05-29 20:06:00 +00:00
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
return new Rectangle(
|
|
|
|
result.x_min,
|
|
|
|
result.y_min,
|
|
|
|
result.x_max - result.x_min,
|
|
|
|
result.y_max - result.y_min
|
|
|
|
);
|
2019-05-29 20:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let bounding_box = this.computeBoundingBox();
|
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
let voronoi = Delaunay.from(this.cells.map(
|
|
|
|
(cell) => [cell.point.x, cell.point.y])
|
|
|
|
).voronoi([
|
|
|
|
bounding_box.x, bounding_box.y,
|
|
|
|
bounding_box.Right, bounding_box.Bottom
|
|
|
|
]);
|
2019-05-29 20:06:00 +00:00
|
|
|
|
2019-06-10 22:24:28 +00:00
|
|
|
console.log(voronoi);
|
2019-05-29 20:08:01 +00:00
|
|
|
|
|
|
|
// TODO: Map the generated polygons back onto this.cells
|
2019-06-10 22:24:28 +00:00
|
|
|
let i = 0;
|
|
|
|
for(let polygon of voronoi.cellPolygons()) {
|
|
|
|
let our_cell = this.cells[i];
|
|
|
|
our_cell.polygon = polygon.map((point) => new Vector2(...point));
|
|
|
|
|
|
|
|
i++;
|
2019-06-09 22:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
console.log(this.cells);
|
2019-05-29 20:06:00 +00:00
|
|
|
|
2019-06-10 23:19:37 +00:00
|
|
|
let svg = new SvgWriter(
|
2019-06-10 22:24:28 +00:00
|
|
|
"100%", "100%",
|
|
|
|
bounding_box,
|
|
|
|
true
|
|
|
|
);
|
2019-05-29 20:08:01 +00:00
|
|
|
|
|
|
|
// TODO: Render the SVG here
|
2019-06-10 22:24:28 +00:00
|
|
|
for(let cell of this.cells) {
|
2019-06-10 23:19:37 +00:00
|
|
|
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");
|
2019-06-10 22:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 23:19:37 +00:00
|
|
|
svg.complete();
|
|
|
|
console.log(svg.toString());
|
|
|
|
return svg.toString();
|
2019-06-10 22:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add_to(map) {
|
|
|
|
let bounds = this.computeBoundingBox();
|
|
|
|
this.layer = L.svgOverlay(
|
|
|
|
SvgWriter.string2element(this.render()),
|
|
|
|
L.latLngBounds(
|
2019-06-10 23:32:01 +00:00
|
|
|
L.latLng(bounds.TopLeft.y, bounds.TopLeft.x),
|
|
|
|
L.latLng(bounds.BottomRight.y, bounds.BottomRight.x)
|
2019-06-10 22:24:28 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
this.layer.addTo(map);
|
2019-05-29 20:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
generate_overlay() {
|
|
|
|
// TODO: Generate the Leaflet SVGOverlay here
|
2019-05-29 19:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 20:06:00 +00:00
|
|
|
|
2019-05-29 19:21:03 +00:00
|
|
|
export default VoronoiOverlay;
|