diff --git a/shared/src/VoxelTree.cpp b/shared/src/VoxelTree.cpp index 36eff8aa21..e119cb7aac 100644 --- a/shared/src/VoxelTree.cpp +++ b/shared/src/VoxelTree.cpp @@ -52,7 +52,7 @@ VoxelTree::~VoxelTree() { } } -VoxelNode * VoxelTree::nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * needleCode) { +VoxelNode * VoxelTree::nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * needleCode, VoxelNode** parentOfFoundNode) { // find the appropriate branch index based on this ancestorNode if (*needleCode > 0) { int branchForNeedle = branchIndexWithDescendant(ancestorNode->octalCode, needleCode); @@ -60,13 +60,18 @@ VoxelNode * VoxelTree::nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * if (childNode != NULL) { if (*childNode->octalCode == *needleCode) { + + // If the caller asked for the parent, then give them that too... + if (parentOfFoundNode) { + *parentOfFoundNode=ancestorNode; + } // the fact that the number of sections is equivalent does not always guarantee // that this is the same node, however due to the recursive traversal // we know that this is our node return childNode; } else { // we need to go deeper - return nodeForOctalCode(childNode, needleCode); + return nodeForOctalCode(childNode, needleCode,parentOfFoundNode); } } } @@ -148,7 +153,7 @@ int VoxelTree::readNodeData(VoxelNode *destinationNode, } void VoxelTree::readBitstreamToTree(unsigned char * bitstream, int bufferSizeBytes) { - VoxelNode *bitstreamRootNode = nodeForOctalCode(rootNode, (unsigned char *)bitstream); + VoxelNode *bitstreamRootNode = nodeForOctalCode(rootNode, (unsigned char *)bitstream, NULL); if (*bitstream != *bitstreamRootNode->octalCode) { // if the octal code returned is not on the same level as @@ -160,8 +165,23 @@ void VoxelTree::readBitstreamToTree(unsigned char * bitstream, int bufferSizeByt readNodeData(bitstreamRootNode, bitstream + octalCodeBytes, bufferSizeBytes - octalCodeBytes); } +// Note: uses the codeColorBuffer format, but the color's are ignored, because +// this only finds and deletes the node from the tree. +void VoxelTree::deleteVoxelCodeFromTree(unsigned char *codeBuffer) { + VoxelNode* parentNode = NULL; + VoxelNode* nodeToDelete = nodeForOctalCode(rootNode, codeBuffer, &parentNode); + // If the node exists... + if (*nodeToDelete->octalCode == *codeBuffer) { + if (parentNode) { + int childNDX = branchIndexWithDescendant(parentNode->octalCode, codeBuffer); + delete parentNode->children[childNDX]; // delete the child nodes + parentNode->children[childNDX]=NULL; // set it to NULL + } + } +} + void VoxelTree::readCodeColorBufferToTree(unsigned char *codeColorBuffer) { - VoxelNode *lastCreatedNode = nodeForOctalCode(rootNode, codeColorBuffer); + VoxelNode *lastCreatedNode = nodeForOctalCode(rootNode, codeColorBuffer, NULL); // create the node if it does not exist if (*lastCreatedNode->octalCode != *codeColorBuffer) { diff --git a/shared/src/VoxelTree.h b/shared/src/VoxelTree.h index a265b9fa9e..1c19505da1 100644 --- a/shared/src/VoxelTree.h +++ b/shared/src/VoxelTree.h @@ -18,7 +18,7 @@ const int MAX_TREE_SLICE_BYTES = 26; const int TREE_SCALE = 10; class VoxelTree { - VoxelNode * nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * needleCode); + VoxelNode * nodeForOctalCode(VoxelNode *ancestorNode, unsigned char * needleCode, VoxelNode** parentOfFoundNode); VoxelNode * createMissingNode(VoxelNode *lastParentNode, unsigned char *deepestCodeToCreate); int readNodeData(VoxelNode *destinationNode, unsigned char * nodeData, int bufferSizeBytes); public: @@ -30,6 +30,7 @@ public: void readBitstreamToTree(unsigned char * bitstream, int bufferSizeBytes); void readCodeColorBufferToTree(unsigned char *codeColorBuffer); + void deleteVoxelCodeFromTree(unsigned char *codeBuffer); void printTreeForDebugging(VoxelNode *startNode); void reaverageVoxelColors(VoxelNode *startNode); unsigned char * loadBitstreamBuffer(unsigned char *& bitstreamBuffer, diff --git a/voxel/src/main.cpp b/voxel/src/main.cpp index 287adfbe10..db79ddff65 100644 --- a/voxel/src/main.cpp +++ b/voxel/src/main.cpp @@ -273,12 +273,17 @@ int main(int argc, const char * argv[]) // XXXBHG: Hacked in support for 'I' insert command if (packetData[0] == 'I') { unsigned short int itemNumber = (*((unsigned short int*)&packetData[1])); - printf("got I command from client receivedBytes=%ld itemNumber=%d\n",receivedBytes,itemNumber); + printf("got I - insert voxels - command from client receivedBytes=%ld itemNumber=%d\n",receivedBytes,itemNumber); int atByte = 3; unsigned char* pVoxelData = (unsigned char*)&packetData[3]; while (atByte < receivedBytes) { unsigned char octets = (unsigned char)*pVoxelData; int voxelDataSize = bytesRequiredForCodeLength(octets)+3; // 3 for color! + + float* vertices = firstVertexForCode(pVoxelData); + printf("inserting voxel at: %f,%f,%f\n",vertices[0],vertices[1],vertices[2]); + delete []vertices; + randomTree.readCodeColorBufferToTree(pVoxelData); //printf("readCodeColorBufferToTree() of size=%d atByte=%d receivedBytes=%ld\n",voxelDataSize,atByte,receivedBytes); // skip to next @@ -286,6 +291,28 @@ int main(int argc, const char * argv[]) atByte+=voxelDataSize; } } + if (packetData[0] == 'R') { + unsigned short int itemNumber = (*((unsigned short int*)&packetData[1])); + printf("got R - remove voxels - command from client receivedBytes=%ld itemNumber=%d\n",receivedBytes,itemNumber); + int atByte = 3; + unsigned char* pVoxelData = (unsigned char*)&packetData[3]; + while (atByte < receivedBytes) { + unsigned char octets = (unsigned char)*pVoxelData; + int voxelDataSize = bytesRequiredForCodeLength(octets)+3; // 3 for color! + + float* vertices = firstVertexForCode(pVoxelData); + printf("deleting voxel at: %f,%f,%f\n",vertices[0],vertices[1],vertices[2]); + delete []vertices; + + randomTree.deleteVoxelCodeFromTree(pVoxelData); + + + //printf("readCodeColorBufferToTree() of size=%d atByte=%d receivedBytes=%ld\n",voxelDataSize,atByte,receivedBytes); + // skip to next + pVoxelData+=voxelDataSize; + atByte+=voxelDataSize; + } + } if (packetData[0] == 'H') { if (agentList.addOrUpdateAgent(&agentPublicAddress, &agentPublicAddress,