-
Notifications
You must be signed in to change notification settings - Fork 0
/
hull.js
367 lines (306 loc) · 11 KB
/
hull.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.hull = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
function _cross(o, a, b) {
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);
}
function _upperTangent(pointset) {
var lower = [];
for (var l = 0; l < pointset.length; l++) {
while (lower.length >= 2 && (_cross(lower[lower.length - 2], lower[lower.length - 1], pointset[l]) <= 0)) {
lower.pop();
}
lower.push(pointset[l]);
}
lower.pop();
return lower;
}
function _lowerTangent(pointset) {
var reversed = pointset.reverse(),
upper = [];
for (var u = 0; u < reversed.length; u++) {
while (upper.length >= 2 && (_cross(upper[upper.length - 2], upper[upper.length - 1], reversed[u]) <= 0)) {
upper.pop();
}
upper.push(reversed[u]);
}
upper.pop();
return upper;
}
// pointset has to be sorted by X
function convex(pointset) {
var convex,
upper = _upperTangent(pointset),
lower = _lowerTangent(pointset);
convex = lower.concat(upper);
convex.push(pointset[0]);
return convex;
}
module.exports = convex;
},{}],2:[function(require,module,exports){
module.exports = {
toXy: function(pointset, format) {
if (format === undefined) {
return pointset.slice();
}
return pointset.map(function(pt) {
/*jslint evil: true */
var _getXY = new Function('pt', 'return [pt' + format[0] + ',' + 'pt' + format[1] + '];');
return _getXY(pt);
});
},
fromXy: function(pointset, format) {
if (format === undefined) {
return pointset.slice();
}
return pointset.map(function(pt) {
/*jslint evil: true */
var _getObj = new Function('pt', 'var o = {}; o' + format[0] + '= pt[0]; o' + format[1] + '= pt[1]; return o;');
return _getObj(pt);
});
}
}
},{}],3:[function(require,module,exports){
function Grid(points, cellSize) {
this._cells = [];
this._cellSize = cellSize;
points.forEach(function(point) {
var cellXY = this.point2CellXY(point),
x = cellXY[0],
y = cellXY[1];
if (this._cells[x] === undefined) {
this._cells[x] = [];
}
if (this._cells[x][y] === undefined) {
this._cells[x][y] = [];
}
this._cells[x][y].push(point);
}, this);
}
Grid.prototype = {
cellPoints: function(x, y) { // (Number, Number) -> Array
return (this._cells[x] !== undefined && this._cells[x][y] !== undefined) ? this._cells[x][y] : [];
},
rangePoints: function(bbox) { // (Array) -> Array
var tlCellXY = this.point2CellXY([bbox[0], bbox[1]]),
brCellXY = this.point2CellXY([bbox[2], bbox[3]]),
points = [];
for (var x = tlCellXY[0]; x <= brCellXY[0]; x++) {
for (var y = tlCellXY[1]; y <= brCellXY[1]; y++) {
points = points.concat(this.cellPoints(x, y));
}
}
return points;
},
removePoint: function(point) { // (Array) -> Array
var cellXY = this.point2CellXY(point),
cell = this._cells[cellXY[0]][cellXY[1]],
pointIdxInCell;
for (var i = 0; i < cell.length; i++) {
if (cell[i][0] === point[0] && cell[i][1] === point[1]) {
pointIdxInCell = i;
break;
}
}
cell.splice(pointIdxInCell, 1);
return cell;
},
point2CellXY: function(point) { // (Array) -> Array
var x = parseInt(point[0] / this._cellSize),
y = parseInt(point[1] / this._cellSize);
return [x, y];
},
extendBbox: function(bbox, scaleFactor) { // (Array, Number) -> Array
return [
bbox[0] - (scaleFactor * this._cellSize),
bbox[1] - (scaleFactor * this._cellSize),
bbox[2] + (scaleFactor * this._cellSize),
bbox[3] + (scaleFactor * this._cellSize)
];
}
};
function grid(points, cellSize) {
return new Grid(points, cellSize);
}
module.exports = grid;
},{}],4:[function(require,module,exports){
/*
(c) 2014-2016, Andrii Heonia
Hull.js, a JavaScript library for concave hull generation by set of points.
https://github.com/AndriiHeonia/hull
*/
'use strict';
var intersect = require('./intersect.js');
var grid = require('./grid.js');
var formatUtil = require('./format.js');
var convexHull = require('./convex.js');
function _filterDuplicates(pointset) {
return pointset.filter(function(el, idx, arr) {
var prevEl = arr[idx - 1];
return idx === 0 || !(prevEl[0] === el[0] && prevEl[1] === el[1]);
});
}
function _sortByX(pointset) {
return pointset.sort(function(a, b) {
if (a[0] == b[0]) {
return a[1] - b[1];
} else {
return a[0] - b[0];
}
});
}
function _sqLength(a, b) {
return Math.pow(b[0] - a[0], 2) + Math.pow(b[1] - a[1], 2);
}
function _cos(o, a, b) {
var aShifted = [a[0] - o[0], a[1] - o[1]],
bShifted = [b[0] - o[0], b[1] - o[1]],
sqALen = _sqLength(o, a),
sqBLen = _sqLength(o, b),
dot = aShifted[0] * bShifted[0] + aShifted[1] * bShifted[1];
return dot / Math.sqrt(sqALen * sqBLen);
}
function _intersect(segment, pointset) {
for (var i = 0; i < pointset.length - 1; i++) {
var seg = [pointset[i], pointset[i + 1]];
if (segment[0][0] === seg[0][0] && segment[0][1] === seg[0][1] ||
segment[0][0] === seg[1][0] && segment[0][1] === seg[1][1]) {
continue;
}
if (intersect(segment, seg)) {
return true;
}
}
return false;
}
function _occupiedArea(pointset) {
var minX = Infinity,
minY = Infinity,
maxX = -Infinity,
maxY = -Infinity;
for (var i = pointset.length - 1; i >= 0; i--) {
if (pointset[i][0] < minX) {
minX = pointset[i][0];
}
if (pointset[i][1] < minY) {
minY = pointset[i][1];
}
if (pointset[i][0] > maxX) {
maxX = pointset[i][0];
}
if (pointset[i][1] > maxY) {
maxY = pointset[i][1];
}
}
return [
maxX - minX, // width
maxY - minY // height
];
}
function _bBoxAround(edge) {
return [
Math.min(edge[0][0], edge[1][0]), // left
Math.min(edge[0][1], edge[1][1]), // top
Math.max(edge[0][0], edge[1][0]), // right
Math.max(edge[0][1], edge[1][1]) // bottom
];
}
function _midPoint(edge, innerPoints, convex) {
var point = null,
angle1Cos = MAX_CONCAVE_ANGLE_COS,
angle2Cos = MAX_CONCAVE_ANGLE_COS,
a1Cos, a2Cos;
for (var i = 0; i < innerPoints.length; i++) {
a1Cos = _cos(edge[0], edge[1], innerPoints[i]);
a2Cos = _cos(edge[1], edge[0], innerPoints[i]);
if (a1Cos > angle1Cos && a2Cos > angle2Cos &&
!_intersect([edge[0], innerPoints[i]], convex) &&
!_intersect([edge[1], innerPoints[i]], convex)) {
angle1Cos = a1Cos;
angle2Cos = a2Cos;
point = innerPoints[i];
}
}
return point;
}
function _concave(convex, maxSqEdgeLen, maxSearchArea, grid, edgeSkipList) {
var edge,
keyInSkipList,
scaleFactor,
midPoint,
bBoxAround,
bBoxWidth,
bBoxHeight,
midPointInserted = false;
for (var i = 0; i < convex.length - 1; i++) {
edge = [convex[i], convex[i + 1]];
keyInSkipList = edge[0].join() + ',' + edge[1].join();
if (_sqLength(edge[0], edge[1]) < maxSqEdgeLen ||
edgeSkipList[keyInSkipList] === true) { continue; }
scaleFactor = 0;
bBoxAround = _bBoxAround(edge);
do {
bBoxAround = grid.extendBbox(bBoxAround, scaleFactor);
bBoxWidth = bBoxAround[2] - bBoxAround[0];
bBoxHeight = bBoxAround[3] - bBoxAround[1];
midPoint = _midPoint(edge, grid.rangePoints(bBoxAround), convex);
scaleFactor++;
} while (midPoint === null && (maxSearchArea[0] > bBoxWidth || maxSearchArea[1] > bBoxHeight));
if (bBoxWidth >= maxSearchArea[0] && bBoxHeight >= maxSearchArea[1]) {
edgeSkipList[keyInSkipList] = true;
}
if (midPoint !== null) {
convex.splice(i + 1, 0, midPoint);
grid.removePoint(midPoint);
midPointInserted = true;
}
}
if (midPointInserted) {
return _concave(convex, maxSqEdgeLen, maxSearchArea, grid, edgeSkipList);
}
return convex;
}
function hull(pointset, concavity, format) {
var convex,
concave,
innerPoints,
occupiedArea,
maxSearchArea,
cellSize,
points,
maxEdgeLen = concavity || 20;
points = _filterDuplicates(_sortByX(formatUtil.toXy(pointset, format)));
if (points.length < 4) {
return points.concat([points[0]]);
}
occupiedArea = _occupiedArea(points);
maxSearchArea = [
occupiedArea[0] * MAX_SEARCH_BBOX_SIZE_PERCENT,
occupiedArea[1] * MAX_SEARCH_BBOX_SIZE_PERCENT
];
convex = convexHull(points);
innerPoints = points.filter(function(pt) {
return convex.indexOf(pt) < 0;
});
cellSize = Math.ceil(1 / (points.length / (occupiedArea[0] * occupiedArea[1])));
concave = _concave(
convex, Math.pow(maxEdgeLen, 2),
maxSearchArea, grid(innerPoints, cellSize), {});
return formatUtil.fromXy(concave, format);
}
var MAX_CONCAVE_ANGLE_COS = Math.cos(90 / (180 / Math.PI)); // angle = 90 deg
var MAX_SEARCH_BBOX_SIZE_PERCENT = 0.6;
module.exports = hull;
},{"./convex.js":1,"./format.js":2,"./grid.js":3,"./intersect.js":5}],5:[function(require,module,exports){
function ccw(x1, y1, x2, y2, x3, y3) {
var cw = ((y3 - y1) * (x2 - x1)) - ((y2 - y1) * (x3 - x1));
return cw > 0 ? true : cw < 0 ? false : true; // colinear
}
function intersect(seg1, seg2) {
var x1 = seg1[0][0], y1 = seg1[0][1],
x2 = seg1[1][0], y2 = seg1[1][1],
x3 = seg2[0][0], y3 = seg2[0][1],
x4 = seg2[1][0], y4 = seg2[1][1];
return ccw(x1, y1, x3, y3, x4, y4) !== ccw(x2, y2, x3, y3, x4, y4) && ccw(x1, y1, x2, y2, x3, y3) !== ccw(x1, y1, x2, y2, x4, y4);
}
module.exports = intersect;
},{}]},{},[4])(4)
});