From f245aa6ac55b7e759eb55b9004b14bccadf125fb Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Sun, 2 Mar 2014 20:12:52 -0800 Subject: [PATCH] remove more cruft from VoxelSystem, move getElementEnclosingPoint() to Octree class where it belongs --- interface/src/VoxelSystem.cpp | 32 ---------- interface/src/VoxelSystem.h | 2 - interface/src/avatar/Hand.cpp | 18 +++--- libraries/octree/src/Octree.cpp | 104 ++++++++++++++++++++++++-------- libraries/octree/src/Octree.h | 9 ++- 5 files changed, 95 insertions(+), 70 deletions(-) diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 6e7c24777f..0f90d51332 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -1774,38 +1774,6 @@ void VoxelSystem::forceRedrawEntireTree() { setupNewVoxelsForDrawing(); } -class VoxelAndPoint { -public: - VoxelTreeElement* voxel; - glm::vec3 point; -}; - -// Find the smallest colored voxel enclosing a point (if there is one) -bool VoxelSystem::getVoxelEnclosingOperation(OctreeElement* element, void* extraData) { - VoxelTreeElement* voxel = (VoxelTreeElement*)element; - VoxelAndPoint* args = (VoxelAndPoint*) extraData; - AABox voxelBox = voxel->getAABox(); - if (voxelBox.contains(args->point)) { - if (voxel->isColored() && voxel->isLeaf()) { - // we've reached a solid leaf containing the point, return the node. - args->voxel = voxel; - return false; - } - } else { - // The point is not inside this voxel, so stop recursing. - return false; - } - return true; // keep looking -} - -VoxelTreeElement* VoxelSystem::getVoxelEnclosing(const glm::vec3& point) { - VoxelAndPoint voxelAndPoint; - voxelAndPoint.point = point; - voxelAndPoint.voxel = NULL; - _tree->recurseTreeWithOperation(getVoxelEnclosingOperation, (void*) &voxelAndPoint); - return voxelAndPoint.voxel; -} - bool VoxelSystem::isViewChanging() { bool result = false; // assume the best diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index 512ff1c065..b49891f8d9 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -84,8 +84,6 @@ public: virtual void elementDeleted(OctreeElement* element); virtual void elementUpdated(OctreeElement* element); - VoxelTreeElement* getVoxelEnclosing(const glm::vec3& point); - public slots: void nodeAdded(SharedNodePointer node); void nodeKilled(SharedNodePointer node); diff --git a/interface/src/avatar/Hand.cpp b/interface/src/avatar/Hand.cpp index 51f919130b..e04760526a 100644 --- a/interface/src/avatar/Hand.cpp +++ b/interface/src/avatar/Hand.cpp @@ -87,23 +87,25 @@ void Hand::simulate(float deltaTime, bool isMine) { // Voxel Drumming with fingertips if enabled if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelDrumming)) { - VoxelTreeElement* fingerNode = Application::getInstance()->getVoxels()->getVoxelEnclosing( + OctreeElement* fingerElement = Application::getInstance()->getVoxelTree()->getElementEnclosingPoint( glm::vec3(fingerTipPosition / (float)TREE_SCALE)); - if (fingerNode) { + VoxelTreeElement* fingerVoxel = static_cast(fingerElement); + if (fingerVoxel) { if (!palm.getIsCollidingWithVoxel()) { // Collision has just started palm.setIsCollidingWithVoxel(true); - handleVoxelCollision(&palm, fingerTipPosition, fingerNode, deltaTime); + handleVoxelCollision(&palm, fingerTipPosition, fingerVoxel, deltaTime); + // Set highlight voxel VoxelDetail voxel; - glm::vec3 pos = fingerNode->getCorner(); + glm::vec3 pos = fingerVoxel->getCorner(); voxel.x = pos.x; voxel.y = pos.y; voxel.z = pos.z; - voxel.s = fingerNode->getScale(); - voxel.red = fingerNode->getColor()[0]; - voxel.green = fingerNode->getColor()[1]; - voxel.blue = fingerNode->getColor()[2]; + voxel.s = fingerVoxel->getScale(); + voxel.red = fingerVoxel->getColor()[0]; + voxel.green = fingerVoxel->getColor()[1]; + voxel.blue = fingerVoxel->getColor()[2]; Application::getInstance()->setHighlightVoxel(voxel); Application::getInstance()->setIsHighlightVoxel(true); } diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 49f1bc9c3a..446eb25a50 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -588,9 +588,16 @@ bool findRayIntersectionOp(OctreeElement* node, void* extraData) { } bool Octree::findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, - OctreeElement*& node, float& distance, BoxFace& face) { + OctreeElement*& node, float& distance, BoxFace& face, bool tryLock) { RayArgs args = { origin / (float)(TREE_SCALE), direction, node, distance, face }; - recurseTreeWithOperation(findRayIntersectionOp, &args); + + if (!tryLock) { + lockForRead(); + } + if (tryLock && tryLockForRead()) { + recurseTreeWithOperation(findRayIntersectionOp, &args); + unlock(); + } return args.found; } @@ -626,25 +633,27 @@ bool findSpherePenetrationOp(OctreeElement* element, void* extraData) { } bool Octree::findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration, - void** penetratedObject) { + void** penetratedObject, bool tryLock) { - bool result = false; // assume no penetration - if (tryLockForRead()) { - SphereArgs args = { - center / (float)(TREE_SCALE), - radius / (float)(TREE_SCALE), - penetration, - false, - NULL }; - penetration = glm::vec3(0.0f, 0.0f, 0.0f); + SphereArgs args = { + center / (float)(TREE_SCALE), + radius / (float)(TREE_SCALE), + penetration, + false, + NULL }; + penetration = glm::vec3(0.0f, 0.0f, 0.0f); + + if (!tryLock) { + lockForRead(); + } + if (tryLock && tryLockForRead()) { recurseTreeWithOperation(findSpherePenetrationOp, &args); if (penetratedObject) { *penetratedObject = args.penetratedObject; } unlock(); - result = args.found; } - return result; + return args.found; } class CapsuleArgs { @@ -677,22 +686,67 @@ bool findCapsulePenetrationOp(OctreeElement* node, void* extraData) { return false; } -bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration) { - bool result = false; // assume no penetration - if (tryLockForRead()) { - CapsuleArgs args = { - start / (float)(TREE_SCALE), - end / (float)(TREE_SCALE), - radius / (float)(TREE_SCALE), - penetration }; - penetration = glm::vec3(0.0f, 0.0f, 0.0f); +bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, + glm::vec3& penetration, bool tryLock) { + + CapsuleArgs args = { + start / (float)(TREE_SCALE), + end / (float)(TREE_SCALE), + radius / (float)(TREE_SCALE), + penetration, + false }; + penetration = glm::vec3(0.0f, 0.0f, 0.0f); + + if (!tryLock) { + lockForRead(); + } + if (tryLock && tryLockForRead()) { recurseTreeWithOperation(findCapsulePenetrationOp, &args); - result = args.found; unlock(); } - return result; + return args.found; } +class GetElementEnclosingArgs { +public: + OctreeElement* element; + glm::vec3 point; +}; + +// Find the smallest colored voxel enclosing a point (if there is one) +bool getElementEnclosingOperation(OctreeElement* element, void* extraData) { + GetElementEnclosingArgs* args = static_cast(extraData); + AABox elementBox = element->getAABox(); + if (elementBox.contains(args->point)) { + if (element->hasContent() && element->isLeaf()) { + // we've reached a solid leaf containing the point, return the node. + args->element = element; + return false; + } + } else { + // The point is not inside this voxel, so stop recursing. + return false; + } + return true; // keep looking +} + +OctreeElement* Octree::getElementEnclosingPoint(const glm::vec3& point, bool tryLock) { + GetElementEnclosingArgs args; + args.point = point; + args.element = NULL; + + if (!tryLock) { + lockForRead(); + } + if (tryLock && tryLockForRead()) { + recurseTreeWithOperation(getElementEnclosingOperation, (void*)&args); + unlock(); + } + return args.element; +} + + + int Octree::encodeTreeBitstream(OctreeElement* node, OctreePacketData* packetData, OctreeElementBag& bag, EncodeBitstreamParams& params) { diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 807de85607..9db83d47b1 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -222,12 +222,15 @@ public: void setDirtyBit() { _isDirty = true; } bool findRayIntersection(const glm::vec3& origin, const glm::vec3& direction, - OctreeElement*& node, float& distance, BoxFace& face); + OctreeElement*& node, float& distance, BoxFace& face, bool tryLock = true); bool findSpherePenetration(const glm::vec3& center, float radius, glm::vec3& penetration, - void** penetratedObject = NULL); + void** penetratedObject = NULL, bool tryLock = true); - bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration); + bool findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, + glm::vec3& penetration, bool tryLock = true); + + OctreeElement* getElementEnclosingPoint(const glm::vec3& point, bool tryLock = true); // Note: this assumes the fileFormat is the HIO individual voxels code files void loadOctreeFile(const char* fileName, bool wantColorRandomizer);