Refactoring of basic voxel types.
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
| @@ -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)) | ||||
| 					{ | ||||
|   | ||||
| @@ -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()); | ||||
|   | ||||
| @@ -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; | ||||
| 	}; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
| 	}; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -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; | ||||
| 	}; | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user