From f5d85ab4f9007d3d48cbd7bb079f3112682d31a2 Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Wed, 17 Jun 2020 15:24:41 +0100 Subject: [PATCH 1/3] interpolatePoint optionally returns Z and M values --- src/ol/geom/flat/interpolate.js | 45 +++++++++++++++------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/ol/geom/flat/interpolate.js b/src/ol/geom/flat/interpolate.js index 37194194e1..72f819f78a 100644 --- a/src/ol/geom/flat/interpolate.js +++ b/src/ol/geom/flat/interpolate.js @@ -11,6 +11,7 @@ import {lerp} from '../../math.js'; * @param {number} stride Stride. * @param {number} fraction Fraction. * @param {Array=} opt_dest Destination. + * @param {number=} opt_dimension Destination dimension (default is `2`) * @return {Array} Destination. */ export function interpolatePoint( @@ -19,21 +20,16 @@ export function interpolatePoint( end, stride, fraction, - opt_dest + opt_dest, + opt_dimension ) { - let pointX = NaN; - let pointY = NaN; + let o, t; 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]; + o = offset; + } else if (n === 2) { + o = offset; + t = fraction; } else if (n !== 0) { let x1 = flatCoordinates[offset]; let y1 = flatCoordinates[offset + 1]; @@ -50,24 +46,25 @@ export function interpolatePoint( const target = fraction * length; const index = binarySearch(cumulativeLengths, target); if (index < 0) { - const t = + 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); + o = offset + (-index - 2) * stride; } else { - pointX = flatCoordinates[offset + index * stride]; - pointY = flatCoordinates[offset + index * stride + 1]; + o = offset + index * stride; } } - if (opt_dest) { - opt_dest[0] = pointX; - opt_dest[1] = pointY; - return opt_dest; - } else { - return [pointX, pointY]; + const dimension = opt_dimension > 1 ? opt_dimension : 2; + const dest = opt_dest ? opt_dest : new Array(dimension); + for (let i = 0; i < dimension; ++i) { + dest[i] = + o === undefined + ? NaN + : t === undefined + ? flatCoordinates[o + i] + : lerp(flatCoordinates[o + i], flatCoordinates[o + stride + i], t); } + return dest; } /** From 19f3c6b94ae94d14589e97a23a430bf4675e09cc Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Wed, 17 Jun 2020 15:46:28 +0100 Subject: [PATCH 2/3] getCoordinateAt return Z and M values if available --- src/ol/geom/LineString.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ol/geom/LineString.js b/src/ol/geom/LineString.js index 51c6e35b21..c791e0ebdd 100644 --- a/src/ol/geom/LineString.js +++ b/src/ol/geom/LineString.js @@ -211,7 +211,8 @@ class LineString extends SimpleGeometry { this.flatCoordinates.length, this.stride, fraction, - opt_dest + opt_dest, + this.stride ); } From 136936979f60a37857fb396323e783e0b3deef3d Mon Sep 17 00:00:00 2001 From: mike-000 <49240900+mike-000@users.noreply.github.com> Date: Wed, 17 Jun 2020 15:56:36 +0100 Subject: [PATCH 3/3] test getCoordinateAt returns Z and M values --- test/spec/ol/geom/linestring.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/spec/ol/geom/linestring.test.js b/test/spec/ol/geom/linestring.test.js index e82ec01b84..13e572cf2c 100644 --- a/test/spec/ol/geom/linestring.test.js +++ b/test/spec/ol/geom/linestring.test.js @@ -427,6 +427,12 @@ describe('ol.geom.LineString', function () { ); }); + describe('#getCoordinateAt', function () { + it('returns the expected value', function () { + expect(lineString.getCoordinateAt(0.5)).to.eql([2.5, 3.5, 4.5]); + }); + }); + describe('#getCoordinateAtM', function () { it('returns the expected value', function () { expect(lineString.getCoordinateAtM(2, false)).to.be(null); @@ -463,6 +469,12 @@ describe('ol.geom.LineString', function () { ]); }); + describe('#getCoordinateAt', function () { + it('returns the expected value', function () { + expect(lineString.getCoordinateAt(0.5)).to.eql([11, -11, 22, 11]); + }); + }); + describe('#getCoordinateAtM', function () { it('returns the expected value', function () { expect(lineString.getLayout()).to.be('XYZM');