|
| 1 | +package dev.slimevr.unit |
| 2 | + |
| 3 | +import com.jme3.math.FastMath |
| 4 | +import dev.slimevr.VRServer.Companion.getNextLocalTrackerId |
| 5 | +import dev.slimevr.tracking.processor.HumanPoseManager |
| 6 | +import dev.slimevr.tracking.trackers.Tracker |
| 7 | +import dev.slimevr.tracking.trackers.TrackerPosition |
| 8 | +import dev.slimevr.tracking.trackers.TrackerStatus |
| 9 | +import dev.slimevr.tracking.trackers.udp.IMUType |
| 10 | +import dev.slimevr.unit.TrackerUtils.assertAnglesApproxEqual |
| 11 | +import dev.slimevr.unit.TrackerUtils.quatApproxEqual |
| 12 | +import io.eiren.util.collections.FastList |
| 13 | +import io.github.axisangles.ktmath.EulerAngles |
| 14 | +import io.github.axisangles.ktmath.EulerOrder |
| 15 | +import io.github.axisangles.ktmath.Quaternion |
| 16 | +import org.junit.jupiter.api.Test |
| 17 | +import kotlin.random.Random |
| 18 | + |
| 19 | +class SkeletonResetTests { |
| 20 | + |
| 21 | + val resetSource = "Unit Test" |
| 22 | + |
| 23 | + @Test |
| 24 | + fun testSkeletonReset() { |
| 25 | + val rand = Random(42) |
| 26 | + |
| 27 | + val hmd = mkTrack(TrackerPosition.HEAD, true, true, false) |
| 28 | + val chest = mkTrack(TrackerPosition.CHEST) |
| 29 | + val hip = mkTrack(TrackerPosition.HIP) |
| 30 | + |
| 31 | + val upperLeft = mkTrack(TrackerPosition.LEFT_UPPER_LEG) |
| 32 | + val lowerLeft = mkTrack(TrackerPosition.LEFT_LOWER_LEG) |
| 33 | + |
| 34 | + val upperRight = mkTrack(TrackerPosition.RIGHT_UPPER_LEG) |
| 35 | + val lowerRight = mkTrack(TrackerPosition.RIGHT_LOWER_LEG) |
| 36 | + |
| 37 | + // Collect all our trackers |
| 38 | + val tracks = arrayOf(chest, hip, upperLeft, lowerLeft, upperRight, lowerRight) |
| 39 | + val tracksWithHmd = tracks.plus(hmd) |
| 40 | + val trackerList = FastList(tracksWithHmd) |
| 41 | + |
| 42 | + // Initialize skeleton and everything |
| 43 | + val hpm = HumanPoseManager(trackerList) |
| 44 | + |
| 45 | + val headRot1 = EulerAngles(EulerOrder.YZX, 0f, FastMath.HALF_PI, FastMath.QUARTER_PI).toQuaternion() |
| 46 | + val expectRot1 = EulerAngles(EulerOrder.YZX, 0f, FastMath.HALF_PI, 0f).toQuaternion() |
| 47 | + |
| 48 | + // Randomize tracker orientations, these should be zeroed and matched to the |
| 49 | + // headset yaw by full reset |
| 50 | + for (tracker in tracks) { |
| 51 | + val init = EulerAngles( |
| 52 | + EulerOrder.YZX, |
| 53 | + rand.nextFloat() * FastMath.TWO_PI, |
| 54 | + rand.nextFloat() * FastMath.TWO_PI, |
| 55 | + rand.nextFloat() * FastMath.TWO_PI, |
| 56 | + ).toQuaternion() |
| 57 | + tracker.setRotation(init) |
| 58 | + } |
| 59 | + hmd.setRotation(headRot1) |
| 60 | + hpm.resetTrackersFull(resetSource) |
| 61 | + |
| 62 | + for (tracker in tracks) { |
| 63 | + val actual = tracker.getRotation() |
| 64 | + assert(quatApproxEqual(expectRot1, actual)) { |
| 65 | + "\"${tracker.name}\" did not reset to the reference rotation. Expected <$expectRot1>, actual <$actual>." |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Randomize full tracker orientations, these should match the headset yaw but |
| 70 | + // retain orientation otherwise |
| 71 | + for (tracker in tracks) { |
| 72 | + val init = EulerAngles( |
| 73 | + EulerOrder.YZX, |
| 74 | + rand.nextFloat() * FastMath.TWO_PI, |
| 75 | + rand.nextFloat() * FastMath.TWO_PI, |
| 76 | + rand.nextFloat() * FastMath.TWO_PI, |
| 77 | + ).toQuaternion() |
| 78 | + tracker.setRotation(init) |
| 79 | + } |
| 80 | + hmd.setRotation(Quaternion.IDENTITY) |
| 81 | + hpm.resetTrackersYaw(resetSource) |
| 82 | + |
| 83 | + for (tracker in tracks) { |
| 84 | + val yaw = TrackerUtils.yaw(tracker.getRotation()) |
| 85 | + assertAnglesApproxEqual(0f, yaw, "\"${tracker.name}\" did not reset to the reference rotation.") |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun testSkeletonMount() { |
| 91 | + val hmd = mkTrack(TrackerPosition.HEAD, true, true, false) |
| 92 | + val chest = mkTrack(TrackerPosition.CHEST) |
| 93 | + val hip = mkTrack(TrackerPosition.HIP) |
| 94 | + |
| 95 | + val upperLeft = mkTrack(TrackerPosition.LEFT_UPPER_LEG) |
| 96 | + val lowerLeft = mkTrack(TrackerPosition.LEFT_LOWER_LEG) |
| 97 | + |
| 98 | + val upperRight = mkTrack(TrackerPosition.RIGHT_UPPER_LEG) |
| 99 | + val lowerRight = mkTrack(TrackerPosition.RIGHT_LOWER_LEG) |
| 100 | + |
| 101 | + // Collect all our trackers |
| 102 | + val tracks = arrayOf(chest, hip, upperLeft, lowerLeft, upperRight, lowerRight) |
| 103 | + val tracksWithHmd = tracks.plus(hmd) |
| 104 | + val trackerList = FastList(tracksWithHmd) |
| 105 | + |
| 106 | + // Initialize skeleton and everything |
| 107 | + val hpm = HumanPoseManager(trackerList) |
| 108 | + |
| 109 | + // Just a bunch of random mounting orientations |
| 110 | + val expected = arrayOf( |
| 111 | + Pair(chest, Quaternion.SLIMEVR.FRONT), |
| 112 | + Pair(hip, Quaternion.SLIMEVR.RIGHT), |
| 113 | + Pair(upperLeft, Quaternion.SLIMEVR.BACK), |
| 114 | + Pair(lowerLeft, Quaternion.SLIMEVR.LEFT), |
| 115 | + Pair(upperRight, Quaternion.SLIMEVR.FRONT), |
| 116 | + Pair(lowerRight, Quaternion.SLIMEVR.RIGHT), |
| 117 | + ) |
| 118 | + // Rotate the tracker to fit the expected mounting orientation |
| 119 | + for ((tracker, mountRot) in expected) { |
| 120 | + tracker.setRotation(mkTrackMount(mountRot)) |
| 121 | + } |
| 122 | + // Then perform a mounting reset |
| 123 | + hpm.resetTrackersMounting(resetSource) |
| 124 | + |
| 125 | + for ((tracker, mountRot) in expected) { |
| 126 | + // Some mounting needs to be inverted (when in a specific pose) |
| 127 | + // TODO: Make this less hardcoded, accept alternative poses |
| 128 | + val expectedMounting = when (tracker.trackerPosition) { |
| 129 | + TrackerPosition.CHEST, |
| 130 | + TrackerPosition.HIP, |
| 131 | + TrackerPosition.LEFT_LOWER_LEG, |
| 132 | + TrackerPosition.RIGHT_LOWER_LEG, |
| 133 | + -> mountRot * Quaternion.SLIMEVR.FRONT |
| 134 | + |
| 135 | + TrackerPosition.LEFT_UPPER_LEG, |
| 136 | + TrackerPosition.RIGHT_UPPER_LEG, |
| 137 | + -> mountRot |
| 138 | + |
| 139 | + else -> mountRot |
| 140 | + } |
| 141 | + val actualMounting = tracker.resetsHandler.mountRotFix |
| 142 | + |
| 143 | + // Make sure yaw matches |
| 144 | + val expectedY = TrackerUtils.yaw(expectedMounting) |
| 145 | + val actualY = TrackerUtils.yaw(actualMounting) |
| 146 | + assertAnglesApproxEqual(expectedY, actualY, "\"${tracker.name}\" did not reset to the reference rotation.") |
| 147 | + |
| 148 | + // X and Z components should be zero for mounting |
| 149 | + assert(FastMath.isApproxZero(actualMounting.x)) { |
| 150 | + "\"${tracker.name}\" did not reset to the reference rotation. Expected <0.0>, actual <${actualMounting.x}>." |
| 151 | + } |
| 152 | + assert(FastMath.isApproxZero(actualMounting.z)) { |
| 153 | + "\"${tracker.name}\" did not reset to the reference rotation. Expected <0.0>, actual <${actualMounting.z}>." |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + fun mkTrack(pos: TrackerPosition, hmd: Boolean = false, computed: Boolean = false, reset: Boolean = true): Tracker { |
| 159 | + val name = "test ${pos.designation}" |
| 160 | + val tracker = Tracker( |
| 161 | + null, |
| 162 | + getNextLocalTrackerId(), |
| 163 | + name, |
| 164 | + name, |
| 165 | + pos, |
| 166 | + hasPosition = hmd, |
| 167 | + hasRotation = true, |
| 168 | + isComputed = computed, |
| 169 | + imuType = IMUType.UNKNOWN, |
| 170 | + needsReset = true, |
| 171 | + needsMounting = reset, |
| 172 | + isHmd = hmd, |
| 173 | + trackRotDirection = false, |
| 174 | + ) |
| 175 | + tracker.status = TrackerStatus.OK |
| 176 | + return tracker |
| 177 | + } |
| 178 | + |
| 179 | + fun mkTrackMount(rot: Quaternion): Quaternion = rot * (TrackerUtils.frontRot / rot) |
| 180 | +} |
0 commit comments