dry up some code

This commit is contained in:
Seth Alves 2017-07-06 18:41:11 -07:00
parent 57ba2c5cd6
commit 801c45898f
2 changed files with 30 additions and 43 deletions

View file

@ -1008,64 +1008,51 @@ glm::vec3 Avatar::getAbsoluteJointTranslationInObjectFrame(int index) const {
}
}
void Avatar::cacheJoints() const {
// _jointIndicesCacheLock should be locked for write before calling this.
if (_jointsCached) {
return;
}
_jointIndicesCache.clear();
if (_skeletonModel && _skeletonModel->isActive()) {
_jointIndicesCache = _skeletonModel->getFBXGeometry().jointIndices;
_jointsCached = true;
}
}
void Avatar::invalidateJointIndicesCache() const {
QWriteLocker writeLock(&_jointIndicesCacheLock);
_jointsCached = false;
}
void Avatar::withValidCache(std::function<void()> const& worker) const {
QReadLocker readLock(&_jointIndicesCacheLock);
if (_jointsCached) {
worker();
} else {
readLock.unlock();
{
QWriteLocker writeLock(&_jointIndicesCacheLock);
if (!_jointsCached) {
_jointIndicesCache.clear();
if (_skeletonModel && _skeletonModel->isActive()) {
_jointIndicesCache = _skeletonModel->getFBXGeometry().jointIndices;
_jointsCached = true;
}
}
worker();
}
}
}
int Avatar::getJointIndex(const QString& name) const {
int result = getFauxJointIndex(name);
if (result != -1) {
return result;
}
auto getJointIndexWorker = [&]() {
withValidCache([&]() {
if (_jointIndicesCache.contains(name)) {
return _jointIndicesCache[name];
} else {
return -1;
result = _jointIndicesCache[name];
}
};
QReadLocker readLock(&_jointIndicesCacheLock);
if (_jointsCached) {
return getJointIndexWorker();
} else {
readLock.unlock();
{
QWriteLocker writeLock(&_jointIndicesCacheLock);
Avatar::cacheJoints();
return getJointIndexWorker();
}
}
});
return result;
}
QStringList Avatar::getJointNames() const {
auto getJointNamesWorker = [&]() {
return _jointIndicesCache.keys();
};
QReadLocker readLock(&_jointIndicesCacheLock);
if (_jointsCached) {
return getJointNamesWorker();
} else {
readLock.unlock();
{
QWriteLocker writeLock(&_jointIndicesCacheLock);
Avatar::cacheJoints();
return getJointNamesWorker();
}
}
QStringList result;
withValidCache([&]() {
result = _jointIndicesCache.keys();
});
return result;
}
glm::vec3 Avatar::getJointPosition(int index) const {

View file

@ -268,8 +268,8 @@ protected:
SkeletonModelPointer _skeletonModel;
void cacheJoints() const;
void invalidateJointIndicesCache() const;
void withValidCache(std::function<void()> const& worker) const;
mutable QHash<QString, int> _jointIndicesCache;
mutable QReadWriteLock _jointIndicesCacheLock;
mutable bool _jointsCached { false };