@math.gl/polygon 3.5.7 → 3.6.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cut-by-grid.d.ts +21 -0
- package/dist/cut-by-grid.d.ts.map +1 -0
- package/dist/cut-by-grid.js +214 -0
- package/dist/cut-by-mercator-bounds.d.ts +17 -0
- package/dist/cut-by-mercator-bounds.d.ts.map +1 -0
- package/dist/cut-by-mercator-bounds.js +144 -0
- package/dist/{esm/earcut.d.ts → earcut.d.ts} +4 -2
- package/dist/earcut.d.ts.map +1 -0
- package/dist/earcut.js +611 -0
- package/dist/es5/cut-by-grid.js +29 -25
- package/dist/es5/cut-by-grid.js.map +1 -1
- package/dist/es5/cut-by-mercator-bounds.js +24 -19
- package/dist/es5/cut-by-mercator-bounds.js.map +1 -1
- package/dist/es5/earcut.js +11 -10
- package/dist/es5/earcut.js.map +1 -1
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/lineclip.js +20 -16
- package/dist/es5/lineclip.js.map +1 -1
- package/dist/es5/polygon-utils.js +6 -4
- package/dist/es5/polygon-utils.js.map +1 -1
- package/dist/es5/polygon.js +5 -0
- package/dist/es5/polygon.js.map +1 -1
- package/dist/es5/utils.js.map +1 -1
- package/dist/esm/cut-by-grid.js +5 -5
- package/dist/esm/cut-by-grid.js.map +1 -1
- package/dist/esm/cut-by-mercator-bounds.js +5 -5
- package/dist/esm/cut-by-mercator-bounds.js.map +1 -1
- package/dist/esm/earcut.js +9 -10
- package/dist/esm/earcut.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lineclip.js +5 -5
- package/dist/esm/lineclip.js.map +1 -1
- package/dist/esm/polygon-utils.js +7 -4
- package/dist/esm/polygon-utils.js.map +1 -1
- package/dist/esm/polygon.js +7 -0
- package/dist/esm/polygon.js.map +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/lineclip.d.ts +30 -0
- package/dist/lineclip.d.ts.map +1 -0
- package/dist/lineclip.js +178 -0
- package/dist/{es5/polygon-utils.d.ts → polygon-utils.d.ts} +28 -64
- package/dist/polygon-utils.d.ts.map +1 -0
- package/dist/polygon-utils.js +140 -0
- package/dist/polygon.d.ts +41 -0
- package/dist/polygon.d.ts.map +1 -0
- package/dist/polygon.js +67 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +34 -0
- package/package.json +4 -3
- package/src/{cut-by-grid.js → cut-by-grid.ts} +79 -33
- package/src/{cut-by-mercator-bounds.js → cut-by-mercator-bounds.ts} +51 -22
- package/src/{earcut.js → earcut.ts} +26 -14
- package/src/{index.js → index.ts} +0 -0
- package/src/{lineclip.js → lineclip.ts} +61 -33
- package/src/polygon-utils.ts +228 -0
- package/src/polygon.ts +98 -0
- package/src/{utils.js → utils.ts} +11 -3
- package/dist/es5/cut-by-grid.d.ts +0 -24
- package/dist/es5/cut-by-mercator-bounds.d.ts +0 -23
- package/dist/es5/earcut.d.ts +0 -9
- package/dist/es5/lineclip.d.ts +0 -25
- package/dist/es5/polygon.d.ts +0 -36
- package/dist/esm/cut-by-grid.d.ts +0 -24
- package/dist/esm/cut-by-mercator-bounds.d.ts +0 -23
- package/dist/esm/lineclip.d.ts +0 -25
- package/dist/esm/polygon-utils.d.ts +0 -117
- package/dist/esm/polygon.d.ts +0 -36
- package/src/cut-by-grid.d.ts +0 -24
- package/src/cut-by-mercator-bounds.d.ts +0 -23
- package/src/earcut.d.ts +0 -9
- package/src/lineclip.d.ts +0 -25
- package/src/polygon-utils.d.ts +0 -117
- package/src/polygon-utils.js +0 -127
- package/src/polygon.d.ts +0 -36
- package/src/polygon.js +0 -66
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { NumericArray } from '@math.gl/core';
|
|
2
|
+
export declare type Polygon = {
|
|
3
|
+
positions: number[];
|
|
4
|
+
holeIndices?: number[];
|
|
5
|
+
edgeTypes?: number[];
|
|
6
|
+
};
|
|
7
|
+
export declare function cutPolylineByGrid(positions: NumericArray, options?: {
|
|
8
|
+
size?: number;
|
|
9
|
+
broken?: boolean;
|
|
10
|
+
gridResolution?: number;
|
|
11
|
+
gridOffset?: [number, number];
|
|
12
|
+
startIndex?: number;
|
|
13
|
+
endIndex?: number;
|
|
14
|
+
}): number[] | number[][];
|
|
15
|
+
export declare function cutPolygonByGrid(positions: number[], holeIndices?: number[] | null, options?: {
|
|
16
|
+
size?: number;
|
|
17
|
+
gridResolution?: number;
|
|
18
|
+
gridOffset?: [number, number];
|
|
19
|
+
edgeTypes?: boolean;
|
|
20
|
+
}): Polygon[];
|
|
21
|
+
//# sourceMappingURL=cut-by-grid.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cut-by-grid.d.ts","sourceRoot":"","sources":["../src/cut-by-grid.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,eAAe,CAAC;AAEhD,oBAAY,OAAO,GAAG;IACpB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,YAAY,EACvB,OAAO,CAAC,EAAE;IACR,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,GACA,MAAM,EAAE,GAAG,MAAM,EAAE,EAAE,CAkDvB;AAYD,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,MAAM,EAAE,EACnB,WAAW,GAAE,MAAM,EAAE,GAAG,IAAW,EACnC,OAAO,CAAC,EAAE;IACR,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9B,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,GACA,OAAO,EAAE,CAmEX"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/* eslint-disable max-statements, max-depth, complexity, no-unused-expressions */
|
|
2
|
+
import { bitCode, intersect } from './lineclip';
|
|
3
|
+
import { getPointAtIndex, copy, push } from './utils';
|
|
4
|
+
export function cutPolylineByGrid(positions, options) {
|
|
5
|
+
const { size = 2, broken = false, gridResolution = 10, gridOffset = [0, 0], startIndex = 0, endIndex = positions.length } = options || {};
|
|
6
|
+
const numPoints = (endIndex - startIndex) / size;
|
|
7
|
+
let part = [];
|
|
8
|
+
const result = [part];
|
|
9
|
+
const a = getPointAtIndex(positions, 0, size, startIndex);
|
|
10
|
+
let b;
|
|
11
|
+
let codeB;
|
|
12
|
+
const cell = getGridCell(a, gridResolution, gridOffset, []);
|
|
13
|
+
const scratchPoint = [];
|
|
14
|
+
push(part, a);
|
|
15
|
+
for (let i = 1; i < numPoints; i++) {
|
|
16
|
+
b = getPointAtIndex(positions, i, size, startIndex, b);
|
|
17
|
+
codeB = bitCode(b, cell);
|
|
18
|
+
while (codeB) {
|
|
19
|
+
// find the intersection with the current cell
|
|
20
|
+
intersect(a, b, codeB, cell, scratchPoint);
|
|
21
|
+
const codeAlt = bitCode(scratchPoint, cell);
|
|
22
|
+
if (codeAlt) {
|
|
23
|
+
intersect(a, scratchPoint, codeAlt, cell, scratchPoint);
|
|
24
|
+
codeB = codeAlt;
|
|
25
|
+
}
|
|
26
|
+
push(part, scratchPoint);
|
|
27
|
+
// move to the next cell
|
|
28
|
+
copy(a, scratchPoint);
|
|
29
|
+
moveToNeighborCell(cell, gridResolution, codeB);
|
|
30
|
+
if (broken && part.length > size) {
|
|
31
|
+
part = [];
|
|
32
|
+
result.push(part);
|
|
33
|
+
push(part, a);
|
|
34
|
+
}
|
|
35
|
+
codeB = bitCode(b, cell);
|
|
36
|
+
}
|
|
37
|
+
push(part, b);
|
|
38
|
+
copy(a, b);
|
|
39
|
+
}
|
|
40
|
+
return broken ? result : result[0];
|
|
41
|
+
}
|
|
42
|
+
const TYPE_INSIDE = 0;
|
|
43
|
+
const TYPE_BORDER = 1;
|
|
44
|
+
function concatInPlace(arr1, arr2) {
|
|
45
|
+
for (let i = 0; i < arr2.length; i++) {
|
|
46
|
+
arr1.push(arr2[i]);
|
|
47
|
+
}
|
|
48
|
+
return arr1;
|
|
49
|
+
}
|
|
50
|
+
export function cutPolygonByGrid(positions, holeIndices = null, options) {
|
|
51
|
+
if (!positions.length) {
|
|
52
|
+
// input is empty
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
const { size = 2, gridResolution = 10, gridOffset = [0, 0], edgeTypes = false } = options || {};
|
|
56
|
+
const result = [];
|
|
57
|
+
const queue = [
|
|
58
|
+
{
|
|
59
|
+
pos: positions,
|
|
60
|
+
types: edgeTypes ? new Array(positions.length / size).fill(TYPE_BORDER) : null,
|
|
61
|
+
holes: holeIndices || []
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
const bbox = [[], []];
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
let cell = [];
|
|
67
|
+
// Recursively bisect polygon until every part fit in a single grid cell
|
|
68
|
+
while (queue.length) {
|
|
69
|
+
const { pos, types, holes } = queue.shift();
|
|
70
|
+
// Get the bounding box of the outer polygon
|
|
71
|
+
getBoundingBox(pos, size, holes[0] || pos.length, bbox);
|
|
72
|
+
cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
|
|
73
|
+
const code = bitCode(bbox[1], cell);
|
|
74
|
+
if (code) {
|
|
75
|
+
// Split the outer ring at the boundary
|
|
76
|
+
let parts = bisectPolygon(pos, types, size, 0, holes[0] || pos.length, cell, code);
|
|
77
|
+
const polygonLow = { pos: parts[0].pos, types: parts[0].types, holes: [] };
|
|
78
|
+
const polygonHigh = { pos: parts[1].pos, types: parts[1].types, holes: [] };
|
|
79
|
+
queue.push(polygonLow, polygonHigh);
|
|
80
|
+
// Split each hole at the boundary
|
|
81
|
+
for (let i = 0; i < holes.length; i++) {
|
|
82
|
+
parts = bisectPolygon(pos, types, size, holes[i], holes[i + 1] || pos.length, cell, code);
|
|
83
|
+
if (parts[0]) {
|
|
84
|
+
polygonLow.holes.push(polygonLow.pos.length);
|
|
85
|
+
polygonLow.pos = concatInPlace(polygonLow.pos, parts[0].pos);
|
|
86
|
+
if (edgeTypes) {
|
|
87
|
+
polygonLow.types = concatInPlace(polygonLow.types, parts[0].types);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (parts[1]) {
|
|
91
|
+
polygonHigh.holes.push(polygonHigh.pos.length);
|
|
92
|
+
polygonHigh.pos = concatInPlace(polygonHigh.pos, parts[1].pos);
|
|
93
|
+
if (edgeTypes) {
|
|
94
|
+
polygonHigh.types = concatInPlace(polygonHigh.types, parts[1].types);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// Polygon fits in a single cell, no more processing required
|
|
101
|
+
const polygon = { positions: pos };
|
|
102
|
+
if (edgeTypes) {
|
|
103
|
+
polygon.edgeTypes = types;
|
|
104
|
+
}
|
|
105
|
+
if (holes.length) {
|
|
106
|
+
polygon.holeIndices = holes;
|
|
107
|
+
}
|
|
108
|
+
result.push(polygon);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
// edgeTypes:
|
|
114
|
+
// TYPE_BORDER - edge from the original polygon
|
|
115
|
+
// TYPE_INSIDE - inside the original polygon
|
|
116
|
+
// eslint-disable-next-line max-params
|
|
117
|
+
function bisectPolygon(positions, edgeTypes, size, startIndex, endIndex, bbox, edge) {
|
|
118
|
+
const numPoints = (endIndex - startIndex) / size;
|
|
119
|
+
const resultLow = [];
|
|
120
|
+
const resultHigh = [];
|
|
121
|
+
const typesLow = [];
|
|
122
|
+
const typesHigh = [];
|
|
123
|
+
const scratchPoint = [];
|
|
124
|
+
let p;
|
|
125
|
+
let side;
|
|
126
|
+
let type;
|
|
127
|
+
const prev = getPointAtIndex(positions, numPoints - 1, size, startIndex);
|
|
128
|
+
let prevSide = Math.sign(edge & 8 ? prev[1] - bbox[3] : prev[0] - bbox[2]);
|
|
129
|
+
let prevType = edgeTypes && edgeTypes[numPoints - 1];
|
|
130
|
+
let lowPointCount = 0;
|
|
131
|
+
let highPointCount = 0;
|
|
132
|
+
for (let i = 0; i < numPoints; i++) {
|
|
133
|
+
p = getPointAtIndex(positions, i, size, startIndex, p);
|
|
134
|
+
side = Math.sign(edge & 8 ? p[1] - bbox[3] : p[0] - bbox[2]);
|
|
135
|
+
type = edgeTypes && edgeTypes[startIndex / size + i];
|
|
136
|
+
// if segment goes through the boundary, add an intersection
|
|
137
|
+
if (side && prevSide && prevSide !== side) {
|
|
138
|
+
intersect(prev, p, edge, bbox, scratchPoint);
|
|
139
|
+
push(resultLow, scratchPoint) && typesLow.push(prevType);
|
|
140
|
+
push(resultHigh, scratchPoint) && typesHigh.push(prevType);
|
|
141
|
+
}
|
|
142
|
+
if (side <= 0) {
|
|
143
|
+
push(resultLow, p) && typesLow.push(type);
|
|
144
|
+
lowPointCount -= side;
|
|
145
|
+
}
|
|
146
|
+
else if (typesLow.length) {
|
|
147
|
+
typesLow[typesLow.length - 1] = TYPE_INSIDE;
|
|
148
|
+
}
|
|
149
|
+
if (side >= 0) {
|
|
150
|
+
push(resultHigh, p) && typesHigh.push(type);
|
|
151
|
+
highPointCount += side;
|
|
152
|
+
}
|
|
153
|
+
else if (typesHigh.length) {
|
|
154
|
+
typesHigh[typesHigh.length - 1] = TYPE_INSIDE;
|
|
155
|
+
}
|
|
156
|
+
copy(prev, p);
|
|
157
|
+
prevSide = side;
|
|
158
|
+
prevType = type;
|
|
159
|
+
}
|
|
160
|
+
return [
|
|
161
|
+
lowPointCount ? { pos: resultLow, types: edgeTypes && typesLow } : null,
|
|
162
|
+
highPointCount ? { pos: resultHigh, types: edgeTypes && typesHigh } : null
|
|
163
|
+
];
|
|
164
|
+
}
|
|
165
|
+
function getGridCell(p, gridResolution, gridOffset, out) {
|
|
166
|
+
const left = Math.floor((p[0] - gridOffset[0]) / gridResolution) * gridResolution + gridOffset[0];
|
|
167
|
+
const bottom = Math.floor((p[1] - gridOffset[1]) / gridResolution) * gridResolution + gridOffset[1];
|
|
168
|
+
out[0] = left;
|
|
169
|
+
out[1] = bottom;
|
|
170
|
+
out[2] = left + gridResolution;
|
|
171
|
+
out[3] = bottom + gridResolution;
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
function moveToNeighborCell(cell, gridResolution, edge) {
|
|
175
|
+
if (edge & 8) {
|
|
176
|
+
// top
|
|
177
|
+
cell[1] += gridResolution;
|
|
178
|
+
cell[3] += gridResolution;
|
|
179
|
+
}
|
|
180
|
+
else if (edge & 4) {
|
|
181
|
+
// bottom
|
|
182
|
+
cell[1] -= gridResolution;
|
|
183
|
+
cell[3] -= gridResolution;
|
|
184
|
+
}
|
|
185
|
+
else if (edge & 2) {
|
|
186
|
+
// right
|
|
187
|
+
cell[0] += gridResolution;
|
|
188
|
+
cell[2] += gridResolution;
|
|
189
|
+
}
|
|
190
|
+
else if (edge & 1) {
|
|
191
|
+
// left
|
|
192
|
+
cell[0] -= gridResolution;
|
|
193
|
+
cell[2] -= gridResolution;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function getBoundingBox(positions, size, endIndex, out) {
|
|
197
|
+
let minX = Infinity;
|
|
198
|
+
let maxX = -Infinity;
|
|
199
|
+
let minY = Infinity;
|
|
200
|
+
let maxY = -Infinity;
|
|
201
|
+
for (let i = 0; i < endIndex; i += size) {
|
|
202
|
+
const x = positions[i];
|
|
203
|
+
const y = positions[i + 1];
|
|
204
|
+
minX = x < minX ? x : minX;
|
|
205
|
+
maxX = x > maxX ? x : maxX;
|
|
206
|
+
minY = y < minY ? y : minY;
|
|
207
|
+
maxY = y > maxY ? y : maxY;
|
|
208
|
+
}
|
|
209
|
+
out[0][0] = minX;
|
|
210
|
+
out[0][1] = minY;
|
|
211
|
+
out[1][0] = maxX;
|
|
212
|
+
out[1][1] = maxY;
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Polygon } from './cut-by-grid';
|
|
2
|
+
import type { NumericArray } from '@math.gl/core';
|
|
3
|
+
/** https://user-images.githubusercontent.com/2059298/78465769-938b7a00-76ae-11ea-9b95-1f4c26425ab9.png */
|
|
4
|
+
export declare function cutPolylineByMercatorBounds(positions: NumericArray, options?: {
|
|
5
|
+
size?: number;
|
|
6
|
+
startIndex?: number;
|
|
7
|
+
endIndex?: number;
|
|
8
|
+
normalize?: boolean;
|
|
9
|
+
}): number[][];
|
|
10
|
+
/** https://user-images.githubusercontent.com/2059298/78465770-94241080-76ae-11ea-809a-6a8534dac1d9.png */
|
|
11
|
+
export declare function cutPolygonByMercatorBounds(positions: number[], holeIndices?: number[] | null, options?: {
|
|
12
|
+
size?: number;
|
|
13
|
+
normalize?: boolean;
|
|
14
|
+
maxLatitude?: number;
|
|
15
|
+
edgeTypes?: boolean;
|
|
16
|
+
}): Polygon[];
|
|
17
|
+
//# sourceMappingURL=cut-by-mercator-bounds.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cut-by-mercator-bounds.d.ts","sourceRoot":"","sources":["../src/cut-by-mercator-bounds.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,eAAe,CAAC;AAKhD,0GAA0G;AAC1G,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,YAAY,EACvB,OAAO,CAAC,EAAE;IACR,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,GACA,MAAM,EAAE,EAAE,CAsBZ;AAED,0GAA0G;AAC1G,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,MAAM,EAAE,EACnB,WAAW,GAAE,MAAM,EAAE,GAAG,IAAW,EACnC,OAAO,CAAC,EAAE;IACR,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,GACA,OAAO,EAAE,CAmDX"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { cutPolylineByGrid, cutPolygonByGrid } from './cut-by-grid';
|
|
2
|
+
import { getPointAtIndex, push } from './utils';
|
|
3
|
+
// https://en.wikipedia.org/wiki/Web_Mercator_projection
|
|
4
|
+
const DEFAULT_MAX_LATITUDE = 85.051129;
|
|
5
|
+
/** https://user-images.githubusercontent.com/2059298/78465769-938b7a00-76ae-11ea-9b95-1f4c26425ab9.png */
|
|
6
|
+
export function cutPolylineByMercatorBounds(positions, options) {
|
|
7
|
+
const { size = 2, startIndex = 0, endIndex = positions.length, normalize = true } = options || {};
|
|
8
|
+
// Remap longitudes so that each segment takes the shorter path
|
|
9
|
+
const newPositions = positions.slice(startIndex, endIndex);
|
|
10
|
+
wrapLongitudesForShortestPath(newPositions, size, 0, endIndex - startIndex);
|
|
11
|
+
const parts = cutPolylineByGrid(newPositions, {
|
|
12
|
+
size,
|
|
13
|
+
broken: true,
|
|
14
|
+
gridResolution: 360,
|
|
15
|
+
gridOffset: [-180, -180]
|
|
16
|
+
});
|
|
17
|
+
if (normalize) {
|
|
18
|
+
// Each part is guaranteed to be in a single copy of the world
|
|
19
|
+
// Map longitudes back to [-180, 180]
|
|
20
|
+
for (const part of parts) {
|
|
21
|
+
shiftLongitudesIntoRange(part, size);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return parts;
|
|
25
|
+
}
|
|
26
|
+
/** https://user-images.githubusercontent.com/2059298/78465770-94241080-76ae-11ea-809a-6a8534dac1d9.png */
|
|
27
|
+
export function cutPolygonByMercatorBounds(positions, holeIndices = null, options) {
|
|
28
|
+
const { size = 2, normalize = true, edgeTypes = false } = options || {};
|
|
29
|
+
holeIndices = holeIndices || [];
|
|
30
|
+
const newPositions = [];
|
|
31
|
+
const newHoleIndices = [];
|
|
32
|
+
let srcStartIndex = 0;
|
|
33
|
+
let targetIndex = 0;
|
|
34
|
+
for (let ringIndex = 0; ringIndex <= holeIndices.length; ringIndex++) {
|
|
35
|
+
// srcStartIndex/srcEndIndex define the ring in the original positions
|
|
36
|
+
const srcEndIndex = holeIndices[ringIndex] || positions.length;
|
|
37
|
+
// targetStartIndex/targetIndex define the ring in newPositions
|
|
38
|
+
const targetStartIndex = targetIndex;
|
|
39
|
+
// In case the ring contains a pole (e.g. Antarctica), we'll have to insert vertices
|
|
40
|
+
// The insertion point is defined by the vertex closest to the pole
|
|
41
|
+
// Split the the ring by the insertion point when copying to newPositions
|
|
42
|
+
const splitIndex = findSplitIndex(positions, size, srcStartIndex, srcEndIndex);
|
|
43
|
+
for (let i = splitIndex; i < srcEndIndex; i++) {
|
|
44
|
+
newPositions[targetIndex++] = positions[i];
|
|
45
|
+
}
|
|
46
|
+
for (let i = srcStartIndex; i < splitIndex; i++) {
|
|
47
|
+
newPositions[targetIndex++] = positions[i];
|
|
48
|
+
}
|
|
49
|
+
// Remap longitudes so that each segment takes the shorter path
|
|
50
|
+
wrapLongitudesForShortestPath(newPositions, size, targetStartIndex, targetIndex);
|
|
51
|
+
// Handle the case when the ring contains a pole
|
|
52
|
+
insertPoleVertices(newPositions, size, targetStartIndex, targetIndex, options?.maxLatitude);
|
|
53
|
+
srcStartIndex = srcEndIndex;
|
|
54
|
+
newHoleIndices[ringIndex] = targetIndex;
|
|
55
|
+
}
|
|
56
|
+
newHoleIndices.pop();
|
|
57
|
+
const parts = cutPolygonByGrid(newPositions, newHoleIndices, {
|
|
58
|
+
size,
|
|
59
|
+
gridResolution: 360,
|
|
60
|
+
gridOffset: [-180, -180],
|
|
61
|
+
edgeTypes
|
|
62
|
+
});
|
|
63
|
+
if (normalize) {
|
|
64
|
+
// Each part is guaranteed to be in a single copy of the world
|
|
65
|
+
// Map longitudes back to [-180, 180]
|
|
66
|
+
for (const part of parts) {
|
|
67
|
+
shiftLongitudesIntoRange(part.positions, size);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return parts;
|
|
71
|
+
}
|
|
72
|
+
/* Helpers */
|
|
73
|
+
// See comments for insertPoleVertices
|
|
74
|
+
function findSplitIndex(positions, size, startIndex, endIndex) {
|
|
75
|
+
let maxLat = -1;
|
|
76
|
+
let pointIndex = -1;
|
|
77
|
+
for (let i = startIndex + 1; i < endIndex; i += size) {
|
|
78
|
+
const lat = Math.abs(positions[i]);
|
|
79
|
+
if (lat > maxLat) {
|
|
80
|
+
maxLat = lat;
|
|
81
|
+
pointIndex = i - 1;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return pointIndex;
|
|
85
|
+
}
|
|
86
|
+
// https://user-images.githubusercontent.com/2059298/78857483-5987e400-79de-11ea-98fc-0631287a8431.png
|
|
87
|
+
//
|
|
88
|
+
// If the polygon contains a pole, to tesselate it correctly, we need to insert the edge
|
|
89
|
+
// of map into the polygon. This requires adding two vertices that represent the pole, by
|
|
90
|
+
// drawing a perpendicular line to the Mercator map edge from a selected vertex on the ring.
|
|
91
|
+
//
|
|
92
|
+
// We select the insertion position carefully so that the inserted line segments do not
|
|
93
|
+
// intersect with the ring itself. This is ensured by findSplitIndex, which returns the
|
|
94
|
+
// vertex closest to the pole.
|
|
95
|
+
function insertPoleVertices(positions, size, startIndex, endIndex, maxLatitude = DEFAULT_MAX_LATITUDE) {
|
|
96
|
+
// Check if the ring contains a pole
|
|
97
|
+
const firstLng = positions[startIndex];
|
|
98
|
+
const lastLng = positions[endIndex - size];
|
|
99
|
+
if (Math.abs(firstLng - lastLng) > 180) {
|
|
100
|
+
// The ring does not make a round trip
|
|
101
|
+
// Add the nearest pole to the vertices so that the polygon tesselates correctly
|
|
102
|
+
const p = getPointAtIndex(positions, 0, size, startIndex);
|
|
103
|
+
// Copy the first vertex to the world of the last vertex
|
|
104
|
+
p[0] += Math.round((lastLng - firstLng) / 360) * 360;
|
|
105
|
+
push(positions, p);
|
|
106
|
+
// Project the copied vertex to the edge of the map
|
|
107
|
+
p[1] = Math.sign(p[1]) * maxLatitude;
|
|
108
|
+
push(positions, p);
|
|
109
|
+
// Project the first vertex to the edge of the map
|
|
110
|
+
p[0] = firstLng;
|
|
111
|
+
push(positions, p);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function wrapLongitudesForShortestPath(positions, size, startIndex, endIndex) {
|
|
115
|
+
let prevLng = positions[0];
|
|
116
|
+
let lng;
|
|
117
|
+
for (let i = startIndex; i < endIndex; i += size) {
|
|
118
|
+
lng = positions[i];
|
|
119
|
+
const delta = lng - prevLng;
|
|
120
|
+
if (delta > 180 || delta < -180) {
|
|
121
|
+
lng -= Math.round(delta / 360) * 360;
|
|
122
|
+
}
|
|
123
|
+
positions[i] = prevLng = lng;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function shiftLongitudesIntoRange(positions, size) {
|
|
127
|
+
let refLng;
|
|
128
|
+
const pointCount = positions.length / size;
|
|
129
|
+
// Find a longitude that is not on the edge of a world
|
|
130
|
+
// Which we will use to determine which world copy it is
|
|
131
|
+
for (let i = 0; i < pointCount; i++) {
|
|
132
|
+
refLng = positions[i * size];
|
|
133
|
+
if ((refLng + 180) % 360 !== 0) {
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const delta = -Math.round(refLng / 360) * 360;
|
|
138
|
+
if (delta === 0) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
for (let i = 0; i < pointCount; i++) {
|
|
142
|
+
positions[i * size] += delta;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
* Computes a triangulation of a polygon
|
|
3
3
|
* @param positions a flat array of the vertex positions that define the polygon.
|
|
4
4
|
* @param holeIndices an array of hole indices if any (e.g. [5, 8] for a 12-vertex input would mean one hole with vertices 5–7 and another with 8–11).
|
|
5
|
-
* @param
|
|
5
|
+
* @param dim the number of elements in each vertex. Size `2` will interpret `positions` as `[x0, y0, x1, y1, ...]` and size `3` will interpret `positions` as `[x0, y0, z0, x1, y1, z1, ...]`. Default `2`.
|
|
6
6
|
* @param areas areas of outer polygon and holes as computed by `getPolygonSignedArea()`. Can be optionally supplied to speed up triangulation
|
|
7
7
|
* @returns array of indices into the `positions` array that describes the triangulation of the polygon
|
|
8
|
+
* Adapted from https://github.com/mapbox/earcut
|
|
8
9
|
*/
|
|
9
|
-
export function earcut(positions: number[], holeIndices?: number[],
|
|
10
|
+
export declare function earcut(positions: number[], holeIndices?: number[], dim?: number, areas?: number[]): number[];
|
|
11
|
+
//# sourceMappingURL=earcut.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"earcut.d.ts","sourceRoot":"","sources":["../src/earcut.ts"],"names":[],"mappings":"AA4BA;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CACpB,SAAS,EAAE,MAAM,EAAE,EACnB,WAAW,CAAC,EAAE,MAAM,EAAE,EACtB,GAAG,GAAE,MAAU,EACf,KAAK,CAAC,EAAE,MAAM,EAAE,GACf,MAAM,EAAE,CAwCV"}
|