diff --git a/library/PolyVoxCore/include/PolyVoxCore/AStarPathfinder.inl b/library/PolyVoxCore/include/PolyVoxCore/AStarPathfinder.inl index 76262eb1..57146e89 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/AStarPathfinder.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/AStarPathfinder.inl @@ -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::HasDensity) { - return false; + //and if their density is above the threshold. + VoxelType voxel = volData->getVoxelAt(v3dPos); + VoxelType::DensityType tThreshold = (VoxelTypeTraits::MinDensity + VoxelTypeTraits::MaxDensity) / 2; + if(voxel.getDensity() >= tThreshold) + { + return false; + } + } + + if(VoxelTypeTraits::HasMaterial) + { + //and if their material is not zero. + VoxelType voxel = volData->getVoxelAt(v3dPos); + if(voxel.getMaterial() != 0) + { + return false; + } } return true; diff --git a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl index 7d8e5206..87ede88f 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl @@ -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)) { diff --git a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl index c448cbba..bfa23f17 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl @@ -48,9 +48,9 @@ namespace PolyVox float regY = static_cast(y - m_regSizeInVoxels.getLowerCorner().getY()); float regZ = static_cast(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(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(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(m_volData->getVoxelAt(x,y,z).getMaterial()); diff --git a/library/PolyVoxCore/include/PolyVoxCore/Material.h b/library/PolyVoxCore/include/PolyVoxCore/Material.h index fb48d3ff..40d976a4 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Material.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Material.h @@ -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; }; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.h b/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.h index f634dcb5..e056ce05 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.h +++ b/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.h @@ -36,7 +36,7 @@ namespace PolyVox class SurfaceExtractor { public: - SurfaceExtractor(VolumeType* volData, Region region, SurfaceMesh* result); + SurfaceExtractor(VolumeType* volData, Region region, SurfaceMesh* result, typename VoxelType::DensityType tThreshold = (VoxelTypeTraits::MinDensity + VoxelTypeTraits::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; }; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.inl index bbba8559..e2d9815d 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.inl @@ -24,11 +24,12 @@ freely, subject to the following restrictions: namespace PolyVox { template< template class VolumeType, typename VoxelType> - SurfaceExtractor::SurfaceExtractor(VolumeType* volData, Region region, SurfaceMesh* result) + SurfaceExtractor::SurfaceExtractor(VolumeType* volData, Region region, SurfaceMesh* 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(v100.getDensity() - VoxelType::getMinDensity()) / static_cast(VoxelType::getMaxDensity() - VoxelType::getMinDensity()); - float fInterp = static_cast(VoxelType::getThreshold() - v000.getDensity()) / static_cast(v100.getDensity() - v000.getDensity()); + float fInterp = static_cast(m_tThreshold - v000.getDensity()) / static_cast(v100.getDensity() - v000.getDensity()); //fInterp = 0.5f; const Vector3DFloat v3dPosition(static_cast(iXVolSpace - m_regSizeInVoxels.getLowerCorner().getX()) + fInterp, static_cast(iYVolSpace - m_regSizeInVoxels.getLowerCorner().getY()), static_cast(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(VoxelType::getThreshold() - v000.getDensity()) / static_cast(v010.getDensity() - v000.getDensity()); + float fInterp = static_cast(m_tThreshold - v000.getDensity()) / static_cast(v010.getDensity() - v000.getDensity()); //fInterp = 0.5f; const Vector3DFloat v3dPosition(static_cast(iXVolSpace - m_regSizeInVoxels.getLowerCorner().getX()), static_cast(iYVolSpace - m_regSizeInVoxels.getLowerCorner().getY()) + fInterp, static_cast(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(VoxelType::getThreshold() - v000.getDensity()) / static_cast(v001.getDensity() - v000.getDensity()); + float fInterp = static_cast(m_tThreshold - v000.getDensity()) / static_cast(v001.getDensity() - v000.getDensity()); //fInterp = 0.5f; const Vector3DFloat v3dPosition(static_cast(iXVolSpace - m_regSizeInVoxels.getLowerCorner().getX()), static_cast(iYVolSpace - m_regSizeInVoxels.getLowerCorner().getY()), static_cast(iZVolSpace - m_regSizeInVoxels.getLowerCorner().getZ()) + fInterp); diff --git a/library/PolyVoxCore/include/PolyVoxCore/Voxel.h b/library/PolyVoxCore/include/PolyVoxCore/Voxel.h index 48cfbb35..e86e5db0 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Voxel.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Voxel.h @@ -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; }; } diff --git a/library/PolyVoxCore/source/Density.cpp b/library/PolyVoxCore/source/Density.cpp index ca6a202b..fec66c10 100644 --- a/library/PolyVoxCore/source/Density.cpp +++ b/library/PolyVoxCore/source/Density.cpp @@ -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; } \ No newline at end of file diff --git a/library/PolyVoxCore/source/Material.cpp b/library/PolyVoxCore/source/Material.cpp index 5b3caa6f..ed44d500 100644 --- a/library/PolyVoxCore/source/Material.cpp +++ b/library/PolyVoxCore/source/Material.cpp @@ -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; } \ No newline at end of file diff --git a/library/PolyVoxCore/source/Voxel.cpp b/library/PolyVoxCore/source/Voxel.cpp index c271e278..ebdf4f7b 100644 --- a/library/PolyVoxCore/source/Voxel.cpp +++ b/library/PolyVoxCore/source/Voxel.cpp @@ -29,4 +29,9 @@ namespace PolyVox const bool VoxelTypeTraits::HasDensity = false; template const bool VoxelTypeTraits::HasMaterial = false; + + template + const typename Type::DensityType VoxelTypeTraits< Type >::MinDensity = 0; + template + const typename Type::DensityType VoxelTypeTraits< Type >::MaxDensity = 0; } \ No newline at end of file diff --git a/tests/TestAStarPathfinder.cpp b/tests/TestAStarPathfinder.cpp index 7df04a0a..667f12ad 100644 --- a/tests/TestAStarPathfinder.cpp +++ b/tests/TestAStarPathfinder.cpp @@ -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 volData(Region(Vector3DInt32(0,0,0), Vector3DInt32(15, 15, 15))); diff --git a/tests/TestVoxels.cpp b/tests/TestVoxels.cpp index 9ba2e8ba..c5eb159a 100644 --- a/tests/TestVoxels.cpp +++ b/tests/TestVoxels.cpp @@ -63,7 +63,7 @@ void TestVoxels::testVoxelTypeLimits() QCOMPARE(VoxelTypeTraits::MaxDensity, DBL_MAX); /*fValue = VoxelTypeTraits::MinDensity; - QCOMPARE(fValue, FLT_MIN); + QCOMPARE(fValue, -FLT_MAX); fValue = VoxelTypeTraits::MaxDensity; QCOMPARE(fValue, FLT_MAX);*/