@math.gl/geometry 5.0.0-beta.1 → 5.0.0-beta.11
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 +61 -0
- package/THIRD-PARTY-NOTICES.md +1 -1
- package/dist/color/color-lut-types.d.ts +16 -0
- package/dist/color/color-lut-types.d.ts.map +1 -0
- package/dist/color/color-lut-types.js +5 -0
- package/dist/color/color-lut-types.js.map +1 -0
- package/dist/color/index.cjs +132 -0
- package/dist/color/index.cjs.map +6 -0
- package/dist/color/index.d.ts +4 -0
- package/dist/color/index.d.ts.map +1 -0
- package/dist/color/index.js +5 -0
- package/dist/color/index.js.map +1 -0
- package/dist/color/sample-lut.d.ts +13 -0
- package/dist/color/sample-lut.d.ts.map +1 -0
- package/dist/color/sample-lut.js +132 -0
- package/dist/color/sample-lut.js.map +1 -0
- package/dist/culling/bounds.d.ts +20 -0
- package/dist/culling/bounds.d.ts.map +1 -0
- package/dist/culling/bounds.js +121 -0
- package/dist/culling/bounds.js.map +1 -0
- package/dist/culling/frustum-planes.d.ts +5 -0
- package/dist/culling/frustum-planes.d.ts.map +1 -0
- package/dist/culling/frustum-planes.js +37 -0
- package/dist/culling/frustum-planes.js.map +1 -0
- package/dist/culling/geometric-queries.d.ts +34 -0
- package/dist/culling/geometric-queries.d.ts.map +1 -0
- package/dist/culling/geometric-queries.js +208 -0
- package/dist/culling/geometric-queries.js.map +1 -0
- package/dist/culling/index.cjs +944 -508
- package/dist/culling/index.cjs.map +3 -3
- package/dist/culling/index.d.ts +7 -1
- package/dist/culling/index.d.ts.map +1 -1
- package/dist/culling/index.js +4 -0
- package/dist/culling/index.js.map +1 -1
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +3 -3
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -1
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js +25 -8
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
- package/dist/culling/lib/culling-volume.d.ts +3 -0
- package/dist/culling/lib/culling-volume.d.ts.map +1 -1
- package/dist/culling/lib/culling-volume.js +7 -0
- package/dist/culling/lib/culling-volume.js.map +1 -1
- package/dist/culling/lib/ray.d.ts +5 -0
- package/dist/culling/lib/ray.d.ts.map +1 -1
- package/dist/culling/lib/ray.js +21 -0
- package/dist/culling/lib/ray.js.map +1 -1
- package/dist/culling/screen-ray.d.ts +22 -0
- package/dist/culling/screen-ray.d.ts.map +1 -0
- package/dist/culling/screen-ray.js +55 -0
- package/dist/culling/screen-ray.js.map +1 -0
- package/dist/curves/adaptive-sampling.d.ts +16 -0
- package/dist/curves/adaptive-sampling.d.ts.map +1 -0
- package/dist/curves/adaptive-sampling.js +59 -0
- package/dist/curves/adaptive-sampling.js.map +1 -0
- package/dist/curves/index.d.ts +4 -0
- package/dist/curves/index.d.ts.map +1 -1
- package/dist/curves/index.js +2 -0
- package/dist/curves/index.js.map +1 -1
- package/dist/curves/parallel-transport.d.ts +12 -0
- package/dist/curves/parallel-transport.d.ts.map +1 -0
- package/dist/curves/parallel-transport.js +83 -0
- package/dist/curves/parallel-transport.js.map +1 -0
- package/dist/index.cjs +1988 -934
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +17 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -2
- package/dist/index.js.map +1 -1
- package/dist/lighting/index.cjs +175 -0
- package/dist/lighting/index.cjs.map +6 -0
- package/dist/lighting/index.d.ts +4 -0
- package/dist/lighting/index.d.ts.map +1 -0
- package/dist/lighting/index.js +5 -0
- package/dist/lighting/index.js.map +1 -0
- package/dist/lighting/lighting-types.d.ts +16 -0
- package/dist/lighting/lighting-types.d.ts.map +1 -0
- package/dist/lighting/lighting-types.js +5 -0
- package/dist/lighting/lighting-types.js.map +1 -0
- package/dist/lighting/photometry.d.ts +36 -0
- package/dist/lighting/photometry.d.ts.map +1 -0
- package/dist/lighting/photometry.js +181 -0
- package/dist/lighting/photometry.js.map +1 -0
- package/dist/polygon/cut-by-grid.d.ts.map +1 -1
- package/dist/polygon/cut-by-grid.js +18 -0
- package/dist/polygon/cut-by-grid.js.map +1 -1
- package/dist/polygon/earcut.d.ts.map +1 -1
- package/dist/polygon/earcut.js +100 -34
- package/dist/polygon/earcut.js.map +1 -1
- package/dist/polygon/index.d.ts +2 -0
- package/dist/polygon/index.d.ts.map +1 -1
- package/dist/polygon/index.js +1 -0
- package/dist/polygon/index.js.map +1 -1
- package/dist/polygon/pole-of-inaccessibility.d.ts +21 -0
- package/dist/polygon/pole-of-inaccessibility.d.ts.map +1 -0
- package/dist/polygon/pole-of-inaccessibility.js +285 -0
- package/dist/polygon/pole-of-inaccessibility.js.map +1 -0
- package/dist/polygon/subdivide-triangle-mesh.d.ts +10 -0
- package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -1
- package/dist/polygon/subdivide-triangle-mesh.js +67 -19
- package/dist/polygon/subdivide-triangle-mesh.js.map +1 -1
- package/dist/utils/compose-attribute-provenance.d.ts +13 -0
- package/dist/utils/compose-attribute-provenance.d.ts.map +1 -0
- package/dist/utils/compose-attribute-provenance.js +63 -0
- package/dist/utils/compose-attribute-provenance.js.map +1 -0
- package/dist/utils/compute-vertex-normals.d.ts +2 -2
- package/dist/utils/compute-vertex-normals.d.ts.map +1 -1
- package/dist/utils/compute-vertex-normals.js +3 -4
- package/dist/utils/compute-vertex-normals.js.map +1 -1
- package/dist/utils/geometry.d.ts +11 -4
- package/dist/utils/geometry.d.ts.map +1 -1
- package/dist/utils/geometry.js +19 -2
- package/dist/utils/geometry.js.map +1 -1
- package/dist/utils/index.d.ts +1 -3
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +0 -2
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/mesh-processing.d.ts +16 -6
- package/dist/utils/mesh-processing.d.ts.map +1 -1
- package/dist/utils/mesh-processing.js +9 -6
- package/dist/utils/mesh-processing.js.map +1 -1
- package/dist/utils/primitive-iterator.d.ts +16 -8
- package/dist/utils/primitive-iterator.d.ts.map +1 -1
- package/dist/utils/primitive-iterator.js +32 -35
- package/dist/utils/primitive-iterator.js.map +1 -1
- package/dist/utils/project-bounds.d.ts +44 -0
- package/dist/utils/project-bounds.d.ts.map +1 -0
- package/dist/utils/project-bounds.js +93 -0
- package/dist/utils/project-bounds.js.map +1 -0
- package/dist/utils/project-texture-coordinates.d.ts +19 -0
- package/dist/utils/project-texture-coordinates.d.ts.map +1 -0
- package/dist/utils/project-texture-coordinates.js +49 -0
- package/dist/utils/project-texture-coordinates.js.map +1 -0
- package/dist/utils/qualify-surface-transition.d.ts +21 -0
- package/dist/utils/qualify-surface-transition.d.ts.map +1 -0
- package/dist/utils/qualify-surface-transition.js +140 -0
- package/dist/utils/qualify-surface-transition.js.map +1 -0
- package/package.json +15 -3
- package/src/color/color-lut-types.ts +19 -0
- package/src/color/index.ts +6 -0
- package/src/color/sample-lut.ts +175 -0
- package/src/culling/bounds.ts +152 -0
- package/src/culling/frustum-planes.ts +41 -0
- package/src/culling/geometric-queries.ts +299 -0
- package/src/culling/index.ts +39 -1
- package/src/culling/lib/bounding-volumes/axis-aligned-bounding-box.ts +26 -8
- package/src/culling/lib/culling-volume.ts +9 -1
- package/src/culling/lib/ray.ts +21 -0
- package/src/culling/screen-ray.ts +75 -0
- package/src/curves/adaptive-sampling.ts +85 -0
- package/src/curves/index.ts +4 -0
- package/src/curves/parallel-transport.ts +104 -0
- package/src/index.ts +71 -9
- package/src/lighting/index.ts +10 -0
- package/src/lighting/lighting-types.ts +20 -0
- package/src/lighting/photometry.ts +264 -0
- package/src/polygon/cut-by-grid.ts +18 -0
- package/src/polygon/earcut.ts +98 -37
- package/src/polygon/index.ts +6 -0
- package/src/polygon/pole-of-inaccessibility.ts +330 -0
- package/src/polygon/subdivide-triangle-mesh.ts +77 -17
- package/src/utils/compose-attribute-provenance.ts +74 -0
- package/src/utils/compute-vertex-normals.ts +5 -6
- package/src/utils/geometry.ts +32 -6
- package/src/utils/index.ts +5 -5
- package/src/utils/mesh-processing.ts +30 -14
- package/src/utils/primitive-iterator.ts +45 -43
- package/src/utils/project-bounds.ts +154 -0
- package/src/utils/project-texture-coordinates.ts +67 -0
- package/src/utils/qualify-surface-transition.ts +169 -0
- package/dist/utils/constants.d.ts +0 -52
- package/dist/utils/constants.d.ts.map +0 -1
- package/dist/utils/constants.js +0 -39
- package/dist/utils/constants.js.map +0 -1
- package/dist/utils/gl-type.d.ts +0 -17
- package/dist/utils/gl-type.d.ts.map +0 -1
- package/dist/utils/gl-type.js +0 -69
- package/dist/utils/gl-type.js.map +0 -1
- package/src/utils/constants.ts +0 -42
- package/src/utils/gl-type.ts +0 -87
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
import type {PhotometricProfile} from './lighting-types';
|
|
5
|
+
|
|
6
|
+
/** Samples a snapshotted polar/azimuth distribution in candela, using angles in degrees. */
|
|
7
|
+
export type PhotometricSampler = (verticalAngle: number, horizontalAngle: number) => number;
|
|
8
|
+
|
|
9
|
+
/** Options for a regular, renderer-independent angular grid. */
|
|
10
|
+
export type PhotometricResampleOptions = {
|
|
11
|
+
/** Samples from vertical 0 through 180 degrees, including both endpoints; default 181. */
|
|
12
|
+
verticalCount?: number;
|
|
13
|
+
/** Horizontal samples around [0, 360), excluding the duplicate seam; default 360. */
|
|
14
|
+
horizontalCount?: number;
|
|
15
|
+
/** Preserve candela or divide by the measured peak; default none. */
|
|
16
|
+
normalization?: 'none' | 'peak';
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Regular angular samples without texture objects or renderer coordinate transforms. */
|
|
20
|
+
export type PhotometricGrid = {
|
|
21
|
+
/** Vertical samples per row. */
|
|
22
|
+
readonly width: number;
|
|
23
|
+
/** Horizontal rows. */
|
|
24
|
+
readonly height: number;
|
|
25
|
+
/** Row-major intensity samples, vertical angle varying fastest. */
|
|
26
|
+
readonly data: Float64Array;
|
|
27
|
+
/** Vertical sample coordinates in degrees. */
|
|
28
|
+
readonly verticalAngles: Float64Array;
|
|
29
|
+
/** Horizontal sample coordinates in degrees. */
|
|
30
|
+
readonly horizontalAngles: Float64Array;
|
|
31
|
+
/** Physical candela or peak-relative values. */
|
|
32
|
+
readonly unit: 'candela' | 'relative';
|
|
33
|
+
/** Measured peak in candela, before optional normalization. */
|
|
34
|
+
readonly peakCandela: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Internal immutable snapshot of the measured distribution. */
|
|
38
|
+
type PreparedPhotometricProfile = {
|
|
39
|
+
/** Measured vertical angles. */
|
|
40
|
+
verticalAngles: Float64Array;
|
|
41
|
+
/** Measured horizontal angles. */
|
|
42
|
+
horizontalAngles: Float64Array;
|
|
43
|
+
/** Scaled candela values. */
|
|
44
|
+
values: Float64Array;
|
|
45
|
+
/** Measured horizontal extent, or zero for rotational symmetry. */
|
|
46
|
+
horizontalSpan: number;
|
|
47
|
+
/** Largest scaled intensity. */
|
|
48
|
+
peak: number;
|
|
49
|
+
};
|
|
50
|
+
const DEGREES_TO_RADIANS = Math.PI / 180;
|
|
51
|
+
const MAX_GRID_SAMPLES = 4_194_304;
|
|
52
|
+
|
|
53
|
+
/** Creates a reusable bilinear sampler; later mutations of the source profile have no effect. */
|
|
54
|
+
export function createPhotometricSampler(profile: PhotometricProfile): PhotometricSampler {
|
|
55
|
+
const prepared = preparePhotometricProfile(profile);
|
|
56
|
+
return (verticalAngle, horizontalAngle) => {
|
|
57
|
+
if (
|
|
58
|
+
!Number.isFinite(verticalAngle) ||
|
|
59
|
+
verticalAngle < 0 ||
|
|
60
|
+
verticalAngle > 180 ||
|
|
61
|
+
!Number.isFinite(horizontalAngle)
|
|
62
|
+
) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
'Photometry: sampling requires a vertical angle in [0, 180] and finite horizontal angle'
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return samplePreparedProfile(prepared, verticalAngle, horizontalAngle);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Integrates the piecewise bilinear angular distribution over solid angle, returning lumens. */
|
|
72
|
+
export function integrateLuminousFlux(profile: PhotometricProfile): number {
|
|
73
|
+
const prepared = preparePhotometricProfile(profile);
|
|
74
|
+
const {verticalAngles, horizontalAngles, values, horizontalSpan} = prepared;
|
|
75
|
+
const rows = new Float64Array(horizontalAngles.length);
|
|
76
|
+
for (let horizontalIndex = 0; horizontalIndex < rows.length; horizontalIndex++) {
|
|
77
|
+
for (let verticalIndex = 0; verticalIndex < verticalAngles.length - 1; verticalIndex++) {
|
|
78
|
+
const lowerAngle = verticalAngles[verticalIndex];
|
|
79
|
+
const upperAngle = verticalAngles[verticalIndex + 1];
|
|
80
|
+
const reflected = lowerAngle + upperAngle > 180;
|
|
81
|
+
const lower = reflected ? 180 - upperAngle : lowerAngle;
|
|
82
|
+
const upper = reflected ? 180 - lowerAngle : upperAngle;
|
|
83
|
+
const midpoint = ((lower + upper) / 2) * DEGREES_TO_RADIANS;
|
|
84
|
+
const halfWidth = ((upper - lower) / 2) * DEGREES_TO_RADIANS;
|
|
85
|
+
// Stable cos(h) - sin(h)/h, including very narrow angular intervals.
|
|
86
|
+
const squaredWidth = halfWidth * halfWidth;
|
|
87
|
+
const difference =
|
|
88
|
+
halfWidth < 0.001
|
|
89
|
+
? squaredWidth * (-1 / 3 + squaredWidth * (1 / 30 - squaredWidth / 840))
|
|
90
|
+
: Math.cos(halfWidth) - Math.sin(halfWidth) / halfWidth;
|
|
91
|
+
const sharedWeight = Math.sin(midpoint) * Math.sin(halfWidth);
|
|
92
|
+
const offsetWeight = Math.cos(midpoint) * difference;
|
|
93
|
+
const lowerWeight = Math.max(0, sharedWeight + offsetWeight);
|
|
94
|
+
const upperWeight = Math.max(0, sharedWeight - offsetWeight);
|
|
95
|
+
const offset = horizontalIndex * verticalAngles.length + verticalIndex;
|
|
96
|
+
rows[horizontalIndex] +=
|
|
97
|
+
values[offset] * (reflected ? upperWeight : lowerWeight) +
|
|
98
|
+
values[offset + 1] * (reflected ? lowerWeight : upperWeight);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
let flux = 0;
|
|
102
|
+
if (horizontalSpan === 0) flux = rows[0] * 2 * Math.PI;
|
|
103
|
+
else {
|
|
104
|
+
for (let index = 0; index < rows.length - 1; index++) {
|
|
105
|
+
const angularWidth =
|
|
106
|
+
(horizontalAngles[index + 1] - horizontalAngles[index]) * DEGREES_TO_RADIANS;
|
|
107
|
+
flux += (rows[index] / 2 + rows[index + 1] / 2) * angularWidth;
|
|
108
|
+
}
|
|
109
|
+
flux *= 360 / horizontalSpan;
|
|
110
|
+
}
|
|
111
|
+
if (!Number.isFinite(flux)) throw new Error('Photometry: luminous flux overflow');
|
|
112
|
+
return flux;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Resamples photometry; no image decoding, world-axis conversion or GPU allocation occurs. */
|
|
116
|
+
export function resamplePhotometricProfile(
|
|
117
|
+
profile: PhotometricProfile,
|
|
118
|
+
options: PhotometricResampleOptions = {}
|
|
119
|
+
): PhotometricGrid {
|
|
120
|
+
const {verticalCount = 181, horizontalCount = 360, normalization = 'none'} = options;
|
|
121
|
+
if (
|
|
122
|
+
!Number.isSafeInteger(verticalCount) ||
|
|
123
|
+
verticalCount < 2 ||
|
|
124
|
+
!Number.isSafeInteger(horizontalCount) ||
|
|
125
|
+
horizontalCount < 1 ||
|
|
126
|
+
verticalCount * horizontalCount > MAX_GRID_SAMPLES ||
|
|
127
|
+
!['none', 'peak'].includes(normalization)
|
|
128
|
+
) {
|
|
129
|
+
throw new Error('Photometry: invalid resampling dimensions or normalization');
|
|
130
|
+
}
|
|
131
|
+
const prepared = preparePhotometricProfile(profile);
|
|
132
|
+
const verticalAngles = Float64Array.from(
|
|
133
|
+
{length: verticalCount},
|
|
134
|
+
(_, index) => (index * 180) / (verticalCount - 1)
|
|
135
|
+
);
|
|
136
|
+
const horizontalAngles = Float64Array.from(
|
|
137
|
+
{length: horizontalCount},
|
|
138
|
+
(_, index) => (index * 360) / horizontalCount
|
|
139
|
+
);
|
|
140
|
+
const data = new Float64Array(verticalCount * horizontalCount);
|
|
141
|
+
for (let horizontalIndex = 0; horizontalIndex < horizontalCount; horizontalIndex++) {
|
|
142
|
+
for (let verticalIndex = 0; verticalIndex < verticalCount; verticalIndex++) {
|
|
143
|
+
const intensity = samplePreparedProfile(
|
|
144
|
+
prepared,
|
|
145
|
+
verticalAngles[verticalIndex],
|
|
146
|
+
horizontalAngles[horizontalIndex]
|
|
147
|
+
);
|
|
148
|
+
data[horizontalIndex * verticalCount + verticalIndex] =
|
|
149
|
+
normalization === 'peak' ? (prepared.peak > 0 ? intensity / prepared.peak : 0) : intensity;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
width: verticalCount,
|
|
154
|
+
height: horizontalCount,
|
|
155
|
+
data,
|
|
156
|
+
verticalAngles,
|
|
157
|
+
horizontalAngles,
|
|
158
|
+
unit: normalization === 'peak' ? 'relative' : 'candela',
|
|
159
|
+
peakCandela: prepared.peak
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Validates and snapshots only the angular and intensity fields used by the calculations. */
|
|
164
|
+
function preparePhotometricProfile(profile: PhotometricProfile): PreparedPhotometricProfile {
|
|
165
|
+
const {verticalAngles, horizontalAngles, intensities, intensityScale = 1, symmetry} = profile;
|
|
166
|
+
if (
|
|
167
|
+
verticalAngles.length < 2 ||
|
|
168
|
+
horizontalAngles.length < 1 ||
|
|
169
|
+
intensities.length !== verticalAngles.length * horizontalAngles.length ||
|
|
170
|
+
!Number.isFinite(intensityScale) ||
|
|
171
|
+
intensityScale < 0
|
|
172
|
+
) {
|
|
173
|
+
throw new Error('Photometry: invalid calculation profile');
|
|
174
|
+
}
|
|
175
|
+
for (const [angles, maximum] of [
|
|
176
|
+
[verticalAngles, 180],
|
|
177
|
+
[horizontalAngles, 360]
|
|
178
|
+
] as const) {
|
|
179
|
+
for (let index = 0; index < angles.length; index++) {
|
|
180
|
+
if (
|
|
181
|
+
!Number.isFinite(angles[index]) ||
|
|
182
|
+
angles[index] < 0 ||
|
|
183
|
+
angles[index] > maximum ||
|
|
184
|
+
(index > 0 && angles[index] <= angles[index - 1])
|
|
185
|
+
)
|
|
186
|
+
throw new Error('Photometry: invalid calculation angles');
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const first = horizontalAngles[0];
|
|
190
|
+
const last = horizontalAngles[horizontalAngles.length - 1];
|
|
191
|
+
const horizontalSpan = last - first;
|
|
192
|
+
const validSymmetry =
|
|
193
|
+
(symmetry === 'rotational' && horizontalAngles.length === 1) ||
|
|
194
|
+
(symmetry === 'quadrant' && horizontalAngles.length > 1 && first === 0 && last === 90) ||
|
|
195
|
+
(symmetry === 'bilateral' && horizontalAngles.length > 1 && horizontalSpan === 180) ||
|
|
196
|
+
(symmetry === 'none' && horizontalAngles.length > 1 && first === 0 && last === 360);
|
|
197
|
+
if (!validSymmetry) throw new Error('Photometry: symmetry does not match horizontal coverage');
|
|
198
|
+
let peak = 0;
|
|
199
|
+
const values = new Float64Array(intensities.length);
|
|
200
|
+
for (let index = 0; index < values.length; index++) {
|
|
201
|
+
const value = intensities[index];
|
|
202
|
+
const scaled = value * intensityScale;
|
|
203
|
+
if (!Number.isFinite(value) || value < 0 || !Number.isFinite(scaled))
|
|
204
|
+
throw new Error('Photometry: invalid calculation intensity');
|
|
205
|
+
values[index] = scaled;
|
|
206
|
+
peak = Math.max(peak, scaled);
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
verticalAngles: Float64Array.from(verticalAngles),
|
|
210
|
+
horizontalAngles: Float64Array.from(horizontalAngles),
|
|
211
|
+
values,
|
|
212
|
+
horizontalSpan,
|
|
213
|
+
peak
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Samples a validated snapshot, returning zero outside its measured vertical extent. */
|
|
218
|
+
function samplePreparedProfile(
|
|
219
|
+
profile: PreparedPhotometricProfile,
|
|
220
|
+
verticalAngle: number,
|
|
221
|
+
horizontalAngle: number
|
|
222
|
+
): number {
|
|
223
|
+
const {verticalAngles, horizontalAngles, values, horizontalSpan} = profile;
|
|
224
|
+
if (
|
|
225
|
+
verticalAngle < verticalAngles[0] ||
|
|
226
|
+
verticalAngle > verticalAngles[verticalAngles.length - 1]
|
|
227
|
+
)
|
|
228
|
+
return 0;
|
|
229
|
+
const verticalIndex = findLowerAngle(verticalAngles, verticalAngle);
|
|
230
|
+
const verticalWeight =
|
|
231
|
+
(verticalAngle - verticalAngles[verticalIndex]) /
|
|
232
|
+
(verticalAngles[verticalIndex + 1] - verticalAngles[verticalIndex]);
|
|
233
|
+
let horizontalIndex = 0;
|
|
234
|
+
let horizontalWeight = 0;
|
|
235
|
+
if (horizontalSpan > 0) {
|
|
236
|
+
const period = horizontalSpan === 360 ? 360 : horizontalSpan * 2;
|
|
237
|
+
let folded = (((horizontalAngle - horizontalAngles[0]) % period) + period) % period;
|
|
238
|
+
if (folded > horizontalSpan) folded = period - folded;
|
|
239
|
+
const measuredAngle = horizontalAngles[0] + folded;
|
|
240
|
+
horizontalIndex = findLowerAngle(horizontalAngles, measuredAngle);
|
|
241
|
+
horizontalWeight =
|
|
242
|
+
(measuredAngle - horizontalAngles[horizontalIndex]) /
|
|
243
|
+
(horizontalAngles[horizontalIndex + 1] - horizontalAngles[horizontalIndex]);
|
|
244
|
+
}
|
|
245
|
+
const lowerOffset = horizontalIndex * verticalAngles.length + verticalIndex;
|
|
246
|
+
const upperOffset = horizontalSpan === 0 ? lowerOffset : lowerOffset + verticalAngles.length;
|
|
247
|
+
const lowerValue =
|
|
248
|
+
values[lowerOffset] + (values[lowerOffset + 1] - values[lowerOffset]) * verticalWeight;
|
|
249
|
+
const upperValue =
|
|
250
|
+
values[upperOffset] + (values[upperOffset + 1] - values[upperOffset]) * verticalWeight;
|
|
251
|
+
return lowerValue + (upperValue - lowerValue) * horizontalWeight;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Finds the lower knot, keeping the final coordinate in the last interval. */
|
|
255
|
+
function findLowerAngle(angles: Float64Array, angle: number): number {
|
|
256
|
+
let lower = 0;
|
|
257
|
+
let upper = angles.length - 1;
|
|
258
|
+
while (upper - lower > 1) {
|
|
259
|
+
const midpoint = Math.floor((lower + upper) / 2);
|
|
260
|
+
if (angle < angles[midpoint]) upper = midpoint;
|
|
261
|
+
else lower = midpoint;
|
|
262
|
+
}
|
|
263
|
+
return Math.min(lower, angles.length - 2);
|
|
264
|
+
}
|
|
@@ -130,6 +130,7 @@ export function cutPolygonByGrid(
|
|
|
130
130
|
// Get the bounding box of the outer polygon
|
|
131
131
|
getBoundingBox(pos, size, holes[0] || pos.length, bbox);
|
|
132
132
|
cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
|
|
133
|
+
containPoint(cell, bbox[0], gridResolution);
|
|
133
134
|
const code = bitCode(bbox[1], cell);
|
|
134
135
|
|
|
135
136
|
if (code) {
|
|
@@ -258,6 +259,23 @@ function getGridCell(
|
|
|
258
259
|
return out as BoundingBox;
|
|
259
260
|
}
|
|
260
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Moves a cell by one step so that it contains `p` on its low edges and excludes it on its high edges.
|
|
264
|
+
* `getGridCell` divides by the grid resolution, and the rounded quotient can select a neighbor cell
|
|
265
|
+
* when `p` is on a cut line. Bisecting that cell again leaves one side empty.
|
|
266
|
+
*/
|
|
267
|
+
function containPoint(cell: number[], p: number[], gridResolution: number): void {
|
|
268
|
+
for (let axis = 0; axis < 2; axis++) {
|
|
269
|
+
if (p[axis] < cell[axis]) {
|
|
270
|
+
cell[axis + 2] = cell[axis];
|
|
271
|
+
cell[axis] -= gridResolution;
|
|
272
|
+
} else if (p[axis] >= cell[axis + 2]) {
|
|
273
|
+
cell[axis] = cell[axis + 2];
|
|
274
|
+
cell[axis + 2] += gridResolution;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
261
279
|
function moveToNeighborCell(cell: number[], gridResolution: number, edge: number): void {
|
|
262
280
|
if (edge & 8) {
|
|
263
281
|
// top
|
package/src/polygon/earcut.ts
CHANGED
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
/* eslint-disable */
|
|
34
34
|
|
|
35
35
|
import type {NumericArray} from '@math.gl/core';
|
|
36
|
-
import {
|
|
36
|
+
import {DimIndex, Plane2D} from './polygon-utils';
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
39
|
* Computes a triangulation of a polygon
|
|
@@ -51,6 +51,25 @@ export function earcut(
|
|
|
51
51
|
areas?: NumericArray,
|
|
52
52
|
plane: Plane2D = 'xy'
|
|
53
53
|
): number[] {
|
|
54
|
+
// Keep nested calls from numeric array getters isolated.
|
|
55
|
+
const savedIndex = vertexPoolIndex;
|
|
56
|
+
const savedQueue = activeHoleQueue;
|
|
57
|
+
if (!holeQueues[callDepth]) holeQueues[callDepth] = [];
|
|
58
|
+
activeHoleQueue = holeQueues[callDepth];
|
|
59
|
+
callDepth++;
|
|
60
|
+
try {
|
|
61
|
+
return triangulate(positions, holeIndices, dim, areas, plane);
|
|
62
|
+
} finally {
|
|
63
|
+
activeHoleQueue.length = 0;
|
|
64
|
+
activeHoleQueue = savedQueue;
|
|
65
|
+
vertexPoolIndex = savedIndex;
|
|
66
|
+
callDepth--;
|
|
67
|
+
// Do not retain arbitrarily large object graphs after a one-off polygon.
|
|
68
|
+
if (callDepth === 0 && vertexPool.length > 65536) vertexPool.length = 0;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function triangulate(positions, holeIndices, dim, areas, plane): number[] {
|
|
54
73
|
const hasHoles = holeIndices && holeIndices.length;
|
|
55
74
|
const outerLen = hasHoles ? holeIndices[0] * dim : positions.length;
|
|
56
75
|
let outerNode = linkedList(positions, 0, outerLen, dim, true, areas && areas[0], plane);
|
|
@@ -70,12 +89,14 @@ export function earcut(
|
|
|
70
89
|
|
|
71
90
|
// if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
|
|
72
91
|
if (positions.length > 80 * dim) {
|
|
73
|
-
|
|
74
|
-
|
|
92
|
+
const i0 = DimIndex[plane[0]];
|
|
93
|
+
const i1 = DimIndex[plane[1]];
|
|
94
|
+
minX = maxX = positions[i0];
|
|
95
|
+
minY = maxY = positions[i1];
|
|
75
96
|
|
|
76
97
|
for (let i = dim; i < outerLen; i += dim) {
|
|
77
|
-
x = positions[i];
|
|
78
|
-
y = positions[i +
|
|
98
|
+
x = positions[i + i0];
|
|
99
|
+
y = positions[i + i1];
|
|
79
100
|
if (x < minX) minX = x;
|
|
80
101
|
if (y < minY) minY = y;
|
|
81
102
|
if (x > maxX) maxX = x;
|
|
@@ -87,7 +108,7 @@ export function earcut(
|
|
|
87
108
|
invSize = invSize !== 0 ? 32767 / invSize : 0;
|
|
88
109
|
}
|
|
89
110
|
|
|
90
|
-
earcutLinked(outerNode, triangles,
|
|
111
|
+
earcutLinked(outerNode, triangles, minX, minY, invSize, 0);
|
|
91
112
|
|
|
92
113
|
return triangles;
|
|
93
114
|
}
|
|
@@ -102,22 +123,40 @@ function linkedList(
|
|
|
102
123
|
area: number | undefined,
|
|
103
124
|
plane: Plane2D
|
|
104
125
|
): Vertex {
|
|
126
|
+
if (start >= end) return null;
|
|
105
127
|
let i;
|
|
106
128
|
let last;
|
|
129
|
+
const i0 = DimIndex[plane[0]];
|
|
130
|
+
const i1 = DimIndex[plane[1]];
|
|
107
131
|
if (area === undefined) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
132
|
+
// Combine coordinate reads and area accumulation, as in loaders.gl.
|
|
133
|
+
let sum = 0;
|
|
134
|
+
let previousX = data[end - dim + i0];
|
|
135
|
+
let previousY = data[end - dim + i1];
|
|
136
|
+
for (i = start; i < end; i += dim) {
|
|
137
|
+
const x = data[i + i0];
|
|
138
|
+
const y = data[i + i1];
|
|
139
|
+
sum += (x - previousX) * (y + previousY);
|
|
140
|
+
last = insertNode(i / dim, x, y, last);
|
|
141
|
+
previousX = x;
|
|
142
|
+
previousY = y;
|
|
143
|
+
}
|
|
144
|
+
if (last && clockwise !== sum / 2 < 0) {
|
|
145
|
+
let node = last;
|
|
146
|
+
do {
|
|
147
|
+
const next = node.next;
|
|
148
|
+
node.next = node.prev;
|
|
149
|
+
node.prev = next;
|
|
150
|
+
node = next;
|
|
151
|
+
} while (node !== last);
|
|
152
|
+
// Preserve the starting vertex and triangle order of reverse insertion.
|
|
153
|
+
last = last.prev;
|
|
154
|
+
}
|
|
155
|
+
} else if (clockwise === area < 0) {
|
|
156
|
+
for (i = start; i < end; i += dim) last = insertNode(i / dim, data[i + i0], data[i + i1], last);
|
|
118
157
|
} else {
|
|
119
158
|
for (i = end - dim; i >= start; i -= dim)
|
|
120
|
-
last = insertNode(i, data[i + i0], data[i + i1], last);
|
|
159
|
+
last = insertNode(i / dim, data[i + i0], data[i + i1], last);
|
|
121
160
|
}
|
|
122
161
|
|
|
123
162
|
if (last && equals(last, last.next)) {
|
|
@@ -152,7 +191,7 @@ function filterPoints(start, end?) {
|
|
|
152
191
|
}
|
|
153
192
|
|
|
154
193
|
// main ear slicing loop which triangulates a polygon (given as a linked list)
|
|
155
|
-
function earcutLinked(ear, triangles,
|
|
194
|
+
function earcutLinked(ear, triangles, minX, minY, invSize, pass?) {
|
|
156
195
|
if (!ear) return;
|
|
157
196
|
|
|
158
197
|
// interlink polygon nodes in z-order
|
|
@@ -169,9 +208,9 @@ function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass?) {
|
|
|
169
208
|
|
|
170
209
|
if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
|
|
171
210
|
// cut off the triangle
|
|
172
|
-
triangles.push(
|
|
173
|
-
triangles.push(
|
|
174
|
-
triangles.push(
|
|
211
|
+
triangles.push(prev.i);
|
|
212
|
+
triangles.push(ear.i);
|
|
213
|
+
triangles.push(next.i);
|
|
175
214
|
|
|
176
215
|
removeNode(ear);
|
|
177
216
|
|
|
@@ -188,16 +227,16 @@ function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass?) {
|
|
|
188
227
|
if (ear === stop) {
|
|
189
228
|
// try filtering points and slicing again
|
|
190
229
|
if (!pass) {
|
|
191
|
-
earcutLinked(filterPoints(ear), triangles,
|
|
230
|
+
earcutLinked(filterPoints(ear), triangles, minX, minY, invSize, 1);
|
|
192
231
|
|
|
193
232
|
// if this didn't work, try curing all small self-intersections locally
|
|
194
233
|
} else if (pass === 1) {
|
|
195
|
-
ear = cureLocalIntersections(filterPoints(ear), triangles
|
|
196
|
-
earcutLinked(ear, triangles,
|
|
234
|
+
ear = cureLocalIntersections(filterPoints(ear), triangles);
|
|
235
|
+
earcutLinked(ear, triangles, minX, minY, invSize, 2);
|
|
197
236
|
|
|
198
237
|
// as a last resort, try splitting the remaining polygon into two
|
|
199
238
|
} else if (pass === 2) {
|
|
200
|
-
splitEarcut(ear, triangles,
|
|
239
|
+
splitEarcut(ear, triangles, minX, minY, invSize);
|
|
201
240
|
}
|
|
202
241
|
|
|
203
242
|
break;
|
|
@@ -336,7 +375,7 @@ function isEarHashed(ear, minX, minY, invSize) {
|
|
|
336
375
|
}
|
|
337
376
|
|
|
338
377
|
// go through all polygon nodes and cure small local self-intersections
|
|
339
|
-
function cureLocalIntersections(start, triangles
|
|
378
|
+
function cureLocalIntersections(start, triangles) {
|
|
340
379
|
let p = start;
|
|
341
380
|
do {
|
|
342
381
|
const a = p.prev;
|
|
@@ -348,9 +387,9 @@ function cureLocalIntersections(start, triangles, dim) {
|
|
|
348
387
|
locallyInside(a, b) &&
|
|
349
388
|
locallyInside(b, a)
|
|
350
389
|
) {
|
|
351
|
-
triangles.push(
|
|
352
|
-
triangles.push(
|
|
353
|
-
triangles.push(
|
|
390
|
+
triangles.push(a.i);
|
|
391
|
+
triangles.push(p.i);
|
|
392
|
+
triangles.push(b.i);
|
|
354
393
|
|
|
355
394
|
// remove two nodes involved
|
|
356
395
|
removeNode(p);
|
|
@@ -365,7 +404,7 @@ function cureLocalIntersections(start, triangles, dim) {
|
|
|
365
404
|
}
|
|
366
405
|
|
|
367
406
|
// try splitting polygon into two and triangulate them independently
|
|
368
|
-
function splitEarcut(start, triangles,
|
|
407
|
+
function splitEarcut(start, triangles, minX, minY, invSize) {
|
|
369
408
|
// look for a valid diagonal that divides the polygon into two
|
|
370
409
|
let a = start;
|
|
371
410
|
do {
|
|
@@ -380,8 +419,8 @@ function splitEarcut(start, triangles, dim, minX, minY, invSize) {
|
|
|
380
419
|
c = filterPoints(c, c.next);
|
|
381
420
|
|
|
382
421
|
// run earcut on each half
|
|
383
|
-
earcutLinked(a, triangles,
|
|
384
|
-
earcutLinked(c, triangles,
|
|
422
|
+
earcutLinked(a, triangles, minX, minY, invSize, 0);
|
|
423
|
+
earcutLinked(c, triangles, minX, minY, invSize, 0);
|
|
385
424
|
return;
|
|
386
425
|
}
|
|
387
426
|
b = b.next;
|
|
@@ -399,7 +438,8 @@ function eliminateHoles(
|
|
|
399
438
|
areas: NumericArray | undefined,
|
|
400
439
|
plane: Plane2D
|
|
401
440
|
): Vertex {
|
|
402
|
-
const queue =
|
|
441
|
+
const queue = activeHoleQueue;
|
|
442
|
+
queue.length = 0;
|
|
403
443
|
let i;
|
|
404
444
|
let len;
|
|
405
445
|
let start;
|
|
@@ -410,6 +450,7 @@ function eliminateHoles(
|
|
|
410
450
|
start = holeIndices[i] * dim;
|
|
411
451
|
end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
|
|
412
452
|
list = linkedList(data, start, end, dim, false, areas && areas[i + 1], plane);
|
|
453
|
+
if (!list) continue;
|
|
413
454
|
if (list === list.next) list.steiner = true;
|
|
414
455
|
queue.push(getLeftmost(list));
|
|
415
456
|
}
|
|
@@ -724,8 +765,8 @@ function middleInside(a, b) {
|
|
|
724
765
|
// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
|
|
725
766
|
// if one belongs to the outer ring and another to a hole, it merges it into a single ring
|
|
726
767
|
function splitPolygon(a, b) {
|
|
727
|
-
const a2 =
|
|
728
|
-
const b2 =
|
|
768
|
+
const a2 = allocateVertex(a.i, a.x, a.y);
|
|
769
|
+
const b2 = allocateVertex(b.i, b.x, b.y);
|
|
729
770
|
const an = a.next;
|
|
730
771
|
const bp = b.prev;
|
|
731
772
|
|
|
@@ -746,7 +787,7 @@ function splitPolygon(a, b) {
|
|
|
746
787
|
|
|
747
788
|
// create a node and optionally link it with previous one (in a circular doubly linked list)
|
|
748
789
|
function insertNode(i, x, y, last) {
|
|
749
|
-
const p =
|
|
790
|
+
const p = allocateVertex(i, x, y);
|
|
750
791
|
|
|
751
792
|
if (!last) {
|
|
752
793
|
p.prev = p;
|
|
@@ -768,8 +809,28 @@ function removeNode(p) {
|
|
|
768
809
|
if (p.nextZ) p.nextZ.prevZ = p.prevZ;
|
|
769
810
|
}
|
|
770
811
|
|
|
812
|
+
// Reuse scratch nodes and queues across calls; no input or output arrays are retained.
|
|
813
|
+
const vertexPool: Vertex[] = [];
|
|
814
|
+
let vertexPoolIndex = 0;
|
|
815
|
+
const holeQueues: Vertex[][] = [];
|
|
816
|
+
let activeHoleQueue: Vertex[] = [];
|
|
817
|
+
let callDepth = 0;
|
|
818
|
+
|
|
819
|
+
function allocateVertex(i: number, x: number, y: number): Vertex {
|
|
820
|
+
let vertex = vertexPool[vertexPoolIndex];
|
|
821
|
+
if (!vertex) vertexPool[vertexPoolIndex] = vertex = new Vertex(i, x, y);
|
|
822
|
+
vertexPoolIndex++;
|
|
823
|
+
vertex.i = i;
|
|
824
|
+
vertex.x = x;
|
|
825
|
+
vertex.y = y;
|
|
826
|
+
vertex.prev = vertex.next = vertex.prevZ = vertex.nextZ = null;
|
|
827
|
+
vertex.z = 0;
|
|
828
|
+
vertex.steiner = false;
|
|
829
|
+
return vertex;
|
|
830
|
+
}
|
|
831
|
+
|
|
771
832
|
class Vertex {
|
|
772
|
-
// vertex index
|
|
833
|
+
// vertex index, independent of coordinate stride
|
|
773
834
|
i: number;
|
|
774
835
|
|
|
775
836
|
// vertex coordinates
|
package/src/polygon/index.ts
CHANGED
|
@@ -16,6 +16,12 @@ export {earcut} from './earcut';
|
|
|
16
16
|
|
|
17
17
|
export {clipPolygon, clipPolyline} from './lineclip';
|
|
18
18
|
|
|
19
|
+
export {
|
|
20
|
+
getPoleOfInaccessibility,
|
|
21
|
+
DEFAULT_POLE_SEARCH_SEGMENT_TESTS
|
|
22
|
+
} from './pole-of-inaccessibility';
|
|
23
|
+
export type {PoleSearchBudget} from './pole-of-inaccessibility';
|
|
24
|
+
|
|
19
25
|
export {cutPolygonByGrid, cutPolylineByGrid} from './cut-by-grid';
|
|
20
26
|
|
|
21
27
|
export {cutPolylineByMercatorBounds, cutPolygonByMercatorBounds} from './cut-by-mercator-bounds';
|