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 { void Avatar::invalidateJointIndicesCache() const {
QWriteLocker writeLock(&_jointIndicesCacheLock); QWriteLocker writeLock(&_jointIndicesCacheLock);
_jointsCached = false; _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 Avatar::getJointIndex(const QString& name) const {
int result = getFauxJointIndex(name); int result = getFauxJointIndex(name);
if (result != -1) { if (result != -1) {
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); return result;
if (_jointsCached) {
return getJointIndexWorker();
} else {
readLock.unlock();
{
QWriteLocker writeLock(&_jointIndicesCacheLock);
Avatar::cacheJoints();
return getJointIndexWorker();
}
}
} }
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 };