diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/neck.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/neck.kt index 584a59b93..3ef470c49 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/neck.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/neck.kt @@ -6,10 +6,10 @@ import io.github.axisangles.ktmath.Vector3 import solarxr_protocol.datatypes.BodyPart // Neck bends less when looking up -private const val UP_RATIO = 0.75f +private const val UP_RATIO = 0.74f // Neck bends slightly more when looking down. -private const val DOWN_RATIO = 1.1f +private const val DOWN_RATIO = 1.14f /** * Guesses where the neck is supposed to be when the user has a head tracker but no neck tracker. diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/old-spine.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/old-spine.kt new file mode 100644 index 000000000..97f5dec4c --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/old-spine.kt @@ -0,0 +1,237 @@ +package dev.slimevr.skeleton.inputprocessors + +import com.jme3.math.FastMath +import dev.slimevr.config.Settings +import dev.slimevr.skeleton.BodyPartMap +import dev.slimevr.skeleton.InputSkeleton +import dev.slimevr.skeleton.SkeletonInputProcessor +import io.github.axisangles.ktmath.Quaternion +import io.github.axisangles.ktmath.Vector3 +import solarxr_protocol.datatypes.BodyPart +import kotlin.enums.enumEntries + +// TODO get rid of this file once new spine is done +object OldSpine { + // At this default value, the user's spine should behave as we meant it to. + const val DEFAULT_SPINE_UPPER_LOWER = 0.5f + + private data class Reliability( + val forward: Float, + val side: Float? = null, // Use forward if null + ) + + private enum class SpineSource(val parts: Array) { + UPPER_CHEST(arrayOf(BodyPart.UPPER_CHEST)), + LOWER_CHEST(arrayOf(BodyPart.LOWER_CHEST)), + UPPER_WAIST(arrayOf(BodyPart.UPPER_WAIST)), + LOWER_WAIST(arrayOf(BodyPart.LOWER_WAIST)), + HIP(arrayOf(BodyPart.HIP)), + UPPER_LEGS(arrayOf(BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG)), + } + + private val SPINE_SOURCES = enumEntries().toTypedArray() + + // The higher a value is, the more reliable that source is. +// If a "To" is 2x the "From", it'll use 100% "To". +// If a "To" is the same as "From", it'll do 50-50. +// A negative value will go towards the opposite rotation. +// A From and To may be taken from different roots. + private val SPINE_SOURCE_RELIABILITY = arrayOf( + SpineSource.UPPER_CHEST to mapOf( + SpineSource.UPPER_CHEST to Reliability(-6.5f, -24f), // Itself + // To + SpineSource.LOWER_CHEST to Reliability(1f), + SpineSource.UPPER_WAIST to Reliability(1.5f), + SpineSource.LOWER_WAIST to Reliability(2f), + SpineSource.HIP to Reliability(4f), + SpineSource.UPPER_LEGS to Reliability(1.5f, 1.75f), + ), + SpineSource.LOWER_CHEST to mapOf( + // From + SpineSource.UPPER_CHEST to Reliability(-10f, -42f), + SpineSource.LOWER_CHEST to Reliability(-11f, -45f), // Itself + // To + SpineSource.UPPER_WAIST to Reliability(1.5f, 10f), + SpineSource.LOWER_WAIST to Reliability(2f, 7f), + SpineSource.HIP to Reliability(4f), + SpineSource.UPPER_LEGS to Reliability(3.25f, 3.75f), + ), + SpineSource.UPPER_WAIST to mapOf( + // From + SpineSource.UPPER_CHEST to Reliability(11f), + SpineSource.LOWER_CHEST to Reliability(11.5f), + SpineSource.UPPER_WAIST to Reliability(60f), // Itself + // To + SpineSource.LOWER_WAIST to Reliability(17f, 20f), + SpineSource.HIP to Reliability(5.5f, 8f), + SpineSource.UPPER_LEGS to Reliability(0.25f, 4.25f), + ), + SpineSource.LOWER_WAIST to mapOf( + // From + SpineSource.UPPER_CHEST to Reliability(6f), + SpineSource.LOWER_CHEST to Reliability(6.5f), + SpineSource.UPPER_WAIST to Reliability(15f), + SpineSource.LOWER_WAIST to Reliability(44f), // Itself + // To + SpineSource.HIP to Reliability(10f, 12f), + SpineSource.UPPER_LEGS to Reliability(1.75f, 10f), + ), + SpineSource.HIP to mapOf( + // From + SpineSource.UPPER_CHEST to Reliability(9.5f, 9f), + SpineSource.LOWER_CHEST to Reliability(9.75f, 9.25f), + SpineSource.UPPER_WAIST to Reliability(13f, 20f), + SpineSource.LOWER_WAIST to Reliability(14f, 30f), + SpineSource.HIP to Reliability(32f, 56f), // Itself + // To + SpineSource.UPPER_LEGS to Reliability(7f, 22f), + ), + ) + + /** The spine bones this processor imputes, top down. Other sources are only read as references. */ + val SPINE_IMPUTED_PARTS: List = SPINE_SOURCE_RELIABILITY.flatMap { (source, _) -> source.parts.toList() } + + private fun nearestActive(startIndex: Int, step: Int, sourceActive: Map): Int? { + var i = startIndex + while (i in SPINE_SOURCES.indices) { + if (sourceActive[SPINE_SOURCES[i]] == true) return i + i += step + } + return null + } + + private fun getFromTo(selfIndex: Int, sourceActive: Map): Pair { + // Find From, the first active bone in itself + its parents. + val from = nearestActive(selfIndex, -1, sourceActive) + + // Find To, the next active bone in its children, skipping the immediate next if itself is active except if it's the only one (for hip). + // If To is null, we return null for the whole thing + val isActive = sourceActive[SPINE_SOURCES[selfIndex]] == true + val immediateNext = selfIndex + 1 + val takeImmediateNext = (!isActive || selfIndex + 2 == SPINE_SOURCES.count()) && sourceActive[SPINE_SOURCES[immediateNext]] == true + val to = if (takeImmediateNext) immediateNext else nearestActive(selfIndex + 2, 1, sourceActive) + + return from to to + } + + /** + * Remaps the From-To ratio with From-To reliability. + */ + private fun remapRatioWithReliability(fromUpperToLower: Float, reliability: Float): Float = if (fromUpperToLower <= DEFAULT_SPINE_UPPER_LOWER) { + fromUpperToLower * (1f / DEFAULT_SPINE_UPPER_LOWER) * reliability + } else { + reliability + (fromUpperToLower - DEFAULT_SPINE_UPPER_LOWER) * (1f / DEFAULT_SPINE_UPPER_LOWER) * (1f - reliability) + } + + private fun interpolateRatio(fromUpperToLower: Float, curvature: Float, fromReliability: Float, toReliability: Float, isActive: Boolean): Float { + val reliability = (toReliability / fromReliability) * DEFAULT_SPINE_UPPER_LOWER + + return if (isActive) { + // Use a reliability-adjusted curvature directly if relaxing an active bone + reliability * curvature + } else { + // Remap the upper-lower ratio with reliability + val reliabilityAdjusted = remapRatioWithReliability(fromUpperToLower, reliability) + // Lerp from upper-lower to reliability-adjusted according to curvature + FastMath.lerp(fromUpperToLower, reliabilityAdjusted, curvature) + } + } + + 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 -> + // Very important that this is interpQ as lerpQ will break on axes/polarity + acc.interpQ(rotation, 1f / (index + 1)) + } ?: Quaternion.IDENTITY + } + + /** + * Interpolates between 2 rotations with a different ratio for forward and the rest/side. + */ + fun interpolateForwardSide(fromRotation: Quaternion, toRotation: Quaternion, forwardRatio: Float, sideRatio: Float?): Quaternion { + if (sideRatio == null) return fromRotation.interpQ(toRotation, forwardRatio) + + // Work with the rotation delta to avoid axes being ambiguous + val delta = fromRotation.inv() * toRotation + + // Decompose the delta into its twist (forward) and swing (remaining/sides) along the x-axis + val forward = delta.project(Vector3.POS_X).unit() + val side = delta / forward + + // Scale each axis of the delta with our ratios + val scaledForward = forward.pow(forwardRatio) + val scaledSide = side.pow(sideRatio) + + // Recompose the delta. We removed formward from delta to get side, so we must add forward to side back. + val scaledDelta = scaledSide * scaledForward + + // Return scaled delta applied to From rotation + return fromRotation * scaledDelta + } + + /** + * Handles imputing the rotation of spine bones that are not actively receiving data from the rotations + * of nearby bones. + */ + class SpineInputProcessor(val settings: Settings) : SkeletonInputProcessor { + override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { + val ratios = settings.context.state.value.data.skeletonConfig.ratios + val boneInputs = BodyPartMap(mutableInputSkeleton) + val sourceActive = SPINE_SOURCES.associateWith { it.parts.all { part -> boneInputs[part]?.isRotationActive == true } } + val fromTo = SPINE_SOURCE_RELIABILITY.withIndex().associate { (selfIndex, source) -> + source.first to getFromTo(selfIndex, sourceActive) + } + + for ((spineIndex, spineSourceReliability) in SPINE_SOURCE_RELIABILITY.withIndex()) { + val spineSource = spineSourceReliability.first + + // For optimization's sake, assume only one BodyPart per SpineSource we traverse. + val bodyPart = spineSource.parts.first() + val bone = boneInputs[bodyPart] ?: continue + val isActive = bone.isRotationActive + + // Get reliabilities mapped to this spine bone + val reliabilities = SPINE_SOURCE_RELIABILITY[spineIndex].second + + // Get the spine sources for the current spineSource + val (fromIndex, toIndex) = fromTo[spineSource] ?: error("No fromTo for $spineSource found.") + // toIndex will be null if the bone has no active children. + if (toIndex == null) continue + val (fromSpineSource, toSpineSource) = if (fromIndex != null) { + SPINE_SOURCES[fromIndex] to SPINE_SOURCES[toIndex] + } else { + // Fall back to itself to its to's to + val toTo = fromTo[SPINE_SOURCES[toIndex]] ?: continue + spineSource to SPINE_SOURCES[toTo.second ?: continue] + } + + // We are interpolating as-if we were the To, but using self instead of to + val sourceActive = isActive || fromIndex == null + + // Get the interpolation ratios. sideRatio is null if nothing specified for side. + val fromReliability = reliabilities[fromSpineSource] ?: continue + val toReliability = reliabilities[toSpineSource] ?: continue + val forwardRatio = interpolateRatio(ratios.imputeSpineFromUpperToLower, ratios.spineCurvature, fromReliability.forward, toReliability.forward, sourceActive) + val sideRatio = if (fromReliability.side != null || toReliability.side != null) { + interpolateRatio(ratios.imputeSpineFromUpperToLower, ratios.spineCurvature, fromReliability.side ?: fromReliability.forward, toReliability.side ?: toReliability.forward, sourceActive) + } else { + null + } + + // If from is null, use to as from and use to's to as to + val fromParts = if (fromIndex != null) fromSpineSource.parts else SPINE_SOURCES[toIndex].parts + val toParts = toSpineSource.parts + // Interpolate between from and to using our interpolation ratio. + mutableInputSkeleton[bodyPart] = bone.copy( + rotation = interpolateForwardSide( + averageRotation(boneInputs, fromParts), + averageRotation(boneInputs, toParts), + forwardRatio, + sideRatio, + ), + ) + } + } + } +} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine.kt index c1325429a..3b96a9ea0 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine.kt @@ -36,36 +36,35 @@ private val SPINE_SOURCES = enumEntries().toTypedArray() // A negative value will go towards the opposite rotation. // A From and To may be taken from different roots. // Order matters! Missing elements of the inner arrays will be skipped gracefully. -// TODO mess with proportion! private val SPINE_SOURCE_RELIABILITY = arrayOf( SpineSource.UPPER_CHEST to arrayOf( - SpineSource.UPPER_CHEST to Reliability(-6.5f, -24f), // Itself + SpineSource.UPPER_CHEST to Reliability(-7f, -20f), // Itself // To - SpineSource.LOWER_CHEST to Reliability(1f), - SpineSource.UPPER_WAIST to Reliability(1.5f), - SpineSource.LOWER_WAIST to Reliability(2f), - SpineSource.HIP to Reliability(4f, null, 1f), - SpineSource.UPPER_LEGS to Reliability(3.25f, 3.75f), + SpineSource.LOWER_CHEST to Reliability(-6f, -15f), + SpineSource.UPPER_WAIST to Reliability(1.5f, 4f,), + SpineSource.LOWER_WAIST to Reliability(2f, 5f), + SpineSource.HIP to Reliability(4f, 6f, 0.25f), + SpineSource.UPPER_LEGS to Reliability(4f, 5f), ), SpineSource.LOWER_CHEST to arrayOf( // From - SpineSource.UPPER_CHEST to Reliability(-10f, -42f), - SpineSource.LOWER_CHEST to Reliability(-11f, -45f), // Itself + SpineSource.UPPER_CHEST to Reliability(-10f, 40f), + SpineSource.LOWER_CHEST to Reliability(-11f, 42f), // Itself // To SpineSource.UPPER_WAIST to Reliability(1.5f, 10f), - SpineSource.LOWER_WAIST to Reliability(2f, 7f), - SpineSource.HIP to Reliability(4f, null, 1f), - SpineSource.UPPER_LEGS to Reliability(3.25f, 3.75f), + SpineSource.LOWER_WAIST to Reliability(2f, 8f), + SpineSource.HIP to Reliability(4f, 7f, 0.33f), + SpineSource.UPPER_LEGS to Reliability(4f, 6f), ), SpineSource.UPPER_WAIST to arrayOf( // From SpineSource.UPPER_CHEST to Reliability(11f), SpineSource.LOWER_CHEST to Reliability(11.5f), - SpineSource.UPPER_WAIST to Reliability(60f), // Itself + SpineSource.UPPER_WAIST to Reliability(50f), // Itself // To SpineSource.LOWER_WAIST to Reliability(17f, 20f), - SpineSource.HIP to Reliability(5.5f, 8f, 1f), - SpineSource.UPPER_LEGS to Reliability(0.25f, 4.25f), + SpineSource.HIP to Reliability(5.5f, 13f, 0.85f), + SpineSource.UPPER_LEGS to Reliability(0.25f, 11f), ), SpineSource.LOWER_WAIST to arrayOf( // From @@ -74,8 +73,8 @@ private val SPINE_SOURCE_RELIABILITY = arrayOf( SpineSource.UPPER_WAIST to Reliability(15f), SpineSource.LOWER_WAIST to Reliability(44f), // Itself // To - SpineSource.HIP to Reliability(10f, 12f, 1f), - SpineSource.UPPER_LEGS to Reliability(1.75f, 10f), + SpineSource.HIP to Reliability(10f, 14f, 0.9f), + SpineSource.UPPER_LEGS to Reliability(1.75f, 12f), ), SpineSource.HIP to arrayOf( // From @@ -83,7 +82,7 @@ private val SPINE_SOURCE_RELIABILITY = arrayOf( SpineSource.LOWER_CHEST to Reliability(9.75f, 9.25f), SpineSource.UPPER_WAIST to Reliability(13f, 20f), SpineSource.LOWER_WAIST to Reliability(14f, 30f), - SpineSource.HIP to Reliability(32f, 56f), // Itself + SpineSource.HIP to Reliability(38f, 60f), // Itself // To SpineSource.UPPER_LEGS to Reliability(7f, 22f), ), @@ -144,7 +143,7 @@ private fun averageRotation(inputSkeleton: InputSkeleton, takeBodyParts: Array, reliabilities: Map, boneInputs: InputSkeleton): Quaternion { var accProportion = 0f - return sources.foldIndexed(Quaternion.IDENTITY) { i, acc, fromSource -> + return sources.foldIndexed(Quaternion.ZERO) { i, acc, fromSource -> val rotation = averageRotation(boneInputs, fromSource.parts) if (rotation != null) { val proportion = if (i + 1 == sources.count()) { @@ -155,11 +154,11 @@ private fun weightedAverageRotation(sources: List, reliabilities: M } accProportion += proportion - acc * rotation.pow(proportion) + acc + (rotation * proportion) } else { acc } - } + }.unit() } private fun weightedAverageReliability(sources: List, reliabilities: Map): Pair { @@ -209,7 +208,7 @@ private fun interpolateRatio(fromUpperToLower: Float, curvature: Float, fromReli /** * Interpolates between 2 rotations with a different ratio for forward and the rest/side. */ -fun interpolateForwardSide(fromRotation: Quaternion, toRotation: Quaternion, forwardRatio: Float, sideRatio: Float?): Quaternion { +private fun interpolateForwardSide(fromRotation: Quaternion, toRotation: Quaternion, forwardRatio: Float, sideRatio: Float?): Quaternion { if (sideRatio == null) return fromRotation.interpQ(toRotation, forwardRatio) // Work with the rotation delta to avoid axes being ambiguous 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 a2596b9d5..559b65ccb 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -14,6 +14,7 @@ import dev.slimevr.skeleton.inputprocessors.FallbackInputProcessor import dev.slimevr.skeleton.inputprocessors.FingersInputProcessor import dev.slimevr.skeleton.inputprocessors.HeadPositionFallbackInputProcessor import dev.slimevr.skeleton.inputprocessors.NeckInputProcessor +import dev.slimevr.skeleton.inputprocessors.OldSpine import dev.slimevr.skeleton.inputprocessors.RotationPredictionInputProcessor import dev.slimevr.skeleton.inputprocessors.RotationSmoothingInputProcessor import dev.slimevr.skeleton.inputprocessors.SpineInputProcessor @@ -281,6 +282,7 @@ class Skeleton( HeadPositionFallbackInputProcessor(), FallbackInputProcessor(), UpperLegsRollAlignInputProcessor(settings), +// OldSpine.SpineInputProcessor(settings), SpineInputProcessor(settings), NeckInputProcessor(), DirectLinkInputProcessor(), diff --git a/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt b/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt index 792e25821..6665a3a63 100644 --- a/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt +++ b/server/core/src/main/java/io/github/axisangles/ktmath/Quaternion.kt @@ -8,6 +8,7 @@ import kotlin.math.* @Serializable class Quaternion(val w: Float, val x: Float, val y: Float, val z: Float) { companion object { + val ZERO = Quaternion(0f, 0f, 0f, 0f) val IDENTITY = Quaternion(1f, 0f, 0f, 0f) val I = Quaternion(0f, 1f, 0f, 0f) val J = Quaternion(0f, 0f, 1f, 0f)