mCurrentVoxel is now zero if the current position is not in the volume. It used to just be set to some invalid address.

This commit is contained in:
Daviw Williams 2012-12-07 15:56:46 +01:00
parent eb0c7e7a9f
commit 4be54c6dd1
4 changed files with 73 additions and 48 deletions

View File

@ -103,21 +103,28 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::setPosition(xPos, yPos, zPos); BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::setPosition(xPos, yPos, zPos);
// Then we update the voxel pointer // Then we update the voxel pointer
const int32_t uXBlock = this->mXPosInVolume >> this->mVolume->m_uBlockSideLengthPower; if(this->isCurrentPositionValid())
const int32_t uYBlock = this->mYPosInVolume >> this->mVolume->m_uBlockSideLengthPower; {
const int32_t uZBlock = this->mZPosInVolume >> this->mVolume->m_uBlockSideLengthPower; const int32_t uXBlock = this->mXPosInVolume >> this->mVolume->m_uBlockSideLengthPower;
const int32_t uYBlock = this->mYPosInVolume >> this->mVolume->m_uBlockSideLengthPower;
const int32_t uZBlock = this->mZPosInVolume >> this->mVolume->m_uBlockSideLengthPower;
const uint16_t uXPosInBlock = static_cast<uint16_t>(this->mXPosInVolume - (uXBlock << this->mVolume->m_uBlockSideLengthPower)); const uint16_t uXPosInBlock = static_cast<uint16_t>(this->mXPosInVolume - (uXBlock << this->mVolume->m_uBlockSideLengthPower));
const uint16_t uYPosInBlock = static_cast<uint16_t>(this->mYPosInVolume - (uYBlock << this->mVolume->m_uBlockSideLengthPower)); const uint16_t uYPosInBlock = static_cast<uint16_t>(this->mYPosInVolume - (uYBlock << this->mVolume->m_uBlockSideLengthPower));
const uint16_t uZPosInBlock = static_cast<uint16_t>(this->mZPosInVolume - (uZBlock << this->mVolume->m_uBlockSideLengthPower)); const uint16_t uZPosInBlock = static_cast<uint16_t>(this->mZPosInVolume - (uZBlock << this->mVolume->m_uBlockSideLengthPower));
const uint32_t uVoxelIndexInBlock = uXPosInBlock + const uint32_t uVoxelIndexInBlock = uXPosInBlock +
uYPosInBlock * this->mVolume->m_uBlockSideLength + uYPosInBlock * this->mVolume->m_uBlockSideLength +
uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength;
Block<VoxelType>* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); Block<VoxelType>* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock);
mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock; mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock;
}
else
{
mCurrentVoxel = 0;
}
} }
template <typename VoxelType> template <typename VoxelType>
@ -145,7 +152,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::movePositiveX(); BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::movePositiveX();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mXPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mXPosInVolume) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
++mCurrentVoxel; ++mCurrentVoxel;
@ -164,7 +171,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::movePositiveY(); BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::movePositiveY();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mYPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mYPosInVolume) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
mCurrentVoxel += this->mVolume->m_uBlockSideLength; mCurrentVoxel += this->mVolume->m_uBlockSideLength;
@ -183,7 +190,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::movePositiveZ(); BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::movePositiveZ();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mZPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mZPosInVolume) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
mCurrentVoxel += this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; mCurrentVoxel += this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength;
@ -202,7 +209,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::moveNegativeX(); BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::moveNegativeX();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mXPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mXPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
--mCurrentVoxel; --mCurrentVoxel;
@ -221,7 +228,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::moveNegativeY(); BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::moveNegativeY();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mYPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mYPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
mCurrentVoxel -= this->mVolume->m_uBlockSideLength; mCurrentVoxel -= this->mVolume->m_uBlockSideLength;
@ -240,7 +247,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::moveNegativeZ(); BaseVolume<VoxelType>::template Sampler< LargeVolume<VoxelType> >::moveNegativeZ();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mZPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mZPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
mCurrentVoxel -= this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; mCurrentVoxel -= this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength;

View File

