From bb6c2bb6fbfa7a5462a4b2269b2dd72377c21fe4 Mon Sep 17 00:00:00 2001 From: Mamadou Babaei Date: Tue, 21 May 2024 18:40:31 +0200 Subject: [PATCH] unfinished improvements based on the last commit --- .../SGVirtualHandTrackingSettings.cpp | 19 +- .../SGVirtualHandTrackingSettings.h | 8 +- .../Private/SGTracking/SGXRTracker.cpp | 206 ++++++++++++++---- 3 files changed, 182 insertions(+), 51 deletions(-) diff --git a/Source/SenseGloveSettings/Private/SGSettings/SGVirtualHandTrackingSettings.cpp b/Source/SenseGloveSettings/Private/SGSettings/SGVirtualHandTrackingSettings.cpp index 3f3b5bf7..d34ca346 100644 --- a/Source/SenseGloveSettings/Private/SGSettings/SGVirtualHandTrackingSettings.cpp +++ b/Source/SenseGloveSettings/Private/SGSettings/SGVirtualHandTrackingSettings.cpp @@ -41,12 +41,13 @@ FSGVirtualHandTrackingSettings::FSGVirtualHandTrackingSettings() : FSGVirtualHandTrackingSettings( nullptr, nullptr, - 1.0f, - 1.0f, - 1.0f, - 1.0f, - 1.0f, - FRotator{0.0f, -90.0f, 0.0f}) + 1.4f, + 1.4f, + 1.4f, + 1.4f, + 1.4f, + FRotator{0.0f, -90.0f, 0.0f}, + FRotator{0.0f, 180.0f, 90.0f}) { } @@ -58,7 +59,8 @@ FSGVirtualHandTrackingSettings::FSGVirtualHandTrackingSettings( const float InMiddleDistalPhalanxLength, const float InRingDistalPhalanxLength, const float InLittleDistalPhalanxLength, - const FRotator& InHandRootBoneRotationCorrection) + const FRotator& InHandMeshToOpenXRRootBoneRotationCorrection, + const FRotator& InHandMeshToSenseGloveRootBoneRotationCorrection) : LeftHandReferenceMesh(InLeftHandReferenceMesh), RightHandReferenceMesh(InRightHandReferenceMesh), ThumbDistalPhalanxLength(InThumbDistalPhalanxLength), @@ -66,7 +68,8 @@ FSGVirtualHandTrackingSettings::FSGVirtualHandTrackingSettings( MiddleDistalPhalanxLength(InMiddleDistalPhalanxLength), RingDistalPhalanxLength(InRingDistalPhalanxLength), LittleDistalPhalanxLength(InLittleDistalPhalanxLength), - HandRootBoneRotationCorrection(InHandRootBoneRotationCorrection), + HandMeshToOpenXRRootBoneRotationCorrection(InHandMeshToOpenXRRootBoneRotationCorrection), + HandMeshToSenseGloveRootBoneRotationCorrection(InHandMeshToSenseGloveRootBoneRotationCorrection), LeftHandDefaultReferenceBoneTransforms{ TPair{ FName{TEXT("hand_l")}, diff --git a/Source/SenseGloveSettings/Public/SGSettings/SGVirtualHandTrackingSettings.h b/Source/SenseGloveSettings/Public/SGSettings/SGVirtualHandTrackingSettings.h index 6c66dd3a..46c40d36 100644 --- a/Source/SenseGloveSettings/Public/SGSettings/SGVirtualHandTrackingSettings.h +++ b/Source/SenseGloveSettings/Public/SGSettings/SGVirtualHandTrackingSettings.h @@ -73,7 +73,10 @@ public: float LittleDistalPhalanxLength; UPROPERTY(Config, EditDefaultsOnly, Category="Virtual Hand") - FRotator HandRootBoneRotationCorrection; + FRotator HandMeshToOpenXRRootBoneRotationCorrection; + + UPROPERTY(Config, EditDefaultsOnly, Category="Virtual Hand") + FRotator HandMeshToSenseGloveRootBoneRotationCorrection; UPROPERTY(Config, VisibleDefaultsOnly, Category="Virtual Hand") TMap LeftHandDefaultReferenceBoneTransforms; @@ -98,6 +101,7 @@ public: float InMiddleDistalPhalanxLength, float InRingDistalPhalanxLength, float InLittleDistalPhalanxLength, - const FRotator& InHandRootBoneRotationCorrection); + const FRotator& InHandMeshToOpenXRRootBoneRotationCorrection, + const FRotator& InHandMeshToSenseGloveRootBoneRotationCorrection); }; \ No newline at end of file diff --git a/Source/SenseGloveTracking/Private/SGTracking/SGXRTracker.cpp b/Source/SenseGloveTracking/Private/SGTracking/SGXRTracker.cpp index 0bdd54c9..b60af50d 100644 --- a/Source/SenseGloveTracking/Private/SGTracking/SGXRTracker.cpp +++ b/Source/SenseGloveTracking/Private/SGTracking/SGXRTracker.cpp @@ -320,40 +320,86 @@ struct FSGXRTracker::FImpl return Transform; } - static FTransform GetMeshBoneRefTransform(bool bRight, const FName& BoneName); + static FTransform GetOpenXRMeshMetacarpalBoneRefTransform(bool bRight, const FName& BoneName); - FORCEINLINE static FTransform GetMeshBoneRefTransform(const bool bRight, const int32 JointIndex) + FORCEINLINE static FTransform GetOpenXRMeshMetacarpalBoneRefTransform(const bool bRight, const int32 JointIndex) { const FName& BoneName{GetMeshBoneName(bRight, JointIndex)}; - const FTransform Transform{GetMeshBoneRefTransform(bRight, BoneName)}; + const FTransform Transform{GetOpenXRMeshMetacarpalBoneRefTransform(bRight, BoneName)}; return Transform; } - FORCEINLINE static FTransform GetMeshBoneRefTransform(const bool bRight, const EHandKeypoint Keypoint) + FORCEINLINE static FTransform GetOpenXRMeshMetacarpalBoneRefTransform( + const bool bRight, const EHandKeypoint Keypoint) { const FName& BoneName{GetMeshBoneName(bRight, Keypoint)}; - const FTransform Transform{GetMeshBoneRefTransform(bRight, BoneName)}; + const FTransform Transform{GetOpenXRMeshMetacarpalBoneRefTransform(bRight, BoneName)}; return Transform; } - FORCEINLINE static FTransform GetMeshBoneRefTransform(const EControllerHand Hand, const FName& BoneName) + FORCEINLINE static FTransform GetOpenXRMeshMetacarpalBoneRefTransform( + const EControllerHand Hand, const FName& BoneName) { const bool bRight = Hand == EControllerHand::Right; - const FTransform Transform{GetMeshBoneRefTransform(bRight, BoneName)}; + const FTransform Transform{GetOpenXRMeshMetacarpalBoneRefTransform(bRight, BoneName)}; return Transform; } - FORCEINLINE static FTransform GetMeshBoneRefTransform(const EControllerHand Hand, const int32 JointIndex) + FORCEINLINE static FTransform GetOpenXRMeshMetacarpalBoneRefTransform( + const EControllerHand Hand, const int32 JointIndex) { const bool bRight = Hand == EControllerHand::Right; - const FTransform Transform{GetMeshBoneRefTransform(bRight, JointIndex)}; + const FTransform Transform{GetOpenXRMeshMetacarpalBoneRefTransform(bRight, JointIndex)}; return Transform; } - FORCEINLINE static FTransform GetMeshBoneRefTransform(const EControllerHand Hand, const EHandKeypoint Keypoint) + FORCEINLINE static FTransform GetOpenXRMeshMetacarpalBoneRefTransform( + const EControllerHand Hand, const EHandKeypoint Keypoint) { const bool bRight = Hand == EControllerHand::Right; - const FTransform Transform{GetMeshBoneRefTransform(bRight, Keypoint)}; + const FTransform Transform{GetOpenXRMeshMetacarpalBoneRefTransform(bRight, Keypoint)}; + return Transform; + } + + static FTransform GetSGMeshMetacarpalBoneRefTransform(bool bRight, const FName& BoneName); + + FORCEINLINE static FTransform GetSGMeshMetacarpalBoneRefTransform( + const bool bRight, const int32 JointIndex) + { + const FName& BoneName{GetMeshBoneName(bRight, JointIndex)}; + const FTransform Transform{GetSGMeshMetacarpalBoneRefTransform(bRight, BoneName)}; + return Transform; + } + + FORCEINLINE static FTransform GetSGMeshMetacarpalBoneRefTransform( + const bool bRight, const EHandKeypoint Keypoint) + { + const FName& BoneName{GetMeshBoneName(bRight, Keypoint)}; + const FTransform Transform{GetSGMeshMetacarpalBoneRefTransform(bRight, BoneName)}; + return Transform; + } + + FORCEINLINE static FTransform GetSGMeshMetacarpalBoneRefTransform( + const EControllerHand Hand, const FName& BoneName) + { + const bool bRight = Hand == EControllerHand::Right; + const FTransform Transform{GetSGMeshMetacarpalBoneRefTransform(bRight, BoneName)}; + return Transform; + } + + FORCEINLINE static FTransform GetSGMeshMetacarpalBoneRefTransform( + const EControllerHand Hand, const int32 JointIndex) + { + const bool bRight = Hand == EControllerHand::Right; + const FTransform Transform{GetSGMeshMetacarpalBoneRefTransform(bRight, JointIndex)}; + return Transform; + } + + FORCEINLINE static FTransform GetSGMeshMetacarpalBoneRefTransform( + const EControllerHand Hand, const EHandKeypoint Keypoint) + { + const bool bRight = Hand == EControllerHand::Right; + const FTransform Transform{GetSGMeshMetacarpalBoneRefTransform(bRight, Keypoint)}; return Transform; } @@ -591,11 +637,11 @@ TArray FSGXRTracker::FImpl::GetFingerLengths( const EHandKeypoint Joint1st, const EHandKeypoint Joint2nd, const EHandKeypoint Joint3rd, const float DistalPhalanxLength) { - const FTransform& Joint1stTransform{GetMeshBoneRefTransform(bRight, Joint1st)}; + const FTransform& Joint1stTransform{GetOpenXRMeshMetacarpalBoneRefTransform(bRight, Joint1st)}; const FVector& Joint1stLocation{Joint1stTransform.GetLocation()}; - const FTransform& Joint2ndTransform{GetMeshBoneRefTransform(bRight, Joint2nd)}; + const FTransform& Joint2ndTransform{GetOpenXRMeshMetacarpalBoneRefTransform(bRight, Joint2nd)}; const FVector& Joint2ndLocation{Joint2ndTransform.GetLocation()}; - const FTransform& Joint3rdTransform{GetMeshBoneRefTransform(bRight, Joint3rd)}; + const FTransform& Joint3rdTransform{GetOpenXRMeshMetacarpalBoneRefTransform(bRight, Joint3rd)}; const FVector& Joint3rdLocation{Joint3rdTransform.GetLocation()}; const float PhalanxLength1st = static_cast(FVector::Dist(Joint1stLocation, Joint2ndLocation)); @@ -647,18 +693,55 @@ TArray> FSGXRTracker::FImpl::GetFingerLengths(const bool bRight) TArray FSGXRTracker::FImpl::GetFingerStartPositions(const bool bRight) { - const FTransform& ThumbStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::ThumbMetacarpal)}; - const FTransform& IndexStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::IndexProximal)}; - const FTransform& MiddleStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::MiddleProximal)}; - const FTransform& RingStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::RingProximal)}; - const FTransform& LittleStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::LittleProximal)}; + const FVector WristPosition{GetRawMeshBoneRefTransform(bRight, EHandKeypoint::Wrist).GetLocation()}; + + const FVector ThumbStartPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::ThumbMetacarpal).GetLocation() + + WristPosition + }; + + const FVector IndexMetacarpalPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::IndexMetacarpal).GetLocation() + + WristPosition + }; + const FVector IndexStartPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::IndexProximal).GetLocation() + + IndexMetacarpalPosition + }; + + const FVector MiddleMetacarpalPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::MiddleMetacarpal).GetLocation() + + WristPosition + }; + const FVector MiddleStartPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::MiddleProximal).GetLocation() + + MiddleMetacarpalPosition + }; + + const FVector RingMetacarpalPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::RingMetacarpal).GetLocation() + + WristPosition + }; + const FVector RingStartPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::RingProximal).GetLocation() + + RingMetacarpalPosition + }; + + const FVector LittleMetacarpalPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::LittleMetacarpal).GetLocation() + + WristPosition + }; + const FVector LittleStartPosition{ + GetOpenXRMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::LittleProximal).GetLocation() + + LittleMetacarpalPosition + }; const TArray StartPositions{ - ThumbStartPosition.GetLocation(), - IndexStartPosition.GetLocation(), - MiddleStartPosition.GetLocation(), - RingStartPosition.GetLocation(), - LittleStartPosition.GetLocation(), + ThumbStartPosition, + IndexStartPosition, + MiddleStartPosition, + RingStartPosition, + LittleStartPosition, }; return StartPositions; @@ -666,18 +749,44 @@ TArray FSGXRTracker::FImpl::GetFingerStartPositions(const bool bRight) TArray FSGXRTracker::FImpl::GetFingerStartRotations(const bool bRight) { - const FTransform& ThumbStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::ThumbMetacarpal)}; - const FTransform& IndexStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::IndexProximal)}; - const FTransform& MiddleStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::MiddleProximal)}; - const FTransform& RingStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::RingProximal)}; - const FTransform& LittleStartPosition{GetMeshBoneRefTransform(bRight, EHandKeypoint::LittleProximal)}; + const FQuat ThumbStartRotation{ + GetSGMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::ThumbMetacarpal).GetRotation() + }; + + const FQuat IndexMetacarpalRotation{ + GetSGMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::IndexMetacarpal).GetRotation() + }; + const FQuat IndexStartRotation{ + GetRawMeshBoneRefTransform(bRight, EHandKeypoint::IndexProximal).GetRotation() * IndexMetacarpalRotation + }; + + const FQuat MiddleMetacarpalRotation{ + GetSGMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::MiddleMetacarpal).GetRotation() + }; + const FQuat MiddleStartRotation{ + GetRawMeshBoneRefTransform(bRight, EHandKeypoint::MiddleProximal).GetRotation() * MiddleMetacarpalRotation + }; + + const FQuat RingMetacarpalRotation{ + GetSGMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::RingMetacarpal).GetRotation() + }; + const FQuat RingStartRotation{ + GetRawMeshBoneRefTransform(bRight, EHandKeypoint::RingProximal).GetRotation() * RingMetacarpalRotation + }; + + const FQuat LittleMetacarpalRotation{ + GetSGMeshMetacarpalBoneRefTransform(bRight, EHandKeypoint::LittleMetacarpal).GetRotation() + }; + const FQuat LittleStartRotation{ + GetRawMeshBoneRefTransform(bRight, EHandKeypoint::LittleProximal).GetRotation() * LittleMetacarpalRotation + }; const TArray StartRotations{ - ThumbStartPosition.GetRotation(), - IndexStartPosition.GetRotation(), - MiddleStartPosition.GetRotation(), - RingStartPosition.GetRotation(), - LittleStartPosition.GetRotation(), + ThumbStartRotation, + IndexStartRotation, + MiddleStartRotation, + RingStartRotation, + LittleStartRotation, }; return StartRotations; @@ -761,9 +870,11 @@ const FTransform& FSGXRTracker::FImpl::GetRawMeshBoneRefTransform(const bool bRi return Transform; } -FTransform FSGXRTracker::FImpl::GetMeshBoneRefTransform(const bool bRight, const FName& BoneName) +FTransform FSGXRTracker::FImpl::GetOpenXRMeshMetacarpalBoneRefTransform(const bool bRight, const FName& BoneName) { - const FQuat RootRotationCorrection{GetVirtualHandTrackingSettings().HandRootBoneRotationCorrection.Quaternion()}; + const FQuat RootRotationCorrection{ + GetVirtualHandTrackingSettings().HandMeshToOpenXRRootBoneRotationCorrection.Quaternion() + }; FTransform RootBoneRefTransform{GetRawMeshBoneRefTransform(bRight, EHandKeypoint::Wrist)}; RootBoneRefTransform.SetRotation(RootRotationCorrection * RootBoneRefTransform.GetRotation()); @@ -772,6 +883,19 @@ FTransform FSGXRTracker::FImpl::GetMeshBoneRefTransform(const bool bRight, const return Transform; } +FTransform FSGXRTracker::FImpl::GetSGMeshMetacarpalBoneRefTransform(const bool bRight, const FName& BoneName) +{ + const FQuat RootRotationCorrection{ + GetVirtualHandTrackingSettings().HandMeshToSenseGloveRootBoneRotationCorrection.Quaternion() + }; + + FTransform RootBoneRefTransform{GetRawMeshBoneRefTransform(bRight, EHandKeypoint::Wrist)}; + RootBoneRefTransform.SetRotation(RootRotationCorrection * RootBoneRefTransform.GetRotation()); + const FTransform Transform{RootBoneRefTransform * GetRawMeshBoneRefTransform(bRight, BoneName)}; + + return Transform; +} + FName FSGXRTracker::ParseEOpenXRHandKeypointEnumName(const FName& EnumName) { static const int32 EnumNameLength = FString{TEXT("EHandKeypoint::")}.Len(); @@ -1678,7 +1802,7 @@ bool FSGXRTracker::FImpl::UpdateSGPalmJointData( return false; } - const FTransform JointTransform{GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::Palm)}; + const FTransform JointTransform{GetOpenXRMeshMetacarpalBoneRefTransform(DeviceHand, EHandKeypoint::Palm)}; FVector JointLocation{JointTransform.GetLocation()}; FQuat JointRotation{JointTransform.GetRotation()}; @@ -1769,7 +1893,7 @@ bool FSGXRTracker::FImpl::UpdateSGIndexJointData( static constexpr int32 IndexTipJointIndex = 3; const FTransform& IndexMetacarpalTransform{ - GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::IndexMetacarpal) + GetOpenXRMeshMetacarpalBoneRefTransform(DeviceHand, EHandKeypoint::IndexMetacarpal) }; FVector IndexMetacarpalLocation{IndexMetacarpalTransform.GetLocation()}; FQuat IndexMetacarpalRotation{IndexMetacarpalTransform.GetRotation()}; @@ -1848,7 +1972,7 @@ bool FSGXRTracker::FImpl::UpdateSGMiddleJointData( static constexpr int32 MiddleTipJointMiddle = 3; const FTransform& MiddleMetacarpalTransform{ - GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::MiddleMetacarpal) + GetOpenXRMeshMetacarpalBoneRefTransform(DeviceHand, EHandKeypoint::MiddleMetacarpal) }; FVector MiddleMetacarpalLocation{MiddleMetacarpalTransform.GetLocation()}; FQuat MiddleMetacarpalRotation{MiddleMetacarpalTransform.GetRotation()}; @@ -1927,7 +2051,7 @@ bool FSGXRTracker::FImpl::UpdateSGRingJointData( static constexpr int32 RingTipJointRing = 3; const FTransform& RingMetacarpalTransform{ - GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::RingMetacarpal) + GetOpenXRMeshMetacarpalBoneRefTransform(DeviceHand, EHandKeypoint::RingMetacarpal) }; FVector RingMetacarpalLocation{RingMetacarpalTransform.GetLocation()}; FQuat RingMetacarpalRotation{RingMetacarpalTransform.GetRotation()}; @@ -2006,7 +2130,7 @@ bool FSGXRTracker::FImpl::UpdateSGLittleJointData( static constexpr int32 LittleTipJointLittle = 3; const FTransform& LittleMetacarpalTransform{ - GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::LittleMetacarpal) + GetOpenXRMeshMetacarpalBoneRefTransform(DeviceHand, EHandKeypoint::LittleMetacarpal) }; FVector LittleMetacarpalLocation{LittleMetacarpalTransform.GetLocation()}; FQuat LittleMetacarpalRotation{LittleMetacarpalTransform.GetRotation()};