From 0f9e2690574a0301d9d22f0584897cfd62680798 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sun, 18 Aug 2013 09:57:26 -0400 Subject: [PATCH] Ignore extra dimensions and pad with NaN for missing dimensions --- src/ol/geom/sharedvertices.js | 11 +++-------- test/spec/ol/geom/linearring.test.js | 7 ------- test/spec/ol/geom/linestring.test.js | 7 ------- test/spec/ol/geom/polygon.test.js | 7 ------- test/spec/ol/geom/sharedvertices.test.js | 19 +++++++++++++++++++ 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/ol/geom/sharedvertices.js b/src/ol/geom/sharedvertices.js index fcb3295c5a..9d2df539ad 100644 --- a/src/ol/geom/sharedvertices.js +++ b/src/ol/geom/sharedvertices.js @@ -71,14 +71,9 @@ ol.geom.SharedVertices.prototype.add = function(vertices) { var vertex, index; for (var i = 0; i < count; ++i) { vertex = vertices[i]; - goog.asserts.assert(vertex.length == dimension); - if (!offset) { - Array.prototype.push.apply(this.coordinates, vertex); - } else { - index = start + (i * dimension); - for (var j = 0; j < dimension; ++j) { - this.coordinates[index + j] = vertex[j] - offset[j]; - } + index = start + (i * dimension); + for (var j = 0; j < dimension; ++j) { + this.coordinates[index + j] = vertex[j] - (offset ? offset[j] : 0); } } var length = this.starts_.push(start); diff --git a/test/spec/ol/geom/linearring.test.js b/test/spec/ol/geom/linearring.test.js index 8681773f7e..341e688ae6 100644 --- a/test/spec/ol/geom/linearring.test.js +++ b/test/spec/ol/geom/linearring.test.js @@ -9,13 +9,6 @@ describe('ol.geom.LinearRing', function() { expect(ring).to.be.a(ol.geom.LinearRing); }); - it('throws when given mismatched dimension', function() { - expect(function() { - var ring = new ol.geom.LinearRing([[10, 20], [30, 40, 50]]); - ring = ring; // suppress gjslint warning about unused variable - }).to.throwException(); - }); - }); describe('#dimension', function() { diff --git a/test/spec/ol/geom/linestring.test.js b/test/spec/ol/geom/linestring.test.js index dfdd1c37ee..3367073f6f 100644 --- a/test/spec/ol/geom/linestring.test.js +++ b/test/spec/ol/geom/linestring.test.js @@ -10,13 +10,6 @@ describe('ol.geom.LineString', function() { expect(line).to.be.a(ol.geom.Geometry); }); - it('throws when given mismatched dimension', function() { - expect(function() { - var line = new ol.geom.LineString([[10, 20], [30, 40, 50]]); - line = line; // suppress gjslint warning about unused variable - }).to.throwException(); - }); - it('accepts shared vertices', function() { var vertices = new ol.geom.SharedVertices(); var l1 = new ol.geom.LineString([[10, 20], [30, 40]], vertices); diff --git a/test/spec/ol/geom/polygon.test.js b/test/spec/ol/geom/polygon.test.js index 355105ff80..80fbefebec 100644 --- a/test/spec/ol/geom/polygon.test.js +++ b/test/spec/ol/geom/polygon.test.js @@ -14,13 +14,6 @@ describe('ol.geom.Polygon', function() { expect(poly).to.be.a(ol.geom.Geometry); }); - it('throws when given mismatched dimension', function() { - expect(function() { - var poly = new ol.geom.Polygon([[[10, 20], [30, 40, 50]]]); - poly = poly; // suppress gjslint warning about unused variable - }).to.throwException(); - }); - it('accepts shared vertices', function() { var vertices = new ol.geom.SharedVertices(); var p1 = new ol.geom.Polygon([outer], vertices); diff --git a/test/spec/ol/geom/sharedvertices.test.js b/test/spec/ol/geom/sharedvertices.test.js index 0e9c3fc29d..3c3adce456 100644 --- a/test/spec/ol/geom/sharedvertices.test.js +++ b/test/spec/ol/geom/sharedvertices.test.js @@ -40,6 +40,25 @@ describe('ol.geom.SharedVertices', function() { expect(vertices.coordinates).to.eql([1, 2, 3, 4, 5, 6]); }); + it('ignores extra dimensions', function() { + var vertices = new ol.geom.SharedVertices({dimension: 2}); + expect(vertices.coordinates.length).to.be(0); + + vertices.add([[1, 2], [3, 4, 5], [6, 7]]); + expect(vertices.coordinates).to.eql([1, 2, 3, 4, 6, 7]); + + vertices.add([[8, 9, 10]]); + expect(vertices.coordinates).to.eql([1, 2, 3, 4, 6, 7, 8, 9]); + }); + + it('pads with NaN when dimension not provided', function() { + var vertices = new ol.geom.SharedVertices({dimension: 3}); + expect(vertices.coordinates.length).to.be(0); + + vertices.add([[1, 2], [3, 4, 5], [6, 7]]); + expect(vertices.coordinates).to.eql([1, 2, NaN, 3, 4, 5, 6, 7, NaN]); + }); + it('returns an identifier for coordinate access', function() { var vertices = new ol.geom.SharedVertices(); var id = vertices.add([[1, 2], [3, 4]]);