From 483644a97ef8597cbe5ac9d446d8803a1925720c Mon Sep 17 00:00:00 2001 From: David Hudlow Date: Fri, 27 Sep 2013 17:25:29 -0500 Subject: [PATCH 1/2] Tolerate (but don't fully render) complex polygons This patches the problem with complex polygons completely failing triangulation. Instead, complex areas of the polygon will not render, but instead be skipped over. This allows polygons which are have only small "complexities" to still render (almost) correctly. --- CHANGES.md | 1 + Source/Core/GeometryPipeline.js | 5 +++++ Source/Core/PolygonGeometry.js | 4 ++++ Source/Core/PolygonPipeline.js | 5 +++-- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b660c083e1ee..4a85554f8962 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -73,6 +73,7 @@ Beta Releases becomes return primitives.add(new Primitive(/* ... */)); +* Fixed bug in triangulation that fails on complex polygons. Instead, it makes a "best effort" to render what it can. ### b20 - 2013-09-03 diff --git a/Source/Core/GeometryPipeline.js b/Source/Core/GeometryPipeline.js index 48a1e711175f..cf6658db5c92 100644 --- a/Source/Core/GeometryPipeline.js +++ b/Source/Core/GeometryPipeline.js @@ -635,6 +635,11 @@ define([ for ( var i = 0; i < values3D.length; i += 3) { var value = Cartesian3.fromArray(values3D, i, scratchProjectTo2DCartesian3); + + if (Cartesian3.equals(value, Cartesian3.ZERO)) { + continue; + } + var lonLat = ellipsoid.cartesianToCartographic(value, scratchProjectTo2DCartographic); var projectedLonLat = projection.project(lonLat, scratchProjectTo2DCartesian3); diff --git a/Source/Core/PolygonGeometry.js b/Source/Core/PolygonGeometry.js index f52c16ae7c15..0eb3dd831adc 100644 --- a/Source/Core/PolygonGeometry.js +++ b/Source/Core/PolygonGeometry.js @@ -102,6 +102,10 @@ define([ } var indices = PolygonPipeline.triangulate(positions2D); + /* If polygon is completely unrenderable, just use the first three vertices */ + if (indices.length < 3) { + indices = [0, 1, 2]; + } return new GeometryInstance({ geometry : PolygonPipeline.computeSubdivision(positions, indices, granularity) }); diff --git a/Source/Core/PolygonPipeline.js b/Source/Core/PolygonPipeline.js index c9d49339a3b9..5d57b53de89a 100644 --- a/Source/Core/PolygonPipeline.js +++ b/Source/Core/PolygonPipeline.js @@ -708,7 +708,6 @@ define([ * @returns {Array} Index array representing triangles that fill the polygon * * @exception {DeveloperError} Invalid polygon: must have at least three vertices. - * @exception {DeveloperERror} Tried x times to find a vild cut and couldn't. * * @private */ @@ -736,7 +735,9 @@ define([ // Make sure we don't go into an endless loop var maxTries = nodeArray.length * 10; if (tries > maxTries) { - throw new DeveloperError('Tried ' + maxTries + ' times to find a valid cut and couldn\'t.'); + console.warn('Tried ' + maxTries + ' times to find a valid cut to triangulate polygon and couldn\'t.'); + // Hopefully that part of the polygon isn't important + return []; } tries++; From f951711e768a4ff0f9ba945c17cecf0db20e8b7f Mon Sep 17 00:00:00 2001 From: David Hudlow Date: Fri, 27 Sep 2013 17:32:12 -0500 Subject: [PATCH 2/2] JSHint doesn't like console --- Source/Core/PolygonPipeline.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Core/PolygonPipeline.js b/Source/Core/PolygonPipeline.js index 5d57b53de89a..43f0b48c97a0 100644 --- a/Source/Core/PolygonPipeline.js +++ b/Source/Core/PolygonPipeline.js @@ -735,7 +735,6 @@ define([ // Make sure we don't go into an endless loop var maxTries = nodeArray.length * 10; if (tries > maxTries) { - console.warn('Tried ' + maxTries + ' times to find a valid cut to triangulate polygon and couldn\'t.'); // Hopefully that part of the polygon isn't important return []; }