Rearranging files in PolyVox.

This commit is contained in:
David Williams
2008-07-03 19:17:17 +00:00
parent 4f546d1dc4
commit 29ef5f021e
49 changed files with 6623 additions and 10 deletions

View File

@ -0,0 +1,174 @@
#include "PolyVoxCore/GradientEstimators.h"
#include "PolyVoxCore/IndexedSurfacePatch.h"
#include "PolyVoxCore/RegionGeometry.h"
#include "PolyVoxCore/SurfaceVertex.h"
#include "PolyVoxCore/PolyVoxCStdInt.h"
using namespace std;
namespace PolyVox
{
POLYVOX_API void computeNormalsForVertices(BlockVolume<uint8>* volumeData, RegionGeometry& regGeom, NormalGenerationMethod normalGenerationMethod)
{
std::vector<SurfaceVertex>& vecVertices = regGeom.m_patchSingleMaterial->m_vecVertices;
std::vector<SurfaceVertex>::iterator iterSurfaceVertex = vecVertices.begin();
while(iterSurfaceVertex != vecVertices.end())
{
const Vector3DFloat& v3dPos = iterSurfaceVertex->getPosition() + static_cast<Vector3DFloat>(regGeom.m_v3dRegionPosition);
const Vector3DInt32 v3dFloor = static_cast<Vector3DInt32>(v3dPos);
BlockVolumeIterator<uint8> volIter(*volumeData);
//Check all corners are within the volume, allowing a boundary for gradient estimation
bool lowerCornerInside = volumeData->containsPoint(v3dFloor,1);
bool upperCornerInside = volumeData->containsPoint(v3dFloor+Vector3DInt32(1,1,1),1);
if(lowerCornerInside && upperCornerInside) //If this test fails the vertex will be left as it was
{
Vector3DFloat v3dGradient; //To store the result
if(normalGenerationMethod == SOBEL)
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor));
const Vector3DFloat gradFloor = computeSobelGradient(volIter);
if((v3dPos.getX() - v3dFloor.getX()) > 0.001)
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(1,0,0)));
}
if((v3dPos.getY() - v3dFloor.getY()) > 0.001)
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,1,0)));
}
if((v3dPos.getZ() - v3dFloor.getZ()) > 0.001)
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,0,1)));
}
const Vector3DFloat gradCeil = computeSobelGradient(volIter);
v3dGradient = (gradFloor + gradCeil);
}
if(normalGenerationMethod == CENTRAL_DIFFERENCE)
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor));
const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter);
if((v3dPos.getX() - v3dFloor.getX()) > 0.001)
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(1,0,0)));
}
if((v3dPos.getY() - v3dFloor.getY()) > 0.001)
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,1,0)));
}
if((v3dPos.getZ() - v3dFloor.getZ()) > 0.001)
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,0,1)));
}
const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter);
v3dGradient = (gradFloor + gradCeil);
}
if(v3dGradient.lengthSquared() > 0.0001)
{
//If we got a normal of significant length then update it.
//Otherwise leave it as it was (should be the 'simple' version)
v3dGradient.normalise();
iterSurfaceVertex->setNormal(v3dGradient);
}
} //(lowerCornerInside && upperCornerInside)
++iterSurfaceVertex;
}
}
Vector3DFloat computeNormal(BlockVolume<uint8>* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod)
{
const float posX = position.getX();
const float posY = position.getY();
const float posZ = position.getZ();
const uint16 floorX = static_cast<uint16>(posX);
const uint16 floorY = static_cast<uint16>(posY);
const uint16 floorZ = static_cast<uint16>(posZ);
//Check all corners are within the volume, allowing a boundary for gradient estimation
bool lowerCornerInside = volumeData->containsPoint(Vector3DInt32(floorX, floorY, floorZ),1);
bool upperCornerInside = volumeData->containsPoint(Vector3DInt32(floorX+1, floorY+1, floorZ+1),1);
if((!lowerCornerInside) || (!upperCornerInside))
{
normalGenerationMethod = SIMPLE;
}
Vector3DFloat result;
BlockVolumeIterator<uint8> volIter(*volumeData); //FIXME - save this somewhere - could be expensive to create?
if(normalGenerationMethod == SOBEL)
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ));
const Vector3DFloat gradFloor = computeSobelGradient(volIter);
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX+1.0),static_cast<uint16>(posY),static_cast<uint16>(posZ));
}
if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY+1.0),static_cast<uint16>(posZ));
}
if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ+1.0));
}
const Vector3DFloat gradCeil = computeSobelGradient(volIter);
result = ((gradFloor + gradCeil) * -1.0f);
if(result.lengthSquared() < 0.0001)
{
//Operation failed - fall back on simple gradient estimation
normalGenerationMethod = SIMPLE;
}
}
if(normalGenerationMethod == CENTRAL_DIFFERENCE)
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ));
const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter);
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX+1.0),static_cast<uint16>(posY),static_cast<uint16>(posZ));
}
if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY+1.0),static_cast<uint16>(posZ));
}
if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ+1.0));
}
const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter);
result = ((gradFloor + gradCeil) * -1.0f);
if(result.lengthSquared() < 0.0001)
{
//Operation failed - fall back on simple gradient estimation
normalGenerationMethod = SIMPLE;
}
}
if(normalGenerationMethod == SIMPLE)
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ));
const uint8 uFloor = volIter.getVoxel() > 0 ? 1 : 0;
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
{
uint8 uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0;
result = Vector3DFloat(static_cast<float>(uFloor - uCeil),0.0,0.0);
}
else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
uint8 uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0;
result = Vector3DFloat(0.0,static_cast<float>(uFloor - uCeil),0.0);
}
else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
uint8 uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0;
result = Vector3DFloat(0.0, 0.0,static_cast<float>(uFloor - uCeil));
}
}
return result;
}
}

View File

@ -0,0 +1,106 @@
#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 "PolyVoxCore/IndexedSurfacePatch.h"
using namespace std;
namespace PolyVox
{
IndexedSurfacePatch::IndexedSurfacePatch()
{
}
IndexedSurfacePatch::~IndexedSurfacePatch()
{
}
void IndexedSurfacePatch::addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2)
{
m_vecVertices.push_back(v0);
m_vecTriangleIndices.push_back(m_vecVertices.size()-1);
m_vecVertices.push_back(v1);
m_vecTriangleIndices.push_back(m_vecVertices.size()-1);
m_vecVertices.push_back(v2);
m_vecTriangleIndices.push_back(m_vecVertices.size()-1);
}
void IndexedSurfacePatch::fillVertexAndIndexData(std::vector<SurfaceVertex>& vecVertices, std::vector<uint32>& vecIndices)
{
vecVertices.resize(m_vecVertices.size());
std::copy(m_vecVertices.begin(), m_vecVertices.end(), vecVertices.begin());
vecIndices.resize(m_vecTriangleIndices.size());
std::copy(m_vecTriangleIndices.begin(), m_vecTriangleIndices.end(), vecIndices.begin());
/*for(std::vector<SurfaceVertexIterator>::iterator iterVertices = m_vecTriangleIndices.begin(); iterVertices != m_vecTriangleIndices.end(); ++iterVertices)
{
std::vector<SurfaceVertex>::iterator iterVertex = lower_bound(vecVertices.begin(), vecVertices.end(), **iterVertices);
vecIndices.push_back(iterVertex - vecVertices.begin());
}*/
}
const std::vector<SurfaceVertex>& IndexedSurfacePatch::getVertices(void) const
{
return m_vecVertices;
}
std::vector<SurfaceVertex>& IndexedSurfacePatch::getVertices(void)
{
return m_vecVertices;
}
const std::vector<uint32>& IndexedSurfacePatch::getIndices(void) const
{
return m_vecTriangleIndices;
}
unsigned short IndexedSurfacePatch::getNoNonUniformTrianges(void)
{
unsigned short result = 0;
for(int i = 0; i < m_vecTriangleIndices.size() - 2; i += 3)
{
if((m_vecVertices[m_vecTriangleIndices[i]].getMaterial() == m_vecVertices[m_vecTriangleIndices[i+1]].getMaterial())
&& (m_vecVertices[m_vecTriangleIndices[i]].getMaterial() == m_vecVertices[m_vecTriangleIndices[i+2]].getMaterial()))
{
}
else
{
result++;
}
}
return result;
}
unsigned short IndexedSurfacePatch::getNoUniformTrianges(void)
{
unsigned short result = 0;
for(int i = 0; i < m_vecTriangleIndices.size() - 2; i += 3)
{
if((m_vecVertices[m_vecTriangleIndices[i]].getMaterial() == m_vecVertices[m_vecTriangleIndices[i+1]].getMaterial())
&& (m_vecVertices[m_vecTriangleIndices[i]].getMaterial() == m_vecVertices[m_vecTriangleIndices[i+2]].getMaterial()))
{
result++;
}
}
return result;
}
}

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 "PolyVoxCore/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,72 @@
#include "PolyVoxCore/Region.h"
namespace PolyVox
{
Region::Region()
:m_v3dLowerCorner(0,0,0)
,m_v3dUpperCorner(0,0,0)
{
}
Region::Region(const Vector3DInt32& v3dLowerCorner, const Vector3DInt32& v3dUpperCorner)
:m_v3dLowerCorner(v3dLowerCorner)
,m_v3dUpperCorner(v3dUpperCorner)
{
}
const Vector3DInt32& Region::getLowerCorner(void) const
{
return m_v3dLowerCorner;
}
const Vector3DInt32& Region::getUpperCorner(void) const
{
return m_v3dUpperCorner;
}
void Region::setLowerCorner(const Vector3DInt32& v3dLowerCorner)
{
m_v3dLowerCorner = v3dLowerCorner;
}
void Region::setUpperCorner(const Vector3DInt32& v3dUpperCorner)
{
m_v3dUpperCorner = v3dUpperCorner;
}
bool Region::containsPoint(const Vector3DFloat& pos, float boundary) const
{
return (pos.getX() <= m_v3dUpperCorner.getX() - boundary)
&& (pos.getY() <= m_v3dUpperCorner.getY() - boundary)
&& (pos.getZ() <= m_v3dUpperCorner.getZ() - boundary)
&& (pos.getX() >= m_v3dLowerCorner.getX() + boundary)
&& (pos.getY() >= m_v3dLowerCorner.getY() + boundary)
&& (pos.getZ() >= m_v3dLowerCorner.getZ() + boundary);
}
bool Region::containsPoint(const Vector3DInt32& pos, uint8 boundary) const
{
return (pos.getX() <= m_v3dUpperCorner.getX() - boundary)
&& (pos.getY() <= m_v3dUpperCorner.getY() - boundary)
&& (pos.getZ() <= m_v3dUpperCorner.getZ() - boundary)
&& (pos.getX() >= m_v3dLowerCorner.getX() + boundary)
&& (pos.getY() >= m_v3dLowerCorner.getY() + boundary)
&& (pos.getZ() >= m_v3dLowerCorner.getZ() + boundary);
}
void Region::cropTo(const Region& other)
{
m_v3dLowerCorner.setX((std::max)(m_v3dLowerCorner.getX(), other.m_v3dLowerCorner.getX()));
m_v3dLowerCorner.setY((std::max)(m_v3dLowerCorner.getY(), other.m_v3dLowerCorner.getY()));
m_v3dLowerCorner.setZ((std::max)(m_v3dLowerCorner.getZ(), other.m_v3dLowerCorner.getZ()));
m_v3dUpperCorner.setX((std::min)(m_v3dUpperCorner.getX(), other.m_v3dUpperCorner.getX()));
m_v3dUpperCorner.setY((std::min)(m_v3dUpperCorner.getY(), other.m_v3dUpperCorner.getY()));
m_v3dUpperCorner.setZ((std::min)(m_v3dUpperCorner.getZ(), other.m_v3dUpperCorner.getZ()));
}
void Region::shift(const Vector3DInt32& amount)
{
m_v3dLowerCorner += amount;
m_v3dUpperCorner += amount;
}
}

