From 3acd9e5553f8452a75002353cb1d80d7ba0f56fd Mon Sep 17 00:00:00 2001 From: David Williams Date: Wed, 20 May 2009 22:56:25 +0000 Subject: [PATCH] Added decimated mesh support to new surface extractor. Integrated with Thermite. --- examples/OpenGL/OpenGLWidget.cpp | 7 +- .../include/PolyVoxForwardDeclarations.h | 1 + .../PolyVoxCore/include/SurfaceExtractor.h | 15 +- .../PolyVoxCore/source/SurfaceExtractor.cpp | 552 +++++++++++++++++- 4 files changed, 554 insertions(+), 21 deletions(-) diff --git a/examples/OpenGL/OpenGLWidget.cpp b/examples/OpenGL/OpenGLWidget.cpp index d2f33202..7f2132f5 100644 --- a/examples/OpenGL/OpenGLWidget.cpp +++ b/examples/OpenGL/OpenGLWidget.cpp @@ -4,6 +4,7 @@ #include "GradientEstimators.h" #include "SurfaceAdjusters.h" +#include "SurfaceExtractor.h" //Some namespaces we need using namespace std; @@ -37,6 +38,9 @@ void OpenGLWidget::setVolume(PolyVox::Volume* volData) m_uVolumeHeightInRegions = volData->getHeight() / m_uRegionSideLength; m_uVolumeDepthInRegions = volData->getDepth() / m_uRegionSideLength; + SurfaceExtractor surfaceExtractor(*volData); + surfaceExtractor.setLodLevel(0); + //Our volume is broken down into cuboid regions, and we create one mesh for each region. //This three-level for loop iterates over each region. for(PolyVox::uint16_t uRegionZ = 0; uRegionZ < m_uVolumeDepthInRegions; ++uRegionZ) @@ -64,7 +68,8 @@ void OpenGLWidget::setVolume(PolyVox::Volume* volData) Vector3DInt32 regUpperCorner(regionEndX, regionEndY, regionEndZ); //Extract the surface for this region - extractSurface(m_volData, 0, PolyVox::Region(regLowerCorner, regUpperCorner), ispCurrent); + //extractSurface(m_volData, 0, PolyVox::Region(regLowerCorner, regUpperCorner), ispCurrent); + surfaceExtractor.extractSurfaceForRegion(PolyVox::Region(regLowerCorner, regUpperCorner), ispCurrent); //computeNormalsForVertices(m_volData, *ispCurrent, SOBEL_SMOOTHED); //*ispCurrent = getSmoothedSurface(*ispCurrent); diff --git a/library/PolyVoxCore/include/PolyVoxForwardDeclarations.h b/library/PolyVoxCore/include/PolyVoxForwardDeclarations.h index 045ba129..7d0ab52a 100644 --- a/library/PolyVoxCore/include/PolyVoxForwardDeclarations.h +++ b/library/PolyVoxCore/include/PolyVoxForwardDeclarations.h @@ -38,6 +38,7 @@ namespace PolyVox class IndexedSurfacePatch; class Region; class SurfaceVertex; + class SurfaceExtractor; //---------- Vector ---------- template class Vector; diff --git a/library/PolyVoxCore/include/SurfaceExtractor.h b/library/PolyVoxCore/include/SurfaceExtractor.h index 3b6c3464..3a0b2b05 100644 --- a/library/PolyVoxCore/include/SurfaceExtractor.h +++ b/library/PolyVoxCore/include/SurfaceExtractor.h @@ -51,10 +51,17 @@ namespace PolyVox uint32_t getIndex(uint32_t x, uint32_t y, uint32_t regionWidth); - void extractFastSurfaceImpl(Volume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); - uint32_t computeRoughBitmaskForSlice(VolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t *bitmask, uint8_t *previousBitmask); - void generateRoughIndicesForSlice(VolumeIterator& volIter, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8_t* bitmask0, uint8_t* bitmask1, int32_t vertexIndicesX0[],int32_t vertexIndicesY0[],int32_t vertexIndicesZ0[], int32_t vertexIndicesX1[],int32_t vertexIndicesY1[],int32_t vertexIndicesZ1[]); - void generateRoughVerticesForSlice(VolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32_t vertexIndicesX[],int32_t vertexIndicesY[],int32_t vertexIndicesZ[]); + void extractSurfaceForRegionLevel0(Volume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); + uint32_t computeBitmaskForSliceLevel0(VolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t *bitmask, uint8_t *previousBitmask); + void generateIndicesForSliceLevel0(VolumeIterator& volIter, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8_t* bitmask0, uint8_t* bitmask1, int32_t vertexIndicesX0[],int32_t vertexIndicesY0[],int32_t vertexIndicesZ0[], int32_t vertexIndicesX1[],int32_t vertexIndicesY1[],int32_t vertexIndicesZ1[]); + void generateVerticesForSliceLevel0(VolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32_t vertexIndicesX[],int32_t vertexIndicesY[],int32_t vertexIndicesZ[]); + + uint32_t getDecimatedIndex(uint32_t x, uint32_t y, uint32_t regionWidth); + + void extractDecimatedSurfaceImpl(Volume* volumeData, uint8_t uLevel, Region region, IndexedSurfacePatch* singleMaterialPatch); + uint32_t computeDecimatedBitmaskForSlice(VolumeIterator& volIter, uint8_t uLevel, const Region& regSlice, const Vector3DFloat& offset, uint8_t *bitmask, uint8_t *previousBitmask); + void generateDecimatedIndicesForSlice(VolumeIterator& volIter, uint8_t uLevel, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8_t* bitmask0, uint8_t* bitmask1, int32_t vertexIndicesX0[],int32_t vertexIndicesY0[],int32_t vertexIndicesZ0[], int32_t vertexIndicesX1[],int32_t vertexIndicesY1[],int32_t vertexIndicesZ1[]); + void generateDecimatedVerticesForSlice(VolumeIterator& volIter, uint8_t uLevel, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32_t vertexIndicesX[],int32_t vertexIndicesY[],int32_t vertexIndicesZ[]); }; } diff --git a/library/PolyVoxCore/source/SurfaceExtractor.cpp b/library/PolyVoxCore/source/SurfaceExtractor.cpp index b123eabf..5179e99f 100644 --- a/library/PolyVoxCore/source/SurfaceExtractor.cpp +++ b/library/PolyVoxCore/source/SurfaceExtractor.cpp @@ -25,12 +25,27 @@ namespace PolyVox void SurfaceExtractor::extractSurfaceForRegion(Region region, IndexedSurfacePatch* singleMaterialPatch) { - extractFastSurfaceImpl(&m_volData, region, singleMaterialPatch); - + if(m_uLodLevel == 0) + { + extractSurfaceForRegionLevel0(&m_volData, region, singleMaterialPatch); + } + else + { + extractDecimatedSurfaceImpl(&m_volData, m_uLodLevel, region, singleMaterialPatch); + } singleMaterialPatch->m_v3dRegionPosition = region.getLowerCorner(); } - void SurfaceExtractor::extractFastSurfaceImpl(Volume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch) + uint32_t SurfaceExtractor::getIndex(uint32_t x, uint32_t y, uint32_t regionWidth) + { + return x + (y * (regionWidth+1)); + } + + //////////////////////////////////////////////////////////////////////////////// + // Level 0 + //////////////////////////////////////////////////////////////////////////////// + + void SurfaceExtractor::extractSurfaceForRegionLevel0(Volume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch) { singleMaterialPatch->clear(); @@ -63,11 +78,11 @@ namespace PolyVox VolumeIterator volIter(*volumeData); //Compute bitmask for initial slice - uint32_t uNoOfNonEmptyCellsForSlice0 = computeRoughBitmaskForSlice(volIter, regSlice0, offset, bitmask0, 0); + uint32_t uNoOfNonEmptyCellsForSlice0 = computeBitmaskForSliceLevel0(volIter, regSlice0, offset, bitmask0, 0); if(uNoOfNonEmptyCellsForSlice0 != 0) { //If there were some non-empty cells then generate initial slice vertices for them - generateRoughVerticesForSlice(volIter,regSlice0, offset, bitmask0, singleMaterialPatch, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0); + generateVerticesForSliceLevel0(volIter,regSlice0, offset, bitmask0, singleMaterialPatch, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0); } for(uint32_t uSlice = 0; ((uSlice < region.depth()) && (uSlice + offset.getZ() < region.getUpperCorner().getZ())); ++uSlice) @@ -75,16 +90,16 @@ namespace PolyVox Region regSlice1(regSlice0); regSlice1.shift(Vector3DInt32(0,0,1)); - uint32_t uNoOfNonEmptyCellsForSlice1 = computeRoughBitmaskForSlice(volIter, regSlice1, offset, bitmask1, bitmask0); + uint32_t uNoOfNonEmptyCellsForSlice1 = computeBitmaskForSliceLevel0(volIter, regSlice1, offset, bitmask1, bitmask0); if(uNoOfNonEmptyCellsForSlice1 != 0) { - generateRoughVerticesForSlice(volIter,regSlice1, offset, bitmask1, singleMaterialPatch, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1); + generateVerticesForSliceLevel0(volIter,regSlice1, offset, bitmask1, singleMaterialPatch, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1); } if((uNoOfNonEmptyCellsForSlice0 != 0) || (uNoOfNonEmptyCellsForSlice1 != 0)) { - generateRoughIndicesForSlice(volIter, regSlice0, singleMaterialPatch, offset, bitmask0, bitmask1, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1); + generateIndicesForSliceLevel0(volIter, regSlice0, singleMaterialPatch, offset, bitmask0, bitmask1, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1); } std::swap(uNoOfNonEmptyCellsForSlice0, uNoOfNonEmptyCellsForSlice1); @@ -106,12 +121,7 @@ namespace PolyVox delete[] vertexIndicesZ1; } - uint32_t SurfaceExtractor::getIndex(uint32_t x, uint32_t y, uint32_t regionWidth) - { - return x + (y * (regionWidth+1)); - } - - uint32_t SurfaceExtractor::computeRoughBitmaskForSlice(VolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, uint8_t* previousBitmask) + uint32_t SurfaceExtractor::computeBitmaskForSliceLevel0(VolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, uint8_t* previousBitmask) { uint32_t uNoOfNonEmptyCells = 0; @@ -349,7 +359,7 @@ namespace PolyVox return uNoOfNonEmptyCells; } - void SurfaceExtractor::generateRoughVerticesForSlice(VolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32_t vertexIndicesX[],int32_t vertexIndicesY[],int32_t vertexIndicesZ[]) + void SurfaceExtractor::generateVerticesForSliceLevel0(VolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32_t vertexIndicesX[],int32_t vertexIndicesY[],int32_t vertexIndicesZ[]) { //Iterate over each cell in the region for(uint16_t uYVolSpace = regSlice.getLowerCorner().getY(); uYVolSpace <= regSlice.getUpperCorner().getY(); uYVolSpace++) @@ -424,7 +434,7 @@ namespace PolyVox } } - void SurfaceExtractor::generateRoughIndicesForSlice(VolumeIterator& volIter, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8_t* bitmask0, uint8_t* bitmask1, int32_t vertexIndicesX0[],int32_t vertexIndicesY0[],int32_t vertexIndicesZ0[], int32_t vertexIndicesX1[],int32_t vertexIndicesY1[],int32_t vertexIndicesZ1[]) + void SurfaceExtractor::generateIndicesForSliceLevel0(VolumeIterator& volIter, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8_t* bitmask0, uint8_t* bitmask1, int32_t vertexIndicesX0[],int32_t vertexIndicesY0[],int32_t vertexIndicesZ0[], int32_t vertexIndicesX1[],int32_t vertexIndicesY1[],int32_t vertexIndicesZ1[]) { uint32_t indlist[12]; @@ -535,4 +545,514 @@ namespace PolyVox } } } + + //////////////////////////////////////////////////////////////////////////////// + // Level 1 + //////////////////////////////////////////////////////////////////////////////// + + uint32_t SurfaceExtractor::getDecimatedIndex(uint32_t x, uint32_t y , uint32_t regionWidth) + { + return x + (y * (regionWidth+1)); + } + + void SurfaceExtractor::extractDecimatedSurfaceImpl(Volume* volumeData, uint8_t uLevel, Region region, IndexedSurfacePatch* singleMaterialPatch) + { + singleMaterialPatch->clear(); + + //For edge indices + //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. + int32_t* vertexIndicesX0 = new int32_t[(region.width()+2) * (region.height()+2)]; + int32_t* vertexIndicesY0 = new int32_t[(region.width()+2) * (region.height()+2)]; + int32_t* vertexIndicesZ0 = new int32_t[(region.width()+2) * (region.height()+2)]; + int32_t* vertexIndicesX1 = new int32_t[(region.width()+2) * (region.height()+2)]; + int32_t* vertexIndicesY1 = new int32_t[(region.width()+2) * (region.height()+2)]; + int32_t* vertexIndicesZ1 = new int32_t[(region.width()+2) * (region.height()+2)]; + + //Cell bitmasks + uint8_t* bitmask0 = new uint8_t[(region.width()+2) * (region.height()+2)]; + uint8_t* bitmask1 = new uint8_t[(region.width()+2) * (region.height()+2)]; + + const uint8_t uStepSize = uLevel == 0 ? 1 : 1 << uLevel; + + //When generating the mesh for a region we actually look outside it in the + // back, bottom, right direction. Protect against access violations by cropping region here + Region regVolume = volumeData->getEnclosingRegion(); + regVolume.setUpperCorner(regVolume.getUpperCorner() - Vector3DInt32(2*uStepSize-1,2*uStepSize-1,2*uStepSize-1)); + region.cropTo(regVolume); + + //Offset from volume corner + const Vector3DFloat offset = static_cast(region.getLowerCorner()); + + //Create a region corresponding to the first slice + Region regSlice0(region); + Vector3DInt32 v3dUpperCorner = regSlice0.getUpperCorner(); + v3dUpperCorner.setZ(regSlice0.getLowerCorner().getZ()); //Set the upper z to the lower z to make it one slice thick. + regSlice0.setUpperCorner(v3dUpperCorner); + + //Iterator to access the volume data + VolumeIterator volIter(*volumeData); + + //Compute bitmask for initial slice + uint32_t uNoOfNonEmptyCellsForSlice0 = computeDecimatedBitmaskForSlice(volIter, uLevel, regSlice0, offset, bitmask0, 0); + if(uNoOfNonEmptyCellsForSlice0 != 0) + { + //If there were some non-empty cells then generate initial slice vertices for them + generateDecimatedVerticesForSlice(volIter, uLevel, regSlice0, offset, bitmask0, singleMaterialPatch, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0); + } + + for(uint32_t uSlice = 1; ((uSlice <= region.depth()) && (uSlice + offset.getZ() <= regVolume.getUpperCorner().getZ())); uSlice += uStepSize) + { + Region regSlice1(regSlice0); + regSlice1.shift(Vector3DInt32(0,0,uStepSize)); + + uint32_t uNoOfNonEmptyCellsForSlice1 = computeDecimatedBitmaskForSlice(volIter, uLevel, regSlice1, offset, bitmask1, bitmask0); + + if(uNoOfNonEmptyCellsForSlice1 != 0) + { + generateDecimatedVerticesForSlice(volIter, uLevel, regSlice1, offset, bitmask1, singleMaterialPatch, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1); + } + + if((uNoOfNonEmptyCellsForSlice0 != 0) || (uNoOfNonEmptyCellsForSlice1 != 0)) + { + generateDecimatedIndicesForSlice(volIter, uLevel, regSlice0, singleMaterialPatch, offset, bitmask0, bitmask1, vertexIndicesX0, vertexIndicesY0, vertexIndicesZ0, vertexIndicesX1, vertexIndicesY1, vertexIndicesZ1); + } + + std::swap(uNoOfNonEmptyCellsForSlice0, uNoOfNonEmptyCellsForSlice1); + std::swap(bitmask0, bitmask1); + std::swap(vertexIndicesX0, vertexIndicesX1); + std::swap(vertexIndicesY0, vertexIndicesY1); + std::swap(vertexIndicesZ0, vertexIndicesZ1); + + regSlice0 = regSlice1; + } + + delete[] bitmask0; + delete[] bitmask1; + delete[] vertexIndicesX0; + delete[] vertexIndicesX1; + delete[] vertexIndicesY0; + delete[] vertexIndicesY1; + delete[] vertexIndicesZ0; + delete[] vertexIndicesZ1; + + + /*std::vector::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin(); + while(iterSurfaceVertex != singleMaterialPatch->getVertices().end()) + { + Vector3DFloat tempNormal = computeDecimatedNormal(volumeData, static_cast(iterSurfaceVertex->getPosition() + offset), CENTRAL_DIFFERENCE); + const_cast(*iterSurfaceVertex).setNormal(tempNormal); + ++iterSurfaceVertex; + }*/ + } + + uint32_t SurfaceExtractor::computeDecimatedBitmaskForSlice(VolumeIterator& volIter, uint8_t uLevel, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, uint8_t* previousBitmask) + { + const uint8_t uStepSize = uLevel == 0 ? 1 : 1 << uLevel; + uint32_t uNoOfNonEmptyCells = 0; + + //Iterate over each cell in the region + for(uint16_t uYVolSpace = regSlice.getLowerCorner().getY(); uYVolSpace <= regSlice.getUpperCorner().getY(); uYVolSpace += uStepSize) + { + for(uint16_t uXVolSpace = regSlice.getLowerCorner().getX(); uXVolSpace <= regSlice.getUpperCorner().getX(); uXVolSpace += uStepSize) + { + uint16_t uZVolSpace = regSlice.getLowerCorner().getZ(); + //Current position + volIter.setPosition(uXVolSpace,uYVolSpace,uZVolSpace); + + const uint16_t uXRegSpace = volIter.getPosX() - offset.getX(); + const uint16_t uYRegSpace = volIter.getPosY() - offset.getY(); + + //Determine the index into the edge table which tells us which vertices are inside of the surface + uint8_t iCubeIndex = 0; + + bool isPrevXAvail = uXRegSpace > 0; + bool isPrevYAvail = uYRegSpace > 0; + bool isPrevZAvail = previousBitmask != 0; + + if(isPrevZAvail) + { + if(isPrevYAvail) + { + if(isPrevXAvail) + { + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v111 = volIter.getSubSampledVoxel(uLevel); + + //z + uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(uXRegSpace,uYRegSpace, regSlice.width()+1)]; + iPreviousCubeIndexZ >>= 4; + + //y + uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(uXRegSpace,uYRegSpace-uStepSize, regSlice.width()+1)]; + iPreviousCubeIndexY &= 192; //192 = 128 + 64 + iPreviousCubeIndexY >>= 2; + + //x + uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(uXRegSpace-uStepSize,uYRegSpace, regSlice.width()+1)]; + iPreviousCubeIndexX &= 128; + iPreviousCubeIndexX >>= 1; + + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY | iPreviousCubeIndexZ; + + if (v111 == 0) iCubeIndex |= 128; + } + else //previous X not available + { + volIter.setPosition(uXVolSpace,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v011 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v111 = volIter.getSubSampledVoxel(uLevel); + + //z + uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(uXRegSpace,uYRegSpace, regSlice.width()+1)]; + iPreviousCubeIndexZ >>= 4; + + //y + uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(uXRegSpace,uYRegSpace-uStepSize, regSlice.width()+1)]; + iPreviousCubeIndexY &= 192; //192 = 128 + 64 + iPreviousCubeIndexY >>= 2; + + iCubeIndex = iPreviousCubeIndexY | iPreviousCubeIndexZ; + + if (v011 == 0) iCubeIndex |= 64; + if (v111 == 0) iCubeIndex |= 128; + } + } + else //previous Y not available + { + if(isPrevXAvail) + { + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace,uZVolSpace+uStepSize); + const uint8_t v101 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v111 = volIter.getSubSampledVoxel(uLevel); + + //z + uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(uXRegSpace,uYRegSpace, regSlice.width()+1)]; + iPreviousCubeIndexZ >>= 4; + + //x + uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(uXRegSpace-uStepSize,uYRegSpace, regSlice.width()+1)]; + iPreviousCubeIndexX &= 160; //160 = 128+32 + iPreviousCubeIndexX >>= 1; + + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexZ; + + if (v101 == 0) iCubeIndex |= 32; + if (v111 == 0) iCubeIndex |= 128; + } + else //previous X not available + { + volIter.setPosition(uXVolSpace,uYVolSpace,uZVolSpace+uStepSize); + const uint8_t v001 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace,uZVolSpace+uStepSize); + const uint8_t v101 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v011 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v111 = volIter.getSubSampledVoxel(uLevel); + + //z + uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(uXRegSpace,uYRegSpace, regSlice.width()+1)]; + iCubeIndex = iPreviousCubeIndexZ >> 4; + + if (v001 == 0) iCubeIndex |= 16; + if (v101 == 0) iCubeIndex |= 32; + if (v011 == 0) iCubeIndex |= 64; + if (v111 == 0) iCubeIndex |= 128; + } + } + } + else //previous Z not available + { + if(isPrevYAvail) + { + if(isPrevXAvail) + { + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace); + const uint8_t v110 = volIter.getSubSampledVoxel(uLevel); + + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v111 = volIter.getSubSampledVoxel(uLevel); + + //y + uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(uXRegSpace,uYRegSpace-uStepSize, regSlice.width()+1)]; + iPreviousCubeIndexY &= 204; //204 = 128+64+8+4 + iPreviousCubeIndexY >>= 2; + + //x + uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(uXRegSpace-uStepSize,uYRegSpace, regSlice.width()+1)]; + iPreviousCubeIndexX &= 170; //170 = 128+32+8+2 + iPreviousCubeIndexX >>= 1; + + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY; + + if (v110 == 0) iCubeIndex |= 8; + if (v111 == 0) iCubeIndex |= 128; + } + else //previous X not available + { + volIter.setPosition(uXVolSpace,uYVolSpace+uStepSize,uZVolSpace); + const uint8_t v010 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace); + const uint8_t v110 = volIter.getSubSampledVoxel(uLevel); + + volIter.setPosition(uXVolSpace,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v011 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v111 = volIter.getSubSampledVoxel(uLevel); + + //y + uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(uXRegSpace,uYRegSpace-uStepSize, regSlice.width()+1)]; + iPreviousCubeIndexY &= 204; //204 = 128+64+8+4 + iPreviousCubeIndexY >>= 2; + + iCubeIndex = iPreviousCubeIndexY; + + if (v010 == 0) iCubeIndex |= 4; + if (v110 == 0) iCubeIndex |= 8; + if (v011 == 0) iCubeIndex |= 64; + if (v111 == 0) iCubeIndex |= 128; + } + } + else //previous Y not available + { + if(isPrevXAvail) + { + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace,uZVolSpace); + const uint8_t v100 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace); + const uint8_t v110 = volIter.getSubSampledVoxel(uLevel); + + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace,uZVolSpace+uStepSize); + const uint8_t v101 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v111 = volIter.getSubSampledVoxel(uLevel); + + //x + uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(uXRegSpace-uStepSize,uYRegSpace, regSlice.width()+1)]; + iPreviousCubeIndexX &= 170; //170 = 128+32+8+2 + iPreviousCubeIndexX >>= 1; + + iCubeIndex = iPreviousCubeIndexX; + + if (v100 == 0) iCubeIndex |= 2; + if (v110 == 0) iCubeIndex |= 8; + if (v101 == 0) iCubeIndex |= 32; + if (v111 == 0) iCubeIndex |= 128; + } + else //previous X not available + { + volIter.setPosition(uXVolSpace,uYVolSpace,uZVolSpace); + const uint8_t v000 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace,uZVolSpace); + const uint8_t v100 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace,uYVolSpace+uStepSize,uZVolSpace); + const uint8_t v010 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace); + const uint8_t v110 = volIter.getSubSampledVoxel(uLevel); + + volIter.setPosition(uXVolSpace,uYVolSpace,uZVolSpace+uStepSize); + const uint8_t v001 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace,uZVolSpace+uStepSize); + const uint8_t v101 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v011 = volIter.getSubSampledVoxel(uLevel); + volIter.setPosition(uXVolSpace+uStepSize,uYVolSpace+uStepSize,uZVolSpace+uStepSize); + const uint8_t v111 = volIter.getSubSampledVoxel(uLevel); + + if (v000 == 0) iCubeIndex |= 1; + if (v100 == 0) iCubeIndex |= 2; + if (v010 == 0) iCubeIndex |= 4; + if (v110 == 0) iCubeIndex |= 8; + if (v001 == 0) iCubeIndex |= 16; + if (v101 == 0) iCubeIndex |= 32; + if (v011 == 0) iCubeIndex |= 64; + if (v111 == 0) iCubeIndex |= 128; + } + } + } + + //Save the bitmask + bitmask[getDecimatedIndex(uXRegSpace,uYVolSpace- offset.getY(), regSlice.width()+1)] = iCubeIndex; + + if(edgeTable[iCubeIndex] != 0) + { + ++uNoOfNonEmptyCells; + } + + }//For each cell + } + + return uNoOfNonEmptyCells; + } + + void SurfaceExtractor::generateDecimatedVerticesForSlice(VolumeIterator& volIter, uint8_t uLevel, Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32_t vertexIndicesX[],int32_t vertexIndicesY[],int32_t vertexIndicesZ[]) + { + const uint8_t uStepSize = uLevel == 0 ? 1 : 1 << uLevel; + + //Iterate over each cell in the region + for(uint16_t y = regSlice.getLowerCorner().getY(); y <= regSlice.getUpperCorner().getY(); y += uStepSize) + { + for(uint16_t x = regSlice.getLowerCorner().getX(); x <= regSlice.getUpperCorner().getX(); x += uStepSize) + { + //Current position + const uint16_t z = regSlice.getLowerCorner().getZ(); + + volIter.setPosition(x,y,z); + const uint8_t v000 = volIter.getSubSampledVoxel(uLevel); + + //Determine the index into the edge table which tells us which vertices are inside of the surface + uint8_t iCubeIndex = bitmask[getDecimatedIndex(x - offset.getX(),y - offset.getY(), regSlice.width()+1)]; + + /* Cube is entirely in/out of the surface */ + if (edgeTable[iCubeIndex] == 0) + { + continue; + } + + /* Find the vertices where the surface intersects the cube */ + if (edgeTable[iCubeIndex] & 1) + { + if(x != regSlice.getUpperCorner().getX()) + { + volIter.setPosition(x + uStepSize,y,z); + const uint8_t v100 = volIter.getSubSampledVoxel(uLevel); + const Vector3DFloat v3dPosition(x - offset.getX() + 0.5f * uStepSize, y - offset.getY(), z - offset.getZ()); + const Vector3DFloat v3dNormal(v000 > v100 ? 1.0f : -1.0f,0.0,0.0); + const uint8_t uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max. + SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); + uint32_t uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY(), regSlice.width()+1)] = uLastVertexIndex; + } + } + if (edgeTable[iCubeIndex] & 8) + { + if(y != regSlice.getUpperCorner().getY()) + { + volIter.setPosition(x,y + uStepSize,z); + const uint8_t v010 = volIter.getSubSampledVoxel(uLevel); + const Vector3DFloat v3dPosition(x - offset.getX(), y - offset.getY() + 0.5f * uStepSize, z - offset.getZ()); + const Vector3DFloat v3dNormal(0.0,v000 > v010 ? 1.0f : -1.0f,0.0); + const uint8_t uMaterial = v000 | v010; //Because one of these is 0, the or operation takes the max. + SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); + uint32_t uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY(), regSlice.width()+1)] = uLastVertexIndex; + } + } + if (edgeTable[iCubeIndex] & 256) + { + //if(z != regSlice.getUpperCorner.getZ()) + { + volIter.setPosition(x,y,z + uStepSize); + const uint8_t v001 = volIter.getSubSampledVoxel(uLevel); + const Vector3DFloat v3dPosition(x - offset.getX(), y - offset.getY(), z - offset.getZ() + 0.5f * uStepSize); + const Vector3DFloat v3dNormal(0.0,0.0,v000 > v001 ? 1.0f : -1.0f); + const uint8_t uMaterial = v000 | v001; //Because one of these is 0, the or operation takes the max. + const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); + uint32_t uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex); + vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY(), regSlice.width()+1)] = uLastVertexIndex; + } + } + }//For each cell + } + } + + void SurfaceExtractor::generateDecimatedIndicesForSlice(VolumeIterator& volIter, uint8_t uLevel, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8_t* bitmask0, uint8_t* bitmask1, int32_t vertexIndicesX0[],int32_t vertexIndicesY0[],int32_t vertexIndicesZ0[], int32_t vertexIndicesX1[],int32_t vertexIndicesY1[],int32_t vertexIndicesZ1[]) + { + const uint8_t uStepSize = uLevel == 0 ? 1 : 1 << uLevel; + uint32_t indlist[12]; + + for(uint16_t y = regSlice.getLowerCorner().getY() - offset.getY(); y < regSlice.getUpperCorner().getY() - offset.getY(); y += uStepSize) + { + for(uint16_t x = regSlice.getLowerCorner().getX() - offset.getX(); x < regSlice.getUpperCorner().getX() - offset.getX(); x += uStepSize) + { + //Current position + const uint16_t z = regSlice.getLowerCorner().getZ() - offset.getZ(); + + //Determine the index into the edge table which tells us which vertices are inside of the surface + uint8_t iCubeIndex = bitmask0[getDecimatedIndex(x,y, regSlice.width()+1)]; + + /* Cube is entirely in/out of the surface */ + if (edgeTable[iCubeIndex] == 0) + { + continue; + } + + /* Find the vertices where the surface intersects the cube */ + if (edgeTable[iCubeIndex] & 1) + { + indlist[0] = vertexIndicesX0[getDecimatedIndex(x,y, regSlice.width()+1)]; + assert(indlist[0] != -1); + } + if (edgeTable[iCubeIndex] & 2) + { + 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, regSlice.width()+1)]; + assert(indlist[2] != -1); + } + if (edgeTable[iCubeIndex] & 8) + { + indlist[3] = vertexIndicesY0[getDecimatedIndex(x,y, regSlice.width()+1)]; + assert(indlist[3] != -1); + } + if (edgeTable[iCubeIndex] & 16) + { + indlist[4] = vertexIndicesX1[getDecimatedIndex(x,y, regSlice.width()+1)]; + assert(indlist[4] != -1); + } + if (edgeTable[iCubeIndex] & 32) + { + 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, regSlice.width()+1)]; + assert(indlist[6] != -1); + } + if (edgeTable[iCubeIndex] & 128) + { + indlist[7] = vertexIndicesY1[getDecimatedIndex(x,y, regSlice.width()+1)]; + assert(indlist[7] != -1); + } + if (edgeTable[iCubeIndex] & 256) + { + indlist[8] = vertexIndicesZ0[getDecimatedIndex(x,y, regSlice.width()+1)]; + assert(indlist[8] != -1); + } + if (edgeTable[iCubeIndex] & 512) + { + 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, regSlice.width()+1)]; + assert(indlist[10] != -1); + } + if (edgeTable[iCubeIndex] & 2048) + { + indlist[11] = vertexIndicesZ0[getDecimatedIndex(x,y+uStepSize, regSlice.width()+1)]; + assert(indlist[11] != -1); + } + + for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3) + { + uint32_t ind0 = indlist[triTable[iCubeIndex][i ]]; + uint32_t ind1 = indlist[triTable[iCubeIndex][i+1]]; + uint32_t ind2 = indlist[triTable[iCubeIndex][i+2]]; + + singleMaterialPatch->addTriangle(ind0, ind1, ind2); + }//For each triangle + }//For each cell + } + } } \ No newline at end of file