Skip to content

Instantly share code, notes, and snippets.

@thw0rted
Created May 4, 2018 12:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thw0rted/4857eea152fcc54265330c00775f0fb5 to your computer and use it in GitHub Desktop.
Save thw0rted/4857eea152fcc54265330c00775f0fb5 to your computer and use it in GitHub Desktop.
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