From ae3db89f63005a902f239159b1cb5b10e9556f52 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 6 Jun 2013 10:55:11 -0700 Subject: [PATCH] added method for general subtree change bookkeeping --- libraries/voxels/src/VoxelNode.cpp | 17 +++++++++++++++++ libraries/voxels/src/VoxelNode.h | 3 +++ libraries/voxels/src/VoxelTree.cpp | 10 ++++++---- libraries/voxels/src/VoxelTree.h | 2 ++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index b5c5b10cac..bd8feadb35 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -12,6 +12,7 @@ #include "SharedUtil.h" #include "Log.h" #include "VoxelNode.h" +#include "VoxelTree.h" #include "VoxelConstants.h" #include "OctalCode.h" #include "AABox.h" @@ -61,6 +62,22 @@ VoxelNode::~VoxelNode() { } } +// This method is called by VoxelTree when the subtree below this node +// is known to have changed. It's intended to be used as a place to do +// bookkeeping that a node may need to do when the subtree below it has +// changed. However, you should hopefully make your bookkeeping relatively +// localized, because this method will get called for every node in an +// recursive unwinding case like delete or add voxel +void VoxelNode::handleSubtreeChanged(VoxelTree* myTree) { + markWithChangedTime(); + + // here's a good place to do color re-averaging... + if (myTree->getShouldReaverage()) { + setColorFromAverageOfChildren(); + } +} + + void VoxelNode::setShouldRender(bool shouldRender) { // if shouldRender is changing, then consider ourselves dirty if (shouldRender != _shouldRender) { diff --git a/libraries/voxels/src/VoxelNode.h b/libraries/voxels/src/VoxelNode.h index 997c495693..3fd8e67e9c 100644 --- a/libraries/voxels/src/VoxelNode.h +++ b/libraries/voxels/src/VoxelNode.h @@ -14,6 +14,8 @@ #include "ViewFrustum.h" #include "VoxelConstants.h" +class VoxelTree; // forward delclaration + typedef unsigned char colorPart; typedef unsigned char nodeColor[4]; typedef unsigned char rgbColor[3]; @@ -79,6 +81,7 @@ public: void clearDirtyBit() { _isDirty = false; }; bool hasChangedSince(double time) const { return (_lastChanged > time); }; void markWithChangedTime() { _lastChanged = usecTimestampNow(); }; + void handleSubtreeChanged(VoxelTree* myTree); glBufferIndex getBufferIndex() const { return _glBufferIndex; }; bool isKnownBufferIndex() const { return (_glBufferIndex != GLBUFFER_INDEX_UNKNOWN); }; diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 30c61fe4a1..ab724d92a9 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -388,9 +388,10 @@ void VoxelTree::deleteVoxelCodeFromTreeRecursion(VoxelNode* node, void* extraDat } } - // If the lower level did some work, then we need to track our lastChanged status. + // If the lower level did some work, then we need to let this node know, so it can + // do any bookkeeping it wants to, like color re-averaging, time stamp marking, etc if (args->pathChanged) { - node->markWithChangedTime(); + node->handleSubtreeChanged(this); } } @@ -482,9 +483,10 @@ void VoxelTree::readCodeColorBufferToTreeRecursion(VoxelNode* node, void* extraD // Unwinding... - // If the lower level did some work, then we need to track our lastChanged status. + // If the lower level did some work, then we need to let this node know, so it can + // do any bookkeeping it wants to, like color re-averaging, time stamp marking, etc if (args->pathChanged) { - node->markWithChangedTime(); + node->handleSubtreeChanged(this); } } diff --git a/libraries/voxels/src/VoxelTree.h b/libraries/voxels/src/VoxelTree.h index 1158596527..a238cb603a 100644 --- a/libraries/voxels/src/VoxelTree.h +++ b/libraries/voxels/src/VoxelTree.h @@ -99,6 +99,8 @@ public: void copySubTreeIntoNewTree(VoxelNode* startNode, VoxelTree* destinationTree, bool rebaseToRoot); void copyFromTreeIntoSubTree(VoxelTree* sourceTree, VoxelNode* destinationNode); + bool getShouldReaverage() const { return _shouldReaverage; } + private: void deleteVoxelCodeFromTreeRecursion(VoxelNode* node, void* extraData); void readCodeColorBufferToTreeRecursion(VoxelNode* node, void* extraData);