@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.
Files changed (179) hide show
  1. package/README.md +61 -0
  2. package/THIRD-PARTY-NOTICES.md +1 -1
  3. package/dist/color/color-lut-types.d.ts +16 -0
  4. package/dist/color/color-lut-types.d.ts.map +1 -0
  5. package/dist/color/color-lut-types.js +5 -0
  6. package/dist/color/color-lut-types.js.map +1 -0
  7. package/dist/color/index.cjs +132 -0
  8. package/dist/color/index.cjs.map +6 -0
  9. package/dist/color/index.d.ts +4 -0
  10. package/dist/color/index.d.ts.map +1 -0
  11. package/dist/color/index.js +5 -0
  12. package/dist/color/index.js.map +1 -0
  13. package/dist/color/sample-lut.d.ts +13 -0
  14. package/dist/color/sample-lut.d.ts.map +1 -0
  15. package/dist/color/sample-lut.js +132 -0
  16. package/dist/color/sample-lut.js.map +1 -0
  17. package/dist/culling/bounds.d.ts +20 -0
  18. package/dist/culling/bounds.d.ts.map +1 -0
  19. package/dist/culling/bounds.js +121 -0
  20. package/dist/culling/bounds.js.map +1 -0
  21. package/dist/culling/frustum-planes.d.ts +5 -0
  22. package/dist/culling/frustum-planes.d.ts.map +1 -0
  23. package/dist/culling/frustum-planes.js +37 -0
  24. package/dist/culling/frustum-planes.js.map +1 -0
  25. package/dist/culling/geometric-queries.d.ts +34 -0
  26. package/dist/culling/geometric-queries.d.ts.map +1 -0
  27. package/dist/culling/geometric-queries.js +208 -0
  28. package/dist/culling/geometric-queries.js.map +1 -0
  29. package/dist/culling/index.cjs +944 -508
  30. package/dist/culling/index.cjs.map +3 -3
  31. package/dist/culling/index.d.ts +7 -1
  32. package/dist/culling/index.d.ts.map +1 -1
  33. package/dist/culling/index.js +4 -0
  34. package/dist/culling/index.js.map +1 -1
  35. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +3 -3
  36. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -1
  37. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js +25 -8
  38. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -1
  39. package/dist/culling/lib/culling-volume.d.ts +3 -0
  40. package/dist/culling/lib/culling-volume.d.ts.map +1 -1
  41. package/dist/culling/lib/culling-volume.js +7 -0
  42. package/dist/culling/lib/culling-volume.js.map +1 -1
  43. package/dist/culling/lib/ray.d.ts +5 -0
  44. package/dist/culling/lib/ray.d.ts.map +1 -1
  45. package/dist/culling/lib/ray.js +21 -0
  46. package/dist/culling/lib/ray.js.map +1 -1
  47. package/dist/culling/screen-ray.d.ts +22 -0
  48. package/dist/culling/screen-ray.d.ts.map +1 -0
  49. package/dist/culling/screen-ray.js +55 -0
  50. package/dist/culling/screen-ray.js.map +1 -0
  51. package/dist/curves/adaptive-sampling.d.ts +16 -0
  52. package/dist/curves/adaptive-sampling.d.ts.map +1 -0
  53. package/dist/curves/adaptive-sampling.js +59 -0
  54. package/dist/curves/adaptive-sampling.js.map +1 -0
  55. package/dist/curves/index.d.ts +4 -0
  56. package/dist/curves/index.d.ts.map +1 -1
  57. package/dist/curves/index.js +2 -0
  58. package/dist/curves/index.js.map +1 -1
  59. package/dist/curves/parallel-transport.d.ts +12 -0
  60. package/dist/curves/parallel-transport.d.ts.map +1 -0
  61. package/dist/curves/parallel-transport.js +83 -0
  62. package/dist/curves/parallel-transport.js.map +1 -0
  63. package/dist/index.cjs +1988 -934
  64. package/dist/index.cjs.map +3 -3
  65. package/dist/index.d.ts +17 -5
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +9 -2
  68. package/dist/index.js.map +1 -1
  69. package/dist/lighting/index.cjs +175 -0
  70. package/dist/lighting/index.cjs.map +6 -0
  71. package/dist/lighting/index.d.ts +4 -0
  72. package/dist/lighting/index.d.ts.map +1 -0
  73. package/dist/lighting/index.js +5 -0
  74. package/dist/lighting/index.js.map +1 -0
  75. package/dist/lighting/lighting-types.d.ts +16 -0
  76. package/dist/lighting/lighting-types.d.ts.map +1 -0
  77. package/dist/lighting/lighting-types.js +5 -0
  78. package/dist/lighting/lighting-types.js.map +1 -0
  79. package/dist/lighting/photometry.d.ts +36 -0
  80. package/dist/lighting/photometry.d.ts.map +1 -0
  81. package/dist/lighting/photometry.js +181 -0
  82. package/dist/lighting/photometry.js.map +1 -0
  83. package/dist/polygon/cut-by-grid.d.ts.map +1 -1
  84. package/dist/polygon/cut-by-grid.js +18 -0
  85. package/dist/polygon/cut-by-grid.js.map +1 -1
  86. package/dist/polygon/earcut.d.ts.map +1 -1
  87. package/dist/polygon/earcut.js +100 -34
  88. package/dist/polygon/earcut.js.map +1 -1
  89. package/dist/polygon/index.d.ts +2 -0
  90. package/dist/polygon/index.d.ts.map +1 -1
  91. package/dist/polygon/index.js +1 -0
  92. package/dist/polygon/index.js.map +1 -1
  93. package/dist/polygon/pole-of-inaccessibility.d.ts +21 -0
  94. package/dist/polygon/pole-of-inaccessibility.d.ts.map +1 -0
  95. package/dist/polygon/pole-of-inaccessibility.js +285 -0
  96. package/dist/polygon/pole-of-inaccessibility.js.map +1 -0
  97. package/dist/polygon/subdivide-triangle-mesh.d.ts +10 -0
  98. package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -1
  99. package/dist/polygon/subdivide-triangle-mesh.js +67 -19
  100. package/dist/polygon/subdivide-triangle-mesh.js.map +1 -1
  101. package/dist/utils/compose-attribute-provenance.d.ts +13 -0
  102. package/dist/utils/compose-attribute-provenance.d.ts.map +1 -0
  103. package/dist/utils/compose-attribute-provenance.js +63 -0
  104. package/dist/utils/compose-attribute-provenance.js.map +1 -0
  105. package/dist/utils/compute-vertex-normals.d.ts +2 -2
  106. package/dist/utils/compute-vertex-normals.d.ts.map +1 -1
  107. package/dist/utils/compute-vertex-normals.js +3 -4
  108. package/dist/utils/compute-vertex-normals.js.map +1 -1
  109. package/dist/utils/geometry.d.ts +11 -4
  110. package/dist/utils/geometry.d.ts.map +1 -1
  111. package/dist/utils/geometry.js +19 -2
  112. package/dist/utils/geometry.js.map +1 -1
  113. package/dist/utils/index.d.ts +1 -3
  114. package/dist/utils/index.d.ts.map +1 -1
  115. package/dist/utils/index.js +0 -2
  116. package/dist/utils/index.js.map +1 -1
  117. package/dist/utils/mesh-processing.d.ts +16 -6
  118. package/dist/utils/mesh-processing.d.ts.map +1 -1
  119. package/dist/utils/mesh-processing.js +9 -6
  120. package/dist/utils/mesh-processing.js.map +1 -1
  121. package/dist/utils/primitive-iterator.d.ts +16 -8
  122. package/dist/utils/primitive-iterator.d.ts.map +1 -1
  123. package/dist/utils/primitive-iterator.js +32 -35
  124. package/dist/utils/primitive-iterator.js.map +1 -1
  125. package/dist/utils/project-bounds.d.ts +44 -0
  126. package/dist/utils/project-bounds.d.ts.map +1 -0
  127. package/dist/utils/project-bounds.js +93 -0
  128. package/dist/utils/project-bounds.js.map +1 -0
  129. package/dist/utils/project-texture-coordinates.d.ts +19 -0
  130. package/dist/utils/project-texture-coordinates.d.ts.map +1 -0
  131. package/dist/utils/project-texture-coordinates.js +49 -0
  132. package/dist/utils/project-texture-coordinates.js.map +1 -0
  133. package/dist/utils/qualify-surface-transition.d.ts +21 -0
  134. package/dist/utils/qualify-surface-transition.d.ts.map +1 -0
  135. package/dist/utils/qualify-surface-transition.js +140 -0
  136. package/dist/utils/qualify-surface-transition.js.map +1 -0
  137. package/package.json +15 -3
  138. package/src/color/color-lut-types.ts +19 -0
  139. package/src/color/index.ts +6 -0
  140. package/src/color/sample-lut.ts +175 -0
  141. package/src/culling/bounds.ts +152 -0
  142. package/src/culling/frustum-planes.ts +41 -0
  143. package/src/culling/geometric-queries.ts +299 -0
  144. package/src/culling/index.ts +39 -1
  145. package/src/culling/lib/bounding-volumes/axis-aligned-bounding-box.ts +26 -8
  146. package/src/culling/lib/culling-volume.ts +9 -1
  147. package/src/culling/lib/ray.ts +21 -0
  148. package/src/culling/screen-ray.ts +75 -0
  149. package/src/curves/adaptive-sampling.ts +85 -0
  150. package/src/curves/index.ts +4 -0
  151. package/src/curves/parallel-transport.ts +104 -0
  152. package/src/index.ts +71 -9
  153. package/src/lighting/index.ts +10 -0
  154. package/src/lighting/lighting-types.ts +20 -0
  155. package/src/lighting/photometry.ts +264 -0
  156. package/src/polygon/cut-by-grid.ts +18 -0
  157. package/src/polygon/earcut.ts +98 -37
  158. package/src/polygon/index.ts +6 -0
  159. package/src/polygon/pole-of-inaccessibility.ts +330 -0
  160. package/src/polygon/subdivide-triangle-mesh.ts +77 -17
  161. package/src/utils/compose-attribute-provenance.ts +74 -0
  162. package/src/utils/compute-vertex-normals.ts +5 -6
  163. package/src/utils/geometry.ts +32 -6
  164. package/src/utils/index.ts +5 -5
  165. package/src/utils/mesh-processing.ts +30 -14
  166. package/src/utils/primitive-iterator.ts +45 -43
  167. package/src/utils/project-bounds.ts +154 -0
  168. package/src/utils/project-texture-coordinates.ts +67 -0
  169. package/src/utils/qualify-surface-transition.ts +169 -0
  170. package/dist/utils/constants.d.ts +0 -52
  171. package/dist/utils/constants.d.ts.map +0 -1
  172. package/dist/utils/constants.js +0 -39
  173. package/dist/utils/constants.js.map +0 -1
  174. package/dist/utils/gl-type.d.ts +0 -17
  175. package/dist/utils/gl-type.d.ts.map +0 -1
  176. package/dist/utils/gl-type.js +0 -69
  177. package/dist/utils/gl-type.js.map +0 -1
  178. package/src/utils/constants.ts +0 -42
  179. 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
