From 7a4e9e70f99dcb215b349d4842448b36ba0096b1 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 22 Sep 2023 08:21:14 +0300 Subject: [PATCH] add some quaternion operations --- .../space/kscience/kmath/ejml/_generated.kt | 10 +++++-- .../kmath/geometry/quaternionOperations.kt | 29 +++++++++++-------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt index f48ab4c19..984f1619b 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt @@ -19,13 +19,19 @@ import org.ejml.sparse.csc.factory.DecompositionFactory_DSCC import org.ejml.sparse.csc.factory.DecompositionFactory_FSCC import org.ejml.sparse.csc.factory.LinearSolverFactory_DSCC import org.ejml.sparse.csc.factory.LinearSolverFactory_FSCC -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.* import space.kscience.kmath.linear.Matrix +import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.StructureFeature -import space.kscience.kmath.operations.Float32Field +import space.kscience.kmath.structures.Float64 +import space.kscience.kmath.structures.Float32 import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Float32Field +import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.FloatField import space.kscience.kmath.operations.invoke +import space.kscience.kmath.structures.Float64Buffer +import space.kscience.kmath.structures.Float32Buffer import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.FloatBuffer import kotlin.reflect.KClass diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/quaternionOperations.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/quaternionOperations.kt index 49ceda04c..b6ad5009e 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/quaternionOperations.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/quaternionOperations.kt @@ -28,22 +28,27 @@ public fun Quaternion.power(number: Number): Quaternion = QuaternionAlgebra.powe public fun QuaternionAlgebra.slerp(from: Quaternion, to: Quaternion, fraction: Double): Quaternion = (to / from).pow(fraction) * from +/** + * Scalar angle between two quaternions + */ public fun QuaternionAlgebra.angleBetween(q1: Quaternion, q2: Quaternion): Angle = (q1.conjugate * q2).theta -public val Quaternion.inclination: Radians get() = asin(2 * (w * y - x * z)).radians - -public val Quaternion.azimuth: Angle get() = atan2(2 * (w * z + x * y), 1 - 2 * (y.pow(2) + z.pow(2))).radians.normalized() - +/** + * Euclidean product of two quaternions + */ public infix fun Quaternion.dot(other: Quaternion): Double = w * other.w + x * other.x + y * other.y + z * other.z - -private fun Quaternion.normalizedToEuler(): Float32Vector3D { - val roll = atan2(2 * y * w + 2 * x * z, 1 - 2 * y * y - 2 * z * z); - val pitch = atan2(2 * x * w - 2 * y * z, 1 - 2 * x * x - 2 * z * z); - val yaw = asin(2 * x * y + 2 * z * w); - - return Float32Vector3D(roll, pitch, yaw) -} +// +///** +// * Convert a quaternion to XYZ Cardan angles assuming it is normalized. +// */ +//private fun Quaternion.normalizedToEuler(): Float32Vector3D { +// val roll = atan2(2 * y * w + 2 * x * z, 1 - 2 * y * y - 2 * z * z) +// val pitch = atan2(2 * x * w - 2 * y * z, 1 - 2 * x * x - 2 * z * z) +// val yaw = asin(2 * x * y + 2 * z * w) +// +// return Float32Vector3D(roll, pitch, yaw) +//} /** * Quaternion to XYZ Cardan angles