Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cesium function to transform relative heading-pitch-roll to absolute rotation Quaternion
import {
Cartesian3,
HeadingPitchRoll,
Matrix3,
Matrix4,
Quaternion,
Transforms,
} from "cesium";
/**
* Convert a local heading/pitch/range rotation to an earth-axis-relative Quaternion
* @param position a Cartesian3 point in space
* @param heading degrees from North, rotation around Earth-normal vector
* @param pitch degrees from horizontal, rotation around earth-tangent X axis
* @param roll degrees from vertical, rotation around earth-tangent Y axis
*/
export function relativeOrientation(position: Cartesian3,
heading: number, pitch: number, roll: number): Quaternion {
// Build a Quaternion that represents the rotation between absolute and
// local-surface frames of reference
const transform4 = Transforms.eastNorthUpToFixedFrame(position);
const transform3 = new Matrix3();
Matrix4.getRotation(transform4, transform3);
const transformQ = Quaternion.fromRotationMatrix(transform3);
// Convert the h/p/r values to a (local-reference) Quaternion
const localHpr = HeadingPitchRoll.fromDegrees(heading, pitch, roll);
const localQ = Quaternion.fromHeadingPitchRoll(localHpr);
// Result is local times transform
const ret = new Quaternion();
Quaternion.multiply(localQ, transformQ, ret);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment