mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-04 15:52:35 -08:00
Fallback processor optimizations
This commit is contained in:
1 parent
61c4e47984
commit
5202013cf5
4 files changed
+137
-77
No files matched your search
@@ -15,12 +15,12 @@ import io.github.axisangles.ktmath.Quaternion
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import solarxr_protocol.rpc.ResetType
|
||||
|
||||
private data class VelocityData(
|
||||
private data class BoneSample(
|
||||
val rotation: Quaternion,
|
||||
val position: Vector3,
|
||||
)
|
||||
|
||||
private fun computeVelocity(currentVelocityData: VelocityData, lastVelocityData: VelocityData, deltaTime: Float): Velocity {
|
||||
private fun computeVelocity(currentVelocityData: BoneSample, lastVelocityData: BoneSample, deltaTime: Float): Velocity {
|
||||
val deltaPosition = currentVelocityData.position - lastVelocityData.position
|
||||
val deltaRotation = currentVelocityData.rotation / lastVelocityData.rotation
|
||||
return Velocity(
|
||||
@@ -29,7 +29,7 @@ private fun computeVelocity(currentVelocityData: VelocityData, lastVelocityData:
|
||||
)
|
||||
}
|
||||
|
||||
private const val SMOOTHING_MS = 25f
|
||||
private const val SMOOTHING_MS = 20f
|
||||
private fun smoothVelocity(currentVelocity: Velocity, lastVelocity: Velocity, deltaTime: Float): Velocity {
|
||||
val t = (deltaTime * (millisecondsInSecond / SMOOTHING_MS)).coerceAtMost(1f)
|
||||
return Velocity(
|
||||
@@ -44,7 +44,7 @@ private fun smoothVelocity(currentVelocity: Velocity, lastVelocity: Velocity, de
|
||||
class VelocityComputedProcessor :
|
||||
SkeletonComputedProcessor,
|
||||
ResettableSkeletonProcessor {
|
||||
private val lastVelocities: BodyPartMap<Pair<Velocity, VelocityData>> = bodyPartMap()
|
||||
private val lastVelocities: BodyPartMap<Pair<Velocity, BoneSample>> = bodyPartMap()
|
||||
private var lastProcessTime = timeSource.markNow()
|
||||
|
||||
override fun process(mutableComputedSkeleton: ComputedSkeleton, inputSkeleton: InputSkeleton) {
|
||||
@@ -55,13 +55,13 @@ class VelocityComputedProcessor :
|
||||
|
||||
for ((bodyPart, bone) in mutableComputedSkeleton) {
|
||||
// Compute current velocity
|
||||
val currentVelocityData = VelocityData(bone.rotation, bone.tailPosition)
|
||||
val currentVelocity = lastVelocities[bodyPart]?.second?.let { computeVelocity(currentVelocityData, it, deltaTime) } ?: ZERO_VELOCITY
|
||||
val currentBoneSample = BoneSample(bone.rotation, bone.tailPosition)
|
||||
val currentVelocity = lastVelocities[bodyPart]?.second?.let { computeVelocity(currentBoneSample, it, deltaTime) } ?: ZERO_VELOCITY
|
||||
|
||||
val smoothedVelocity = smoothVelocity(currentVelocity, lastVelocities[bodyPart]?.first ?: ZERO_VELOCITY, deltaTime)
|
||||
mutableComputedSkeleton[bodyPart] = bone.copy(velocity = smoothedVelocity)
|
||||
|
||||
lastVelocities[bodyPart] = smoothedVelocity to currentVelocityData
|
||||
lastVelocities[bodyPart] = smoothedVelocity to currentBoneSample
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,14 @@ package dev.slimevr.skeleton.inputprocessors
|
||||
|
||||
import dev.slimevr.skeleton.InputSkeleton
|
||||
import dev.slimevr.skeleton.SkeletonInputProcessor
|
||||
import dev.slimevr.skeleton.bodyPartMap
|
||||
import dev.slimevr.skeleton.findFirstParent
|
||||
import dev.slimevr.util.timeSource
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
import kotlin.collections.set
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
private val FALLBACK_REFRESH = 1.seconds
|
||||
|
||||
// Only allow child fallbacks in certain cases
|
||||
val CHILD_FALLBACKS = mapOf(
|
||||
@@ -59,23 +64,38 @@ val CHILD_FALLBACKS = mapOf(
|
||||
* This does not produce reliable data. For that, see the direct link processor.
|
||||
*/
|
||||
class FallbackInputProcessor : SkeletonInputProcessor {
|
||||
// First is rotation, second is acceleration
|
||||
val cachedFallbacks = bodyPartMap<Pair<BodyPart?, BodyPart?>>()
|
||||
var timeSinceRefresh = timeSource.markNow()
|
||||
|
||||
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
|
||||
// Only lookup fallbacks every so often for optimization's sake
|
||||
if (timeSinceRefresh.elapsedNow() > FALLBACK_REFRESH) {
|
||||
for ((bodyPart, bone) in mutableInputSkeleton) {
|
||||
if (bone.isRotationActive && bone.isAccelerationActive) continue
|
||||
val rotationFallback = if (!bone.isRotationActive) {
|
||||
bodyPart.findFirstParent { mutableInputSkeleton[it]?.isRotationActive == true }
|
||||
?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isRotationActive == true }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val accelFallback = if (!bone.isAccelerationActive) {
|
||||
bodyPart.findFirstParent { mutableInputSkeleton[it]?.isAccelerationActive == true }
|
||||
?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isAccelerationActive == true }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
cachedFallbacks[bodyPart] = rotationFallback to accelFallback
|
||||
}
|
||||
timeSinceRefresh = timeSource.markNow()
|
||||
}
|
||||
|
||||
for ((bodyPart, bone) in mutableInputSkeleton) {
|
||||
if (bone.isRotationActive && bone.isAccelerationActive) continue
|
||||
val newRotation = if (!bone.isRotationActive) {
|
||||
val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isRotationActive == true }
|
||||
?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isRotationActive == true }
|
||||
mutableInputSkeleton[fallbackPart]?.rotation?.eulerHeading() ?: bone.rotation
|
||||
} else {
|
||||
bone.rotation
|
||||
}
|
||||
val newAcceleration = if (!bone.isAccelerationActive) {
|
||||
val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isAccelerationActive == true }
|
||||
?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isAccelerationActive == true }
|
||||
mutableInputSkeleton[fallbackPart]?.acceleration ?: bone.acceleration
|
||||
} else {
|
||||
bone.acceleration
|
||||
}
|
||||
val cachedFallback = cachedFallbacks[bodyPart]
|
||||
val newRotation = mutableInputSkeleton[cachedFallback?.first]?.rotation?.eulerHeading() ?: bone.rotation
|
||||
val newAcceleration = mutableInputSkeleton[cachedFallback?.second]?.acceleration ?: bone.acceleration
|
||||
|
||||
mutableInputSkeleton[bodyPart] = bone.copy(rotation = newRotation, acceleration = newAcceleration)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ package io.github.axisangles.ktmath
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.math.*
|
||||
|
||||
private const val FLOAT_EPSILON = 1e-7f
|
||||
|
||||
@Serializable
|
||||
class Matrix3
|
||||
@Suppress("ktlint") constructor(
|
||||
@@ -339,115 +341,153 @@ class Matrix3
|
||||
*/
|
||||
fun toEulerAnglesAssumingOrthonormal(order: EulerOrder): EulerAngles {
|
||||
val ETA = 1.5707964f
|
||||
when (order) {
|
||||
return when (order) {
|
||||
EulerOrder.XYZ -> {
|
||||
val kc = sqrt(zy * zy + zz * zz)
|
||||
if (kc < 1e-7f) {
|
||||
return EulerAngles(
|
||||
if (kc < FLOAT_EPSILON) {
|
||||
EulerAngles(
|
||||
EulerOrder.XYZ,
|
||||
atan2(yz, yy),
|
||||
ETA.withSign(zx),
|
||||
0f
|
||||
)
|
||||
} else {
|
||||
EulerAngles(
|
||||
EulerOrder.XYZ,
|
||||
atan2(-zy, zz),
|
||||
atan2(zx, kc),
|
||||
atan2(xy * zz - xz * zy, yy * zz - yz * zy)
|
||||
)
|
||||
}
|
||||
|
||||
return EulerAngles(
|
||||
EulerOrder.XYZ,
|
||||
atan2(-zy, zz),
|
||||
atan2(zx, kc),
|
||||
atan2(xy * zz - xz * zy, yy * zz - yz * zy)
|
||||
)
|
||||
}
|
||||
EulerOrder.YZX -> {
|
||||
val kc = sqrt(xx * xx + xz * xz)
|
||||
if (kc < 1e-7f) {
|
||||
return EulerAngles(
|
||||
if (kc < FLOAT_EPSILON) {
|
||||
EulerAngles(
|
||||
EulerOrder.YZX,
|
||||
0f,
|
||||
atan2(zx, zz),
|
||||
ETA.withSign(xy)
|
||||
)
|
||||
} else {
|
||||
EulerAngles(
|
||||
EulerOrder.YZX,
|
||||
atan2(xx * yz - xz * yx, xx * zz - xz * zx),
|
||||
atan2(-xz, xx),
|
||||
atan2(xy, kc)
|
||||
)
|
||||
}
|
||||
|
||||
return EulerAngles(
|
||||
EulerOrder.YZX,
|
||||
atan2(xx * yz - xz * yx, xx * zz - xz * zx),
|
||||
atan2(-xz, xx),
|
||||
atan2(xy, kc)
|
||||
)
|
||||
}
|
||||
EulerOrder.ZXY -> {
|
||||
val kc = sqrt(yy * yy + yx * yx)
|
||||
if (kc < 1e-7f) {
|
||||
return EulerAngles(
|
||||
if (kc < FLOAT_EPSILON) {
|
||||
EulerAngles(
|
||||
EulerOrder.ZXY,
|
||||
ETA.withSign(yz),
|
||||
0f,
|
||||
atan2(xy, xx)
|
||||
)
|
||||
} else {
|
||||
EulerAngles(
|
||||
EulerOrder.ZXY,
|
||||
atan2(yz, kc),
|
||||
atan2(yy * zx - yx * zy, yy * xx - yx * xy),
|
||||
atan2(-yx, yy)
|
||||
)
|
||||
}
|
||||
|
||||
return EulerAngles(
|
||||
EulerOrder.ZXY,
|
||||
atan2(yz, kc),
|
||||
atan2(yy * zx - yx * zy, yy * xx - yx * xy),
|
||||
atan2(-yx, yy)
|
||||
)
|
||||
}
|
||||
EulerOrder.ZYX -> {
|
||||
val kc = sqrt(xy * xy + xx * xx)
|
||||
if (kc < 1e-7f) {
|
||||
return EulerAngles(
|
||||
if (kc < FLOAT_EPSILON) {
|
||||
EulerAngles(
|
||||
EulerOrder.ZYX,
|
||||
0f,
|
||||
ETA.withSign(-xz),
|
||||
atan2(-yx, yy)
|
||||
)
|
||||
} else {
|
||||
EulerAngles(
|
||||
EulerOrder.ZYX,
|
||||
atan2(zx * xy - zy * xx, yy * xx - yx * xy),
|
||||
atan2(-xz, kc),
|
||||
atan2(xy, xx)
|
||||
)
|
||||
}
|
||||
|
||||
return EulerAngles(
|
||||
EulerOrder.ZYX,
|
||||
atan2(zx * xy - zy * xx, yy * xx - yx * xy),
|
||||
atan2(-xz, kc),
|
||||
atan2(xy, xx)
|
||||
)
|
||||
}
|
||||
|
||||
EulerOrder.YXZ -> {
|
||||
val kc = sqrt(zx * zx + zz * zz)
|
||||
if (kc < 1e-7f) {
|
||||
return EulerAngles(
|
||||
if (kc < FLOAT_EPSILON) {
|
||||
EulerAngles(
|
||||
EulerOrder.YXZ,
|
||||
ETA.withSign(-zy),
|
||||
atan2(-xz, xx),
|
||||
0f
|
||||
)
|
||||
} else {
|
||||
EulerAngles(
|
||||
EulerOrder.YXZ,
|
||||
atan2(-zy, kc),
|
||||
atan2(zx, zz),
|
||||
atan2(yz * zx - yx * zz, xx * zz - xz * zx)
|
||||
)
|
||||
}
|
||||
|
||||
return EulerAngles(
|
||||
EulerOrder.YXZ,
|
||||
atan2(-zy, kc),
|
||||
atan2(zx, zz),
|
||||
atan2(yz * zx - yx * zz, xx * zz - xz * zx)
|
||||
)
|
||||
}
|
||||
EulerOrder.XZY -> {
|
||||
val kc = sqrt(yz * yz + yy * yy)
|
||||
if (kc < 1e-7f) {
|
||||
return EulerAngles(
|
||||
if (kc < FLOAT_EPSILON) {
|
||||
EulerAngles(
|
||||
EulerOrder.XZY,
|
||||
atan2(-zy, zz),
|
||||
0f,
|
||||
ETA.withSign(-yx)
|
||||
)
|
||||
} else {
|
||||
EulerAngles(
|
||||
EulerOrder.XZY,
|
||||
atan2(yz, yy),
|
||||
atan2(xy * yz - xz * yy, zz * yy - zy * yz),
|
||||
atan2(-yx, kc)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return EulerAngles(
|
||||
EulerOrder.XZY,
|
||||
atan2(yz, yy),
|
||||
atan2(xy * yz - xz * yy, zz * yy - zy * yz),
|
||||
atan2(-yx, kc)
|
||||
)
|
||||
/**
|
||||
* Returns just the y-axis of the Euler angle.
|
||||
*/
|
||||
fun eulerHeadingAssumingOrthonormal(order: EulerOrder): Float {
|
||||
val ETA = 1.5707964f
|
||||
return when (order) {
|
||||
EulerOrder.XYZ -> {
|
||||
val kc = sqrt(zy * zy + zz * zz)
|
||||
if (kc < FLOAT_EPSILON) ETA.withSign(zx)
|
||||
else atan2(zx, kc)
|
||||
}
|
||||
EulerOrder.YZX -> {
|
||||
val kc = sqrt(xx * xx + xz * xz)
|
||||
if (kc < FLOAT_EPSILON) atan2(zx, zz)
|
||||
else atan2(-xz, xx)
|
||||
}
|
||||
EulerOrder.ZXY -> {
|
||||
val kc = sqrt(yy * yy + yx * yx)
|
||||
if (kc < FLOAT_EPSILON) 0f
|
||||
else atan2(yy * zx - yx * zy, yy * xx - yx * xy)
|
||||
}
|
||||
EulerOrder.ZYX -> {
|
||||
val kc = sqrt(xy * xy + xx * xx)
|
||||
if (kc < FLOAT_EPSILON) ETA.withSign(-xz)
|
||||
else atan2(-xz, kc)
|
||||
}
|
||||
EulerOrder.YXZ -> {
|
||||
val kc = sqrt(zx * zx + zz * zz)
|
||||
if (kc < FLOAT_EPSILON) atan2(-xz, xx)
|
||||
else atan2(zx, zz)
|
||||
}
|
||||
EulerOrder.XZY -> {
|
||||
val kc = sqrt(yz * yz + yy * yy)
|
||||
if (kc < FLOAT_EPSILON) 0f
|
||||
else atan2(xy * yz - xz * yy, zz * yy - zy * yz)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,7 +396,7 @@ class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) {
|
||||
/**
|
||||
* Returns a Quaternion representing the heading of this Quaternion using Euler angles and preserving polarity.
|
||||
*/
|
||||
fun eulerHeading() = rotationAroundYAxis(this.toEulerAngles(EulerOrder.YZX).y).twinNearest(this)
|
||||
fun eulerHeading() = rotationAroundYAxis(this.toMatrix().eulerHeadingAssumingOrthonormal(EulerOrder.YZX)).twinNearest(this)
|
||||
|
||||
/**
|
||||
* applies this quaternion's rotation to that vector
|
||||
|
||||
Reference in new issue
Block a user