From 9d0ff1595af462cfd08bce7584ac20e42d653f51 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 15 Oct 2013 13:33:38 -0700 Subject: [PATCH] make voxelnode population statistics static members instead of per node --- .../src/VoxelPersistThread.cpp | 7 +-- .../voxel-server-library/src/VoxelServer.cpp | 7 ++- libraries/voxels/src/VoxelNode.cpp | 47 +++++++++++-------- libraries/voxels/src/VoxelNode.h | 13 ++--- libraries/voxels/src/VoxelSceneStats.cpp | 7 +-- libraries/voxels/src/VoxelTree.cpp | 3 -- 6 files changed, 45 insertions(+), 39 deletions(-) diff --git a/libraries/voxel-server-library/src/VoxelPersistThread.cpp b/libraries/voxel-server-library/src/VoxelPersistThread.cpp index c77ff44755..ee8147eaad 100644 --- a/libraries/voxel-server-library/src/VoxelPersistThread.cpp +++ b/libraries/voxel-server-library/src/VoxelPersistThread.cpp @@ -42,9 +42,10 @@ bool VoxelPersistThread::process() { _tree->clearDirtyBit(); // the tree is clean since we just loaded it qDebug("DONE loading voxels from file... fileRead=%s\n", debug::valueOf(persistantFileRead)); - unsigned long nodeCount = _tree->rootNode->getSubTreeNodeCount(); - unsigned long internalNodeCount = _tree->rootNode->getSubTreeInternalNodeCount(); - unsigned long leafNodeCount = _tree->rootNode->getSubTreeLeafNodeCount(); + + unsigned long nodeCount = VoxelNode::getNodeCount(); + unsigned long internalNodeCount = VoxelNode::getInternalNodeCount(); + unsigned long leafNodeCount = VoxelNode::getLeafNodeCount(); qDebug("Nodes after loading scene %lu nodes %lu internal %lu leaves\n", nodeCount, internalNodeCount, leafNodeCount); } diff --git a/libraries/voxel-server-library/src/VoxelServer.cpp b/libraries/voxel-server-library/src/VoxelServer.cpp index f02069af2e..d261f0348f 100644 --- a/libraries/voxel-server-library/src/VoxelServer.cpp +++ b/libraries/voxel-server-library/src/VoxelServer.cpp @@ -139,10 +139,9 @@ int VoxelServer::civetwebRequestHandler(struct mg_connection* connection) { mg_printf(connection, "Voxel Node Memory Usage: %f MB\r\n", VoxelNode::getVoxelMemoryUsage() / 1000000.f); mg_printf(connection, "Octcode Memory Usage: %f MB\r\n", VoxelNode::getOctcodeMemoryUsage() / 1000000.f); - VoxelTree* theTree = VoxelServer::GetInstance()->getTree(); - unsigned long nodeCount = theTree->rootNode->getSubTreeNodeCount(); - unsigned long internalNodeCount = theTree->rootNode->getSubTreeInternalNodeCount(); - unsigned long leafNodeCount = theTree->rootNode->getSubTreeLeafNodeCount(); + unsigned long nodeCount = VoxelNode::getNodeCount(); + unsigned long internalNodeCount = VoxelNode::getInternalNodeCount(); + unsigned long leafNodeCount = VoxelNode::getLeafNodeCount(); mg_printf(connection, "%s", "Current Nodes in scene\r\n"); mg_printf(connection, " Total Nodes: %lu nodes\r\n", nodeCount); diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index c7b5c9e7e6..af9fa0a117 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -23,15 +23,22 @@ uint64_t VoxelNode::_voxelMemoryUsage = 0; uint64_t VoxelNode::_octcodeMemoryUsage = 0; +uint64_t VoxelNode::_voxelNodeCount = 0; +uint64_t VoxelNode::_voxelNodeLeafCount = 0; VoxelNode::VoxelNode() { unsigned char* rootCode = new unsigned char[1]; *rootCode = 0; init(rootCode); + + _voxelNodeCount++; + _voxelNodeLeafCount++; // all nodes start as leaf nodes } VoxelNode::VoxelNode(unsigned char * octalCode) { init(octalCode); + _voxelNodeCount++; + _voxelNodeLeafCount++; // all nodes start as leaf nodes } void VoxelNode::init(unsigned char * octalCode) { @@ -49,8 +56,6 @@ void VoxelNode::init(unsigned char * octalCode) { _children[i] = NULL; } _childCount = 0; - _subtreeNodeCount = 1; // that's me - _subtreeLeafNodeCount = 0; // that's me _glBufferIndex = GLBUFFER_INDEX_UNKNOWN; _voxelSystem = NULL; @@ -70,6 +75,11 @@ VoxelNode::~VoxelNode() { _voxelMemoryUsage -= sizeof(VoxelNode); _octcodeMemoryUsage -= bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(getOctalCode())); + _voxelNodeCount--; + if (isLeaf()) { + _voxelNodeLeafCount--; + } + delete[] _octalCode; // delete all of this node's children @@ -97,27 +107,9 @@ void VoxelNode::handleSubtreeChanged(VoxelTree* myTree) { setColorFromAverageOfChildren(); } - recalculateSubTreeNodeCount(); markWithChangedTime(); } -void VoxelNode::recalculateSubTreeNodeCount() { - // Assuming the tree below me as changed, I need to recalculate my node count - _subtreeNodeCount = 1; // that's me - if (isLeaf()) { - _subtreeLeafNodeCount = 1; - } else { - _subtreeLeafNodeCount = 0; - for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { - if (_children[i]) { - _subtreeNodeCount += _children[i]->_subtreeNodeCount; - _subtreeLeafNodeCount += _children[i]->_subtreeLeafNodeCount; - } - } - } -} - - void VoxelNode::setShouldRender(bool shouldRender) { // if shouldRender is changing, then consider ourselves dirty if (shouldRender != _shouldRender) { @@ -145,6 +137,11 @@ void VoxelNode::deleteChildAtIndex(int childIndex) { _isDirty = true; _childCount--; markWithChangedTime(); + + // after deleting the child, check to see if we're a leaf + if (isLeaf()) { + _voxelNodeLeafCount++; + } } } @@ -156,12 +153,22 @@ VoxelNode* VoxelNode::removeChildAtIndex(int childIndex) { _isDirty = true; _childCount--; markWithChangedTime(); + + // after removing the child, check to see if we're a leaf + if (isLeaf()) { + _voxelNodeLeafCount++; + } } return returnedChild; } VoxelNode* VoxelNode::addChildAtIndex(int childIndex) { if (!_children[childIndex]) { + // before adding a child, see if we're currently a leaf + if (isLeaf()) { + _voxelNodeLeafCount--; + } + _children[childIndex] = new VoxelNode(childOctalCode(getOctalCode(), childIndex)); _children[childIndex]->setVoxelSystem(_voxelSystem); // our child is always part of our voxel system NULL ok _isDirty = true; diff --git a/libraries/voxels/src/VoxelNode.h b/libraries/voxels/src/VoxelNode.h index 2fe3507669..3b36b2d3e9 100644 --- a/libraries/voxels/src/VoxelNode.h +++ b/libraries/voxels/src/VoxelNode.h @@ -121,10 +121,9 @@ public: static void addUpdateHook(VoxelNodeUpdateHook* hook); static void removeUpdateHook(VoxelNodeUpdateHook* hook); - void recalculateSubTreeNodeCount(); - unsigned long getSubTreeNodeCount() const { return _subtreeNodeCount; } - unsigned long getSubTreeInternalNodeCount() const { return _subtreeNodeCount - _subtreeLeafNodeCount; } - unsigned long getSubTreeLeafNodeCount() const { return _subtreeLeafNodeCount; } + static unsigned long getNodeCount() { return _voxelNodeCount; } + static unsigned long getInternalNodeCount() { return _voxelNodeCount - _voxelNodeLeafCount; } + static unsigned long getLeafNodeCount() { return _voxelNodeLeafCount; } static uint64_t getVoxelMemoryUsage() { return _voxelMemoryUsage; } static uint64_t getOctcodeMemoryUsage() { return _octcodeMemoryUsage; } @@ -140,8 +139,6 @@ private: unsigned char* _octalCode; /// Client and server, pointer to octal code for this node, 8 bytes uint64_t _lastChanged; /// Client and server, timestamp this node was last changed, 8 bytes - unsigned long _subtreeNodeCount; /// Client and server, nodes below this node, 8 bytes - unsigned long _subtreeLeafNodeCount; /// Client and server, leaves below this node, 8 bytes glBufferIndex _glBufferIndex; /// Client only, vbo index for this voxel if being rendered, 8 bytes VoxelSystem* _voxelSystem; /// Client only, pointer to VoxelSystem rendering this voxel, 8 bytes @@ -162,6 +159,10 @@ private: static std::vector _deleteHooks; static std::vector _updateHooks; + + static uint64_t _voxelNodeCount; + static uint64_t _voxelNodeLeafCount; + static uint64_t _voxelMemoryUsage; static uint64_t _octcodeMemoryUsage; }; diff --git a/libraries/voxels/src/VoxelSceneStats.cpp b/libraries/voxels/src/VoxelSceneStats.cpp index 1ebc4015e0..f4570d93a2 100644 --- a/libraries/voxels/src/VoxelSceneStats.cpp +++ b/libraries/voxels/src/VoxelSceneStats.cpp @@ -33,9 +33,10 @@ void VoxelSceneStats::sceneStarted(bool isFullScene, bool isMoving, VoxelNode* r reset(); // resets packet and voxel stats _isStarted = true; _start = usecTimestampNow(); - _totalVoxels = root->getSubTreeNodeCount(); - _totalInternal = root->getSubTreeInternalNodeCount(); - _totalLeaves = root->getSubTreeLeafNodeCount(); + + _totalVoxels = VoxelNode::getNodeCount(); + _totalInternal = VoxelNode::getInternalNodeCount(); + _totalLeaves = VoxelNode::getLeafNodeCount(); _isFullScene = isFullScene; _isMoving = isMoving; diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 3b79722686..ff51cd3044 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -647,9 +647,6 @@ void VoxelTree::reaverageVoxelColors(VoxelNode *startNode) { if (hasChildren && !startNode->collapseIdenticalLeaves()) { startNode->setColorFromAverageOfChildren(); } - - // this is also a good time to recalculateSubTreeNodeCount() - startNode->recalculateSubTreeNodeCount(); } }