mapshaper 0.6.110 → 0.6.111

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/mapshaper.js CHANGED
@@ -3113,1037 +3113,70 @@
3113
3113
  snapEndpointsByInterval: snapEndpointsByInterval
3114
3114
  });
3115
3115
 
3116
- /*
3117
- * big.js v7.0.1
3118
- * A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic.
3119
- * Copyright (c) 2025 Michael Mclaughlin
3120
- * https://github.com/MikeMcl/big.js/LICENCE.md
3121
- */
3122
-
3123
-
3124
- /************************************** EDITABLE DEFAULTS *****************************************/
3125
-
3126
-
3127
- // The default values below must be integers within the stated ranges.
3128
-
3129
- /*
3130
- * The maximum number of decimal places (DP) of the results of operations involving division:
3131
- * div and sqrt, and pow with negative exponents.
3132
- */
3133
- var DP = 20, // 0 to MAX_DP
3134
-
3135
- /*
3136
- * The rounding mode (RM) used when rounding to the above decimal places.
3137
- *
3138
- * 0 Towards zero (i.e. truncate, no rounding). (ROUND_DOWN)
3139
- * 1 To nearest neighbour. If equidistant, round up. (ROUND_HALF_UP)
3140
- * 2 To nearest neighbour. If equidistant, to even. (ROUND_HALF_EVEN)
3141
- * 3 Away from zero. (ROUND_UP)
3142
- */
3143
- RM = 1, // 0, 1, 2 or 3
3144
-
3145
- // The maximum value of DP and Big.DP.
3146
- MAX_DP = 1E6, // 0 to 1000000
3147
-
3148
- // The maximum magnitude of the exponent argument to the pow method.
3149
- MAX_POWER = 1E6, // 1 to 1000000
3150
-
3151
- /*
3152
- * The negative exponent (NE) at and beneath which toString returns exponential notation.
3153
- * (JavaScript numbers: -7)
3154
- * -1000000 is the minimum recommended exponent value of a Big.
3155
- */
3156
- NE = -7, // 0 to -1000000
3157
-
3158
- /*
3159
- * The positive exponent (PE) at and above which toString returns exponential notation.
3160
- * (JavaScript numbers: 21)
3161
- * 1000000 is the maximum recommended exponent value of a Big, but this limit is not enforced.
3162
- */
3163
- PE = 21, // 0 to 1000000
3164
-
3165
- /*
3166
- * When true, an error will be thrown if a primitive number is passed to the Big constructor,
3167
- * or if valueOf is called, or if toNumber is called on a Big which cannot be converted to a
3168
- * primitive number without a loss of precision.
3169
- */
3170
- STRICT = false, // true or false
3171
-
3172
-
3173
- /**************************************************************************************************/
3174
-
3175
-
3176
- // Error messages.
3177
- NAME = '[big.js] ',
3178
- INVALID = NAME + 'Invalid ',
3179
- INVALID_DP = INVALID + 'decimal places',
3180
- INVALID_RM = INVALID + 'rounding mode',
3181
- DIV_BY_ZERO = NAME + 'Division by zero',
3182
-
3183
- // The shared prototype object.
3184
- P = {},
3185
- UNDEFINED = void 0,
3186
- NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
3187
-
3188
-
3189
- /*
3190
- * Create and return a Big constructor.
3191
- */
3192
- function _Big_() {
3193
-
3194
- /*
3195
- * The Big constructor and exported function.
3196
- * Create and return a new instance of a Big number object.
3197
- *
3198
- * n {number|string|Big} A numeric value.
3199
- */
3200
- function Big(n) {
3201
- var x = this;
3202
-
3203
- // Enable constructor usage without new.
3204
- if (!(x instanceof Big)) {
3205
- return n === UNDEFINED && arguments.length === 0 ? _Big_() : new Big(n);
3206
- }
3207
-
3208
-
3209
- // Duplicate.
3210
- if (n instanceof Big) {
3211
- x.s = n.s;
3212
- x.e = n.e;
3213
- x.c = n.c.slice();
3214
- } else {
3215
- if (typeof n !== 'string') {
3216
- if (Big.strict === true && typeof n !== 'bigint') {
3217
- throw TypeError(INVALID + 'value');
3218
- }
3219
-
3220
- // Minus zero?
3221
- n = n === 0 && 1 / n < 0 ? '-0' : String(n);
3222
- }
3223
-
3224
- parse(x, n);
3225
- }
3226
-
3227
- // Retain a reference to this Big constructor.
3228
- // Shadow Big.prototype.constructor which points to Object.
3229
- x.constructor = Big;
3230
- }
3231
-
3232
- Big.prototype = P;
3233
- Big.DP = DP;
3234
- Big.RM = RM;
3235
- Big.NE = NE;
3236
- Big.PE = PE;
3237
- Big.strict = STRICT;
3238
- Big.roundDown = 0;
3239
- Big.roundHalfUp = 1;
3240
- Big.roundHalfEven = 2;
3241
- Big.roundUp = 3;
3242
-
3243
- return Big;
3244
- }
3245
-
3246
-
3247
- /*
3248
- * Parse the number or string value passed to a Big constructor.
3249
- *
3250
- * x {Big} A Big number instance.
3251
- * n {number|string} A numeric value.
3252
- */
3253
- function parse(x, n) {
3254
- var e, i, nl;
3255
-
3256
- if (!NUMERIC.test(n)) {
3257
- throw Error(INVALID + 'number');
3258
- }
3259
-
3260
- // Determine sign.
3261
- x.s = n.charAt(0) == '-' ? (n = n.slice(1), -1) : 1;
3262
-
3263
- // Decimal point?
3264
- if ((e = n.indexOf('.')) > -1) n = n.replace('.', '');
3265
-
3266
- // Exponential form?
3267
- if ((i = n.search(/e/i)) > 0) {
3268
-
3269
- // Determine exponent.
3270
- if (e < 0) e = i;
3271
- e += +n.slice(i + 1);
3272
- n = n.substring(0, i);
3273
- } else if (e < 0) {
3274
-
3275
- // Integer.
3276
- e = n.length;
3277
- }
3278
-
3279
- nl = n.length;
3280
-
3281
- // Determine leading zeros.
3282
- for (i = 0; i < nl && n.charAt(i) == '0';) ++i;
3283
-
3284
- if (i == nl) {
3285
-
3286
- // Zero.
3287
- x.c = [x.e = 0];
3288
- } else {
3289
-
3290
- // Determine trailing zeros.
3291
- for (; nl > 0 && n.charAt(--nl) == '0';);
3292
- x.e = e - i - 1;
3293
- x.c = [];
3294
-
3295
- // Convert string to array of digits without leading/trailing zeros.
3296
- for (e = 0; i <= nl;) x.c[e++] = +n.charAt(i++);
3297
- }
3298
-
3299
- return x;
3300
- }
3301
-
3302
-
3303
- /*
3304
- * Round Big x to a maximum of sd significant digits using rounding mode rm.
3305
- *
3306
- * x {Big} The Big to round.
3307
- * sd {number} Significant digits: integer, 0 to MAX_DP inclusive.
3308
- * rm {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
3309
- * [more] {boolean} Whether the result of division was truncated.
3310
- */
3311
- function round(x, sd, rm, more) {
3312
- var xc = x.c;
3313
-
3314
- if (rm === UNDEFINED) rm = x.constructor.RM;
3315
- if (rm !== 0 && rm !== 1 && rm !== 2 && rm !== 3) {
3316
- throw Error(INVALID_RM);
3317
- }
3318
-
3319
- if (sd < 1) {
3320
- more =
3321
- rm === 3 && (more || !!xc[0]) || sd === 0 && (
3322
- rm === 1 && xc[0] >= 5 ||
3323
- rm === 2 && (xc[0] > 5 || xc[0] === 5 && (more || xc[1] !== UNDEFINED))
3324
- );
3325
-
3326
- xc.length = 1;
3327
-
3328
- if (more) {
3329
-
3330
- // 1, 0.1, 0.01, 0.001, 0.0001 etc.
3331
- x.e = x.e - sd + 1;
3332
- xc[0] = 1;
3333
- } else {
3334
-
3335
- // Zero.
3336
- xc[0] = x.e = 0;
3337
- }
3338
- } else if (sd < xc.length) {
3339
-
3340
- // xc[sd] is the digit after the digit that may be rounded up.
3341
- more =
3342
- rm === 1 && xc[sd] >= 5 ||
3343
- rm === 2 && (xc[sd] > 5 || xc[sd] === 5 &&
3344
- (more || xc[sd + 1] !== UNDEFINED || xc[sd - 1] & 1)) ||
3345
- rm === 3 && (more || !!xc[0]);
3346
-
3347
- // Remove any digits after the required precision.
3348
- xc.length = sd;
3349
-
3350
- // Round up?
3351
- if (more) {
3352
-
3353
- // Rounding up may mean the previous digit has to be rounded up.
3354
- for (; ++xc[--sd] > 9;) {
3355
- xc[sd] = 0;
3356
- if (sd === 0) {
3357
- ++x.e;
3358
- xc.unshift(1);
3359
- break;
3360
- }
3361
- }
3362
- }
3363
-
3364
- // Remove trailing zeros.
3365
- for (sd = xc.length; !xc[--sd];) xc.pop();
3366
- }
3367
-
3368
- return x;
3369
- }
3370
-
3371
-
3372
- /*
3373
- * Return a string representing the value of Big x in normal or exponential notation.
3374
- * Handles P.toExponential, P.toFixed, P.toJSON, P.toPrecision, P.toString and P.valueOf.
3375
- */
3376
- function stringify$1(x, doExponential, isNonzero) {
3377
- var e = x.e,
3378
- s = x.c.join(''),
3379
- n = s.length;
3380
-
3381
- // Exponential notation?
3382
- if (doExponential) {
3383
- s = s.charAt(0) + (n > 1 ? '.' + s.slice(1) : '') + (e < 0 ? 'e' : 'e+') + e;
3384
-
3385
- // Normal notation.
3386
- } else if (e < 0) {
3387
- for (; ++e;) s = '0' + s;
3388
- s = '0.' + s;
3389
- } else if (e > 0) {
3390
- if (++e > n) {
3391
- for (e -= n; e--;) s += '0';
3392
- } else if (e < n) {
3393
- s = s.slice(0, e) + '.' + s.slice(e);
3394
- }
3395
- } else if (n > 1) {
3396
- s = s.charAt(0) + '.' + s.slice(1);
3397
- }
3398
-
3399
- return x.s < 0 && isNonzero ? '-' + s : s;
3400
- }
3401
-
3402
-
3403
- // Prototype/instance methods
3404
-
3405
-
3406
- /*
3407
- * Return a new Big whose value is the absolute value of this Big.
3408
- */
3409
- P.abs = function () {
3410
- var x = new this.constructor(this);
3411
- x.s = 1;
3412
- return x;
3413
- };
3414
-
3415
-
3416
- /*
3417
- * Return 1 if the value of this Big is greater than the value of Big y,
3418
- * -1 if the value of this Big is less than the value of Big y, or
3419
- * 0 if they have the same value.
3420
- */
3421
- P.cmp = function (y) {
3422
- var isneg,
3423
- x = this,
3424
- xc = x.c,
3425
- yc = (y = new x.constructor(y)).c,
3426
- i = x.s,
3427
- j = y.s,
3428
- k = x.e,
3429
- l = y.e;
3430
-
3431
- // Either zero?
3432
- if (!xc[0] || !yc[0]) return !xc[0] ? !yc[0] ? 0 : -j : i;
3433
-
3434
- // Signs differ?
3435
- if (i != j) return i;
3436
-
3437
- isneg = i < 0;
3438
-
3439
- // Compare exponents.
3440
- if (k != l) return k > l ^ isneg ? 1 : -1;
3441
-
3442
- j = (k = xc.length) < (l = yc.length) ? k : l;
3443
-
3444
- // Compare digit by digit.
3445
- for (i = -1; ++i < j;) {
3446
- if (xc[i] != yc[i]) return xc[i] > yc[i] ^ isneg ? 1 : -1;
3447
- }
3448
-
3449
- // Compare lengths.
3450
- return k == l ? 0 : k > l ^ isneg ? 1 : -1;
3451
- };
3452
-
3453
-
3454
- /*
3455
- * Return a new Big whose value is the value of this Big divided by the value of Big y, rounded,
3456
- * if necessary, to a maximum of Big.DP decimal places using rounding mode Big.RM.
3457
- */
3458
- P.div = function (y) {
3459
- var x = this,
3460
- Big = x.constructor,
3461
- a = x.c, // dividend
3462
- b = (y = new Big(y)).c, // divisor
3463
- k = x.s == y.s ? 1 : -1,
3464
- dp = Big.DP;
3465
-
3466
- if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
3467
- throw Error(INVALID_DP);
3468
- }
3469
-
3470
- // Divisor is zero?
3471
- if (!b[0]) {
3472
- throw Error(DIV_BY_ZERO);
3473
- }
3474
-
3475
- // Dividend is 0? Return +-0.
3476
- if (!a[0]) {
3477
- y.s = k;
3478
- y.c = [y.e = 0];
3479
- return y;
3480
- }
3481
-
3482
- var bl, bt, n, cmp, ri,
3483
- bz = b.slice(),
3484
- ai = bl = b.length,
3485
- al = a.length,
3486
- r = a.slice(0, bl), // remainder
3487
- rl = r.length,
3488
- q = y, // quotient
3489
- qc = q.c = [],
3490
- qi = 0,
3491
- p = dp + (q.e = x.e - y.e) + 1; // precision of the result
3492
-
3493
- q.s = k;
3494
- k = p < 0 ? 0 : p;
3495
-
3496
- // Create version of divisor with leading zero.
3497
- bz.unshift(0);
3498
-
3499
- // Add zeros to make remainder as long as divisor.
3500
- for (; rl++ < bl;) r.push(0);
3501
-
3502
- do {
3503
-
3504
- // n is how many times the divisor goes into current remainder.
3505
- for (n = 0; n < 10; n++) {
3506
-
3507
- // Compare divisor and remainder.
3508
- if (bl != (rl = r.length)) {
3509
- cmp = bl > rl ? 1 : -1;
3510
- } else {
3511
- for (ri = -1, cmp = 0; ++ri < bl;) {
3512
- if (b[ri] != r[ri]) {
3513
- cmp = b[ri] > r[ri] ? 1 : -1;
3514
- break;
3515
- }
3516
- }
3517
- }
3518
-
3519
- // If divisor < remainder, subtract divisor from remainder.
3520
- if (cmp < 0) {
3521
-
3522
- // Remainder can't be more than 1 digit longer than divisor.
3523
- // Equalise lengths using divisor with extra leading zero?
3524
- for (bt = rl == bl ? b : bz; rl;) {
3525
- if (r[--rl] < bt[rl]) {
3526
- ri = rl;
3527
- for (; ri && !r[--ri];) r[ri] = 9;
3528
- --r[ri];
3529
- r[rl] += 10;
3530
- }
3531
- r[rl] -= bt[rl];
3532
- }
3533
-
3534
- for (; !r[0];) r.shift();
3535
- } else {
3536
- break;
3537
- }
3538
- }
3539
-
3540
- // Add the digit n to the result array.
3541
- qc[qi++] = cmp ? n : ++n;
3542
-
3543
- // Update the remainder.
3544
- if (r[0] && cmp) r[rl] = a[ai] || 0;
3545
- else r = [a[ai]];
3546
-
3547
- } while ((ai++ < al || r[0] !== UNDEFINED) && k--);
3548
-
3549
- // Leading zero? Do not remove if result is simply zero (qi == 1).
3550
- if (!qc[0] && qi != 1) {
3551
-
3552
- // There can't be more than one zero.
3553
- qc.shift();
3554
- q.e--;
3555
- p--;
3556
- }
3557
-
3558
- // Round?
3559
- if (qi > p) round(q, p, Big.RM, r[0] !== UNDEFINED);
3560
-
3561
- return q;
3562
- };
3563
-
3564
-
3565
- /*
3566
- * Return true if the value of this Big is equal to the value of Big y, otherwise return false.
3567
- */
3568
- P.eq = function (y) {
3569
- return this.cmp(y) === 0;
3570
- };
3571
-
3572
-
3573
- /*
3574
- * Return true if the value of this Big is greater than the value of Big y, otherwise return
3575
- * false.
3576
- */
3577
- P.gt = function (y) {
3578
- return this.cmp(y) > 0;
3579
- };
3580
-
3581
-
3582
- /*
3583
- * Return true if the value of this Big is greater than or equal to the value of Big y, otherwise
3584
- * return false.
3585
- */
3586
- P.gte = function (y) {
3587
- return this.cmp(y) > -1;
3588
- };
3589
-
3590
-
3591
- /*
3592
- * Return true if the value of this Big is less than the value of Big y, otherwise return false.
3593
- */
3594
- P.lt = function (y) {
3595
- return this.cmp(y) < 0;
3596
- };
3597
-
3598
-
3599
- /*
3600
- * Return true if the value of this Big is less than or equal to the value of Big y, otherwise
3601
- * return false.
3602
- */
3603
- P.lte = function (y) {
3604
- return this.cmp(y) < 1;
3605
- };
3606
-
3607
-
3608
- /*
3609
- * Return a new Big whose value is the value of this Big minus the value of Big y.
3610
- */
3611
- P.minus = P.sub = function (y) {
3612
- var i, j, t, xlty,
3613
- x = this,
3614
- Big = x.constructor,
3615
- a = x.s,
3616
- b = (y = new Big(y)).s;
3617
-
3618
- // Signs differ?
3619
- if (a != b) {
3620
- y.s = -b;
3621
- return x.plus(y);
3622
- }
3623
-
3624
- var xc = x.c.slice(),
3625
- xe = x.e,
3626
- yc = y.c,
3627
- ye = y.e;
3628
-
3629
- // Either zero?
3630
- if (!xc[0] || !yc[0]) {
3631
- if (yc[0]) {
3632
- y.s = -b;
3633
- } else if (xc[0]) {
3634
- y = new Big(x);
3635
- } else {
3636
- y.s = 1;
3637
- }
3638
- return y;
3639
- }
3640
-
3641
- // Determine which is the bigger number. Prepend zeros to equalise exponents.
3642
- if (a = xe - ye) {
3643
-
3644
- if (xlty = a < 0) {
3645
- a = -a;
3646
- t = xc;
3647
- } else {
3648
- ye = xe;
3649
- t = yc;
3650
- }
3651
-
3652
- t.reverse();
3653
- for (b = a; b--;) t.push(0);
3654
- t.reverse();
3655
- } else {
3656
-
3657
- // Exponents equal. Check digit by digit.
3658
- j = ((xlty = xc.length < yc.length) ? xc : yc).length;
3659
-
3660
- for (a = b = 0; b < j; b++) {
3661
- if (xc[b] != yc[b]) {
3662
- xlty = xc[b] < yc[b];
3663
- break;
3664
- }
3665
- }
3666
- }
3667
-
3668
- // x < y? Point xc to the array of the bigger number.
3669
- if (xlty) {
3670
- t = xc;
3671
- xc = yc;
3672
- yc = t;
3673
- y.s = -y.s;
3674
- }
3675
-
3676
- /*
3677
- * Append zeros to xc if shorter. No need to add zeros to yc if shorter as subtraction only
3678
- * needs to start at yc.length.
3679
- */
3680
- if ((b = (j = yc.length) - (i = xc.length)) > 0) for (; b--;) xc[i++] = 0;
3681
-
3682
- // Subtract yc from xc.
3683
- for (b = i; j > a;) {
3684
- if (xc[--j] < yc[j]) {
3685
- for (i = j; i && !xc[--i];) xc[i] = 9;
3686
- --xc[i];
3687
- xc[j] += 10;
3688
- }
3689
-
3690
- xc[j] -= yc[j];
3691
- }
3692
-
3693
- // Remove trailing zeros.
3694
- for (; xc[--b] === 0;) xc.pop();
3695
-
3696
- // Remove leading zeros and adjust exponent accordingly.
3697
- for (; xc[0] === 0;) {
3698
- xc.shift();
3699
- --ye;
3700
- }
3701
-
3702
- if (!xc[0]) {
3703
-
3704
- // n - n = +0
3705
- y.s = 1;
3706
-
3707
- // Result must be zero.
3708
- xc = [ye = 0];
3709
- }
3710
-
3711
- y.c = xc;
3712
- y.e = ye;
3713
-
3714
- return y;
3715
- };
3716
-
3717
-
3718
- /*
3719
- * Return a new Big whose value is the value of this Big modulo the value of Big y.
3720
- */
3721
- P.mod = function (y) {
3722
- var ygtx,
3723
- x = this,
3724
- Big = x.constructor,
3725
- a = x.s,
3726
- b = (y = new Big(y)).s;
3727
-
3728
- if (!y.c[0]) {
3729
- throw Error(DIV_BY_ZERO);
3730
- }
3731
-
3732
- x.s = y.s = 1;
3733
- ygtx = y.cmp(x) == 1;
3734
- x.s = a;
3735
- y.s = b;
3736
-
3737
- if (ygtx) return new Big(x);
3738
-
3739
- a = Big.DP;
3740
- b = Big.RM;
3741
- Big.DP = Big.RM = 0;
3742
- x = x.div(y);
3743
- Big.DP = a;
3744
- Big.RM = b;
3745
-
3746
- return this.minus(x.times(y));
3747
- };
3748
-
3749
-
3750
- /*
3751
- * Return a new Big whose value is the value of this Big negated.
3752
- */
3753
- P.neg = function () {
3754
- var x = new this.constructor(this);
3755
- x.s = -x.s;
3756
- return x;
3757
- };
3758
-
3759
-
3760
- /*
3761
- * Return a new Big whose value is the value of this Big plus the value of Big y.
3762
- */
3763
- P.plus = P.add = function (y) {
3764
- var e, k, t,
3765
- x = this,
3766
- Big = x.constructor;
3767
-
3768
- y = new Big(y);
3769
-
3770
- // Signs differ?
3771
- if (x.s != y.s) {
3772
- y.s = -y.s;
3773
- return x.minus(y);
3774
- }
3775
-
3776
- var xe = x.e,
3777
- xc = x.c,
3778
- ye = y.e,
3779
- yc = y.c;
3780
-
3781
- // Either zero?
3782
- if (!xc[0] || !yc[0]) {
3783
- if (!yc[0]) {
3784
- if (xc[0]) {
3785
- y = new Big(x);
3786
- } else {
3787
- y.s = x.s;
3788
- }
3789
- }
3790
- return y;
3791
- }
3792
-
3793
- xc = xc.slice();
3794
-
3795
- // Prepend zeros to equalise exponents.
3796
- // Note: reverse faster than unshifts.
3797
- if (e = xe - ye) {
3798
- if (e > 0) {
3799
- ye = xe;
3800
- t = yc;
3801
- } else {
3802
- e = -e;
3803
- t = xc;
3804
- }
3805
-
3806
- t.reverse();
3807
- for (; e--;) t.push(0);
3808
- t.reverse();
3809
- }
3810
-
3811
- // Point xc to the longer array.
3812
- if (xc.length - yc.length < 0) {
3813
- t = yc;
3814
- yc = xc;
3815
- xc = t;
3816
- }
3817
-
3818
- e = yc.length;
3819
-
3820
- // Only start adding at yc.length - 1 as the further digits of xc can be left as they are.
3821
- for (k = 0; e; xc[e] %= 10) k = (xc[--e] = xc[e] + yc[e] + k) / 10 | 0;
3822
-
3823
- // No need to check for zero, as +x + +y != 0 && -x + -y != 0
3824
-
3825
- if (k) {
3826
- xc.unshift(k);
3827
- ++ye;
3828
- }
3829
-
3830
- // Remove trailing zeros.
3831
- for (e = xc.length; xc[--e] === 0;) xc.pop();
3832
-
3833
- y.c = xc;
3834
- y.e = ye;
3835
-
3836
- return y;
3837
- };
3838
-
3839
-
3840
- /*
3841
- * Return a Big whose value is the value of this Big raised to the power n.
3842
- * If n is negative, round to a maximum of Big.DP decimal places using rounding
3843
- * mode Big.RM.
3844
- *
3845
- * n {number} Integer, -MAX_POWER to MAX_POWER inclusive.
3846
- */
3847
- P.pow = function (n) {
3848
- var x = this,
3849
- one = new x.constructor('1'),
3850
- y = one,
3851
- isneg = n < 0;
3852
-
3853
- if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) {
3854
- throw Error(INVALID + 'exponent');
3855
- }
3856
-
3857
- if (isneg) n = -n;
3858
-
3859
- for (;;) {
3860
- if (n & 1) y = y.times(x);
3861
- n >>= 1;
3862
- if (!n) break;
3863
- x = x.times(x);
3864
- }
3865
-
3866
- return isneg ? one.div(y) : y;
3867
- };
3868
-
3869
-
3870
- /*
3871
- * Return a new Big whose value is the value of this Big rounded to a maximum precision of sd
3872
- * significant digits using rounding mode rm, or Big.RM if rm is not specified.
3873
- *
3874
- * sd {number} Significant digits: integer, 1 to MAX_DP inclusive.
3875
- * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
3876
- */
3877
- P.prec = function (sd, rm) {
3878
- if (sd !== ~~sd || sd < 1 || sd > MAX_DP) {
3879
- throw Error(INVALID + 'precision');
3880
- }
3881
- return round(new this.constructor(this), sd, rm);
3882
- };
3883
-
3884
-
3885
- /*
3886
- * Return a new Big whose value is the value of this Big rounded to a maximum of dp decimal places
3887
- * using rounding mode rm, or Big.RM if rm is not specified.
3888
- * If dp is negative, round to an integer which is a multiple of 10**-dp.
3889
- * If dp is not specified, round to 0 decimal places.
3890
- *
3891
- * dp? {number} Integer, -MAX_DP to MAX_DP inclusive.
3892
- * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
3893
- */
3894
- P.round = function (dp, rm) {
3895
- if (dp === UNDEFINED) dp = 0;
3896
- else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) {
3897
- throw Error(INVALID_DP);
3898
- }
3899
- return round(new this.constructor(this), dp + this.e + 1, rm);
3900
- };
3901
-
3902
-
3903
- /*
3904
- * Return a new Big whose value is the square root of the value of this Big, rounded, if
3905
- * necessary, to a maximum of Big.DP decimal places using rounding mode Big.RM.
3906
- */
3907
- P.sqrt = function () {
3908
- var r, c, t,
3909
- x = this,
3910
- Big = x.constructor,
3911
- s = x.s,
3912
- e = x.e,
3913
- half = new Big('0.5');
3914
-
3915
- // Zero?
3916
- if (!x.c[0]) return new Big(x);
3917
-
3918
- // Negative?
3919
- if (s < 0) {
3920
- throw Error(NAME + 'No square root');
3921
- }
3922
-
3923
- // Estimate.
3924
- s = Math.sqrt(+stringify$1(x, true, true));
3925
-
3926
- // Math.sqrt underflow/overflow?
3927
- // Re-estimate: pass x coefficient to Math.sqrt as integer, then adjust the result exponent.
3928
- if (s === 0 || s === 1 / 0) {
3929
- c = x.c.join('');
3930
- if (!(c.length + e & 1)) c += '0';
3931
- s = Math.sqrt(c);
3932
- e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
3933
- r = new Big((s == 1 / 0 ? '5e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e);
3934
- } else {
3935
- r = new Big(s + '');
3936
- }
3937
-
3938
- e = r.e + (Big.DP += 4);
3939
-
3940
- // Newton-Raphson iteration.
3941
- do {
3942
- t = r;
3943
- r = half.times(t.plus(x.div(t)));
3944
- } while (t.c.slice(0, e).join('') !== r.c.slice(0, e).join(''));
3945
-
3946
- return round(r, (Big.DP -= 4) + r.e + 1, Big.RM);
3947
- };
3948
-
3949
-
3950
- /*
3951
- * Return a new Big whose value is the value of this Big times the value of Big y.
3952
- */
3953
- P.times = P.mul = function (y) {
3954
- var c,
3955
- x = this,
3956
- Big = x.constructor,
3957
- xc = x.c,
3958
- yc = (y = new Big(y)).c,
3959
- a = xc.length,
3960
- b = yc.length,
3961
- i = x.e,
3962
- j = y.e;
3963
-
3964
- // Determine sign of result.
3965
- y.s = x.s == y.s ? 1 : -1;
3966
-
3967
- // Return signed 0 if either 0.
3968
- if (!xc[0] || !yc[0]) {
3969
- y.c = [y.e = 0];
3970
- return y;
3971
- }
3972
-
3973
- // Initialise exponent of result as x.e + y.e.
3974
- y.e = i + j;
3975
-
3976
- // If array xc has fewer digits than yc, swap xc and yc, and lengths.
3977
- if (a < b) {
3978
- c = xc;
3979
- xc = yc;
3980
- yc = c;
3981
- j = a;
3982
- a = b;
3983
- b = j;
3984
- }
3985
-
3986
- // Initialise coefficient array of result with zeros.
3987
- for (c = new Array(j = a + b); j--;) c[j] = 0;
3988
-
3989
- // Multiply.
3990
-
3991
- // i is initially xc.length.
3992
- for (i = b; i--;) {
3993
- b = 0;
3994
-
3995
- // a is yc.length.
3996
- for (j = a + i; j > i;) {
3997
-
3998
- // Current sum of products at this digit position, plus carry.
3999
- b = c[j] + yc[i] * xc[j - i - 1] + b;
4000
- c[j--] = b % 10;
4001
-
4002
- // carry
4003
- b = b / 10 | 0;
4004
- }
4005
-
4006
- c[j] = b;
4007
- }
4008
-
4009
- // Increment result exponent if there is a final carry, otherwise remove leading zero.
4010
- if (b) ++y.e;
4011
- else c.shift();
4012
-
4013
- // Remove trailing zeros.
4014
- for (i = c.length; !c[--i];) c.pop();
4015
- y.c = c;
4016
-
4017
- return y;
4018
- };
4019
-
4020
-
4021
- /*
4022
- * Return a string representing the value of this Big in exponential notation rounded to dp fixed
4023
- * decimal places using rounding mode rm, or Big.RM if rm is not specified.
4024
- *
4025
- * dp? {number} Decimal places: integer, 0 to MAX_DP inclusive.
4026
- * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
4027
- */
4028
- P.toExponential = function (dp, rm) {
4029
- var x = this,
4030
- n = x.c[0];
4031
-
4032
- if (dp !== UNDEFINED) {
4033
- if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
4034
- throw Error(INVALID_DP);
4035
- }
4036
- x = round(new x.constructor(x), ++dp, rm);
4037
- for (; x.c.length < dp;) x.c.push(0);
4038
- }
4039
-
4040
- return stringify$1(x, true, !!n);
4041
- };
4042
-
4043
-
4044
- /*
4045
- * Return a string representing the value of this Big in normal notation rounded to dp fixed
4046
- * decimal places using rounding mode rm, or Big.RM if rm is not specified.
4047
- *
4048
- * dp? {number} Decimal places: integer, 0 to MAX_DP inclusive.
4049
- * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
4050
- *
4051
- * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'.
4052
- * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'.
4053
- */
4054
- P.toFixed = function (dp, rm) {
4055
- var x = this,
4056
- n = x.c[0];
4057
-
4058
- if (dp !== UNDEFINED) {
4059
- if (dp !== ~~dp || dp < 0 || dp > MAX_DP) {
4060
- throw Error(INVALID_DP);
4061
- }
4062
- x = round(new x.constructor(x), dp + x.e + 1, rm);
4063
-
4064
- // x.e may have changed if the value is rounded up.
4065
- for (dp = dp + x.e + 1; x.c.length < dp;) x.c.push(0);
4066
- }
4067
-
4068
- return stringify$1(x, false, !!n);
4069
- };
4070
-
4071
-
4072
- /*
4073
- * Return a string representing the value of this Big.
4074
- * Return exponential notation if this Big has a positive exponent equal to or greater than
4075
- * Big.PE, or a negative exponent equal to or less than Big.NE.
4076
- * Omit the sign for negative zero.
4077
- */
4078
- P.toJSON = P.toString = function () {
4079
- var x = this,
4080
- Big = x.constructor;
4081
- return stringify$1(x, x.e <= Big.NE || x.e >= Big.PE, !!x.c[0]);
4082
- };
4083
-
4084
- if (typeof Symbol !== "undefined") {
4085
- P[Symbol.for('nodejs.util.inspect.custom')] = P.toJSON;
4086
- }
4087
-
4088
-
4089
- /*
4090
- * Return the value of this Big as a primitive number.
4091
- */
4092
- P.toNumber = function () {
4093
- var n = +stringify$1(this, true, true);
4094
- if (this.constructor.strict === true && !this.eq(n.toString())) {
4095
- throw Error(NAME + 'Imprecise conversion');
4096
- }
4097
- return n;
4098
- };
4099
-
4100
-
4101
- /*
4102
- * Return a string representing the value of this Big rounded to sd significant digits using
4103
- * rounding mode rm, or Big.RM if rm is not specified.
4104
- * Use exponential notation if sd is less than the number of digits necessary to represent
4105
- * the integer part of the value in normal notation.
4106
- *
4107
- * sd {number} Significant digits: integer, 1 to MAX_DP inclusive.
4108
- * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).
4109
- */
4110
- P.toPrecision = function (sd, rm) {
4111
- var x = this,
4112
- Big = x.constructor,
4113
- n = x.c[0];
4114
-
4115
- if (sd !== UNDEFINED) {
4116
- if (sd !== ~~sd || sd < 1 || sd > MAX_DP) {
4117
- throw Error(INVALID + 'precision');
4118
- }
4119
- x = round(new Big(x), sd, rm);
4120
- for (; x.c.length < sd;) x.c.push(0);
4121
- }
4122
-
4123
- return stringify$1(x, sd <= x.e || x.e <= Big.NE || x.e >= Big.PE, !!n);
4124
- };
4125
-
4126
-
4127
- /*
4128
- * Return a string representing the value of this Big.
4129
- * Return exponential notation if this Big has a positive exponent equal to or greater than
4130
- * Big.PE, or a negative exponent equal to or less than Big.NE.
4131
- * Include the sign for negative zero.
4132
- */
4133
- P.valueOf = function () {
4134
- var x = this,
4135
- Big = x.constructor;
4136
- if (Big.strict === true) {
4137
- throw Error(NAME + 'valueOf disallowed');
4138
- }
4139
- return stringify$1(x, x.e <= Big.NE || x.e >= Big.PE, true);
4140
- };
4141
-
4142
-
4143
- // Export
4144
-
4145
-
4146
- var Big = _Big_();
3116
+ function toScaledStr(num, k) {
3117
+ var abs = Math.abs(num);
3118
+ var signed = num != abs;
3119
+ // String() matches behavior of big.js
3120
+ // (as opposed to toFixed() or toPrecision())
3121
+ var s = abs < 1e-6 ? abs.toFixed(k) : String(abs);
3122
+ var parts = s.split('.');
3123
+ var integer = parts[0] == '0' ? '' : parts[0];
3124
+ var decimal = parts.length == 2 ? parts[1] : '';
3125
+ if (decimal.length < k) {
3126
+ decimal = decimal.padEnd(k, '0');
3127
+ } else if (decimal.length > k) {
3128
+ decimal = decimal.slice(0, k);
3129
+ }
3130
+ var s2 = integer + decimal;
3131
+ // remove any leading 0s
3132
+ while (s2[0] == 0 && s2.length > 1) {
3133
+ s2 = s2.slice(1);
3134
+ }
3135
+ if (signed) {
3136
+ s2 = '-' + s2;
3137
+ }
3138
+ return s2;
3139
+ }
3140
+
3141
+ // returns Number
3142
+ function fromScaledStr(s, decimals) {
3143
+ var signed = s[0] == '-';
3144
+ var uns = signed ? s.slice(1) : s;
3145
+ var s2, len;
3146
+ len = uns.length;
3147
+ if (len > decimals) {
3148
+ s2 = uns.slice(0, len - decimals) + '.' + uns.slice(-decimals);
3149
+ } else if (len == decimals) {
3150
+ s2 = '0.' + uns;
3151
+ } else {
3152
+ s2 = '0.' + uns.padStart(decimals, '0');
3153
+ }
3154
+ if (signed) {
3155
+ s2 = '-' + s2;
3156
+ }
3157
+ return Number(s2);
3158
+ }
3159
+
3160
+ function findBigIntScaleFactor() {
3161
+ var minVal = Infinity;
3162
+ var intLen = 0, s;
3163
+ for (var i=0, n=arguments.length; i<n; i++) {
3164
+ minVal = Math.min(minVal, Math.abs(arguments[i]));
3165
+ }
3166
+ if (minVal >= 1) {
3167
+ s = minVal.toFixed(1);
3168
+ intLen = s.indexOf('.');
3169
+ } else if (minVal !== 0) {
3170
+ s = minVal.toFixed(10).slice(2); // decimal part, up to _ decimals
3171
+ while (s[0] === '0') {
3172
+ intLen--;
3173
+ s = s.slice(1);
3174
+ }
3175
+ }
3176
+ return 17 - intLen;
3177
+ }
3178
+
3179
+ //import { findCrossIntersection_big } from '../geom/mapshaper-segment-geom-big';
4147
3180
 
4148
3181
  // Find the intersection between two 2D segments
4149
3182
  // Returns 0, 1 or 2 [x, y] locations as null, [x, y], or [x1, y1, x2, y2]
@@ -4179,18 +3212,31 @@
4179
3212
  return touches || cross || null;
4180
3213
  }