View File

@ -0,0 +1,30 @@
#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 "PolyVoxCore/IndexedSurfacePatch.h"
#include "PolyVoxCore/RegionGeometry.h"
namespace PolyVox
{
RegionGeometry::RegionGeometry()
{
}
}

View File

@ -0,0 +1,151 @@
#include "PolyVoxCore/SurfaceAdjusters.h"
#include "PolyVoxCore/BlockVolumeIterator.h"
#include "PolyVoxCore/GradientEstimators.h"
#include "PolyVoxCore/IndexedSurfacePatch.h"
#include "PolyVoxCore/RegionGeometry.h"
#include "PolyVoxCore/Utility.h"
#include "PolyVoxCore/VoxelFilters.h"
#include <vector>
using namespace std;
namespace PolyVox
{
void smoothRegionGeometry(BlockVolume<uint8>* volumeData, RegionGeometry& regGeom)
{
const uint8 uSmoothingFactor = 2;
const float fThreshold = 0.5f;
BlockVolumeIterator<uint8> volIter(*volumeData);
std::vector<SurfaceVertex>& vecVertices = regGeom.m_patchSingleMaterial->m_vecVertices;
std::vector<SurfaceVertex>::iterator iterSurfaceVertex = vecVertices.begin();
while(iterSurfaceVertex != vecVertices.end())
{
for(int ct = 0; ct < uSmoothingFactor; ++ct)
{
const Vector3DFloat& v3dPos = iterSurfaceVertex->getPosition() + static_cast<Vector3DFloat>(regGeom.m_v3dRegionPosition);
const Vector3DInt32 v3dFloor = static_cast<Vector3DInt32>(v3dPos);
const Vector3DFloat& v3dRem = v3dPos - static_cast<Vector3DFloat>(v3dFloor);
//Check all corners are within the volume, allowing a boundary for gradient estimation
bool lowerCornerInside = volumeData->containsPoint(v3dFloor,2);
bool upperCornerInside = volumeData->containsPoint(v3dFloor+Vector3DInt32(1,1,1),2);
if(lowerCornerInside && upperCornerInside) //If this test fails the vertex will be left as it was
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor + Vector3DInt32(0,0,0)));
const float v000 = computeSmoothedVoxel(volIter);
Vector3DFloat grad000 = computeSmoothCentralDifferenceGradient(volIter);
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor + Vector3DInt32(1,0,0)));
const float v100 = computeSmoothedVoxel(volIter);
Vector3DFloat grad100 = computeSmoothCentralDifferenceGradient(volIter);
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor + Vector3DInt32(0,1,0)));
const float v010 = computeSmoothedVoxel(volIter);
Vector3DFloat grad010 = computeSmoothCentralDifferenceGradient(volIter);
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor + Vector3DInt32(1,1,0)));
const float v110 = computeSmoothedVoxel(volIter);
Vector3DFloat grad110 = computeSmoothCentralDifferenceGradient(volIter);
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor + Vector3DInt32(0,0,1)));
const float v001 = computeSmoothedVoxel(volIter);
Vector3DFloat grad001 = computeSmoothCentralDifferenceGradient(volIter);
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor + Vector3DInt32(1,0,1)));
const float v101 = computeSmoothedVoxel(volIter);
Vector3DFloat grad101 = computeSmoothCentralDifferenceGradient(volIter);
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor + Vector3DInt32(0,1,1)));
const float v011 = computeSmoothedVoxel(volIter);
Vector3DFloat grad011 = computeSmoothCentralDifferenceGradient(volIter);
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor + Vector3DInt32(1,1,1)));
const float v111 = computeSmoothedVoxel(volIter);
Vector3DFloat grad111 = computeSmoothCentralDifferenceGradient(volIter);
float fInterVal = trilinearlyInterpolate(v000,v100,v010,v110,v001,v101,v011,v111,v3dRem.getX(),v3dRem.getY(),v3dRem.getZ());
Vector3DFloat fInterGrad = trilinearlyInterpolate(grad000,grad100,grad010,grad110,grad001,grad101,grad011,grad111,v3dRem.getX(),v3dRem.getY(),v3dRem.getZ());
fInterGrad.normalise();
float fDiff = fInterVal - fThreshold;
iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() + (fInterGrad * fDiff));
iterSurfaceVertex->setNormal(fInterGrad); //This is actually the gradient for the previous position, but it won't have moved much.
} //if(lowerCornerInside && upperCornerInside)
} //for(int ct = 0; ct < uSmoothingFactor; ++ct)
++iterSurfaceVertex;
} //while(iterSurfaceVertex != vecVertices.end())
}
void adjustDecimatedGeometry(BlockVolume<uint8>* volumeData, RegionGeometry& regGeom, uint8 val)
{
BlockVolumeIterator<uint8> volIter(*volumeData);
std::vector<SurfaceVertex>& vecVertices = regGeom.m_patchSingleMaterial->m_vecVertices;
std::vector<SurfaceVertex>::iterator iterSurfaceVertex = vecVertices.begin();
while(iterSurfaceVertex != vecVertices.end())
{
Vector3DFloat v3dPos = iterSurfaceVertex->getPosition() + static_cast<Vector3DFloat>(regGeom.m_v3dRegionPosition);
Vector3DInt32 v3dFloor = static_cast<Vector3DInt32>(v3dPos);
BlockVolumeIterator<uint8> volIter(*volumeData);
//Check all corners are within the volume, allowing a boundary for gradient estimation
bool lowerCornerInside = volumeData->containsPoint(v3dFloor,1);
bool upperCornerInside = volumeData->containsPoint(v3dFloor+Vector3DInt32(1,1,1),1);
if(lowerCornerInside && upperCornerInside) //If this test fails the vertex will be left as it was
{
//volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor));
//const uint8 uFloor = volIter.getVoxel();
if(((v3dPos.getX() - v3dFloor.getX()) < 0.001) && ((v3dPos.getY() - v3dFloor.getY()) < 0.001) && ((v3dPos.getZ() - v3dFloor.getZ()) < 0.001))
//int x = v3dPos.getX();
//if(x % 2 != 0)
//if((iterSurfaceVertex->getNormal().getX() > 0.5f) || (iterSurfaceVertex->getNormal().getX() < -0.5f))
{
//exit(0);
//volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(1,0,0)));
//const uint8 uCeil = volIter.getVoxel();
//if(uFloor == uCeil) //In this case they must both be zero
{
//if(iterSurfaceVertex->getNormal().getX() > 0)
{
iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() - iterSurfaceVertex->getNormal() * 0.5f);
v3dPos = iterSurfaceVertex->getPosition() + static_cast<Vector3DFloat>(regGeom.m_v3dRegionPosition);
v3dFloor = static_cast<Vector3DInt32>(v3dPos);
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor));
const uint8 uFloor = volIter.getVoxel();
uint8 uCeil;
if((iterSurfaceVertex->getNormal().getX() > 0.5f) || (iterSurfaceVertex->getNormal().getX() < -0.5f))
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(1,0,0)));
uCeil = volIter.getVoxel();
}
if((iterSurfaceVertex->getNormal().getY() > 0.5f) || (iterSurfaceVertex->getNormal().getY() < -0.5f))
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,1,0)));
uCeil = volIter.getVoxel();
}
if((iterSurfaceVertex->getNormal().getZ() > 0.5f) || (iterSurfaceVertex->getNormal().getZ() < -0.5f))
{
volIter.setPosition(static_cast<Vector3DInt16>(v3dFloor+Vector3DInt32(0,0,1)));
uCeil = volIter.getVoxel();
}
if(uFloor == uCeil)
{
//NOTE: The normal should actually be multiplied by 1.0f. This works
//for the simple cube but causes depth fighting on more complex shapes.
iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() - iterSurfaceVertex->getNormal() * 0.5f);
}
}
}
}
}
++iterSurfaceVertex;
} //while(iterSurfaceVertex != vecVertices.end())
}
}

View File

