@math.gl/geometry 5.0.0-alpha.17 → 5.0.0-alpha.19
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/README.md +7 -1
- package/dist/culling/constants.d.ts +3 -0
- package/dist/culling/constants.d.ts.map +1 -0
- package/dist/culling/constants.js +5 -0
- package/dist/culling/constants.js.map +1 -0
- package/dist/culling/index.cjs +2022 -0
- package/dist/culling/index.cjs.map +6 -0
- package/dist/culling/index.d.ts +28 -0
- package/dist/culling/index.d.ts.map +1 -0
- package/dist/culling/index.js +22 -0
- package/dist/culling/index.js.map +1 -0
- package/dist/culling/lib/algorithms/bounding-box-from-points.d.ts +15 -0
- package/dist/culling/lib/algorithms/bounding-box-from-points.d.ts.map +1 -0
- package/dist/culling/lib/algorithms/bounding-box-from-points.js +137 -0
- package/dist/culling/lib/algorithms/bounding-box-from-points.js.map +1 -0
- package/dist/culling/lib/algorithms/bounding-sphere-from-points.d.ts +14 -0
- package/dist/culling/lib/algorithms/bounding-sphere-from-points.d.ts.map +1 -0
- package/dist/culling/lib/algorithms/bounding-sphere-from-points.js +146 -0
- package/dist/culling/lib/algorithms/bounding-sphere-from-points.js.map +1 -0
- package/dist/culling/lib/algorithms/compute-eigen-decomposition.d.ts +37 -0
- package/dist/culling/lib/algorithms/compute-eigen-decomposition.d.ts.map +1 -0
- package/dist/culling/lib/algorithms/compute-eigen-decomposition.js +137 -0
- package/dist/culling/lib/algorithms/compute-eigen-decomposition.js.map +1 -0
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +56 -0
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -0
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js +116 -0
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -0
- package/dist/culling/lib/bounding-volumes/bounding-sphere.d.ts +40 -0
- package/dist/culling/lib/bounding-volumes/bounding-sphere.d.ts.map +1 -0
- package/dist/culling/lib/bounding-volumes/bounding-sphere.js +123 -0
- package/dist/culling/lib/bounding-volumes/bounding-sphere.js.map +1 -0
- package/dist/culling/lib/bounding-volumes/bounding-volume.d.ts +30 -0
- package/dist/culling/lib/bounding-volumes/bounding-volume.d.ts.map +1 -0
- package/dist/culling/lib/bounding-volumes/bounding-volume.js +7 -0
- package/dist/culling/lib/bounding-volumes/bounding-volume.js.map +1 -0
- package/dist/culling/lib/bounding-volumes/oriented-bounding-box.d.ts +68 -0
- package/dist/culling/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -0
- package/dist/culling/lib/bounding-volumes/oriented-bounding-box.js +259 -0
- package/dist/culling/lib/bounding-volumes/oriented-bounding-box.js.map +1 -0
- package/dist/culling/lib/culling-volume.d.ts +46 -0
- package/dist/culling/lib/culling-volume.d.ts.map +1 -0
- package/dist/culling/lib/culling-volume.js +126 -0
- package/dist/culling/lib/culling-volume.js.map +1 -0
- package/dist/culling/lib/perspective-frustum.d.ts +153 -0
- package/dist/culling/lib/perspective-frustum.d.ts.map +1 -0
- package/dist/culling/lib/perspective-frustum.js +191 -0
- package/dist/culling/lib/perspective-frustum.js.map +1 -0
- package/dist/culling/lib/perspective-off-center-frustum.d.ts +158 -0
- package/dist/culling/lib/perspective-off-center-frustum.d.ts.map +1 -0
- package/dist/culling/lib/perspective-off-center-frustum.js +276 -0
- package/dist/culling/lib/perspective-off-center-frustum.js.map +1 -0
- package/dist/culling/lib/plane.d.ts +35 -0
- package/dist/culling/lib/plane.d.ts.map +1 -0
- package/dist/culling/lib/plane.js +90 -0
- package/dist/culling/lib/plane.js.map +1 -0
- package/dist/culling/lib/ray.d.ts +13 -0
- package/dist/culling/lib/ray.d.ts.map +1 -0
- package/dist/culling/lib/ray.js +30 -0
- package/dist/culling/lib/ray.js.map +1 -0
- package/dist/culling/lib/shapes/box-shape.d.ts +19 -0
- package/dist/culling/lib/shapes/box-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/box-shape.js +69 -0
- package/dist/culling/lib/shapes/box-shape.js.map +1 -0
- package/dist/culling/lib/shapes/capsule-shape.d.ts +23 -0
- package/dist/culling/lib/shapes/capsule-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/capsule-shape.js +151 -0
- package/dist/culling/lib/shapes/capsule-shape.js.map +1 -0
- package/dist/culling/lib/shapes/cylinder-shape.d.ts +22 -0
- package/dist/culling/lib/shapes/cylinder-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/cylinder-shape.js +99 -0
- package/dist/culling/lib/shapes/cylinder-shape.js.map +1 -0
- package/dist/culling/lib/shapes/implicit-shape.d.ts +56 -0
- package/dist/culling/lib/shapes/implicit-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/implicit-shape.js +142 -0
- package/dist/culling/lib/shapes/implicit-shape.js.map +1 -0
- package/dist/culling/lib/shapes/plane-shape.d.ts +27 -0
- package/dist/culling/lib/shapes/plane-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/plane-shape.js +71 -0
- package/dist/culling/lib/shapes/plane-shape.js.map +1 -0
- package/dist/culling/lib/shapes/sphere-shape.d.ts +18 -0
- package/dist/culling/lib/shapes/sphere-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/sphere-shape.js +43 -0
- package/dist/culling/lib/shapes/sphere-shape.js.map +1 -0
- package/dist/culling/oriented-box-2d.cjs +73 -0
- package/dist/culling/oriented-box-2d.cjs.map +6 -0
- package/dist/culling/oriented-box-2d.d.ts +22 -0
- package/dist/culling/oriented-box-2d.d.ts.map +1 -0
- package/dist/culling/oriented-box-2d.js +86 -0
- package/dist/culling/oriented-box-2d.js.map +1 -0
- package/dist/culling/queries.cjs +161 -0
- package/dist/culling/queries.cjs.map +6 -0
- package/dist/culling/queries.d.ts +20 -0
- package/dist/culling/queries.d.ts.map +1 -0
- package/dist/culling/queries.js +139 -0
- package/dist/culling/queries.js.map +1 -0
- package/dist/curves/bezier-curves.d.ts +18 -0
- package/dist/curves/bezier-curves.d.ts.map +1 -0
- package/dist/curves/bezier-curves.js +54 -0
- package/dist/curves/bezier-curves.js.map +1 -0
- package/dist/curves/catmull-rom-curve.d.ts +15 -0
- package/dist/curves/catmull-rom-curve.d.ts.map +1 -0
- package/dist/curves/catmull-rom-curve.js +77 -0
- package/dist/curves/catmull-rom-curve.js.map +1 -0
- package/dist/curves/curve-arc-length.d.ts +17 -0
- package/dist/curves/curve-arc-length.d.ts.map +1 -0
- package/dist/curves/curve-arc-length.js +58 -0
- package/dist/curves/curve-arc-length.js.map +1 -0
- package/dist/curves/curve.d.ts +23 -0
- package/dist/curves/curve.d.ts.map +1 -0
- package/dist/curves/curve.js +77 -0
- package/dist/curves/curve.js.map +1 -0
- package/dist/curves/hermite-curve.d.ts +9 -0
- package/dist/curves/hermite-curve.d.ts.map +1 -0
- package/dist/curves/hermite-curve.js +24 -0
- package/dist/curves/hermite-curve.js.map +1 -0
- package/dist/curves/index.cjs +293 -0
- package/dist/curves/index.cjs.map +6 -0
- package/dist/curves/index.d.ts +8 -0
- package/dist/curves/index.d.ts.map +1 -0
- package/dist/curves/index.js +9 -0
- package/dist/curves/index.js.map +1 -0
- package/dist/lib/geometry.d.ts +1 -1
- package/dist/lib/geometry.d.ts.map +1 -1
- package/dist/polygon/cut-by-grid.d.ts +24 -0
- package/dist/polygon/cut-by-grid.d.ts.map +1 -0
- package/dist/polygon/cut-by-grid.js +221 -0
- package/dist/polygon/cut-by-grid.js.map +1 -0
- package/dist/polygon/cut-by-mercator-bounds.d.ts +17 -0
- package/dist/polygon/cut-by-mercator-bounds.d.ts.map +1 -0
- package/dist/polygon/cut-by-mercator-bounds.js +149 -0
- package/dist/polygon/cut-by-mercator-bounds.js.map +1 -0
- package/dist/polygon/earcut.d.ts +13 -0
- package/dist/polygon/earcut.d.ts.map +1 -0
- package/dist/polygon/earcut.js +627 -0
- package/dist/polygon/earcut.js.map +1 -0
- package/dist/polygon/index.cjs +1414 -0
- package/dist/polygon/index.cjs.map +6 -0
- package/dist/polygon/index.d.ts +12 -0
- package/dist/polygon/index.d.ts.map +1 -0
- package/dist/polygon/index.js +12 -0
- package/dist/polygon/index.js.map +1 -0
- package/dist/polygon/lineclip.d.ts +31 -0
- package/dist/polygon/lineclip.d.ts.map +1 -0
- package/dist/polygon/lineclip.js +184 -0
- package/dist/polygon/lineclip.js.map +1 -0
- package/dist/polygon/polygon-utils.d.ts +103 -0
- package/dist/polygon/polygon-utils.d.ts.map +1 -0
- package/dist/polygon/polygon-utils.js +155 -0
- package/dist/polygon/polygon-utils.js.map +1 -0
- package/dist/polygon/polygon.d.ts +41 -0
- package/dist/polygon/polygon.d.ts.map +1 -0
- package/dist/polygon/polygon.js +76 -0
- package/dist/polygon/polygon.js.map +1 -0
- package/dist/polygon/subdivide-polyline.d.ts +42 -0
- package/dist/polygon/subdivide-polyline.d.ts.map +1 -0
- package/dist/polygon/subdivide-polyline.js +109 -0
- package/dist/polygon/subdivide-polyline.js.map +1 -0
- package/dist/polygon/subdivide-triangle-mesh.d.ts +68 -0
- package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -0
- package/dist/polygon/subdivide-triangle-mesh.js +228 -0
- package/dist/polygon/subdivide-triangle-mesh.js.map +1 -0
- package/dist/polygon/utils.d.ts +5 -0
- package/dist/polygon/utils.d.ts.map +1 -0
- package/dist/polygon/utils.js +38 -0
- package/dist/polygon/utils.js.map +1 -0
- package/dist/spatial-index/box-index.cjs +328 -0
- package/dist/spatial-index/box-index.cjs.map +6 -0
- package/dist/spatial-index/box-index.d.ts +65 -0
- package/dist/spatial-index/box-index.d.ts.map +1 -0
- package/dist/spatial-index/box-index.js +263 -0
- package/dist/spatial-index/box-index.js.map +1 -0
- package/dist/spatial-index/index.cjs +494 -0
- package/dist/spatial-index/index.cjs.map +6 -0
- package/dist/spatial-index/index.d.ts +5 -0
- package/dist/spatial-index/index.d.ts.map +1 -0
- package/dist/spatial-index/index.js +6 -0
- package/dist/spatial-index/index.js.map +1 -0
- package/dist/spatial-index/triangle-bvh.d.ts +53 -0
- package/dist/spatial-index/triangle-bvh.d.ts.map +1 -0
- package/dist/spatial-index/triangle-bvh.js +102 -0
- package/dist/spatial-index/triangle-bvh.js.map +1 -0
- package/dist/utils/attribute-compression.d.ts +50 -0
- package/dist/utils/attribute-compression.d.ts.map +1 -0
- package/dist/utils/attribute-compression.js +186 -0
- package/dist/utils/attribute-compression.js.map +1 -0
- package/dist/utils/attribute-iterator.d.ts +8 -0
- package/dist/utils/attribute-iterator.d.ts.map +1 -0
- package/dist/utils/attribute-iterator.js +25 -0
- package/dist/utils/attribute-iterator.js.map +1 -0
- package/dist/utils/compute-vertex-normals.d.ts +4 -0
- package/dist/utils/compute-vertex-normals.d.ts.map +1 -0
- package/dist/utils/compute-vertex-normals.js +54 -0
- package/dist/utils/compute-vertex-normals.js.map +1 -0
- package/dist/utils/constants.d.ts +52 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +39 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/coordinates.d.ts +3 -0
- package/dist/utils/coordinates.d.ts.map +1 -0
- package/dist/utils/coordinates.js +8 -0
- package/dist/utils/coordinates.js.map +1 -0
- package/dist/utils/geometry.d.ts +20 -0
- package/dist/utils/geometry.d.ts.map +1 -0
- package/dist/utils/geometry.js +31 -0
- package/dist/utils/geometry.js.map +1 -0
- package/dist/utils/gl-type.d.ts +17 -0
- package/dist/utils/gl-type.d.ts.map +1 -0
- package/dist/utils/gl-type.js +69 -0
- package/dist/utils/gl-type.js.map +1 -0
- package/dist/utils/index.cjs +965 -0
- package/dist/utils/index.cjs.map +6 -0
- package/dist/utils/index.d.ts +18 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +16 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/interpolate-packed-attributes.cjs +176 -0
- package/dist/utils/interpolate-packed-attributes.cjs.map +6 -0
- package/dist/utils/interpolate-packed-attributes.d.ts +43 -0
- package/dist/utils/interpolate-packed-attributes.d.ts.map +1 -0
- package/dist/utils/interpolate-packed-attributes.js +192 -0
- package/dist/utils/interpolate-packed-attributes.js.map +1 -0
- package/dist/utils/mesh-processing.d.ts +18 -0
- package/dist/utils/mesh-processing.d.ts.map +1 -0
- package/dist/utils/mesh-processing.js +300 -0
- package/dist/utils/mesh-processing.js.map +1 -0
- package/dist/utils/primitive-iterator.d.ts +21 -0
- package/dist/utils/primitive-iterator.d.ts.map +1 -0
- package/dist/utils/primitive-iterator.js +117 -0
- package/dist/utils/primitive-iterator.js.map +1 -0
- package/dist/utils/rgb565.d.ts +5 -0
- package/dist/utils/rgb565.d.ts.map +1 -0
- package/dist/utils/rgb565.js +24 -0
- package/dist/utils/rgb565.js.map +1 -0
- package/dist/utils/typed-array-utils.d.ts +3 -0
- package/dist/utils/typed-array-utils.d.ts.map +1 -0
- package/dist/utils/typed-array-utils.js +16 -0
- package/dist/utils/typed-array-utils.js.map +1 -0
- package/package.json +49 -3
- package/src/culling/constants.ts +6 -0
- package/src/culling/index.ts +40 -0
- package/src/culling/lib/algorithms/bounding-box-from-points.ts +174 -0
- package/src/culling/lib/algorithms/bounding-sphere-from-points.ts +175 -0
- package/src/culling/lib/algorithms/compute-eigen-decomposition.ts +171 -0
- package/src/culling/lib/bounding-volumes/axis-aligned-bounding-box.ts +153 -0
- package/src/culling/lib/bounding-volumes/bounding-sphere.ts +154 -0
- package/src/culling/lib/bounding-volumes/bounding-volume.ts +38 -0
- package/src/culling/lib/bounding-volumes/oriented-bounding-box.ts +355 -0
- package/src/culling/lib/culling-volume.ts +163 -0
- package/src/culling/lib/perspective-frustum.ts +288 -0
- package/src/culling/lib/perspective-off-center-frustum.ts +381 -0
- package/src/culling/lib/plane.ts +118 -0
- package/src/culling/lib/ray.ts +33 -0
- package/src/culling/lib/shapes/box-shape.ts +78 -0
- package/src/culling/lib/shapes/capsule-shape.ts +232 -0
- package/src/culling/lib/shapes/cylinder-shape.ts +133 -0
- package/src/culling/lib/shapes/implicit-shape.ts +209 -0
- package/src/culling/lib/shapes/plane-shape.ts +78 -0
- package/src/culling/lib/shapes/sphere-shape.ts +60 -0
- package/src/culling/oriented-box-2d.ts +165 -0
- package/src/culling/queries.ts +155 -0
- package/src/curves/bezier-curves.ts +62 -0
- package/src/curves/catmull-rom-curve.ts +89 -0
- package/src/curves/curve-arc-length.ts +70 -0
- package/src/curves/curve.ts +96 -0
- package/src/curves/hermite-curve.ts +34 -0
- package/src/curves/index.ts +15 -0
- package/src/lib/geometry-utils.ts +1 -1
- package/src/lib/geometry.ts +1 -1
- package/src/polygon/cut-by-grid.ts +313 -0
- package/src/polygon/cut-by-mercator-bounds.ts +204 -0
- package/src/polygon/earcut.ts +798 -0
- package/src/polygon/index.ts +31 -0
- package/src/polygon/lineclip.ts +218 -0
- package/src/polygon/polygon-utils.ts +273 -0
- package/src/polygon/polygon.ts +105 -0
- package/src/polygon/subdivide-polyline.ts +160 -0
- package/src/polygon/subdivide-triangle-mesh.ts +322 -0
- package/src/polygon/utils.ts +50 -0
- package/src/spatial-index/box-index.ts +343 -0
- package/src/spatial-index/index.ts +18 -0
- package/src/spatial-index/triangle-bvh.ts +162 -0
- package/src/utils/attribute-compression.ts +234 -0
- package/src/utils/attribute-iterator.ts +28 -0
- package/src/utils/compute-vertex-normals.ts +63 -0
- package/src/utils/constants.ts +42 -0
- package/src/utils/coordinates.ts +8 -0
- package/src/utils/geometry.ts +49 -0
- package/src/utils/gl-type.ts +87 -0
- package/src/utils/index.ts +55 -0
- package/src/utils/interpolate-packed-attributes.ts +284 -0
- package/src/utils/mesh-processing.ts +377 -0
- package/src/utils/primitive-iterator.ts +145 -0
- package/src/utils/rgb565.ts +26 -0
- package/src/utils/typed-array-utils.ts +16 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {NumericArray} from '@math.gl/core/types';
|
|
6
|
+
|
|
7
|
+
export type CurvePoint = Readonly<ArrayLike<number>>;
|
|
8
|
+
|
|
9
|
+
/** Parameterized Euclidean curve, with t in [0, 1]. */
|
|
10
|
+
export abstract class Curve {
|
|
11
|
+
readonly dimension: number;
|
|
12
|
+
|
|
13
|
+
protected constructor(dimension: number) {
|
|
14
|
+
if (dimension !== 2 && dimension !== 3) {
|
|
15
|
+
throw new RangeError('Curve points must have two or three coordinates');
|
|
16
|
+
}
|
|
17
|
+
this.dimension = dimension;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getPoint(t: number): number[];
|
|
21
|
+
getPoint<T extends NumericArray>(t: number, result: T): T;
|
|
22
|
+
getPoint(t: number, result: NumericArray = new Array(this.dimension)): NumericArray {
|
|
23
|
+
validateParameter(t);
|
|
24
|
+
validateResult(result, this.dimension);
|
|
25
|
+
this.evaluate(t, result, false);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Derivative with respect to the normalized curve parameter t. */
|
|
30
|
+
getDerivative(t: number): number[];
|
|
31
|
+
getDerivative<T extends NumericArray>(t: number, result: T): T;
|
|
32
|
+
getDerivative(t: number, result: NumericArray = new Array(this.dimension)): NumericArray {
|
|
33
|
+
validateParameter(t);
|
|
34
|
+
validateResult(result, this.dimension);
|
|
35
|
+
this.evaluate(t, result, true);
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Unit tangent; returns a zero vector at stationary points. */
|
|
40
|
+
getTangent(t: number): number[];
|
|
41
|
+
getTangent<T extends NumericArray>(t: number, result: T): T;
|
|
42
|
+
getTangent(t: number, result: NumericArray = new Array(this.dimension)): NumericArray {
|
|
43
|
+
this.getDerivative(t, result);
|
|
44
|
+
const length = Math.hypot(result[0], result[1], this.dimension === 3 ? result[2] : 0);
|
|
45
|
+
if (length > 0) {
|
|
46
|
+
for (let i = 0; i < this.dimension; i++) result[i] /= length;
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Flat coordinates for divisions + 1 evenly spaced parameter samples. */
|
|
52
|
+
getPoints(divisions = 64): Float64Array {
|
|
53
|
+
validateDivisions(divisions);
|
|
54
|
+
const points = new Float64Array((divisions + 1) * this.dimension);
|
|
55
|
+
const point = new Array<number>(this.dimension);
|
|
56
|
+
for (let i = 0; i <= divisions; i++) {
|
|
57
|
+
this.getPoint(i / divisions, point);
|
|
58
|
+
points.set(point, i * this.dimension);
|
|
59
|
+
}
|
|
60
|
+
return points;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
protected abstract evaluate(t: number, result: NumericArray, derivative: boolean): void;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Copy caller-owned control data so curves and their length tables stay stable. */
|
|
67
|
+
export function copyPoints(points: readonly CurvePoint[]): number[][] {
|
|
68
|
+
if (!points.length) throw new RangeError('Invalid number of curve control points');
|
|
69
|
+
const dimension = points[0].length;
|
|
70
|
+
if (dimension !== 2 && dimension !== 3) {
|
|
71
|
+
throw new RangeError('Curve points must have two or three coordinates');
|
|
72
|
+
}
|
|
73
|
+
return points.map(point => {
|
|
74
|
+
if (point.length !== dimension) throw new RangeError('Control point dimensions must match');
|
|
75
|
+
return Array.from(point, value => {
|
|
76
|
+
if (!Number.isFinite(value)) throw new RangeError('Control coordinates must be finite');
|
|
77
|
+
return value;
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function validateParameter(t: number): void {
|
|
83
|
+
if (!Number.isFinite(t) || t < 0 || t > 1) {
|
|
84
|
+
throw new RangeError('Curve parameter must be finite and in [0, 1]');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function validateDivisions(divisions: number): void {
|
|
89
|
+
if (!Number.isSafeInteger(divisions) || divisions < 1) {
|
|
90
|
+
throw new RangeError('divisions must be a positive safe integer');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function validateResult(result: NumericArray, dimension: number): void {
|
|
95
|
+
if (result.length < dimension) throw new RangeError('Result buffer is too short');
|
|
96
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {NumericArray} from '@math.gl/core/types';
|
|
6
|
+
import {Curve, copyPoints, type CurvePoint} from './curve';
|
|
7
|
+
|
|
8
|
+
/** Cubic curve with endpoint derivatives (not unit tangent directions). */
|
|
9
|
+
export class HermiteCurve extends Curve {
|
|
10
|
+
private readonly data: number[][];
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
start: CurvePoint,
|
|
14
|
+
end: CurvePoint,
|
|
15
|
+
startTangent: CurvePoint,
|
|
16
|
+
endTangent: CurvePoint
|
|
17
|
+
) {
|
|
18
|
+
const data = copyPoints([start, end, startTangent, endTangent]);
|
|
19
|
+
super(data[0].length);
|
|
20
|
+
this.data = data;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected evaluate(t: number, result: NumericArray, derivative: boolean): void {
|
|
24
|
+
const t2 = t * t;
|
|
25
|
+
const h0 = derivative ? 6 * t2 - 6 * t : (1 + 2 * t) * (1 - t) ** 2;
|
|
26
|
+
const h1 = derivative ? -6 * t2 + 6 * t : t2 * (3 - 2 * t);
|
|
27
|
+
const h2 = derivative ? 3 * t2 - 4 * t + 1 : t * (1 - t) ** 2;
|
|
28
|
+
const h3 = derivative ? 3 * t2 - 2 * t : t2 * (t - 1);
|
|
29
|
+
for (let i = 0; i < this.dimension; i++) {
|
|
30
|
+
result[i] =
|
|
31
|
+
h0 * this.data[0][i] + h1 * this.data[1][i] + h2 * this.data[2][i] + h3 * this.data[3][i];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
export {Curve} from './curve';
|
|
6
|
+
export type {CurvePoint} from './curve';
|
|
7
|
+
export {
|
|
8
|
+
LineCurve,
|
|
9
|
+
QuadraticBezierCurve,
|
|
10
|
+
CubicBezierCurve
|
|
11
|
+
} from './bezier-curves';
|
|
12
|
+
export {HermiteCurve} from './hermite-curve';
|
|
13
|
+
export {CatmullRomCurve} from './catmull-rom-curve';
|
|
14
|
+
export type {CatmullRomCurveOptions} from './catmull-rom-curve';
|
|
15
|
+
export {CurveArcLength} from './curve-arc-length';
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
import type {TypedArray, TypedArrayConstructor} from '@math.gl/types';
|
|
5
|
+
import type {TypedArray, TypedArrayConstructor} from '@math.gl/core/types';
|
|
6
6
|
import type {GeometryAttribute} from './geometry';
|
|
7
7
|
|
|
8
8
|
type GeometryLike = {
|
package/src/lib/geometry.ts
CHANGED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/* eslint-disable max-statements, max-depth, complexity, no-unused-expressions */
|
|
6
|
+
import {bitCode, intersect, BoundingBox} from './lineclip';
|
|
7
|
+
import {getPointAtIndex, copy, push} from './utils';
|
|
8
|
+
|
|
9
|
+
import type {NumericArray} from '@math.gl/core';
|
|
10
|
+
|
|
11
|
+
export type Polygon = {
|
|
12
|
+
positions: Readonly<NumericArray>;
|
|
13
|
+
holeIndices?: Readonly<NumericArray>;
|
|
14
|
+
edgeTypes?: Readonly<NumericArray>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export function cutPolylineByGrid(
|
|
18
|
+
positions: NumericArray,
|
|
19
|
+
options?: {
|
|
20
|
+
size?: number;
|
|
21
|
+
broken?: boolean;
|
|
22
|
+
gridResolution?: number;
|
|
23
|
+
gridOffset?: [number, number];
|
|
24
|
+
startIndex?: number;
|
|
25
|
+
endIndex?: number;
|
|
26
|
+
}
|
|
27
|
+
): number[] | number[][] {
|
|
28
|
+
const {
|
|
29
|
+
size = 2,
|
|
30
|
+
broken = false,
|
|
31
|
+
gridResolution = 10,
|
|
32
|
+
gridOffset = [0, 0],
|
|
33
|
+
startIndex = 0,
|
|
34
|
+
endIndex = positions.length
|
|
35
|
+
} = options || {};
|
|
36
|
+
const numPoints = (endIndex - startIndex) / size;
|
|
37
|
+
let part: number[] = [];
|
|
38
|
+
const result: number[][] = [part];
|
|
39
|
+
const a: number[] = getPointAtIndex(positions, 0, size, startIndex);
|
|
40
|
+
let b: number[];
|
|
41
|
+
let codeB: number;
|
|
42
|
+
const cell: BoundingBox = getGridCell(a, gridResolution, gridOffset, []);
|
|
43
|
+
const scratchPoint: number[] = [];
|
|
44
|
+
push(part, a);
|
|
45
|
+
|
|
46
|
+
for (let i = 1; i < numPoints; i++) {
|
|
47
|
+
b = getPointAtIndex(positions, i, size, startIndex, b);
|
|
48
|
+
codeB = bitCode(b, cell);
|
|
49
|
+
|
|
50
|
+
while (codeB) {
|
|
51
|
+
// find the intersection with the current cell
|
|
52
|
+
intersect(a, b, codeB, cell, scratchPoint);
|
|
53
|
+
const codeAlt = bitCode(scratchPoint, cell);
|
|
54
|
+
if (codeAlt) {
|
|
55
|
+
intersect(a, scratchPoint, codeAlt, cell, scratchPoint);
|
|
56
|
+
codeB = codeAlt;
|
|
57
|
+
}
|
|
58
|
+
push(part, scratchPoint);
|
|
59
|
+
// move to the next cell
|
|
60
|
+
copy(a, scratchPoint);
|
|
61
|
+
|
|
62
|
+
moveToNeighborCell(cell, gridResolution, codeB);
|
|
63
|
+
if (broken && part.length > size) {
|
|
64
|
+
part = [];
|
|
65
|
+
result.push(part);
|
|
66
|
+
push(part, a);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
codeB = bitCode(b, cell);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
push(part, b);
|
|
73
|
+
copy(a, b);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return broken ? result : result[0];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const TYPE_INSIDE = 0;
|
|
80
|
+
const TYPE_BORDER = 1;
|
|
81
|
+
|
|
82
|
+
/** Internal helper type during cutting, name TBD */
|
|
83
|
+
type PolygonCut = {
|
|
84
|
+
pos: Readonly<NumericArray>;
|
|
85
|
+
types: number[];
|
|
86
|
+
holes: Readonly<NumericArray>;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** Internal helper type during cutting, name TBD */
|
|
90
|
+
type MutablePolygonCut = {
|
|
91
|
+
pos: number[];
|
|
92
|
+
types: number[];
|
|
93
|
+
holes: number[];
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Cuts a polygon by a pre-defined grid
|
|
98
|
+
*/
|
|
99
|
+
export function cutPolygonByGrid(
|
|
100
|
+
positions: Readonly<NumericArray>,
|
|
101
|
+
holeIndices: Readonly<NumericArray> | null = null,
|
|
102
|
+
options?: {
|
|
103
|
+
size?: number;
|
|
104
|
+
gridResolution?: number;
|
|
105
|
+
gridOffset?: [number, number];
|
|
106
|
+
edgeTypes?: boolean;
|
|
107
|
+
}
|
|
108
|
+
): Polygon[] {
|
|
109
|
+
if (!positions.length) {
|
|
110
|
+
// input is empty
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
const {size = 2, gridResolution = 10, gridOffset = [0, 0], edgeTypes = false} = options || {};
|
|
114
|
+
const result: Polygon[] = [];
|
|
115
|
+
const queue: PolygonCut[] = [
|
|
116
|
+
{
|
|
117
|
+
pos: positions,
|
|
118
|
+
types: edgeTypes ? (new Array(positions.length / size).fill(TYPE_BORDER) as number[]) : null,
|
|
119
|
+
holes: holeIndices || []
|
|
120
|
+
}
|
|
121
|
+
];
|
|
122
|
+
const bbox: number[][] = [[], []];
|
|
123
|
+
// @ts-ignore
|
|
124
|
+
let cell: BoundingBox = [];
|
|
125
|
+
|
|
126
|
+
// Recursively bisect polygon until every part fit in a single grid cell
|
|
127
|
+
while (queue.length) {
|
|
128
|
+
const {pos, types, holes} = queue.shift();
|
|
129
|
+
|
|
130
|
+
// Get the bounding box of the outer polygon
|
|
131
|
+
getBoundingBox(pos, size, holes[0] || pos.length, bbox);
|
|
132
|
+
cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
|
|
133
|
+
const code = bitCode(bbox[1], cell);
|
|
134
|
+
|
|
135
|
+
if (code) {
|
|
136
|
+
// Split the outer ring at the boundary
|
|
137
|
+
let parts = bisectPolygon(pos, types, size, 0, holes[0] || pos.length, cell, code);
|
|
138
|
+
const polygonLow: MutablePolygonCut = {pos: parts[0].pos, types: parts[0].types, holes: []};
|
|
139
|
+
const polygonHigh: MutablePolygonCut = {pos: parts[1].pos, types: parts[1].types, holes: []};
|
|
140
|
+
queue.push(polygonLow, polygonHigh);
|
|
141
|
+
|
|
142
|
+
// Split each hole at the boundary
|
|
143
|
+
for (let i = 0; i < holes.length; i++) {
|
|
144
|
+
parts = bisectPolygon(pos, types, size, holes[i], holes[i + 1] || pos.length, cell, code);
|
|
145
|
+
|
|
146
|
+
if (parts[0]) {
|
|
147
|
+
polygonLow.holes.push(polygonLow.pos.length);
|
|
148
|
+
polygonLow.pos = concatInPlace(polygonLow.pos, parts[0].pos);
|
|
149
|
+
if (edgeTypes) {
|
|
150
|
+
polygonLow.types = concatInPlace(polygonLow.types, parts[0].types);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (parts[1]) {
|
|
154
|
+
polygonHigh.holes.push(polygonHigh.pos.length);
|
|
155
|
+
polygonHigh.pos = concatInPlace(polygonHigh.pos, parts[1].pos);
|
|
156
|
+
if (edgeTypes) {
|
|
157
|
+
polygonHigh.types = concatInPlace(polygonHigh.types, parts[1].types);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
} else {
|
|
162
|
+
// Polygon fits in a single cell, no more processing required
|
|
163
|
+
const polygon: Polygon = {positions: pos};
|
|
164
|
+
if (edgeTypes) {
|
|
165
|
+
polygon.edgeTypes = types;
|
|
166
|
+
}
|
|
167
|
+
if (holes.length) {
|
|
168
|
+
polygon.holeIndices = holes;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
result.push(polygon);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// edgeTypes:
|
|
178
|
+
// TYPE_BORDER - edge from the original polygon
|
|
179
|
+
// TYPE_INSIDE - inside the original polygon
|
|
180
|
+
// eslint-disable-next-line max-params
|
|
181
|
+
function bisectPolygon(
|
|
182
|
+
positions: Readonly<NumericArray>,
|
|
183
|
+
edgeTypes: number[] | undefined,
|
|
184
|
+
size: number,
|
|
185
|
+
startIndex: number,
|
|
186
|
+
endIndex: number,
|
|
187
|
+
bbox: BoundingBox,
|
|
188
|
+
edge: number
|
|
189
|
+
): {
|
|
190
|
+
pos: number[];
|
|
191
|
+
types?: number[];
|
|
192
|
+
}[] {
|
|
193
|
+
const numPoints = (endIndex - startIndex) / size;
|
|
194
|
+
const resultLow: number[] = [];
|
|
195
|
+
const resultHigh: number[] = [];
|
|
196
|
+
const typesLow: number[] = [];
|
|
197
|
+
const typesHigh: number[] = [];
|
|
198
|
+
const scratchPoint: number[] = [];
|
|
199
|
+
|
|
200
|
+
let p: number[];
|
|
201
|
+
let side: number;
|
|
202
|
+
let type: number;
|
|
203
|
+
const prev = getPointAtIndex(positions, numPoints - 1, size, startIndex);
|
|
204
|
+
let prevSide = Math.sign(edge & 8 ? prev[1] - bbox[3] : prev[0] - bbox[2]);
|
|
205
|
+
let prevType = edgeTypes && edgeTypes[numPoints - 1];
|
|
206
|
+
let lowPointCount = 0;
|
|
207
|
+
let highPointCount = 0;
|
|
208
|
+
|
|
209
|
+
for (let i = 0; i < numPoints; i++) {
|
|
210
|
+
p = getPointAtIndex(positions, i, size, startIndex, p);
|
|
211
|
+
side = Math.sign(edge & 8 ? p[1] - bbox[3] : p[0] - bbox[2]);
|
|
212
|
+
type = edgeTypes && edgeTypes[startIndex / size + i];
|
|
213
|
+
|
|
214
|
+
// if segment goes through the boundary, add an intersection
|
|
215
|
+
if (side && prevSide && prevSide !== side) {
|
|
216
|
+
intersect(prev, p, edge, bbox, scratchPoint);
|
|
217
|
+
push(resultLow, scratchPoint) && typesLow.push(prevType);
|
|
218
|
+
push(resultHigh, scratchPoint) && typesHigh.push(prevType);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (side <= 0) {
|
|
222
|
+
push(resultLow, p) && typesLow.push(type);
|
|
223
|
+
lowPointCount -= side;
|
|
224
|
+
} else if (typesLow.length) {
|
|
225
|
+
typesLow[typesLow.length - 1] = TYPE_INSIDE;
|
|
226
|
+
}
|
|
227
|
+
if (side >= 0) {
|
|
228
|
+
push(resultHigh, p) && typesHigh.push(type);
|
|
229
|
+
highPointCount += side;
|
|
230
|
+
} else if (typesHigh.length) {
|
|
231
|
+
typesHigh[typesHigh.length - 1] = TYPE_INSIDE;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
copy(prev, p);
|
|
235
|
+
prevSide = side;
|
|
236
|
+
prevType = type;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return [
|
|
240
|
+
lowPointCount ? {pos: resultLow, types: edgeTypes && typesLow} : null,
|
|
241
|
+
highPointCount ? {pos: resultHigh, types: edgeTypes && typesHigh} : null
|
|
242
|
+
];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function getGridCell(
|
|
246
|
+
p: number[],
|
|
247
|
+
gridResolution: number,
|
|
248
|
+
gridOffset: [number, number],
|
|
249
|
+
out: number[]
|
|
250
|
+
): BoundingBox {
|
|
251
|
+
const left = Math.floor((p[0] - gridOffset[0]) / gridResolution) * gridResolution + gridOffset[0];
|
|
252
|
+
const bottom =
|
|
253
|
+
Math.floor((p[1] - gridOffset[1]) / gridResolution) * gridResolution + gridOffset[1];
|
|
254
|
+
out[0] = left;
|
|
255
|
+
out[1] = bottom;
|
|
256
|
+
out[2] = left + gridResolution;
|
|
257
|
+
out[3] = bottom + gridResolution;
|
|
258
|
+
return out as BoundingBox;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function moveToNeighborCell(cell: number[], gridResolution: number, edge: number): void {
|
|
262
|
+
if (edge & 8) {
|
|
263
|
+
// top
|
|
264
|
+
cell[1] += gridResolution;
|
|
265
|
+
cell[3] += gridResolution;
|
|
266
|
+
} else if (edge & 4) {
|
|
267
|
+
// bottom
|
|
268
|
+
cell[1] -= gridResolution;
|
|
269
|
+
cell[3] -= gridResolution;
|
|
270
|
+
} else if (edge & 2) {
|
|
271
|
+
// right
|
|
272
|
+
cell[0] += gridResolution;
|
|
273
|
+
cell[2] += gridResolution;
|
|
274
|
+
} else if (edge & 1) {
|
|
275
|
+
// left
|
|
276
|
+
cell[0] -= gridResolution;
|
|
277
|
+
cell[2] -= gridResolution;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function getBoundingBox(
|
|
282
|
+
positions: Readonly<NumericArray>,
|
|
283
|
+
size: number,
|
|
284
|
+
endIndex: number,
|
|
285
|
+
out: number[][]
|
|
286
|
+
): number[][] {
|
|
287
|
+
let minX = Infinity;
|
|
288
|
+
let maxX = -Infinity;
|
|
289
|
+
let minY = Infinity;
|
|
290
|
+
let maxY = -Infinity;
|
|
291
|
+
|
|
292
|
+
for (let i = 0; i < endIndex; i += size) {
|
|
293
|
+
const x = positions[i];
|
|
294
|
+
const y = positions[i + 1];
|
|
295
|
+
minX = x < minX ? x : minX;
|
|
296
|
+
maxX = x > maxX ? x : maxX;
|
|
297
|
+
minY = y < minY ? y : minY;
|
|
298
|
+
maxY = y > maxY ? y : maxY;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
out[0][0] = minX;
|
|
302
|
+
out[0][1] = minY;
|
|
303
|
+
out[1][0] = maxX;
|
|
304
|
+
out[1][1] = maxY;
|
|
305
|
+
return out;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function concatInPlace(arr1: number[], arr2: number[]): number[] {
|
|
309
|
+
for (let i = 0; i < arr2.length; i++) {
|
|
310
|
+
arr1.push(arr2[i]);
|
|
311
|
+
}
|
|
312
|
+
return arr1;
|
|
313
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {cutPolylineByGrid, cutPolygonByGrid} from './cut-by-grid';
|
|
6
|
+
import {getPointAtIndex, push} from './utils';
|
|
7
|
+
import type {Polygon} from './cut-by-grid';
|
|
8
|
+
import type {NumericArray} from '@math.gl/core';
|
|
9
|
+
|
|
10
|
+
// https://en.wikipedia.org/wiki/Web_Mercator_projection
|
|
11
|
+
const DEFAULT_MAX_LATITUDE = 85.051129;
|
|
12
|
+
|
|
13
|
+
/** https://user-images.githubusercontent.com/2059298/78465769-938b7a00-76ae-11ea-9b95-1f4c26425ab9.png */
|
|
14
|
+
export function cutPolylineByMercatorBounds(
|
|
15
|
+
positions: Readonly<NumericArray>,
|
|
16
|
+
options?: {
|
|
17
|
+
size?: number;
|
|
18
|
+
startIndex?: number;
|
|
19
|
+
endIndex?: number;
|
|
20
|
+
normalize?: boolean;
|
|
21
|
+
}
|
|
22
|
+
): number[][] {
|
|
23
|
+
const {size = 2, startIndex = 0, endIndex = positions.length, normalize = true} = options || {};
|
|
24
|
+
|
|
25
|
+
// Remap longitudes so that each segment takes the shorter path
|
|
26
|
+
const newPositions = positions.slice(startIndex, endIndex);
|
|
27
|
+
wrapLongitudesForShortestPath(newPositions, size, 0, endIndex - startIndex);
|
|
28
|
+
|
|
29
|
+
const parts = cutPolylineByGrid(newPositions, {
|
|
30
|
+
size,
|
|
31
|
+
broken: true,
|
|
32
|
+
gridResolution: 360,
|
|
33
|
+
gridOffset: [-180, -180]
|
|
34
|
+
}) as number[][];
|
|
35
|
+
|
|
36
|
+
if (normalize) {
|
|
37
|
+
// Each part is guaranteed to be in a single copy of the world
|
|
38
|
+
// Map longitudes back to [-180, 180]
|
|
39
|
+
for (const part of parts) {
|
|
40
|
+
shiftLongitudesIntoRange(part, size);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return parts;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** https://user-images.githubusercontent.com/2059298/78465770-94241080-76ae-11ea-809a-6a8534dac1d9.png */
|
|
47
|
+
export function cutPolygonByMercatorBounds(
|
|
48
|
+
positions: Readonly<NumericArray>,
|
|
49
|
+
holeIndices: Readonly<NumericArray> | null = null,
|
|
50
|
+
options?: {
|
|
51
|
+
size?: number;
|
|
52
|
+
normalize?: boolean;
|
|
53
|
+
maxLatitude?: number;
|
|
54
|
+
edgeTypes?: boolean;
|
|
55
|
+
}
|
|
56
|
+
): Polygon[] {
|
|
57
|
+
const {size = 2, normalize = true, edgeTypes = false} = options || {};
|
|
58
|
+
holeIndices = holeIndices || [];
|
|
59
|
+
const newPositions: number[] = [];
|
|
60
|
+
const newHoleIndices: number[] = [];
|
|
61
|
+
let srcStartIndex = 0;
|
|
62
|
+
let targetIndex = 0;
|
|
63
|
+
|
|
64
|
+
for (let ringIndex = 0; ringIndex <= holeIndices.length; ringIndex++) {
|
|
65
|
+
// srcStartIndex/srcEndIndex define the ring in the original positions
|
|
66
|
+
const srcEndIndex = holeIndices[ringIndex] || positions.length;
|
|
67
|
+
// targetStartIndex/targetIndex define the ring in newPositions
|
|
68
|
+
const targetStartIndex = targetIndex;
|
|
69
|
+
|
|
70
|
+
// In case the ring contains a pole (e.g. Antarctica), we'll have to insert vertices
|
|
71
|
+
// The insertion point is defined by the vertex closest to the pole
|
|
72
|
+
// Split the the ring by the insertion point when copying to newPositions
|
|
73
|
+
const splitIndex = findSplitIndex(positions, size, srcStartIndex, srcEndIndex);
|
|
74
|
+
for (let i = splitIndex; i < srcEndIndex; i++) {
|
|
75
|
+
newPositions[targetIndex++] = positions[i];
|
|
76
|
+
}
|
|
77
|
+
for (let i = srcStartIndex; i < splitIndex; i++) {
|
|
78
|
+
newPositions[targetIndex++] = positions[i];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Remap longitudes so that each segment takes the shorter path
|
|
82
|
+
wrapLongitudesForShortestPath(newPositions, size, targetStartIndex, targetIndex);
|
|
83
|
+
|
|
84
|
+
// Handle the case when the ring contains a pole
|
|
85
|
+
insertPoleVertices(newPositions, size, targetStartIndex, targetIndex, options?.maxLatitude);
|
|
86
|
+
|
|
87
|
+
srcStartIndex = srcEndIndex;
|
|
88
|
+
newHoleIndices[ringIndex] = targetIndex;
|
|
89
|
+
}
|
|
90
|
+
newHoleIndices.pop();
|
|
91
|
+
|
|
92
|
+
const parts = cutPolygonByGrid(newPositions, newHoleIndices, {
|
|
93
|
+
size,
|
|
94
|
+
gridResolution: 360,
|
|
95
|
+
gridOffset: [-180, -180],
|
|
96
|
+
edgeTypes
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
if (normalize) {
|
|
100
|
+
// Each part is guaranteed to be in a single copy of the world
|
|
101
|
+
// Map longitudes back to [-180, 180]
|
|
102
|
+
for (const part of parts) {
|
|
103
|
+
// May mutate newPositions, which is created by us.
|
|
104
|
+
shiftLongitudesIntoRange(part.positions as NumericArray, size);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return parts;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* Helpers */
|
|
111
|
+
|
|
112
|
+
// See comments for insertPoleVertices
|
|
113
|
+
function findSplitIndex(
|
|
114
|
+
positions: Readonly<NumericArray>,
|
|
115
|
+
size: number,
|
|
116
|
+
startIndex: number,
|
|
117
|
+
endIndex: number
|
|
118
|
+
): number {
|
|
119
|
+
let maxLat = -1;
|
|
120
|
+
let pointIndex = -1;
|
|
121
|
+
for (let i = startIndex + 1; i < endIndex; i += size) {
|
|
122
|
+
const lat = Math.abs(positions[i]);
|
|
123
|
+
if (lat > maxLat) {
|
|
124
|
+
maxLat = lat;
|
|
125
|
+
pointIndex = i - 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return pointIndex;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// https://user-images.githubusercontent.com/2059298/78857483-5987e400-79de-11ea-98fc-0631287a8431.png
|
|
132
|
+
//
|
|
133
|
+
// If the polygon contains a pole, to tesselate it correctly, we need to insert the edge
|
|
134
|
+
// of map into the polygon. This requires adding two vertices that represent the pole, by
|
|
135
|
+
// drawing a perpendicular line to the Mercator map edge from a selected vertex on the ring.
|
|
136
|
+
//
|
|
137
|
+
// We select the insertion position carefully so that the inserted line segments do not
|
|
138
|
+
// intersect with the ring itself. This is ensured by findSplitIndex, which returns the
|
|
139
|
+
// vertex closest to the pole.
|
|
140
|
+
function insertPoleVertices(
|
|
141
|
+
positions: number[],
|
|
142
|
+
size: number,
|
|
143
|
+
startIndex: number,
|
|
144
|
+
endIndex: number,
|
|
145
|
+
maxLatitude: number = DEFAULT_MAX_LATITUDE
|
|
146
|
+
): void {
|
|
147
|
+
// Check if the ring contains a pole
|
|
148
|
+
const firstLng = positions[startIndex];
|
|
149
|
+
const lastLng = positions[endIndex - size];
|
|
150
|
+
if (Math.abs(firstLng - lastLng) > 180) {
|
|
151
|
+
// The ring does not make a round trip
|
|
152
|
+
// Add the nearest pole to the vertices so that the polygon tesselates correctly
|
|
153
|
+
const p = getPointAtIndex(positions, 0, size, startIndex);
|
|
154
|
+
// Copy the first vertex to the world of the last vertex
|
|
155
|
+
p[0] += Math.round((lastLng - firstLng) / 360) * 360;
|
|
156
|
+
push(positions, p);
|
|
157
|
+
// Project the copied vertex to the edge of the map
|
|
158
|
+
p[1] = Math.sign(p[1]) * maxLatitude;
|
|
159
|
+
push(positions, p);
|
|
160
|
+
// Project the first vertex to the edge of the map
|
|
161
|
+
p[0] = firstLng;
|
|
162
|
+
push(positions, p);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function wrapLongitudesForShortestPath(
|
|
167
|
+
positions: NumericArray,
|
|
168
|
+
size: number,
|
|
169
|
+
startIndex: number,
|
|
170
|
+
endIndex: number
|
|
171
|
+
): void {
|
|
172
|
+
let prevLng: number = positions[0];
|
|
173
|
+
let lng: number;
|
|
174
|
+
for (let i = startIndex; i < endIndex; i += size) {
|
|
175
|
+
lng = positions[i];
|
|
176
|
+
const delta = lng - prevLng;
|
|
177
|
+
if (delta > 180 || delta < -180) {
|
|
178
|
+
lng -= Math.round(delta / 360) * 360;
|
|
179
|
+
}
|
|
180
|
+
positions[i] = prevLng = lng;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function shiftLongitudesIntoRange(positions: NumericArray, size: number): void {
|
|
185
|
+
let refLng: number;
|
|
186
|
+
const pointCount = positions.length / size;
|
|
187
|
+
|
|
188
|
+
// Find a longitude that is not on the edge of a world
|
|
189
|
+
// Which we will use to determine which world copy it is
|
|
190
|
+
for (let i = 0; i < pointCount; i++) {
|
|
191
|
+
refLng = positions[i * size];
|
|
192
|
+
if ((refLng + 180) % 360 !== 0) {
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const delta = -Math.round(refLng / 360) * 360;
|
|
198
|
+
if (delta === 0) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
for (let i = 0; i < pointCount; i++) {
|
|
202
|
+
positions[i * size] += delta;
|
|
203
|
+
}
|
|
204
|
+
}
|