4181
3214
 
4182
-
4183
3215
  function findCrossIntersection(ax, ay, bx, by, cx, cy, dx, dy, eps) {
4184
3216
  var p;
4185
- if (eps > 0 && !segmentHit_fast(ax, ay, bx, by, cx, cy, dx, dy)) return null;
4186
- else if (eps === 0 && !segmentHit_big(ax, ay, bx, by, cx, cy, dx, dy)) return null;
3217
+ // The normal-precision hit function works for all inputs when eps > 0 because
3218
+ // the geometries that cause the ordinary function fails are detected as
3219
+ // 'touches' or endpoint hits (at least this was true in all the real-world
3220
+ // data samples that were tested).
3221
+ //
3222
+ if (eps > 0 && !segmentHit_fast(ax, ay, bx, by, cx, cy, dx, dy)) {
3223
+ return null;
3224
+ } else if (eps === 0 && !segmentHit_robust(ax, ay, bx, by, cx, cy, dx, dy)) {
3225
+ return null;
3226
+ }
4187
3227
 
4188
- // in a typical layer with many intersections, robust is preferred in
4189
- // most (>90%) segment intersections in order to keep the positional
4190
- // error within a small interval (e.g. 50% of eps)
3228
+ // in a typical layer with many intersections along shared polygon boundaries,
3229
+ // robust is preferred in most (>90%) segment intersections in order to keep
3230
+ // the positional error within a small interval (e.g. 50% of eps)
4191
3231
  //
4192
3232
  if (useRobustCross(ax, ay, bx, by, cx, cy, dx, dy)) {
4193
3233
  p = findCrossIntersection_robust(ax, ay, bx, by, cx, cy, dx, dy);
3234
+ // var p2 = findCrossIntersection_big(ax, ay, bx, by, cx, cy, dx, dy);
3235
+ // var dx = p[0] - p2[0];
3236
+ // var dy = p[1] - p2[1];
3237
+ // if (dx != 0 || dy != 0) {
3238
+ // console.log(dx, dy)
3239
+ // }
4194
3240
  } else {
4195
3241
  p = findCrossIntersection_fast(ax, ay, bx, by, cx, cy, dx, dy);
4196
3242
  }
@@ -4210,41 +3256,6 @@
4210
3256
  return p;
4211
3257
  }