@ -0,0 +1,106 @@
#include <sstream>
#include "SurfaceEdge.h"
#include "SurfaceTriangle.h"
#include "SurfaceVertex.h"
namespace PolyVox
{
SurfaceEdge::SurfaceEdge(const SurfaceVertexIterator& targetToSet,const SurfaceVertexIterator& sourceToSet)
{
target = targetToSet;
source = sourceToSet;
}
std::string SurfaceEdge::tostring(void)
{
std::stringstream ss;
ss << "SurfaceEdge: Target Vertex = " << target->tostring() << "Source Vertex = " << source->tostring();
return ss.str();
}
bool operator == (const SurfaceEdge& lhs, const SurfaceEdge& rhs)
{
//Vertices are unique in the set, so if the two positions are the same the
//two iterators must also be the same. So we just check the iterators.
return
(
(lhs.target == rhs.target) &&
(lhs.source == rhs.source)
);
}
bool SurfaceEdge::isDegenerate(void)
{
return (target == source);
}
bool operator < (const SurfaceEdge& lhs, const SurfaceEdge& rhs)
{
//Unlike the equality operator, we can't compare iterators.
//So dereference and compare the results.
if ((*lhs.target) < (*rhs.target))
return true;
if ((*rhs.target) < (*lhs.target))
return false;
if ((*lhs.source) < (*rhs.source))
return true;
if ((*rhs.source) < (*lhs.source))
return false;
return false;
}
const SurfaceVertexIterator& SurfaceEdge::getTarget(void) const
{
return target;
}
const SurfaceVertexIterator& SurfaceEdge::getSource(void) const
{
return source;
}
void SurfaceEdge::pairWithOtherHalfEdge(const SurfaceEdgeIterator& otherHalfEdgeToPair)
{
otherHalfEdge = otherHalfEdgeToPair;
previousHalfEdge = otherHalfEdgeToPair;
nextHalfEdge = otherHalfEdgeToPair;
}
const SurfaceEdgeIterator& SurfaceEdge::getOtherHalfEdge(void) const
{
return otherHalfEdge;
}
const SurfaceEdgeIterator& SurfaceEdge::getPreviousHalfEdge(void) const
{
return previousHalfEdge;
}
const SurfaceEdgeIterator& SurfaceEdge::getNextHalfEdge(void) const
{
return nextHalfEdge;
}
const SurfaceTriangleIterator& SurfaceEdge::getTriangle(void) const
{
return triangle;
}
void SurfaceEdge::setPreviousHalfEdge(const SurfaceEdgeIterator& previousHalfEdgeToSet)
{
previousHalfEdge = previousHalfEdgeToSet;
}
void SurfaceEdge::setNextHalfEdge(const SurfaceEdgeIterator& nextHalfEdgeToSet)
{
nextHalfEdge = nextHalfEdgeToSet;
}
void SurfaceEdge::setTriangle(const SurfaceTriangleIterator& triangleToSet)
{
triangle = triangleToSet;
}
}

View File