@ -68,16 +68,23 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::setPosition(xPos, yPos, zPos); BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::setPosition(xPos, yPos, zPos);
// Then we update the voxel pointer // Then we update the voxel pointer
const Vector3DInt32& v3dLowerCorner = this->mVolume->m_regValidRegion.getLowerCorner(); if(this->isCurrentPositionValid())
int32_t iLocalXPos = xPos - v3dLowerCorner.getX(); {
int32_t iLocalYPos = yPos - v3dLowerCorner.getY(); const Vector3DInt32& v3dLowerCorner = this->mVolume->m_regValidRegion.getLowerCorner();
int32_t iLocalZPos = zPos - v3dLowerCorner.getZ(); int32_t iLocalXPos = xPos - v3dLowerCorner.getX();
int32_t iLocalYPos = yPos - v3dLowerCorner.getY();
int32_t iLocalZPos = zPos - v3dLowerCorner.getZ();
const int32_t uVoxelIndex = iLocalXPos + const int32_t uVoxelIndex = iLocalXPos +
iLocalYPos * this->mVolume->getWidth() + iLocalYPos * this->mVolume->getWidth() +
iLocalZPos * this->mVolume->getWidth() * this->mVolume->getHeight(); iLocalZPos * this->mVolume->getWidth() * this->mVolume->getHeight();
mCurrentVoxel = this->mVolume->m_pData + uVoxelIndex; mCurrentVoxel = this->mVolume->m_pData + uVoxelIndex;
}
else
{
mCurrentVoxel = 0;
}
} }
template <typename VoxelType> template <typename VoxelType>
@ -102,7 +109,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::movePositiveX(); BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::movePositiveX();
// Then we update the voxel pointer // Then we update the voxel pointer
if(this->isCurrentPositionValid()) if(this->isCurrentPositionValid() && mCurrentVoxel )
{ {
++mCurrentVoxel; ++mCurrentVoxel;
} }
@ -119,7 +126,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::movePositiveY(); BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::movePositiveY();
// Then we update the voxel pointer // Then we update the voxel pointer
if(this->isCurrentPositionValid()) if(this->isCurrentPositionValid() && mCurrentVoxel )
{ {
mCurrentVoxel += this->mVolume->getWidth(); mCurrentVoxel += this->mVolume->getWidth();
} }
@ -136,7 +143,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::movePositiveZ(); BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::movePositiveZ();
// Then we update the voxel pointer // Then we update the voxel pointer
if(this->isCurrentPositionValid()) if(this->isCurrentPositionValid() && mCurrentVoxel )
{ {
mCurrentVoxel += this->mVolume->getWidth() * this->mVolume->getHeight(); mCurrentVoxel += this->mVolume->getWidth() * this->mVolume->getHeight();
} }
@ -153,7 +160,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::moveNegativeX(); BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::moveNegativeX();
// Then we update the voxel pointer // Then we update the voxel pointer
if(this->isCurrentPositionValid()) if(this->isCurrentPositionValid() && mCurrentVoxel )
{ {
--mCurrentVoxel; --mCurrentVoxel;
} }
@ -170,7 +177,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::moveNegativeY(); BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::moveNegativeY();
// Then we update the voxel pointer // Then we update the voxel pointer
if(this->isCurrentPositionValid()) if(this->isCurrentPositionValid() && mCurrentVoxel )
{ {
mCurrentVoxel -= this->mVolume->getWidth(); mCurrentVoxel -= this->mVolume->getWidth();
} }
@ -187,7 +194,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::moveNegativeZ(); BaseVolume<VoxelType>::template Sampler< RawVolume<VoxelType> >::moveNegativeZ();
// Then we update the voxel pointer // Then we update the voxel pointer
if(this->isCurrentPositionValid()) if(this->isCurrentPositionValid() && mCurrentVoxel )
{ {
mCurrentVoxel -= this->mVolume->getWidth() * this->mVolume->getHeight(); mCurrentVoxel -= this->mVolume->getWidth() * this->mVolume->getHeight();
} }

View File

@ -306,6 +306,10 @@ namespace PolyVox
uBlockY -= m_regValidRegionInBlocks.getLowerCorner().getY(); uBlockY -= m_regValidRegionInBlocks.getLowerCorner().getY();
uBlockZ -= m_regValidRegionInBlocks.getLowerCorner().getZ(); uBlockZ -= m_regValidRegionInBlocks.getLowerCorner().getZ();
assert(uBlockX >= 0);
assert(uBlockY >= 0);
assert(uBlockZ >= 0);
//Compute the block index //Compute the block index
uint32_t uBlockIndex = uint32_t uBlockIndex =
uBlockX + uBlockX +

