@math.gl/polygon 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/lineclip.js CHANGED
@@ -1,129 +1,178 @@
1
- import { push, copy, getPointAtIndex } from "./utils.js";
2
- export function clipPolyline(positions, bbox, options) {
3
- const {
4
- size = 2,
5
- startIndex = 0,
6
- endIndex = positions.length
7
- } = options || {};
8
- const numPoints = (endIndex - startIndex) / size;
9
- const result = [];
10
- let part = [];
11
- let a;
12
- let b;
13
- let codeA = -1;
14
- let codeB;
15
- let lastCode;
16
-
17
- for (let i = 1; i < numPoints; i++) {
18
- a = getPointAtIndex(positions, i - 1, size, startIndex, a);
19
- b = getPointAtIndex(positions, i, size, startIndex, b);
1
+ /*
2
+ Adapted from https://github.com/mapbox/lineclip to work with flat arrays
3
+ and 3d positions
20
4
 
21
- if (codeA < 0) {
22
- codeA = bitCode(a, bbox);
23
- }
5
+ ISC License
24
6
 
25
- codeB = lastCode = bitCode(b, bbox);
7
+ Copyright (c) 2015, Mapbox
26
8
 
27
- while (true) {
28
- if (!(codeA | codeB)) {
29
- push(part, a);
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.
30
12
 
31
- if (codeB !== lastCode) {
32
- push(part, b);
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.
33
20
 
34
- if (i < numPoints - 1) {
35
- result.push(part);
36
- part = [];
37
- }
38
- } else if (i === numPoints - 1) {
39
- push(part, b);
21
+ */
22
+ /* eslint-disable max-statements, max-depth, complexity */
23
+ import { push, copy, getPointAtIndex } from "./utils.js";
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);
40
43
  }
41
-
42
- break;
43
- } else if (codeA & codeB) {
44
- break;
45
- } else if (codeA) {
46
- intersect(a, b, codeA, bbox, a);
47
- codeA = bitCode(a, bbox);
48
- } else {
49
- intersect(a, b, codeB, bbox, b);
50
- codeB = bitCode(b, bbox);
51
- }
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;
52
80
  }
53
-
54
- codeA = lastCode;
55
- }
56
-
57
- if (part.length) result.push(part);
58
- return result;
81
+ if (part.length)
82
+ result.push(part);
83
+ return result;
59
84
  }
85
+ /**
86
+ * Sutherland-Hodgeman polygon clipping algorithm
87
+ * polygon must be closed (first vertex == last vertex)
88
+ */
60
89
  export function clipPolygon(positions, bbox, options) {
61
- const {
62
- size = 2,
63
- endIndex = positions.length
64
- } = options || {};
65
- let {
66
- startIndex = 0
67
- } = options || {};
68
- let numPoints = (endIndex - startIndex) / size;
69
- let result;
70
- let p;
71
- let prev;
72
- let inside;
73
- let prevInside;
74
-
75
- for (let edge = 1; edge <= 8; edge *= 2) {
76
- result = [];
77
- prev = getPointAtIndex(positions, numPoints - 1, size, startIndex, prev);
78
- prevInside = !(bitCode(prev, bbox) & edge);
79
-
80
- for (let i = 0; i < numPoints; i++) {
81
- p = getPointAtIndex(positions, i, size, startIndex, p);
82
- inside = !(bitCode(p, bbox) & edge);
83
- if (inside !== prevInside) push(result, intersect(prev, p, edge, bbox));
84
- if (inside) push(result, p);
85
- copy(prev, p);
86
- prevInside = inside;
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;
87
120
  }
88
-
89
- positions = result;
90
- startIndex = 0;
91
- numPoints = result.length / size;
92
- if (!numPoints) break;
93
- }
94
-
95
- return result;
121
+ return result;
96
122
  }
