From 1b592cd1fd4198107e42add6720701171c8a96de Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 26 Apr 2008 17:21:25 +0000 Subject: [PATCH] Changes to allow varying block sizes. --- include/Block.h | 13 ++++++++---- include/Block.inl | 41 ++++++++++++++++++++++++++++++-------- include/Volume.inl | 2 +- include/VolumeIterator.inl | 10 +++++----- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/include/Block.h b/include/Block.h index 9424a46c..cd97bce5 100644 --- a/include/Block.h +++ b/include/Block.h @@ -23,11 +23,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define __PolyVox_Block_H__ #pragma region Headers -#include "boost/cstdint.hpp" - #include "Constants.h" #include "PolyVoxForwardDeclarations.h" #include "TypeDef.h" + +#include "boost/cstdint.hpp" #pragma endregion namespace PolyVox @@ -40,7 +40,7 @@ namespace PolyVox //Block interface public: - Block(); + Block(boost::uint8_t uSideLengthPower); Block(const Block& rhs); ~Block(); @@ -48,13 +48,18 @@ namespace PolyVox //bool isHomogeneous(void); + boost::uint16_t getSideLength(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); //void fillWithValue(const VoxelType value); private: - VoxelType mData[POLYVOX_NO_OF_VOXELS_IN_BLOCK]; + boost::uint32_t getNoOfVoxels(void); + boost::uint8_t m_uSideLengthPower; + boost::uint16_t m_uSideLength; + VoxelType* m_tData; }; } diff --git a/include/Block.inl b/include/Block.inl index 709b9a13..544803c8 100644 --- a/include/Block.inl +++ b/include/Block.inl @@ -23,8 +23,19 @@ namespace PolyVox { template - Block::Block() + Block::Block(boost::uint8_t uSideLengthPower) + :m_tData(0) { + //Check the block size is sensible. This corresponds to a side length of 256 voxels + assert(uSideLengthPower <= 8); + + //Compute the side length + m_uSideLengthPower = uSideLengthPower; + m_uSideLength = 0x01 << uSideLengthPower; + + //If this fails an exception will be thrown. Memory is not + //allocated and there is nothing else in this class to clean up + m_tData = new VoxelType[getNoOfVoxels()]; } template @@ -36,6 +47,8 @@ namespace PolyVox template Block::~Block() { + delete[] m_tData; + m_tData = 0; } template @@ -46,7 +59,7 @@ namespace PolyVox return *this; } - memcpy(mData,rhs.mData,POLYVOX_NO_OF_VOXELS_IN_BLOCK); + memcpy(m_tData,rhs.m_tData,getNoOfVoxels()); return *this; } @@ -54,25 +67,37 @@ namespace PolyVox template VoxelType Block::getVoxelAt(const boost::uint16_t xPosition, const boost::uint16_t yPosition, const boost::uint16_t zPosition) const { - return mData + return m_tData [ xPosition + - yPosition * POLYVOX_BLOCK_SIDE_LENGTH + - zPosition * POLYVOX_BLOCK_SIDE_LENGTH * POLYVOX_BLOCK_SIDE_LENGTH + yPosition * m_uSideLength + + zPosition * m_uSideLength * m_uSideLength ]; } template void Block::setVoxelAt(const boost::uint16_t xPosition, const boost::uint16_t yPosition, const boost::uint16_t zPosition, const VoxelType value) { - mData + m_tData [ xPosition + - yPosition * POLYVOX_BLOCK_SIDE_LENGTH + - zPosition * POLYVOX_BLOCK_SIDE_LENGTH * POLYVOX_BLOCK_SIDE_LENGTH + yPosition * m_uSideLength + + zPosition * m_uSideLength * m_uSideLength ] = value; } + template + boost::uint16_t Block::getSideLength(void) + { + return m_uSideLength; + } + + template + boost::uint32_t Block::getNoOfVoxels(void) + { + return m_uSideLength * m_uSideLength * m_uSideLength; + } + /*void Block::fillWithValue(const VoxelType value) { memset(mData,value,POLYVOX_NO_OF_VOXELS_IN_BLOCK); diff --git a/include/Volume.inl b/include/Volume.inl index c27fc697..6aa6887e 100644 --- a/include/Volume.inl +++ b/include/Volume.inl @@ -32,7 +32,7 @@ namespace PolyVox { for(boost::uint16_t i = 0; i < POLYVOX_NO_OF_BLOCKS_IN_VOLUME; ++i) { - mBlocks[i] = new Block; + mBlocks[i] = new Block(POLYVOX_BLOCK_SIDE_LENGTH_POWER); } } diff --git a/include/VolumeIterator.inl b/include/VolumeIterator.inl index afbb4a5e..8e21c7c5 100644 --- a/include/VolumeIterator.inl +++ b/include/VolumeIterator.inl @@ -49,7 +49,7 @@ namespace PolyVox ,mYPosInBlock(0) ,mZPosInBlock(0) ,mIsValidForRegion(true) - ,mCurrentVoxel(volume.mBlocks[0]->mData) + ,mCurrentVoxel(volume.mBlocks[0]->m_tData) //,mCurrentBlock(volume->mBlocks[0]) ,mVoxelIndexInBlock(0) ,mBlockIndexInVolume(0) @@ -172,7 +172,7 @@ namespace PolyVox mYPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH + mZPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH * POLYVOX_BLOCK_SIDE_LENGTH; - mCurrentVoxel = currentBlock->mData + mVoxelIndexInBlock; + mCurrentVoxel = currentBlock->m_tData + mVoxelIndexInBlock; } template @@ -217,7 +217,7 @@ namespace PolyVox mYPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH + mZPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH * POLYVOX_BLOCK_SIDE_LENGTH; Block* currentBlock = mVolume.mBlocks[mBlockIndexInVolume]; - mCurrentVoxel = currentBlock->mData + mVoxelIndexInBlock; + mCurrentVoxel = currentBlock->m_tData + mVoxelIndexInBlock; mYPosInBlock++; mYPosInVolume++; @@ -230,7 +230,7 @@ namespace PolyVox mYPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH + mZPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH * POLYVOX_BLOCK_SIDE_LENGTH; Block* currentBlock = mVolume.mBlocks[mBlockIndexInVolume]; - mCurrentVoxel = currentBlock->mData + mVoxelIndexInBlock; + mCurrentVoxel = currentBlock->m_tData + mVoxelIndexInBlock; mZPosInBlock++; mZPosInVolume++; @@ -283,7 +283,7 @@ namespace PolyVox mYPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH + mZPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH * POLYVOX_BLOCK_SIDE_LENGTH; - mCurrentVoxel = currentBlock->mData + mVoxelIndexInBlock; + mCurrentVoxel = currentBlock->m_tData + mVoxelIndexInBlock; } } }