Rotate model by 180 degrees in SampledPositionProperty path using CallbackProperty and VelocityOrientationProperty

I have a path using SampledPositionProperty, which I would like to have a model to follow.
The model is 180 degrees rotated and moves “backwards” along the path.
Inspired by this thread, I added the CallbackProperty on the orientation, calling VelocityOrientationProperty for each timestep.
I can then get the quaternion at a certain point in time. But I lack the skills, to add an additional heading rotation in a local reference system by 180 degrees.
Here is the code snippet. Some help in that orientation CallbackProperty would be highly appreciated.

var position = new Cesium.SampledPositionProperty();
for (var i = 0; i < points.length; i ++) {
	var time = Cesium.JulianDate.addSeconds(start, points[i].time - points[0].time, new Cesium.JulianDate());
	var pos = Cesium.Cartesian3.fromDegrees(points[i].lon, points[i].lat, points[i].alt);
	position.addSample(time, pos);
}
var path = this.viewer.entities.add({
	position : position,
	orientation: new Cesium.CallbackProperty(function(time, result){
		var rwx = new Cesium.VelocityOrientationProperty(position).getValue(time);
		var rwx1 = new Cesium.HeadingPitchRoll.fromQuaternion(rwx);
		rwx1.heading += Math.PI;
		var orientation = Cesium.Quaternion.fromHeadingPitchRoll(rwx1);
		return orientation;
	},false),
	path : {
		leadTime: 0,
		trailTime: 20 * 60,
		resolution : 5,
		material: new Cesium.PolylineGlowMaterialProperty({
			glowPower: 0.5,
			taperPower: 0.3,
			color: Cesium.Color.LIGHTSTEELBLUE,
		}),
		width : 5
	},
	model: { 
		uri: "./myModel.glb",
		maximumScale:10000,
		minimumPixelSize: 128
	},
});

I believe this will do it. The import part is the Transforms.headingPitchRollToFixedFrame which will transform your local 180 degree location into a rotation in world coordinates.

  let rotation = Cesium.Matrix3.fromQuaternion(rwx);
  const hpr = new Cesium.HeadingPitchRoll(Math.PI, 0.0, 0.0);
  const transform = Cesium.Transforms.headingPitchRollToFixedFrame(position.getValue(time), hpr);
  const additionalRotation = Cesium.Matrix4.getRotation(transform);
  rotation = Matrix3.multiply(rotation, additionalRotation, rotation);
  return Quaternion.fromRotationMatrix(rotation, result);

Thanks for the response!
I tried it out but the getRotation function comes back with a TypeError, even though the transform looks alright.

Just a comment for folks, that want to copy and past this:

  • line 2 needs to be const
  • line 3 has to reference hpr instead of headingPitchRoll
  • Cesium. needed with Matrix3 (line 5) and Quaternion (line 6)

Thanks again