From 769383582b7638f4773df9e6deebd260fc1fa562 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Sat, 8 Jun 2013 14:24:29 -0700 Subject: [PATCH] Adding density to nodes --- libraries/voxels/src/VoxelNode.cpp | 26 ++++++++++++++++++++++---- libraries/voxels/src/VoxelNode.h | 4 ++++ voxel-server/src/main.cpp | 19 +++++++++---------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/libraries/voxels/src/VoxelNode.cpp b/libraries/voxels/src/VoxelNode.cpp index bd8feadb35..f5d175fadb 100644 --- a/libraries/voxels/src/VoxelNode.cpp +++ b/libraries/voxels/src/VoxelNode.cpp @@ -35,7 +35,7 @@ void VoxelNode::init(unsigned char * octalCode) { _currentColor[0] = _currentColor[1] = _currentColor[2] = _currentColor[3] = 0; #endif _trueColor[0] = _trueColor[1] = _trueColor[2] = _trueColor[3] = 0; - + _density = 0.0f; // default pointers to child nodes to NULL for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { @@ -165,6 +165,7 @@ void VoxelNode::safeDeepDeleteChildAtIndex(int childIndex, bool& stagedForDeleti // will average the child colors... void VoxelNode::setColorFromAverageOfChildren() { int colorArray[4] = {0,0,0,0}; + float density = 0.0f; for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { if (_children[i] && !_children[i]->isStagedForDeletion() && _children[i]->isColored()) { for (int j = 0; j < 3; j++) { @@ -172,11 +173,27 @@ void VoxelNode::setColorFromAverageOfChildren() { } colorArray[3]++; } + if (_children[i]) { + density += _children[i]->getDensity(); + } } + if (_childCount > 0) { + // If there are children, density is sum of child densities + density /= (float) NUMBER_OF_CHILDREN; + } else { + // If there are no children + density = 1.f; + } + + if (colorArray[3] > 0) { + printf("children: %d, density = %4.2f\n", colorArray[3], density); + } + + const float VISIBLE_ABOVE_DENSITY = 0.0f; nodeColor newColor = { 0, 0, 0, 0}; - if (colorArray[3] > 4) { - // we need at least 4 colored children to have an average color value - // or if we have none we generate random values + // if (density > VISIBLE_ABOVE_DENSITY) { + if (colorArray[3] > 0) { + // The density of material in the space of the voxel sets whether it is actually colored for (int c = 0; c < 3; c++) { // set the average color value newColor[c] = colorArray[c] / colorArray[3]; @@ -188,6 +205,7 @@ void VoxelNode::setColorFromAverageOfChildren() { // this will be the default value all zeros, and therefore be marked as // transparent with a 4th element of 0 setColor(newColor); + setDensity(density); } // Note: !NO_FALSE_COLOR implementations of setFalseColor(), setFalseColored(), and setColor() here. diff --git a/libraries/voxels/src/VoxelNode.h b/libraries/voxels/src/VoxelNode.h index 3fd8e67e9c..952ad15bb7 100644 --- a/libraries/voxels/src/VoxelNode.h +++ b/libraries/voxels/src/VoxelNode.h @@ -36,6 +36,7 @@ private: unsigned char* _octalCode; VoxelNode* _children[8]; int _childCount; + float _density; // If leaf: density = 1, if internal node: 0-1 density of voxels inside void calculateAABox(); @@ -102,11 +103,14 @@ public: void setColor(const nodeColor& color); const nodeColor& getTrueColor() const { return _trueColor; }; const nodeColor& getColor() const { return _currentColor; }; + void setDensity(const float density) { _density = density; }; + const float getDensity() const { return _density; }; #else void setFalseColor(colorPart red, colorPart green, colorPart blue) { /* no op */ }; void setFalseColored(bool isFalseColored) { /* no op */ }; bool getFalseColored() { return false; }; void setColor(const nodeColor& color) { memcpy(_trueColor,color,sizeof(nodeColor)); }; + void setDensity(const float density) { _density = density; }; const nodeColor& getTrueColor() const { return _trueColor; }; const nodeColor& getColor() const { return _trueColor; }; #endif diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index 81a92d4d07..44dbde76c9 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -387,16 +387,6 @@ void persistVoxelsWhenDirty() { // check the dirty bit and persist here... if (::wantVoxelPersist && ::serverTree.isDirty() && sinceLastTime > VOXEL_PERSIST_INTERVAL) { - - { - PerformanceWarning warn(::shouldShowAnimationDebug, - "persistVoxelsWhenDirty() - reaverageVoxelColors()", ::shouldShowAnimationDebug); - - // after done inserting all these voxels, then reaverage colors - serverTree.reaverageVoxelColors(serverTree.rootNode); - } - - { PerformanceWarning warn(::shouldShowAnimationDebug, "persistVoxelsWhenDirty() - writeToSVOFile()", ::shouldShowAnimationDebug); @@ -506,6 +496,15 @@ int main(int argc, const char * argv[]) { if (::wantVoxelPersist) { printf("loading voxels from file...\n"); persistantFileRead = ::serverTree.readFromSVOFile(::wantLocalDomain ? LOCAL_VOXELS_PERSIST_FILE : VOXELS_PERSIST_FILE); + if (persistantFileRead) { + PerformanceWarning warn(::shouldShowAnimationDebug, + "persistVoxelsWhenDirty() - reaverageVoxelColors()", ::shouldShowAnimationDebug); + + // after done inserting all these voxels, then reaverage colors + serverTree.reaverageVoxelColors(serverTree.rootNode); + printf("Voxels reAveraged\n"); + } + ::serverTree.clearDirtyBit(); // the tree is clean since we just loaded it printf("DONE loading voxels from file... fileRead=%s\n", debug::valueOf(persistantFileRead)); unsigned long nodeCount = ::serverTree.getVoxelCount();