From 44a34d614218bb69c6c9af07f6e85bf621acc8af Mon Sep 17 00:00:00 2001 From: Butterscotch! Date: Wed, 28 Oct 2026 18:51:30 -0400 Subject: [PATCH] Add space translation helpers to Quaternion --- .../io/github/axisangles/ktmath/Quaternion.kt | 14 ++++++++++++++ .../github/axisangles/ktmath/QuaternionTest.kt | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt b/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt index 6665a3a63..b8932aab9 100644 --- a/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt +++ b/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt @@ -441,6 +441,20 @@ class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { w * w - x * x - y * y + z * z, ) + /** + * Aligns this global quaternion to be local within the [parent]'s space. + * @param parent The global quaternion to become relative to. + * @return This quaternion aligned to [parent]'s local space. + */ + fun localAbout(parent: Quaternion): Quaternion = parent.inv() * this + + /** + * Aligns this local quaternion to be global from within the parent's space. + * @param parent The global quaternion which this quaternion is relative to. + * @return This quaternion aligned to a global space. + */ + fun globalAbout(parent: Quaternion): Quaternion = parent * this + /** * computes this quaternion's unit length rotation axis * @return rotation axis diff --git a/server/core/src/test/java/io/github/axisangles/ktmath/QuaternionTest.kt b/server/core/src/test/java/io/github/axisangles/ktmath/QuaternionTest.kt index 230af7e6b..012d50ae8 100644 --- a/server/core/src/test/java/io/github/axisangles/ktmath/QuaternionTest.kt +++ b/server/core/src/test/java/io/github/axisangles/ktmath/QuaternionTest.kt @@ -198,6 +198,22 @@ class QuaternionTest { assertTrue(q.sandwichUnitZ().isNear(q.sandwich(Vector3.POS_Z))) } + @Test + fun localAbout() { + val parent = Quaternion(1f, 1f, 0f, 0f).unit() + val global = Quaternion(1f, 1f, -1f, 1f).unit() + val local = Quaternion(1f, 0f, 0f, 1f).unit() + assertEquals(local, global.localAbout(parent), 1e-5) + } + + @Test + fun globalAbout() { + val parent = Quaternion(1f, 1f, 0f, 0f).unit() + val global = Quaternion(1f, 1f, -1f, 1f).unit() + val local = Quaternion(1f, 0f, 0f, 1f).unit() + assertEquals(global, local.globalAbout(parent), 1e-5) + } + @Test fun axis() { val v1 = Quaternion(0f, Quaternion(1f, 2f, 3f, 4f).axis())