4212
3258
 
4213
- // Find the intersection point of two segments that cross each other,
4214
- // or return null if the segments do not cross.
4215
- // Assumes endpoint intersections have already been detected
4216
- function findCrossIntersection_robust(ax, ay, bx, by, cx, cy, dx, dy) {
4217
- var ax_big = Big(ax);
4218
- var ay_big = Big(ay);
4219
- var bx_big = Big(bx);
4220
- var by_big = Big(by);
4221
- var cx_big = Big(cx);
4222
- var cy_big = Big(cy);
4223
- var dx_big = Big(dx);
4224
- var dy_big = Big(dy);
4225
- var v1x = bx_big.minus(ax_big);
4226
- var v1y = by_big.minus(ay_big);
4227
- var den_big = determinant2D_big(v1x, v1y, dx_big.minus(cx), dy_big.minus(cy));
4228
- if (den_big.eq(0)) {
4229
- debug("DIV0 error (should have been caught upstream)");
4230
- // console.log("hit?", segmentHit_big(ax, ay, bx, by, cx, cy, dx, dy))
4231
- // console.log('Seg 1', getSegFeature(ax, ay, bx, by, true))
4232
- // console.log('Seg 2', getSegFeature(cx, cy, dx, dy, false))
4233
- return null;
4234
- }
4235
- // perform division using regular math, which does not reduce overall
4236
- // precision in test data (big.js division is very slow)
4237
- // tests show identical result to:
4238
- // orient2D_big(cx_big, cy_big, dx_big, dy_big, ax_big, ay_big).div(den_big)
4239
- var m = orient2D_big(cx_big, cy_big, dx_big, dy_big, ax_big,
4240
- ay_big).toNumber() / den_big.toNumber();
4241
- var m_big = Big(m);
4242
- var x_big = ax_big.plus(m_big.times(v1x).round(16));
4243
- var y_big = ay_big.plus(m_big.times(v1y).round(16));
4244
- var p = [x_big.toNumber(), y_big.toNumber()];
4245
- return p;
4246
- }
4247
-
4248
3259
  function findCrossIntersection_fast(ax, ay, bx, by, cx, cy, dx, dy) {
4249
3260
  var den = determinant2D(bx - ax, by - ay, dx - cx, dy - cy);
4250
3261
  var m = orient2D(cx, cy, dx, dy, ax, ay) / den;
@@ -4259,6 +3270,33 @@
4259
3270
  return p;
4260
3271
  }
