@math.gl/polygon 3.5.6 → 3.6.0-alpha.1

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.
Files changed (80) hide show
  1. package/dist/cut-by-grid.d.ts +20 -0
  2. package/dist/cut-by-grid.d.ts.map +1 -0
  3. package/dist/cut-by-grid.js +216 -0
  4. package/dist/cut-by-mercator-bounds.d.ts +16 -0
  5. package/dist/cut-by-mercator-bounds.d.ts.map +1 -0
  6. package/dist/cut-by-mercator-bounds.js +145 -0
  7. package/dist/{esm/earcut.d.ts → earcut.d.ts} +4 -2
  8. package/dist/earcut.d.ts.map +1 -0
  9. package/dist/earcut.js +611 -0
  10. package/dist/es5/cut-by-grid.js +70 -79
  11. package/dist/es5/cut-by-grid.js.map +1 -1
  12. package/dist/es5/cut-by-mercator-bounds.js +55 -87
  13. package/dist/es5/cut-by-mercator-bounds.js.map +1 -1
  14. package/dist/es5/earcut.js +97 -102
  15. package/dist/es5/earcut.js.map +1 -1
  16. package/dist/es5/index.js +14 -14
  17. package/dist/es5/index.js.map +1 -1
  18. package/dist/es5/lineclip.js +38 -41
  19. package/dist/es5/lineclip.js.map +1 -1
  20. package/dist/es5/polygon-utils.js +61 -69
  21. package/dist/es5/polygon-utils.js.map +1 -1
  22. package/dist/es5/polygon.js +35 -43
  23. package/dist/es5/polygon.js.map +1 -1
  24. package/dist/es5/utils.js +11 -12
  25. package/dist/es5/utils.js.map +1 -1
  26. package/dist/esm/cut-by-grid.js +4 -4
  27. package/dist/esm/cut-by-grid.js.map +1 -1
  28. package/dist/esm/cut-by-mercator-bounds.js +5 -5
  29. package/dist/esm/cut-by-mercator-bounds.js.map +1 -1
  30. package/dist/esm/earcut.js +9 -10
  31. package/dist/esm/earcut.js.map +1 -1
  32. package/dist/esm/index.js.map +1 -1
  33. package/dist/esm/lineclip.js +5 -5
  34. package/dist/esm/lineclip.js.map +1 -1
  35. package/dist/esm/polygon-utils.js +7 -4
  36. package/dist/esm/polygon-utils.js.map +1 -1
  37. package/dist/esm/polygon.js +7 -0
  38. package/dist/esm/polygon.js.map +1 -1
  39. package/dist/esm/utils.js.map +1 -1
  40. package/dist/index.d.ts +8 -0
  41. package/dist/index.d.ts.map +1 -0
  42. package/dist/index.js +8 -0
  43. package/dist/lineclip.d.ts +31 -0
  44. package/dist/lineclip.d.ts.map +1 -0
  45. package/dist/lineclip.js +178 -0
  46. package/dist/{es5/polygon-utils.d.ts → polygon-utils.d.ts} +28 -64
  47. package/dist/polygon-utils.d.ts.map +1 -0
  48. package/dist/polygon-utils.js +140 -0
  49. package/dist/polygon.d.ts +40 -0
  50. package/dist/polygon.d.ts.map +1 -0
  51. package/dist/polygon.js +67 -0
  52. package/dist/utils.d.ts +4 -0
  53. package/dist/utils.d.ts.map +1 -0
  54. package/dist/utils.js +34 -0
  55. package/package.json +4 -3
  56. package/src/{cut-by-grid.js → cut-by-grid.ts} +32 -4
  57. package/src/{cut-by-mercator-bounds.js → cut-by-mercator-bounds.ts} +26 -7
  58. package/src/{earcut.js → earcut.ts} +25 -13
  59. package/src/{index.js → index.ts} +0 -0
  60. package/src/{lineclip.js → lineclip.ts} +48 -20
  61. package/src/polygon-utils.ts +225 -0
  62. package/src/{polygon.js → polygon.ts} +40 -9
  63. package/src/{utils.js → utils.ts} +0 -0
  64. package/dist/es5/cut-by-grid.d.ts +0 -24
  65. package/dist/es5/cut-by-mercator-bounds.d.ts +0 -23
  66. package/dist/es5/earcut.d.ts +0 -9
  67. package/dist/es5/lineclip.d.ts +0 -25
  68. package/dist/es5/polygon.d.ts +0 -36
  69. package/dist/esm/cut-by-grid.d.ts +0 -24
  70. package/dist/esm/cut-by-mercator-bounds.d.ts +0 -23
  71. package/dist/esm/lineclip.d.ts +0 -25
  72. package/dist/esm/polygon-utils.d.ts +0 -117
  73. package/dist/esm/polygon.d.ts +0 -36
  74. package/src/cut-by-grid.d.ts +0 -24
  75. package/src/cut-by-mercator-bounds.d.ts +0 -23
  76. package/src/earcut.d.ts +0 -9
  77. package/src/lineclip.d.ts +0 -25
  78. package/src/polygon-utils.d.ts +0 -117
  79. package/src/polygon-utils.js +0 -127
  80. package/src/polygon.d.ts +0 -36
