Factor out ol.geom.flat.flip

This commit is contained in:
Tom Payne
2014-03-12 12:55:30 +01:00
parent 266c43dc78
commit 9ca996725e
4 changed files with 76 additions and 66 deletions

View File

@@ -0,0 +1,37 @@
goog.provide('ol.geom.flat.flip');
goog.require('goog.asserts');
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {number} end End.
* @param {number} stride Stride.
* @param {Array.<number>=} opt_dest Destination.
* @param {number=} opt_destOffset Destination offset.
* @return {Array.<number>} Flat coordinates.
*/
ol.geom.flat.flip.flipXY =
function(flatCoordinates, offset, end, stride, opt_dest, opt_destOffset) {
var dest, destOffset;
if (goog.isDef(opt_dest)) {
dest = opt_dest;
destOffset = goog.isDef(opt_destOffset) ? opt_destOffset : 0;
} else {
goog.asserts.assert(!goog.isDef(opt_destOffset));
dest = [];
destOffset = 0;
}
var j, k;
for (j = offset; j < end; ) {
var x = flatCoordinates[j++];
dest[destOffset++] = flatCoordinates[j++];
dest[destOffset++] = x;
for (k = 2; k < stride; ++k) {
dest[destOffset++] = flatCoordinates[j++];
}
}
dest.length = destOffset;
return dest;
};

View File

@@ -1,45 +1,10 @@
goog.provide('ol.geom.flat');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.vec.Mat4');
goog.require('ol.extent');
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {number} end End.
* @param {number} stride Stride.
* @param {Array.<number>=} opt_dest Destination.
* @param {number=} opt_destOffset Destination offset.
* @return {Array.<number>} Flat coordinates.
*/
ol.geom.flat.flipXY =
function(flatCoordinates, offset, end, stride, opt_dest, opt_destOffset) {
var dest, destOffset;
if (goog.isDef(opt_dest)) {
dest = opt_dest;
destOffset = goog.isDef(opt_destOffset) ? opt_destOffset : 0;
} else {
goog.asserts.assert(!goog.isDef(opt_destOffset));
dest = [];
destOffset = 0;
}
var j, k;
for (j = offset; j < end; ) {
var x = flatCoordinates[j++];
dest[destOffset++] = flatCoordinates[j++];
dest[destOffset++] = x;
for (k = 2; k < stride; ++k) {
dest[destOffset++] = flatCoordinates[j++];
}
}
dest.length = destOffset;
return dest;
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.

View File

@@ -0,0 +1,39 @@
goog.provide('ol.test.geom.flat.flip');
describe('ol.geom.flat.flip', function() {
describe('ol.geom.flat.flip.flipXY', function() {
it('can flip XY coordinates', function() {
var flatCoordinates = ol.geom.flat.flip.flipXY([1, 2, 3, 4], 0, 4, 2);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);
});
it('can flip XY coordinates while preserving other dimensions', function() {
var flatCoordinates = ol.geom.flat.flip.flipXY(
[1, 2, 3, 4, 5, 6, 7, 8], 0, 8, 4);
expect(flatCoordinates).to.eql([2, 1, 3, 4, 6, 5, 7, 8]);
});
it('can flip XY coordinates in place', function() {
var flatCoordinates = [1, 2, 3, 4];
expect(ol.geom.flat.flip.flipXY(
flatCoordinates, 0, 4, 2, flatCoordinates)).to.be(flatCoordinates);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);
});
it('can flip XY coordinates in place while preserving other dimensions',
function() {
var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9];
expect(ol.geom.flat.flip.flipXY(
flatCoordinates, 0, 9, 3, flatCoordinates)).
to.be(flatCoordinates);
expect(flatCoordinates).to.eql([2, 1, 3, 5, 4, 6, 8, 7, 9]);
});
});
});
goog.require('ol.geom.flat');
goog.require('ol.geom.flat.flip');

View File

@@ -3,37 +3,6 @@ goog.provide('ol.test.geom.flat');
describe('ol.geom.flat', function() {
describe('ol.geom.flat.flipXY', function() {
it('can flip XY coordinates', function() {
var flatCoordinates = ol.geom.flat.flipXY([1, 2, 3, 4], 0, 4, 2);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);
});
it('can flip XY coordinates while preserving other dimensions', function() {
var flatCoordinates = ol.geom.flat.flipXY(
[1, 2, 3, 4, 5, 6, 7, 8], 0, 8, 4);
expect(flatCoordinates).to.eql([2, 1, 3, 4, 6, 5, 7, 8]);
});
it('can flip XY coordinates in place', function() {
var flatCoordinates = [1, 2, 3, 4];
expect(ol.geom.flat.flipXY(flatCoordinates, 0, 4, 2, flatCoordinates)).
to.be(flatCoordinates);
expect(flatCoordinates).to.eql([2, 1, 4, 3]);
});
it('can flip XY coordinates in place while preserving other dimensions',
function() {
var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9];
expect(ol.geom.flat.flipXY(
flatCoordinates, 0, 9, 3, flatCoordinates)).
to.be(flatCoordinates);
expect(flatCoordinates).to.eql([2, 1, 3, 5, 4, 6, 8, 7, 9]);
});
});
describe('ol.geom.flat.inflateCoordinates', function() {
it('inflates coordinates', function() {