4261
3272
 
3273
+ // this function, using BigInt, is 3-4x faster than the version using big.js
3274
+ function findCrossIntersection_robust(ax, ay, bx, by, cx, cy, dx, dy) {
3275
+ var d = findBigIntScaleFactor(ax, ay, bx, by, cx, cy, dx, dy);
3276
+ var d2 = 16; // scale numerator of integer division by this many decimal digits
3277
+ var k_bi = 10000000000000000n; // matches d2
3278
+ var ax_bi = BigInt(toScaledStr(ax, d));
3279
+ var ay_bi = BigInt(toScaledStr(ay, d));
3280
+ var bx_bi = BigInt(toScaledStr(bx, d));
3281
+ var by_bi = BigInt(toScaledStr(by, d));
3282
+ var cx_bi = BigInt(toScaledStr(cx, d));
3283
+ var cy_bi = BigInt(toScaledStr(cy, d));
3284
+ var dx_bi = BigInt(toScaledStr(dx, d));
3285
+ var dy_bi = BigInt(toScaledStr(dy, d));
3286
+ var den = determinant2D(bx_bi - ax_bi, by_bi - ay_bi, dx_bi - cx_bi, dy_bi - cy_bi);
3287
+ if (den === 0n) {
3288
+ debug('DIV0 error - should have been identified as collinear "touch" intersection.');
3289
+ return null;
3290
+ }
3291
+ var num = orient2D(cx_bi, cy_bi, dx_bi, dy_bi, ax_bi, ay_bi) * k_bi;
3292
+ var m_bi = num / den;
3293
+ var x_bi = ax_bi * k_bi + m_bi * (bx_bi - ax_bi);
3294
+ var y_bi = ay_bi * k_bi + m_bi * (by_bi - ay_bi);
3295
+ var x = fromScaledStr(x_bi.toString(), d + d2);
3296
+ var y = fromScaledStr(y_bi.toString(), d + d2);
3297
+ return [x, y];
3298
+ }
3299
+
4262
3300
  function useRobustCross(ax, ay, bx, by, cx, cy, dx, dy) {
4263
3301
  // angle and seg length ratio thresholds were found by comparing
4264
3302
  // fast and robust outputs on sample data
@@ -4270,7 +3308,7 @@
4270
3308
  return false;
4271
3309
  }
