Changed ONE to IDENTITY and ZERO to NULL. Added Quaternion.fromRotationVector(x, y, z)

This commit is contained in:
AxisAngles committed 2023-01-26 19:32:57 -08:00
1 parent 07d97e0d77
commit 2cf7d6b2d3
5 files changed
+35 -19

No files matched your search

@@ -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)) {
@@ -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)
}
@@ -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
@@ -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(