Rework isClosed with added tests
This commit is contained in:
20
src/ol/geom/flat/topologyflatgeom.js
Normal file
20
src/ol/geom/flat/topologyflatgeom.js
Normal file
@@ -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.<number>} 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;
|
||||
};
|
||||
@@ -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.<number>} 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.<number>} flatCoordinates Flat coordinates.
|
||||
|
||||
33
test/spec/ol/geom/flat/topologyflatgeom.test.js
Normal file
33
test/spec/ol/geom/flat/topologyflatgeom.test.js
Normal file
@@ -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');
|
||||
Reference in New Issue
Block a user