@math.gl/geometry 5.0.0-beta.6 → 5.0.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -35,6 +35,7 @@ __export(index_exports, {
35
35
  CurveArcLength: () => CurveArcLength,
36
36
  CylinderGeometry: () => CylinderGeometry,
37
37
  CylinderShape: () => CylinderShape,
38
+ DEFAULT_POLE_SEARCH_SEGMENT_TESTS: () => DEFAULT_POLE_SEARCH_SEGMENT_TESTS,
38
39
  GL: () => GL,
39
40
  GLType: () => GLType,
40
41
  GL_PRIMITIVE: () => GL_PRIMITIVE,
@@ -100,6 +101,7 @@ __export(index_exports, {
100
101
  getClosestPointsSegmentTriangle: () => getClosestPointsSegmentTriangle,
101
102
  getDegenerateTriangles: () => getDegenerateTriangles,
102
103
  getOrientedBoundingBoxQuaternion: () => getOrientedBoundingBoxQuaternion,
104
+ getPoleOfInaccessibility: () => getPoleOfInaccessibility,
103
105
  getPolygonSignedArea: () => getPolygonSignedArea,
104
106
  getPolygonWindingDirection: () => getPolygonWindingDirection,
105
107
  getRayPoint: () => getRayPoint,
@@ -3362,6 +3364,234 @@ function bitCode(p, bbox) {
3362
3364
  return code;
3363
3365
  }
3364
3366
 
3367
+ // dist/polygon/pole-of-inaccessibility.js
3368
+ var MAX_GRID_CELLS_PER_AXIS = 64;
3369
+ var MIN_RELATIVE_PRECISION = 1e-6;
3370
+ var DEFAULT_POLE_SEARCH_SEGMENT_TESTS = 5e6;
3371
+ function getPoleOfInaccessibility(polygon, precision, budget = {
3372
+ segmentTests: DEFAULT_POLE_SEARCH_SEGMENT_TESTS
3373
+ }) {
3374
+ const outline = polygon[0];
3375
+ if (!outline || outline.length === 0) {
3376
+ return null;
3377
+ }
3378
+ const vertexCount = countFiniteVertices(polygon);
3379
+ if (vertexCount < 0) {
3380
+ return null;
3381
+ }
3382
+ const box = getRingBox(outline);
3383
+ const width = box.right - box.left;
3384
+ const height = box.top - box.bottom;
3385
+ if (width === 0 || height === 0) {
3386
+ return null;
3387
+ }
3388
+ const longSide = Math.max(width, height);
3389
+ const minTolerance = longSide * MIN_RELATIVE_PRECISION;
3390
+ const tolerance = Math.max(Number.isFinite(precision) && precision > 0 ? precision : 0, minTolerance);
3391
+ const search = new PoleSearch(polygon, vertexCount, budget);
3392
+ const [centroidX, centroidY] = getAreaCentroid(polygon[0]);
3393
+ if (!search.score(centroidX, centroidY, 0)) {
3394
+ return null;
3395
+ }
3396
+ if (!search.score(box.left + width / 2, box.bottom + height / 2, 0)) {
3397
+ return search.bestPoint();
3398
+ }
3399
+ const side = Math.max(Math.min(width, height), longSide / MAX_GRID_CELLS_PER_AXIS);
3400
+ const columns = Math.ceil(width / side);
3401
+ const rows = Math.ceil(height / side);
3402
+ for (let column = 0; column < columns; column++) {
3403
+ for (let row = 0; row < rows; row++) {
3404
+ const x = box.left + (column + 0.5) * side;
3405
+ const y = box.bottom + (row + 0.5) * side;
3406
+ if (!search.score(x, y, side / 2)) {
3407
+ return search.bestPoint();
3408
+ }
3409
+ }
3410
+ }
3411
+ search.refine(tolerance, minTolerance);
3412
+ return search.bestPoint();
3413
+ }
3414
+ function countFiniteVertices(polygon) {
3415
+ let count = 0;
3416
+ for (const ring of polygon) {
3417
+ for (const point2 of ring) {
3418
+ if (!Number.isFinite(point2[0]) || !Number.isFinite(point2[1])) {
3419
+ return -1;
3420
+ }
3421
+ }
3422
+ count += ring.length;
3423
+ }
3424
+ return count;
3425
+ }
3426
+ function getRingBox(ring) {
3427
+ let left = Infinity;
3428
+ let bottom = Infinity;
3429
+ let right = -Infinity;
3430
+ let top = -Infinity;
3431
+ for (const point2 of ring) {
3432
+ left = Math.min(left, point2[0]);
3433
+ right = Math.max(right, point2[0]);
3434
+ bottom = Math.min(bottom, point2[1]);
3435
+ top = Math.max(top, point2[1]);
3436
+ }
3437
+ return { left, bottom, right, top };
3438
+ }
3439
+ function getAreaCentroid(ring) {
3440
+ let twiceArea = 0;
3441
+ let sumX = 0;
3442
+ let sumY = 0;
3443
+ let previous = ring[ring.length - 1];
3444
+ for (const current of ring) {
3445
+ const cross6 = previous[0] * current[1] - current[0] * previous[1];
3446
+ twiceArea += cross6;
3447
+ sumX += (previous[0] + current[0]) * cross6;
3448
+ sumY += (previous[1] + current[1]) * cross6;
3449
+ previous = current;
3450
+ }
3451
+ if (twiceArea === 0) {
3452
+ return [ring[0][0], ring[0][1]];
3453
+ }
3454
+ return [sumX / (3 * twiceArea), sumY / (3 * twiceArea)];
3455
+ }
3456
+ var PoleSearch = class {
3457
+ constructor(polygon, vertexCount, budget) {
3458
+ this.centerX = [];
3459
+ this.centerY = [];
3460
+ this.halfSide = [];
3461
+ this.reach = [];
3462
+ this.heap = [];
3463
+ this.bestX = NaN;
3464
+ this.bestY = NaN;
3465
+ this.bestDistance = -Infinity;
3466
+ this.polygon = polygon;
3467
+ this.vertexCount = vertexCount;
3468
+ this.budget = budget;
3469
+ }
3470
+ /** The best point so far, or null if no scored point is inside the polygon. */
3471
+ bestPoint() {
3472
+ return this.bestDistance > 0 ? [this.bestX, this.bestY] : null;
3473
+ }
3474
+ /**
3475
+ * Scores the square centered at (x, y), keeps its center if it is the best so far, and queues
3476
+ * the square if it has area. Returns false, without scoring, when the budget cannot pay.
3477
+ */
3478
+ score(x, y, half) {
3479
+ if (this.budget.segmentTests < this.vertexCount) {
3480
+ return false;
3481
+ }
3482
+ this.budget.segmentTests -= this.vertexCount;
3483
+ const distance = getSignedOutlineDistance(this.polygon, x, y);
3484
+ if (distance > this.bestDistance) {
3485
+ this.bestX = x;
3486
+ this.bestY = y;
3487
+ this.bestDistance = distance;
3488
+ }
3489
+ if (half > 0) {
3490
+ this.enqueue(x, y, half, distance + half * Math.SQRT2);
3491
+ }
3492
+ return true;
3493
+ }
3494
+ /**
3495
+ * Splits the most promising squares until none can beat the best point by `tolerance`. While no
3496
+ * scored point is inside the polygon, `tolerance` does not apply: the search continues while a
3497
+ * square can hold an inside point and is larger than `minTolerance`.
3498
+ */
3499
+ refine(tolerance, minTolerance) {
3500
+ while (this.heap.length > 0) {
3501
+ const index = this.dequeue();
3502
+ const reach = this.reach[index];
3503
+ if (this.bestDistance > 0 ? reach - this.bestDistance <= tolerance : reach <= 0) {
3504
+ return;
3505
+ }
3506
+ if (this.bestDistance <= 0 && this.halfSide[index] * Math.SQRT2 <= minTolerance) {
3507
+ continue;
3508
+ }
3509
+ const quarter = this.halfSide[index] / 2;
3510
+ const x = this.centerX[index];
3511
+ const y = this.centerY[index];
3512
+ if (!this.score(x - quarter, y - quarter, quarter) || !this.score(x + quarter, y - quarter, quarter) || !this.score(x - quarter, y + quarter, quarter) || !this.score(x + quarter, y + quarter, quarter)) {
3513
+ return;
3514
+ }
3515
+ }
3516
+ }
3517
+ enqueue(x, y, half, reach) {
3518
+ const index = this.reach.length;
3519
+ this.centerX.push(x);
3520
+ this.centerY.push(y);
3521
+ this.halfSide.push(half);
3522
+ this.reach.push(reach);
3523
+ const { heap } = this;
3524
+ let slot = heap.length;
3525
+ heap.push(index);
3526
+ while (slot > 0) {
3527
+ const parentSlot = slot - 1 >> 1;
3528
+ if (this.reach[heap[parentSlot]] >= reach) {
3529
+ break;
3530
+ }
3531
+ heap[slot] = heap[parentSlot];
3532
+ slot = parentSlot;
3533
+ }
3534
+ heap[slot] = index;
3535
+ }
3536
+ dequeue() {
3537
+ const { heap, reach } = this;
3538
+ const top = heap[0];
3539
+ const moved = heap.pop();
3540
+ const size = heap.length;
3541
+ if (size === 0) {
3542
+ return top;
3543
+ }
3544
+ let slot = 0;
3545
+ for (; ; ) {
3546
+ let next = slot * 2 + 1;
3547
+ if (next >= size) {
3548
+ break;
3549
+ }
3550
+ if (next + 1 < size && reach[heap[next + 1]] > reach[heap[next]]) {
3551
+ next += 1;
3552
+ }
3553
+ if (reach[heap[next]] <= reach[moved]) {
3554
+ break;
3555
+ }
3556
+ heap[slot] = heap[next];
3557
+ slot = next;
3558
+ }
3559
+ heap[slot] = moved;
3560
+ return top;
3561
+ }
3562
+ };
3563
+ function getSignedOutlineDistance(polygon, x, y) {
3564
+ let nearestSquared = Infinity;
3565
+ let crossings = 0;
3566
+ for (const ring of polygon) {
3567
+ let start = ring[ring.length - 1];
3568
+ for (const end of ring) {
3569
+ if (start[1] <= y !== end[1] <= y) {
3570
+ const t = (y - start[1]) / (end[1] - start[1]);
3571
+ if (start[0] + t * (end[0] - start[0]) > x) {
3572
+ crossings++;
3573
+ }
3574
+ }
3575
+ nearestSquared = Math.min(nearestSquared, getSquaredSegmentDistance(x, y, start, end));
3576
+ start = end;
3577
+ }
3578
+ }
3579
+ if (nearestSquared === 0) {
3580
+ return 0;
3581
+ }
3582
+ const distance = Math.sqrt(nearestSquared);
3583
+ return crossings % 2 === 1 ? distance : -distance;
3584
+ }
3585
+ function getSquaredSegmentDistance(x, y, start, end) {
3586
+ const edgeX = end[0] - start[0];
3587
+ const edgeY = end[1] - start[1];
3588
+ const lengthSquared = edgeX * edgeX + edgeY * edgeY;
3589
+ const along = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, ((x - start[0]) * edgeX + (y - start[1]) * edgeY) / lengthSquared));
3590
+ const offsetX = start[0] + along * edgeX - x;
3591
+ const offsetY = start[1] + along * edgeY - y;
3592
+ return offsetX * offsetX + offsetY * offsetY;
3593
+ }
3594
+
3365
3595
  // dist/polygon/cut-by-grid.js
3366
3596
  function cutPolylineByGrid(positions, options) {
3367
3597
  const { size = 2, broken = false, gridResolution = 10, gridOffset = [0, 0], startIndex = 0, endIndex = positions.length } = options || {};