Refactor linestring geom validation

Now it only focuses on issues not addressed by ol.geom.flat.simplify methods.
This commit is contained in:
GaborFarkas
2016-06-21 16:02:34 +02:00
parent f15a9652d8
commit 4be8de62ae

View File

@@ -1050,15 +1050,13 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
var lastSign = 1; var lastSign = 1;
//We need the adjacent vertices to define normals in joins. p0 = last, p1 = current, p2 = next. //We need the adjacent vertices to define normals in joins. p0 = last, p1 = current, p2 = next.
//We rotate those points, thus every point is RTE corrected only once. //We rotate those points, thus every point is RTE corrected only once.
var p0, p1, p2, tempP; var p0, p1, p2;
if (this.isValid_(flatCoordinates, offset, end, stride, closed)) {
for (i = offset, ii = end; i < ii; i += stride) { for (i = offset, ii = end; i < ii; i += stride) {
var n = numVertices / 7; var n = numVertices / 7;
tempP = tempP || p1; p0 = p1;
p1 = p2 || [flatCoordinates[i] - this.origin_[0], flatCoordinates[i + 1] - this.origin_[1]]; p1 = p2 || [flatCoordinates[i] - this.origin_[0], flatCoordinates[i + 1] - this.origin_[1]];
//First vertex. //First vertex.
if (i === offset) { if (i === offset) {
@@ -1068,15 +1066,8 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
} }
if (closed) { if (closed) {
//A closed line! Complete the circle. //A closed line! Complete the circle.
var j = end - stride * 2; p0 = [flatCoordinates[end - stride * 2] - this.origin_[0],
var startCoord = [flatCoordinates[offset], flatCoordinates[offset + 1]]; flatCoordinates[end - stride * 2 + 1] - this.origin_[1]];
tempP = [flatCoordinates[j], flatCoordinates[j + 1]];
while (ol.array.equals(tempP, startCoord)) {
j -= stride;
tempP = [flatCoordinates[j], flatCoordinates[j + 1]];
}
tempP[0] -= this.origin_[0];
tempP[1] -= this.origin_[1];
} else { } else {
//Add the first two/four vertices. //Add the first two/four vertices.
@@ -1095,7 +1086,6 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
this.indices_[numIndices++] = n + 3; this.indices_[numIndices++] = n + 3;
this.indices_[numIndices++] = n + 2; this.indices_[numIndices++] = n + 2;
n = n + 2;
} }
numVertices = this.addVertices_([0, 0], p1, p2, numVertices = this.addVertices_([0, 0], p1, p2,
@@ -1104,7 +1094,7 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
numVertices = this.addVertices_([0, 0], p1, p2, numVertices = this.addVertices_([0, 0], p1, p2,
-lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE * (lineCap || 1), numVertices); -lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE * (lineCap || 1), numVertices);
lastIndex = n + 1; lastIndex = n + 3;
continue; continue;
} }
@@ -1114,10 +1104,8 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
//Same as the first vertex. //Same as the first vertex.
break; break;
} else { } else {
p2 = undefined; //For the compiler not to complain. This will be never [0, 0].
//Note, that the third case will never happen, we just have to assure the compiler, p0 = p0 || [0, 0];
//p0 is always an array of nums.
p0 = tempP || p0 || [0, 0];
numVertices = this.addVertices_(p0, p1, [0, 0], numVertices = this.addVertices_(p0, p1, [0, 0],
lastSign * ol.render.webgl.LineStringInstruction.END_LINE * (lineCap || 1), numVertices); lastSign * ol.render.webgl.LineStringInstruction.END_LINE * (lineCap || 1), numVertices);
@@ -1155,10 +1143,6 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
} else { } else {
p2 = [flatCoordinates[i + stride] - this.origin_[0], flatCoordinates[i + stride + 1] - this.origin_[1]]; p2 = [flatCoordinates[i + stride] - this.origin_[0], flatCoordinates[i + stride + 1] - this.origin_[1]];
} }
//Sort out duplicate points.
if (ol.array.equals(p1, p2)) continue;
p0 = tempP || p0;
tempP = undefined;
var sign = ol.geom.flat.orient.linearRingIsClockwise([p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]], 0, 6, 2) var sign = ol.geom.flat.orient.linearRingIsClockwise([p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]], 0, 6, 2)
? 1 : -1; ? 1 : -1;
@@ -1210,7 +1194,6 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
this.indices_[numIndices++] = lastIndex - 1; this.indices_[numIndices++] = lastIndex - 1;
this.indices_[numIndices++] = lastIndex; this.indices_[numIndices++] = lastIndex;
} }
}
}; };
/** /**
@@ -1258,31 +1241,20 @@ ol.render.webgl.LineStringReplay.prototype.isClosed_ = function(flatCoordinates,
* @param {number} offset Offset. * @param {number} offset Offset.
* @param {number} end End. * @param {number} end End.
* @param {number} stride Stride. * @param {number} stride Stride.
* @param {boolean} closed The linestring is a boundary.
* @return {boolean} The linestring can be drawn. * @return {boolean} The linestring can be drawn.
* @private * @private
*/ */
ol.render.webgl.LineStringReplay.prototype.isValid_ = function(flatCoordinates, offset, end, stride, ol.render.webgl.LineStringReplay.prototype.isValid_ = function(flatCoordinates, offset, end, stride) {
closed) { var range = end - offset;
var uniqueCoords = [[flatCoordinates[offset], flatCoordinates[offset + 1]]]; if (range < stride * 2) {
var minUnique = closed ? 3 : 2;
var i, ii, j;
for (i = offset + stride, ii = end; i < ii; i += stride) {
var currentCoords = [flatCoordinates[i], flatCoordinates[i + 1]];
for (j = 0; j < uniqueCoords.length; ++j) {
if (ol.array.equals(currentCoords, uniqueCoords[j])) {
j = -1;
break;
}
}
if (j > -1) {
uniqueCoords.push(currentCoords);
}
if (uniqueCoords.length >= minUnique) {
return true;
}
}
return false; return false;
} else if (range === stride * 2) {
var firstP = [flatCoordinates[offset], flatCoordinates[offset + 1]];
var lastP = [flatCoordinates[offset + stride], flatCoordinates[offset + stride + 1]];
return !ol.array.equals(firstP, lastP);
}
return true;
}; };
@@ -1292,7 +1264,7 @@ ol.render.webgl.LineStringReplay.prototype.isValid_ = function(flatCoordinates,
ol.render.webgl.LineStringReplay.prototype.drawLineString = function(lineStringGeometry, feature) { ol.render.webgl.LineStringReplay.prototype.drawLineString = function(lineStringGeometry, feature) {
var flatCoordinates = lineStringGeometry.getFlatCoordinates(); var flatCoordinates = lineStringGeometry.getFlatCoordinates();
var stride = lineStringGeometry.getStride(); var stride = lineStringGeometry.getStride();
if (flatCoordinates.length > stride) { if (this.isValid_(flatCoordinates, 0, flatCoordinates.length, stride)) {
this.drawCoordinates_( this.drawCoordinates_(
flatCoordinates, 0, flatCoordinates.length, stride); flatCoordinates, 0, flatCoordinates.length, stride);
this.startIndices_.push(this.indices_.length); this.startIndices_.push(this.indices_.length);
@@ -1311,7 +1283,7 @@ ol.render.webgl.LineStringReplay.prototype.drawMultiLineString = function(multiL
for (i = 0, ii = lineStringGeometries.length; i < ii; ++i) { for (i = 0, ii = lineStringGeometries.length; i < ii; ++i) {
var flatCoordinates = lineStringGeometries[i].getFlatCoordinates(); var flatCoordinates = lineStringGeometries[i].getFlatCoordinates();
var stride = lineStringGeometries[i].getStride(); var stride = lineStringGeometries[i].getStride();
if (flatCoordinates.length > stride) { if (this.isValid_(flatCoordinates, 0, flatCoordinates.length, stride)) {
this.drawCoordinates_( this.drawCoordinates_(
flatCoordinates, 0, flatCoordinates.length, stride); flatCoordinates, 0, flatCoordinates.length, stride);
} }