ol/geom/flat/closest export

This commit is contained in:
raiyni
2018-02-13 08:44:09 -06:00
parent 4575569238
commit 9fc09db89c
7 changed files with 59 additions and 61 deletions

View File

@@ -7,7 +7,7 @@ import {closestSquaredDistanceXY} from '../extent.js';
import GeometryLayout from '../geom/GeometryLayout.js';
import GeometryType from '../geom/GeometryType.js';
import SimpleGeometry from '../geom/SimpleGeometry.js';
import _ol_geom_flat_closest_ from '../geom/flat/closest.js';
import {assignClosestPoint, maxSquaredDelta} from '../geom/flat/closest.js';
import {deflateCoordinates} from '../geom/flat/deflate.js';
import {inflateCoordinates} from '../geom/flat/inflate.js';
import _ol_geom_flat_interpolate_ from '../geom/flat/interpolate.js';
@@ -97,11 +97,11 @@ LineString.prototype.closestPointXY = function(x, y, closestPoint, minSquaredDis
return minSquaredDistance;
}
if (this.maxDeltaRevision_ != this.getRevision()) {
this.maxDelta_ = Math.sqrt(_ol_geom_flat_closest_.getMaxSquaredDelta(
this.maxDelta_ = Math.sqrt(maxSquaredDelta(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, 0));
this.maxDeltaRevision_ = this.getRevision();
}
return _ol_geom_flat_closest_.getClosestPoint(
return assignClosestPoint(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride,
this.maxDelta_, false, x, y, closestPoint, minSquaredDistance);
};

View File

@@ -7,7 +7,7 @@ import GeometryLayout from '../geom/GeometryLayout.js';
import GeometryType from '../geom/GeometryType.js';
import SimpleGeometry from '../geom/SimpleGeometry.js';
import {linearRing as linearRingArea} from '../geom/flat/area.js';
import _ol_geom_flat_closest_ from '../geom/flat/closest.js';
import {assignClosestPoint, maxSquaredDelta} from '../geom/flat/closest.js';
import {deflateCoordinates} from '../geom/flat/deflate.js';
import {inflateCoordinates} from '../geom/flat/inflate.js';
import {douglasPeucker} from '../geom/flat/simplify.js';
@@ -67,11 +67,11 @@ LinearRing.prototype.closestPointXY = function(x, y, closestPoint, minSquaredDis
return minSquaredDistance;
}
if (this.maxDeltaRevision_ != this.getRevision()) {
this.maxDelta_ = Math.sqrt(_ol_geom_flat_closest_.getMaxSquaredDelta(
this.maxDelta_ = Math.sqrt(maxSquaredDelta(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, 0));
this.maxDeltaRevision_ = this.getRevision();
}
return _ol_geom_flat_closest_.getClosestPoint(
return assignClosestPoint(
this.flatCoordinates, 0, this.flatCoordinates.length, this.stride,
this.maxDelta_, true, x, y, closestPoint, minSquaredDistance);
};

View File

@@ -8,7 +8,7 @@ import GeometryLayout from '../geom/GeometryLayout.js';
import GeometryType from '../geom/GeometryType.js';
import LineString from '../geom/LineString.js';
import SimpleGeometry from '../geom/SimpleGeometry.js';
import _ol_geom_flat_closest_ from '../geom/flat/closest.js';
import {assignClosestArrayPoint, arrayMaxSquaredDelta} from '../geom/flat/closest.js';
import {deflateCoordinatesArray} from '../geom/flat/deflate.js';
import {inflateCoordinatesArray} from '../geom/flat/inflate.js';
import _ol_geom_flat_interpolate_ from '../geom/flat/interpolate.js';
@@ -92,11 +92,11 @@ MultiLineString.prototype.closestPointXY = function(x, y, closestPoint, minSquar
return minSquaredDistance;
}
if (this.maxDeltaRevision_ != this.getRevision()) {
this.maxDelta_ = Math.sqrt(_ol_geom_flat_closest_.getsMaxSquaredDelta(
this.maxDelta_ = Math.sqrt(arrayMaxSquaredDelta(
this.flatCoordinates, 0, this.ends_, this.stride, 0));
this.maxDeltaRevision_ = this.getRevision();
}
return _ol_geom_flat_closest_.getsClosestPoint(
return assignClosestArrayPoint(
this.flatCoordinates, 0, this.ends_, this.stride,
this.maxDelta_, false, x, y, closestPoint, minSquaredDistance);
};

View File

@@ -11,7 +11,7 @@ import Polygon from '../geom/Polygon.js';
import SimpleGeometry from '../geom/SimpleGeometry.js';
import {linearRingss as linearRingssArea} from '../geom/flat/area.js';
import {linearRingss as linearRingssCenter} from '../geom/flat/center.js';
import _ol_geom_flat_closest_ from '../geom/flat/closest.js';
import {assignClosestMultiArrayPoint, multiArrayMaxSquaredDelta} from '../geom/flat/closest.js';
import {linearRingssContainsXY} from '../geom/flat/contains.js';
import {deflateMultiCoordinatesArray} from '../geom/flat/deflate.js';
import {inflateMultiCoordinatesArray} from '../geom/flat/inflate.js';
@@ -137,11 +137,11 @@ MultiPolygon.prototype.closestPointXY = function(x, y, closestPoint, minSquaredD
return minSquaredDistance;
}
if (this.maxDeltaRevision_ != this.getRevision()) {
this.maxDelta_ = Math.sqrt(_ol_geom_flat_closest_.getssMaxSquaredDelta(
this.maxDelta_ = Math.sqrt(multiArrayMaxSquaredDelta(
this.flatCoordinates, 0, this.endss_, this.stride, 0));
this.maxDeltaRevision_ = this.getRevision();
}
return _ol_geom_flat_closest_.getssClosestPoint(
return assignClosestMultiArrayPoint(
this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride,
this.maxDelta_, true, x, y, closestPoint, minSquaredDistance);
};

View File

@@ -11,7 +11,7 @@ import Point from '../geom/Point.js';
import SimpleGeometry from '../geom/SimpleGeometry.js';
import {offset as sphereOffset} from '../sphere.js';
import {linearRings as linearRingsArea} from '../geom/flat/area.js';
import _ol_geom_flat_closest_ from '../geom/flat/closest.js';
import {assignClosestArrayPoint, arrayMaxSquaredDelta} from '../geom/flat/closest.js';
import {linearRingsContainsXY} from '../geom/flat/contains.js';
import {deflateCoordinatesArray} from '../geom/flat/deflate.js';
import {inflateCoordinatesArray} from '../geom/flat/inflate.js';
@@ -127,11 +127,11 @@ Polygon.prototype.closestPointXY = function(x, y, closestPoint, minSquaredDistan
return minSquaredDistance;
}
if (this.maxDeltaRevision_ != this.getRevision()) {
this.maxDelta_ = Math.sqrt(_ol_geom_flat_closest_.getsMaxSquaredDelta(
this.maxDelta_ = Math.sqrt(arrayMaxSquaredDelta(
this.flatCoordinates, 0, this.ends_, this.stride, 0));
this.maxDeltaRevision_ = this.getRevision();
}
return _ol_geom_flat_closest_.getsClosestPoint(
return assignClosestArrayPoint(
this.flatCoordinates, 0, this.ends_, this.stride,
this.maxDelta_, true, x, y, closestPoint, minSquaredDistance);
};

View File

@@ -2,7 +2,6 @@
* @module ol/geom/flat/closest
*/
import {lerp, squaredDistance as squaredDx} from '../../math.js';
const _ol_geom_flat_closest_ = {};
/**
@@ -17,7 +16,7 @@ const _ol_geom_flat_closest_ = {};
* @param {number} y Y.
* @param {Array.<number>} closestPoint Closest point.
*/
_ol_geom_flat_closest_.point = function(flatCoordinates, offset1, offset2, stride, x, y, closestPoint) {
function assignClosest(flatCoordinates, offset1, offset2, stride, x, y, closestPoint) {
const x1 = flatCoordinates[offset1];
const y1 = flatCoordinates[offset1 + 1];
const dx = flatCoordinates[offset2] - x1;
@@ -44,7 +43,7 @@ _ol_geom_flat_closest_.point = function(flatCoordinates, offset1, offset2, strid
closestPoint[i] = flatCoordinates[offset + i];
}
closestPoint.length = stride;
};
}
/**
@@ -54,24 +53,24 @@ _ol_geom_flat_closest_.point = function(flatCoordinates, offset1, offset2, strid
* @param {number} offset Offset.
* @param {number} end End.
* @param {number} stride Stride.
* @param {number} maxSquaredDelta Max squared delta.
* @param {number} max Max squared delta.
* @return {number} Max squared delta.
*/
_ol_geom_flat_closest_.getMaxSquaredDelta = function(flatCoordinates, offset, end, stride, maxSquaredDelta) {
export function maxSquaredDelta(flatCoordinates, offset, end, stride, max) {
let x1 = flatCoordinates[offset];
let y1 = flatCoordinates[offset + 1];
for (offset += stride; offset < end; offset += stride) {
const x2 = flatCoordinates[offset];
const y2 = flatCoordinates[offset + 1];
const squaredDelta = squaredDx(x1, y1, x2, y2);
if (squaredDelta > maxSquaredDelta) {
maxSquaredDelta = squaredDelta;
if (squaredDelta > max) {
max = squaredDelta;
}
x1 = x2;
y1 = y2;
}
return maxSquaredDelta;
};
return max;
}
/**
@@ -79,18 +78,18 @@ _ol_geom_flat_closest_.getMaxSquaredDelta = function(flatCoordinates, offset, en
* @param {number} offset Offset.
* @param {Array.<number>} ends Ends.
* @param {number} stride Stride.
* @param {number} maxSquaredDelta Max squared delta.
* @param {number} max Max squared delta.
* @return {number} Max squared delta.
*/
_ol_geom_flat_closest_.getsMaxSquaredDelta = function(flatCoordinates, offset, ends, stride, maxSquaredDelta) {
export function arrayMaxSquaredDelta(flatCoordinates, offset, ends, stride, max) {
for (let i = 0, ii = ends.length; i < ii; ++i) {
const end = ends[i];
maxSquaredDelta = _ol_geom_flat_closest_.getMaxSquaredDelta(
flatCoordinates, offset, end, stride, maxSquaredDelta);
max = maxSquaredDelta(
flatCoordinates, offset, end, stride, max);
offset = end;
}
return maxSquaredDelta;
};
return max;
}
/**
@@ -98,18 +97,18 @@ _ol_geom_flat_closest_.getsMaxSquaredDelta = function(flatCoordinates, offset, e
* @param {number} offset Offset.
* @param {Array.<Array.<number>>} endss Endss.
* @param {number} stride Stride.
* @param {number} maxSquaredDelta Max squared delta.
* @param {number} max Max squared delta.
* @return {number} Max squared delta.
*/
_ol_geom_flat_closest_.getssMaxSquaredDelta = function(flatCoordinates, offset, endss, stride, maxSquaredDelta) {
export function multiArrayMaxSquaredDelta(flatCoordinates, offset, endss, stride, max) {
for (let i = 0, ii = endss.length; i < ii; ++i) {
const ends = endss[i];
maxSquaredDelta = _ol_geom_flat_closest_.getsMaxSquaredDelta(
flatCoordinates, offset, ends, stride, maxSquaredDelta);
max = arrayMaxSquaredDelta(
flatCoordinates, offset, ends, stride, max);
offset = ends[ends.length - 1];
}
return maxSquaredDelta;
};
return max;
}
/**
@@ -126,7 +125,7 @@ _ol_geom_flat_closest_.getssMaxSquaredDelta = function(flatCoordinates, offset,
* @param {Array.<number>=} opt_tmpPoint Temporary point object.
* @return {number} Minimum squared distance.
*/
_ol_geom_flat_closest_.getClosestPoint = function(flatCoordinates, offset, end,
export function assignClosestPoint(flatCoordinates, offset, end,
stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance,
opt_tmpPoint) {
if (offset == end) {
@@ -150,7 +149,7 @@ _ol_geom_flat_closest_.getClosestPoint = function(flatCoordinates, offset, end,
const tmpPoint = opt_tmpPoint ? opt_tmpPoint : [NaN, NaN];
let index = offset + stride;
while (index < end) {
_ol_geom_flat_closest_.point(
assignClosest(
flatCoordinates, index - stride, index, stride, x, y, tmpPoint);
squaredDistance = squaredDx(x, y, tmpPoint[0], tmpPoint[1]);
if (squaredDistance < minSquaredDistance) {
@@ -178,7 +177,7 @@ _ol_geom_flat_closest_.getClosestPoint = function(flatCoordinates, offset, end,
}
if (isRing) {
// Check the closing segment.
_ol_geom_flat_closest_.point(
assignClosest(
flatCoordinates, end - stride, offset, stride, x, y, tmpPoint);
squaredDistance = squaredDx(x, y, tmpPoint[0], tmpPoint[1]);
if (squaredDistance < minSquaredDistance) {
@@ -190,7 +189,7 @@ _ol_geom_flat_closest_.getClosestPoint = function(flatCoordinates, offset, end,
}
}
return minSquaredDistance;
};
}
/**
@@ -207,19 +206,19 @@ _ol_geom_flat_closest_.getClosestPoint = function(flatCoordinates, offset, end,
* @param {Array.<number>=} opt_tmpPoint Temporary point object.
* @return {number} Minimum squared distance.
*/
_ol_geom_flat_closest_.getsClosestPoint = function(flatCoordinates, offset, ends,
export function assignClosestArrayPoint(flatCoordinates, offset, ends,
stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance,
opt_tmpPoint) {
const tmpPoint = opt_tmpPoint ? opt_tmpPoint : [NaN, NaN];
for (let i = 0, ii = ends.length; i < ii; ++i) {
const end = ends[i];
minSquaredDistance = _ol_geom_flat_closest_.getClosestPoint(
minSquaredDistance = assignClosestPoint(
flatCoordinates, offset, end, stride,
maxDelta, isRing, x, y, closestPoint, minSquaredDistance, tmpPoint);
offset = end;
}
return minSquaredDistance;
};
}
/**
@@ -236,17 +235,16 @@ _ol_geom_flat_closest_.getsClosestPoint = function(flatCoordinates, offset, ends
* @param {Array.<number>=} opt_tmpPoint Temporary point object.
* @return {number} Minimum squared distance.
*/
_ol_geom_flat_closest_.getssClosestPoint = function(flatCoordinates, offset,
export function assignClosestMultiArrayPoint(flatCoordinates, offset,
endss, stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance,
opt_tmpPoint) {
const tmpPoint = opt_tmpPoint ? opt_tmpPoint : [NaN, NaN];
for (let i = 0, ii = endss.length; i < ii; ++i) {
const ends = endss[i];
minSquaredDistance = _ol_geom_flat_closest_.getsClosestPoint(
minSquaredDistance = assignClosestArrayPoint(
flatCoordinates, offset, ends, stride,
maxDelta, isRing, x, y, closestPoint, minSquaredDistance, tmpPoint);
offset = ends[ends.length - 1];
}
return minSquaredDistance;
};
export default _ol_geom_flat_closest_;
}

View File

@@ -1,4 +1,4 @@
import _ol_geom_flat_closest_ from '../../../../../src/ol/geom/flat/closest.js';
import {assignClosestPoint, maxSquaredDelta} from '../../../../../src/ol/geom/flat/closest.js';
describe('ol.geom.flat.closest', function() {
@@ -10,7 +10,7 @@ describe('ol.geom.flat.closest', function() {
describe('ol.geom.flat.closest.getMaxSquaredDelta', function() {
it('returns the expected value in simple cases', function() {
expect(_ol_geom_flat_closest_.getMaxSquaredDelta(
expect(maxSquaredDelta(
flatCoordinates, 0, flatCoordinates.length, 2, 0)).to.be(9);
});
@@ -19,23 +19,23 @@ describe('ol.geom.flat.closest', function() {
describe('ol.geom.flat.closest.getClosestPoint', function() {
it('returns the expected value', function() {
const maxDelta = Math.sqrt(_ol_geom_flat_closest_.getMaxSquaredDelta(
const maxDelta = Math.sqrt(maxSquaredDelta(
flatCoordinates, 0, flatCoordinates.length, 2, 0));
expect(maxDelta).to.be(3);
const closestPoint = [NaN, NaN];
expect(_ol_geom_flat_closest_.getClosestPoint(
expect(assignClosestPoint(
flatCoordinates, 0, flatCoordinates.length, 2,
maxDelta, false, 0, 0, closestPoint, Infinity)).to.be(0);
expect(closestPoint).to.eql([0, 0]);
expect(_ol_geom_flat_closest_.getClosestPoint(
expect(assignClosestPoint(
flatCoordinates, 0, flatCoordinates.length, 2,
maxDelta, false, 4, 1, closestPoint, Infinity)).to.be(1);
expect(closestPoint).to.eql([4, 0]);
expect(_ol_geom_flat_closest_.getClosestPoint(
expect(assignClosestPoint(
flatCoordinates, 0, flatCoordinates.length, 2,
maxDelta, false, 5, 2, closestPoint, Infinity)).to.be(4);
expect(closestPoint).to.eql([5, 0]);
expect(_ol_geom_flat_closest_.getClosestPoint(
expect(assignClosestPoint(
flatCoordinates, 0, flatCoordinates.length, 2,
maxDelta, false, 10, 100, closestPoint, Infinity)).to.be(10000);
expect(closestPoint).to.eql([10, 0]);
@@ -78,7 +78,7 @@ describe('ol.geom.flat.closest', function() {
describe('ol.geom.closet.maSquaredDelta', function() {
it('returns the expected value', function() {
expect(_ol_geom_flat_closest_.getMaxSquaredDelta(
expect(maxSquaredDelta(
flatCoordinates, 0, flatCoordinates.length, 2, 0)).
to.roughlyEqual(1389.1058, 1e-9);
});
@@ -88,21 +88,21 @@ describe('ol.geom.flat.closest', function() {
describe('ol.geom.flat.closest.getClosestPoint', function() {
it('returns the expected value', function() {
const maxDelta = Math.sqrt(_ol_geom_flat_closest_.getMaxSquaredDelta(
const maxDelta = Math.sqrt(maxSquaredDelta(
flatCoordinates, 0, flatCoordinates.length, 2, 0));
expect(maxDelta).to.roughlyEqual(Math.sqrt(1389.1058), 1e-9);
const closestPoint = [NaN, NaN];
expect(_ol_geom_flat_closest_.getClosestPoint(
expect(assignClosestPoint(
flatCoordinates, 0, flatCoordinates.length, 2,
maxDelta, false, 0, 0, closestPoint, Infinity)).
to.roughlyEqual(110902.405, 1e-9);
expect(closestPoint).to.eql([292.41, 159.37]);
expect(_ol_geom_flat_closest_.getClosestPoint(
expect(assignClosestPoint(
flatCoordinates, 0, flatCoordinates.length, 2,
maxDelta, false, 500, 500, closestPoint, Infinity)).
to.roughlyEqual(106407.905, 1e-9);
expect(closestPoint).to.eql([671.55, 222.55]);
expect(_ol_geom_flat_closest_.getClosestPoint(
expect(assignClosestPoint(
flatCoordinates, 0, flatCoordinates.length, 2,
maxDelta, false, 1000, 500, closestPoint, Infinity)).
to.roughlyEqual(18229.4425, 1e-9);
@@ -121,11 +121,11 @@ describe('ol.geom.flat.closest', function() {
describe('ol.geom.flat.closest.getClosestPoint', function() {
it('interpolates M coordinates', function() {
const maxDelta = Math.sqrt(_ol_geom_flat_closest_.getMaxSquaredDelta(
const maxDelta = Math.sqrt(maxSquaredDelta(
flatCoordinates, 0, flatCoordinates.length, stride, 0));
expect(maxDelta).to.roughlyEqual(Math.sqrt(8), 1e-9);
const closestPoint = [NaN, NaN];
expect(_ol_geom_flat_closest_.getClosestPoint(
expect(assignClosestPoint(
flatCoordinates, 0, flatCoordinates.length, stride,
maxDelta, false, 1, 1, closestPoint, Infinity)).
to.roughlyEqual(0, 1e-9);