Fixed Quaternion angleAboutR, Added FloatMath, Quaternion.fromTo, switched to atan(co, si), Added more Quaternion tests

This commit is contained in:
AxisAngles committed 2023-01-26 13:53:30 -08:00
1 parent c5733069c1
commit bed94a9ed8
4 files changed
+193 -39

No files matched your search

@@ -0,0 +1,71 @@
package io.github.axisangles.ktmath
import kotlin.math.sqrt
/**
* guarantees last bit floating point accuracy
*/
fun atan(co: Float, si: Float) = atan(co.toDouble(), si.toDouble()).toFloat()
fun atan(ta: Float) = atan(ta.toDouble()).toFloat()
fun acos(co: Float) = acos(co.toDouble()).toFloat()
fun asin(si: Float) = asin(si.toDouble()).toFloat()
fun atan(co: Double, si: Double) =
if (co == 0.0 && si == 0.0) 0.0
else atanFold1(co, si)
fun atan(ta: Double) = when (ta) {
1.0/0.0 -> 1.570796326794897
-1.0/0.0 -> -1.570796326794897
else -> atanFold1(1.0, ta)
}
fun acos(co: Double) = atanFold1(co, sqrt(1.0 - co*co))
fun asin(si: Double) = atanFold1(sqrt(1.0 - si*si), si)
// The goal was to make use of as much FMA and parallel instruction execution as possible
// make a viktor version to replace this in the future
private fun atanFold1(x: Double, y: Double) =
if (y < 0.0) -atanFold2(x, -y)
else atanFold2(x, y)
private fun atanFold2(x: Double, y: Double) =
if (x < 0.0) 3.141592653589793 - atanFold3(-x, y)
else atanFold3(x, y)
private fun atanFold3(x: Double, y: Double) =
if (y > x) 1.570796326794897 - atanFold4(y, x)
else atanFold4(x, y)
private fun atanFold4(x: Double, y: Double) =
if (y > 0.4142135623730950*x) 0.7853981633974483 - atanFold5(
x + y, x - y)
else atanFold5(x, y)
private fun atanFold5(x: Double, y: Double) =
if (y > 0.1989123673796580*x) 0.3926990816987242 - atanFold6(
x + 0.4142135623730950*y, 0.4142135623730950*x - y)
else atanFold6(x, y)
private fun atanFold6(x: Double, y: Double) =
if (y > 0.09849140335716425*x) 0.1963495408493621 - atanFold7(
x + 0.1989123673796580*y, 0.1989123673796580*x - y)
else atanFold7(x, y)
private fun atanFold7(x: Double, y: Double) =
if (y > 0.04912684976946725*x) 0.09817477042468103 - atanFinal(
x + 0.09849140335716425*y, 0.09849140335716425*x - y)
else atanFinal(x, y)
private fun atanFinal(x: Double, y: Double): Double {
val s = y/x
val t = s*s
return s*(1.0 - t*(1.0/3.0 - t*(1.0/5.0)))// - t*(1.0/7.0))))// - t*(1.0/9.0)))))// - t*(1.0/11.0 - t*(1.0/13.0 - t*(1.0/15.0))))))))
// val r = y/x
// val s = r*r
// val t = s*s
// // help the CPU take advantage of parallel instruction execution
// val odd = s*(1.0/3.0 + t*(1.0/7.0))
// val evn = 1.0 + t*(1.0/5.0 + t*(1.0/9.0))
// return r*(evn - odd)
}
@@ -153,6 +153,10 @@ data class Matrix3(
* @return the rotation matrix
*/
fun orthonormalize(): Matrix3 {
if (this.det() <= 0f) { // maybe this doesn't have to be so
throw Exception("Attempt to convert non-positive determinant matrix to rotation matrix")
}
var curMat = this
var curDet = Float.POSITIVE_INFINITY
@@ -200,10 +204,6 @@ data class Matrix3(
* @return the quaternion
*/
fun toQuaternionAssumingOrthonormal(): Quaternion {
if (this.det() <= 0f) {
throw Exception("Attempt to convert negative determinant matrix to quaternion")
}
return if (yy > -zz && zz > -xx && xx > -yy) {
Quaternion(1 + xx + yy + zz, yz - zy, zx - xz, xy - yx).unit()
} else if (xx > yy && xx > zz) {
@@ -306,10 +306,6 @@ data class Matrix3(
* @return the eulerAngles
*/
fun toEulerAnglesAssumingOrthonormal(order: EulerOrder): EulerAngles {
if (this.det() <= 0f) {
throw Exception("Attempt to convert negative determinant matrix to euler angles")
}
val ETA = 1.57079632f
when (order) {
EulerOrder.XYZ -> {
@@ -22,6 +22,21 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) {
* @return the new quaternion
**/
fun fromRotationVector(v: Vector3): Quaternion = Quaternion(0f, v / 2f).exp()
/**
* finds Q, the smallest-angled quaternion whose local u direction aligns with
* the global v direction.
* @param u the local direction
* @param v the global direction
* @return Q
**/
fun fromTo(u: Vector3, v: Vector3): Quaternion {
val U = Quaternion(0f, u)
val V = Quaternion(0f, v)
val D = V / U
return (D + D.len()).unit()
}
}
constructor(w: Float, xyz: Vector3) : this(w, xyz.x, xyz.y, xyz.z)
@@ -59,14 +74,16 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) {
this.z - that.z
)
operator fun minus(that: Float): Quaternion = Quaternion(this.w - that, this.x, this.y, this.z)
operator fun minus(that: Float): Quaternion =
Quaternion(this.w - that, this.x, this.y, this.z)
/**
* computes the dot product of this quaternion with that quaternion
* @param that the quaternion with which to be dotted
* @return the inversed quaternion
* @return the inverse quaternion
**/
fun dot(that: Quaternion): Float = this.w * that.w + this.x * that.x + this.y * that.y + this.z * that.z
fun dot(that: Quaternion): Float =
this.w * that.w + this.x * that.x + this.y * that.y + this.z * that.z
/**
* computes the square of the length of this quaternion
@@ -141,7 +158,7 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) {
return Quaternion(ln(len), xyz / w)
}
val ang = atan2(si, co)
val ang = atan(co, si)
return Quaternion(ln(len), ang / si * xyz)
}
@@ -172,16 +189,15 @@ 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 = imLen()
// val ang = atan2(imLen, w)
// val imLen = xyz.Len()
// val ang = atan(w, imLen)
//
// val len = len().pow(t)
// val co = cos(t*ang)
// val si = sin(t*ang)
//
// return if (imLen == 0f) {
// Quaternion(
// len*co,
// Quaternion(len*co,
// len*t*x,
// len*t*y,
// len*t*z
@@ -248,13 +264,13 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) {
* computes this quaternion's angle to identity in quaternion space
* @return angle
**/
fun angle(): Float = atan2(xyz.len(), w)
fun angle(): Float = atan(w, xyz.len())
/**
* computes this quaternion's angle to identity in rotation space
* @return angle
**/
fun angleR(): Float = 2f * atan2(xyz.len(), abs(w))
fun angleR(): Float = 2f * atan(abs(w), xyz.len())
/**
* computes the angle between this quaternion and that quaternion in quaternion space
@@ -276,9 +292,9 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) {
* @return angle
**/
fun angleAbout(u: Vector3): Float {
val uDotIm = u.dot(xyz)
val uLen = u.len()
return atan2(uDotIm, uLen * w)
val si = u.dot(xyz)
val co = u.len()*w
return atan(co, si)
}
/**
@@ -287,34 +303,30 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) {
* @return angle
**/
fun angleAboutR(u: Vector3): Float {
val uDotIm = u.dot(xyz)
val uLen = u.len()
return if (uDotIm < 0f) {
2f * atan2(-uDotIm, -uLen * w)
} else {
2f * atan2(uDotIm, uLen * w)
}
}
val si = u.dot(xyz)
val co = u.len()*w
return 2f * atan(abs(co), si)
}//: Float = 2f*this.twinNearest(Quaternion.ONE).angleAbout(u)
/**
* finds Q, the quaternion nearest to this quaternion representing a rotation purely about the global u axis
* Q is NOT unitized
* finds Q, the quaternion nearest to this quaternion representing a rotation purely
* about the global u axis. Q is NOT unitized
* @param v the global axis
* @return Q
**/
fun project(v: Vector3) = Quaternion(w, xyz.dot(v) / v.lenSq() * v)
/**
* finds Q, the quaternion nearest to this quaternion representing a rotation NOT on the global u axis.
* Q is NOT unitized
* finds Q, the quaternion nearest to this quaternion representing a rotation NOT
* on the global u axis. Q is NOT unitized
* @param v the global axis
* @return Q
**/
fun reject(v: Vector3) = Quaternion(w, v.cross(xyz).cross(v) / v.lenSq())
/**
* finds Q, the quaternion nearest to this quaternion whose local u direction aligns with the global v direction.
* Q is NOT unitized
* finds Q, the quaternion nearest to this quaternion whose local u direction aligns
* with the global v direction. Q is NOT unitized
* @param u the local direction
* @param v the global direction
* @return Q
@@ -351,11 +363,12 @@ data class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) {
**/
fun toMatrix(): Matrix3 {
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 */
}
/**