View File

@ -117,21 +117,28 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::setPosition(xPos, yPos, zPos); BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::setPosition(xPos, yPos, zPos);
// Then we update the voxel pointer // Then we update the voxel pointer
const int32_t uXBlock = this->mXPosInVolume >> this->mVolume->m_uBlockSideLengthPower; if(this->isCurrentPositionValid())
const int32_t uYBlock = this->mYPosInVolume >> this->mVolume->m_uBlockSideLengthPower; {
const int32_t uZBlock = this->mZPosInVolume >> this->mVolume->m_uBlockSideLengthPower; const int32_t uXBlock = this->mXPosInVolume >> this->mVolume->m_uBlockSideLengthPower;
const int32_t uYBlock = this->mYPosInVolume >> this->mVolume->m_uBlockSideLengthPower;
const int32_t uZBlock = this->mZPosInVolume >> this->mVolume->m_uBlockSideLengthPower;
const uint16_t uXPosInBlock = static_cast<uint16_t>(this->mXPosInVolume - (uXBlock << this->mVolume->m_uBlockSideLengthPower)); const uint16_t uXPosInBlock = static_cast<uint16_t>(this->mXPosInVolume - (uXBlock << this->mVolume->m_uBlockSideLengthPower));
const uint16_t uYPosInBlock = static_cast<uint16_t>(this->mYPosInVolume - (uYBlock << this->mVolume->m_uBlockSideLengthPower)); const uint16_t uYPosInBlock = static_cast<uint16_t>(this->mYPosInVolume - (uYBlock << this->mVolume->m_uBlockSideLengthPower));
const uint16_t uZPosInBlock = static_cast<uint16_t>(this->mZPosInVolume - (uZBlock << this->mVolume->m_uBlockSideLengthPower)); const uint16_t uZPosInBlock = static_cast<uint16_t>(this->mZPosInVolume - (uZBlock << this->mVolume->m_uBlockSideLengthPower));
const uint32_t uVoxelIndexInBlock = uXPosInBlock + const uint32_t uVoxelIndexInBlock = uXPosInBlock +
uYPosInBlock * this->mVolume->m_uBlockSideLength + uYPosInBlock * this->mVolume->m_uBlockSideLength +
uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength;
Block* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); Block* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock);
mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock; mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock;
}
else
{
mCurrentVoxel = 0;
}
} }
/** /**
@ -164,7 +171,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::movePositiveX(); BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::movePositiveX();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mXPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mXPosInVolume) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
++mCurrentVoxel; ++mCurrentVoxel;
@ -183,7 +190,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::movePositiveY(); BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::movePositiveY();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mYPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mYPosInVolume) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
mCurrentVoxel += this->mVolume->m_uBlockSideLength; mCurrentVoxel += this->mVolume->m_uBlockSideLength;
@ -202,7 +209,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::movePositiveZ(); BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::movePositiveZ();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mZPosInVolume) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mZPosInVolume) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
mCurrentVoxel += this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; mCurrentVoxel += this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength;
@ -221,7 +228,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::moveNegativeX(); BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::moveNegativeX();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mXPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mXPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
--mCurrentVoxel; --mCurrentVoxel;
@ -240,7 +247,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::moveNegativeY(); BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::moveNegativeY();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mYPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mYPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
mCurrentVoxel -= this->mVolume->m_uBlockSideLength; mCurrentVoxel -= this->mVolume->m_uBlockSideLength;
@ -259,7 +266,7 @@ namespace PolyVox
BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::moveNegativeZ(); BaseVolume<VoxelType>::template Sampler< SimpleVolume<VoxelType> >::moveNegativeZ();
// Then we update the voxel pointer // Then we update the voxel pointer
if((this->isCurrentPositionValid()) && ((this->mZPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0)) if((this->isCurrentPositionValid()) && mCurrentVoxel && ((this->mZPosInVolume + 1) % this->mVolume->m_uBlockSideLength != 0))
{ {
//No need to compute new block. //No need to compute new block.
mCurrentVoxel -= this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; mCurrentVoxel -= this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength;