Added support for non-cubic volumes.

This commit is contained in:
David Williams
2009-04-19 22:48:56 +00:00
parent 1f6a3231c1
commit c627c90cd0
9 changed files with 150 additions and 69 deletions

View File

@ -43,14 +43,19 @@ namespace PolyVox
friend class VolumeIterator<VoxelType>;
public:
Volume(uint16_t uSideLength, uint16_t uBlockSideLength = 64);
Volume(uint16_t uWidth, uint16_t uHeight, uint16_t uDepth, uint16_t uBlockSideLength = 64);
//Volume(const Volume& rhs);
~Volume();
//Volume& operator=(const Volume& rhs);
Region getEnclosingRegion(void) const;
uint16_t getSideLength(void) const;
//uint16_t getSideLength(void) const;
uint16_t getWidth(void) const;
uint16_t getHeight(void) const;
uint16_t getDepth(void) const;
uint16_t getLongestSideLength(void) const;
float getDiagonalLength(void) const;
VoxelType getVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const;
VoxelType getVoxelAt(const Vector3DUint16& v3dPos) const;
@ -74,10 +79,23 @@ namespace PolyVox
static std::map<VoxelType, POLYVOX_SHARED_PTR< BlockData<VoxelType> > > m_pHomogenousBlockData;
uint32_t m_uNoOfBlocksInVolume;
uint16_t m_uSideLengthInBlocks;
uint8_t m_uSideLengthPower;
uint16_t m_uSideLength;
//uint16_t m_uSideLengthInBlocks;
uint16_t m_uWidthInBlocks;
uint16_t m_uHeightInBlocks;
uint16_t m_uDepthInBlocks;
//uint8_t m_uSideLengthPower;
//uint16_t m_uSideLength;
uint16_t m_uWidth;
uint8_t m_uWidthPower;
uint16_t m_uHeight;
uint8_t m_uHeightPower;
uint16_t m_uDepth;
uint8_t m_uDepthPower;
uint8_t m_uBlockSideLengthPower;
uint16_t m_uBlockSideLength;

View File

