From d96af009d94781ccce456fc4a4861e37b9c5d211 Mon Sep 17 00:00:00 2001 From: Erimel Date: Wed, 22 Jul 2026 18:56:02 -0400 Subject: [PATCH] Optimize skeleton processors and rid order assumption --- .../dev/slimevr/android/ForegroundService.kt | 2 +- .../main/java/dev/slimevr/skeleton/module.kt | 14 ++-- .../skeleton/processors/bone-active-link.kt | 25 +++--- .../skeleton/processors/bone-direct-link.kt | 7 +- .../processors/bone-yaw-roll-align.kt | 25 +++--- .../skeleton/processors/fingers-impute.kt | 7 +- .../skeleton/processors/spine-impute.kt | 81 +++++++++---------- .../src/main/java/dev/slimevr/desktop/Main.kt | 1 - 8 files changed, 80 insertions(+), 82 deletions(-) diff --git a/server/android/src/main/java/dev/slimevr/android/ForegroundService.kt b/server/android/src/main/java/dev/slimevr/android/ForegroundService.kt index bba20e896..523483ab6 100644 --- a/server/android/src/main/java/dev/slimevr/android/ForegroundService.kt +++ b/server/android/src/main/java/dev/slimevr/android/ForegroundService.kt @@ -167,7 +167,7 @@ class ForegroundService : Service() { featureFlags = FeatureFlags( skipCheckUdev = true, keybindSupport = KeybindSupport.UNSUPPORTED, - udevRulesInstalled = false + udevRulesInstalled = false, ), keybindManager = keybindManager, skeleton = skeleton, 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 1cf40a1c7..4ea79acae 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -3,10 +3,10 @@ package dev.slimevr.skeleton import dev.slimevr.Phase1ContextProvider import dev.slimevr.context.Behaviour import dev.slimevr.context.Context +import dev.slimevr.skeleton.processors.BoneActiveLinkProcessor import dev.slimevr.skeleton.processors.BoneDirectLinkProcessor import dev.slimevr.skeleton.processors.BoneFallbackProcessor import dev.slimevr.skeleton.processors.BonePredictionProcessor -import dev.slimevr.skeleton.processors.BoneActiveLinkProcessor import dev.slimevr.skeleton.processors.BoneSmoothingProcessor import dev.slimevr.skeleton.processors.BoneYawRollAlignProcessor import dev.slimevr.skeleton.processors.FingerImputeProcessor @@ -34,14 +34,10 @@ data class BoneState( val tailPosition: Vector3 = Vector3.NULL, val parentBone: BoneState? = null, ) { - private val orientationOffset = if (offset.len() > 0f) { - if (offset.unit().y == 1f) { - Quaternion.I - } else { - Quaternion.fromTo(Vector3.NEG_Y, offset) - } - } else { - Quaternion.IDENTITY + private val orientationOffset = when { + offset.len() == 0f -> Quaternion.IDENTITY + offset.unit().y == 1f -> Quaternion.I + else -> Quaternion.fromTo(Vector3.NEG_Y, offset) } val orientation: Quaternion = rotation * orientationOffset val localRotation: Quaternion diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-active-link.kt b/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-active-link.kt index e1b168fad..72cb142ca 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-active-link.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-active-link.kt @@ -25,18 +25,19 @@ class BoneActiveLinkProcessor : SkeletonProcessor { override fun process(state: SkeletonState): SkeletonState { val boneInputs = state.boneInputs - return state.copy( - boneInputs = boneInputs.mapValues { (bodyPart, bone) -> - if (bone.isActive) return@mapValues bone + val updatedLinkedBones = linkedToSources.keys.associateWith { bodyPart -> + val bone = boneInputs.getValue(bodyPart) + if (bone.isActive) return@associateWith bone - val closestActiveBone = linkedToSources[bodyPart] - ?.firstNotNullOfOrNull { part -> - boneInputs[part]?.takeIf { it.isActive } - } - bone.copy( - rawRotation = closestActiveBone?.rawRotation ?: bone.rawRotation, - ) - }, - ) + val closestActiveBone = linkedToSources.getValue(bodyPart) + .firstNotNullOfOrNull { part -> + boneInputs[part]?.takeIf { it.isActive } + } + bone.copy( + rawRotation = closestActiveBone?.rawRotation ?: bone.rawRotation, + ) + } + + return state.copy(boneInputs = boneInputs + updatedLinkedBones) } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-direct-link.kt b/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-direct-link.kt index e3e9c53a6..e8dd76b54 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-direct-link.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-direct-link.kt @@ -43,10 +43,11 @@ class BoneDirectLinkProcessor : SkeletonProcessor { override fun process(state: SkeletonState): SkeletonState { val updatedBoneInputs = state.boneInputs.toMutableMap() - for ((bodyPart, bone) in state.boneInputs) { - if (bone.isActive || bodyPart !in linkedToSource) continue + for (bodyPart in linkedToSource.keys) { + val bone = updatedBoneInputs.getValue(bodyPart) + if (bone.isActive) continue - val sourceBone = updatedBoneInputs[linkedToSource[bodyPart]] + val sourceBone = updatedBoneInputs[linkedToSource.getValue(bodyPart)] updatedBoneInputs[bodyPart] = bone.copy(rawRotation = sourceBone?.rawRotation ?: bone.rawRotation) } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-yaw-roll-align.kt b/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-yaw-roll-align.kt index e87dcfbf7..a96f6afa1 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-yaw-roll-align.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/processors/bone-yaw-roll-align.kt @@ -15,7 +15,7 @@ class BoneYawRollAlignProcessor(val settings: Settings) : SkeletonProcessor { * First value is the BodyPart to be aligned. * * Second value is a Pair containing a list of BodyParts to align with as well as a boolean specifying - * whether the BodyPart to be aligned must be active. + * whether the BodyPart to be aligned must be inactive. */ private val bodyPartToSources = mapOf( BodyPart.HIP to (setOf(BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG) to true), @@ -32,19 +32,20 @@ class BoneYawRollAlignProcessor(val settings: Settings) : SkeletonProcessor { BodyPart.RIGHT_UPPER_LEG to ratios.interpolateUpperLegsWithLowerLegs, ) - return state.copy( - boneInputs = boneInputs.mapValues { (bodyPart, bone) -> - val sources = bodyPartToSources[bodyPart] ?: return@mapValues bone - if (!sources.second || bone.isActive) return@mapValues bone - val mixFactor = bodyPartToMixFactor[bodyPart] ?: return@mapValues bone + val updatedAlignedBones = bodyPartToSources.keys.associateWith { bodyPart -> + val bone = boneInputs.getValue(bodyPart) + val sources = bodyPartToSources.getValue(bodyPart) + if (sources.second && bone.isActive) return@associateWith bone + val mixFactor = bodyPartToMixFactor[bodyPart] ?: return@associateWith bone - val sourceRotation = boneInputs.resolveRotationFor(sources.first) - val aligned = alignYawRoll(bone.rawRotation, sourceRotation) - val newRotation = bone.rawRotation.interpR(aligned, mixFactor) + val sourceRotation = boneInputs.resolveRotationFor(sources.first) + val aligned = alignYawRoll(bone.rawRotation, sourceRotation) + val newRotation = bone.rawRotation.interpR(aligned, mixFactor) - bone.copy(rawRotation = newRotation) - }, - ) + bone.copy(rawRotation = newRotation) + } + + return state.copy(boneInputs = boneInputs + updatedAlignedBones) } /** diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/fingers-impute.kt b/server/core/src/main/java/dev/slimevr/skeleton/processors/fingers-impute.kt index 2602a8d3a..7fe2150df 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/fingers-impute.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/processors/fingers-impute.kt @@ -52,10 +52,11 @@ class FingerImputeProcessor : SkeletonProcessor { override fun process(state: SkeletonState): SkeletonState { val updatedBoneInputs = state.boneInputs.toMutableMap() - for ((bodyPart, bone) in state.boneInputs) { - if (bone.isActive || bodyPart !in fingerToSource) continue + for (bodyPart in fingerToSource.keys) { + val bone = updatedBoneInputs.getValue(bodyPart) + if (bone.isActive) continue - val sourceBone = updatedBoneInputs[fingerToSource[bodyPart]] + val sourceBone = updatedBoneInputs[fingerToSource.getValue(bodyPart)] updatedBoneInputs[bodyPart] = bone.copy(rawRotation = sourceBone?.rawRotation ?: bone.rawRotation) } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/processors/spine-impute.kt b/server/core/src/main/java/dev/slimevr/skeleton/processors/spine-impute.kt index 6d177116e..837518f30 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/processors/spine-impute.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/processors/spine-impute.kt @@ -47,54 +47,53 @@ class SpineImputeProcessor(val settings: Settings) : SkeletonProcessor { if (!hasHip) add(BodyPart.HIP) } - return state.copy( - boneInputs = boneInputs.mapValues { (bodyPart, bone) -> - val chainIndex = missingSpineParts.indexOf(bodyPart) - if (chainIndex == -1) return@mapValues bone + val updatedSpineBones = missingSpineParts.withIndex().associate { (chainIndex, bodyPart) -> + val bone = boneInputs.getValue(bodyPart) - // Get the first active bones above and below this one in the chain - val (fromBodyParts, toBodyParts) = when (bodyPart) { - BodyPart.WAIST -> { - val from = chestSet.takeIf { hasChest } ?: return@mapValues bone - val to = when { - hasHip -> hipSet - hasUpperLegs -> upperLegsSet - else -> return@mapValues bone - } - from to to + // Get the first active bones above and below this one in the chain + val (fromBodyParts, toBodyParts) = when (bodyPart) { + BodyPart.WAIST -> { + val from = chestSet.takeIf { hasChest } ?: return@associate bodyPart to bone + val to = when { + hasHip -> hipSet + hasUpperLegs -> upperLegsSet + else -> return@associate bodyPart to bone } - - BodyPart.HIP -> { - val from = when { - hasWaist -> waistSet - hasChest -> chestSet - else -> return@mapValues bone - } - val to = upperLegsSet.takeIf { hasUpperLegs } ?: return@mapValues bone - from to to - } - - else -> error("Invalid missing spine body part $bodyPart") + from to to } - val fromReliability = combinationToReliability[(bodyPart to fromBodyParts)] ?: error("Invalid from body part combination $bodyPart, $fromBodyParts") - val toReliability = combinationToReliability[(bodyPart to toBodyParts)] ?: error("Invalid to body part combination $bodyPart, $toBodyParts") + BodyPart.HIP -> { + val from = when { + hasWaist -> waistSet + hasChest -> chestSet + else -> return@associate bodyPart to bone + } + val to = upperLegsSet.takeIf { hasUpperLegs } ?: return@associate bodyPart to bone + from to to + } - val interpolateRatio = interpolateRatio( - chainIndex, - missingSpineParts.size, - ratios.imputeSpineFromUpperToLower, - ratios.imputeSpineCurvature, - fromReliability, - toReliability, - ) + else -> error("Invalid missing spine body part $bodyPart") + } - val fromRotation = boneInputs.resolveRotationFor(fromBodyParts) - val toRotation = boneInputs.resolveRotationFor(toBodyParts) + val fromReliability = combinationToReliability[(bodyPart to fromBodyParts)] ?: error("Invalid from body part combination $bodyPart, $fromBodyParts") + val toReliability = combinationToReliability[(bodyPart to toBodyParts)] ?: error("Invalid to body part combination $bodyPart, $toBodyParts") - bone.copy(rawRotation = fromRotation.interpQ(toRotation, interpolateRatio)) - }, - ) + val interpolateRatio = interpolateRatio( + chainIndex, + missingSpineParts.size, + ratios.imputeSpineFromUpperToLower, + ratios.imputeSpineCurvature, + fromReliability, + toReliability, + ) + + val fromRotation = boneInputs.resolveRotationFor(fromBodyParts) + val toRotation = boneInputs.resolveRotationFor(toBodyParts) + + bodyPart to bone.copy(rawRotation = fromRotation.interpQ(toRotation, interpolateRatio)) + } + + return state.copy(boneInputs = boneInputs + updatedSpineBones) } /** diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt b/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt index ece046c9f..49359dcfe 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt @@ -146,7 +146,6 @@ fun main(args: Array) = runBlocking { tapDetectionManager = tapDetectionManager, ) - try { appContext.startObserving()