Don't introduce new self-intersections. Fixes #6823

This commit is contained in:
GaborFarkas
2017-05-29 15:51:11 +02:00
parent bcda41b508
commit 7ea8cf5fb3

View File

@@ -96,7 +96,7 @@ if (ol.ENABLE_WEBGL) {
stride, holeList.list, holeList.rtree, false); stride, holeList.list, holeList.rtree, false);
} }
holeLists.sort(function(a, b) { holeLists.sort(function(a, b) {
return b.maxX - a.maxX; return b.maxX[0] === a.maxX[0] ? a.maxX[1] - b.maxX[1] : b.maxX[0] - a.maxX[0];
}); });
for (i = 0; i < holeLists.length; ++i) { for (i = 0; i < holeLists.length; ++i) {
var currList = holeLists[i].list; var currList = holeLists[i].list;
@@ -112,12 +112,15 @@ if (ol.ENABLE_WEBGL) {
} while (start !== currItem); } while (start !== currItem);
if (!intersection) { if (!intersection) {
this.classifyPoints_(currList, holeLists[i].rtree, true); this.classifyPoints_(currList, holeLists[i].rtree, true);
this.bridgeHole_(currList, holeLists[i].maxX, outerRing, maxX, rtree); if (this.bridgeHole_(currList, holeLists[i].maxX[0], outerRing, maxX[0], rtree)) {
rtree.concat(holeLists[i].rtree); rtree.concat(holeLists[i].rtree);
}
}
}
this.classifyPoints_(outerRing, rtree, false); this.classifyPoints_(outerRing, rtree, false);
}
}
}
} else {
this.classifyPoints_(outerRing, rtree, false);
}
this.triangulate_(outerRing, rtree); this.triangulate_(outerRing, rtree);
}; };
@@ -130,13 +133,13 @@ if (ol.ENABLE_WEBGL) {
* @param {ol.structs.LinkedList} list Linked list. * @param {ol.structs.LinkedList} list Linked list.
* @param {ol.structs.RBush} rtree R-Tree of the polygon. * @param {ol.structs.RBush} rtree R-Tree of the polygon.
* @param {boolean} clockwise Coordinate order should be clockwise. * @param {boolean} clockwise Coordinate order should be clockwise.
* @return {number} Maximum X value. * @return {Array.<number>} X and Y coords of maximum X value.
*/ */
ol.render.webgl.PolygonReplay.prototype.processFlatCoordinates_ = function( ol.render.webgl.PolygonReplay.prototype.processFlatCoordinates_ = function(
flatCoordinates, stride, list, rtree, clockwise) { flatCoordinates, stride, list, rtree, clockwise) {
var isClockwise = ol.geom.flat.orient.linearRingIsClockwise(flatCoordinates, var isClockwise = ol.geom.flat.orient.linearRingIsClockwise(flatCoordinates,
0, flatCoordinates.length, stride); 0, flatCoordinates.length, stride);
var i, ii, maxX; var i, ii, maxXX, maxXY;
var n = this.vertices.length / 2; var n = this.vertices.length / 2;
/** @type {ol.WebglPolygonVertex} */ /** @type {ol.WebglPolygonVertex} */
var start; var start;
@@ -149,13 +152,17 @@ if (ol.ENABLE_WEBGL) {
if (clockwise === isClockwise) { if (clockwise === isClockwise) {
start = this.createPoint_(flatCoordinates[0], flatCoordinates[1], n++); start = this.createPoint_(flatCoordinates[0], flatCoordinates[1], n++);
p0 = start; p0 = start;
maxX = flatCoordinates[0]; maxXX = flatCoordinates[0];
maxXY = flatCoordinates[1];
for (i = stride, ii = flatCoordinates.length; i < ii; i += stride) { for (i = stride, ii = flatCoordinates.length; i < ii; i += stride) {
p1 = this.createPoint_(flatCoordinates[i], flatCoordinates[i + 1], n++); p1 = this.createPoint_(flatCoordinates[i], flatCoordinates[i + 1], n++);
segments.push(this.insertItem_(p0, p1, list)); segments.push(this.insertItem_(p0, p1, list));
extents.push([Math.min(p0.x, p1.x), Math.min(p0.y, p1.y), Math.max(p0.x, p1.x), extents.push([Math.min(p0.x, p1.x), Math.min(p0.y, p1.y), Math.max(p0.x, p1.x),
Math.max(p0.y, p1.y)]); Math.max(p0.y, p1.y)]);
maxX = flatCoordinates[i] > maxX ? flatCoordinates[i] : maxX; if (flatCoordinates[i] > maxXX) {
maxXX = flatCoordinates[i];
maxXY = flatCoordinates[i + 1];
}
p0 = p1; p0 = p1;
} }
segments.push(this.insertItem_(p1, start, list)); segments.push(this.insertItem_(p1, start, list));
@@ -165,13 +172,17 @@ if (ol.ENABLE_WEBGL) {
var end = flatCoordinates.length - stride; var end = flatCoordinates.length - stride;
start = this.createPoint_(flatCoordinates[end], flatCoordinates[end + 1], n++); start = this.createPoint_(flatCoordinates[end], flatCoordinates[end + 1], n++);
p0 = start; p0 = start;
maxX = flatCoordinates[end]; maxXX = flatCoordinates[end];
maxXY = flatCoordinates[end + 1];
for (i = end - stride, ii = 0; i >= ii; i -= stride) { for (i = end - stride, ii = 0; i >= ii; i -= stride) {
p1 = this.createPoint_(flatCoordinates[i], flatCoordinates[i + 1], n++); p1 = this.createPoint_(flatCoordinates[i], flatCoordinates[i + 1], n++);
segments.push(this.insertItem_(p0, p1, list)); segments.push(this.insertItem_(p0, p1, list));
extents.push([Math.min(p0.x, p1.x), Math.min(p0.y, p1.y), Math.max(p0.x, p1.x), extents.push([Math.min(p0.x, p1.x), Math.min(p0.y, p1.y), Math.max(p0.x, p1.x),
Math.max(p0.y, p1.y)]); Math.max(p0.y, p1.y)]);
maxX = flatCoordinates[i] > maxX ? flatCoordinates[i] : maxX; if (flatCoordinates[i] > maxXX) {
maxXX = flatCoordinates[i];
maxXY = flatCoordinates[i + 1];
}
p0 = p1; p0 = p1;
} }
segments.push(this.insertItem_(p1, start, list)); segments.push(this.insertItem_(p1, start, list));
@@ -180,7 +191,7 @@ if (ol.ENABLE_WEBGL) {
} }
rtree.load(extents, segments); rtree.load(extents, segments);
return maxX; return [maxXX, maxXY];
}; };
@@ -228,6 +239,7 @@ if (ol.ENABLE_WEBGL) {
* @param {ol.structs.LinkedList} list Linked list of the polygon. * @param {ol.structs.LinkedList} list Linked list of the polygon.
* @param {number} listMaxX Maximum X value of the polygon. * @param {number} listMaxX Maximum X value of the polygon.
* @param {ol.structs.RBush} rtree R-Tree of the polygon. * @param {ol.structs.RBush} rtree R-Tree of the polygon.
* @return {boolean} Bridging was successful.
*/ */
ol.render.webgl.PolygonReplay.prototype.bridgeHole_ = function(hole, holeMaxX, ol.render.webgl.PolygonReplay.prototype.bridgeHole_ = function(hole, holeMaxX,
list, listMaxX, rtree) { list, listMaxX, rtree) {
@@ -247,19 +259,18 @@ if (ol.ENABLE_WEBGL) {
var intersectingSegments = this.getIntersections_({p0: p1, p1: p2}, rtree, true); var intersectingSegments = this.getIntersections_({p0: p1, p1: p2}, rtree, true);
for (i = 0, ii = intersectingSegments.length; i < ii; ++i) { for (i = 0, ii = intersectingSegments.length; i < ii; ++i) {
var currSeg = intersectingSegments[i]; var currSeg = intersectingSegments[i];
if (currSeg.p0.reflex === undefined) {
var intersection = this.calculateIntersection_(p1, p2, currSeg.p0, var intersection = this.calculateIntersection_(p1, p2, currSeg.p0,
currSeg.p1, true); currSeg.p1, true);
var dist = Math.abs(p1.x - intersection[0]); var dist = Math.abs(p1.x - intersection[0]);
if (dist < minDist) { if (dist < minDist && ol.render.webgl.triangleIsCounterClockwise(p1.x, p1.y,
currSeg.p0.x, currSeg.p0.y, currSeg.p1.x, currSeg.p1.y) !== undefined) {
minDist = dist; minDist = dist;
p5 = {x: intersection[0], y: intersection[1], i: -1}; p5 = {x: intersection[0], y: intersection[1], i: -1};
seg = currSeg; seg = currSeg;
} }
} }
}
if (minDist === Infinity) { if (minDist === Infinity) {
return; return false;
} }
bestPoint = seg.p1; bestPoint = seg.p1;
@@ -279,7 +290,7 @@ if (ol.ENABLE_WEBGL) {
} }
seg = list.firstItem(); seg = list.firstItem();
while (seg.p1 !== bestPoint) { while (seg.p1.x !== bestPoint.x || seg.p1.y !== bestPoint.y) {
seg = list.nextItem(); seg = list.nextItem();
} }
@@ -293,6 +304,8 @@ if (ol.ENABLE_WEBGL) {
seg.p1 = p1Bridge; seg.p1 = p1Bridge;
hole.setFirstItem(); hole.setFirstItem();
list.concat(hole); list.concat(hole);
return true;
}; };