@ -0,0 +1,853 @@
#include "PolyVoxCore/SurfaceExtractors.h"
#include "PolyVoxCore/BlockVolume.h"
#include "PolyVoxCore/GradientEstimators.h"
#include "PolyVoxCore/IndexedSurfacePatch.h"
#include "PolyVoxCore/MarchingCubesTables.h"
#include "PolyVoxCore/Region.h"
#include "PolyVoxCore/RegionGeometry.h"
#include "PolyVoxCore/SurfaceAdjusters.h"
#include "PolyVoxCore/SurfaceExtractorsDecimated.h"
#include "PolyVoxCore/BlockVolumeIterator.h"
#include <algorithm>
using namespace std;
namespace PolyVox
{
uint32 getIndex(uint32 x, uint32 y)
{
return x + (y * (POLYVOX_REGION_SIDE_LENGTH+1));
}
void generateRoughMeshDataForRegion(BlockVolume<uint8>* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch)
{
singleMaterialPatch->m_vecVertices.clear();
singleMaterialPatch->m_vecTriangleIndices.clear();
//For edge indices
int32* vertexIndicesX0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesY0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesZ0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesX1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesY1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesZ1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
//Cell bitmasks
uint8* bitmask0 = new uint8[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
uint8* bitmask1 = new uint8[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
//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
BlockVolumeIterator<uint8> volIter(*volumeData);
//Compute bitmask for initial slice
uint32 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 uSlice = 0; ((uSlice <= POLYVOX_REGION_SIDE_LENGTH-1) && (uSlice + offset.getZ() < region.getUpperCorner().getZ())); ++uSlice)
{
Region regSlice1(regSlice0);
regSlice1.shift(Vector3DInt32(0,0,1));
uint32 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 computeInitialRoughBitmaskForSlice(BlockVolumeIterator<uint8>& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8* bitmask)
{
uint32 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 x = volIter.getPosX() - offset.getX();
const uint16 y = volIter.getPosY() - offset.getY();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8 iCubeIndex = 0;
if((x==0) && (y==0))
{
const uint8 v000 = volIter.getVoxel();
const uint8 v100 = volIter.peekVoxel1px0py0pz();
const uint8 v010 = volIter.peekVoxel0px1py0pz();
const uint8 v110 = volIter.peekVoxel1px1py0pz();
const uint8 v001 = volIter.peekVoxel0px0py1pz();
const uint8 v101 = volIter.peekVoxel1px0py1pz();
const uint8 v011 = volIter.peekVoxel0px1py1pz();
const uint8 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 v100 = volIter.peekVoxel1px0py0pz();
const uint8 v110 = volIter.peekVoxel1px1py0pz();
const uint8 v101 = volIter.peekVoxel1px0py1pz();
const uint8 v111 = volIter.peekVoxel1px1py1pz();
//x
uint8 iPreviousCubeIndexX = bitmask[getIndex(x-1,y)];
uint8 srcBit6 = iPreviousCubeIndexX & 64;
uint8 destBit7 = srcBit6 << 1;
uint8 srcBit5 = iPreviousCubeIndexX & 32;
uint8 destBit4 = srcBit5 >> 1;
uint8 srcBit2 = iPreviousCubeIndexX & 4;
uint8 destBit3 = srcBit2 << 1;
uint8 srcBit1 = iPreviousCubeIndexX & 2;
uint8 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 v010 = volIter.peekVoxel0px1py0pz();
const uint8 v110 = volIter.peekVoxel1px1py0pz();
const uint8 v011 = volIter.peekVoxel0px1py1pz();
const uint8 v111 = volIter.peekVoxel1px1py1pz();
//y
uint8 iPreviousCubeIndexY = bitmask[getIndex(x,y-1)];
uint8 srcBit7 = iPreviousCubeIndexY & 128;
uint8 destBit4 = srcBit7 >> 3;
uint8 srcBit6 = iPreviousCubeIndexY & 64;
uint8 destBit5 = srcBit6 >> 1;
uint8 srcBit3 = iPreviousCubeIndexY & 8;
uint8 destBit0 = srcBit3 >> 3;
uint8 srcBit2 = iPreviousCubeIndexY & 4;
uint8 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 v110 = volIter.peekVoxel1px1py0pz();
const uint8 v111 = volIter.peekVoxel1px1py1pz();
//y
uint8 iPreviousCubeIndexY = bitmask[getIndex(x,y-1)];
uint8 srcBit7 = iPreviousCubeIndexY & 128;
uint8 destBit4 = srcBit7 >> 3;
uint8 srcBit6 = iPreviousCubeIndexY & 64;
uint8 destBit5 = srcBit6 >> 1;
uint8 srcBit3 = iPreviousCubeIndexY & 8;
uint8 destBit0 = srcBit3 >> 3;
uint8 srcBit2 = iPreviousCubeIndexY & 4;
uint8 destBit1 = srcBit2 >> 1;
//x
uint8 iPreviousCubeIndexX = bitmask[getIndex(x-1,y)];
srcBit6 = iPreviousCubeIndexX & 64;
uint8 destBit7 = srcBit6 << 1;
srcBit2 = iPreviousCubeIndexX & 4;
uint8 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)] = iCubeIndex;
if(edgeTable[iCubeIndex] != 0)
{
++uNoOfNonEmptyCells;
}
}while(volIter.moveForwardInRegionXYZ());//For each cell
return uNoOfNonEmptyCells;
}
uint32 computeRoughBitmaskForSliceFromPrevious(BlockVolumeIterator<uint8>& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8* bitmask, uint8* previousBitmask)
{
uint32 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 x = volIter.getPosX() - offset.getX();
const uint16 y = volIter.getPosY() - offset.getY();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8 iCubeIndex = 0;
if((x==0) && (y==0))
{
const uint8 v001 = volIter.peekVoxel0px0py1pz();
const uint8 v101 = volIter.peekVoxel1px0py1pz();
const uint8 v011 = volIter.peekVoxel0px1py1pz();
const uint8 v111 = volIter.peekVoxel1px1py1pz();
//z
uint8 iPreviousCubeIndexZ = previousBitmask[getIndex(x,y)];
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 v101 = volIter.peekVoxel1px0py1pz();
const uint8 v111 = volIter.peekVoxel1px1py1pz();
//z
uint8 iPreviousCubeIndexZ = previousBitmask[getIndex(x,y)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//x
uint8 iPreviousCubeIndexX = bitmask[getIndex(x-1,y)];
uint8 srcBit6 = iPreviousCubeIndexX & 64;
uint8 destBit7 = srcBit6 << 1;
uint8 srcBit5 = iPreviousCubeIndexX & 32;
uint8 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 v011 = volIter.peekVoxel0px1py1pz();
const uint8 v111 = volIter.peekVoxel1px1py1pz();
//z
uint8 iPreviousCubeIndexZ = previousBitmask[getIndex(x,y)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//y
uint8 iPreviousCubeIndexY = bitmask[getIndex(x,y-1)];
uint8 srcBit7 = iPreviousCubeIndexY & 128;
uint8 destBit4 = srcBit7 >> 3;
uint8 srcBit6 = iPreviousCubeIndexY & 64;
uint8 destBit5 = srcBit6 >> 1;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
if (v011 == 0) iCubeIndex |= 128;
}
else
{
const uint8 v111 = volIter.peekVoxel1px1py1pz();
//z
uint8 iPreviousCubeIndexZ = previousBitmask[getIndex(x,y)];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//y
uint8 iPreviousCubeIndexY = bitmask[getIndex(x,y-1)];
uint8 srcBit7 = iPreviousCubeIndexY & 128;
uint8 destBit4 = srcBit7 >> 3;
uint8 srcBit6 = iPreviousCubeIndexY & 64;
uint8 destBit5 = srcBit6 >> 1;
//x
uint8 iPreviousCubeIndexX = bitmask[getIndex(x-1,y)];
srcBit6 = iPreviousCubeIndexX & 64;
uint8 destBit7 = srcBit6 << 1;
iCubeIndex |= destBit4;
iCubeIndex |= destBit5;
if (v111 == 0) iCubeIndex |= 64;
iCubeIndex |= destBit7;
}
//Save the bitmask
bitmask[getIndex(x,y)] = iCubeIndex;
if(edgeTable[iCubeIndex] != 0)
{
++uNoOfNonEmptyCells;
}
}while(volIter.moveForwardInRegionXYZ());//For each cell
return uNoOfNonEmptyCells;
}
void generateRoughVerticesForSlice(BlockVolumeIterator<uint8>& volIter, Region& regSlice, const Vector3DFloat& offset, uint8* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32 vertexIndicesX[],int32 vertexIndicesY[],int32 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 x = volIter.getPosX() - offset.getX();
const uint16 y = volIter.getPosY() - offset.getY();
const uint16 z = volIter.getPosZ() - offset.getZ();
const uint8 v000 = volIter.getVoxel();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8 iCubeIndex = bitmask[getIndex(x,y)];
/* 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 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 uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max.
const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
singleMaterialPatch->m_vecVertices.push_back(surfaceVertex);
vertexIndicesX[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1;
}
}
if (edgeTable[iCubeIndex] & 8)
{
if((y + offset.getY()) != regSlice.getUpperCorner().getY())
{
const uint8 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 uMaterial = v000 | v010;
SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
singleMaterialPatch->m_vecVertices.push_back(surfaceVertex);
vertexIndicesY[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1;
}
}
if (edgeTable[iCubeIndex] & 256)
{
//if((z + offset.getZ()) != upperCorner.getZ())
{
const uint8 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 uMaterial = v000 | v001;
SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
singleMaterialPatch->m_vecVertices.push_back(surfaceVertex);
vertexIndicesZ[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1;
}
}
}while(volIter.moveForwardInRegionXYZ());//For each cell
}
void generateRoughIndicesForSlice(BlockVolumeIterator<uint8>& volIter, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8* bitmask0, uint8* bitmask1, int32 vertexIndicesX0[],int32 vertexIndicesY0[],int32 vertexIndicesZ0[], int32 vertexIndicesX1[],int32 vertexIndicesY1[],int32 vertexIndicesZ1[])
{
uint32 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 x = volIter.getPosX() - offset.getX();
const uint16 y = volIter.getPosY() - offset.getY();
const uint16 z = volIter.getPosZ() - offset.getZ();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8 iCubeIndex = bitmask0[getIndex(x,y)];
/* 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)];
assert(indlist[0] != -1);
}
if (edgeTable[iCubeIndex] & 2)
{
indlist[1] = vertexIndicesY0[getIndex(x+1,y)];
assert(indlist[1] != -1);
}
if (edgeTable[iCubeIndex] & 4)
{
indlist[2] = vertexIndicesX0[getIndex(x,y+1)];
assert(indlist[2] != -1);
}
if (edgeTable[iCubeIndex] & 8)
{
indlist[3] = vertexIndicesY0[getIndex(x,y)];
assert(indlist[3] != -1);
}
if (edgeTable[iCubeIndex] & 16)
{
indlist[4] = vertexIndicesX1[getIndex(x,y)];
assert(indlist[4] != -1);
}
if (edgeTable[iCubeIndex] & 32)
{
indlist[5] = vertexIndicesY1[getIndex(x+1,y)];
assert(indlist[5] != -1);
}
if (edgeTable[iCubeIndex] & 64)
{
indlist[6] = vertexIndicesX1[getIndex(x,y+1)];
assert(indlist[6] != -1);
}
if (edgeTable[iCubeIndex] & 128)
{
indlist[7] = vertexIndicesY1[getIndex(x,y)];
assert(indlist[7] != -1);
}
if (edgeTable[iCubeIndex] & 256)
{
indlist[8] = vertexIndicesZ0[getIndex(x,y)];
assert(indlist[8] != -1);
}
if (edgeTable[iCubeIndex] & 512)
{
indlist[9] = vertexIndicesZ0[getIndex(x+1,y)];
assert(indlist[9] != -1);
}
if (edgeTable[iCubeIndex] & 1024)
{
indlist[10] = vertexIndicesZ0[getIndex(x+1,y+1)];
assert(indlist[10] != -1);
}
if (edgeTable[iCubeIndex] & 2048)
{
indlist[11] = vertexIndicesZ0[getIndex(x,y+1)];
assert(indlist[11] != -1);
}
for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3)
{
uint32 ind0 = indlist[triTable[iCubeIndex][i ]];
uint32 ind1 = indlist[triTable[iCubeIndex][i+1]];
uint32 ind2 = indlist[triTable[iCubeIndex][i+2]];
singleMaterialPatch->m_vecTriangleIndices.push_back(ind0);
singleMaterialPatch->m_vecTriangleIndices.push_back(ind1);
singleMaterialPatch->m_vecTriangleIndices.push_back(ind2);
}//For each triangle
}while(volIter.moveForwardInRegionXYZ());//For each cell
}
void generateReferenceMeshDataForRegion(BlockVolume<uint8>* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch)
{
static int32 vertexIndicesX[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1];
static int32 vertexIndicesY[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1];
static int32 vertexIndicesZ[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1];
memset(vertexIndicesX,0xFF,sizeof(vertexIndicesX)); //0xFF is -1 as two's complement - this may not be portable...
memset(vertexIndicesY,0xFF,sizeof(vertexIndicesY));
memset(vertexIndicesZ,0xFF,sizeof(vertexIndicesZ));
//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 vertMaterials[12];
BlockVolumeIterator<uint8> 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 x = volIter.getPosX();
const uint16 y = volIter.getPosY();
const uint16 z = volIter.getPosZ();
//Voxels values
const uint8 v000 = volIter.getVoxel();
const uint8 v100 = volIter.peekVoxel1px0py0pz();
const uint8 v010 = volIter.peekVoxel0px1py0pz();
const uint8 v110 = volIter.peekVoxel1px1py0pz();
const uint8 v001 = volIter.peekVoxel0px0py1pz();
const uint8 v101 = volIter.peekVoxel1px0py1pz();
const uint8 v011 = volIter.peekVoxel0px1py1pz();
const uint8 v111 = volIter.peekVoxel1px1py1pz();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8 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 material0 = vertMaterials[triTable[iCubeIndex][i ]];
const uint8 material1 = vertMaterials[triTable[iCubeIndex][i+1]];
const uint8 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);
//singleMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1);
int32 index = getIndexFor(v0.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ);
if(index == -1)
{
singleMaterialPatch->m_vecVertices.push_back(v0);
singleMaterialPatch->m_vecTriangleIndices.push_back(singleMaterialPatch->m_vecVertices.size()-1);
setIndexFor(v0.getPosition(), singleMaterialPatch->m_vecVertices.size()-1, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
}
else
{
singleMaterialPatch->m_vecTriangleIndices.push_back(index);
}
index = getIndexFor(v1.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ);
if(index == -1)
{
singleMaterialPatch->m_vecVertices.push_back(v1);
singleMaterialPatch->m_vecTriangleIndices.push_back(singleMaterialPatch->m_vecVertices.size()-1);
setIndexFor(v1.getPosition(), singleMaterialPatch->m_vecVertices.size()-1, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
}
else
{
singleMaterialPatch->m_vecTriangleIndices.push_back(index);
}
index = getIndexFor(v2.getPosition(), vertexIndicesX, vertexIndicesY, vertexIndicesZ);
if(index == -1)
{
singleMaterialPatch->m_vecVertices.push_back(v2);
singleMaterialPatch->m_vecTriangleIndices.push_back(singleMaterialPatch->m_vecVertices.size()-1);
setIndexFor(v2.getPosition(), singleMaterialPatch->m_vecVertices.size()-1, vertexIndicesX, vertexIndicesY, vertexIndicesZ);
}
else
{
singleMaterialPatch->m_vecTriangleIndices.push_back(index);
}
}//For each triangle
}//For each cell
}
int32 getIndexFor(const Vector3DFloat& pos, int32 vertexIndicesX[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1], int32 vertexIndicesY[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1], int32 vertexIndicesZ[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1])
{
assert(pos.getX() >= 0.0f);
assert(pos.getY() >= 0.0f);
assert(pos.getZ() >= 0.0f);
assert(pos.getX() <= POLYVOX_REGION_SIDE_LENGTH);
assert(pos.getY() <= POLYVOX_REGION_SIDE_LENGTH);
assert(pos.getZ() <= POLYVOX_REGION_SIDE_LENGTH);
float xIntPart;
float xFracPart = std::modf(pos.getX(), &xIntPart);
float yIntPart;
float yFracPart = std::modf(pos.getY(), &yIntPart);
float zIntPart;
float zFracPart = std::modf(pos.getZ(), &zIntPart);
//Of all the fractional parts, two should be zero and one should have a value.
if(xFracPart > 0.000001f)
{
return vertexIndicesX[static_cast<uint16>(xIntPart)][static_cast<uint16>(yIntPart)][static_cast<uint16>(zIntPart)];
}
if(yFracPart > 0.000001f)
{
return vertexIndicesY[static_cast<uint16>(xIntPart)][static_cast<uint16>(yIntPart)][static_cast<uint16>(zIntPart)];
}
if(zFracPart > 0.000001f)
{
return vertexIndicesZ[static_cast<uint16>(xIntPart)][static_cast<uint16>(yIntPart)][static_cast<uint16>(zIntPart)];
}
while(true);
}
void setIndexFor(const Vector3DFloat& pos, int32 newIndex, int32 vertexIndicesX[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1], int32 vertexIndicesY[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1], int32 vertexIndicesZ[POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1][POLYVOX_REGION_SIDE_LENGTH+1])
{
assert(pos.getX() >= 0.0f);
assert(pos.getY() >= 0.0f);
assert(pos.getZ() >= 0.0f);
assert(pos.getX() <= POLYVOX_REGION_SIDE_LENGTH);
assert(pos.getY() <= POLYVOX_REGION_SIDE_LENGTH);
assert(pos.getZ() <= POLYVOX_REGION_SIDE_LENGTH);
assert(newIndex < 10000);
float xIntPart;
float xFracPart = std::modf(pos.getX(), &xIntPart);
float yIntPart;
float yFracPart = std::modf(pos.getY(), &yIntPart);
float zIntPart;
float zFracPart = std::modf(pos.getZ(), &zIntPart);
//Of all the fractional parts, two should be zero and one should have a value.
if(xFracPart > 0.000001f)
{
vertexIndicesX[static_cast<uint16>(xIntPart)][static_cast<uint16>(yIntPart)][static_cast<uint16>(zIntPart)] = newIndex;
}
if(yFracPart > 0.000001f)
{
vertexIndicesY[static_cast<uint16>(xIntPart)][static_cast<uint16>(yIntPart)][static_cast<uint16>(zIntPart)] = newIndex;
}
if(zFracPart > 0.000001f)
{
vertexIndicesZ[static_cast<uint16>(xIntPart)][static_cast<uint16>(yIntPart)][static_cast<uint16>(zIntPart)] = newIndex;
}
}
}

View File

@ -0,0 +1,892 @@
#include "PolyVoxCore/SurfaceExtractorsDecimated.h"
#include "PolyVoxCore/BlockVolume.h"
#include "PolyVoxCore/GradientEstimators.h"
#include "PolyVoxCore/IndexedSurfacePatch.h"
#include "PolyVoxCore/MarchingCubesTables.h"
#include "PolyVoxCore/Region.h"
#include "PolyVoxCore/RegionGeometry.h"
#include "PolyVoxCore/BlockVolumeIterator.h"
#include <algorithm>
using namespace std;
namespace PolyVox
{
uint32 getDecimatedIndex(uint32 x, uint32 y)
{
return x + (y * (POLYVOX_REGION_SIDE_LENGTH+1));
}
void generateDecimatedMeshDataForRegion(BlockVolume<uint8>* volumeData, uint8 uLevel, Region region, IndexedSurfacePatch* singleMaterialPatch)
{
singleMaterialPatch->m_vecVertices.clear();
singleMaterialPatch->m_vecTriangleIndices.clear();
//For edge indices
int32* vertexIndicesX0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesY0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesZ0 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesX1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesY1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
int32* vertexIndicesZ1 = new int32[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
//Cell bitmasks
uint8* bitmask0 = new uint8[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
uint8* bitmask1 = new uint8[(POLYVOX_REGION_SIDE_LENGTH+1) * (POLYVOX_REGION_SIDE_LENGTH+1)];
const uint8 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
BlockVolumeIterator<uint8> volIter(*volumeData);
//Compute bitmask for initial slice
uint32 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 uSlice = 1; ((uSlice <= POLYVOX_REGION_SIDE_LENGTH) && (uSlice + offset.getZ() <= regVolume.getUpperCorner().getZ())); uSlice += uStepSize)
{
Region regSlice1(regSlice0);
regSlice1.shift(Vector3DInt32(0,0,uStepSize));
uint32 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 computeInitialDecimatedBitmaskForSlice(BlockVolumeIterator<uint8>& volIter, uint8 uLevel, const Region& regSlice, const Vector3DFloat& offset, uint8* bitmask)
{
const uint8 uStepSize = uLevel == 0 ? 1 : 1 << uLevel;
uint32 uNoOfNonEmptyCells = 0;
//Iterate over each cell in the region
for(uint16 y = regSlice.getLowerCorner().getY(); y <= regSlice.getUpperCorner().getY(); y += uStepSize)
{
for(uint16 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 iCubeIndex = 0;
if((x==regSlice.getLowerCorner().getX()) && (y==regSlice.getLowerCorner().getY()))
{
volIter.setPosition(x,y,regSlice.getLowerCorner().getZ());
const uint8 v000 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ());
const uint8 v100 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8 v010 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8 v110 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v001 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v101 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v011 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 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 v100 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8 v110 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v101 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v111 = volIter.getSubSampledVoxel(uLevel);
//x
uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY())];
uint8 srcBit6 = iPreviousCubeIndexX & 64;
uint8 destBit7 = srcBit6 << 1;
uint8 srcBit5 = iPreviousCubeIndexX & 32;
uint8 destBit4 = srcBit5 >> 1;
uint8 srcBit2 = iPreviousCubeIndexX & 4;
uint8 destBit3 = srcBit2 << 1;
uint8 srcBit1 = iPreviousCubeIndexX & 2;
uint8 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 v010 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ());
const uint8 v110 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v011 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v111 = volIter.getSubSampledVoxel(uLevel);
//y
uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize)];
uint8 srcBit7 = iPreviousCubeIndexY & 128;
uint8 destBit4 = srcBit7 >> 3;
uint8 srcBit6 = iPreviousCubeIndexY & 64;
uint8 destBit5 = srcBit6 >> 1;
uint8 srcBit3 = iPreviousCubeIndexY & 8;
uint8 destBit0 = srcBit3 >> 3;
uint8 srcBit2 = iPreviousCubeIndexY & 4;
uint8 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 v110 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v111 = volIter.getSubSampledVoxel(uLevel);
//y
uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize)];
uint8 srcBit7 = iPreviousCubeIndexY & 128;
uint8 destBit4 = srcBit7 >> 3;
uint8 srcBit6 = iPreviousCubeIndexY & 64;
uint8 destBit5 = srcBit6 >> 1;
uint8 srcBit3 = iPreviousCubeIndexY & 8;
uint8 destBit0 = srcBit3 >> 3;
uint8 srcBit2 = iPreviousCubeIndexY & 4;
uint8 destBit1 = srcBit2 >> 1;
//x
uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY())];
srcBit6 = iPreviousCubeIndexX & 64;
uint8 destBit7 = srcBit6 << 1;
srcBit2 = iPreviousCubeIndexX & 4;
uint8 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())] = iCubeIndex;
if(edgeTable[iCubeIndex] != 0)
{
++uNoOfNonEmptyCells;
}
}
}
return uNoOfNonEmptyCells;
}
uint32 computeDecimatedBitmaskForSliceFromPrevious(BlockVolumeIterator<uint8>& volIter, uint8 uLevel, const Region& regSlice, const Vector3DFloat& offset, uint8* bitmask, uint8* previousBitmask)
{
const uint8 uStepSize = uLevel == 0 ? 1 : 1 << uLevel;
uint32 uNoOfNonEmptyCells = 0;
//Iterate over each cell in the region
for(uint16 y = regSlice.getLowerCorner().getY(); y <= regSlice.getUpperCorner().getY(); y += uStepSize)
{
for(uint16 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 iCubeIndex = 0;
if((x==regSlice.getLowerCorner().getX()) && (y==regSlice.getLowerCorner().getY()))
{
volIter.setPosition(x,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v001 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v101 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v011 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v111 = volIter.getSubSampledVoxel(uLevel);
//z
uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())];
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 v101 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v111 = volIter.getSubSampledVoxel(uLevel);
//z
uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//x
uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY())];
uint8 srcBit6 = iPreviousCubeIndexX & 64;
uint8 destBit7 = srcBit6 << 1;
uint8 srcBit5 = iPreviousCubeIndexX & 32;
uint8 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 v011 = volIter.getSubSampledVoxel(uLevel);
volIter.setPosition(x+uStepSize,y+uStepSize,regSlice.getLowerCorner().getZ()+uStepSize);
const uint8 v111 = volIter.getSubSampledVoxel(uLevel);
//z
uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//y
uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize)];
uint8 srcBit7 = iPreviousCubeIndexY & 128;
uint8 destBit4 = srcBit7 >> 3;
uint8 srcBit6 = iPreviousCubeIndexY & 64;
uint8 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 v111 = volIter.getSubSampledVoxel(uLevel);
//z
uint8 iPreviousCubeIndexZ = previousBitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY())];
iCubeIndex = iPreviousCubeIndexZ >> 4;
//y
uint8 iPreviousCubeIndexY = bitmask[getDecimatedIndex(x- offset.getX(),y- offset.getY()-uStepSize)];
uint8 srcBit7 = iPreviousCubeIndexY & 128;
uint8 destBit4 = srcBit7 >> 3;
uint8 srcBit6 = iPreviousCubeIndexY & 64;
uint8 destBit5 = srcBit6 >> 1;
//x
uint8 iPreviousCubeIndexX = bitmask[getDecimatedIndex(x- offset.getX()-uStepSize,y- offset.getY())];
srcBit6 = iPreviousCubeIndexX & 64;
uint8 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())] = iCubeIndex;
if(edgeTable[iCubeIndex] != 0)
{
++uNoOfNonEmptyCells;
}
}//For each cell
}
return uNoOfNonEmptyCells;
}
void generateDecimatedVerticesForSlice(BlockVolumeIterator<uint8>& volIter, uint8 uLevel, Region& regSlice, const Vector3DFloat& offset, uint8* bitmask, IndexedSurfacePatch* singleMaterialPatch,int32 vertexIndicesX[],int32 vertexIndicesY[],int32 vertexIndicesZ[])
{
const uint8 uStepSize = uLevel == 0 ? 1 : 1 << uLevel;
//Iterate over each cell in the region
for(uint16 y = regSlice.getLowerCorner().getY(); y <= regSlice.getUpperCorner().getY(); y += uStepSize)
{
for(uint16 x = regSlice.getLowerCorner().getX(); x <= regSlice.getUpperCorner().getX(); x += uStepSize)
{
//Current position
const uint16 z = regSlice.getLowerCorner().getZ();
volIter.setPosition(x,y,z);
const uint8 v000 = volIter.getSubSampledVoxel(uLevel);
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8 iCubeIndex = bitmask[getDecimatedIndex(x - offset.getX(),y - offset.getY())];
/* 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 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 uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max.
SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
singleMaterialPatch->m_vecVertices.push_back(surfaceVertex);
vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1;
}
}
if (edgeTable[iCubeIndex] & 8)
{
if(y != regSlice.getUpperCorner().getY())
{
volIter.setPosition(x,y + uStepSize,z);
const uint8 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 uMaterial = v000 | v010; //Because one of these is 0, the or operation takes the max.
SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
singleMaterialPatch->m_vecVertices.push_back(surfaceVertex);
vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1;
}
}
if (edgeTable[iCubeIndex] & 256)
{
//if(z != regSlice.getUpperCorner.getZ())
{
volIter.setPosition(x,y,z + uStepSize);
const uint8 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 uMaterial = v000 | v001; //Because one of these is 0, the or operation takes the max.
const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial);
singleMaterialPatch->m_vecVertices.push_back(surfaceVertex);
vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1;
}
}
}//For each cell
}
}
void generateDecimatedIndicesForSlice(BlockVolumeIterator<uint8>& volIter, uint8 uLevel, const Region& regSlice, IndexedSurfacePatch* singleMaterialPatch, const Vector3DFloat& offset, uint8* bitmask0, uint8* bitmask1, int32 vertexIndicesX0[],int32 vertexIndicesY0[],int32 vertexIndicesZ0[], int32 vertexIndicesX1[],int32 vertexIndicesY1[],int32 vertexIndicesZ1[])
{
const uint8 uStepSize = uLevel == 0 ? 1 : 1 << uLevel;
uint32 indlist[12];
for(uint16 y = regSlice.getLowerCorner().getY() - offset.getY(); y < regSlice.getUpperCorner().getY() - offset.getY(); y += uStepSize)
{
for(uint16 x = regSlice.getLowerCorner().getX() - offset.getX(); x < regSlice.getUpperCorner().getX() - offset.getX(); x += uStepSize)
{
//Current position
const uint16 z = regSlice.getLowerCorner().getZ() - offset.getZ();
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8 iCubeIndex = bitmask0[getDecimatedIndex(x,y)];
/* 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)];
assert(indlist[0] != -1);
}
if (edgeTable[iCubeIndex] & 2)
{
indlist[1] = vertexIndicesY0[getDecimatedIndex(x+uStepSize,y)];
assert(indlist[1] != -1);
}
if (edgeTable[iCubeIndex] & 4)
{
indlist[2] = vertexIndicesX0[getDecimatedIndex(x,y+uStepSize)];
assert(indlist[2] != -1);
}
if (edgeTable[iCubeIndex] & 8)
{
indlist[3] = vertexIndicesY0[getDecimatedIndex(x,y)];
assert(indlist[3] != -1);
}
if (edgeTable[iCubeIndex] & 16)
{
indlist[4] = vertexIndicesX1[getDecimatedIndex(x,y)];
assert(indlist[4] != -1);
}
if (edgeTable[iCubeIndex] & 32)
{
indlist[5] = vertexIndicesY1[getDecimatedIndex(x+uStepSize,y)];
assert(indlist[5] != -1);
}
if (edgeTable[iCubeIndex] & 64)
{
indlist[6] = vertexIndicesX1[getDecimatedIndex(x,y+uStepSize)];
assert(indlist[6] != -1);
}
if (edgeTable[iCubeIndex] & 128)
{
indlist[7] = vertexIndicesY1[getDecimatedIndex(x,y)];
assert(indlist[7] != -1);
}
if (edgeTable[iCubeIndex] & 256)
{
indlist[8] = vertexIndicesZ0[getDecimatedIndex(x,y)];
assert(indlist[8] != -1);
}
if (edgeTable[iCubeIndex] & 512)
{
indlist[9] = vertexIndicesZ0[getDecimatedIndex(x+uStepSize,y)];
assert(indlist[9] != -1);
}
if (edgeTable[iCubeIndex] & 1024)
{
indlist[10] = vertexIndicesZ0[getDecimatedIndex(x+uStepSize,y+uStepSize)];
assert(indlist[10] != -1);
}
if (edgeTable[iCubeIndex] & 2048)
{
indlist[11] = vertexIndicesZ0[getDecimatedIndex(x,y+uStepSize)];
assert(indlist[11] != -1);
}
for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3)
{
uint32 ind0 = indlist[triTable[iCubeIndex][i ]];
uint32 ind1 = indlist[triTable[iCubeIndex][i+1]];
uint32 ind2 = indlist[triTable[iCubeIndex][i+2]];
singleMaterialPatch->m_vecTriangleIndices.push_back(ind0);
singleMaterialPatch->m_vecTriangleIndices.push_back(ind1);
singleMaterialPatch->m_vecTriangleIndices.push_back(ind2);
}//For each triangle
}//For each cell
}
}
void generateDecimatedMeshDataForRegionSlow(BlockVolume<uint8>* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch)
{
//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 vertMaterials[12];
BlockVolumeIterator<uint8> 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());
for(uint16 z = region.getLowerCorner().getZ(); z <= region.getUpperCorner().getZ(); z += 2)
{
for(uint16 y = region.getLowerCorner().getY(); y <= region.getUpperCorner().getY(); y += 2)
{
for(uint16 x = region.getLowerCorner().getX(); x <= region.getUpperCorner().getX(); x += 2)
{
//while(volIter.moveForwardInRegionXYZ())
//{
volIter.setPosition(x,y,z);
const uint8 v000 = volIter.getSubSampledVoxel(1);
volIter.setPosition(x+2,y,z);
const uint8 v100 = volIter.getSubSampledVoxel(1);
volIter.setPosition(x,y+2,z);
const uint8 v010 = volIter.getSubSampledVoxel(1);
volIter.setPosition(x+2,y+2,z);
const uint8 v110 = volIter.getSubSampledVoxel(1);
volIter.setPosition(x,y,z+2);
const uint8 v001 = volIter.getSubSampledVoxel(1);
volIter.setPosition(x+2,y,z+2);
const uint8 v101 = volIter.getSubSampledVoxel(1);
volIter.setPosition(x,y+2,z+2);
const uint8 v011 = volIter.getSubSampledVoxel(1);
volIter.setPosition(x+2,y+2,z+2);
const uint8 v111 = volIter.getSubSampledVoxel(1);
//Determine the index into the edge table which tells us which vertices are inside of the surface
uint8 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 * 2.0f);
vertlist[0].setY(y);
vertlist[0].setZ(z);
normlist[0] = Vector3DFloat(v000 - v100,0.0,0.0);
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 * 2.0f);
vertlist[1].setY(y + 0.5f * 2.0f);
vertlist[1].setZ(z);
vertMaterials[1] = v100 | v110;
normlist[1] = Vector3DFloat(0.0,v100 - v110,0.0);
}
if (edgeTable[iCubeIndex] & 4)
{
vertlist[2].setX(x + 0.5f * 2.0f);
vertlist[2].setY(y + 1.0f * 2.0f);
vertlist[2].setZ(z);
vertMaterials[2] = v010 | v110;
normlist[2] = Vector3DFloat(v010 - v110,0.0,0.0);
}
if (edgeTable[iCubeIndex] & 8)
{
vertlist[3].setX(x);
vertlist[3].setY(y + 0.5f * 2.0f);
vertlist[3].setZ(z);
vertMaterials[3] = v000 | v010;
normlist[3] = Vector3DFloat(0.0,v000 - v010,0.0);
}
if (edgeTable[iCubeIndex] & 16)
{
vertlist[4].setX(x + 0.5f * 2.0f);
vertlist[4].setY(y);
vertlist[4].setZ(z + 1.0f * 2.0f);
vertMaterials[4] = v001 | v101;
normlist[4] = Vector3DFloat(v001 - v101,0.0,0.0);
}
if (edgeTable[iCubeIndex] & 32)
{
vertlist[5].setX(x + 1.0f * 2.0f);
vertlist[5].setY(y + 0.5f * 2.0f);
vertlist[5].setZ(z + 1.0f * 2.0f);
vertMaterials[5] = v101 | v111;
normlist[5] = Vector3DFloat(0.0,v101 - v111,0.0);
}
if (edgeTable[iCubeIndex] & 64)
{
vertlist[6].setX(x + 0.5f * 2.0f);
vertlist[6].setY(y + 1.0f * 2.0f);
vertlist[6].setZ(z + 1.0f * 2.0f);
vertMaterials[6] = v011 | v111;
normlist[6] = Vector3DFloat(v011 - v111,0.0,0.0);
}
if (edgeTable[iCubeIndex] & 128)
{
vertlist[7].setX(x);
vertlist[7].setY(y + 0.5f * 2.0f);
vertlist[7].setZ(z + 1.0f * 2.0f);
vertMaterials[7] = v001 | v011;
normlist[7] = Vector3DFloat(0.0,v001 - v011,0.0);
}
if (edgeTable[iCubeIndex] & 256)
{
vertlist[8].setX(x);
vertlist[8].setY(y);
vertlist[8].setZ(z + 0.5f * 2.0f);
vertMaterials[8] = v000 | v001;
normlist[8] = Vector3DFloat(0.0,0.0,v000 - v001);
}
if (edgeTable[iCubeIndex] & 512)
{
vertlist[9].setX(x + 1.0f * 2.0f);
vertlist[9].setY(y);
vertlist[9].setZ(z + 0.5f * 2.0f);
vertMaterials[9] = v100 | v101;
normlist[9] = Vector3DFloat(0.0,0.0,v100 - v101);
}
if (edgeTable[iCubeIndex] & 1024)
{
vertlist[10].setX(x + 1.0f * 2.0f);
vertlist[10].setY(y + 1.0f * 2.0f);
vertlist[10].setZ(z + 0.5f * 2.0f);
vertMaterials[10] = v110 | v111;
normlist[10] = Vector3DFloat(0.0,0.0,v110 - v111);
}
if (edgeTable[iCubeIndex] & 2048)
{
vertlist[11].setX(x);
vertlist[11].setY(y + 1.0f * 2.0f);
vertlist[11].setZ(z + 0.5f * 2.0f);
vertMaterials[11] = v010 | v011;
normlist[11] = Vector3DFloat(0.0,0.0,v010 - v011);
}
for (int i=0;triTable[iCubeIndex][i]!=-1;i+=3)
{
//The three vertices forming a triangle
Vector3DFloat vertex0 = vertlist[triTable[iCubeIndex][i ]] - offset;
Vector3DFloat vertex1 = vertlist[triTable[iCubeIndex][i+1]] - offset;
Vector3DFloat vertex2 = vertlist[triTable[iCubeIndex][i+2]] - offset;
Vector3DFloat normal0 = normlist[triTable[iCubeIndex][i ]];
Vector3DFloat normal1 = normlist[triTable[iCubeIndex][i+1]];
Vector3DFloat normal2 = normlist[triTable[iCubeIndex][i+2]];
normal0.normalise();
normal1.normalise();
normal2.normalise();
vertex0 += (normal0);
vertex1 += (normal1);
vertex2 += (normal2);
//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 material0 = vertMaterials[triTable[iCubeIndex][i ]];
const uint8 material1 = vertMaterials[triTable[iCubeIndex][i+1]];
const uint8 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 surfaceVertex0Alpha1(vertex0,material0 + 0.1f);
surfaceVertex0Alpha1.setNormal(normal0);
SurfaceVertex surfaceVertex1Alpha1(vertex1,material1 + 0.1f);
surfaceVertex1Alpha1.setNormal(normal1);
SurfaceVertex surfaceVertex2Alpha1(vertex2,material2 + 0.1f);
surfaceVertex2Alpha1.setNormal(normal2);
singleMaterialPatch->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1);
}//For each triangle
//}//For each cell
}
}
}
//FIXME - can it happen that we have no vertices or triangles? Should exit early?
//for(std::map<uint8, IndexedSurfacePatch*>::iterator iterPatch = surfacePatchMapResult.begin(); iterPatch != surfacePatchMapResult.end(); ++iterPatch)
{
/*std::vector<SurfaceVertex>::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin();
while(iterSurfaceVertex != singleMaterialPatch->getVertices().end())
{
Vector3DFloat tempNormal = computeNormal(volumeData, static_cast<Vector3DFloat>(iterSurfaceVertex->getPosition() + offset), SIMPLE);
const_cast<SurfaceVertex&>(*iterSurfaceVertex).setNormal(tempNormal);
++iterSurfaceVertex;
}*/
}
}
Vector3DFloat computeDecimatedNormal(BlockVolume<uint8>* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod)
{
const float posX = position.getX();
const float posY = position.getY();
const float posZ = position.getZ();
const uint16 floorX = static_cast<uint16>(posX);
const uint16 floorY = static_cast<uint16>(posY);
const uint16 floorZ = static_cast<uint16>(posZ);
//Check all corners are within the volume, allowing a boundary for gradient estimation
bool lowerCornerInside = volumeData->containsPoint(Vector3DInt32(floorX, floorY, floorZ),1);
bool upperCornerInside = volumeData->containsPoint(Vector3DInt32(floorX+1, floorY+1, floorZ+1),1);
if((!lowerCornerInside) || (!upperCornerInside))
{
normalGenerationMethod = SIMPLE;
}
Vector3DFloat result;
BlockVolumeIterator<uint8> volIter(*volumeData); //FIXME - save this somewhere - could be expensive to create?
if(normalGenerationMethod == SOBEL)
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ));
const Vector3DFloat gradFloor = computeSobelGradient(volIter);
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX+1.0),static_cast<uint16>(posY),static_cast<uint16>(posZ));
}
if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY+1.0),static_cast<uint16>(posZ));
}
if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ+1.0));
}
const Vector3DFloat gradCeil = computeSobelGradient(volIter);
result = ((gradFloor + gradCeil) * -1.0f);
if(result.lengthSquared() < 0.0001)
{
//Operation failed - fall back on simple gradient estimation
normalGenerationMethod = SIMPLE;
}
}
if(normalGenerationMethod == CENTRAL_DIFFERENCE)
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ));
const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter);
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX+1.0),static_cast<uint16>(posY),static_cast<uint16>(posZ));
}
if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY+1.0),static_cast<uint16>(posZ));
}
if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ+1.0));
}
const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter);
result = ((gradFloor + gradCeil) * -1.0f);
if(result.lengthSquared() < 0.0001)
{
//Operation failed - fall back on simple gradient estimation
normalGenerationMethod = SIMPLE;
}
}
if(normalGenerationMethod == SIMPLE)
{
volIter.setPosition(static_cast<uint16>(posX),static_cast<uint16>(posY),static_cast<uint16>(posZ));
const uint8 uFloor = volIter.getVoxel() > 0 ? 1 : 0;
if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5
{
uint8 uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0;
result = Vector3DFloat(static_cast<float>(uFloor - uCeil),0.0,0.0);
}
else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5
{
uint8 uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0;
result = Vector3DFloat(0.0,static_cast<float>(uFloor - uCeil),0.0);
}
else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5
{
uint8 uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0;
result = Vector3DFloat(0.0, 0.0,static_cast<float>(uFloor - uCeil));
}
}
return result;
}
}

