Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split ol.geom.flat into smaller modules #1847

Merged
merged 17 commits into from Mar 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ol/format/polylineformat.js
Expand Up @@ -4,7 +4,7 @@ goog.require('goog.asserts');
goog.require('ol.Feature');
goog.require('ol.format.TextFeature');
goog.require('ol.geom.LineString');
goog.require('ol.geom.flat');
goog.require('ol.geom.flat.inflate');
goog.require('ol.proj');


Expand Down Expand Up @@ -279,7 +279,7 @@ ol.format.Polyline.prototype.readFeaturesFromText = function(text) {
*/
ol.format.Polyline.prototype.readGeometryFromText = function(text) {
var flatCoordinates = ol.format.Polyline.decodeFlatCoordinates(text, 2);
var coordinates = ol.geom.flat.inflateCoordinates(
var coordinates = ol.geom.flat.inflate.coordinates(
flatCoordinates, 0, flatCoordinates.length, 2);
return new ol.geom.LineString(coordinates);
};
Expand Down
4 changes: 2 additions & 2 deletions src/ol/geom/circle.js
Expand Up @@ -4,7 +4,7 @@ goog.require('goog.asserts');
goog.require('ol.extent');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SimpleGeometry');
goog.require('ol.geom.flat');
goog.require('ol.geom.flat.deflate');



Expand Down Expand Up @@ -174,7 +174,7 @@ ol.geom.Circle.prototype.setCenterAndRadius =
}
/** @type {Array.<number>} */
var flatCoordinates = this.flatCoordinates;
var offset = ol.geom.flat.deflateCoordinate(
var offset = ol.geom.flat.deflate.coordinate(
flatCoordinates, 0, center, this.stride);
flatCoordinates[offset++] = flatCoordinates[0] + radius;
var i, ii;
Expand Down
64 changes: 64 additions & 0 deletions src/ol/geom/flat/areaflatgeom.js
@@ -0,0 +1,64 @@
goog.provide('ol.geom.flat.area');


/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {number} end End.
* @param {number} stride Stride.
* @return {number} Area.
*/
ol.geom.flat.area.linearRing = function(flatCoordinates, offset, end, stride) {
var twiceArea = 0;
var x1 = flatCoordinates[end - stride];
var y1 = flatCoordinates[end - stride + 1];
for (; offset < end; offset += stride) {
var x2 = flatCoordinates[offset];
var y2 = flatCoordinates[offset + 1];
twiceArea += y1 * x2 - x1 * y2;
x1 = x2;
y1 = y2;
}
return twiceArea / 2;
};


/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {Array.<number>} ends Ends.
* @param {number} stride Stride.
* @return {number} Area.
*/
ol.geom.flat.area.linearRings =
function(flatCoordinates, offset, ends, stride) {
var area = 0;
var i, ii;
for (i = 0, ii = ends.length; i < ii; ++i) {
var end = ends[i];
area += ol.geom.flat.area.linearRing(flatCoordinates, offset, end, stride);
offset = end;
}
return area;
};


/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {Array.<Array.<number>>} endss Endss.
* @param {number} stride Stride.
* @return {number} Area.
*/
ol.geom.flat.area.linearRingss =
function(flatCoordinates, offset, endss, stride) {
var area = 0;
var i, ii;
for (i = 0, ii = endss.length; i < ii; ++i) {
var ends = endss[i];
area +=
ol.geom.flat.area.linearRings(flatCoordinates, offset, ends, stride);
offset = ends[ends.length - 1];
}
return area;
};
26 changes: 26 additions & 0 deletions src/ol/geom/flat/centerflatgeom.js
@@ -0,0 +1,26 @@
goog.provide('ol.geom.flat.center');

goog.require('ol.extent');


/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {Array.<Array.<number>>} endss Endss.
* @param {number} stride Stride.
* @return {Array.<number>} Flat centers.
*/
ol.geom.flat.center.linearRingss =
function(flatCoordinates, offset, endss, stride) {
var flatCenters = [];
var i, ii;
var extent = ol.extent.createEmpty();
for (i = 0, ii = endss.length; i < ii; ++i) {
var ends = endss[i];
extent = ol.extent.createOrUpdateFromFlatCoordinates(
flatCoordinates, offset, ends[0], stride);
flatCenters.push((extent[0] + extent[2]) / 2, (extent[1] + extent[3]) / 2);
offset = ends[ends.length - 1];
}
return flatCenters;
};
89 changes: 65 additions & 24 deletions src/ol/geom/closestgeom.js → src/ol/geom/flat/closestflatgeom.js
@@ -1,9 +1,51 @@
// FIXME find better names for these functions

goog.provide('ol.geom.closest');
goog.provide('ol.geom.flat.closest');

goog.require('goog.asserts');
goog.require('ol.geom.flat');
goog.require('goog.math');
goog.require('ol.math');


/**
* 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;
};


/**
Expand All @@ -16,14 +58,14 @@ 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];
for (offset += stride; offset < end; offset += stride) {
var x2 = flatCoordinates[offset];
var y2 = flatCoordinates[offset + 1];
var squaredDelta = ol.geom.flat.squaredDistance(x1, y1, x2, y2);
var squaredDelta = ol.math.squaredDistance(x1, y1, x2, y2);
if (squaredDelta > maxSquaredDelta) {
maxSquaredDelta = squaredDelta;
}
Expand All @@ -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;
}
Expand All @@ -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];
}
Expand All @@ -90,15 +132,16 @@ 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;
}
var i, squaredDistance;
if (maxDelta === 0) {
// All points are identical, so just test the first point.
squaredDistance = ol.geom.flat.squaredDistance(
squaredDistance = ol.math.squaredDistance(
x, y, flatCoordinates[offset], flatCoordinates[offset + 1]);
if (squaredDistance < minSquaredDistance) {
for (i = 0; i < stride; ++i) {
Expand All @@ -114,10 +157,9 @@ 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]);
squaredDistance = ol.math.squaredDistance(x, y, tmpPoint[0], tmpPoint[1]);
if (squaredDistance < minSquaredDistance) {
minSquaredDistance = squaredDistance;
for (i = 0; i < stride; ++i) {
Expand All @@ -143,10 +185,9 @@ 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]);
squaredDistance = ol.math.squaredDistance(x, y, tmpPoint[0], tmpPoint[1]);
if (squaredDistance < minSquaredDistance) {
minSquaredDistance = squaredDistance;
for (i = 0; i < stride; ++i) {
Expand All @@ -173,14 +214,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;
Expand All @@ -203,14 +244,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];
Expand Down