/** * @file * * @author Mamadou Babaei * * @section LICENSE * * (The MIT License) * * Copyright (c) 2020 - 2022 SenseGlove * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * @section DESCRIPTION * * */ #include "SGCoreImpl/SGBasicHandModelImpl.h" #include #include #include "Containers/StringConv.h" #include "Runtime/Launch/Resources/Version.h" #include "SGBuildHacks/SGInclude_Core_BasicHandModel.h" #include "SGBuildHacks/SGInclude_Core_Vect3D.h" #include "SGCoreImpl/SGCoreEnumCast.h" #include "SGCoreImpl/SGQuatImpl.h" #include "SGCoreImpl/SGVect3DImpl.h" #include "SGUtils/SGArrayUtils.h" #include "SGUtils/SGUnits.h" struct FSGBasicHandModelImpl::FImpl { /************************ * Member variables ************************/ FBasicHandModelType BasicHandModel; /************************ * Owner object ************************/ FSGBasicHandModelImpl* Owner; /************************ * Constructor / Destructor ************************/ explicit FImpl(FSGBasicHandModelImpl* InOwner); ~FImpl(); /************************ * Default copy constructor & copy assignment operator ************************/ FImpl(const FImpl& Rhs) = default; FImpl& operator=(const FImpl& Rhs) = default; }; FSGBasicHandModelImpl FSGBasicHandModelImpl::Default(bool bRightHanded) { return FSGBasicHandModelImpl(FSGBasicHandModelImpl::FBasicHandModelType::Default(bRightHanded)); } FSGBasicHandModelImpl FSGBasicHandModelImpl::Deserialize(const FString& SerializedString) { std::string Serialized(TCHAR_TO_UTF8(*SerializedString)); const FSGBasicHandModelImpl BasicHandModel(FBasicHandModelType::Deserialize(MoveTemp(Serialized))); return BasicHandModel; } const TArray>& FSGBasicHandModelImpl::GetBaseFingerLengths() { static const TArray> BaseFingerLengths{ FSGArrayUtils::FromStdVector( FSGBasicHandModelImpl::FBasicHandModelType::GetBaseFingerLengths()) }; return BaseFingerLengths; } const TArray& FSGBasicHandModelImpl::GetBaseJointPositions() { static const TArray BaseJointPositions{ FSGArrayUtils::FromStdVector( FBasicHandModelType::GetBaseJointPositions()) }; return BaseJointPositions; } const TArray& FSGBasicHandModelImpl::GetBaseJointRotations() { static const TArray BaseJointRotations{ FSGArrayUtils::FromStdVector( FBasicHandModelType::GetBaseJointRotations()) }; return BaseJointRotations; } FSGBasicHandModelImpl::FSGBasicHandModelImpl() : #if ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) Pimpl(TUniquePtr(new FImpl{this}, PimplDeleter)) #else Pimpl(TUniquePtr(new FImpl{this})) #endif /* ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) */ { } FSGBasicHandModelImpl::FSGBasicHandModelImpl(bool bRightHanded, const TArray>& Lengths, const TArray& StartPositions) : #if ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) Pimpl(TUniquePtr(new FImpl{this}, PimplDeleter)) #else Pimpl(TUniquePtr(new FImpl{this})) #endif /* ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) */ { std::vector> LengthsVector{ FSGArrayUtils::ToStdVector(Lengths) }; std::vector StartPositionsVector{FSGArrayUtils::ToStdVector< FVector, FSGVect3DImpl::FVect3DType, FSGVect3DImpl, &FSGVect3DImpl::ToVect3D>(StartPositions)}; Pimpl->BasicHandModel = FSGBasicHandModelImpl::FBasicHandModelType(bRightHanded, MoveTemp(LengthsVector), MoveTemp(StartPositionsVector)); } FSGBasicHandModelImpl::FSGBasicHandModelImpl(bool bRightHanded, const TArray>& Lengths, const TArray& StartPositions, const TArray& StartRotations) : #if ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) Pimpl(TUniquePtr(new FImpl{this}, PimplDeleter)) #else Pimpl(TUniquePtr(new FImpl{this})) #endif /* ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) */ { std::vector> LengthsVector{ FSGArrayUtils::ToStdVector(Lengths) }; std::vector StartPositionsVector{FSGArrayUtils::ToStdVector< FVector, FSGVect3DImpl::FVect3DType, FSGVect3DImpl, &FSGVect3DImpl::ToVect3D>(StartPositions)}; std::vector StartRotationsVector{FSGArrayUtils::ToStdVector< FQuat, FSGQuatImpl::FQuatType, FSGQuatImpl, &FSGQuatImpl::ToQuat>(StartRotations)}; Pimpl->BasicHandModel = FSGBasicHandModelImpl::FBasicHandModelType(bRightHanded, MoveTemp(LengthsVector), MoveTemp(StartPositionsVector), MoveTemp(StartRotationsVector)); } FSGBasicHandModelImpl::FSGBasicHandModelImpl(const FBasicHandModelType& BasicHandModel) : #if ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) Pimpl(TUniquePtr(new FImpl{this}, PimplDeleter)) #else Pimpl(TUniquePtr(new FImpl{this})) #endif /* ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) */ { Pimpl->BasicHandModel = BasicHandModel; } FSGBasicHandModelImpl::FSGBasicHandModelImpl(const FSGBasicHandModelImpl& Rhs) : #if ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) Pimpl(TUniquePtr(new FImpl{*Rhs.Pimpl}, PimplDeleter)) #else Pimpl(TUniquePtr(new FImpl{*Rhs.Pimpl})) #endif /* ENGINE_MAJOR_VERSION == 5 || ( ENGINE_MAJOR_VERSION == 4 && ENGINE_MINOR_VERSION > 22 ) */ { Pimpl->Owner = this; } FSGBasicHandModelImpl::FSGBasicHandModelImpl(FSGBasicHandModelImpl&& Rhs) noexcept = default; FSGBasicHandModelImpl::~FSGBasicHandModelImpl() = default; FSGBasicHandModelImpl& FSGBasicHandModelImpl::operator=(const FSGBasicHandModelImpl& Rhs) { *Pimpl = *Rhs.Pimpl; Pimpl->Owner = this; return *this; } FSGBasicHandModelImpl& FSGBasicHandModelImpl::operator=(FSGBasicHandModelImpl&& Rhs) noexcept = default; const FSGBasicHandModelImpl::FBasicHandModelType& FSGBasicHandModelImpl::ToBasicHandModel() const { return Pimpl->BasicHandModel; } bool FSGBasicHandModelImpl::IsRight() const { return Pimpl->BasicHandModel.IsRight(); } void FSGBasicHandModelImpl::SetIsRight(const bool bRightHanded) { Pimpl->BasicHandModel.SetIsRight(bRightHanded); } TArray> FSGBasicHandModelImpl::GetFingerLengths() const { TArray> FingerLengths{ FSGArrayUtils::FromMillimeters(Pimpl->BasicHandModel.GetFingerLengths()) }; return MoveTemp(FingerLengths); } void FSGBasicHandModelImpl::SetFingerLengths(const TArray>& FingerLengths) { std::vector> FingerLengthsVector{ FSGArrayUtils::ToMillimeters(FingerLengths) }; Pimpl->BasicHandModel.SetFingerLengths(MoveTemp(FingerLengthsVector)); } TArray FSGBasicHandModelImpl::GetFingerLengths(const ESGFinger Finger) const { TArray FingerLengths{ FSGArrayUtils::FromMillimeters(Pimpl->BasicHandModel.GetFingerLengths(FSGCoreEnumCast::ToEFinger(Finger))) }; return MoveTemp(FingerLengths); } void FSGBasicHandModelImpl::SetFingerLengths(const ESGFinger Finger, const TArray& Lengths) { std::vector LengthsVector{ FSGArrayUtils::ToMillimeters(Lengths) }; Pimpl->BasicHandModel.SetFingerLengths(FSGCoreEnumCast::ToEFinger(Finger), MoveTemp(LengthsVector)); } void FSGBasicHandModelImpl::SetFingerLengths(const uint8 Finger, const TArray& Lengths) { std::vector LengthsVector{ FSGArrayUtils::ToMillimeters(Lengths) }; Pimpl->BasicHandModel.SetFingerLengths(Finger, MoveTemp(LengthsVector)); } TArray> FSGBasicHandModelImpl::GetFingerRatios() const { TArray> FingerRatios{ FSGArrayUtils::FromStdVector(Pimpl->BasicHandModel.GetFingerRatios()) }; return MoveTemp(FingerRatios); } TArray FSGBasicHandModelImpl::GetFingerRatios(const ESGFinger Finger) const { TArray FingerRatios{ FSGArrayUtils::FromStdVector(Pimpl->BasicHandModel.GetFingerRatios(FSGCoreEnumCast::ToEFinger(Finger))) }; return MoveTemp(FingerRatios); } TArray FSGBasicHandModelImpl::GetStartJointPositions() const { TArray StartJointPositions{ FSGArrayUtils::FromStdVector( Pimpl->BasicHandModel.GetStartJointPositions()) }; return MoveTemp(StartJointPositions); } void FSGBasicHandModelImpl::SetStartJointPositions(const TArray& StartJointPositions) { std::vector StartJointPositionsVector{ FSGArrayUtils::ToStdVector< FVector, FSGVect3DImpl::FVect3DType, FSGVect3DImpl, &FSGVect3DImpl::ToVect3D>(StartJointPositions) }; Pimpl->BasicHandModel.SetStartJointPositions(MoveTemp(StartJointPositionsVector)); } void FSGBasicHandModelImpl::SetStartJointPosition(ESGFinger Finger, const FVector& Position) { Pimpl->BasicHandModel.SetStartJointPosition(FSGCoreEnumCast::ToEFinger(Finger), FSGVect3DImpl(Position).ToVect3D()); } void FSGBasicHandModelImpl::SetStartJointPosition(const uint8 Finger, const FVector& Position) { Pimpl->BasicHandModel.SetStartJointPosition(Finger, FSGVect3DImpl(Position).ToVect3D()); } TArray FSGBasicHandModelImpl::GetStartJointRotations() const { TArray StartJointRotations{ FSGArrayUtils::FromStdVector< FSGQuatImpl::FQuatType, FQuat, FSGQuatImpl, &FSGQuatImpl::ToFQuat>( Pimpl->BasicHandModel.GetStartJointRotations()) }; return MoveTemp(StartJointRotations); } void FSGBasicHandModelImpl::SetStartJointRotations(const TArray& StartJointRotations) { std::vector StartJointRotationsVector{ FSGArrayUtils::ToStdVector< FQuat, FSGQuatImpl::FQuatType, FSGQuatImpl, &FSGQuatImpl::ToQuat>(StartJointRotations) }; Pimpl->BasicHandModel.SetStartJointRotations(MoveTemp(StartJointRotationsVector)); } FVector FSGBasicHandModelImpl::GetJointPosition(const ESGFinger Finger) const { const FSGVect3DImpl Position(Pimpl->BasicHandModel.GetJointPosition(FSGCoreEnumCast::ToEFinger(Finger))); return Position.ToFVector(); } void FSGBasicHandModelImpl::SetJointPosition(const FVector& NewPosition, const ESGFinger Finger) { Pimpl->BasicHandModel.SetJointPosition(FSGVect3DImpl(NewPosition).ToVect3D(), FSGCoreEnumCast::ToEFinger(Finger)); } FQuat FSGBasicHandModelImpl::GetJointRotation(const ESGFinger Finger) const { const FSGQuatImpl Rotation(Pimpl->BasicHandModel.GetJointRotation(FSGCoreEnumCast::ToEFinger(Finger))); return Rotation.ToFQuat(); } void FSGBasicHandModelImpl::SetJointRotation(const FQuat& NewRotation, const ESGFinger Finger) { Pimpl->BasicHandModel.SetJointRotation(FSGQuatImpl(NewRotation).ToQuat(), FSGCoreEnumCast::ToEFinger(Finger)); } TArray FSGBasicHandModelImpl::GetTotalLengths() const { const TArray TotalLengths{ FSGArrayUtils::FromMillimeters(Pimpl->BasicHandModel.GetTotalLengths()) }; return TotalLengths; } float FSGBasicHandModelImpl::GetTotalLength(const ESGFinger Finger) const { return FSGUnits::MillimetersToCentimeters( Pimpl->BasicHandModel.GetTotalLength(FSGCoreEnumCast::ToEFinger(Finger))); } TArray FSGBasicHandModelImpl::Get3DLengths(const ESGFinger Finger) const { const TArray Lengths{ FSGArrayUtils::FromStdVector( Pimpl->BasicHandModel.Get3DLengths(FSGCoreEnumCast::ToEFinger(Finger))) }; return Lengths; } bool FSGBasicHandModelImpl::Equals(const FSGBasicHandModelImpl& BasicHandModelImpl) const { return Pimpl->BasicHandModel.Equals(BasicHandModelImpl.ToBasicHandModel()); } FString FSGBasicHandModelImpl::ToString() const { const FString String(UTF8_TO_TCHAR(Pimpl->BasicHandModel.ToString().c_str())); return String; } FString FSGBasicHandModelImpl::ToString(const bool bLengthsOnly) const { const FString String(UTF8_TO_TCHAR(Pimpl->BasicHandModel.ToString(bLengthsOnly).c_str())); return String; } FString FSGBasicHandModelImpl::Serialize() const { const FString Serialized(UTF8_TO_TCHAR(Pimpl->BasicHandModel.Serialize().c_str())); return Serialized; } FSGBasicHandModelImpl::FImpl::FImpl(FSGBasicHandModelImpl* InOwner) : Owner(InOwner) { } FSGBasicHandModelImpl::FImpl::~FImpl() = default; void FSGBasicHandModelImpl::FImplDeleter::operator()(const FImpl* P) const { delete P; }