Skip to content

Commit e8986a8

Browse files
committed
Rename TrackerTestUtils methods & add quat/vec
1 parent 0236a05 commit e8986a8

3 files changed

Lines changed: 82 additions & 42 deletions

File tree

server/core/src/test/java/dev/slimevr/unit/MountingResetTests.kt

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import com.jme3.math.FastMath
44
import dev.slimevr.VRServer.Companion.getNextLocalTrackerId
55
import dev.slimevr.tracking.trackers.Tracker
66
import dev.slimevr.tracking.trackers.udp.IMUType
7-
import dev.slimevr.unit.TrackerTestUtils.assertAnglesApproxEqual
8-
import dev.slimevr.unit.TrackerTestUtils.deg
9-
import dev.slimevr.unit.TrackerTestUtils.yaw
7+
import dev.slimevr.unit.TrackerTestUtils.assertAngleEquals
8+
import dev.slimevr.unit.TrackerTestUtils.degYaw
9+
import dev.slimevr.unit.TrackerTestUtils.radToDeg
10+
import dev.slimevr.unit.TrackerTestUtils.radYaw
1011
import io.github.axisangles.ktmath.EulerAngles
1112
import io.github.axisangles.ktmath.EulerOrder
1213
import io.github.axisangles.ktmath.Quaternion
@@ -26,7 +27,7 @@ class MountingResetTests {
2627
fun testResetAndMounting(): List<DynamicTest> = TrackerTestUtils.directions.flatMap { e ->
2728
TrackerTestUtils.directions.map { m ->
2829
DynamicTest.dynamicTest(
29-
"Full and Mounting Reset Test of Tracker (Expected: ${deg(e)}, reference: ${deg(m)})",
30+
"Full and Mounting Reset Test of Tracker (Expected: ${degYaw(e)}, reference: ${degYaw(m)})",
3031
) {
3132
checkResetMounting(e, m)
3233
}
@@ -56,12 +57,12 @@ class MountingResetTests {
5657
tracker.setRotation(trackerRot)
5758
tracker.resetsHandler.resetMounting(Quaternion.IDENTITY)
5859

59-
val expectedYaw = yaw(expected)
60-
val resultYaw = yaw(tracker.resetsHandler.mountRotFix)
61-
assertAnglesApproxEqual(
60+
val expectedYaw = radYaw(expected)
61+
val resultYaw = radYaw(tracker.resetsHandler.mountRotFix)
62+
assertAngleEquals(
6263
expectedYaw,
6364
resultYaw,
64-
"Resulting mounting yaw after full reset is not equal to reference yaw (${deg(expectedYaw)} vs ${deg(resultYaw)})",
65+
message = "Resulting mounting yaw after full reset is not equal to reference yaw (${radToDeg(expectedYaw)} vs ${radToDeg(resultYaw)})",
6566
)
6667

6768
// Apply full reset and mounting plus offset
@@ -73,12 +74,12 @@ class MountingResetTests {
7374
// it needs to be applied twice
7475
tracker.resetsHandler.resetMounting(reference * reference)
7576

76-
val expectedYaw2 = yaw(expected)
77-
val resultYaw2 = yaw(tracker.resetsHandler.mountRotFix)
78-
assertAnglesApproxEqual(
77+
val expectedYaw2 = radYaw(expected)
78+
val resultYaw2 = radYaw(tracker.resetsHandler.mountRotFix)
79+
assertAngleEquals(
7980
expectedYaw2,
8081
resultYaw2,
81-
"Resulting mounting yaw after full reset with offset is not equal to reference yaw (${deg(expectedYaw2)} vs ${deg(resultYaw2)})",
82+
message = "Resulting mounting yaw after full reset with offset is not equal to reference yaw (${radToDeg(expectedYaw2)} vs ${radToDeg(resultYaw2)})",
8283
)
8384

8485
// Apply yaw reset and mounting
@@ -88,12 +89,12 @@ class MountingResetTests {
8889
tracker.setRotation(trackerRot)
8990
tracker.resetsHandler.resetMounting(Quaternion.IDENTITY)
9091

91-
val expectedYaw3 = yaw(expected)
92-
val resultYaw3 = yaw(tracker.resetsHandler.mountRotFix)
93-
assertAnglesApproxEqual(
92+
val expectedYaw3 = radYaw(expected)
93+
val resultYaw3 = radYaw(tracker.resetsHandler.mountRotFix)
94+
assertAngleEquals(
9495
expectedYaw3,
9596
resultYaw3,
96-
"Resulting mounting yaw after yaw reset is not equal to reference yaw (${deg(expectedYaw3)} vs ${deg(resultYaw3)})",
97+
message = "Resulting mounting yaw after yaw reset is not equal to reference yaw (${radToDeg(expectedYaw3)} vs ${radToDeg(resultYaw3)})",
9798
)
9899

99100
// Apply yaw reset and mounting plus offset
@@ -106,12 +107,12 @@ class MountingResetTests {
106107
// it needs to be applied twice
107108
tracker.resetsHandler.resetMounting(reference * reference)
108109

109-
val expectedYaw4 = yaw(expected)
110-
val resultYaw4 = yaw(tracker.resetsHandler.mountRotFix)
111-
assertAnglesApproxEqual(
110+
val expectedYaw4 = radYaw(expected)
111+
val resultYaw4 = radYaw(tracker.resetsHandler.mountRotFix)
112+
assertAngleEquals(
112113
expectedYaw3,
113114
resultYaw3,
114-
"Resulting mounting yaw after yaw reset with offset is not equal to reference yaw (${deg(expectedYaw4)} vs ${deg(resultYaw4)})",
115+
message = "Resulting mounting yaw after yaw reset with offset is not equal to reference yaw (${radToDeg(expectedYaw4)} vs ${radToDeg(resultYaw4)})",
115116
)
116117
}
117118

@@ -141,23 +142,23 @@ class MountingResetTests {
141142
tracker.setRotation(trackerRot)
142143
tracker.resetsHandler.resetMounting(Quaternion.IDENTITY)
143144

144-
val expectedYaw = yaw(expected)
145-
val resultYaw = yaw(tracker.resetsHandler.mountRotFix)
146-
assertAnglesApproxEqual(
145+
val expectedYaw = radYaw(expected)
146+
val resultYaw = radYaw(tracker.resetsHandler.mountRotFix)
147+
assertAngleEquals(
147148
expectedYaw,
148149
resultYaw,
149-
"Resulting mounting yaw after full reset is not equal to reference yaw (${deg(expectedYaw)} vs ${deg(resultYaw)})",
150+
message = "Resulting mounting yaw after full reset is not equal to reference yaw (${radToDeg(expectedYaw)} vs ${radToDeg(resultYaw)})",
150151
)
151152

152153
tracker.setRotation(reference * reference)
153154
tracker.resetsHandler.resetYaw(reference)
154155

155-
val expectedYaw2 = yaw(reference)
156-
val resultYaw2 = yaw(tracker.getRotation())
157-
assertAnglesApproxEqual(
156+
val expectedYaw2 = radYaw(reference)
157+
val resultYaw2 = radYaw(tracker.getRotation())
158+
assertAngleEquals(
158159
expectedYaw2,
159160
resultYaw2,
160-
"Resulting rotation after yaw reset is not equal to reference yaw (${deg(expectedYaw2)} vs ${deg(resultYaw2)})",
161+
message = "Resulting rotation after yaw reset is not equal to reference yaw (${radToDeg(expectedYaw2)} vs ${radToDeg(resultYaw2)})",
161162
)
162163
}
163164
}

server/core/src/test/java/dev/slimevr/unit/SkeletonResetTests.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dev.slimevr.unit
33
import com.jme3.math.FastMath
44
import dev.slimevr.tracking.processor.HumanPoseManager
55
import dev.slimevr.tracking.trackers.TrackerPosition
6-
import dev.slimevr.unit.TrackerTestUtils.assertAnglesApproxEqual
6+
import dev.slimevr.unit.TrackerTestUtils.assertAngleEquals
77
import dev.slimevr.unit.TrackerTestUtils.quatApproxEqual
88
import io.github.axisangles.ktmath.EulerAngles
99
import io.github.axisangles.ktmath.EulerOrder
@@ -57,8 +57,8 @@ class SkeletonResetTests {
5757
hpm.resetTrackersYaw(resetSource)
5858

5959
for (tracker in trackers.set) {
60-
val yaw = TrackerTestUtils.yaw(tracker.getRotation())
61-
assertAnglesApproxEqual(0f, yaw, "\"${tracker.name}\" did not reset to the reference rotation.")
60+
val yaw = TrackerTestUtils.radYaw(tracker.getRotation())
61+
assertAngleEquals(0f, yaw, message = "\"${tracker.name}\" did not reset to the reference rotation.")
6262
}
6363
}
6464

@@ -104,9 +104,9 @@ class SkeletonResetTests {
104104
val actualMounting = tracker.resetsHandler.mountRotFix
105105

106106
// Make sure yaw matches
107-
val expectedY = TrackerTestUtils.yaw(expectedMounting)
108-
val actualY = TrackerTestUtils.yaw(actualMounting)
109-
assertAnglesApproxEqual(expectedY, actualY, "\"${tracker.name}\" did not reset to the reference rotation.")
107+
val expectedY = TrackerTestUtils.radYaw(expectedMounting)
108+
val actualY = TrackerTestUtils.radYaw(actualMounting)
109+
assertAngleEquals(expectedY, actualY, message = "\"${tracker.name}\" did not reset to the reference rotation.")
110110

111111
// X and Z components should be zero for mounting
112112
assert(FastMath.isApproxZero(actualMounting.x)) {

server/core/src/test/java/dev/slimevr/unit/TrackerTestUtils.kt

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,71 @@ object TrackerTestUtils {
5656
/**
5757
* Gets the yaw of a rotation in radians
5858
*/
59-
fun yaw(rot: Quaternion): Float = posRad(rot.toEulerAngles(EulerOrder.YZX).y)
59+
fun radYaw(rot: Quaternion): Float = posRad(rot.toEulerAngles(EulerOrder.YZX).y)
6060

6161
/**
6262
* Converts radians to degrees
6363
*/
64-
fun deg(rot: Float): Float = rot * FastMath.RAD_TO_DEG
64+
fun radToDeg(rot: Float): Float = rot * FastMath.RAD_TO_DEG
6565

66-
fun deg(rot: Quaternion): Float = deg(yaw(rot))
66+
fun degYaw(rot: Quaternion): Float = radToDeg(radYaw(rot))
6767

68-
private fun anglesApproxEqual(a: Float, b: Float): Boolean = FastMath.isApproxEqual(a, b) ||
69-
FastMath.isApproxEqual(a - FastMath.TWO_PI, b) ||
70-
FastMath.isApproxEqual(a, b - FastMath.TWO_PI)
68+
private fun angleApproxEqual(a: Float, b: Float, tolerance: Float = FastMath.ZERO_TOLERANCE): Boolean = FastMath.isApproxEqual(a, b, tolerance) ||
69+
FastMath.isApproxEqual(a - FastMath.TWO_PI, b, tolerance) ||
70+
FastMath.isApproxEqual(a, b - FastMath.TWO_PI, tolerance)
7171

72-
fun assertAnglesApproxEqual(expected: Float, actual: Float, message: String?) {
73-
if (!anglesApproxEqual(expected, actual)) {
72+
fun assertAngleEquals(expected: Float, actual: Float, tolerance: Float = FastMath.ZERO_TOLERANCE, message: String? = null) {
73+
if (!angleApproxEqual(expected, actual, tolerance)) {
7474
AssertionFailureBuilder.assertionFailure().message(message)
7575
.expected(expected).actual(actual).buildAndThrow()
7676
}
7777
}
7878

79+
fun assertAngleNotEquals(expected: Float, actual: Float, tolerance: Float = FastMath.ZERO_TOLERANCE, message: String? = null) {
80+
if (angleApproxEqual(expected, actual, tolerance)) {
81+
AssertionFailureBuilder.assertionFailure().message(message)
82+
.expected(expected).actual(actual).buildAndThrow()
83+
}
84+
}
85+
86+
/**
87+
* True if the quaternions are approximately equal in quaternion space, not rotation
88+
* space.
89+
*/
7990
fun quatApproxEqual(q1: Quaternion, q2: Quaternion, tolerance: Float = FastMath.ZERO_TOLERANCE): Boolean = FastMath.isApproxEqual(q1.w, q2.w, tolerance) &&
8091
FastMath.isApproxEqual(q1.x, q2.x, tolerance) &&
8192
FastMath.isApproxEqual(q1.y, q2.y, tolerance) &&
8293
FastMath.isApproxEqual(q1.z, q2.z, tolerance)
8394

95+
fun assertQuatEquals(expected: Quaternion, actual: Quaternion, tolerance: Float = FastMath.ZERO_TOLERANCE, message: String? = null) {
96+
if (!quatApproxEqual(expected, actual, tolerance)) {
97+
AssertionFailureBuilder.assertionFailure().message(message)
98+
.expected(expected).actual(actual).buildAndThrow()
99+
}
100+
}
101+
102+
fun assertQuatNotEquals(expected: Quaternion, actual: Quaternion, tolerance: Float = FastMath.ZERO_TOLERANCE, message: String? = null) {
103+
if (quatApproxEqual(expected, actual, tolerance)) {
104+
AssertionFailureBuilder.assertionFailure().message(message)
105+
.expected(expected).actual(actual).buildAndThrow()
106+
}
107+
}
108+
84109
fun vectorApproxEqual(v1: Vector3, v2: Vector3, tolerance: Float = FastMath.ZERO_TOLERANCE): Boolean = FastMath.isApproxEqual(v1.x, v2.x, tolerance) &&
85110
FastMath.isApproxEqual(v1.y, v2.y, tolerance) &&
86111
FastMath.isApproxEqual(v1.z, v2.z, tolerance)
112+
113+
fun assertVectorEquals(expected: Vector3, actual: Vector3, tolerance: Float = FastMath.ZERO_TOLERANCE, message: String? = null) {
114+
if (!vectorApproxEqual(expected, actual, tolerance)) {
115+
AssertionFailureBuilder.assertionFailure().message(message)
116+
.expected(expected).actual(actual).buildAndThrow()
117+
}
118+
}
119+
120+
fun assertVectorNotEquals(expected: Vector3, actual: Vector3, tolerance: Float = FastMath.ZERO_TOLERANCE, message: String? = null) {
121+
if (vectorApproxEqual(expected, actual, tolerance)) {
122+
AssertionFailureBuilder.assertionFailure().message(message)
123+
.expected(expected).actual(actual).buildAndThrow()
124+
}
125+
}
87126
}

0 commit comments

Comments
 (0)