diff --git a/CHANGELOG.md b/CHANGELOG.md index 58a7bf47968ca93abbbcd9c19eea20575ba9c349..dc3d7d7e9f7b3ac1b64d3b7a34047f49b22eb243 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ release. - Fixed incorrect distortion look up in Orex camera when working with PolyCam images [#583](https://github.com/DOI-USGS/ale/pull/583) - Brought timing in line with ISIS for the KaguyaMiIsisLabelNaifSpiceDriver [#599](https://github.com/DOI-USGS/ale/pull/599) - Brought timing in line with ISIS for the MroMarciIsisLabelNaifSpiceDriver [#600](https://github.com/DOI-USGS/ale/pull/600) +- Fixed a bug in which quaternions would flip sign in a way that caused interpolation errors [#603](https://github.com/DOI-USGS/ale/issues/603) ## [0.10.0] - 2024-01-08 diff --git a/ale/rotation.py b/ale/rotation.py index aa603d63bf62d74c86fc1c15db09b4ad8ac3a4ce..eaded5785dc976332d58ed76896723914a077bb0 100644 --- a/ale/rotation.py +++ b/ale/rotation.py @@ -196,7 +196,27 @@ class TimeDependentRotation: the destination reference frame. The quaternions are in scalar last format (x, y, z, w). """ - return self._rots.as_quat() + quats = self._rots.as_quat() + + # First find the largest magnitude quaternion coefficient and its coordinate + num_quats = len(quats) + max_q = 0.0 + max_j = 0 + for i in range(num_quats): + for j in range(4): + if abs(quats[i][j]) > abs(max_q): + max_q = quats[i][j] + max_j = j + + # Ensure the signs are consistent + qcnt = 0 + for i in range(num_quats): + if quats[i][max_j] * max_q < 0: + qcnt = qcnt + 1 + for j in range(4): + quats[i][j] *= -1.0 + + return quats @quats.setter def quats(self, new_quats): diff --git a/tests/pytests/data/isds/dawnvir_isd.json b/tests/pytests/data/isds/dawnvir_isd.json index 90674bb71784097ff4be716c054d5cffb2c10695..01c3df4d17c14e4845bbd8c28341a6f4fecdd725 100644 --- a/tests/pytests/data/isds/dawnvir_isd.json +++ b/tests/pytests/data/isds/dawnvir_isd.json @@ -339,10 +339,10 @@ ], "quaternions": [ [ - -0.08339426680875163, - 0.2825790922154143, - -0.2899354389711647, - 0.9105667982826637 + 0.08339426680875163, + -0.2825790922154143, + 0.2899354389711647, + -0.9105667982826637 ], [ -0.05753008804150366,