948 lines
29 KiB
JavaScript
948 lines
29 KiB
JavaScript
// https://github.com/d3/d3-delaunay v4.1.5 Copyright 2018 Mike Bostock
|
|
// https://github.com/mapbox/delaunator v2.0.5. Copyright 2017 Mapbox, Inc.
|
|
(function (global, factory) {
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
(factory((global.d3 = global.d3 || {})));
|
|
}(this, (function (exports) { 'use strict';
|
|
|
|
const EPSILON = Math.pow(2, -52);
|
|
|
|
class Delaunator {
|
|
|
|
static from(points, getX, getY) {
|
|
if (!getX) getX = defaultGetX;
|
|
if (!getY) getY = defaultGetY;
|
|
|
|
const n = points.length;
|
|
const coords = new Float64Array(n * 2);
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
const p = points[i];
|
|
coords[2 * i] = getX(p);
|
|
coords[2 * i + 1] = getY(p);
|
|
}
|
|
|
|
return new Delaunator(coords);
|
|
}
|
|
|
|
constructor(coords) {
|
|
let minX = Infinity;
|
|
let minY = Infinity;
|
|
let maxX = -Infinity;
|
|
let maxY = -Infinity;
|
|
|
|
const n = coords.length >> 1;
|
|
const ids = this.ids = new Uint32Array(n);
|
|
|
|
if (n > 0 && typeof coords[0] !== 'number') throw new Error('Expected coords to contain numbers.');
|
|
|
|
this.coords = coords;
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
const x = coords[2 * i];
|
|
const y = coords[2 * i + 1];
|
|
if (x < minX) minX = x;
|
|
if (y < minY) minY = y;
|
|
if (x > maxX) maxX = x;
|
|
if (y > maxY) maxY = y;
|
|
ids[i] = i;
|
|
}
|
|
|
|
const cx = (minX + maxX) / 2;
|
|
const cy = (minY + maxY) / 2;
|
|
|
|
let minDist = Infinity;
|
|
let i0, i1, i2;
|
|
|
|
// pick a seed point close to the centroid
|
|
for (let i = 0; i < n; i++) {
|
|
const d = dist(cx, cy, coords[2 * i], coords[2 * i + 1]);
|
|
if (d < minDist) {
|
|
i0 = i;
|
|
minDist = d;
|
|
}
|
|
}
|
|
const i0x = coords[2 * i0];
|
|
const i0y = coords[2 * i0 + 1];
|
|
|
|
minDist = Infinity;
|
|
|
|
// find the point closest to the seed
|
|
for (let i = 0; i < n; i++) {
|
|
if (i === i0) continue;
|
|
const d = dist(i0x, i0y, coords[2 * i], coords[2 * i + 1]);
|
|
if (d < minDist && d > 0) {
|
|
i1 = i;
|
|
minDist = d;
|
|
}
|
|
}
|
|
let i1x = coords[2 * i1];
|
|
let i1y = coords[2 * i1 + 1];
|
|
|
|
let minRadius = Infinity;
|
|
|
|
// find the third point which forms the smallest circumcircle with the first two
|
|
for (let i = 0; i < n; i++) {
|
|
if (i === i0 || i === i1) continue;
|
|
const r = circumradius(i0x, i0y, i1x, i1y, coords[2 * i], coords[2 * i + 1]);
|
|
if (r < minRadius) {
|
|
i2 = i;
|
|
minRadius = r;
|
|
}
|
|
}
|
|
let i2x = coords[2 * i2];
|
|
let i2y = coords[2 * i2 + 1];
|
|
|
|
if (minRadius === Infinity) {
|
|
throw new Error('No Delaunay triangulation exists for this input.');
|
|
}
|
|
|
|
// swap the order of the seed points for counter-clockwise orientation
|
|
if (orient(i0x, i0y, i1x, i1y, i2x, i2y)) {
|
|
const i = i1;
|
|
const x = i1x;
|
|
const y = i1y;
|
|
i1 = i2;
|
|
i1x = i2x;
|
|
i1y = i2y;
|
|
i2 = i;
|
|
i2x = x;
|
|
i2y = y;
|
|
}
|
|
|
|
const center = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
|
|
this._cx = center.x;
|
|
this._cy = center.y;
|
|
|
|
// sort the points by distance from the seed triangle circumcenter
|
|
quicksort(ids, coords, 0, ids.length - 1, center.x, center.y);
|
|
|
|
// initialize a hash table for storing edges of the advancing convex hull
|
|
this._hashSize = Math.ceil(Math.sqrt(n));
|
|
this._hash = new Array(this._hashSize);
|
|
|
|
// initialize a circular doubly-linked list that will hold an advancing convex hull
|
|
let e = this.hull = insertNode(coords, i0);
|
|
this._hashEdge(e);
|
|
e.t = 0;
|
|
e = insertNode(coords, i1, e);
|
|
this._hashEdge(e);
|
|
e.t = 1;
|
|
e = insertNode(coords, i2, e);
|
|
this._hashEdge(e);
|
|
e.t = 2;
|
|
|
|
const maxTriangles = 2 * n - 5;
|
|
const triangles = this.triangles = new Uint32Array(maxTriangles * 3);
|
|
const halfedges = this.halfedges = new Int32Array(maxTriangles * 3);
|
|
|
|
this.trianglesLen = 0;
|
|
|
|
this._addTriangle(i0, i1, i2, -1, -1, -1);
|
|
|
|
for (let k = 0, xp, yp; k < ids.length; k++) {
|
|
const i = ids[k];
|
|
const x = coords[2 * i];
|
|
const y = coords[2 * i + 1];
|
|
|
|
// skip near-duplicate points
|
|
if (k > 0 && Math.abs(x - xp) <= EPSILON && Math.abs(y - yp) <= EPSILON) continue;
|
|
xp = x;
|
|
yp = y;
|
|
|
|
// skip seed triangle points
|
|
if (i === i0 || i === i1 || i === i2) continue;
|
|
|
|
// find a visible edge on the convex hull using edge hash
|
|
const startKey = this._hashKey(x, y);
|
|
let key = startKey;
|
|
let start;
|
|
do {
|
|
start = this._hash[key];
|
|
key = (key + 1) % this._hashSize;
|
|
} while ((!start || start.removed) && key !== startKey);
|
|
|
|
start = start.prev;
|
|
e = start;
|
|
while (!orient(x, y, e.x, e.y, e.next.x, e.next.y)) {
|
|
e = e.next;
|
|
if (e === start) {
|
|
e = null;
|
|
break;
|
|
}
|
|
}
|
|
// likely a near-duplicate point; skip it
|
|
if (!e) continue;
|
|
|
|
const walkBack = e === start;
|
|
|
|
// add the first triangle from the point
|
|
let t = this._addTriangle(e.i, i, e.next.i, -1, -1, e.t);
|
|
|
|
e.t = t; // keep track of boundary triangles on the hull
|
|
e = insertNode(coords, i, e);
|
|
|
|
// recursively flip triangles from the point until they satisfy the Delaunay condition
|
|
e.t = this._legalize(t + 2);
|
|
|
|
// walk forward through the hull, adding more triangles and flipping recursively
|
|
let q = e.next;
|
|
while (orient(x, y, q.x, q.y, q.next.x, q.next.y)) {
|
|
t = this._addTriangle(q.i, i, q.next.i, q.prev.t, -1, q.t);
|
|
q.prev.t = this._legalize(t + 2);
|
|
this.hull = removeNode(q);
|
|
q = q.next;
|
|
}
|
|
|
|
if (walkBack) {
|
|
// walk backward from the other side, adding more triangles and flipping
|
|
q = e.prev;
|
|
while (orient(x, y, q.prev.x, q.prev.y, q.x, q.y)) {
|
|
t = this._addTriangle(q.prev.i, i, q.i, -1, q.t, q.prev.t);
|
|
this._legalize(t + 2);
|
|
q.prev.t = t;
|
|
this.hull = removeNode(q);
|
|
q = q.prev;
|
|
}
|
|
}
|
|
|
|
// save the two new edges in the hash table
|
|
this._hashEdge(e);
|
|
this._hashEdge(e.prev);
|
|
}
|
|
|
|
// trim typed triangle mesh arrays
|
|
this.triangles = triangles.subarray(0, this.trianglesLen);
|
|
this.halfedges = halfedges.subarray(0, this.trianglesLen);
|
|
}
|
|
|
|
_hashEdge(e) {
|
|
this._hash[this._hashKey(e.x, e.y)] = e;
|
|
}
|
|
|
|
_hashKey(x, y) {
|
|
return Math.floor(pseudoAngle(x - this._cx, y - this._cy) * this._hashSize) % this._hashSize;
|
|
}
|
|
|
|
_legalize(a) {
|
|
const {triangles, coords, halfedges} = this;
|
|
|
|
const b = halfedges[a];
|
|
|
|
/* if the pair of triangles doesn't satisfy the Delaunay condition
|
|
* (p1 is inside the circumcircle of [p0, pl, pr]), flip them,
|
|
* then do the same check/flip recursively for the new pair of triangles
|
|
*
|
|
* pl pl
|
|
* /||\ / \
|
|
* al/ || \bl al/ \a
|
|
* / || \ / \
|
|
* / a||b \ flip /___ar___\
|
|
* p0\ || /p1 => p0\---bl---/p1
|
|
* \ || / \ /
|
|
* ar\ || /br b\ /br
|
|
* \||/ \ /
|
|
* pr pr
|
|
*/
|
|
const a0 = a - a % 3;
|
|
const b0 = b - b % 3;
|
|
|
|
const al = a0 + (a + 1) % 3;
|
|
const ar = a0 + (a + 2) % 3;
|
|
const bl = b0 + (b + 2) % 3;
|
|
|
|
if (b === -1) return ar;
|
|
|
|
const p0 = triangles[ar];
|
|
const pr = triangles[a];
|
|
const pl = triangles[al];
|
|
const p1 = triangles[bl];
|
|
|
|
const illegal = inCircle(
|
|
coords[2 * p0], coords[2 * p0 + 1],
|
|
coords[2 * pr], coords[2 * pr + 1],
|
|
coords[2 * pl], coords[2 * pl + 1],
|
|
coords[2 * p1], coords[2 * p1 + 1]);
|
|
|
|
if (illegal) {
|
|
triangles[a] = p1;
|
|
triangles[b] = p0;
|
|
|
|
const hbl = halfedges[bl];
|
|
|
|
// edge swapped on the other side of the hull (rare); fix the halfedge reference
|
|
if (hbl === -1) {
|
|
let e = this.hull;
|
|
do {
|
|
if (e.t === bl) {
|
|
e.t = a;
|
|
break;
|
|
}
|
|
e = e.next;
|
|
} while (e !== this.hull);
|
|
}
|
|
this._link(a, hbl);
|
|
this._link(b, halfedges[ar]);
|
|
this._link(ar, bl);
|
|
|
|
const br = b0 + (b + 1) % 3;
|
|
|
|
this._legalize(a);
|
|
return this._legalize(br);
|
|
}
|
|
|
|
return ar;
|
|
}
|
|
|
|
_link(a, b) {
|
|
this.halfedges[a] = b;
|
|
if (b !== -1) this.halfedges[b] = a;
|
|
}
|
|
|
|
// add a new triangle given vertex indices and adjacent half-edge ids
|
|
_addTriangle(i0, i1, i2, a, b, c) {
|
|
const t = this.trianglesLen;
|
|
|
|
this.triangles[t] = i0;
|
|
this.triangles[t + 1] = i1;
|
|
this.triangles[t + 2] = i2;
|
|
|
|
this._link(t, a);
|
|
this._link(t + 1, b);
|
|
this._link(t + 2, c);
|
|
|
|
this.trianglesLen += 3;
|
|
|
|
return t;
|
|
}
|
|
}
|
|
|
|
// monotonically increases with real angle, but doesn't need expensive trigonometry
|
|
function pseudoAngle(dx, dy) {
|
|
const p = dx / (Math.abs(dx) + Math.abs(dy));
|
|
return (dy > 0 ? 3 - p : 1 + p) / 4; // [0..1]
|
|
}
|
|
|
|
function dist(ax, ay, bx, by) {
|
|
const dx = ax - bx;
|
|
const dy = ay - by;
|
|
return dx * dx + dy * dy;
|
|
}
|
|
|
|
function orient(px, py, qx, qy, rx, ry) {
|
|
return (qy - py) * (rx - qx) - (qx - px) * (ry - qy) < 0;
|
|
}
|
|
|
|
function inCircle(ax, ay, bx, by, cx, cy, px, py) {
|
|
const dx = ax - px;
|
|
const dy = ay - py;
|
|
const ex = bx - px;
|
|
const ey = by - py;
|
|
const fx = cx - px;
|
|
const fy = cy - py;
|
|
|
|
const ap = dx * dx + dy * dy;
|
|
const bp = ex * ex + ey * ey;
|
|
const cp = fx * fx + fy * fy;
|
|
|
|
return dx * (ey * cp - bp * fy) -
|
|
dy * (ex * cp - bp * fx) +
|
|
ap * (ex * fy - ey * fx) < 0;
|
|
}
|
|
|
|
function circumradius(ax, ay, bx, by, cx, cy) {
|
|
const dx = bx - ax;
|
|
const dy = by - ay;
|
|
const ex = cx - ax;
|
|
const ey = cy - ay;
|
|
|
|
const bl = dx * dx + dy * dy;
|
|
const cl = ex * ex + ey * ey;
|
|
const d = dx * ey - dy * ex;
|
|
|
|
const x = (ey * bl - dy * cl) * 0.5 / d;
|
|
const y = (dx * cl - ex * bl) * 0.5 / d;
|
|
|
|
return bl && cl && d && (x * x + y * y) || Infinity;
|
|
}
|
|
|
|
function circumcenter(ax, ay, bx, by, cx, cy) {
|
|
const dx = bx - ax;
|
|
const dy = by - ay;
|
|
const ex = cx - ax;
|
|
const ey = cy - ay;
|
|
|
|
const bl = dx * dx + dy * dy;
|
|
const cl = ex * ex + ey * ey;
|
|
const d = dx * ey - dy * ex;
|
|
|
|
const x = ax + (ey * bl - dy * cl) * 0.5 / d;
|
|
const y = ay + (dx * cl - ex * bl) * 0.5 / d;
|
|
|
|
return {x, y};
|
|
}
|
|
|
|
// create a new node in a doubly linked list
|
|
function insertNode(coords, i, prev) {
|
|
const node = {
|
|
i,
|
|
x: coords[2 * i],
|
|
y: coords[2 * i + 1],
|
|
t: 0,
|
|
prev: null,
|
|
next: null,
|
|
removed: false
|
|
};
|
|
|
|
if (!prev) {
|
|
node.prev = node;
|
|
node.next = node;
|
|
|
|
} else {
|
|
node.next = prev.next;
|
|
node.prev = prev;
|
|
prev.next.prev = node;
|
|
prev.next = node;
|
|
}
|
|
return node;
|
|
}
|
|
|
|
function removeNode(node) {
|
|
node.prev.next = node.next;
|
|
node.next.prev = node.prev;
|
|
node.removed = true;
|
|
return node.prev;
|
|
}
|
|
|
|
function quicksort(ids, coords, left, right, cx, cy) {
|
|
let i, j, temp;
|
|
|
|
if (right - left <= 20) {
|
|
for (i = left + 1; i <= right; i++) {
|
|
temp = ids[i];
|
|
j = i - 1;
|
|
while (j >= left && compare(coords, ids[j], temp, cx, cy) > 0) ids[j + 1] = ids[j--];
|
|
ids[j + 1] = temp;
|
|
}
|
|
} else {
|
|
const median = (left + right) >> 1;
|
|
i = left + 1;
|
|
j = right;
|
|
swap(ids, median, i);
|
|
if (compare(coords, ids[left], ids[right], cx, cy) > 0) swap(ids, left, right);
|
|
if (compare(coords, ids[i], ids[right], cx, cy) > 0) swap(ids, i, right);
|
|
if (compare(coords, ids[left], ids[i], cx, cy) > 0) swap(ids, left, i);
|
|
|
|
temp = ids[i];
|
|
while (true) {
|
|
do i++; while (compare(coords, ids[i], temp, cx, cy) < 0);
|
|
do j--; while (compare(coords, ids[j], temp, cx, cy) > 0);
|
|
if (j < i) break;
|
|
swap(ids, i, j);
|
|
}
|
|
ids[left + 1] = ids[j];
|
|
ids[j] = temp;
|
|
|
|
if (right - i + 1 >= j - left) {
|
|
quicksort(ids, coords, i, right, cx, cy);
|
|
quicksort(ids, coords, left, j - 1, cx, cy);
|
|
} else {
|
|
quicksort(ids, coords, left, j - 1, cx, cy);
|
|
quicksort(ids, coords, i, right, cx, cy);
|
|
}
|
|
}
|
|
}
|
|
|
|
function compare(coords, i, j, cx, cy) {
|
|
const d1 = dist(coords[2 * i], coords[2 * i + 1], cx, cy);
|
|
const d2 = dist(coords[2 * j], coords[2 * j + 1], cx, cy);
|
|
return (d1 - d2) || (coords[2 * i] - coords[2 * j]) || (coords[2 * i + 1] - coords[2 * j + 1]);
|
|
}
|
|
|
|
function swap(arr, i, j) {
|
|
const tmp = arr[i];
|
|
arr[i] = arr[j];
|
|
arr[j] = tmp;
|
|
}
|
|
|
|
function defaultGetX(p) {
|
|
return p[0];
|
|
}
|
|
function defaultGetY(p) {
|
|
return p[1];
|
|
}
|
|
|
|
const epsilon = 1e-6;
|
|
|
|
class Path {
|
|
constructor() {
|
|
this._x0 = this._y0 = // start of current subpath
|
|
this._x1 = this._y1 = null; // end of current subpath
|
|
this._ = "";
|
|
}
|
|
moveTo(x, y) {
|
|
this._ += `M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}`;
|
|
}
|
|
closePath() {
|
|
if (this._x1 !== null) {
|
|
this._x1 = this._x0, this._y1 = this._y0;
|
|
this._ += "Z";
|
|
}
|
|
}
|
|
lineTo(x, y) {
|
|
this._ += `L${this._x1 = +x},${this._y1 = +y}`;
|
|
}
|
|
arc(x, y, r) {
|
|
x = +x, y = +y, r = +r;
|
|
const x0 = x + r;
|
|
const y0 = y;
|
|
if (r < 0) throw new Error("negative radius");
|
|
if (this._x1 === null) this._ += `M${x0},${y0}`;
|
|
else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) this._ += "L" + x0 + "," + y0;
|
|
if (!r) return;
|
|
this._ += `A${r},${r},0,1,1,${x - r},${y}A${r},${r},0,1,1,${this._x1 = x0},${this._y1 = y0}`;
|
|
}
|
|
rect(x, y, w, h) {
|
|
this._ += `M${this._x0 = this._x1 = +x},${this._y0 = this._y1 = +y}h${+w}v${+h}h${-w}Z`;
|
|
}
|
|
value() {
|
|
return this._ || null;
|
|
}
|
|
}
|
|
|
|
class Polygon {
|
|
constructor() {
|
|
this._ = [];
|
|
}
|
|
moveTo(x, y) {
|
|
this._.push([x, y]);
|
|
}
|
|
closePath() {
|
|
this._.push(this._[0].slice());
|
|
}
|
|
lineTo(x, y) {
|
|
this._.push([x, y]);
|
|
}
|
|
value() {
|
|
return this._.length ? this._ : null;
|
|
}
|
|
}
|
|
|
|
class Voronoi {
|
|
constructor(delaunay, [xmin, ymin, xmax, ymax] = [0, 0, 960, 500]) {
|
|
if (!((xmax = +xmax) >= (xmin = +xmin)) || !((ymax = +ymax) >= (ymin = +ymin))) throw new Error("invalid bounds");
|
|
const {points, hull, triangles} = this.delaunay = delaunay;
|
|
const circumcenters = this.circumcenters = new Float64Array(triangles.length / 3 * 2);
|
|
const vectors = this.vectors = new Float64Array(points.length * 2);
|
|
this.xmax = xmax, this.xmin = xmin;
|
|
this.ymax = ymax, this.ymin = ymin;
|
|
|
|
// Compute circumcenters.
|
|
for (let i = 0, j = 0, n = triangles.length; i < n; i += 3, j += 2) {
|
|
const t1 = triangles[i] * 2;
|
|
const t2 = triangles[i + 1] * 2;
|
|
const t3 = triangles[i + 2] * 2;
|
|
const x1 = points[t1];
|
|
const y1 = points[t1 + 1];
|
|
const x2 = points[t2];
|
|
const y2 = points[t2 + 1];
|
|
const x3 = points[t3];
|
|
const y3 = points[t3 + 1];
|
|
const a2 = x1 - x2;
|
|
const a3 = x1 - x3;
|
|
const b2 = y1 - y2;
|
|
const b3 = y1 - y3;
|
|
const d1 = x1 * x1 + y1 * y1;
|
|
const d2 = d1 - x2 * x2 - y2 * y2;
|
|
const d3 = d1 - x3 * x3 - y3 * y3;
|
|
const ab = (a3 * b2 - a2 * b3) * 2;
|
|
circumcenters[j] = (b2 * d3 - b3 * d2) / ab;
|
|
circumcenters[j + 1] = (a3 * d2 - a2 * d3) / ab;
|
|
}
|
|
|
|
// Compute exterior cell rays.
|
|
let node = hull;
|
|
let p0, p1 = node.i * 4;
|
|
let x0, x1 = node.x;
|
|
let y0, y1 = node.y;
|
|
do {
|
|
node = node.next, p0 = p1, x0 = x1, y0 = y1, p1 = node.i * 4, x1 = node.x, y1 = node.y;
|
|
vectors[p0 + 2] = vectors[p1] = y0 - y1;
|
|
vectors[p0 + 3] = vectors[p1 + 1] = x1 - x0;
|
|
} while (node !== hull);
|
|
}
|
|
render(context) {
|
|
const buffer = context == null ? context = new Path : undefined;
|
|
const {delaunay: {halfedges, hull}, circumcenters, vectors} = this;
|
|
for (let i = 0, n = halfedges.length; i < n; ++i) {
|
|
const j = halfedges[i];
|
|
if (j < i) continue;
|
|
const ti = Math.floor(i / 3) * 2;
|
|
const tj = Math.floor(j / 3) * 2;
|
|
const xi = circumcenters[ti];
|
|
const yi = circumcenters[ti + 1];
|
|
const xj = circumcenters[tj];
|
|
const yj = circumcenters[tj + 1];
|
|
this._renderSegment(xi, yi, xj, yj, context);
|
|
}
|
|
let node = hull;
|
|
do {
|
|
node = node.next;
|
|
const t = Math.floor(node.t / 3) * 2;
|
|
const x = circumcenters[t];
|
|
const y = circumcenters[t + 1];
|
|
const v = node.i * 4;
|
|
const p = this._project(x, y, vectors[v + 2], vectors[v + 3]);
|
|
if (p) this._renderSegment(x, y, p[0], p[1], context);
|
|
} while (node !== hull);
|
|
return buffer && buffer.value();
|
|
}
|
|
renderBounds(context) {
|
|
const buffer = context == null ? context = new Path : undefined;
|
|
context.rect(this.xmin, this.ymin, this.xmax - this.xmin, this.ymax - this.ymin);
|
|
return buffer && buffer.value();
|
|
}
|
|
renderCell(i, context) {
|
|
const buffer = context == null ? context = new Path : undefined;
|
|
const points = this._clip(i);
|
|
if (points === null) return;
|
|
context.moveTo(points[0], points[1]);
|
|
for (let i = 2, n = points.length; i < n; i += 2) {
|
|
context.lineTo(points[i], points[i + 1]);
|
|
}
|
|
context.closePath();
|
|
return buffer && buffer.value();
|
|
}
|
|
*cellPolygons() {
|
|
const {delaunay: {points}} = this;
|
|
for (let i = 0, n = points.length / 2; i < n; ++i) {
|
|
const cell = this.cellPolygon(i);
|
|
if (cell) yield cell;
|
|
}
|
|
}
|
|
cellPolygon(i) {
|
|
const polygon = new Polygon;
|
|
this.renderCell(i, polygon);
|
|
return polygon.value();
|
|
}
|
|
_renderSegment(x0, y0, x1, y1, context) {
|
|
let S;
|
|
const c0 = this._regioncode(x0, y0);
|
|
const c1 = this._regioncode(x1, y1);
|
|
if (c0 === 0 && c1 === 0) {
|
|
context.moveTo(x0, y0);
|
|
context.lineTo(x1, y1);
|
|
} else if (S = this._clipSegment(x0, y0, x1, y1, c0, c1)) {
|
|
context.moveTo(S[0], S[1]);
|
|
context.lineTo(S[2], S[3]);
|
|
}
|
|
}
|
|
contains(i, x, y) {
|
|
if ((x = +x, x !== x) || (y = +y, y !== y)) return false;
|
|
return this.delaunay._step(i, x, y) === i;
|
|
}
|
|
_cell(i) {
|
|
const {circumcenters, delaunay: {inedges, halfedges, triangles}} = this;
|
|
const e0 = inedges[i];
|
|
if (e0 === -1) return null; // coincident point
|
|
const points = [];
|
|
let e = e0;
|
|
do {
|
|
const t = Math.floor(e / 3);
|
|
points.push(circumcenters[t * 2], circumcenters[t * 2 + 1]);
|
|
e = e % 3 === 2 ? e - 2 : e + 1;
|
|
if (triangles[e] !== i) break; // bad triangulation
|
|
e = halfedges[e];
|
|
} while (e !== e0 && e !== -1);
|
|
return points;
|
|
}
|
|
_clip(i) {
|
|
const points = this._cell(i);
|
|
if (points === null) return null;
|
|
const {vectors: V} = this;
|
|
const v = i * 4;
|
|
return V[v] || V[v + 1]
|
|
? this._clipInfinite(i, points, V[v], V[v + 1], V[v + 2], V[v + 3])
|
|
: this._clipFinite(i, points);
|
|
}
|
|
_clipFinite(i, points) {
|
|
const n = points.length;
|
|
let P = null;
|
|
let x0, y0, x1 = points[n - 2], y1 = points[n - 1];
|
|
let c0, c1 = this._regioncode(x1, y1);
|
|
let e0, e1;
|
|
for (let j = 0; j < n; j += 2) {
|
|
x0 = x1, y0 = y1, x1 = points[j], y1 = points[j + 1];
|
|
c0 = c1, c1 = this._regioncode(x1, y1);
|
|
if (c0 === 0 && c1 === 0) {
|
|
e0 = e1, e1 = 0;
|
|
if (P) P.push(x1, y1);
|
|
else P = [x1, y1];
|
|
} else {
|
|
let S, sx0, sy0, sx1, sy1;
|
|
if (c0 === 0) {
|
|
if ((S = this._clipSegment(x0, y0, x1, y1, c0, c1)) === null) continue;
|
|
[sx0, sy0, sx1, sy1] = S;
|
|
} else {
|
|
if ((S = this._clipSegment(x1, y1, x0, y0, c1, c0)) === null) continue;
|
|
[sx1, sy1, sx0, sy0] = S;
|
|
e0 = e1, e1 = this._edgecode(sx0, sy0);
|
|
if (e0 && e1) this._edge(i, e0, e1, P, P.length);
|
|
if (P) P.push(sx0, sy0);
|
|
else P = [sx0, sy0];
|
|
}
|
|
e0 = e1, e1 = this._edgecode(sx1, sy1);
|
|
if (e0 && e1) this._edge(i, e0, e1, P, P.length);
|
|
if (P) P.push(sx1, sy1);
|
|
else P = [sx1, sy1];
|
|
}
|
|
}
|
|
if (P) {
|
|
e0 = e1, e1 = this._edgecode(P[0], P[1]);
|
|
if (e0 && e1) this._edge(i, e0, e1, P, P.length);
|
|
} else if (this.contains(i, (this.xmin + this.xmax) / 2, (this.ymin + this.ymax) / 2)) {
|
|
return [this.xmax, this.ymin, this.xmax, this.ymax, this.xmin, this.ymax, this.xmin, this.ymin];
|
|
}
|
|
return P;
|
|
}
|
|
_clipSegment(x0, y0, x1, y1, c0, c1) {
|
|
while (true) {
|
|
if (c0 === 0 && c1 === 0) return [x0, y0, x1, y1];
|
|
if (c0 & c1) return null;
|
|
let x, y, c = c0 || c1;
|
|
if (c & 0b1000) x = x0 + (x1 - x0) * (this.ymax - y0) / (y1 - y0), y = this.ymax;
|
|
else if (c & 0b0100) x = x0 + (x1 - x0) * (this.ymin - y0) / (y1 - y0), y = this.ymin;
|
|
else if (c & 0b0010) y = y0 + (y1 - y0) * (this.xmax - x0) / (x1 - x0), x = this.xmax;
|
|
else y = y0 + (y1 - y0) * (this.xmin - x0) / (x1 - x0), x = this.xmin;
|
|
if (c0) x0 = x, y0 = y, c0 = this._regioncode(x0, y0);
|
|
else x1 = x, y1 = y, c1 = this._regioncode(x1, y1);
|
|
}
|
|
}
|
|
_clipInfinite(i, points, vx0, vy0, vxn, vyn) {
|
|
let P = Array.from(points), p;
|
|
if (p = this._project(P[0], P[1], vx0, vy0)) P.unshift(p[0], p[1]);
|
|
if (p = this._project(P[P.length - 2], P[P.length - 1], vxn, vyn)) P.push(p[0], p[1]);
|
|
if (P = this._clipFinite(i, P)) {
|
|
for (let j = 0, n = P.length, c0, c1 = this._edgecode(P[n - 2], P[n - 1]); j < n; j += 2) {
|
|
c0 = c1, c1 = this._edgecode(P[j], P[j + 1]);
|
|
if (c0 && c1) j = this._edge(i, c0, c1, P, j), n = P.length;
|
|
}
|
|
} else if (this.contains(i, (this.xmin + this.xmax) / 2, (this.ymin + this.ymax) / 2)) {
|
|
P = [this.xmin, this.ymin, this.xmax, this.ymin, this.xmax, this.ymax, this.xmin, this.ymax];
|
|
}
|
|
return P;
|
|
}
|
|
_edge(i, e0, e1, P, j) {
|
|
while (e0 !== e1) {
|
|
let x, y;
|
|
switch (e0) {
|
|
case 0b0101: e0 = 0b0100; continue; // top-left
|
|
case 0b0100: e0 = 0b0110, x = this.xmax, y = this.ymin; break; // top
|
|
case 0b0110: e0 = 0b0010; continue; // top-right
|
|
case 0b0010: e0 = 0b1010, x = this.xmax, y = this.ymax; break; // right
|
|
case 0b1010: e0 = 0b1000; continue; // bottom-right
|
|
case 0b1000: e0 = 0b1001, x = this.xmin, y = this.ymax; break; // bottom
|
|
case 0b1001: e0 = 0b0001; continue; // bottom-left
|
|
case 0b0001: e0 = 0b0101, x = this.xmin, y = this.ymin; break; // left
|
|
}
|
|
if ((P[j] !== x || P[j + 1] !== y) && this.contains(i, x, y)) {
|
|
P.splice(j, 0, x, y), j += 2;
|
|
}
|
|
}
|
|
return j;
|
|
}
|
|
_project(x0, y0, vx, vy) {
|
|
let t = Infinity, c, x, y;
|
|
if (vy < 0) { // top
|
|
if (y0 <= this.ymin) return null;
|
|
if ((c = (this.ymin - y0) / vy) < t) y = this.ymin, x = x0 + (t = c) * vx;
|
|
} else if (vy > 0) { // bottom
|
|
if (y0 >= this.ymax) return null;
|
|
if ((c = (this.ymax - y0) / vy) < t) y = this.ymax, x = x0 + (t = c) * vx;
|
|
}
|
|
if (vx > 0) { // right
|
|
if (x0 >= this.xmax) return null;
|
|
if ((c = (this.xmax - x0) / vx) < t) x = this.xmax, y = y0 + (t = c) * vy;
|
|
} else if (vx < 0) { // left
|
|
if (x0 <= this.xmin) return null;
|
|
if ((c = (this.xmin - x0) / vx) < t) x = this.xmin, y = y0 + (t = c) * vy;
|
|
}
|
|
return [x, y];
|
|
}
|
|
_edgecode(x, y) {
|
|
return (x === this.xmin ? 0b0001
|
|
: x === this.xmax ? 0b0010 : 0b0000)
|
|
| (y === this.ymin ? 0b0100
|
|
: y === this.ymax ? 0b1000 : 0b0000);
|
|
}
|
|
_regioncode(x, y) {
|
|
return (x < this.xmin ? 0b0001
|
|
: x > this.xmax ? 0b0010 : 0b0000)
|
|
| (y < this.ymin ? 0b0100
|
|
: y > this.ymax ? 0b1000 : 0b0000);
|
|
}
|
|
}
|
|
|
|
const tau = 2 * Math.PI;
|
|
|
|
function pointX(p) {
|
|
return p[0];
|
|
}
|
|
|
|
function pointY(p) {
|
|
return p[1];
|
|
}
|
|
|
|
class Delaunay {
|
|
constructor(points) {
|
|
const {halfedges, hull, triangles} = new Delaunator(points);
|
|
this.points = points;
|
|
this.halfedges = halfedges;
|
|
this.hull = hull;
|
|
this.triangles = triangles;
|
|
const inedges = this.inedges = new Int32Array(points.length / 2).fill(-1);
|
|
const outedges = this.outedges = new Int32Array(points.length / 2).fill(-1);
|
|
|
|
// Compute an index from each point to an (arbitrary) incoming halfedge.
|
|
for (let e = 0, n = halfedges.length; e < n; ++e) {
|
|
inedges[triangles[e % 3 === 2 ? e - 2 : e + 1]] = e;
|
|
}
|
|
|
|
// For points on the hull, index both the incoming and outgoing halfedges.
|
|
let node0, node1 = hull;
|
|
do {
|
|
node0 = node1, node1 = node1.next;
|
|
inedges[node1.i] = node0.t;
|
|
outedges[node0.i] = node1.t;
|
|
} while (node1 !== hull);
|
|
}
|
|
voronoi(bounds) {
|
|
return new Voronoi(this, bounds);
|
|
}
|
|
*neighbors(i) {
|
|
const {inedges, outedges, halfedges, triangles} = this;
|
|
const e0 = inedges[i];
|
|
if (e0 === -1) return; // coincident point
|
|
let e = e0;
|
|
do {
|
|
yield triangles[e];
|
|
e = e % 3 === 2 ? e - 2 : e + 1;
|
|
if (triangles[e] !== i) return; // bad triangulation
|
|
e = halfedges[e];
|
|
if (e === -1) return yield triangles[outedges[i]];
|
|
} while (e !== e0);
|
|
}
|
|
find(x, y, i = 0) {
|
|
if ((x = +x, x !== x) || (y = +y, y !== y)) return -1;
|
|
let c;
|
|
while ((c = this._step(i, x, y)) >= 0 && c !== i) i = c;
|
|
return c;
|
|
}
|
|
_step(i, x, y) {
|
|
const {inedges, points} = this;
|
|
if (inedges[i] === -1) return -1; // coincident point
|
|
let c = i;
|
|
let dc = (x - points[i * 2]) ** 2 + (y - points[i * 2 + 1]) ** 2;
|
|
for (const t of this.neighbors(i)) {
|
|
const dt = (x - points[t * 2]) ** 2 + (y - points[t * 2 + 1]) ** 2;
|
|
if (dt < dc) dc = dt, c = t;
|
|
}
|
|
return c;
|
|
}
|
|
render(context) {
|
|
const buffer = context == null ? context = new Path : undefined;
|
|
const {points, halfedges, triangles} = this;
|
|
for (let i = 0, n = halfedges.length; i < n; ++i) {
|
|
const j = halfedges[i];
|
|
if (j < i) continue;
|
|
const ti = triangles[i] * 2;
|
|
const tj = triangles[j] * 2;
|
|
context.moveTo(points[ti], points[ti + 1]);
|
|
context.lineTo(points[tj], points[tj + 1]);
|
|
}
|
|
this.renderHull(context);
|
|
return buffer && buffer.value();
|
|
}
|
|
renderPoints(context, r = 2) {
|
|
const buffer = context == null ? context = new Path : undefined;
|
|
const {points} = this;
|
|
for (let i = 0, n = points.length; i < n; i += 2) {
|
|
const x = points[i], y = points[i + 1];
|
|
context.moveTo(x + r, y);
|
|
context.arc(x, y, r, 0, tau);
|
|
}
|
|
return buffer && buffer.value();
|
|
}
|
|
renderHull(context) {
|
|
const buffer = context == null ? context = new Path : undefined;
|
|
const {hull} = this;
|
|
let node = hull;
|
|
context.moveTo(node.x, node.y);
|
|
while (node = node.next, node !== hull) context.lineTo(node.x, node.y);
|
|
context.closePath();
|
|
return buffer && buffer.value();
|
|
}
|
|
hullPolygon() {
|
|
const polygon = new Polygon;
|
|
this.renderHull(polygon);
|
|
return polygon.value();
|
|
}
|
|
renderTriangle(i, context) {
|
|
const buffer = context == null ? context = new Path : undefined;
|
|
const {points, triangles} = this;
|
|
const t0 = triangles[i *= 3] * 2;
|
|
const t1 = triangles[i + 1] * 2;
|
|
const t2 = triangles[i + 2] * 2;
|
|
context.moveTo(points[t0], points[t0 + 1]);
|
|
context.lineTo(points[t1], points[t1 + 1]);
|
|
context.lineTo(points[t2], points[t2 + 1]);
|
|
context.closePath();
|
|
return buffer && buffer.value();
|
|
}
|
|
*trianglePolygons() {
|
|
const {triangles} = this;
|
|
for (let i = 0, n = triangles.length / 3; i < n; ++i) {
|
|
yield this.trianglePolygon(i);
|
|
}
|
|
}
|
|
trianglePolygon(i) {
|
|
const polygon = new Polygon;
|
|
this.renderTriangle(i, polygon);
|
|
return polygon.value();
|
|
}
|
|
}
|
|
|
|
Delaunay.from = function(points, fx = pointX, fy = pointY, that) {
|
|
return new Delaunay("length" in points
|
|
? flatArray(points, fx, fy, that)
|
|
: Float64Array.from(flatIterable(points, fx, fy, that)));
|
|
};
|
|
|
|
function flatArray(points, fx, fy, that) {
|
|
const n = points.length;
|
|
const array = new Float64Array(n * 2);
|
|
for (let i = 0; i < n; ++i) {
|
|
const p = points[i];
|
|
array[i * 2] = fx.call(that, p, i, points);
|
|
array[i * 2 + 1] = fy.call(that, p, i, points);
|
|
}
|
|
return array;
|
|
}
|
|
|
|
function* flatIterable(points, fx, fy, that) {
|
|
let i = 0;
|
|
for (const p of points) {
|
|
yield fx.call(that, p, i, points);
|
|
yield fy.call(that, p, i, points);
|
|
++i;
|
|
}
|
|
}
|
|
|
|
exports.Delaunay = Delaunay;
|
|
exports.Voronoi = Voronoi;
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
})));
|