From 286ba35b42ac1002887b84e35873cecd25d6be91 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 20 Apr 2008 21:32:44 +0000 Subject: [PATCH] Various refactoring of volume stuff. Including moving region growing code to scrapyard. --- include/PolyVoxForwardDeclarations.h | 4 + include/Volume.h | 12 +- include/Volume.inl | 206 +-------------------------- include/VolumeIterator.h | 6 +- include/VolumeIterator.inl | 29 ---- source/PolyVoxSceneManager.cpp | 17 ++- 6 files changed, 25 insertions(+), 249 deletions(-) diff --git a/include/PolyVoxForwardDeclarations.h b/include/PolyVoxForwardDeclarations.h index 2e32f51c..b4958628 100644 --- a/include/PolyVoxForwardDeclarations.h +++ b/include/PolyVoxForwardDeclarations.h @@ -21,6 +21,10 @@ namespace PolyVox typedef Vector<3,boost::int32_t> Vector3DInt32; typedef Vector<3,boost::uint32_t> Vector3DUint32; template class Volume; + //Some handy typedefs + typedef Volume FloatVolume; + typedef Volume UInt8Volume; + typedef Volume UInt16Volume; template class VolumeIterator; } diff --git a/include/Volume.h b/include/Volume.h index 252f2cc0..12be1265 100644 --- a/include/Volume.h +++ b/include/Volume.h @@ -44,20 +44,20 @@ namespace PolyVox Volume& operator=(const Volume& rhs); public: - Block* getBlock(boost::uint16_t index); - bool containsPoint(Vector3DFloat pos, float boundary); bool containsPoint(Vector3DInt32 pos, boost::uint16_t boundary); - bool loadFromFile(const std::string& sFilename); - bool saveToFile(const std::string& sFilename); - - void regionGrow(boost::uint16_t xStart, boost::uint16_t yStart, boost::uint16_t zStart, VoxelType value); void tidy(void); private: + Block* getBlock(boost::uint16_t index); Block* mBlocks[POLYVOX_NO_OF_BLOCKS_IN_VOLUME]; }; + + //Some handy typedefs + typedef Volume FloatVolume; + typedef Volume UInt8Volume; + typedef Volume UInt16Volume; } #include "Volume.inl" diff --git a/include/Volume.inl b/include/Volume.inl index a0eb9a26..c27fc697 100644 --- a/include/Volume.inl +++ b/include/Volume.inl @@ -85,46 +85,6 @@ namespace PolyVox return *this; } - /*VoxelType Volume::getVoxelAt(const uint16_t xPosition, const uint16_t yPosition, const uint16_t zPosition) const - { - const uint16_t blockX = xPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - const uint16_t blockY = yPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - const uint16_t blockZ = zPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - - const uint16_t xOffset = xPosition - (blockX << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - const uint16_t yOffset = yPosition - (blockY << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - const uint16_t zOffset = zPosition - (blockZ << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - - Block* block = mBlocks - [ - blockX + - blockY * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS + - blockZ * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS - ]; - - return block->getVoxelAt(xOffset,yOffset,zOffset); - }*/ - - /*void Volume::setVoxelAt(const uint16_t xPosition, const uint16_t yPosition, const uint16_t zPosition, const VoxelType value) - { - const uint16_t blockX = xPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - const uint16_t blockY = yPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - const uint16_t blockZ = zPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - - const uint16_t xOffset = xPosition - (blockX << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - const uint16_t yOffset = yPosition - (blockY << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - const uint16_t zOffset = zPosition - (blockZ << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - - Block* block = mBlocks - [ - blockX + - blockY * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS + - blockZ * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS - ]; - - block->setVoxelAt(xOffset,yOffset,zOffset, value); - }*/ - template Block* Volume::getBlock(boost::uint16_t index) { @@ -151,171 +111,7 @@ namespace PolyVox && (pos.x() > boundary) && (pos.y() > boundary) && (pos.z() > boundary); - } - - template - bool Volume::loadFromFile(const std::string& sFilename) - { - //Open the file - std::ifstream file; - file.open(sFilename.c_str(), std::ios::in | std::ios::binary); - if(!file.is_open()) - { - //LogManager::getSingleton().logMessage("Failed to open volume file " + sFilename); - return false; - } - - //Read volume dimensions - uint8_t volumeWidth = 0; - uint8_t volumeHeight = 0; - uint8_t volumeDepth = 0; - file.read(reinterpret_cast(&volumeWidth), sizeof(volumeWidth)); - file.read(reinterpret_cast(&volumeHeight), sizeof(volumeHeight)); - file.read(reinterpret_cast(&volumeDepth), sizeof(volumeDepth)); - if(file.fail()) - { - //LogManager::getSingleton().logMessage("Failed to read dimentions"); - return false; - } - - //Read data - VolumeIterator volIter(*this); - for(uint16_t z = 0; z < POLYVOX_VOLUME_SIDE_LENGTH; ++z) - { - for(uint16_t y = 0; y < POLYVOX_VOLUME_SIDE_LENGTH; ++y) - { - for(uint16_t x = 0; x < POLYVOX_VOLUME_SIDE_LENGTH; ++x) - { - VoxelType value; - file.read(reinterpret_cast(&value), sizeof(value)); //FIXME - check for error here - volIter.setVoxelAt(x,y,z,value); - } - } - - //Periodically see if we can tidy the memory to avoid excessive usage during loading. - if(z%POLYVOX_BLOCK_SIDE_LENGTH == POLYVOX_BLOCK_SIDE_LENGTH-1) - { - tidy(); //FIXME - we don't actually have to tidy the whole volume here - just the part we loaded since the last call to tidy. - } - } - return true; - } - - template - bool Volume::saveToFile(const std::string& sFilename) - { - //Open the file - std::ofstream file; - - file.open(sFilename.c_str(), std::ios::out | std::ios::binary); - if(!file.is_open()) - { - //LogManager::getSingleton().logMessage("Failed to open file for saving volume"); - return false; - } - - //Read volume dimensions - boost::uint8_t volumeWidth = 0; - boost::uint8_t volumeHeight = 0; - boost::uint8_t volumeDepth = 0; - file.write(reinterpret_cast(&volumeWidth), sizeof(volumeWidth)); - file.write(reinterpret_cast(&volumeHeight), sizeof(volumeHeight)); - file.write(reinterpret_cast(&volumeDepth), sizeof(volumeDepth)); - if(file.fail()) - { - //LogManager::getSingleton().logMessage("Failed to write dimensions"); - return false; - } - - //Write data - VolumeIterator volIter(*this); - for(boost::uint16_t z = 0; z < POLYVOX_VOLUME_SIDE_LENGTH; ++z) - { - for(boost::uint16_t y = 0; y < POLYVOX_VOLUME_SIDE_LENGTH; ++y) - { - for(boost::uint16_t x = 0; x < POLYVOX_VOLUME_SIDE_LENGTH; ++x) - { - VoxelType value = volIter.getVoxelAt(x,y,z); - file.write(reinterpret_cast(&value), sizeof(value)); //FIXME - check for error here - } - } - } - return true; - } - - template - void Volume::regionGrow(boost::uint16_t xStart, boost::uint16_t yStart, boost::uint16_t zStart, VoxelType value) - { - //FIXME - introduce integrer 'isInVolume' function - if((xStart > POLYVOX_VOLUME_SIDE_LENGTH-1) || (yStart > POLYVOX_VOLUME_SIDE_LENGTH-1) || (zStart > POLYVOX_VOLUME_SIDE_LENGTH-1) - || (xStart < 0) || (yStart < 0) || (zStart < 0)) - { - //FIXME - error message.. - return; - } - - VolumeIterator volIter(*this); - const VoxelType uSeedValue = volIter.getVoxelAt(xStart,yStart,zStart); - - if(value == uSeedValue) - { - return; //FIXME - Error message? Exception? - } - - std::queue seeds; - seeds.push(Vector3DUint32(xStart,yStart,zStart)); - - while(!seeds.empty()) - { - Vector3DUint32 currentSeed = seeds.front(); - seeds.pop(); - - //std::cout << "x = " << currentSeed.x << " y = " << currentSeed.y << " z = " << currentSeed.z << std::endl; - - //FIXME - introduce 'safe' function which tests this? - if((currentSeed.x() > POLYVOX_VOLUME_SIDE_LENGTH-2) || (currentSeed.y() > POLYVOX_VOLUME_SIDE_LENGTH-2) || (currentSeed.z() > POLYVOX_VOLUME_SIDE_LENGTH-2) - || (currentSeed.x() < 1) || (currentSeed.y() < 1) || (currentSeed.z() < 1)) - { - continue; - } - - if(volIter.getVoxelAt(currentSeed.x(), currentSeed.y(), currentSeed.z()+1) == uSeedValue) - { - volIter.setVoxelAt(currentSeed.x(), currentSeed.y(), currentSeed.z()+1, value); - seeds.push(Vector3DUint32(currentSeed.x(), currentSeed.y(), currentSeed.z()+1)); - } - - if(volIter.getVoxelAt(currentSeed.x(), currentSeed.y(), currentSeed.z()-1) == uSeedValue) - { - volIter.setVoxelAt(currentSeed.x(), currentSeed.y(), currentSeed.z()-1, value); - seeds.push(Vector3DUint32(currentSeed.x(), currentSeed.y(), currentSeed.z()-1)); - } - - if(volIter.getVoxelAt(currentSeed.x(), currentSeed.y()+1, currentSeed.z()) == uSeedValue) - { - volIter.setVoxelAt(currentSeed.x(), currentSeed.y()+1, currentSeed.z(), value); - seeds.push(Vector3DUint32(currentSeed.x(), currentSeed.y()+1, currentSeed.z())); - } - - if(volIter.getVoxelAt(currentSeed.x(), currentSeed.y()-1, currentSeed.z()) == uSeedValue) - { - volIter.setVoxelAt(currentSeed.x(), currentSeed.y()-1, currentSeed.z(), value); - seeds.push(Vector3DUint32(currentSeed.x(), currentSeed.y()-1, currentSeed.z())); - } - - if(volIter.getVoxelAt(currentSeed.x()+1, currentSeed.y(), currentSeed.z()) == uSeedValue) - { - volIter.setVoxelAt(currentSeed.x()+1, currentSeed.y(), currentSeed.z(), value); - seeds.push(Vector3DUint32(currentSeed.x()+1, currentSeed.y(), currentSeed.z())); - } - - if(volIter.getVoxelAt(currentSeed.x()-1, currentSeed.y(), currentSeed.z()) == uSeedValue) - { - volIter.setVoxelAt(currentSeed.x()-1, currentSeed.y(), currentSeed.z(), value); - seeds.push(Vector3DUint32(currentSeed.x()-1, currentSeed.y(), currentSeed.z())); - } - } - } + } template void Volume::tidy(void) diff --git a/include/VolumeIterator.h b/include/VolumeIterator.h index 41912e5c..4753afa5 100644 --- a/include/VolumeIterator.h +++ b/include/VolumeIterator.h @@ -37,9 +37,6 @@ namespace PolyVox void setVoxel(VoxelType value); VoxelType getVoxel(void); - VoxelType getVoxelAt(const boost::uint16_t xPosition, const boost::uint16_t yPosition, const boost::uint16_t zPosition) const; - void setVoxelAt(const boost::uint16_t xPosition, const boost::uint16_t yPosition, const boost::uint16_t zPosition, const VoxelType value); - float getAveragedVoxelAt(const boost::uint16_t xPosition, const boost::uint16_t yPosition, const boost::uint16_t zPosition, boost::uint16_t size) const; //FIXME - this shouldn't return float vector @@ -88,6 +85,9 @@ namespace PolyVox VoxelType peekVoxel1px1py1pz(void) const; private: + + VoxelType getVoxelAt(const boost::uint16_t xPosition, const boost::uint16_t yPosition, const boost::uint16_t zPosition) const; + //The current volume Volume& mVolume; diff --git a/include/VolumeIterator.inl b/include/VolumeIterator.inl index 7203ea92..a0c8fc90 100644 --- a/include/VolumeIterator.inl +++ b/include/VolumeIterator.inl @@ -130,35 +130,6 @@ namespace PolyVox return sum; } - template - void VolumeIterator::setVoxelAt(const uint16_t xPosition, const uint16_t yPosition, const uint16_t zPosition, const VoxelType value) - { - const uint16_t blockX = xPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - const uint16_t blockY = yPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - const uint16_t blockZ = zPosition >> POLYVOX_BLOCK_SIDE_LENGTH_POWER; - - const uint16_t xOffset = xPosition - (blockX << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - const uint16_t yOffset = yPosition - (blockY << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - const uint16_t zOffset = zPosition - (blockZ << POLYVOX_BLOCK_SIDE_LENGTH_POWER); - - Block* block = mVolume.mBlocks - [ - blockX + - blockY * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS + - blockZ * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS * POLYVOX_VOLUME_SIDE_LENGTH_IN_BLOCKS - ]; - - /*if(!block.unique()) - { - Block* copy(new Block(*block)); - block = copy; - - mCurrentVoxel = block->mData + mVoxelIndexInBlock; - }*/ - - block->setVoxelAt(xOffset,yOffset,zOffset, value); - } - template Vector3DFloat VolumeIterator::getCentralDifferenceGradient(void) const { diff --git a/source/PolyVoxSceneManager.cpp b/source/PolyVoxSceneManager.cpp index ea24e1e2..b5784f6b 100644 --- a/source/PolyVoxSceneManager.cpp +++ b/source/PolyVoxSceneManager.cpp @@ -159,10 +159,11 @@ namespace PolyVox { for(uint16_t x = 0; x < POLYVOX_VOLUME_SIDE_LENGTH; ++x) { + volIter.setPosition(x,y,z); if((x/16+y/16+z/16)%2 == 0) - volIter.setVoxelAt(x,y,z,4); + volIter.setVoxel(4); else - volIter.setVoxelAt(x,y,z,8); + volIter.setVoxel(8); } } } @@ -182,7 +183,8 @@ namespace PolyVox (x>225) ) { - volIter.setVoxelAt(x,y,z,2); + volIter.setPosition(x,y,z); + volIter.setVoxel(2); } } } @@ -202,7 +204,8 @@ namespace PolyVox { for(uint16_t x = static_cast(centre.x()) - uHalfX; x < static_cast(centre.x()) + uHalfX; x++) { - volIter.setVoxelAt(x,y,z,0); + volIter.setPosition(x,y,z); + volIter.setVoxel(0); } } } @@ -224,7 +227,8 @@ namespace PolyVox (x<=225) ) { - volIter.setVoxelAt(x,y,z,1); + volIter.setPosition(x,y,z); + volIter.setVoxel(1); } } } @@ -696,7 +700,8 @@ namespace PolyVox if(volumeData->containsPoint(Vector3DInt32(uX,uY,uZ),0)) { VolumeIterator volIter(*volumeData); - return volIter.getVoxelAt(uX,uY,uZ); + volIter.setPosition(uX,uY,uZ); + return volIter.getVoxel(); } else {