I want to use Python to develop the transformation module for my application (from .xml to .czml). However, I’m stuck on the Quaternion transformation part. CesiumJS provides the function Transforms.headingPitchRollQuaternion(center, hpr) to correctly translate the Quaternion from longitude, latitude, and HPR (heading, pitch, roll). How can I perform this transformation using Python? It seems that it’s not just converting Euler angles to Quaternion, but also considering the longitude and latitude. Does anyone have relevant experience? Please help me.
1 Answer
The reason Cesium's Transforms.headingPitchRollQuaternion(center, hpr) requires a location (center) is because it is specifically transforming from a local-up HPR to Cesium's global ECF (or ECEF as some say) Earth-Centered (Earth-)Fixed frame.
So for the purpose of this function, the incoming Heading, Pitch, Roll assume that +Z is locally "up" at some point (center) somewhere on the globe. The output quaternion assumes a global +Z aligned with the planet's North Pole, which is how Cesium renders objects in 3D. This local-to-ECF transformation requires the location of the "local" system, and implicitly requires Cesium's copy of the WGS84 ellipsoid to make the leap from local to global.
Think of it this way: If an aircraft flies due East for many hours, the HPR doesn't need to change, but the location is changing. From the point of view of Cesium's rendering engine, the aircraft is (very slowly) changing orientation as its path wraps substantially around the globe. The 3D model's orientation quaternion must take this into account during travel.
Unfortunately I don't know of a Python implementation of this. Also I'm not sure from your question what data is inside your XML file exactly, and whether it requires this extra transformational step (local-to-ECF) or not. I'll point out that Cesium is open-source, so it may be possible to port the function in question from JavaScript to Python (and bring WGS84 along for the ride). But first you should determine whether you need all that, or you just need a basic non-location-aware HPR to Quaternion, which you can probably find anywhere quality math is available. If you try the latter first and a level HPR tips towards the North Pole, then you probably need Cesium's version.