Refactoring of basic voxel types.

This commit is contained in:
unknown 2012-02-02 16:34:06 +01:00
parent a796672645
commit 1ab1d9bed3
12 changed files with 118 additions and 76 deletions

View File

@ -37,11 +37,25 @@ namespace PolyVox
return false;
}
//and if their density is below the threshold.
VoxelType voxel = volData->getVoxelAt(v3dPos);
if(voxel.getDensity() >= VoxelType::getThreshold())
if(VoxelTypeTraits<VoxelType>::HasDensity)
{
return false;
//and if their density is above the threshold.
VoxelType voxel = volData->getVoxelAt(v3dPos);
VoxelType::DensityType tThreshold = (VoxelTypeTraits<VoxelType>::MinDensity + VoxelTypeTraits<VoxelType>::MaxDensity) / 2;
if(voxel.getDensity() >= tThreshold)
{
return false;
}
}
if(VoxelTypeTraits<VoxelType>::HasMaterial)
{
//and if their material is not zero.
VoxelType voxel = volData->getVoxelAt(v3dPos);
if(voxel.getMaterial() != 0)
{
return false;
}
}
return true;

View File

@ -91,10 +91,10 @@ namespace PolyVox
volumeSampler.setPosition(x,y,z);
VoxelType currentVoxel = volumeSampler.getVoxel();
bool currentVoxelIsSolid = currentVoxel.getDensity() >= VoxelType::getThreshold();
bool currentVoxelIsSolid = currentVoxel.getMaterial() != 0;
VoxelType negXVoxel = volumeSampler.peekVoxel1nx0py0pz();
bool negXVoxelIsSolid = negXVoxel.getDensity() >= VoxelType::getThreshold();
bool negXVoxelIsSolid = negXVoxel.getMaterial() != 0;
if((currentVoxelIsSolid != negXVoxelIsSolid) && (finalY == false) && (finalZ == false))
{
@ -131,7 +131,7 @@ namespace PolyVox
}
VoxelType negYVoxel = volumeSampler.peekVoxel0px1ny0pz();
bool negYVoxelIsSolid = negYVoxel.getDensity() >= VoxelType::getThreshold();
bool negYVoxelIsSolid = negYVoxel.getMaterial() != 0;
if((currentVoxelIsSolid != negYVoxelIsSolid) && (finalX == false) && (finalZ == false))
{
@ -168,7 +168,7 @@ namespace PolyVox
}
VoxelType negZVoxel = volumeSampler.peekVoxel0px0py1nz();
bool negZVoxelIsSolid = negZVoxel.getDensity() >= VoxelType::getThreshold();
bool negZVoxelIsSolid = negZVoxel.getMaterial() != 0;
if((currentVoxelIsSolid != negZVoxelIsSolid) && (finalX == false) && (finalY == false))
{

View File

@ -48,9 +48,9 @@ namespace PolyVox
float regY = static_cast<float>(y - m_regSizeInVoxels.getLowerCorner().getY());
float regZ = static_cast<float>(z - m_regSizeInVoxels.getLowerCorner().getZ());
int currentVoxel = m_volData->getVoxelAt(x,y,z).getDensity() >= VoxelType::getThreshold();
int currentVoxel = m_volData->getVoxelAt(x,y,z).getMaterial() != 0;
int plusXVoxel = m_volData->getVoxelAt(x+1,y,z).getDensity() >= VoxelType::getThreshold();
int plusXVoxel = m_volData->getVoxelAt(x+1,y,z).getMaterial() != 0;
if(currentVoxel > plusXVoxel)
{
float material = static_cast<float>(m_volData->getVoxelAt(x,y,z).getMaterial());
@ -76,7 +76,7 @@ namespace PolyVox
m_meshCurrent->addTriangleCubic(v1,v3,v2);
}
int plusYVoxel = m_volData->getVoxelAt(x,y+1,z).getDensity() >= VoxelType::getThreshold();
int plusYVoxel = m_volData->getVoxelAt(x,y+1,z).getMaterial() != 0;
if(currentVoxel > plusYVoxel)
{
float material = static_cast<float>(m_volData->getVoxelAt(x,y,z).getMaterial());
@ -102,7 +102,7 @@ namespace PolyVox
m_meshCurrent->addTriangleCubic(v1,v2,v3);
}
int plusZVoxel = m_volData->getVoxelAt(x,y,z+1).getDensity() >= VoxelType::getThreshold();
int plusZVoxel = m_volData->getVoxelAt(x,y,z+1).getMaterial() != 0;
if(currentVoxel > plusZVoxel)
{
float material = static_cast<float>(m_volData->getVoxelAt(x,y,z).getMaterial());

View File

@ -113,6 +113,8 @@ namespace PolyVox
public:
const static bool HasDensity;
const static bool HasMaterial;
const static MaterialU8::DensityType MinDensity;
const static MaterialU8::DensityType MaxDensity;
};
template<>
@ -121,6 +123,8 @@ namespace PolyVox
public:
const static bool HasDensity;
const static bool HasMaterial;
const static MaterialU16::DensityType MinDensity;
const static MaterialU16::DensityType MaxDensity;
};
template<>
@ -129,6 +133,8 @@ namespace PolyVox
public:
const static bool HasDensity;
const static bool HasMaterial;
const static MaterialU32::DensityType MinDensity;
const static MaterialU32::DensityType MaxDensity;
};
}

