@math.gl/polygon 3.6.3 → 4.0.0-alpha.2

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 (58) hide show
  1. package/dist/cut-by-grid.d.ts +3 -0
  2. package/dist/cut-by-grid.d.ts.map +1 -1
  3. package/dist/{esm/cut-by-grid.js → cut-by-grid.js} +10 -11
  4. package/dist/cut-by-grid.js.map +1 -0
  5. package/dist/{esm/cut-by-mercator-bounds.js → cut-by-mercator-bounds.js} +2 -2
  6. package/dist/cut-by-mercator-bounds.js.map +1 -0
  7. package/dist/earcut.d.ts +3 -1
  8. package/dist/earcut.d.ts.map +1 -1
  9. package/dist/{esm/earcut.js → earcut.js} +96 -65
  10. package/dist/earcut.js.map +1 -0
  11. package/dist/index.cjs +1129 -0
  12. package/dist/index.d.ts +3 -2
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +8 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/{esm/lineclip.js → lineclip.js} +1 -1
  17. package/dist/lineclip.js.map +1 -0
  18. package/dist/polygon-utils.d.ts +22 -0
  19. package/dist/polygon-utils.d.ts.map +1 -1
  20. package/dist/{esm/polygon-utils.js → polygon-utils.js} +15 -4
  21. package/dist/polygon-utils.js.map +1 -0
  22. package/dist/polygon.d.ts +1 -1
  23. package/dist/polygon.d.ts.map +1 -1
  24. package/dist/{esm/polygon.js → polygon.js} +2 -2
  25. package/dist/polygon.js.map +1 -0
  26. package/dist/utils.js.map +1 -0
  27. package/package.json +6 -5
  28. package/src/cut-by-grid.ts +26 -9
  29. package/src/earcut.ts +143 -77
  30. package/src/index.ts +3 -3
  31. package/src/polygon-utils.ts +42 -8
  32. package/src/polygon.ts +1 -1
  33. package/dist/es5/cut-by-grid.js +0 -276
  34. package/dist/es5/cut-by-grid.js.map +0 -1
  35. package/dist/es5/cut-by-mercator-bounds.js +0 -194
  36. package/dist/es5/cut-by-mercator-bounds.js.map +0 -1
  37. package/dist/es5/earcut.js +0 -540
  38. package/dist/es5/earcut.js.map +0 -1
  39. package/dist/es5/index.js +0 -104
  40. package/dist/es5/index.js.map +0 -1
  41. package/dist/es5/lineclip.js +0 -150
  42. package/dist/es5/lineclip.js.map +0 -1
  43. package/dist/es5/polygon-utils.js +0 -154
  44. package/dist/es5/polygon-utils.js.map +0 -1
  45. package/dist/es5/polygon.js +0 -79
  46. package/dist/es5/polygon.js.map +0 -1
  47. package/dist/es5/utils.js +0 -54
  48. package/dist/es5/utils.js.map +0 -1
  49. package/dist/esm/cut-by-grid.js.map +0 -1
  50. package/dist/esm/cut-by-mercator-bounds.js.map +0 -1
  51. package/dist/esm/earcut.js.map +0 -1
  52. package/dist/esm/index.js +0 -8
  53. package/dist/esm/index.js.map +0 -1
  54. package/dist/esm/lineclip.js.map +0 -1
  55. package/dist/esm/polygon-utils.js.map +0 -1
  56. package/dist/esm/polygon.js.map +0 -1
  57. package/dist/esm/utils.js.map +0 -1
  58. /package/dist/{esm/utils.js → utils.js} +0 -0