@ -36,42 +36,63 @@ namespace PolyVox
{
#pragma region Constructors/Destructors
template <typename VoxelType>
Volume<VoxelType>::Volume(uint16_t uSideLength, uint16_t uBlockSideLength)
Volume<VoxelType>::Volume(uint16_t uWidth, uint16_t uHeight, uint16_t uDepth, uint16_t uBlockSideLength)
:m_pBlocks(0)
,m_uCurrentBlockForTidying(0)
{
//Debug mode validation
assert(isPowerOf2(uSideLength));
assert(isPowerOf2(uWidth));
assert(isPowerOf2(uHeight));
assert(isPowerOf2(uDepth));
assert(isPowerOf2(uBlockSideLength));
assert(uBlockSideLength <= uSideLength);
assert(uBlockSideLength <= uWidth);
assert(uBlockSideLength <= uHeight);
assert(uBlockSideLength <= uDepth);
//Release mode validation
if(!isPowerOf2(uSideLength))
if(!(isPowerOf2(uWidth) && isPowerOf2(uHeight) && isPowerOf2(uDepth)))
{
throw std::invalid_argument("Volume side length must be a power of two.");
throw std::invalid_argument("Volume width, height, and depth must all be a power of two.");
}
if(!isPowerOf2(uBlockSideLength))
{
throw std::invalid_argument("Block side length must be a power of two.");
}
if(uBlockSideLength > uSideLength)
if(uBlockSideLength > uWidth)
{
throw std::invalid_argument("Block side length cannot be greater than volume side length.");
throw std::invalid_argument("Block side length cannot be greater than volume width.");
}
if(uBlockSideLength > uHeight)
{
throw std::invalid_argument("Block side length cannot be greater than volume height.");
}
if(uBlockSideLength > uDepth)
{
throw std::invalid_argument("Block side length cannot be greater than volume depth.");
}
//Compute the volume side length
m_uSideLength = uSideLength;
m_uSideLengthPower = logBase2(m_uSideLength);
//Compute the volume side lengths
m_uWidth = uWidth;
m_uWidthPower = logBase2(m_uWidth);
m_uHeight = uHeight;
m_uHeightPower = logBase2(m_uHeight);
m_uDepth = uDepth;
m_uDepthPower = logBase2(m_uDepth);
//Compute the block side length
m_uBlockSideLength = uBlockSideLength;
m_uBlockSideLengthPower = logBase2(m_uBlockSideLength);
//Compute the side length in blocks
m_uSideLengthInBlocks = m_uSideLength / m_uBlockSideLength;
//m_uSideLengthInBlocks = m_uSideLength / m_uBlockSideLength;
m_uWidthInBlocks = m_uWidth / m_uBlockSideLength;
m_uHeightInBlocks = m_uHeight / m_uBlockSideLength;
m_uDepthInBlocks = m_uDepth / m_uBlockSideLength;
//Compute number of blocks in the volume
m_uNoOfBlocksInVolume = m_uSideLengthInBlocks * m_uSideLengthInBlocks * m_uSideLengthInBlocks;
m_uNoOfBlocksInVolume = m_uWidthInBlocks * m_uHeightInBlocks * m_uDepthInBlocks;
//Create the blocks
m_pBlocks.resize(m_uNoOfBlocksInVolume);
@ -107,21 +128,45 @@ namespace PolyVox
template <typename VoxelType>
Region Volume<VoxelType>::getEnclosingRegion(void) const
{
return Region(Vector3DInt32(0,0,0), Vector3DInt32(m_uSideLength-1,m_uSideLength-1,m_uSideLength-1));
return Region(Vector3DInt32(0,0,0), Vector3DInt32(m_uWidth-1,m_uHeight-1,m_uDepth-1));
}
template <typename VoxelType>
uint16_t Volume<VoxelType>::getSideLength(void) const
uint16_t Volume<VoxelType>::getDepth(void) const
{
return m_uSideLength;
return m_uDepth;
}
template <typename VoxelType>
float Volume<VoxelType>::getDiagonalLength(void) const
{
return sqrtf(static_cast<float>(m_uWidth * m_uWidth + m_uHeight * m_uHeight + m_uDepth * m_uDepth));
}
template <typename VoxelType>
uint16_t Volume<VoxelType>::getHeight(void) const
{
return m_uHeight;
}
template <typename VoxelType>
uint16_t Volume<VoxelType>::getLongestSideLength(void) const
{
return (std::max)((std::max)(m_uWidth,m_uHeight),m_uDepth);
}
template <typename VoxelType>
uint16_t Volume<VoxelType>::getWidth(void) const
{
return m_uWidth;
}
template <typename VoxelType>
VoxelType Volume<VoxelType>::getVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const
{
assert(uXPos < getSideLength());
assert(uYPos < getSideLength());
assert(uZPos < getSideLength());
assert(uXPos < getWidth());
assert(uYPos < getHeight());
assert(uZPos < getDepth());
const uint16_t blockX = uXPos >> m_uBlockSideLengthPower;
const uint16_t blockY = uYPos >> m_uBlockSideLengthPower;
@ -134,8 +179,8 @@ namespace PolyVox
const POLYVOX_SHARED_PTR< BlockData< VoxelType > >& block = m_pBlocks
[
blockX +
blockY * m_uSideLengthInBlocks +
blockZ * m_uSideLengthInBlocks * m_uSideLengthInBlocks
blockY * m_uWidthInBlocks +
blockZ * m_uWidthInBlocks * m_uHeightInBlocks
];
return block->getVoxelAt(xOffset,yOffset,zOffset);
@ -144,9 +189,9 @@ namespace PolyVox
template <typename VoxelType>
VoxelType Volume<VoxelType>::getVoxelAt(const Vector3DUint16& v3dPos) const
{
assert(v3dPos.getX() < m_uSideLength);
assert(v3dPos.getY() < m_uSideLength);
assert(v3dPos.getZ() < m_uSideLength);
assert(v3dPos.getX() < m_uWidth);
assert(v3dPos.getY() < m_uHeight);
assert(v3dPos.getZ() < m_uDepth);
return getVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ());
}
@ -166,8 +211,8 @@ namespace PolyVox
uint32_t uBlockIndex =
blockX +
blockY * m_uSideLengthInBlocks +
blockZ * m_uSideLengthInBlocks * m_uSideLengthInBlocks;
blockY * m_uWidthInBlocks +
blockZ * m_uWidthInBlocks * m_uHeightInBlocks;
POLYVOX_SHARED_PTR< BlockData<VoxelType> >& block = m_pBlocks[uBlockIndex];

View File

@ -199,8 +199,8 @@ namespace PolyVox
mZPosInBlock = mZPosInVolume - (mZBlock << mVolume.m_uBlockSideLengthPower);
mBlockIndexInVolume = mXBlock +
mYBlock * mVolume.m_uSideLengthInBlocks +
mZBlock * mVolume.m_uSideLengthInBlocks * mVolume.m_uSideLengthInBlocks;
mYBlock * mVolume.m_uWidthInBlocks +
mZBlock * mVolume.m_uWidthInBlocks * mVolume.m_uHeightInBlocks;
POLYVOX_SHARED_PTR< BlockData<VoxelType> > currentBlock = mVolume.m_pBlocks[mBlockIndexInVolume];
mVoxelIndexInBlock = mXPosInBlock +
@ -287,11 +287,11 @@ namespace PolyVox
{
mXBlock = mXRegionFirstBlock;
mBlockIndexInVolume = mXBlock +
mYBlock * mVolume.m_uSideLengthInBlocks +
mZBlock * mVolume.m_uSideLengthInBlocks * mVolume.m_uSideLengthInBlocks;
mYBlock * mVolume.m_uWidthInBlocks +
mZBlock * mVolume.m_uWidthInBlocks * mVolume.m_uHeightInBlocks;
++mYBlock;
mBlockIndexInVolume += mVolume.m_uSideLengthInBlocks;
mBlockIndexInVolume += mVolume.m_uWidthInBlocks;
if(mYBlock > mYRegionLastBlock)
{
mYBlock = mYRegionFirstBlock;
@ -300,7 +300,7 @@ namespace PolyVox
mZBlock * mVolume.m_uSideLengthInBlocks * mVolume.m_uSideLengthInBlocks;
++mZBlock;
mBlockIndexInVolume += mVolume.m_uSideLengthInBlocks * mVolume.m_uSideLengthInBlocks;
mBlockIndexInVolume += mVolume.m_uWidthInBlocks * mVolume.m_uHeightInBlocks;
if(mZBlock > mZRegionLastBlock)
{
mIsValidForRegion = false;

View File

@ -9,9 +9,9 @@ namespace PolyVox
assert(volIter.getPosX() >= 1);
assert(volIter.getPosY() >= 1);
assert(volIter.getPosZ() >= 1);
assert(volIter.getPosX() <= volIter.getVolume().getSideLength() - 2);
assert(volIter.getPosY() <= volIter.getVolume().getSideLength() - 2);
assert(volIter.getPosZ() <= volIter.getVolume().getSideLength() - 2);
assert(volIter.getPosX() <= volIter.getVolume().getWidth() - 2);
assert(volIter.getPosY() <= volIter.getVolume().getHeight() - 2);
assert(volIter.getPosZ() <= volIter.getVolume().getDepth() - 2);
float sum = 0.0;