diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index a376e827ec8..8cdbe557551 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -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'); @@ -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); }; diff --git a/src/ol/geom/circle.js b/src/ol/geom/circle.js index 94d4293deb3..e0c7916c066 100644 --- a/src/ol/geom/circle.js +++ b/src/ol/geom/circle.js @@ -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'); @@ -174,7 +174,7 @@ ol.geom.Circle.prototype.setCenterAndRadius = } /** @type {Array.} */ 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; diff --git a/src/ol/geom/flat/areaflatgeom.js b/src/ol/geom/flat/areaflatgeom.js new file mode 100644 index 00000000000..eb0b1e4275a --- /dev/null +++ b/src/ol/geom/flat/areaflatgeom.js @@ -0,0 +1,64 @@ +goog.provide('ol.geom.flat.area'); + + +/** + * @param {Array.} 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.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} 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.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} 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; +}; diff --git a/src/ol/geom/flat/centerflatgeom.js b/src/ol/geom/flat/centerflatgeom.js new file mode 100644 index 00000000000..81f24369ff6 --- /dev/null +++ b/src/ol/geom/flat/centerflatgeom.js @@ -0,0 +1,26 @@ +goog.provide('ol.geom.flat.center'); + +goog.require('ol.extent'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @return {Array.} 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; +}; diff --git a/src/ol/geom/closestgeom.js b/src/ol/geom/flat/closestflatgeom.js similarity index 72% rename from src/ol/geom/closestgeom.js rename to src/ol/geom/flat/closestflatgeom.js index 0084a0d6f6e..9170d2478c1 100644 --- a/src/ol/geom/closestgeom.js +++ b/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.} 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.} 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; +}; /** @@ -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; } @@ -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,15 +132,16 @@ ol.geom.closest.getssMaxSquaredDelta = * @param {Array.=} 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) { @@ -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) { @@ -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) { @@ -173,14 +214,14 @@ ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride, * @param {Array.=} 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 +244,14 @@ ol.geom.closest.getsClosestPoint = function(flatCoordinates, offset, ends, * @param {Array.=} 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]; diff --git a/src/ol/geom/flat/containsflatgeom.js b/src/ol/geom/flat/containsflatgeom.js new file mode 100644 index 00000000000..5b5c93aacb2 --- /dev/null +++ b/src/ol/geom/flat/containsflatgeom.js @@ -0,0 +1,91 @@ +goog.provide('ol.geom.flat.contains'); + +goog.require('goog.asserts'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {number} x X. + * @param {number} y Y. + * @return {boolean} Contains (x, y). + */ +ol.geom.flat.contains.linearRingContainsXY = + function(flatCoordinates, offset, end, stride, x, y) { + // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html + var contains = false; + 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]; + var intersect = ((y1 > y) != (y2 > y)) && + (x < (x2 - x1) * (y - y1) / (y2 - y1) + x1); + if (intersect) { + contains = !contains; + } + x1 = x2; + y1 = y2; + } + return contains; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @param {number} x X. + * @param {number} y Y. + * @return {boolean} Contains (x, y). + */ +ol.geom.flat.contains.linearRingsContainsXY = + function(flatCoordinates, offset, ends, stride, x, y) { + goog.asserts.assert(ends.length > 0); + if (ends.length === 0) { + return false; + } + if (!ol.geom.flat.contains.linearRingContainsXY( + flatCoordinates, offset, ends[0], stride, x, y)) { + return false; + } + var i, ii; + for (i = 1, ii = ends.length; i < ii; ++i) { + if (ol.geom.flat.contains.linearRingContainsXY( + flatCoordinates, ends[i - 1], ends[i], stride, x, y)) { + return false; + } + } + return true; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @param {number} x X. + * @param {number} y Y. + * @return {boolean} Contains (x, y). + */ +ol.geom.flat.contains.linearRingssContainsXY = + function(flatCoordinates, offset, endss, stride, x, y) { + goog.asserts.assert(endss.length > 0); + if (endss.length === 0) { + return false; + } + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + var ends = endss[i]; + if (ol.geom.flat.contains.linearRingsContainsXY( + flatCoordinates, offset, ends, stride, x, y)) { + return true; + } + offset = ends[ends.length - 1]; + } + return false; +}; diff --git a/src/ol/geom/flat/deflateflatgeom.js b/src/ol/geom/flat/deflateflatgeom.js new file mode 100644 index 00000000000..e0aca18d910 --- /dev/null +++ b/src/ol/geom/flat/deflateflatgeom.js @@ -0,0 +1,91 @@ +goog.provide('ol.geom.flat.deflate'); + +goog.require('goog.asserts'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {ol.Coordinate} coordinate Coordinate. + * @param {number} stride Stride. + * @return {number} offset Offset. + */ +ol.geom.flat.deflate.coordinate = + function(flatCoordinates, offset, coordinate, stride) { + goog.asserts.assert(coordinate.length == stride); + var i, ii; + for (i = 0, ii = coordinate.length; i < ii; ++i) { + flatCoordinates[offset++] = coordinate[i]; + } + return offset; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} coordinates Coordinates. + * @param {number} stride Stride. + * @return {number} offset Offset. + */ +ol.geom.flat.deflate.coordinates = + function(flatCoordinates, offset, coordinates, stride) { + var i, ii; + for (i = 0, ii = coordinates.length; i < ii; ++i) { + var coordinate = coordinates[i]; + goog.asserts.assert(coordinate.length == stride); + var j; + for (j = 0; j < stride; ++j) { + flatCoordinates[offset++] = coordinate[j]; + } + } + return offset; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} coordinatess Coordinatess. + * @param {number} stride Stride. + * @param {Array.=} opt_ends Ends. + * @return {Array.} Ends. + */ +ol.geom.flat.deflate.coordinatess = + function(flatCoordinates, offset, coordinatess, stride, opt_ends) { + var ends = goog.isDef(opt_ends) ? opt_ends : []; + var i = 0; + var j, jj; + for (j = 0, jj = coordinatess.length; j < jj; ++j) { + var end = ol.geom.flat.deflate.coordinates( + flatCoordinates, offset, coordinatess[j], stride); + ends[i++] = end; + offset = end; + } + ends.length = i; + return ends; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>>} coordinatesss Coordinatesss. + * @param {number} stride Stride. + * @param {Array.>=} opt_endss Endss. + * @return {Array.>} Endss. + */ +ol.geom.flat.deflate.coordinatesss = + function(flatCoordinates, offset, coordinatesss, stride, opt_endss) { + var endss = goog.isDef(opt_endss) ? opt_endss : []; + var i = 0; + var j, jj; + for (j = 0, jj = coordinatesss.length; j < jj; ++j) { + var ends = ol.geom.flat.deflate.coordinatess( + flatCoordinates, offset, coordinatesss[j], stride, endss[i]); + endss[i++] = ends; + offset = ends[ends.length - 1]; + } + endss.length = i; + return endss; +}; diff --git a/src/ol/geom/flat/flipflatgeom.js b/src/ol/geom/flat/flipflatgeom.js new file mode 100644 index 00000000000..220f2115acb --- /dev/null +++ b/src/ol/geom/flat/flipflatgeom.js @@ -0,0 +1,37 @@ +goog.provide('ol.geom.flat.flip'); + +goog.require('goog.asserts'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {Array.=} opt_dest Destination. + * @param {number=} opt_destOffset Destination offset. + * @return {Array.} Flat coordinates. + */ +ol.geom.flat.flip.flipXY = + function(flatCoordinates, offset, end, stride, opt_dest, opt_destOffset) { + var dest, destOffset; + if (goog.isDef(opt_dest)) { + dest = opt_dest; + destOffset = goog.isDef(opt_destOffset) ? opt_destOffset : 0; + } else { + goog.asserts.assert(!goog.isDef(opt_destOffset)); + dest = []; + destOffset = 0; + } + var j, k; + for (j = offset; j < end; ) { + var x = flatCoordinates[j++]; + dest[destOffset++] = flatCoordinates[j++]; + dest[destOffset++] = x; + for (k = 2; k < stride; ++k) { + dest[destOffset++] = flatCoordinates[j++]; + } + } + dest.length = destOffset; + return dest; +}; diff --git a/src/ol/geom/flat/inflateflatgeom.js b/src/ol/geom/flat/inflateflatgeom.js new file mode 100644 index 00000000000..53c4620c3f0 --- /dev/null +++ b/src/ol/geom/flat/inflateflatgeom.js @@ -0,0 +1,71 @@ +goog.provide('ol.geom.flat.inflate'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {Array.=} opt_coordinates Coordinates. + * @return {Array.} Coordinates. + */ +ol.geom.flat.inflate.coordinates = + function(flatCoordinates, offset, end, stride, opt_coordinates) { + var coordinates = goog.isDef(opt_coordinates) ? opt_coordinates : []; + var i = 0; + var j; + for (j = offset; j < end; j += stride) { + coordinates[i++] = flatCoordinates.slice(j, j + stride); + } + coordinates.length = i; + return coordinates; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @param {Array.>=} opt_coordinatess Coordinatess. + * @return {Array.>} Coordinatess. + */ +ol.geom.flat.inflate.coordinatess = + function(flatCoordinates, offset, ends, stride, opt_coordinatess) { + var coordinatess = goog.isDef(opt_coordinatess) ? opt_coordinatess : []; + var i = 0; + var j, jj; + for (j = 0, jj = ends.length; j < jj; ++j) { + var end = ends[j]; + coordinatess[i++] = ol.geom.flat.inflate.coordinates( + flatCoordinates, offset, end, stride, coordinatess[i]); + offset = end; + } + coordinatess.length = i; + return coordinatess; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @param {Array.>>=} opt_coordinatesss + * Coordinatesss. + * @return {Array.>>} Coordinatesss. + */ +ol.geom.flat.inflate.coordinatesss = + function(flatCoordinates, offset, endss, stride, opt_coordinatesss) { + var coordinatesss = goog.isDef(opt_coordinatesss) ? opt_coordinatesss : []; + var i = 0; + var j, jj; + for (j = 0, jj = endss.length; j < jj; ++j) { + var ends = endss[j]; + coordinatesss[i++] = ol.geom.flat.inflate.coordinatess( + flatCoordinates, offset, ends, stride, coordinatesss[i]); + offset = ends[ends.length - 1]; + } + coordinatesss.length = i; + return coordinatesss; +}; diff --git a/src/ol/geom/flat/interiorpointflatgeom.js b/src/ol/geom/flat/interiorpointflatgeom.js new file mode 100644 index 00000000000..bc8d1082229 --- /dev/null +++ b/src/ol/geom/flat/interiorpointflatgeom.js @@ -0,0 +1,92 @@ +goog.provide('ol.geom.flat.interiorpoint'); + +goog.require('goog.asserts'); +goog.require('ol.geom.flat.contains'); + + +/** + * Calculates a point that is likely to lie in the interior of the linear rings. + * Inspired by JTS's com.vividsolutions.jts.geom.Geometry#getInteriorPoint. + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @param {Array.} flatCenters Flat centers. + * @param {number} flatCentersOffset Flat center offset. + * @param {Array.=} opt_dest Destination. + * @return {Array.} Destination. + */ +ol.geom.flat.interiorpoint.linearRings = function(flatCoordinates, offset, + ends, stride, flatCenters, flatCentersOffset, opt_dest) { + var i, ii, x, x1, x2, y1, y2; + var y = flatCenters[flatCentersOffset + 1]; + /** @type {Array.} */ + var intersections = []; + // Calculate intersections with the horizontal line + var end = ends[0]; + x1 = flatCoordinates[end - stride]; + y1 = flatCoordinates[end - stride + 1]; + for (i = offset; i < end; i += stride) { + x2 = flatCoordinates[i]; + y2 = flatCoordinates[i + 1]; + if ((y <= y1 && y2 <= y) || (y1 <= y && y <= y2)) { + x = (y - y1) / (y2 - y1) * (x2 - x1) + x1; + intersections.push(x); + } + x1 = x2; + y1 = y2; + } + // Find the longest segment of the horizontal line that has its center point + // inside the linear ring. + var pointX = NaN; + var maxSegmentLength = -Infinity; + intersections.sort(); + x1 = intersections[0]; + for (i = 1, ii = intersections.length; i < ii; ++i) { + x2 = intersections[i]; + var segmentLength = Math.abs(x2 - x1); + if (segmentLength > maxSegmentLength) { + x = (x1 + x2) / 2; + if (ol.geom.flat.contains.linearRingsContainsXY( + flatCoordinates, offset, ends, stride, x, y)) { + pointX = x; + maxSegmentLength = segmentLength; + } + } + x1 = x2; + } + if (isNaN(pointX)) { + // There is no horizontal line that has its center point inside the linear + // ring. Use the center of the the linear ring's extent. + pointX = flatCenters[flatCentersOffset]; + } + if (goog.isDef(opt_dest)) { + opt_dest.push(pointX, y); + return opt_dest; + } else { + return [pointX, y]; + } +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @param {Array.} flatCenters Flat centers. + * @return {Array.} Interior points. + */ +ol.geom.flat.interiorpoint.linearRingss = + function(flatCoordinates, offset, endss, stride, flatCenters) { + goog.asserts.assert(2 * endss.length == flatCenters.length); + var interiorPoints = []; + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + var ends = endss[i]; + interiorPoints = ol.geom.flat.interiorpoint.linearRings(flatCoordinates, + offset, ends, stride, flatCenters, 2 * i, interiorPoints); + offset = ends[ends.length - 1]; + } + return interiorPoints; +}; diff --git a/src/ol/geom/flat/interpolateflatgeom.js b/src/ol/geom/flat/interpolateflatgeom.js new file mode 100644 index 00000000000..dcfb16e2cb5 --- /dev/null +++ b/src/ol/geom/flat/interpolateflatgeom.js @@ -0,0 +1,189 @@ +goog.provide('ol.geom.flat.interpolate'); + +goog.require('goog.array'); +goog.require('goog.asserts'); +goog.require('goog.math'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {number} fraction Fraction. + * @param {Array.=} opt_dest Destination. + * @return {Array.} Destination. + */ +ol.geom.flat.interpolate.lineString = + function(flatCoordinates, offset, end, stride, fraction, opt_dest) { + // FIXME interpolate extra dimensions + goog.asserts.assert(0 <= fraction && fraction <= 1); + var pointX = NaN; + var pointY = NaN; + var n = (end - offset) / stride; + if (n === 0) { + goog.asserts.fail(); + } else if (n == 1) { + pointX = flatCoordinates[offset]; + pointY = flatCoordinates[offset + 1]; + } else if (n == 2) { + pointX = (1 - fraction) * flatCoordinates[offset] + + fraction * flatCoordinates[offset + stride]; + pointY = (1 - fraction) * flatCoordinates[offset + 1] + + fraction * flatCoordinates[offset + stride + 1]; + } else { + var x1 = flatCoordinates[offset]; + var y1 = flatCoordinates[offset + 1]; + var length = 0; + var cumulativeLengths = [0]; + var i; + for (i = offset + stride; i < end; i += stride) { + var x2 = flatCoordinates[i]; + var y2 = flatCoordinates[i + 1]; + length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); + cumulativeLengths.push(length); + x1 = x2; + y1 = y2; + } + var target = fraction * length; + var index = goog.array.binarySearch(cumulativeLengths, target); + if (index < 0) { + var t = (target - cumulativeLengths[-index - 2]) / + (cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]); + var o = offset + (-index - 2) * stride; + pointX = goog.math.lerp( + flatCoordinates[o], flatCoordinates[o + stride], t); + pointY = goog.math.lerp( + flatCoordinates[o + 1], flatCoordinates[o + stride + 1], t); + } else { + pointX = flatCoordinates[offset + index * stride]; + pointY = flatCoordinates[offset + index * stride + 1]; + } + } + if (goog.isDefAndNotNull(opt_dest)) { + opt_dest.push(pointX, pointY); + return opt_dest; + } else { + return [pointX, pointY]; + } +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {number} m M. + * @param {boolean} extrapolate Extrapolate. + * @return {ol.Coordinate} Coordinate. + */ +ol.geom.flat.lineStringCoordinateAtM = + function(flatCoordinates, offset, end, stride, m, extrapolate) { + if (end == offset) { + return null; + } + var coordinate; + if (m < flatCoordinates[offset + stride - 1]) { + if (extrapolate) { + coordinate = flatCoordinates.slice(offset, offset + stride); + coordinate[stride - 1] = m; + return coordinate; + } else { + return null; + } + } else if (flatCoordinates[end - 1] < m) { + if (extrapolate) { + coordinate = flatCoordinates.slice(end - stride, end); + coordinate[stride - 1] = m; + return coordinate; + } else { + return null; + } + } + // FIXME use O(1) search + if (m == flatCoordinates[offset + stride - 1]) { + return flatCoordinates.slice(offset, offset + stride); + } + var lo = offset / stride; + var hi = end / stride; + while (lo < hi) { + var mid = (lo + hi) >> 1; + if (m < flatCoordinates[(mid + 1) * stride - 1]) { + hi = mid; + } else { + lo = mid + 1; + } + } + var m0 = flatCoordinates[lo * stride - 1]; + if (m == m0) { + return flatCoordinates.slice((lo - 1) * stride, (lo - 1) * stride + stride); + } + var m1 = flatCoordinates[(lo + 1) * stride - 1]; + goog.asserts.assert(m0 < m); + goog.asserts.assert(m <= m1); + var t = (m - m0) / (m1 - m0); + coordinate = []; + var i; + for (i = 0; i < stride - 1; ++i) { + coordinate.push(goog.math.lerp(flatCoordinates[(lo - 1) * stride + i], + flatCoordinates[lo * stride + i], t)); + } + coordinate.push(m); + goog.asserts.assert(coordinate.length == stride); + return coordinate; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} 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; +}; diff --git a/src/ol/geom/flat/lengthflatgeom.js b/src/ol/geom/flat/lengthflatgeom.js new file mode 100644 index 00000000000..ac1c6855041 --- /dev/null +++ b/src/ol/geom/flat/lengthflatgeom.js @@ -0,0 +1,43 @@ +goog.provide('ol.geom.flat.length'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {number} Length. + */ +ol.geom.flat.length.lineString = + function(flatCoordinates, offset, end, stride) { + var x1 = flatCoordinates[offset]; + var y1 = flatCoordinates[offset + 1]; + var length = 0; + var i; + for (i = offset + stride; i < end; i += stride) { + var x2 = flatCoordinates[i]; + var y2 = flatCoordinates[i + 1]; + length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); + x1 = x2; + y1 = y2; + } + return length; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {number} Perimeter. + */ +ol.geom.flat.length.linearRing = + function(flatCoordinates, offset, end, stride) { + var perimeter = + ol.geom.flat.length.lineString(flatCoordinates, offset, end, stride); + var dx = flatCoordinates[end - stride] - flatCoordinates[offset]; + var dy = flatCoordinates[end - stride + 1] - flatCoordinates[offset + 1]; + perimeter += Math.sqrt(dx * dx + dy * dy); + return perimeter; +}; diff --git a/src/ol/geom/flat/orientflatgeom.js b/src/ol/geom/flat/orientflatgeom.js new file mode 100644 index 00000000000..48c7dd50083 --- /dev/null +++ b/src/ol/geom/flat/orientflatgeom.js @@ -0,0 +1,115 @@ +goog.provide('ol.geom.flat.orient'); + +goog.require('ol.geom.flat.reverse'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {boolean} Is clockwise. + */ +ol.geom.flat.orient.linearRingIsClockwise = + function(flatCoordinates, offset, end, stride) { + // http://tinyurl.com/clockwise-method + // https://github.com/OSGeo/gdal/blob/trunk/gdal/ogr/ogrlinearring.cpp + var edge = 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]; + edge += (x2 - x1) * (y2 + y1); + x1 = x2; + y1 = y2; + } + return edge > 0; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @return {boolean} `true` if all rings are correctly oriented, `false` + * otherwise. + */ +ol.geom.flat.orient.linearRingsAreOriented = + function(flatCoordinates, offset, ends, stride) { + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + var isClockwise = ol.geom.flat.orient.linearRingIsClockwise( + flatCoordinates, offset, end, stride); + if (i === 0 ? !isClockwise : isClockwise) { + return false; + } + offset = end; + } + return true; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @return {boolean} `true` if all rings are correctly oriented, `false` + * otherwise. + */ +ol.geom.flat.linearRingssAreOriented = + function(flatCoordinates, offset, endss, stride) { + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + if (!ol.geom.flat.orient.linearRingsAreOriented( + flatCoordinates, offset, endss[i], stride)) { + return false; + } + } + return true; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @return {number} End. + */ +ol.geom.flat.orient.orientLinearRings = + function(flatCoordinates, offset, ends, stride) { + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + var isClockwise = ol.geom.flat.orient.linearRingIsClockwise( + flatCoordinates, offset, end, stride); + var reverse = i === 0 ? !isClockwise : isClockwise; + if (reverse) { + ol.geom.flat.reverse.coordinates(flatCoordinates, offset, end, stride); + } + offset = end; + } + return offset; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @return {number} End. + */ +ol.geom.flat.orient.orientLinearRingss = + function(flatCoordinates, offset, endss, stride) { + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + offset = ol.geom.flat.orient.orientLinearRings( + flatCoordinates, offset, endss[i], stride); + } + return offset; +}; diff --git a/src/ol/geom/flat/reverseflatgeom.js b/src/ol/geom/flat/reverseflatgeom.js new file mode 100644 index 00000000000..32ad1dbe01e --- /dev/null +++ b/src/ol/geom/flat/reverseflatgeom.js @@ -0,0 +1,22 @@ +goog.provide('ol.geom.flat.reverse'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + */ +ol.geom.flat.reverse.coordinates = + function(flatCoordinates, offset, end, stride) { + while (offset < end - stride) { + var i; + for (i = 0; i < stride; ++i) { + var tmp = flatCoordinates[offset + i]; + flatCoordinates[offset + i] = flatCoordinates[end - stride + i]; + flatCoordinates[end - stride + i] = tmp; + } + offset += stride; + end -= stride; + } +}; diff --git a/src/ol/geom/simplifygeom.js b/src/ol/geom/flat/simplifyflatgeom.js similarity index 88% rename from src/ol/geom/simplifygeom.js rename to src/ol/geom/flat/simplifyflatgeom.js index 1e361ae3e5d..b5f20320402 100644 --- a/src/ol/geom/simplifygeom.js +++ b/src/ol/geom/flat/simplifyflatgeom.js @@ -24,9 +24,9 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -goog.provide('ol.geom.simplify'); +goog.provide('ol.geom.flat.simplify'); -goog.require('ol.geom.flat'); +goog.require('ol.math'); /** @@ -40,19 +40,19 @@ goog.require('ol.geom.flat'); * coordinates. * @return {Array.} Simplified line string. */ -ol.geom.simplify.lineString = function(flatCoordinates, offset, end, stride, - squaredTolerance, highQuality, opt_simplifiedFlatCoordinates) { +ol.geom.flat.simplify.lineString = function(flatCoordinates, offset, end, + stride, squaredTolerance, highQuality, opt_simplifiedFlatCoordinates) { var simplifiedFlatCoordinates = goog.isDef(opt_simplifiedFlatCoordinates) ? opt_simplifiedFlatCoordinates : []; if (!highQuality) { - end = ol.geom.simplify.radialDistance(flatCoordinates, offset, end, + end = ol.geom.flat.simplify.radialDistance(flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, 0); flatCoordinates = simplifiedFlatCoordinates; offset = 0; stride = 2; } - simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeucker( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeucker( flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, 0); return simplifiedFlatCoordinates; @@ -70,7 +70,7 @@ ol.geom.simplify.lineString = function(flatCoordinates, offset, end, stride, * @param {number} simplifiedOffset Simplified offset. * @return {number} Simplified offset. */ -ol.geom.simplify.douglasPeucker = function(flatCoordinates, offset, end, +ol.geom.flat.simplify.douglasPeucker = function(flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset) { var n = (end - offset) / stride; if (n < 3) { @@ -101,7 +101,7 @@ ol.geom.simplify.douglasPeucker = function(flatCoordinates, offset, end, for (i = first + stride; i < last; i += stride) { var x = flatCoordinates[i]; var y = flatCoordinates[i + 1]; - var squaredDistance = ol.geom.flat.squaredSegmentDistance( + var squaredDistance = ol.math.squaredSegmentDistance( x, y, x1, y1, x2, y2); if (squaredDistance > maxSquaredDistance) { index = i; @@ -142,13 +142,13 @@ ol.geom.simplify.douglasPeucker = function(flatCoordinates, offset, end, * @param {Array.} simplifiedEnds Simplified ends. * @return {number} Simplified offset. */ -ol.geom.simplify.douglasPeuckers = function(flatCoordinates, offset, +ol.geom.flat.simplify.douglasPeuckers = function(flatCoordinates, offset, ends, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds) { var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - simplifiedOffset = ol.geom.simplify.douglasPeucker( + simplifiedOffset = ol.geom.flat.simplify.douglasPeucker( flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset); simplifiedEnds.push(simplifiedOffset); @@ -170,14 +170,14 @@ ol.geom.simplify.douglasPeuckers = function(flatCoordinates, offset, * @param {Array.>} simplifiedEndss Simplified endss. * @return {number} Simplified offset. */ -ol.geom.simplify.douglasPeuckerss = function( +ol.geom.flat.simplify.douglasPeuckerss = function( flatCoordinates, offset, endss, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEndss) { var i, ii; for (i = 0, ii = endss.length; i < ii; ++i) { var ends = endss[i]; var simplifiedEnds = []; - simplifiedOffset = ol.geom.simplify.douglasPeuckers( + simplifiedOffset = ol.geom.flat.simplify.douglasPeuckers( flatCoordinates, offset, ends, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds); simplifiedEndss.push(simplifiedEnds); @@ -198,7 +198,7 @@ ol.geom.simplify.douglasPeuckerss = function( * @param {number} simplifiedOffset Simplified offset. * @return {number} Simplified offset. */ -ol.geom.simplify.radialDistance = function(flatCoordinates, offset, end, +ol.geom.flat.simplify.radialDistance = function(flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset) { if (end <= offset + stride) { // zero or one point, no simplification possible, so copy and return @@ -219,7 +219,7 @@ ol.geom.simplify.radialDistance = function(flatCoordinates, offset, end, for (offset += stride; offset < end; offset += stride) { x2 = flatCoordinates[offset]; y2 = flatCoordinates[offset + 1]; - if (ol.geom.flat.squaredDistance(x1, y1, x2, y2) > squaredTolerance) { + if (ol.math.squaredDistance(x1, y1, x2, y2) > squaredTolerance) { // copy point at offset simplifiedFlatCoordinates[simplifiedOffset++] = x2; simplifiedFlatCoordinates[simplifiedOffset++] = y2; @@ -241,7 +241,7 @@ ol.geom.simplify.radialDistance = function(flatCoordinates, offset, end, * @param {number} tolerance Squared tolerance. * @return {number} Rounded value. */ -ol.geom.simplify.snap = function(value, tolerance) { +ol.geom.flat.simplify.snap = function(value, tolerance) { return tolerance * Math.round(value / tolerance); }; @@ -265,15 +265,15 @@ ol.geom.simplify.snap = function(value, tolerance) { * @param {number} simplifiedOffset Simplified offset. * @return {number} Simplified offset. */ -ol.geom.simplify.quantize = function(flatCoordinates, offset, end, stride, +ol.geom.flat.simplify.quantize = function(flatCoordinates, offset, end, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset) { // do nothing if the line is empty if (offset == end) { return simplifiedOffset; } // snap the first coordinate (P1) - var x1 = ol.geom.simplify.snap(flatCoordinates[offset], tolerance); - var y1 = ol.geom.simplify.snap(flatCoordinates[offset + 1], tolerance); + var x1 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance); + var y1 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance); offset += stride; // add the first coordinate to the output simplifiedFlatCoordinates[simplifiedOffset++] = x1; @@ -282,8 +282,8 @@ ol.geom.simplify.quantize = function(flatCoordinates, offset, end, stride, // coordinate (P2) var x2, y2; do { - x2 = ol.geom.simplify.snap(flatCoordinates[offset], tolerance); - y2 = ol.geom.simplify.snap(flatCoordinates[offset + 1], tolerance); + x2 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance); + y2 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance); offset += stride; if (offset == end) { // all coordinates snap to the same value, the line collapses to a point @@ -298,8 +298,8 @@ ol.geom.simplify.quantize = function(flatCoordinates, offset, end, stride, while (offset < end) { var x3, y3; // snap the next coordinate (P3) - x3 = ol.geom.simplify.snap(flatCoordinates[offset], tolerance); - y3 = ol.geom.simplify.snap(flatCoordinates[offset + 1], tolerance); + x3 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance); + y3 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance); offset += stride; // skip P3 if it is equal to P2 if (x3 == x2 && y3 == y2) { @@ -351,14 +351,14 @@ ol.geom.simplify.quantize = function(flatCoordinates, offset, end, stride, * @param {Array.} simplifiedEnds Simplified ends. * @return {number} Simplified offset. */ -ol.geom.simplify.quantizes = function( +ol.geom.flat.simplify.quantizes = function( flatCoordinates, offset, ends, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds) { var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - simplifiedOffset = ol.geom.simplify.quantize( + simplifiedOffset = ol.geom.flat.simplify.quantize( flatCoordinates, offset, end, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset); @@ -381,7 +381,7 @@ ol.geom.simplify.quantizes = function( * @param {Array.>} simplifiedEndss Simplified endss. * @return {number} Simplified offset. */ -ol.geom.simplify.quantizess = function( +ol.geom.flat.simplify.quantizess = function( flatCoordinates, offset, endss, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEndss) { @@ -389,7 +389,7 @@ ol.geom.simplify.quantizess = function( for (i = 0, ii = endss.length; i < ii; ++i) { var ends = endss[i]; var simplifiedEnds = []; - simplifiedOffset = ol.geom.simplify.quantizes( + simplifiedOffset = ol.geom.flat.simplify.quantizes( flatCoordinates, offset, ends, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds); diff --git a/src/ol/geom/flat/transformflatgeom.js b/src/ol/geom/flat/transformflatgeom.js new file mode 100644 index 00000000000..dc8c77e45ec --- /dev/null +++ b/src/ol/geom/flat/transformflatgeom.js @@ -0,0 +1,34 @@ +goog.provide('ol.geom.flat.transform'); + +goog.require('goog.vec.Mat4'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} stride Stride. + * @param {goog.vec.Mat4.Number} transform Transform. + * @param {Array.=} opt_dest Destination. + * @return {Array.} Transformed coordinates. + */ +ol.geom.flat.transform.transform2D = + function(flatCoordinates, stride, transform, opt_dest) { + var m00 = goog.vec.Mat4.getElement(transform, 0, 0); + var m10 = goog.vec.Mat4.getElement(transform, 1, 0); + var m01 = goog.vec.Mat4.getElement(transform, 0, 1); + var m11 = goog.vec.Mat4.getElement(transform, 1, 1); + var m03 = goog.vec.Mat4.getElement(transform, 0, 3); + var m13 = goog.vec.Mat4.getElement(transform, 1, 3); + var dest = goog.isDef(opt_dest) ? opt_dest : []; + var i = 0; + var j, jj; + for (j = 0, jj = flatCoordinates.length; j < jj; j += stride) { + var x = flatCoordinates[j]; + var y = flatCoordinates[j + 1]; + dest[i++] = m00 * x + m01 * y + m03; + dest[i++] = m10 * x + m11 * y + m13; + } + if (goog.isDef(opt_dest) && dest.length != i) { + dest.length = i; + } + return dest; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js deleted file mode 100644 index c463be099f5..00000000000 --- a/src/ol/geom/flatgeom.js +++ /dev/null @@ -1,933 +0,0 @@ -goog.provide('ol.geom.flat'); - -goog.require('goog.array'); -goog.require('goog.asserts'); -goog.require('goog.math'); -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.} 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.} 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.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {ol.Coordinate} coordinate Coordinate. - * @param {number} stride Stride. - * @return {number} offset Offset. - */ -ol.geom.flat.deflateCoordinate = - function(flatCoordinates, offset, coordinate, stride) { - goog.asserts.assert(coordinate.length == stride); - var i, ii; - for (i = 0, ii = coordinate.length; i < ii; ++i) { - flatCoordinates[offset++] = coordinate[i]; - } - return offset; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} coordinates Coordinates. - * @param {number} stride Stride. - * @return {number} offset Offset. - */ -ol.geom.flat.deflateCoordinates = - function(flatCoordinates, offset, coordinates, stride) { - var i, ii; - for (i = 0, ii = coordinates.length; i < ii; ++i) { - var coordinate = coordinates[i]; - goog.asserts.assert(coordinate.length == stride); - var j; - for (j = 0; j < stride; ++j) { - flatCoordinates[offset++] = coordinate[j]; - } - } - return offset; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} coordinatess Coordinatess. - * @param {number} stride Stride. - * @param {Array.=} opt_ends Ends. - * @return {Array.} Ends. - */ -ol.geom.flat.deflateCoordinatess = - function(flatCoordinates, offset, coordinatess, stride, opt_ends) { - var ends = goog.isDef(opt_ends) ? opt_ends : []; - var i = 0; - var j, jj; - for (j = 0, jj = coordinatess.length; j < jj; ++j) { - var end = ol.geom.flat.deflateCoordinates( - flatCoordinates, offset, coordinatess[j], stride); - ends[i++] = end; - offset = end; - } - ends.length = i; - return ends; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>>} coordinatesss Coordinatesss. - * @param {number} stride Stride. - * @param {Array.>=} opt_endss Endss. - * @return {Array.>} Endss. - */ -ol.geom.flat.deflateCoordinatesss = - function(flatCoordinates, offset, coordinatesss, stride, opt_endss) { - var endss = goog.isDef(opt_endss) ? opt_endss : []; - var i = 0; - var j, jj; - for (j = 0, jj = coordinatesss.length; j < jj; ++j) { - var ends = ol.geom.flat.deflateCoordinatess( - flatCoordinates, offset, coordinatesss[j], stride, endss[i]); - endss[i++] = ends; - offset = ends[ends.length - 1]; - } - endss.length = i; - return endss; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {Array.=} opt_dest Destination. - * @param {number=} opt_destOffset Destination offset. - * @return {Array.} Flat coordinates. - */ -ol.geom.flat.flipXY = - function(flatCoordinates, offset, end, stride, opt_dest, opt_destOffset) { - var dest, destOffset; - if (goog.isDef(opt_dest)) { - dest = opt_dest; - destOffset = goog.isDef(opt_destOffset) ? opt_destOffset : 0; - } else { - goog.asserts.assert(!goog.isDef(opt_destOffset)); - dest = []; - destOffset = 0; - } - var j, k; - for (j = offset; j < end; ) { - var x = flatCoordinates[j++]; - dest[destOffset++] = flatCoordinates[j++]; - dest[destOffset++] = x; - for (k = 2; k < stride; ++k) { - dest[destOffset++] = flatCoordinates[j++]; - } - } - dest.length = destOffset; - return dest; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {Array.=} opt_coordinates Coordinates. - * @return {Array.} Coordinates. - */ -ol.geom.flat.inflateCoordinates = - function(flatCoordinates, offset, end, stride, opt_coordinates) { - var coordinates = goog.isDef(opt_coordinates) ? opt_coordinates : []; - var i = 0; - var j; - for (j = offset; j < end; j += stride) { - coordinates[i++] = flatCoordinates.slice(j, j + stride); - } - coordinates.length = i; - return coordinates; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @param {Array.>=} opt_coordinatess Coordinatess. - * @return {Array.>} Coordinatess. - */ -ol.geom.flat.inflateCoordinatess = - function(flatCoordinates, offset, ends, stride, opt_coordinatess) { - var coordinatess = goog.isDef(opt_coordinatess) ? opt_coordinatess : []; - var i = 0; - var j, jj; - for (j = 0, jj = ends.length; j < jj; ++j) { - var end = ends[j]; - coordinatess[i++] = ol.geom.flat.inflateCoordinates( - flatCoordinates, offset, end, stride, coordinatess[i]); - offset = end; - } - coordinatess.length = i; - return coordinatess; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @param {Array.>>=} opt_coordinatesss - * Coordinatesss. - * @return {Array.>>} Coordinatesss. - */ -ol.geom.flat.inflateCoordinatesss = - function(flatCoordinates, offset, endss, stride, opt_coordinatesss) { - var coordinatesss = goog.isDef(opt_coordinatesss) ? opt_coordinatesss : []; - var i = 0; - var j, jj; - for (j = 0, jj = endss.length; j < jj; ++j) { - var ends = endss[j]; - coordinatesss[i++] = ol.geom.flat.inflateCoordinatess( - flatCoordinates, offset, ends, stride, coordinatesss[i]); - offset = ends[ends.length - 1]; - } - coordinatesss.length = i; - return coordinatesss; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {number} fraction Fraction. - * @param {Array.=} opt_dest Destination. - * @return {Array.} Destination. - */ -ol.geom.flat.lineStringInterpolate = - function(flatCoordinates, offset, end, stride, fraction, opt_dest) { - // FIXME interpolate extra dimensions - goog.asserts.assert(0 <= fraction && fraction <= 1); - var pointX = NaN; - var pointY = NaN; - var n = (end - offset) / stride; - if (n === 0) { - goog.asserts.fail(); - } else if (n == 1) { - pointX = flatCoordinates[offset]; - pointY = flatCoordinates[offset + 1]; - } else if (n == 2) { - pointX = (1 - fraction) * flatCoordinates[offset] + - fraction * flatCoordinates[offset + stride]; - pointY = (1 - fraction) * flatCoordinates[offset + 1] + - fraction * flatCoordinates[offset + stride + 1]; - } else { - var x1 = flatCoordinates[offset]; - var y1 = flatCoordinates[offset + 1]; - var length = 0; - var cumulativeLengths = [0]; - var i; - for (i = offset + stride; i < end; i += stride) { - var x2 = flatCoordinates[i]; - var y2 = flatCoordinates[i + 1]; - length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); - cumulativeLengths.push(length); - x1 = x2; - y1 = y2; - } - var target = fraction * length; - var index = goog.array.binarySearch(cumulativeLengths, target); - if (index < 0) { - var t = (target - cumulativeLengths[-index - 2]) / - (cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]); - var o = offset + (-index - 2) * stride; - pointX = (1 - t) * flatCoordinates[o] + t * flatCoordinates[o + stride]; - pointY = (1 - t) * flatCoordinates[o + 1] + - t * flatCoordinates[o + stride + 1]; - } else { - pointX = flatCoordinates[offset + index * stride]; - pointY = flatCoordinates[offset + index * stride + 1]; - } - } - if (goog.isDefAndNotNull(opt_dest)) { - opt_dest.push(pointX, pointY); - return opt_dest; - } else { - return [pointX, pointY]; - } -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {number} m M. - * @param {boolean} extrapolate Extrapolate. - * @return {ol.Coordinate} Coordinate. - */ -ol.geom.flat.lineStringCoordinateAtM = - function(flatCoordinates, offset, end, stride, m, extrapolate) { - if (end == offset) { - return null; - } - var coordinate; - if (m < flatCoordinates[offset + stride - 1]) { - if (extrapolate) { - coordinate = flatCoordinates.slice(offset, offset + stride); - coordinate[stride - 1] = m; - return coordinate; - } else { - return null; - } - } else if (flatCoordinates[end - 1] < m) { - if (extrapolate) { - coordinate = flatCoordinates.slice(end - stride, end); - coordinate[stride - 1] = m; - return coordinate; - } else { - return null; - } - } - // FIXME use O(1) search - if (m == flatCoordinates[offset + stride - 1]) { - return flatCoordinates.slice(offset, offset + stride); - } - var lo = offset / stride; - var hi = end / stride; - while (lo < hi) { - var mid = (lo + hi) >> 1; - if (m < flatCoordinates[(mid + 1) * stride - 1]) { - hi = mid; - } else { - lo = mid + 1; - } - } - var m0 = flatCoordinates[lo * stride - 1]; - if (m == m0) { - return flatCoordinates.slice((lo - 1) * stride, (lo - 1) * stride + stride); - } - var m1 = flatCoordinates[(lo + 1) * stride - 1]; - goog.asserts.assert(m0 < m); - goog.asserts.assert(m <= m1); - var t = (m - m0) / (m1 - m0); - coordinate = []; - var i; - for (i = 0; i < stride - 1; ++i) { - coordinate.push((1 - t) * flatCoordinates[(lo - 1) * stride + i] + - t * flatCoordinates[lo * stride + i]); - } - coordinate.push(m); - goog.asserts.assert(coordinate.length == stride); - return coordinate; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} 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.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {number} Length. - */ -ol.geom.flat.lineStringLength = function(flatCoordinates, offset, end, stride) { - var x1 = flatCoordinates[offset]; - var y1 = flatCoordinates[offset + 1]; - var length = 0; - var i; - for (i = offset + stride; i < end; i += stride) { - var x2 = flatCoordinates[i]; - var y2 = flatCoordinates[i + 1]; - length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); - x1 = x2; - y1 = y2; - } - return length; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingArea = 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.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {number} x X. - * @param {number} y Y. - * @return {boolean} Contains (x, y). - */ -ol.geom.flat.linearRingContainsXY = - function(flatCoordinates, offset, end, stride, x, y) { - // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html - var contains = false; - 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]; - var intersect = ((y1 > y) != (y2 > y)) && - (x < (x2 - x1) * (y - y1) / (y2 - y1) + x1); - if (intersect) { - contains = !contains; - } - x1 = x2; - y1 = y2; - } - return contains; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {boolean} Is clockwise. - */ -ol.geom.flat.linearRingIsClockwise = - function(flatCoordinates, offset, end, stride) { - // http://tinyurl.com/clockwise-method - // https://github.com/OSGeo/gdal/blob/trunk/gdal/ogr/ogrlinearring.cpp - var edge = 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]; - edge += (x2 - x1) * (y2 + y1); - x1 = x2; - y1 = y2; - } - return edge > 0; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {number} Perimeter. - */ -ol.geom.flat.linearRingPerimeter = - function(flatCoordinates, offset, end, stride) { - var perimeter = - ol.geom.flat.lineStringLength(flatCoordinates, offset, end, stride); - var dx = flatCoordinates[end - stride] - flatCoordinates[offset]; - var dy = flatCoordinates[end - stride + 1] - flatCoordinates[offset + 1]; - perimeter += Math.sqrt(dx * dx + dy * dy); - return perimeter; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingsArea = 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.linearRingArea(flatCoordinates, offset, end, stride); - offset = end; - } - return area; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @param {number} x X. - * @param {number} y Y. - * @return {boolean} Contains (x, y). - */ -ol.geom.flat.linearRingsContainsXY = - function(flatCoordinates, offset, ends, stride, x, y) { - goog.asserts.assert(ends.length > 0); - if (ends.length === 0) { - return false; - } - if (!ol.geom.flat.linearRingContainsXY( - flatCoordinates, offset, ends[0], stride, x, y)) { - return false; - } - var i, ii; - for (i = 1, ii = ends.length; i < ii; ++i) { - if (ol.geom.flat.linearRingContainsXY( - flatCoordinates, ends[i - 1], ends[i], stride, x, y)) { - return false; - } - } - return true; -}; - - -/** - * Calculates a point that is likely to lie in the interior of the linear rings. - * Inspired by JTS's com.vividsolutions.jts.geom.Geometry#getInteriorPoint. - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @param {Array.} flatCenters Flat centers. - * @param {number} flatCentersOffset Flat center offset. - * @param {Array.=} opt_dest Destination. - * @return {Array.} Destination. - */ -ol.geom.flat.linearRingsGetInteriorPoint = function(flatCoordinates, offset, - ends, stride, flatCenters, flatCentersOffset, opt_dest) { - var i, ii, x, x1, x2, y1, y2; - var y = flatCenters[flatCentersOffset + 1]; - /** @type {Array.} */ - var intersections = []; - // Calculate intersections with the horizontal line - var end = ends[0]; - x1 = flatCoordinates[end - stride]; - y1 = flatCoordinates[end - stride + 1]; - for (i = offset; i < end; i += stride) { - x2 = flatCoordinates[i]; - y2 = flatCoordinates[i + 1]; - if ((y <= y1 && y2 <= y) || (y1 <= y && y <= y2)) { - x = (y - y1) / (y2 - y1) * (x2 - x1) + x1; - intersections.push(x); - } - x1 = x2; - y1 = y2; - } - // Find the longest segment of the horizontal line that has its center point - // inside the linear ring. - var pointX = NaN; - var maxSegmentLength = -Infinity; - intersections.sort(); - x1 = intersections[0]; - for (i = 1, ii = intersections.length; i < ii; ++i) { - x2 = intersections[i]; - var segmentLength = Math.abs(x2 - x1); - if (segmentLength > maxSegmentLength) { - x = (x1 + x2) / 2; - if (ol.geom.flat.linearRingsContainsXY( - flatCoordinates, offset, ends, stride, x, y)) { - pointX = x; - maxSegmentLength = segmentLength; - } - } - x1 = x2; - } - if (isNaN(pointX)) { - // There is no horizontal line that has its center point inside the linear - // ring. Use the center of the the linear ring's extent. - pointX = flatCenters[flatCentersOffset]; - } - if (goog.isDef(opt_dest)) { - opt_dest.push(pointX, y); - return opt_dest; - } else { - return [pointX, y]; - } -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @return {boolean} `true` if all rings are correctly oriented, `false` - * otherwise. - */ -ol.geom.flat.linearRingsAreOriented = - function(flatCoordinates, offset, ends, stride) { - var i, ii; - for (i = 0, ii = ends.length; i < ii; ++i) { - var end = ends[i]; - var isClockwise = ol.geom.flat.linearRingIsClockwise( - flatCoordinates, offset, end, stride); - if (i === 0 ? !isClockwise : isClockwise) { - return false; - } - offset = end; - } - return true; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {boolean} `true` if all rings are correctly oriented, `false` - * otherwise. - */ -ol.geom.flat.linearRingssAreOriented = - function(flatCoordinates, offset, endss, stride) { - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - if (!ol.geom.flat.linearRingsAreOriented( - flatCoordinates, offset, endss[i], stride)) { - return false; - } - } - return true; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingssArea = - 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.linearRingsArea(flatCoordinates, offset, ends, stride); - offset = ends[ends.length - 1]; - } - return area; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @param {number} x X. - * @param {number} y Y. - * @return {boolean} Contains (x, y). - */ -ol.geom.flat.linearRingssContainsXY = - function(flatCoordinates, offset, endss, stride, x, y) { - goog.asserts.assert(endss.length > 0); - if (endss.length === 0) { - return false; - } - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - var ends = endss[i]; - if (ol.geom.flat.linearRingsContainsXY( - flatCoordinates, offset, ends, stride, x, y)) { - return true; - } - offset = ends[ends.length - 1]; - } - return false; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @param {Array.} flatCenters Flat centers. - * @return {Array.} Interior points. - */ -ol.geom.flat.linearRingssGetInteriorPoints = - function(flatCoordinates, offset, endss, stride, flatCenters) { - goog.asserts.assert(2 * endss.length == flatCenters.length); - var interiorPoints = []; - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - var ends = endss[i]; - interiorPoints = ol.geom.flat.linearRingsGetInteriorPoint(flatCoordinates, - offset, ends, stride, flatCenters, 2 * i, interiorPoints); - offset = ends[ends.length - 1]; - } - return interiorPoints; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {Array.} Flat centers. - */ -ol.geom.flat.linearRingssGetFlatCenters = - 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; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @return {number} End. - */ -ol.geom.flat.orientLinearRings = - function(flatCoordinates, offset, ends, stride) { - var i, ii; - for (i = 0, ii = ends.length; i < ii; ++i) { - var end = ends[i]; - var isClockwise = ol.geom.flat.linearRingIsClockwise( - flatCoordinates, offset, end, stride); - var reverse = i === 0 ? !isClockwise : isClockwise; - if (reverse) { - ol.geom.flat.reverseCoordinates(flatCoordinates, offset, end, stride); - } - offset = end; - } - return offset; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {number} End. - */ -ol.geom.flat.orientLinearRingss = - function(flatCoordinates, offset, endss, stride) { - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - offset = ol.geom.flat.orientLinearRings( - flatCoordinates, offset, endss[i], stride); - } - return offset; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - */ -ol.geom.flat.reverseCoordinates = - function(flatCoordinates, offset, end, stride) { - while (offset < end - stride) { - var i; - for (i = 0; i < stride; ++i) { - var tmp = flatCoordinates[offset + i]; - flatCoordinates[offset + i] = flatCoordinates[end - stride + i]; - flatCoordinates[end - stride + i] = tmp; - } - offset += stride; - end -= stride; - } -}; - - -/** - * Returns the square of the closest distance between the point (x, y) and the - * line segment (x1, y1) to (x2, y2). - * @param {number} x X. - * @param {number} y Y. - * @param {number} x1 X1. - * @param {number} y1 Y1. - * @param {number} x2 X2. - * @param {number} y2 Y2. - * @return {number} Squared distance. - */ -ol.geom.flat.squaredSegmentDistance = function(x, y, x1, y1, x2, y2) { - var dx = x2 - x1; - var dy = y2 - y1; - if (dx !== 0 || dy !== 0) { - var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy); - if (t > 1) { - x1 = x2; - y1 = y2; - } else if (t > 0) { - x1 += dx * t; - y1 += dy * t; - } - } - return ol.geom.flat.squaredDistance(x, y, x1, y1); -}; - - -/** - * Returns the square of the distance between the points (x1, y1) and (x2, y2). - * @param {number} x1 X1. - * @param {number} y1 Y1. - * @param {number} x2 X2. - * @param {number} y2 Y2. - * @return {number} Squared distance. - */ -ol.geom.flat.squaredDistance = function(x1, y1, x2, y2) { - var dx = x2 - x1; - var dy = y2 - y1; - return dx * dx + dy * dy; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} stride Stride. - * @param {goog.vec.Mat4.Number} transform Transform. - * @param {Array.=} opt_dest Destination. - * @return {Array.} Transformed coordinates. - */ -ol.geom.flat.transform2D = - function(flatCoordinates, stride, transform, opt_dest) { - var m00 = goog.vec.Mat4.getElement(transform, 0, 0); - var m10 = goog.vec.Mat4.getElement(transform, 1, 0); - var m01 = goog.vec.Mat4.getElement(transform, 0, 1); - var m11 = goog.vec.Mat4.getElement(transform, 1, 1); - var m03 = goog.vec.Mat4.getElement(transform, 0, 3); - var m13 = goog.vec.Mat4.getElement(transform, 1, 3); - var dest = goog.isDef(opt_dest) ? opt_dest : []; - var i = 0; - var j, jj; - for (j = 0, jj = flatCoordinates.length; j < jj; j += stride) { - var x = flatCoordinates[j]; - var y = flatCoordinates[j + 1]; - dest[i++] = m00 * x + m01 * y + m03; - dest[i++] = m10 * x + m11 * y + m13; - } - if (goog.isDef(opt_dest) && dest.length != i) { - dest.length = i; - } - return dest; -}; diff --git a/src/ol/geom/linearring.js b/src/ol/geom/linearring.js index 92d1ec19e14..389ccf561e9 100644 --- a/src/ol/geom/linearring.js +++ b/src/ol/geom/linearring.js @@ -3,9 +3,11 @@ 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.simplify'); +goog.require('ol.geom.flat.area'); +goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); +goog.require('ol.geom.flat.simplify'); @@ -58,11 +60,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); }; @@ -73,7 +75,7 @@ ol.geom.LinearRing.prototype.closestPointXY = * @todo stability experimental */ ol.geom.LinearRing.prototype.getArea = function() { - return ol.geom.flat.linearRingArea( + return ol.geom.flat.area.linearRing( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; @@ -83,7 +85,7 @@ ol.geom.LinearRing.prototype.getArea = function() { * @todo stability experimental */ ol.geom.LinearRing.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinates( + return ol.geom.flat.inflate.coordinates( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; @@ -94,7 +96,7 @@ ol.geom.LinearRing.prototype.getCoordinates = function() { ol.geom.LinearRing.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeucker( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeucker( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, squaredTolerance, simplifiedFlatCoordinates, 0); var simplifiedLinearRing = new ol.geom.LinearRing(null); @@ -126,7 +128,7 @@ ol.geom.LinearRing.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - this.flatCoordinates.length = ol.geom.flat.deflateCoordinates( + this.flatCoordinates.length = ol.geom.flat.deflate.coordinates( this.flatCoordinates, 0, coordinates, this.stride); this.dispatchChangeEvent(); } diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 8ac6ec912e1..83bed93c143 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -5,9 +5,12 @@ 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.simplify'); +goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); +goog.require('ol.geom.flat.interpolate'); +goog.require('ol.geom.flat.length'); +goog.require('ol.geom.flat.simplify'); @@ -86,11 +89,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); }; @@ -125,7 +128,7 @@ ol.geom.LineString.prototype.getCoordinateAtM = function(m, opt_extrapolate) { * @todo stability experimental */ ol.geom.LineString.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinates( + return ol.geom.flat.inflate.coordinates( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; @@ -135,7 +138,7 @@ ol.geom.LineString.prototype.getCoordinates = function() { * @todo stability experimental */ ol.geom.LineString.prototype.getLength = function() { - return ol.geom.flat.lineStringLength( + return ol.geom.flat.length.lineString( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; @@ -145,7 +148,7 @@ ol.geom.LineString.prototype.getLength = function() { */ ol.geom.LineString.prototype.getFlatMidpoint = function() { if (this.flatMidpointRevision_ != this.getRevision()) { - this.flatMidpoint_ = ol.geom.flat.lineStringInterpolate( + this.flatMidpoint_ = ol.geom.flat.interpolate.lineString( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, 0.5, this.flatMidpoint_); this.flatMidpointRevision_ = this.getRevision(); @@ -160,7 +163,7 @@ ol.geom.LineString.prototype.getFlatMidpoint = function() { ol.geom.LineString.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeucker( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeucker( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, squaredTolerance, simplifiedFlatCoordinates, 0); var simplifiedLineString = new ol.geom.LineString(null); @@ -192,7 +195,7 @@ ol.geom.LineString.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - this.flatCoordinates.length = ol.geom.flat.deflateCoordinates( + this.flatCoordinates.length = ol.geom.flat.deflate.coordinates( this.flatCoordinates, 0, coordinates, this.stride); this.dispatchChangeEvent(); } diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index 816abef889b..a415be1ed21 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -6,9 +6,11 @@ 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.simplify'); +goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); +goog.require('ol.geom.flat.interpolate'); +goog.require('ol.geom.flat.simplify'); @@ -84,11 +86,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); }; @@ -134,7 +136,7 @@ ol.geom.MultiLineString.prototype.getCoordinateAtM = * @todo stability experimental */ ol.geom.MultiLineString.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinatess( + return ol.geom.flat.inflate.coordinatess( this.flatCoordinates, 0, this.ends_, this.stride); }; @@ -198,7 +200,7 @@ ol.geom.MultiLineString.prototype.getFlatMidpoints = function() { var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - var midpoint = ol.geom.flat.lineStringInterpolate( + var midpoint = ol.geom.flat.interpolate.lineString( flatCoordinates, offset, end, stride, 0.5); goog.array.extend(midpoints, midpoint); offset = end; @@ -214,7 +216,7 @@ ol.geom.MultiLineString.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; var simplifiedEnds = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeuckers( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeuckers( this.flatCoordinates, 0, this.ends_, this.stride, squaredTolerance, simplifiedFlatCoordinates, 0, simplifiedEnds); var simplifiedMultiLineString = new ol.geom.MultiLineString(null); @@ -246,7 +248,7 @@ ol.geom.MultiLineString.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - var ends = ol.geom.flat.deflateCoordinatess( + var ends = ol.geom.flat.deflate.coordinatess( this.flatCoordinates, 0, coordinates, this.stride, this.ends_); this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1]; this.dispatchChangeEvent(); diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index 931761f3504..54c7a8e1702 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -6,7 +6,9 @@ goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); +goog.require('ol.math'); @@ -61,7 +63,7 @@ ol.geom.MultiPoint.prototype.closestPointXY = var stride = this.stride; var i, ii, j; for (i = 0, ii = flatCoordinates.length; i < ii; i += stride) { - var squaredDistance = ol.geom.flat.squaredDistance( + var squaredDistance = ol.math.squaredDistance( x, y, flatCoordinates[i], flatCoordinates[i + 1]); if (squaredDistance < minSquaredDistance) { minSquaredDistance = squaredDistance; @@ -80,7 +82,7 @@ ol.geom.MultiPoint.prototype.closestPointXY = * @todo stability experimental */ ol.geom.MultiPoint.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinates( + return ol.geom.flat.inflate.coordinates( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; @@ -145,7 +147,7 @@ ol.geom.MultiPoint.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - this.flatCoordinates.length = ol.geom.flat.deflateCoordinates( + this.flatCoordinates.length = ol.geom.flat.deflate.coordinates( this.flatCoordinates, 0, coordinates, this.stride); this.dispatchChangeEvent(); } diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index acd04669da0..bf694bc4603 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -7,9 +7,15 @@ 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.simplify'); +goog.require('ol.geom.flat.area'); +goog.require('ol.geom.flat.center'); +goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.contains'); +goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); +goog.require('ol.geom.flat.interiorpoint'); +goog.require('ol.geom.flat.orient'); +goog.require('ol.geom.flat.simplify'); @@ -118,11 +124,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); }; @@ -132,7 +138,7 @@ ol.geom.MultiPolygon.prototype.closestPointXY = * @inheritDoc */ ol.geom.MultiPolygon.prototype.containsXY = function(x, y) { - return ol.geom.flat.linearRingssContainsXY( + return ol.geom.flat.contains.linearRingssContainsXY( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride, x, y); }; @@ -142,7 +148,7 @@ ol.geom.MultiPolygon.prototype.containsXY = function(x, y) { * @todo stability experimental */ ol.geom.MultiPolygon.prototype.getArea = function() { - return ol.geom.flat.linearRingssArea( + return ol.geom.flat.area.linearRingss( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride); }; @@ -152,7 +158,7 @@ ol.geom.MultiPolygon.prototype.getArea = function() { * @todo stability experimental */ ol.geom.MultiPolygon.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinatesss( + return ol.geom.flat.inflate.coordinatesss( this.flatCoordinates, 0, this.endss_, this.stride); }; @@ -170,9 +176,9 @@ ol.geom.MultiPolygon.prototype.getEndss = function() { */ ol.geom.MultiPolygon.prototype.getFlatInteriorPoints = function() { if (this.flatInteriorPointsRevision_ != this.getRevision()) { - var flatCenters = ol.geom.flat.linearRingssGetFlatCenters( + var flatCenters = ol.geom.flat.center.linearRingss( this.flatCoordinates, 0, this.endss_, this.stride); - this.flatInteriorPoints_ = ol.geom.flat.linearRingssGetInteriorPoints( + this.flatInteriorPoints_ = ol.geom.flat.interiorpoint.linearRingss( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride, flatCenters); this.flatInteriorPointsRevision_ = this.getRevision(); @@ -203,8 +209,9 @@ ol.geom.MultiPolygon.prototype.getOrientedFlatCoordinates = function() { this.orientedFlatCoordinates_ = flatCoordinates; } else { this.orientedFlatCoordinates_ = flatCoordinates.slice(); - this.orientedFlatCoordinates_.length = ol.geom.flat.orientLinearRingss( - this.orientedFlatCoordinates_, 0, this.endss_, this.stride); + this.orientedFlatCoordinates_.length = + ol.geom.flat.orient.orientLinearRingss( + this.orientedFlatCoordinates_, 0, this.endss_, this.stride); } this.orientedRevision_ = this.getRevision(); } @@ -219,7 +226,7 @@ ol.geom.MultiPolygon.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; var simplifiedEndss = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.quantizess( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.quantizess( this.flatCoordinates, 0, this.endss_, this.stride, Math.sqrt(squaredTolerance), simplifiedFlatCoordinates, 0, simplifiedEndss); @@ -312,7 +319,7 @@ ol.geom.MultiPolygon.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - var endss = ol.geom.flat.deflateCoordinatesss( + var endss = ol.geom.flat.deflate.coordinatesss( this.flatCoordinates, 0, coordinates, this.stride, this.endss_); var lastEnds = endss[endss.length - 1]; this.flatCoordinates.length = lastEnds.length === 0 ? diff --git a/src/ol/geom/point.js b/src/ol/geom/point.js index 3e32ef01cbc..19275c48f44 100644 --- a/src/ol/geom/point.js +++ b/src/ol/geom/point.js @@ -4,7 +4,8 @@ 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'); +goog.require('ol.math'); @@ -38,7 +39,7 @@ ol.geom.Point.prototype.clone = function() { ol.geom.Point.prototype.closestPointXY = function(x, y, closestPoint, minSquaredDistance) { var flatCoordinates = this.flatCoordinates; - var squaredDistance = ol.geom.flat.squaredDistance( + var squaredDistance = ol.math.squaredDistance( x, y, flatCoordinates[0], flatCoordinates[1]); if (squaredDistance < minSquaredDistance) { var stride = this.stride; @@ -98,7 +99,7 @@ ol.geom.Point.prototype.setCoordinates = function(coordinates, opt_layout) { if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - this.flatCoordinates.length = ol.geom.flat.deflateCoordinate( + this.flatCoordinates.length = ol.geom.flat.deflate.coordinate( this.flatCoordinates, 0, coordinates, this.stride); this.dispatchChangeEvent(); } diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 1cd2d1fd4e1..1ab9f138181 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -7,9 +7,14 @@ 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.simplify'); +goog.require('ol.geom.flat.area'); +goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.contains'); +goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); +goog.require('ol.geom.flat.interiorpoint'); +goog.require('ol.geom.flat.orient'); +goog.require('ol.geom.flat.simplify'); @@ -108,11 +113,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); }; @@ -122,7 +127,7 @@ ol.geom.Polygon.prototype.closestPointXY = * @inheritDoc */ ol.geom.Polygon.prototype.containsXY = function(x, y) { - return ol.geom.flat.linearRingsContainsXY( + return ol.geom.flat.contains.linearRingsContainsXY( this.getOrientedFlatCoordinates(), 0, this.ends_, this.stride, x, y); }; @@ -132,7 +137,7 @@ ol.geom.Polygon.prototype.containsXY = function(x, y) { * @todo stability experimental */ ol.geom.Polygon.prototype.getArea = function() { - return ol.geom.flat.linearRingsArea( + return ol.geom.flat.area.linearRings( this.getOrientedFlatCoordinates(), 0, this.ends_, this.stride); }; @@ -142,7 +147,7 @@ ol.geom.Polygon.prototype.getArea = function() { * @todo stability experimental */ ol.geom.Polygon.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinatess( + return ol.geom.flat.inflate.coordinatess( this.flatCoordinates, 0, this.ends_, this.stride); }; @@ -161,7 +166,7 @@ ol.geom.Polygon.prototype.getEnds = function() { ol.geom.Polygon.prototype.getFlatInteriorPoint = function() { if (this.flatInteriorPointRevision_ != this.getRevision()) { var flatCenter = ol.extent.getCenter(this.getExtent()); - this.flatInteriorPoint_ = ol.geom.flat.linearRingsGetInteriorPoint( + this.flatInteriorPoint_ = ol.geom.flat.interiorpoint.linearRings( this.getOrientedFlatCoordinates(), 0, this.ends_, this.stride, flatCenter, 0); this.flatInteriorPointRevision_ = this.getRevision(); @@ -222,13 +227,14 @@ ol.geom.Polygon.prototype.getLinearRings = function() { ol.geom.Polygon.prototype.getOrientedFlatCoordinates = function() { if (this.orientedRevision_ != this.getRevision()) { var flatCoordinates = this.flatCoordinates; - if (ol.geom.flat.linearRingsAreOriented( + if (ol.geom.flat.orient.linearRingsAreOriented( flatCoordinates, 0, this.ends_, this.stride)) { this.orientedFlatCoordinates_ = flatCoordinates; } else { this.orientedFlatCoordinates_ = flatCoordinates.slice(); - this.orientedFlatCoordinates_.length = ol.geom.flat.orientLinearRings( - this.orientedFlatCoordinates_, 0, this.ends_, this.stride); + this.orientedFlatCoordinates_.length = + ol.geom.flat.orient.orientLinearRings( + this.orientedFlatCoordinates_, 0, this.ends_, this.stride); } this.orientedRevision_ = this.getRevision(); } @@ -243,7 +249,7 @@ ol.geom.Polygon.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; var simplifiedEnds = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.quantizes( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.quantizes( this.flatCoordinates, 0, this.ends_, this.stride, Math.sqrt(squaredTolerance), simplifiedFlatCoordinates, 0, simplifiedEnds); @@ -275,7 +281,7 @@ ol.geom.Polygon.prototype.setCoordinates = function(coordinates, opt_layout) { if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - var ends = ol.geom.flat.deflateCoordinatess( + var ends = ol.geom.flat.deflate.coordinatess( this.flatCoordinates, 0, coordinates, this.stride, this.ends_); this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1]; this.dispatchChangeEvent(); diff --git a/src/ol/geom/simplegeometry.js b/src/ol/geom/simplegeometry.js index 879a84f2e91..e1841008d31 100644 --- a/src/ol/geom/simplegeometry.js +++ b/src/ol/geom/simplegeometry.js @@ -5,7 +5,7 @@ goog.require('goog.functions'); goog.require('goog.object'); goog.require('ol.extent'); goog.require('ol.geom.Geometry'); -goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.transform'); @@ -260,7 +260,7 @@ ol.geom.transformSimpleGeometry2D = return null; } else { var stride = simpleGeometry.getStride(); - return ol.geom.flat.transform2D( + return ol.geom.flat.transform.transform2D( flatCoordinates, stride, transform, opt_dest); } }; diff --git a/src/ol/math.js b/src/ol/math.js index 14ffd2977aa..70c46df4c63 100644 --- a/src/ol/math.js +++ b/src/ol/math.js @@ -59,6 +59,49 @@ ol.math.sinh = function(x) { }; +/** + * Returns the square of the closest distance between the point (x, y) and the + * line segment (x1, y1) to (x2, y2). + * @param {number} x X. + * @param {number} y Y. + * @param {number} x1 X1. + * @param {number} y1 Y1. + * @param {number} x2 X2. + * @param {number} y2 Y2. + * @return {number} Squared distance. + */ +ol.math.squaredSegmentDistance = function(x, y, x1, y1, x2, y2) { + var dx = x2 - x1; + var dy = y2 - y1; + if (dx !== 0 || dy !== 0) { + var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy); + if (t > 1) { + x1 = x2; + y1 = y2; + } else if (t > 0) { + x1 += dx * t; + y1 += dy * t; + } + } + return ol.math.squaredDistance(x, y, x1, y1); +}; + + +/** + * Returns the square of the distance between the points (x1, y1) and (x2, y2). + * @param {number} x1 X1. + * @param {number} y1 Y1. + * @param {number} x2 X2. + * @param {number} y2 Y2. + * @return {number} Squared distance. + */ +ol.math.squaredDistance = function(x1, y1, x2, y2) { + var dx = x2 - x1; + var dy = y2 - y1; + return dx * dx + dy * dy; +}; + + /** * @param {number} x X. * @return {number} Hyperbolic tangent of x. diff --git a/src/ol/render/canvas/canvasimmediate.js b/src/ol/render/canvas/canvasimmediate.js index ed1efeb6a77..2abdb6e2e5b 100644 --- a/src/ol/render/canvas/canvasimmediate.js +++ b/src/ol/render/canvas/canvasimmediate.js @@ -1,6 +1,6 @@ // FIXME test, especially polygons with holes and multipolygons // FIXME need to handle large thick features (where pixel size matters) -// FIXME add offset and end to ol.geom.flat.transform2D? +// FIXME add offset and end to ol.geom.flat.transform.transform2D? goog.provide('ol.render.canvas.Immediate'); @@ -11,7 +11,7 @@ goog.require('goog.vec.Mat4'); goog.require('ol.BrowserFeature'); goog.require('ol.color'); goog.require('ol.extent'); -goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.transform'); goog.require('ol.render.IVectorContext'); goog.require('ol.render.canvas'); goog.require('ol.vec.Mat4'); @@ -235,7 +235,7 @@ ol.render.canvas.Immediate.prototype.drawImages_ = } goog.asserts.assert(offset === 0); goog.asserts.assert(end == flatCoordinates.length); - var pixelCoordinates = ol.geom.flat.transform2D( + var pixelCoordinates = ol.geom.flat.transform.transform2D( flatCoordinates, 2, this.transform_, this.pixelCoordinates_); var context = this.context_; var localTransform = this.tmpLocalTransform_; @@ -301,7 +301,7 @@ ol.render.canvas.Immediate.prototype.drawText_ = this.setContextTextState_(this.textState_); goog.asserts.assert(offset === 0); goog.asserts.assert(end == flatCoordinates.length); - var pixelCoordinates = ol.geom.flat.transform2D( + var pixelCoordinates = ol.geom.flat.transform.transform2D( flatCoordinates, stride, this.transform_, this.pixelCoordinates_); var context = this.context_; for (; offset < end; offset += stride) { diff --git a/src/ol/render/canvas/canvasreplay.js b/src/ol/render/canvas/canvasreplay.js index 5f89a901cbe..47b9e4e2be8 100644 --- a/src/ol/render/canvas/canvasreplay.js +++ b/src/ol/render/canvas/canvasreplay.js @@ -16,8 +16,8 @@ goog.require('ol.array'); goog.require('ol.color'); goog.require('ol.extent'); goog.require('ol.extent.Relationship'); -goog.require('ol.geom.flat'); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat.simplify'); +goog.require('ol.geom.flat.transform'); goog.require('ol.render.IReplayGroup'); goog.require('ol.render.IVectorContext'); goog.require('ol.render.canvas'); @@ -217,7 +217,7 @@ ol.render.canvas.Replay.prototype.replay_ = function( if (ol.vec.Mat4.equals2D(transform, this.renderedTransform_)) { pixelCoordinates = this.pixelCoordinates_; } else { - pixelCoordinates = ol.geom.flat.transform2D( + pixelCoordinates = ol.geom.flat.transform.transform2D( this.coordinates, 2, transform, this.pixelCoordinates_); goog.vec.Mat4.setFromArray(this.renderedTransform_, transform); goog.asserts.assert(pixelCoordinates === this.pixelCoordinates_); @@ -1310,7 +1310,7 @@ ol.render.canvas.PolygonReplay.prototype.finish = function() { var coordinates = this.coordinates; var i, ii; for (i = 0, ii = coordinates.length; i < ii; ++i) { - coordinates[i] = ol.geom.simplify.snap(coordinates[i], tolerance); + coordinates[i] = ol.geom.flat.simplify.snap(coordinates[i], tolerance); } } }; @@ -1852,7 +1852,7 @@ ol.render.canvas.ReplayGroup.prototype.replay_ = function( var minY = maxExtent[1]; var maxX = maxExtent[2]; var maxY = maxExtent[3]; - var flatClipCoords = ol.geom.flat.transform2D( + var flatClipCoords = ol.geom.flat.transform.transform2D( [minX, minY, minX, maxY, maxX, maxY, maxX, minY], 2, transform); context.save(); context.beginPath(); diff --git a/test/spec/ol/geom/flat/areaflatgeom.test.js b/test/spec/ol/geom/flat/areaflatgeom.test.js new file mode 100644 index 00000000000..afd0b8abdf8 --- /dev/null +++ b/test/spec/ol/geom/flat/areaflatgeom.test.js @@ -0,0 +1,32 @@ +goog.provide('ol.test.geom.flat.area'); + +describe('ol.geom.flat.area', function() { + + describe('ol.geom.flat.area.linearRing', function() { + + it('calcaultes the area of a triangle', function() { + var area = ol.geom.flat.area.linearRing([0, 0, 0.5, 1, 1, 0], 0, 6, 2); + expect(area).to.be(0.5); + }); + + it('calculates the area of a unit square', function() { + var area = + ol.geom.flat.area.linearRing([0, 0, 0, 1, 1, 1, 1, 0], 0, 8, 2); + expect(area).to.be(1); + }); + + }); + + describe('ol.geom.flat.area.linearRings', function() { + + it('calculates the area with holes', function() { + var area = ol.geom.flat.area.linearRings( + [0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2], 0, [8, 16], 2); + expect(area).to.be(8); + }); + + }); + +}); + +goog.require('ol.geom.flat.area'); diff --git a/test/spec/ol/geom/closestgeom.test.js b/test/spec/ol/geom/flat/closestflatgeom.test.js similarity index 81% rename from test/spec/ol/geom/closestgeom.test.js rename to test/spec/ol/geom/flat/closestflatgeom.test.js index 124b5afde69..e583ed0c1fc 100644 --- a/test/spec/ol/geom/closestgeom.test.js +++ b/test/spec/ol/geom/flat/closestflatgeom.test.js @@ -1,41 +1,41 @@ -goog.provide('ol.test.geom.closest'); +goog.provide('ol.test.geom.flat.closest'); -describe('ol.geom.closest', function() { +describe('ol.geom.flat.closest', function() { describe('with simple data', function() { var flatCoordinates = [0, 0, 1, 0, 3, 0, 5, 0, 6, 0, 8, 0, 11, 0]; - describe('ol.geom.closest.getMaxSquaredDelta', function() { + describe('ol.geom.flat.closest.getMaxSquaredDelta', function() { it('returns the expected value in simple cases', function() { - expect(ol.geom.closest.getMaxSquaredDelta( + expect(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, 2, 0)).to.be(9); }); }); - describe('ol.geom.closest.getClosestPoint', function() { + describe('ol.geom.flat.closest.getClosestPoint', function() { it('returns the expected value', function() { - var maxDelta = Math.sqrt(ol.geom.closest.getMaxSquaredDelta( + var maxDelta = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, 2, 0)); expect(maxDelta).to.be(3); var closestPoint = [NaN, NaN]; - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 0, 0, closestPoint, Infinity)).to.be(0); expect(closestPoint).to.eql([0, 0]); - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 4, 1, closestPoint, Infinity)).to.be(1); expect(closestPoint).to.eql([4, 0]); - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 5, 2, closestPoint, Infinity)).to.be(4); expect(closestPoint).to.eql([5, 0]); - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 10, 100, closestPoint, Infinity)).to.be(10000); expect(closestPoint).to.eql([10, 0]); @@ -78,31 +78,31 @@ describe('ol.geom.closest', function() { describe('ol.geom.closet.maSquaredDelta', function() { it('returns the expected value', function() { - expect(ol.geom.closest.getMaxSquaredDelta( + expect(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, 2, 0)). to.roughlyEqual(1389.1058, 1e-9); }); }); - describe('ol.geom.closest.getClosestPoint', function() { + describe('ol.geom.flat.closest.getClosestPoint', function() { it('returns the expected value', function() { - var maxDelta = Math.sqrt(ol.geom.closest.getMaxSquaredDelta( + var maxDelta = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, 2, 0)); expect(maxDelta).to.roughlyEqual(Math.sqrt(1389.1058), 1e-9); var closestPoint = [NaN, NaN]; - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( 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.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( 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.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 1000, 500, closestPoint, Infinity)). to.roughlyEqual(18229.4425, 1e-9); @@ -118,14 +118,14 @@ describe('ol.geom.closest', function() { var flatCoordinates = [0, 0, 10, -10, 2, 2, 30, -20]; var stride = 4; - describe('ol.geom.closest.getClosestPoint', function() { + describe('ol.geom.flat.closest.getClosestPoint', function() { it('interpolates M coordinates', function() { - var maxDelta = Math.sqrt(ol.geom.closest.getMaxSquaredDelta( + var maxDelta = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, stride, 0)); expect(maxDelta).to.roughlyEqual(Math.sqrt(8), 1e-9); var closestPoint = [NaN, NaN]; - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, stride, maxDelta, false, 1, 1, closestPoint, Infinity)). to.roughlyEqual(0, 1e-9); @@ -143,4 +143,4 @@ describe('ol.geom.closest', function() { }); -goog.require('ol.geom.closest'); +goog.require('ol.geom.flat.closest'); diff --git a/test/spec/ol/geom/flat/deflateflatgeom.test.js b/test/spec/ol/geom/flat/deflateflatgeom.test.js new file mode 100644 index 00000000000..75ff97e7dd4 --- /dev/null +++ b/test/spec/ol/geom/flat/deflateflatgeom.test.js @@ -0,0 +1,39 @@ +goog.provide('ol.test.geom.flat.deflate'); + +describe('ol.geom.flat.deflate', function() { + + describe('ol.geom.flat.deflate.coordinates', function() { + + var flatCoordinates; + beforeEach(function() { + flatCoordinates = []; + }); + + it('flattens coordinates', function() { + var offset = ol.geom.flat.deflate.coordinates( + flatCoordinates, 0, [[1, 2], [3, 4]], 2); + expect(offset).to.be(4); + expect(flatCoordinates).to.eql([1, 2, 3, 4]); + }); + + }); + + describe('ol.geom.flat.deflate.coordinatess', function() { + + var flatCoordinates; + beforeEach(function() { + flatCoordinates = []; + }); + + it('flattens arrays of coordinates', function() { + var ends = ol.geom.flat.deflate.coordinatess(flatCoordinates, 0, + [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], 2); + expect(ends).to.eql([4, 8]); + expect(flatCoordinates).to.eql([1, 2, 3, 4, 5, 6, 7, 8]); + }); + + }); + +}); + +goog.require('ol.geom.flat.deflate'); diff --git a/test/spec/ol/geom/flat/flipflatgeom.test.js b/test/spec/ol/geom/flat/flipflatgeom.test.js new file mode 100644 index 00000000000..ec5da639624 --- /dev/null +++ b/test/spec/ol/geom/flat/flipflatgeom.test.js @@ -0,0 +1,38 @@ +goog.provide('ol.test.geom.flat.flip'); + +describe('ol.geom.flat.flip', function() { + + describe('ol.geom.flat.flip.flipXY', function() { + + it('can flip XY coordinates', function() { + var flatCoordinates = ol.geom.flat.flip.flipXY([1, 2, 3, 4], 0, 4, 2); + expect(flatCoordinates).to.eql([2, 1, 4, 3]); + }); + + it('can flip XY coordinates while preserving other dimensions', function() { + var flatCoordinates = ol.geom.flat.flip.flipXY( + [1, 2, 3, 4, 5, 6, 7, 8], 0, 8, 4); + expect(flatCoordinates).to.eql([2, 1, 3, 4, 6, 5, 7, 8]); + }); + + it('can flip XY coordinates in place', function() { + var flatCoordinates = [1, 2, 3, 4]; + expect(ol.geom.flat.flip.flipXY( + flatCoordinates, 0, 4, 2, flatCoordinates)).to.be(flatCoordinates); + expect(flatCoordinates).to.eql([2, 1, 4, 3]); + }); + + it('can flip XY coordinates in place while preserving other dimensions', + function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9]; + expect(ol.geom.flat.flip.flipXY( + flatCoordinates, 0, 9, 3, flatCoordinates)). + to.be(flatCoordinates); + expect(flatCoordinates).to.eql([2, 1, 3, 5, 4, 6, 8, 7, 9]); + }); + + }); + +}); + +goog.require('ol.geom.flat.flip'); diff --git a/test/spec/ol/geom/flat/inflateflatgeom.test.js b/test/spec/ol/geom/flat/inflateflatgeom.test.js new file mode 100644 index 00000000000..478aecd0345 --- /dev/null +++ b/test/spec/ol/geom/flat/inflateflatgeom.test.js @@ -0,0 +1,26 @@ +goog.provide('ol.test.geom.flat.inflate'); + +describe('ol.geom.flat.inflate', function() { + + describe('ol.geom.flat.inflate.coordinates', function() { + + it('inflates coordinates', function() { + var coordinates = ol.geom.flat.inflate.coordinates([1, 2, 3, 4], 0, 4, 2); + expect(coordinates).to.eql([[1, 2], [3, 4]]); + }); + + }); + + describe('ol.geom.flat.inflate.coordinatess', function() { + + it('inflates arrays of coordinates', function() { + var coordinatess = ol.geom.flat.inflate.coordinatess( + [1, 2, 3, 4, 5, 6, 7, 8], 0, [4, 8], 2); + expect(coordinatess).to.eql([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); + }); + + }); + +}); + +goog.require('ol.geom.flat.inflate'); diff --git a/test/spec/ol/geom/flat/interpolateflatgeom.test.js b/test/spec/ol/geom/flat/interpolateflatgeom.test.js new file mode 100644 index 00000000000..13cdf9f0cac --- /dev/null +++ b/test/spec/ol/geom/flat/interpolateflatgeom.test.js @@ -0,0 +1,49 @@ +goog.provide('ol.test.geom.flat.interpolate'); + +describe('ol.geom.flat.interpolate', function() { + + describe('ol.geom.flat.interpolate.lineString', function() { + + it('returns the expected value for single points', function() { + var flatCoordinates = [0, 1]; + var point = + ol.geom.flat.interpolate.lineString(flatCoordinates, 0, 2, 2, 0.5); + expect(point).to.eql([0, 1]); + }); + + it('returns the expected value for simple line segments', function() { + var flatCoordinates = [0, 1, 2, 3]; + var point = + ol.geom.flat.interpolate.lineString(flatCoordinates, 0, 4, 2, 0.5); + expect(point).to.eql([1, 2]); + }); + + it('returns the expected value when the mid point is an existing ' + + 'coordinate', function() { + var flatCoordinates = [0, 1, 2, 3, 4, 5]; + var point = ol.geom.flat.interpolate.lineString( + flatCoordinates, 0, 6, 2, 0.5); + expect(point).to.eql([2, 3]); + }); + + it('returns the expected value when the midpoint falls halfway between ' + + 'two existing coordinates', function() { + var flatCoordinates = [0, 1, 2, 3, 4, 5, 6, 7]; + var point = ol.geom.flat.interpolate.lineString( + flatCoordinates, 0, 8, 2, 0.5); + expect(point).to.eql([3, 4]); + }); + + it('returns the expected value when the coordinates are not evenly spaced', + function() { + var flatCoordinates = [0, 1, 2, 3, 6, 7]; + var point = ol.geom.flat.interpolate.lineString( + flatCoordinates, 0, 6, 2, 0.5); + expect(point).to.eql([3, 4]); + }); + + }); + +}); + +goog.require('ol.geom.flat.interpolate'); diff --git a/test/spec/ol/geom/flat/orientflatgeom.test.js b/test/spec/ol/geom/flat/orientflatgeom.test.js new file mode 100644 index 00000000000..c8842eb5b20 --- /dev/null +++ b/test/spec/ol/geom/flat/orientflatgeom.test.js @@ -0,0 +1,25 @@ +goog.provide('ol.test.geom.flat.orient'); + +describe('ol.geom.flat.orient', function() { + + describe('ol.geom.flat.orient.linearRingIsClockwise', function() { + + it('identifies clockwise rings', function() { + var flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0]; + var isClockwise = ol.geom.flat.orient.linearRingIsClockwise( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(isClockwise).to.be(true); + }); + + it('identifies anti-clockwise rings', function() { + var flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3]; + var isClockwise = ol.geom.flat.orient.linearRingIsClockwise( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(isClockwise).to.be(false); + }); + + }); + +}); + +goog.require('ol.geom.flat.orient'); diff --git a/test/spec/ol/geom/flat/reverseflatgeom.js b/test/spec/ol/geom/flat/reverseflatgeom.js new file mode 100644 index 00000000000..a2c8fa0038a --- /dev/null +++ b/test/spec/ol/geom/flat/reverseflatgeom.js @@ -0,0 +1,130 @@ +goog.provide('ol.test.geom.flat.reverse'); + +describe('ol.geom.flat.reverse', function() { + + describe('ol.geom.flat.reverse.coordinates', function() { + + describe('with a stride of 2', function() { + + it('can reverse empty flat coordinates', function() { + var flatCoordinates = []; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.be.empty(); + }); + + it('can reverse one flat coordinates', function() { + var flatCoordinates = [1, 2]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.eql([1, 2]); + }); + + it('can reverse two flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.eql([3, 4, 1, 2]); + }); + + it('can reverse three flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.eql([5, 6, 3, 4, 1, 2]); + }); + + it('can reverse four flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.eql([7, 8, 5, 6, 3, 4, 1, 2]); + }); + + }); + + describe('with a stride of 3', function() { + + it('can reverse empty flat coordinates', function() { + var flatCoordinates = []; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.be.empty(); + }); + + it('can reverse one flat coordinates', function() { + var flatCoordinates = [1, 2, 3]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.eql([1, 2, 3]); + }); + + it('can reverse two flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.eql([4, 5, 6, 1, 2, 3]); + }); + + it('can reverse three flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.eql([7, 8, 9, 4, 5, 6, 1, 2, 3]); + }); + + it('can reverse four flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.eql([10, 11, 12, 7, 8, 9, 4, 5, 6, 1, 2, 3]); + }); + + }); + + describe('with a stride of 4', function() { + + it('can reverse empty flat coordinates', function() { + var flatCoordinates = []; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.be.empty(); + }); + + it('can reverse one flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.eql([1, 2, 3, 4]); + }); + + it('can reverse two flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.eql([5, 6, 7, 8, 1, 2, 3, 4]); + }); + + it('can reverse three flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.eql([9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]); + }); + + it('can reverse four flat coordinates', function() { + var flatCoordinates = + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.eql( + [13, 14, 15, 16, 9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]); + }); + + }); + + }); + +}); + +goog.require('ol.geom.flat.reverse'); diff --git a/test/spec/ol/geom/simplifygeom.test.js b/test/spec/ol/geom/flat/simplifyflatgeom.test.js similarity index 84% rename from test/spec/ol/geom/simplifygeom.test.js rename to test/spec/ol/geom/flat/simplifyflatgeom.test.js index a59f1283042..abc4a12b7db 100644 --- a/test/spec/ol/geom/simplifygeom.test.js +++ b/test/spec/ol/geom/flat/simplifyflatgeom.test.js @@ -1,7 +1,7 @@ goog.provide('ol.test.geom.simplify'); -describe('ol.geom.simplify', function() { +describe('ol.geom.flat.simplify', function() { var flatCoordinates = [ 224.55, 250.15, 226.91, 244.19, 233.31, 241.45, 234.98, 236.06, @@ -81,31 +81,31 @@ describe('ol.geom.simplify', function() { 866.36, 480.77 ]; - describe('ol.geom.simplify.lineString', function() { + describe('ol.geom.flat.simplify.lineString', function() { it('works with empty line strings', function() { - expect(ol.geom.simplify.lineString([], 0, 0, 2, 1, true)).to. + expect(ol.geom.flat.simplify.lineString([], 0, 0, 2, 1, true)).to. eql([]); - expect(ol.geom.simplify.lineString([], 0, 0, 2, 1, false)).to. + expect(ol.geom.flat.simplify.lineString([], 0, 0, 2, 1, false)).to. eql([]); }); it('works with a line string with a single point', function() { - expect(ol.geom.simplify.lineString([1, 2], 0, 2, 2, 1, true)).to. + expect(ol.geom.flat.simplify.lineString([1, 2], 0, 2, 2, 1, true)).to. eql([1, 2]); - expect(ol.geom.simplify.lineString([1, 2], 0, 2, 2, 1, false)).to. + expect(ol.geom.flat.simplify.lineString([1, 2], 0, 2, 2, 1, false)).to. eql([1, 2]); }); it('returns the expected result with low quality', function() { - var result = ol.geom.simplify.lineString( + var result = ol.geom.flat.simplify.lineString( flatCoordinates, 0, flatCoordinates.length, 2, 25, false); expect(result.length).to.be(simplifiedFlatCoordinates.length); expect(result).to.eql(simplifiedFlatCoordinates); }); it('returns the expected result with high quality', function() { - var result = ol.geom.simplify.lineString( + var result = ol.geom.flat.simplify.lineString( flatCoordinates, 0, flatCoordinates.length, 2, 25, true); expect(result.length).to.be(simplifiedHighQualityFlatCoordinates.length); expect(result).to.eql(simplifiedHighQualityFlatCoordinates); @@ -113,7 +113,7 @@ describe('ol.geom.simplify', function() { }); - describe('ol.geom.simplify.radialDistance', function() { + describe('ol.geom.flat.simplify.radialDistance', function() { var dest; beforeEach(function() { @@ -121,63 +121,63 @@ describe('ol.geom.simplify', function() { }); it('works with empty line strings', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [], 0, 0, 2, 1, dest, 0)).to.be(0); expect(dest).to.eql([]); }); it('works with a line string with a single point', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [1, 2], 0, 2, 2, 1, dest, 0)).to.be(2); expect(dest).to.eql([1, 2]); }); it('works with a line string with two points', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [1, 2, 3, 4], 0, 4, 2, 1, dest, 0)).to.be(4); expect(dest).to.eql([1, 2, 3, 4]); }); it('works when the points are widely spaced', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 0.5, dest, 0)).to.be(8); expect(dest).to.eql([0, 0, 1, 0, 2, 0, 3, 0]); }); it('works when the spacing matches the tolerance', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1, dest, 0)).to.be(6); expect(dest).to.eql([0, 0, 2, 0, 3, 0]); }); it('works when the points are closely spaced', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1.5, dest, 0)).to.be(6); expect(dest).to.eql([0, 0, 2, 0, 3, 0]); }); it('works when the line oscillates with widely spaced points', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 0, 12, 2, 1, dest, 0)). to.be(12); expect(dest).to.eql([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]); }); it('works when the line oscillates with closely spaced points', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 0, 12, 2, 2, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 1, 1]); }); it('works when the line oscillates within the tolerance', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0], 0, 14, 2, 2, dest, 0)). to.be(2); expect(dest).to.eql([0, 0]); }); it('works with real data', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( flatCoordinates, 0, flatCoordinates.length, 2, 25, dest, 0)). to.be(simplifiedRadiallyFlatCoordinates.length); expect(dest).to.eql(simplifiedRadiallyFlatCoordinates); @@ -185,7 +185,7 @@ describe('ol.geom.simplify', function() { }); - describe('ol.geom.simplify.douglasPeucker', function() { + describe('ol.geom.flat.simplify.douglasPeucker', function() { var dest; beforeEach(function() { @@ -193,93 +193,93 @@ describe('ol.geom.simplify', function() { }); it('works with empty line strings', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [], 0, 0, 2, 1, dest, 0)).to.be(0); expect(dest).to.eql([]); }); it('works with a line string with a single point', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [1, 2], 0, 2, 2, 1, dest, 0)).to.be(2); expect(dest).to.eql([1, 2]); }); it('works with a line string with two points', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [1, 2, 3, 4], 0, 4, 2, 1, dest, 0)).to.be(4); expect(dest).to.eql([1, 2, 3, 4]); }); it('works when the points are widely spaced', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 0.5, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 3, 0]); }); it('works when the spacing matches the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 3, 0]); }); it('works when the points are closely spaced', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1.5, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 3, 0]); }); it('does not elimnate points outside the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 2, 0], 0, 6, 2, 0.5, dest, 0)).to.be(6); expect(dest).to.eql([0, 0, 1, 1, 2, 0]); }); it('does eliminate points within the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 2, 0], 0, 6, 2, 2, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 2, 0]); }); it('does not eliminate multiple points outside the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 1, -1, 2, 0], 0, 8, 2, 0.5, dest, 0)).to.be(8); expect(dest).to.eql([0, 0, 1, 1, 1, -1, 2, 0]); }); it('does eliminate multiple points within the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 1, -1, 2, 0], 0, 8, 2, 2, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 2, 0]); }); it('works when the line oscillates with widely spaced points', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 0, 12, 2, 1, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 1, 1]); }); it('works when the line oscillates with closely spaced points', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 0, 12, 2, 2, dest, 0)). to.be(4); expect(dest).to.eql([0, 0, 1, 1]); }); it('works when the line oscillates within the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0], 0, 14, 2, 2, dest, 0)). to.be(4); expect(dest).to.eql([0, 0, 0, 0]); }); it('works on small triangles', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [3, 0, 4, 1, 5, 2, 5, 0], 0, 8, 2, 1, dest, 0)).to.be(6); expect(dest).to.eql([3, 0, 5, 2, 5, 0]); }); it('is the same as high quality simplification', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( flatCoordinates, 0, flatCoordinates.length, 2, 25, dest, 0)). to.be(simplifiedHighQualityFlatCoordinates.length); expect(dest).to.eql(simplifiedHighQualityFlatCoordinates); @@ -287,32 +287,32 @@ describe('ol.geom.simplify', function() { }); - describe('ol.geom.simplify.quantize', function() { + describe('ol.geom.flat.simplify.quantize', function() { it('handles empty coordinates', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [], 0, 0, 2, 2, simplifiedFlatCoordinates, 0)).to.be(0); expect(simplifiedFlatCoordinates).to.be.empty(); }); it('expands points to a zero-length line', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 0, 0], 0, 4, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, 0]); }); it('snaps near-by points to the same value', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0.1, 0, 0, 0.1], 0, 4, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, 0]); }); it('eliminates duplicate snapped points', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0.1, 0, 2, 0, 2.1, 0, 2, 0.1, 1.9, 0, 2, -0.1], 0, 12, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 2, 0]); @@ -320,7 +320,7 @@ describe('ol.geom.simplify', function() { it('eliminates horizontal colinear points', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 2, 0, 4, 0, 6, 0], 0, 8, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 6, 0]); @@ -328,7 +328,7 @@ describe('ol.geom.simplify', function() { it('eliminates vertical colinear points', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 0, -2, 0, -4, 0, -6], 0, 8, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, -6]); @@ -336,7 +336,7 @@ describe('ol.geom.simplify', function() { it('eliminates diagonal colinear points', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 2, -2, 4, -4, 6, -6], 0, 8, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 6, -6]); @@ -344,7 +344,7 @@ describe('ol.geom.simplify', function() { it('handles switchbacks', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 2, 0, 0, 0, 4, 0], 0, 8, 2, 2, simplifiedFlatCoordinates, 0)).to.be(8); expect(simplifiedFlatCoordinates).to.eql([0, 0, 2, 0, 0, 0, 4, 0]); @@ -355,4 +355,4 @@ describe('ol.geom.simplify', function() { }); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat.simplify'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js deleted file mode 100644 index dbbf55bf90e..00000000000 --- a/test/spec/ol/geom/flatgeom.test.js +++ /dev/null @@ -1,298 +0,0 @@ -goog.provide('ol.test.geom.flat'); - - -describe('ol.geom.flat', function() { - - describe('ol.geom.flat.deflateCoordinates', function() { - - var flatCoordinates; - beforeEach(function() { - flatCoordinates = []; - }); - - it('flattens coordinates', function() { - var offset = ol.geom.flat.deflateCoordinates( - flatCoordinates, 0, [[1, 2], [3, 4]], 2); - expect(offset).to.be(4); - expect(flatCoordinates).to.eql([1, 2, 3, 4]); - }); - - }); - - describe('ol.geom.flat.deflateCoordinatess', function() { - - var flatCoordinates; - beforeEach(function() { - flatCoordinates = []; - }); - - it('flattens arrays of coordinates', function() { - var ends = ol.geom.flat.deflateCoordinatess(flatCoordinates, 0, - [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], 2); - expect(ends).to.eql([4, 8]); - expect(flatCoordinates).to.eql([1, 2, 3, 4, 5, 6, 7, 8]); - }); - - }); - - describe('ol.geom.flat.flipXY', function() { - - it('can flip XY coordinates', function() { - var flatCoordinates = ol.geom.flat.flipXY([1, 2, 3, 4], 0, 4, 2); - expect(flatCoordinates).to.eql([2, 1, 4, 3]); - }); - - it('can flip XY coordinates while preserving other dimensions', function() { - var flatCoordinates = ol.geom.flat.flipXY( - [1, 2, 3, 4, 5, 6, 7, 8], 0, 8, 4); - expect(flatCoordinates).to.eql([2, 1, 3, 4, 6, 5, 7, 8]); - }); - - it('can flip XY coordinates in place', function() { - var flatCoordinates = [1, 2, 3, 4]; - expect(ol.geom.flat.flipXY(flatCoordinates, 0, 4, 2, flatCoordinates)). - to.be(flatCoordinates); - expect(flatCoordinates).to.eql([2, 1, 4, 3]); - }); - - it('can flip XY coordinates in place while preserving other dimensions', - function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - expect(ol.geom.flat.flipXY( - flatCoordinates, 0, 9, 3, flatCoordinates)). - to.be(flatCoordinates); - expect(flatCoordinates).to.eql([2, 1, 3, 5, 4, 6, 8, 7, 9]); - }); - - }); - - describe('ol.geom.flat.inflateCoordinates', function() { - - it('inflates coordinates', function() { - var coordinates = ol.geom.flat.inflateCoordinates([1, 2, 3, 4], 0, 4, 2); - expect(coordinates).to.eql([[1, 2], [3, 4]]); - }); - - }); - - describe('ol.geom.flat.inflateCoordinatess', function() { - - it('inflates arrays of coordinates', function() { - var coordinatess = ol.geom.flat.inflateCoordinatess( - [1, 2, 3, 4, 5, 6, 7, 8], 0, [4, 8], 2); - expect(coordinatess).to.eql([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); - }); - - }); - - describe('ol.geom.flat.lineStringInterpolate', function() { - - it('returns the expected value for single points', function() { - var flatCoordinates = [0, 1]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 2, 2, 0.5); - expect(point).to.eql([0, 1]); - }); - - it('returns the expected value for simple line segments', function() { - var flatCoordinates = [0, 1, 2, 3]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 4, 2, 0.5); - expect(point).to.eql([1, 2]); - }); - - it('returns the expected value when the mid point is an existing ' + - 'coordinate', function() { - var flatCoordinates = [0, 1, 2, 3, 4, 5]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 6, 2, 0.5); - expect(point).to.eql([2, 3]); - }); - - it('returns the expected value when the midpoint falls halfway between ' + - 'two existing coordinates', function() { - var flatCoordinates = [0, 1, 2, 3, 4, 5, 6, 7]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 8, 2, 0.5); - expect(point).to.eql([3, 4]); - }); - - it('returns the expected value when the coordinates are not evenly spaced', - function() { - var flatCoordinates = [0, 1, 2, 3, 6, 7]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 6, 2, 0.5); - expect(point).to.eql([3, 4]); - }); - - }); - - describe('ol.geom.flat.linearRingArea', function() { - - it('calcaultes the area of a triangle', function() { - var area = ol.geom.flat.linearRingArea([0, 0, 0.5, 1, 1, 0], 0, 6, 2); - expect(area).to.be(0.5); - }); - - it('calculates the area of a unit square', function() { - var area = ol.geom.flat.linearRingArea([0, 0, 0, 1, 1, 1, 1, 0], 0, 8, 2); - expect(area).to.be(1); - }); - - }); - - describe('ol.geom.flat.linearRingIsClockwise', function() { - - it('identifies clockwise rings', function() { - var flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0]; - var isClockwise = ol.geom.flat.linearRingIsClockwise( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(isClockwise).to.be(true); - }); - - it('identifies anti-clockwise rings', function() { - var flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3]; - var isClockwise = ol.geom.flat.linearRingIsClockwise( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(isClockwise).to.be(false); - }); - - }); - - describe('ol.geom.flat.linearRingsArea', function() { - - it('calculates the area with holes', function() { - var area = ol.geom.flat.linearRingsArea( - [0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2], 0, [8, 16], 2); - expect(area).to.be(8); - }); - - }); - - describe('ol.geom.flat.reverseCoordinates', function() { - - describe('with a stride of 2', function() { - - it('can reverse empty flat coordinates', function() { - var flatCoordinates = []; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.be.empty(); - }); - - it('can reverse one flat coordinates', function() { - var flatCoordinates = [1, 2]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.eql([1, 2]); - }); - - it('can reverse two flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.eql([3, 4, 1, 2]); - }); - - it('can reverse three flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.eql([5, 6, 3, 4, 1, 2]); - }); - - it('can reverse four flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.eql([7, 8, 5, 6, 3, 4, 1, 2]); - }); - - }); - - describe('with a stride of 3', function() { - - it('can reverse empty flat coordinates', function() { - var flatCoordinates = []; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.be.empty(); - }); - - it('can reverse one flat coordinates', function() { - var flatCoordinates = [1, 2, 3]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.eql([1, 2, 3]); - }); - - it('can reverse two flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.eql([4, 5, 6, 1, 2, 3]); - }); - - it('can reverse three flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.eql([7, 8, 9, 4, 5, 6, 1, 2, 3]); - }); - - it('can reverse four flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.eql([10, 11, 12, 7, 8, 9, 4, 5, 6, 1, 2, 3]); - }); - - }); - - describe('with a stride of 4', function() { - - it('can reverse empty flat coordinates', function() { - var flatCoordinates = []; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.be.empty(); - }); - - it('can reverse one flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.eql([1, 2, 3, 4]); - }); - - it('can reverse two flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.eql([5, 6, 7, 8, 1, 2, 3, 4]); - }); - - it('can reverse three flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.eql([9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]); - }); - - it('can reverse four flat coordinates', function() { - var flatCoordinates = - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.eql( - [13, 14, 15, 16, 9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]); - }); - - }); - - }); - -}); - - -goog.require('ol.geom.flat');