All files / lib/path CurveLocation.ts

91.31% Statements 284/311
96.35% Branches 132/137
86.2% Functions 25/29
91.31% Lines 284/311

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621                                1x 1x 1x 1x 1x                                   1x 1x 1x                     1x     4901x 1480x 1480x 1430x 1430x 1430x 1480x 4901x 4901x 4901x 4901x 4901x   4901x 4901x   1x       6778x 6778x 6778x   1x 6778x 6778x 6778x       6778x 6778x 6778x   1x 1835x 1835x 1835x 1835x         1835x 1835x   1835x 1835x               1x   86x 86x 42x 42x 42x 30x 42x   12x   6x 6x 42x 42x 86x 86x               1x 44858x 44858x 44858x     102x 102x           44858x 246x 246x   42x 42x 42x 246x   44858x 44858x 102x 82x 62x   44858x               1x 11446x 11446x 11446x                 1x 8208x 8208x 8208x                   1x 13326x 13326x 13326x 13326x             1x                 1x 9555x 9555x                   1x 5754x 5754x 1844x 1844x 1844x 1844x 1838x 1838x 1838x 1844x 1844x 5754x 5754x                 1x 1848x 1848x 1848x 1848x 1848x 1848x 1848x 1848x                   1x                                                                       1x           1x                   1x 19x 19x 19x 19x   19x 19x 19x 19x                   1x 3079x 3079x 3079x 3079x 3079x 3079x 3079x         2875x 2875x 2875x 2875x 2875x 2875x 2875x     894x 2875x 3079x 3079x 3079x         1x                                           1x 621x 621x     9x 9x 9x 9x 612x 621x                   1x       1778x 1778x 1778x 1778x 1778x 1778x   1778x 1778x     1778x               1157x 1778x 1778x 1778x   1778x 1778x 1778x                 1150x   1150x     4270x 4270x 4270x 4270x     4270x 4270x   1150x 3156x 1266x   1890x 3156x   1778x 1058x 1058x 1058x 1778x 1077x 1077x 1077x 1150x     1150x 1778x 1778x 1778x 1778x 1778x 1778x 1778x 1778x         1778x   92x   51x   1058x   377x 1778x                     1x 2230x 2230x 1x 1x 1x 1x         216x 216x 1397x 1397x 1397x 1397x 216x 1x   1x 1x 1x   1x     36x     2250x 2250x 2250x   2250x           3834x   4026x   4026x 3918x 4026x 3608x 3834x   2250x 5151x 5151x 5151x     5151x     360x 129x 129x 360x 360x 4791x 4791x     4791x 4791x   1643x     3148x 5151x 874x 5151x 3917x 3917x 5151x   1890x 1890x 2250x   36x 36x 36x   36x     545x 545x 906x 906x 545x 545x 36x 36x 1x 1x   1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import type { Path } from './Path';
import { ref } from '~/globals';
import { Base } from '~/straps';
import { Numerical } from '~/util/Numerical';
import { Formatter } from '~/util/Formatter';
import { Curve } from './Curve';
 
/**
 * @name CurveLocation
 *
 * @class CurveLocation objects describe a location on {@link Curve} objects, as
 *     defined by the curve-time {@link #time}, a value between `0` (beginning
 *     of the curve) and `1` (end of the curve). If the curve is part of a
 *     {@link Path} item, its {@link #index} inside the {@link Path#curves}
 *     array is also provided.
 *
 * The class is in use in many places, such as
 * {@link Path#getLocationAt(offset)},
 * {@link Path#getLocationOf(point)},
 * {@link PathItem#getNearestLocation(point)},
 * {@link PathItem#getIntersections(path)},
 * etc.
 */
