Massive changes to the organisation of PolyVoxCore and PolyVoxYtil.

Also added start of logging capability.
This commit is contained in:
David Williams
2009-04-03 21:36:22 +00:00
parent dbf7257edd
commit df1bf690c9
222 changed files with 3353 additions and 186 deletions

View File

@ -0,0 +1,595 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#include "PolyVoxImpl/DecimatedSurfaceExtractor.h"
#include "Volume.h"
#include "GradientEstimators.h"
#include "IndexedSurfacePatch.h"
#include "PolyVoxImpl/MarchingCubesTables.h"
#include "Region.h"
#include "VolumeIterator.h"
#include <algorithm>
using namespace std;
namespace PolyVox
{
uint32_t getDecimatedIndex(uint32_t x, uint32_t y , uint32_t regionWidth)
{
return x + (y * (regionWidth+1));
}
void extractDecimatedSurfaceImpl(Volume<uint8_t>* 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<Vector3DFloat>(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<uint8_t> volIter(*volumeData);
//Compute bitmask for initial slice
uint32_t uNoOfNonEmptyCellsForSlice0 = computeInitialDecimatedBitmaskForSlice(volIter, uLevel, regSlice0, offset, bitmask0);
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 = computeDecimatedBitmaskForSliceFromPrevious(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<SurfaceVertex>::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin();
while(iterSurfaceVertex != singleMaterialPatch->getVertices().end())
{
Vector3DFloat tempNormal = computeDecimatedNormal(volumeData, static_cast<Vector3DFloat>(iterSurfaceVertex->getPosition() + offset), CENTRAL_DIFFERENCE);
const_cast<SurfaceVertex&>(*iterSurfaceVertex).setNormal(tempNormal);
++iterSurfaceVertex;
}*/
}
uint32_t computeInitialDecimatedBitmaskForSlice(VolumeIterator<uint8_t>& volIter, uint8_t uLevel, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask)
{
const uint8_t uStepSize = uLevel == 0 ? 1 : 1 << uLevel;
uint32_t uNoOfNonEmptyCells = 0;
//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
volIter.setPosition(x,y,regSlice.getLowerCorner().getZ());
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8_t iCubeIndex = 0;
if((x==regSlice.getLowerCorner().getX()) && (y==regSlice.getLowerCorner().getY()))
{
volIter.setPosition(x,y,regSlice.getLowerCorner().getZ());
const uint8_t v000 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ());
const uint8_t v100 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8_t v010 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8_t v110 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v001 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v101 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v011 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v111 = volIter.getSubSampledVoxel(uLevel);
if (v000 == 0) iCubeIndex |= 1;
if (v100 == 0) iCubeIndex |= 2;
if (v110 == 0) iCubeIndex |= 4;
if (v010 == 0) iCubeIndex |= 8;
if (v001 == 0) iCubeIndex |= 16;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else if((x>regSlice.getLowerCorner().getX()) && y==regSlice.getLowerCorner().getY())
{
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ());
const uint8_t v100 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8_t v110 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v101 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v111 = volIter.getSubSampledVoxel(uLevel);
//x
uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)];
uint8_t srcBit6 = iPreviousCubeIndexX & 64;
uint8_t destBit7 = srcBit6 << 1;
uint8_t srcBit5 = iPreviousCubeIndexX & 32;
uint8_t destBit4 = srcBit5 >> 1;
uint8_t srcBit2 = iPreviousCubeIndexX & 4;
uint8_t destBit3 = srcBit2 << 1;
uint8_t srcBit1 = iPreviousCubeIndexX & 2;
uint8_t destBit0 = srcBit1 >> 1;
iCubeIndex |= destBit0;
if (v100 == 0) iCubeIndex |= 2;
if (v110 == 0) iCubeIndex |= 4;
iCubeIndex |= destBit3;
iCubeIndex |= destBit4;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
else if((x==regSlice.getLowerCorner().getX()) && (y>regSlice.getLowerCorner().getY()))
{
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8_t v010 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8_t v110 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v011 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v111 = volIter.getSubSampledVoxel(uLevel);
//y
uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)];
uint8_t srcBit7 = iPreviousCubeIndexY & 128;
uint8_t destBit4 = srcBit7 >> 3;
uint8_t srcBit6 = iPreviousCubeIndexY & 64;
uint8_t destBit5 = srcBit6 >> 1;
uint8_t srcBit3 = iPreviousCubeIndexY & 8;
uint8_t destBit0 = srcBit3 >> 3;
uint8_t srcBit2 = iPreviousCubeIndexY & 4;
uint8_t destBit1 = srcBit2 >> 1;
iCubeIndex |= destBit0;
iCubeIndex |= destBit1;
if (v110 == 0) iCubeIndex |= 4;
if (v010 == 0) iCubeIndex |= 8;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else
{
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8_t v110 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v111 = volIter.getSubSampledVoxel(uLevel);
//y
uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)];
uint8_t srcBit7 = iPreviousCubeIndexY & 128;
uint8_t destBit4 = srcBit7 >> 3;
uint8_t srcBit6 = iPreviousCubeIndexY & 64;
uint8_t destBit5 = srcBit6 >> 1;
uint8_t srcBit3 = iPreviousCubeIndexY & 8;
uint8_t destBit0 = srcBit3 >> 3;
uint8_t srcBit2 = iPreviousCubeIndexY & 4;
uint8_t destBit1 = srcBit2 >> 1;
//x
uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)];
srcBit6 = iPreviousCubeIndexX & 64;
uint8_t destBit7 = srcBit6 << 1;
srcBit2 = iPreviousCubeIndexX & 4;
uint8_t destBit3 = srcBit2 << 1;
iCubeIndex |= destBit0;
iCubeIndex |= destBit1;
if (v110 == 0) iCubeIndex |= 4;
iCubeIndex |= destBit3;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
//Save the bitmask
bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)] = iCubeIndex;
if(edgeTable[iCubeIndex] != 0)
{
++uNoOfNonEmptyCells;
}
}
}
return uNoOfNonEmptyCells;
}
uint32_t computeDecimatedBitmaskForSliceFromPrevious(VolumeIterator<uint8_t>& 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 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
volIter.setPosition(x,y,regSlice.getLowerCorner().getZ());
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8_t iCubeIndex = 0;
if((x==regSlice.getLowerCorner().getX()) && (y==regSlice.getLowerCorner().getY()))
{
volIter.setPosition(x,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v001 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v101 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v011 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v111 = volIter.getSubSampledVoxel(uLevel);
//z
uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
if (v001 == 0) iCubeIndex |= 16;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else if((x>regSlice.getLowerCorner().getX()) && y==regSlice.getLowerCorner().getY())
{
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v101 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v111 = volIter.getSubSampledVoxel(uLevel);
//z
uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//x
uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)];
uint8_t srcBit6 = iPreviousCubeIndexX & 64;
uint8_t destBit7 = srcBit6 << 1;
uint8_t srcBit5 = iPreviousCubeIndexX & 32;
uint8_t destBit4 = srcBit5 >> 1;
iCubeIndex |= destBit4;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
else if((x==regSlice.getLowerCorner().getX()) && (y>regSlice.getLowerCorner().getY()))
{
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v011 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v111 = volIter.getSubSampledVoxel(uLevel);
//z
uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//y
uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)];
uint8_t srcBit7 = iPreviousCubeIndexY & 128;
uint8_t destBit4 = srcBit7 >> 3;
uint8_t srcBit6 = iPreviousCubeIndexY & 64;
uint8_t destBit5 = srcBit6 >> 1;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else
{
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8_t v111 = volIter.getSubSampledVoxel(uLevel);
//z
uint8_t iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//y
uint8_t iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize, regSlice.width()+1)];
uint8_t srcBit7 = iPreviousCubeIndexY & 128;
uint8_t destBit4 = srcBit7 >> 3;
uint8_t srcBit6 = iPreviousCubeIndexY & 64;
uint8_t destBit5 = srcBit6 >> 1;
//x
uint8_t iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY(), regSlice.width()+1)];
srcBit6 = iPreviousCubeIndexX & 64;
uint8_t destBit7 = srcBit6 << 1;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
//Save the bitmask
bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY(), regSlice.width()+1)] = iCubeIndex;
if(edgeTable[iCubeIndex] != 0)
{
++uNoOfNonEmptyCells;
}
}//For each cell
}
return uNoOfNonEmptyCells;
}
void generateDecimatedVerticesForSlice(VolumeIterator<uint8_t>& 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 generateDecimatedIndicesForSlice(VolumeIterator<uint8_t>& 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
}
}
}

