Comments and renaming variables.

This commit is contained in:
David Williams 2015-05-29 20:34:03 +02:00
parent a0c32e22b7
commit 8c35399fc6

View File

@ -51,31 +51,41 @@ namespace PolyVox
template< typename VolumeType, typename MeshType, typename ControllerType > template< typename VolumeType, typename MeshType, typename ControllerType >
void extractMarchingCubesMeshCustom(VolumeType* volData, Region region, MeshType* result, ControllerType controller) void extractMarchingCubesMeshCustom(VolumeType* volData, Region region, MeshType* result, ControllerType controller)
{ {
// Validate parameters
POLYVOX_THROW_IF(volData == nullptr, std::invalid_argument, "Provided volume cannot be null");
POLYVOX_THROW_IF(result == nullptr, std::invalid_argument, "Provided mesh cannot be null"); POLYVOX_THROW_IF(result == nullptr, std::invalid_argument, "Provided mesh cannot be null");
// For profiling this function
Timer timer; Timer timer;
result->clear(); result->clear();
typename ControllerType::DensityType tThreshold = controller.getThreshold(); // Store some commonly used values for performance and convienience
const uint32_t uRegionWidthInVoxels = region.getWidthInVoxels(); const uint32_t uRegionWidthInVoxels = region.getWidthInVoxels();
const uint32_t uRegionHeightInVoxels = region.getHeightInVoxels(); const uint32_t uRegionHeightInVoxels = region.getHeightInVoxels();
const uint32_t uRegionDepthInVoxels = region.getDepthInVoxels(); const uint32_t uRegionDepthInVoxels = region.getDepthInVoxels();
// No need to clear memory because we only read from elements we have written to. typename ControllerType::DensityType tThreshold = controller.getThreshold();
// A naive implemetation of Marching Cubes might sample the eight corner voxels of every cell to determine the cell index.
// However, when processing the cells sequentially we cn observe that many of the voxels are shared with previous adjacent
// cells, and so we can obtain these by careful bit-shifting. These variables keep track of previous cells for this purpose.
// We don't clear the arrays because the algorithm ensures that we only read from elements we have previously written to.
uint8_t uPreviousCellIndex = 0;
Array1DUint8 pPreviousRowCellIndices(uRegionWidthInVoxels);
Array2DUint8 pPreviousSliceCellIndices(uRegionWidthInVoxels, uRegionHeightInVoxels);
// A given vertex may be shared by multiple triangles, so we need to keep track of the indices into the vertex array.
// We don't clear the arrays because the algorithm ensures that we only read from elements we have previously written to.
Array<2, Vector3DInt32> pIndices(uRegionWidthInVoxels, uRegionHeightInVoxels); Array<2, Vector3DInt32> pIndices(uRegionWidthInVoxels, uRegionHeightInVoxels);
Array<2, Vector3DInt32> pPreviousIndices(uRegionWidthInVoxels, uRegionHeightInVoxels); Array<2, Vector3DInt32> pPreviousIndices(uRegionWidthInVoxels, uRegionHeightInVoxels);
Array2DUint8 pPreviousSliceBitmask(uRegionWidthInVoxels, uRegionHeightInVoxels); // A sampler pointing at the beginning of the region, which gets incremented to always point at the beginning of a slice.
Array1DUint8 pPreviousRowBitmask(uRegionWidthInVoxels);
uint8_t uPreviousCell = 0;
typename VolumeType::Sampler startOfSlice(volData); typename VolumeType::Sampler startOfSlice(volData);
startOfSlice.setPosition(region.getLowerX(), region.getLowerY(), region.getLowerZ()); startOfSlice.setPosition(region.getLowerX(), region.getLowerY(), region.getLowerZ());
for (uint32_t uZRegSpace = 0; uZRegSpace < uRegionDepthInVoxels; uZRegSpace++) for (uint32_t uZRegSpace = 0; uZRegSpace < uRegionDepthInVoxels; uZRegSpace++)
{ {
// A sampler pointing at the beginning of the slice, which gets incremented to always point at the beginning of a row.
typename VolumeType::Sampler startOfRow = startOfSlice; typename VolumeType::Sampler startOfRow = startOfSlice;
for (uint32_t uYRegSpace = 0; uYRegSpace < uRegionHeightInVoxels; uYRegSpace++) for (uint32_t uYRegSpace = 0; uYRegSpace < uRegionHeightInVoxels; uYRegSpace++)
@ -87,7 +97,7 @@ namespace PolyVox
for (uint32_t uXRegSpace = 0; uXRegSpace < uRegionWidthInVoxels; uXRegSpace++) for (uint32_t uXRegSpace = 0; uXRegSpace < uRegionWidthInVoxels; uXRegSpace++)
{ {
// Note: In many cases the provided region will be (mostly) empty which means mesh vertices/indices // Note: In many cases the provided region will be (mostly) empty which means mesh vertices/indices
// are not generated and the only thing that is done for each cell is the computation of iCubeIndex. // are not generated and the only thing that is done for each cell is the computation of uCellIndex.
// It appears that retriving the voxel value is not so expensive and that it is the bitwise combining // It appears that retriving the voxel value is not so expensive and that it is the bitwise combining
// which actually carries the cost. // which actually carries the cost.
// //
@ -96,39 +106,41 @@ namespace PolyVox
// However, this complicates the code and there would still be the cost of packing/unpacking so it's // However, this complicates the code and there would still be the cost of packing/unpacking so it's
// not clear if there is really a benefit. It's something to consider in the future. // not clear if there is really a benefit. It's something to consider in the future.
uint8_t iCubeIndex = 0; // Each bit of the cell index specifies whether a given corner of the cell is above or below the threshold.
uint8_t uCellIndex = 0;
// Four bits of our cube index are obtained by looking at the cube index for // Four bits of our cube index are obtained by looking at the cube index for
// the previous slice and copying four of those bits into their new positions. // the previous slice and copying four of those bits into their new positions.
uint8_t iPreviousCubeIndexZ = pPreviousSliceBitmask(uXRegSpace, uYRegSpace); uint8_t uPreviousCellIndexZ = pPreviousSliceCellIndices(uXRegSpace, uYRegSpace);
iPreviousCubeIndexZ >>= 4; uPreviousCellIndexZ >>= 4;
iCubeIndex |= iPreviousCubeIndexZ; uCellIndex |= uPreviousCellIndexZ;
// Two bits of our cube index are obtained by looking at the cube index for // Two bits of our cube index are obtained by looking at the cube index for
// the previous row and copying two of those bits into their new positions. // the previous row and copying two of those bits into their new positions.
uint8_t iPreviousCubeIndexY = pPreviousRowBitmask(uXRegSpace); uint8_t uPreviousCellIndexY = pPreviousRowCellIndices(uXRegSpace);
iPreviousCubeIndexY &= 204; //204 = 128+64+8+4 uPreviousCellIndexY &= 204; //204 = 128+64+8+4
iPreviousCubeIndexY >>= 2; uPreviousCellIndexY >>= 2;
iCubeIndex |= iPreviousCubeIndexY; uCellIndex |= uPreviousCellIndexY;
// One bit of our cube index are obtained by looking at the cube index for // One bit of our cube index are obtained by looking at the cube index for
// the previous cell and copying one of those bits into it's new position. // the previous cell and copying one of those bits into it's new position.
uint8_t iPreviousCubeIndexX = uPreviousCell; uint8_t UPreviousCellIndexX = uPreviousCellIndex;
iPreviousCubeIndexX &= 170; //170 = 128+32+8+2 UPreviousCellIndexX &= 170; //170 = 128+32+8+2
iPreviousCubeIndexX >>= 1; UPreviousCellIndexX >>= 1;
iCubeIndex |= iPreviousCubeIndexX; uCellIndex |= UPreviousCellIndexX;
// The last bit of our cube index is obtained by looking // The last bit of our cube index is obtained by looking
// at the relevant voxel and comparing it to the threshold // at the relevant voxel and comparing it to the threshold
typename VolumeType::VoxelType v111 = sampler.getVoxel(); typename VolumeType::VoxelType v111 = sampler.getVoxel();
if (controller.convertToDensity(v111) < tThreshold) iCubeIndex |= 128; if (controller.convertToDensity(v111) < tThreshold) uCellIndex |= 128;
// The current value becomes the previous value, ready for the next iteration. // The current value becomes the previous value, ready for the next iteration.
uPreviousCell = iCubeIndex; uPreviousCellIndex = uCellIndex;
pPreviousRowBitmask(uXRegSpace) = iCubeIndex; pPreviousRowCellIndices(uXRegSpace) = uCellIndex;
pPreviousSliceBitmask(uXRegSpace, uYRegSpace) = iCubeIndex; pPreviousSliceCellIndices(uXRegSpace, uYRegSpace) = uCellIndex;
uint16_t uEdge = edgeTable[iCubeIndex]; // 12 bits of uEdge determine whether a vertex is placed on each of the 12 edges of the cell.
uint16_t uEdge = edgeTable[uCellIndex];
// Test whether any vertices and indices should be generated for the current cell (i.e. it is occupied). // Test whether any vertices and indices should be generated for the current cell (i.e. it is occupied).
// Performance note: This condition is usually false because most cells in a volume are completely above // Performance note: This condition is usually false because most cells in a volume are completely above
@ -314,11 +326,11 @@ namespace PolyVox
indlist[11] = pIndices(uXRegSpace - 1, uYRegSpace).getZ(); indlist[11] = pIndices(uXRegSpace - 1, uYRegSpace).getZ();
} }
for (int i = 0; triTable[iCubeIndex][i] != -1; i += 3) for (int i = 0; triTable[uCellIndex][i] != -1; i += 3)
{ {
const int32_t ind0 = indlist[triTable[iCubeIndex][i]]; const int32_t ind0 = indlist[triTable[uCellIndex][i]];
const int32_t ind1 = indlist[triTable[iCubeIndex][i + 1]]; const int32_t ind1 = indlist[triTable[uCellIndex][i + 1]];
const int32_t ind2 = indlist[triTable[iCubeIndex][i + 2]]; const int32_t ind2 = indlist[triTable[uCellIndex][i + 2]];
if ((ind0 != -1) && (ind1 != -1) && (ind2 != -1)) if ((ind0 != -1) && (ind1 != -1) && (ind2 != -1))
{ {