diff --git a/src/ol/geom/flat/topologyflatgeom.js b/src/ol/geom/flat/topologyflatgeom.js new file mode 100644 index 0000000000..a1b9f053f5 --- /dev/null +++ b/src/ol/geom/flat/topologyflatgeom.js @@ -0,0 +1,20 @@ +goog.provide('ol.geom.flat.topology'); + +goog.require('ol.geom.flat.area'); + +/** + * Check if the linestring is a boundary. + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {boolean} The linestring is a boundary. + */ +ol.geom.flat.topology.lineStringIsClosed = function(flatCoordinates, offset, end, stride) { + var lastCoord = end - stride; + if (flatCoordinates[offset] === flatCoordinates[lastCoord] && + flatCoordinates[offset + 1] === flatCoordinates[lastCoord + 1] && (end - offset) / stride > 3) { + return !!ol.geom.flat.area.linearRing(flatCoordinates, offset, end, stride); + } + return false; +}; diff --git a/src/ol/render/webgl/imagereplay/index.js b/src/ol/render/webgl/imagereplay/index.js index 54a187a368..3c1633341f 100644 --- a/src/ol/render/webgl/imagereplay/index.js +++ b/src/ol/render/webgl/imagereplay/index.js @@ -10,6 +10,8 @@ goog.require('ol.ext.earcut'); goog.require('ol.extent'); goog.require('ol.obj'); goog.require('ol.render.ReplayGroup'); +goog.require('ol.geom.flat.orient'); +goog.require('ol.geom.flat.topology'); goog.require('ol.render.VectorContext'); goog.require('ol.render.replay'); goog.require('ol.render.webgl.imagereplay.defaultshader'); @@ -1044,7 +1046,7 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord this.state_.lineJoin === 'miter' ? 1 : 2; var lineCap = this.state_.lineCap === 'butt' ? 0 : this.state_.lineCap === 'square' ? 1 : 2; - var closed = this.isClosed_(flatCoordinates, offset, end, stride); + var closed = ol.geom.flat.topology.lineStringIsClosed(flatCoordinates, offset, end, stride); var startCoords, sign, n; var lastIndex = numIndices; var lastSign = 1; @@ -1231,24 +1233,6 @@ ol.render.webgl.LineStringReplay.prototype.addVertices_ = function(p0, p1, p2, p return numVertices; }; -/** - * Check if the linestring is a boundary. - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {boolean} The linestring is a boundary. - * @private - */ -ol.render.webgl.LineStringReplay.prototype.isClosed_ = function(flatCoordinates, offset, end, stride) { - var lastCoord = end - stride; - if (flatCoordinates[offset] === flatCoordinates[lastCoord] && - flatCoordinates[offset + 1] === flatCoordinates[lastCoord + 1] && (end - offset) / stride > 3) { - return true; - } - return false; -}; - /** * Check if the linestring can be drawn (i. e. valid). * @param {Array.} flatCoordinates Flat coordinates. diff --git a/test/spec/ol/geom/flat/topologyflatgeom.test.js b/test/spec/ol/geom/flat/topologyflatgeom.test.js new file mode 100644 index 0000000000..dd93d83ac3 --- /dev/null +++ b/test/spec/ol/geom/flat/topologyflatgeom.test.js @@ -0,0 +1,33 @@ +goog.provide('ol.test.geom.flat.topology'); + +describe('ol.geom.flat.topology', function() { + + describe('ol.geom.flat.topology.lineStringIsClosed', function() { + + it('identifies closed lines aka boundaries', function() { + var flatCoordinates = [0, 0, 3, 0, 0, 3, 0, 0]; + var isClosed = ol.geom.flat.topology.lineStringIsClosed(flatCoordinates, 0, flatCoordinates.length, 2); + expect(isClosed).to.be(true); + }); + + it('identifies regular linestrings', function() { + var flatCoordinates = [0, 0, 3, 0, 0, 3, 5, 2]; + var isClosed = ol.geom.flat.topology.lineStringIsClosed(flatCoordinates, 0, flatCoordinates.length, 2); + expect(isClosed).to.be(false); + }); + + it('identifies degenerate boundaries', function() { + var flatCoordinates = [0, 0, 3, 0, 0, 0]; + var isClosed = ol.geom.flat.topology.lineStringIsClosed(flatCoordinates, 0, flatCoordinates.length, 2); + expect(isClosed).to.be(false); + + flatCoordinates = [0, 0, 1, 1, 3, 3, 5, 5, 0, 0]; + isClosed = ol.geom.flat.topology.lineStringIsClosed(flatCoordinates, 0, flatCoordinates.length, 2); + expect(isClosed).to.be(false); + }); + + }); + +}); + +goog.require('ol.geom.flat.topology');