Merge pull request #749 from openlayers/webgl-vector

Skeleton WebGL vector support
This commit is contained in:
Tom Payne
2013-06-17 07:08:33 -07:00
24 changed files with 1889 additions and 12 deletions

View File

@@ -371,6 +371,20 @@
return this;
};
/**
* Checks if the array sort of equals another array.
*
* @api public
*/
Assertion.prototype.arreql = function (obj) {
this.assert(
goog.array.equals(this.obj, obj)
, function(){ return 'expected ' + i(this.obj) + ' to sort of equal ' + i(obj) }
, function(){ return 'expected ' + i(this.obj) + ' to sort of not equal ' + i(obj) });
return this;
};
/**
* Checks if the obj sortof equals another.
*

View File

@@ -0,0 +1,71 @@
goog.provide('ol.test.geom2');
describe('ol.geom2', function() {
var buf, dim;
beforeEach(function() {
buf = new ol.structs.Buffer([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 12);
dim = 2;
});
describe('ol.geom2.getExtent', function() {
it('returns the expected extent', function() {
var extent = ol.geom2.getExtent(buf, dim);
expect(extent).to.eql([0, 10, 1, 11]);
});
it('returns the expect extent in three dimensions', function() {
var extent = ol.geom2.getExtent(buf, 3);
expect(extent).to.eql([0, 9, 1, 10, 2, 11]);
});
it('returns the expect extent in four dimensions', function() {
var extent = ol.geom2.getExtent(buf, 4);
expect(extent).to.eql([0, 8, 1, 9, 2, 10, 3, 11]);
});
it('returns the expect extent in six dimensions', function() {
var extent = ol.geom2.getExtent(buf, 6);
expect(extent).to.eql([0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11]);
});
});
describe('ol.geom2.packPoints', function() {
it('packs points as expected', function() {
var arr = [];
var offset = ol.geom2.packPoints(arr, 0, [[0, 1], [2, 3], [4, 5]], 2);
expect(offset).to.be(6);
expect(arr).to.eql([0, 1, 2, 3, 4, 5]);
});
it('raises an exception if dimensions do not match', function() {
expect(function() {
ol.geom2.packPoints([], 0, [[0, 1, 2]], 2);
}).to.throwException();
});
});
describe('ol.geom2.unpackPoints', function() {
it('unpacks points in two dimensions', function() {
var unpackedPoints = ol.geom2.unpackPoints([0, 1, 2, 3, 4, 5], 0, 6, 2);
expect(unpackedPoints).to.eql([[0, 1], [2, 3], [4, 5]]);
});
it('unpacks points in three dimensions', function() {
var unpackedPoints = ol.geom2.unpackPoints([0, 1, 2, 3, 4, 5], 0, 6, 3);
expect(unpackedPoints).to.eql([[0, 1, 2], [3, 4, 5]]);
});
});
});
goog.require('ol.geom2');
goog.require('ol.structs.Buffer');

View File

@@ -0,0 +1,330 @@
goog.provide('ol.test.geom2.LineStringCollection');
describe('ol.geom2.LineStringCollection', function() {
describe('createEmpty', function() {
it('creates an empty instance with the specified capacity', function() {
var lsc = ol.geom2.LineStringCollection.createEmpty(16);
expect(lsc.getCount()).to.be(0);
expect(lsc.buf.getArray()).to.have.length(32);
});
it('can create empty collections for higher dimensions', function() {
var lsc = ol.geom2.LineStringCollection.createEmpty(16, 3);
expect(lsc.getCount()).to.be(0);
expect(lsc.buf.getArray()).to.have.length(48);
});
});
describe('pack', function() {
it('packs an empty array', function() {
var lsc = ol.geom2.LineStringCollection.pack([]);
expect(lsc.buf.getArray()).to.be.empty();
expect(lsc.ranges).to.be.empty();
expect(lsc.dim).to.be(2);
});
it('packs an empty array with a capacity', function() {
var lsc = ol.geom2.LineStringCollection.pack([], 4);
expect(lsc.buf.getArray()).to.eql(
[NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]);
expect(lsc.ranges).to.be.empty();
expect(lsc.dim).to.be(2);
});
it('packs an array of line strings', function() {
var lsc = ol.geom2.LineStringCollection.pack(
[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9]]]);
expect(lsc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(lsc.getCount()).to.be(2);
expect(lsc.ranges[0]).to.eql([0, 6]);
expect(lsc.ranges[6]).to.eql([6, 10]);
expect(lsc.dim).to.be(2);
});
it('packs an array of line strings with a different dimension', function() {
var lsc = ol.geom2.LineStringCollection.pack(
[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]);
expect(lsc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
expect(lsc.getCount()).to.be(2);
expect(lsc.ranges[0]).to.eql([0, 6]);
expect(lsc.ranges[6]).to.eql([6, 12]);
expect(lsc.dim).to.be(3);
});
it('packs an array of line strings with extra capacity', function() {
var lsc = ol.geom2.LineStringCollection.pack(
[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9]]], 16);
expect(lsc.buf.getArray().slice(0, 10)).to.eql(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(lsc.buf.getArray()).to.have.length(32);
expect(lsc.getCount()).to.be(2);
expect(lsc.ranges[0]).to.eql([0, 6]);
expect(lsc.ranges[6]).to.eql([6, 10]);
expect(lsc.dim).to.be(2);
});
it('throws an error when dimensions are inconsistent', function() {
expect(function() {
var lsc = ol.geom2.LineStringCollection.pack([[[0, 1], [2, 3, 4]]]);
lsc = lsc; // suppress gjslint warning about unused variable
}).to.throwException();
});
it('throws an error when a line string is too short', function() {
expect(function() {
var lsc = ol.geom2.LineStringCollection.pack([[[0, 1]]]);
lsc = lsc; // suppress gjslint warning about unused variable
}).to.throwException();
});
it('throws an error when the capacity is too small', function() {
expect(function() {
var lsc = ol.geom2.LineStringCollection.pack(
[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9]]], 4);
lsc = lsc; // suppress gjslint warning about unused variable
}).to.throwException();
});
});
describe('with an empty instance with spare capacity', function() {
var lsc;
beforeEach(function() {
var buf = new ol.structs.Buffer(new Array(8), 0);
lsc = new ol.geom2.LineStringCollection(buf);
});
describe('add', function() {
it('adds a line string', function() {
var offset = lsc.add([[0, 1], [2, 3]]);
expect(offset).to.be(0);
expect(lsc.getCount()).to.be(1);
expect(lsc.ranges[0]).to.eql([0, 4]);
expect(lsc.dim).to.be(2);
});
});
describe('getCount', function() {
it('returns zero', function() {
expect(lsc.getCount()).to.be(0);
});
});
describe('getExtent', function() {
it('returns an empty extent', function() {
expect(ol.extent.isEmpty(lsc.getExtent())).to.be(true);
});
});
describe('getIndices', function() {
it('returns the expected value', function() {
expect(lsc.getIndices()).to.be.empty();
});
});
describe('remove', function() {
it('throws an exception', function() {
expect(function() {
lsc.remove(0);
}).to.throwException();
});
});
});
describe('with an initial line string', function() {
var lsc, offset;
beforeEach(function() {
var buf = new ol.structs.Buffer(new Array(8), 0);
lsc = new ol.geom2.LineStringCollection(buf);
offset = lsc.add([[0, 1], [2, 3]]);
});
describe('add', function() {
it('can add a second line string', function() {
var offset2 = lsc.add([[4, 5], [6, 7]]);
expect(offset2).to.be(4);
expect(lsc.getCount()).to.be(2);
expect(lsc.ranges[0]).to.eql([0, 4]);
expect(lsc.ranges[4]).to.eql([4, 8]);
expect(lsc.dim).to.be(2);
});
});
describe('get', function() {
it('returns the expected line string', function() {
expect(lsc.get(0)).to.eql([[0, 1], [2, 3]]);
});
});
describe('getCount', function() {
it('returns the expected value', function() {
expect(lsc.getCount()).to.be(1);
});
});
describe('getExtent', function() {
it('returns the expected extent', function() {
expect(lsc.getExtent()).to.eql([0, 2, 1, 3]);
});
});
describe('getIndices', function() {
it('returns the expected value', function() {
expect(lsc.getIndices()).to.arreql([0, 1]);
});
});
describe('remove', function() {
it('removes the line string', function() {
lsc.remove(0);
expect(lsc.getCount()).to.be(0);
});
});
describe('set', function() {
it('can update the line string in place', function() {
expect(lsc.set(0, [[4, 5], [6, 7]])).to.be(0);
expect(lsc.buf.getArray()).to.eql([4, 5, 6, 7, NaN, NaN, NaN, NaN]);
});
it('can replace the line string with a shorter one', function() {
expect(lsc.set(0, [[4, 5]])).to.be(0);
expect(lsc.buf.getArray()).to.eql([4, 5, NaN, NaN, NaN, NaN, NaN, NaN]);
});
it('can replace the line string with a longer one', function() {
expect(lsc.set(0, [[4, 5], [6, 7], [8, 9], [10, 11]])).to.be(0);
expect(lsc.buf.getArray()).to.eql([4, 5, 6, 7, 8, 9, 10, 11]);
});
});
describe('unpack', function() {
it('returns the expected value', function() {
expect(lsc.unpack()).to.eql([[[0, 1], [2, 3]]]);
});
});
});
describe('with multiple initial line strings', function() {
var lsc;
beforeEach(function() {
lsc = ol.geom2.LineStringCollection.pack(
[[[0, 1], [2, 3]], [[4, 5], [6, 7], [8, 9]]], 16);
});
describe('get', function() {
it('returns the expected values', function() {
expect(lsc.get(0)).to.eql([[0, 1], [2, 3]]);
expect(lsc.get(4)).to.eql([[4, 5], [6, 7], [8, 9]]);
});
});
describe('getCount', function() {
it('returns the expected value', function() {
expect(lsc.getCount()).to.be(2);
});
});
describe('getExtent', function() {
it('returns the expected value', function() {
expect(lsc.getExtent()).to.eql([0, 8, 1, 9]);
});
});
describe('getIndices', function() {
it('returns the expected value', function() {
expect(lsc.getIndices()).to.arreql([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.arreql([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.arreql([0, 1]);
});
});
describe('usage examples', function() {
it('allows the first line string to be replaced', function() {
lsc.remove(0);
expect(lsc.getCount()).to.be(1);
expect(lsc.add([[10, 11], [12, 13]])).to.be(0);
expect(lsc.getCount()).to.be(2);
expect(lsc.get(0)).to.eql([[10, 11], [12, 13]]);
});
it('will allocate at the end of the array', function() {
lsc.remove(0);
expect(lsc.getCount()).to.be(1);
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.arreql([2, 3, 3, 4, 5, 6, 6, 7]);
});
});
});
});
goog.require('ol.geom2.LineStringCollection');
goog.require('ol.extent');
goog.require('ol.structs.Buffer');

View File

@@ -0,0 +1,293 @@
goog.provide('ol.test.geom2.PointCollection');
describe('ol.geom2.PointCollection', function() {
describe('createEmpty', function() {
it('creates an empty instance with the specified capacity', function() {
var pc = ol.geom2.PointCollection.createEmpty(16);
expect(pc.getCount()).to.be(0);
expect(pc.buf.getArray()).to.have.length(32);
});
it('can create empty collections for higher dimensions', function() {
var pc = ol.geom2.PointCollection.createEmpty(16, 3);
expect(pc.getCount()).to.be(0);
expect(pc.buf.getArray()).to.have.length(48);
});
});
describe('pack', function() {
it('packs an empty array', function() {
var pc = ol.geom2.PointCollection.pack([]);
expect(pc.buf.getArray()).to.be.empty();
expect(pc.dim).to.be(2);
});
it('packs an empty array with a capacity', function() {
var pc = ol.geom2.PointCollection.pack([], 4);
expect(pc.buf.getArray()).to.eql([NaN, NaN, NaN, NaN]);
expect(pc.dim).to.be(2);
});
it('packs an empty array with a capacity and a dimension', function() {
var pc = ol.geom2.PointCollection.pack([], 8, 2);
expect(pc.buf.getArray()).to.eql(
[NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]);
expect(pc.dim).to.be(2);
});
it('packs a single point', function() {
var pc = ol.geom2.PointCollection.pack([[0, 1]]);
expect(pc.buf.getArray()).to.eql([0, 1]);
expect(pc.dim).to.be(2);
});
it('can pack multiple points', function() {
var pc = ol.geom2.PointCollection.pack([[0, 1], [2, 3], [4, 5]]);
expect(pc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5]);
expect(pc.dim).to.be(2);
});
it('can pack multiple points with a capacity', function() {
var pc = ol.geom2.PointCollection.pack([[0, 1], [2, 3], [4, 5]], 8);
expect(pc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, NaN, NaN]);
expect(pc.dim).to.be(2);
});
it('can pack a single 3-dimensional point', function() {
var pc = ol.geom2.PointCollection.pack([[0, 1, 2]]);
expect(pc.buf.getArray()).to.eql([0, 1, 2]);
expect(pc.dim).to.be(3);
});
it('can pack a multiple 3-dimensional points', function() {
var pc = ol.geom2.PointCollection.pack([[0, 1, 2], [4, 5, 6]]);
expect(pc.buf.getArray()).to.eql([0, 1, 2, 4, 5, 6]);
expect(pc.dim).to.be(3);
});
it('raises an error when not all points have the same dimension',
function() {
expect(function() {
var pc = ol.geom2.PointCollection.pack([[0, 1], [2]]);
pc = pc; // suppress gjslint warning about unused variable
}).to.throwException();
});
it('raises an error when the capacity is too small', function() {
expect(function() {
var pc = ol.geom2.PointCollection.pack([[0, 1], [2, 3], [4, 5]], 2);
pc = pc; // suppress gjslint warning about unused variable
}).to.throwException();
});
});
describe('with an empty buffer, with capacity for two points', function() {
var pc;
beforeEach(function() {
var buf = new ol.structs.Buffer(new Array(4), 0);
pc = new ol.geom2.PointCollection(buf);
});
describe('add', function() {
it('can add a first point', function() {
expect(pc.add([0, 1])).to.be(0);
expect(pc.buf.getArray()).to.eql([0, 1, NaN, NaN]);
});
it('can add a second point', function() {
expect(pc.add([0, 1])).to.be(0);
expect(pc.buf.getArray()).to.eql([0, 1, NaN, NaN]);
expect(pc.add([2, 3])).to.be(2);
expect(pc.buf.getArray()).to.eql([0, 1, 2, 3]);
});
it('raises an error when the third point is added', function() {
expect(pc.add([0, 1])).to.be(0);
expect(pc.buf.getArray()).to.eql([0, 1, NaN, NaN]);
expect(pc.add([2, 3])).to.be(2);
expect(pc.buf.getArray()).to.eql([0, 1, 2, 3]);
expect(function() {
pc.add([4, 5]);
}).to.throwException();
});
it('raises an error if a point of the wrong dimension is added',
function() {
expect(function() {
pc.add([0, 1, 2]);
}).to.throwException();
});
});
describe('getCount', function() {
it('returns 0', function() {
expect(pc.getCount()).to.be(0);
});
});
describe('getExtent', function() {
it('returns an empty extent', function() {
expect(ol.extent.isEmpty(pc.getExtent())).to.be(true);
});
});
describe('unpack', function() {
it('returns an empty array', function() {
expect(pc.unpack()).to.be.empty();
});
});
});
describe('with a partially populated instance', function() {
var dirtySet, pc;
beforeEach(function() {
dirtySet = new ol.structs.IntegerSet();
pc = ol.geom2.PointCollection.pack([[0, 1], [2, 3]], 8);
pc.buf.addDirtySet(dirtySet);
});
describe('add', function() {
it('can add more points', function() {
expect(pc.add([4, 5])).to.be(4);
expect(pc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, NaN, NaN]);
expect(pc.add([6, 7])).to.be(6);
expect(pc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, 6, 7]);
});
});
describe('get', function() {
it('returns the expected value for the first point', function() {
expect(pc.get(0)).to.eql([0, 1]);
});
it('returns the expected value for the second point', function() {
expect(pc.get(2)).to.eql([2, 3]);
});
});
describe('getCount', function() {
it('returns the expected value', function() {
expect(pc.getCount()).to.be(2);
});
});
describe('getExtent', function() {
it('returns the expected value', function() {
var extent = pc.getExtent();
expect(extent).to.eql([0, 2, 1, 3]);
});
});
describe('remove', function() {
it('can remove the first point', function() {
pc.remove(0);
expect(pc.buf.getArray()).to.eql([NaN, NaN, 2, 3, NaN, NaN, NaN, NaN]);
});
it('can remove the second point', function() {
pc.remove(2);
expect(pc.buf.getArray()).to.eql([0, 1, NaN, NaN, NaN, NaN, NaN, NaN]);
});
});
describe('set', function() {
it('marks the updated elements as dirty', function() {
pc.set(2, [4, 5]);
expect(pc.buf.getArray()).to.eql([0, 1, 4, 5, NaN, NaN, NaN, NaN]);
expect(dirtySet.getArray()).to.eql([2, 4]);
});
});
describe('unpack', function() {
it('returns the expect value', function() {
expect(pc.unpack()).to.eql([[0, 1], [2, 3]]);
});
});
describe('after removing the first point', function() {
beforeEach(function() {
pc.remove(0);
});
describe('getCount', function() {
it('returns the expected value', function() {
expect(pc.getCount()).to.be(1);
});
});
describe('unpack', function() {
it('returns the expected value', function() {
expect(pc.unpack()).to.eql([[2, 3]]);
});
});
});
});
describe('usage example', function() {
it('works as expected', function() {
var pc = ol.geom2.PointCollection.pack([[0, 1], [2, 3], [4, 5]], 8);
var dirtySet = new ol.structs.IntegerSet();
pc.buf.addDirtySet(dirtySet);
expect(pc.buf.getArray()).to.eql([0, 1, 2, 3, 4, 5, NaN, NaN]);
expect(pc.unpack()).to.have.length(3);
expect(pc.getCount()).to.be(3);
expect(pc.get(2)).to.eql([2, 3]);
pc.remove(2);
expect(pc.buf.getArray()).to.eql([0, 1, NaN, NaN, 4, 5, NaN, NaN]);
expect(pc.unpack()).to.have.length(2);
expect(pc.getCount()).to.be(2);
expect(pc.add([6, 7])).to.be(2);
expect(pc.buf.getArray()).to.eql([0, 1, 6, 7, 4, 5, NaN, NaN]);
expect(pc.unpack()).to.have.length(3);
expect(pc.getCount()).to.be(3);
expect(dirtySet.getArray()).to.eql([2, 4]);
});
});
});
goog.require('ol.geom2.PointCollection');
goog.require('ol.extent');
goog.require('ol.structs.Buffer');
goog.require('ol.structs.IntegerSet');

View File

@@ -220,6 +220,41 @@ describe('ol.structs.Buffer', function() {
});
describe('with a populated instance', function() {
var b;
beforeEach(function() {
b = new ol.structs.Buffer([1234567.1234567, -7654321.7654321]);
});
describe('getSplit32', function() {
it('returns the expected value', function() {
var split32 = b.getSplit32();
expect(split32).to.be.a(Float32Array);
expect(split32).to.have.length(4);
expect(split32[0]).to.roughlyEqual(1179648.0, 1e1);
expect(split32[1]).to.roughlyEqual(54919.12345670001, 1e-2);
expect(split32[2]).to.roughlyEqual(-7602176.0, 1e1);
expect(split32[3]).to.roughlyEqual(-52145.76543209981, 1e-2);
});
it('tracks updates', function() {
b.getSplit32();
b.getArray()[0] = 0;
b.markDirty(1, 0);
var split32 = b.getSplit32();
expect(split32).to.be.a(Float32Array);
expect(split32).to.have.length(4);
expect(split32[0]).to.be(0);
expect(split32[1]).to.be(0);
expect(split32[2]).to.roughlyEqual(-7602176.0, 1e1);
expect(split32[3]).to.roughlyEqual(-52145.76543209981, 1e-2);
});
});
});
describe('usage tests', function() {
it('allows multiple adds and removes', function() {