Add ol.geom2.LineStringCollection#getIndices

This commit is contained in:
Tom Payne
2013-05-31 12:58:02 +02:00
parent db2f805ed9
commit 3225a07f6f
2 changed files with 51 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
goog.provide('ol.geom2.LineString');
goog.provide('ol.geom2.LineStringCollection');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.object');
goog.require('ol.geom2');
@@ -134,6 +135,29 @@ ol.geom2.LineStringCollection.prototype.getExtent = function() {
};
/**
* @return {Array} Indices.
*/
ol.geom2.LineStringCollection.prototype.getIndices = function() {
// FIXME cache and track dirty / track output length
// FIXME return UInt16Array?
var dim = this.dim;
var offsets = goog.array.map(goog.object.getKeys(this.ranges), Number);
goog.array.sort(offsets);
var n = offsets.length;
var indices = [];
var i, j, range, stop;
for (i = 0; i < n; ++i) {
range = this.ranges[offsets[i]];
stop = range[1] / dim - 1;
for (j = range[0] / dim; j < stop; ++j) {
indices.push(j, j + 1);
}
}
return indices;
};
/**
* @param {number} offset Offset.
*/

View File

@@ -128,6 +128,14 @@ describe('ol.geom2.LineStringCollection', function() {
});
describe('getIndices', function() {
it('returns the expected value', function() {
expect(lsc.getIndices()).to.be.empty();
});
});
describe('remove', function() {
it('throws an exception', function() {
@@ -186,6 +194,14 @@ describe('ol.geom2.LineStringCollection', function() {
});
describe('getIndices', function() {
it('returns the expected value', function() {
expect(lsc.getIndices()).to.eql([0, 1]);
});
});
describe('remove', function() {
it('removes the line string', function() {
@@ -257,18 +273,28 @@ describe('ol.geom2.LineStringCollection', function() {
});
describe('getIndices', function() {
it('returns the expected value', function() {
expect(lsc.getIndices()).to.eql([0, 1, 2, 3, 3, 4]);
});
});
describe('remove', function() {
it('can remove the first line string', function() {
lsc.remove(0);
expect(lsc.getCount()).to.be(1);
expect(lsc.get(4)).to.eql([[4, 5], [6, 7], [8, 9]]);
expect(lsc.getIndices()).to.eql([2, 3, 3, 4]);
});
it('can remove the second line string', function() {
lsc.remove(4);
expect(lsc.getCount()).to.be(1);
expect(lsc.get(0)).to.eql([[0, 1], [2, 3]]);
expect(lsc.getIndices()).to.eql([0, 1]);
});
});
@@ -289,6 +315,7 @@ describe('ol.geom2.LineStringCollection', function() {
expect(lsc.add([[10, 11], [12, 13], [14, 15]])).to.be(10);
expect(lsc.getCount()).to.be(2);
expect(lsc.get(10)).to.eql([[10, 11], [12, 13], [14, 15]]);
expect(lsc.getIndices()).to.eql([2, 3, 3, 4, 5, 6, 6, 7]);
});
});