From 44357fcfb91a8377a25ebc2ea4e0fbb17970aea1 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 9 Jul 2013 13:17:21 -0700 Subject: [PATCH] add 'level' param to tree recursion --- interface/src/Application.cpp | 2 +- interface/src/Application.h | 2 +- interface/src/VoxelSystem.cpp | 28 +++++++++---------- interface/src/VoxelSystem.h | 24 ++++++++--------- libraries/voxels/src/ViewFrustum.cpp | 8 +++--- libraries/voxels/src/ViewFrustum.h | 2 +- libraries/voxels/src/VoxelTree.cpp | 40 ++++++++++++++++++---------- libraries/voxels/src/VoxelTree.h | 11 ++++---- voxel-edit/src/main.cpp | 2 +- 9 files changed, 67 insertions(+), 52 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 6254d38e22..66fc55c51c 100755 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1268,7 +1268,7 @@ struct SendVoxelsOperationArgs { }; -bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { +bool Application::sendVoxelsOperation(VoxelNode* node, int level, void* extraData) { SendVoxelsOperationArgs* args = (SendVoxelsOperationArgs*)extraData; if (node->isColored()) { unsigned char* nodeOctalCode = node->getOctalCode(); diff --git a/interface/src/Application.h b/interface/src/Application.h index 8d1cc276d4..fe53c6edfa 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -164,7 +164,7 @@ private: static void broadcastToNodes(unsigned char* data, size_t bytes, const char type); static void sendVoxelServerAddScene(); - static bool sendVoxelsOperation(VoxelNode* node, void* extraData); + static bool sendVoxelsOperation(VoxelNode* node, int level, void* extraData); static void sendAvatarVoxelURLMessage(const QUrl& url); static void processAvatarVoxelURLMessage(unsigned char *packetData, size_t dataBytes); static void sendVoxelEditMessage(PACKET_HEADER header, VoxelDetail& detail); diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 791729a442..0bc9b104be 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -701,7 +701,7 @@ void VoxelSystem::killLocalVoxels() { } -bool VoxelSystem::randomColorOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::randomColorOperation(VoxelNode* node, int level, void* extraData) { _nodeCount++; if (node->isColored()) { nodeColor newColor = { 255, randomColorValue(150), randomColorValue(150), 1 }; @@ -717,7 +717,7 @@ void VoxelSystem::randomizeVoxelColors() { setupNewVoxelsForDrawing(); } -bool VoxelSystem::falseColorizeRandomOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::falseColorizeRandomOperation(VoxelNode* node, int level, void* extraData) { _nodeCount++; // always false colorize node->setFalseColor(255, randomColorValue(150), randomColorValue(150)); @@ -731,7 +731,7 @@ void VoxelSystem::falseColorizeRandom() { setupNewVoxelsForDrawing(); } -bool VoxelSystem::trueColorizeOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::trueColorizeOperation(VoxelNode* node, int level, void* extraData) { _nodeCount++; node->setFalseColored(false); return true; @@ -746,7 +746,7 @@ void VoxelSystem::trueColorize() { } // Will false colorize voxels that are not in view -bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::falseColorizeInViewOperation(VoxelNode* node, int level, void* extraData) { const ViewFrustum* viewFrustum = (const ViewFrustum*) extraData; _nodeCount++; if (node->isColored()) { @@ -766,7 +766,7 @@ void VoxelSystem::falseColorizeInView(ViewFrustum* viewFrustum) { } // Will false colorize voxels based on distance from view -bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::falseColorizeDistanceFromViewOperation(VoxelNode* node, int level, void* extraData) { ViewFrustum* viewFrustum = (ViewFrustum*) extraData; if (node->isColored()) { float distance = node->distanceToCamera(*viewFrustum); @@ -789,7 +789,7 @@ float VoxelSystem::_minDistance = FLT_MAX; // Helper function will get the distance from view range, would be nice if you could just keep track // of this as voxels are created and/or colored... seems like some transform math could do that so // we wouldn't need to do two passes of the tree -bool VoxelSystem::getDistanceFromViewRangeOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::getDistanceFromViewRangeOperation(VoxelNode* node, int level, void* extraData) { ViewFrustum* viewFrustum = (ViewFrustum*) extraData; // only do this for truly colored voxels... if (node->isColored()) { @@ -842,7 +842,7 @@ public: // "Remove" voxels from the tree that are not in view. We don't actually delete them, // we remove them from the tree and place them into a holding area for later deletion -bool VoxelSystem::removeOutOfViewOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::removeOutOfViewOperation(VoxelNode* node, int level, void* extraData) { removeOutOfViewArgs* args = (removeOutOfViewArgs*)extraData; // If our node was previously added to the don't recurse bag, then return false to @@ -977,7 +977,7 @@ public: bool colorThis; }; -bool VoxelSystem::falseColorizeRandomEveryOtherOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::falseColorizeRandomEveryOtherOperation(VoxelNode* node, int level, void* extraData) { falseColorizeRandomEveryOtherArgs* args = (falseColorizeRandomEveryOtherArgs*)extraData; args->totalNodes++; if (node->isColored()) { @@ -1030,7 +1030,7 @@ public: bool hasIndexFound[MAX_VOXELS_PER_SYSTEM]; }; -bool VoxelSystem::collectStatsForTreesAndVBOsOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::collectStatsForTreesAndVBOsOperation(VoxelNode* node, int level, void* extraData) { collectStatsForTreesAndVBOsArgs* args = (collectStatsForTreesAndVBOsArgs*)extraData; args->totalNodes++; @@ -1182,7 +1182,7 @@ struct FalseColorizeSubTreeOperationArgs { long voxelsTouched; }; -bool VoxelSystem::falseColorizeSubTreeOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::falseColorizeSubTreeOperation(VoxelNode* node, int level, void* extraData) { if (node->getShouldRender()) { FalseColorizeSubTreeOperationArgs* args = (FalseColorizeSubTreeOperationArgs*) extraData; node->setFalseColor(args->color[0], args->color[1], args->color[2]); @@ -1191,7 +1191,7 @@ bool VoxelSystem::falseColorizeSubTreeOperation(VoxelNode* node, void* extraData return true; } -bool VoxelSystem::falseColorizeOccludedOperation(VoxelNode* node, void* extraData) { +bool VoxelSystem::falseColorizeOccludedOperation(VoxelNode* node, int level, void* extraData) { FalseColorizeOccludedArgs* args = (FalseColorizeOccludedArgs*) extraData; args->totalVoxels++; @@ -1228,7 +1228,7 @@ bool VoxelSystem::falseColorizeOccludedOperation(VoxelNode* node, void* extraDat subArgs.color[2] = 0; subArgs.voxelsTouched = 0; - args->tree->recurseNodeWithOperation(node, falseColorizeSubTreeOperation, &subArgs ); + args->tree->recurseNodeWithOperation(node, level, falseColorizeSubTreeOperation, &subArgs ); args->subtreeVoxelsSkipped += (subArgs.voxelsTouched - 1); args->totalVoxels += (subArgs.voxelsTouched - 1); @@ -1312,7 +1312,7 @@ void VoxelSystem::falseColorizeOccluded() { setupNewVoxelsForDrawing(); } -bool VoxelSystem::falseColorizeOccludedV2Operation(VoxelNode* node, void* extraData) { +bool VoxelSystem::falseColorizeOccludedV2Operation(VoxelNode* node, int level, void* extraData) { FalseColorizeOccludedArgs* args = (FalseColorizeOccludedArgs*) extraData; args->totalVoxels++; @@ -1349,7 +1349,7 @@ bool VoxelSystem::falseColorizeOccludedV2Operation(VoxelNode* node, void* extraD subArgs.color[2] = 0; subArgs.voxelsTouched = 0; - args->tree->recurseNodeWithOperation(node, falseColorizeSubTreeOperation, &subArgs ); + args->tree->recurseNodeWithOperation(node, level, falseColorizeSubTreeOperation, &subArgs ); args->subtreeVoxelsSkipped += (subArgs.voxelsTouched - 1); args->totalVoxels += (subArgs.voxelsTouched - 1); diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index 470ec79aa2..0b7b271411 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -120,18 +120,18 @@ private: bool _renderWarningsOn; // Operation functions for tree recursion methods static int _nodeCount; - static bool randomColorOperation(VoxelNode* node, void* extraData); - static bool falseColorizeRandomOperation(VoxelNode* node, void* extraData); - static bool trueColorizeOperation(VoxelNode* node, void* extraData); - static bool falseColorizeInViewOperation(VoxelNode* node, void* extraData); - static bool falseColorizeDistanceFromViewOperation(VoxelNode* node, void* extraData); - static bool getDistanceFromViewRangeOperation(VoxelNode* node, void* extraData); - static bool removeOutOfViewOperation(VoxelNode* node, void* extraData); - static bool falseColorizeRandomEveryOtherOperation(VoxelNode* node, void* extraData); - static bool collectStatsForTreesAndVBOsOperation(VoxelNode* node, void* extraData); - static bool falseColorizeOccludedOperation(VoxelNode* node, void* extraData); - static bool falseColorizeSubTreeOperation(VoxelNode* node, void* extraData); - static bool falseColorizeOccludedV2Operation(VoxelNode* node, void* extraData); + static bool randomColorOperation(VoxelNode* node, int level, void* extraData); + static bool falseColorizeRandomOperation(VoxelNode* node, int level, void* extraData); + static bool trueColorizeOperation(VoxelNode* node, int level, void* extraData); + static bool falseColorizeInViewOperation(VoxelNode* node, int level, void* extraData); + static bool falseColorizeDistanceFromViewOperation(VoxelNode* node, int level, void* extraData); + static bool getDistanceFromViewRangeOperation(VoxelNode* node, int level, void* extraData); + static bool removeOutOfViewOperation(VoxelNode* node, int level, void* extraData); + static bool falseColorizeRandomEveryOtherOperation(VoxelNode* node, int level, void* extraData); + static bool collectStatsForTreesAndVBOsOperation(VoxelNode* node, int level, void* extraData); + static bool falseColorizeOccludedOperation(VoxelNode* node, int level, void* extraData); + static bool falseColorizeSubTreeOperation(VoxelNode* node, int level, void* extraData); + static bool falseColorizeOccludedV2Operation(VoxelNode* node, int level, void* extraData); int updateNodeInArraysAsFullVBO(VoxelNode* node); diff --git a/libraries/voxels/src/ViewFrustum.cpp b/libraries/voxels/src/ViewFrustum.cpp index 7b7b0d2894..aaa03855d3 100644 --- a/libraries/voxels/src/ViewFrustum.cpp +++ b/libraries/voxels/src/ViewFrustum.cpp @@ -266,6 +266,7 @@ ViewFrustum::location ViewFrustum::sphereInFrustum(const glm::vec3& center, floa ViewFrustum::location ViewFrustum::boxInFrustum(const AABox& box) const { + ViewFrustum::location regularResult = INSIDE; ViewFrustum::location keyholeResult = OUTSIDE; @@ -274,15 +275,16 @@ ViewFrustum::location ViewFrustum::boxInFrustum(const AABox& box) const { keyholeResult = boxInKeyhole(box); } if (keyholeResult == INSIDE) { +//printLog("ViewFrustum::boxInFrustum() keyholeResult == INSIDE _keyholeRadius=%f\n",_keyholeRadius); return keyholeResult; } for(int i=0; i < 6; i++) { - glm::vec3 normal = _planes[i].getNormal(); - glm::vec3 boxVertexP = box.getVertexP(normal); + const glm::vec3& normal = _planes[i].getNormal(); + const glm::vec3& boxVertexP = box.getVertexP(normal); float planeToBoxVertexPDistance = _planes[i].distance(boxVertexP); - glm::vec3 boxVertexN = box.getVertexN(normal); + const glm::vec3& boxVertexN = box.getVertexN(normal); float planeToBoxVertexNDistance = _planes[i].distance(boxVertexN); if (planeToBoxVertexPDistance < 0) { diff --git a/libraries/voxels/src/ViewFrustum.h b/libraries/voxels/src/ViewFrustum.h index 6833eb6134..7308d5bb64 100644 --- a/libraries/voxels/src/ViewFrustum.h +++ b/libraries/voxels/src/ViewFrustum.h @@ -17,7 +17,7 @@ #include "AABox.h" #include "VoxelProjectedPolygon.h" -const float DEFAULT_KEYHOLE_RADIUS = 2.0f; +const float DEFAULT_KEYHOLE_RADIUS = 3.0f; class ViewFrustum { public: diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 083765aa0b..604e50f1d3 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -28,11 +28,16 @@ #include -int boundaryDistanceForRenderLevel(unsigned int renderLevel) { - float voxelSizeScale = 50000.0f; +float boundaryDistanceForRenderLevel(unsigned int renderLevel) { + const float voxelSizeScale = 50000.0f; return voxelSizeScale / powf(2, renderLevel); } +float boundaryDistanceSquaredForRenderLevel(unsigned int renderLevel) { + const float voxelSizeScale = (50000.0f/TREE_SCALE) * (50000.0f/TREE_SCALE); + return voxelSizeScale / powf(2, (2 * renderLevel)); +} + VoxelTree::VoxelTree(bool shouldReaverage) : voxelsCreated(0), voxelsColored(0), @@ -56,16 +61,19 @@ VoxelTree::~VoxelTree() { // Recurses voxel tree calling the RecurseVoxelTreeOperation function for each node. // stops recursion if operation function returns false. void VoxelTree::recurseTreeWithOperation(RecurseVoxelTreeOperation operation, void* extraData) { - recurseNodeWithOperation(rootNode, operation, extraData); + int level = 0; + recurseNodeWithOperation(rootNode, level, operation, extraData); } // Recurses voxel node with an operation function -void VoxelTree::recurseNodeWithOperation(VoxelNode* node,RecurseVoxelTreeOperation operation, void* extraData) { - if (operation(node, extraData)) { +void VoxelTree::recurseNodeWithOperation(VoxelNode* node, int& level, RecurseVoxelTreeOperation operation, void* extraData) { + if (operation(node, level, extraData)) { for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { VoxelNode* child = node->getChildAtIndex(i); if (child) { - recurseNodeWithOperation(child, operation, extraData); + level++; + recurseNodeWithOperation(child, level, operation, extraData); + level--; } } } @@ -75,13 +83,15 @@ void VoxelTree::recurseNodeWithOperation(VoxelNode* node,RecurseVoxelTreeOperati // stops recursion if operation function returns false. void VoxelTree::recurseTreeWithOperationDistanceSorted(RecurseVoxelTreeOperation operation, const glm::vec3& point, void* extraData) { - recurseNodeWithOperationDistanceSorted(rootNode, operation, point, extraData); + + int level = 0; + recurseNodeWithOperationDistanceSorted(rootNode, level, operation, point, extraData); } // Recurses voxel node with an operation function -void VoxelTree::recurseNodeWithOperationDistanceSorted(VoxelNode* node, RecurseVoxelTreeOperation operation, +void VoxelTree::recurseNodeWithOperationDistanceSorted(VoxelNode* node, int& level, RecurseVoxelTreeOperation operation, const glm::vec3& point, void* extraData) { - if (operation(node, extraData)) { + if (operation(node, level, extraData)) { // determine the distance sorted order of our children VoxelNode* sortedChildren[NUMBER_OF_CHILDREN]; @@ -107,7 +117,9 @@ void VoxelTree::recurseNodeWithOperationDistanceSorted(VoxelNode* node, RecurseV if (childNode) { //printLog("recurseNodeWithOperationDistanceSorted() PROCESSING child[%d] distance=%f...\n", i, distancesToChildren[i]); //childNode->printDebugDetails(""); - recurseNodeWithOperationDistanceSorted(childNode, operation, point, extraData); + level++; + recurseNodeWithOperationDistanceSorted(childNode, level, operation, point, extraData); + level--; } } } @@ -852,7 +864,7 @@ public: bool found; }; -bool findRayIntersectionOp(VoxelNode* node, void* extraData) { +bool findRayIntersectionOp(VoxelNode* node, int level, void* extraData) { RayArgs* args = static_cast(extraData); AABox box = node->getAABox(); float distance; @@ -888,7 +900,7 @@ public: bool found; }; -bool findSpherePenetrationOp(VoxelNode* node, void* extraData) { +bool findSpherePenetrationOp(VoxelNode* node, int level, void* extraData) { SphereArgs* args = static_cast(extraData); // coarse check against bounds @@ -925,7 +937,7 @@ public: bool found; }; -bool findCapsulePenetrationOp(VoxelNode* node, void* extraData) { +bool findCapsulePenetrationOp(VoxelNode* node, int level, void* extraData) { CapsuleArgs* args = static_cast(extraData); // coarse check against bounds @@ -1493,7 +1505,7 @@ unsigned long VoxelTree::getVoxelCount() { return nodeCount; } -bool VoxelTree::countVoxelsOperation(VoxelNode* node, void* extraData) { +bool VoxelTree::countVoxelsOperation(VoxelNode* node, int level, void* extraData) { (*(unsigned long*)extraData)++; return true; // keep going } diff --git a/libraries/voxels/src/VoxelTree.h b/libraries/voxels/src/VoxelTree.h index 3d87fbc557..6d718d4cc9 100644 --- a/libraries/voxels/src/VoxelTree.h +++ b/libraries/voxels/src/VoxelTree.h @@ -16,7 +16,7 @@ #include "CoverageMap.h" // Callback function, for recuseTreeWithOperation -typedef bool (*RecurseVoxelTreeOperation)(VoxelNode* node, void* extraData); +typedef bool (*RecurseVoxelTreeOperation)(VoxelNode* node, int level, void* extraData); typedef enum {GRADIENT, RANDOM, NATURAL} creationMode; #define NO_EXISTS_BITS false @@ -144,8 +144,8 @@ public: bool getShouldReaverage() const { return _shouldReaverage; } - void recurseNodeWithOperation(VoxelNode* node, RecurseVoxelTreeOperation operation, void* extraData); - void recurseNodeWithOperationDistanceSorted(VoxelNode* node, RecurseVoxelTreeOperation operation, + void recurseNodeWithOperation(VoxelNode* node, int& level, RecurseVoxelTreeOperation operation, void* extraData); + void recurseNodeWithOperationDistanceSorted(VoxelNode* node, int& level, RecurseVoxelTreeOperation operation, const glm::vec3& point, void* extraData); private: @@ -159,7 +159,7 @@ private: VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, bool deltaViewFrustum, const ViewFrustum* lastViewFrustum); - static bool countVoxelsOperation(VoxelNode* node, void* extraData); + static bool countVoxelsOperation(VoxelNode* node, int level, void* extraData); VoxelNode* nodeForOctalCode(VoxelNode* ancestorNode, unsigned char* needleCode, VoxelNode** parentOfFoundNode) const; VoxelNode* createMissingNode(VoxelNode* lastParentNode, unsigned char* deepestCodeToCreate); @@ -171,6 +171,7 @@ private: bool _shouldReaverage; }; -int boundaryDistanceForRenderLevel(unsigned int renderLevel); +float boundaryDistanceForRenderLevel(unsigned int renderLevel); +float boundaryDistanceSquaredForRenderLevel(unsigned int renderLevel); #endif /* defined(__hifi__VoxelTree__) */ diff --git a/voxel-edit/src/main.cpp b/voxel-edit/src/main.cpp index 9571e4adf5..a2d9d80292 100644 --- a/voxel-edit/src/main.cpp +++ b/voxel-edit/src/main.cpp @@ -13,7 +13,7 @@ VoxelTree myTree; int _nodeCount=0; -bool countVoxelsOperation(VoxelNode* node, void* extraData) { +bool countVoxelsOperation(VoxelNode* node, int level, void* extraData) { if (node->isColored()){ _nodeCount++; }