Remove use of shared vertices in geom package

This commit is contained in:
Tim Schaub
2013-09-25 15:06:53 +02:00
parent 3349bded1c
commit 2850c761cf
11 changed files with 54 additions and 304 deletions

View File

@@ -2,22 +2,13 @@ goog.provide('ol.geom.Geometry');
goog.provide('ol.geom.GeometryType');
goog.require('ol.Extent');
goog.require('ol.geom.SharedVertices');
/**
* @constructor
*/
ol.geom.Geometry = function() {
/**
* @type {ol.geom.SharedVertices}
* @protected
*/
this.vertices = null;
};
ol.geom.Geometry = function() {};
/**
@@ -28,8 +19,7 @@ ol.geom.Geometry.prototype.dimension;
/**
* Create a clone of this geometry. The clone will not be represented in any
* shared structure.
* Create a clone of this geometry.
* @return {ol.geom.Geometry} The cloned geometry.
*/
ol.geom.Geometry.prototype.clone = function() {
@@ -53,15 +43,6 @@ ol.geom.Geometry.prototype.getBounds = goog.abstractMethod;
ol.geom.Geometry.prototype.getCoordinates = goog.abstractMethod;
/**
* Get the shared vertices for this geometry.
* @return {ol.geom.SharedVertices} The shared vertices.
*/
ol.geom.Geometry.prototype.getSharedVertices = function() {
return this.vertices;
};
/**
* Get the geometry type.
* @return {ol.geom.GeometryType} The geometry type.
@@ -69,6 +50,13 @@ ol.geom.Geometry.prototype.getSharedVertices = function() {
ol.geom.Geometry.prototype.getType = goog.abstractMethod;
/**
* Transform a geometry in place.
* @param {ol.TransformFunction} transform Transform function.
*/
ol.geom.Geometry.prototype.transform = goog.abstractMethod;
/**
* Geometry types.
*

View File

@@ -3,7 +3,6 @@ goog.provide('ol.geom.LinearRing');
goog.require('ol.CoordinateArray');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString');
goog.require('ol.geom.SharedVertices');
@@ -12,10 +11,9 @@ goog.require('ol.geom.SharedVertices');
* @extends {ol.geom.LineString}
* @param {ol.CoordinateArray} coordinates Vertex array (e.g.
* [[x0, y0], [x1, y1]]).
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/
ol.geom.LinearRing = function(coordinates, opt_shared) {
goog.base(this, coordinates, opt_shared);
ol.geom.LinearRing = function(coordinates) {
goog.base(this, coordinates);
/**
* We're intentionally not enforcing that rings be closed right now. This

View File

@@ -2,47 +2,34 @@ goog.provide('ol.geom.LineString');
goog.require('goog.asserts');
goog.require('ol.CoordinateArray');
goog.require('ol.extent');
goog.require('ol.geom');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SharedVertices');
/**
* @constructor
* @extends {ol.geom.Geometry}
* @param {ol.CoordinateArray} coordinates Vertex array (e.g.
* @param {ol.CoordinateArray} coordinates Array of coordinates (e.g.
* [[x0, y0], [x1, y1]]).
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/
ol.geom.LineString = function(coordinates, opt_shared) {
ol.geom.LineString = function(coordinates) {
goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
dimension = coordinates[0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/**
* @type {ol.geom.SharedVertices}
*/
this.vertices = vertices;
/**
* @type {number}
* Array of coordinates.
* @type {ol.CoordinateArray}
* @private
*/
this.sharedId_ = vertices.add(coordinates);
this.coordinates_ = coordinates;
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
this.dimension = coordinates[0].length;
goog.asserts.assert(this.dimension >= 2);
/**
@@ -62,7 +49,9 @@ goog.inherits(ol.geom.LineString, ol.geom.Geometry);
* @return {number} The vertex coordinate value.
*/
ol.geom.LineString.prototype.get = function(index, dim) {
return this.vertices.get(this.sharedId_, index, dim);
var coordinates = this.getCoordinates();
goog.asserts.assert(coordinates.length > index);
return coordinates[index][dim];
};
@@ -71,17 +60,7 @@ ol.geom.LineString.prototype.get = function(index, dim) {
* @return {ol.CoordinateArray} Coordinates array.
*/
ol.geom.LineString.prototype.getCoordinates = function() {
var count = this.getCount();
var coordinates = new Array(count);
var vertex;
for (var i = 0; i < count; ++i) {
vertex = new Array(this.dimension);
for (var j = 0; j < this.dimension; ++j) {
vertex[j] = this.get(i, j);
}
coordinates[i] = vertex;
}
return coordinates;
return this.coordinates_;
};
@@ -90,7 +69,7 @@ ol.geom.LineString.prototype.getCoordinates = function() {
* @return {number} The vertex count.
*/
ol.geom.LineString.prototype.getCount = function() {
return this.vertices.getCount(this.sharedId_);
return this.getCoordinates().length;
};
@@ -99,34 +78,12 @@ ol.geom.LineString.prototype.getCount = function() {
*/
ol.geom.LineString.prototype.getBounds = function() {
if (goog.isNull(this.bounds_)) {
var dimension = this.dimension,
vertices = this.vertices,
id = this.sharedId_,
count = vertices.getCount(id),
start = vertices.getStart(id),
end = start + (count * dimension),
coordinates = vertices.coordinates,
minX, maxX,
minY, maxY,
x, y, i;
minX = maxX = coordinates[start];
minY = maxY = coordinates[start + 1];
for (i = start + dimension; i < end; i += dimension) {
x = coordinates[i];
y = coordinates[i + 1];
if (x < minX) {
minX = x;
} else if (x > maxX) {
maxX = x;
var coordinates = this.getCoordinates();
var extent = ol.extent.createEmpty();
for (var i = 0, ii = coordinates.length; i < ii; ++i) {
ol.extent.extendCoordinate(extent, coordinates[i]);
}
if (y < minY) {
minY = y;
} else if (y > maxY) {
maxY = y;
}
}
this.bounds_ = [minX, minY, maxX, maxY];
this.bounds_ = extent;
}
return this.bounds_;
};
@@ -140,15 +97,6 @@ ol.geom.LineString.prototype.getType = function() {
};
/**
* Get the identifier used to mark this line in the shared vertices structure.
* @return {number} The identifier.
*/
ol.geom.LineString.prototype.getSharedId = function() {
return this.sharedId_;
};
/**
* Calculate the distance from a coordinate to this linestring.
*

View File

@@ -5,7 +5,6 @@ goog.require('ol.CoordinateArray');
goog.require('ol.geom.AbstractCollection');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LineString');
goog.require('ol.geom.SharedVertices');
@@ -13,21 +12,11 @@ goog.require('ol.geom.SharedVertices');
* @constructor
* @extends {ol.geom.AbstractCollection}
* @param {Array.<ol.CoordinateArray>} coordinates Coordinates array.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/
ol.geom.MultiLineString = function(coordinates, opt_shared) {
ol.geom.MultiLineString = function(coordinates) {
goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0][0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
// try to get dimension from first vertex in first line
dimension = coordinates[0][0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
var numParts = coordinates.length;
/**
@@ -35,13 +24,13 @@ ol.geom.MultiLineString = function(coordinates, opt_shared) {
*/
this.components = new Array(numParts);
for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.LineString(coordinates[i], vertices);
this.components[i] = new ol.geom.LineString(coordinates[i]);
}
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
this.dimension = coordinates[0][0].length;
};
goog.inherits(ol.geom.MultiLineString, ol.geom.AbstractCollection);
@@ -78,14 +67,13 @@ ol.geom.MultiLineString.prototype.distanceFromCoordinate =
* Create a multi-linestring geometry from an array of linestring geometries.
*
* @param {Array.<ol.geom.LineString>} geometries Array of geometries.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
* @return {ol.geom.MultiLineString} A new geometry.
*/
ol.geom.MultiLineString.fromParts = function(geometries, opt_shared) {
ol.geom.MultiLineString.fromParts = function(geometries) {
var count = geometries.length;
var coordinates = new Array(count);
for (var i = 0; i < count; ++i) {
coordinates[i] = geometries[i].getCoordinates();
}
return new ol.geom.MultiLineString(coordinates, opt_shared);
return new ol.geom.MultiLineString(coordinates);
};

View File

@@ -5,7 +5,6 @@ goog.require('ol.CoordinateArray');
goog.require('ol.geom.AbstractCollection');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.Point');
goog.require('ol.geom.SharedVertices');
@@ -13,26 +12,11 @@ goog.require('ol.geom.SharedVertices');
* @constructor
* @extends {ol.geom.AbstractCollection}
* @param {ol.CoordinateArray} coordinates Coordinates array.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/
ol.geom.MultiPoint = function(coordinates, opt_shared) {
ol.geom.MultiPoint = function(coordinates) {
goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
// try to get dimension from first vertex
dimension = coordinates[0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/**
* @type {ol.geom.SharedVertices}
*/
this.vertices = vertices;
var numParts = coordinates.length;
/**
@@ -40,13 +24,13 @@ ol.geom.MultiPoint = function(coordinates, opt_shared) {
*/
this.components = new Array(numParts);
for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.Point(coordinates[i], vertices);
this.components[i] = new ol.geom.Point(coordinates[i]);
}
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
this.dimension = coordinates[0].length;
};
goog.inherits(ol.geom.MultiPoint, ol.geom.AbstractCollection);
@@ -64,14 +48,13 @@ ol.geom.MultiPoint.prototype.getType = function() {
* Create a multi-point geometry from an array of point geometries.
*
* @param {Array.<ol.geom.Point>} geometries Array of geometries.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
* @return {ol.geom.MultiPoint} A new geometry.
*/
ol.geom.MultiPoint.fromParts = function(geometries, opt_shared) {
ol.geom.MultiPoint.fromParts = function(geometries) {
var count = geometries.length;
var coordinates = new Array(count);
for (var i = 0; i < count; ++i) {
coordinates[i] = geometries[i].getCoordinates();
}
return new ol.geom.MultiPoint(coordinates, opt_shared);
return new ol.geom.MultiPoint(coordinates);
};

View File

@@ -5,7 +5,6 @@ goog.require('ol.CoordinateArray');
goog.require('ol.geom.AbstractCollection');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');
@@ -14,21 +13,11 @@ goog.require('ol.geom.SharedVertices');
* @extends {ol.geom.AbstractCollection}
* @param {Array.<Array.<ol.CoordinateArray>>} coordinates Coordinates
* array.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/
ol.geom.MultiPolygon = function(coordinates, opt_shared) {
ol.geom.MultiPolygon = function(coordinates) {
goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0][0][0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
// try to get dimension from first vertex in first ring of the first poly
dimension = coordinates[0][0][0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
var numParts = coordinates.length;
/**
@@ -36,13 +25,13 @@ ol.geom.MultiPolygon = function(coordinates, opt_shared) {
*/
this.components = new Array(numParts);
for (var i = 0; i < numParts; ++i) {
this.components[i] = new ol.geom.Polygon(coordinates[i], vertices);
this.components[i] = new ol.geom.Polygon(coordinates[i]);
}
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
this.dimension = coordinates[0][0][0].length;
};
goog.inherits(ol.geom.MultiPolygon, ol.geom.AbstractCollection);
@@ -78,14 +67,13 @@ ol.geom.MultiPolygon.prototype.containsCoordinate = function(coordinate) {
* Create a multi-polygon geometry from an array of polygon geometries.
*
* @param {Array.<ol.geom.Polygon>} geometries Array of geometries.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
* @return {ol.geom.MultiPolygon} A new geometry.
*/
ol.geom.MultiPolygon.fromParts = function(geometries, opt_shared) {
ol.geom.MultiPolygon.fromParts = function(geometries) {
var count = geometries.length;
var coordinates = new Array(count);
for (var i = 0; i < count; ++i) {
coordinates[i] = geometries[i].getCoordinates();
}
return new ol.geom.MultiPolygon(coordinates, opt_shared);
return new ol.geom.MultiPolygon(coordinates);
};

View File

@@ -4,42 +4,28 @@ goog.require('goog.asserts');
goog.require('ol.Coordinate');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SharedVertices');
/**
* @constructor
* @extends {ol.geom.Geometry}
* @param {ol.Coordinate} coordinates Coordinates array (e.g. [x, y]).
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
* @param {ol.Coordinate} coordinates Coordinate values (e.g. [x, y]).
*/
ol.geom.Point = function(coordinates, opt_shared) {
ol.geom.Point = function(coordinates) {
goog.base(this);
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
dimension = coordinates.length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/**
* @type {ol.geom.SharedVertices}
*/
this.vertices = vertices;
/**
* @type {number}
* Point coordinate values.
* @type {ol.Coordinate}
* @private
*/
this.sharedId_ = vertices.add([coordinates]);
this.coordinates_ = coordinates;
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
this.dimension = coordinates.length;
goog.asserts.assert(this.dimension >= 2);
/**
@@ -57,7 +43,7 @@ goog.inherits(ol.geom.Point, ol.geom.Geometry);
* @return {number} The coordinate value.
*/
ol.geom.Point.prototype.get = function(dim) {
return this.vertices.get(this.sharedId_, 0, dim);
return this.getCoordinates()[dim];
};
@@ -79,11 +65,7 @@ ol.geom.Point.prototype.getBounds = function() {
* @return {ol.Coordinate} Coordinates array.
*/
ol.geom.Point.prototype.getCoordinates = function() {
var coordinates = new Array(this.dimension);
for (var i = 0; i < this.dimension; ++i) {
coordinates[i] = this.get(i);
}
return coordinates;
return this.coordinates_;
};
@@ -93,12 +75,3 @@ ol.geom.Point.prototype.getCoordinates = function() {
ol.geom.Point.prototype.getType = function() {
return ol.geom.GeometryType.POINT;
};
/**
* Get the identifier used to mark this point in the shared vertices structure.
* @return {number} The identifier.
*/
ol.geom.Point.prototype.getSharedId = function() {
return this.sharedId_;
};

View File

@@ -6,7 +6,6 @@ goog.require('ol.extent');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.SharedVertices');
@@ -21,32 +20,17 @@ goog.require('ol.geom.SharedVertices');
* @extends {ol.geom.Geometry}
* @param {Array.<ol.CoordinateArray>} coordinates Array of rings. First
* is outer, any remaining are inner.
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/
ol.geom.Polygon = function(coordinates, opt_shared) {
ol.geom.Polygon = function(coordinates) {
goog.base(this);
goog.asserts.assert(goog.isArray(coordinates[0][0]));
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
// try to get dimension from first vertex in first ring
dimension = coordinates[0][0].length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/**
* @private
* @type {ol.Coordinate}
*/
this.labelPoint_ = null;
/**
* @type {ol.geom.SharedVertices}
*/
this.vertices = vertices;
var numRings = coordinates.length;
/**
@@ -67,13 +51,13 @@ ol.geom.Polygon = function(coordinates, opt_shared) {
ringCoords.reverse();
}
}
this.rings[i] = new ol.geom.LinearRing(ringCoords, vertices);
this.rings[i] = new ol.geom.LinearRing(ringCoords);
}
/**
* @type {number}
*/
this.dimension = vertices.getDimension();
this.dimension = coordinates[0][0].length;
goog.asserts.assert(this.dimension >= 2);
};

View File

@@ -10,14 +10,6 @@ describe('ol.geom.LineString', function() {
expect(line).to.be.a(ol.geom.Geometry);
});
it('accepts shared vertices', function() {
var vertices = new ol.geom.SharedVertices();
var l1 = new ol.geom.LineString([[10, 20], [30, 40]], vertices);
var l2 = new ol.geom.LineString([[50, 60], [70, 80]], vertices);
expect(l1.getCoordinates()).to.eql([[10, 20], [30, 40]]);
expect(l2.getCoordinates()).to.eql([[50, 60], [70, 80]]);
});
});
describe('#dimension', function() {
@@ -56,42 +48,7 @@ describe('ol.geom.LineString', function() {
});
describe('#getSharedId()', function() {
it('returns identifiers', function() {
var vertices = new ol.geom.SharedVertices();
var l1 = new ol.geom.LineString([[10, 20], [30, 40]], vertices);
var l2 = new ol.geom.LineString(
[[50, 60], [70, 80], [90, 100]], vertices);
var id1 = l1.getSharedId();
var id2 = l2.getSharedId();
expect(vertices.coordinates).to.eql(
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]);
expect(vertices.getStart(id1)).to.be(0);
expect(vertices.getCount(id1)).to.be(2);
expect(vertices.get(id1, 0, 0)).to.be(10);
expect(vertices.get(id1, 0, 1)).to.be(20);
expect(vertices.get(id1, 1, 0)).to.be(30);
expect(vertices.get(id1, 1, 1)).to.be(40);
expect(vertices.getStart(id2)).to.be(4);
expect(vertices.getCount(id2)).to.be(3);
expect(vertices.get(id2, 0, 0)).to.be(50);
expect(vertices.get(id2, 0, 1)).to.be(60);
expect(vertices.get(id2, 1, 0)).to.be(70);
expect(vertices.get(id2, 1, 1)).to.be(80);
expect(vertices.get(id2, 2, 0)).to.be(90);
expect(vertices.get(id2, 2, 1)).to.be(100);
});
});
});
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LineString');
goog.require('ol.geom.SharedVertices');

View File

@@ -10,16 +10,6 @@ describe('ol.geom.Point', function() {
expect(point).to.be.a(ol.geom.Geometry);
});
it('accepts shared vertices', function() {
var vertices = new ol.geom.SharedVertices();
var p1 = new ol.geom.Point([10, 20], vertices);
var p2 = new ol.geom.Point([30, 40], vertices);
var p3 = new ol.geom.Point([50, 60], vertices);
expect(p1.getCoordinates()).to.eql([10, 20]);
expect(p2.getCoordinates()).to.eql([30, 40]);
expect(p3.getCoordinates()).to.eql([50, 60]);
});
it('throws when given with insufficient dimensions', function() {
expect(function() {
var point = new ol.geom.Point([1]);
@@ -65,43 +55,7 @@ describe('ol.geom.Point', function() {
});
describe('#getSharedId()', function() {
it('returns identifiers', function() {
var vertices = new ol.geom.SharedVertices();
var p1 = new ol.geom.Point([10, 20], vertices);
var p2 = new ol.geom.Point([30, 40], vertices);
var p3 = new ol.geom.Point([50, 60], vertices);
var id1 = p1.getSharedId();
var id2 = p2.getSharedId();
var id3 = p3.getSharedId();
expect(vertices.coordinates).to.eql(
[10, 20, 30, 40, 50, 60]);
expect(vertices.getStart(id1)).to.be(0);
expect(vertices.getCount(id1)).to.be(1);
expect(vertices.get(id1, 0, 0)).to.be(10);
expect(vertices.get(id1, 0, 1)).to.be(20);
expect(vertices.getStart(id2)).to.be(2);
expect(vertices.getCount(id2)).to.be(1);
expect(vertices.get(id2, 0, 0)).to.be(30);
expect(vertices.get(id2, 0, 1)).to.be(40);
expect(vertices.getStart(id3)).to.be(4);
expect(vertices.getCount(id3)).to.be(1);
expect(vertices.get(id3, 0, 0)).to.be(50);
expect(vertices.get(id3, 0, 1)).to.be(60);
});
});
});
goog.require('ol.geom.Geometry');
goog.require('ol.geom.Point');
goog.require('ol.geom.SharedVertices');

View File

@@ -14,16 +14,6 @@ describe('ol.geom.Polygon', function() {
expect(poly).to.be.a(ol.geom.Geometry);
});
it('accepts shared vertices', function() {
var vertices = new ol.geom.SharedVertices();
var p1 = new ol.geom.Polygon([outer], vertices);
var p2 = new ol.geom.Polygon([outer, inner1], vertices);
var p3 = new ol.geom.Polygon([outer, inner2], vertices);
expect(p1.getCoordinates()).to.eql([outer]);
expect(p2.getCoordinates()).to.eql([outer, inner1]);
expect(p3.getCoordinates()).to.eql([outer, inner2]);
});
});
describe('#rings', function() {
@@ -102,4 +92,3 @@ describe('ol.geom.Polygon', function() {
goog.require('ol.geom.Geometry');
goog.require('ol.geom.LinearRing');
goog.require('ol.geom.Polygon');
goog.require('ol.geom.SharedVertices');