4272
3310
 
4273
- // Returns smaller unsigned angle between two segments
3311
+ // Returns smaller of two angles between two segments (unsigned)
4274
3312
  function innerAngle(ax, ay, bx, by, cx, cy, dx, dy) {
4275
3313
  var v1x = bx - ax;
4276
3314
  var v1y = by - ay;
@@ -4419,18 +3457,6 @@
4419
3457
  p[1] = y;
4420
3458
  }
4421
3459
 
4422
- // function getSegFeature(x1, y1, x2, y2, hot) {
4423
- // return JSON.stringify({
4424
- // type: "Feature",
4425
- // properties: {
4426
- // stroke: hot ? "orange" : "blue"
4427
- // },
4428
- // geometry: {
4429
- // type: "LineString",
4430
- // coordinates: [[x1, y1], [x2, y2]]
4431
- // }
4432
- // });
4433
- // }
4434
3460
 
4435
3461
  // a: coordinate of point
4436
3462
  // b: endpoint coordinate of segment
@@ -4463,10 +3489,6 @@
4463
3489
  return a * d - b * c;
4464
3490
  }
4465
3491
 
4466
- function determinant2D_big(a, b, c, d) {
4467
- return a.times(d).minus(b.times(c));
4468
- }
4469
-
4470
3492
  // returns a positive value if the points a, b, and c are arranged in
