diff --git a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt index f1b495a8c..a69e0f350 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt @@ -257,13 +257,15 @@ class ComputedSkeletonBehaviour( } // Use single map for the inputs that we'll mutate for performance - val mutableBoneInputs: InputSkeleton = BodyPartMap(if (targetState.pausedProcessedBoneInputs != null) { - // If tracking is paused, we only run the skeleton loop for the always active bones (head and neck) - // and add the pausedProcessedBoneInputs later on. - targetState.boneInputs.filter { it.key in ALWAYS_ACTIVE_PARTS } - } else { - targetState.boneInputs - }) + val mutableBoneInputs: InputSkeleton = BodyPartMap( + if (targetState.pausedProcessedBoneInputs != null) { + // If tracking is paused, we only run the skeleton loop for the always active bones (head and neck) + // and add the pausedProcessedBoneInputs later on. + targetState.boneInputs.filter { it.key in ALWAYS_ACTIVE_PARTS } + } else { + targetState.boneInputs + }, + ) // Run pre-FK processors (input processors) for (processor in inputProcessors) processor.process(mutableBoneInputs, targetState.skeletonHeight) @@ -306,6 +308,7 @@ class ComputedSkeletonBehaviour( for (processor in targetProcessors) processor.process(ikTargets, mutableBoneInputs, mutableComputedSkeleton, targetState.floorLevel) // Run IK + // TODO to optimize we could pass previous post-IK mutableBoneInputs as starting point ccdIk( mutableBoneInputs, mutableComputedSkeleton, diff --git a/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt b/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt index a4b78e1f5..571a623da 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt @@ -8,8 +8,6 @@ import dev.slimevr.skeleton.SkeletonComputedProcessor import dev.slimevr.skeleton.Velocity import dev.slimevr.skeleton.ZERO_VELOCITY import dev.slimevr.skeleton.bodyPartMap -import dev.slimevr.skeleton.findFirstParent -import dev.slimevr.skeleton.forEachBone import dev.slimevr.util.inFloatingSeconds import dev.slimevr.util.millisecondsInSecond import dev.slimevr.util.timeSource @@ -58,26 +56,20 @@ class VelocityComputedProcessor( val deltaTime = (now - lastProcessTime).inFloatingSeconds lastProcessTime = now - mutableComputedSkeleton.forEachBone { part, bone -> - // The expected TPS for the bone is its input's or its first parent that has one. - // If there's no available expected TPS, that means there's no movement to derive velocity from anyways. - val expectedTps = inputSkeleton[part]?.expectedTps - ?: inputSkeleton[part.findFirstParent { inputSkeleton[it]?.expectedTps != null }]?.expectedTps - ?: return@forEachBone - + for ((bodyPart, bone) in mutableComputedSkeleton) { // Compute current velocity val currentVelocityData = VelocityData(bone.rotation, bone.tailPosition) - val currentVelocity = lastVelocityData[part]?.let { computeVelocity(currentVelocityData, it, deltaTime) } ?: ZERO_VELOCITY + val currentVelocity = lastVelocityData[bodyPart]?.let { computeVelocity(currentVelocityData, it, deltaTime) } ?: ZERO_VELOCITY val newVelocity = if (smooth) { - smoothVelocity(currentVelocity, lastVelocities[part] ?: ZERO_VELOCITY, deltaTime) + smoothVelocity(currentVelocity, lastVelocities[bodyPart] ?: ZERO_VELOCITY, deltaTime) } else { currentVelocity } - mutableComputedSkeleton[part] = bone.copy(velocity = newVelocity) + mutableComputedSkeleton[bodyPart] = bone.copy(velocity = newVelocity) - lastVelocityData[part] = currentVelocityData - lastVelocities[part] = newVelocity + lastVelocityData[bodyPart] = currentVelocityData + lastVelocities[bodyPart] = newVelocity } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt index 852299219..40d84997d 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt @@ -46,7 +46,7 @@ val alwaysActiveBodyParts = arrayOf( BodyPart.NECK, BodyPart.HEAD, ) -fun getActiveBodyParts(inputs: InputSkeleton) = inputs.filter { it.value.isRotationActive }.map { it.key } + alwaysActiveBodyParts +fun getActiveBodyParts(inputs: InputSkeleton) = alwaysActiveBodyParts + inputs.filter { it.value.isRotationActive }.map { it.key } // Follows a bone touching that floor that is considered planted object FloorLocalizer { diff --git a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt index cde4e0959..b919ff209 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt @@ -113,12 +113,10 @@ fun ccdIk( if (goals.isEmpty()) return for (i in 0..maxIterations) { - // Iterate chains not meeting the threshold - goals.filter { - chainDistanceFromTarget(mutableComputedSkeleton, it.chain, it.target) > threshold - }.ifEmpty { - break - }.forEach { goal -> + for (goal in goals) { + // Only iterate chains not meeting the threshold + if (chainDistanceFromTarget(mutableComputedSkeleton, goal.chain, goal.target) <= threshold) break + // The chain from the current bone to the end, iterating backwards val iterationChain = goal.chain.takeLast((i % goal.chain.size) + 1) ccdIkIteration( diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt index a8ea9dc1b..1f7377b11 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt @@ -3,7 +3,6 @@ package dev.slimevr.skeleton.inputprocessors import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.findFirstParent -import dev.slimevr.skeleton.forEachBone import solarxr_protocol.datatypes.BodyPart import kotlin.collections.set @@ -61,8 +60,8 @@ val CHILD_FALLBACKS = mapOf( */ class FallbackInputProcessor : SkeletonInputProcessor { override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { - mutableInputSkeleton.forEach { (bodyPart, bone) -> - if (bone.isRotationActive && bone.isAccelerationActive) return@forEach + 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 } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-prediction.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-prediction.kt index 2cc738ba4..b75bf842b 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-prediction.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-prediction.kt @@ -6,7 +6,6 @@ import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.ResettableSkeletonProcessor import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.bodyPartMap -import dev.slimevr.skeleton.forEachBone import dev.slimevr.util.MonotonicValueTimeMark import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Quaternion @@ -56,13 +55,13 @@ class RotationPredictionInputProcessor(val settings: Settings) : val now = timeSource.markNow() val newVelocities = bodyPartMap() - mutableInputSkeleton.forEachBone { bodyPart, bone -> - if (!bone.isRotationActive) return@forEachBone + for ((bodyPart, bone) in mutableInputSkeleton) { + if (!bone.isRotationActive) continue val prev = deltas[bodyPart] if (prev == null) { newVelocities[bodyPart] = BoneDelta(bone.rotation, Quaternion.IDENTITY, now) - return@forEachBone + continue } val bonePredictionAmount = filteringAmount * getMultiplier(bodyPart) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-smoothing.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-smoothing.kt index 2f69056fe..acfd80bf9 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-smoothing.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-smoothing.kt @@ -6,7 +6,6 @@ import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.ResettableSkeletonProcessor import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.bodyPartMap -import dev.slimevr.skeleton.forEachBone import dev.slimevr.util.inFloatingSeconds import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Quaternion @@ -45,8 +44,8 @@ class RotationSmoothingInputProcessor(val settings: Settings) : val alpha = ((1 - smoothingAmount) * lastFrameTimeSeconds * SMOOTHING_MULTIPLIER).coerceIn(0f, 1f) val newSmoothed = bodyPartMap() - mutableInputSkeleton.forEachBone { bodyPart, bone -> - if (!bone.isRotationActive) return@forEachBone + for ((bodyPart, bone) in mutableInputSkeleton) { + if (!bone.isRotationActive) continue val prev = smoothed[bodyPart] ?: bone.rotation val rotation = prev.lerpQ(bone.rotation, alpha).unit() diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/tracker-offset.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/tracker-offset.kt index 1beef5403..c71711e4f 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/tracker-offset.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/tracker-offset.kt @@ -2,17 +2,16 @@ package dev.slimevr.skeleton.inputprocessors import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor -import io.github.axisangles.ktmath.Vector3 /** * Handles tracker offset. For example, an HMD's rotation will be about 10cm in front of the head bone. */ class TrackerOffsetInputProcessor : SkeletonInputProcessor { override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { - mutableInputSkeleton.forEach { (part, input) -> - if (input.trackerOffset.isZero()) return@forEach - val localOffset = input.rotation.sandwich(input.trackerOffset) - mutableInputSkeleton[part] = input.copy(position = input.position?.plus(localOffset)) + for ((bodyPart, bone) in mutableInputSkeleton) { + if (bone.trackerOffset.isZero()) continue + val localOffset = bone.rotation.sandwich(bone.trackerOffset) + mutableInputSkeleton[bodyPart] = bone.copy(position = bone.position?.plus(localOffset)) } } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upsampling.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upsampling.kt index 547dfc297..c6bc40034 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upsampling.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upsampling.kt @@ -5,7 +5,6 @@ import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.ResettableSkeletonProcessor import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.skeleton.bodyPartMap -import dev.slimevr.skeleton.forEachBone import dev.slimevr.util.inFloatingSeconds import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Quaternion @@ -42,8 +41,8 @@ class UpsamplingInputProcessor : val deltaTime = lastProcessTime.elapsedNow().inFloatingSeconds lastProcessTime = timeSource.markNow() - mutableInputSkeleton.forEachBone { bodyPart, bone -> - if (bone.expectedTps == null) return@forEachBone + for ((bodyPart, bone) in mutableInputSkeleton) { + if (bone.expectedTps == null) continue // For a 100hz tracker on 500hz skeleton, this will be 0.2f (interpolating 1/5 every frame) val interpolationRatio = (deltaTime * bone.expectedTps.toFloat()).coerceAtMost(1f)