From 2cf7d6b2d3779334349c949d3cd1310d041e6553 Mon Sep 17 00:00:00 2001 From: AxisAngles Date: Thu, 26 Jan 2023 19:32:57 -0800 Subject: [PATCH] Changed ONE to IDENTITY and ZERO to NULL. Added Quaternion.fromRotationVector(x, y, z) --- .../java/io/github/axisangles/ktmath/Main.kt | 6 ++-- .../io/github/axisangles/ktmath/Matrix3.kt | 2 +- .../io/github/axisangles/ktmath/Quaternion.kt | 30 ++++++++++++++----- .../io/github/axisangles/ktmath/Vector3.kt | 4 +-- .../axisangles/ktmath/QuaternionTest.kt | 12 ++++---- 5 files changed, 35 insertions(+), 19 deletions(-) diff --git a/server/src/main/java/io/github/axisangles/ktmath/Main.kt b/server/src/main/java/io/github/axisangles/ktmath/Main.kt index a64ae0fbf..f9204f04a 100644 --- a/server/src/main/java/io/github/axisangles/ktmath/Main.kt +++ b/server/src/main/java/io/github/axisangles/ktmath/Main.kt @@ -121,7 +121,7 @@ fun testQuaternionInv() { for (i in 1..1000) { val Q = randQuaternion() - if (relError(Q * Q.inv(), Quaternion.ONE) > 1e-6f) { + if (relError(Q * Q.inv(), Quaternion.IDENTITY) > 1e-6f) { throw Exception("Quaternion inv accuracy test failed") } } @@ -131,7 +131,7 @@ fun testQuaternionDiv() { for (i in 1..1000) { val Q = randQuaternion() - if (!checkError(1e-6f, Q/Q, Quaternion.ONE)) { + if (!checkError(1e-6f, Q/Q, Quaternion.IDENTITY)) { throw Exception("Quaternion div accuracy test failed") } if (!checkError(1e-6f, 2f/Q, 2f*Q.inv())) { @@ -151,7 +151,7 @@ fun testQuaternionPow() { if (!checkError(2e-6f, Q.pow(-1f), Q.inv())) { throw Exception("Quaternion pow -1 accuracy test failed") } - if (!checkError(2e-6f, Q.pow(0f), Quaternion.ONE)) { + if (!checkError(2e-6f, Q.pow(0f), Quaternion.IDENTITY)) { throw Exception("Quaternion pow 0 accuracy test failed") } if (!checkError(2e-6f, Q.pow(1f), Q)) { diff --git a/server/src/main/java/io/github/axisangles/ktmath/Matrix3.kt b/server/src/main/java/io/github/axisangles/ktmath/Matrix3.kt index 12ce08611..52fca5488 100644 --- a/server/src/main/java/io/github/axisangles/ktmath/Matrix3.kt +++ b/server/src/main/java/io/github/axisangles/ktmath/Matrix3.kt @@ -16,7 +16,7 @@ data class Matrix3( val zz: Float ) { companion object { - val ZERO = Matrix3(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f) + val NULL = Matrix3(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f) val IDENTITY = Matrix3(1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f) } diff --git a/server/src/main/java/io/github/axisangles/ktmath/Quaternion.kt b/server/src/main/java/io/github/axisangles/ktmath/Quaternion.kt index 36b01ad79..b886d661d 100644 --- a/server/src/main/java/io/github/axisangles/ktmath/Quaternion.kt +++ b/server/src/main/java/io/github/axisangles/ktmath/Quaternion.kt @@ -10,19 +10,32 @@ import kotlin.math.* */ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { companion object { - val ZERO = Quaternion(0f, 0f, 0f, 0f) - val ONE = Quaternion(1f, 0f, 0f, 0f) + val NULL = Quaternion(0f, 0f, 0f, 0f) + val IDENTITY = Quaternion(1f, 0f, 0f, 0f) val I = Quaternion(0f, 1f, 0f, 0f) val J = Quaternion(0f, 0f, 1f, 0f) val K = Quaternion(0f, 0f, 0f, 1f) // creates a new quaternion representing the rotation about axis v by rotational angle v /** - * creates a new quaternion representing the rotation about axis v by rotational angle of v's length + * creates a new quaternion representing the rotation about v's axis + * by an angle of v's length + * @param v the rotation vector * @return the new quaternion **/ fun fromRotationVector(v: Vector3): Quaternion = Quaternion(0f, v / 2f).exp() + /** + * creates a new quaternion representing the rotation about axis v + * by an angle of v's length + * @param vx the rotation vector's x component + * @param vy the rotation vector's y component + * @param vz the rotation vector's z component + * @return the new quaternion + **/ + fun fromRotationVector(vx: Float, vy: Float, vz: Float): Quaternion = + fromRotationVector(Vector3(vx, vy, vz)) + /** * finds Q, the smallest-angled quaternion whose local u direction aligns with * the global v direction. @@ -39,6 +52,9 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { } } + /** + * @return the quaternion with w real component and xyz imaginary components + */ constructor(w: Float, xyz: Vector3) : this(w, xyz.x, xyz.y, xyz.z) /** @@ -102,7 +118,7 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { **/ fun unit(): Quaternion { val m = len() - return if (m == 0f) ZERO else this / m + return if (m == 0f) NULL else this / m } operator fun times(that: Float): Quaternion = Quaternion( @@ -293,7 +309,7 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { **/ fun angleAbout(u: Vector3): Float { val si = u.dot(xyz) - val co = u.len()*w + val co = u.len() * w return atan2(si, co) } @@ -302,7 +318,7 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { * @param u the axis * @return angle **/ - fun angleAboutR(u: Vector3): Float = 2f * twinNearest(ONE).angleAbout(u) + fun angleAboutR(u: Vector3): Float = 2f * twinNearest(IDENTITY).angleAbout(u) /** * finds Q, the quaternion nearest to this quaternion representing a rotation purely @@ -351,7 +367,7 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { * computes the rotation vector representing this quaternion's rotation * @return rotation vector **/ - fun toRotationVector(): Vector3 = 2f * twinNearest(ONE).log().xyz + fun toRotationVector(): Vector3 = 2f * twinNearest(IDENTITY).log().xyz /** * computes the matrix representing this quaternion's rotation diff --git a/server/src/main/java/io/github/axisangles/ktmath/Vector3.kt b/server/src/main/java/io/github/axisangles/ktmath/Vector3.kt index db9a7221c..6e181f826 100644 --- a/server/src/main/java/io/github/axisangles/ktmath/Vector3.kt +++ b/server/src/main/java/io/github/axisangles/ktmath/Vector3.kt @@ -7,7 +7,7 @@ import kotlin.math.sqrt data class Vector3(val x: Float, val y: Float, val z: Float) { companion object { - val ZERO = Vector3(0f, 0f, 0f) + val NULL = Vector3(0f, 0f, 0f) val POS_X = Vector3(1f, 0f, 0f) val POS_Y = Vector3(0f, 1f, 0f) val POS_Z = Vector3(0f, 0f, 1f) @@ -65,7 +65,7 @@ data class Vector3(val x: Float, val y: Float, val z: Float) { **/ fun unit(): Vector3 { val m = len() - return if (m == 0f) ZERO else this / m + return if (m == 0f) NULL else this / m } operator fun times(that: Float) = Vector3( diff --git a/server/src/test/java/io/github/axisangles/ktmath/QuaternionTest.kt b/server/src/test/java/io/github/axisangles/ktmath/QuaternionTest.kt index 00b02ac14..e3223100a 100644 --- a/server/src/test/java/io/github/axisangles/ktmath/QuaternionTest.kt +++ b/server/src/test/java/io/github/axisangles/ktmath/QuaternionTest.kt @@ -72,7 +72,7 @@ class QuaternionTest { val q = Quaternion(1f, 2f, 3f, 4f) assertEquals(q.pow(1f), q, 1e-5) assertEquals(q.pow(2f), q * q, 1e-5) - assertEquals(q.pow(0f), Quaternion.ONE, 1e-5) + assertEquals(q.pow(0f), Quaternion.IDENTITY, 1e-5) assertEquals(q.pow(-1f), q.inv(), 1e-5) } @@ -112,7 +112,7 @@ class QuaternionTest { fun angleTo() { val q1 = Quaternion(1f, 0f, 0f, 0f) val q2 = Quaternion(0f, 1f, 0f, 0f) - assertEquals(q1.angleTo(q2), PI.toFloat()/2f) + assertEquals(q1.angleTo(q2), PI.toFloat() / 2f) } @Test @@ -125,7 +125,7 @@ class QuaternionTest { @Test fun angle() { val q = Quaternion(0f, 1f, 0f, 0f) - assertEquals(q.angle(), PI.toFloat()/2f) + assertEquals(q.angle(), PI.toFloat() / 2f) } @Test @@ -137,13 +137,13 @@ class QuaternionTest { @Test fun angleAbout() { val q = Quaternion(1f, 1f, 1f, 0f) - assertEquals(q.angleAbout(Vector3.POS_Y), PI.toFloat()/4f) + assertEquals(q.angleAbout(Vector3.POS_Y), PI.toFloat() / 4f) } @Test fun angleAboutR() { val q = Quaternion(1f, 1f, 1f, 0f) - assertEquals(q.angleAboutR(Vector3.POS_Y), PI.toFloat()/2f) + assertEquals(q.angleAboutR(Vector3.POS_Y), PI.toFloat() / 2f) } @Test @@ -216,7 +216,7 @@ class QuaternionTest { private fun testEulerAngles(order: EulerOrder) { val inputQ = Quaternion(1f, 2f, 3f, 4f).unit() val outputQ = inputQ.toEulerAngles(order) - .toQuaternion().twinNearest(Quaternion.ONE) + .toQuaternion().twinNearest(Quaternion.IDENTITY) assertEquals(inputQ, outputQ, 1e-7) }