refactor the way we calculate the mesh bone reference transforms to read from the settings system instead of the hardcoded values

This commit is contained in:
Mamadou Babaei
2024-05-21 13:10:05 +02:00
parent 8458e12d0b
commit 76720fc777
@@ -254,177 +254,80 @@ struct FSGXRTracker::FImpl
return Name;
}
static const FTransform& GetMeshBoneRefTransform(bool bRight, const FName& BoneName);
static const FTransform& GetRawMeshBoneRefTransform(bool bRight, const FName& BoneName);
FORCEINLINE static const FTransform& GetMeshBoneRefTransform(const bool bRight, const int32 JointIndex)
FORCEINLINE static const FTransform& GetRawMeshBoneRefTransform(const bool bRight, const int32 JointIndex)
{
const FName& Name{GetMeshBoneName(bRight, JointIndex)};
const FTransform& Transform{GetMeshBoneRefTransform(bRight, Name)};
const FName& BoneName{GetMeshBoneName(bRight, JointIndex)};
const FTransform& Transform{GetRawMeshBoneRefTransform(bRight, BoneName)};
return Transform;
}
FORCEINLINE static const FTransform& GetMeshBoneRefTransform(const bool bRight, const EHandKeypoint Keypoint)
FORCEINLINE static const FTransform& GetRawMeshBoneRefTransform(const bool bRight, const EHandKeypoint Keypoint)
{
const FName& Name{GetMeshBoneName(bRight, Keypoint)};
const FTransform& Transform{GetMeshBoneRefTransform(bRight, Name)};
const FName& BoneName{GetMeshBoneName(bRight, Keypoint)};
const FTransform& Transform{GetRawMeshBoneRefTransform(bRight, BoneName)};
return Transform;
}
FORCEINLINE static const FTransform& GetMeshBoneRefTransform(const EControllerHand Hand, const FName& BoneName)
FORCEINLINE static const FTransform& GetRawMeshBoneRefTransform(const EControllerHand Hand, const FName& BoneName)
{
const bool bRight = Hand == EControllerHand::Right;
const FTransform& Transform{GetMeshBoneRefTransform(bRight, BoneName)};
const FTransform& Transform{GetRawMeshBoneRefTransform(bRight, BoneName)};
return Transform;
}
FORCEINLINE static const FTransform& GetMeshBoneRefTransform(const EControllerHand Hand, const int32 JointIndex)
FORCEINLINE static const FTransform& GetRawMeshBoneRefTransform(const EControllerHand Hand, const int32 JointIndex)
{
const bool bRight = Hand == EControllerHand::Right;
const FTransform& Transform{GetMeshBoneRefTransform(bRight, JointIndex)};
const FName& BoneName{GetMeshBoneName(bRight, JointIndex)};
const FTransform& Transform{GetRawMeshBoneRefTransform(bRight, BoneName)};
return Transform;
}
FORCEINLINE static const FTransform& GetMeshBoneRefTransform(
FORCEINLINE static const FTransform& GetRawMeshBoneRefTransform(
const EControllerHand Hand, const EHandKeypoint Keypoint)
{
const bool bRight = Hand == EControllerHand::Right;
const FTransform& Transform{GetMeshBoneRefTransform(bRight, Keypoint)};
const FName& BoneName{GetMeshBoneName(bRight, Keypoint)};
const FTransform& Transform{GetRawMeshBoneRefTransform(bRight, BoneName)};
return Transform;
}
FORCEINLINE static const FVector& GetMeshDefaultRootBoneLocation(const bool bRight)
{
static const FVector LocationLeft{0.0f, 0.0f, 0.0f};
static const FVector LocationRight{0.0f, 0.0f, 0.0f};
return bRight ? LocationRight : LocationLeft;
}
static FTransform GetMeshBoneRefTransform(bool bRight, const FName& BoneName);
FORCEINLINE static const FQuat& GetMeshDefaultRootBoneRotation(const bool bRight)
FORCEINLINE static FTransform GetMeshBoneRefTransform(const bool bRight, const int32 JointIndex)
{
static const FQuat RotationLeft{FRotator{0.0f, -180.0f, 90.0f}};
static const FQuat RotationRight{FRotator{0.0f, -180.0f, 90.0f}};
return bRight ? RotationRight : RotationLeft;
}
FORCEINLINE static const FVector& GetMeshDefaultPalmBoneLocation(const bool bRight)
{
static const FVector LocationLeft{0.0f, 0.0f, -6.615570f};
static const FVector LocationRight{0.000052f, -0.000629f, -6.615405f};
return bRight ? LocationRight : LocationLeft;
}
FORCEINLINE static const FQuat& GetMeshDefaultPalmBoneRotation(const bool bRight)
{
static const FQuat RotationLeft{FRotator{0.0f, 0.0f, 0.0f}};
static const FQuat RotationRight{FRotator{0.0f, 0.0f, 0.0f}};
return bRight ? RotationRight : RotationLeft;
}
FORCEINLINE static const FVector& GetMeshDefaultIndexMetacarpalBoneLocation(const bool bRight)
{
static const FVector LocationLeft{2.379324f, -0.384681f, -3.444506f};
static const FVector LocationRight{-2.379309f, -0.385167f, -3.444514f};
return bRight ? LocationRight : LocationLeft;
}
FORCEINLINE static const FQuat& GetMeshDefaultIndexMetacarpalBoneRotation(const bool bRight)
{
static const FQuat RotationLeft{FRotator{7.277920f, 3.392215f, 1.032481f}};
static const FQuat RotationRight{FRotator{-7.277920f, -3.392215f, 1.032481f}};
return bRight ? RotationRight : RotationLeft;
}
FORCEINLINE static const FVector& GetMeshDefaultMiddleMetacarpalBoneLocation(const bool bRight)
{
static const FVector LocationLeft{0.182864f, -0.753571f, -3.375834f};
static const FVector LocationRight{-0.182806f, -0.754004f, -3.37580f};
return bRight ? LocationRight : LocationLeft;
}
FORCEINLINE static const FQuat& GetMeshDefaultMiddleMetacarpalBoneRotation(const bool bRight)
{
static const FQuat RotationLeft{FRotator{0.042392f, -4.274287f, 2.321687f}};
static const FQuat RotationRight{FRotator{-0.042392f, 4.274287f, 2.321687f}};
return bRight ? RotationRight : RotationLeft;
}
FORCEINLINE static const FVector& GetMeshDefaultRingMetacarpalBoneLocation(const bool bRight)
{
static const FVector LocationLeft{-1.091757f, -0.542517f, -3.374304f};
static const FVector LocationRight{1.091782f, -0.542992f, -3.374208f};
return bRight ? LocationRight : LocationLeft;
}
FORCEINLINE static const FQuat& GetMeshDefaultRingMetacarpalBoneRotation(const bool bRight)
{
static const FQuat RotationLeft{FRotator{-11.109678f, -13.886395f, 4.333313f}};
static const FQuat RotationRight{FRotator{11.109678f, 13.886395f, 4.333313f}};
return bRight ? RotationRight : RotationLeft;
}
FORCEINLINE static const FVector& GetMeshDefaultLittleMetacarpalBoneLocation(const bool bRight)
{
static const FVector LocationLeft{-2.391111f, -0.305917f, -3.314378f};
static const FVector LocationRight{2.391287f, -0.305935f, -3.314677f};
return bRight ? LocationRight : LocationLeft;
}
FORCEINLINE static const FQuat& GetMeshDefaultLittleMetacarpalBoneRotation(const bool bRight)
{
static const FQuat RotationLeft{FRotator{-22.652560f, -25.358337f, -1.818999f}};
static const FQuat RotationRight{FRotator{22.652560f, 25.358337f, -1.818999f}};
return bRight ? RotationRight : RotationLeft;
}
static const FVector& GetMeshDefaultBoneLocation(bool bRight, const FName& BoneName);
static const FQuat& GetMeshDefaultBoneRotation(bool bRight, const FName& BoneName);
FORCEINLINE static FTransform GetMeshBoneDefaultRefTransform(const bool bRight, const FName& BoneName)
{
const FTransform Transform{
GetMeshDefaultBoneRotation(bRight, BoneName), GetMeshDefaultBoneLocation(bRight, BoneName)
};
const FName& BoneName{GetMeshBoneName(bRight, JointIndex)};
const FTransform Transform{GetMeshBoneRefTransform(bRight, BoneName)};
return Transform;
}
static FTransform GetRootAppliedMeshBoneRefTransformOrDefault(bool bRight, const FName& BoneName);
FORCEINLINE static FTransform GetRootAppliedMeshBoneRefTransformOrDefault(const bool bRight, const int32 JointIndex)
FORCEINLINE static FTransform GetMeshBoneRefTransform(const bool bRight, const EHandKeypoint Keypoint)
{
const FName& Name{GetMeshBoneName(bRight, JointIndex)};
const FTransform Transform{GetRootAppliedMeshBoneRefTransformOrDefault(bRight, Name)};
const FName& BoneName{GetMeshBoneName(bRight, Keypoint)};
const FTransform Transform{GetMeshBoneRefTransform(bRight, BoneName)};
return Transform;
}
FORCEINLINE static FTransform GetRootAppliedMeshBoneRefTransformOrDefault(
const bool bRight, const EHandKeypoint Keypoint)
{
const FName& Name{GetMeshBoneName(bRight, Keypoint)};
const FTransform Transform{GetRootAppliedMeshBoneRefTransformOrDefault(bRight, Name)};
return Transform;
}
FORCEINLINE static FTransform GetRootAppliedMeshBoneRefTransformOrDefault(
const EControllerHand Hand, const FName& BoneName)
FORCEINLINE static FTransform GetMeshBoneRefTransform(const EControllerHand Hand, const FName& BoneName)
{
const bool bRight = Hand == EControllerHand::Right;
const FTransform Transform{GetRootAppliedMeshBoneRefTransformOrDefault(bRight, BoneName)};
const FTransform Transform{GetMeshBoneRefTransform(bRight, BoneName)};
return Transform;
}
FORCEINLINE static FTransform GetRootAppliedMeshBoneRefTransformOrDefault(
const EControllerHand Hand, const int32 JointIndex)
FORCEINLINE static FTransform GetMeshBoneRefTransform(const EControllerHand Hand, const int32 JointIndex)
{
const bool bRight = Hand == EControllerHand::Right;
const FTransform Transform{GetRootAppliedMeshBoneRefTransformOrDefault(bRight, JointIndex)};
const FTransform Transform{GetMeshBoneRefTransform(bRight, JointIndex)};
return Transform;
}
FORCEINLINE static FTransform GetRootAppliedMeshBoneRefTransformOrDefault(
const EControllerHand Hand, const EHandKeypoint Keypoint)
FORCEINLINE static FTransform GetMeshBoneRefTransform(const EControllerHand Hand, const EHandKeypoint Keypoint)
{
const bool bRight = Hand == EControllerHand::Right;
const FTransform Transform{GetRootAppliedMeshBoneRefTransformOrDefault(bRight, Keypoint)};
const FTransform Transform{GetMeshBoneRefTransform(bRight, Keypoint)};
return Transform;
}
@@ -657,117 +560,55 @@ ESGPositionalTrackingHardware FSGXRTracker::FImpl::GetPositionalTrackingHardware
return TrackerHardware;
}
const FTransform& FSGXRTracker::FImpl::GetMeshBoneRefTransform(const bool bRight, const FName& BoneName)
const FTransform& FSGXRTracker::FImpl::GetRawMeshBoneRefTransform(const bool bRight, const FName& BoneName)
{
const FReferenceSkeleton& ReferenceSkeleton{
const TSoftObjectPtr<USkeletalMesh>& HandMesh{
bRight
? GetVirtualHandTrackingSettings().LeftHandReferenceMesh->GetRefSkeleton()
: GetVirtualHandTrackingSettings().RightHandReferenceMesh->GetRefSkeleton()
? GetVirtualHandTrackingSettings().LeftHandReferenceMesh
: GetVirtualHandTrackingSettings().RightHandReferenceMesh
};
if (HandMesh.IsValid())
{
const FReferenceSkeleton& ReferenceSkeleton{HandMesh->GetRefSkeleton()};
const int32 BoneIndex = ReferenceSkeleton.FindBoneIndex(BoneName);
if (BoneIndex == INDEX_NONE)
{
SGLOG_WARNING(FString::Printf(TEXT("Could not find a bone index for the '%s' joint!"),
*BoneName.ToString()));
return FTransform::Identity;
}
const FTransform& BoneTransform{ReferenceSkeleton.GetRefBonePose()[BoneIndex]};
return BoneTransform;
const TArray<FTransform> Transforms{ReferenceSkeleton.GetRefBonePose()};
ensureAlwaysMsgf(Transforms.Num() > 0 && Transforms.Num() > BoneIndex,
TEXT("Invalid bone index '%d' or no transforms found!"), BoneIndex);
const FTransform& Transform{Transforms[BoneIndex]};
return Transform;
}
const FVector& FSGXRTracker::FImpl::GetMeshDefaultBoneLocation(const bool bRight, const FName& BoneName)
const FSGVirtualHandTrackingSettings& Settings{GetVirtualHandTrackingSettings()};
ensureAlwaysMsgf(
bRight
? Settings.RightHandDefaultReferenceBoneTransforms.Contains(BoneName)
: Settings.LeftHandDefaultReferenceBoneTransforms.Contains(BoneName),
TEXT("Invalid bone name '%s'!"), *BoneName.ToString());
const FTransform& Transform{
bRight
? Settings.RightHandDefaultReferenceBoneTransforms[BoneName]
: Settings.LeftHandDefaultReferenceBoneTransforms[BoneName]
};
return Transform;
}
FTransform FSGXRTracker::FImpl::GetMeshBoneRefTransform(const bool bRight, const FName& BoneName)
{
static const FVector Dummy{FVector::ZeroVector};
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::Palm))
{
return GetMeshDefaultPalmBoneLocation(bRight);
}
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::IndexMetacarpal))
{
return GetMeshDefaultIndexMetacarpalBoneLocation(bRight);
}
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::MiddleMetacarpal))
{
return GetMeshDefaultMiddleMetacarpalBoneLocation(bRight);
}
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::RingMetacarpal))
{
return GetMeshDefaultRingMetacarpalBoneLocation(bRight);
}
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::LittleMetacarpal))
{
return GetMeshDefaultLittleMetacarpalBoneLocation(bRight);
}
ensureAlwaysMsgf(false, TEXT("FATAL: the code flow should never reach here!"));
return Dummy;
}
const FQuat& FSGXRTracker::FImpl::GetMeshDefaultBoneRotation(const bool bRight, const FName& BoneName)
{
static const FQuat Dummy{FQuat::Identity};
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::Palm))
{
return GetMeshDefaultPalmBoneRotation(bRight);
}
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::IndexMetacarpal))
{
return GetMeshDefaultIndexMetacarpalBoneRotation(bRight);
}
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::MiddleMetacarpal))
{
return GetMeshDefaultMiddleMetacarpalBoneRotation(bRight);
}
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::RingMetacarpal))
{
return GetMeshDefaultRingMetacarpalBoneRotation(bRight);
}
if (BoneName == GetMeshBoneName(bRight, EHandKeypoint::LittleMetacarpal))
{
return GetMeshDefaultLittleMetacarpalBoneRotation(bRight);
}
ensureAlwaysMsgf(false, TEXT("FATAL: the code flow should never reach here!"));
return Dummy;
}
FTransform FSGXRTracker::FImpl::GetRootAppliedMeshBoneRefTransformOrDefault(const bool bRight, const FName& BoneName)
{
FTransform Transform;
const FQuat RootRotationCorrection{GetVirtualHandTrackingSettings().HandRootBoneRotationCorrection.Quaternion()};
if ((bRight && GetVirtualHandTrackingSettings().RightHandReferenceMesh.IsValid())
|| (!bRight && GetVirtualHandTrackingSettings().LeftHandReferenceMesh.IsValid()))
{
FTransform RootBoneRefTransform{GetMeshBoneRefTransform(bRight, EHandKeypoint::Wrist)};
FTransform RootBoneRefTransform{GetRawMeshBoneRefTransform(bRight, EHandKeypoint::Wrist)};
RootBoneRefTransform.SetRotation(RootRotationCorrection * RootBoneRefTransform.GetRotation());
Transform = GetMeshBoneRefTransform(bRight, BoneName) * RootBoneRefTransform;
}
else
{
FTransform RootBoneRefTransform{
GetMeshDefaultRootBoneRotation(bRight), GetMeshDefaultRootBoneLocation(bRight)
};
RootBoneRefTransform.SetRotation(RootRotationCorrection * RootBoneRefTransform.GetRotation());
Transform = FTransform{
GetMeshDefaultBoneRotation(bRight, BoneName), GetMeshDefaultBoneLocation(bRight, BoneName)
} * RootBoneRefTransform;
}
const FTransform Transform{GetRawMeshBoneRefTransform(bRight, BoneName) * RootBoneRefTransform};
return Transform;
}
@@ -1678,7 +1519,7 @@ bool FSGXRTracker::FImpl::UpdateSGPalmJointData(
return false;
}
const FTransform JointTransform{GetRootAppliedMeshBoneRefTransformOrDefault(DeviceHand, EHandKeypoint::Palm)};
const FTransform JointTransform{GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::Palm)};
FVector JointLocation{JointTransform.GetLocation()};
FQuat JointRotation{JointTransform.GetRotation()};
@@ -1769,7 +1610,7 @@ bool FSGXRTracker::FImpl::UpdateSGIndexJointData(
static constexpr int32 IndexTipJointIndex = 3;
const FTransform& IndexMetacarpalTransform{
GetRootAppliedMeshBoneRefTransformOrDefault(DeviceHand, EHandKeypoint::IndexMetacarpal)
GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::IndexMetacarpal)
};
FVector IndexMetacarpalLocation{IndexMetacarpalTransform.GetLocation()};
FQuat IndexMetacarpalRotation{IndexMetacarpalTransform.GetRotation()};
@@ -1848,7 +1689,7 @@ bool FSGXRTracker::FImpl::UpdateSGMiddleJointData(
static constexpr int32 MiddleTipJointMiddle = 3;
const FTransform& MiddleMetacarpalTransform{
GetRootAppliedMeshBoneRefTransformOrDefault(DeviceHand, EHandKeypoint::MiddleMetacarpal)
GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::MiddleMetacarpal)
};
FVector MiddleMetacarpalLocation{MiddleMetacarpalTransform.GetLocation()};
FQuat MiddleMetacarpalRotation{MiddleMetacarpalTransform.GetRotation()};
@@ -1927,7 +1768,7 @@ bool FSGXRTracker::FImpl::UpdateSGRingJointData(
static constexpr int32 RingTipJointRing = 3;
const FTransform& RingMetacarpalTransform{
GetRootAppliedMeshBoneRefTransformOrDefault(DeviceHand, EHandKeypoint::RingMetacarpal)
GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::RingMetacarpal)
};
FVector RingMetacarpalLocation{RingMetacarpalTransform.GetLocation()};
FQuat RingMetacarpalRotation{RingMetacarpalTransform.GetRotation()};
@@ -2006,7 +1847,7 @@ bool FSGXRTracker::FImpl::UpdateSGLittleJointData(
static constexpr int32 LittleTipJointLittle = 3;
const FTransform& LittleMetacarpalTransform{
GetRootAppliedMeshBoneRefTransformOrDefault(DeviceHand, EHandKeypoint::LittleMetacarpal)
GetMeshBoneRefTransform(DeviceHand, EHandKeypoint::LittleMetacarpal)
};
FVector LittleMetacarpalLocation{LittleMetacarpalTransform.GetLocation()};
FQuat LittleMetacarpalRotation{LittleMetacarpalTransform.GetRotation()};