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

View file

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