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

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