@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.
- package/dist/cut-by-grid.d.ts +21 -0
- package/dist/cut-by-grid.d.ts.map +1 -0
- package/dist/cut-by-grid.js +214 -0
- package/dist/cut-by-mercator-bounds.d.ts +17 -0
- package/dist/cut-by-mercator-bounds.d.ts.map +1 -0
- package/dist/cut-by-mercator-bounds.js +144 -0
- package/dist/{esm/earcut.d.ts → earcut.d.ts} +4 -2
- package/dist/earcut.d.ts.map +1 -0
- package/dist/earcut.js +611 -0
- package/dist/es5/cut-by-grid.js +29 -25
- package/dist/es5/cut-by-grid.js.map +1 -1
- package/dist/es5/cut-by-mercator-bounds.js +24 -19
- package/dist/es5/cut-by-mercator-bounds.js.map +1 -1
- package/dist/es5/earcut.js +11 -10
- package/dist/es5/earcut.js.map +1 -1
- package/dist/es5/index.js.map +1 -1
- package/dist/es5/lineclip.js +20 -16
- package/dist/es5/lineclip.js.map +1 -1
- package/dist/es5/polygon-utils.js +6 -4
- package/dist/es5/polygon-utils.js.map +1 -1
- package/dist/es5/polygon.js +5 -0
- package/dist/es5/polygon.js.map +1 -1
- package/dist/es5/utils.js.map +1 -1
- package/dist/esm/cut-by-grid.js +5 -5
- package/dist/esm/cut-by-grid.js.map +1 -1
- package/dist/esm/cut-by-mercator-bounds.js +5 -5
- package/dist/esm/cut-by-mercator-bounds.js.map +1 -1
- package/dist/esm/earcut.js +9 -10
- package/dist/esm/earcut.js.map +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lineclip.js +5 -5
- package/dist/esm/lineclip.js.map +1 -1
- package/dist/esm/polygon-utils.js +7 -4
- package/dist/esm/polygon-utils.js.map +1 -1
- package/dist/esm/polygon.js +7 -0
- package/dist/esm/polygon.js.map +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/lineclip.d.ts +30 -0
- package/dist/lineclip.d.ts.map +1 -0
- package/dist/lineclip.js +178 -0
- package/dist/{es5/polygon-utils.d.ts → polygon-utils.d.ts} +28 -64
- package/dist/polygon-utils.d.ts.map +1 -0
- package/dist/polygon-utils.js +140 -0
- package/dist/polygon.d.ts +41 -0
- package/dist/polygon.d.ts.map +1 -0
- package/dist/polygon.js +67 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +34 -0
- package/package.json +4 -3
- package/src/{cut-by-grid.js → cut-by-grid.ts} +79 -33
- package/src/{cut-by-mercator-bounds.js → cut-by-mercator-bounds.ts} +51 -22
- package/src/{earcut.js → earcut.ts} +26 -14
- package/src/{index.js → index.ts} +0 -0
- package/src/{lineclip.js → lineclip.ts} +61 -33
- package/src/polygon-utils.ts +228 -0
- package/src/polygon.ts +98 -0
- package/src/{utils.js → utils.ts} +11 -3
- package/dist/es5/cut-by-grid.d.ts +0 -24
- package/dist/es5/cut-by-mercator-bounds.d.ts +0 -23
- package/dist/es5/earcut.d.ts +0 -9
- package/dist/es5/lineclip.d.ts +0 -25
- package/dist/es5/polygon.d.ts +0 -36
- package/dist/esm/cut-by-grid.d.ts +0 -24
- package/dist/esm/cut-by-mercator-bounds.d.ts +0 -23
- package/dist/esm/lineclip.d.ts +0 -25
- package/dist/esm/polygon-utils.d.ts +0 -117
- package/dist/esm/polygon.d.ts +0 -36
- package/src/cut-by-grid.d.ts +0 -24
- package/src/cut-by-mercator-bounds.d.ts +0 -23
- package/src/earcut.d.ts +0 -9
- package/src/lineclip.d.ts +0 -25
- package/src/polygon-utils.d.ts +0 -117
- package/src/polygon-utils.js +0 -127
- package/src/polygon.d.ts +0 -36
- package/src/polygon.js +0 -66
package/dist/lineclip.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Adapted from https://github.com/mapbox/lineclip to work with flat arrays
|
|
3
|
+
and 3d positions
|
|
4
|
+
|
|
5
|
+
ISC License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2015, Mapbox
|
|
8
|
+
|
|
9
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
10
|
+
with or without fee is hereby granted, provided that the above copyright notice
|
|
11
|
+
and this permission notice appear in all copies.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
15
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
17
|
+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
18
|
+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
19
|
+
THIS SOFTWARE.
|
|
20
|
+
|
|
21
|
+
*/
|
|
22
|
+
/* eslint-disable max-statements, max-depth, complexity */
|
|
23
|
+
import { push, copy, getPointAtIndex } from './utils';
|
|
24
|
+
/**
|
|
25
|
+
* Cohen-Sutherland line clipping algorithm, adapted to efficiently
|
|
26
|
+
* handle polylines rather than just segments
|
|
27
|
+
*/
|
|
28
|
+
export function clipPolyline(positions, bbox, options) {
|
|
29
|
+
const { size = 2, startIndex = 0, endIndex = positions.length } = options || {};
|
|
30
|
+
const numPoints = (endIndex - startIndex) / size;
|
|
31
|
+
const result = [];
|
|
32
|
+
let part = [];
|
|
33
|
+
let a;
|
|
34
|
+
let b;
|
|
35
|
+
let codeA = -1;
|
|
36
|
+
let codeB;
|
|
37
|
+
let lastCode;
|
|
38
|
+
for (let i = 1; i < numPoints; i++) {
|
|
39
|
+
a = getPointAtIndex(positions, i - 1, size, startIndex, a);
|
|
40
|
+
b = getPointAtIndex(positions, i, size, startIndex, b);
|
|
41
|
+
if (codeA < 0) {
|
|
42
|
+
codeA = bitCode(a, bbox);
|
|
43
|
+
}
|
|
44
|
+
codeB = lastCode = bitCode(b, bbox);
|
|
45
|
+
// eslint-disable-next-line no-constant-condition
|
|
46
|
+
while (true) {
|
|
47
|
+
if (!(codeA | codeB)) {
|
|
48
|
+
// accept
|
|
49
|
+
push(part, a);
|
|
50
|
+
if (codeB !== lastCode) {
|
|
51
|
+
// segment went outside
|
|
52
|
+
push(part, b);
|
|
53
|
+
if (i < numPoints - 1) {
|
|
54
|
+
// start a new line
|
|
55
|
+
result.push(part);
|
|
56
|
+
part = [];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else if (i === numPoints - 1) {
|
|
60
|
+
push(part, b);
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
else if (codeA & codeB) {
|
|
65
|
+
// trivial reject
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
else if (codeA) {
|
|
69
|
+
// a outside, intersect with clip edge
|
|
70
|
+
intersect(a, b, codeA, bbox, a);
|
|
71
|
+
codeA = bitCode(a, bbox);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
// b outside
|
|
75
|
+
intersect(a, b, codeB, bbox, b);
|
|
76
|
+
codeB = bitCode(b, bbox);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
codeA = lastCode;
|
|
80
|
+
}
|
|
81
|
+
if (part.length)
|
|
82
|
+
result.push(part);
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Sutherland-Hodgeman polygon clipping algorithm
|
|
87
|
+
* polygon must be closed (first vertex == last vertex)
|
|
88
|
+
*/
|
|
89
|
+
export function clipPolygon(positions, bbox, options) {
|
|
90
|
+
const { size = 2, endIndex = positions.length } = options || {};
|
|
91
|
+
let { startIndex = 0 } = options || {};
|
|
92
|
+
let numPoints = (endIndex - startIndex) / size;
|
|
93
|
+
let result;
|
|
94
|
+
let p;
|
|
95
|
+
let prev;
|
|
96
|
+
let inside;
|
|
97
|
+
let prevInside;
|
|
98
|
+
// clip against each side of the clip rectangle
|
|
99
|
+
for (let edge = 1; edge <= 8; edge *= 2) {
|
|
100
|
+
result = [];
|
|
101
|
+
prev = getPointAtIndex(positions, numPoints - 1, size, startIndex, prev);
|
|
102
|
+
prevInside = !(bitCode(prev, bbox) & edge);
|
|
103
|
+
for (let i = 0; i < numPoints; i++) {
|
|
104
|
+
p = getPointAtIndex(positions, i, size, startIndex, p);
|
|
105
|
+
inside = !(bitCode(p, bbox) & edge);
|
|
106
|
+
// if segment goes through the clip window, add an intersection
|
|
107
|
+
if (inside !== prevInside)
|
|
108
|
+
push(result, intersect(prev, p, edge, bbox));
|
|
109
|
+
if (inside)
|
|
110
|
+
push(result, p); // add a point if it's inside
|
|
111
|
+
copy(prev, p);
|
|
112
|
+
prevInside = inside;
|
|
113
|
+
}
|
|
114
|
+
// close loop
|
|
115
|
+
positions = result;
|
|
116
|
+
startIndex = 0;
|
|
117
|
+
numPoints = result.length / size;
|
|
118
|
+
if (!numPoints)
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
/** intersect a segment against one of the 4 lines that make up the bbox */
|
|
124
|
+
export function intersect(a, b, edge, bbox, out = []) {
|
|
125
|
+
let t;
|
|
126
|
+
// Forces out[snapI] to be on the bbox edge
|
|
127
|
+
// Interpolation introduces precision issue which may cause lineclip to be
|
|
128
|
+
// stuck in an infinite loop
|
|
129
|
+
let snap;
|
|
130
|
+
if (edge & 8) {
|
|
131
|
+
// top
|
|
132
|
+
t = (bbox[3] - a[1]) / (b[1] - a[1]);
|
|
133
|
+
snap = 3;
|
|
134
|
+
}
|
|
135
|
+
else if (edge & 4) {
|
|
136
|
+
// bottom
|
|
137
|
+
t = (bbox[1] - a[1]) / (b[1] - a[1]);
|
|
138
|
+
snap = 1;
|
|
139
|
+
}
|
|
140
|
+
else if (edge & 2) {
|
|
141
|
+
// right
|
|
142
|
+
t = (bbox[2] - a[0]) / (b[0] - a[0]);
|
|
143
|
+
snap = 2;
|
|
144
|
+
}
|
|
145
|
+
else if (edge & 1) {
|
|
146
|
+
// left
|
|
147
|
+
t = (bbox[0] - a[0]) / (b[0] - a[0]);
|
|
148
|
+
snap = 0;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
for (let i = 0; i < a.length; i++) {
|
|
154
|
+
out[i] = (snap & 1) === i ? bbox[snap] : t * (b[i] - a[i]) + a[i];
|
|
155
|
+
}
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* bit code reflects the point position relative to the bbox:
|
|
160
|
+
* left mid right
|
|
161
|
+
* top 1001 1000 1010
|
|
162
|
+
* mid 0001 0000 0010
|
|
163
|
+
* bottom 0101 0100 0110
|
|
164
|
+
*/
|
|
165
|
+
export function bitCode(p, bbox) {
|
|
166
|
+
let code = 0;
|
|
167
|
+
if (p[0] < bbox[0])
|
|
168
|
+
code |= 1;
|
|
169
|
+
// left
|
|
170
|
+
else if (p[0] > bbox[2])
|
|
171
|
+
code |= 2; // right
|
|
172
|
+
if (p[1] < bbox[1])
|
|
173
|
+
code |= 4;
|
|
174
|
+
// bottom
|
|
175
|
+
else if (p[1] > bbox[3])
|
|
176
|
+
code |= 8; // top
|
|
177
|
+
return code;
|
|
178
|
+
}
|
|
@@ -1,34 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import type { NumericArray } from '@math.gl/core';
|
|
2
|
+
export declare enum WINDING {
|
|
3
|
+
CLOCKWISE = 1,
|
|
4
|
+
COUNTER_CLOCKWISE = -1
|
|
5
|
+
}
|
|
6
|
+
/** Polygon representation where each point is represented as a separate array of positions. */
|
|
7
|
+
declare type PointsArray = NumericArray[];
|
|
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;
|
|
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;
|
|
12
|
+
/** Parameters of a polygon. */
|
|
13
|
+
declare type PolygonParams = {
|
|
14
|
+
start?: number;
|
|
15
|
+
end?: number;
|
|
16
|
+
size?: number;
|
|
17
|
+
isClosed?: boolean;
|
|
6
18
|
};
|
|
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
19
|
/**
|
|
33
20
|
* Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
|
|
34
21
|
* Note: points are modified in-place.
|
|
@@ -37,40 +24,29 @@ type PolygonParams = {
|
|
|
37
24
|
* @param options Parameters of the polygon.
|
|
38
25
|
* @return Returns true if the winding direction was changed.
|
|
39
26
|
*/
|
|
40
|
-
export function modifyPolygonWindingDirection(
|
|
41
|
-
points: NumberArray,
|
|
42
|
-
direction: number,
|
|
43
|
-
options?: PolygonParams
|
|
44
|
-
): boolean;
|
|
45
|
-
|
|
27
|
+
export declare function modifyPolygonWindingDirection(points: NumericArray, direction: number, options?: PolygonParams): boolean;
|
|
46
28
|
/**
|
|
47
29
|
* Returns winding direction of the polygon.
|
|
48
30
|
* @param points An array that represents points of the polygon.
|
|
49
31
|
* @param options Parameters of the polygon.
|
|
50
32
|
* @returns Winding direction of the polygon.
|
|
51
33
|
*/
|
|
52
|
-
export function getPolygonWindingDirection(points:
|
|
53
|
-
|
|
34
|
+
export declare function getPolygonWindingDirection(points: NumericArray, options?: PolygonParams): number;
|
|
54
35
|
/**
|
|
55
36
|
* Returns signed area of the polygon.
|
|
56
37
|
* @param points An array that represents points of the polygon.
|
|
57
38
|
* @param options Parameters of the polygon.
|
|
58
39
|
* @returns Signed area of the polygon.
|
|
40
|
+
* https://en.wikipedia.org/wiki/Shoelace_formula
|
|
59
41
|
*/
|
|
60
|
-
export function getPolygonSignedArea(points:
|
|
61
|
-
|
|
42
|
+
export declare function getPolygonSignedArea(points: NumericArray, options?: PolygonParams): number;
|
|
62
43
|
/**
|
|
63
44
|
* Calls the visitor callback for each segment in the polygon.
|
|
64
45
|
* @param points An array that represents points of the polygon
|
|
65
46
|
* @param visitor A callback to call for each segment.
|
|
66
47
|
* @param options Parameters of the polygon.
|
|
67
48
|
*/
|
|
68
|
-
export function forEachSegmentInPolygon(
|
|
69
|
-
points: NumberArray,
|
|
70
|
-
visitor: SegmentVisitorFlat,
|
|
71
|
-
options?: PolygonParams
|
|
72
|
-
): void;
|
|
73
|
-
|
|
49
|
+
export declare function forEachSegmentInPolygon(points: NumericArray, visitor: SegmentVisitorFlat, options?: PolygonParams): void;
|
|
74
50
|
/**
|
|
75
51
|
* Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
|
|
76
52
|
* Note: points are modified in-place.
|
|
@@ -79,39 +55,27 @@ export function forEachSegmentInPolygon(
|
|
|
79
55
|
* @param options Parameters of the polygon.
|
|
80
56
|
* @return Returns true if the winding direction was changed.
|
|
81
57
|
*/
|
|
82
|
-
export function modifyPolygonWindingDirectionPoints(
|
|
83
|
-
points: PointsArray,
|
|
84
|
-
direction: number,
|
|
85
|
-
options?: PolygonParams
|
|
86
|
-
): boolean;
|
|
87
|
-
|
|
58
|
+
export declare function modifyPolygonWindingDirectionPoints(points: PointsArray, direction: number, options?: PolygonParams): boolean;
|
|
88
59
|
/**
|
|
89
60
|
* Returns winding direction of the polygon.
|
|
90
61
|
* @param points Array of points that represent the polygon.
|
|
91
62
|
* @param options Parameters of the polygon.
|
|
92
63
|
* @returns Winding direction of the polygon.
|
|
93
64
|
*/
|
|
94
|
-
export function getPolygonWindingDirectionPoints(
|
|
95
|
-
points: PointsArray,
|
|
96
|
-
options?: PolygonParams
|
|
97
|
-
): number;
|
|
98
|
-
|
|
65
|
+
export declare function getPolygonWindingDirectionPoints(points: PointsArray, options?: PolygonParams): number;
|
|
99
66
|
/**
|
|
100
67
|
* Returns signed area of the polygon.
|
|
101
68
|
* @param points Array of points that represent the polygon.
|
|
102
69
|
* @param options Parameters of the polygon.
|
|
103
70
|
* @returns Signed area of the polygon.
|
|
104
71
|
*/
|
|
105
|
-
export function getPolygonSignedAreaPoints(points: PointsArray, options?: PolygonParams): number;
|
|
106
|
-
|
|
72
|
+
export declare function getPolygonSignedAreaPoints(points: PointsArray, options?: PolygonParams): number;
|
|
107
73
|
/**
|
|
108
74
|
* Calls visitor callback for each segment in the polygon.
|
|
109
75
|
* @param points Array of points that represent the polygon.
|
|
110
76
|
* @param visitor A callback to call for each segment.
|
|
111
77
|
* @param options Parameters of the polygon.
|
|
112
78
|
*/
|
|
113
|
-
export function forEachSegmentInPolygonPoints(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
options?: PolygonParams
|
|
117
|
-
): void;
|
|
79
|
+
export declare function forEachSegmentInPolygonPoints(points: PointsArray, visitor: SegmentVisitorPoints, options?: PolygonParams): void;
|
|
80
|
+
export {};
|
|
81
|
+
//# sourceMappingURL=polygon-utils.d.ts.map
|
|
@@ -0,0 +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,oBAAY,OAAO;IACjB,SAAS,IAAI;IACb,iBAAiB,KAAK;CACvB;AAED,+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,EACV,EAAE,EAAE,MAAM,KACP,IAAI,CAAC;AAEV,+BAA+B;AAC/B,aAAK,aAAa,GAAG;IACnB,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;;;;;;;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;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,GAAE,aAAkB,GAAG,MAAM,CAS9F;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,CASR;AAED;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE,oBAAoB,EAC7B,OAAO,GAAE,aAAkB,GAC1B,IAAI,CAUN"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/* eslint-disable max-statements, max-depth, complexity, no-unused-expressions */
|
|
2
|
+
import { equals } from '@math.gl/core';
|
|
3
|
+
export var WINDING;
|
|
4
|
+
(function (WINDING) {
|
|
5
|
+
WINDING[WINDING["CLOCKWISE"] = 1] = "CLOCKWISE";
|
|
6
|
+
WINDING[WINDING["COUNTER_CLOCKWISE"] = -1] = "COUNTER_CLOCKWISE";
|
|
7
|
+
})(WINDING || (WINDING = {}));
|
|
8
|
+
/**
|
|
9
|
+
* Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
|
|
10
|
+
* Note: points are modified in-place.
|
|
11
|
+
* @param points An array that represents points of the polygon.
|
|
12
|
+
* @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
|
|
13
|
+
* @param options Parameters of the polygon.
|
|
14
|
+
* @return Returns true if the winding direction was changed.
|
|
15
|
+
*/
|
|
16
|
+
export function modifyPolygonWindingDirection(points, direction, options = {}) {
|
|
17
|
+
const windingDirection = getPolygonWindingDirection(points, options);
|
|
18
|
+
if (windingDirection !== direction) {
|
|
19
|
+
reversePolygon(points, options);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Returns winding direction of the polygon.
|
|
26
|
+
* @param points An array that represents points of the polygon.
|
|
27
|
+
* @param options Parameters of the polygon.
|
|
28
|
+
* @returns Winding direction of the polygon.
|
|
29
|
+
*/
|
|
30
|
+
export function getPolygonWindingDirection(points, options = {}) {
|
|
31
|
+
return Math.sign(getPolygonSignedArea(points, options));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Returns signed area of the polygon.
|
|
35
|
+
* @param points An array that represents points of the polygon.
|
|
36
|
+
* @param options Parameters of the polygon.
|
|
37
|
+
* @returns Signed area of the polygon.
|
|
38
|
+
* https://en.wikipedia.org/wiki/Shoelace_formula
|
|
39
|
+
*/
|
|
40
|
+
export function getPolygonSignedArea(points, options = {}) {
|
|
41
|
+
const { start = 0, end = points.length } = options;
|
|
42
|
+
const dim = options.size || 2;
|
|
43
|
+
let area = 0;
|
|
44
|
+
for (let i = start, j = end - dim; i < end; i += dim) {
|
|
45
|
+
area += (points[i] - points[j]) * (points[i + 1] + points[j + 1]);
|
|
46
|
+
j = i;
|
|
47
|
+
}
|
|
48
|
+
return area / 2;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Calls the visitor callback for each segment in the polygon.
|
|
52
|
+
* @param points An array that represents points of the polygon
|
|
53
|
+
* @param visitor A callback to call for each segment.
|
|
54
|
+
* @param options Parameters of the polygon.
|
|
55
|
+
*/
|
|
56
|
+
export function forEachSegmentInPolygon(points, visitor, options = {}) {
|
|
57
|
+
const { start = 0, end = points.length, size = 2, isClosed } = options;
|
|
58
|
+
const numPoints = (end - start) / size;
|
|
59
|
+
for (let i = 0; i < numPoints - 1; ++i) {
|
|
60
|
+
visitor(points[start + i * size], points[start + i * size + 1], points[start + (i + 1) * size], points[start + (i + 1) * size + 1], i, i + 1);
|
|
61
|
+
}
|
|
62
|
+
const endPointIndex = start + (numPoints - 1) * size;
|
|
63
|
+
const isClosedEx = isClosed ||
|
|
64
|
+
(equals(points[start], points[endPointIndex]) &&
|
|
65
|
+
equals(points[start + 1], points[endPointIndex + 1]));
|
|
66
|
+
if (!isClosedEx) {
|
|
67
|
+
visitor(points[endPointIndex], points[endPointIndex + 1], points[start], points[start + 1], numPoints - 1, 0);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function reversePolygon(points, options) {
|
|
71
|
+
const { start = 0, end = points.length, size = 2 } = options;
|
|
72
|
+
const numPoints = (end - start) / size;
|
|
73
|
+
const numSwaps = Math.floor(numPoints / 2);
|
|
74
|
+
for (let i = 0; i < numSwaps; ++i) {
|
|
75
|
+
const b1 = start + i * size;
|
|
76
|
+
const b2 = start + (numPoints - 1 - i) * size;
|
|
77
|
+
for (let j = 0; j < size; ++j) {
|
|
78
|
+
const tmp = points[b1 + j];
|
|
79
|
+
points[b1 + j] = points[b2 + j];
|
|
80
|
+
points[b2 + j] = tmp;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
|
|
86
|
+
* Note: points are modified in-place.
|
|
87
|
+
* @param points Array of points that represent the polygon.
|
|
88
|
+
* @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
|
|
89
|
+
* @param options Parameters of the polygon.
|
|
90
|
+
* @return Returns true if the winding direction was changed.
|
|
91
|
+
*/
|
|
92
|
+
export function modifyPolygonWindingDirectionPoints(points, direction, options = {}) {
|
|
93
|
+
const currentDirection = getPolygonWindingDirectionPoints(points, options);
|
|
94
|
+
if (currentDirection !== direction) {
|
|
95
|
+
points.reverse();
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Returns winding direction of the polygon.
|
|
102
|
+
* @param points Array of points that represent the polygon.
|
|
103
|
+
* @param options Parameters of the polygon.
|
|
104
|
+
* @returns Winding direction of the polygon.
|
|
105
|
+
*/
|
|
106
|
+
export function getPolygonWindingDirectionPoints(points, options = {}) {
|
|
107
|
+
return Math.sign(getPolygonSignedAreaPoints(points, options));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Returns signed area of the polygon.
|
|
111
|
+
* @param points Array of points that represent the polygon.
|
|
112
|
+
* @param options Parameters of the polygon.
|
|
113
|
+
* @returns Signed area of the polygon.
|
|
114
|
+
*/
|
|
115
|
+
export function getPolygonSignedAreaPoints(points, options = {}) {
|
|
116
|
+
// https://en.wikipedia.org/wiki/Shoelace_formula
|
|
117
|
+
const { start = 0, end = points.length } = options;
|
|
118
|
+
let area = 0;
|
|
119
|
+
for (let i = start, j = end - 1; i < end; ++i) {
|
|
120
|
+
area += (points[i][0] - points[j][0]) * (points[i][1] + points[j][1]);
|
|
121
|
+
j = i;
|
|
122
|
+
}
|
|
123
|
+
return area / 2;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Calls visitor callback for each segment in the polygon.
|
|
127
|
+
* @param points Array of points that represent the polygon.
|
|
128
|
+
* @param visitor A callback to call for each segment.
|
|
129
|
+
* @param options Parameters of the polygon.
|
|
130
|
+
*/
|
|
131
|
+
export function forEachSegmentInPolygonPoints(points, visitor, options = {}) {
|
|
132
|
+
const { start = 0, end = points.length, isClosed } = options;
|
|
133
|
+
for (let i = start; i < end - 1; ++i) {
|
|
134
|
+
visitor(points[i], points[i + 1], i, i + 1);
|
|
135
|
+
}
|
|
136
|
+
const isClosedEx = isClosed || equals(points[end - 1], points[0]);
|
|
137
|
+
if (!isClosedEx) {
|
|
138
|
+
visitor(points[end - 1], points[0], end - 1, 0);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { SegmentVisitorPoints } from './polygon-utils';
|
|
2
|
+
import type { NumericArray } from '@math.gl/core';
|
|
3
|
+
export declare type PolygonOptions = {
|
|
4
|
+
start?: number;
|
|
5
|
+
end?: number;
|
|
6
|
+
size?: number;
|
|
7
|
+
isClosed?: boolean;
|
|
8
|
+
};
|
|
9
|
+
export default class Polygon {
|
|
10
|
+
points: NumericArray | number[][];
|
|
11
|
+
isFlatArray: boolean;
|
|
12
|
+
options: PolygonOptions;
|
|
13
|
+
constructor(points: NumericArray | number[][], options?: PolygonOptions);
|
|
14
|
+
/**
|
|
15
|
+
* Returns signed area of the polygon.
|
|
16
|
+
* @returns Signed area of the polygon.
|
|
17
|
+
*/
|
|
18
|
+
getSignedArea(): number;
|
|
19
|
+
/**
|
|
20
|
+
* Returns absolute area of the polygon.
|
|
21
|
+
* @returns Absolute area of the polygon.
|
|
22
|
+
*/
|
|
23
|
+
getArea(): number;
|
|
24
|
+
/**
|
|
25
|
+
* Returns winding direction of the polygon.
|
|
26
|
+
* @returns Winding direction of the polygon. 1 is for clockwise, -1 for counterclockwise winding direction.
|
|
27
|
+
*/
|
|
28
|
+
getWindingDirection(): number;
|
|
29
|
+
/**
|
|
30
|
+
* Calls the visitor callback for each segment in the polygon.
|
|
31
|
+
* @param visitor A callback to call for each segment.
|
|
32
|
+
*/
|
|
33
|
+
forEachSegment(visitor: SegmentVisitorPoints): void;
|
|
34
|
+
/**
|
|
35
|
+
* Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
|
|
36
|
+
* @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
|
|
37
|
+
* @return Returns true if the winding direction was changed.
|
|
38
|
+
*/
|
|
39
|
+
modifyWindingDirection(direction: number): boolean;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=polygon.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/polygon.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/* eslint-disable no-undef, no-console */
|
|
2
|
+
import { isArray } from '@math.gl/core';
|
|
3
|
+
import { getPolygonSignedArea, forEachSegmentInPolygon, modifyPolygonWindingDirection, getPolygonSignedAreaPoints, forEachSegmentInPolygonPoints, modifyPolygonWindingDirectionPoints } from './polygon-utils';
|
|
4
|
+
export default class Polygon {
|
|
5
|
+
constructor(points, options = {}) {
|
|
6
|
+
this.points = points;
|
|
7
|
+
this.isFlatArray = !isArray(points[0]);
|
|
8
|
+
this.options = {
|
|
9
|
+
start: options.start || 0,
|
|
10
|
+
end: options.end || points.length,
|
|
11
|
+
size: options.size || 2,
|
|
12
|
+
isClosed: options.isClosed
|
|
13
|
+
};
|
|
14
|
+
Object.freeze(this);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Returns signed area of the polygon.
|
|
18
|
+
* @returns Signed area of the polygon.
|
|
19
|
+
*/
|
|
20
|
+
getSignedArea() {
|
|
21
|
+
if (this.isFlatArray)
|
|
22
|
+
return getPolygonSignedArea(this.points, this.options);
|
|
23
|
+
return getPolygonSignedAreaPoints(this.points, this.options);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns absolute area of the polygon.
|
|
27
|
+
* @returns Absolute area of the polygon.
|
|
28
|
+
*/
|
|
29
|
+
getArea() {
|
|
30
|
+
return Math.abs(this.getSignedArea());
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Returns winding direction of the polygon.
|
|
34
|
+
* @returns Winding direction of the polygon. 1 is for clockwise, -1 for counterclockwise winding direction.
|
|
35
|
+
*/
|
|
36
|
+
getWindingDirection() {
|
|
37
|
+
return Math.sign(this.getSignedArea());
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Calls the visitor callback for each segment in the polygon.
|
|
41
|
+
* @param visitor A callback to call for each segment.
|
|
42
|
+
*/
|
|
43
|
+
forEachSegment(visitor) {
|
|
44
|
+
if (this.isFlatArray) {
|
|
45
|
+
forEachSegmentInPolygon(this.points,
|
|
46
|
+
// eslint-disable-next-line max-params
|
|
47
|
+
(x1, y1, x2, y2, i1, i2) => {
|
|
48
|
+
// TODO @igorDykhta original visitor uses arrays for each point, but with flat arrays performance degrades if we allocate points for each segment
|
|
49
|
+
visitor([x1, y1], [x2, y2], i1, i2);
|
|
50
|
+
}, this.options);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
forEachSegmentInPolygonPoints(this.points, visitor, this.options);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
|
|
58
|
+
* @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
|
|
59
|
+
* @return Returns true if the winding direction was changed.
|
|
60
|
+
*/
|
|
61
|
+
modifyWindingDirection(direction) {
|
|
62
|
+
if (this.isFlatArray) {
|
|
63
|
+
return modifyPolygonWindingDirection(this.points, direction, this.options);
|
|
64
|
+
}
|
|
65
|
+
return modifyPolygonWindingDirectionPoints(this.points, direction, this.options);
|
|
66
|
+
}
|
|
67
|
+
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { NumericArray } from '@math.gl/core';
|
|
2
|
+
export declare function push(target: number[], source: number[]): boolean;
|
|
3
|
+
export declare function copy(target: number[], source: Readonly<NumericArray>): void;
|
|
4
|
+
export declare function getPointAtIndex(positions: NumericArray, index: number, size: number, offset: number, out?: number[]): number[];
|
|
5
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,eAAe,CAAC;AAEhD,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAsBhE;AAED,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,IAAI,CAK3E;AAED,wBAAgB,eAAe,CAC7B,SAAS,EAAE,YAAY,EACvB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,GAAG,GAAE,MAAM,EAAO,GACjB,MAAM,EAAE,CAMV"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function push(target, source) {
|
|
2
|
+
const size = source.length;
|
|
3
|
+
const startIndex = target.length;
|
|
4
|
+
// dedupe, if source is the same point as the last vertex
|
|
5
|
+
if (startIndex > 0) {
|
|
6
|
+
let isDuplicate = true;
|
|
7
|
+
for (let i = 0; i < size; i++) {
|
|
8
|
+
if (target[startIndex - size + i] !== source[i]) {
|
|
9
|
+
isDuplicate = false;
|
|
10
|
+
break;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
if (isDuplicate) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
for (let i = 0; i < size; i++) {
|
|
18
|
+
target[startIndex + i] = source[i];
|
|
19
|
+
}
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
export function copy(target, source) {
|
|
23
|
+
const size = source.length;
|
|
24
|
+
for (let i = 0; i < size; i++) {
|
|
25
|
+
target[i] = source[i];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function getPointAtIndex(positions, index, size, offset, out = []) {
|
|
29
|
+
const startI = offset + index * size;
|
|
30
|
+
for (let i = 0; i < size; i++) {
|
|
31
|
+
out[i] = positions[startI + i];
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
-
"version": "3.
|
|
8
|
+
"version": "3.6.0-alpha.2",
|
|
9
9
|
"keywords": [
|
|
10
10
|
"webgl",
|
|
11
11
|
"javascript",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"type": "git",
|
|
18
18
|
"url": "https://github.com/uber-web/math.gl.git"
|
|
19
19
|
},
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
20
21
|
"main": "dist/es5/index.js",
|
|
21
22
|
"module": "dist/esm/index.js",
|
|
22
23
|
"files": [
|
|
@@ -24,7 +25,7 @@
|
|
|
24
25
|
"src"
|
|
25
26
|
],
|
|
26
27
|
"dependencies": {
|
|
27
|
-
"@math.gl/core": "3.
|
|
28
|
+
"@math.gl/core": "3.6.0-alpha.2"
|
|
28
29
|
},
|
|
29
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "ea7dc265c98b68af56218302d1d42b7d65f74ccb"
|
|
30
31
|
}
|