View File

@ -36,7 +36,7 @@ namespace PolyVox
class SurfaceExtractor
{
public:
SurfaceExtractor(VolumeType<VoxelType>* volData, Region region, SurfaceMesh<PositionMaterialNormal>* result);
SurfaceExtractor(VolumeType<VoxelType>* volData, Region region, SurfaceMesh<PositionMaterialNormal>* result, typename VoxelType::DensityType tThreshold = (VoxelTypeTraits<VoxelType>::MinDensity + VoxelTypeTraits<VoxelType>::MaxDensity) / 2);
void execute();
@ -205,6 +205,9 @@ namespace PolyVox
Region m_regVolumeCropped;*/
Region m_regSlicePrevious;
Region m_regSliceCurrent;
//Our threshold value
typename VoxelType::DensityType m_tThreshold;
};
}

View File

@ -24,11 +24,12 @@ freely, subject to the following restrictions:
namespace PolyVox
{
template< template<typename> class VolumeType, typename VoxelType>
SurfaceExtractor<VolumeType, VoxelType>::SurfaceExtractor(VolumeType<VoxelType>* volData, Region region, SurfaceMesh<PositionMaterialNormal>* result)
SurfaceExtractor<VolumeType, VoxelType>::SurfaceExtractor(VolumeType<VoxelType>* volData, Region region, SurfaceMesh<PositionMaterialNormal>* result, typename VoxelType::DensityType tThreshold)
:m_volData(volData)
,m_sampVolume(volData)
,m_meshCurrent(result)
,m_regSizeInVoxels(region)
,m_tThreshold(tThreshold)
{
//m_regSizeInVoxels.cropTo(m_volData->getEnclosingRegion());
m_regSizeInCells = m_regSizeInVoxels;
@ -229,7 +230,7 @@ namespace PolyVox
iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY | iPreviousCubeIndexZ;
if (v111.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 128;
if (v111.getDensity() < m_tThreshold) iCubeIndex |= 128;
}
else //previous X not available
{
@ -247,8 +248,8 @@ namespace PolyVox
iCubeIndex = iPreviousCubeIndexY | iPreviousCubeIndexZ;
if (v011.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 64;
if (v111.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 128;
if (v011.getDensity() < m_tThreshold) iCubeIndex |= 64;
if (v111.getDensity() < m_tThreshold) iCubeIndex |= 128;
}
}
else //previous Y not available
@ -269,8 +270,8 @@ namespace PolyVox
iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexZ;
if (v101.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 32;
if (v111.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 128;
if (v101.getDensity() < m_tThreshold) iCubeIndex |= 32;
if (v111.getDensity() < m_tThreshold) iCubeIndex |= 128;
}
else //previous X not available
{
@ -283,10 +284,10 @@ namespace PolyVox
uint8_t iPreviousCubeIndexZ = pPreviousBitmask[uXRegSpace][uYRegSpace];
iCubeIndex = iPreviousCubeIndexZ >> 4;
if (v001.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 16;
if (v101.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 32;
if (v011.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 64;
if (v111.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 128;
if (v001.getDensity() < m_tThreshold) iCubeIndex |= 16;
if (v101.getDensity() < m_tThreshold) iCubeIndex |= 32;
if (v011.getDensity() < m_tThreshold) iCubeIndex |= 64;
if (v111.getDensity() < m_tThreshold) iCubeIndex |= 128;
}
}
}
@ -311,8 +312,8 @@ namespace PolyVox
iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY;
if (v110.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 8;
if (v111.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 128;
if (v110.getDensity() < m_tThreshold) iCubeIndex |= 8;
if (v111.getDensity() < m_tThreshold) iCubeIndex |= 128;
}
else //previous X not available
{
@ -329,10 +330,10 @@ namespace PolyVox
iCubeIndex = iPreviousCubeIndexY;
if (v010.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 4;
if (v110.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 8;
if (v011.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 64;
if (v111.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 128;
if (v010.getDensity() < m_tThreshold) iCubeIndex |= 4;
if (v110.getDensity() < m_tThreshold) iCubeIndex |= 8;
if (v011.getDensity() < m_tThreshold) iCubeIndex |= 64;
if (v111.getDensity() < m_tThreshold) iCubeIndex |= 128;
}
}
else //previous Y not available
@ -352,10 +353,10 @@ namespace PolyVox
iCubeIndex = iPreviousCubeIndexX;
if (v100.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 2;
if (v110.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 8;
if (v101.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 32;
if (v111.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 128;
if (v100.getDensity() < m_tThreshold) iCubeIndex |= 2;
if (v110.getDensity() < m_tThreshold) iCubeIndex |= 8;
if (v101.getDensity() < m_tThreshold) iCubeIndex |= 32;
if (v111.getDensity() < m_tThreshold) iCubeIndex |= 128;
}
else //previous X not available
{
@ -369,14 +370,14 @@ namespace PolyVox
v011 = m_sampVolume.peekVoxel0px1py1pz();
v111 = m_sampVolume.peekVoxel1px1py1pz();
if (v000.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 1;
if (v100.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 2;
if (v010.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 4;
if (v110.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 8;
if (v001.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 16;
if (v101.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 32;
if (v011.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 64;
if (v111.getDensity() < VoxelType::getThreshold()) iCubeIndex |= 128;
if (v000.getDensity() < m_tThreshold) iCubeIndex |= 1;
if (v100.getDensity() < m_tThreshold) iCubeIndex |= 2;
if (v010.getDensity() < m_tThreshold) iCubeIndex |= 4;
if (v110.getDensity() < m_tThreshold) iCubeIndex |= 8;
if (v001.getDensity() < m_tThreshold) iCubeIndex |= 16;
if (v101.getDensity() < m_tThreshold) iCubeIndex |= 32;
if (v011.getDensity() < m_tThreshold) iCubeIndex |= 64;
if (v111.getDensity() < m_tThreshold) iCubeIndex |= 128;
}
}
}
@ -433,7 +434,7 @@ namespace PolyVox
const Vector3DFloat n100 = computeCentralDifferenceGradient(m_sampVolume);
//float fInterp = static_cast<float>(v100.getDensity() - VoxelType::getMinDensity()) / static_cast<float>(VoxelType::getMaxDensity() - VoxelType::getMinDensity());
float fInterp = static_cast<float>(VoxelType::getThreshold() - v000.getDensity()) / static_cast<float>(v100.getDensity() - v000.getDensity());
float fInterp = static_cast<float>(m_tThreshold - v000.getDensity()) / static_cast<float>(v100.getDensity() - v000.getDensity());
//fInterp = 0.5f;
const Vector3DFloat v3dPosition(static_cast<float>(iXVolSpace - m_regSizeInVoxels.getLowerCorner().getX()) + fInterp, static_cast<float>(iYVolSpace - m_regSizeInVoxels.getLowerCorner().getY()), static_cast<float>(iZVolSpace - m_regSizeInCells.getLowerCorner().getZ()));
@ -463,7 +464,7 @@ namespace PolyVox
const VoxelType v010 = m_sampVolume.getVoxel();
const Vector3DFloat n010 = computeCentralDifferenceGradient(m_sampVolume);
float fInterp = static_cast<float>(VoxelType::getThreshold() - v000.getDensity()) / static_cast<float>(v010.getDensity() - v000.getDensity());
float fInterp = static_cast<float>(m_tThreshold - v000.getDensity()) / static_cast<float>(v010.getDensity() - v000.getDensity());
//fInterp = 0.5f;
const Vector3DFloat v3dPosition(static_cast<float>(iXVolSpace - m_regSizeInVoxels.getLowerCorner().getX()), static_cast<float>(iYVolSpace - m_regSizeInVoxels.getLowerCorner().getY()) + fInterp, static_cast<float>(iZVolSpace - m_regSizeInVoxels.getLowerCorner().getZ()));
@ -493,7 +494,7 @@ namespace PolyVox
const VoxelType v001 = m_sampVolume.getVoxel();
const Vector3DFloat n001 = computeCentralDifferenceGradient(m_sampVolume);
float fInterp = static_cast<float>(VoxelType::getThreshold() - v000.getDensity()) / static_cast<float>(v001.getDensity() - v000.getDensity());
float fInterp = static_cast<float>(m_tThreshold - v000.getDensity()) / static_cast<float>(v001.getDensity() - v000.getDensity());
//fInterp = 0.5f;
const Vector3DFloat v3dPosition(static_cast<float>(iXVolSpace - m_regSizeInVoxels.getLowerCorner().getX()), static_cast<float>(iYVolSpace - m_regSizeInVoxels.getLowerCorner().getY()), static_cast<float>(iZVolSpace - m_regSizeInVoxels.getLowerCorner().getZ()) + fInterp);

View File

@ -70,6 +70,8 @@ namespace PolyVox
public:
const static bool HasDensity;
const static bool HasMaterial;
const static typename Type::DensityType MinDensity;
const static typename Type::DensityType MaxDensity;
};
}

View File

@ -49,11 +49,11 @@ namespace PolyVox
const bool VoxelTypeTraits< DensityFloat >::HasDensity = true;
const bool VoxelTypeTraits< DensityFloat >::HasMaterial = false;
const DensityFloat::DensityType VoxelTypeTraits< DensityFloat >::MinDensity = FLT_MIN;
const DensityFloat::DensityType VoxelTypeTraits< DensityFloat >::MinDensity = -FLT_MAX;
const DensityFloat::DensityType VoxelTypeTraits< DensityFloat >::MaxDensity = FLT_MAX;
const bool VoxelTypeTraits< DensityDouble >::HasDensity = true;
const bool VoxelTypeTraits< DensityDouble >::HasMaterial = false;
const DensityDouble::DensityType VoxelTypeTraits< DensityDouble >::MinDensity = DBL_MIN;
const DensityDouble::DensityType VoxelTypeTraits< DensityDouble >::MinDensity = -DBL_MAX;
const DensityDouble::DensityType VoxelTypeTraits< DensityDouble >::MaxDensity = DBL_MAX;
}

View File

@ -29,10 +29,16 @@ namespace PolyVox
{
const bool VoxelTypeTraits< MaterialU8 >::HasDensity = false;
const bool VoxelTypeTraits< MaterialU8 >::HasMaterial = true;
const MaterialU8::DensityType VoxelTypeTraits< MaterialU8 >::MinDensity = 0;
const MaterialU8::DensityType VoxelTypeTraits< MaterialU8 >::MaxDensity = 0;
const bool VoxelTypeTraits< MaterialU16 >::HasDensity = false;
const bool VoxelTypeTraits< MaterialU16 >::HasMaterial = true;
const MaterialU16::DensityType VoxelTypeTraits< MaterialU16 >::MinDensity = 0;
const MaterialU16::DensityType VoxelTypeTraits< MaterialU16 >::MaxDensity = 0;
const bool VoxelTypeTraits< MaterialU32 >::HasDensity = false;
const bool VoxelTypeTraits< MaterialU32 >::HasMaterial = true;
const MaterialU32::DensityType VoxelTypeTraits< MaterialU32 >::MinDensity = 0;
const MaterialU32::DensityType VoxelTypeTraits< MaterialU32 >::MaxDensity = 0;
}

View File

@ -29,4 +29,9 @@ namespace PolyVox
const bool VoxelTypeTraits<Type>::HasDensity = false;
template<typename Type>
const bool VoxelTypeTraits<Type>::HasMaterial = false;
template<typename Type>
const typename Type::DensityType VoxelTypeTraits< Type >::MinDensity = 0;
template<typename Type>
const typename Type::DensityType VoxelTypeTraits< Type >::MaxDensity = 0;
}

View File

@ -34,6 +34,39 @@ using namespace PolyVox;
void TestAStarPathfinder::testExecute()
{
//The expected path
#ifdef _MSC_VER
//The following results work on Windows/VS2010
const Vector3DInt32 expectedResult[] =
{
Vector3DInt32(0,0,0),
Vector3DInt32(1,1,1),
Vector3DInt32(1,2,1),
Vector3DInt32(1,3,1),
Vector3DInt32(1,4,1),
Vector3DInt32(1,5,1),
Vector3DInt32(1,6,1),
Vector3DInt32(1,7,1),
Vector3DInt32(1,8,1),
Vector3DInt32(2,9,2),
Vector3DInt32(3,10,3),
Vector3DInt32(3,11,4),
Vector3DInt32(4,12,5),
Vector3DInt32(5,12,5),
Vector3DInt32(6,13,6),
Vector3DInt32(7,13,7),
Vector3DInt32(8,13,8),
Vector3DInt32(9,13,9),
Vector3DInt32(10,13,10),
Vector3DInt32(11,13,11),
Vector3DInt32(12,13,12),
Vector3DInt32(13,13,13),
Vector3DInt32(14,14,14),
Vector3DInt32(15,15,15)
};
#else
//The following results work on Linux/GCC
const Vector3DInt32 expectedResult[] =
{
@ -62,35 +95,7 @@ void TestAStarPathfinder::testExecute()
Vector3DInt32(15,15,14),
Vector3DInt32(15,15,15),
};
//The following results work on Windows/VS2010
/*const Vector3DInt32 expectedResult[] =
{
Vector3DInt32(0,0,0),
Vector3DInt32(1,1,1),
Vector3DInt32(1,2,1),
Vector3DInt32(1,3,1),
Vector3DInt32(1,4,1),
Vector3DInt32(1,5,1),
Vector3DInt32(1,6,1),
Vector3DInt32(1,7,1),
Vector3DInt32(1,8,1),
Vector3DInt32(2,9,2),
Vector3DInt32(3,10,3),
Vector3DInt32(3,11,4),
Vector3DInt32(4,12,5),
Vector3DInt32(5,12,5),
Vector3DInt32(6,13,6),
Vector3DInt32(7,13,7),
Vector3DInt32(8,13,8),
Vector3DInt32(9,13,9),
Vector3DInt32(10,13,10),
Vector3DInt32(11,13,11),
Vector3DInt32(12,13,12),
Vector3DInt32(13,13,13),
Vector3DInt32(14,14,14),
Vector3DInt32(15,15,15)
};*/
#endif //_MSC_VER
//Create an empty volume
RawVolume<Material8> volData(Region(Vector3DInt32(0,0,0), Vector3DInt32(15, 15, 15)));

View File

@ -63,7 +63,7 @@ void TestVoxels::testVoxelTypeLimits()
QCOMPARE(VoxelTypeTraits<DensityDouble>::MaxDensity, DBL_MAX);
/*fValue = VoxelTypeTraits<DensityFloat>::MinDensity;
QCOMPARE(fValue, FLT_MIN);
QCOMPARE(fValue, -FLT_MAX);
fValue = VoxelTypeTraits<DensityFloat>::MaxDensity;
QCOMPARE(fValue, FLT_MAX);*/