-
Notifications
You must be signed in to change notification settings - Fork 150
/
polylabel.js
156 lines (125 loc) · 4.48 KB
/
polylabel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import Queue from 'tinyqueue';
export default function polylabel(polygon, precision = 1.0, debug = false) {
// find the bounding box of the outer ring
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const [x, y] of polygon[0]) {
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
}
const width = maxX - minX;
const height = maxY - minY;
const cellSize = Math.max(precision, Math.min(width, height));
if (cellSize === precision) {
const result = [minX, minY];
result.distance = 0;
return result;
}
// a priority queue of cells in order of their "potential" (max distance to polygon)
const cellQueue = new Queue([], (a, b) => b.max - a.max);
// take centroid as the first best guess
let bestCell = getCentroidCell(polygon);
// second guess: bounding box centroid
const bboxCell = new Cell(minX + width / 2, minY + height / 2, 0, polygon);
if (bboxCell.d > bestCell.d) bestCell = bboxCell;
let numProbes = 2;
function potentiallyQueue(x, y, h) {
const cell = new Cell(x, y, h, polygon);
numProbes++;
if (cell.max > bestCell.d + precision) cellQueue.push(cell);
// update the best cell if we found a better one
if (cell.d > bestCell.d) {
bestCell = cell;
if (debug) console.log(`found best ${Math.round(1e4 * cell.d) / 1e4} after ${numProbes} probes`);
}
}
// cover polygon with initial cells
let h = cellSize / 2;
for (let x = minX; x < maxX; x += cellSize) {
for (let y = minY; y < maxY; y += cellSize) {
potentiallyQueue(x + h, y + h, h);
}
}
while (cellQueue.length) {
// pick the most promising cell from the queue
const {max, x, y, h: ch} = cellQueue.pop();
// do not drill down further if there's no chance of a better solution
if (max - bestCell.d <= precision) break;
// split the cell into four cells
h = ch / 2;
potentiallyQueue(x - h, y - h, h);
potentiallyQueue(x + h, y - h, h);
potentiallyQueue(x - h, y + h, h);
potentiallyQueue(x + h, y + h, h);
}
if (debug) {
console.log(`num probes: ${numProbes}\nbest distance: ${bestCell.d}`);
}
const result = [bestCell.x, bestCell.y];
result.distance = bestCell.d;
return result;
}
function Cell(x, y, h, polygon) {
this.x = x; // cell center x
this.y = y; // cell center y
this.h = h; // half the cell size
this.d = pointToPolygonDist(x, y, polygon); // distance from cell center to polygon
this.max = this.d + this.h * Math.SQRT2; // max distance to polygon within a cell
}
// signed distance from point to polygon outline (negative if point is outside)
function pointToPolygonDist(x, y, polygon) {
let inside = false;
let minDistSq = Infinity;
for (const ring of polygon) {
for (let i = 0, len = ring.length, j = len - 1; i < len; j = i++) {
const a = ring[i];
const b = ring[j];
if ((a[1] > y !== b[1] > y) &&
(x < (b[0] - a[0]) * (y - a[1]) / (b[1] - a[1]) + a[0])) inside = !inside;
minDistSq = Math.min(minDistSq, getSegDistSq(x, y, a, b));
}
}
return minDistSq === 0 ? 0 : (inside ? 1 : -1) * Math.sqrt(minDistSq);
}
// get polygon centroid
function getCentroidCell(polygon) {
let area = 0;
let x = 0;
let y = 0;
const points = polygon[0];
for (let i = 0, len = points.length, j = len - 1; i < len; j = i++) {
const a = points[i];
const b = points[j];
const f = a[0] * b[1] - b[0] * a[1];
x += (a[0] + b[0]) * f;
y += (a[1] + b[1]) * f;
area += f * 3;
}
const centroid = new Cell(x / area, y / area, 0, polygon);
if (area === 0 || centroid.d < 0) return new Cell(points[0][0], points[0][1], 0, polygon);
return centroid;
}
// get squared distance from a point to a segment
function getSegDistSq(px, py, a, b) {
let x = a[0];
let y = a[1];
let dx = b[0] - x;
let dy = b[1] - y;
if (dx !== 0 || dy !== 0) {
const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = b[0];
y = b[1];
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = px - x;
dy = py - y;
return dx * dx + dy * dy;
}