Allow geometries to use a shared vertex array

The ol.geom.SharedVertices structure represents a flattened array of vertex coordinates.  This is intended to support optimal WebGL rendering.
This commit is contained in:
Tim Schaub
2013-03-02 18:39:24 +01:00
parent bdfa2cc88c
commit b52d283641
23 changed files with 1086 additions and 217 deletions

View File

@@ -2,30 +2,45 @@ goog.provide('ol.geom.Point');
goog.require('goog.asserts');
goog.require('ol.Extent');
goog.require('ol.geom.Coordinate');
goog.require('ol.geom.Geometry');
goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SharedVertices');
goog.require('ol.geom.Vertex');
/**
* @constructor
* @extends {ol.geom.Geometry}
* @param {ol.geom.Coordinate} coordinates Coordinates array (e.g. [x, y]).
* @param {ol.geom.Vertex} coordinates Coordinates array (e.g. [x, y]).
* @param {ol.geom.SharedVertices=} opt_shared Shared vertices.
*/
ol.geom.Point = function(coordinates) {
ol.geom.Point = function(coordinates, opt_shared) {
goog.base(this);
var vertices = opt_shared,
dimension;
if (!goog.isDef(vertices)) {
dimension = coordinates.length;
vertices = new ol.geom.SharedVertices({dimension: dimension});
}
/**
* @type {Array}
* @type {ol.geom.SharedVertices}
*/
this.coordinates = coordinates;
this.vertices = vertices;
/**
* @type {string}
* @private
*/
this.sharedId_ = vertices.add([coordinates]);
/**
* @type {number}
*/
this.dimension = coordinates.length;
this.dimension = vertices.getDimension();
goog.asserts.assert(this.dimension >= 2);
/**
@@ -38,22 +53,53 @@ ol.geom.Point = function(coordinates) {
goog.inherits(ol.geom.Point, ol.geom.Geometry);
/**
* @param {number} dim Coordinate dimension.
* @return {number} The coordinate value.
*/
ol.geom.Point.prototype.get = function(dim) {
return this.vertices.get(this.sharedId_, 0, dim);
};
/**
* @inheritDoc
*/
ol.geom.Point.prototype.getBounds = function() {
if (goog.isNull(this.bounds_)) {
var x = this.coordinates[0],
y = this.coordinates[1];
var x = this.get(0),
y = this.get(1);
this.bounds_ = new ol.Extent(x, y, x, y);
}
return this.bounds_;
};
/**
* @inheritDoc
* @return {ol.geom.Vertex} 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;
};
/**
* @inheritDoc
*/
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 {string} The identifier.
*/
ol.geom.Point.prototype.getSharedId = function() {
return this.sharedId_;
};