Changes to allow varying block sizes.

This commit is contained in:
David Williams 2008-04-26 17:21:25 +00:00
parent 1d3c60f341
commit 1b592cd1fd
4 changed files with 48 additions and 18 deletions

View File

@ -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;
};
}

View File

@ -23,8 +23,19 @@ namespace PolyVox
{
template <typename VoxelType>
Block<VoxelType>::Block()
Block<VoxelType>::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 <typename VoxelType>
@ -36,6 +47,8 @@ namespace PolyVox
template <typename VoxelType>
Block<VoxelType>::~Block()
{
delete[] m_tData;
m_tData = 0;
}
template <typename VoxelType>
@ -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 <typename VoxelType>
VoxelType Block<VoxelType>::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 <typename VoxelType>
void Block<VoxelType>::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 <typename VoxelType>
boost::uint16_t Block<VoxelType>::getSideLength(void)
{
return m_uSideLength;
}
template <typename VoxelType>
boost::uint32_t Block<VoxelType>::getNoOfVoxels(void)
{
return m_uSideLength * m_uSideLength * m_uSideLength;
}
/*void Block::fillWithValue(const VoxelType value)
{
memset(mData,value,POLYVOX_NO_OF_VOXELS_IN_BLOCK);

View File

@ -32,7 +32,7 @@ namespace PolyVox
{
for(boost::uint16_t i = 0; i < POLYVOX_NO_OF_BLOCKS_IN_VOLUME; ++i)
{
mBlocks[i] = new Block<VoxelType>;
mBlocks[i] = new Block<VoxelType>(POLYVOX_BLOCK_SIDE_LENGTH_POWER);
}
}

View File

@ -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 <typename VoxelType>
@ -217,7 +217,7 @@ namespace PolyVox
mYPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH +
mZPosInBlock * POLYVOX_BLOCK_SIDE_LENGTH * POLYVOX_BLOCK_SIDE_LENGTH;
Block<VoxelType>* 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<VoxelType>* 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;
}
}
}