From cee15a145f822b913d33e3bb26c82cdb49954ff7 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 23 May 2015 17:45:16 +0200 Subject: [PATCH] Avoid setting the sampler position every iteration. --- .../PolyVox/MarchingCubesSurfaceExtractor.inl | 370 +++++++++--------- 1 file changed, 184 insertions(+), 186 deletions(-) diff --git a/include/PolyVox/MarchingCubesSurfaceExtractor.inl b/include/PolyVox/MarchingCubesSurfaceExtractor.inl index 71e8e9c3..5fde56e4 100644 --- a/include/PolyVox/MarchingCubesSurfaceExtractor.inl +++ b/include/PolyVox/MarchingCubesSurfaceExtractor.inl @@ -81,12 +81,12 @@ namespace PolyVox { const uint32_t uYRegSpace = iYVolSpace - m_regSizeInVoxels.getLowerY(); + m_sampVolume.setPosition(m_regSizeInVoxels.getLowerX(), iYVolSpace, iZVolSpace); + for (int32_t iXVolSpace = m_regSizeInVoxels.getLowerX(); iXVolSpace <= m_regSizeInVoxels.getUpperX(); iXVolSpace++) { const uint32_t uXRegSpace = iXVolSpace - m_regSizeInVoxels.getLowerX(); - m_sampVolume.setPosition(iXVolSpace, iYVolSpace, iZVolSpace); - typename VolumeType::VoxelType v000; typename VolumeType::VoxelType v100; typename VolumeType::VoxelType v010; @@ -216,7 +216,7 @@ namespace PolyVox { v010 = m_sampVolume.peekVoxel1nx0py1nz(); v110 = m_sampVolume.peekVoxel0px0py1nz(); - + v011 = m_sampVolume.peekVoxel1nx0py0pz(); v111 = m_sampVolume.peekVoxel0px0py0pz(); @@ -236,7 +236,7 @@ namespace PolyVox else //previous Y not available { if (isPrevXAvail) - { + { v100 = m_sampVolume.peekVoxel0px1ny1nz(); v110 = m_sampVolume.peekVoxel0px0py1nz(); @@ -286,197 +286,195 @@ namespace PolyVox } /* Cube is entirely in/out of the surface */ - if (edgeTable[iCubeIndex] == 0) + if (edgeTable[iCubeIndex] != 0) { - continue; - } - // These three might not have been sampled, as v111 is the only one we sample every iteration. - v110 = m_sampVolume.peekVoxel0px0py1nz(); - v101 = m_sampVolume.peekVoxel0px1ny0pz(); - v011 = m_sampVolume.peekVoxel1nx0py0pz(); - - const Vector3DFloat n000 = computeCentralDifferenceGradient(m_sampVolume); + // These three might not have been sampled, as v111 is the only one we sample every iteration. + v110 = m_sampVolume.peekVoxel0px0py1nz(); + v101 = m_sampVolume.peekVoxel0px1ny0pz(); + v011 = m_sampVolume.peekVoxel1nx0py0pz(); - /* Find the vertices where the surface intersects the cube */ - if ((edgeTable[iCubeIndex] & 64) && (uXRegSpace > 0)) - { - m_sampVolume.moveNegativeX(); - POLYVOX_ASSERT(v011 != v111, "Attempting to insert vertex between two voxels with the same value"); - const Vector3DFloat n100 = computeCentralDifferenceGradient(m_sampVolume); + const Vector3DFloat n000 = computeCentralDifferenceGradient(m_sampVolume); - const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v011)) / static_cast(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v011)); - - const Vector3DFloat v3dPosition(static_cast(uXRegSpace - 1) + fInterp, static_cast(uYRegSpace), static_cast(uZRegSpace)); - const Vector3DUint16 v3dScaledPosition(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); - - Vector3DFloat v3dNormal = (n100*fInterp) + (n000*(1 - fInterp)); - - // The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so - // the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels). - if (v3dNormal.lengthSquared() > 0.000001f) + /* Find the vertices where the surface intersects the cube */ + if ((edgeTable[iCubeIndex] & 64) && (uXRegSpace > 0)) { - v3dNormal.normalise(); + m_sampVolume.moveNegativeX(); + POLYVOX_ASSERT(v011 != v111, "Attempting to insert vertex between two voxels with the same value"); + const Vector3DFloat n100 = computeCentralDifferenceGradient(m_sampVolume); + + const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v011)) / static_cast(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v011)); + + const Vector3DFloat v3dPosition(static_cast(uXRegSpace - 1) + fInterp, static_cast(uYRegSpace), static_cast(uZRegSpace)); + const Vector3DUint16 v3dScaledPosition(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); + + Vector3DFloat v3dNormal = (n100*fInterp) + (n000*(1 - fInterp)); + + // The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so + // the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels). + if (v3dNormal.lengthSquared() > 0.000001f) + { + v3dNormal.normalise(); + } + + // Allow the controller to decide how the material should be derived from the voxels. + const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v011, v111, fInterp); + + MarchingCubesVertex surfaceVertex; + surfaceVertex.encodedPosition = v3dScaledPosition; + surfaceVertex.encodedNormal = encodeNormal(v3dNormal); + surfaceVertex.data = uMaterial; + + const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); + pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; + + m_sampVolume.movePositiveX(); + } + if ((edgeTable[iCubeIndex] & 32) && (uYRegSpace > 0)) + { + m_sampVolume.moveNegativeY(); + POLYVOX_ASSERT(v101 != v111, "Attempting to insert vertex between two voxels with the same value"); + const Vector3DFloat n010 = computeCentralDifferenceGradient(m_sampVolume); + + const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v101)) / static_cast(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v101)); + + const Vector3DFloat v3dPosition(static_cast(uXRegSpace), static_cast(uYRegSpace - 1) + fInterp, static_cast(uZRegSpace)); + const Vector3DUint16 v3dScaledPosition(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); + + Vector3DFloat v3dNormal = (n010*fInterp) + (n000*(1 - fInterp)); + + // The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so + // the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels). + if (v3dNormal.lengthSquared() > 0.000001f) + { + v3dNormal.normalise(); + } + + // Allow the controller to decide how the material should be derived from the voxels. + const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v101, v111, fInterp); + + MarchingCubesVertex surfaceVertex; + surfaceVertex.encodedPosition = v3dScaledPosition; + surfaceVertex.encodedNormal = encodeNormal(v3dNormal); + surfaceVertex.data = uMaterial; + + uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); + pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; + + m_sampVolume.movePositiveY(); + } + if ((edgeTable[iCubeIndex] & 1024) && (uZRegSpace > 0)) + { + m_sampVolume.moveNegativeZ(); + POLYVOX_ASSERT(v110 != v111, "Attempting to insert vertex between two voxels with the same value"); + const Vector3DFloat n001 = computeCentralDifferenceGradient(m_sampVolume); + + const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v110)) / static_cast(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v110)); + + const Vector3DFloat v3dPosition(static_cast(uXRegSpace), static_cast(uYRegSpace), static_cast(uZRegSpace - 1) + fInterp); + const Vector3DUint16 v3dScaledPosition(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); + + Vector3DFloat v3dNormal = (n001*fInterp) + (n000*(1 - fInterp)); + // The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so + // the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels). + if (v3dNormal.lengthSquared() > 0.000001f) + { + v3dNormal.normalise(); + } + + // Allow the controller to decide how the material should be derived from the voxels. + const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v110, v111, fInterp); + + MarchingCubesVertex surfaceVertex; + surfaceVertex.encodedPosition = v3dScaledPosition; + surfaceVertex.encodedNormal = encodeNormal(v3dNormal); + surfaceVertex.data = uMaterial; + + const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); + pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; + + m_sampVolume.movePositiveZ(); } - // Allow the controller to decide how the material should be derived from the voxels. - const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v011, v111, fInterp); - - MarchingCubesVertex surfaceVertex; - surfaceVertex.encodedPosition = v3dScaledPosition; - surfaceVertex.encodedNormal = encodeNormal(v3dNormal); - surfaceVertex.data = uMaterial; - - const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); - pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; - - m_sampVolume.movePositiveX(); - } - if ((edgeTable[iCubeIndex] & 32) && (uYRegSpace > 0)) - { - m_sampVolume.moveNegativeY(); - POLYVOX_ASSERT(v101 != v111, "Attempting to insert vertex between two voxels with the same value"); - const Vector3DFloat n010 = computeCentralDifferenceGradient(m_sampVolume); - - const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v101)) / static_cast(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v101)); - - const Vector3DFloat v3dPosition(static_cast(uXRegSpace), static_cast(uYRegSpace - 1) + fInterp, static_cast(uZRegSpace)); - const Vector3DUint16 v3dScaledPosition(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); - - Vector3DFloat v3dNormal = (n010*fInterp) + (n000*(1 - fInterp)); - - // The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so - // the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels). - if (v3dNormal.lengthSquared() > 0.000001f) + // Now output the indices. For the first row, column or slice there aren't + // any (the region size in cells is one less than the region size in voxels) + if ((isPrevXAvail) && (isPrevYAvail) && (isPrevZAvail)) { - v3dNormal.normalise(); + + int32_t indlist[12]; + + m_sampVolume.setPosition(iXVolSpace, iYVolSpace, iZVolSpace); + + /* Cube is entirely in/out of the surface */ + if (edgeTable[iCubeIndex] != 0) + { + + /* Find the vertices where the surface intersects the cube */ + if (edgeTable[iCubeIndex] & 1) + { + indlist[0] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace - 1); + } + if (edgeTable[iCubeIndex] & 2) + { + indlist[1] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace - 1); + } + if (edgeTable[iCubeIndex] & 4) + { + indlist[2] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace - 1); + } + if (edgeTable[iCubeIndex] & 8) + { + indlist[3] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace - 1); + } + if (edgeTable[iCubeIndex] & 16) + { + indlist[4] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace); + } + if (edgeTable[iCubeIndex] & 32) + { + indlist[5] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace); + } + if (edgeTable[iCubeIndex] & 64) + { + indlist[6] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace); + } + if (edgeTable[iCubeIndex] & 128) + { + indlist[7] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace); + } + if (edgeTable[iCubeIndex] & 256) + { + indlist[8] = pIndicesZ(uXRegSpace - 1, uYRegSpace - 1, uZRegSpace); + } + if (edgeTable[iCubeIndex] & 512) + { + indlist[9] = pIndicesZ(uXRegSpace, uYRegSpace - 1, uZRegSpace); + } + if (edgeTable[iCubeIndex] & 1024) + { + indlist[10] = pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace); + } + if (edgeTable[iCubeIndex] & 2048) + { + indlist[11] = pIndicesZ(uXRegSpace - 1, uYRegSpace, uZRegSpace); + } + + for (int i = 0; triTable[iCubeIndex][i] != -1; i += 3) + { + const int32_t ind0 = indlist[triTable[iCubeIndex][i]]; + const int32_t ind1 = indlist[triTable[iCubeIndex][i + 1]]; + const int32_t ind2 = indlist[triTable[iCubeIndex][i + 2]]; + + if ((ind0 != -1) && (ind1 != -1) && (ind2 != -1)) + { + m_meshCurrent->addTriangle(ind0, ind1, ind2); + } + }//For each triangle + } } - - // Allow the controller to decide how the material should be derived from the voxels. - const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v101, v111, fInterp); - - MarchingCubesVertex surfaceVertex; - surfaceVertex.encodedPosition = v3dScaledPosition; - surfaceVertex.encodedNormal = encodeNormal(v3dNormal); - surfaceVertex.data = uMaterial; - - uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); - pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; - - m_sampVolume.movePositiveY(); - } - if ((edgeTable[iCubeIndex] & 1024) && (uZRegSpace > 0)) - { - m_sampVolume.moveNegativeZ(); - POLYVOX_ASSERT(v110 != v111, "Attempting to insert vertex between two voxels with the same value"); - const Vector3DFloat n001 = computeCentralDifferenceGradient(m_sampVolume); - - const float fInterp = static_cast(m_tThreshold - m_controller.convertToDensity(v110)) / static_cast(m_controller.convertToDensity(v111) - m_controller.convertToDensity(v110)); - - const Vector3DFloat v3dPosition(static_cast(uXRegSpace), static_cast(uYRegSpace), static_cast(uZRegSpace - 1) + fInterp); - const Vector3DUint16 v3dScaledPosition(static_cast(v3dPosition.getX() * 256.0f), static_cast(v3dPosition.getY() * 256.0f), static_cast(v3dPosition.getZ() * 256.0f)); - - Vector3DFloat v3dNormal = (n001*fInterp) + (n000*(1 - fInterp)); - // The gradient for a voxel can be zero (e.g. solid voxel surrounded by empty ones) and so - // the interpolated normal can also be zero (e.g. a grid of alternating solid and empty voxels). - if (v3dNormal.lengthSquared() > 0.000001f) - { - v3dNormal.normalise(); - } - - // Allow the controller to decide how the material should be derived from the voxels. - const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v110, v111, fInterp); - - MarchingCubesVertex surfaceVertex; - surfaceVertex.encodedPosition = v3dScaledPosition; - surfaceVertex.encodedNormal = encodeNormal(v3dNormal); - surfaceVertex.data = uMaterial; - - const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); - pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; - - m_sampVolume.movePositiveZ(); - } - - // Now output the indices. For the first row, column or slice there aren't - // any (the region size in cells is one less than the region size in voxels) - if ((!isPrevXAvail) || (!isPrevYAvail) || (!isPrevZAvail)) - { - continue; - } - - int32_t indlist[12]; - - m_sampVolume.setPosition(iXVolSpace, iYVolSpace, iZVolSpace); - - /* 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] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace - 1); - } - if (edgeTable[iCubeIndex] & 2) - { - indlist[1] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace - 1); - } - if (edgeTable[iCubeIndex] & 4) - { - indlist[2] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace - 1); - } - if (edgeTable[iCubeIndex] & 8) - { - indlist[3] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace - 1); - } - if (edgeTable[iCubeIndex] & 16) - { - indlist[4] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace); - } - if (edgeTable[iCubeIndex] & 32) - { - indlist[5] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace); - } - if (edgeTable[iCubeIndex] & 64) - { - indlist[6] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace); - } - if (edgeTable[iCubeIndex] & 128) - { - indlist[7] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace); - } - if (edgeTable[iCubeIndex] & 256) - { - indlist[8] = pIndicesZ(uXRegSpace - 1, uYRegSpace - 1, uZRegSpace); - } - if (edgeTable[iCubeIndex] & 512) - { - indlist[9] = pIndicesZ(uXRegSpace, uYRegSpace - 1, uZRegSpace); - } - if (edgeTable[iCubeIndex] & 1024) - { - indlist[10] = pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace); - } - if (edgeTable[iCubeIndex] & 2048) - { - indlist[11] = pIndicesZ(uXRegSpace - 1, uYRegSpace, uZRegSpace); - } - - for (int i = 0; triTable[iCubeIndex][i] != -1; i += 3) - { - const int32_t ind0 = indlist[triTable[iCubeIndex][i]]; - const int32_t ind1 = indlist[triTable[iCubeIndex][i + 1]]; - const int32_t ind2 = indlist[triTable[iCubeIndex][i + 2]]; - - if ((ind0 != -1) && (ind1 != -1) && (ind2 != -1)) - { - m_meshCurrent->addTriangle(ind0, ind1, ind2); - } - }//For each triangle - }//For each cell + }//For each cell + m_sampVolume.movePositiveX(); + } } pPreviousBitmask.swap(pCurrentBitmask);