diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h index 88e04f6b..6a586174 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h @@ -103,6 +103,9 @@ namespace PolyVox //Other current position information VoxelType* mCurrentVoxel; + + //Whether the current position is inside the volume + bool m_bIsCurrentPositionValid; }; #endif diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl index 6542a5bb..570ea47a 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl @@ -32,6 +32,11 @@ namespace PolyVox template RawVolume::Sampler::Sampler(RawVolume* volume) :mVolume(volume) + ,mXPosInVolume(0) + ,mYPosInVolume(0) + ,mZPosInVolume(0) + ,mCurrentVoxel(0) + ,m_bIsCurrentPositionValid(false) { } @@ -82,7 +87,7 @@ namespace PolyVox template VoxelType RawVolume::Sampler::getVoxel(void) const { - return *mCurrentVoxel; + return m_bIsCurrentPositionValid ? *mCurrentVoxel : mVolume->m_tBorderValue; } template @@ -98,114 +103,61 @@ namespace PolyVox mYPosInVolume = yPos; mZPosInVolume = zPos; - const uint32_t uVoxelIndex = uXPos + - uYPos * mVolume->getWidth() + - uZPos * mVolume->getWidth() * mVolume->getHeight(); + const uint32_t uVoxelIndex = xPos + + yPos * mVolume->getWidth() + + zPos * mVolume->getWidth() * mVolume->getHeight(); - if(mVolume->m_regValidRegionInBlocks.containsPoint(Vector3DInt32(uXBlock, uYBlock, uZBlock))) - { - mCurrentVoxel = mVolume->m_pData + uVoxelIndex; - } - else - { - mCurrentVoxel = mVolume->m_tBorderValue; - } + mCurrentVoxel = mVolume->m_pData + uVoxelIndex; + + m_bIsCurrentPositionValid = mVolume->m_regValidRegion.containsPoint(Vector3DInt32(xPos, yPos, zPos)); } template void RawVolume::Sampler::movePositiveX(void) { mXPosInVolume++; - - if(mXPosInVolume <= mVolume->getEnclosingRegion().getUpperCorner().getX()) - { - ++mCurrentVoxel; - } - else - { - //We've hit the volume boundary. - mCurrentVoxel = &mVolume->m_tBorderValue; - } + ++mCurrentVoxel; + m_bIsCurrentPositionValid = mXPosInVolume <= mVolume->getEnclosingRegion().getUpperCorner().getX(); } template void RawVolume::Sampler::movePositiveY(void) { mYPosInVolume++; - - if(mYPosInVolume <= mVolume->getEnclosingRegion().getUpperCorner().getY()) - { - mCurrentVoxel += mVolume->getWidth(); - } - else - { - //We've hit the volume boundary. - mCurrentVoxel = &mVolume->m_tBorderValue; - } + mCurrentVoxel += mVolume->getWidth(); + m_bIsCurrentPositionValid = mYPosInVolume <= mVolume->getEnclosingRegion().getUpperCorner().getY(); } template void RawVolume::Sampler::movePositiveZ(void) { mZPosInVolume--; - - if(mZPosInVolume <= mVolume->getEnclosingRegion().getUpperCorner().getZ()) - { - mCurrentVoxel += mVolume->getWidth() * mVolume->getHeight(); - } - else - { - //We've hit the volume boundary. - mCurrentVoxel = &mVolume->m_tBorderValue; - } + mCurrentVoxel += mVolume->getWidth() * mVolume->getHeight(); + m_bIsCurrentPositionValid = mZPosInVolume <= mVolume->getEnclosingRegion().getUpperCorner().getZ(); } template void RawVolume::Sampler::moveNegativeX(void) { mXPosInVolume--; - - if(mXPosInVolume >= mVolume->getEnclosingRegion().getLowerCorner().getX()) - { - --mCurrentVoxel; - } - else - { - //We've hit the volume boundary. - mCurrentVoxel = &mVolume->m_tBorderValue; - } + --mCurrentVoxel; + m_bIsCurrentPositionValid = mXPosInVolume >= mVolume->getEnclosingRegion().getLowerCorner().getX(); } template void RawVolume::Sampler::moveNegativeY(void) { mYPosInVolume--; - - if(mYPosInVolume >= mVolume->getEnclosingRegion().getLowerCorner().getY()) - { - mCurrentVoxel -= mVolume->getWidth(); - } - else - { - //We've hit the volume boundary. - mCurrentVoxel = &mVolume->m_tBorderValue; - } + mCurrentVoxel -= mVolume->getWidth(); + m_bIsCurrentPositionValid = mYPosInVolume >= mVolume->getEnclosingRegion().getLowerCorner().getY(); } template void RawVolume::Sampler::moveNegativeZ(void) { mZPosInVolume--; - - if(mZPosInVolume >= mVolume->getEnclosingRegion().getLowerCorner().getZ()) - { - mCurrentVoxel -= mVolume->getWidth() * mVolume->getHeight(); - } - else - { - //We've hit the volume boundary. - mCurrentVoxel = &mVolume->m_tBorderValue; - } + mCurrentVoxel -= mVolume->getWidth() * mVolume->getHeight(); + m_bIsCurrentPositionValid =mZPosInVolume >= mVolume->getEnclosingRegion().getLowerCorner().getZ(); } template