diff --git a/server/src/main/java/io/github/axisangles/ktmath/EulerAngles.kt b/server/src/main/java/io/github/axisangles/ktmath/EulerAngles.kt index e027cf4df..57bc8a611 100644 --- a/server/src/main/java/io/github/axisangles/ktmath/EulerAngles.kt +++ b/server/src/main/java/io/github/axisangles/ktmath/EulerAngles.kt @@ -80,36 +80,39 @@ data class EulerAngles(val order: EulerOrder, val x: Float, val y: Float, val z: val sZ = sin(z) return when (order) { + // ktlint ruining spacing + /* ktlint-disable */ EulerOrder.XYZ -> Matrix3( - cY * cZ, -cY * sZ, sY, - cZ * sX * sY + cX * sZ, cX * cZ - sX * sY * sZ, -cY * sX, - sX * sZ - cX * cZ * sY, cZ * sX + cX * sY * sZ, cX * cY + cY*cZ, -cY*sZ, sY, + cZ*sX*sY + cX*sZ, cX*cZ - sX*sY*sZ, -cY*sX, + sX*sZ - cX*cZ*sY, cZ*sX + cX*sY*sZ, cX*cY ) EulerOrder.YZX -> Matrix3( - cY * cZ, sX * sY - cX * cY * sZ, cX * sY + cY * sX * sZ, - sZ, cX * cZ, -cZ * sX, - -cZ * sY, cY * sX + cX * sY * sZ, cX * cY - sX * sY * sZ + cY * cZ, sX * sY - cX * cY * sZ, cX * sY + cY * sX * sZ, + sZ, cX * cZ, -cZ * sX, + -cZ * sY, cY * sX + cX * sY * sZ, cX * cY - sX * sY * sZ ) EulerOrder.ZXY -> Matrix3( - cY * cZ - sX * sY * sZ, -cX * sZ, cZ * sY + cY * sX * sZ, - cZ * sX * sY + cY * sZ, cX * cZ, sY * sZ - cY * cZ * sX, - -cX * sY, sX, cX * cY + cY * cZ - sX * sY * sZ, -cX * sZ, cZ * sY + cY * sX * sZ, + cZ * sX * sY + cY * sZ, cX * cZ, sY * sZ - cY * cZ * sX, + -cX * sY, sX, cX * cY ) EulerOrder.ZYX -> Matrix3( - cY * cZ, cZ * sX * sY - cX * sZ, cX * cZ * sY + sX * sZ, - cY * sZ, cX * cZ + sX * sY * sZ, cX * sY * sZ - cZ * sX, - -sY, cY * sX, cX * cY + cY * cZ, cZ * sX * sY - cX * sZ, cX * cZ * sY + sX * sZ, + cY * sZ, cX * cZ + sX * sY * sZ, cX * sY * sZ - cZ * sX, + -sY, cY * sX, cX * cY ) EulerOrder.YXZ -> Matrix3( - cY * cZ + sX * sY * sZ, cZ * sX * sY - cY * sZ, cX * sY, - cX * sZ, cX * cZ, -sX, - cY * sX * sZ - cZ * sY, cY * cZ * sX + sY * sZ, cX * cY + cY * cZ + sX * sY * sZ, cZ * sX * sY - cY * sZ, cX * sY, + cX * sZ, cX * cZ, -sX, + cY * sX * sZ - cZ * sY, cY * cZ * sX + sY * sZ, cX * cY ) EulerOrder.XZY -> Matrix3( - cY * cZ, -sZ, cZ * sY, - sX * sY + cX * cY * sZ, cX * cZ, cX * sY * sZ - cY * sX, - cY * sX * sZ - cX * sY, cZ * sX, cX * cY + sX * sY * sZ + cY * cZ, -sZ, cZ * sY, + sX * sY + cX * cY * sZ, cX * cZ, cX * sY * sZ - cY * sX, + cY * sX * sZ - cX * sY, cZ * sX, cX * cY + sX * sY * sZ ) + /* ktlint-enable */ } } } 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 52fca5488..9c68b0f30 100644 --- a/server/src/main/java/io/github/axisangles/ktmath/Matrix3.kt +++ b/server/src/main/java/io/github/axisangles/ktmath/Matrix3.kt @@ -4,20 +4,24 @@ package io.github.axisangles.ktmath import kotlin.math.* +/* ktlint-disable */ data class Matrix3( - val xx: Float, - val yx: Float, - val zx: Float, - val xy: Float, - val yy: Float, - val zy: Float, - val xz: Float, - val yz: Float, - val zz: Float + val xx: Float, val yx: Float, val zx: Float, + val xy: Float, val yy: Float, val zy: Float, + val xz: Float, val yz: Float, val zz: Float ) { +/* ktlint-enable */ companion object { - val NULL = Matrix3(0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f) - val IDENTITY = Matrix3(1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f) + val NULL = Matrix3( + 0f, 0f, 0f, + 0f, 0f, 0f, + 0f, 0f, 0f + ) + val IDENTITY = Matrix3( + 1f, 0f, 0f, + 0f, 1f, 0f, + 0f, 0f, 1f + ) } /** @@ -85,7 +89,9 @@ data class Matrix3( * computes the square of the frobenius norm of this matrix * @return the frobenius norm squared */ - fun normSq(): Float = xx * xx + yx * yx + zx * zx + xy * xy + yy * yy + zy * zy + xz * xz + yz * yz + zz * zz + fun normSq(): Float = (xx * xx + yx * yx + zx * zx + + xy * xy + yy * yy + zy * zy + + xz * xz + yz * yz + zz * zz) /** * computes the frobenius norm of this matrix @@ -97,7 +103,9 @@ data class Matrix3( * computes the determinant of this matrix * @return the determinant */ - fun det(): Float = (xz * yx - xx * yz) * zy + (xx * yy - xy * yx) * zz + (xy * yz - xz * yy) * zx + fun det(): Float = ((xz * yx - xx * yz) * zy + + (xx * yy - xy * yx) * zz + + (xy * yz - xz * yy) * zx) /** * computes the trace of this matrix @@ -200,7 +208,8 @@ data class Matrix3( // assumes this matrix is orthonormal and converts this to a quaternion /** - * creates a quaternion representing the same rotation as this matrix, assuming the matrix is a rotation matrix + * creates a quaternion representing the same rotation as this matrix, + * assuming the matrix is a rotation matrix * @return the quaternion */ fun toQuaternionAssumingOrthonormal(): Quaternion { @@ -222,87 +231,88 @@ data class Matrix3( */ fun toQuaternion(): Quaternion = orthonormalize().toQuaternionAssumingOrthonormal() - /* - the standard algorithm: + /* + the standard algorithm: - yAng = asin(clamp(zx, -1, 1)) - if (abs(zx) < 0.9999999f) { - xAng = atan2(-zy, zz) - zAng = atan2(-yx, xx) - } else { - xAng = atan2(yz, yy) - zAng = 0 - } + yAng = asin(clamp(zx, -1, 1)) + if (abs(zx) < 0.9999999f) { + xAng = atan2(-zy, zz) + zAng = atan2(-yx, xx) + } else { + xAng = atan2(yz, yy) + zAng = 0 + } - problems with the standard algorithm: + problems with the standard algorithm: - 1) - yAng = asin(clamp(zx, -1, 1)) + 1) + yAng = asin(clamp(zx, -1, 1)) - FIX: - yAng = atan2(zx, sqrt(zy*zy + zz*zz)) + FIX: + yAng = atan2(zx, sqrt(zy*zy + zz*zz)) - this loses many bits of accuracy when near the singularity, zx = +-1 and - can cause the algorithm to return completely inaccurate results with only - small floating point errors in the matrix. this happens because zx is - NOT sin(pitch), but rather errorTerm*sin(pitch), and small changes in zx - when zx is near +-1 make large changes in asin(zx). + this loses many bits of accuracy when near the singularity, zx = +-1 and + can cause the algorithm to return completely inaccurate results with only + small floating point errors in the matrix. this happens because zx is + NOT sin(pitch), but rather errorTerm*sin(pitch), and small changes in zx + when zx is near +-1 make large changes in asin(zx). - 2) - if (abs(zx) < 0.9999999f) { + 2) + if (abs(zx) < 0.9999999f) { - FIX: - if (zy*zy + zz*zz > 0f) { + FIX: + if (zy*zy + zz*zz > 0f) { - this clause, meant to reduce the inaccuracy of the code following does - not actually test for the condition that makes the following atans unstable. - that is, when (zy, zz) and (yx, xx) are near 0. - after several matrix multiplications, the error term is expected to be - larger than 0.0000001. Often times, this clause will not catch the conditions - it is trying to catch. + this clause, meant to reduce the inaccuracy of the code following does + not actually test for the condition that makes the following atans unstable. + that is, when (zy, zz) and (yx, xx) are near 0. + after several matrix multiplications, the error term is expected to be + larger than 0.0000001. Often times, this clause will not catch the conditions + it is trying to catch. - 3) - zAng = atan2(-yx, xx) + 3) + zAng = atan2(-yx, xx) - FIX: - zAng = atan2(xy*zz - xz*zy, yy*zz - yz*zy) + FIX: + zAng = atan2(xy*zz - xz*zy, yy*zz - yz*zy) - xAng and zAng are being computed separately. In the case of near singularity - the angles of xAng and zAng are effectively added together as they represent - the same operation (a rotation about the global y-axis). When computed - separately, it is not guaranteed that the xAng + zAng add together to give - the actual final rotation about the global y-axis. + xAng and zAng are being computed separately. In the case of near singularity + the angles of xAng and zAng are effectively added together as they represent + the same operation (a rotation about the global y-axis). When computed + separately, it is not guaranteed that the xAng + zAng add together to give + the actual final rotation about the global y-axis. - 4) - after many matrix operations are performed, without orthonormalization - the matrix will contain floating point errors that will throw off the - accuracy of any euler angles algorithm. orthonormalization should be - built into the prerequisites for this function - */ + 4) + after many matrix operations are performed, without orthonormalization + the matrix will contain floating point errors that will throw off the + accuracy of any euler angles algorithm. orthonormalization should be + built into the prerequisites for this function + */ -// fun toEulerAnglesXYZFaulty(): EulerAngles { -// return if (abs(zx) < 0.9999999f) -// EulerAngles(EulerOrder.XYZ, -// atan2(-zy, zz), -// asin(zx.coerceIn(-1f, 1f)), -// atan2(-yx, xx)) -// else -// EulerAngles(EulerOrder.XYZ, -// atan2(yz, yy), -// asin(zx.coerceIn(-1f, 1f)), -// 0f) -// } +//fun toEulerAnglesXYZFaulty(): EulerAngles { +// return if (abs(zx) < 0.9999999f) +// EulerAngles(EulerOrder.XYZ, +// atan2(-zy, zz), +// asin(zx.coerceIn(-1f, 1f)), +// atan2(-yx, xx)) +// else +// EulerAngles(EulerOrder.XYZ, +// atan2(yz, yy), +// asin(zx.coerceIn(-1f, 1f)), +// 0f) +//} /** - * creates an eulerAngles representing the same rotation as this matrix, assuming the matrix is a rotation matrix + * creates an eulerAngles representing the same rotation as this matrix, + * assuming the matrix is a rotation matrix * @return the eulerAngles */ fun toEulerAnglesAssumingOrthonormal(order: EulerOrder): EulerAngles { @@ -310,7 +320,11 @@ data class Matrix3( when (order) { EulerOrder.XYZ -> { val kc = zy * zy + zz * zz - if (kc == 0f) return EulerAngles(EulerOrder.XYZ, atan2(yz, yy), ETA.withSign(zx), 0f) + if (kc == 0f) { + return EulerAngles( + EulerOrder.XYZ, atan2(yz, yy), ETA.withSign(zx), 0f + ) + } return EulerAngles( EulerOrder.XYZ, @@ -321,7 +335,11 @@ data class Matrix3( } EulerOrder.YZX -> { val kc = xx * xx + xz * xz - if (kc == 0f) return EulerAngles(EulerOrder.YZX, 0f, atan2(zx, zz), ETA.withSign(xy)) + if (kc == 0f) { + return EulerAngles( + EulerOrder.YZX, 0f, atan2(zx, zz), ETA.withSign(xy) + ) + } return EulerAngles( EulerOrder.YZX, @@ -332,7 +350,11 @@ data class Matrix3( } EulerOrder.ZXY -> { val kc = yy * yy + yx * yx - if (kc == 0f) return EulerAngles(EulerOrder.ZXY, ETA.withSign(yz), 0f, atan2(xy, xx)) + if (kc == 0f) { + return EulerAngles( + EulerOrder.ZXY, ETA.withSign(yz), 0f, atan2(xy, xx) + ) + } return EulerAngles( EulerOrder.ZXY, @@ -343,7 +365,11 @@ data class Matrix3( } EulerOrder.ZYX -> { val kc = xy * xy + xx * xx - if (kc == 0f) return EulerAngles(EulerOrder.ZYX, 0f, ETA.withSign(-xz), atan2(-yx, yy)) + if (kc == 0f) { + return EulerAngles( + EulerOrder.ZYX, 0f, ETA.withSign(-xz), atan2(-yx, yy) + ) + } return EulerAngles( EulerOrder.ZYX, @@ -354,7 +380,11 @@ data class Matrix3( } EulerOrder.YXZ -> { val kc = zx * zx + zz * zz - if (kc == 0f) return EulerAngles(EulerOrder.YXZ, ETA.withSign(-zy), atan2(-xz, xx), 0f) + if (kc == 0f) { + return EulerAngles( + EulerOrder.YXZ, ETA.withSign(-zy), atan2(-xz, xx), 0f + ) + } return EulerAngles( EulerOrder.YXZ, @@ -365,7 +395,11 @@ data class Matrix3( } EulerOrder.XZY -> { val kc = yz * yz + yy * yy - if (kc == 0f) return EulerAngles(EulerOrder.XZY, atan2(-zy, zz), 0f, ETA.withSign(-yx)) + if (kc == 0f) { + return EulerAngles( + EulerOrder.XZY, atan2(-zy, zz), 0f, ETA.withSign(-yx) + ) + } return EulerAngles( EulerOrder.XZY, @@ -374,9 +408,6 @@ data class Matrix3( atan2(-yx, sqrt(kc)) ) } - else -> { - throw Exception("EulerAngles not implemented for given EulerOrder") - } } } @@ -385,7 +416,8 @@ data class Matrix3( * creates an eulerAngles representing the same rotation as this matrix * @return the eulerAngles */ - fun toEulerAngles(order: EulerOrder): EulerAngles = orthonormalize().toEulerAnglesAssumingOrthonormal(order) + fun toEulerAngles(order: EulerOrder): EulerAngles = + orthonormalize().toEulerAnglesAssumingOrthonormal(order) } operator fun Float.times(that: Matrix3): Matrix3 = that * this 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 b886d661d..67e8fd762 100644 --- a/server/src/main/java/io/github/axisangles/ktmath/Quaternion.kt +++ b/server/src/main/java/io/github/axisangles/ktmath/Quaternion.kt @@ -81,7 +81,8 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { this.z + that.z ) - operator fun plus(that: Float): Quaternion = Quaternion(this.w + that, this.x, this.y, this.z) + operator fun plus(that: Float): Quaternion = + Quaternion(this.w + that, this.x, this.y, this.z) operator fun minus(that: Quaternion): Quaternion = Quaternion( this.w - that.w, @@ -118,7 +119,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) NULL else this / m + return if (m == 0f) NULL else (this / m) } operator fun times(that: Float): Quaternion = Quaternion( @@ -204,36 +205,40 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { // for a slight improvement in performance // not fully implemented -// fun pow(t: Float): Quaternion { -// val imLen = xyz.Len() -// val ang = atan(w, imLen) +// fun pow(t: Float): Quaternion { +// val imLen = xyz.Len() +// val ang = atan(w, imLen) // -// val len = len().pow(t) -// val co = cos(t*ang) -// val si = sin(t*ang) +// val len = len().pow(t) +// val co = cos(t*ang) +// val si = sin(t*ang) // -// return if (imLen == 0f) { -// Quaternion(len*co, -// len*t*x, -// len*t*y, -// len*t*z -// ) -// } else { -// Quaternion( -// len*co, -// len*si*x/imLen, -// len*si*y/imLen, -// len*si*z/imLen -// ) -// } -// } +// return if (imLen == 0f) { +// Quaternion(len*co, +// len*t*x, +// len*t*y, +// len*t*z +// ) +// } else { +// Quaternion( +// len*co, +// len*si*x/imLen, +// len*si*y/imLen, +// len*si*z/imLen +// ) +// } +// } /** * between this and -this, picks the one nearest to that * @param that the quaternion to be nearest * @return nearest quaternion **/ - fun twinNearest(that: Quaternion): Quaternion = if (this.dot(that) < 0f) -this else this + fun twinNearest(that: Quaternion): Quaternion = if (this.dot(that) < 0f) { + -this + } else { + this + } /** * interpolates from this quaternion to that quaternion by t in quaternion space @@ -261,7 +266,8 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { fun interpR(that: Quaternion, t: Float) = this.interp(that.twinNearest(this), t) /** - * linearly interpolates from this quaternion to that quaternion by t in quaternion space + * linearly interpolates from this quaternion to that quaternion by t in + * quaternion space * @param that the quaternion to interpolate to * @param t the amount to interpolate * @return interpolated quaternion @@ -269,7 +275,8 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { fun lerp(that: Quaternion, t: Float): Quaternion = (1f - t) * this + t * that /** - * linearly interpolates from this quaternion to that quaternion by t in rotation space + * linearly interpolates from this quaternion to that quaternion by t in + * rotation space * @param that the quaternion to interpolate to * @param t the amount to interpolate * @return interpolated quaternion @@ -377,9 +384,9 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { val d = lenSq() /* ktlint-disable */ return Matrix3( - (w*w + x*x - y*y - z*z)/d, 2f*(x*y - w*z)/d , 2f*(w*y + x*z)/d , - 2f*(x*y + w*z)/d , (w*w - x*x + y*y - z*z)/d, 2f*(y*z - w*x)/d , - 2f*(x*z - w*y)/d , 2f*(w*x + y*z)/d , (w*w - x*x - y*y + z*z)/d) + (w*w + x*x - y*y - z*z)/d, 2f*(x*y - w*z)/d, 2f*(w*y + x*z)/d, + 2f*(x*y + w*z)/d, (w*w - x*x + y*y - z*z)/d, 2f*(y*z - w*x)/d, + 2f*(x*z - w*y)/d, 2f*(w*x + y*z)/d, (w*w - x*x - y*y + z*z)/d) /* ktlint-enable */ } @@ -388,7 +395,8 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { * @param order the order in which to decompose this quaternion into euler angles * @return euler angles **/ - fun toEulerAngles(order: EulerOrder): EulerAngles = this.toMatrix().toEulerAnglesAssumingOrthonormal(order) + fun toEulerAngles(order: EulerOrder): EulerAngles = + this.toMatrix().toEulerAnglesAssumingOrthonormal(order) } operator fun Float.plus(that: Quaternion): Quaternion = that + this