View File

@ -0,0 +1,59 @@
#include "SurfaceTriangle.h"
#include "SurfaceVertex.h"
#include "SurfaceEdge.h"
namespace PolyVox
{
SurfaceTriangle::SurfaceTriangle()
{
}
const SurfaceEdgeIterator& SurfaceTriangle::getEdge(void) const
{
return edge;
}
void SurfaceTriangle::setEdge(const SurfaceEdgeIterator& edgeToSet)
{
edge = edgeToSet;
}
/*std::string SurfaceTriangle::tostring(void)
{
std::stringstream ss;
uint16_t ct = 0;
SurfaceEdgeIterator edgeIter = edge;
ss << "SurfaceTriangle:";
do
{
ss << "\n Edge " << ct << " = " << edgeIter->tostring();
if(edgeIter->hasOtherHalfEdge)
{
ss << "\n Opp Edge " << ct << " = " << edgeIter->otherHalfEdge->tostring();
}
else
{
ss << "\n No Other Half";
}
edgeIter = edgeIter->nextHalfEdge;
++ct;
}
while(edgeIter != edge);
return ss.str();
}*/
bool operator == (const SurfaceTriangle& lhs, const SurfaceTriangle& rhs)
{
//Edges are unique in the set, so if the two positions are the same the
//two iterators must also be the same. So we just check the iterators.
return (lhs.edge == rhs.edge);
}
bool operator < (const SurfaceTriangle& lhs, const SurfaceTriangle& rhs)
{
//Unlike the equality operator, we can't compare iterators.
//So dereference and compare the results.
return (*lhs.edge < *rhs.edge);
}
}