View File

@ -0,0 +1,543 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#include "PolyVoxImpl/FastSurfaceExtractor.h"
#include "VolumeIterator.h"
#include "IndexedSurfacePatch.h"
#include "PolyVoxImpl/MarchingCubesTables.h"
#include "SurfaceVertex.h"
namespace PolyVox
{
void extractFastSurfaceImpl(Volume<uint8_t>* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch)
{
singleMaterialPatch->clear();
//For edge indices
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)];
//When generating the mesh for a region we actually look one voxel 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(1,1,1));
region.cropTo(regVolume);
//Offset from volume corner
const Vector3DFloat offset = static_cast<Vector3DFloat>(region.getLowerCorner());
//Create a region corresponding to the first slice
Region regSlice0(region);
regSlice0.setUpperCorner(Vector3DInt32(regSlice0.getUpperCorner().getX(),regSlice0.getUpperCorner().getY(),regSlice0.getLowerCorner().getZ()));
//Iterator to access the volume data
VolumeIterator<uint8_t> volIter(*volumeData);
//Compute bitmask for initial slice
uint32_t uNoOfNonEmptyCellsForSlice0 = computeInitialRoughBitmaskForSlice(volIter, regSlice0, offset, bitmask0);
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);
}
for(uint32_t uSlice = 0; ((uSlice <= region.depth()-1) && (uSlice + offset.getZ() < region.getUpperCorner().getZ())); ++uSlice)
{
Region regSlice1(regSlice0);
regSlice1.shift(Vector3DInt32(0,0,1));
uint32_t uNoOfNonEmptyCellsForSlice1 = computeRoughBitmaskForSliceFromPrevious(volIter, regSlice1, offset, bitmask1, bitmask0);
if(uNoOfNonEmptyCellsForSlice1 != 0)
{
generateRoughVerticesForSlice(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);
}
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;
}
uint32_t getIndex(uint32_t x, uint32_t y, uint32_t regionWidth)
{
return x + (y * (regionWidth+1));
}
uint32_t computeInitialRoughBitmaskForSlice(VolumeIterator<uint8_t>& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask)
{
uint32_t uNoOfNonEmptyCells = 0;
//Iterate over each cell in the region
volIter.setPosition(regSlice.getLowerCorner().getX(),regSlice.getLowerCorner().getY(), regSlice.getLowerCorner().getZ());
volIter.setValidRegion(regSlice);
do
{
//Current position
const uint16_t x = volIter.getPosX() - offset.getX();
const uint16_t y = 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;
if((x==0) && (y==0))
{
const uint8_t v000 = volIter.getVoxel();
const uint8_t v100 = volIter.peekVoxel1px0py0pz();
const uint8_t v010 = volIter.peekVoxel0px1py0pz();
const uint8_t v110 = volIter.peekVoxel1px1py0pz();
const uint8_t v001 = volIter.peekVoxel0px0py1pz();
const uint8_t v101 = volIter.peekVoxel1px0py1pz();
const uint8_t v011 = volIter.peekVoxel0px1py1pz();
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
if (v000 == 0) iCubeIndex |= 1;
if (v100 == 0) iCubeIndex |= 2;
if (v110 == 0) iCubeIndex |= 4;
if (v010 == 0) iCubeIndex |= 8;
if (v001 == 0) iCubeIndex |= 16;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else if((x>0) && y==0)
{
const uint8_t v100 = volIter.peekVoxel1px0py0pz();
const uint8_t v110 = volIter.peekVoxel1px1py0pz();
const uint8_t v101 = volIter.peekVoxel1px0py1pz();
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
//x
uint8_t iPreviousCubeIndexX = bitmask[getIndex(x-1,y, regSlice.width()+1)];
uint8_t srcBit6 = iPreviousCubeIndexX & 64;
uint8_t destBit7 = srcBit6 << 1;
uint8_t srcBit5 = iPreviousCubeIndexX & 32;
uint8_t destBit4 = srcBit5 >> 1;
uint8_t srcBit2 = iPreviousCubeIndexX & 4;
uint8_t destBit3 = srcBit2 << 1;
uint8_t srcBit1 = iPreviousCubeIndexX & 2;
uint8_t destBit0 = srcBit1 >> 1;
iCubeIndex |= destBit0;
if (v100 == 0) iCubeIndex |= 2;
if (v110 == 0) iCubeIndex |= 4;
iCubeIndex |= destBit3;
iCubeIndex |= destBit4;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
else if((x==0) && (y>0))
{
const uint8_t v010 = volIter.peekVoxel0px1py0pz();
const uint8_t v110 = volIter.peekVoxel1px1py0pz();
const uint8_t v011 = volIter.peekVoxel0px1py1pz();
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
//y
uint8_t iPreviousCubeIndexY = bitmask[getIndex(x,y-1, regSlice.width()+1)];
uint8_t srcBit7 = iPreviousCubeIndexY & 128;
uint8_t destBit4 = srcBit7 >> 3;
uint8_t srcBit6 = iPreviousCubeIndexY & 64;
uint8_t destBit5 = srcBit6 >> 1;
uint8_t srcBit3 = iPreviousCubeIndexY & 8;
uint8_t destBit0 = srcBit3 >> 3;
uint8_t srcBit2 = iPreviousCubeIndexY & 4;
uint8_t destBit1 = srcBit2 >> 1;
iCubeIndex |= destBit0;
iCubeIndex |= destBit1;
if (v110 == 0) iCubeIndex |= 4;
if (v010 == 0) iCubeIndex |= 8;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else
{
const uint8_t v110 = volIter.peekVoxel1px1py0pz();
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
//y
uint8_t iPreviousCubeIndexY = bitmask[getIndex(x,y-1, regSlice.width()+1)];
uint8_t srcBit7 = iPreviousCubeIndexY & 128;
uint8_t destBit4 = srcBit7 >> 3;
uint8_t srcBit6 = iPreviousCubeIndexY & 64;
uint8_t destBit5 = srcBit6 >> 1;
uint8_t srcBit3 = iPreviousCubeIndexY & 8;
uint8_t destBit0 = srcBit3 >> 3;
uint8_t srcBit2 = iPreviousCubeIndexY & 4;
uint8_t destBit1 = srcBit2 >> 1;
//x
uint8_t iPreviousCubeIndexX = bitmask[getIndex(x-1,y, regSlice.width()+1)];
srcBit6 = iPreviousCubeIndexX & 64;
uint8_t destBit7 = srcBit6 << 1;
srcBit2 = iPreviousCubeIndexX & 4;
uint8_t destBit3 = srcBit2 << 1;
iCubeIndex |= destBit0;
iCubeIndex |= destBit1;
if (v110 == 0) iCubeIndex |= 4;
iCubeIndex |= destBit3;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
//Save the bitmask
bitmask[getIndex(x,y, regSlice.width()+1)] = iCubeIndex;
if(edgeTable[iCubeIndex] != 0)
{
++uNoOfNonEmptyCells;
}
}while(volIter.moveForwardInRegionXYZ());//For each cell
return uNoOfNonEmptyCells;
}
uint32_t computeRoughBitmaskForSliceFromPrevious(VolumeIterator<uint8_t>& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask, uint8_t* previousBitmask)
{
uint32_t uNoOfNonEmptyCells = 0;
//Iterate over each cell in the region
volIter.setPosition(regSlice.getLowerCorner().getX(),regSlice.getLowerCorner().getY(), regSlice.getLowerCorner().getZ());
volIter.setValidRegion(regSlice);
do
{
//Current position
const uint16_t x = volIter.getPosX() - offset.getX();
const uint16_t y = 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;
if((x==0) && (y==0))
{
const uint8_t v001 = volIter.peekVoxel0px0py1pz();
const uint8_t v101 = volIter.peekVoxel1px0py1pz();
const uint8_t v011 = volIter.peekVoxel0px1py1pz();
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
//z
uint8_t iPreviousCubeIndexZ = previousBitmask[getIndex(x,y, regSlice.width()+1)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
if (v001 == 0) iCubeIndex |= 16;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else if((x>0) && y==0)
{
const uint8_t v101 = volIter.peekVoxel1px0py1pz();
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
//z
uint8_t iPreviousCubeIndexZ = previousBitmask[getIndex(x,y, regSlice.width()+1)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//x
uint8_t iPreviousCubeIndexX = bitmask[getIndex(x-1,y, regSlice.width()+1)];
uint8_t srcBit6 = iPreviousCubeIndexX & 64;
uint8_t destBit7 = srcBit6 << 1;
uint8_t srcBit5 = iPreviousCubeIndexX & 32;
uint8_t destBit4 = srcBit5 >> 1;
iCubeIndex |= destBit4;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
else if((x==0) && (y>0))
{
const uint8_t v011 = volIter.peekVoxel0px1py1pz();
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
//z
uint8_t iPreviousCubeIndexZ = previousBitmask[getIndex(x,y, regSlice.width()+1)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//y
uint8_t iPreviousCubeIndexY = bitmask[getIndex(x,y-1, regSlice.width()+1)];
uint8_t srcBit7 = iPreviousCubeIndexY & 128;
uint8_t destBit4 = srcBit7 >> 3;
uint8_t srcBit6 = iPreviousCubeIndexY & 64;
uint8_t destBit5 = srcBit6 >> 1;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else
{
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
//z
uint8_t iPreviousCubeIndexZ = previousBitmask[getIndex(x,y, regSlice.width()+1)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//y
uint8_t iPreviousCubeIndexY = bitmask[getIndex(x,y-1, regSlice.width()+1)];
uint8_t srcBit7 = iPreviousCubeIndexY & 128;
uint8_t destBit4 = srcBit7 >> 3;
uint8_t srcBit6 = iPreviousCubeIndexY & 64;
uint8_t destBit5 = srcBit6 >> 1;
//x
uint8_t iPreviousCubeIndexX = bitmask[getIndex(x-1,y, regSlice.width()+1)];
srcBit6 = iPreviousCubeIndexX & 64;
uint8_t destBit7 = srcBit6 << 1;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
//Save the bitmask
bitmask[getIndex(x,y, regSlice.width()+1)] = iCubeIndex;
if(edgeTable[iCubeIndex] != 0)
{
++uNoOfNonEmptyCells;
}
}while(volIter.moveForwardInRegionXYZ());//For each cell
return uNoOfNonEmptyCells;
}
void generateRoughVerticesForSlice(VolumeIterator<uint8_t>& 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
volIter.setPosition(regSlice.getLowerCorner().getX(),regSlice.getLowerCorner().getY(), regSlice.getLowerCorner().getZ());
volIter.setValidRegion(regSlice);
//while(volIter.moveForwardInRegionXYZ())
do
{
//Current position
const uint16_t x = volIter.getPosX() - offset.getX();
const uint16_t y = volIter.getPosY() - offset.getY();
const uint16_t z = volIter.getPosZ() - offset.getZ();
const uint8_t v000 = volIter.getVoxel();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8_t iCubeIndex = bitmask[getIndex(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)
{
if((x + offset.getX()) != regSlice.getUpperCorner().getX())
{
const uint8_t v100 = volIter.peekVoxel1px0py0pz();
const Vector3DFloat v3dPosition(x + 0.5f, y, z);
const Vector3DFloat v3dNormal(v000 > v100 ? 1.0f : -1.0f, 0.0f, 0.0f);
const uint8_t uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max.
const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
uint32_t uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex);
vertexIndicesX[getIndex(x,y, regSlice.width()+1)] = uLastVertexIndex;
}
}
if (edgeTable[iCubeIndex] & 8)
{
if((y + offset.getY()) != regSlice.getUpperCorner().getY())
{
const uint8_t v010 = volIter.peekVoxel0px1py0pz();
const Vector3DFloat v3dPosition(x, y + 0.5f, z);
const Vector3DFloat v3dNormal(0.0f, v000 > v010 ? 1.0f : -1.0f, 0.0f);
const uint8_t uMaterial = v000 | v010;
SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
uint32_t uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex);
vertexIndicesY[getIndex(x,y, regSlice.width()+1)] = uLastVertexIndex;
}
}
if (edgeTable[iCubeIndex] & 256)
{
//if((z + offset.getZ()) != upperCorner.getZ())
{
const uint8_t v001 = volIter.peekVoxel0px0py1pz();
const Vector3DFloat v3dPosition(x, y, z + 0.5f);
const Vector3DFloat v3dNormal(0.0f, 0.0f, v000 > v001 ? 1.0f : -1.0f);
const uint8_t uMaterial = v000 | v001;
SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
uint32_t uLastVertexIndex = singleMaterialPatch->addVertex(surfaceVertex);
vertexIndicesZ[getIndex(x,y, regSlice.width()+1)] = uLastVertexIndex;
}
}
}while(volIter.moveForwardInRegionXYZ());//For each cell
}
void generateRoughIndicesForSlice(VolumeIterator<uint8_t>& 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];
Region regCroppedSlice(regSlice);
regCroppedSlice.setUpperCorner(regCroppedSlice.getUpperCorner() - Vector3DInt32(1,1,0));
volIter.setPosition(regCroppedSlice.getLowerCorner().getX(),regCroppedSlice.getLowerCorner().getY(), regCroppedSlice.getLowerCorner().getZ());
volIter.setValidRegion(regCroppedSlice);
do
{
//Current position
const uint16_t x = volIter.getPosX() - offset.getX();
const uint16_t y = volIter.getPosY() - offset.getY();
const uint16_t z = volIter.getPosZ() - offset.getZ();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8_t iCubeIndex = bitmask0[getIndex(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[getIndex(x,y, regSlice.width()+1)];
assert(indlist[0] != -1);
}
if (edgeTable[iCubeIndex] & 2)
{
indlist[1] = vertexIndicesY0[getIndex(x+1,y, regSlice.width()+1)];
assert(indlist[1] != -1);
}
if (edgeTable[iCubeIndex] & 4)
{
indlist[2] = vertexIndicesX0[getIndex(x,y+1, regSlice.width()+1)];
assert(indlist[2] != -1);
}
if (edgeTable[iCubeIndex] & 8)
{
indlist[3] = vertexIndicesY0[getIndex(x,y, regSlice.width()+1)];
assert(indlist[3] != -1);
}
if (edgeTable[iCubeIndex] & 16)
{
indlist[4] = vertexIndicesX1[getIndex(x,y, regSlice.width()+1)];
assert(indlist[4] != -1);
}
if (edgeTable[iCubeIndex] & 32)
{
indlist[5] = vertexIndicesY1[getIndex(x+1,y, regSlice.width()+1)];
assert(indlist[5] != -1);
}
if (edgeTable[iCubeIndex] & 64)
{
indlist[6] = vertexIndicesX1[getIndex(x,y+1, regSlice.width()+1)];
assert(indlist[6] != -1);
}
if (edgeTable[iCubeIndex] & 128)
{
indlist[7] = vertexIndicesY1[getIndex(x,y, regSlice.width()+1)];
assert(indlist[7] != -1);
}
if (edgeTable[iCubeIndex] & 256)
{
indlist[8] = vertexIndicesZ0[getIndex(x,y, regSlice.width()+1)];
assert(indlist[8] != -1);
}
if (edgeTable[iCubeIndex] & 512)
{
indlist[9] = vertexIndicesZ0[getIndex(x+1,y, regSlice.width()+1)];
assert(indlist[9] != -1);
}
if (edgeTable[iCubeIndex] & 1024)
{
indlist[10] = vertexIndicesZ0[getIndex(x+1,y+1, regSlice.width()+1)];
assert(indlist[10] != -1);
}
if (edgeTable[iCubeIndex] & 2048)
{
indlist[11] = vertexIndicesZ0[getIndex(x,y+1, 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
}while(volIter.moveForwardInRegionXYZ());//For each cell
}
}

View File

@ -0,0 +1,326 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
//Based on code by Paul Bourke
//From the article "Polygonising a scalar field"
// http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/index.html
#include "PolyVoxImpl/MarchingCubesTables.h"
namespace PolyVox
{
int edgeTable[256]=
{
0x0 , 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c,
0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,
0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c,
0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,
0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c,
0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,
0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac,
0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,
0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c,
0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc,
0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,
0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c,
0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,
0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc ,
0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,
0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc,
0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,
0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c,
0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc,
0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,
0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c,
0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460,
0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac,
0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0,
0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c,
0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230,
0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c,
0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190,
0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c,
0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0
};
int triTable[256][16] =
{
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1, -1},
{3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1, -1},
{3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1, -1},
{3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1, -1},
{9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1},
{9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1},
{2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1, -1},
{8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1, -1},
{9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1},
{4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1, -1},
{3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1, -1},
{1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1, -1},
{4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1, -1},
{4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1},
{9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1},
{5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1, -1},
{2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1, -1},
{9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1, -1},
{0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1},
{2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1, -1},
{10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1},
{4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1, -1},
{5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1, -1},
{5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1},
{9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1, -1},
{0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1, -1},
{1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1, -1},
{10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1, -1},
{8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1, -1},
{2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1},
{7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1},
{9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1, -1},
{2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1, -1},
{11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1, -1},
{9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1, -1},
{5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0, -1},
{11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0, -1},
{11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1},
{1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1, -1},
{9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1, -1},
{5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1, -1},
{2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1},
{0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1},
{5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1, -1},
{6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1, -1},
{3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1, -1},
{6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1, -1},
{5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1, -1},
{1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1},
{10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1, -1},
{6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1, -1},
{8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1, -1},
{7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9, -1},
{3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1},
{5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1, -1},
{0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1},
{9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6, -1},
{8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1, -1},
{5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11, -1},
{0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7, -1},
{6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1, -1},
{10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1},
{10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1, -1},
{8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1, -1},
{1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1, -1},
{3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1, -1},
{0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1},
{10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1, -1},
{3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1, -1},
{6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1, -1},
{9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1, -1},
{8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1, -1},
{3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1, -1},
{6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1},
{0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1, -1},
{10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1, -1},
{10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1, -1},
{2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9, -1},
{7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1, -1},
{7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1, -1},
{2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7, -1},
{1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11, -1},
{11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1, -1},
{8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6, -1},
{0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1, -1},
{7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1},
{10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1},
{2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1},
{6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1, -1},
{7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1, -1},
{2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1, -1},
{1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1, -1},
{10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1, -1},
{10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1, -1},
{0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1, -1},
{7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1, -1},
{6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1, -1},
{8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1, -1},
{9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1, -1},
{6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1, -1},
{4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1, -1},
{10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3, -1},
{8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1},
{0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1, -1},
{1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1, -1},
{8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1, -1},
{10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1, -1},
{4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3, -1},
{10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1},
{5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1},
{11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1, -1},
{9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1},
{6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1, -1},
{7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1, -1},
{3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6, -1},
{7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1, -1},
{9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1, -1},
{3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1, -1},
{6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8, -1},
{9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1, -1},
{1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4, -1},
{4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10, -1},
{7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1, -1},
{6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1, -1},
{3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1, -1},
{0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1, -1},
{6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1, -1},
{0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10, -1},
{11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5, -1},
{6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1, -1},
{5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1, -1},
{9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1, -1},
{1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8, -1},
{1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6, -1},
{10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1, -1},
{0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1, -1},
{5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1, -1},
{10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1, -1},
{11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1, -1},
{9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1, -1},
{7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2, -1},
{2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1},
{8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1, -1},
{9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1, -1},
{9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2, -1},
{1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1, -1},
{9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1, -1},
{9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1},
{5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1, -1},
{0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1, -1},
{10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4, -1},
{2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1, -1},
{0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11, -1},
{0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5, -1},
{9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1, -1},
{5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1, -1},
{3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9, -1},
{5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1, -1},
{8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1, -1},
{0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1, -1},
{9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1, -1},
{0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1, -1},
{1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1, -1},
{3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4, -1},
{4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1, -1},
{9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3, -1},
{11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1, -1},
{11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1, -1},
{2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1, -1},
{9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7, -1},
{3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10, -1},
{1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1, -1},
{4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1, -1},
{4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1, -1},
{0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1, -1},
{3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1, -1},
{3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1, -1},
{0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1, -1},
{9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1, -1},
{1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
};
}

View File

@ -0,0 +1,334 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#include "PolyVoxImpl/ReferenceSurfaceExtractor.h"
#include "Volume.h"
#include "VolumeIterator.h"
#include "IndexedSurfacePatch.h"
#include "PolyVoxImpl/MarchingCubesTables.h"
#include "Region.h"
#include "SurfaceVertex.h"
#include "Vector.h"
namespace PolyVox
{
void extractReferenceSurfaceImpl(Volume<uint8_t>* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch)
{
/*static int32_t vertexIndicesX[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1];
static int32_t vertexIndicesY[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1];
static int32_t vertexIndicesZ[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1];*/
Vector3DInt32 regionDimensions = region.dimensions() + Vector3DInt32(1,1,1);
std::vector<int32_t> vertexIndicesX(regionDimensions.getX() * regionDimensions.getY() * regionDimensions.getZ());
std::vector<int32_t> vertexIndicesY(regionDimensions.getX() * regionDimensions.getY() * regionDimensions.getZ());
std::vector<int32_t> vertexIndicesZ(regionDimensions.getX() * regionDimensions.getY() * regionDimensions.getZ());
memset(&vertexIndicesX[0],0xFF,sizeof(vertexIndicesX[0]) * vertexIndicesX.size()); //0xFF is -1 as two's complement - this may not be portable...
memset(&vertexIndicesY[0],0xFF,sizeof(vertexIndicesY[0]) * vertexIndicesY.size()); //FIXME - can we just use sizeof(vertexIndicesY)?
memset(&vertexIndicesZ[0],0xFF,sizeof(vertexIndicesZ[0]) * vertexIndicesZ.size());
//When generating the mesh for a region we actually look one voxel 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(1,1,1));
region.cropTo(regVolume);
region.setUpperCorner(region.getUpperCorner() - Vector3DInt32(1,1,1));
//Offset from lower block corner
const Vector3DFloat offset = static_cast<Vector3DFloat>(region.getLowerCorner());
Vector3DFloat vertlist[12];
Vector3DFloat normlist[12];
uint8_t vertMaterials[12];
VolumeIterator<uint8_t> volIter(*volumeData);
volIter.setValidRegion(region);
//////////////////////////////////////////////////////////////////////////
//Get mesh data
//////////////////////////////////////////////////////////////////////////
//Iterate over each cell in the region
volIter.setPosition(region.getLowerCorner().getX(),region.getLowerCorner().getY(), region.getLowerCorner().getZ());
while(volIter.moveForwardInRegionXYZ())
{
//Current position
const uint16_t x = volIter.getPosX();
const uint16_t y = volIter.getPosY();
const uint16_t z = volIter.getPosZ();
//Voxels values
const uint8_t v000 = volIter.getVoxel();
const uint8_t v100 = volIter.peekVoxel1px0py0pz();
const uint8_t v010 = volIter.peekVoxel0px1py0pz();
const uint8_t v110 = volIter.peekVoxel1px1py0pz();
const uint8_t v001 = volIter.peekVoxel0px0py1pz();
const uint8_t v101 = volIter.peekVoxel1px0py1pz();
const uint8_t v011 = volIter.peekVoxel0px1py1pz();
const uint8_t v111 = volIter.peekVoxel1px1py1pz();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8_t iCubeIndex = 0;
if (v000 == 0) iCubeIndex |= 1;
if (v100 == 0) iCubeIndex |= 2;
if (v110 == 0) iCubeIndex |= 4;
if (v010 == 0) iCubeIndex |= 8;
if (v001 == 0) iCubeIndex |= 16;
if (v101 == 0) iCubeIndex |= 32;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
/* 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)
{
vertlist[0].setX(x + 0.5f);
vertlist[0].setY(y);
vertlist[0].setZ(z);
normlist[0].setX(v000 > v100 ? 1.0f : -1.0f);
normlist[0].setY(0.0f);
normlist[0].setZ(0.0f);
vertMaterials[0] = v000 | v100; //Because one of these is 0, the or operation takes the max.
}
if (edgeTable[iCubeIndex] & 2)
{
vertlist[1].setX(x + 1.0f);
vertlist[1].setY(y + 0.5f);
vertlist[1].setZ(z);
normlist[1].setX(0.0f);
normlist[1].setY(v100 > v110 ? 1.0f : -1.0f);
normlist[1].setZ(0.0f);
vertMaterials[1] = v100 | v110;
}
if (edgeTable[iCubeIndex] & 4)
{
vertlist[2].setX(x + 0.5f);
vertlist[2].setY(y + 1.0f);
vertlist[2].setZ(z);
normlist[2].setX(v010 > v110 ? 1.0f : -1.0f);
normlist[2].setY(0.0f);
normlist[2].setZ(0.0f);
vertMaterials[2] = v010 | v110;
}
if (edgeTable[iCubeIndex] & 8)
{
vertlist[3].setX(x);
vertlist[3].setY(y + 0.5f);
vertlist[3].setZ(z);
normlist[3].setX(0.0f);
normlist[3].setY(v000 > v010 ? 1.0f : -1.0f);
normlist[3].setZ(0.0f);
vertMaterials[3] = v000 | v010;
}
if (edgeTable[iCubeIndex] & 16)
{
vertlist[4].setX(x + 0.5f);
vertlist[4].setY(y);
vertlist[4].setZ(z + 1.0f);
normlist[4].setX(v001 > v101 ? 1.0f : -1.0f);
normlist[4].setY(0.0f);
normlist[4].setZ(0.0f);
vertMaterials[4] = v001 | v101;
}
if (edgeTable[iCubeIndex] & 32)
{
vertlist[5].setX(x + 1.0f);
vertlist[5].setY(y + 0.5f);
vertlist[5].setZ(z + 1.0f);
normlist[5].setX(0.0f);
normlist[5].setY(v101 > v111 ? 1.0f : -1.0f);
normlist[5].setZ(0.0f);
vertMaterials[5] = v101 | v111;
}
if (edgeTable[iCubeIndex] & 64)
{
vertlist[6].setX(x + 0.5f);
vertlist[6].setY(y + 1.0f);
vertlist[6].setZ(z + 1.0f);
normlist[6].setX(v011 > v111 ? 1.0f : -1.0f);
normlist[6].setY(0.0f);
normlist[6].setZ(0.0f);
vertMaterials[6] = v011 | v111;
}
if (edgeTable[iCubeIndex] & 128)
{
vertlist[7].setX(x);
vertlist[7].setY(y + 0.5f);
vertlist[7].setZ(z + 1.0f);
normlist[7].setX(0.0f);
normlist[7].setY(v001 > v011 ? 1.0f : -1.0f);
normlist[7].setZ(0.0f);
vertMaterials[7] = v001 | v011;
}
if (edgeTable[iCubeIndex] & 256)
{
vertlist[8].setX(x);
vertlist[8].setY(y);
vertlist[8].setZ(z + 0.5f);
normlist[8].setX(0.0f);
normlist[8].setY(0.0f);
normlist[8].setZ(v000 > v001 ? 1.0f : -1.0f);
vertMaterials[8] = v000 | v001;
}
if (edgeTable[iCubeIndex] & 512)
{
vertlist[9].setX(x + 1.0f);
vertlist[9].setY(y);
vertlist[9].setZ(z + 0.5f);
normlist[9].setX(0.0f);
normlist[9].setY(0.0f);
normlist[9].setZ(v100 > v101 ? 1.0f : -1.0f);
vertMaterials[9] = v100 | v101;
}
if (edgeTable[iCubeIndex] & 1024)
{
vertlist[10].setX(x + 1.0f);
vertlist[10].setY(y + 1.0f);
vertlist[10].setZ(z + 0.5f);
normlist[10].setX(0.0f);
normlist[10].setY(0.0f);
normlist[10].setZ(v110 > v111 ? 1.0f : -1.0f);
vertMaterials[10] = v110 | v111;
}
if (edgeTable[iCubeIndex] & 2048)
{
vertlist[11].setX(x);
vertlist[11].setY(y + 1.0f);
vertlist[11].setZ(z + 0.5f);
normlist[11].setX(0.0f);
normlist[11].setY(0.0f);
normlist[11].setZ(v010 > v011 ? 1.0f : -1.0f);
vertMaterials[11] = v010 | v011;
}
for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3)
{
//The three vertices forming a triangle
const Vector3DFloat vertex0 = vertlist[triTable[iCubeIndex][i ]] - offset;
const Vector3DFloat vertex1 = vertlist[triTable[iCubeIndex][i+1]] - offset;
const Vector3DFloat vertex2 = vertlist[triTable[iCubeIndex][i+2]] - offset;
const Vector3DFloat normal0 = normlist[triTable[iCubeIndex][i ]];
const Vector3DFloat normal1 = normlist[triTable[iCubeIndex][i+1]];
const Vector3DFloat normal2 = normlist[triTable[iCubeIndex][i+2]];
//Cast to floats and divide by two.
//const Vector3DFloat vertex0AsFloat = (static_cast<Vector3DFloat>(vertex0) / 2.0f) - offset;
//const Vector3DFloat vertex1AsFloat = (static_cast<Vector3DFloat>(vertex1) / 2.0f) - offset;
//const Vector3DFloat vertex2AsFloat = (static_cast<Vector3DFloat>(vertex2) / 2.0f) - offset;
const uint8_t material0 = vertMaterials[triTable[iCubeIndex][i ]];
const uint8_t material1 = vertMaterials[triTable[iCubeIndex][i+1]];
const uint8_t material2 = vertMaterials[triTable[iCubeIndex][i+2]];
//If all the materials are the same, we just need one triangle for that material with all the alphas set high.
SurfaceVertex v0(vertex0, normal0, material0 + 0.1f);
SurfaceVertex v1(vertex1, normal1, material1 + 0.1f);
SurfaceVertex v2(vertex2, normal2, material2 + 0.1f);
int32_t index0 = getIndexFor(v0.getPosition(), regionDimensions, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
if(index0 == -1)
{
index0 = singleMaterialPatch->addVertex(v0);
setIndexFor(v0.getPosition(), regionDimensions, index0, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
}
int32_t index1 = getIndexFor(v1.getPosition(), regionDimensions, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
if(index1 == -1)
{
index1 = singleMaterialPatch->addVertex(v1);
setIndexFor(v1.getPosition(), regionDimensions, index1, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
}
int32_t index2 = getIndexFor(v2.getPosition(), regionDimensions, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
if(index2 == -1)
{
index2 = singleMaterialPatch->addVertex(v2);
setIndexFor(v2.getPosition(), regionDimensions, index2, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
}
singleMaterialPatch->addTriangle(index0, index1, index2);
}//For each triangle
}//For each cell
}
int32_t getIndexFor(const Vector3DFloat& pos, const Vector3DInt32& regionDimensions, const std::vector<int32_t>& vertexIndicesX, const std::vector<int32_t>& vertexIndicesY, const std::vector<int32_t>& vertexIndicesZ)
{
float xIntPartAsFloat;
float xFracPart = std::modf(pos.getX(), &xIntPartAsFloat);
float yIntPartAsFloat;
float yFracPart = std::modf(pos.getY(), &yIntPartAsFloat);
float zIntPartAsFloat;
float zFracPart = std::modf(pos.getZ(), &zIntPartAsFloat);
uint16_t xIntPart = static_cast<uint16_t>(xIntPartAsFloat);
uint16_t yIntPart = static_cast<uint16_t>(yIntPartAsFloat);
uint16_t zIntPart = static_cast<uint16_t>(zIntPartAsFloat);
//Of all the fractional parts, two should be zero and one should be 0.5.
if(xFracPart > 0.25f)
{
return vertexIndicesX[xIntPart + yIntPart * regionDimensions.getX() + zIntPart * regionDimensions.getX() * regionDimensions.getY()];
}
if(yFracPart > 0.25f)
{
return vertexIndicesY[xIntPart + yIntPart * regionDimensions.getX() + zIntPart * regionDimensions.getX() * regionDimensions.getY()];
}
if(zFracPart > 0.25f)
{
return vertexIndicesZ[xIntPart + yIntPart * regionDimensions.getX() + zIntPart * regionDimensions.getX() * regionDimensions.getY()];
}
}
void setIndexFor(const Vector3DFloat& pos, const Vector3DInt32& regionDimensions, int32_t newIndex, std::vector<int32_t>& vertexIndicesX, std::vector<int32_t>& vertexIndicesY, std::vector<int32_t>& vertexIndicesZ)
{
float xIntPartAsFloat;
float xFracPart = std::modf(pos.getX(), &xIntPartAsFloat);
float yIntPartAsFloat;
float yFracPart = std::modf(pos.getY(), &yIntPartAsFloat);
float zIntPartAsFloat;
float zFracPart = std::modf(pos.getZ(), &zIntPartAsFloat);
uint16_t xIntPart = static_cast<uint16_t>(xIntPartAsFloat);
uint16_t yIntPart = static_cast<uint16_t>(yIntPartAsFloat);
uint16_t zIntPart = static_cast<uint16_t>(zIntPartAsFloat);
//Of all the fractional parts, two should be zero and one should be 0.5.
if(xFracPart > 0.25f)
{
vertexIndicesX[xIntPart + yIntPart * regionDimensions.getX() + zIntPart * regionDimensions.getX() * regionDimensions.getY()] = newIndex;
}
if(yFracPart > 0.25f)
{
vertexIndicesY[xIntPart + yIntPart * regionDimensions.getX() + zIntPart * regionDimensions.getX() * regionDimensions.getY()] = newIndex;
}
if(zFracPart > 0.25f)
{
vertexIndicesZ[xIntPart + yIntPart * regionDimensions.getX() + zIntPart * regionDimensions.getX() * regionDimensions.getY()] = newIndex;
}
}
}

View File

@ -0,0 +1,63 @@
#pragma region License
/******************************************************************************
This file is part of the PolyVox library
Copyright (C) 2006 David Williams
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
******************************************************************************/
#pragma endregion
#include "PolyVoxImpl/Utility.h"
#include <cassert>
#include <stdexcept>
namespace PolyVox
{
//Note: this function only works for inputs which are a power of two and not zero
//If this is not the case then the output is undefined.
uint8_t logBase2(uint32_t uInput)
{
//Debug mode validation
assert(uInput != 0);
assert(isPowerOf2(uInput));
//Release mode validation
if(uInput == 0)
{
throw std::invalid_argument("Cannot compute the log of zero.");
}
if(!isPowerOf2(uInput))
{
throw std::invalid_argument("Input must be a power of two in order to compute the log.");
}
uint32_t uResult = 0;
while( (uInput >> uResult) != 0)
{
++uResult;
}
return static_cast<uint8_t>(uResult-1);
}
bool isPowerOf2(uint32_t uInput)
{
if(uInput == 0)
return false;
else
return ((uInput & (uInput-1)) == 0);
}
}