From c03c18c1e4ff5d3b6dcb718f84ba899b00ce7e9f Mon Sep 17 00:00:00 2001 From: David Williams Date: Mon, 4 May 2009 19:18:18 +0000 Subject: [PATCH] Simplification of bitwise operations in Marching Cubes algorithm. --- examples/OpenGL/OpenGLWidget.cpp | 2 +- .../PolyVoxImpl/DecimatedSurfaceExtractor.cpp | 107 +++++------------ .../PolyVoxImpl/FastSurfaceExtractor.cpp | 111 +++++------------- 3 files changed, 59 insertions(+), 161 deletions(-) diff --git a/examples/OpenGL/OpenGLWidget.cpp b/examples/OpenGL/OpenGLWidget.cpp index d2f33202..1f80298e 100644 --- a/examples/OpenGL/OpenGLWidget.cpp +++ b/examples/OpenGL/OpenGLWidget.cpp @@ -64,7 +64,7 @@ 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, 2, PolyVox::Region(regLowerCorner, regUpperCorner), ispCurrent); //computeNormalsForVertices(m_volData, *ispCurrent, SOBEL_SMOOTHED); //*ispCurrent = getSmoothedSurface(*ispCurrent); diff --git a/library/PolyVoxCore/source/PolyVoxImpl/DecimatedSurfaceExtractor.cpp b/library/PolyVoxCore/source/PolyVoxImpl/DecimatedSurfaceExtractor.cpp index 6e96a9e3..608ff4fd 100644 --- a/library/PolyVoxCore/source/PolyVoxImpl/DecimatedSurfaceExtractor.cpp +++ b/library/PolyVoxCore/source/PolyVoxImpl/DecimatedSurfaceExtractor.cpp @@ -191,25 +191,14 @@ namespace PolyVox //x uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)]; - uint8_t srcBit7 = iPreviousCubeIndexX & 128; - uint8_t destBit6 = srcBit7 >> 1; + iPreviousCubeIndexX &= 170; //170 = 128+32+8+2 + iPreviousCubeIndexX >>= 1; - uint8_t srcBit5 = iPreviousCubeIndexX & 32; - uint8_t destBit4 = srcBit5 >> 1; + iCubeIndex = iPreviousCubeIndexX; - uint8_t srcBit3 = iPreviousCubeIndexX & 8; - uint8_t destBit2 = srcBit3 >> 1; - - uint8_t srcBit1 = iPreviousCubeIndexX & 2; - uint8_t destBit0 = srcBit1 >> 1; - - iCubeIndex |= destBit0; - if (v100 == 0) iCubeIndex |= 2; - iCubeIndex |= destBit2; + if (v100 == 0) iCubeIndex |= 2; if (v110 == 0) iCubeIndex |= 8; - iCubeIndex |= destBit4; - if (v101 == 0) iCubeIndex |= 32; - iCubeIndex |= destBit6; + if (v101 == 0) iCubeIndex |= 32; if (v111 == 0) iCubeIndex |= 128; } else if((x==regSlice.getLowerCorner().getX()) && (y>regSlice.getLowerCorner().getY())) @@ -226,25 +215,13 @@ namespace PolyVox //y uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)]; - uint8_t srcBit6 = iPreviousCubeIndexY & 64; - uint8_t destBit4 = srcBit6 >> 2; + iPreviousCubeIndexY &= 204; //204 = 128+64+8+4 + iPreviousCubeIndexY >>= 2; - uint8_t srcBit7 = iPreviousCubeIndexY & 128; - uint8_t destBit5 = srcBit7 >> 2; + iCubeIndex = iPreviousCubeIndexY; - - uint8_t srcBit2 = iPreviousCubeIndexY & 4; - uint8_t destBit0 = srcBit2 >> 2; - - uint8_t srcBit3 = iPreviousCubeIndexY & 8; - uint8_t destBit1 = srcBit3 >> 2; - - iCubeIndex |= destBit0; - iCubeIndex |= destBit1; if (v010 == 0) iCubeIndex |= 4; if (v110 == 0) iCubeIndex |= 8; - iCubeIndex |= destBit4; - iCubeIndex |= destBit5; if (v011 == 0) iCubeIndex |= 64; if (v111 == 0) iCubeIndex |= 128; } @@ -258,33 +235,17 @@ namespace PolyVox //y uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)]; - uint8_t srcBit6 = iPreviousCubeIndexY & 64; - uint8_t destBit4 = srcBit6 >> 2; - - uint8_t srcBit7 = iPreviousCubeIndexY & 128; - uint8_t destBit5 = srcBit7 >> 2; - - uint8_t srcBit2 = iPreviousCubeIndexY & 4; - uint8_t destBit0 = srcBit2 >> 2; - - uint8_t srcBit3 = iPreviousCubeIndexY & 8; - uint8_t destBit1 = srcBit3 >> 2; + iPreviousCubeIndexY &= 204; //204 = 128+64+8+4 + iPreviousCubeIndexY >>= 2; //x uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)]; - srcBit7 = iPreviousCubeIndexX & 128; - uint8_t destBit6 = srcBit7 >> 1; + iPreviousCubeIndexX &= 170; //170 = 128+32+8+2 + iPreviousCubeIndexX >>= 1; - srcBit3 = iPreviousCubeIndexX & 8; - uint8_t destBit2 = srcBit3 >> 1; + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY; - iCubeIndex |= destBit0; - iCubeIndex |= destBit1; - iCubeIndex |= destBit2; if (v110 == 0) iCubeIndex |= 8; - iCubeIndex |= destBit4; - iCubeIndex |= destBit5; - iCubeIndex |= destBit6; if (v111 == 0) iCubeIndex |= 128; } @@ -347,19 +308,16 @@ namespace PolyVox //z uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)]; - iCubeIndex = iPreviousCubeIndexZ >> 4; + iPreviousCubeIndexZ >>= 4; //x uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)]; - uint8_t srcBit7 = iPreviousCubeIndexX & 128; - uint8_t destBit6 = srcBit7 >> 1; + iPreviousCubeIndexX &= 160; //160 = 128+32 + iPreviousCubeIndexX >>= 1; - uint8_t srcBit5 = iPreviousCubeIndexX & 32; - uint8_t destBit4 = srcBit5 >> 1; + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexZ; - iCubeIndex |= destBit4; - if (v101 == 0) iCubeIndex |= 32; - iCubeIndex |= destBit6; + if (v101 == 0) iCubeIndex |= 32; if (v111 == 0) iCubeIndex |= 128; } else if((x==regSlice.getLowerCorner().getX()) && (y>regSlice.getLowerCorner().getY())) @@ -371,18 +329,15 @@ namespace PolyVox //z uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)]; - iCubeIndex = iPreviousCubeIndexZ >> 4; + iPreviousCubeIndexZ >>= 4; //y uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)]; - uint8_t srcBit6 = iPreviousCubeIndexY & 64; - uint8_t destBit4 = srcBit6 >> 2; + iPreviousCubeIndexY &= 192; //192 = 128 + 64 + iPreviousCubeIndexY >>= 2; - uint8_t srcBit7 = iPreviousCubeIndexY & 128; - uint8_t destBit5 = srcBit7 >> 2; + iCubeIndex = iPreviousCubeIndexY | iPreviousCubeIndexZ; - iCubeIndex |= destBit4; - iCubeIndex |= destBit5; if (v011 == 0) iCubeIndex |= 64; if (v111 == 0) iCubeIndex |= 128; } @@ -393,24 +348,20 @@ namespace PolyVox //z uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)]; - iCubeIndex = iPreviousCubeIndexZ >> 4; + iPreviousCubeIndexZ >>= 4; //y uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)]; - uint8_t srcBit6 = iPreviousCubeIndexY & 64; - uint8_t destBit4 = srcBit6 >> 2; - - uint8_t srcBit7 = iPreviousCubeIndexY & 128; - uint8_t destBit5 = srcBit7 >> 2; + iPreviousCubeIndexY &= 192; //192 = 128 + 64 + iPreviousCubeIndexY >>= 2; //x uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)]; - srcBit7 = iPreviousCubeIndexX & 128; - uint8_t destBit6 = srcBit7 >> 1; + iPreviousCubeIndexX &= 128; + iPreviousCubeIndexX >>= 1; + + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY | iPreviousCubeIndexZ; - iCubeIndex |= destBit4; - iCubeIndex |= destBit5; - iCubeIndex |= destBit6; if (v111 == 0) iCubeIndex |= 128; } diff --git a/library/PolyVoxCore/source/PolyVoxImpl/FastSurfaceExtractor.cpp b/library/PolyVoxCore/source/PolyVoxImpl/FastSurfaceExtractor.cpp index 7c0a5cf9..870430a9 100644 --- a/library/PolyVoxCore/source/PolyVoxImpl/FastSurfaceExtractor.cpp +++ b/library/PolyVoxCore/source/PolyVoxImpl/FastSurfaceExtractor.cpp @@ -163,25 +163,14 @@ namespace PolyVox //x uint8_t iPreviousCubeIndexX = bitmask[getIndex(uXRegSpace-1,uYRegSpace, regSlice.width()+1)]; - uint8_t srcBit7 = iPreviousCubeIndexX & 128; - uint8_t destBit6 = srcBit7 >> 1; + iPreviousCubeIndexX &= 170; //170 = 128+32+8+2 + iPreviousCubeIndexX >>= 1; - uint8_t srcBit5 = iPreviousCubeIndexX & 32; - uint8_t destBit4 = srcBit5 >> 1; + iCubeIndex = iPreviousCubeIndexX; - uint8_t srcBit3 = iPreviousCubeIndexX & 8; - uint8_t destBit2 = srcBit3 >> 1; - - uint8_t srcBit1 = iPreviousCubeIndexX & 2; - uint8_t destBit0 = srcBit1 >> 1; - - iCubeIndex |= destBit0; - if (v100 == 0) iCubeIndex |= 2; - iCubeIndex |= destBit2; + if (v100 == 0) iCubeIndex |= 2; if (v110 == 0) iCubeIndex |= 8; - iCubeIndex |= destBit4; - if (v101 == 0) iCubeIndex |= 32; - iCubeIndex |= destBit6; + if (v101 == 0) iCubeIndex |= 32; if (v111 == 0) iCubeIndex |= 128; } else if((uXRegSpace==0) && (uYRegSpace>0)) @@ -194,65 +183,34 @@ namespace PolyVox //y uint8_t iPreviousCubeIndexY = bitmask[getIndex(uXRegSpace,uYRegSpace-1, regSlice.width()+1)]; - - uint8_t srcBit6 = iPreviousCubeIndexY & 64; - uint8_t destBit4 = srcBit6 >> 2; + iPreviousCubeIndexY &= 204; //204 = 128+64+8+4 + iPreviousCubeIndexY >>= 2; - uint8_t srcBit7 = iPreviousCubeIndexY & 128; - uint8_t destBit5 = srcBit7 >> 2; + iCubeIndex = iPreviousCubeIndexY; - - uint8_t srcBit2 = iPreviousCubeIndexY & 4; - uint8_t destBit0 = srcBit2 >> 2; - - uint8_t srcBit3 = iPreviousCubeIndexY & 8; - uint8_t destBit1 = srcBit3 >> 2; - - iCubeIndex |= destBit0; - iCubeIndex |= destBit1; if (v010 == 0) iCubeIndex |= 4; if (v110 == 0) iCubeIndex |= 8; - iCubeIndex |= destBit4; - iCubeIndex |= destBit5; if (v011 == 0) iCubeIndex |= 64; if (v111 == 0) iCubeIndex |= 128; } else { const uint8_t v110 = volIter.peekVoxel1px1py0pz(); - const uint8_t v111 = volIter.peekVoxel1px1py1pz(); //y uint8_t iPreviousCubeIndexY = bitmask[getIndex(uXRegSpace,uYRegSpace-1, regSlice.width()+1)]; - - uint8_t srcBit6 = iPreviousCubeIndexY & 64; - uint8_t destBit4 = srcBit6 >> 2; - - uint8_t srcBit7 = iPreviousCubeIndexY & 128; - uint8_t destBit5 = srcBit7 >> 2; - - uint8_t srcBit2 = iPreviousCubeIndexY & 4; - uint8_t destBit0 = srcBit2 >> 2; - - uint8_t srcBit3 = iPreviousCubeIndexY & 8; - uint8_t destBit1 = srcBit3 >> 2; + iPreviousCubeIndexY &= 204; //204 = 128+64+8+4 + iPreviousCubeIndexY >>= 2; //x uint8_t iPreviousCubeIndexX = bitmask[getIndex(uXRegSpace-1,uYRegSpace, regSlice.width()+1)]; - srcBit7 = iPreviousCubeIndexX & 128; - uint8_t destBit6 = srcBit7 >> 1; + iPreviousCubeIndexX &= 170; //170 = 128+32+8+2 + iPreviousCubeIndexX >>= 1; - srcBit3 = iPreviousCubeIndexX & 8; - uint8_t destBit2 = srcBit3 >> 1; + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY; - iCubeIndex |= destBit0; - iCubeIndex |= destBit1; - iCubeIndex |= destBit2; if (v110 == 0) iCubeIndex |= 8; - iCubeIndex |= destBit4; - iCubeIndex |= destBit5; - iCubeIndex |= destBit6; if (v111 == 0) iCubeIndex |= 128; } } @@ -337,19 +295,16 @@ namespace PolyVox //z uint8_t iPreviousCubeIndexZ = previousBitmask[getIndex(uXRegSpace,uYRegSpace, regSlice.width()+1)]; - iCubeIndex = iPreviousCubeIndexZ >> 4; + iPreviousCubeIndexZ >>= 4; //x uint8_t iPreviousCubeIndexX = bitmask[getIndex(uXRegSpace-1,uYRegSpace, regSlice.width()+1)]; - uint8_t srcBit7 = iPreviousCubeIndexX & 128; - uint8_t destBit6 = srcBit7 >> 1; + iPreviousCubeIndexX &= 160; //160 = 128+32 + iPreviousCubeIndexX >>= 1; - uint8_t srcBit5 = iPreviousCubeIndexX & 32; - uint8_t destBit4 = srcBit5 >> 1; + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexZ; - iCubeIndex |= destBit4; - if (v101 == 0) iCubeIndex |= 32; - iCubeIndex |= destBit6; + if (v101 == 0) iCubeIndex |= 32; if (v111 == 0) iCubeIndex |= 128; } else if((uXRegSpace==0) && (uYRegSpace>0)) @@ -359,19 +314,15 @@ namespace PolyVox //z uint8_t iPreviousCubeIndexZ = previousBitmask[getIndex(uXRegSpace,uYRegSpace, regSlice.width()+1)]; - iCubeIndex = iPreviousCubeIndexZ >> 4; + iPreviousCubeIndexZ >>= 4; //y uint8_t iPreviousCubeIndexY = bitmask[getIndex(uXRegSpace,uYRegSpace-1, regSlice.width()+1)]; - - uint8_t srcBit6 = iPreviousCubeIndexY & 64; - uint8_t destBit4 = srcBit6 >> 2; + iPreviousCubeIndexY &= 192; //192 = 128 + 64 + iPreviousCubeIndexY >>= 2; - uint8_t srcBit7 = iPreviousCubeIndexY & 128; - uint8_t destBit5 = srcBit7 >> 2; + iCubeIndex = iPreviousCubeIndexY | iPreviousCubeIndexZ; - iCubeIndex |= destBit4; - iCubeIndex |= destBit5; if (v011 == 0) iCubeIndex |= 64; if (v111 == 0) iCubeIndex |= 128; } @@ -381,24 +332,20 @@ namespace PolyVox //z uint8_t iPreviousCubeIndexZ = previousBitmask[getIndex(uXRegSpace,uYRegSpace, regSlice.width()+1)]; - iCubeIndex = iPreviousCubeIndexZ >> 4; + iPreviousCubeIndexZ >>= 4; //y uint8_t iPreviousCubeIndexY = bitmask[getIndex(uXRegSpace,uYRegSpace-1, regSlice.width()+1)]; - uint8_t srcBit6 = iPreviousCubeIndexY & 64; - uint8_t destBit4 = srcBit6 >> 2; - - uint8_t srcBit7 = iPreviousCubeIndexY & 128; - uint8_t destBit5 = srcBit7 >> 2; + iPreviousCubeIndexY &= 192; //192 = 128 + 64 + iPreviousCubeIndexY >>= 2; //x uint8_t iPreviousCubeIndexX = bitmask[getIndex(uXRegSpace-1,uYRegSpace, regSlice.width()+1)]; - srcBit7 = iPreviousCubeIndexX & 128; - uint8_t destBit6 = srcBit7 >> 1; + iPreviousCubeIndexX &= 128; + iPreviousCubeIndexX >>= 1; + + iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY | iPreviousCubeIndexZ; - iCubeIndex |= destBit4; - iCubeIndex |= destBit5; - iCubeIndex |= destBit6; if (v111 == 0) iCubeIndex |= 128; } }