View File

@ -0,0 +1,83 @@
#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 <sstream>
#include "PolyVoxCore/Constants.h"
#include "PolyVoxCore/SurfaceVertex.h"
namespace PolyVox
{
SurfaceVertex::SurfaceVertex()
{
}
SurfaceVertex::SurfaceVertex(Vector3DFloat positionToSet, float materialToSet)
:position(positionToSet)
,material(materialToSet)
{
}
SurfaceVertex::SurfaceVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet, float materialToSet)
:position(positionToSet)
,normal(normalToSet)
,material(materialToSet)
{
}
float SurfaceVertex::getMaterial(void) const
{
return material;
}
const Vector3DFloat& SurfaceVertex::getNormal(void) const
{
return normal;
}
const Vector3DFloat& SurfaceVertex::getPosition(void) const
{
return position;
}
void SurfaceVertex::setMaterial(float materialToSet)
{
material = materialToSet;
}
void SurfaceVertex::setNormal(const Vector3DFloat& normalToSet)
{
normal = normalToSet;
}
void SurfaceVertex::setPosition(const Vector3DFloat& positionToSet)
{
position = positionToSet;
}
std::string SurfaceVertex::tostring(void) const
{
std::stringstream ss;
ss << "SurfaceVertex: Position = (" << position.getX() << "," << position.getY() << "," << position.getZ() << "), Normal = " << normal;
return ss.str();
}
}

