Merge pull request #1788 from twpayne/interpolate-m
getCoordinateAtM for MultiLineStrings
This commit is contained in:
@@ -310,16 +310,13 @@ ol.geom.flat.lineStringInterpolate =
|
||||
* @param {number} offset Offset.
|
||||
* @param {number} end End.
|
||||
* @param {number} stride Stride.
|
||||
* @param {ol.geom.GeometryLayout} layout Layout.
|
||||
* @param {number} m M.
|
||||
* @param {boolean} extrapolate Extrapolate.
|
||||
* @return {ol.Coordinate} Coordinate.
|
||||
*/
|
||||
ol.geom.flat.lineStringCoordinateAtM =
|
||||
function(flatCoordinates, offset, end, stride, layout, m, extrapolate) {
|
||||
if ((layout != ol.geom.GeometryLayout.XYM &&
|
||||
layout != ol.geom.GeometryLayout.XYZM) ||
|
||||
end == offset) {
|
||||
function(flatCoordinates, offset, end, stride, m, extrapolate) {
|
||||
if (end == offset) {
|
||||
return null;
|
||||
}
|
||||
var coordinate;
|
||||
@@ -374,6 +371,60 @@ ol.geom.flat.lineStringCoordinateAtM =
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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];
|
||||
if (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();
|
||||
return null;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||
* @param {number} offset Offset.
|
||||
|
||||
@@ -81,14 +81,26 @@ ol.geom.LineString.prototype.closestPointXY =
|
||||
|
||||
|
||||
/**
|
||||
* Returns the coordinate at `m` using linear interpolation, or `null` if no
|
||||
* such coordinate exists.
|
||||
*
|
||||
* `opt_extrapolate` controls extrapolation beyond the range of Ms in the
|
||||
* MultiLineString. If `opt_extrapolate` is `true` then Ms less than the first
|
||||
* M will return the first coordinate and Ms greater than the last M will
|
||||
* return the last coordinate.
|
||||
*
|
||||
* @param {number} m M.
|
||||
* @param {boolean=} opt_extrapolate Extrapolate.
|
||||
* @return {ol.Coordinate} Coordinate.
|
||||
*/
|
||||
ol.geom.LineString.prototype.getCoordinateAtM = function(m, opt_extrapolate) {
|
||||
if (this.layout != ol.geom.GeometryLayout.XYM &&
|
||||
this.layout != ol.geom.GeometryLayout.XYZM) {
|
||||
return null;
|
||||
}
|
||||
var extrapolate = goog.isDef(opt_extrapolate) ? opt_extrapolate : false;
|
||||
return ol.geom.flat.lineStringCoordinateAtM(this.flatCoordinates, 0,
|
||||
this.flatCoordinates.length, this.stride, this.layout, m, extrapolate);
|
||||
this.flatCoordinates.length, this.stride, m, extrapolate);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
@exportSymbol ol.geom.MultiLineString
|
||||
@exportProperty ol.geom.MultiLineString.prototype.clone
|
||||
@exportProperty ol.geom.MultiLineString.prototype.getCoordinateAtM
|
||||
@exportProperty ol.geom.MultiLineString.prototype.getCoordinates
|
||||
@exportProperty ol.geom.MultiLineString.prototype.getLineStrings
|
||||
@exportProperty ol.geom.MultiLineString.prototype.getType
|
||||
|
||||
@@ -78,6 +78,41 @@ ol.geom.MultiLineString.prototype.closestPointXY =
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the coordinate at `m` using linear interpolation, or `null` if no
|
||||
* such coordinate exists.
|
||||
*
|
||||
* `opt_extrapolate` controls extrapolation beyond the range of Ms in the
|
||||
* MultiLineString. If `opt_extrapolate` is `true` then Ms less than the first
|
||||
* M will return the first coordinate and Ms greater than the last M will
|
||||
* return the last coordinate.
|
||||
*
|
||||
* `opt_interpolate` controls interpolation between consecutive LineStrings
|
||||
* within the MultiLineString. If `opt_interpolate` is `true` the coordinates
|
||||
* will be linearly interpolated between the last coordinate of one LineString
|
||||
* and the first coordinate of the next LineString. If `opt_interpolate` is
|
||||
* `false` then the function will return `null` for Ms falling between
|
||||
* LineStrings.
|
||||
*
|
||||
* @param {number} m M.
|
||||
* @param {boolean=} opt_extrapolate Extrapolate.
|
||||
* @param {boolean=} opt_interpolate Interpolate.
|
||||
* @return {ol.Coordinate} Coordinate.
|
||||
*/
|
||||
ol.geom.MultiLineString.prototype.getCoordinateAtM =
|
||||
function(m, opt_extrapolate, opt_interpolate) {
|
||||
if ((this.layout != ol.geom.GeometryLayout.XYM &&
|
||||
this.layout != ol.geom.GeometryLayout.XYZM) ||
|
||||
this.flatCoordinates.length === 0) {
|
||||
return null;
|
||||
}
|
||||
var extrapolate = goog.isDef(opt_extrapolate) ? opt_extrapolate : false;
|
||||
var interpolate = goog.isDef(opt_interpolate) ? opt_interpolate : false;
|
||||
return ol.geom.flat.lineStringsCoordinateAtM(this.flatCoordinates, 0,
|
||||
this.ends_, this.stride, m, extrapolate, interpolate);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.geom.RawMultiLineString} Coordinates.
|
||||
* @todo stability experimental
|
||||
|
||||
@@ -142,6 +142,110 @@ describe('ol.geom.MultiLineString', function() {
|
||||
expect(multiLineString.getStride()).to.be(3);
|
||||
});
|
||||
|
||||
describe('#getCoordinateAtM', function() {
|
||||
|
||||
describe('with extrapolation and interpolation', function() {
|
||||
|
||||
it('returns the expected value', function() {
|
||||
expect(multiLineString.getCoordinateAtM(0, true, true)).to.eql(
|
||||
[1, 2, 0]);
|
||||
expect(multiLineString.getCoordinateAtM(3, true, true)).to.eql(
|
||||
[1, 2, 3]);
|
||||
expect(multiLineString.getCoordinateAtM(4.5, true, true)).to.eql(
|
||||
[2.5, 3.5, 4.5]);
|
||||
expect(multiLineString.getCoordinateAtM(6, true, true)).to.eql(
|
||||
[4, 5, 6]);
|
||||
expect(multiLineString.getCoordinateAtM(7.5, true, true)).to.eql(
|
||||
[5.5, 6.5, 7.5]);
|
||||
expect(multiLineString.getCoordinateAtM(9, true, true)).to.eql(
|
||||
[7, 8, 9]);
|
||||
expect(multiLineString.getCoordinateAtM(10.5, true, true)).to.eql(
|
||||
[8.5, 9.5, 10.5]);
|
||||
expect(multiLineString.getCoordinateAtM(12, true, true)).to.eql(
|
||||
[10, 11, 12]);
|
||||
expect(multiLineString.getCoordinateAtM(15, true, true)).to.eql(
|
||||
[10, 11, 15]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('with extrapolation and no interpolation', function() {
|
||||
|
||||
it('returns the expected value', function() {
|
||||
expect(multiLineString.getCoordinateAtM(0, true, false)).to.eql(
|
||||
[1, 2, 0]);
|
||||
expect(multiLineString.getCoordinateAtM(3, true, false)).to.eql(
|
||||
[1, 2, 3]);
|
||||
expect(multiLineString.getCoordinateAtM(4.5, true, false)).to.eql(
|
||||
[2.5, 3.5, 4.5]);
|
||||
expect(multiLineString.getCoordinateAtM(6, true, false)).to.eql(
|
||||
[4, 5, 6]);
|
||||
expect(multiLineString.getCoordinateAtM(7.5, true, false)).to.be(
|
||||
null);
|
||||
expect(multiLineString.getCoordinateAtM(9, true, false)).to.eql(
|
||||
[7, 8, 9]);
|
||||
expect(multiLineString.getCoordinateAtM(10.5, true, false)).to.eql(
|
||||
[8.5, 9.5, 10.5]);
|
||||
expect(multiLineString.getCoordinateAtM(12, true, false)).to.eql(
|
||||
[10, 11, 12]);
|
||||
expect(multiLineString.getCoordinateAtM(15, true, false)).to.eql(
|
||||
[10, 11, 15]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('with no extrapolation and interpolation', function() {
|
||||
|
||||
it('returns the expected value', function() {
|
||||
expect(multiLineString.getCoordinateAtM(0, false, true)).to.eql(
|
||||
null);
|
||||
expect(multiLineString.getCoordinateAtM(3, false, true)).to.eql(
|
||||
[1, 2, 3]);
|
||||
expect(multiLineString.getCoordinateAtM(4.5, false, true)).to.eql(
|
||||
[2.5, 3.5, 4.5]);
|
||||
expect(multiLineString.getCoordinateAtM(6, false, true)).to.eql(
|
||||
[4, 5, 6]);
|
||||
expect(multiLineString.getCoordinateAtM(7.5, false, true)).to.eql(
|
||||
[5.5, 6.5, 7.5]);
|
||||
expect(multiLineString.getCoordinateAtM(9, false, true)).to.eql(
|
||||
[7, 8, 9]);
|
||||
expect(multiLineString.getCoordinateAtM(10.5, false, true)).to.eql(
|
||||
[8.5, 9.5, 10.5]);
|
||||
expect(multiLineString.getCoordinateAtM(12, false, true)).to.eql(
|
||||
[10, 11, 12]);
|
||||
expect(multiLineString.getCoordinateAtM(15, false, true)).to.eql(
|
||||
null);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('with no extrapolation or interpolation', function() {
|
||||
|
||||
it('returns the expected value', function() {
|
||||
expect(multiLineString.getCoordinateAtM(0, false, false)).to.eql(
|
||||
null);
|
||||
expect(multiLineString.getCoordinateAtM(3, false, false)).to.eql(
|
||||
[1, 2, 3]);
|
||||
expect(multiLineString.getCoordinateAtM(4.5, false, false)).to.eql(
|
||||
[2.5, 3.5, 4.5]);
|
||||
expect(multiLineString.getCoordinateAtM(6, false, false)).to.eql(
|
||||
[4, 5, 6]);
|
||||
expect(multiLineString.getCoordinateAtM(7.5, false, false)).to.eql(
|
||||
null);
|
||||
expect(multiLineString.getCoordinateAtM(9, false, false)).to.eql(
|
||||
[7, 8, 9]);
|
||||
expect(multiLineString.getCoordinateAtM(10.5, false, false)).to.eql(
|
||||
[8.5, 9.5, 10.5]);
|
||||
expect(multiLineString.getCoordinateAtM(12, false, false)).to.eql(
|
||||
[10, 11, 12]);
|
||||
expect(multiLineString.getCoordinateAtM(15, false, false)).to.eql(
|
||||
null);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('construct with 4D coordinates', function() {
|
||||
|
||||
Reference in New Issue
Block a user