From 152717e90436a63096cc50b672ee735d0c460947 Mon Sep 17 00:00:00 2001 From: David Williams Date: Mon, 23 Mar 2009 23:02:07 +0000 Subject: [PATCH] Work removing POLYVOX_REGION_SIDE_LENGTH from decimated surface extractor. --- .../PolyVoxImpl/DecimatedSurfaceExtractor.h | 2 +- library/include/PolyVoxCore/Region.h | 3 + .../PolyVoxImpl/DecimatedSurfaceExtractor.cpp | 89 ++++++++++--------- .../PolyVoxImpl/ReferenceSurfaceExtractor.cpp | 14 --- library/source/PolyVoxCore/Region.cpp | 15 ++++ library/source/PolyVoxCore/VoxelFilters.cpp | 6 +- 6 files changed, 69 insertions(+), 60 deletions(-) diff --git a/library/include/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.h b/library/include/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.h index dbc1c3ba..5a7e0e27 100644 --- a/library/include/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.h +++ b/library/include/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.h @@ -32,7 +32,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. namespace PolyVox { - uint32 getDecimatedIndex(uint32 x, uint32 y); + uint32 getDecimatedIndex(uint32 x, uint32 y, uint32 regionWidth); void extractDecimatedSurfaceImpl(Volume* volumeData, uint8 uLevel, Region region, IndexedSurfacePatch* singleMaterialPatch); uint32 computeInitialDecimatedBitmaskForSlice(VolumeIterator& volIter, uint8 uLevel, const Region& regSlice, const Vector3DFloat& offset, uint8 *bitmask); diff --git a/library/include/PolyVoxCore/Region.h b/library/include/PolyVoxCore/Region.h index 7d845dc9..249e702c 100644 --- a/library/include/PolyVoxCore/Region.h +++ b/library/include/PolyVoxCore/Region.h @@ -44,10 +44,13 @@ namespace PolyVox bool containsPoint(const Vector3DFloat& pos, float boundary) const; bool containsPoint(const Vector3DInt32& pos, uint8 boundary) const; void cropTo(const Region& other); + int32 depth(void) const; + int32 height(void) const; void shift(const Vector3DInt32& amount); void shiftLowerCorner(const Vector3DInt32& amount); void shiftUpperCorner(const Vector3DInt32& amount); Vector3DInt32 dimensions(void); + int32 width(void) const; private: Vector3DInt32 m_v3dLowerCorner; diff --git a/library/source/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.cpp b/library/source/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.cpp index 84964e27..c130a0de 100644 --- a/library/source/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.cpp +++ b/library/source/PolyVoxCore/PolyVoxImpl/DecimatedSurfaceExtractor.cpp @@ -34,9 +34,9 @@ using namespace std; namespace PolyVox { - uint32 getDecimatedIndex(uint32 x, uint32 y) + uint32 getDecimatedIndex(uint32 x, uint32 y , uint32 regionWidth) { - return x + (y * (POLYVOX_REGION_SIDE_LENGTH+1)); + return x + (y * (regionWidth+1)); } void extractDecimatedSurfaceImpl(Volume* volumeData, uint8 uLevel, Region region, IndexedSurfacePatch* singleMaterialPatch) @@ -44,16 +44,21 @@ namespace PolyVox singleMaterialPatch->clear(); //For edge indices - int32* vertexIndicesX0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; - int32* vertexIndicesY0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; - int32* vertexIndicesZ0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; - int32* vertexIndicesX1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; - int32* vertexIndicesY1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; - int32* vertexIndicesZ1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; + //FIXME - do the slices need to be this big? Surely for a decimated mesh they can be smaller? + //FIXME - Instead of region.width()+2 we used to use POLYVOX_REGION_SIDE_LENGTH+1 + //Normally POLYVOX_REGION_SIDE_LENGTH is the same as region.width() (often 32) but at the + //edges of the volume it is 1 smaller. Need to think what values really belong here. + Vector3DInt32 regionDimensions = region.dimensions(); + int32* vertexIndicesX0 = new int32[(region.width()+2) * (region.height()+2)]; + int32* vertexIndicesY0 = new int32[(region.width()+2) * (region.height()+2)]; + int32* vertexIndicesZ0 = new int32[(region.width()+2) * (region.height()+2)]; + int32* vertexIndicesX1 = new int32[(region.width()+2) * (region.height()+2)]; + int32* vertexIndicesY1 = new int32[(region.width()+2) * (region.height()+2)]; + int32* vertexIndicesZ1 = new int32[(region.width()+2) * (region.height()+2)]; //Cell bitmasks - uint8* bitmask0 = new uint8[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; - uint8* bitmask1 = new uint8[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)]; + uint8* bitmask0 = new uint8[(region.width()+2) * (region.height()+2)]; + uint8* bitmask1 = new uint8[(region.width()+2) * (region.height()+2)]; const uint8 uStepSize = uLevel == 0 ? 1 : 1 << uLevel; @@ -83,7 +88,7 @@ namespace PolyVox generateDecimatedVerticesForSlice(volIter, uLevel, regSlice0, offset, bitmask0, singleMaterialPatch, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0); } - for(uint32 uSlice = 1; ((uSlice <= POLYVOX_REGION_SIDE_LENGTH) && (uSlice + offset.getZ() <= regVolume.getUpperCorner().getZ())); uSlice += uStepSize) + for(uint32 uSlice = 1; ((uSlice <= region.depth()) && (uSlice + offset.getZ() <= regVolume.getUpperCorner().getZ())); uSlice += uStepSize) { Region regSlice1(regSlice0); regSlice1.shift(Vector3DInt32(0,0,uStepSize)); @@ -186,7 +191,7 @@ namespace PolyVox const uint8 v111 = volIter.getSubSampledVoxel(uLevel); //x - uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY())]; + uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)]; uint8 srcBit6 = iPreviousCubeIndexX & 64; uint8 destBit7 = srcBit6 << 1; @@ -221,7 +226,7 @@ namespace PolyVox const uint8 v111 = volIter.getSubSampledVoxel(uLevel); //y - uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize)]; + uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)]; uint8 srcBit7 = iPreviousCubeIndexY & 128; uint8 destBit4 = srcBit7 >> 3; @@ -252,7 +257,7 @@ namespace PolyVox const uint8 v111 = volIter.getSubSampledVoxel(uLevel); //y - uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize)]; + uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)]; uint8 srcBit7 = iPreviousCubeIndexY & 128; uint8 destBit4 = srcBit7 >> 3; @@ -266,7 +271,7 @@ namespace PolyVox uint8 destBit1 = srcBit2 >> 1; //x - uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY())]; + uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)]; srcBit6 = iPreviousCubeIndexX & 64; uint8 destBit7 = srcBit6 << 1; @@ -284,7 +289,7 @@ namespace PolyVox } //Save the bitmask - bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())] = iCubeIndex; + bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)] = iCubeIndex; if(edgeTable[iCubeIndex] != 0) { @@ -325,7 +330,7 @@ namespace PolyVox const uint8 v111 = volIter.getSubSampledVoxel(uLevel); //z - uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())]; + uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)]; iCubeIndex = iPreviousCubeIndexZ >> 4; if (v001 == 0) iCubeIndex |= 16; @@ -341,11 +346,11 @@ namespace PolyVox const uint8 v111 = volIter.getSubSampledVoxel(uLevel); //z - uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())]; + uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)]; iCubeIndex = iPreviousCubeIndexZ >> 4; //x - uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY())]; + uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)]; uint8 srcBit6 = iPreviousCubeIndexX & 64; uint8 destBit7 = srcBit6 << 1; @@ -365,11 +370,11 @@ namespace PolyVox const uint8 v111 = volIter.getSubSampledVoxel(uLevel); //z - uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())]; + uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)]; iCubeIndex = iPreviousCubeIndexZ >> 4; //y - uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize)]; + uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)]; uint8 srcBit7 = iPreviousCubeIndexY & 128; uint8 destBit4 = srcBit7 >> 3; @@ -387,11 +392,11 @@ namespace PolyVox const uint8 v111 = volIter.getSubSampledVoxel(uLevel); //z - uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())]; + uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)]; iCubeIndex = iPreviousCubeIndexZ >> 4; //y - uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize)]; + uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)]; uint8 srcBit7 = iPreviousCubeIndexY & 128; uint8 destBit4 = srcBit7 >> 3; @@ -399,7 +404,7 @@ namespace PolyVox uint8 destBit5 = srcBit6 >> 1; //x - uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY())]; + uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)]; srcBit6 = iPreviousCubeIndexX & 64; uint8 destBit7 = srcBit6 << 1; @@ -410,7 +415,7 @@ namespace PolyVox } //Save the bitmask - bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())] = iCubeIndex; + bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)] = iCubeIndex; if(edgeTable[iCubeIndex] != 0) { @@ -439,7 +444,7 @@ namespace PolyVox const uint8 v000 = volIter.getSubSampledVoxel(uLevel); //Determine the index into the edge table which tells us which vertices are inside of the surface - uint8 iCubeIndex = bitmask[getDecimatedIndex(x - offset.getX(),y - offset.getY())]; + uint8 iCubeIndex = bitmask[getDecimatedIndex(x - offset.getX(),y - offset.getY(), regSlice.width()+1)]; /* Cube is entirely in/out of the surface */ if (edgeTable[iCubeIndex] == 0) @@ -459,7 +464,7 @@ namespace PolyVox const uint8 uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max. SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); - vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = uLastVertexIndex; + vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY(), regSlice.width()+1)] = uLastVertexIndex; } } if (edgeTable[iCubeIndex] & 8) @@ -473,7 +478,7 @@ namespace PolyVox const uint8 uMaterial = v000 | v010; //Because one of these is 0, the or operation takes the max. SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); - vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = uLastVertexIndex; + vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY(), regSlice.width()+1)] = uLastVertexIndex; } } if (edgeTable[iCubeIndex] & 256) @@ -487,7 +492,7 @@ namespace PolyVox const uint8 uMaterial = v000 | v001; //Because one of these is 0, the or operation takes the max. const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); uint32 uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); - vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = uLastVertexIndex; + vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY(), regSlice.width()+1)] = uLastVertexIndex; } } }//For each cell @@ -507,7 +512,7 @@ namespace PolyVox const uint16 z = regSlice.getLowerCorner().getZ() - offset.getZ(); //Determine the index into the edge table which tells us which vertices are inside of the surface - uint8 iCubeIndex = bitmask0[getDecimatedIndex(x,y)]; + uint8 iCubeIndex = bitmask0[getDecimatedIndex(x,y, regSlice.width()+1)]; /* Cube is entirely in/out of the surface */ if (edgeTable[iCubeIndex] == 0) @@ -518,62 +523,62 @@ namespace PolyVox /* Find the vertices where the surface intersects the cube */ if (edgeTable[iCubeIndex] & 1) { - indlist[0] = vertexIndicesX0[getDecimatedIndex(x,y)]; + indlist[0] = vertexIndicesX0[getDecimatedIndex(x,y, regSlice.width()+1)]; assert(indlist[0] != -1); } if (edgeTable[iCubeIndex] & 2) { - indlist[1] = vertexIndicesY0[getDecimatedIndex(x+uStepSize,y)]; + indlist[1] = vertexIndicesY0[getDecimatedIndex(x+uStepSize,y, regSlice.width()+1)]; assert(indlist[1] != -1); } if (edgeTable[iCubeIndex] & 4) { - indlist[2] = vertexIndicesX0[getDecimatedIndex(x,y+uStepSize)]; + indlist[2] = vertexIndicesX0[getDecimatedIndex(x,y+uStepSize, regSlice.width()+1)]; assert(indlist[2] != -1); } if (edgeTable[iCubeIndex] & 8) { - indlist[3] = vertexIndicesY0[getDecimatedIndex(x,y)]; + indlist[3] = vertexIndicesY0[getDecimatedIndex(x,y, regSlice.width()+1)]; assert(indlist[3] != -1); } if (edgeTable[iCubeIndex] & 16) { - indlist[4] = vertexIndicesX1[getDecimatedIndex(x,y)]; + indlist[4] = vertexIndicesX1[getDecimatedIndex(x,y, regSlice.width()+1)]; assert(indlist[4] != -1); } if (edgeTable[iCubeIndex] & 32) { - indlist[5] = vertexIndicesY1[getDecimatedIndex(x+uStepSize,y)]; + indlist[5] = vertexIndicesY1[getDecimatedIndex(x+uStepSize,y, regSlice.width()+1)]; assert(indlist[5] != -1); } if (edgeTable[iCubeIndex] & 64) { - indlist[6] = vertexIndicesX1[getDecimatedIndex(x,y+uStepSize)]; + indlist[6] = vertexIndicesX1[getDecimatedIndex(x,y+uStepSize, regSlice.width()+1)]; assert(indlist[6] != -1); } if (edgeTable[iCubeIndex] & 128) { - indlist[7] = vertexIndicesY1[getDecimatedIndex(x,y)]; + indlist[7] = vertexIndicesY1[getDecimatedIndex(x,y, regSlice.width()+1)]; assert(indlist[7] != -1); } if (edgeTable[iCubeIndex] & 256) { - indlist[8] = vertexIndicesZ0[getDecimatedIndex(x,y)]; + indlist[8] = vertexIndicesZ0[getDecimatedIndex(x,y, regSlice.width()+1)]; assert(indlist[8] != -1); } if (edgeTable[iCubeIndex] & 512) { - indlist[9] = vertexIndicesZ0[getDecimatedIndex(x+uStepSize,y)]; + indlist[9] = vertexIndicesZ0[getDecimatedIndex(x+uStepSize,y, regSlice.width()+1)]; assert(indlist[9] != -1); } if (edgeTable[iCubeIndex] & 1024) { - indlist[10] = vertexIndicesZ0[getDecimatedIndex(x+uStepSize,y+uStepSize)]; + indlist[10] = vertexIndicesZ0[getDecimatedIndex(x+uStepSize,y+uStepSize, regSlice.width()+1)]; assert(indlist[10] != -1); } if (edgeTable[iCubeIndex] & 2048) { - indlist[11] = vertexIndicesZ0[getDecimatedIndex(x,y+uStepSize)]; + indlist[11] = vertexIndicesZ0[getDecimatedIndex(x,y+uStepSize, regSlice.width()+1)]; assert(indlist[11] != -1); } diff --git a/library/source/PolyVoxCore/PolyVoxImpl/ReferenceSurfaceExtractor.cpp b/library/source/PolyVoxCore/PolyVoxImpl/ReferenceSurfaceExtractor.cpp index c94e215a..c8ccb7f5 100644 --- a/library/source/PolyVoxCore/PolyVoxImpl/ReferenceSurfaceExtractor.cpp +++ b/library/source/PolyVoxCore/PolyVoxImpl/ReferenceSurfaceExtractor.cpp @@ -278,13 +278,6 @@ namespace PolyVox int32 getIndexFor(const Vector3DFloat& pos, const Vector3DInt32& regionDimensions, const std::vector& vertexIndicesX, const std::vector& vertexIndicesY, const std::vector& vertexIndicesZ) { - assert(pos.getX() >= 0.0f); - assert(pos.getY() >= 0.0f); - assert(pos.getZ() >= 0.0f); - assert(pos.getX() <= POLYVOX_REGION_SIDE_LENGTH); - assert(pos.getY() <= POLYVOX_REGION_SIDE_LENGTH); - assert(pos.getZ() <= POLYVOX_REGION_SIDE_LENGTH); - float xIntPartAsFloat; float xFracPart = std::modf(pos.getX(), &xIntPartAsFloat); float yIntPartAsFloat; @@ -313,13 +306,6 @@ namespace PolyVox void setIndexFor(const Vector3DFloat& pos, const Vector3DInt32& regionDimensions, int32 newIndex, std::vector& vertexIndicesX, std::vector& vertexIndicesY, std::vector& vertexIndicesZ) { - assert(pos.getX() >= 0.0f); - assert(pos.getY() >= 0.0f); - assert(pos.getZ() >= 0.0f); - assert(pos.getX() <= POLYVOX_REGION_SIDE_LENGTH); - assert(pos.getY() <= POLYVOX_REGION_SIDE_LENGTH); - assert(pos.getZ() <= POLYVOX_REGION_SIDE_LENGTH); - float xIntPartAsFloat; float xFracPart = std::modf(pos.getX(), &xIntPartAsFloat); float yIntPartAsFloat; diff --git a/library/source/PolyVoxCore/Region.cpp b/library/source/PolyVoxCore/Region.cpp index 36d35949..247b2cdb 100644 --- a/library/source/PolyVoxCore/Region.cpp +++ b/library/source/PolyVoxCore/Region.cpp @@ -64,6 +64,16 @@ namespace PolyVox m_v3dUpperCorner.setZ((std::min)(m_v3dUpperCorner.getZ(), other.m_v3dUpperCorner.getZ())); } + int32 Region::depth(void) const + { + return m_v3dUpperCorner.getZ() - m_v3dLowerCorner.getZ(); + } + + int32 Region::height(void) const + { + return m_v3dUpperCorner.getY() - m_v3dLowerCorner.getY(); + } + void Region::shift(const Vector3DInt32& amount) { m_v3dLowerCorner += amount; @@ -84,4 +94,9 @@ namespace PolyVox { return m_v3dUpperCorner - m_v3dLowerCorner; } + + int32 Region::width(void) const + { + return m_v3dUpperCorner.getX() - m_v3dLowerCorner.getX(); + } } diff --git a/library/source/PolyVoxCore/VoxelFilters.cpp b/library/source/PolyVoxCore/VoxelFilters.cpp index cd191001..3c54c29c 100644 --- a/library/source/PolyVoxCore/VoxelFilters.cpp +++ b/library/source/PolyVoxCore/VoxelFilters.cpp @@ -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().getSideLength() - 2); + assert(volIter.getPosY() <= volIter.getVolume().getSideLength() - 2); + assert(volIter.getPosZ() <= volIter.getVolume().getSideLength() - 2); float sum = 0.0;