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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/cut-by-grid.d.ts +21 -0
  2. package/dist/cut-by-grid.d.ts.map +1 -0
  3. package/dist/cut-by-grid.js +214 -0
  4. package/dist/cut-by-mercator-bounds.d.ts +17 -0
  5. package/dist/cut-by-mercator-bounds.d.ts.map +1 -0
  6. package/dist/cut-by-mercator-bounds.js +144 -0
  7. package/dist/{esm/earcut.d.ts → earcut.d.ts} +4 -2
  8. package/dist/earcut.d.ts.map +1 -0
  9. package/dist/earcut.js +611 -0
  10. package/dist/es5/cut-by-grid.js +29 -25
  11. package/dist/es5/cut-by-grid.js.map +1 -1
  12. package/dist/es5/cut-by-mercator-bounds.js +24 -19
  13. package/dist/es5/cut-by-mercator-bounds.js.map +1 -1
  14. package/dist/es5/earcut.js +11 -10
  15. package/dist/es5/earcut.js.map +1 -1
  16. package/dist/es5/index.js.map +1 -1
  17. package/dist/es5/lineclip.js +20 -16
  18. package/dist/es5/lineclip.js.map +1 -1
  19. package/dist/es5/polygon-utils.js +6 -4
  20. package/dist/es5/polygon-utils.js.map +1 -1
  21. package/dist/es5/polygon.js +5 -0
  22. package/dist/es5/polygon.js.map +1 -1
  23. package/dist/es5/utils.js.map +1 -1
  24. package/dist/esm/cut-by-grid.js +5 -5
  25. package/dist/esm/cut-by-grid.js.map +1 -1
  26. package/dist/esm/cut-by-mercator-bounds.js +5 -5
  27. package/dist/esm/cut-by-mercator-bounds.js.map +1 -1
  28. package/dist/esm/earcut.js +9 -10
  29. package/dist/esm/earcut.js.map +1 -1
  30. package/dist/esm/index.js.map +1 -1
  31. package/dist/esm/lineclip.js +5 -5
  32. package/dist/esm/lineclip.js.map +1 -1
  33. package/dist/esm/polygon-utils.js +7 -4
  34. package/dist/esm/polygon-utils.js.map +1 -1
  35. package/dist/esm/polygon.js +7 -0
  36. package/dist/esm/polygon.js.map +1 -1
  37. package/dist/esm/utils.js.map +1 -1
  38. package/dist/index.d.ts +8 -0
  39. package/dist/index.d.ts.map +1 -0
  40. package/dist/index.js +8 -0
  41. package/dist/lineclip.d.ts +30 -0
  42. package/dist/lineclip.d.ts.map +1 -0
  43. package/dist/lineclip.js +178 -0
  44. package/dist/{es5/polygon-utils.d.ts → polygon-utils.d.ts} +28 -64
  45. package/dist/polygon-utils.d.ts.map +1 -0
  46. package/dist/polygon-utils.js +140 -0
  47. package/dist/polygon.d.ts +41 -0
  48. package/dist/polygon.d.ts.map +1 -0
  49. package/dist/polygon.js +67 -0
  50. package/dist/utils.d.ts +5 -0
  51. package/dist/utils.d.ts.map +1 -0
  52. package/dist/utils.js +34 -0
  53. package/package.json +4 -3
  54. package/src/{cut-by-grid.js → cut-by-grid.ts} +79 -33
  55. package/src/{cut-by-mercator-bounds.js → cut-by-mercator-bounds.ts} +51 -22
  56. package/src/{earcut.js → earcut.ts} +26 -14
  57. package/src/{index.js → index.ts} +0 -0
  58. package/src/{lineclip.js → lineclip.ts} +61 -33
  59. package/src/polygon-utils.ts +228 -0
  60. package/src/polygon.ts +98 -0
  61. package/src/{utils.js → utils.ts} +11 -3
  62. package/dist/es5/cut-by-grid.d.ts +0 -24
  63. package/dist/es5/cut-by-mercator-bounds.d.ts +0 -23
  64. package/dist/es5/earcut.d.ts +0 -9
  65. package/dist/es5/lineclip.d.ts +0 -25
  66. package/dist/es5/polygon.d.ts +0 -36
  67. package/dist/esm/cut-by-grid.d.ts +0 -24
  68. package/dist/esm/cut-by-mercator-bounds.d.ts +0 -23
  69. package/dist/esm/lineclip.d.ts +0 -25
  70. package/dist/esm/polygon-utils.d.ts +0 -117
  71. package/dist/esm/polygon.d.ts +0 -36
  72. package/src/cut-by-grid.d.ts +0 -24
  73. package/src/cut-by-mercator-bounds.d.ts +0 -23
  74. package/src/earcut.d.ts +0 -9
  75. package/src/lineclip.d.ts +0 -25
  76. package/src/polygon-utils.d.ts +0 -117
  77. package/src/polygon-utils.js +0 -127
  78. package/src/polygon.d.ts +0 -36
  79. package/src/polygon.js +0 -66
