Added deleteVoxelCodeFromTree() and 'R' command to voxel server

Also added extra optional parameter to nodeForOctalCode() to return the parent node on node lookup
This commit is contained in:
ZappoMan 2013-04-02 14:26:54 -07:00
parent 9851a9b0f5
commit 804943e2fb
3 changed files with 54 additions and 6 deletions

View file

@ -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 // find the appropriate branch index based on this ancestorNode
if (*needleCode > 0) { if (*needleCode > 0) {
int branchForNeedle = branchIndexWithDescendant(ancestorNode->octalCode, needleCode); int branchForNeedle = branchIndexWithDescendant(ancestorNode->octalCode, needleCode);
@ -60,13 +60,18 @@ VoxelNode * VoxelTree::nodeForOctalCode(VoxelNode *ancestorNode, unsigned char *
if (childNode != NULL) { if (childNode != NULL) {
if (*childNode->octalCode == *needleCode) { 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 // 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 // that this is the same node, however due to the recursive traversal
// we know that this is our node // we know that this is our node
return childNode; return childNode;
} else { } else {
// we need to go deeper // 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) { 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 (*bitstream != *bitstreamRootNode->octalCode) {
// if the octal code returned is not on the same level as // 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); 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) { 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 // create the node if it does not exist
if (*lastCreatedNode->octalCode != *codeColorBuffer) { if (*lastCreatedNode->octalCode != *codeColorBuffer) {

View file

@ -18,7 +18,7 @@ const int MAX_TREE_SLICE_BYTES = 26;
const int TREE_SCALE = 10; const int TREE_SCALE = 10;
class VoxelTree { 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); VoxelNode * createMissingNode(VoxelNode *lastParentNode, unsigned char *deepestCodeToCreate);
int readNodeData(VoxelNode *destinationNode, unsigned char * nodeData, int bufferSizeBytes); int readNodeData(VoxelNode *destinationNode, unsigned char * nodeData, int bufferSizeBytes);
public: public:
@ -30,6 +30,7 @@ public:
void readBitstreamToTree(unsigned char * bitstream, int bufferSizeBytes); void readBitstreamToTree(unsigned char * bitstream, int bufferSizeBytes);
void readCodeColorBufferToTree(unsigned char *codeColorBuffer); void readCodeColorBufferToTree(unsigned char *codeColorBuffer);
void deleteVoxelCodeFromTree(unsigned char *codeBuffer);
void printTreeForDebugging(VoxelNode *startNode); void printTreeForDebugging(VoxelNode *startNode);
void reaverageVoxelColors(VoxelNode *startNode); void reaverageVoxelColors(VoxelNode *startNode);
unsigned char * loadBitstreamBuffer(unsigned char *& bitstreamBuffer, unsigned char * loadBitstreamBuffer(unsigned char *& bitstreamBuffer,

View file

@ -273,12 +273,17 @@ int main(int argc, const char * argv[])
// XXXBHG: Hacked in support for 'I' insert command // XXXBHG: Hacked in support for 'I' insert command
if (packetData[0] == 'I') { if (packetData[0] == 'I') {
unsigned short int itemNumber = (*((unsigned short int*)&packetData[1])); 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; int atByte = 3;
unsigned char* pVoxelData = (unsigned char*)&packetData[3]; unsigned char* pVoxelData = (unsigned char*)&packetData[3];
while (atByte < receivedBytes) { while (atByte < receivedBytes) {
unsigned char octets = (unsigned char)*pVoxelData; unsigned char octets = (unsigned char)*pVoxelData;
int voxelDataSize = bytesRequiredForCodeLength(octets)+3; // 3 for color! 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); randomTree.readCodeColorBufferToTree(pVoxelData);
//printf("readCodeColorBufferToTree() of size=%d atByte=%d receivedBytes=%ld\n",voxelDataSize,atByte,receivedBytes); //printf("readCodeColorBufferToTree() of size=%d atByte=%d receivedBytes=%ld\n",voxelDataSize,atByte,receivedBytes);
// skip to next // skip to next
@ -286,6 +291,28 @@ int main(int argc, const char * argv[])
atByte+=voxelDataSize; 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 (packetData[0] == 'H') {
if (agentList.addOrUpdateAgent(&agentPublicAddress, if (agentList.addOrUpdateAgent(&agentPublicAddress,
&agentPublicAddress, &agentPublicAddress,