@math.gl/polygon 3.6.0 → 3.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es5/polygon-utils.js +4 -6
- package/dist/es5/polygon-utils.js.map +1 -1
- package/dist/esm/polygon-utils.js +4 -7
- package/dist/esm/polygon-utils.js.map +1 -1
- package/dist/polygon-utils.d.ts +4 -4
- package/dist/polygon-utils.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/polygon-utils.ts +4 -4
- package/dist/cut-by-grid.js +0 -214
- package/dist/cut-by-mercator-bounds.js +0 -145
- package/dist/earcut.js +0 -611
- package/dist/index.js +0 -8
- package/dist/lineclip.js +0 -178
- package/dist/polygon-utils.js +0 -140
- package/dist/polygon.js +0 -67
- package/dist/utils.js +0 -34
|
@@ -15,14 +15,12 @@ exports.WINDING = void 0;
|
|
|
15
15
|
|
|
16
16
|
var _core = require("@math.gl/core");
|
|
17
17
|
|
|
18
|
-
var WINDING
|
|
18
|
+
var WINDING = {
|
|
19
|
+
CLOCKWISE: 1,
|
|
20
|
+
COUNTER_CLOCKWISE: -1
|
|
21
|
+
};
|
|
19
22
|
exports.WINDING = WINDING;
|
|
20
23
|
|
|
21
|
-
(function (WINDING) {
|
|
22
|
-
WINDING[WINDING["CLOCKWISE"] = 1] = "CLOCKWISE";
|
|
23
|
-
WINDING[WINDING["COUNTER_CLOCKWISE"] = -1] = "COUNTER_CLOCKWISE";
|
|
24
|
-
})(WINDING || (exports.WINDING = WINDING = {}));
|
|
25
|
-
|
|
26
24
|
function modifyPolygonWindingDirection(points, direction) {
|
|
27
25
|
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
28
26
|
var windingDirection = getPolygonWindingDirection(points, options);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/polygon-utils.ts"],"names":["WINDING","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;;IAGYA,O;;;WAAAA,O;AAAAA,EAAAA,O,CAAAA,O;AAAAA,EAAAA,O,CAAAA,O;GAAAA,O,uBAAAA,O;;AA0CL,SAASC,6BAAT,CACLC,MADK,EAELC,SAFK,EAII;AAAA,MADTC,OACS,uEADgB,EAChB;AACT,MAAMC,gBAAgB,GAAGC,0BAA0B,CAACJ,MAAD,EAASE,OAAT,CAAnD;;AACA,MAAIC,gBAAgB,KAAKF,SAAzB,EAAoC;AAClCI,IAAAA,cAAc,CAACL,MAAD,EAASE,OAAT,CAAd;AACA,WAAO,IAAP;AACD;;AACD,SAAO,KAAP;AACD;;AAQM,SAASE,0BAAT,CACLJ,MADK,EAGG;AAAA,MADRE,OACQ,uEADiB,EACjB;AACR,SAAOI,IAAI,CAACC,IAAL,CAAUC,oBAAoB,CAACR,MAAD,EAASE,OAAT,CAA9B,CAAP;AACD;;AASM,SAASM,oBAAT,CAA8BR,MAA9B,EAAyF;AAAA,MAArCE,OAAqC,uEAAZ,EAAY;AAC9F,uBAAyCA,OAAzC,CAAOO,KAAP;AAAA,MAAOA,KAAP,+BAAe,CAAf;AAAA,qBAAyCP,OAAzC,CAAkBQ,GAAlB;AAAA,MAAkBA,GAAlB,6BAAwBV,MAAM,CAACW,MAA/B;AACA,MAAMC,GAAG,GAAGV,OAAO,CAACW,IAAR,IAAgB,CAA5B;AACA,MAAIC,IAAI,GAAG,CAAX;;AACA,OAAK,IAAIC,CAAC,GAAGN,KAAR,EAAeO,CAAC,GAAGN,GAAG,GAAGE,GAA9B,EAAmCG,CAAC,GAAGL,GAAvC,EAA4CK,CAAC,IAAIH,GAAjD,EA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B,KAAiChB,MAAM,CAACe,CAAD,CAAN,CAAU,CAAV,IAAef,MAAM,CAACgB,CAAD,CAAN,CAAU,CAAV,CAAhD,CAAR;AACAA,IAAAA,CAAC,GAAGD,CAAJ;AACD;;AACD,SAAOD,IAAI,GAAG,CAAd;AACD;;AAQM,SAASmB,6BAAT,CACLjC,MADK,EAELkB,OAFK,EAIC;AAAA,MADNhB,OACM,uEADmB,EACnB;AACN,wBAAmDA,OAAnD,CAAOO,KAAP;AAAA,MAAOA,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 enum WINDING {\n CLOCKWISE = 1,\n COUNTER_CLOCKWISE = -1\n}\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
|
+
{"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","getPolygonSignedAreaP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AAG,CAAd;AACD;;AAQM,SAASmB,6BAAT,CACLjC,MADK,EAELkB,OAFK,EAIC;AAAA,MADNhB,OACM,uEADmB,EACnB;AACN,wBAAmDA,OAAnD,CAAOO,KAAP;AAAA,MAAOA,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,11 +1,8 @@
|
|
|
1
1
|
import { equals } from '@math.gl/core';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
WINDING[WINDING["COUNTER_CLOCKWISE"] = -1] = "COUNTER_CLOCKWISE";
|
|
7
|
-
})(WINDING || (WINDING = {}));
|
|
8
|
-
|
|
2
|
+
export const WINDING = {
|
|
3
|
+
CLOCKWISE: 1,
|
|
4
|
+
COUNTER_CLOCKWISE: -1
|
|
5
|
+
};
|
|
9
6
|
export function modifyPolygonWindingDirection(points, direction, options = {}) {
|
|
10
7
|
const windingDirection = getPolygonWindingDirection(points, options);
|
|
11
8
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/polygon-utils.ts"],"names":["equals","WINDING","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,SAAQA,MAAR,QAAqB,eAArB;AAGA,WAAYC,OAAZ;;WAAYA,O;AAAAA,EAAAA,O,CAAAA,O;AAAAA,EAAAA,O,CAAAA,O;GAAAA,O,KAAAA,O;;AA0CZ,OAAO,SAASC,6BAAT,CACLC,MADK,EAELC,SAFK,EAGLC,OAAsB,GAAG,EAHpB,EAII;AACT,QAAMC,gBAAgB,GAAGC,0BAA0B,CAACJ,MAAD,EAASE,OAAT,CAAnD;;AACA,MAAIC,gBAAgB,KAAKF,SAAzB,EAAoC;AAClCI,IAAAA,cAAc,CAACL,MAAD,EAASE,OAAT,CAAd;AACA,WAAO,IAAP;AACD;;AACD,SAAO,KAAP;AACD;AAQD,OAAO,SAASE,0BAAT,CACLJ,MADK,EAELE,OAAsB,GAAG,EAFpB,EAGG;AACR,SAAOI,IAAI,CAACC,IAAL,CAAUC,oBAAoB,CAACR,MAAD,EAASE,OAAT,CAA9B,CAAP;AACD;AASD,OAAO,SAASM,oBAAT,CAA8BR,MAA9B,EAAoDE,OAAsB,GAAG,EAA7E,EAAyF;AAC9F,QAAM;AAACO,IAAAA,KAAK,GAAG,CAAT;AAAYC,IAAAA,GAAG,GAAGV,MAAM,CAACW;AAAzB,MAAmCT,OAAzC;AACA,QAAMU,GAAG,GAAGV,OAAO,CAACW,IAAR,IAAgB,CAA5B;AACA,MAAIC,IAAI,GAAG,CAAX;;AACA,OAAK,IAAIC,CAAC,GAAGN,KAAR,EAAeO,CAAC,GAAGN,GAAG,GAAGE,GAA9B,EAAmCG,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6BAAT,CACLjC,MADK,EAELkB,OAFK,EAGLhB,OAAsB,GAAG,EAHpB,EAIC;AACN,QAAM;AAACO,IAAAA,KAAK,GAAG,CAAT;AAAYC,IAAAA,GAAG,GAAGV,MAAM,CAACW,MAAzB;AAAiCQ,IAAAA;AAAjC,MAA6CjB,OAAnD;;AACA,OAAK,IAAIa,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,QAAMO,UAAU,GAAGH,QAAQ,IAAItB,MAAM,CAACG,MAAM,CAACU,GAAG,GAAG,CAAP,CAAP,EAAkBV,MAAM,CAAC,CAAD,CAAxB,CAArC;;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 enum WINDING {\n CLOCKWISE = 1,\n COUNTER_CLOCKWISE = -1\n}\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
|
+
{"version":3,"sources":["../../src/polygon-utils.ts"],"names":["equals","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,SAAQA,MAAR,QAAqB,eAArB;AAGA,OAAO,MAAMC,OAAO,GAAG;AACrBC,EAAAA,SAAS,EAAE,CADU;AAErBC,EAAAA,iBAAiB,EAAE,CAAC;AAFC,CAAhB;AA0CP,OAAO,SAASC,6BAAT,CACLC,MADK,EAELC,SAFK,EAGLC,OAAsB,GAAG,EAHpB,EAII;AACT,QAAMC,gBAAg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,GAAG,CAAP,CAAP,EAAkBV,MAAM,CAAC,CAAD,CAAxB,CAArC;;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"}
|
package/dist/polygon-utils.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { NumericArray } from '@math.gl/core';
|
|
2
|
-
export declare
|
|
3
|
-
CLOCKWISE
|
|
4
|
-
COUNTER_CLOCKWISE
|
|
5
|
-
}
|
|
2
|
+
export declare const WINDING: {
|
|
3
|
+
readonly CLOCKWISE: 1;
|
|
4
|
+
readonly COUNTER_CLOCKWISE: -1;
|
|
5
|
+
};
|
|
6
6
|
/** Polygon representation where each point is represented as a separate array of positions. */
|
|
7
7
|
declare type PointsArray = NumericArray[];
|
|
8
8
|
/** Segment visitor callback type for polygons defined with flat arrays, */
|
|
@@ -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,
|
|
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;AAElC,2EAA2E;AAC3E,aAAK,kBAAkB,GAAG,CACxB,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,KACP,IAAI,CAAC;AAEV,+EAA+E;AAC/E,oBAAY,oBAAoB,GAAG,CACjC,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,YAAY,EAChB,EAAE,EAAE,MAAM,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@@
|
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
-
"version": "3.6.
|
|
8
|
+
"version": "3.6.3",
|
|
9
9
|
"keywords": [
|
|
10
10
|
"webgl",
|
|
11
11
|
"javascript",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"src"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@math.gl/core": "3.6.
|
|
28
|
+
"@math.gl/core": "3.6.3"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "0efab394df9babad7ed93027c1003f30528b2090"
|
|
31
31
|
}
|
package/src/polygon-utils.ts
CHANGED
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
import {equals} from '@math.gl/core';
|
|
4
4
|
import type {NumericArray} from '@math.gl/core';
|
|
5
5
|
|
|
6
|
-
export
|
|
7
|
-
CLOCKWISE
|
|
8
|
-
COUNTER_CLOCKWISE
|
|
9
|
-
}
|
|
6
|
+
export const WINDING = {
|
|
7
|
+
CLOCKWISE: 1,
|
|
8
|
+
COUNTER_CLOCKWISE: -1
|
|
9
|
+
} as const;
|
|
10
10
|
|
|
11
11
|
/** Polygon representation where each point is represented as a separate array of positions. */
|
|
12
12
|
type PointsArray = NumericArray[];
|
package/dist/cut-by-grid.js
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
/* eslint-disable max-statements, max-depth, complexity, no-unused-expressions */
|
|
2
|
-
import { bitCode, intersect } from './lineclip';
|
|
3
|
-
import { getPointAtIndex, copy, push } from './utils';
|
|
4
|
-
export function cutPolylineByGrid(positions, options) {
|
|
5
|
-
const { size = 2, broken = false, gridResolution = 10, gridOffset = [0, 0], startIndex = 0, endIndex = positions.length } = options || {};
|
|
6
|
-
const numPoints = (endIndex - startIndex) / size;
|
|
7
|
-
let part = [];
|
|
8
|
-
const result = [part];
|
|
9
|
-
const a = getPointAtIndex(positions, 0, size, startIndex);
|
|
10
|
-
let b;
|
|
11
|
-
let codeB;
|
|
12
|
-
const cell = getGridCell(a, gridResolution, gridOffset, []);
|
|
13
|
-
const scratchPoint = [];
|
|
14
|
-
push(part, a);
|
|
15
|
-
for (let i = 1; i < numPoints; i++) {
|
|
16
|
-
b = getPointAtIndex(positions, i, size, startIndex, b);
|
|
17
|
-
codeB = bitCode(b, cell);
|
|
18
|
-
while (codeB) {
|
|
19
|
-
// find the intersection with the current cell
|
|
20
|
-
intersect(a, b, codeB, cell, scratchPoint);
|
|
21
|
-
const codeAlt = bitCode(scratchPoint, cell);
|
|
22
|
-
if (codeAlt) {
|
|
23
|
-
intersect(a, scratchPoint, codeAlt, cell, scratchPoint);
|
|
24
|
-
codeB = codeAlt;
|
|
25
|
-
}
|
|
26
|
-
push(part, scratchPoint);
|
|
27
|
-
// move to the next cell
|
|
28
|
-
copy(a, scratchPoint);
|
|
29
|
-
moveToNeighborCell(cell, gridResolution, codeB);
|
|
30
|
-
if (broken && part.length > size) {
|
|
31
|
-
part = [];
|
|
32
|
-
result.push(part);
|
|
33
|
-
push(part, a);
|
|
34
|
-
}
|
|
35
|
-
codeB = bitCode(b, cell);
|
|
36
|
-
}
|
|
37
|
-
push(part, b);
|
|
38
|
-
copy(a, b);
|
|
39
|
-
}
|
|
40
|
-
return broken ? result : result[0];
|
|
41
|
-
}
|
|
42
|
-
const TYPE_INSIDE = 0;
|
|
43
|
-
const TYPE_BORDER = 1;
|
|
44
|
-
function concatInPlace(arr1, arr2) {
|
|
45
|
-
for (let i = 0; i < arr2.length; i++) {
|
|
46
|
-
arr1.push(arr2[i]);
|
|
47
|
-
}
|
|
48
|
-
return arr1;
|
|
49
|
-
}
|
|
50
|
-
export function cutPolygonByGrid(positions, holeIndices = null, options) {
|
|
51
|
-
if (!positions.length) {
|
|
52
|
-
// input is empty
|
|
53
|
-
return [];
|
|
54
|
-
}
|
|
55
|
-
const { size = 2, gridResolution = 10, gridOffset = [0, 0], edgeTypes = false } = options || {};
|
|
56
|
-
const result = [];
|
|
57
|
-
const queue = [
|
|
58
|
-
{
|
|
59
|
-
pos: positions,
|
|
60
|
-
types: edgeTypes ? new Array(positions.length / size).fill(TYPE_BORDER) : null,
|
|
61
|
-
holes: holeIndices || []
|
|
62
|
-
}
|
|
63
|
-
];
|
|
64
|
-
const bbox = [[], []];
|
|
65
|
-
// @ts-ignore
|
|
66
|
-
let cell = [];
|
|
67
|
-
// Recursively bisect polygon until every part fit in a single grid cell
|
|
68
|
-
while (queue.length) {
|
|
69
|
-
const { pos, types, holes } = queue.shift();
|
|
70
|
-
// Get the bounding box of the outer polygon
|
|
71
|
-
getBoundingBox(pos, size, holes[0] || pos.length, bbox);
|
|
72
|
-
cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
|
|
73
|
-
const code = bitCode(bbox[1], cell);
|
|
74
|
-
if (code) {
|
|
75
|
-
// Split the outer ring at the boundary
|
|
76
|
-
let parts = bisectPolygon(pos, types, size, 0, holes[0] || pos.length, cell, code);
|
|
77
|
-
const polygonLow = { pos: parts[0].pos, types: parts[0].types, holes: [] };
|
|
78
|
-
const polygonHigh = { pos: parts[1].pos, types: parts[1].types, holes: [] };
|
|
79
|
-
queue.push(polygonLow, polygonHigh);
|
|
80
|
-
// Split each hole at the boundary
|
|
81
|
-
for (let i = 0; i < holes.length; i++) {
|
|
82
|
-
parts = bisectPolygon(pos, types, size, holes[i], holes[i + 1] || pos.length, cell, code);
|
|
83
|
-
if (parts[0]) {
|
|
84
|
-
polygonLow.holes.push(polygonLow.pos.length);
|
|
85
|
-
polygonLow.pos = concatInPlace(polygonLow.pos, parts[0].pos);
|
|
86
|
-
if (edgeTypes) {
|
|
87
|
-
polygonLow.types = concatInPlace(polygonLow.types, parts[0].types);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (parts[1]) {
|
|
91
|
-
polygonHigh.holes.push(polygonHigh.pos.length);
|
|
92
|
-
polygonHigh.pos = concatInPlace(polygonHigh.pos, parts[1].pos);
|
|
93
|
-
if (edgeTypes) {
|
|
94
|
-
polygonHigh.types = concatInPlace(polygonHigh.types, parts[1].types);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
// Polygon fits in a single cell, no more processing required
|
|
101
|
-
const polygon = { positions: pos };
|
|
102
|
-
if (edgeTypes) {
|
|
103
|
-
polygon.edgeTypes = types;
|
|
104
|
-
}
|
|
105
|
-
if (holes.length) {
|
|
106
|
-
polygon.holeIndices = holes;
|
|
107
|
-
}
|
|
108
|
-
result.push(polygon);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return result;
|
|
112
|
-
}
|
|
113
|
-
// edgeTypes:
|
|
114
|
-
// TYPE_BORDER - edge from the original polygon
|
|
115
|
-
// TYPE_INSIDE - inside the original polygon
|
|
116
|
-
// eslint-disable-next-line max-params
|
|
117
|
-
function bisectPolygon(positions, edgeTypes, size, startIndex, endIndex, bbox, edge) {
|
|
118
|
-
const numPoints = (endIndex - startIndex) / size;
|
|
119
|
-
const resultLow = [];
|
|
120
|
-
const resultHigh = [];
|
|
121
|
-
const typesLow = [];
|
|
122
|
-
const typesHigh = [];
|
|
123
|
-
const scratchPoint = [];
|
|
124
|
-
let p;
|
|
125
|
-
let side;
|
|
126
|
-
let type;
|
|
127
|
-
const prev = getPointAtIndex(positions, numPoints - 1, size, startIndex);
|
|
128
|
-
let prevSide = Math.sign(edge & 8 ? prev[1] - bbox[3] : prev[0] - bbox[2]);
|
|
129
|
-
let prevType = edgeTypes && edgeTypes[numPoints - 1];
|
|
130
|
-
let lowPointCount = 0;
|
|
131
|
-
let highPointCount = 0;
|
|
132
|
-
for (let i = 0; i < numPoints; i++) {
|
|
133
|
-
p = getPointAtIndex(positions, i, size, startIndex, p);
|
|
134
|
-
side = Math.sign(edge & 8 ? p[1] - bbox[3] : p[0] - bbox[2]);
|
|
135
|
-
type = edgeTypes && edgeTypes[startIndex / size + i];
|
|
136
|
-
// if segment goes through the boundary, add an intersection
|
|
137
|
-
if (side && prevSide && prevSide !== side) {
|
|
138
|
-
intersect(prev, p, edge, bbox, scratchPoint);
|
|
139
|
-
push(resultLow, scratchPoint) && typesLow.push(prevType);
|
|
140
|
-
push(resultHigh, scratchPoint) && typesHigh.push(prevType);
|
|
141
|
-
}
|
|
142
|
-
if (side <= 0) {
|
|
143
|
-
push(resultLow, p) && typesLow.push(type);
|
|
144
|
-
lowPointCount -= side;
|
|
145
|
-
}
|
|
146
|
-
else if (typesLow.length) {
|
|
147
|
-
typesLow[typesLow.length - 1] = TYPE_INSIDE;
|
|
148
|
-
}
|
|
149
|
-
if (side >= 0) {
|
|
150
|
-
push(resultHigh, p) && typesHigh.push(type);
|
|
151
|
-
highPointCount += side;
|
|
152
|
-
}
|
|
153
|
-
else if (typesHigh.length) {
|
|
154
|
-
typesHigh[typesHigh.length - 1] = TYPE_INSIDE;
|
|
155
|
-
}
|
|
156
|
-
copy(prev, p);
|
|
157
|
-
prevSide = side;
|
|
158
|
-
prevType = type;
|
|
159
|
-
}
|
|
160
|
-
return [
|
|
161
|
-
lowPointCount ? { pos: resultLow, types: edgeTypes && typesLow } : null,
|
|
162
|
-
highPointCount ? { pos: resultHigh, types: edgeTypes && typesHigh } : null
|
|
163
|
-
];
|
|
164
|
-
}
|
|
165
|
-
function getGridCell(p, gridResolution, gridOffset, out) {
|
|
166
|
-
const left = Math.floor((p[0] - gridOffset[0]) / gridResolution) * gridResolution + gridOffset[0];
|
|
167
|
-
const bottom = Math.floor((p[1] - gridOffset[1]) / gridResolution) * gridResolution + gridOffset[1];
|
|
168
|
-
out[0] = left;
|
|
169
|
-
out[1] = bottom;
|
|
170
|
-
out[2] = left + gridResolution;
|
|
171
|
-
out[3] = bottom + gridResolution;
|
|
172
|
-
return out;
|
|
173
|
-
}
|
|
174
|
-
function moveToNeighborCell(cell, gridResolution, edge) {
|
|
175
|
-
if (edge & 8) {
|
|
176
|
-
// top
|
|
177
|
-
cell[1] += gridResolution;
|
|
178
|
-
cell[3] += gridResolution;
|
|
179
|
-
}
|
|
180
|
-
else if (edge & 4) {
|
|
181
|
-
// bottom
|
|
182
|
-
cell[1] -= gridResolution;
|
|
183
|
-
cell[3] -= gridResolution;
|
|
184
|
-
}
|
|
185
|
-
else if (edge & 2) {
|
|
186
|
-
// right
|
|
187
|
-
cell[0] += gridResolution;
|
|
188
|
-
cell[2] += gridResolution;
|
|
189
|
-
}
|
|
190
|
-
else if (edge & 1) {
|
|
191
|
-
// left
|
|
192
|
-
cell[0] -= gridResolution;
|
|
193
|
-
cell[2] -= gridResolution;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
function getBoundingBox(positions, size, endIndex, out) {
|
|
197
|
-
let minX = Infinity;
|
|
198
|
-
let maxX = -Infinity;
|
|
199
|
-
let minY = Infinity;
|
|
200
|
-
let maxY = -Infinity;
|
|
201
|
-
for (let i = 0; i < endIndex; i += size) {
|
|
202
|
-
const x = positions[i];
|
|
203
|
-
const y = positions[i + 1];
|
|
204
|
-
minX = x < minX ? x : minX;
|
|
205
|
-
maxX = x > maxX ? x : maxX;
|
|
206
|
-
minY = y < minY ? y : minY;
|
|
207
|
-
maxY = y > maxY ? y : maxY;
|
|
208
|
-
}
|
|
209
|
-
out[0][0] = minX;
|
|
210
|
-
out[0][1] = minY;
|
|
211
|
-
out[1][0] = maxX;
|
|
212
|
-
out[1][1] = maxY;
|
|
213
|
-
return out;
|
|
214
|
-
}
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { cutPolylineByGrid, cutPolygonByGrid } from './cut-by-grid';
|
|
2
|
-
import { getPointAtIndex, push } from './utils';
|
|
3
|
-
// https://en.wikipedia.org/wiki/Web_Mercator_projection
|
|
4
|
-
const DEFAULT_MAX_LATITUDE = 85.051129;
|
|
5
|
-
/** https://user-images.githubusercontent.com/2059298/78465769-938b7a00-76ae-11ea-9b95-1f4c26425ab9.png */
|
|
6
|
-
export function cutPolylineByMercatorBounds(positions, options) {
|
|
7
|
-
const { size = 2, startIndex = 0, endIndex = positions.length, normalize = true } = options || {};
|
|
8
|
-
// Remap longitudes so that each segment takes the shorter path
|
|
9
|
-
const newPositions = positions.slice(startIndex, endIndex);
|
|
10
|
-
wrapLongitudesForShortestPath(newPositions, size, 0, endIndex - startIndex);
|
|
11
|
-
const parts = cutPolylineByGrid(newPositions, {
|
|
12
|
-
size,
|
|
13
|
-
broken: true,
|
|
14
|
-
gridResolution: 360,
|
|
15
|
-
gridOffset: [-180, -180]
|
|
16
|
-
});
|
|
17
|
-
if (normalize) {
|
|
18
|
-
// Each part is guaranteed to be in a single copy of the world
|
|
19
|
-
// Map longitudes back to [-180, 180]
|
|
20
|
-
for (const part of parts) {
|
|
21
|
-
shiftLongitudesIntoRange(part, size);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return parts;
|
|
25
|
-
}
|
|
26
|
-
/** https://user-images.githubusercontent.com/2059298/78465770-94241080-76ae-11ea-809a-6a8534dac1d9.png */
|
|
27
|
-
export function cutPolygonByMercatorBounds(positions, holeIndices = null, options) {
|
|
28
|
-
const { size = 2, normalize = true, edgeTypes = false } = options || {};
|
|
29
|
-
holeIndices = holeIndices || [];
|
|
30
|
-
const newPositions = [];
|
|
31
|
-
const newHoleIndices = [];
|
|
32
|
-
let srcStartIndex = 0;
|
|
33
|
-
let targetIndex = 0;
|
|
34
|
-
for (let ringIndex = 0; ringIndex <= holeIndices.length; ringIndex++) {
|
|
35
|
-
// srcStartIndex/srcEndIndex define the ring in the original positions
|
|
36
|
-
const srcEndIndex = holeIndices[ringIndex] || positions.length;
|
|
37
|
-
// targetStartIndex/targetIndex define the ring in newPositions
|
|
38
|
-
const targetStartIndex = targetIndex;
|
|
39
|
-
// In case the ring contains a pole (e.g. Antarctica), we'll have to insert vertices
|
|
40
|
-
// The insertion point is defined by the vertex closest to the pole
|
|
41
|
-
// Split the the ring by the insertion point when copying to newPositions
|
|
42
|
-
const splitIndex = findSplitIndex(positions, size, srcStartIndex, srcEndIndex);
|
|
43
|
-
for (let i = splitIndex; i < srcEndIndex; i++) {
|
|
44
|
-
newPositions[targetIndex++] = positions[i];
|
|
45
|
-
}
|
|
46
|
-
for (let i = srcStartIndex; i < splitIndex; i++) {
|
|
47
|
-
newPositions[targetIndex++] = positions[i];
|
|
48
|
-
}
|
|
49
|
-
// Remap longitudes so that each segment takes the shorter path
|
|
50
|
-
wrapLongitudesForShortestPath(newPositions, size, targetStartIndex, targetIndex);
|
|
51
|
-
// Handle the case when the ring contains a pole
|
|
52
|
-
insertPoleVertices(newPositions, size, targetStartIndex, targetIndex, options?.maxLatitude);
|
|
53
|
-
srcStartIndex = srcEndIndex;
|
|
54
|
-
newHoleIndices[ringIndex] = targetIndex;
|
|
55
|
-
}
|
|
56
|
-
newHoleIndices.pop();
|
|
57
|
-
const parts = cutPolygonByGrid(newPositions, newHoleIndices, {
|
|
58
|
-
size,
|
|
59
|
-
gridResolution: 360,
|
|
60
|
-
gridOffset: [-180, -180],
|
|
61
|
-
edgeTypes
|
|
62
|
-
});
|
|
63
|
-
if (normalize) {
|
|
64
|
-
// Each part is guaranteed to be in a single copy of the world
|
|
65
|
-
// Map longitudes back to [-180, 180]
|
|
66
|
-
for (const part of parts) {
|
|
67
|
-
// @ts-expect-error (mutates readonly array) May mutate newPositions, which is created by us
|
|
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
|
-
}
|