@math.gl/geometry 5.0.0-beta.3 → 5.0.0-beta.5
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 +58 -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 +917 -514
- package/dist/culling/index.cjs.map +3 -3
- package/dist/culling/index.d.ts +5 -1
- package/dist/culling/index.d.ts.map +1 -1
- package/dist/culling/index.js +3 -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/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 +1225 -699
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- 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/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 +37 -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/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 +35 -0
- package/src/lighting/index.ts +10 -0
- package/src/lighting/lighting-types.ts +20 -0
- package/src/lighting/photometry.ts +264 -0
|
@@ -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
|
+
}
|