Add ol.geom2 utility functions
This commit is contained in:
71
src/ol/geom2/geom2.js
Normal file
71
src/ol/geom2/geom2.js
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
goog.provide('ol.geom2');
|
||||||
|
|
||||||
|
goog.require('goog.asserts');
|
||||||
|
goog.require('ol.Extent');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ol.structs.Buffer} buf Buffer.
|
||||||
|
* @param {number} dim Dimension.
|
||||||
|
* @return {ol.Extent} Extent.
|
||||||
|
*/
|
||||||
|
ol.geom2.getExtent = function(buf, dim) {
|
||||||
|
var extent = new Array(2 * dim);
|
||||||
|
var extentIndex = 0;
|
||||||
|
var i;
|
||||||
|
for (i = 0; i < dim; ++i) {
|
||||||
|
extent[extentIndex++] = Infinity;
|
||||||
|
extent[extentIndex++] = -Infinity;
|
||||||
|
}
|
||||||
|
var bufArr = buf.getArray();
|
||||||
|
buf.forEachRange(function(start, stop) {
|
||||||
|
var extentIndex, i, j;
|
||||||
|
for (i = start; i < stop; i += dim) {
|
||||||
|
extentIndex = 0;
|
||||||
|
for (j = 0; j < dim; ++j) {
|
||||||
|
extent[extentIndex++] = Math.min(extent[2 * j], bufArr[i + j]);
|
||||||
|
extent[extentIndex++] = Math.max(extent[2 * j + 1], bufArr[i + j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return extent;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Array.<number>} arr Array.
|
||||||
|
* @param {number} offset Offset.
|
||||||
|
* @param {Array.<Array.<number>>} unpackedPoints Unpacked points.
|
||||||
|
* @param {number} dim Dimension.
|
||||||
|
* @return {number} Offset.
|
||||||
|
*/
|
||||||
|
ol.geom2.packPoints = function(arr, offset, unpackedPoints, dim) {
|
||||||
|
var n = unpackedPoints.length;
|
||||||
|
var i, j, point;
|
||||||
|
for (i = 0; i < n; ++i) {
|
||||||
|
point = unpackedPoints[i];
|
||||||
|
goog.asserts.assert(point.length == dim);
|
||||||
|
for (j = 0; j < dim; ++j) {
|
||||||
|
arr[offset++] = point[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return offset;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Array.<number>} arr Array.
|
||||||
|
* @param {number} offset Offset.
|
||||||
|
* @param {number} end End.
|
||||||
|
* @param {number} dim Dimension.
|
||||||
|
* @return {Array.<Array.<number>>} Unpacked points.
|
||||||
|
*/
|
||||||
|
ol.geom2.unpackPoints = function(arr, offset, end, dim) {
|
||||||
|
var unpackedPoints = new Array((end - offset) / dim);
|
||||||
|
var i = 0;
|
||||||
|
var j;
|
||||||
|
for (j = offset; j < end; j += dim) {
|
||||||
|
unpackedPoints[i++] = arr.slice(j, j + dim);
|
||||||
|
}
|
||||||
|
return unpackedPoints;
|
||||||
|
};
|
||||||
71
test/spec/ol/geom2/geom2.test.js
Normal file
71
test/spec/ol/geom2/geom2.test.js
Normal 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');
|
||||||
Reference in New Issue
Block a user