mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-27 04:16:32 -08:00
Clean up constraint code
This commit is contained in:
1 file changed
+48
-28
@@ -34,10 +34,9 @@ data class TwistSwingConstraint(
|
||||
val maxDeviationFromTracker: Float = 15f,
|
||||
) : Constraint {
|
||||
override fun apply(localRotation: Quaternion): Quaternion {
|
||||
var (swingQ, twistQ) = decompose(localRotation, Vector3.NEG_Y)
|
||||
var (swingQ, twistQ) = decomposeToSwingTwist(localRotation, Vector3.NEG_Y)
|
||||
swingQ = constrain(swingQ, swing)
|
||||
twistQ = constrain(twistQ, twist)
|
||||
|
||||
return swingQ * twistQ
|
||||
}
|
||||
}
|
||||
@@ -49,8 +48,8 @@ data class HingeConstraint(
|
||||
val maxDeviationFromTracker: Float = 15f,
|
||||
) : Constraint {
|
||||
override fun apply(localRotation: Quaternion): Quaternion {
|
||||
val (_, hingeAxisRot) = decompose(localRotation, Vector3.NEG_X)
|
||||
return constrain(hingeAxisRot, min, max, Vector3.NEG_X)
|
||||
val (_, hingeAxisRot) = decomposeToSwingTwist(localRotation, Vector3.NEG_X)
|
||||
return constrainOnAxis(hingeAxisRot, min, max, Vector3.NEG_X)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,24 +60,30 @@ data class LooseHingeConstraint(
|
||||
val maxDeviationFromTracker: Float = 15f,
|
||||
) : Constraint {
|
||||
override fun apply(localRotation: Quaternion): Quaternion {
|
||||
var (nonHingeRot, hingeAxisRot) = decompose(localRotation, Vector3.NEG_X)
|
||||
hingeAxisRot = constrain(hingeAxisRot, min, max, Vector3.NEG_X)
|
||||
var (nonHingeRot, hingeAxisRot) = decomposeToSwingTwist(
|
||||
localRotation,
|
||||
Vector3.NEG_X,
|
||||
)
|
||||
hingeAxisRot = constrainOnAxis(hingeAxisRot, min, max, Vector3.NEG_X)
|
||||
nonHingeRot = constrain(nonHingeRot, allowedDeviation)
|
||||
return nonHingeRot * hingeAxisRot
|
||||
}
|
||||
}
|
||||
|
||||
private fun decompose(
|
||||
fun decomposeToSwingTwist(
|
||||
rotation: Quaternion,
|
||||
twistAxis: Vector3,
|
||||
): Pair<Quaternion, Quaternion> {
|
||||
val projection = rotation.project(twistAxis).unit()
|
||||
val twist = Quaternion(sqrt(1.0f - projection.xyz.lenSq()) * if (rotation.w >= 0f) 1f else -1f, projection.xyz).unit()
|
||||
val twist = Quaternion(
|
||||
sqrt(1.0f - projection.xyz.lenSq()) * if (rotation.w >= 0f) 1f else -1f,
|
||||
projection.xyz,
|
||||
).unit()
|
||||
val swing = (rotation * twist.inv()).unit()
|
||||
return Pair(swing, twist)
|
||||
}
|
||||
|
||||
private fun constrain(rotation: Quaternion, angle: Float): Quaternion {
|
||||
fun constrain(rotation: Quaternion, angle: Float): Quaternion {
|
||||
// Use angle to get the maximum magnitude the vector part of rotation can be
|
||||
// before it has violated a constraint.
|
||||
// Multiplying by 0.5 uniquely maps angles 0-180 degrees to 0-1 which works
|
||||
@@ -86,36 +91,51 @@ private fun constrain(rotation: Quaternion, angle: Float): Quaternion {
|
||||
val magnitude = sin(angle * 0.5f)
|
||||
val magnitudeSqr = magnitude * magnitude
|
||||
val sign = if (rotation.w >= 0f) 1f else -1f
|
||||
var vector = rotation.xyz
|
||||
var rot = rotation
|
||||
val vector = rotation.xyz
|
||||
|
||||
if (vector.lenSq() > magnitudeSqr) {
|
||||
vector = vector.unit() * magnitude
|
||||
rot = Quaternion(sqrt(1f - magnitudeSqr) * sign, vector)
|
||||
return if (vector.lenSq() > magnitudeSqr) {
|
||||
Quaternion(
|
||||
sqrt(1f - magnitudeSqr) * sign,
|
||||
vector.unit() * magnitude,
|
||||
)
|
||||
} else {
|
||||
rotation.unit()
|
||||
}
|
||||
|
||||
return rot.unit()
|
||||
}
|
||||
|
||||
private fun constrain(rotation: Quaternion, minAngle: Float, maxAngle: Float, axis: Vector3): Quaternion {
|
||||
fun constrainOnAxis(
|
||||
rotation: Quaternion,
|
||||
minAngle: Float,
|
||||
maxAngle: Float,
|
||||
axis: Vector3,
|
||||
): Quaternion {
|
||||
val magnitudeMin = sin(minAngle * 0.5f)
|
||||
val magnitudeMax = sin(maxAngle * 0.5f)
|
||||
val magnitudeSqrMin = magnitudeMin * magnitudeMin * if (minAngle >= 0f) 1f else -1f
|
||||
val magnitudeSqrMax = magnitudeMax * magnitudeMax * if (maxAngle >= 0f) 1f else -1f
|
||||
var vector = rotation.xyz
|
||||
var rot = rotation
|
||||
val vector = rotation.xyz
|
||||
|
||||
val rotMagnitude = vector.lenSq() * if (vector.dot(axis) * sign(rot.w) < 0) -1f else 1f
|
||||
if (rotMagnitude !in magnitudeSqrMin..magnitudeSqrMax) {
|
||||
val distToMin = min(abs(rotMagnitude - magnitudeSqrMin), abs(rotMagnitude + magnitudeSqrMin))
|
||||
val distToMax = min(abs(rotMagnitude - magnitudeSqrMax), abs(rotMagnitude + magnitudeSqrMax))
|
||||
val rotMagnitude =
|
||||
vector.lenSq() * if (vector.dot(axis) * sign(rotation.w) < 0) -1f else 1f
|
||||
return if (rotMagnitude !in magnitudeSqrMin..magnitudeSqrMax) {
|
||||
val distToMin = min(
|
||||
abs(rotMagnitude - magnitudeSqrMin),
|
||||
abs(rotMagnitude + magnitudeSqrMin),
|
||||
)
|
||||
val distToMax = min(
|
||||
abs(rotMagnitude - magnitudeSqrMax),
|
||||
abs(rotMagnitude + magnitudeSqrMax),
|
||||
)
|
||||
|
||||
val magnitude = if (distToMin < distToMax) magnitudeMin else magnitudeMax
|
||||
val magnitudeSqr = abs(if (distToMin < distToMax) magnitudeSqrMin else magnitudeSqrMax)
|
||||
vector = vector.unit() * -magnitude
|
||||
val magnitudeSqr =
|
||||
abs(if (distToMin < distToMax) magnitudeSqrMin else magnitudeSqrMax)
|
||||
|
||||
rot = Quaternion(sqrt(1.0f - magnitudeSqr), vector)
|
||||
Quaternion(
|
||||
sqrt(1.0f - magnitudeSqr),
|
||||
vector.unit() * -magnitude,
|
||||
)
|
||||
} else {
|
||||
rotation.unit()
|
||||
}
|
||||
|
||||
return rot.unit()
|
||||
}
|
||||
Reference in new issue
Block a user