@@ -0,0 +1,20 @@
1
+ export declare type Polygon = {
2
+ positions: Array<number>;
3
+ holeIndices?: Array<number>;
4
+ vertexTypes?: Array<number>;
5
+ };
6
+ export declare function cutPolylineByGrid(positions: Array<number>, options?: {
7
+ size?: number;
8
+ broken?: boolean;
9
+ gridResolution?: number;
10
+ gridOffset?: [number, number];
11
+ startIndex?: number;
12
+ endIndex?: number;
13
+ }): Array<number> | Array<Array<number>>;
14
+ export declare function cutPolygonByGrid(positions: Array<number>, holeIndices?: Array<number> | null, options?: {
15
+ size?: number;
16
+ gridResolution?: number;
17
+ gridOffset?: [number, number];
18
+ edgeTypes?: boolean;
19
+ }): Array<Polygon>;
20
+ //# 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,oBAAY,OAAO,GAAG;IACpB,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACzB,WAAW,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC5B,WAAW,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;CAC7B,CAAC;AAEF,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,EACxB,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,KAAK,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAkDtC;AAYD,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,EACxB,WAAW,GAAE,KAAK,CAAC,MAAM,CAAC,GAAG,IAAW,EACxC,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,KAAK,CAAC,OAAO,CAAC,CAqEhB"}
@@ -0,0 +1,216 @@
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),
61
+ holes: holeIndices || []
62
+ }
63
+ ];
64
+ const bbox = [[], []];
65
+ let cell = [];
66
+ // Recursively bisect polygon until every part fit in a single grid cell
67
+ while (queue.length) {
68
+ const { pos, types, holes } = queue.shift();
69
+ // Get the bounding box of the outer polygon
70
+ getBoundingBox(pos, size, holes[0] || pos.length, bbox);
71
+ cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
72
+ // @ts-expect-error
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
+ // @ts-expect-error
104
+ polygon.edgeTypes = types;
105
+ }
106
+ if (holes.length) {
107
+ // @ts-expect-error
108
+ polygon.holeIndices = holes;
109
+ }
110
+ result.push(polygon);
111
+ }
112
+ }
113
+ return result;
114
+ }
115
+ // edgeTypes:
116
+ // TYPE_BORDER - edge from the original polygon
117
+ // TYPE_INSIDE - inside the original polygon
118
+ // eslint-disable-next-line max-params
119
+ function bisectPolygon(positions, edgeTypes, size, startIndex, endIndex, bbox, edge) {
120
+ const numPoints = (endIndex - startIndex) / size;
121
+ const resultLow = [];
122
+ const resultHigh = [];
123
+ const typesLow = [];
124
+ const typesHigh = [];
125
+ const scratchPoint = [];
126
+ let p;
127
+ let side;
128
+ let type;
129
+ const prev = getPointAtIndex(positions, numPoints - 1, size, startIndex);
130
+ let prevSide = Math.sign(edge & 8 ? prev[1] - bbox[3] : prev[0] - bbox[2]);
131
+ let prevType = edgeTypes && edgeTypes[numPoints - 1];
132
+ let lowPointCount = 0;
133
+ let highPointCount = 0;
134
+ for (let i = 0; i < numPoints; i++) {
135
+ p = getPointAtIndex(positions, i, size, startIndex, p);
136
+ side = Math.sign(edge & 8 ? p[1] - bbox[3] : p[0] - bbox[2]);
137
+ type = edgeTypes && edgeTypes[startIndex / size + i];
138
+ // if segment goes through the boundary, add an intersection
139
+ if (side && prevSide && prevSide !== side) {
140
+ intersect(prev, p, edge, bbox, scratchPoint);
141
+ push(resultLow, scratchPoint) && typesLow.push(prevType);
142
+ push(resultHigh, scratchPoint) && typesHigh.push(prevType);
143
+ }
144
+ if (side <= 0) {
145
+ push(resultLow, p) && typesLow.push(type);
146
+ lowPointCount -= side;
147
+ }
148
+ else if (typesLow.length) {
149
+ typesLow[typesLow.length - 1] = TYPE_INSIDE;
150
+ }
151
+ if (side >= 0) {
152
+ push(resultHigh, p) && typesHigh.push(type);
153
+ highPointCount += side;
154
+ }
155
+ else if (typesHigh.length) {
156
+ typesHigh[typesHigh.length - 1] = TYPE_INSIDE;
157
+ }
158
+ copy(prev, p);
159
+ prevSide = side;
160
+ prevType = type;
161
+ }
162
+ return [
163
+ lowPointCount ? { pos: resultLow, types: edgeTypes && typesLow } : null,
164
+ highPointCount ? { pos: resultHigh, types: edgeTypes && typesHigh } : null
165
+ ];
166
+ }
167
+ function getGridCell(p, gridResolution, gridOffset, out) {
168
+ const left = Math.floor((p[0] - gridOffset[0]) / gridResolution) * gridResolution + gridOffset[0];
169
+ const bottom = Math.floor((p[1] - gridOffset[1]) / gridResolution) * gridResolution + gridOffset[1];
170
+ out[0] = left;
171
+ out[1] = bottom;
172
+ out[2] = left + gridResolution;
173
+ out[3] = bottom + gridResolution;
174
+ return out;
175
+ }
176
+ function moveToNeighborCell(cell, gridResolution, edge) {
177
+ if (edge & 8) {
178
+ // top
179
+ cell[1] += gridResolution;
180
+ cell[3] += gridResolution;
181
+ }
182
+ else if (edge & 4) {
183
+ // bottom
184
+ cell[1] -= gridResolution;
185
+ cell[3] -= gridResolution;
186
+ }
187
+ else if (edge & 2) {
188
+ // right
189
+ cell[0] += gridResolution;
190
+ cell[2] += gridResolution;
191
+ }
192
+ else if (edge & 1) {
193
+ // left
194
+ cell[0] -= gridResolution;
195
+ cell[2] -= gridResolution;
196
+ }
197
+ }
198
+ function getBoundingBox(positions, size, endIndex, out) {
199
+ let minX = Infinity;
200
+ let maxX = -Infinity;
201
+ let minY = Infinity;
202
+ let maxY = -Infinity;
203
+ for (let i = 0; i < endIndex; i += size) {
204
+ const x = positions[i];
205
+ const y = positions[i + 1];
206
+ minX = x < minX ? x : minX;
207
+ maxX = x > maxX ? x : maxX;
208
+ minY = y < minY ? y : minY;
209
+ maxY = y > maxY ? y : maxY;
210
+ }
211
+ out[0][0] = minX;
212
+ out[0][1] = minY;
213
+ out[1][0] = maxX;
214
+ out[1][1] = maxY;
215
+ return out;
216
+ }
@@ -0,0 +1,16 @@
1
+ import type { Polygon } from './cut-by-grid';
2
+ /** https://user-images.githubusercontent.com/2059298/78465769-938b7a00-76ae-11ea-9b95-1f4c26425ab9.png */
3
+ export declare function cutPolylineByMercatorBounds(positions: Array<number>, options?: {
4
+ size?: number;
5
+ startIndex?: number;
6
+ endIndex?: number;
7
+ normalize?: boolean;
8
+ }): Array<number>;
9
+ /** https://user-images.githubusercontent.com/2059298/78465770-94241080-76ae-11ea-809a-6a8534dac1d9.png */
10
+ export declare function cutPolygonByMercatorBounds(positions: Array<number>, holeIndices?: Array<number> | null, options?: {
11
+ size?: number;
12
+ normalize?: boolean;
13
+ maxLatitude?: number;
14
+ edgeTypes?: boolean;
15
+ }): Array<Polygon>;
16
+ //# 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;AAK3C,0GAA0G;AAC1G,wBAAgB,2BAA2B,CACzC,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,EACxB,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,KAAK,CAAC,MAAM,CAAC,CAuBf;AAED,0GAA0G;AAC1G,wBAAgB,0BAA0B,CACxC,SAAS,EAAE,KAAK,CAAC,MAAM,CAAC,EACxB,WAAW,GAAE,KAAK,CAAC,MAAM,CAAC,GAAG,IAAW,EACxC,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,KAAK,CAAC,OAAO,CAAC,CAmDhB"}
@@ -0,0 +1,145 @@
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
+ // @ts-expect-error
25
+ return parts;
26
+ }
27
+ /** https://user-images.githubusercontent.com/2059298/78465770-94241080-76ae-11ea-809a-6a8534dac1d9.png */
28
+ export function cutPolygonByMercatorBounds(positions, holeIndices = null, options) {
29
+ const { size = 2, normalize = true, edgeTypes = false } = options || {};
30
+ holeIndices = holeIndices || [];
31
+ const newPositions = [];
32
+ const newHoleIndices = [];
33
+ let srcStartIndex = 0;
34
+ let targetIndex = 0;
35
+ for (let ringIndex = 0; ringIndex <= holeIndices.length; ringIndex++) {
36
+ // srcStartIndex/srcEndIndex define the ring in the original positions
37
+ const srcEndIndex = holeIndices[ringIndex] || positions.length;
38
+ // targetStartIndex/targetIndex define the ring in newPositions
39
+ const targetStartIndex = targetIndex;
40
+ // In case the ring contains a pole (e.g. Antarctica), we'll have to insert vertices
41
+ // The insertion point is defined by the vertex closest to the pole
42
+ // Split the the ring by the insertion point when copying to newPositions
43
+ const splitIndex = findSplitIndex(positions, size, srcStartIndex, srcEndIndex);
44
+ for (let i = splitIndex; i < srcEndIndex; i++) {
45
+ newPositions[targetIndex++] = positions[i];
46
+ }
47
+ for (let i = srcStartIndex; i < splitIndex; i++) {
48
+ newPositions[targetIndex++] = positions[i];
49
+ }
50
+ // Remap longitudes so that each segment takes the shorter path
51
+ wrapLongitudesForShortestPath(newPositions, size, targetStartIndex, targetIndex);
52
+ // Handle the case when the ring contains a pole
53
+ insertPoleVertices(newPositions, size, targetStartIndex, targetIndex, options?.maxLatitude);
54
+ srcStartIndex = srcEndIndex;
55
+ newHoleIndices[ringIndex] = targetIndex;
56
+ }
57
+ newHoleIndices.pop();
58
+ const parts = cutPolygonByGrid(newPositions, newHoleIndices, {
59
+ size,
60
+ gridResolution: 360,
61
+ gridOffset: [-180, -180],
62
+ edgeTypes
63
+ });
64
+ if (normalize) {
65
+ // Each part is guaranteed to be in a single copy of the world
66
+ // Map longitudes back to [-180, 180]
67
+ for (const part of parts) {
68
+ shiftLongitudesIntoRange(part.positions, size);
69
+ }
70
+ }
71
+ return parts;
72
+ }
73
+ /* Helpers */
74
+ // See comments for insertPoleVertices
75
+ function findSplitIndex(positions, size, startIndex, endIndex) {
76
+ let maxLat = -1;
77
+ let pointIndex = -1;
78
+ for (let i = startIndex + 1; i < endIndex; i += size) {
79
+ const lat = Math.abs(positions[i]);
80
+ if (lat > maxLat) {
81
+ maxLat = lat;
82
+ pointIndex = i - 1;
83
+ }
84
+ }
85
+ return pointIndex;
86
+ }
87
+ // https://user-images.githubusercontent.com/2059298/78857483-5987e400-79de-11ea-98fc-0631287a8431.png
88
+ //
89
+ // If the polygon contains a pole, to tesselate it correctly, we need to insert the edge
90
+ // of map into the polygon. This requires adding two vertices that represent the pole, by
91
+ // drawing a perpendicular line to the Mercator map edge from a selected vertex on the ring.
92
+ //
93
+ // We select the insertion position carefully so that the inserted line segments do not
94
+ // intersect with the ring itself. This is ensured by findSplitIndex, which returns the
95
+ // vertex closest to the pole.
96
+ function insertPoleVertices(positions, size, startIndex, endIndex, maxLatitude = DEFAULT_MAX_LATITUDE) {
97
+ // Check if the ring contains a pole
98
+ const firstLng = positions[startIndex];
99
+ const lastLng = positions[endIndex - size];
100
+ if (Math.abs(firstLng - lastLng) > 180) {
101
+ // The ring does not make a round trip
102
+ // Add the nearest pole to the vertices so that the polygon tesselates correctly
103
+ const p = getPointAtIndex(positions, 0, size, startIndex);
104
+ // Copy the first vertex to the world of the last vertex
105
+ p[0] += Math.round((lastLng - firstLng) / 360) * 360;
106
+ push(positions, p);
107
+ // Project the copied vertex to the edge of the map
108
+ p[1] = Math.sign(p[1]) * maxLatitude;
109
+ push(positions, p);
110
+ // Project the first vertex to the edge of the map
111
+ p[0] = firstLng;
112
+ push(positions, p);
113
+ }
114
+ }
115
+ function wrapLongitudesForShortestPath(positions, size, startIndex, endIndex) {
116
+ let prevLng = positions[0];
117
+ let lng;
118
+ for (let i = startIndex; i < endIndex; i += size) {
119
+ lng = positions[i];
120
+ const delta = lng - prevLng;
121
+ if (delta > 180 || delta < -180) {
122
+ lng -= Math.round(delta / 360) * 360;
123
+ }
124
+ positions[i] = prevLng = lng;
125
+ }
126
+ }
127
+ function shiftLongitudesIntoRange(positions, size) {
128
+ let refLng;
129
+ const pointCount = positions.length / size;
130
+ // Find a longitude that is not on the edge of a world
131
+ // Which we will use to determine which world copy it is
132
+ for (let i = 0; i < pointCount; i++) {
133
+ refLng = positions[i * size];
134
+ if ((refLng + 180) % 360 !== 0) {
135
+ break;
136
+ }
137
+ }
138
+ const delta = -Math.round(refLng / 360) * 360;
139
+ if (delta === 0) {
140
+ return;
141
+ }
142
+ for (let i = 0; i < pointCount; i++) {
143
+ positions[i * size] += delta;
144
+ }
145
+ }
@@ -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 size 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`.
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[], size?: number, areas?: number[]): 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"}