how to calculate angle between two HeadingPitchRoll?

hi,

here the code I’m using.

function calcAngleBetweenHeadingPitchRolls(hpr1, hpr2){

if(!(hpr1 instanceof Cesium.HeadingPitchRoll) || !(hpr2 instanceof Cesium.HeadingPitchRoll)){

throw “Invalid input”;

}

var q1 = Cesium.Quaternion.fromHeadingPitchRoll(hpr1);

var q2 = Cesium.Quaternion.fromHeadingPitchRoll(hpr2);

if(q1.equals(q2)){

return 0;

}

Cesium.Quaternion.normalize(q1,q1);

Cesium.Quaternion.normalize(q2,q2);

var qDiff = Cesium.Quaternion.multiply(q1, q2, new Cesium.Quaternion());

var diffAngleRadians = Cesium.Quaternion.computeAngle(qDiff);

console.log("diffAngleRad: " + diffAngleRadians);

console.log("diffAngleDeg: " + Cesium.Math.toDegrees(diffAngleRadians));

return diffAngleRadians;

}

You’ll probably have more success for the mathematics solution on a more math-oriented forum, however, you should be able to implement the algorithm in Cesium using the Quaternion class.

For example,

var q1 = Cesium.Quaternion.fromHeadingPitchRoll(hpr1);

var q2 = Cesium.Quaternion.fromHeadingPitchRoll(hpr2);

var dq = Cesium.Quaternion.subtract(q1, q2, new Cesium.Quaternion());

var angle = Cesium.Quaternion.computeAngle(dq);

``

Thanks,
Gabby

Thanks for your time, but it does not work.
Here the full code I’m using.

function createHPRFromDegrees(h,p,r){

//arguments are in degree

this.h = warpTo360(h);

this.p = warpTo360§;

this.r = warpTo360®;

return Cesium.HeadingPitchRoll.fromDegrees(this.h,this.p,this.r, new Cesium.HeadingPitchRoll());

}

function warpTo360(degree){

if(!degree || isNaN(degree)){

return 0;

}

var result = degree;

result = result % 360;

if(result === 0){

//just for handling ‘-0’

return 0;

}

else if(result < 0) {

return (result + 360);

} else {

return result;

}

}

function calcAngleBetweenHeadingPitchRolls(hpr1, hpr2){

if(!(hpr1 instanceof Cesium.HeadingPitchRoll) || !(hpr2 instanceof Cesium.HeadingPitchRoll)){

throw “Invalid input”;

}

var q1 = Cesium.Quaternion.fromHeadingPitchRoll(hpr1,new Cesium.Quaternion());

var q2 = Cesium.Quaternion.fromHeadingPitchRoll(hpr2,new Cesium.Quaternion());

console.log(q1);

console.log(q2);

if(q1.equals(q2)){

return 0;

}

var qDiff = Cesium.Quaternion.subtract(q2, q1, new Cesium.Quaternion());

console.log(qDiff);

var diffAngleRadians = Cesium.Quaternion.computeAngle(qDiff);

console.log("diffAngleRad: " + diffAngleRadians);

console.log("diffAngleDeg: " + Cesium.Math.toDegrees(diffAngleRadians));

return diffAngleRadians;

}

``

If use it like the following it sholud give me 10 degree but returning 180 degree

var camHPR = createHPRFromDegrees(15,0,0);

var imgHPR = createHPRFromDegrees(5,0,0);

calcAngleBetweenHeadingPitchRolls(camHPR, imgHPR);

diffAngleRad: 3.1263857866503817
diffAngleDeg: 179.1287107047547

``

Solution is described here:

Assume your quaternions xx and yy are represented as x=[x0,x1,x2,x3]x=[x0,x1,x2,x3] and y=[y0,y1,y2,y3]y=[y0,y1,y2,y3] and that they are unit quaternions. Then let inv()inv⁡() denote the inverse of a quaternion which for unit quaternions is equivalent to the conjugate (i.e. inv(x)=conj(x)=[x0,−x1,−x2,−x3]. Then the quantity that captures the true difference is z=x∗conj(y). We can then recover the angle using θ=2arccos(z0). Then θ gives you an angle by which the two quaternions differ.

The normalize, inverse, and multiply functions are available in the Quaternion class to perform the described operations.

Sorry, my formatting got messed up.

Solution is described here: https://math.stackexchange.com/questions/167827/compute-angle-between-quaternions-in-matlab

To get an exact answer you would have to use the following process. Assume your quaternions x and y are represented as x=[x0,x1,x2,x3] and y=[y0,y1,y2,y3] and that they are unit quaternions. Then let inv() denote the inverse of a quaternion which for unit quaternions is equivalent to the conjugate (i.e. inv(x)=conj(x)=[x0,−x1,−x2,−x3]). Then the quantity that captures the true difference is z=x∗conj(y). We can then recover the angle using θ=2arccos(z0). Then θ gives you an angle by which the two quaternions differ.

The normalize, inverse, and multiply functions are all available in the Cesium Quaternion API.

Thanks,

Gabby

Thanks Gabby for your time, this is what I was looking for.