From 301f93d8967db2b5e1f4789df14a2e562b8c3c00 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 9 Aug 2012 16:06:10 +0200 Subject: [PATCH] Turned isQuadNeeded() (which was a stl::function) into a function object. --- examples/Paging/main.cpp | 4 +- library/PolyVoxCore/CMakeLists.txt | 1 + .../CubicSurfaceExtractorWithNormals.h | 25 ++-------- .../CubicSurfaceExtractorWithNormals.inl | 13 +++-- .../include/PolyVoxCore/DefaultIsQuadNeeded.h | 48 +++++++++++++++++++ .../include/PolyVoxCore/Material.h | 13 ----- .../include/PolyVoxCore/MaterialDensityPair.h | 16 +++++-- .../PolyVoxCore/source/SimpleInterface.cpp | 2 +- tests/TestCubicSurfaceExtractor.cpp | 2 +- 9 files changed, 75 insertions(+), 49 deletions(-) create mode 100644 library/PolyVoxCore/include/PolyVoxCore/DefaultIsQuadNeeded.h diff --git a/examples/Paging/main.cpp b/examples/Paging/main.cpp index 43abff47..4a6c3cfe 100644 --- a/examples/Paging/main.cpp +++ b/examples/Paging/main.cpp @@ -286,8 +286,8 @@ int main(int argc, char *argv[]) //Extract the surface SurfaceMesh mesh; - //CubicSurfaceExtractorWithNormals surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh); - MarchingCubesSurfaceExtractor< LargeVolume > surfaceExtractor(&volData, reg, &mesh); + CubicSurfaceExtractorWithNormals< LargeVolume > surfaceExtractor(&volData, reg, &mesh); + //MarchingCubesSurfaceExtractor< LargeVolume > surfaceExtractor(&volData, reg, &mesh); //CubicSurfaceExtractorWithNormals surfaceExtractor(&volData, reg, &mesh); surfaceExtractor.execute(); std::cout << "#vertices: " << mesh.getNoOfVertices() << std::endl; diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index ed3f33c5..474f1e47 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -32,6 +32,7 @@ SET(CORE_INC_FILES include/PolyVoxCore/CubicSurfaceExtractor.inl include/PolyVoxCore/CubicSurfaceExtractorWithNormals.h include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl + include/PolyVoxCore/DefaultIsQuadNeeded.h include/PolyVoxCore/DefaultMarchingCubesController.h include/PolyVoxCore/Density.h include/PolyVoxCore/GradientEstimators.h diff --git a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.h b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.h index 02b8d045..70266813 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.h +++ b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.h @@ -24,39 +24,24 @@ freely, subject to the following restrictions: #ifndef __PolyVox_CubicSurfaceExtractorWithNormals_H__ #define __PolyVox_CubicSurfaceExtractorWithNormals_H__ -#include "PolyVoxImpl/MarchingCubesTables.h" +#include "PolyVoxCore/DefaultIsQuadNeeded.h" #include "PolyVoxCore/Array.h" #include "PolyVoxCore/SurfaceMesh.h" namespace PolyVox { - - //FIXME - Make this a member of CubicSurfaceExtractorWithNormals? - template - bool defaultIsQuadNeeded(VoxelType back, VoxelType front, float& materialToUse) - { - if((back > 0) && (front == 0)) - { - materialToUse = static_cast(back); - return true; - } - else - { - return false; - } - } - - template + template > class CubicSurfaceExtractorWithNormals { public: - CubicSurfaceExtractorWithNormals(VolumeType* volData, Region region, SurfaceMesh* result, polyvox_function funcIsQuadNeededCallback = defaultIsQuadNeeded); + CubicSurfaceExtractorWithNormals(VolumeType* volData, Region region, SurfaceMesh* result, IsQuadNeeded isQuadNeeded = IsQuadNeeded()); void execute(); private: - polyvox_function m_funcIsQuadNeededCallback; + //polyvox_function m_funcIsQuadNeededCallback; + IsQuadNeeded m_funcIsQuadNeededCallback; //The volume data and a sampler to access it. VolumeType* m_volData; diff --git a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl index 45a45fbd..59e752a7 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractorWithNormals.inl @@ -23,19 +23,18 @@ freely, subject to the following restrictions: namespace PolyVox { - template - CubicSurfaceExtractorWithNormals::CubicSurfaceExtractorWithNormals(VolumeType* volData, Region region, SurfaceMesh* result, polyvox_function funcIsQuadNeededCallback) - :m_funcIsQuadNeededCallback(funcIsQuadNeededCallback) - ,m_volData(volData) + template + CubicSurfaceExtractorWithNormals::CubicSurfaceExtractorWithNormals(VolumeType* volData, Region region, SurfaceMesh* result, IsQuadNeeded isQuadNeeded) + :m_volData(volData) ,m_sampVolume(volData) ,m_meshCurrent(result) ,m_regSizeInVoxels(region) { - assert(m_funcIsQuadNeededCallback); + m_funcIsQuadNeededCallback = isQuadNeeded; } - template - void CubicSurfaceExtractorWithNormals::execute() + template + void CubicSurfaceExtractorWithNormals::execute() { m_meshCurrent->clear(); diff --git a/library/PolyVoxCore/include/PolyVoxCore/DefaultIsQuadNeeded.h b/library/PolyVoxCore/include/PolyVoxCore/DefaultIsQuadNeeded.h new file mode 100644 index 00000000..e679edf6 --- /dev/null +++ b/library/PolyVoxCore/include/PolyVoxCore/DefaultIsQuadNeeded.h @@ -0,0 +1,48 @@ +/******************************************************************************* +Copyright (c) 2005-2009 David Williams + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +*******************************************************************************/ + +#ifndef __PolyVox_DefaultIsQuadNeeded_H__ +#define __PolyVox_DefaultIsQuadNeeded_H__ + +namespace PolyVox +{ + template + class DefaultIsQuadNeeded + { + public: + bool operator()(VoxelType back, VoxelType front, float& materialToUse) + { + if((back > 0) && (front == 0)) + { + materialToUse = static_cast(back); + return true; + } + else + { + return false; + } + } + }; +} + +#endif //__PolyVox_DefaultIsQuadNeeded_H__ \ No newline at end of file diff --git a/library/PolyVoxCore/include/PolyVoxCore/Material.h b/library/PolyVoxCore/include/PolyVoxCore/Material.h index cb7c43a8..091e9495 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Material.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Material.h @@ -73,19 +73,6 @@ namespace PolyVox MaterialType getMaterial() const throw() { return m_uMaterial; } void setMaterial(MaterialType uMaterial) { m_uMaterial = uMaterial; } - static bool isQuadNeeded(Material back, Material front, float& materialToUse) - { - if((back.getMaterial() > 0) && (front.getMaterial() == 0)) - { - materialToUse = static_cast(back.getMaterial()); - return true; - } - else - { - return false; - } - } - private: MaterialType m_uMaterial; }; diff --git a/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h b/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h index a59b175d..6ab9a563 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h +++ b/library/PolyVoxCore/include/PolyVoxCore/MaterialDensityPair.h @@ -24,6 +24,7 @@ freely, subject to the following restrictions: #ifndef __PolyVox_MaterialDensityPair_H__ #define __PolyVox_MaterialDensityPair_H__ +#include "PolyVoxCore/DefaultIsQuadNeeded.h" //we'll specialise this function for this voxel type #include "PolyVoxCore/DefaultMarchingCubesController.h" //We'll specialise the controller contained in here #include "PolyVoxImpl/TypeDef.h" @@ -95,7 +96,16 @@ namespace PolyVox static DensityType getMaxDensity() throw() { return (0x01 << NoOfDensityBits) - 1; } static DensityType getMinDensity() throw() { return 0; } - static bool isQuadNeeded(MaterialDensityPair back, MaterialDensityPair front, float& materialToUse) + private: + MaterialType m_uMaterial : NoOfMaterialBits; + DensityType m_uDensity : NoOfDensityBits; + }; + + template + class DefaultIsQuadNeeded< MaterialDensityPair > + { + public: + bool operator()(MaterialDensityPair back, MaterialDensityPair front, float& materialToUse) { if((back.getMaterial() > 0) && (front.getMaterial() == 0)) { @@ -107,10 +117,6 @@ namespace PolyVox return false; } } - - private: - MaterialType m_uMaterial : NoOfMaterialBits; - DensityType m_uDensity : NoOfDensityBits; }; template diff --git a/library/PolyVoxCore/source/SimpleInterface.cpp b/library/PolyVoxCore/source/SimpleInterface.cpp index b5e50b3d..e098e175 100644 --- a/library/PolyVoxCore/source/SimpleInterface.cpp +++ b/library/PolyVoxCore/source/SimpleInterface.cpp @@ -31,7 +31,7 @@ namespace PolyVox { void extractCubicMesh(Volume& volume, const Region& region, Mesh& resultMesh) { - CubicSurfaceExtractorWithNormals< SimpleVolume > surfaceExtractor(&volume, region, &resultMesh, MaterialDensityPair88::isQuadNeeded); + CubicSurfaceExtractorWithNormals< SimpleVolume > surfaceExtractor(&volume, region, &resultMesh); surfaceExtractor.execute(); } diff --git a/tests/TestCubicSurfaceExtractor.cpp b/tests/TestCubicSurfaceExtractor.cpp index 673278fd..7a7557de 100644 --- a/tests/TestCubicSurfaceExtractor.cpp +++ b/tests/TestCubicSurfaceExtractor.cpp @@ -94,7 +94,7 @@ void testForType(SurfaceMesh& result) } } - CubicSurfaceExtractorWithNormals< SimpleVolume > extractor(&volData, volData.getEnclosingRegion(), &result, VoxelType::isQuadNeeded); + CubicSurfaceExtractorWithNormals< SimpleVolume > extractor(&volData, volData.getEnclosingRegion(), &result); extractor.execute(); }