Merge pull request #11194 from mike-000/patch-15

ol/geom/LineString#getCoordinateAt() to return Z and M values if available
This commit is contained in:
Andreas Hocevar
2020-06-23 23:51:01 +02:00
committed by GitHub
3 changed files with 35 additions and 25 deletions
+2 -1
View File
@@ -211,7 +211,8 @@ class LineString extends SimpleGeometry {
this.flatCoordinates.length, this.flatCoordinates.length,
this.stride, this.stride,
fraction, fraction,
opt_dest opt_dest,
this.stride
); );
} }
+21 -24
View File
@@ -11,6 +11,7 @@ import {lerp} from '../../math.js';
* @param {number} stride Stride. * @param {number} stride Stride.
* @param {number} fraction Fraction. * @param {number} fraction Fraction.
* @param {Array<number>=} opt_dest Destination. * @param {Array<number>=} opt_dest Destination.
* @param {number=} opt_dimension Destination dimension (default is `2`)
* @return {Array<number>} Destination. * @return {Array<number>} Destination.
*/ */
export function interpolatePoint( export function interpolatePoint(
@@ -19,21 +20,16 @@ export function interpolatePoint(
end, end,
stride, stride,
fraction, fraction,
opt_dest opt_dest,
opt_dimension
) { ) {
let pointX = NaN; let o, t;
let pointY = NaN;
const n = (end - offset) / stride; const n = (end - offset) / stride;
if (n === 1) { if (n === 1) {
pointX = flatCoordinates[offset]; o = offset;
pointY = flatCoordinates[offset + 1]; } else if (n === 2) {
} else if (n == 2) { o = offset;
pointX = t = fraction;
(1 - fraction) * flatCoordinates[offset] +
fraction * flatCoordinates[offset + stride];
pointY =
(1 - fraction) * flatCoordinates[offset + 1] +
fraction * flatCoordinates[offset + stride + 1];
} else if (n !== 0) { } else if (n !== 0) {
let x1 = flatCoordinates[offset]; let x1 = flatCoordinates[offset];
let y1 = flatCoordinates[offset + 1]; let y1 = flatCoordinates[offset + 1];
@@ -50,24 +46,25 @@ export function interpolatePoint(
const target = fraction * length; const target = fraction * length;
const index = binarySearch(cumulativeLengths, target); const index = binarySearch(cumulativeLengths, target);
if (index < 0) { if (index < 0) {
const t = t =
(target - cumulativeLengths[-index - 2]) / (target - cumulativeLengths[-index - 2]) /
(cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]); (cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]);
const o = offset + (-index - 2) * stride; o = offset + (-index - 2) * stride;
pointX = lerp(flatCoordinates[o], flatCoordinates[o + stride], t);
pointY = lerp(flatCoordinates[o + 1], flatCoordinates[o + stride + 1], t);
} else { } else {
pointX = flatCoordinates[offset + index * stride]; o = offset + index * stride;
pointY = flatCoordinates[offset + index * stride + 1];
} }
} }
if (opt_dest) { const dimension = opt_dimension > 1 ? opt_dimension : 2;
opt_dest[0] = pointX; const dest = opt_dest ? opt_dest : new Array(dimension);
opt_dest[1] = pointY; for (let i = 0; i < dimension; ++i) {
return opt_dest; dest[i] =
} else { o === undefined
return [pointX, pointY]; ? NaN
: t === undefined
? flatCoordinates[o + i]
: lerp(flatCoordinates[o + i], flatCoordinates[o + stride + i], t);
} }
return dest;
} }
/** /**
+12
View File
@@ -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 () { describe('#getCoordinateAtM', function () {
it('returns the expected value', function () { it('returns the expected value', function () {
expect(lineString.getCoordinateAtM(2, false)).to.be(null); 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 () { describe('#getCoordinateAtM', function () {
it('returns the expected value', function () { it('returns the expected value', function () {
expect(lineString.getLayout()).to.be('XYZM'); expect(lineString.getLayout()).to.be('XYZM');