From 7f38e87adea2302069629fed7779747741f458f8 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 15 Jun 2012 16:34:37 +0200 Subject: [PATCH] New 'ConvertToDensity' class for use in the SurfaceExtractor. --- .../PolyVoxCore/include/PolyVoxCore/Density.h | 14 ++++++++ .../include/PolyVoxCore/MaterialDensityPair.h | 34 +++++++++++++++++-- .../include/PolyVoxCore/SurfaceExtractor.h | 22 ++++++++++-- .../include/PolyVoxCore/SurfaceExtractor.inl | 20 ++++++----- library/PolyVoxCore/source/Density.cpp | 6 ++++ .../source/MaterialDensityPair.cpp | 20 ++++++++--- 6 files changed, 99 insertions(+), 17 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/Density.h b/library/PolyVoxCore/include/PolyVoxCore/Density.h index acce62b1..b2e01217 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Density.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Density.h @@ -102,9 +102,23 @@ namespace PolyVox static Density8::DensityType minDensity() { return std::numeric_limits::min(); } static Density8::DensityType maxDensity() { return std::numeric_limits::max(); } }; +} +#include "PolyVoxCore/SurfaceExtractor.h" //VERY UGLY THAT WE NEED THIS!!! TO BE CONSIDERED... + +namespace PolyVox +{ template<> typename VoxelTypeTraits::DensityType convertToDensity(Density8 voxel); + + template<> + class ConvertToDensity + { + public: + typedef uint8_t DensityType; + + DensityType operator()(Density8 voxel); + }; } #endif //__PolyVox_Density_H__ diff --git a/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h b/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h index 86d1d95f..7c364844 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h +++ b/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h @@ -131,10 +131,40 @@ namespace PolyVox typename VoxelTypeTraits::DensityType convertToDensity(MaterialDensityPair88 voxel); template<> - typename VoxelTypeTraits::MaterialType convertToMaterial(MaterialDensityPair44 voxel); + class ConvertToDensity + { + public: + typedef uint32_t DensityType; + + DensityType operator()(MaterialDensityPair44 voxel); + }; template<> - typename VoxelTypeTraits::MaterialType convertToMaterial(MaterialDensityPair88 voxel); + class ConvertToDensity + { + public: + typedef uint32_t DensityType; + + DensityType operator()(MaterialDensityPair88 voxel); + }; + + template<> + class ConvertToMaterial + { + public: + typedef uint32_t MaterialType; + + MaterialType operator()(MaterialDensityPair44 voxel); + }; + + template<> + class ConvertToMaterial + { + public: + typedef uint32_t MaterialType; + + MaterialType operator()(MaterialDensityPair88 voxel); + }; } #endif diff --git a/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.h b/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.h index e628fea6..d4d84092 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.h +++ b/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.h @@ -43,10 +43,26 @@ namespace PolyVox }; template - typename VoxelTypeTraits::MaterialType convertToMaterial(VoxelType voxel) + class ConvertToDensity { - return 1; - } + public: + typedef VoxelType DensityType; + DensityType operator()(VoxelType voxel) + { + return voxel; + } + }; + + template + class ConvertToMaterial + { + public: + typedef float MaterialType; + MaterialType operator()(VoxelType voxel) + { + return 1; + } + }; template< typename VolumeType> class SurfaceExtractor diff --git a/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.inl index b4562b81..65815cbb 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/SurfaceExtractor.inl @@ -206,6 +206,8 @@ namespace PolyVox VolumeType::VoxelType v011; VolumeType::VoxelType v111; + ConvertToDensity DensityConverter; + if(isPrevZAvail) { if(isPrevYAvail) @@ -230,7 +232,7 @@ namespace PolyVox iCubeIndex = iPreviousCubeIndexX | iPreviousCubeIndexY | iPreviousCubeIndexZ; - if (convertToDensity(v111) < m_tThreshold) iCubeIndex |= 128; + if (DensityConverter(v111) < m_tThreshold) iCubeIndex |= 128; } else //previous X not available { @@ -397,6 +399,8 @@ namespace PolyVox Array2DInt32& m_pCurrentVertexIndicesY, Array2DInt32& m_pCurrentVertexIndicesZ) { + ConvertToMaterial converter; + int32_t iZVolSpace = m_regSliceCurrent.getLowerCorner().getZ(); const uint32_t uZRegSpace = iZVolSpace - m_regSizeInVoxels.getLowerCorner().getZ(); @@ -443,9 +447,9 @@ namespace PolyVox //Choose one of the two materials to use for the vertex (we don't interpolate as interpolation of //material IDs does not make sense). We take the largest, so that if we are working on a material-only //volume we get the one which is non-zero. Both materials can be non-zero if our volume has a density component. - uint32_t uMaterial000 = convertToMaterial(v000); - uint32_t uMaterial100 = convertToMaterial(v100); - uint32_t uMaterial = (std::max)(uMaterial000, uMaterial100); + ConvertToMaterial::MaterialType uMaterial000 = converter(v000); + ConvertToMaterial::MaterialType uMaterial100 = converter(v100); + ConvertToMaterial::MaterialType uMaterial = (std::max)(uMaterial000, uMaterial100); PositionMaterialNormal surfaceVertex(v3dPosition, v3dNormal, static_cast(uMaterial)); uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); @@ -469,8 +473,8 @@ namespace PolyVox //Choose one of the two materials to use for the vertex (we don't interpolate as interpolation of //material IDs does not make sense). We take the largest, so that if we are working on a material-only //volume we get the one which is non-zero. Both materials can be non-zero if our volume has a density component. - uint32_t uMaterial000 = convertToMaterial(v000); - uint32_t uMaterial010 = convertToMaterial(v010); + ConvertToMaterial::MaterialType uMaterial000 = converter(v000); + ConvertToMaterial::MaterialType uMaterial010 = converter(v010); uint32_t uMaterial = (std::max)(uMaterial000, uMaterial010); PositionMaterialNormal surfaceVertex(v3dPosition, v3dNormal, static_cast(uMaterial)); @@ -495,8 +499,8 @@ namespace PolyVox //Choose one of the two materials to use for the vertex (we don't interpolate as interpolation of //material IDs does not make sense). We take the largest, so that if we are working on a material-only //volume we get the one which is non-zero. Both materials can be non-zero if our volume has a density component. - uint32_t uMaterial000 = convertToMaterial(v000); - uint32_t uMaterial001 = convertToMaterial(v001); + ConvertToMaterial::MaterialType uMaterial000 = converter(v000); + ConvertToMaterial::MaterialType uMaterial001 = converter(v001); uint32_t uMaterial = (std::max)(uMaterial000, uMaterial001); PositionMaterialNormal surfaceVertex(v3dPosition, v3dNormal, static_cast(uMaterial)); diff --git a/library/PolyVoxCore/source/Density.cpp b/library/PolyVoxCore/source/Density.cpp index 976dd04a..3131c504 100644 --- a/library/PolyVoxCore/source/Density.cpp +++ b/library/PolyVoxCore/source/Density.cpp @@ -30,4 +30,10 @@ namespace PolyVox { return voxel.getDensity(); } + + //template<> + ConvertToDensity::DensityType ConvertToDensity::operator()(Density8 voxel) + { + return voxel.getDensity(); + } } diff --git a/library/PolyVoxCore/source/MaterialDensityPair.cpp b/library/PolyVoxCore/source/MaterialDensityPair.cpp index 00a34a2b..199514a0 100644 --- a/library/PolyVoxCore/source/MaterialDensityPair.cpp +++ b/library/PolyVoxCore/source/MaterialDensityPair.cpp @@ -37,14 +37,26 @@ namespace PolyVox return voxel.getDensity(); } - template<> - typename VoxelTypeTraits::MaterialType convertToMaterial(MaterialDensityPair44 voxel) + //template<> + ConvertToDensity::DensityType ConvertToDensity::operator()(MaterialDensityPair44 voxel) + { + return voxel.getDensity(); + } + + //template<> + ConvertToDensity::DensityType ConvertToDensity::operator()(MaterialDensityPair88 voxel) + { + return voxel.getDensity(); + } + + //template<> + ConvertToMaterial::MaterialType ConvertToMaterial::operator()(MaterialDensityPair44 voxel) { return voxel.getMaterial(); } - template<> - typename VoxelTypeTraits::MaterialType convertToMaterial(MaterialDensityPair88 voxel) + //template<> + ConvertToMaterial::MaterialType ConvertToMaterial::operator()(MaterialDensityPair88 voxel) { return voxel.getMaterial(); }