mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-30 05:42:46 -08:00
AutoBone weighted average bone contribution
This commit is contained in:
3 files changed
+135
-68
No files matched your search
@@ -0,0 +1,29 @@
|
||||
package dev.slimevr.autobone
|
||||
|
||||
class WeightedAverage {
|
||||
var value: Float = 0f
|
||||
private set
|
||||
var weight: Float = 0f
|
||||
private set
|
||||
|
||||
fun average() = if (weight > 0f) {
|
||||
value / weight
|
||||
} else {
|
||||
0f
|
||||
}
|
||||
|
||||
fun addValue(value: Float, weight: Float) {
|
||||
this.value += value * weight
|
||||
this.weight += weight
|
||||
}
|
||||
|
||||
fun subtractValue(value: Float, weight: Float) {
|
||||
this.value -= value * weight
|
||||
this.weight -= weight
|
||||
}
|
||||
|
||||
fun reset() {
|
||||
value = 0f
|
||||
weight = 0f
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package dev.slimevr.autobone
|
||||
|
||||
import dev.slimevr.skeleton.BodyPartMap
|
||||
import dev.slimevr.skeleton.ComputedSkeleton
|
||||
import dev.slimevr.skeleton.bodyPartMap
|
||||
import dev.slimevr.skeleton.mapValues
|
||||
import dev.slimevr.util.Side
|
||||
import dev.slimevr.util.side
|
||||
import io.github.axisangles.ktmath.Quaternion
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
|
||||
val BODY_PARTS_TO_ADJUST = arrayOf(
|
||||
BodyPart.NECK,
|
||||
BodyPart.UPPER_CHEST,
|
||||
BodyPart.LOWER_CHEST,
|
||||
BodyPart.UPPER_WAIST,
|
||||
BodyPart.LOWER_WAIST,
|
||||
BodyPart.HIP,
|
||||
|
||||
BodyPart.LEFT_UPPER_LEG,
|
||||
BodyPart.LEFT_LOWER_LEG,
|
||||
|
||||
BodyPart.RIGHT_UPPER_LEG,
|
||||
BodyPart.RIGHT_LOWER_LEG,
|
||||
)
|
||||
|
||||
// TODO This should probably be every bone excluding arms, feet, and toes?
|
||||
val CONTRIBUTING_BODY_PARTS = BODY_PARTS_TO_ADJUST.plus(
|
||||
arrayOf(
|
||||
BodyPart.HEAD,
|
||||
),
|
||||
)
|
||||
|
||||
data class TrackerFrame(
|
||||
val rotation: Quaternion,
|
||||
val position: Vector3?,
|
||||
)
|
||||
typealias AutoBoneRecording = List<TrackerFrame>
|
||||
// TODO Figure out actually doing the recording
|
||||
|
||||
typealias BoneContribution = WeightedAverage
|
||||
typealias BoneContributions = BodyPartMap<BoneContribution>
|
||||
|
||||
fun process(recording: BodyPartMap<AutoBoneRecording>) {
|
||||
// TODO Actually make skeletons
|
||||
val skeletonA: ComputedSkeleton = bodyPartMap()
|
||||
val skeletonB: ComputedSkeleton = bodyPartMap()
|
||||
|
||||
val contributions: BoneContributions = bodyPartMap()
|
||||
|
||||
val recordingLength = recording.minOf { it.value.size }
|
||||
for (i in 0 until recordingLength) {
|
||||
val trackers = recording.mapValues { _, frames -> frames[i] }
|
||||
// TODO Apply trackers to skeletons A and B somehow
|
||||
|
||||
computeContributions(contributions, skeletonA, skeletonB)
|
||||
|
||||
// TODO Figure out how to choose adjustment scale
|
||||
// TODO Test new config to see if it actually reduces error
|
||||
}
|
||||
}
|
||||
|
||||
fun boneContribution(contribution: BoneContribution, ankleAToB: Vector3, localBoneTailAToB: Vector3) {
|
||||
val boneToSlideDirectionRatio = ankleAToB.unit().dot(localBoneTailAToB.unit())
|
||||
// - if bone is too short, + if bone is too long
|
||||
contribution.addValue(boneToSlideDirectionRatio, localBoneTailAToB.len())
|
||||
}
|
||||
|
||||
fun computeContributions(contributions: BoneContributions, skeletonA: ComputedSkeleton, skeletonB: ComputedSkeleton) {
|
||||
val leftAnkleA = skeletonA[BodyPart.LEFT_LOWER_LEG]?.tailPosition ?: error("Missing left ankle position A")
|
||||
val leftAnkleB = skeletonB[BodyPart.LEFT_LOWER_LEG]?.tailPosition ?: error("Missing left ankle position B")
|
||||
val leftAnkleAToB = leftAnkleB - leftAnkleA
|
||||
|
||||
val rightAnkleA = skeletonA[BodyPart.RIGHT_LOWER_LEG]?.tailPosition ?: error("Missing right ankle position A")
|
||||
val rightAnkleB = skeletonB[BodyPart.RIGHT_LOWER_LEG]?.tailPosition ?: error("Missing right ankle position B")
|
||||
val rightAnkleAToB = rightAnkleB - rightAnkleA
|
||||
|
||||
// TODO Consider headset sliding; if HMD consistently aligns with sliding, then
|
||||
// either height is wrong, or the headset has weird movement
|
||||
CONTRIBUTING_BODY_PARTS.forEach { bodyPart ->
|
||||
val boneA = skeletonA[bodyPart] ?: return@forEach
|
||||
val boneB = skeletonB[bodyPart] ?: return@forEach
|
||||
|
||||
val contribution = contributions.getOrPut(bodyPart) { BoneContribution() }
|
||||
|
||||
val localBoneTailAToB = boneB.localTailPosition - boneA.localTailPosition
|
||||
val side = bodyPart.side
|
||||
// LEFT (or both)
|
||||
if (side != Side.RIGHT) {
|
||||
boneContribution(
|
||||
contribution,
|
||||
leftAnkleAToB,
|
||||
localBoneTailAToB,
|
||||
)
|
||||
}
|
||||
// RIGHT (or both)
|
||||
if (side != Side.LEFT) {
|
||||
boneContribution(
|
||||
contribution,
|
||||
rightAnkleAToB,
|
||||
localBoneTailAToB,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package dev.slimevr.autobone
|
||||
|
||||
import dev.slimevr.skeleton.ComputedSkeleton
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
|
||||
fun slideContributionWeight(ankleA: Vector3, ankleB: Vector3, localBoneTailA: Vector3, localBoneTailB: Vector3): Float {
|
||||
val ankleAToB = ankleB - ankleA
|
||||
val localBoneTailAToB = localBoneTailB - localBoneTailA
|
||||
|
||||
val boneToSlideDirectionRatio = ankleAToB.unit().dot(localBoneTailAToB.unit())
|
||||
|
||||
// if +, shorten bone, if -, lengthen bone
|
||||
// sum all contribution weights, divide each bone's weight by the sum, then multiply
|
||||
// by the slide magnitude in order to find the corresponding bone change
|
||||
return localBoneTailAToB.len() * boneToSlideDirectionRatio
|
||||
}
|
||||
|
||||
// TODO This is a placeholder
|
||||
val PLACEHOLDER_BODY_PARTS_TO_ADJUST = arrayOf(
|
||||
BodyPart.UPPER_CHEST,
|
||||
BodyPart.LOWER_CHEST,
|
||||
BodyPart.UPPER_WAIST,
|
||||
BodyPart.LOWER_WAIST,
|
||||
BodyPart.HIP,
|
||||
// etc.
|
||||
)
|
||||
|
||||
fun step(skeletonA: ComputedSkeleton, skeletonB: ComputedSkeleton) {
|
||||
// TODO We need to consider both ankles, there are two... We can do contribution per
|
||||
// ankle
|
||||
// TODO This is a placeholder
|
||||
val ankleA = Vector3.ZERO
|
||||
// TODO This is a placeholder
|
||||
val ankleB = Vector3.ZERO
|
||||
|
||||
// TODO Consider headset sliding; if HMD consistently aligns with sliding, then
|
||||
// either height is wrong, or the headset has weird movement
|
||||
val contributions = PLACEHOLDER_BODY_PARTS_TO_ADJUST.map { bodyPart ->
|
||||
val boneA = skeletonA[bodyPart] ?: return@map 0f
|
||||
val boneB = skeletonB[bodyPart] ?: return@map 0f
|
||||
|
||||
slideContributionWeight(
|
||||
ankleA,
|
||||
ankleB,
|
||||
boneA.localTailPosition,
|
||||
boneB.localTailPosition,
|
||||
)
|
||||
}.filter { it > 0f }
|
||||
|
||||
// TODO This is a placeholder
|
||||
val slideMag = 1f
|
||||
|
||||
val contributionSum = contributions.sum()
|
||||
val perBoneSlide = contributions.map {
|
||||
(it * slideMag) / contributionSum
|
||||
}
|
||||
|
||||
// TODO This is a placeholder
|
||||
val config = 1f
|
||||
val newConfigs = perBoneSlide.map {
|
||||
config + it
|
||||
}
|
||||
|
||||
// TODO Test new config to see if it actually reduces error? Or do we just
|
||||
// accept the changes? The contribution scales for each frame and considers
|
||||
// direction; we should be able to skip checking if it reduces error
|
||||
}
|
||||
Reference in new issue
Block a user