@@ -33,7 +33,7 @@
33
33
  /* eslint-disable */
34
34
 
35
35
  import type {NumericArray} from '@math.gl/core';
36
- import {getPolygonSignedArea, DimIndex, Plane2D} from './polygon-utils';
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
- minX = maxX = positions[0];
74
- minY = maxY = positions[1];
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 + 1];
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, dim, minX, minY, invSize, 0);
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
- area = getPolygonSignedArea(data, {start, end, size: dim, plane});
109
- }
110
-
111
- let i0 = DimIndex[plane[0]];
112
- let i1 = DimIndex[plane[1]];
113
- // Note that the signed area calculation in math.gl
114
- // has the opposite sign to that which was originally
115
- // present in earcut, thus the `< 0` is reversed
116
- if (clockwise === area < 0) {
117
- for (i = start; i < end; i += dim) last = insertNode(i, data[i + i0], data[i + i1], last);
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, dim, minX, minY, invSize, pass?) {
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((prev.i / dim) | 0);
173
- triangles.push((ear.i / dim) | 0);
174
- triangles.push((next.i / dim) | 0);
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, dim, minX, minY, invSize, 1);
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, dim);
196
- earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
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, dim, minX, minY, invSize);
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, dim) {
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((a.i / dim) | 0);
352
- triangles.push((p.i / dim) | 0);
353
- triangles.push((b.i / dim) | 0);
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, dim, minX, minY, invSize) {
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, dim, minX, minY, invSize, 0);
384
- earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
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 = new Vertex(a.i, a.x, a.y);
728
- const b2 = new Vertex(b.i, b.x, b.y);
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 = new Vertex(i, x, y);
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 in coordinates array
833
+ // vertex index, independent of coordinate stride
773
834
  i: number;
774
835
 
775
836
  // vertex coordinates
@@ -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';