4471
3493
  // counterclockwise order, a negative value if the points are in clockwise
4472
3494
  // order, and zero if the points are collinear.
@@ -4475,22 +3497,31 @@
4475
3497
  return determinant2D(ax - cx, ay - cy, bx - cx, by - cy);
4476
3498
  }
4477
3499
 
4478
- function orient2D_big(ax, ay, bx, by, cx, cy) {
4479
- var a = (ax.minus(cx)).times(by.minus(cy));
4480
- var b = (ay.minus(cy)).times(bx.minus(cx));
4481
- return a.minus(b);
3500
+ function orient2D_robust(ax, ay, bx, by, cx, cy) {
3501
+ var d = findBigIntScaleFactor(ax, ay, bx, by, cx, cy);
3502
+ var ax_bi = BigInt(toScaledStr(ax, d));
3503
+ var ay_bi = BigInt(toScaledStr(ay, d));
3504
+ var bx_bi = BigInt(toScaledStr(bx, d));
3505
+ var by_bi = BigInt(toScaledStr(by, d));
3506
+ var cx_bi = BigInt(toScaledStr(cx, d));
3507
+ var cy_bi = BigInt(toScaledStr(cy, d));
3508
+ var o2d_bi = orient2D(ax_bi, ay_bi, bx_bi, by_bi, cx_bi, cy_bi);
3509
+ return fromScaledStr(o2d_bi.toString(), d);
4482
3510
  }
4483
3511
 
4484
- function orient2D_big2(ax, ay, bx, by, cx, cy) {
4485
- return orient2D_big(Big(ax), Big(ay), Big(bx), Big(by), Big(cx), Big(cy));
3512
+ function segmentHit_robust(ax, ay, bx, by, cx, cy, dx, dy) {
3513
+ var d = findBigIntScaleFactor(ax, ay, bx, by, cx, cy, dx, dy);
3514
+ var ax_bi = BigInt(toScaledStr(ax, d));
3515
+ var ay_bi = BigInt(toScaledStr(ay, d));
3516
+ var bx_bi = BigInt(toScaledStr(bx, d));
3517
+ var by_bi = BigInt(toScaledStr(by, d));
3518
+ var cx_bi = BigInt(toScaledStr(cx, d));
3519
+ var cy_bi = BigInt(toScaledStr(cy, d));
3520
+ var dx_bi = BigInt(toScaledStr(dx, d));
3521
+ var dy_bi = BigInt(toScaledStr(dy, d));
3522
+ return segmentHit_fast(ax_bi, ay_bi, bx_bi, by_bi, cx_bi, cy_bi, dx_bi, dy_bi);
4486
3523
  }
4487
3524
 
4488
-
4489
- // export function orient2D_v2(ax, ay, bx, by, cx, cy) {
4490
- // return -orient2D_robust(ax, ay, bx, by, cx, cy);
4491
- // }
4492
-
4493
-
4494
3525
  // Source: Sedgewick, _Algorithms in C_
4495
3526
  // (Other functions were tried that were more sensitive to floating point errors
4496
3527
  // than this function)
@@ -4501,25 +3532,6 @@
4501
3532
  orient2D(cx, cy, dx, dy, bx, by) <= 0;
4502
3533
  }
4503
3534
 
4504
- function segmentHit_big(ax, ay, bx, by, cx, cy, dx, dy) {
4505
- return orient2D_big(ax, ay, bx, by, cx, cy).times(
4506
- orient2D_big(ax, ay, bx, by, dx, dy)).lte(0) &&
4507
- orient2D_big(cx, cy, dx, dy, ax, ay).times(
4508
- orient2D_big(cx, cy, dx, dy, bx, by)).lte(0);
4509
- }
4510
-
4511
- function segmentHit_big2(ax, ay, bx, by, cx, cy, dx, dy) {
4512
- return segmentHit_big(Big(ax), Big(ay), Big(bx), Big(by),
4513
- Big(cx), Big(cy), Big(dx), Big(dy));
4514
- }
4515
-
4516
- // export function segmentHit_robust(ax, ay, bx, by, cx, cy, dx, dy) {
4517
- // return -orient2D_robust(ax, ay, bx, by, cx, cy) *
4518
- // -orient2D_robust(ax, ay, bx, by, dx, dy) <= 0 &&
4519
- // -orient2D_robust(cx, cy, dx, dy, ax, ay) *
4520
- // -orient2D_robust(cx, cy, dx, dy, bx, by) <= 0;
4521
- // }
4522
-
4523
3535
  // Useful for determining if a segment that intersects another segment is
4524
3536
  // entering or leaving an enclosed buffer area
4525
3537
  // returns -1 if angle of p1p2 -> p3p4 is counter-clockwise (left turn)
@@ -4543,11 +3555,9 @@
4543
3555
  var SegmentGeom = /*#__PURE__*/Object.freeze({
4544
3556
  __proto__: null,
4545
3557
  findClosestPointOnSeg: findClosestPointOnSeg,
3558
+ findCrossIntersection_robust: findCrossIntersection_robust,
4546
3559
  orient2D: orient2D,
4547
- orient2D_big: orient2D_big,
4548
- orient2D_big2: orient2D_big2,
4549
- segmentHit_big: segmentHit_big,
4550
- segmentHit_big2: segmentHit_big2,
3560
+ orient2D_robust: orient2D_robust,
4551
3561
  segmentHit_fast: segmentHit_fast,
4552
3562
  segmentIntersection: segmentIntersection,
4553
3563
  segmentTurn: segmentTurn
@@ -7058,10 +6068,11 @@
7058
6068
  };
7059
6069
 
7060
6070
  this.arcIsDegenerate = function(arcId) {
7061
- return this.arcHasZeroLength(arcId) || this.arcIsSpike(arcId);
6071
+ return this.arcHasZeroLength(arcId) || this.arcIsSpike(arcId) || this.arcIsSpike_v1(arcId);
7062
6072
  };
7063
6073
 
7064
- this.arcIsSpike = function(arcId) {
6074
+ // only finds two-segment spikes
6075
+ this.arcIsSpike_v1 = function(arcId) {
7065
6076
  var iter = this.getArcIter(arcId);
7066
6077
  var x0, y0;
7067
6078
  if (iter.hasNext()) {
@@ -7078,6 +6089,22 @@
7078
6089
  return false;
7079
6090
  };
7080
6091
 
6092
+ // identifies n-segment spikes
6093
+ this.arcIsSpike = function(arcId) {
6094
+ arcId = absArcId(arcId);
6095
+ var i = _ii[arcId];
6096
+ var j = i + _nn[arcId] - 1;
6097
+ while (j > i) {
6098
+ if (_xx[i] != _xx[j] || _yy[i] != _yy[j]) {
6099
+ return false;
6100
+ }
6101
+ j--;
6102
+ i++;
6103
+ }
6104
+ return true;
6105
+ };
6106
+
6107
+
7081
6108
  this.arcHasZeroLength = function(arcId) {
7082
6109
  var iter = this.getArcIter(arcId);
7083
6110
  var i = 0,
@@ -13912,6 +12939,23 @@
13912
12939
  vertexIsArcStart: vertexIsArcStart
13913
12940
  });
13914
12941
 
12942
+ function debugConnectedArcs(ids, arcs) {
12943
+ var colors = ['orange', 'blue', 'green', 'red', 'magenta', 'grey'];
12944
+ var features = ids.map(function(arcId, i) {
12945
+ return getArcFeature(arcId, arcs, {arcId: arcId, stroke: colors[i] || 'black'});
12946
+ });
12947
+ var geojson = '{"type": "FeatureCollection", "features": [' + features.join(',') + ']}';
12948
+ debug(geojson);
12949
+ }
12950
+
12951
+ function getArcFeature(arcId, arcs, properties) {
12952
+ return JSON.stringify({
12953
+ type: "Feature",
12954
+ properties: properties,
12955
+ geometry: GeoJSON.exportLineGeom([[arcId]], arcs)
12956
+ });
12957
+ }
12958
+
13915
12959
  function isValidArc(arcId, arcs) {
13916
12960
  // check for arcs with no vertices
13917
12961
  // TODO: also check for other kinds of degenerate arcs
@@ -13958,7 +13002,6 @@
13958
13002
  error("Duplicate point error");
13959
13003
  }*/
13960
13004
 
13961
-
13962
13005
  for (j=0; j<ids.length; j++) {
13963
13006
  candId = ids[j];
13964
13007
  if (!isValidArc(candId, arcs)) {
@@ -13977,6 +13020,10 @@
13977
13020
 
13978
13021
  if (xx[ito] == xx[icand] && yy[ito] == yy[icand]) {
13979
13022
  debug("Pathfinder warning: duplicate segments: i:", ito, "j:", icand, "ids:", [fromArcId].concat(ids));
13023
+ if (useDebug()) {
13024
+ // debugDuplicatePathfinderSegments(nodeX, nodeY, ito, icand, nodes.arcs);
13025
+ debugConnectedArcs(ids, nodes.arcs);
13026
+ }
13980
13027
  }
13981
13028
  if (code == 2) {
13982
13029
  ito = icand;
@@ -14027,8 +13074,7 @@
14027
13074
  }
14028
13075
 
14029
13076
  function chooseBetweenClosePaths(nodeX, nodeY, ax, ay, bx, by) {
14030
- // var orient = orient2D_big2(ax, ay, 0, 0, bx, by);
14031
- var orient = orient2D_big2(ax, ay, nodeX, nodeY, bx, by);
13077
+ var orient = orient2D_robust(ax, ay, nodeX, nodeY, bx, by);
14032
13078
  var code;
14033
13079
  if (orient > 0) {
14034
13080
  code = 2;
@@ -17728,6 +16774,7 @@
17728
16774
 
17729
16775
  function cleanPath(path, arcs) {
17730
16776
  var nulls = 0;
16777
+ // console.log("[cleanPath()]", path, path?.[0])
17731
16778
  for (var i=0, n=path.length; i<n; i++) {
17732
16779
  if (arcs.arcIsDegenerate(path[i])) {
17733
16780
  nulls++;
@@ -18016,17 +17063,14 @@
18016
17063
  if (changed || opts.rebuild_topology) {
18017
17064
  buildTopology(dataset);
18018
17065
  }
18019
-
18020
- // Clean shapes by removing collapsed arc references, etc.
18021
- // TODO: consider alternative -- avoid creating degenerate arcs
18022
- // in insertCutPoints()
17066
+ // Remove degenerate shapes
17067
+ // Without this step, pathfinder function would encounter dead ends.
18023
17068
  dataset.layers.forEach(function(lyr) {
18024
17069
  if (layerHasPaths(lyr)) {
18025
17070
  cleanShapes(lyr.shapes, arcs, lyr.geometry_type);
18026
17071
  }
18027
17072
  });
18028
-
18029
- // Further clean-up -- remove duplicate and missing arcs
17073
+ // Further clean-up -- remove duplicate and unused arcs, etc.
18030
17074
  nodes = cleanArcReferences(dataset);
18031
17075
  return nodes;
18032
17076
  }
@@ -18036,38 +17080,26 @@
18036
17080
  var cutOpts = snapDist > 0 ? {tolerance: snapDist} : {tolerance: 0};
18037
17081
  var coordsHaveChanged = false;
18038
17082
  var snapCount, dupeCount, cutCount;
18039
- var maxPasses = 4, passCount = 0;
18040
- snapCount = snapCoordsByInterval(arcs, snapDist);
18041
- dupeCount = arcs.dedupCoords();
18042
-
18043
- // why was topology built here previously????
18044
- // if (snapCount > 0 || dupeCount > 0) {
18045
- // // Detect topology again if coordinates have changed
18046
- // internal.buildTopology(dataset);
18047
- // }
17083
+ var maxLoops = 4, loopCount = 0;
18048
17084
 
18049
- // cut arcs at points where segments intersect
18050
- cutCount = cutPathsAtIntersections(dataset, cutOpts);
18051
- passCount++;
18052
- if (cutCount > 0 || snapCount > 0 || dupeCount > 0) {
18053
- coordsHaveChanged = true;
18054
- }
18055
-
18056
- // perform a second snap + cut pass if needed
18057
- while (cutCount > 0 && passCount < maxPasses) {
18058
- passCount++;
17085
+ // snap + cut until no more cuts are needed
17086
+ do {
17087
+ loopCount++;
17088
+ cutCount = 0;
18059
17089
  snapCount = snapCoordsByInterval(arcs, snapDist);
18060
17090
  dupeCount = arcs.dedupCoords();
18061
- // cutCount = 0;
18062
- if (snapCount > 0 || cutCount > 0) {
17091
+ if (snapCount > 0 || cutCount > 0 || loopCount == 1) {
17092
+ // cut arcs at points where segments intersect
18063
17093
  cutCount = cutPathsAtIntersections(dataset, cutOpts);
18064
- debug("[snapAndCut] pass:", passCount, "snaps:", snapCount, "dupes:", dupeCount, "cuts:", cutCount);
18065
17094
  }
18066
- }
17095
+ coordsHaveChanged |= (snapCount + dupeCount + cutCount) > 0;
17096
+ debug("[snapAndCut] pass:", loopCount, "snaps:", snapCount, "dupes:", dupeCount, "cuts:", cutCount);
17097
+ } while (loopCount < maxLoops && cutCount > 0);
18067
17098
 
18068
- // if (cutCount > 0) {
17099
+ // should this be added?
17100
+ // if (cutCount > 0 && loopCount == maxLoops) {
17101
+ // snapCount = snapCoordsByInterval(arcs, snapDist);
18069
17102
  // arcs.dedupCoords(); // need to do this here?
18070
- // debug('Second-pass vertices added:', cutCount, 'consider third pass?');
18071
17103
  // }
18072
17104
 
18073
17105
  return coordsHaveChanged;
@@ -19596,6 +18628,7 @@
19596
18628
  return geom;
19597
18629
  };
19598
18630
 
18631
+ // ids: shape data (array of array of arc ids)
19599
18632
  GeoJSON.exportLineGeom = function(ids, arcs) {
19600
18633
  var obj = exportPathData(ids, arcs, "polyline");
19601
18634
  if (obj.pointCount === 0) return null;
@@ -19611,6 +18644,7 @@
19611
18644
  };
19612
18645
  };
19613
18646
 
18647
+ // ids: shape data (array of array of arc ids)
19614
18648
  GeoJSON.exportPolygonGeom = function(ids, arcs, opts) {
19615
18649
  var obj = exportPathData(ids, arcs, "polygon");
19616
18650
  if (obj.pointCount === 0) return null;
@@ -47259,7 +46293,7 @@ ${svg}
47259
46293
  });
47260
46294
  }
47261
46295
 
47262
- var version = "0.6.110";
46296
+ var version = "0.6.111";
47263
46297
 
47264
46298
  // Parse command line args into commands and run them
47265
46299
  // Function takes an optional Node-style callback. A Promise is returned if no callback is given.