View File

@ -0,0 +1,51 @@
#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 "PolyVoxCore/Utility.h"
#include <cassert>
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 logBase2(uint32 uInput)
{
assert(uInput != 0);
assert(isPowerOf2(uInput));
uint32 uResult = 0;
while( (uInput >> uResult) != 0)
{
++uResult;
}
return static_cast<uint8>(uResult-1);
}
bool isPowerOf2(uint32 uInput)
{
if(uInput == 0)
return false;
else
return ((uInput & (uInput-1)) == 0);
}
}

View File

@ -0,0 +1,51 @@
#include "PolyVoxCore/VoxelFilters.h"
#include "PolyVoxCore/BlockVolumeIterator.h"
namespace PolyVox
{
float computeSmoothedVoxel(BlockVolumeIterator<uint8>& volIter)
{
assert(volIter.getPosX() >= 1);
assert(volIter.getPosY() >= 1);
assert(volIter.getPosZ() >= 1);
assert(volIter.getPosX() < volIter.getVolume().getSideLength() - 2);
assert(volIter.getPosY() < volIter.getVolume().getSideLength() - 2);
assert(volIter.getPosZ() < volIter.getVolume().getSideLength() - 2);
float sum = 0.0;
if(volIter.peekVoxel1nx1ny1nz() != 0) sum += 1.0f;
if(volIter.peekVoxel1nx1ny0pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1nx1ny1pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1nx0py1nz() != 0) sum += 1.0f;
if(volIter.peekVoxel1nx0py0pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1nx0py1pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1nx1py1nz() != 0) sum += 1.0f;
if(volIter.peekVoxel1nx1py0pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1nx1py1pz() != 0) sum += 1.0f;
if(volIter.peekVoxel0px1ny1nz() != 0) sum += 1.0f;
if(volIter.peekVoxel0px1ny0pz() != 0) sum += 1.0f;
if(volIter.peekVoxel0px1ny1pz() != 0) sum += 1.0f;
if(volIter.peekVoxel0px0py1nz() != 0) sum += 1.0f;
if(volIter.getVoxel() != 0) sum += 1.0f;
if(volIter.peekVoxel0px0py1pz() != 0) sum += 1.0f;
if(volIter.peekVoxel0px1py1nz() != 0) sum += 1.0f;
if(volIter.peekVoxel0px1py0pz() != 0) sum += 1.0f;
if(volIter.peekVoxel0px1py1pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px1ny1nz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px1ny0pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px1ny1pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px0py1nz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px0py0pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px0py1pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px1py1nz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px1py0pz() != 0) sum += 1.0f;
if(volIter.peekVoxel1px1py1pz() != 0) sum += 1.0f;
sum /= 27.0f;
return sum;
}
}

