From ccb76bc6d71d5683161326440657d355587d4309 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 17 May 2015 09:52:13 +0200 Subject: [PATCH] More work refactoring code. Examples appear to work now but tests still fail. --- .../PolyVox/MarchingCubesSurfaceExtractor.inl | 75 ++++++++++--------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/include/PolyVox/MarchingCubesSurfaceExtractor.inl b/include/PolyVox/MarchingCubesSurfaceExtractor.inl index b7f8a362..a071bd96 100644 --- a/include/PolyVox/MarchingCubesSurfaceExtractor.inl +++ b/include/PolyVox/MarchingCubesSurfaceExtractor.inl @@ -147,28 +147,33 @@ namespace PolyVox continue; } - typename VolumeType::VoxelType v000 = m_sampVolume.peekVoxel1nx1ny1nz(); + /*typename VolumeType::VoxelType v000 = m_sampVolume.peekVoxel1nx1ny1nz(); typename VolumeType::VoxelType v100 = m_sampVolume.peekVoxel0px1ny1nz(); typename VolumeType::VoxelType v010 = m_sampVolume.peekVoxel1nx0py1nz(); - typename VolumeType::VoxelType v001 = m_sampVolume.peekVoxel1nx1ny0pz(); + typename VolumeType::VoxelType v001 = m_sampVolume.peekVoxel1nx1ny0pz();*/ /*typename VolumeType::VoxelType v000 = m_sampVolume.peekVoxel0px0py0pz(); typename VolumeType::VoxelType v100 = m_sampVolume.peekVoxel1px0py0pz(); typename VolumeType::VoxelType v010 = m_sampVolume.peekVoxel0px1py0pz(); typename VolumeType::VoxelType v001 = m_sampVolume.peekVoxel0px0py1pz();*/ + + typename VolumeType::VoxelType v110 = m_sampVolume.peekVoxel0px0py1nz(); + typename VolumeType::VoxelType v101 = m_sampVolume.peekVoxel0px1ny0pz(); + typename VolumeType::VoxelType v011 = m_sampVolume.peekVoxel1nx0py0pz(); + typename VolumeType::VoxelType v111 = m_sampVolume.peekVoxel0px0py0pz(); const Vector3DFloat n000 = computeCentralDifferenceGradient(m_sampVolume); /* Find the vertices where the surface intersects the cube */ - if (edgeTable[iCubeIndex] & 1) + if (edgeTable[iCubeIndex] & 64) { - m_sampVolume.movePositiveX(); - POLYVOX_ASSERT(v000 != v100, "Attempting to insert vertex between two voxels with the same value"); + 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(v000)) / static_cast(m_controller.convertToDensity(v100) - m_controller.convertToDensity(v000)); + 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 - 1), static_cast(uZRegSpace - 1)); + 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)); @@ -181,7 +186,7 @@ namespace PolyVox } // Allow the controller to decide how the material should be derived from the voxels. - const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v100, fInterp); + const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v011, v111, fInterp); MarchingCubesVertex surfaceVertex; surfaceVertex.encodedPosition = v3dScaledPosition; @@ -191,17 +196,17 @@ namespace PolyVox const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; - m_sampVolume.moveNegativeX(); + m_sampVolume.movePositiveX(); } - if (edgeTable[iCubeIndex] & 8) + if (edgeTable[iCubeIndex] & 32) { - m_sampVolume.movePositiveY(); - POLYVOX_ASSERT(v000 != v010, "Attempting to insert vertex between two voxels with the same value"); + 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(v000)) / static_cast(m_controller.convertToDensity(v010) - m_controller.convertToDensity(v000)); + 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 - 1), static_cast(uYRegSpace - 1) + fInterp, static_cast(uZRegSpace - 1)); + 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)); @@ -214,7 +219,7 @@ namespace PolyVox } // Allow the controller to decide how the material should be derived from the voxels. - const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v010, fInterp); + const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v101, v111, fInterp); MarchingCubesVertex surfaceVertex; surfaceVertex.encodedPosition = v3dScaledPosition; @@ -224,17 +229,17 @@ namespace PolyVox uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; - m_sampVolume.moveNegativeY(); + m_sampVolume.movePositiveY(); } - if (edgeTable[iCubeIndex] & 256) + if (edgeTable[iCubeIndex] & 1024) { - m_sampVolume.movePositiveZ(); - POLYVOX_ASSERT(v000 != v001, "Attempting to insert vertex between two voxels with the same value"); + 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(v000)) / static_cast(m_controller.convertToDensity(v001) - m_controller.convertToDensity(v000)); + 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 - 1), static_cast(uYRegSpace - 1), static_cast(uZRegSpace - 1) + fInterp); + 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)); @@ -246,7 +251,7 @@ namespace PolyVox } // Allow the controller to decide how the material should be derived from the voxels. - const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v001, fInterp); + const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v110, v111, fInterp); MarchingCubesVertex surfaceVertex; surfaceVertex.encodedPosition = v3dScaledPosition; @@ -256,7 +261,7 @@ namespace PolyVox const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace) = uLastVertexIndex; - m_sampVolume.moveNegativeZ(); + m_sampVolume.movePositiveZ(); } } } @@ -289,51 +294,51 @@ namespace PolyVox /* Find the vertices where the surface intersects the cube */ if (edgeTable[iCubeIndex] & 1) { - indlist[0] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace); + indlist[0] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace - 1); } if (edgeTable[iCubeIndex] & 2) { - indlist[1] = pIndicesY(uXRegSpace + 1, uYRegSpace, uZRegSpace); + indlist[1] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace - 1); } if (edgeTable[iCubeIndex] & 4) { - indlist[2] = pIndicesX(uXRegSpace, uYRegSpace + 1, uZRegSpace); + indlist[2] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace - 1); } if (edgeTable[iCubeIndex] & 8) { - indlist[3] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace); + indlist[3] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace - 1); } if (edgeTable[iCubeIndex] & 16) { - indlist[4] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace + 1); + indlist[4] = pIndicesX(uXRegSpace, uYRegSpace - 1, uZRegSpace); } if (edgeTable[iCubeIndex] & 32) { - indlist[5] = pIndicesY(uXRegSpace + 1, uYRegSpace, uZRegSpace + 1); + indlist[5] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace); } if (edgeTable[iCubeIndex] & 64) { - indlist[6] = pIndicesX(uXRegSpace, uYRegSpace + 1, uZRegSpace + 1); + indlist[6] = pIndicesX(uXRegSpace, uYRegSpace, uZRegSpace); } if (edgeTable[iCubeIndex] & 128) { - indlist[7] = pIndicesY(uXRegSpace, uYRegSpace, uZRegSpace + 1); + indlist[7] = pIndicesY(uXRegSpace - 1, uYRegSpace, uZRegSpace); } if (edgeTable[iCubeIndex] & 256) { - indlist[8] = pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace); + indlist[8] = pIndicesZ(uXRegSpace - 1, uYRegSpace - 1, uZRegSpace); } if (edgeTable[iCubeIndex] & 512) { - indlist[9] = pIndicesZ(uXRegSpace + 1, uYRegSpace, uZRegSpace); + indlist[9] = pIndicesZ(uXRegSpace, uYRegSpace - 1, uZRegSpace); } if (edgeTable[iCubeIndex] & 1024) { - indlist[10] = pIndicesZ(uXRegSpace + 1, uYRegSpace + 1, uZRegSpace); + indlist[10] = pIndicesZ(uXRegSpace, uYRegSpace, uZRegSpace); } if (edgeTable[iCubeIndex] & 2048) { - indlist[11] = pIndicesZ(uXRegSpace, uYRegSpace + 1, uZRegSpace); + indlist[11] = pIndicesZ(uXRegSpace - 1, uYRegSpace, uZRegSpace); } for (int i = 0; triTable[iCubeIndex][i] != -1; i += 3)