Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly use unitQuaternion czml property in Cesium

Tags:

cesium

czml

I use the gltf branch of Cesium, and I want to display 3d model of planes. To do that I create czmlDataSource that I load and add to dataSources.

The problem is that I can't figure out how to calculate orientation quaternion to have planes parallel to the ground at a given lat,lon,alt heading north by default (and then impact their heading, eventually pitch and roll).

here is what I do to compute my actual quaternions, but the 3d models are not correctly oriented (and I don't know how to change heading, pitch, roll) :

    var geoPosition = new Cesium.Cartographic(Cesium.Math.toRadians(inputPosition.lon), Cesium.Math.toRadians(inputPosition.lat), inputPosition.alt);
    var cartesianPosition = Cesium.Ellipsoid.WGS84.cartographicToCartesian(geoPosition);

    var euler = [cartesianPosition.x, cartesianPosition.y, cartesianPosition.z];
    var qx = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_X, euler[0]);
    var qy = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_Y, euler[1]);
    var qz = Cesium.Quaternion.fromAxisAngle(Cesium.Cartesian3.UNIT_Z, euler[2]);
    var qt = Cesium.Quaternion.multiply(qz, qy);
    var q = Cesium.Quaternion.multiply(qt, qx);
    Cesium.Quaternion.normalize(q, q);

var czmlSrc = [{
       "orientation": {
            "epoch": "2012-08-04T16:00:00Z",
            "interpolationAlgorithm": "LINEAR",
            "interpolationDegree": 1,
            "unitQuaternion": [0, q.x,q.y,q.z,q.w,
                            3600, q.x,q.y,q.z,q.w]
        }
   }];
like image 624
typedef Avatar asked Jan 24 '14 17:01

typedef


2 Answers

CZML currently has the orientation "backwards" compared to Cesium's convention. So if you are working with CZML you actually want the conjugate of the orientation. We plan on fixing this in a major CZML update within a few months, but didn't want to break all of the existing documents out there with the current format. When we do update, the plan is to try and do it in a backwards compatible way, so existing CZML will still work.

like image 109
Matthew Amato Avatar answered Sep 30 '22 12:09

Matthew Amato


var C3 = Cesium.Cartesian3
var Q  = Cesium.Quaternion
// radians everywhere
var q  =      Q.fromAxisAngle(C3.UNIT_X, -o.pitch              ) // or maybe roll first?
Q.multiply(q, Q.fromAxisAngle(C3.UNIT_Y, -o.roll               ), q)
Q.multiply(q, Q.fromAxisAngle(C3.UNIT_Z,  o.heading - Math.PI/2), q)
Q.multiply(q, Q.fromAxisAngle(C3.UNIT_Y,  o.lat     - Math.PI/2), q)
Q.multiply(q, Q.fromAxisAngle(C3.UNIT_Z, -o.lon                ), q)
Q.conjugate(q, q)

czml.process([{
    position   : { cartographicRadians: [o.lon, o.lat, 0] }
  , orientation: { unitQuaternion: [q.x, q.y, q.z, q.w] }
  , model      : { gltf: model.key + '.gltf' }
}])

Lots of calcs here. Don't know if and how this could be optimized.

like image 26
sas18 Avatar answered Sep 30 '22 14:09

sas18