@@ -1,36 +0,0 @@
1
- import {SegmentVisitorPoints} from './polygon-utils';
2
-
3
- export default class Polygon {
4
- constructor(points: number[], options: {start?: number, end?: number,size?: number, isClosed?: boolean});
5
-
6
- /**
7
- * Returns signed area of the polygon.
8
- * @returns Signed area of the polygon.
9
- */
10
- getSignedArea(): number;
11
-
12
- /**
13
- * Returns absolute area of the polygon.
14
- * @returns Absolute area of the polygon.
15
- */
16
- getArea(): number;
17
-
18
- /**
19
- * Returns winding direction of the polygon.
20
- * @returns Winding direction of the polygon. 1 is for clockwise, -1 for counterclockwise winding direction.
21
- */
22
- getWindingDirection(): number;
23
-
24
- /**
25
- * Calls the visitor callback for each segment in the polygon.
26
- * @param visitor A callback to call for each segment.
27
- */
28
- forEachSegment(SegmentVisitorPoints): void;
29
-
30
- /**
31
- * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
32
- * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
33
- * @return Returns true if the winding direction was changed.
34
- */
35
- modifyWindingDirection(direction: number): boolean;
36
- }
@@ -1,24 +0,0 @@
1
- type Polygon = {positions: Array<number>, holeIndices?: Array<number>, vertexTypes?: Array<Number>};
2
-
3
- export function cutPolylineByGrid(
4
- positions : Array<number>,
5
- options? : {
6
- size? : number,
7
- broken? : boolean,
8
- gridResolution? : number,
9
- gridOffset? : [number, number],
10
- startIndex? : number,
11
- endIndex? : number
12
- }
13
- ) : Array<number> | Array<Array<number>>;
14
-
15
- export function cutPolygonByGrid(
16
- positions : Array<number>,
17
- holeIndices : Array<number> | null,
18
- options? : {
19
- size? : number,
20
- gridResolution? : number,
21
- gridOffset? : [number, number],
22
- edgeTypes? : boolean
23
- }
24
- ) : Array<Polygon>;
@@ -1,23 +0,0 @@
1
- import {Polygon} from "./cut-by-grid";
2
-
3
- export function cutPolylineByMercatorBounds(
4
- positions : Array<number>,
5
- options? : {
6
- size? : number,
7
- startIndex? : number,
8
- endIndex? : number,
9
- normalize? : boolean
10
- }
11
- ) : Array<number>;
12
-
13
- export function cutPolygonByMercatorBounds(
14
- positions : Array<number>,
15
- holeIndices : Array<number> | null,
16
- options? : {
17
- size? : number,
18
- normalize? : boolean,
19
- maxLatitude?: number,
20
- edgeTypes? : boolean
21
- }
22
- ) : Array<Polygon>;
23
-
package/src/earcut.d.ts DELETED
@@ -1,9 +0,0 @@
1
- /**
2
- * Computes a triangulation of a polygon
3
- * @param positions a flat array of the vertex positions that define the polygon.
4
- * @param holeIndices an array of hole indices if any (e.g. [5, 8] for a 12-vertex input would mean one hole with vertices 5–7 and another with 8–11).
5
- * @param size the number of elements in each vertex. Size `2` will interpret `positions` as `[x0, y0, x1, y1, ...]` and size `3` will interpret `positions` as `[x0, y0, z0, x1, y1, z1, ...]`. Default `2`.
6
- * @param areas areas of outer polygon and holes as computed by `getPolygonSignedArea()`. Can be optionally supplied to speed up triangulation
7
- * @returns array of indices into the `positions` array that describes the triangulation of the polygon
8
- */
9
- export function earcut(positions: number[], holeIndices?: number[], size?: number, areas?: number[]): number[];
package/src/lineclip.d.ts DELETED
@@ -1,25 +0,0 @@
1
- type BoundingBox = [number, number, number, number];
2
-
3
- export function clipPolyline(
4
- positions : Array<number>,
5
- bbox : BoundingBox,
6
- options? : {
7
- size? : number,
8
- startIndex? : number,
9
- endIndex? : number
10
- }
11
- ) : Array<Array<number>>;
12
-
13
- export function clipPolygon(
14
- positions: Array<number>,
15
- bbox: BoundingBox,
16
- options? : {
17
- size? : number,
18
- startIndex? : number,
19
- endIndex? : number
20
- }
21
- ) : Array<number>;
22
-
23
- export function intersect(a : Array<number>, b : Array<number>, edge : number, bbox : BoundingBox, out? : Array<number>);
24
-
25
- export function bitCode(p: Array<number>, bbox : BoundingBox) : number;
@@ -1,117 +0,0 @@
1
- import {NumberArray} from '@math.gl/core/';
2
-
3
- export const WINDING: {
4
- CLOCKWISE: number;
5
- COUNTER_CLOCKWISE: number;
6
- };
7
-
8
- // Polygon representation where each point is represented as a separate array of positions.
9
- type PointsArray = NumberArray[];
10
-
11
- // Segment visitor callback type for polygons defined with flat arrays,
12
- type SegmentVisitorFlat = (
13
- p1x: number,
14
- p1y: number,
15
- p2x: number,
16
- p2y: number,
17
- i1: number,
18
- i2: number
19
- ) => void;
20
-
21
- // Segment visitor callback type for polygons defined with array of points.
22
- export type SegmentVisitorPoints = (p1: NumberArray, p2: NumberArray, i1: number, i2: number) => void;
23
-
24
- // Parameters of a polygon.
25
- type PolygonParams = {
26
- start?: number; // Start index of the polygon in the array of positions. Defaults to 0.
27
- end?: number; // End index of the polygon in the array of positions. Defaults to number of positions.
28
- size?: number; // Size of a point, 2 (XZ) or 3 (XYZ). Defaults to 2. Affects only polygons stored in flat arrays.
29
- isClosed?: boolean; // Indicates that the first point of the polygon is equal to the last point, and additional checks should be ommited.
30
- };
31
-
32
- /**
33
- * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
34
- * Note: points are modified in-place.
35
- * @param points An array that represents points of the polygon.
36
- * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
37
- * @param options Parameters of the polygon.
38
- * @return Returns true if the winding direction was changed.
39
- */
40
- export function modifyPolygonWindingDirection(
41
- points: NumberArray,
42
- direction: number,
43
- options?: PolygonParams
44
- ): boolean;
45
-
46
- /**
47
- * Returns winding direction of the polygon.
48
- * @param points An array that represents points of the polygon.
49
- * @param options Parameters of the polygon.
50
- * @returns Winding direction of the polygon.
51
- */
52
- export function getPolygonWindingDirection(points: NumberArray, options?: PolygonParams): number;
53
-
54
- /**
55
- * Returns signed area of the polygon.
56
- * @param points An array that represents points of the polygon.
57
- * @param options Parameters of the polygon.
58
- * @returns Signed area of the polygon.
59
- */
60
- export function getPolygonSignedArea(points: NumberArray, options?: PolygonParams): number;
61
-
62
- /**
63
- * Calls the visitor callback for each segment in the polygon.
64
- * @param points An array that represents points of the polygon
65
- * @param visitor A callback to call for each segment.
66
- * @param options Parameters of the polygon.
67
- */
68
- export function forEachSegmentInPolygon(
69
- points: NumberArray,
70
- visitor: SegmentVisitorFlat,
71
- options?: PolygonParams
72
- ): void;
73
-
74
- /**
75
- * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
76
- * Note: points are modified in-place.
77
- * @param points Array of points that represent the polygon.
78
- * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
79
- * @param options Parameters of the polygon.
80
- * @return Returns true if the winding direction was changed.
81
- */
82
- export function modifyPolygonWindingDirectionPoints(
83
- points: PointsArray,
84
- direction: number,
85
- options?: PolygonParams
86
- ): boolean;
87
-
88
- /**
89
- * Returns winding direction of the polygon.
90
- * @param points Array of points that represent the polygon.
91
- * @param options Parameters of the polygon.
92
- * @returns Winding direction of the polygon.
93
- */
94
- export function getPolygonWindingDirectionPoints(
95
- points: PointsArray,
96
- options?: PolygonParams
97
- ): number;
98
-
99
- /**
100
- * Returns signed area of the polygon.
101
- * @param points Array of points that represent the polygon.
102
- * @param options Parameters of the polygon.
103
- * @returns Signed area of the polygon.
104
- */
105
- export function getPolygonSignedAreaPoints(points: PointsArray, options?: PolygonParams): number;
106
-
107
- /**
108
- * Calls visitor callback for each segment in the polygon.
109
- * @param points Array of points that represent the polygon.
110
- * @param visitor A callback to call for each segment.
111
- * @param options Parameters of the polygon.
112
- */
113
- export function forEachSegmentInPolygonPoints(
114
- points: PointsArray,
115
- visitor: SegmentVisitorPoints,
116
- options?: PolygonParams
117
- ): void;
@@ -1,127 +0,0 @@
1
- /* eslint-disable max-statements, max-depth, complexity, no-unused-expressions */
2
-
3
- import {equals} from '@math.gl/core';
4
-
5
- /** @type {typeof import('./polygon-utils').WINDING} */
6
- export const WINDING = {
7
- CLOCKWISE: 1,
8
- COUNTER_CLOCKWISE: -1
9
- };
10
-
11
- /** @type {typeof import('./polygon-utils').modifyPolygonWindingDirection} */
12
- export function modifyPolygonWindingDirection(points, direction, options = {}) {
13
- const windingDirection = getPolygonWindingDirection(points, options);
14
- if (windingDirection !== direction) {
15
- reversePolygon(points, options);
16
- return true;
17
- }
18
- return false;
19
- }
20
-
21
- /** @type {typeof import('./polygon-utils').getPolygonWindingDirection} */
22
- export function getPolygonWindingDirection(points, options = {}) {
23
- return Math.sign(getPolygonSignedArea(points, options));
24
- }
25
-
26
- /** @type {typeof import('./polygon-utils').getPolygonSignedArea} */
27
- export function getPolygonSignedArea(points, options = {}) {
28
- // https://en.wikipedia.org/wiki/Shoelace_formula
29
- const {start = 0, end = points.length} = options;
30
- const dim = options.size || 2;
31
- let area = 0;
32
- for (let i = start, j = end - dim; i < end; i += dim) {
33
- area += (points[i] - points[j]) * (points[i + 1] + points[j + 1]);
34
- j = i;
35
- }
36
- return area / 2;
37
- }
38
-
39
- /** @type {typeof import('./polygon-utils').forEachSegmentInPolygon} */
40
- export function forEachSegmentInPolygon(points, visitor, options = {}) {
41
- const {start = 0, end = points.length, size = 2, isClosed} = options;
42
-
43
- const numPoints = (end - start) / size;
44
- for (let i = 0; i < numPoints - 1; ++i) {
45
- visitor(
46
- points[start + i * size],
47
- points[start + i * size + 1],
48
- points[start + (i + 1) * size],
49
- points[start + (i + 1) * size + 1],
50
- i,
51
- i + 1
52
- );
53
- }
54
-
55
- const endPointIndex = start + (numPoints - 1) * size;
56
- const isClosedEx =
57
- isClosed ||
58
- (equals(points[start], points[endPointIndex]) &&
59
- equals(points[start + 1], points[endPointIndex + 1]));
60
-
61
- if (!isClosedEx) {
62
- visitor(
63
- points[endPointIndex],
64
- points[endPointIndex + 1],
65
- points[start],
66
- points[start + 1],
67
- numPoints - 1,
68
- 0
69
- );
70
- }
71
- }
72
-
73
- function reversePolygon(points, options) {
74
- const {start = 0, end = points.length, size = 2} = options;
75
-
76
- const numPoints = (end - start) / size;
77
- const numSwaps = Math.floor(numPoints / 2);
78
- for (let i = 0; i < numSwaps; ++i) {
79
- const b1 = start + i * size;
80
- const b2 = start + (numPoints - 1 - i) * size;
81
- for (let j = 0; j < size; ++j) {
82
- const tmp = points[b1 + j];
83
- points[b1 + j] = points[b2 + j];
84
- points[b2 + j] = tmp;
85
- }
86
- }
87
- }
88
-
89
- /** @type {typeof import('./polygon-utils').modifyPolygonWindingDirectionPoints} */
90
- export function modifyPolygonWindingDirectionPoints(points, direction, options = {}) {
91
- const currentDirection = getPolygonWindingDirectionPoints(points, options);
92
- if (currentDirection !== direction) {
93
- points.reverse();
94
- return true;
95
- }
96
- return false;
97
- }
98
-
99
- /** @type {typeof import('./polygon-utils').getPolygonWindingDirectionPoints} */
100
- export function getPolygonWindingDirectionPoints(points, options = {}) {
101
- return Math.sign(getPolygonSignedAreaPoints(points, options));
102
- }
103
-
104
- /** @type {typeof import('./polygon-utils').getPolygonSignedAreaPoints} */
105
- export function getPolygonSignedAreaPoints(points, options = {}) {
106
- // https://en.wikipedia.org/wiki/Shoelace_formula
107
- const {start = 0, end = points.length} = options;
108
- let area = 0;
109
- for (let i = start, j = end - 1; i < end; ++i) {
110
- area += (points[i][0] - points[j][0]) * (points[i][1] + points[j][1]);
111
- j = i;
112
- }
113
- return area / 2;
114
- }
115
-
116
- /** @type {typeof import('./polygon-utils').forEachSegmentInPolygonPoints} */
117
- export function forEachSegmentInPolygonPoints(points, visitor, options = {}) {
118
- const {start = 0, end = points.length, isClosed} = options;
119
- for (let i = start; i < end - 1; ++i) {
120
- visitor(points[i], points[i + 1], i, i + 1);
121
- }
122
-
123
- const isClosedEx = isClosed || equals(points[end - 1], points[0]);
124
- if (!isClosedEx) {
125
- visitor(points[end - 1], points[0], end - 1, 0);
126
- }
127
- }
package/src/polygon.d.ts DELETED
@@ -1,36 +0,0 @@
1
- import {SegmentVisitorPoints} from './polygon-utils';
2
-
3
- export default class Polygon {
4
- constructor(points: number[], options: {start?: number, end?: number,size?: number, isClosed?: boolean});
5
-
6
- /**
7
- * Returns signed area of the polygon.
8
- * @returns Signed area of the polygon.
9
- */
10
- getSignedArea(): number;
11
-
12
- /**
13
- * Returns absolute area of the polygon.
14
- * @returns Absolute area of the polygon.
15
- */
16
- getArea(): number;
17
-
18
- /**
19
- * Returns winding direction of the polygon.
20
- * @returns Winding direction of the polygon. 1 is for clockwise, -1 for counterclockwise winding direction.
21
- */
22
- getWindingDirection(): number;
23
-
24
- /**
25
- * Calls the visitor callback for each segment in the polygon.
26
- * @param visitor A callback to call for each segment.
27
- */
28
- forEachSegment(SegmentVisitorPoints): void;
29
-
30
- /**
31
- * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
32
- * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
33
- * @return Returns true if the winding direction was changed.
34
- */
35
- modifyWindingDirection(direction: number): boolean;
36
- }
package/src/polygon.js DELETED
@@ -1,66 +0,0 @@
1
- /* eslint-disable no-undef */
2
- /* eslint-disable no-console */
3
-
4
- import {isArray} from '@math.gl/core';
5
-
6
- import {
7
- getPolygonSignedArea,
8
- forEachSegmentInPolygon,
9
- modifyPolygonWindingDirection,
10
- getPolygonSignedAreaPoints,
11
- forEachSegmentInPolygonPoints,
12
- modifyPolygonWindingDirectionPoints
13
- } from './polygon-utils';
14
-
15
- export default class Polygon {
16
- constructor(points, options = {}) {
17
- this.points = points;
18
- this.isFlatArray = !isArray(points[0]);
19
-
20
- this.options = {
21
- start: options.start || 0,
22
- end: options.end || points.length,
23
- size: options.size || 2,
24
- isClosed: options.isClosed
25
- };
26
-
27
- Object.freeze(this);
28
- }
29
-
30
- getSignedArea() {
31
- if (this.isFlatArray) return getPolygonSignedArea(this.points, this.options);
32
-
33
- return getPolygonSignedAreaPoints(this.points, this.options);
34
- }
35
-
36
- getArea() {
37
- return Math.abs(this.getSignedArea());
38
- }
39
-
40
- getWindingDirection() {
41
- return Math.sign(this.getSignedArea());
42
- }
43
-
44
- forEachSegment(visitor) {
45
- if (this.isFlatArray) {
46
- forEachSegmentInPolygon(
47
- this.points,
48
- // eslint-disable-next-line max-params
49
- (x1, y1, x2, y2, i1, i2) => {
50
- // TODO @igorDykhta original visitor uses arrays for each point, but with flat arrays performance degrades if we allocate points for each segment
51
- visitor([x1, y1], [x2, y2], i1, i2);
52
- },
53
- this.options
54
- );
55
- } else {
56
- forEachSegmentInPolygonPoints(this.points, visitor, this.options);
57
- }
58
- }
59
-
60
- modifyWindingDirection(direction) {
61
- if (this.isFlatArray) {
62
- return modifyPolygonWindingDirection(this.points, direction, this.options);
63
- }
64
- return modifyPolygonWindingDirectionPoints(this.points, direction, this.options);
65
- }
66
- }