Use FastMath Pi and angle conversion

This commit is contained in:
Erimel committed 2026-10-05 19:24:33 -04:00
1 parent 46acc4768d
commit c5f9b82395
8 files changed
+28 -31

No files matched your search

@@ -2,6 +2,7 @@
package dev.slimevr.hid
import com.jme3.math.FastMath
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.MagnetometerStatus
@@ -201,7 +202,7 @@ private fun decodeExpMapQuat(data: ByteArray, offset: Int): Quaternion {
val vz = ((buf shr 21 and 2047u).toFloat() / 2048f) * 2f - 1f
val d = vx * vx + vy * vy + vz * vz
val invSqrtD = 1f / sqrt(d + 1e-6f)
val a = (PI.toFloat() / 2f) * d * invSqrtD
val a = (FastMath.PI / 2f) * d * invSqrtD
val s = sin(a)
val k = s * invSqrtD
return AXES_OFFSET * Quaternion(cos(a), k * vx, k * vy, k * vz)
@@ -1,5 +1,6 @@
package dev.slimevr.hid
import com.jme3.math.FastMath
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import kotlin.math.PI
@@ -24,7 +25,7 @@ const val HID_REPORT_SIZE = 64
const val HID_PROTOCOL_LEGACY = 2
const val HID_PROTOCOL_V3 = 3
val AXES_OFFSET: Quaternion = Quaternion.fromRotationVector(-PI.toFloat() / 2f, 0f, 0f)
val AXES_OFFSET: Quaternion = Quaternion.fromRotationVector(-FastMath.PI / 2f, 0f, 0f)
fun readU8(data: ByteArray, offset: Int): Int = data[offset].toUByte().toInt()
@@ -58,12 +58,11 @@ fun overrideRoutes(config: BoneRoutingConfig): Routes = config.manualRoutes.orEm
fun isActive(states: OutputStates, output: RoutingOutput): Boolean = states[output] == RoutingOutputState.ACTIVE
/** Whether any SolarXR bridge currently has a driver on the other end */
fun solarXRDriverConnectedFlow(server: VRServer): Flow<Boolean> =
allContextStates(server.context.state.map { it.solarxr }.distinctUntilChanged(), { it.values }) { bridge ->
bridge.context.state.map { it.driverName != null }.distinctUntilChanged()
}
.map { connected -> connected.any { it } }
.distinctUntilChanged()
fun solarXRDriverConnectedFlow(server: VRServer): Flow<Boolean> = allContextStates(server.context.state.map { it.solarxr }.distinctUntilChanged(), { it.values }) { bridge ->
bridge.context.state.map { it.driverName != null }.distinctUntilChanged()
}
.map { connected -> connected.any { it } }
.distinctUntilChanged()
fun driverStateFlow(appContext: AppContextProvider): Flow<RoutingOutputState> = combine(
appContext.config.settings.context.state.map { it.data.driverConfig.enabled },
@@ -45,10 +45,9 @@ internal val PREREQUISITE_BODY_PARTS: List<BodyPart> =
*/
internal data class TrackingPoint(val bodyPart: BodyPart?, val hasPosition: Boolean)
private fun trackingPointsFlow(server: VRServer): Flow<List<TrackingPoint>> =
allContextStates(server.context.state, { it.trackers.values }) { tracker ->
tracker.context.state.map { TrackingPoint(it.bodyPart, it.position != null) }.distinctUntilChanged()
}
private fun trackingPointsFlow(server: VRServer): Flow<List<TrackingPoint>> = allContextStates(server.context.state, { it.trackers.values }) { tracker ->
tracker.context.state.map { TrackingPoint(it.bodyPart, it.position != null) }.distinctUntilChanged()
}
/**
* List the bones that are solved by IK
@@ -74,8 +73,7 @@ internal fun ikSolvedParts(trackers: List<TrackingPoint>, useTrackerPositions: B
*
* Read off the hierarchy and the processors that fill in missing bones, so it follows them.
*/
fun bodyPartPrerequisites(bodyPart: BodyPart, ikSolved: Set<BodyPart> = emptySet()): List<List<BodyPart>> =
requirementGroups(bodyPart).filterNot { group -> group.all { it in ikSolved } }
fun bodyPartPrerequisites(bodyPart: BodyPart, ikSolved: Set<BodyPart> = emptySet()): List<List<BodyPart>> = requirementGroups(bodyPart).filterNot { group -> group.all { it in ikSolved } }
private fun requirementGroups(bodyPart: BodyPart): List<List<BodyPart>> {
val standsAlone = bodyPart in SPINE_IMPUTED_PARTS ||
@@ -1,16 +1,15 @@
package dev.slimevr.vrcosc
import com.jme3.math.FastMath
import dev.slimevr.osc.OscArg
import dev.slimevr.osc.OscBundle
import dev.slimevr.osc.OscContent
import dev.slimevr.osc.OscMessage
import dev.slimevr.skeleton.BoneState
import dev.slimevr.skeleton.ComputedSkeleton
import io.github.axisangles.ktmath.EulerOrder
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.math.PI
private val trackerIdsByBodyPart = mapOf(
BodyPart.HIP to 1,
@@ -67,7 +66,7 @@ internal fun buildYawAlignMessage(headRotation: Quaternion): OscMessage {
"/tracking/trackers/head/rotation",
listOf(
OscArg.Float(0f),
OscArg.Float(-yaw * 180f / PI.toFloat()),
OscArg.Float(-yaw * FastMath.RAD_TO_DEG),
OscArg.Float(0f),
),
)
@@ -88,9 +87,9 @@ private fun rotationMessage(address: String, rotation: Quaternion): OscMessage {
return OscMessage(
address,
listOf(
OscArg.Float(x * 180f / PI.toFloat()),
OscArg.Float(y * 180f / PI.toFloat()),
OscArg.Float(z * 180f / PI.toFloat()),
OscArg.Float(x * FastMath.RAD_TO_DEG),
OscArg.Float(y * FastMath.RAD_TO_DEG),
OscArg.Float(z * FastMath.RAD_TO_DEG),
),
)
}
@@ -1,5 +1,6 @@
package dev.slimevr.vrcosc
import com.jme3.math.FastMath
import dev.slimevr.osc.OscArg
import dev.slimevr.osc.OscContent
import dev.slimevr.osc.OscMessage
@@ -7,10 +8,7 @@ import dev.slimevr.skeleton.BoneState
import dev.slimevr.util.Side
import dev.slimevr.util.opposite
import io.github.axisangles.ktmath.EulerOrder
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.math.*
private const val ABSOLUTE_SPLAY_THRESHOLD_ANGLE = 7
private const val MINIMUM_TIP_TOE_PITCH = -14
@@ -20,7 +18,7 @@ private const val MAXIMUM_ABSOLUTE_TOE_RANGE = 90
internal fun buildToeMessages(bones: Map<BodyPart, BoneState>): List<OscContent> {
val messages = mutableListOf<OscContent>()
// LEFT FOOT + TOES
// Left foot and toes
val leftFoot = bones[BodyPart.LEFT_FOOT]
if (leftFoot != null) {
val leftToes = listOf(
@@ -33,7 +31,7 @@ internal fun buildToeMessages(bones: Map<BodyPart, BoneState>): List<OscContent>
processToesForFoot(leftFoot, leftToes, Side.LEFT, messages)
}
// RIGHT FOOT + TOES
// Right foot and toes
val rightFoot = bones[BodyPart.RIGHT_FOOT]
if (rightFoot != null) {
val rightToes = listOf(
@@ -91,8 +89,8 @@ private fun processToe(
val euler = currentRelative.toEulerAngles(EulerOrder.XYZ)
val pitch = Math.toDegrees(euler.x.toDouble()).toFloat()
val yaw = Math.toDegrees(euler.z.toDouble()).toFloat()
val pitch = euler.x * FastMath.RAD_TO_DEG
val yaw = euler.z * FastMath.RAD_TO_DEG
val tipToe = pitch < MINIMUM_TIP_TOE_PITCH
val bending = pitch > MINIMUM_BENDING_PITCH
val splayed = when (splayDirection) {
@@ -1,5 +1,6 @@
package dev.slimevr.vrcosc
import com.jme3.math.FastMath
import dev.slimevr.osc.OscArg
import io.github.axisangles.ktmath.EulerAngles
import io.github.axisangles.ktmath.EulerOrder
@@ -22,9 +23,9 @@ internal fun parseVrcEulerRotation(args: List<OscArg>, startIndex: Int = 0): Qua
val z = args.getOrNull(startIndex + 2)?.asFloatOrNull() ?: return null
val (w, rx, ry, rz) = EulerAngles(
EulerOrder.YXZ,
x * PI.toFloat() / 180f,
y * PI.toFloat() / 180f,
z * PI.toFloat() / 180f,
x * FastMath.DEG_TO_RAD,
y * FastMath.DEG_TO_RAD,
z * FastMath.DEG_TO_RAD,
).toQuaternion()
return Quaternion(w, -rx, -ry, rz)
}