This commit is contained in:
Andrzej Kapolka 2014-03-03 15:48:38 -08:00
commit f5eaa330d8
8 changed files with 24 additions and 30 deletions

View file

@ -33,10 +33,7 @@ void ParticleTreeRenderer::init() {
void ParticleTreeRenderer::update() { void ParticleTreeRenderer::update() {
if (_tree) { if (_tree) {
ParticleTree* tree = static_cast<ParticleTree*>(_tree); ParticleTree* tree = static_cast<ParticleTree*>(_tree);
if (tree->tryLockForWrite()) { tree->update();
tree->update();
tree->unlock();
}
} }
} }

View file

@ -41,7 +41,8 @@ float boundaryDistanceForRenderLevel(unsigned int renderLevel, float voxelSizeSc
Octree::Octree(bool shouldReaverage) : Octree::Octree(bool shouldReaverage) :
_isDirty(true), _isDirty(true),
_shouldReaverage(shouldReaverage), _shouldReaverage(shouldReaverage),
_stopImport(false) { _stopImport(false),
_lock(QReadWriteLock::Recursive) {
_rootNode = NULL; _rootNode = NULL;
_isViewing = false; _isViewing = false;
} }
@ -551,7 +552,10 @@ OctreeElement* Octree::getOctreeElementAt(float x, float y, float z, float s) co
OctreeElement* Octree::getOrCreateChildElementAt(float x, float y, float z, float s) { OctreeElement* Octree::getOrCreateChildElementAt(float x, float y, float z, float s) {
return getRoot()->getOrCreateChildElementAt(x, y, z, s); lockForWrite();
OctreeElement* result = getRoot()->getOrCreateChildElementAt(x, y, z, s);
unlock();
return result;
} }

View file

@ -240,11 +240,11 @@ public:
bool readFromSVOFile(const char* filename); bool readFromSVOFile(const char* filename);
// Octree does not currently handle its own locking, caller must use these to lock/unlock // Octree does not currently handle its own locking, caller must use these to lock/unlock
void lockForRead() { lock.lockForRead(); } void lockForRead() { _lock.lockForRead(); }
bool tryLockForRead() { return lock.tryLockForRead(); } bool tryLockForRead() { return _lock.tryLockForRead(); }
void lockForWrite() { lock.lockForWrite(); } void lockForWrite() { _lock.lockForWrite(); }
bool tryLockForWrite() { return lock.tryLockForWrite(); } bool tryLockForWrite() { return _lock.tryLockForWrite(); }
void unlock() { lock.unlock(); } void unlock() { _lock.unlock(); }
unsigned long getOctreeElementsCount(); unsigned long getOctreeElementsCount();
@ -329,7 +329,7 @@ protected:
/// flushes out any Octal Codes that had to be queued /// flushes out any Octal Codes that had to be queued
void emptyDeleteQueue(); void emptyDeleteQueue();
QReadWriteLock lock; QReadWriteLock _lock;
/// This tree is receiving inbound viewer datagrams. /// This tree is receiving inbound viewer datagrams.
bool _isViewing; bool _isViewing;

View file

@ -385,7 +385,7 @@ Particle Particle::fromEditPacket(const unsigned char* data, int length, int& pr
} else { } else {
// look up the existing particle // look up the existing particle
const Particle* existingParticle = tree->findParticleByID(editID, true); const Particle* existingParticle = tree->findParticleByID(editID);
// copy existing properties before over-writing with new properties // copy existing properties before over-writing with new properties
if (existingParticle) { if (existingParticle) {

View file

@ -99,8 +99,8 @@ void ParticleTree::storeParticle(const Particle& particle, const SharedNodePoint
if (!args.found) { if (!args.found) {
glm::vec3 position = particle.getPosition(); glm::vec3 position = particle.getPosition();
float size = std::max(MINIMUM_PARTICLE_ELEMENT_SIZE, particle.getRadius()); float size = std::max(MINIMUM_PARTICLE_ELEMENT_SIZE, particle.getRadius());
ParticleTreeElement* element = (ParticleTreeElement*)getOrCreateChildElementAt(position.x, position.y, position.z, size);
ParticleTreeElement* element = (ParticleTreeElement*)getOrCreateChildElementAt(position.x, position.y, position.z, size);
element->storeParticle(particle); element->storeParticle(particle);
} }
// what else do we need to do here to get reaveraging to work // what else do we need to do here to get reaveraging to work
@ -149,8 +149,8 @@ void ParticleTree::addParticle(const ParticleID& particleID, const ParticlePrope
Particle particle(particleID, properties); Particle particle(particleID, properties);
glm::vec3 position = particle.getPosition(); glm::vec3 position = particle.getPosition();
float size = std::max(MINIMUM_PARTICLE_ELEMENT_SIZE, particle.getRadius()); float size = std::max(MINIMUM_PARTICLE_ELEMENT_SIZE, particle.getRadius());
ParticleTreeElement* element = (ParticleTreeElement*)getOrCreateChildElementAt(position.x, position.y, position.z, size); ParticleTreeElement* element = (ParticleTreeElement*)getOrCreateChildElementAt(position.x, position.y, position.z, size);
element->storeParticle(particle); element->storeParticle(particle);
_isDirty = true; _isDirty = true;
@ -370,17 +370,12 @@ bool ParticleTree::findByIDOperation(OctreeElement* element, void* extraData) {
} }
const Particle* ParticleTree::findParticleByID(uint32_t id, bool alreadyLocked) { const Particle* ParticleTree::findParticleByID(uint32_t id) {
FindByIDArgs args = { id, false, NULL }; FindByIDArgs args = { id, false, NULL };
if (!alreadyLocked) {
//qDebug() << "ParticleTree::findParticleByID().... about to call lockForRead()...."; lockForRead();
lockForRead();
//qDebug() << "ParticleTree::findParticleByID().... after call lockForRead()....";
}
recurseTreeWithOperation(findByIDOperation, &args); recurseTreeWithOperation(findByIDOperation, &args);
if (!alreadyLocked) { unlock();
unlock();
}
return args.foundParticle; return args.foundParticle;
} }
@ -487,7 +482,9 @@ void ParticleTree::update() {
} }
// prune the tree... // prune the tree...
lockForWrite();
recurseTreeWithOperation(pruneOperation, NULL); recurseTreeWithOperation(pruneOperation, NULL);
unlock();
} }

View file

@ -44,7 +44,7 @@ public:
void addParticle(const ParticleID& particleID, const ParticleProperties& properties); void addParticle(const ParticleID& particleID, const ParticleProperties& properties);
void deleteParticle(const ParticleID& particleID); void deleteParticle(const ParticleID& particleID);
const Particle* findClosestParticle(glm::vec3 position, float targetRadius); const Particle* findClosestParticle(glm::vec3 position, float targetRadius);
const Particle* findParticleByID(uint32_t id, bool alreadyLocked = false); const Particle* findParticleByID(uint32_t id);
/// finds all particles that touch a sphere /// finds all particles that touch a sphere
/// \param center the center of the sphere /// \param center the center of the sphere

View file

@ -33,9 +33,7 @@ ParticleID ParticlesScriptingInterface::addParticle(const ParticleProperties& pr
// If we have a local particle tree set, then also update it. // If we have a local particle tree set, then also update it.
if (_particleTree) { if (_particleTree) {
_particleTree->lockForWrite();
_particleTree->addParticle(id, properties); _particleTree->addParticle(id, properties);
_particleTree->unlock();
} }
return id; return id;
@ -66,7 +64,7 @@ ParticleProperties ParticlesScriptingInterface::getParticleProperties(ParticleID
} }
if (_particleTree) { if (_particleTree) {
_particleTree->lockForRead(); _particleTree->lockForRead();
const Particle* particle = _particleTree->findParticleByID(identity.id, true); const Particle* particle = _particleTree->findParticleByID(identity.id);
if (particle) { if (particle) {
results.copyFromParticle(*particle); results.copyFromParticle(*particle);
} else { } else {

View file

@ -30,12 +30,10 @@ public:
private slots: private slots:
/// inbound slots for external collision systems /// inbound slots for external collision systems
void forwardParticleCollisionWithVoxel(const ParticleID& particleID, const VoxelDetail& voxel) { void forwardParticleCollisionWithVoxel(const ParticleID& particleID, const VoxelDetail& voxel) {
qDebug() << "forwardParticleCollisionWithVoxel()";
emit particleCollisionWithVoxel(particleID, voxel); emit particleCollisionWithVoxel(particleID, voxel);
} }
void forwardParticleCollisionWithParticle(const ParticleID& idA, const ParticleID& idB) { void forwardParticleCollisionWithParticle(const ParticleID& idA, const ParticleID& idB) {
qDebug() << "forwardParticleCollisionWithParticle()";
emit particleCollisionWithParticle(idA, idB); emit particleCollisionWithParticle(idA, idB);
} }