View File

@ -0,0 +1,231 @@
#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 "PolyVoxUtil/VolumeChangeTracker.h"
#include "PolyVoxCore/GradientEstimators.h"
#include "PolyVoxCore/IndexedSurfacePatch.h"
#include "PolyVoxCore/LinearVolume.h"
#include "PolyVoxCore/MarchingCubesTables.h"
#include "PolyVoxCore/RegionGeometry.h"
#include "PolyVoxCore/SurfaceExtractors.h"
#include "PolyVoxCore/SurfaceVertex.h"
#include "PolyVoxCore/Utility.h"
#include "PolyVoxCore/Vector.h"
#include "PolyVoxCore/BlockVolume.h"
#include "PolyVoxCore/BlockVolumeIterator.h"
using namespace std;
namespace PolyVox
{
//////////////////////////////////////////////////////////////////////////
// VolumeChangeTracker
//////////////////////////////////////////////////////////////////////////
VolumeChangeTracker::VolumeChangeTracker()
:m_bIsLocked(false)
,volumeData(0)
{
}
VolumeChangeTracker::~VolumeChangeTracker()
{
}
void VolumeChangeTracker::setVolumeData(BlockVolume<uint8>* volumeDataToSet)
{
volumeData = volumeDataToSet;
volRegionUpToDate = new LinearVolume<bool>(PolyVox::logBase2(POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS));
}
void VolumeChangeTracker::getChangedRegions(std::list<Region>& listToFill) const
{
//Clear the list
listToFill.clear();
//Regenerate meshes.
for(uint16 regionZ = 0; regionZ < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++regionZ)
//for(uint16 regionZ = 0; regionZ < 1; ++regionZ)
{
for(uint16 regionY = 0; regionY < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++regionY)
//for(uint16 regionY = 0; regionY < 2; ++regionY)
{
for(uint16 regionX = 0; regionX < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++regionX)
//for(uint16 regionX = 0; regionX < 2; ++regionX)
{
if(volRegionUpToDate->getVoxelAt(regionX, regionY, regionZ) == false)
{
const uint16 firstX = regionX * POLYVOX_REGION_SIDE_LENGTH;
const uint16 firstY = regionY * POLYVOX_REGION_SIDE_LENGTH;
const uint16 firstZ = regionZ * POLYVOX_REGION_SIDE_LENGTH;
const uint16 lastX = firstX + POLYVOX_REGION_SIDE_LENGTH;
const uint16 lastY = firstY + POLYVOX_REGION_SIDE_LENGTH;
const uint16 lastZ = firstZ + POLYVOX_REGION_SIDE_LENGTH;
listToFill.push_back(Region(Vector3DInt32(firstX, firstY, firstZ), Vector3DInt32(lastX, lastY, lastZ)));
}
}
}
}
}
void VolumeChangeTracker::setAllRegionsUpToDate(bool newUpToDateValue)
{
for(uint16 blockZ = 0; blockZ < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++blockZ)
{
for(uint16 blockY = 0; blockY < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++blockY)
{
for(uint16 blockX = 0; blockX < POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS; ++blockX)
{
volRegionUpToDate->setVoxelAt(blockX, blockY, blockZ, newUpToDateValue);
}
}
}
}
uint16 VolumeChangeTracker::getSideLength(void)
{
return volumeData->getSideLength();
}
Region VolumeChangeTracker::getEnclosingRegion(void) const
{
return volumeData->getEnclosingRegion();
}
uint8 VolumeChangeTracker::getVoxelAt(const Vector3DUint16& pos)
{
return getVoxelAt(pos.getX(), pos.getY(), pos.getZ());
}
uint8 VolumeChangeTracker::getVoxelAt(uint16 uX, uint16 uY, uint16 uZ)
{
assert(uX < volumeData->getSideLength());
assert(uY < volumeData->getSideLength());
assert(uZ < volumeData->getSideLength());
BlockVolumeIterator<uint8> volIter(*volumeData);
volIter.setPosition(uX,uY,uZ);
return volIter.getVoxel();
}
BlockVolume<uint8>* VolumeChangeTracker::getVolumeData(void) const
{
return volumeData;
}
void VolumeChangeTracker::setVoxelAt(uint16 x, uint16 y, uint16 z, uint8 value)
{
//FIXME - rather than creating a iterator each time we should have one stored
BlockVolumeIterator<uint8> iterVol(*volumeData);
iterVol.setPosition(x,y,z);
iterVol.setVoxel(value);
//If we are not on a boundary, just mark one region.
if((x % POLYVOX_REGION_SIDE_LENGTH != 0) &&
(x % POLYVOX_REGION_SIDE_LENGTH != POLYVOX_REGION_SIDE_LENGTH-1) &&
(y % POLYVOX_REGION_SIDE_LENGTH != 0) &&
(y % POLYVOX_REGION_SIDE_LENGTH != POLYVOX_REGION_SIDE_LENGTH-1) &&
(z % POLYVOX_REGION_SIDE_LENGTH != 0) &&
(z % POLYVOX_REGION_SIDE_LENGTH != POLYVOX_REGION_SIDE_LENGTH-1))
{
volRegionUpToDate->setVoxelAt(x >> POLYVOX_REGION_SIDE_LENGTH_POWER, y >> POLYVOX_REGION_SIDE_LENGTH_POWER, z >> POLYVOX_REGION_SIDE_LENGTH_POWER, false);
}
else //Mark surrounding regions as well
{
const uint16 regionX = x >> POLYVOX_REGION_SIDE_LENGTH_POWER;
const uint16 regionY = y >> POLYVOX_REGION_SIDE_LENGTH_POWER;
const uint16 regionZ = z >> POLYVOX_REGION_SIDE_LENGTH_POWER;
const uint16 minRegionX = (std::max)(uint16(0),uint16(regionX-1));
const uint16 minRegionY = (std::max)(uint16(0),uint16(regionY-1));
const uint16 minRegionZ = (std::max)(uint16(0),uint16(regionZ-1));
const uint16 maxRegionX = (std::min)(uint16(POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS-1),uint16(regionX+1));
const uint16 maxRegionY = (std::min)(uint16(POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS-1),uint16(regionY+1));
const uint16 maxRegionZ = (std::min)(uint16(POLYVOX_VOLUME_SIDE_LENGTH_IN_REGIONS-1),uint16(regionZ+1));
for(uint16 zCt = minRegionZ; zCt <= maxRegionZ; zCt++)
{
for(uint16 yCt = minRegionY; yCt <= maxRegionY; yCt++)
{
for(uint16 xCt = minRegionX; xCt <= maxRegionX; xCt++)
{
volRegionUpToDate->setVoxelAt(xCt,yCt,zCt,false);
}
}
}
}
}
void VolumeChangeTracker::setLockedVoxelAt(uint16 x, uint16 y, uint16 z, uint8 value)
{
assert(m_bIsLocked);
//FIXME - rather than creating a iterator each time we should have one stored
BlockVolumeIterator<uint8> iterVol(*volumeData);
iterVol.setPosition(x,y,z);
iterVol.setVoxel(value);
}
void VolumeChangeTracker::lockRegion(const Region& regToLock)
{
if(m_bIsLocked)
{
throw std::logic_error("A region is already locked. Please unlock it before locking another.");
}
m_regLastLocked = regToLock;
m_bIsLocked = true;
}
void VolumeChangeTracker::unlockRegion(void)
{
if(!m_bIsLocked)
{
throw std::logic_error("No region is locked. You must lock a region before you can unlock it.");
}
const uint16 firstRegionX = m_regLastLocked.getLowerCorner().getX() >> POLYVOX_REGION_SIDE_LENGTH_POWER;
const uint16 firstRegionY = m_regLastLocked.getLowerCorner().getY() >> POLYVOX_REGION_SIDE_LENGTH_POWER;
const uint16 firstRegionZ = m_regLastLocked.getLowerCorner().getZ() >> POLYVOX_REGION_SIDE_LENGTH_POWER;
const uint16 lastRegionX = m_regLastLocked.getUpperCorner().getX() >> POLYVOX_REGION_SIDE_LENGTH_POWER;
const uint16 lastRegionY = m_regLastLocked.getUpperCorner().getY() >> POLYVOX_REGION_SIDE_LENGTH_POWER;
const uint16 lastRegionZ = m_regLastLocked.getUpperCorner().getZ() >> POLYVOX_REGION_SIDE_LENGTH_POWER;
for(uint16 zCt = firstRegionZ; zCt <= lastRegionZ; zCt++)
{
for(uint16 yCt = firstRegionY; yCt <= lastRegionY; yCt++)
{
for(uint16 xCt = firstRegionX; xCt <= lastRegionX; xCt++)
{
volRegionUpToDate->setVoxelAt(xCt,yCt,zCt,false);
}
}
}
m_bIsLocked = false;
}
}