From bd50a8c207a1a02d1e8538b92c552a6e1ae371ce Mon Sep 17 00:00:00 2001 From: Brian Armstrong Date: Sun, 31 Oct 2021 16:17:16 -0700 Subject: [PATCH 1/3] Driver Scale Parameter Also stash to serialized config and allow editing from editor --- OpenVR-SpaceCalibrator/Calibration.cpp | 5 +++-- OpenVR-SpaceCalibrator/Calibration.h | 2 ++ OpenVR-SpaceCalibrator/Configuration.cpp | 6 ++++++ OpenVR-SpaceCalibrator/UserInterface.cpp | 4 ++++ .../ServerTrackedDeviceProvider.cpp | 9 ++++++--- .../ServerTrackedDeviceProvider.h | 1 + Protocol.h | 18 +++++++++++++----- 7 files changed, 35 insertions(+), 10 deletions(-) diff --git a/OpenVR-SpaceCalibrator/Calibration.cpp b/OpenVR-SpaceCalibrator/Calibration.cpp index 5472b8e..cac0fbb 100644 --- a/OpenVR-SpaceCalibrator/Calibration.cpp +++ b/OpenVR-SpaceCalibrator/Calibration.cpp @@ -269,7 +269,7 @@ void ResetAndDisableOffsets(uint32_t id) zeroQ.x = 0; zeroQ.y = 0; zeroQ.z = 0; zeroQ.w = 1; protocol::Request req(protocol::RequestSetDeviceTransform); - req.setDeviceTransform = { id, false, zeroV, zeroQ }; + req.setDeviceTransform = { id, false, zeroV, zeroQ, 1.0 }; Driver.SendBlocking(req); } @@ -338,7 +338,8 @@ void ScanAndApplyProfile(CalibrationContext &ctx) id, true, VRTranslationVec(ctx.calibratedTranslation), - VRRotationQuat(ctx.calibratedRotation) + VRRotationQuat(ctx.calibratedRotation), + ctx.calibratedScale }; Driver.SendBlocking(req); } diff --git a/OpenVR-SpaceCalibrator/Calibration.h b/OpenVR-SpaceCalibrator/Calibration.h index 3a49f5c..df55484 100644 --- a/OpenVR-SpaceCalibrator/Calibration.h +++ b/OpenVR-SpaceCalibrator/Calibration.h @@ -20,6 +20,7 @@ struct CalibrationContext Eigen::Vector3d calibratedRotation; Eigen::Vector3d calibratedTranslation; + double calibratedScale; std::string referenceTrackingSystem; std::string targetTrackingSystem; @@ -57,6 +58,7 @@ struct CalibrationContext calibratedRotation = Eigen::Vector3d(); calibratedTranslation = Eigen::Vector3d(); + calibratedScale = 1.0; referenceTrackingSystem = ""; targetTrackingSystem = ""; enabled = false; diff --git a/OpenVR-SpaceCalibrator/Configuration.cpp b/OpenVR-SpaceCalibrator/Configuration.cpp index e068c27..1d27fdb 100644 --- a/OpenVR-SpaceCalibrator/Configuration.cpp +++ b/OpenVR-SpaceCalibrator/Configuration.cpp @@ -54,6 +54,11 @@ static void ParseProfile(CalibrationContext &ctx, std::istream &stream) ctx.calibratedTranslation(1) = obj["y"].get(); ctx.calibratedTranslation(2) = obj["z"].get(); + if (obj["scale"].is()) + ctx.calibratedScale = obj["scale"].get(); + else + ctx.calibratedScale = 1.0; + if (obj["calibration_speed"].is()) ctx.calibrationSpeed = (CalibrationContext::Speed)(int) obj["calibration_speed"].get(); @@ -101,6 +106,7 @@ static void WriteProfile(CalibrationContext &ctx, std::ostream &out) profile["x"].set(ctx.calibratedTranslation(0)); profile["y"].set(ctx.calibratedTranslation(1)); profile["z"].set(ctx.calibratedTranslation(2)); + profile["scale"].set(ctx.calibratedScale); double speed = (int) ctx.calibrationSpeed; profile["calibration_speed"].set(speed); diff --git a/OpenVR-SpaceCalibrator/UserInterface.cpp b/OpenVR-SpaceCalibrator/UserInterface.cpp index c19f6af..1e81944 100644 --- a/OpenVR-SpaceCalibrator/UserInterface.cpp +++ b/OpenVR-SpaceCalibrator/UserInterface.cpp @@ -489,6 +489,10 @@ void BuildProfileEditor() ImGui::InputDouble("##Y", &CalCtx.calibratedTranslation(1), 1.0, 10.0, "%.8f"); ImGui::SameLine(); ImGui::InputDouble("##Z", &CalCtx.calibratedTranslation(2), 1.0, 10.0, "%.8f"); + + TextWithWidth("ScaleLabel", "Scale", width); + + ImGui::InputDouble("##Scale", &CalCtx.calibratedScale, 0.01, 0.1, "%.8f"); ImGui::PopItemWidth(); } diff --git a/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.cpp b/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.cpp index eafa62a..22e7fde 100644 --- a/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.cpp +++ b/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.cpp @@ -49,6 +49,9 @@ void ServerTrackedDeviceProvider::SetDeviceTransform(const protocol::SetDeviceTr if (newTransform.updateRotation) tf.rotation = newTransform.rotation; + + if (newTransform.updateScale) + tf.scale = newTransform.scale; } bool ServerTrackedDeviceProvider::HandleDevicePoseUpdated(uint32_t openVRID, vr::DriverPose_t &pose) @@ -59,9 +62,9 @@ bool ServerTrackedDeviceProvider::HandleDevicePoseUpdated(uint32_t openVRID, vr: pose.qWorldFromDriverRotation = tf.rotation * pose.qWorldFromDriverRotation; vr::HmdVector3d_t rotatedTranslation = quaternionRotateVector(tf.rotation, pose.vecWorldFromDriverTranslation); - pose.vecWorldFromDriverTranslation[0] = rotatedTranslation.v[0] + tf.translation.v[0]; - pose.vecWorldFromDriverTranslation[1] = rotatedTranslation.v[1] + tf.translation.v[1]; - pose.vecWorldFromDriverTranslation[2] = rotatedTranslation.v[2] + tf.translation.v[2]; + pose.vecWorldFromDriverTranslation[0] = tf.scale * rotatedTranslation.v[0] + tf.translation.v[0]; + pose.vecWorldFromDriverTranslation[1] = tf.scale * rotatedTranslation.v[1] + tf.translation.v[1]; + pose.vecWorldFromDriverTranslation[2] = tf.scale * rotatedTranslation.v[2] + tf.translation.v[2]; } return true; } diff --git a/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.h b/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.h index 9c50a05..04e2edd 100644 --- a/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.h +++ b/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.h @@ -46,6 +46,7 @@ class ServerTrackedDeviceProvider : public vr::IServerTrackedDeviceProvider bool enabled = false; vr::HmdVector3d_t translation; vr::HmdQuaternion_t rotation; + double scale; }; DeviceTransform transforms[vr::k_unMaxTrackedDeviceCount]; diff --git a/Protocol.h b/Protocol.h index 8fc89ae..ff0b8e7 100644 --- a/Protocol.h +++ b/Protocol.h @@ -10,7 +10,7 @@ namespace protocol { - const uint32_t Version = 1; + const uint32_t Version = 2; enum RequestType { @@ -37,20 +37,28 @@ namespace protocol bool enabled; bool updateTranslation; bool updateRotation; + bool updateScale; vr::HmdVector3d_t translation; vr::HmdQuaternion_t rotation; + double scale; SetDeviceTransform(uint32_t id, bool enabled) : - openVRID(id), enabled(enabled), updateTranslation(false), updateRotation(false) { } + openVRID(id), enabled(enabled), updateTranslation(false), updateRotation(false), updateScale(false) { } SetDeviceTransform(uint32_t id, bool enabled, vr::HmdVector3d_t translation) : - openVRID(id), enabled(enabled), updateTranslation(true), updateRotation(false), translation(translation) { } + openVRID(id), enabled(enabled), updateTranslation(true), updateRotation(false), updateScale(false), translation(translation) { } SetDeviceTransform(uint32_t id, bool enabled, vr::HmdQuaternion_t rotation) : - openVRID(id), enabled(enabled), updateTranslation(false), updateRotation(true), rotation(rotation) { } + openVRID(id), enabled(enabled), updateTranslation(false), updateRotation(true), updateScale(false), rotation(rotation) { } + + SetDeviceTransform(uint32_t id, bool enabled, double scale) : + openVRID(id), enabled(enabled), updateTranslation(false), updateRotation(false), updateScale(true), scale(scale) { } SetDeviceTransform(uint32_t id, bool enabled, vr::HmdVector3d_t translation, vr::HmdQuaternion_t rotation) : - openVRID(id), enabled(enabled), updateTranslation(true), updateRotation(true), translation(translation), rotation(rotation) { } + openVRID(id), enabled(enabled), updateTranslation(true), updateRotation(true), updateScale(false), translation(translation), rotation(rotation) { } + + SetDeviceTransform(uint32_t id, bool enabled, vr::HmdVector3d_t translation, vr::HmdQuaternion_t rotation, double scale) : + openVRID(id), enabled(enabled), updateTranslation(true), updateRotation(true), updateScale(true), translation(translation), rotation(rotation), scale(scale) { } }; struct Request From e599ac136ddcee804245f437ffed4423425f81c8 Mon Sep 17 00:00:00 2001 From: Brian Armstrong Date: Tue, 2 Nov 2021 00:46:33 -0700 Subject: [PATCH 2/3] make the steps smaller The correct value is probably between 0.99 and 1.01. Let's use really small steps here. --- OpenVR-SpaceCalibrator/UserInterface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenVR-SpaceCalibrator/UserInterface.cpp b/OpenVR-SpaceCalibrator/UserInterface.cpp index 1e81944..8676673 100644 --- a/OpenVR-SpaceCalibrator/UserInterface.cpp +++ b/OpenVR-SpaceCalibrator/UserInterface.cpp @@ -492,7 +492,7 @@ void BuildProfileEditor() TextWithWidth("ScaleLabel", "Scale", width); - ImGui::InputDouble("##Scale", &CalCtx.calibratedScale, 0.01, 0.1, "%.8f"); + ImGui::InputDouble("##Scale", &CalCtx.calibratedScale, 0.0001, 0.01, "%.8f"); ImGui::PopItemWidth(); } From c6eb49d58009e4ba0d4a7c88acd048fba185042c Mon Sep 17 00:00:00 2001 From: Brian Armstrong Date: Tue, 2 Nov 2021 00:48:27 -0700 Subject: [PATCH 3/3] scale vecPosition This looks like the right thing to scale --- .../ServerTrackedDeviceProvider.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.cpp b/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.cpp index 22e7fde..55b0951 100644 --- a/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.cpp +++ b/OpenVR-SpaceCalibratorDriver/ServerTrackedDeviceProvider.cpp @@ -61,10 +61,14 @@ bool ServerTrackedDeviceProvider::HandleDevicePoseUpdated(uint32_t openVRID, vr: { pose.qWorldFromDriverRotation = tf.rotation * pose.qWorldFromDriverRotation; + pose.vecPosition[0] *= tf.scale; + pose.vecPosition[1] *= tf.scale; + pose.vecPosition[2] *= tf.scale; + vr::HmdVector3d_t rotatedTranslation = quaternionRotateVector(tf.rotation, pose.vecWorldFromDriverTranslation); - pose.vecWorldFromDriverTranslation[0] = tf.scale * rotatedTranslation.v[0] + tf.translation.v[0]; - pose.vecWorldFromDriverTranslation[1] = tf.scale * rotatedTranslation.v[1] + tf.translation.v[1]; - pose.vecWorldFromDriverTranslation[2] = tf.scale * rotatedTranslation.v[2] + tf.translation.v[2]; + pose.vecWorldFromDriverTranslation[0] = rotatedTranslation.v[0] + tf.translation.v[0]; + pose.vecWorldFromDriverTranslation[1] = rotatedTranslation.v[1] + tf.translation.v[1]; + pose.vecWorldFromDriverTranslation[2] = rotatedTranslation.v[2] + tf.translation.v[2]; } return true; }