From 7abfc93ff964cc57db629c1549c5971784b4adf4 Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Fri, 4 Dec 2015 16:16:17 -0800 Subject: [PATCH] Avatar::getJointRotation and getJointTranslation is thread-safe It can be called from script with minimal blocking, because it inspects a copy of the joint values from the Rig, which is updated atomically. This copy occurs in Rig::updateAnimations() --- interface/src/avatar/Avatar.cpp | 6 ------ libraries/animation/src/Rig.cpp | 12 +++++++----- libraries/animation/src/Rig.h | 4 ++-- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 0fecb3a761..2c8d970336 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -857,18 +857,12 @@ QVector Avatar::getJointRotations() const { } glm::quat Avatar::getJointRotation(int index) const { - if (QThread::currentThread() != thread()) { - return AvatarData::getJointRotation(index); - } glm::quat rotation; _skeletonModel.getJointRotation(index, rotation); return rotation; } glm::vec3 Avatar::getJointTranslation(int index) const { - if (QThread::currentThread() != thread()) { - return AvatarData::getJointTranslation(index); - } glm::vec3 translation; _skeletonModel.getJointTranslation(index, translation); return translation; diff --git a/libraries/animation/src/Rig.cpp b/libraries/animation/src/Rig.cpp index 5ad000a62c..8fb56ed699 100644 --- a/libraries/animation/src/Rig.cpp +++ b/libraries/animation/src/Rig.cpp @@ -300,7 +300,7 @@ void Rig::clearJointAnimationPriority(int index) { void Rig::setJointTranslation(int index, bool valid, const glm::vec3& translation, float priority) { if (isIndexValid(index)) { if (valid) { - assert(_overrideFlags.size() == _internalPoseSet._overridePoses.size()); + assert(_internalPoseSet._overrideFlags.size() == _internalPoseSet._overridePoses.size()); _internalPoseSet._overrideFlags[index] = true; _internalPoseSet._overridePoses[index].trans = translation; } @@ -364,8 +364,9 @@ bool Rig::getJointRotationInWorldFrame(int jointIndex, glm::quat& result, const } bool Rig::getJointRotation(int jointIndex, glm::quat& rotation) const { - if (isIndexValid(jointIndex)) { - rotation = _internalPoseSet._relativePoses[jointIndex].rot; + QReadLocker readLock(&_externalPoseSetLock); + if (jointIndex >= 0 && jointIndex < (int)_externalPoseSet._relativePoses.size()) { + rotation = _externalPoseSet._relativePoses[jointIndex].rot; return true; } else { return false; @@ -373,8 +374,9 @@ bool Rig::getJointRotation(int jointIndex, glm::quat& rotation) const { } bool Rig::getJointTranslation(int jointIndex, glm::vec3& translation) const { - if (isIndexValid(jointIndex)) { - translation = _internalPoseSet._relativePoses[jointIndex].trans; + QReadLocker readLock(&_externalPoseSetLock); + if (jointIndex >= 0 && jointIndex < (int)_externalPoseSet._relativePoses.size()) { + translation = _externalPoseSet._relativePoses[jointIndex].trans; return true; } else { return false; diff --git a/libraries/animation/src/Rig.h b/libraries/animation/src/Rig.h index 3d8b0f3bc7..64ce8c52ef 100644 --- a/libraries/animation/src/Rig.h +++ b/libraries/animation/src/Rig.h @@ -127,10 +127,10 @@ public: // if rotation is identity, result will be in rig space bool getJointRotationInWorldFrame(int jointIndex, glm::quat& result, const glm::quat& rotation) const; - // geometry space + // geometry space (thread-safe) bool getJointRotation(int jointIndex, glm::quat& rotation) const; - // geometry space + // geometry space (thread-safe) bool getJointTranslation(int jointIndex, glm::vec3& translation) const; // legacy