Factor out ol.geom.flat.closest
This commit is contained in:
@@ -1,11 +1,53 @@
|
||||
// FIXME find better names for these functions
|
||||
|
||||
goog.provide('ol.geom.closest');
|
||||
goog.provide('ol.geom.flat.closest');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.math');
|
||||
goog.require('ol.geom.flat');
|
||||
|
||||
|
||||
/**
|
||||
* Returns the point on the 2D line segment flatCoordinates[offset1] to
|
||||
* flatCoordinates[offset2] that is closest to the point (x, y). Extra
|
||||
* dimensions are linearly interpolated.
|
||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||
* @param {number} offset1 Offset 1.
|
||||
* @param {number} offset2 Offset 2.
|
||||
* @param {number} stride Stride.
|
||||
* @param {number} x X.
|
||||
* @param {number} y Y.
|
||||
* @param {Array.<number>} closestPoint Closest point.
|
||||
*/
|
||||
ol.geom.flat.closest.point =
|
||||
function(flatCoordinates, offset1, offset2, stride, x, y, closestPoint) {
|
||||
var x1 = flatCoordinates[offset1];
|
||||
var y1 = flatCoordinates[offset1 + 1];
|
||||
var dx = flatCoordinates[offset2] - x1;
|
||||
var dy = flatCoordinates[offset2 + 1] - y1;
|
||||
var i, offset;
|
||||
if (dx === 0 && dy === 0) {
|
||||
offset = offset1;
|
||||
} else {
|
||||
var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy);
|
||||
if (t > 1) {
|
||||
offset = offset2;
|
||||
} else if (t > 0) {
|
||||
for (i = 0; i < stride; ++i) {
|
||||
closestPoint[i] = goog.math.lerp(flatCoordinates[offset1 + i],
|
||||
flatCoordinates[offset2 + i], t);
|
||||
}
|
||||
closestPoint.length = stride;
|
||||
return;
|
||||
} else {
|
||||
offset = offset1;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < stride; ++i) {
|
||||
closestPoint[i] = flatCoordinates[offset + i];
|
||||
}
|
||||
closestPoint.length = stride;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Return the squared of the largest distance between any pair of consecutive
|
||||
* coordinates.
|
||||
@@ -16,7 +58,7 @@ goog.require('ol.geom.flat');
|
||||
* @param {number} maxSquaredDelta Max squared delta.
|
||||
* @return {number} Max squared delta.
|
||||
*/
|
||||
ol.geom.closest.getMaxSquaredDelta =
|
||||
ol.geom.flat.closest.getMaxSquaredDelta =
|
||||
function(flatCoordinates, offset, end, stride, maxSquaredDelta) {
|
||||
var x1 = flatCoordinates[offset];
|
||||
var y1 = flatCoordinates[offset + 1];
|
||||
@@ -42,12 +84,12 @@ ol.geom.closest.getMaxSquaredDelta =
|
||||
* @param {number} maxSquaredDelta Max squared delta.
|
||||
* @return {number} Max squared delta.
|
||||
*/
|
||||
ol.geom.closest.getsMaxSquaredDelta =
|
||||
ol.geom.flat.closest.getsMaxSquaredDelta =
|
||||
function(flatCoordinates, offset, ends, stride, maxSquaredDelta) {
|
||||
var i, ii;
|
||||
for (i = 0, ii = ends.length; i < ii; ++i) {
|
||||
var end = ends[i];
|
||||
maxSquaredDelta = ol.geom.closest.getMaxSquaredDelta(
|
||||
maxSquaredDelta = ol.geom.flat.closest.getMaxSquaredDelta(
|
||||
flatCoordinates, offset, end, stride, maxSquaredDelta);
|
||||
offset = end;
|
||||
}
|
||||
@@ -63,12 +105,12 @@ ol.geom.closest.getsMaxSquaredDelta =
|
||||
* @param {number} maxSquaredDelta Max squared delta.
|
||||
* @return {number} Max squared delta.
|
||||
*/
|
||||
ol.geom.closest.getssMaxSquaredDelta =
|
||||
ol.geom.flat.closest.getssMaxSquaredDelta =
|
||||
function(flatCoordinates, offset, endss, stride, maxSquaredDelta) {
|
||||
var i, ii;
|
||||
for (i = 0, ii = endss.length; i < ii; ++i) {
|
||||
var ends = endss[i];
|
||||
maxSquaredDelta = ol.geom.closest.getsMaxSquaredDelta(
|
||||
maxSquaredDelta = ol.geom.flat.closest.getsMaxSquaredDelta(
|
||||
flatCoordinates, offset, ends, stride, maxSquaredDelta);
|
||||
offset = ends[ends.length - 1];
|
||||
}
|
||||
@@ -90,8 +132,9 @@ ol.geom.closest.getssMaxSquaredDelta =
|
||||
* @param {Array.<number>=} opt_tmpPoint Temporary point object.
|
||||
* @return {number} Minimum squared distance.
|
||||
*/
|
||||
ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride,
|
||||
maxDelta, isRing, x, y, closestPoint, minSquaredDistance, opt_tmpPoint) {
|
||||
ol.geom.flat.closest.getClosestPoint = function(flatCoordinates, offset, end,
|
||||
stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance,
|
||||
opt_tmpPoint) {
|
||||
if (offset == end) {
|
||||
return minSquaredDistance;
|
||||
}
|
||||
@@ -114,7 +157,7 @@ ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride,
|
||||
var tmpPoint = goog.isDef(opt_tmpPoint) ? opt_tmpPoint : [NaN, NaN];
|
||||
var index = offset + stride;
|
||||
while (index < end) {
|
||||
ol.geom.flat.closestPoint(
|
||||
ol.geom.flat.closest.point(
|
||||
flatCoordinates, index - stride, index, stride, x, y, tmpPoint);
|
||||
squaredDistance = ol.geom.flat.squaredDistance(
|
||||
x, y, tmpPoint[0], tmpPoint[1]);
|
||||
@@ -143,7 +186,7 @@ ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride,
|
||||
}
|
||||
if (isRing) {
|
||||
// Check the closing segment.
|
||||
ol.geom.flat.closestPoint(
|
||||
ol.geom.flat.closest.point(
|
||||
flatCoordinates, end - stride, offset, stride, x, y, tmpPoint);
|
||||
squaredDistance = ol.geom.flat.squaredDistance(
|
||||
x, y, tmpPoint[0], tmpPoint[1]);
|
||||
@@ -173,14 +216,14 @@ ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride,
|
||||
* @param {Array.<number>=} opt_tmpPoint Temporary point object.
|
||||
* @return {number} Minimum squared distance.
|
||||
*/
|
||||
ol.geom.closest.getsClosestPoint = function(flatCoordinates, offset, ends,
|
||||
ol.geom.flat.closest.getsClosestPoint = function(flatCoordinates, offset, ends,
|
||||
stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance,
|
||||
opt_tmpPoint) {
|
||||
var tmpPoint = goog.isDef(opt_tmpPoint) ? opt_tmpPoint : [NaN, NaN];
|
||||
var i, ii;
|
||||
for (i = 0, ii = ends.length; i < ii; ++i) {
|
||||
var end = ends[i];
|
||||
minSquaredDistance = ol.geom.closest.getClosestPoint(
|
||||
minSquaredDistance = ol.geom.flat.closest.getClosestPoint(
|
||||
flatCoordinates, offset, end, stride,
|
||||
maxDelta, isRing, x, y, closestPoint, minSquaredDistance, tmpPoint);
|
||||
offset = end;
|
||||
@@ -203,14 +246,14 @@ ol.geom.closest.getsClosestPoint = function(flatCoordinates, offset, ends,
|
||||
* @param {Array.<number>=} opt_tmpPoint Temporary point object.
|
||||
* @return {number} Minimum squared distance.
|
||||
*/
|
||||
ol.geom.closest.getssClosestPoint = function(flatCoordinates, offset, endss,
|
||||
stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance,
|
||||
ol.geom.flat.closest.getssClosestPoint = function(flatCoordinates, offset,
|
||||
endss, stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance,
|
||||
opt_tmpPoint) {
|
||||
var tmpPoint = goog.isDef(opt_tmpPoint) ? opt_tmpPoint : [NaN, NaN];
|
||||
var i, ii;
|
||||
for (i = 0, ii = endss.length; i < ii; ++i) {
|
||||
var ends = endss[i];
|
||||
minSquaredDistance = ol.geom.closest.getsClosestPoint(
|
||||
minSquaredDistance = ol.geom.flat.closest.getsClosestPoint(
|
||||
flatCoordinates, offset, ends, stride,
|
||||
maxDelta, isRing, x, y, closestPoint, minSquaredDistance, tmpPoint);
|
||||
offset = ends[ends.length - 1];
|
||||
@@ -7,49 +7,6 @@ goog.require('goog.vec.Mat4');
|
||||
goog.require('ol.extent');
|
||||
|
||||
|
||||
/**
|
||||
* Returns the point on the 2D line segment flatCoordinates[offset1] to
|
||||
* flatCoordinates[offset2] that is closest to the point (x, y). Extra
|
||||
* dimensions are linearly interpolated.
|
||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||
* @param {number} offset1 Offset 1.
|
||||
* @param {number} offset2 Offset 2.
|
||||
* @param {number} stride Stride.
|
||||
* @param {number} x X.
|
||||
* @param {number} y Y.
|
||||
* @param {Array.<number>} closestPoint Closest point.
|
||||
*/
|
||||
ol.geom.flat.closestPoint =
|
||||
function(flatCoordinates, offset1, offset2, stride, x, y, closestPoint) {
|
||||
var x1 = flatCoordinates[offset1];
|
||||
var y1 = flatCoordinates[offset1 + 1];
|
||||
var dx = flatCoordinates[offset2] - x1;
|
||||
var dy = flatCoordinates[offset2 + 1] - y1;
|
||||
var i, offset;
|
||||
if (dx === 0 && dy === 0) {
|
||||
offset = offset1;
|
||||
} else {
|
||||
var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy);
|
||||
if (t > 1) {
|
||||
offset = offset2;
|
||||
} else if (t > 0) {
|
||||
for (i = 0; i < stride; ++i) {
|
||||
closestPoint[i] = goog.math.lerp(flatCoordinates[offset1 + i],
|
||||
flatCoordinates[offset2 + i], t);
|
||||
}
|
||||
closestPoint.length = stride;
|
||||
return;
|
||||
} else {
|
||||
offset = offset1;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < stride; ++i) {
|
||||
closestPoint[i] = flatCoordinates[offset + i];
|
||||
}
|
||||
closestPoint.length = stride;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Array.<number>} flatCoordinates Flat coordinates.
|
||||
* @param {number} offset Offset.
|
||||
|
||||
@@ -3,8 +3,8 @@ goog.provide('ol.geom.LinearRing');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.SimpleGeometry');
|
||||
goog.require('ol.geom.closest');
|
||||
goog.require('ol.geom.flat');
|
||||
goog.require('ol.geom.flat.closest');
|
||||
goog.require('ol.geom.simplify');
|
||||
|
||||
|
||||
@@ -58,11 +58,11 @@ ol.geom.LinearRing.prototype.closestPointXY =
|
||||
return minSquaredDistance;
|
||||
}
|
||||
if (this.maxDeltaRevision_ != this.getRevision()) {
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.closest.getMaxSquaredDelta(
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta(
|
||||
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, 0));
|
||||
this.maxDeltaRevision_ = this.getRevision();
|
||||
}
|
||||
return ol.geom.closest.getClosestPoint(
|
||||
return ol.geom.flat.closest.getClosestPoint(
|
||||
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride,
|
||||
this.maxDelta_, true, x, y, closestPoint, minSquaredDistance);
|
||||
};
|
||||
|
||||
@@ -5,8 +5,8 @@ goog.require('goog.asserts');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.SimpleGeometry');
|
||||
goog.require('ol.geom.closest');
|
||||
goog.require('ol.geom.flat');
|
||||
goog.require('ol.geom.flat.closest');
|
||||
goog.require('ol.geom.simplify');
|
||||
|
||||
|
||||
@@ -86,11 +86,11 @@ ol.geom.LineString.prototype.closestPointXY =
|
||||
return minSquaredDistance;
|
||||
}
|
||||
if (this.maxDeltaRevision_ != this.getRevision()) {
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.closest.getMaxSquaredDelta(
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta(
|
||||
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, 0));
|
||||
this.maxDeltaRevision_ = this.getRevision();
|
||||
}
|
||||
return ol.geom.closest.getClosestPoint(
|
||||
return ol.geom.flat.closest.getClosestPoint(
|
||||
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride,
|
||||
this.maxDelta_, false, x, y, closestPoint, minSquaredDistance);
|
||||
};
|
||||
|
||||
@@ -6,8 +6,8 @@ goog.require('ol.extent');
|
||||
goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.LineString');
|
||||
goog.require('ol.geom.SimpleGeometry');
|
||||
goog.require('ol.geom.closest');
|
||||
goog.require('ol.geom.flat');
|
||||
goog.require('ol.geom.flat.closest');
|
||||
goog.require('ol.geom.simplify');
|
||||
|
||||
|
||||
@@ -84,11 +84,11 @@ ol.geom.MultiLineString.prototype.closestPointXY =
|
||||
return minSquaredDistance;
|
||||
}
|
||||
if (this.maxDeltaRevision_ != this.getRevision()) {
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.closest.getsMaxSquaredDelta(
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getsMaxSquaredDelta(
|
||||
this.flatCoordinates, 0, this.ends_, this.stride, 0));
|
||||
this.maxDeltaRevision_ = this.getRevision();
|
||||
}
|
||||
return ol.geom.closest.getsClosestPoint(
|
||||
return ol.geom.flat.closest.getsClosestPoint(
|
||||
this.flatCoordinates, 0, this.ends_, this.stride,
|
||||
this.maxDelta_, false, x, y, closestPoint, minSquaredDistance);
|
||||
};
|
||||
|
||||
@@ -7,8 +7,8 @@ goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.MultiPoint');
|
||||
goog.require('ol.geom.Polygon');
|
||||
goog.require('ol.geom.SimpleGeometry');
|
||||
goog.require('ol.geom.closest');
|
||||
goog.require('ol.geom.flat');
|
||||
goog.require('ol.geom.flat.closest');
|
||||
goog.require('ol.geom.simplify');
|
||||
|
||||
|
||||
@@ -118,11 +118,11 @@ ol.geom.MultiPolygon.prototype.closestPointXY =
|
||||
return minSquaredDistance;
|
||||
}
|
||||
if (this.maxDeltaRevision_ != this.getRevision()) {
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.closest.getssMaxSquaredDelta(
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getssMaxSquaredDelta(
|
||||
this.flatCoordinates, 0, this.endss_, this.stride, 0));
|
||||
this.maxDeltaRevision_ = this.getRevision();
|
||||
}
|
||||
return ol.geom.closest.getssClosestPoint(
|
||||
return ol.geom.flat.closest.getssClosestPoint(
|
||||
this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride,
|
||||
this.maxDelta_, true, x, y, closestPoint, minSquaredDistance);
|
||||
};
|
||||
|
||||
@@ -7,8 +7,8 @@ goog.require('ol.geom.GeometryType');
|
||||
goog.require('ol.geom.LinearRing');
|
||||
goog.require('ol.geom.Point');
|
||||
goog.require('ol.geom.SimpleGeometry');
|
||||
goog.require('ol.geom.closest');
|
||||
goog.require('ol.geom.flat');
|
||||
goog.require('ol.geom.flat.closest');
|
||||
goog.require('ol.geom.simplify');
|
||||
|
||||
|
||||
@@ -108,11 +108,11 @@ ol.geom.Polygon.prototype.closestPointXY =
|
||||
return minSquaredDistance;
|
||||
}
|
||||
if (this.maxDeltaRevision_ != this.getRevision()) {
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.closest.getsMaxSquaredDelta(
|
||||
this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getsMaxSquaredDelta(
|
||||
this.flatCoordinates, 0, this.ends_, this.stride, 0));
|
||||
this.maxDeltaRevision_ = this.getRevision();
|
||||
}
|
||||
return ol.geom.closest.getsClosestPoint(
|
||||
return ol.geom.flat.closest.getsClosestPoint(
|
||||
this.flatCoordinates, 0, this.ends_, this.stride,
|
||||
this.maxDelta_, true, x, y, closestPoint, minSquaredDistance);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user