@math.gl/polygon 3.6.2 → 4.0.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 (56) hide show
  1. package/dist/cut-by-grid.d.ts +4 -1
  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.js +8 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/lineclip.d.ts +1 -1
  15. package/dist/lineclip.d.ts.map +1 -1
  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 +26 -4
  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} +1 -1
  25. package/dist/polygon.js.map +1 -0
  26. package/dist/{esm/utils.js → utils.js} +0 -0
  27. package/dist/utils.js.map +1 -0
  28. package/package.json +6 -5
  29. package/src/cut-by-grid.ts +26 -9
  30. package/src/earcut.ts +143 -77
  31. package/src/polygon-utils.ts +42 -8
  32. package/dist/es5/cut-by-grid.js +0 -276
  33. package/dist/es5/cut-by-grid.js.map +0 -1
  34. package/dist/es5/cut-by-mercator-bounds.js +0 -194
  35. package/dist/es5/cut-by-mercator-bounds.js.map +0 -1
  36. package/dist/es5/earcut.js +0 -540
  37. package/dist/es5/earcut.js.map +0 -1
  38. package/dist/es5/index.js +0 -104
  39. package/dist/es5/index.js.map +0 -1
  40. package/dist/es5/lineclip.js +0 -150
  41. package/dist/es5/lineclip.js.map +0 -1
  42. package/dist/es5/polygon-utils.js +0 -154
  43. package/dist/es5/polygon-utils.js.map +0 -1
  44. package/dist/es5/polygon.js +0 -79
  45. package/dist/es5/polygon.js.map +0 -1
  46. package/dist/es5/utils.js +0 -54
  47. package/dist/es5/utils.js.map +0 -1
  48. package/dist/esm/cut-by-grid.js.map +0 -1
  49. package/dist/esm/cut-by-mercator-bounds.js.map +0 -1
  50. package/dist/esm/earcut.js.map +0 -1
  51. package/dist/esm/index.js +0 -8
  52. package/dist/esm/index.js.map +0 -1
  53. package/dist/esm/lineclip.js.map +0 -1
  54. package/dist/esm/polygon-utils.js.map +0 -1
  55. package/dist/esm/polygon.js.map +0 -1
  56. package/dist/esm/utils.js.map +0 -1
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { default as Polygon } from "./polygon.js";
2
+ export { getPolygonSignedArea, getPolygonWindingDirection, forEachSegmentInPolygon, modifyPolygonWindingDirection, WINDING } from "./polygon-utils.js";
3
+ export { earcut } from "./earcut.js";
4
+ export { clipPolygon, clipPolyline } from "./lineclip.js";
5
+ export { cutPolygonByGrid, cutPolylineByGrid } from "./cut-by-grid.js";
6
+ export { cutPolylineByMercatorBounds, cutPolygonByMercatorBounds } from "./cut-by-mercator-bounds.js";
7
+ export { default as _Polygon } from "./polygon.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":["default","Polygon","getPolygonSignedArea","getPolygonWindingDirection","forEachSegmentInPolygon","modifyPolygonWindingDirection","WINDING","earcut","clipPolygon","clipPolyline","cutPolygonByGrid","cutPolylineByGrid","cutPolylineByMercatorBounds","cutPolygonByMercatorBounds","_Polygon"],"mappings":"SAAQA,OAAO,IAAIC,O;SAGjBC,oB,EACAC,0B,EACAC,uB,EACAC,6B,EACAC,O;SAGMC,M;SAEAC,W,EAAaC,Y;SAEbC,gB,EAAkBC,iB;SAElBC,2B,EAA6BC,0B;SAG7Bb,OAAO,IAAIc,Q","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,5 +1,5 @@
1
1
  import type { NumericArray } from '@math.gl/core';
