Download code/test/JavaScript/0024170_interpolate.js from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 5.34 kB
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/code/test/JavaScript/0024170_interpolate.js
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/code/test/JavaScript/0024170_interpolate.js
-
curl -L -o 0024170_interpolate.js https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/code/test/JavaScript/0024170_interpolate.js
5.34 kB
| /** | |
| * @module ol/geom/flat/interpolate | |
| */ | |
| import {binarySearch} from '../../array.js'; | |
| import {lerp} from '../../math.js'; | |
| /** | |
| * @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. | |
| */ | |
| export function interpolatePoint(flatCoordinates, offset, end, stride, fraction, opt_dest) { | |
| let pointX = NaN; | |
| let pointY = NaN; | |
| const n = (end - offset) / stride; | |
| 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 if (n !== 0) { | |
| let x1 = flatCoordinates[offset]; | |
| let y1 = flatCoordinates[offset + 1]; | |
| let length = 0; | |
| const cumulativeLengths = [0]; | |
| for (let i = offset + stride; i < end; i += stride) { | |
| const x2 = flatCoordinates[i]; | |
| const y2 = flatCoordinates[i + 1]; | |
| length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); | |
| cumulativeLengths.push(length); | |
| x1 = x2; | |
| y1 = y2; | |
| } | |
| const target = fraction * length; | |
| const index = binarySearch(cumulativeLengths, target); | |
| if (index < 0) { | |
| const t = (target - cumulativeLengths[-index - 2]) / | |
| (cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]); | |
| const o = offset + (-index - 2) * stride; | |
| pointX = lerp( | |
| flatCoordinates[o], flatCoordinates[o + stride], t); | |
| pointY = 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 {module:ol/coordinate~Coordinate} Coordinate. | |
| */ | |
| export function lineStringCoordinateAtM(flatCoordinates, offset, end, stride, m, extrapolate) { | |
| if (end == offset) { | |
| return null; | |
| } | |
| let 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); | |
| } | |
| let lo = offset / stride; | |
| let hi = end / stride; | |
| while (lo < hi) { | |
| const mid = (lo + hi) >> 1; | |
| if (m < flatCoordinates[(mid + 1) * stride - 1]) { | |
| hi = mid; | |
| } else { | |
| lo = mid + 1; | |
| } | |
| } | |
| const m0 = flatCoordinates[lo * stride - 1]; | |
| if (m == m0) { | |
| return flatCoordinates.slice((lo - 1) * stride, (lo - 1) * stride + stride); | |
| } | |
| const m1 = flatCoordinates[(lo + 1) * stride - 1]; | |
| const t = (m - m0) / (m1 - m0); | |
| coordinate = []; | |
| for (let i = 0; i < stride - 1; ++i) { | |
| coordinate.push(lerp(flatCoordinates[(lo - 1) * stride + i], | |
| flatCoordinates[lo * stride + i], t)); | |
| } | |
| coordinate.push(m); | |
| 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 {module:ol/coordinate~Coordinate} Coordinate. | |
| */ | |
| export function lineStringsCoordinateAtM( | |
| flatCoordinates, offset, ends, stride, m, extrapolate, interpolate) { | |
| if (interpolate) { | |
| return lineStringCoordinateAtM( | |
| flatCoordinates, offset, ends[ends.length - 1], stride, m, extrapolate); | |
| } | |
| let 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; | |
| } | |
| } | |
| for (let i = 0, ii = ends.length; i < ii; ++i) { | |
| const end = ends[i]; | |
| if (offset == end) { | |
| continue; | |
| } | |
| if (m < flatCoordinates[offset + stride - 1]) { | |
| return null; | |
| } else if (m <= flatCoordinates[end - 1]) { | |
| return lineStringCoordinateAtM( | |
| flatCoordinates, offset, end, stride, m, false); | |
| } | |
| offset = end; | |
| } | |
| return null; | |
| } | |