package/dist/es5/index.js DELETED
@@ -1,104 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- Object.defineProperty(exports, "Polygon", {
9
- enumerable: true,
10
- get: function get() {
11
- return _polygon.default;
12
- }
13
- });
14
- Object.defineProperty(exports, "_Polygon", {
15
- enumerable: true,
16
- get: function get() {
17
- return _polygon.default;
18
- }
19
- });
20
- Object.defineProperty(exports, "getPolygonSignedArea", {
21
- enumerable: true,
22
- get: function get() {
23
- return _polygonUtils.getPolygonSignedArea;
24
- }
25
- });
26
- Object.defineProperty(exports, "getPolygonWindingDirection", {
27
- enumerable: true,
28
- get: function get() {
29
- return _polygonUtils.getPolygonWindingDirection;
30
- }
31
- });
32
- Object.defineProperty(exports, "forEachSegmentInPolygon", {
33
- enumerable: true,
34
- get: function get() {
35
- return _polygonUtils.forEachSegmentInPolygon;
36
- }
37
- });
38
- Object.defineProperty(exports, "modifyPolygonWindingDirection", {
39
- enumerable: true,
40
- get: function get() {
41
- return _polygonUtils.modifyPolygonWindingDirection;
42
- }
43
- });
44
- Object.defineProperty(exports, "WINDING", {
45
- enumerable: true,
46
- get: function get() {
47
- return _polygonUtils.WINDING;
48
- }
49
- });
50
- Object.defineProperty(exports, "earcut", {
51
- enumerable: true,
52
- get: function get() {
53
- return _earcut.earcut;
54
- }
55
- });
56
- Object.defineProperty(exports, "clipPolygon", {
57
- enumerable: true,
58
- get: function get() {
59
- return _lineclip.clipPolygon;
60
- }
61
- });
62
- Object.defineProperty(exports, "clipPolyline", {
63
- enumerable: true,
64
- get: function get() {
65
- return _lineclip.clipPolyline;
66
- }
67
- });
68
- Object.defineProperty(exports, "cutPolygonByGrid", {
69
- enumerable: true,
70
- get: function get() {
71
- return _cutByGrid.cutPolygonByGrid;
72
- }
73
- });
74
- Object.defineProperty(exports, "cutPolylineByGrid", {
75
- enumerable: true,
76
- get: function get() {
77
- return _cutByGrid.cutPolylineByGrid;
78
- }
79
- });
80
- Object.defineProperty(exports, "cutPolylineByMercatorBounds", {
81
- enumerable: true,
82
- get: function get() {
83
- return _cutByMercatorBounds.cutPolylineByMercatorBounds;
84
- }
85
- });
86
- Object.defineProperty(exports, "cutPolygonByMercatorBounds", {
87
- enumerable: true,
88
- get: function get() {
89
- return _cutByMercatorBounds.cutPolygonByMercatorBounds;
90
- }
91
- });
92
-
93
- var _polygon = _interopRequireDefault(require("./polygon"));
94
-
95
- var _polygonUtils = require("./polygon-utils");
96
-
97
- var _earcut = require("./earcut");
98
-
99
- var _lineclip = require("./lineclip");
100
-
101
- var _cutByGrid = require("./cut-by-grid");
102
-
103
- var _cutByMercatorBounds = require("./cut-by-mercator-bounds");
104
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AAEA;;AAQA;;AAEA;;AAEA;;AAEA","sourcesContent":["export {default as Polygon} from './polygon';\n\nexport {\n getPolygonSignedArea,\n getPolygonWindingDirection,\n forEachSegmentInPolygon,\n modifyPolygonWindingDirection,\n WINDING\n} from './polygon-utils';\n\nexport {earcut} from './earcut';\n\nexport {clipPolygon, clipPolyline} from './lineclip';\n\nexport {cutPolygonByGrid, cutPolylineByGrid} from './cut-by-grid';\n\nexport {cutPolylineByMercatorBounds, cutPolygonByMercatorBounds} from './cut-by-mercator-bounds';\n\n// DEPRECATED\nexport {default as _Polygon} from './polygon';\n"],"file":"index.js"}
@@ -1,150 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.clipPolyline = clipPolyline;
7
- exports.clipPolygon = clipPolygon;
8
- exports.intersect = intersect;
9
- exports.bitCode = bitCode;
10
-
11
- var _utils = require("./utils");
12
-
13
- function clipPolyline(positions, bbox, options) {
14
- var _ref = options || {},
15
- _ref$size = _ref.size,
16
- size = _ref$size === void 0 ? 2 : _ref$size,
17
- _ref$startIndex = _ref.startIndex,
18
- startIndex = _ref$startIndex === void 0 ? 0 : _ref$startIndex,
19
- _ref$endIndex = _ref.endIndex,
20
- endIndex = _ref$endIndex === void 0 ? positions.length : _ref$endIndex;
21
-
22
- var numPoints = (endIndex - startIndex) / size;
23
- var result = [];
24
- var part = [];
25
- var a;
26
- var b;
27
- var codeA = -1;
28
- var codeB;
29
- var lastCode;
30
-
31
- for (var i = 1; i < numPoints; i++) {
32
- a = (0, _utils.getPointAtIndex)(positions, i - 1, size, startIndex, a);
33
- b = (0, _utils.getPointAtIndex)(positions, i, size, startIndex, b);
34
-
35
- if (codeA < 0) {
36
- codeA = bitCode(a, bbox);
37
- }
38
-
39
- codeB = lastCode = bitCode(b, bbox);
40
-
41
- while (true) {
42
- if (!(codeA | codeB)) {
43
- (0, _utils.push)(part, a);
44
-
45
- if (codeB !== lastCode) {
46
- (0, _utils.push)(part, b);
47
-
48
- if (i < numPoints - 1) {
49
- result.push(part);
50
- part = [];
51
- }
52
- } else if (i === numPoints - 1) {
53
- (0, _utils.push)(part, b);
54
- }
55
-
56
- break;
57
- } else if (codeA & codeB) {
58
- break;
59
- } else if (codeA) {
60
- intersect(a, b, codeA, bbox, a);
61
- codeA = bitCode(a, bbox);
62
- } else {
63
- intersect(a, b, codeB, bbox, b);
64
- codeB = bitCode(b, bbox);
65
- }
66
- }
67
-
68
- codeA = lastCode;
69
- }
70
-
71
- if (part.length) result.push(part);
72
- return result;
73
- }
74
-
75
- function clipPolygon(positions, bbox, options) {
76
- var _ref2 = options || {},
77
- _ref2$size = _ref2.size,
78
- size = _ref2$size === void 0 ? 2 : _ref2$size,
79
- _ref2$endIndex = _ref2.endIndex,
80
- endIndex = _ref2$endIndex === void 0 ? positions.length : _ref2$endIndex;
81
-
82
- var _ref3 = options || {},
83
- _ref3$startIndex = _ref3.startIndex,
84
- startIndex = _ref3$startIndex === void 0 ? 0 : _ref3$startIndex;
85
-
86
- var numPoints = (endIndex - startIndex) / size;
87
- var result;
88
- var p;
89
- var prev;
90
- var inside;
91
- var prevInside;
92
-
93
- for (var edge = 1; edge <= 8; edge *= 2) {
94
- result = [];
95
- prev = (0, _utils.getPointAtIndex)(positions, numPoints - 1, size, startIndex, prev);
96
- prevInside = !(bitCode(prev, bbox) & edge);
97
-
98
- for (var i = 0; i < numPoints; i++) {
99
- p = (0, _utils.getPointAtIndex)(positions, i, size, startIndex, p);
100
- inside = !(bitCode(p, bbox) & edge);
101
- if (inside !== prevInside) (0, _utils.push)(result, intersect(prev, p, edge, bbox));
102
- if (inside) (0, _utils.push)(result, p);
103
- (0, _utils.copy)(prev, p);
104
- prevInside = inside;
105
- }
106
-
107
- positions = result;
108
- startIndex = 0;
109
- numPoints = result.length / size;
110
- if (!numPoints) break;
111
- }
112
-
113
- return result;
114
- }
115
-
116
- function intersect(a, b, edge, bbox) {
117
- var out = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
118
- var t;
119
- var snap;
120
-
121
- if (edge & 8) {
122
- t = (bbox[3] - a[1]) / (b[1] - a[1]);
123
- snap = 3;
124
- } else if (edge & 4) {
125
- t = (bbox[1] - a[1]) / (b[1] - a[1]);
126
- snap = 1;
127
- } else if (edge & 2) {
128
- t = (bbox[2] - a[0]) / (b[0] - a[0]);
129
- snap = 2;
130
- } else if (edge & 1) {
131
- t = (bbox[0] - a[0]) / (b[0] - a[0]);
132
- snap = 0;
133
- } else {
134
- return null;
135
- }
136
-
137
- for (var i = 0; i < a.length; i++) {
138
- out[i] = (snap & 1) === i ? bbox[snap] : t * (b[i] - a[i]) + a[i];
139
- }
140
-
141
- return out;
142
- }
143
-
144
- function bitCode(p, bbox) {
145
- var code = 0;
146
- if (p[0] < bbox[0]) code |= 1;else if (p[0] > bbox[2]) code |= 2;
147
- if (p[1] < bbox[1]) code |= 4;else if (p[1] > bbox[3]) code |= 8;
148
- return code;
149
- }
150
- //# sourceMappingURL=lineclip.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/lineclip.ts"],"names":["clipPolyline","positions","bbox","options","size","startIndex","endIndex","length","numPoints","result","part","a","b","codeA","codeB","lastCode","i","bitCode","push","intersect","clipPolygon","p","prev","inside","prevInside","edge","out","t","snap","code"],"mappings":";;;;;;;;;;AAwBA;;AASO,SAASA,YAAT,CACLC,SADK,EAELC,IAFK,EAGLC,OAHK,EAQO;AACZ,aAAgEA,OAAO,IAAI,EAA3E;AAAA,uBAAOC,IAAP;AAAA,MAAOA,IAAP,0BAAc,CAAd;AAAA,6BAAiBC,UAAjB;AAAA,MAAiBA,UAAjB,gCAA8B,CAA9B;AAAA,2BAAiCC,QAAjC;AAAA,MAAiCA,QAAjC,8BAA4CL,SAAS,CAACM,MAAtD;;AACA,MAAMC,SAAS,GAAG,CAACF,QAAQ,GAAGD,UAAZ,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X;AAEA,MAAIR,CAAC,CAAC,CAAD,CAAD,GAAOnB,IAAI,CAAC,CAAD,CAAf,EAAoB2B,IAAI,IAAI,CAAR,CAApB,KAEK,IAAIR,CAAC,CAAC,CAAD,CAAD,GAAOnB,IAAI,CAAC,CAAD,CAAf,EAAoB2B,IAAI,IAAI,CAAR;AAEzB,MAAIR,CAAC,CAAC,CAAD,CAAD,GAAOnB,IAAI,CAAC,CAAD,CAAf,EAAoB2B,IAAI,IAAI,CAAR,CAApB,KAEK,IAAIR,CAAC,CAAC,CAAD,CAAD,GAAOnB,IAAI,CAAC,CAAD,CAAf,EAAoB2B,IAAI,IAAI,CAAR;AAEzB,SAAOA,IAAP;AACD","sourcesContent":["/*\n Adapted from https://github.com/mapbox/lineclip to work with flat arrays\n and 3d positions\n\n ISC License\n\n Copyright (c) 2015, Mapbox\n\n Permission to use, copy, modify, and/or distribute this software for any purpose\n with or without fee is hereby granted, provided that the above copyright notice\n and this permission notice appear in all copies.\n\n THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\n REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND\n FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\n INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS\n OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\n TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF\n THIS SOFTWARE.\n\n */\n\n/* eslint-disable max-statements, max-depth, complexity */\n\nimport {push, copy, getPointAtIndex} from './utils';\nimport type {NumericArray} from '@math.gl/core';\n\nexport type BoundingBox = [number, number, number, number];\n\n/**\n * Cohen-Sutherland line clipping algorithm, adapted to efficiently\n * handle polylines rather than just segments\n */\nexport function clipPolyline(\n positions: Readonly<NumericArray>,\n bbox: BoundingBox,\n options?: {\n size?: number;\n startIndex?: number;\n endIndex?: number;\n }\n): number[][] {\n const {size = 2, startIndex = 0, endIndex = positions.length} = options || {};\n const numPoints = (endIndex - startIndex) / size;\n const result: number[][] = [];\n let part: number[] = [];\n let a: number[];\n let b: number[];\n let codeA: number = -1;\n let codeB: number;\n let lastCode: number;\n\n for (let i = 1; i < numPoints; i++) {\n a = getPointAtIndex(positions, i - 1, size, startIndex, a);\n b = getPointAtIndex(positions, i, size, startIndex, b);\n if (codeA < 0) {\n codeA = bitCode(a, bbox);\n }\n codeB = lastCode = bitCode(b, bbox);\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n if (!(codeA | codeB)) {\n // accept\n push(part, a);\n\n if (codeB !== lastCode) {\n // segment went outside\n push(part, b);\n\n if (i < numPoints - 1) {\n // start a new line\n result.push(part);\n part = [];\n }\n } else if (i === numPoints - 1) {\n push(part, b);\n }\n break;\n } else if (codeA & codeB) {\n // trivial reject\n break;\n } else if (codeA) {\n // a outside, intersect with clip edge\n intersect(a, b, codeA, bbox, a);\n codeA = bitCode(a, bbox);\n } else {\n // b outside\n intersect(a, b, codeB, bbox, b);\n codeB = bitCode(b, bbox);\n }\n }\n\n codeA = lastCode;\n }\n\n if (part.length) result.push(part);\n\n return result;\n}\n\n/**\n * Sutherland-Hodgeman polygon clipping algorithm\n * polygon must be closed (first vertex == last vertex)\n */\nexport function clipPolygon(\n positions: Readonly<NumericArray>,\n bbox: BoundingBox,\n options?: {\n size?: number;\n startIndex?: number;\n endIndex?: number;\n }\n): number[] {\n const {size = 2, endIndex = positions.length} = options || {};\n let {startIndex = 0} = options || {};\n let numPoints = (endIndex - startIndex) / size;\n let result: number[];\n let p: number[];\n let prev: number[];\n let inside: boolean;\n let prevInside: boolean;\n\n // clip against each side of the clip rectangle\n for (let edge = 1; edge <= 8; edge *= 2) {\n result = [];\n prev = getPointAtIndex(positions, numPoints - 1, size, startIndex, prev);\n prevInside = !(bitCode(prev, bbox) & edge);\n\n for (let i = 0; i < numPoints; i++) {\n p = getPointAtIndex(positions, i, size, startIndex, p);\n inside = !(bitCode(p, bbox) & edge);\n\n // if segment goes through the clip window, add an intersection\n if (inside !== prevInside) push(result, intersect(prev, p, edge, bbox));\n\n if (inside) push(result, p); // add a point if it's inside\n\n copy(prev, p);\n prevInside = inside;\n }\n\n // close loop\n positions = result;\n startIndex = 0;\n numPoints = result.length / size;\n\n if (!numPoints) break;\n }\n\n return result;\n}\n\n/** intersect a segment against one of the 4 lines that make up the bbox */\n\nexport function intersect(\n a: number[],\n b: number[],\n edge: number,\n bbox: BoundingBox,\n out: number[] = []\n): number[] {\n let t;\n // Forces out[snapI] to be on the bbox edge\n // Interpolation introduces precision issue which may cause lineclip to be\n // stuck in an infinite loop\n let snap: number;\n if (edge & 8) {\n // top\n t = (bbox[3] - a[1]) / (b[1] - a[1]);\n snap = 3;\n } else if (edge & 4) {\n // bottom\n t = (bbox[1] - a[1]) / (b[1] - a[1]);\n snap = 1;\n } else if (edge & 2) {\n // right\n t = (bbox[2] - a[0]) / (b[0] - a[0]);\n snap = 2;\n } else if (edge & 1) {\n // left\n t = (bbox[0] - a[0]) / (b[0] - a[0]);\n snap = 0;\n } else {\n return null;\n }\n for (let i = 0; i < a.length; i++) {\n out[i] = (snap & 1) === i ? bbox[snap] : t * (b[i] - a[i]) + a[i];\n }\n return out;\n}\n\n/**\n * bit code reflects the point position relative to the bbox:\n * left mid right\n * top 1001 1000 1010\n * mid 0001 0000 0010\n * bottom 0101 0100 0110\n */\nexport function bitCode(p: number[], bbox: BoundingBox): number {\n let code = 0;\n\n if (p[0] < bbox[0]) code |= 1;\n // left\n else if (p[0] > bbox[2]) code |= 2; // right\n\n if (p[1] < bbox[1]) code |= 4;\n // bottom\n else if (p[1] > bbox[3]) code |= 8; // top\n\n return code;\n}\n"],"file":"lineclip.js"}
@@ -1,154 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.modifyPolygonWindingDirection = modifyPolygonWindingDirection;
7
- exports.getPolygonWindingDirection = getPolygonWindingDirection;
8
- exports.getPolygonSignedArea = getPolygonSignedArea;
9
- exports.forEachSegmentInPolygon = forEachSegmentInPolygon;
10
- exports.modifyPolygonWindingDirectionPoints = modifyPolygonWindingDirectionPoints;
11
- exports.getPolygonWindingDirectionPoints = getPolygonWindingDirectionPoints;
12
- exports.getPolygonSignedAreaPoints = getPolygonSignedAreaPoints;
13
- exports.forEachSegmentInPolygonPoints = forEachSegmentInPolygonPoints;
14
- exports.WINDING = void 0;
15
-
16
- var _core = require("@math.gl/core");
17
-
18
- var WINDING = {
19
- CLOCKWISE: 1,
20
- COUNTER_CLOCKWISE: -1
21
- };
22
- exports.WINDING = WINDING;
23
-
24
- function modifyPolygonWindingDirection(points, direction) {
25
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
26
- var windingDirection = getPolygonWindingDirection(points, options);
27
-
28
- if (windingDirection !== direction) {
29
- reversePolygon(points, options);
30
- return true;
31
- }
32
-
33
- return false;
34
- }
35
-
36
- function getPolygonWindingDirection(points) {
37
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
38
- return Math.sign(getPolygonSignedArea(points, options));
39
- }
40
-
41
- function getPolygonSignedArea(points) {
42
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
43
- var _options$start = options.start,
44
- start = _options$start === void 0 ? 0 : _options$start,
45
- _options$end = options.end,
46
- end = _options$end === void 0 ? points.length : _options$end;
47
- var dim = options.size || 2;
48
- var area = 0;
49
-
50
- for (var i = start, j = end - dim; i < end; i += dim) {
51
- area += (points[i] - points[j]) * (points[i + 1] + points[j + 1]);
52
- j = i;
53
- }
54
-
55
- return area / 2;
56
- }
57
-
58
- function forEachSegmentInPolygon(points, visitor) {
59
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
60
- var _options$start2 = options.start,
61
- start = _options$start2 === void 0 ? 0 : _options$start2,
62
- _options$end2 = options.end,
63
- end = _options$end2 === void 0 ? points.length : _options$end2,
64
- _options$size = options.size,
65
- size = _options$size === void 0 ? 2 : _options$size,
66
- isClosed = options.isClosed;
67
- var numPoints = (end - start) / size;
68
-
69
- for (var i = 0; i < numPoints - 1; ++i) {
70
- visitor(points[start + i * size], points[start + i * size + 1], points[start + (i + 1) * size], points[start + (i + 1) * size + 1], i, i + 1);
71
- }
72
-
73
- var endPointIndex = start + (numPoints - 1) * size;
74
- var isClosedEx = isClosed || (0, _core.equals)(points[start], points[endPointIndex]) && (0, _core.equals)(points[start + 1], points[endPointIndex + 1]);
75
-
76
- if (!isClosedEx) {
77
- visitor(points[endPointIndex], points[endPointIndex + 1], points[start], points[start + 1], numPoints - 1, 0);
78
- }
79
- }
80
-
81
- function reversePolygon(points, options) {
82
- var _options$start3 = options.start,
83
- start = _options$start3 === void 0 ? 0 : _options$start3,
84
- _options$end3 = options.end,
85
- end = _options$end3 === void 0 ? points.length : _options$end3,
86
- _options$size2 = options.size,
87
- size = _options$size2 === void 0 ? 2 : _options$size2;
88
- var numPoints = (end - start) / size;
89
- var numSwaps = Math.floor(numPoints / 2);
90
-
91
- for (var i = 0; i < numSwaps; ++i) {
92
- var b1 = start + i * size;
93
- var b2 = start + (numPoints - 1 - i) * size;
94
-
95
- for (var j = 0; j < size; ++j) {
96
- var tmp = points[b1 + j];
97
- points[b1 + j] = points[b2 + j];
98
- points[b2 + j] = tmp;
99
- }
100
- }
101
- }
102
-
103
- function modifyPolygonWindingDirectionPoints(points, direction) {
104
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
105
- var currentDirection = getPolygonWindingDirectionPoints(points, options);
106
-
107
- if (currentDirection !== direction) {
108
- points.reverse();
109
- return true;
110
- }
111
-
112
- return false;
113
- }
114
-
115
- function getPolygonWindingDirectionPoints(points) {
116
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
117
- return Math.sign(getPolygonSignedAreaPoints(points, options));
118
- }
119
-
120
- function getPolygonSignedAreaPoints(points) {
121
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
122
- var _options$start4 = options.start,
123
- start = _options$start4 === void 0 ? 0 : _options$start4,
124
- _options$end4 = options.end,
125
- end = _options$end4 === void 0 ? points.length : _options$end4;
126
- var area = 0;
127
-
128
- for (var i = start, j = end - 1; i < end; ++i) {
129
- area += (points[i][0] - points[j][0]) * (points[i][1] + points[j][1]);
130
- j = i;
131
- }
132
-
133
- return area / 2;
134
- }
135
-
136
- function forEachSegmentInPolygonPoints(points, visitor) {
137
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
138
- var _options$start5 = options.start,
139
- start = _options$start5 === void 0 ? 0 : _options$start5,
140
- _options$end5 = options.end,
141
- end = _options$end5 === void 0 ? points.length : _options$end5,
142
- isClosed = options.isClosed;
143
-
144
- for (var i = start; i < end - 1; ++i) {
145
- visitor(points[i], points[i + 1], i, i + 1);
146
- }
147
-
148
- var isClosedEx = isClosed || (0, _core.equals)(points[end - 1], points[0]);
149
-
150
- if (!isClosedEx) {
151
- visitor(points[end - 1], points[0], end - 1, 0);
152
- }
153
- }
154
- //# sourceMappingURL=polygon-utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/polygon-utils.ts"],"names":["WINDING","CLOCKWISE","COUNTER_CLOCKWISE","modifyPolygonWindingDirection","points","direction","options","windingDirection","getPolygonWindingDirection","reversePolygon","Math","sign","getPolygonSignedArea","start","end","length","dim","size","area","i","j","forEachSegmentInPolygon","visitor","isClosed","numPoints","endPointIndex","isClosedEx","numSwaps","floor","b1","b2","tmp","modifyPolygonWindingDirectionPoints","currentDirection","getPolygonWindingDirectionPoints","reverse","getPolygonSignedAreaPoints","forEachSegmentInPolygonPoints"],"mappings":";;;;;;;;;;;;;;;AAEA;;AAGO,IAAMA,OAAO,GAAG;AACrBC,EAAAA,SAAS,EAAE,CADU;AAErBC,EAAAA,iBAAiB,EAAE,CA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AOA,KAAP,gCAAe,CAAf;AAAA,sBAAmDP,OAAnD,CAAkBQ,GAAlB;AAAA,MAAkBA,GAAlB,8BAAwBV,MAAM,CAACW,MAA/B;AAAA,MAAuCQ,QAAvC,GAAmDjB,OAAnD,CAAuCiB,QAAvC;;AACA,OAAK,IAAIJ,CAAC,GAAGN,KAAb,EAAoBM,CAAC,GAAGL,GAAG,GAAG,CAA9B,EAAiC,EAAEK,CAAnC,EAAsC;AACpCG,IAAAA,OAAO,CAAClB,MAAM,CAACe,CAAD,CAAP,EAAYf,MAAM,CAACe,CAAC,GAAG,CAAL,CAAlB,EAA2BA,CAA3B,EAA8BA,CAAC,GAAG,CAAlC,CAAP;AACD;;AAED,MAAMO,UAAU,GAAGH,QAAQ,IAAI,kBAAOnB,MAAM,CAACU,GAAG,GAAG,CAAP,CAAb,EAAwBV,MAAM,CAAC,CAAD,CAA9B,CAA/B;;AACA,MAAI,CAACsB,UAAL,EAAiB;AACfJ,IAAAA,OAAO,CAAClB,MAAM,CAACU,GAAG,GAAG,CAAP,CAAP,EAAkBV,MAAM,CAAC,CAAD,CAAxB,EAA6BU,GAAG,GAAG,CAAnC,EAAsC,CAAtC,CAAP;AACD;AACF","sourcesContent":["/* eslint-disable max-statements, max-depth, complexity, no-unused-expressions */\n\nimport {equals} from '@math.gl/core';\nimport type {NumericArray} from '@math.gl/core';\n\nexport const WINDING = {\n CLOCKWISE: 1,\n COUNTER_CLOCKWISE: -1\n} as const;\n\n/** Polygon representation where each point is represented as a separate array of positions. */\ntype PointsArray = NumericArray[];\n\n/** Segment visitor callback type for polygons defined with flat arrays, */\ntype SegmentVisitorFlat = (\n p1x: number,\n p1y: number,\n p2x: number,\n p2y: number,\n i1: number,\n i2: number\n) => void;\n\n/** Segment visitor callback type for polygons defined with array of points. */\nexport type SegmentVisitorPoints = (\n p1: NumericArray,\n p2: NumericArray,\n i1: number,\n i2: number\n) => void;\n\n/** Parameters of a polygon. */\ntype PolygonParams = {\n start?: number; // Start index of the polygon in the array of positions. Defaults to 0.\n end?: number; // End index of the polygon in the array of positions. Defaults to number of positions.\n size?: number; // Size of a point, 2 (XZ) or 3 (XYZ). Defaults to 2. Affects only polygons stored in flat arrays.\n isClosed?: boolean; // Indicates that the first point of the polygon is equal to the last point, and additional checks should be ommited.\n};\n\n/**\n * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.\n * Note: points are modified in-place.\n * @param points An array that represents points of the polygon.\n * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.\n * @param options Parameters of the polygon.\n * @return Returns true if the winding direction was changed.\n */\nexport function modifyPolygonWindingDirection(\n points: NumericArray,\n direction: number,\n options: PolygonParams = {}\n): boolean {\n const windingDirection = getPolygonWindingDirection(points, options);\n if (windingDirection !== direction) {\n reversePolygon(points, options);\n return true;\n }\n return false;\n}\n\n/**\n * Returns winding direction of the polygon.\n * @param points An array that represents points of the polygon.\n * @param options Parameters of the polygon.\n * @returns Winding direction of the polygon.\n */\nexport function getPolygonWindingDirection(\n points: NumericArray,\n options: PolygonParams = {}\n): number {\n return Math.sign(getPolygonSignedArea(points, options));\n}\n\n/**\n * Returns signed area of the polygon.\n * @param points An array that represents points of the polygon.\n * @param options Parameters of the polygon.\n * @returns Signed area of the polygon.\n * https://en.wikipedia.org/wiki/Shoelace_formula\n */\nexport function getPolygonSignedArea(points: NumericArray, options: PolygonParams = {}): number {\n const {start = 0, end = points.length} = options;\n const dim = options.size || 2;\n let area = 0;\n for (let i = start, j = end - dim; i < end; i += dim) {\n area += (points[i] - points[j]) * (points[i + 1] + points[j + 1]);\n j = i;\n }\n return area / 2;\n}\n\n/**\n * Calls the visitor callback for each segment in the polygon.\n * @param points An array that represents points of the polygon\n * @param visitor A callback to call for each segment.\n * @param options Parameters of the polygon.\n */\nexport function forEachSegmentInPolygon(\n points: NumericArray,\n visitor: SegmentVisitorFlat,\n options: PolygonParams = {}\n): void {\n const {start = 0, end = points.length, size = 2, isClosed} = options;\n\n const numPoints = (end - start) / size;\n for (let i = 0; i < numPoints - 1; ++i) {\n visitor(\n points[start + i * size],\n points[start + i * size + 1],\n points[start + (i + 1) * size],\n points[start + (i + 1) * size + 1],\n i,\n i + 1\n );\n }\n\n const endPointIndex = start + (numPoints - 1) * size;\n const isClosedEx =\n isClosed ||\n (equals(points[start], points[endPointIndex]) &&\n equals(points[start + 1], points[endPointIndex + 1]));\n\n if (!isClosedEx) {\n visitor(\n points[endPointIndex],\n points[endPointIndex + 1],\n points[start],\n points[start + 1],\n numPoints - 1,\n 0\n );\n }\n}\n\nfunction reversePolygon(\n points: NumericArray,\n options: {start?: number; end?: number; size?: number}\n): void {\n const {start = 0, end = points.length, size = 2} = options;\n\n const numPoints = (end - start) / size;\n const numSwaps = Math.floor(numPoints / 2);\n for (let i = 0; i < numSwaps; ++i) {\n const b1 = start + i * size;\n const b2 = start + (numPoints - 1 - i) * size;\n for (let j = 0; j < size; ++j) {\n const tmp = points[b1 + j];\n points[b1 + j] = points[b2 + j];\n points[b2 + j] = tmp;\n }\n }\n}\n\n/**\n * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.\n * Note: points are modified in-place.\n * @param points Array of points that represent the polygon.\n * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.\n * @param options Parameters of the polygon.\n * @return Returns true if the winding direction was changed.\n */\nexport function modifyPolygonWindingDirectionPoints(\n points: PointsArray,\n direction: number,\n options: PolygonParams = {}\n): boolean {\n const currentDirection = getPolygonWindingDirectionPoints(points, options);\n if (currentDirection !== direction) {\n points.reverse();\n return true;\n }\n return false;\n}\n\n/**\n * Returns winding direction of the polygon.\n * @param points Array of points that represent the polygon.\n * @param options Parameters of the polygon.\n * @returns Winding direction of the polygon.\n */\nexport function getPolygonWindingDirectionPoints(\n points: PointsArray,\n options: PolygonParams = {}\n): number {\n return Math.sign(getPolygonSignedAreaPoints(points, options));\n}\n\n/**\n * Returns signed area of the polygon.\n * @param points Array of points that represent the polygon.\n * @param options Parameters of the polygon.\n * @returns Signed area of the polygon.\n */\nexport function getPolygonSignedAreaPoints(\n points: PointsArray,\n options: PolygonParams = {}\n): number {\n // https://en.wikipedia.org/wiki/Shoelace_formula\n const {start = 0, end = points.length} = options;\n let area = 0;\n for (let i = start, j = end - 1; i < end; ++i) {\n area += (points[i][0] - points[j][0]) * (points[i][1] + points[j][1]);\n j = i;\n }\n return area / 2;\n}\n\n/**\n * Calls visitor callback for each segment in the polygon.\n * @param points Array of points that represent the polygon.\n * @param visitor A callback to call for each segment.\n * @param options Parameters of the polygon.\n */\nexport function forEachSegmentInPolygonPoints(\n points: PointsArray,\n visitor: SegmentVisitorPoints,\n options: PolygonParams = {}\n): void {\n const {start = 0, end = points.length, isClosed} = options;\n for (let i = start; i < end - 1; ++i) {\n visitor(points[i], points[i + 1], i, i + 1);\n }\n\n const isClosedEx = isClosed || equals(points[end - 1], points[0]);\n if (!isClosedEx) {\n visitor(points[end - 1], points[0], end - 1, 0);\n }\n}\n"],"file":"polygon-utils.js"}
@@ -1,79 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = void 0;
9
-
10
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
-
12
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
-
14
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
15
-
16
- var _core = require("@math.gl/core");
17
-
18
- var _polygonUtils = require("./polygon-utils");
19
-
20
- var Polygon = function () {
21
- function Polygon(points) {
22
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
23
- (0, _classCallCheck2.default)(this, Polygon);
24
- (0, _defineProperty2.default)(this, "points", void 0);
25
- (0, _defineProperty2.default)(this, "isFlatArray", void 0);
26
- (0, _defineProperty2.default)(this, "options", void 0);
27
- this.points = points;
28
- this.isFlatArray = !(0, _core.isArray)(points[0]);
29
- this.options = {
30
- start: options.start || 0,
31
- end: options.end || points.length,
32
- size: options.size || 2,
33
- isClosed: options.isClosed
34
- };
35
- Object.freeze(this);
36
- }
37
-
38
- (0, _createClass2.default)(Polygon, [{
39
- key: "getSignedArea",
40
- value: function getSignedArea() {
41
- if (this.isFlatArray) return (0, _polygonUtils.getPolygonSignedArea)(this.points, this.options);
42
- return (0, _polygonUtils.getPolygonSignedAreaPoints)(this.points, this.options);
43
- }
44
- }, {
45
- key: "getArea",
46
- value: function getArea() {
47
- return Math.abs(this.getSignedArea());
48
- }
49
- }, {
50
- key: "getWindingDirection",
51
- value: function getWindingDirection() {
52
- return Math.sign(this.getSignedArea());
53
- }
54
- }, {
55
- key: "forEachSegment",
56
- value: function forEachSegment(visitor) {
57
- if (this.isFlatArray) {
58
- (0, _polygonUtils.forEachSegmentInPolygon)(this.points, function (x1, y1, x2, y2, i1, i2) {
59
- visitor([x1, y1], [x2, y2], i1, i2);
60
- }, this.options);
61
- } else {
62
- (0, _polygonUtils.forEachSegmentInPolygonPoints)(this.points, visitor, this.options);
63
- }
64
- }
65
- }, {
66
- key: "modifyWindingDirection",
67
- value: function modifyWindingDirection(direction) {
68
- if (this.isFlatArray) {
69
- return (0, _polygonUtils.modifyPolygonWindingDirection)(this.points, direction, this.options);
70
- }
71
-
72
- return (0, _polygonUtils.modifyPolygonWindingDirectionPoints)(this.points, direction, this.options);
73
- }
74
- }]);
75
- return Polygon;
76
- }();
77
-
78
- exports.default = Polygon;
79
- //# sourceMappingURL=polygon.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/polygon.ts"],"names":["Polygon","points","options","isFlatArray","start","end","length","size","isClosed","Object","freeze","Math","abs","getSignedArea","sign","visitor","x1","y1","x2","y2","i1","i2","direction"],"mappings":";;;;;;;;;;;;;;;AACA;;AAIA;;IAgBqBA,O;AAKnB,mBAAYC,MAAZ,EAA6E;AAAA,QAA9BC,OAA8B,uEAAJ,EAAI;A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CAAuBoB,SAAvB,EAAmD;AACjD,UAAI,KAAKnB,WAAT,EAAsB;AACpB,eAAO,iDAA8B,KAAKF,MAAnC,EAA2DqB,SAA3D,EAAsE,KAAKpB,OAA3E,CAAP;AACD;;AACD,aAAO,uDAAoC,KAAKD,MAAzC,EAA+DqB,SAA/D,EAA0E,KAAKpB,OAA/E,CAAP;AACD","sourcesContent":["/* eslint-disable no-undef, no-console */\nimport {isArray} from '@math.gl/core';\nimport type {SegmentVisitorPoints} from './polygon-utils';\nimport type {NumericArray} from '@math.gl/core';\n\nimport {\n getPolygonSignedArea,\n forEachSegmentInPolygon,\n modifyPolygonWindingDirection,\n getPolygonSignedAreaPoints,\n forEachSegmentInPolygonPoints,\n modifyPolygonWindingDirectionPoints\n} from './polygon-utils';\n\nexport type PolygonOptions = {\n start?: number;\n end?: number;\n size?: number;\n isClosed?: boolean;\n};\n\nexport default class Polygon {\n points: NumericArray | number[][];\n isFlatArray: boolean;\n options: PolygonOptions;\n\n constructor(points: NumericArray | number[][], options: PolygonOptions = {}) {\n this.points = points;\n this.isFlatArray = !isArray(points[0]);\n\n this.options = {\n start: options.start || 0,\n end: options.end || points.length,\n size: options.size || 2,\n isClosed: options.isClosed\n };\n\n Object.freeze(this);\n }\n\n /**\n * Returns signed area of the polygon.\n * @returns Signed area of the polygon.\n */\n getSignedArea(): number {\n if (this.isFlatArray) return getPolygonSignedArea(this.points as NumericArray, this.options);\n\n return getPolygonSignedAreaPoints(this.points as number[][], this.options);\n }\n\n /**\n * Returns absolute area of the polygon.\n * @returns Absolute area of the polygon.\n */\n getArea(): number {\n return Math.abs(this.getSignedArea());\n }\n\n /**\n * Returns winding direction of the polygon.\n * @returns Winding direction of the polygon. 1 is for clockwise, -1 for counterclockwise winding direction.\n */\n getWindingDirection(): number {\n return Math.sign(this.getSignedArea());\n }\n\n /**\n * Calls the visitor callback for each segment in the polygon.\n * @param visitor A callback to call for each segment.\n */\n forEachSegment(visitor: SegmentVisitorPoints): void {\n if (this.isFlatArray) {\n forEachSegmentInPolygon(\n this.points as NumericArray,\n // eslint-disable-next-line max-params\n (x1, y1, x2, y2, i1, i2) => {\n // TODO @igorDykhta original visitor uses arrays for each point, but with flat arrays performance degrades if we allocate points for each segment\n visitor([x1, y1], [x2, y2], i1, i2);\n },\n this.options\n );\n } else {\n forEachSegmentInPolygonPoints(this.points as number[][], visitor, this.options);\n }\n }\n\n /**\n * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.\n * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.\n * @return Returns true if the winding direction was changed.\n */\n modifyWindingDirection(direction: number): boolean {\n if (this.isFlatArray) {\n return modifyPolygonWindingDirection(this.points as NumericArray, direction, this.options);\n }\n return modifyPolygonWindingDirectionPoints(this.points as number[][], direction, this.options);\n }\n}\n"],"file":"polygon.js"}
package/dist/es5/utils.js DELETED
@@ -1,54 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.push = push;
7
- exports.copy = copy;
8
- exports.getPointAtIndex = getPointAtIndex;
9
-
10
- function push(target, source) {
11
- var size = source.length;
12
- var startIndex = target.length;
13
-
14
- if (startIndex > 0) {
15
- var isDuplicate = true;
16
-
17
- for (var i = 0; i < size; i++) {
18
- if (target[startIndex - size + i] !== source[i]) {
19
- isDuplicate = false;
20
- break;
21
- }
22
- }
23
-
24
- if (isDuplicate) {
25
- return false;
26
- }
27
- }
28
-
29
- for (var _i = 0; _i < size; _i++) {
30
- target[startIndex + _i] = source[_i];
31
- }
32
-
33
- return true;
34
- }
35
-
36
- function copy(target, source) {
37
- var size = source.length;
38
-
39
- for (var i = 0; i < size; i++) {
40
- target[i] = source[i];
41
- }
42
- }
43
-
44
- function getPointAtIndex(positions, index, size, offset) {
45
- var out = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
46
- var startI = offset + index * size;
47
-
48
- for (var i = 0; i < size; i++) {
49
- out[i] = positions[startI + i];
50
- }
51
-
52
- return out;
53
- }
54
- //# sourceMappingURL=utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/utils.ts"],"names":["push","target","source","size","length","startIndex","isDuplicate","i","copy","getPointAtIndex","positions","index","offset","out","startI"],"mappings":";;;;;;;;;AAEO,SAASA,IAAT,CAAcC,MAAd,EAAgCC,MAAhC,EAA2D;AAChE,MAAMC,IAAI,GAAGD,MAAM,CAACE,MAApB;AACA,MAAMC,UAAU,GAAGJ,MAAM,CAACG,MAA1B;;AAGA,MAAIC,UAAU,GAAG,CAAjB,EAAoB;AAClB,QAAIC,WAAW,GAAG,IAAlB;;AACA,SAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGJ,IAApB,EAA0BI,CAAC,EAA3B,EAA+B;AAC7B,UAAIN,MAAM,CAACI,UAAU,GAAGF,IAAb,GAAoBI,CAArB,CAAN,KAAkCL,MAAM,CAACK,CAAD,CAA5C,EAAiD;AAC/CD,QAAAA,WAAW,GAAG,KAAd;AACA;AACD;AACF;;AACD,QAAIA,WAAJ,EAAiB;AACf,aAAO,KAAP;AACD;AACF;;AAED,OAAK,IAAIC,EAAC,GAAG,CAAb,EAAgBA,EAAC,GAAGJ,IAApB,EAA0BI,EAAC,EAA3B,EAA+B;AAC7BN,IAAAA,MAAM,CAACI,UAAU,GAAGE,EAAd,CAAN,GAAyBL,MAAM,CAACK,EAAD,CAA/B;AACD;;AACD,SAAO,IAAP;AACD;;AAEM,SAASC,IAAT,CAAcP,MAAd,EAAgCC,MAAhC,EAAsE;AAC3E,MAAMC,IAAI,GAAGD,MAAM,CAACE,MAApB;;AACA,OAAK,IAAIG,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGJ,IAApB,EAA0BI,CAAC,EAA3B,EAA+B;AAC7BN,IAAAA,MAAM,CAACM,CAAD,CAAN,GAAYL,MAAM,CAACK,CAAD,CAAlB;AACD;AACF;;AAEM,SAASE,eAAT,CACLC,SADK,EAELC,KAFK,EAGLR,IAHK,EAILS,MAJK,EAMK;AAAA,MADVC,GACU,uEADM,EACN;AACV,MAAMC,MAAM,GAAGF,MAAM,GAAGD,KAAK,GAAGR,IAAhC;;AACA,OAAK,IAAII,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGJ,IAApB,EAA0BI,CAAC,EAA3B,EAA+B;AAC7BM,IAAAA,GAAG,CAACN,CAAD,CAAH,GAASG,SAAS,CAACI,MAAM,GAAGP,CAAV,CAAlB;AACD;;AACD,SAAOM,GAAP;AACD","sourcesContent":["import type {NumericArray} from '@math.gl/core';\n\nexport function push(target: number[], source: number[]): boolean {\n const size = source.length;\n const startIndex = target.length;\n\n // dedupe, if source is the same point as the last vertex\n if (startIndex > 0) {\n let isDuplicate = true;\n for (let i = 0; i < size; i++) {\n if (target[startIndex - size + i] !== source[i]) {\n isDuplicate = false;\n break;\n }\n }\n if (isDuplicate) {\n return false;\n }\n }\n\n for (let i = 0; i < size; i++) {\n target[startIndex + i] = source[i];\n }\n return true;\n}\n\nexport function copy(target: number[], source: Readonly<NumericArray>): void {\n const size = source.length;\n for (let i = 0; i < size; i++) {\n target[i] = source[i];\n }\n}\n\nexport function getPointAtIndex(\n positions: Readonly<NumericArray>,\n index: number,\n size: number,\n offset: number,\n out: number[] = []\n): number[] {\n const startI = offset + index * size;\n for (let i = 0; i < size; i++) {\n out[i] = positions[startI + i];\n }\n return out;\n}\n"],"file":"utils.js"}