Add ol.geom2.LineStringCollection
This commit is contained in:
2
src/ol/geom2/linestringcollection.exports
Normal file
2
src/ol/geom2/linestringcollection.exports
Normal file
@@ -0,0 +1,2 @@
|
||||
@exportSymbol ol.geom2.LineStringCollection
|
||||
@exportSymbol ol.geom2.LineStringCollection.pack
|
||||
183
src/ol/geom2/linestringcollection.js
Normal file
183
src/ol/geom2/linestringcollection.js
Normal file
@@ -0,0 +1,183 @@
|
||||
goog.provide('ol.geom2.LineString');
|
||||
goog.provide('ol.geom2.LineStringCollection');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.object');
|
||||
goog.require('ol.geom2');
|
||||
goog.require('ol.structs.Buffer');
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Array.<Array.<number>>}
|
||||
*/
|
||||
ol.geom2.LineString;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {ol.structs.Buffer} buf Buffer.
|
||||
* @param {Object.<number, Array.<number>>=} opt_ranges Ranges.
|
||||
* @param {number=} opt_dim Dimension.
|
||||
*/
|
||||
ol.geom2.LineStringCollection = function(buf, opt_ranges, opt_dim) {
|
||||
|
||||
/**
|
||||
* @type {ol.structs.Buffer}
|
||||
*/
|
||||
this.buf = buf;
|
||||
|
||||
/**
|
||||
* @type {Object.<number, Array.<number>>}
|
||||
*/
|
||||
this.ranges = goog.isDef(opt_ranges) ? opt_ranges : {};
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
this.dim = goog.isDef(opt_dim) ? opt_dim : 2;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} capacity Capacity.
|
||||
* @param {number=} opt_dim Dimension.
|
||||
* @return {ol.geom2.LineStringCollection} Line string collection.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.createEmpty = function(capacity, opt_dim) {
|
||||
var dim = goog.isDef(opt_dim) ? opt_dim : 2;
|
||||
var buf = new ol.structs.Buffer(new Array(capacity * dim), 0);
|
||||
return new ol.geom2.LineStringCollection(buf, undefined, dim);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Array.<ol.geom2.LineString>} unpackedLineStrings Unpacked line
|
||||
* strings.
|
||||
* @param {number=} opt_capacity Capacity.
|
||||
* @param {number=} opt_dim Dimension.
|
||||
* @return {ol.geom2.LineStringCollection} Line string collection.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.pack =
|
||||
function(unpackedLineStrings, opt_capacity, opt_dim) {
|
||||
var i;
|
||||
var n = unpackedLineStrings.length;
|
||||
var dim = goog.isDef(opt_dim) ? opt_dim :
|
||||
n > 0 ? unpackedLineStrings[0][0].length : 2;
|
||||
var capacity;
|
||||
if (goog.isDef(opt_capacity)) {
|
||||
capacity = opt_capacity;
|
||||
} else {
|
||||
capacity = 0;
|
||||
for (i = 0; i < n; ++i) {
|
||||
capacity += unpackedLineStrings[i].length;
|
||||
}
|
||||
}
|
||||
capacity *= dim;
|
||||
var arr = new Array(capacity);
|
||||
/** @type {Object.<number, Array.<number>>} */
|
||||
var ranges = {};
|
||||
var offset = 0;
|
||||
var start;
|
||||
for (i = 0; i < n; ++i) {
|
||||
goog.asserts.assert(unpackedLineStrings[i].length > 1);
|
||||
start = offset;
|
||||
offset = ol.geom2.packPoints(arr, offset, unpackedLineStrings[i], dim);
|
||||
ranges[start] = [start, offset];
|
||||
}
|
||||
goog.asserts.assert(offset <= capacity);
|
||||
var buf = new ol.structs.Buffer(arr, offset);
|
||||
return new ol.geom2.LineStringCollection(buf, ranges, dim);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {ol.geom2.LineString} lineString Line string.
|
||||
* @return {number} Offset.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.prototype.add = function(lineString) {
|
||||
var n = lineString.length * this.dim;
|
||||
var offset = this.buf.allocate(n);
|
||||
goog.asserts.assert(offset != -1);
|
||||
this.ranges[offset] = [offset, offset + n];
|
||||
ol.geom2.packPoints(this.buf.getArray(), offset, lineString, this.dim);
|
||||
return offset;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} offset Offset.
|
||||
* @return {ol.geom2.LineString} Line string.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.prototype.get = function(offset) {
|
||||
goog.asserts.assert(offset in this.ranges);
|
||||
var range = this.ranges[offset];
|
||||
return ol.geom2.unpackPoints(
|
||||
this.buf.getArray(), range[0], range[1], this.dim);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {number} Count.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.prototype.getCount = function() {
|
||||
return goog.object.getCount(this.ranges);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.Extent} Extent.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.prototype.getExtent = function() {
|
||||
return ol.geom2.getExtent(this.buf, this.dim);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} offset Offset.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.prototype.remove = function(offset) {
|
||||
goog.asserts.assert(offset in this.ranges);
|
||||
var range = this.ranges[offset];
|
||||
this.buf.remove(range[1] - range[0], range[0]);
|
||||
delete this.ranges[offset];
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} offset Offset.
|
||||
* @param {ol.geom2.LineString} lineString Line string.
|
||||
* @return {number} Offset.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.prototype.set = function(offset, lineString) {
|
||||
var dim = this.dim;
|
||||
goog.asserts.assert(offset in this.ranges);
|
||||
var range = this.ranges[offset];
|
||||
if (lineString.length * dim == range[1] - range[0]) {
|
||||
ol.geom2.packPoints(this.buf.getArray(), range[0], lineString, dim);
|
||||
this.buf.markDirty(range[1] - range[0], range[0]);
|
||||
return offset;
|
||||
} else {
|
||||
this.remove(offset);
|
||||
return this.add(lineString);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {Array.<ol.geom2.LineString>} Line strings.
|
||||
*/
|
||||
ol.geom2.LineStringCollection.prototype.unpack = function() {
|
||||
var dim = this.dim;
|
||||
var n = this.getCount();
|
||||
var lineStrings = new Array(n);
|
||||
var i = 0;
|
||||
var offset, range;
|
||||
for (offset in this.ranges) {
|
||||
range = this.ranges[Number(offset)];
|
||||
lineStrings[i++] = ol.geom2.unpackPoints(
|
||||
this.buf.getArray(), range[0], range[1], dim);
|
||||
}
|
||||
return lineStrings;
|
||||
};
|
||||
Reference in New Issue
Block a user