From d7ae023492815026dd5b42e6f3dc40c59f825b81 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 3 Dec 2013 20:40:40 -0800 Subject: [PATCH] make addChildAtIndex() use createNewElement() so we get our correct classes --- libraries/octree/src/Octree.cpp | 10 ---------- libraries/octree/src/Octree.h | 2 -- libraries/octree/src/OctreeElement.cpp | 4 ++-- libraries/octree/src/OctreeElement.h | 4 +++- libraries/shared/src/SharedUtil.cpp | 1 + libraries/voxels/src/VoxelTree.cpp | 9 +++++---- libraries/voxels/src/VoxelTreeElement.cpp | 7 +++++-- libraries/voxels/src/VoxelTreeElement.h | 4 +++- voxel-edit/src/main.cpp | 15 ++++++++------- 9 files changed, 27 insertions(+), 29 deletions(-) diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index 42dfb06809..1ac8262788 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -537,16 +537,6 @@ OctreeElement* Octree::getOctreeElementAt(float x, float y, float z, float s) co return node; } -void Octree::createOctreeElement(float x, float y, float z, float s, - unsigned char red, unsigned char green, unsigned char blue, bool destructive) { - - // XXXBHG Octree cleanup - - //unsigned char* voxelData = pointToVoxel(x,y,z,s,red,green,blue); - //this->readCodeColorBufferToTree(voxelData, destructive); - //delete[] voxelData; -} - // combines the ray cast arguments into a single object class RayArgs { public: diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 8f572bace8..1cdd789a3c 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -191,8 +191,6 @@ public: void deleteOctreeElementAt(float x, float y, float z, float s); OctreeElement* getOctreeElementAt(float x, float y, float z, float s) const; - void createOctreeElement(float x, float y, float z, float s, - unsigned char red, unsigned char green, unsigned char blue, bool destructive = false); void recurseTreeWithOperation(RecurseOctreeOperation operation, void* extraData=NULL); diff --git a/libraries/octree/src/OctreeElement.cpp b/libraries/octree/src/OctreeElement.cpp index 51b9de1a2a..72f28a4725 100644 --- a/libraries/octree/src/OctreeElement.cpp +++ b/libraries/octree/src/OctreeElement.cpp @@ -1108,8 +1108,8 @@ OctreeElement* OctreeElement::addChildAtIndex(int childIndex) { _voxelNodeLeafCount--; } - childAt = new OctreeElement(childOctalCode(getOctalCode(), childIndex)); - //childAt->setVoxelSystem(getVoxelSystem()); // our child is always part of our voxel system NULL ok + unsigned char* newChildCode = childOctalCode(getOctalCode(), childIndex); + childAt = createNewElement(newChildCode); setChildAtIndex(childIndex, childAt); _isDirty = true; diff --git a/libraries/octree/src/OctreeElement.h b/libraries/octree/src/OctreeElement.h index 0682ce7f74..24ea8a12b8 100644 --- a/libraries/octree/src/OctreeElement.h +++ b/libraries/octree/src/OctreeElement.h @@ -46,6 +46,8 @@ class OctreeElement { protected: // can only be constructed by derived implementation OctreeElement(unsigned char * octalCode = NULL); + + virtual OctreeElement* createNewElement(unsigned char * octalCode = NULL) const = 0; public: virtual ~OctreeElement(); @@ -160,7 +162,7 @@ protected: void checkStoreFourChildren(OctreeElement* childOne, OctreeElement* childTwo, OctreeElement* childThree, OctreeElement* childFour); #endif void calculateAABox(); - virtual void init(unsigned char * octalCode); + void init(unsigned char * octalCode); void notifyDeleteHooks(); void notifyUpdateHooks(); diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index 2476929c28..19ac62f092 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -339,6 +339,7 @@ unsigned char* pointToVoxel(float x, float y, float z, float s, unsigned char r, if (s >= 1.0) { unsigned char* voxelOut = new unsigned char; *voxelOut = 0; + return voxelOut; } float xTest, yTest, zTest, sTest; diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 172a9b2ef2..ff722143c4 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -43,11 +43,12 @@ VoxelTreeElement* VoxelTree::getVoxelAt(float x, float y, float z, float s) cons void VoxelTree::createVoxel(float x, float y, float z, float s, unsigned char red, unsigned char green, unsigned char blue, bool destructive) { + unsigned char* voxelData = pointToVoxel(x,y,z,s,red,green,blue); - int length = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(voxelData)) + BYTES_PER_COLOR; - printf("createVoxel()..."); - outputBufferBits(voxelData,length); + //int length = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(voxelData)) + BYTES_PER_COLOR; + //printf("createVoxel()..."); + //outputBufferBits(voxelData,length); this->readCodeColorBufferToTree(voxelData, destructive); delete[] voxelData; @@ -617,7 +618,7 @@ void VoxelTree::readCodeColorBufferToTreeRecursion(VoxelTreeElement* node, ReadC } // Ok, we know we haven't reached our target node yet, so keep looking - printOctalCode(args.codeColorBuffer); + //printOctalCode(args.codeColorBuffer); int childIndex = branchIndexWithDescendant(node->getOctalCode(), args.codeColorBuffer); VoxelTreeElement* childNode = node->getChildAtIndex(childIndex); diff --git a/libraries/voxels/src/VoxelTreeElement.cpp b/libraries/voxels/src/VoxelTreeElement.cpp index 0dda7cc874..08b9e220bf 100644 --- a/libraries/voxels/src/VoxelTreeElement.cpp +++ b/libraries/voxels/src/VoxelTreeElement.cpp @@ -16,11 +16,14 @@ VoxelTreeElement::VoxelTreeElement(unsigned char* octalCode) : OctreeElement(octalCode) { // probably need to do all the color init here.... + init(); }; +OctreeElement* VoxelTreeElement::createNewElement(unsigned char* octalCode) const { + return new VoxelTreeElement(octalCode); +} -void VoxelTreeElement::init(unsigned char* octalCode) { - OctreeElement::init(octalCode); +void VoxelTreeElement::init() { _falseColored = false; // assume true color _currentColor[0] = _currentColor[1] = _currentColor[2] = _currentColor[3] = 0; _trueColor[0] = _trueColor[1] = _trueColor[2] = _trueColor[3] = 0; diff --git a/libraries/voxels/src/VoxelTreeElement.h b/libraries/voxels/src/VoxelTreeElement.h index eda76202e5..15418ad0db 100644 --- a/libraries/voxels/src/VoxelTreeElement.h +++ b/libraries/voxels/src/VoxelTreeElement.h @@ -30,9 +30,11 @@ class VoxelTreeElement : public OctreeElement { friend class VoxelTree; // to allow createElement to new us... VoxelTreeElement(unsigned char* octalCode = NULL); + + virtual OctreeElement* createNewElement(unsigned char* octalCode = NULL) const; public: - virtual void init(unsigned char* octalCode); + void init(); virtual bool hasContent() const { return isColored(); } virtual void splitChildren(); diff --git a/voxel-edit/src/main.cpp b/voxel-edit/src/main.cpp index fee37ee887..97f7249b05 100644 --- a/voxel-edit/src/main.cpp +++ b/voxel-edit/src/main.cpp @@ -362,11 +362,12 @@ int main(int argc, const char * argv[]) } void unitTest(VoxelTree * tree) { + VoxelTreeElement* node = NULL; printf("unit tests...\n"); unsigned long nodeCount; // We want our corner voxels to be about 1/2 meter high, and our TREE_SCALE is in meters, so... - float voxelSize = 0.5f; + float voxelSize = 0.5f / TREE_SCALE; // Here's an example of how to create a voxel. printf("creating corner points...\n"); @@ -375,7 +376,7 @@ void unitTest(VoxelTree * tree) { // Here's an example of how to test if a voxel exists - VoxelTreeElement* node = tree->getVoxelAt(0, 0, 0, voxelSize); + node = tree->getVoxelAt(0, 0, 0, voxelSize); if (node) { // and how to access it's color printf("CORRECT - corner point 0,0,0 exists... color is (%d,%d,%d) \n", @@ -391,7 +392,7 @@ void unitTest(VoxelTree * tree) { printf("Nodes at line %d... %ld nodes\n", __LINE__, tree->getOctreeElementsCount()); // Test to see that the delete worked... it should be FALSE... - if (tree->getVoxelAt(0, 0, 0, voxelSize)) { + if ((node = tree->getVoxelAt(0, 0, 0, voxelSize))) { printf("FAIL corner point 0,0,0 exists...\n"); } else { printf("CORRECT corner point 0,0,0 does not exists...\n"); @@ -400,7 +401,7 @@ void unitTest(VoxelTree * tree) { printf("Nodes at line %d... %ld nodes\n", __LINE__, tree->getOctreeElementsCount()); tree->createVoxel(0, 0, 0, voxelSize, 255, 255 ,255); - if (tree->getVoxelAt(0, 0, 0, voxelSize)) { + if ((node = tree->getVoxelAt(0, 0, 0, voxelSize))) { printf("CORRECT - corner point 0,0,0 exists... color is (%d,%d,%d) \n", node->getColor()[0], node->getColor()[1], node->getColor()[2]); } else { @@ -410,7 +411,7 @@ void unitTest(VoxelTree * tree) { printf("Nodes at line %d... %ld nodes\n", __LINE__, tree->getOctreeElementsCount()); tree->createVoxel(voxelSize, 0, 0, voxelSize, 255, 255 ,0); - if (tree->getVoxelAt(voxelSize, 0, 0, voxelSize)) { + if ((node = tree->getVoxelAt(voxelSize, 0, 0, voxelSize))) { printf("CORRECT - corner point voxelSize,0,0 exists... color is (%d,%d,%d) \n", node->getColor()[0], node->getColor()[1], node->getColor()[2]); } else { @@ -420,7 +421,7 @@ void unitTest(VoxelTree * tree) { printf("Nodes at line %d... %ld nodes\n", __LINE__, tree->getOctreeElementsCount()); tree->createVoxel(0, 0, voxelSize, voxelSize, 255, 0 ,0); - if (tree->getVoxelAt(0, 0, voxelSize, voxelSize)) { + if ((node = tree->getVoxelAt(0, 0, voxelSize, voxelSize))) { printf("CORRECT - corner point 0, 0, voxelSize exists... color is (%d,%d,%d) \n", node->getColor()[0], node->getColor()[1], node->getColor()[2]); } else { @@ -430,7 +431,7 @@ void unitTest(VoxelTree * tree) { printf("Nodes at line %d... %ld nodes\n", __LINE__, tree->getOctreeElementsCount()); tree->createVoxel(voxelSize, 0, voxelSize, voxelSize, 0, 0 ,255); - if (tree->getVoxelAt(voxelSize, 0, voxelSize, voxelSize)) { + if ((node = tree->getVoxelAt(voxelSize, 0, voxelSize, voxelSize))) { printf("CORRECT - corner point voxelSize, 0, voxelSize exists... color is (%d,%d,%d) \n", node->getColor()[0], node->getColor()[1], node->getColor()[2]); } else {