export const CurveLocation = Base.extend(
  /** @lends CurveLocation# */ {
    _class: 'CurveLocation',
 
    // DOCS: CurveLocation class description: add these back when the mentioned
    // functioned have been added: {@link Path#split(location)}
    /**
     * Creates a new CurveLocation object.
     *
     * @param {Curve} curve
     * @param {Number} time
     * @param {Point} [point]
     */
    initialize: function CurveLocation(curve, time, point, _overlap, _distance) {
      // Merge intersections very close to the end of a curve with the
      // beginning of the next curve.
      if (time >= /*#=*/ 1 - Numerical.CURVETIME_EPSILON) {
        var next = curve.getNext();
        if (next) {
          time = 0;
          curve = next;
        }
      }
      this._setCurve(curve);
      this._time = time;
      this._point = point || curve.getPointAtTime(time);
      this._overlap = _overlap;
      this._distance = _distance;
      // Properties related to linked intersection locations
      this._intersection = this._next = this._previous = null;
    },
 
    _setPath: function (path) {
      // We only store the path to verify versions for cached values.
      // To ensure we use the right path (e.g. after splitting), we shall
      // always access the path on the result of getCurve().
      this._path = path;
      this._version = path ? path._version : 0;
    },
 
    _setCurve: function (curve) {
      this._setPath(curve._path);
      this._curve = curve;
      this._segment = null; // To be determined, see #getSegment()
      // Also store references to segment1 and segment2, in case path
      // splitting / dividing is going to happen, in which case the segments
      // can be used to determine the new curves, see #getCurve(true)
      this._segment1 = curve._segment1;
      this._segment2 = curve._segment2;
    },
 
    _setSegment: function (segment) {
      var curve = segment.getCurve();
      if (curve) {
        this._setCurve(curve);
      } else {
        this._setPath(segment._path);
        this._segment1 = segment;
        this._segment2 = null;
      }
      this._segment = segment;
      this._time = segment === this._segment1 ? 0 : 1;
      // To avoid issues with imprecision in getCurve() / trySegment()
      this._point = segment._point.clone();
    },
 
    /**
     * The segment of the curve which is closer to the described location.
     *
     * @bean
     * @type Segment
     */
    getSegment: function () {
      // Request curve first, so _segment gets invalidated if it's out of sync
      var segment = this._segment;
      if (!segment) {
        var curve = this.getCurve(),
          time = this.getTime();
        if (time === 0) {
          segment = curve._segment1;
        } else if (time === 1) {
          segment = curve._segment2;
        } else if (time != null) {
          // Determine the closest segment by comparing curve lengths
          segment = curve.getPartLength(0, time) < curve.getPartLength(time, 1) ? curve._segment1 : curve._segment2;
        }
        this._segment = segment;
      }
      return segment;
    },
 
    /**
     * The curve that this location belongs to.
     *
     * @bean
     * @type Curve
     */
    getCurve: function () {
      var path = this._path,
        that = this;
      if (path && path._version !== this._version) {
        // If the path's segments have changed, clear the cached time and
        // offset values and force re-fetching of the correct curve.
        this._time = this._offset = this._curveOffset = this._curve = null;
      }
 
      // If path is out of sync, access current curve objects through segment1
      // / segment2. Since path splitting or dividing might have happened in
      // the meantime, try segment1's curve, and see if _point lies on it
      // still, otherwise assume it's the curve before segment2.
      function trySegment(segment) {
        var curve = segment && segment.getCurve();
        if (curve && (that._time = curve.getTimeOf(that._point)) != null) {
          // Fetch path again as it could be on a new one through split()
          that._setCurve(curve);
          return curve;
        }
      }
 
      return (
        this._curve ||
        trySegment(this._segment) ||
        trySegment(this._segment1) ||
        trySegment(this._segment2.getPrevious())
      );
    },
 
    /**
     * The path that this locations is situated on.
     *
     * @bean
     * @type Path
     */
    getPath: function () {
      var curve = this.getCurve();
      return curve && curve._path;
    },
 
    /**
     * The index of the {@link #curve} within the {@link Path#curves} list, if
     * it is part of a {@link Path} item.
     *
     * @bean
     * @type Number
     */
    getIndex: function () {
      var curve = this.getCurve();
      return curve && curve.getIndex();
    },
 
    /**
     * The curve-time parameter, as used by various bezier curve calculations.
     * It is value between `0` (beginning of the curve) and `1` (end of the
     * curve).
     *
     * @bean
     * @type Number
     */
    getTime: function () {
      var curve = this.getCurve(),
        time = this._time;
      return curve && time == null ? (this._time = curve.getTimeOf(this._point)) : time;
    },
 
    /**
     * @private
     * @bean
     * @deprecated use {@link #time} instead.
     */
    getParameter: '#getTime',
 
    /**
     * The point which is defined by the {@link #curve} and
     * {@link #time}.
     *
     * @bean
     * @type Point
     */
    getPoint: function () {
      return this._point;
    },
 
    /**
     * The length of the path from its beginning up to the location described
     * by this object. If the curve is not part of a path, then the length
     * within the curve is returned instead.
     *
     * @bean
     * @type Number
     */
    getOffset: function () {
      var offset = this._offset;
      if (offset == null) {
        offset = 0;
        var path = this.getPath(),
          index = this.getIndex();
        if (path && index != null) {
          var curves = path.getCurves();
          for (var i = 0; i < index; i++) offset += curves[i].getLength();
        }
        this._offset = offset += this.getCurveOffset();
      }
      return offset;
    },
 
    /**
     * The length of the curve from its beginning up to the location described
     * by this object.
     *
     * @bean
     * @type Number
     */
    getCurveOffset: function () {
      var offset = this._curveOffset;
      if (offset == null) {
        var curve = this.getCurve(),
          time = this.getTime();
        this._curveOffset = offset = time != null && curve && curve.getPartLength(0, time);
      }
      return offset;
    },
 
    /**
     * The curve location on the intersecting curve, if this location is the
     * result of a call to {@link PathItem#getIntersections(path)} /
     * {@link Curve#getIntersections(curve)}.
     *
     * @bean
     * @type CurveLocation
     */
    getIntersection: function () {
      return this._intersection;
    },
 
    /**
     * The tangential vector to the {@link #curve} at the given location.
     *
     * @name CurveLocation#getTangent
     * @bean
     * @type Point
     */
 
    /**
     * The normal vector to the {@link #curve} at the given location.
     *
     * @name CurveLocation#getNormal
     * @bean
     * @type Point
     */
 
    /**
     * The curvature of the {@link #curve} at the given location.
     *
     * @name CurveLocation#getCurvature
     * @bean
     * @type Number
     */
 
    /**
     * The distance from the queried point to the returned location.
     *
     * @bean
     * @type Number
     * @see Curve#getNearestLocation(point)
     * @see PathItem#getNearestLocation(point)
     */
    getDistance: function () {
      return this._distance;
    },
 
    // DOCS: divide(), split()
 
    divide: function () {
      var curve = this.getCurve(),
        res = curve && curve.divideAtTime(this.getTime());
      // Change to the newly inserted segment, also adjusts _time.
      if (res) {
        this._setSegment(res._segment1);
      }
      return res;
    },
 
    split: function () {
      var curve = this.getCurve(),
        path = curve._path,
        res = curve && curve.splitAtTime(this.getTime());
      if (res) {
        // Set the segment to the end-segment of the path after splitting.
        this._setSegment(path.getLastSegment());
      }
      return res;
    },
 
    /**
     * Checks whether tow CurveLocation objects are describing the same location
     * on a path, by applying the same tolerances as elsewhere when dealing with
     * curve-time parameters.
     *
     * @param {CurveLocation} location
     * @return {Boolean} {@true if the locations are equal}
     */
    equals: function (loc, _ignoreOther) {
      var res = this === loc;
      if (!res && loc instanceof CurveLocation) {
        var c1 = this.getCurve(),
          c2 = loc.getCurve(),
          p1 = c1._path,
          p2 = c2._path;
        if (p1 === p2) {
          // instead of comparing curve-time, compare the actual offsets
          // of both locations to determine if they're in the same spot,
          // taking into account the wrapping around path ends. This is
          // necessary to avoid issues wit CURVETIME_EPSILON imprecisions.
          var abs = Math.abs,
            epsilon = /*#=*/ Numerical.GEOMETRIC_EPSILON,
            diff = abs(this.getOffset() - loc.getOffset()),
            i1 = !_ignoreOther && this._intersection,
            i2 = !_ignoreOther && loc._intersection;
          res =
            (diff < epsilon || (p1 && abs(p1.getLength() - diff) < epsilon)) &&
            // Compare the the other location, but prevent endless
            // recursion by passing `true` for _ignoreOther.
            ((!i1 && !i2) || (i1 && i2 && i1.equals(i2, true)));
        }
      }
      return res;
    },
 
    /**
     * @return {String} a string representation of the curve location
     */
    toString: function () {
      var parts = [],
        point = this.getPoint(),
        f = Formatter.instance;
      if (point) parts.push('point: ' + point);
      var index = this.getIndex();
      if (index != null) parts.push('index: ' + index);
      var time = this.getTime();
      if (time != null) parts.push('time: ' + f.number(time));
      if (this._distance != null) parts.push('distance: ' + f.number(this._distance));
      return '{ ' + parts.join(', ') + ' }';
    },
 
    /**
     * {@grouptitle Tests}
     * Checks if the location is an intersection with another curve and is
     * merely touching the other curve, as opposed to crossing it.
     *
     * @return {Boolean} {@true if the location is an intersection that is
     * merely touching another curve}
     * @see #isCrossing()
     */
    isTouching: function () {
      var inter = this._intersection;
      if (inter && this.getTangent().isCollinear(inter.getTangent())) {
        // Only consider two straight curves as touching if their lines
        // don't intersect.
        var curve1 = this.getCurve(),
          curve2 = inter.getCurve();
        return !(curve1.isStraight() && curve2.isStraight() && curve1.getLine().intersect(curve2.getLine()));
      }
      return false;
    },
 
    /**
     * Checks if the location is an intersection with another curve and is
     * crossing the other curve, as opposed to just touching it.
     *
     * @return {Boolean} {@true if the location is an intersection that is
     * crossing another curve}
     * @see #isTouching()
     */
    isCrossing: function () {
      // Implementation loosely based on work by Andy Finnell:
      // http://losingfight.com/blog/2011/07/09/how-to-implement-boolean-operations-on-bezier-paths-part-3/
      // https://bitbucket.org/andyfinnell/vectorboolean
      var inter = this._intersection;
      if (!inter) return false;
      var t1 = this.getTime(),
        t2 = inter.getTime(),
        tMin = /*#=*/ Numerical.CURVETIME_EPSILON,
        tMax = 1 - tMin,
        // t*Inside specifies if the found intersection is inside the curve.
        t1Inside = t1 >= tMin && t1 <= tMax,
        t2Inside = t2 >= tMin && t2 <= tMax;
      // If the intersection is in the middle of both paths, it is either a
      // tangent or a crossing, no need for the detailed corner check below:
      if (t1Inside && t2Inside) return !this.isTouching();
      // Now get the references to the 4 curves involved in the intersection:
      // - c1 & c2 are the curves on the first intersecting path, left and
      //   right of the intersection.
      // - c3 & c4 are the same for the second intersecting path.
      // - If the intersection is in the middle of the curve (t*Inside), then
      //   both values point to the same curve, and the curve-time is to be
      //   handled accordingly further down.
      var c2 = this.getCurve(),
        c1 = c2 && t1 < tMin ? c2.getPrevious() : c2,
        c4 = inter.getCurve(),
        c3 = c4 && t2 < tMin ? c4.getPrevious() : c4;
      // If t1 / t2 are at the end, then step to the next curve.
      if (t1 > tMax) c2 = c2.getNext();
      if (t2 > tMax) c4 = c4.getNext();
      if (!c1 || !c2 || !c3 || !c4) return false;
 
      // Calculate unambiguous angles for all 4 tangents at the intersection:
      // - If the intersection is inside a curve (t1 / t2Inside), the tangent
      //   at t1 / t2 is unambiguous, because the curve is continuous.
      // - If the intersection is on a segment, step away at equal offsets on
      //   each curve, to calculate unambiguous angles. The vector from the
      //   intersection to this new location is used to determine the angle.
 
      var offsets = [];
 
      function addOffsets(curve, end) {
        // Find the largest offset of unambiguous direction on the curve,
        // taking their loops, cusps, inflections, and "peaks" into account.
        var v = curve.getValues(),
          roots = Curve.classify(v).roots || Curve.getPeaks(v),
          count = roots.length,
          offset = Curve.getLength(v, end && count ? roots[count - 1] : 0, !end && count ? roots[0] : 1);
        // When no root was found, the full length was calculated. Use a
        // fraction of it. By trial & error, 64 was determined to work well.
        offsets.push(count ? offset : offset / 32);
      }
 
      function isInRange(angle, min, max) {
        return min < max
          ? angle > min && angle < max
          : // min > max: the range wraps around -180 / 180 degrees
            angle > min || angle < max;
      }
 
      if (!t1Inside) {
        addOffsets(c1, true);
        addOffsets(c2, false);
      }
      if (!t2Inside) {
        addOffsets(c3, true);
        addOffsets(c4, false);
      }
      var pt = this.getPoint(),
        // Determined the shared unambiguous offset by the taking the
        // shortest offsets on all involved curves that are unambiguous.
        offset = Math.min.apply(Math, offsets),
        v2 = t1Inside ? c2.getTangentAtTime(t1) : c2.getPointAt(offset).subtract(pt),
        v1 = t1Inside ? v2.negate() : c1.getPointAt(-offset).subtract(pt),
        v4 = t2Inside ? c4.getTangentAtTime(t2) : c4.getPointAt(offset).subtract(pt),
        v3 = t2Inside ? v4.negate() : c3.getPointAt(-offset).subtract(pt),
        a1 = v1.getAngle(),
        a2 = v2.getAngle(),
        a3 = v3.getAngle(),
        a4 = v4.getAngle();
      // Count how many times curve2 angles appear between the curve1 angles.
      // If each pair of angles split the other two, then the edges cross.
      // Use t1Inside to decide which angle pair to check against.
      // If t1 is inside the curve, check against a3 & a4, otherwise a1 & a2.
      return !!(t1Inside
        ? // @ts-expect-error = The '^' operator is not allowed for boolean types
          isInRange(a1, a3, a4) ^ isInRange(a2, a3, a4) &&
          // @ts-expect-error = The '^' operator is not allowed for boolean types
          isInRange(a1, a4, a3) ^ isInRange(a2, a4, a3)
        : // @ts-expect-error = The '^' operator is not allowed for boolean types
          isInRange(a3, a1, a2) ^ isInRange(a4, a1, a2) &&
          // @ts-expect-error = The '^' operator is not allowed for boolean types
          isInRange(a3, a2, a1) ^ isInRange(a4, a2, a1));
    },
 
    /**
     * Checks if the location is an intersection with another curve and is
     * part of an overlap between the two involved paths.
     *
     * @return {Boolean} {@true if the location is an intersection that is
     * part of an overlap between the two involved paths}
     * @see #isCrossing()
     * @see #isTouching()
     */
    hasOverlap: function () {
      return !!this._overlap;
    },
  },
  Base.each(
    Curve._evaluateMethods,
    function (name) {
      // Produce getters for #getTangent() / #getNormal() / #getCurvature()
      // NOTE: (For easier searching): This loop produces:
      // getPointAt, getTangentAt, getNormalAt, getWeightedTangentAt,
      // getWeightedNormalAt, getCurvatureAt
      var get = name + 'At';
      this[name] = function () {
        var curve = this.getCurve(),
          time = this.getTime();
        return time != null && curve && curve[get](time, true);
      };
    },
    {
      // Do not override the existing #getPoint():
      preserve: true,
    }
  ),
  // @ts-expect-error = 'new' expression, whose target lacks a construct signature
  new (function () {
    // Scope for statics
 
    function insert(locations, loc, merge) {
      // Insert-sort by path-id, curve, time so we can easily merge
      // duplicates with calls to equals() after.
      var length = locations.length,
        l = 0,
        r = length - 1;
 
      function search(index, dir) {
        // If we reach the beginning/end of the list, also compare with the
        // location at the other end, as paths are circular lists.
        // NOTE: When merging, the locations array will only contain
        // locations on the same path, so it is fine that check for the end
        // to address circularity. See PathItem#getIntersections()
        for (var i = index + dir; i >= -1 && i <= length; i += dir) {
          // Wrap the index around, to match the other ends:
          var loc2 = locations[((i % length) + length) % length];
          // Once we're outside the spot, we can stop searching.
          if (!loc.getPoint().isClose(loc2.getPoint(), /*#=*/ Numerical.GEOMETRIC_EPSILON)) break;
          if (loc.equals(loc2)) return loc2;
        }
        return null;
      }
 
      while (l <= r) {
        var m = (l + r) >>> 1,
          loc2 = locations[m],
          found;
        // See if the two locations are actually the same, and merge if
        // they are. If they aren't check the other neighbors with search()
        if (merge && (found = loc.equals(loc2) ? loc2 : search(m, -1) || search(m, 1))) {
          // We're done, don't insert, merge with the found location
          // instead, and carry over overlap:
          if (loc._overlap) {
            found._overlap = found._intersection._overlap = true;
          }
          return found;
        }
        var path1 = loc.getPath(),
          path2 = loc2.getPath(),
          // NOTE: equals() takes the intersection location into account,
          // while this calculation of diff doesn't!
          diff =
            path1 !== path2
              ? // Sort by path id to group all locs on same path.
                path1._id - path2._id
              : // Sort by both index and time on the same path. The two values
                // added together provides a convenient sorting index.
                loc.getIndex() + loc.getTime() - (loc2.getIndex() + loc2.getTime());
        if (diff < 0) {
          r = m - 1;
        } else {
          l = m + 1;
        }
      }
      // We didn't merge with a preexisting location, insert it now.
      locations.splice(l, 0, loc);
      return loc;
    }
 
    return {
      statics: {
        insert: insert,
 
        expand: function (locations) {
          // Create a copy since insert() keeps modifying the array and
          // inserting at sorted indices.
          var expanded = locations.slice();
          for (var i = locations.length - 1; i >= 0; i--) {
            insert(expanded, locations[i]._intersection, false);
          }
          return expanded;
        },
      },
    };
  })()
);
 
ref.CurveLocation = CurveLocation;