# all files / ol/geom/flat/ interpolateflatgeom.js

95.45% Statements 105/110
93.18% Branches 41/44
100% Functions 3/3
95.45% Lines 105/110

    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
    1941×

    1×
    1×
    1×

    1×

    9×

    9×
    9×
    9×
    9×

    9×
    1×
    1×
    8×
    3×

    3×

    5×
    5×
    5×
    5×
    5×
    5×
    14×
    14×
    14×
    14×
    14×
    14×

    5×
    5×
    5×
    4×

    4×
    4×

    4×

    1×
    1×

    9×
    1×
    1×
    1×

    8×

    1×

    87×

    87×
    87×
    4×
    2×
    2×
    2×

    2×

    83×
    4×
    2×
    2×
    2×

    2×

    79×
    9×

    70×
    70×
    70×
    211×
    211×
    97×

    114×

    70×
    70×
    22×

    48×
    48×
    48×
    48×
    48×
    48×
    48×
    130×

    48×
    48×

    48×

    1×

    36×
    18×

    18×
    18×
    2×
    1×
    1×
    1×

    1×

    16×
    2×
    1×
    1×
    1×

    1×

    14×
    14×
    22×
    22×

    22×
    2×
    20×
    12×

    8×

     goog.provide('ol.geom.flat.interpolate');

    goog.require('goog.array');
    goog.require('goog.asserts');
    goog.require('goog.math');

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {number} end End.
     * @param {number} stride Stride.
     * @param {number} fraction Fraction.
     * @param {Array.<number>=} opt_dest Destination.
     * @return {Array.<number>} Destination.
     */
    ol.geom.flat.interpolate.lineString =
        function(flatCoordinates, offset, end, stride, fraction, opt_dest) {
      // FIXME interpolate extra dimensions
      goog.asserts.assert(0 <= fraction && fraction <= 1,
          'fraction should be in between 0 and 1');
      var pointX = NaN;
      var pointY = NaN;
      var n = (end - offset) / stride;
      Iif (n === 0) {
        goog.asserts.fail('n cannot be 0');
      } else if (n == 1) {
        pointX = flatCoordinates[offset];
        pointY = flatCoordinates[offset + 1];
      } else if (n == 2) {
        pointX = (1 - fraction) * flatCoordinates[offset] +
            fraction * flatCoordinates[offset + stride];
        pointY = (1 - fraction) * flatCoordinates[offset + 1] +
            fraction * flatCoordinates[offset + stride + 1];
      } else {
        var x1 = flatCoordinates[offset];
        var y1 = flatCoordinates[offset + 1];
        var length = 0;
        var cumulativeLengths = [0];
        var i;
        for (i = offset + stride; i < end; i += stride) {
          var x2 = flatCoordinates[i];
          var y2 = flatCoordinates[i + 1];
          length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
          cumulativeLengths.push(length);
          x1 = x2;
          y1 = y2;
        }
        var target = fraction * length;
        var index = goog.array.binarySearch(cumulativeLengths, target);
        if (index < 0) {
          var t = (target - cumulativeLengths[-index - 2]) /
              (cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]);
          var o = offset + (-index - 2) * stride;
          pointX = goog.math.lerp(
              flatCoordinates[o], flatCoordinates[o + stride], t);
          pointY = goog.math.lerp(
              flatCoordinates[o + 1], flatCoordinates[o + stride + 1], t);
        } else {
          pointX = flatCoordinates[offset + index * stride];
          pointY = flatCoordinates[offset + index * stride + 1];
        }
      }
      if (opt_dest) {
        opt_dest[0] = pointX;
        opt_dest[1] = pointY;
        return opt_dest;
      } else {
        return [pointX, pointY];
      }
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {number} end End.
     * @param {number} stride Stride.
     * @param {number} m M.
     * @param {boolean} extrapolate Extrapolate.
     * @return {ol.Coordinate} Coordinate.
     */
    ol.geom.flat.lineStringCoordinateAtM =
        function(flatCoordinates, offset, end, stride, m, extrapolate) {
      Iif (end == offset) {
        return null;
      }
      var coordinate;
      if (m < flatCoordinates[offset + stride - 1]) {
        if (extrapolate) {
          coordinate = flatCoordinates.slice(offset, offset + stride);
          coordinate[stride - 1] = m;
          return coordinate;
        } else {
          return null;
        }
      } else if (flatCoordinates[end - 1] < m) {
        if (extrapolate) {
          coordinate = flatCoordinates.slice(end - stride, end);
          coordinate[stride - 1] = m;
          return coordinate;
        } else {
          return null;
        }
      }
      // FIXME use O(1) search
      if (m == flatCoordinates[offset + stride - 1]) {
        return flatCoordinates.slice(offset, offset + stride);
      }
      var lo = offset / stride;
      var hi = end / stride;
      while (lo < hi) {
        var mid = (lo + hi) >> 1;
        if (m < flatCoordinates[(mid + 1) * stride - 1]) {
          hi = mid;
        } else {
          lo = mid + 1;
        }
      }
      var m0 = flatCoordinates[lo * stride - 1];
      if (m == m0) {
        return flatCoordinates.slice((lo - 1) * stride, (lo - 1) * stride + stride);
      }
      var m1 = flatCoordinates[(lo + 1) * stride - 1];
      goog.asserts.assert(m0 < m, 'm0 should be less than m');
      goog.asserts.assert(m <= m1, 'm should be less than or equal to m1');
      var t = (m - m0) / (m1 - m0);
      coordinate = [];
      var i;
      for (i = 0; i < stride - 1; ++i) {
        coordinate.push(goog.math.lerp(flatCoordinates[(lo - 1) * stride + i],
            flatCoordinates[lo * stride + i], t));
      }
      coordinate.push(m);
      goog.asserts.assert(coordinate.length == stride,
          'length of coordinate array should match stride');
      return coordinate;
    };

    /**
     * @param {Array.<number>} flatCoordinates Flat coordinates.
     * @param {number} offset Offset.
     * @param {Array.<number>} ends Ends.
     * @param {number} stride Stride.
     * @param {number} m M.
     * @param {boolean} extrapolate Extrapolate.
     * @param {boolean} interpolate Interpolate.
     * @return {ol.Coordinate} Coordinate.
     */
    ol.geom.flat.lineStringsCoordinateAtM = function(
        flatCoordinates, offset, ends, stride, m, extrapolate, interpolate) {
      if (interpolate) {
        return ol.geom.flat.lineStringCoordinateAtM(
            flatCoordinates, offset, ends[ends.length - 1], stride, m, extrapolate);
      }
      var coordinate;
      if (m < flatCoordinates[stride - 1]) {
        if (extrapolate) {
          coordinate = flatCoordinates.slice(0, stride);
          coordinate[stride - 1] = m;
          return coordinate;
        } else {
          return null;
        }
      }
      if (flatCoordinates[flatCoordinates.length - 1] < m) {
        if (extrapolate) {
          coordinate = flatCoordinates.slice(flatCoordinates.length - stride);
          coordinate[stride - 1] = m;
          return coordinate;
        } else {
          return null;
        }
      }
      var i, ii;
      for (i = 0, ii = ends.length; i < ii; ++i) {
        var end = ends[i];
        Iif (offset == end) {
          continue;
        }
        if (m < flatCoordinates[offset + stride - 1]) {
          return null;
        } else if (m <= flatCoordinates[end - 1]) {
          return ol.geom.flat.lineStringCoordinateAtM(
              flatCoordinates, offset, end, stride, m, false);
        }
        offset = end;
      }
      goog.asserts.fail(
          'ol.geom.flat.lineStringsCoordinateAtM should have returned');
      return null;
    };