-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpresimplify.js
79 lines (64 loc) · 1.94 KB
/
presimplify.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
import {transform} from "topojson-client";
import newHeap from "./heap.js";
import {planarTriangleArea} from "./planar.js";
function copy(point) {
return [point[0], point[1], 0];
}
export default function(topology, weight) {
var point = topology.transform ? transform(topology.transform) : copy,
heap = newHeap();
if (weight == null) weight = planarTriangleArea;
var arcs = topology.arcs.map(function(arc) {
var triangles = [],
maxWeight = 0,
triangle,
i,
n;
arc = arc.map(point);
for (i = 1, n = arc.length - 1; i < n; ++i) {
triangle = [arc[i - 1], arc[i], arc[i + 1]];
triangle[1][2] = weight(triangle);
triangles.push(triangle);
heap.push(triangle);
}
// Always keep the arc endpoints!
arc[0][2] = arc[n][2] = Infinity;
for (i = 0, n = triangles.length; i < n; ++i) {
triangle = triangles[i];
triangle.previous = triangles[i - 1];
triangle.next = triangles[i + 1];
}
while (triangle = heap.pop()) {
var previous = triangle.previous,
next = triangle.next;
// If the weight of the current point is less than that of the previous
// point to be eliminated, use the latter’s weight instead. This ensures
// that the current point cannot be eliminated without eliminating
// previously- eliminated points.
if (triangle[1][2] < maxWeight) triangle[1][2] = maxWeight;
else maxWeight = triangle[1][2];
if (previous) {
previous.next = next;
previous[2] = triangle[2];
update(previous);
}
if (next) {
next.previous = previous;
next[0] = triangle[0];
update(next);
}
}
return arc;
});
function update(triangle) {
heap.remove(triangle);
triangle[1][2] = weight(triangle);
heap.push(triangle);
}
return {
type: "Topology",
bbox: topology.bbox,
objects: topology.objects,
arcs: arcs
};
}