-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdwindle.js
199 lines (149 loc) · 4.6 KB
/
dwindle.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var nanoq = require('nanoq');
var err = "Expected: array of numerical coordinate arrays, e.g. [[0,1],[1,1],[1,0]]";
module.exports = Dwindle;
function Dwindle(points)
{
// Basic type and order checks
if (!Array.isArray(points))
throw new Error(err);
if (points.length == 0) return;
let p = points[0];
if (!Array.isArray(p))
throw new Error(err);
if (p.length != 2)
throw new Error(err);
if (isNaN(p[0]))
throw new Error(err);
// Seems ok: go johnny go-go go go
this.rebuild(points);
}
Dwindle.prototype = {
simplify: function(options)
{
// Select points by significance (area)
// Easiest to implement, but least useful to the user
if (options.area)
{
return this.points.filter((p,i) => {
// always return the first and last points
if (i < this.minpoint || i > this.maxpoint) return true;
// otherwise return this point if deemed significant
return this.meta[i].area >= options.area
});
}
// Select specific target point count
// More practical/useful
if (options.target || options.percent)
{
let pc = Math.max(Math.min(options.percent, 100),0);
let count = pc ? ((pc / 100) * this.points.length) >> 0 : options.target;
count = Math.min(this.maxpoint+1, count);
count = Math.max(count, 2);
let max = new nanoq(null, (a, b) => this.meta[a].area < this.meta[b].area );
let min = new nanoq();
// My somewhat clumsy implementation involves ordering points by area
// significance, selecting the top N, then reordering the list
// by winding order (index/id) so they render correctly. Not ideal.
// Order all points by area/significance
for (let i=this.minpoint; i<=this.maxpoint; i++)
max.push(i);
let out = [], c = count;
// Output must include endpoints
out.push(this.points[0]);
out.push(this.points[this.maxpoint+1]);
c = count-2;
// Order points by index
while (c--) min.push(max.pop());
c = count-2;
// Create subset array
while (c--) out.push(this.points[min.pop()]);
return out;
}
},
rebuild: function(points)
{
// Descriptor array for every point in `this.points[]`
this.meta = [];
// Store a reference to the user's point data
this.points = points;
// minheap to order the simplification
this.q = new nanoq(null, (a, b) => this.meta[a].area > this.meta[b].area );
this.minpoint = 1;
this.maxpoint = this.points.length-2;
this.minarea = 0;
this.maxarea = 0;
this.init();
this.dwindle();
},
// Called only once to calculate areas
dwindle: function()
{
let p;
this.minarea = Number.MAX_VALUE;
this.maxarea = -1;
//Process all points in order of area significance
while((p = this.q.pop()))
{
let prev = this.meta[p].prev;
let next = this.meta[p].next;
if (this.meta[p].area > 0)
this.minarea = Math.min(this.minarea, this.meta[p].area);
this.maxarea = Math.max(this.maxarea, this.meta[p].area);
// join the dots left orphaned by p's removal
// and recalc neighbours' areas
if (prev >= this.minpoint)
{
this.meta[prev].next = next;
this.meta[prev].area = this.recalc(prev);
}
if (next <= this.maxpoint)
{
this.meta[next].prev = prev;
this.meta[next].area = this.recalc(next);
}
}
},
recalc: function(p) // p = index into this.points[]
{
let prev = this.meta[p].prev;
let next = this.meta[p].next;
let a = this.area([this.points[prev], this.points[p], this.points[next]]);
return isNaN(a)? 0 : a;
},
// For each point given;
// - create a next/prev pointer
// - calculate the area of the point with its neighbours
// - add the point to our minheap (which uses its area)
init: function()
{
for (let i=1; i<this.points.length-1; i++)
{
let p = this.points[i];
let t = {
prev: i-1,
next: i+1,
area: this.area([ this.points[i-1], p, this.points[i+1] ])
};
// Math.sqrt() returns NaN for unrepresentable small numbers
if (isNaN(t.area)) t.area = 0;
this.meta[i] = t;
this.q.push(i);
}
},
area: function(tri)
{
let a = this.dist(tri[0], tri[1]);
let b = this.dist(tri[1], tri[2]);
let c = this.dist(tri[2], tri[0]);
let s = (a + b + c) / 2;
return Math.sqrt(
s * (s - a) * (s - b) * (s - c)
);
},
dist: function(a, b)
{
var xdiff = b[0] - a[0];
var ydiff = b[1] - a[1];
return Math.sqrt( xdiff*xdiff + ydiff*ydiff );
}
}