From 30f9178fec6ef3866d65efee58819b48e75ec5c4 Mon Sep 17 00:00:00 2001 From: Erimel Date: Wed, 2 Sep 2026 21:32:57 -0400 Subject: [PATCH] Skeleton processors extract stuff --- .../slimevr/skeleton/fkprocessors/velocity.kt | 38 ++--- .../inputprocessors/bone-smoothing.kt | 10 +- .../inputprocessors/hip-yaw-roll-align.kt | 28 ++-- .../skeleton/inputprocessors/spine-impute.kt | 130 +++++++++--------- .../inputprocessors/upper-legs-roll-align.kt | 28 ++-- .../main/java/dev/slimevr/skeleton/module.kt | 1 - 6 files changed, 116 insertions(+), 119 deletions(-) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/velocity.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/velocity.kt index 177f73d96..bd02034f4 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/velocity.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/velocity.kt @@ -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 = 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] diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-smoothing.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-smoothing.kt index 1f39520a7..6eed05556 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-smoothing.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-smoothing.kt @@ -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 - } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt index 7d93ed504..33d3c3569 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt @@ -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 diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine-impute.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine-impute.kt index 3d5453a1e..8a0dc4d89 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine-impute.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine-impute.kt @@ -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) { + 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 = 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) { - 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 = 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 diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt index 7c03a511a..823c22d33 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt @@ -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 diff --git a/server/core/src/main/java/dev/slimevr/skeleton/module.kt b/server/core/src/main/java/dev/slimevr/skeleton/module.kt index a1b13c43a..65d5099d3 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -204,7 +204,6 @@ class Skeleton( fkProcessors = listOf( VelocityFkProcessor(), // LocalizerFkProcessor(settings), - // TODO move prediction and smoothing here? FootPlantFkProcessor(settings), ToeSnapFkProcessor(settings), ),