2
- export declare type BoundingBox = [number, number, number, number];
2
+ export type BoundingBox = [number, number, number, number];
3
3
  /**
4
4
  * Cohen-Sutherland line clipping algorithm, adapted to efficiently
5
5
  * handle polylines rather than just segments
@@ -1 +1 @@
1
- {"version":3,"file":"lineclip.d.ts","sourceRoot":"","sources":["../src/lineclip.ts"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,eAAe,CAAC;AAEhD,oBAAY,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAE3D;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,SAAS,EAAE,QAAQ,CAAC,YAAY,CAAC,EACjC,IAAI,EAAE,WAAW,EACjB,OAAO,CAAC,EAAE;IACR,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,GACA,MAAM,EAAE,EAAE,CA0DZ;AAED;;;GAGG;AACH,wBAAgB,WAAW,CACzB,SAAS,EAAE,QAAQ,CAAC,Y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GACA,MAAM,EAAE,CAsCV;AAED,2EAA2E;AAE3E,wBAAgB,SAAS,CACvB,CAAC,EAAE,MAAM,EAAE,EACX,CAAC,EAAE,MAAM,EAAE,EACX,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,WAAW,EACjB,GAAG,GAAE,MAAM,EAAO,GACjB,MAAM,EAAE,CA6BV;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,WAAW,GAAG,MAAM,CAY9D"}
@@ -1,4 +1,4 @@
1
- import { push, copy, getPointAtIndex } from './utils';
1
+ import { push, copy, getPointAtIndex } from "./utils.js";
2
2
  export function clipPolyline(positions, bbox, options) {
3
3
  const {
4
4
  size = 2,
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/lineclip.ts"],"names":["push","copy","getPointAtIndex","clipPolyline","positions","bbox","options","size","startIndex","endIndex","length","numPoints","result","part","a","b","codeA","codeB","lastCode","i","bitCode","intersect","clipPolygon","p","prev","inside","prevInside","edge","out","t","snap","code"],"mappings":"SAwBQA,I,EAAMC,I,EAAMC,e;AASpB,OAAO,SAASC,YAAT,CACLC,SADK,EAELC,IAFK,EAGLC,OAHK,EAQO;AACZ,QAAM;AAACC,IAAAA,IAAI,GAAG,CAAR;AAAWC,IAAAA,UAAU,GAAG,CAAxB;AAA2BC,IAAAA,QAAQ,GAAGL,SAAS,CAACM;AAAhD,MAA0DJ,OAAO,IAAI,EAA3E;AACA,QAAMK,SAAS,GAAG,CAACF,QAAQ,GAAGD,UAAZ,IAA0BD,IAA5C;AACA,QAAMK,MAAkB,GAAG,EAA3B;AACA,MAAIC,IAAc,GAAG,EAArB;AACA,MAAIC,CAAJ;AACA,MAAIC,CAAJ;AACA,MAAIC,KAAa,GAAG,CAAC,CAArB;AACA,MAAIC,KAAJ;AACA,MAAIC,QAAJ;;AAEA,OAAK,IAAIC,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGR,SAApB,EAA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,MAAIR,CAAC,CAAC,CAAD,CAAD,GAAOlB,IAAI,CAAC,CAAD,CAAf,EAAoB0B,IAAI,IAAI,CAAR,CAApB,KAEK,IAAIR,CAAC,CAAC,CAAD,CAAD,GAAOlB,IAAI,CAAC,CAAD,CAAf,EAAoB0B,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"}
@@ -4,17 +4,38 @@ export declare const WINDING: {
4
4
  readonly COUNTER_CLOCKWISE: -1;
5
5
  };
6
6
  /** Polygon representation where each point is represented as a separate array of positions. */
7
- declare type PointsArray = NumericArray[];
7
+ type PointsArray = NumericArray[];
8
8
  /** Segment visitor callback type for polygons defined with flat arrays, */
9
- declare type SegmentVisitorFlat = (p1x: number, p1y: number, p2x: number, p2y: number, i1: number, i2: number) => void;
9
+ type SegmentVisitorFlat = (p1x: number, p1y: number, p2x: number, p2y: number, i1: number, i2: number) => void;
10
10
  /** Segment visitor callback type for polygons defined with array of points. */
11
- export declare type SegmentVisitorPoints = (p1: NumericArray, p2: NumericArray, i1: number, i2: number) => void;
11
+ export type SegmentVisitorPoints = (p1: NumericArray, p2: NumericArray, i1: number, i2: number) => void;
12
+ export type Plane2D = 'xy' | 'yz' | 'xz';
12
13
  /** Parameters of a polygon. */
