Skip to content

Commit

Permalink
Merge pull request #2531 from AnalyticalGraphicsInc/normalize-quatern…
Browse files Browse the repository at this point in the history
…ions

Normalize quaternions on CZML load
  • Loading branch information
pjcozzi committed Mar 2, 2015
2 parents 054df9a + 7411a8d commit 1d581d9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Source/DataSources/CzmlDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,34 @@ define([
return result;
}

function normalizePackedQuaternionArray(array, startingIndex) {
var x = array[startingIndex];
var y = array[startingIndex + 1];
var z = array[startingIndex + 2];
var w = array[startingIndex + 3];

var inverseMagnitude = 1.0 / Math.sqrt(x * x + y * y + z * z + w * w);
array[startingIndex] = x * inverseMagnitude;
array[startingIndex + 1] = y * inverseMagnitude;
array[startingIndex + 2] = z * inverseMagnitude;
array[startingIndex + 3] = w * inverseMagnitude;
}

function unwrapQuaternionInterval(czmlInterval) {
var unitQuaternion = czmlInterval.unitQuaternion;
if (defined(unitQuaternion)) {
if (unitQuaternion.length === 4) {
normalizePackedQuaternionArray(unitQuaternion, 0);
return unitQuaternion;
}

for (var i = 1; i < unitQuaternion.length; i += 5) {
normalizePackedQuaternionArray(unitQuaternion, i);
}
}
return unitQuaternion;
}

function unwrapInterval(type, czmlInterval, sourceUri) {
/*jshint sub:true*/
switch (type) {
Expand Down Expand Up @@ -339,7 +367,7 @@ define([
case Array:
return czmlInterval.array;
case Quaternion:
return czmlInterval.unitQuaternion;
return unwrapQuaternionInterval(czmlInterval);
case Rectangle:
return unwrapRectangleInterval(czmlInterval);
case Uri:
Expand Down
38 changes: 38 additions & 0 deletions Specs/DataSources/CzmlDataSourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,44 @@ defineSuite([
expect(entity.orientation.getValue(Iso8601.MINIMUM_VALUE)).toEqual(new Quaternion(0.0, 0.0, 0.0, 1.0));
});

it('CZML Orientation is normalized on load.', function() {
var packet = {
orientation : {
unitQuaternion : [0.0, 0.0, 0.7071067, 0.7071067]
}
};

var expected = new Quaternion(0.0, 0.0, 0.7071067, 0.7071067);
Quaternion.normalize(expected, expected);

var dataSource = new CzmlDataSource();
dataSource.load(makePacket(packet));
var entity = dataSource.entities.values[0];
expect(entity.orientation.getValue(Iso8601.MINIMUM_VALUE)).toEqual(expected);
});

it('CZML Orientation is normalized on load.', function() {
var time1 = '2000-01-01T00:00:00Z';
var time2 = '2000-01-01T00:00:01Z';
var packet = {
orientation : {
unitQuaternion : [time1, 0.0, 0.0, 0.7071067, 0.7071067, time2, 0.7071067, 0.7071067, 0.0, 0.0]
}
};

var expected1 = new Quaternion(0.0, 0.0, 0.7071067, 0.7071067);
Quaternion.normalize(expected1, expected1);

var expected2 = new Quaternion(0.7071067, 0.7071067, 0.0, 0.0);
Quaternion.normalize(expected2, expected2);

var dataSource = new CzmlDataSource();
dataSource.load(makePacket(packet));
var entity = dataSource.entities.values[0];
expect(entity.orientation.getValue(JulianDate.fromIso8601(time1))).toEqual(expected1);
expect(entity.orientation.getValue(JulianDate.fromIso8601(time2))).toEqual(expected2);
});

it('positions work with cartesians.', function() {
var expectedResult = [new Cartesian3(1.0, 2.0, 3.0), new Cartesian3(5.0, 6.0, 7.0)];

Expand Down

0 comments on commit 1d581d9

Please sign in to comment.