From 90063354bdf10b8bfd9fa0012ba3d1ffc3ef7e85 Mon Sep 17 00:00:00 2001 From: David Williams Date: Mon, 15 Aug 2011 21:10:09 +0100 Subject: [PATCH] Added setVoxel() funtion to volume iterators. Fixed bugs with RawVolume iterator movement funtions. Proper implementation of RawVolume peek...() functions. --- .../include/PolyVoxCore/LargeVolume.h | 1 + .../PolyVoxCore/LargeVolumeSampler.inl | 15 +- .../include/PolyVoxCore/RawVolume.h | 6 +- .../include/PolyVoxCore/RawVolumeSampler.inl | 176 +++++++++++++++--- .../PolyVoxCore/include/PolyVoxCore/Region.h | 7 + .../include/PolyVoxCore/SimpleVolume.h | 2 + .../include/PolyVoxCore/SimpleVolume.inl | 9 +- .../PolyVoxCore/SimpleVolumeSampler.inl | 23 ++- .../PolyVoxCore/include/PolyVoxCore/Volume.h | 3 +- .../include/PolyVoxCore/VolumeSampler.inl | 7 + library/PolyVoxCore/source/Region.cpp | 36 ++++ 11 files changed, 254 insertions(+), 31 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h index 2fb4fd0e..323c7054 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h @@ -162,6 +162,7 @@ namespace PolyVox void setPosition(const Vector3DInt32& v3dNewPos); void setPosition(int32_t xPos, int32_t yPos, int32_t zPos); + inline bool setVoxel(VoxelType tValue); void movePositiveX(void); void movePositiveY(void); diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl index 994a9ff3..f89d4c1b 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl @@ -26,12 +26,13 @@ freely, subject to the following restrictions: #include "PolyVoxCore/Vector.h" #include "PolyVoxCore/Region.h" +#include + #define BORDER_LOW(x) ((( x >> this->mVolume->m_uBlockSideLengthPower) << this->mVolume->m_uBlockSideLengthPower) != x) #define BORDER_HIGH(x) ((( (x+1) >> this->mVolume->m_uBlockSideLengthPower) << this->mVolume->m_uBlockSideLengthPower) != (x+1)) //#define BORDER_LOW(x) (( x % mVolume->m_uBlockSideLength) != 0) //#define BORDER_HIGH(x) (( x % mVolume->m_uBlockSideLength) != mVolume->m_uBlockSideLength - 1) -#include namespace PolyVox { template @@ -159,6 +160,15 @@ namespace PolyVox } } + template + bool LargeVolume::Sampler::setVoxel(VoxelType tValue) + { + //*mCurrentVoxel = tValue; + //Need to think what effect this has on any existing iterators. + assert(false); + return false; + } + template void LargeVolume::Sampler::movePositiveX(void) { @@ -525,3 +535,6 @@ namespace PolyVox return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume+1); } } + +#undef BORDER_LOW +#undef BORDER_HIGH \ No newline at end of file diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h index d7d2b1bf..83a287d1 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h @@ -52,6 +52,7 @@ namespace PolyVox void setPosition(const Vector3DInt32& v3dNewPos); void setPosition(int32_t xPos, int32_t yPos, int32_t zPos); + inline bool setVoxel(VoxelType tValue); void movePositiveX(void); void movePositiveY(void); @@ -96,7 +97,10 @@ namespace PolyVox VoxelType* mCurrentVoxel; //Whether the current position is inside the volume - bool m_bIsCurrentPositionValid; + //FIXME - Replace these with flags + bool m_bIsCurrentPositionValidInX; + bool m_bIsCurrentPositionValidInY; + bool m_bIsCurrentPositionValidInZ; }; #endif diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl index 6d22784b..bf4f97b1 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolumeSampler.inl @@ -27,13 +27,23 @@ freely, subject to the following restrictions: #include "PolyVoxCore/Region.h" #include + +#define BORDER_LOWX(val) (val > this->mVolume->getEnclosingRegion().getLowerCorner().getX()) +#define BORDER_HIGHX(val) (val < this->mVolume->getEnclosingRegion().getUpperCorner().getX()) +#define BORDER_LOWY(val) (val > this->mVolume->getEnclosingRegion().getLowerCorner().getY()) +#define BORDER_HIGHY(val) (val < this->mVolume->getEnclosingRegion().getUpperCorner().getY()) +#define BORDER_LOWZ(val) (val > this->mVolume->getEnclosingRegion().getLowerCorner().getZ()) +#define BORDER_HIGHZ(val) (val < this->mVolume->getEnclosingRegion().getUpperCorner().getZ()) + namespace PolyVox { template RawVolume::Sampler::Sampler(RawVolume* volume) :Volume::template Sampler< RawVolume >(volume) ,mCurrentVoxel(0) - ,m_bIsCurrentPositionValid(false) + ,m_bIsCurrentPositionValidInX(false) + ,m_bIsCurrentPositionValidInY(false) + ,m_bIsCurrentPositionValidInZ(false) { } @@ -63,7 +73,7 @@ namespace PolyVox template VoxelType RawVolume::Sampler::getVoxel(void) const { - return m_bIsCurrentPositionValid ? *mCurrentVoxel : this->mVolume->getBorderValue(); + return (m_bIsCurrentPositionValidInX && m_bIsCurrentPositionValidInY && m_bIsCurrentPositionValidInZ) ? *mCurrentVoxel : this->mVolume->getBorderValue(); } template @@ -79,25 +89,34 @@ namespace PolyVox this->mYPosInVolume = yPos; this->mZPosInVolume = zPos; - if(this->mVolume->getEnclosingRegion().containsPoint(Vector3DInt32(xPos, yPos, zPos))) + const Vector3DInt32& v3dLowerCorner = this->mVolume->m_regValidRegion.getLowerCorner(); + int32_t iLocalXPos = xPos - v3dLowerCorner.getX(); + int32_t iLocalYPos = yPos - v3dLowerCorner.getY(); + int32_t iLocalZPos = zPos - v3dLowerCorner.getZ(); + + const int32_t uVoxelIndex = iLocalXPos + + iLocalYPos * this->mVolume->getWidth() + + iLocalZPos * this->mVolume->getWidth() * this->mVolume->getHeight(); + + mCurrentVoxel = this->mVolume->m_pData + uVoxelIndex; + + m_bIsCurrentPositionValidInX = mVolume->getEnclosingRegion().containsPointInX(xPos); + m_bIsCurrentPositionValidInY = mVolume->getEnclosingRegion().containsPointInY(yPos); + m_bIsCurrentPositionValidInZ = mVolume->getEnclosingRegion().containsPointInZ(zPos); + } + + template + bool RawVolume::Sampler::setVoxel(VoxelType tValue) + { + //return m_bIsCurrentPositionValid ? *mCurrentVoxel : this->mVolume->getBorderValue(); + if(m_bIsCurrentPositionValidInX && m_bIsCurrentPositionValidInY && m_bIsCurrentPositionValidInZ) { - const Vector3DInt32& v3dLowerCorner = this->mVolume->m_regValidRegion.getLowerCorner(); - int32_t iLocalXPos = xPos - v3dLowerCorner.getX(); - int32_t iLocalYPos = yPos - v3dLowerCorner.getY(); - int32_t iLocalZPos = zPos - v3dLowerCorner.getZ(); - - const uint32_t uVoxelIndex = iLocalXPos + - iLocalYPos * this->mVolume->getWidth() + - iLocalZPos * this->mVolume->getWidth() * this->mVolume->getHeight(); - - mCurrentVoxel = this->mVolume->m_pData + uVoxelIndex; - - m_bIsCurrentPositionValid = true; + *mCurrentVoxel = tValue; + return true; } else { - mCurrentVoxel = 0; - m_bIsCurrentPositionValid = false; + return false; } } @@ -106,7 +125,7 @@ namespace PolyVox { this->mXPosInVolume++; ++mCurrentVoxel; - m_bIsCurrentPositionValid = this->mXPosInVolume <= this->mVolume->getEnclosingRegion().getUpperCorner().getX(); + m_bIsCurrentPositionValidInX = this->mVolume->getEnclosingRegion().containsPointInX(this->mXPosInVolume); } template @@ -114,7 +133,7 @@ namespace PolyVox { this->mYPosInVolume++; mCurrentVoxel += this->mVolume->getWidth(); - m_bIsCurrentPositionValid = this->mYPosInVolume <= this->mVolume->getEnclosingRegion().getUpperCorner().getY(); + m_bIsCurrentPositionValidInY = this->mVolume->getEnclosingRegion().containsPointInY(this->mYPosInVolume); } template @@ -122,7 +141,7 @@ namespace PolyVox { this->mZPosInVolume++; mCurrentVoxel += this->mVolume->getWidth() * this->mVolume->getHeight(); - m_bIsCurrentPositionValid = this->mZPosInVolume <= this->mVolume->getEnclosingRegion().getUpperCorner().getZ(); + m_bIsCurrentPositionValidInZ = this->mVolume->getEnclosingRegion().containsPointInZ(this->mZPosInVolume); } template @@ -130,7 +149,7 @@ namespace PolyVox { this->mXPosInVolume--; --mCurrentVoxel; - m_bIsCurrentPositionValid = this->mXPosInVolume >= this->mVolume->getEnclosingRegion().getLowerCorner().getX(); + m_bIsCurrentPositionValidInX = this->mVolume->getEnclosingRegion().containsPointInX(this->mXPosInVolume); } template @@ -138,7 +157,7 @@ namespace PolyVox { this->mYPosInVolume--; mCurrentVoxel -= this->mVolume->getWidth(); - m_bIsCurrentPositionValid = this->mYPosInVolume >= this->mVolume->getEnclosingRegion().getLowerCorner().getY(); + m_bIsCurrentPositionValidInY = this->mVolume->getEnclosingRegion().containsPointInY(this->mYPosInVolume); } template @@ -146,60 +165,96 @@ namespace PolyVox { this->mZPosInVolume--; mCurrentVoxel -= this->mVolume->getWidth() * this->mVolume->getHeight(); - m_bIsCurrentPositionValid =this->mZPosInVolume >= this->mVolume->getEnclosingRegion().getLowerCorner().getZ(); + m_bIsCurrentPositionValidInZ = this->mVolume->getEnclosingRegion().containsPointInZ(this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel1nx1ny1nz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel - 1 - this->mVolume->getWidth() - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume-1); } template VoxelType RawVolume::Sampler::peekVoxel1nx1ny0pz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) ) + { + return *(mCurrentVoxel - 1 - this->mVolume->getWidth()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel1nx1ny1pz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel - 1 - this->mVolume->getWidth() + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume-1,this->mZPosInVolume+1); } template VoxelType RawVolume::Sampler::peekVoxel1nx0py1nz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel - 1 - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume-1); } template VoxelType RawVolume::Sampler::peekVoxel1nx0py0pz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) ) + { + return *(mCurrentVoxel - 1); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel1nx0py1pz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel - 1 + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume,this->mZPosInVolume+1); } template VoxelType RawVolume::Sampler::peekVoxel1nx1py1nz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) && BORDER_LOWZ(this->mYPosInVolume) ) + { + return *(mCurrentVoxel - 1 + this->mVolume->getWidth() - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume-1); } template VoxelType RawVolume::Sampler::peekVoxel1nx1py0pz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) ) + { + return *(mCurrentVoxel - 1 + this->mVolume->getWidth()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel1nx1py1pz(void) const { + if( BORDER_LOWX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel - 1 + this->mVolume->getWidth() + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume-1,this->mYPosInVolume+1,this->mZPosInVolume+1); } @@ -208,24 +263,40 @@ namespace PolyVox template VoxelType RawVolume::Sampler::peekVoxel0px1ny1nz(void) const { + if( BORDER_LOWX(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel - this->mVolume->getWidth() - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume-1); } template VoxelType RawVolume::Sampler::peekVoxel0px1ny0pz(void) const { + if( BORDER_LOWY(this->mYPosInVolume) ) + { + return *(mCurrentVoxel - this->mVolume->getWidth()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel0px1ny1pz(void) const { + if( BORDER_LOWY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel - this->mVolume->getWidth() + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume-1,this->mZPosInVolume+1); } template VoxelType RawVolume::Sampler::peekVoxel0px0py1nz(void) const { + if( BORDER_LOWZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume,this->mZPosInVolume-1); } @@ -238,24 +309,40 @@ namespace PolyVox template VoxelType RawVolume::Sampler::peekVoxel0px0py1pz(void) const { + if( BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume,this->mZPosInVolume+1); } template VoxelType RawVolume::Sampler::peekVoxel0px1py1nz(void) const { + if( BORDER_HIGHY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + this->mVolume->getWidth() - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume-1); } template VoxelType RawVolume::Sampler::peekVoxel0px1py0pz(void) const { + if( BORDER_HIGHY(this->mYPosInVolume) ) + { + return *(mCurrentVoxel + this->mVolume->getWidth()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel0px1py1pz(void) const { + if( BORDER_HIGHY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + this->mVolume->getWidth() + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume,this->mYPosInVolume+1,this->mZPosInVolume+1); } @@ -264,54 +351,97 @@ namespace PolyVox template VoxelType RawVolume::Sampler::peekVoxel1px1ny1nz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + 1 - this->mVolume->getWidth() - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume-1); } template VoxelType RawVolume::Sampler::peekVoxel1px1ny0pz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) ) + { + return *(mCurrentVoxel + 1 - this->mVolume->getWidth()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel1px1ny1pz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_LOWY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + 1 - this->mVolume->getWidth() + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume-1,this->mZPosInVolume+1); } template VoxelType RawVolume::Sampler::peekVoxel1px0py1nz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + 1 - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume-1); } template VoxelType RawVolume::Sampler::peekVoxel1px0py0pz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) ) + { + return *(mCurrentVoxel + 1); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel1px0py1pz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + 1 + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume,this->mZPosInVolume+1); } template VoxelType RawVolume::Sampler::peekVoxel1px1py1nz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) && BORDER_LOWZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + 1 + this->mVolume->getWidth() - this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume-1); } template VoxelType RawVolume::Sampler::peekVoxel1px1py0pz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) ) + { + return *(mCurrentVoxel + 1 + this->mVolume->getWidth()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume); } template VoxelType RawVolume::Sampler::peekVoxel1px1py1pz(void) const { + if( BORDER_HIGHX(this->mXPosInVolume) && BORDER_HIGHY(this->mYPosInVolume) && BORDER_HIGHZ(this->mZPosInVolume) ) + { + return *(mCurrentVoxel + 1 + this->mVolume->getWidth() + this->mVolume->getHeight() * this->mVolume->getHeight()); + } return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume+1); } } + +#undef BORDER_LOWX +#undef BORDER_HIGHX +#undef BORDER_LOWY +#undef BORDER_HIGHY +#undef BORDER_LOWZ +#undef BORDER_HIGHZ \ No newline at end of file diff --git a/library/PolyVoxCore/include/PolyVoxCore/Region.h b/library/PolyVoxCore/include/PolyVoxCore/Region.h index 5888df0d..e9297492 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Region.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Region.h @@ -51,6 +51,13 @@ namespace PolyVox bool containsPoint(const Vector3DFloat& pos, float boundary = 0.0f) const; bool containsPoint(const Vector3DInt32& pos, uint8_t boundary = 0) const; + //FIXME - Don't like these. Make containsPoint take flags indicating which axes to check? + bool containsPointInX(float pos, float boundary = 0.0f) const; + bool containsPointInX(int32_t pos, uint8_t boundary = 0) const; + bool containsPointInY(float pos, float boundary = 0.0f) const; + bool containsPointInY(int32_t pos, uint8_t boundary = 0) const; + bool containsPointInZ(float pos, float boundary = 0.0f) const; + bool containsPointInZ(int32_t pos, uint8_t boundary = 0) const; void cropTo(const Region& other); /// Deprecated and misleading int32_t depth(void) const; diff --git a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.h b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.h index b75cd367..c11674f5 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.h @@ -77,6 +77,7 @@ namespace PolyVox void setPosition(const Vector3DInt32& v3dNewPos); void setPosition(int32_t xPos, int32_t yPos, int32_t zPos); + inline bool setVoxel(VoxelType tValue); void movePositiveX(void); void movePositiveY(void); @@ -179,6 +180,7 @@ private: uint16_t m_uDepthInBlocks; //The size of the blocks + uint32_t m_uNoOfVoxelsPerBlock; uint16_t m_uBlockSideLength; uint8_t m_uBlockSideLengthPower; }; diff --git a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.inl index a401ec95..75133e32 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolume.inl @@ -143,7 +143,7 @@ namespace PolyVox { /*Block* pUncompressedBorderBlock = getUncompressedBlock(&m_pBorderBlock); return pUncompressedBorderBlock->fill(tBorder);*/ - std::fill(m_pUncompressedBorderData, m_pUncompressedBorderData + m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength, tBorder); + std::fill(m_pUncompressedBorderData, m_pUncompressedBorderData + m_uNoOfVoxelsPerBlock, tBorder); } //////////////////////////////////////////////////////////////////////////////// @@ -205,6 +205,7 @@ namespace PolyVox } m_uBlockSideLength = uBlockSideLength; + m_uNoOfVoxelsPerBlock = m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength; m_pUncompressedBorderData = 0; this->m_regValidRegion = regValidRegion; @@ -230,8 +231,8 @@ namespace PolyVox } //Create the border block - m_pUncompressedBorderData = new VoxelType[m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength]; - std::fill(m_pUncompressedBorderData, m_pUncompressedBorderData + m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength, VoxelType()); + m_pUncompressedBorderData = new VoxelType[m_uNoOfVoxelsPerBlock]; + std::fill(m_pUncompressedBorderData, m_pUncompressedBorderData + m_uNoOfVoxelsPerBlock, VoxelType()); //Other properties we might find useful later this->m_uLongestSideLength = (std::max)((std::max)(this->getWidth(),this->getHeight()),this->getDepth()); @@ -266,7 +267,7 @@ namespace PolyVox { uint32_t uSizeInBytes = sizeof(SimpleVolume); - uint32_t uSizeOfBlockInBytes = m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength * sizeof(VoxelType); + uint32_t uSizeOfBlockInBytes = m_uNoOfVoxelsPerBlock * sizeof(VoxelType); //Memory used by the blocks ( + 1 is for border) uSizeInBytes += uSizeOfBlockInBytes * (m_uNoOfBlocksInVolume + 1); diff --git a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl index ba8bdb1a..7148b510 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/SimpleVolumeSampler.inl @@ -26,12 +26,13 @@ freely, subject to the following restrictions: #include "PolyVoxCore/Vector.h" #include "PolyVoxCore/Region.h" +#include + #define BORDER_LOW(x) ((( x >> this->mVolume->m_uBlockSideLengthPower) << this->mVolume->m_uBlockSideLengthPower) != x) #define BORDER_HIGH(x) ((( (x+1) >> this->mVolume->m_uBlockSideLengthPower) << this->mVolume->m_uBlockSideLengthPower) != (x+1)) //#define BORDER_LOW(x) (( x % this->mVolume->m_uBlockSideLength) != 0) //#define BORDER_HIGH(x) (( x % this->mVolume->m_uBlockSideLength) != this->mVolume->m_uBlockSideLength - 1) -#include namespace PolyVox { template @@ -159,6 +160,23 @@ namespace PolyVox } } + template + bool SimpleVolume::Sampler::setVoxel(VoxelType tValue) + { + VoxelType* pBorderDataEndPlusOne = this->mVolume->m_pUncompressedBorderData + this->mVolume->m_uNoOfVoxelsPerBlock; + + //Make sure we're not trying to write to the border data + if((mCurrentVoxel < this->mVolume->m_pUncompressedBorderData) || (mCurrentVoxel >= pBorderDataEndPlusOne)) + { + *mCurrentVoxel = tValue; + return true; + } + else + { + return false; + } + } + template void SimpleVolume::Sampler::movePositiveX(void) { @@ -525,3 +543,6 @@ namespace PolyVox return this->mVolume->getVoxelAt(this->mXPosInVolume+1,this->mYPosInVolume+1,this->mZPosInVolume+1); } } + +#undef BORDER_LOW +#undef BORDER_HIGH diff --git a/library/PolyVoxCore/include/PolyVoxCore/Volume.h b/library/PolyVoxCore/include/PolyVoxCore/Volume.h index 2d5d7ba2..298b90af 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Volume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Volume.h @@ -47,10 +47,11 @@ namespace PolyVox int32_t getPosX(void) const; int32_t getPosY(void) const; int32_t getPosZ(void) const; - inline VoxelType getVoxel(void) const; + inline VoxelType getVoxel(void) const; void setPosition(const Vector3DInt32& v3dNewPos); void setPosition(int32_t xPos, int32_t yPos, int32_t zPos); + inline bool setVoxel(VoxelType tValue); void movePositiveX(void); void movePositiveY(void); diff --git a/library/PolyVoxCore/include/PolyVoxCore/VolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/VolumeSampler.inl index 68b2e314..0dbf6ac1 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/VolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/VolumeSampler.inl @@ -91,6 +91,13 @@ namespace PolyVox mZPosInVolume = zPos; } + template + template + bool Volume::Sampler::setVoxel(VoxelType tValue) + { + return mVolume->setVoxelAt(mXPosInVolume, mYPosInVolume, mZPosInVolume, tValue); + } + template template void Volume::Sampler::movePositiveX(void) diff --git a/library/PolyVoxCore/source/Region.cpp b/library/PolyVoxCore/source/Region.cpp index 8b6491d5..fcb4f6fd 100644 --- a/library/PolyVoxCore/source/Region.cpp +++ b/library/PolyVoxCore/source/Region.cpp @@ -100,6 +100,42 @@ namespace PolyVox && (pos.getZ() >= m_v3dLowerCorner.getZ() + boundary); } + bool Region::containsPointInX(float pos, float boundary) const + { + return (pos <= m_v3dUpperCorner.getX() - boundary) + && (pos >= m_v3dLowerCorner.getX() + boundary); + } + + bool Region::containsPointInX(int32_t pos, uint8_t boundary) const + { + return (pos <= m_v3dUpperCorner.getX() - boundary) + && (pos >= m_v3dLowerCorner.getX() + boundary); + } + + bool Region::containsPointInY(float pos, float boundary) const + { + return (pos <= m_v3dUpperCorner.getY() - boundary) + && (pos >= m_v3dLowerCorner.getY() + boundary); + } + + bool Region::containsPointInY(int32_t pos, uint8_t boundary) const + { + return (pos <= m_v3dUpperCorner.getY() - boundary) + && (pos >= m_v3dLowerCorner.getY() + boundary); + } + + bool Region::containsPointInZ(float pos, float boundary) const + { + return (pos <= m_v3dUpperCorner.getZ() - boundary) + && (pos >= m_v3dLowerCorner.getZ() + boundary); + } + + bool Region::containsPointInZ(int32_t pos, uint8_t boundary) const + { + return (pos <= m_v3dUpperCorner.getZ() - boundary) + && (pos >= m_v3dLowerCorner.getZ() + boundary); + } + void Region::cropTo(const Region& other) { m_v3dLowerCorner.setX((std::max)(m_v3dLowerCorner.getX(), other.m_v3dLowerCorner.getX()));