123
+ /** intersect a segment against one of the 4 lines that make up the bbox */
97
124
  export function intersect(a, b, edge, bbox, out = []) {
98
- let t;
99
- let snap;
100
-
101
- if (edge & 8) {
102
- t = (bbox[3] - a[1]) / (b[1] - a[1]);
103
- snap = 3;
104
- } else if (edge & 4) {
105
- t = (bbox[1] - a[1]) / (b[1] - a[1]);
106
- snap = 1;
107
- } else if (edge & 2) {
108
- t = (bbox[2] - a[0]) / (b[0] - a[0]);
109
- snap = 2;
110
- } else if (edge & 1) {
111
- t = (bbox[0] - a[0]) / (b[0] - a[0]);
112
- snap = 0;
113
- } else {
114
- return null;
115
- }
116
-
117
- for (let i = 0; i < a.length; i++) {
118
- out[i] = (snap & 1) === i ? bbox[snap] : t * (b[i] - a[i]) + a[i];
119
- }
120
-
121
- return 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;
122
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
+ */
123
165
  export function bitCode(p, bbox) {
124
- let code = 0;
125
- if (p[0] < bbox[0]) code |= 1;else if (p[0] > bbox[2]) code |= 2;
126
- if (p[1] < bbox[1]) code |= 4;else if (p[1] > bbox[3]) code |= 8;
127
- return code;
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;
128
178
  }
129
- //# sourceMappingURL=lineclip.js.map
@@ -4,14 +4,14 @@ export declare const WINDING: {
4
4
  readonly COUNTER_CLOCKWISE: -1;
5
5
  };
6
6
  /** Polygon representation where each point is represented as a separate array of positions. */
7
- declare type PointsArray = NumericArray[];
7
+ type PointsArray = NumericArray[];
8
8
  /** Segment visitor callback type for polygons defined with flat arrays, */
9
- declare type SegmentVisitorFlat = (p1x: number, p1y: number, p2x: number, p2y: number, i1: number, i2: number) => void;
9
+ type SegmentVisitorFlat = (p1x: number, p1y: number, p2x: number, p2y: number, i1: number, i2: number) => void;
10
10
  /** Segment visitor callback type for polygons defined with array of points. */
11
- export declare type SegmentVisitorPoints = (p1: NumericArray, p2: NumericArray, i1: number, i2: number) => void;
12
- export declare type Plane2D = 'xy' | 'yz' | 'xz';
11
+ export type SegmentVisitorPoints = (p1: NumericArray, p2: NumericArray, i1: number, i2: number) => void;
12
+ export type Plane2D = 'xy' | 'yz' | 'xz';
13
13
  /** Parameters of a polygon. */
14
- declare type PolygonParams = {
14
+ type PolygonParams = {
15
15
  /**
16
16
  * Start index of the polygon in the array of positions.
17
17
  * @default `0`
@@ -100,4 +100,3 @@ export declare function getPolygonSignedAreaPoints(points: PointsArray, options?
100
100
  */
101
101
  export declare function forEachSegmentInPolygonPoints(points: PointsArray, visitor: SegmentVisitorPoints, options?: PolygonParams): void;
102
102
  export {};
103
- //# sourceMappingURL=polygon-utils.d.ts.map
@@ -1,131 +1,148 @@
1
+ /* eslint-disable max-statements, max-depth, complexity, no-unused-expressions */
1
2
  import { equals } from '@math.gl/core';
2
3
  export const WINDING = {
3
- CLOCKWISE: 1,
4
- COUNTER_CLOCKWISE: -1
4
+ CLOCKWISE: 1,
5
+ COUNTER_CLOCKWISE: -1
5
6
  };
7
+ /**
8
+ * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
9
+ * Note: points are modified in-place.
10
+ * @param points An array that represents points of the polygon.
11
+ * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
12
+ * @param options Parameters of the polygon.
13
+ * @return Returns true if the winding direction was changed.
14
+ */
6
15
  export function modifyPolygonWindingDirection(points, direction, options = {}) {
7
- const windingDirection = getPolygonWindingDirection(points, options);
8
-
9
- if (windingDirection !== direction) {
10
- reversePolygon(points, options);
11
- return true;
12
- }
13
-
14
- return false;
16
+ const windingDirection = getPolygonWindingDirection(points, options);
17
+ if (windingDirection !== direction) {
18
+ reversePolygon(points, options);
19
+ return true;
20
+ }
21
+ return false;
15
22
  }
23
+ /**
24
+ * Returns winding direction of the polygon.
25
+ * @param points An array that represents points of the polygon.
26
+ * @param options Parameters of the polygon.
27
+ * @returns Winding direction of the polygon.
28
+ */
16
29
  export function getPolygonWindingDirection(points, options = {}) {
17
- return Math.sign(getPolygonSignedArea(points, options));
30
+ return Math.sign(getPolygonSignedArea(points, options));
18
31
  }
19
32
  export const DimIndex = {
20
- x: 0,
21
- y: 1,
22
- z: 2
33
+ x: 0,
34
+ y: 1,
35
+ z: 2
23
36
  };
37
+ /**
38
+ * Returns signed area of the polygon.
39
+ * @param points An array that represents points of the polygon.
40
+ * @param options Parameters of the polygon.
41
+ * @returns Signed area of the polygon.
42
+ * https://en.wikipedia.org/wiki/Shoelace_formula
43
+ */
24
44
  export function getPolygonSignedArea(points, options = {}) {
25
- const {
26
- start = 0,
27
- end = points.length,
28
- plane = 'xy'
29
- } = options;
30
- const dim = options.size || 2;
31
- let area = 0;
32
- const i0 = DimIndex[plane[0]];
33
- const i1 = DimIndex[plane[1]];
34
-
35
- for (let i = start, j = end - dim; i < end; i += dim) {
36
- area += (points[i + i0] - points[j + i0]) * (points[i + i1] + points[j + i1]);
37
- j = i;
38
- }
39
-
40
- return area / 2;
45
+ const { start = 0, end = points.length, plane = 'xy' } = options;
46
+ const dim = options.size || 2;
47
+ let area = 0;
48
+ const i0 = DimIndex[plane[0]];
49
+ const i1 = DimIndex[plane[1]];
50
+ for (let i = start, j = end - dim; i < end; i += dim) {
51
+ area += (points[i + i0] - points[j + i0]) * (points[i + i1] + points[j + i1]);
52
+ j = i;
53
+ }
54
+ return area / 2;
41
55
  }
56
+ /**
57
+ * Calls the visitor callback for each segment in the polygon.
58
+ * @param points An array that represents points of the polygon
59
+ * @param visitor A callback to call for each segment.
60
+ * @param options Parameters of the polygon.
61
+ */
42
62
  export function forEachSegmentInPolygon(points, visitor, options = {}) {
43
- const {
44
- start = 0,
45
- end = points.length,
46
- size = 2,
47
- isClosed
48
- } = options;
49
- const numPoints = (end - start) / size;
50
-
51
- for (let i = 0; i < numPoints - 1; ++i) {
52
- visitor(points[start + i * size], points[start + i * size + 1], points[start + (i + 1) * size], points[start + (i + 1) * size + 1], i, i + 1);
53
- }
54
-
55
- const endPointIndex = start + (numPoints - 1) * size;
56
- const isClosedEx = isClosed || equals(points[start], points[endPointIndex]) && equals(points[start + 1], points[endPointIndex + 1]);
57
-
58
- if (!isClosedEx) {
59
- visitor(points[endPointIndex], points[endPointIndex + 1], points[start], points[start + 1], numPoints - 1, 0);
60
- }
63
+ const { start = 0, end = points.length, size = 2, isClosed } = options;
64
+ const numPoints = (end - start) / size;
65
+ for (let i = 0; i < numPoints - 1; ++i) {
66
+ visitor(points[start + i * size], points[start + i * size + 1], points[start + (i + 1) * size], points[start + (i + 1) * size + 1], i, i + 1);
67
+ }
68
+ const endPointIndex = start + (numPoints - 1) * size;
69
+ const isClosedEx = isClosed ||
70
+ (equals(points[start], points[endPointIndex]) &&
71
+ equals(points[start + 1], points[endPointIndex + 1]));
72
+ if (!isClosedEx) {
73
+ visitor(points[endPointIndex], points[endPointIndex + 1], points[start], points[start + 1], numPoints - 1, 0);
74
+ }
61
75
  }
62
-
63
76
  function reversePolygon(points, options) {
64
- const {
65
- start = 0,
66
- end = points.length,
67
- size = 2
68
- } = options;
69
- const numPoints = (end - start) / size;
70
- const numSwaps = Math.floor(numPoints / 2);
71
-
72
- for (let i = 0; i < numSwaps; ++i) {
73
- const b1 = start + i * size;
74
- const b2 = start + (numPoints - 1 - i) * size;
75
-
76
- for (let j = 0; j < size; ++j) {
77
- const tmp = points[b1 + j];
78
- points[b1 + j] = points[b2 + j];
79
- points[b2 + j] = tmp;
77
+ const { start = 0, end = points.length, size = 2 } = options;
78
+ const numPoints = (end - start) / size;
79
+ const numSwaps = Math.floor(numPoints / 2);
80
+ for (let i = 0; i < numSwaps; ++i) {
81
+ const b1 = start + i * size;
82
+ const b2 = start + (numPoints - 1 - i) * size;
83
+ for (let j = 0; j < size; ++j) {
84
+ const tmp = points[b1 + j];
85
+ points[b1 + j] = points[b2 + j];
86
+ points[b2 + j] = tmp;
87
+ }
80
88
  }
81
- }
82
89
  }
83
-
90
+ /**
91
+ * Checks winding direction of the polygon and reverses the polygon in case of opposite winding direction.
92
+ * Note: points are modified in-place.
93
+ * @param points Array of points that represent the polygon.
94
+ * @param direction Requested winding direction. 1 is for clockwise, -1 for counterclockwise winding direction.
95
+ * @param options Parameters of the polygon.
96
+ * @return Returns true if the winding direction was changed.
97
+ */
84
98
  export function modifyPolygonWindingDirectionPoints(points, direction, options = {}) {
85
- const currentDirection = getPolygonWindingDirectionPoints(points, options);
86
-
87
- if (currentDirection !== direction) {
88
- points.reverse();
89
- return true;
90
- }
91
-
92
- return false;
99
+ const currentDirection = getPolygonWindingDirectionPoints(points, options);
100
+ if (currentDirection !== direction) {
101
+ points.reverse();
102
+ return true;
103
+ }
104
+ return false;
93
105
  }
106
+ /**
107
+ * Returns winding direction of the polygon.
108
+ * @param points Array of points that represent the polygon.
109
+ * @param options Parameters of the polygon.
110
+ * @returns Winding direction of the polygon.
111
+ */
94
112
  export function getPolygonWindingDirectionPoints(points, options = {}) {
95
- return Math.sign(getPolygonSignedAreaPoints(points, options));
113
+ return Math.sign(getPolygonSignedAreaPoints(points, options));
96
114
  }
115
+ /**
116
+ * Returns signed area of the polygon.
117
+ * @param points Array of points that represent the polygon.
118
+ * @param options Parameters of the polygon.
119
+ * @returns Signed area of the polygon.
120
+ */
97
121
  export function getPolygonSignedAreaPoints(points, options = {}) {
98
- const {
99
- start = 0,
100
- end = points.length,
101
- plane = 'xy'
102
- } = options;
103
- let area = 0;
104
- const i0 = DimIndex[plane[0]];
105
- const i1 = DimIndex[plane[1]];
106
-
107
- for (let i = start, j = end - 1; i < end; ++i) {
108
- area += (points[i][i0] - points[j][i0]) * (points[i][i1] + points[j][i1]);
109
- j = i;
110
- }
111
-
112
- return area / 2;
122
+ // https://en.wikipedia.org/wiki/Shoelace_formula
123
+ const { start = 0, end = points.length, plane = 'xy' } = options;
124
+ let area = 0;
125
+ const i0 = DimIndex[plane[0]];
126
+ const i1 = DimIndex[plane[1]];
127
+ for (let i = start, j = end - 1; i < end; ++i) {
128
+ area += (points[i][i0] - points[j][i0]) * (points[i][i1] + points[j][i1]);
129
+ j = i;
130
+ }
131
+ return area / 2;
113
132
  }
133
+ /**
134
+ * Calls visitor callback for each segment in the polygon.
135
+ * @param points Array of points that represent the polygon.
136
+ * @param visitor A callback to call for each segment.
137
+ * @param options Parameters of the polygon.
138
+ */
114
139
  export function forEachSegmentInPolygonPoints(points, visitor, options = {}) {
115
- const {
116
- start = 0,
117
- end = points.length,
118
- isClosed
119
- } = options;
120
-
121
- for (let i = start; i < end - 1; ++i) {
122
- visitor(points[i], points[i + 1], i, i + 1);
123
- }
124
-
125
- const isClosedEx = isClosed || equals(points[end - 1], points[0]);
126
-
127
- if (!isClosedEx) {
128
- visitor(points[end - 1], points[0], end - 1, 0);
129
- }
140
+ const { start = 0, end = points.length, isClosed } = options;
141
+ for (let i = start; i < end - 1; ++i) {
142
+ visitor(points[i], points[i + 1], i, i + 1);
143
+ }
144
+ const isClosedEx = isClosed || equals(points[end - 1], points[0]);
145
+ if (!isClosedEx) {
146
+ visitor(points[end - 1], points[0], end - 1, 0);
147
+ }
130
148
  }
131
- //# sourceMappingURL=polygon-utils.js.map
package/dist/polygon.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { SegmentVisitorPoints } from './polygon-utils';
1
+ import type { SegmentVisitorPoints } from "./polygon-utils.js";
2
2
  import type { NumericArray } from '@math.gl/core';
3
- export declare type PolygonOptions = {
3
+ export type PolygonOptions = {
4
4
  start?: number;
5
5
  end?: number;
6
6
  size?: number;
@@ -38,4 +38,3 @@ export declare class Polygon {
38
38
  */
39
39
  modifyWindingDirection(direction: number): boolean;
40
40
  }
41
- //# sourceMappingURL=polygon.d.ts.map