13
- declare type PolygonParams = {
14
+ type PolygonParams = {
15
+ /**
16
+ * Start index of the polygon in the array of positions.
17
+ * @default `0`
18
+ */
14
19
  start?: number;
20
+ /**
21
+ * End index of the polygon in the array of positions.
22
+ * @default number of positions
23
+ */
15
24
  end?: number;
25
+ /**
26
+ * Size of a point, 2 (XZ) or 3 (XYZ). Affects only polygons stored in flat arrays.
27
+ * @default `2`
28
+ */
16
29
  size?: number;
30
+ /**
31
+ * Indicates that the first point of the polygon is equal to the last point, and additional checks should be ommited.
32
+ */
17
33
  isClosed?: boolean;
34
+ /**
35
+ * The 2D projection plane on which to calculate the area of a 3D polygon.
36
+ * @default `'xy'`
37
+ */
38
+ plane?: Plane2D;
18
39
  };
19
40
  /**
20
41
  * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
@@ -32,6 +53,7 @@ export declare function modifyPolygonWindingDirection(points: NumericArray, dire
32
53
  * @returns Winding direction of the polygon.
33
54
  */
34
55
  export declare function getPolygonWindingDirection(points: NumericArray, options?: PolygonParams): number;
56
+ export declare const DimIndex: Record<string, number>;
35
57
  /**
36
58
  * Returns signed area of the polygon.
37
59
  * @param points An array that represents points of the polygon.
@@ -1 +1 @@
1
- {"version":3,"file":"polygon-utils.d.ts","sourceRoot":"","sources":["../src/polygon-utils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,eAAe,CAAC;AAEhD,eAAO,MAAM,OAAO;;;CAGV,CAAC;AAEX,+FAA+F;AAC/F,aAAK,WAAW,GAAG,YAAY,EAAE,CAAC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;;;;;;;GAOG;AACH,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,YAAY,EACpB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAOT;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,YAAY,EACpB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAER;AAED,eAAO,MAAM,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAIlC,CAAC;AAEX;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CAY9F;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,kBAAkB,EAC3B,OAAO,GAAE,aAAkB,GAC1B,IAAI,CA+BN;AAqBD;;;;;;;GAOG;AACH,wBAAgB,mCAAmC,CACjD,MAAM,EAAE,WAAW,EACnB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAOT;AAED;;;;;GAKG;AACH,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,WAAW,EACnB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAER;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,WAAW,EACnB,OAAO,GAAE,aAAkB,GAC1B,MAAM,CAYR;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,oBAAoB,EAC7B,OAAO,GAAE,aAAkB,GAC1B,IAAI,CAUN"}
@@ -16,16 +16,24 @@ export function modifyPolygonWindingDirection(points, direction, options = {}) {
16
16
  export function getPolygonWindingDirection(points, options = {}) {
17
17
  return Math.sign(getPolygonSignedArea(points, options));
18
18
  }
19
+ export const DimIndex = {
20
+ x: 0,
21
+ y: 1,
22
+ z: 2
23
+ };
19
24
  export function getPolygonSignedArea(points, options = {}) {
20
25
  const {
21
26
  start = 0,
22
- end = points.length
27
+ end = points.length,
28
+ plane = 'xy'
23
29
  } = options;
24
30
  const dim = options.size || 2;
25
31
  let area = 0;
32
+ const i0 = DimIndex[plane[0]];
33
+ const i1 = DimIndex[plane[1]];
26
34
 
27
35
  for (let i = start, j = end - dim; i < end; i += dim) {
28
- area += (points[i] - points[j]) * (points[i + 1] + points[j + 1]);
36
+ area += (points[i + i0] - points[j + i0]) * (points[i + i1] + points[j + i1]);
29
37
  j = i;
30
38
  }
31
39
 
@@ -89,12 +97,15 @@ export function getPolygonWindingDirectionPoints(points, options = {}) {
89
97
  export function getPolygonSignedAreaPoints(points, options = {}) {
90
98
  const {
91
99
  start = 0,
92
- end = points.length
100
+ end = points.length,
101
+ plane = 'xy'
93
102
  } = options;
94
103
  let area = 0;
104
+ const i0 = DimIndex[plane[0]];
105
+ const i1 = DimIndex[plane[1]];
95
106
 
96
107
  for (let i = start, j = end - 1; i < end; ++i) {
97
- area += (points[i][0] - points[j][0]) * (points[i][1] + points[j][1]);
108
+ area += (points[i][i0] - points[j][i0]) * (points[i][i1] + points[j][i1]);
98
109
  j = i;
99
110
  }
100
111
 
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/polygon-utils.ts"],"names":["equals","WINDING","CLOCKWISE","COUNTER_CLOCKWISE","modifyPolygonWindingDirection","points","direction","options","windingDirection","getPolygonWindingDirection","reversePolygon","Math","sign","getPolygonSignedArea","DimIndex","x","y","z","start","end","length","plane","dim","size","area","i0","i1","i","j","forEachSegmentInPolygon","visitor","isClosed","numPoints","endPointIndex","isClosedEx","numSwaps","floor","b1","b2","tmp","modifyPolygonWindingDirectionPoints","currentDirection","getPolygonWindingDirectionPoints","reverse","getPolygonSignedAreaPoints","forEachSegmentInPolygonPoints"],"mappings":"AAEA,SAAQA,MAAR,QAAqB,eAArB;AAGA,OAAO,MAAMC,OAAO,GAAG;AACrBC,EAAAA,SAAS,EAAE,CADU;AAErB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AACsB,CAAD,CAAN,CAAUD,EAAV,IAAgBrB,MAAM,CAACuB,CAAD,CAAN,CAAUF,EAAV,CAAnD,CAAR;AACAE,IAAAA,CAAC,GAAGD,CAAJ;AACD;;AACD,SAAOH,IAAI,GAAG,CAAd;AACD;AAQD,OAAO,SAASqB,6BAAT,CACLxC,MADK,EAELyB,OAFK,EAGLvB,OAAsB,GAAG,EAHpB,EAIC;AACN,QAAM;AAACW,IAAAA,KAAK,GAAG,CAAT;AAAYC,IAAAA,GAAG,GAAGd,MAAM,CAACe,MAAzB;AAAiCW,IAAAA;AAAjC,MAA6CxB,OAAnD;;AACA,OAAK,IAAIoB,CAAC,GAAGT,KAAb,EAAoBS,CAAC,GAAGR,GAAG,GAAG,CAA9B,EAAiC,EAAEQ,CAAnC,EAAsC;AACpCG,IAAAA,OAAO,CAACzB,MAAM,CAACsB,CAAD,CAAP,EAAYtB,MAAM,CAACsB,CAAC,GAAG,CAAL,CAAlB,EAA2BA,CAA3B,EAA8BA,CAAC,GAAG,CAAlC,CAAP;AACD;;AAED,QAAMO,UAAU,GAAGH,QAAQ,IAAI/B,MAAM,CAACK,MAAM,CAACc,GAAG,GAAG,CAAP,CAAP,EAAkBd,MAAM,CAAC,CAAD,CAAxB,CAArC;;AACA,MAAI,CAAC6B,UAAL,EAAiB;AACfJ,IAAAA,OAAO,CAACzB,MAAM,CAACc,GAAG,GAAG,CAAP,CAAP,EAAkBd,MAAM,CAAC,CAAD,CAAxB,EAA6Bc,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\nexport type Plane2D = 'xy' | 'yz' | 'xz';\n\n/** Parameters of a polygon. */\ntype PolygonParams = {\n /**\n * Start index of the polygon in the array of positions.\n * @default `0`\n */\n start?: number;\n /**\n * End index of the polygon in the array of positions.\n * @default number of positions\n */\n end?: number;\n /**\n * Size of a point, 2 (XZ) or 3 (XYZ). Affects only polygons stored in flat arrays.\n * @default `2`\n */\n size?: number;\n /**\n * Indicates that the first point of the polygon is equal to the last point, and additional checks should be ommited.\n */\n isClosed?: boolean;\n /**\n * The 2D projection plane on which to calculate the area of a 3D polygon.\n * @default `'xy'`\n */\n plane?: Plane2D;\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\nexport const DimIndex: Record<string, number> = {\n x: 0,\n y: 1,\n z: 2\n} as const;\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, plane = 'xy'} = options;\n const dim = options.size || 2;\n let area = 0;\n const i0 = DimIndex[plane[0]];\n const i1 = DimIndex[plane[1]];\n\n for (let i = start, j = end - dim; i < end; i += dim) {\n area += (points[i + i0] - points[j + i0]) * (points[i + i1] + points[j + i1]);\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, plane = 'xy'} = options;\n let area = 0;\n const i0 = DimIndex[plane[0]];\n const i1 = DimIndex[plane[1]];\n\n for (let i = start, j = end - 1; i < end; ++i) {\n area += (points[i][i0] - points[j][i0]) * (points[i][i1] + points[j][i1]);\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"}
package/dist/polygon.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { SegmentVisitorPoints } from './polygon-utils';
2
2
  import type { NumericArray } from '@math.gl/core';
3
- export declare type PolygonOptions = {
3
+ export type PolygonOptions = {
4
4
  start?: number;
5
5
  end?: number;
6
6
  size?: number;
@@ -1 +1 @@
1
- {"version":3,"file":"polygon.d.ts","sourceRoot":"","sources":["../src/polygon.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,oBAAoB,EAAC,MAAM,iBAAiB,CAAC;AAC1D,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,eAAe,CAAC;AAWhD,oBAAY,cAAc,GAAG;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,OAAO;IAC1B,MAAM,EAAE,YAAY,GAAG,MAAM,EAAE,EAAE,CAAC;IAClC,WAAW,EAAE,OAAO,CAAC;IACrB,OAAO,EAAE,cAAc,CAAC;gBAEZ,MAAM,EAAE,YAAY,GAAG,MAAM,EAAE,EAAE,EAAE,OAAO,GAAE,cAAmB;IAc3E;;;OAGG;IACH,aAAa,IAAI,MAAM;IAMvB;;;OAGG;IACH,OAAO,IAAI,MAAM;IAIjB;;;OAGG;IACH,mBAAmB,IAAI,MAAM;IAI7B;;;OAGG;IACH,cAAc,CAAC,OAAO,EAAE,oBAAoB,GAAG,IAAI;IAgBnD;;;;OAIG;IACH,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;CAMnD"}
1
+ {"version":3,"file":"polygon.d.ts","sourceRoot":"","sources":["../src/polygon.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,oBAAoB,EAAC,MAAM,iBAAiB,CAAC;AAC1D,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,eAAe,CAAC;AAWhD,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,OAAO;IAC1B,MAAM,EAAE,YAAY,GAAG,MAAM,EAAE,EAAE,CAAC;IAClC,WAAW,EAAE,OAAO,CAAC;IACrB,OAAO,EAAE,cAAc,CAAC;gBAEZ,MAAM,EAAE,YAAY,GAAG,MAAM,EAAE,EAAE,EAAE,OAAO,GAAE,cAAmB;IAc3E;;;OAGG;IACH,aAAa,IAAI,MAAM;IAMvB;;;OAGG;IACH,OAAO,IAAI,MAAM;IAIjB;;;OAGG;IACH,mBAAmB,IAAI,MAAM;IAI7B;;;OAGG;IACH,cAAc,CAAC,OAAO,EAAE,oBAAoB,GAAG,IAAI;IAgBnD;;;;OAIG;IACH,sBAAsB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;CAMnD"}
@@ -1,6 +1,6 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
2
  import { isArray } from '@math.gl/core';
3
- import { getPolygonSignedArea, forEachSegmentInPolygon, modifyPolygonWindingDirection, getPolygonSignedAreaPoints, forEachSegmentInPolygonPoints, modifyPolygonWindingDirectionPoints } from './polygon-utils';
3
+ import { getPolygonSignedArea, forEachSegmentInPolygon, modifyPolygonWindingDirection, getPolygonSignedAreaPoints, forEachSegmentInPolygonPoints, modifyPolygonWindingDirectionPoints } from "./polygon-utils.js";
4
4
  export default class Polygon {
5
5
  constructor(points, options = {}) {
6
6
  _defineProperty(this, "points", void 0);
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/polygon.ts"],"names":["isArray","getPolygonSignedArea","forEachSegmentInPolygon","modifyPolygonWindingDirection","getPolygonSignedAreaPoints","forEachSegmentInPolygonPoints","modifyPolygonWindingDirectionPoints","Polygon","constructor","points","options","isFlatArray","start","end","length","size","isClosed","Object","freeze","getSignedArea","getArea","Math","abs","getWindingDirection","sign","forEachSegment","visitor","x1","y1","x2","y2","i1","i2","modifyWindingDirection","direction"],"mappings":";AACA,SAAQA,OAAR,QAAsB,eAAtB;SAKEC,oB,EACAC,uB,EACAC,6B,EACAC,0B,EACAC,6B,EACAC,mC;AAUF,eAAe,MAAMC,OAAN,CAAc;AAK3BC,EAAAA,WAAW,CAACC,MAAD,EAAoCC,OAAuB,GAAG,EAA9D,EAAkE;AAAA;;AAAA;;AAAA;;AAC3E,SAAKD,MAAL,GAAcA,MAAd;AACA,SAAKE,WAAL,GAAmB,CAACX,OAAO,CAACS,MAAM,CAAC,CAAD,CAAP,CAA3B;AAEA,SAAKC,OAAL,GAAe;AACbE,MAAAA,KAAK,EAAEF,OAAO,CAACE,KAAR,IAAiB,CADX;AAEbC,MAAAA,GAAG,EAAEH,OAAO,CAACG,GAAR,IAAeJ,MAAM,CAACK,MAFd;AAGbC,MAAAA,IAAI,EAAEL,OAAO,CAACK,IAAR,IAAgB,CAHT;AAIbC,MAAAA,QAAQ,EAAEN,OAAO,CAACM;AAJL,KAAf;AAOAC,IAAAA,MAAM,CAACC,MAAP,CAAc,IAAd;AACD;;AAMDC,EAAAA,aAAa,GAAW;AACtB,QAAI,KAAKR,WAAT,EAAsB,OAAOV,oBAAoB,CAAC,KAAKQ,MAAN,EAA8B,KAAKC,OAAnC,CAA3B;AAEtB,WAAON,0BAA0B,CAAC,KAAKK,MAAN,EAA4B,KAAKC,OAAjC,CAAjC;AACD;;AAMDU,EAAAA,OAAO,GAAW;AAChB,WAAOC,IAAI,CAACC,GAAL,CAAS,KAAKH,aAAL,EAAT,CAAP;AACD;;AAMDI,EAAAA,mBAAmB,GAAW;AAC5B,WAAOF,IAAI,CAACG,IAAL,CAAU,KAAKL,aAAL,EAAV,CAAP;AACD;;AAMDM,EAAAA,cAAc,CAACC,OAAD,EAAsC;AAClD,QAAI,KAAKf,WAAT,EAAsB;AACpBT,MAAAA,uBAAuB,CACrB,KAAKO,MADgB,EAGrB,CAACkB,EAAD,EAAKC,EAAL,EAASC,EAAT,EAAaC,EAAb,EAAiBC,EAAjB,EAAqBC,EAArB,KAA4B;AAE1BN,QAAAA,OAAO,CAAC,CAACC,EAAD,EAAKC,EAAL,CAAD,EAAW,CAACC,EAAD,EAAKC,EAAL,CAAX,EAAqBC,EAArB,EAAyBC,EAAzB,CAAP;AACD,OANoB,EAOrB,KAAKtB,OAPgB,CAAvB;AASD,KAVD,MAUO;AACLL,MAAAA,6BAA6B,CAAC,KAAKI,MAAN,EAA4BiB,OAA5B,EAAqC,KAAKhB,OAA1C,CAA7B;AACD;AACF;;AAODuB,EAAAA,sBAAsB,CAACC,SAAD,EAA6B;AACjD,QAAI,KAAKvB,WAAT,EAAsB;AACpB,aAAOR,6BAA6B,CAAC,KAAKM,MAAN,EAA8ByB,SAA9B,EAAyC,KAAKxB,OAA9C,CAApC;AACD;;AACD,WAAOJ,mCAAmC,CAAC,KAAKG,MAAN,EAA4ByB,SAA5B,EAAuC,KAAKxB,OAA5C,CAA1C;AACD;;AA3E0B","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"}
File without changes
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts"],"names":["push","target","source","size","length","startIndex","isDuplicate","i","copy","getPointAtIndex","positions","index","offset","out","startI"],"mappings":"AAEA,OAAO,SAASA,IAAT,CAAcC,MAAd,EAAgCC,MAAhC,EAA2D;AAChE,QAAMC,IAAI,GAAGD,MAAM,CAACE,MAApB;AACA,QAAMC,UAAU,GAAGJ,MAAM,CAACG,MAA1B;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ACD","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"}
package/package.json CHANGED
@@ -2,10 +2,11 @@
2
2
  "name": "@math.gl/polygon",
3
3
  "description": "Polygon/polyline processing utilities",
4
4
  "license": "MIT",
5
+ "type": "module",
5
6
  "publishConfig": {
6
7
  "access": "public"
7
8
  },
8
- "version": "3.6.2",
9
+ "version": "4.0.0-alpha.1",
9
10
  "keywords": [
10
11
  "webgl",
11
12
  "javascript",
@@ -18,14 +19,14 @@
18
19
  "url": "https://github.com/uber-web/math.gl.git"
19
20
  },
20
21
  "types": "dist/index.d.ts",
21
- "main": "dist/es5/index.js",
22
- "module": "dist/esm/index.js",
22
+ "main": "dist/index.cjs",
23
+ "module": "dist/index.js",
23
24
  "files": [
24
25
  "dist",
25
26
  "src"
26
27
  ],
27
28
  "dependencies": {
28
- "@math.gl/core": "3.6.2"
29
+ "@math.gl/core": "4.0.0-alpha.1"
29
30
  },
30
- "gitHead": "517015873bbd7143bfc4bb761e70071c7a86771b"
31
+ "gitHead": "66f872ea615a3ef81431595727cdf58dcadf3670"
31
32
  }
@@ -75,13 +75,23 @@ export function cutPolylineByGrid(
75
75
  const TYPE_INSIDE = 0;
76
76
  const TYPE_BORDER = 1;
77
77
 
78
- function concatInPlace(arr1: number[], arr2: number[]): number[] {
79
- for (let i = 0; i < arr2.length; i++) {
80
- arr1.push(arr2[i]);
81
- }
82
- return arr1;
83
- }
78
+ /** Internal helper type during cutting, name TBD */
79
+ type PolygonCut = {
80
+ pos: Readonly<NumericArray>;
81
+ types: number[];
82
+ holes: Readonly<NumericArray>;
83
+ };
84
84
 
85
+ /** Internal helper type during cutting, name TBD */
86
+ type MutablePolygonCut = {
87
+ pos: number[];
88
+ types: number[];
89
+ holes: number[];
90
+ };
91
+
92
+ /**
93
+ * Cuts a polygon by a pre-defined grid
94
+ */
85
95
  export function cutPolygonByGrid(
86
96
  positions: Readonly<NumericArray>,
87
97
  holeIndices: Readonly<NumericArray> | null = null,
@@ -98,7 +108,7 @@ export function cutPolygonByGrid(
98
108
  }
99
109
  const {size = 2, gridResolution = 10, gridOffset = [0, 0], edgeTypes = false} = options || {};
100
110
  const result: Polygon[] = [];
101
- const queue: {pos: Readonly<NumericArray>; types: number[]; holes: Readonly<NumericArray>}[] = [
111
+ const queue: PolygonCut[] = [
102
112
  {
103
113
  pos: positions,
104
114
  types: edgeTypes ? (new Array(positions.length / size).fill(TYPE_BORDER) as number[]) : null,
@@ -121,8 +131,8 @@ export function cutPolygonByGrid(
121
131
  if (code) {
122
132
  // Split the outer ring at the boundary
123
133
  let parts = bisectPolygon(pos, types, size, 0, holes[0] || pos.length, cell, code);
124
- const polygonLow = {pos: parts[0].pos, types: parts[0].types, holes: []};
125
- const polygonHigh = {pos: parts[1].pos, types: parts[1].types, holes: []};
134
+ const polygonLow: MutablePolygonCut = {pos: parts[0].pos, types: parts[0].types, holes: []};
135
+ const polygonHigh: MutablePolygonCut = {pos: parts[1].pos, types: parts[1].types, holes: []};
126
136
  queue.push(polygonLow, polygonHigh);
127
137
 
128
138
  // Split each hole at the boundary
@@ -290,3 +300,10 @@ function getBoundingBox(
290
300
  out[1][1] = maxY;
291
301
  return out;
292
302
  }
303
+
304
+ function concatInPlace(arr1: number[], arr2: number[]): number[] {
305
+ for (let i = 0; i < arr2.length; i++) {
306
+ arr1.push(arr2[i]);
307
+ }
308
+ return arr1;
309
+ }