Skeleton processors extract stuff

This commit is contained in:
Erimel committed 2026-09-02 21:32:57 -04:00
1 parent bcfe86e8ac
commit 30f9178fec
6 files changed
+116 -119

No files matched your search

@@ -14,32 +14,32 @@ import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import kotlin.time.ComparableTimeMark
data class VelocityBoneData(
val rotation: Quaternion,
val tailPosition: Vector3,
)
fun computeVelocity(
currentBone: BoneState,
lastVelocityData: VelocityBoneData,
lastTime: ComparableTimeMark,
): Velocity {
val deltaPosition = currentBone.tailPosition - lastVelocityData.tailPosition
val deltaRotation = currentBone.rotation / lastVelocityData.rotation
val deltaTime = (timeSource.markNow() - lastTime).inFloatingSeconds
return Velocity(
linear = deltaPosition / deltaTime,
angular = deltaRotation.toRotationVector() / deltaTime,
)
}
/**
* Computes linear (m/s) and angular (rad/s) velocity for the bones.
*/
class VelocityFkProcessor : SkeletonFkProcessor {
private data class VelocityBoneData(
val rotation: Quaternion,
val tailPosition: Vector3,
)
private val lastVelocityBoneData: BodyPartMap<VelocityBoneData> = bodyPartMap()
private var lastProcessTime = timeSource.markNow()
private fun computeVelocity(
currentBone: BoneState,
lastVelocityData: VelocityBoneData,
lastTime: ComparableTimeMark,
): Velocity {
val deltaPosition = currentBone.tailPosition - lastVelocityData.tailPosition
val deltaRotation = currentBone.rotation / lastVelocityData.rotation
val deltaTime = (timeSource.markNow() - lastTime).inFloatingSeconds
return Velocity(
linear = deltaPosition / deltaTime,
angular = deltaRotation.toRotationVector() / deltaTime,
)
}
override fun process(mutableInputSkeleton: InputSkeleton, fk: ComputedSkeleton, floorLevel: Float) {
fk.forEachBone { part, bone ->
val lastVelocityData = lastVelocityBoneData[part]
@@ -11,6 +11,10 @@ import dev.slimevr.util.timeSource
import io.github.axisangles.ktmath.Quaternion
import solarxr_protocol.rpc.FilteringType
private const val SMOOTHING_MULTIPLIER = 100f
private const val SMOOTH_MIN = 0.6f
private const val SMOOTH_MAX = 0.95f
/**
* Running average of bone rotations to smooth them out.
*/
@@ -44,10 +48,4 @@ class BoneSmoothingInputProcessor(val settings: Settings) : SkeletonInputProcess
}
smoothed = newSmoothed
}
companion object {
private const val SMOOTHING_MULTIPLIER = 100f
private const val SMOOTH_MIN = 0.6f
private const val SMOOTH_MAX = 0.95f
}
}
@@ -6,24 +6,24 @@ import dev.slimevr.skeleton.SkeletonInputProcessor
import io.github.axisangles.ktmath.Quaternion
import solarxr_protocol.datatypes.BodyPart
/**
* Rotates the first Quaternion to match its yaw and roll to the rotation of
* the second Quaternion
*
* @param from the first Quaternion
* @param to the second Quaternion
* @return the rotated Quaternion
*/
private fun alignYawRoll(from: Quaternion, to: Quaternion): Quaternion {
val r = from.inv() * to
val c = Quaternion(r.w, -r.x, 0f, 0f)
return (from * r * c).unit()
}
/**
* Handles rotating the hip' yaw and roll to match the upper legs' yaw and roll.
*/
class HipYawRollAlignInputProcessor(val settings: Settings) : SkeletonInputProcessor {
/**
* Rotates the first Quaternion to match its yaw and roll to the rotation of
* the second Quaternion
*
* @param from the first Quaternion
* @param to the second Quaternion
* @return the rotated Quaternion
*/
private fun alignYawRoll(from: Quaternion, to: Quaternion): Quaternion {
val r = from.inv() * to
val c = Quaternion(r.w, -r.x, 0f, 0f)
return (from * r * c).unit()
}
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
val hipBone = mutableInputSkeleton[BodyPart.HIP] ?: return
if (hipBone.isRotationActive) return
@@ -10,6 +10,71 @@ import solarxr_protocol.datatypes.BodyPart
// At this default value, the user's spine should behave as we meant it to.
const val DEFAULT_SPINE_RATIO = 0.5f
private enum class SpineSource(val parts: Array<BodyPart>) {
CHEST(arrayOf(BodyPart.CHEST)),
HIP(arrayOf(BodyPart.HIP)),
WAIST(arrayOf(BodyPart.WAIST)),
UPPER_LEGS(arrayOf(BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG)),
}
/**
* Returns the interpolation ratio modified with the reliability and curve.
* If a single bone is missing, ratio is reliability adjusted.
* At 0.0 curve, ratio is raw.
* At 0.5 curve, ratio is reliability adjusted.
* At 1.0 curve, ratio is 0 for the first in chain and 1 for the last in chain.
*/
private fun interpolateRatio(chainIndex: Int, chainSize: Int, fromUpperToLower: Float, curvature: Float, fromReliability: Float, toReliability: Float): Float {
val reliabilityAdjusted = remapRatioWithReliability(fromUpperToLower, fromReliability, toReliability)
if (chainSize <= 1) return reliabilityAdjusted // Single missing bone; reliability adjusted
return if (curvature <= DEFAULT_SPINE_RATIO) {
// Raw to reliability adjusted
FastMath.lerp(fromUpperToLower, reliabilityAdjusted, curvature * 2f)
} else {
// Reliability adjusted to max curve
val maxCurve = chainIndex / (chainSize - 1).toFloat()
FastMath.lerp(reliabilityAdjusted, maxCurve, (curvature - DEFAULT_SPINE_RATIO) * 2f)
}
}
/**
* Remaps a ratio This assumes a default ratio of 50%.
*/
private fun remapRatioWithReliability(ratio: Float, fromReliability: Float, toReliability: Float): Float {
val reliability = (fromReliability / toReliability) * DEFAULT_SPINE_RATIO
return if (ratio <= DEFAULT_SPINE_RATIO) {
ratio * 2f * reliability
} else {
reliability + (ratio - DEFAULT_SPINE_RATIO) * 2f * (1f - reliability)
}
}
private fun averageRotation(inputSkeleton: InputSkeleton, takeBodyParts: Array<BodyPart> = arrayOf()): Quaternion {
val bonesToAverage = inputSkeleton.values.filter { it.bodyPart in takeBodyParts }
return bonesToAverage.map { it.rotation }
.reduceIndexedOrNull { index, acc, rotation ->
acc.lerpQ(rotation, 1f / (index + 1))
} ?: Quaternion.IDENTITY
}
private fun reliabilityOf(bodyPart: BodyPart, source: SpineSource): Float = when (bodyPart) {
BodyPart.WAIST -> when (source) {
SpineSource.CHEST -> 0.6f
SpineSource.HIP, SpineSource.UPPER_LEGS -> 1.0f
else -> error("Invalid spine combination $bodyPart, $source")
}
BodyPart.HIP -> when (source) {
SpineSource.CHEST, SpineSource.UPPER_LEGS -> 1.0f
SpineSource.WAIST -> 0.8f
else -> error("Invalid spine combination $bodyPart, $source")
}
else -> error("Invalid missing spine body part $bodyPart")
}
/**
* Handles imputing the rotation of spine bones that are not actively receiving data from the rotations
* of nearby bones.
@@ -17,71 +82,6 @@ const val DEFAULT_SPINE_RATIO = 0.5f
* Similar to FallbackProcessor specifically for the waist and hip.
*/
class SpineImputeInputProcessor(val settings: Settings) : SkeletonInputProcessor {
private enum class SpineSource(val parts: Array<BodyPart>) {
CHEST(arrayOf(BodyPart.CHEST)),
HIP(arrayOf(BodyPart.HIP)),
WAIST(arrayOf(BodyPart.WAIST)),
UPPER_LEGS(arrayOf(BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG)),
}
/**
* Returns the interpolation ratio modified with the reliability and curve.
* If a single bone is missing, ratio is reliability adjusted.
* At 0.0 curve, ratio is raw.
* At 0.5 curve, ratio is reliability adjusted.
* At 1.0 curve, ratio is 0 for the first in chain and 1 for the last in chain.
*/
private fun interpolateRatio(chainIndex: Int, chainSize: Int, fromUpperToLower: Float, curvature: Float, fromReliability: Float, toReliability: Float): Float {
val reliabilityAdjusted = remapRatioWithReliability(fromUpperToLower, fromReliability, toReliability)
if (chainSize <= 1) return reliabilityAdjusted // Single missing bone; reliability adjusted
return if (curvature <= DEFAULT_SPINE_RATIO) {
// Raw to reliability adjusted
FastMath.lerp(fromUpperToLower, reliabilityAdjusted, curvature * 2f)
} else {
// Reliability adjusted to max curve
val maxCurve = chainIndex / (chainSize - 1).toFloat()
FastMath.lerp(reliabilityAdjusted, maxCurve, (curvature - DEFAULT_SPINE_RATIO) * 2f)
}
}
/**
* Remaps a ratio This assumes a default ratio of 50%.
*/
private fun remapRatioWithReliability(ratio: Float, fromReliability: Float, toReliability: Float): Float {
val reliability = (fromReliability / toReliability) * DEFAULT_SPINE_RATIO
return if (ratio <= DEFAULT_SPINE_RATIO) {
ratio * 2f * reliability
} else {
reliability + (ratio - DEFAULT_SPINE_RATIO) * 2f * (1f - reliability)
}
}
private fun averageRotation(inputSkeleton: InputSkeleton, takeBodyParts: Array<BodyPart> = arrayOf()): Quaternion {
val bonesToAverage = inputSkeleton.values.filter { it.bodyPart in takeBodyParts }
return bonesToAverage.map { it.rotation }
.reduceIndexedOrNull { index, acc, rotation ->
acc.lerpQ(rotation, 1f / (index + 1))
} ?: Quaternion.IDENTITY
}
private fun reliabilityOf(bodyPart: BodyPart, source: SpineSource): Float = when (bodyPart) {
BodyPart.WAIST -> when (source) {
SpineSource.CHEST -> 0.6f
SpineSource.HIP, SpineSource.UPPER_LEGS -> 1.0f
else -> error("Invalid spine combination $bodyPart, $source")
}
BodyPart.HIP -> when (source) {
SpineSource.CHEST, SpineSource.UPPER_LEGS -> 1.0f
SpineSource.WAIST -> 0.8f
else -> error("Invalid spine combination $bodyPart, $source")
}
else -> error("Invalid missing spine body part $bodyPart")
}
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
val ratios = settings.context.state.value.data.skeletonConfig.ratios
@@ -6,6 +6,20 @@ import dev.slimevr.skeleton.SkeletonInputProcessor
import io.github.axisangles.ktmath.Quaternion
import solarxr_protocol.datatypes.BodyPart
/**
* Rotates the first Quaternion to match its roll to the rotation of
* the second Quaternion
*
* @param from the first Quaternion
* @param to the second Quaternion
* @return the rotated Quaternion
*/
private fun alignRoll(from: Quaternion, to: Quaternion): Quaternion {
val r = to.inv() * from
val c = Quaternion(r.w, 0f, -r.y, 0f)
return (to * r * c).unit()
}
/**
* Handles rotating the upper legs' roll to match the lower legs' roll.
*/
@@ -20,20 +34,6 @@ class UpperLegsRollAlignInputProcessor(val settings: Settings) : SkeletonInputPr
BodyPart.RIGHT_UPPER_LEG to BodyPart.RIGHT_LOWER_LEG,
)
/**
* Rotates the first Quaternion to match its roll to the rotation of
* the second Quaternion
*
* @param from the first Quaternion
* @param to the second Quaternion
* @return the rotated Quaternion
*/
private fun alignRoll(from: Quaternion, to: Quaternion): Quaternion {
val r = to.inv() * from
val c = Quaternion(r.w, 0f, -r.y, 0f)
return (to * r * c).unit()
}
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
val ratio = settings.context.state.value.data.skeletonConfig.ratios.interpolateUpperLegsTwistWithLowerLegs
if (ratio == 0f) return
@@ -204,7 +204,6 @@ class Skeleton(
fkProcessors = listOf(
VelocityFkProcessor(),
// LocalizerFkProcessor(settings),
// TODO move prediction and smoothing here?
FootPlantFkProcessor(settings),
ToeSnapFkProcessor(settings),
),