From 764c9ac97cf0efcfbe285625e388a11ae7a23d8b Mon Sep 17 00:00:00 2001 From: David Williams Date: Tue, 27 May 2014 17:05:15 +0200 Subject: [PATCH] Simplified vertex types to structs with public members. Added (currently dummy) decode methods. --- .../PolyVoxCore/CubicSurfaceExtractor.inl | 7 +- .../MarchingCubesSurfaceExtractor.inl | 18 ++- .../PolyVoxCore/include/PolyVoxCore/Mesh.h | 18 +++ .../PolyVoxCore/PolyVoxForwardDeclarations.h | 4 +- .../include/PolyVoxCore/VertexTypes.h | 150 +++++------------- tests/TestSurfaceExtractor.cpp | 22 +-- 6 files changed, 95 insertions(+), 124 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl index 5bc067a9..f62ce7ae 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/CubicSurfaceExtractor.inl @@ -214,7 +214,10 @@ namespace PolyVox if(rEntry.iIndex == -1) { //No vertices matched and we've now hit an empty space. Fill it by creating a vertex. The 0.5f offset is because vertices set between voxels in order to build cubes around them. - rEntry.iIndex = m_meshCurrent->addVertex(CubicVertex(Vector3DFloat(static_cast(uX)-0.5f, static_cast(uY)-0.5f, static_cast(uZ)-0.5f), uMaterialIn)); + CubicVertex cubicVertex; + cubicVertex.position.setElements(static_cast(uX)-0.5f, static_cast(uY)-0.5f, static_cast(uZ)-0.5f); + cubicVertex.material = uMaterialIn; + rEntry.iIndex = m_meshCurrent->addVertex(cubicVertex); rEntry.uMaterial = uMaterialIn; return rEntry.iIndex; @@ -268,7 +271,7 @@ namespace PolyVox { //All four vertices of a given quad have the same material, //so just check that the first pair of vertices match. - if(m_meshCurrent->getVertices()[q1.vertices[0]].getMaterial() == m_meshCurrent->getVertices()[q2.vertices[0]].getMaterial()) + if(m_meshCurrent->getVertices()[q1.vertices[0]].material == m_meshCurrent->getVertices()[q2.vertices[0]].material) { //Now check whether quad 2 is adjacent to quad one by comparing vertices. //Adjacent quads must share two vertices, and the second quad could be to the diff --git a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl index 99998bf4..6b6a688b 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/MarchingCubesSurfaceExtractor.inl @@ -459,7 +459,11 @@ namespace PolyVox // Allow the controller to decide how the material should be derived from the voxels. const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v100, fInterp); - const MarchingCubesVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); + MarchingCubesVertex surfaceVertex; + surfaceVertex.position = v3dPosition; + surfaceVertex.normal = v3dNormal; + surfaceVertex.material = uMaterial; + const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); m_pCurrentVertexIndicesX[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; @@ -488,7 +492,11 @@ namespace PolyVox // Allow the controller to decide how the material should be derived from the voxels. const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v010, fInterp); - MarchingCubesVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); + MarchingCubesVertex surfaceVertex; + surfaceVertex.position = v3dPosition; + surfaceVertex.normal = v3dNormal; + surfaceVertex.material = uMaterial; + uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); m_pCurrentVertexIndicesY[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; @@ -516,7 +524,11 @@ namespace PolyVox // Allow the controller to decide how the material should be derived from the voxels. const typename VolumeType::VoxelType uMaterial = m_controller.blendMaterials(v000, v001, fInterp); - const MarchingCubesVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial); + MarchingCubesVertex surfaceVertex; + surfaceVertex.position = v3dPosition; + surfaceVertex.normal = v3dNormal; + surfaceVertex.material = uMaterial; + const uint32_t uLastVertexIndex = m_meshCurrent->addVertex(surfaceVertex); m_pCurrentVertexIndicesZ[iXVolSpace - m_regSizeInVoxels.getLowerX()][iYVolSpace - m_regSizeInVoxels.getLowerY()] = uLastVertexIndex; diff --git a/library/PolyVoxCore/include/PolyVoxCore/Mesh.h b/library/PolyVoxCore/include/PolyVoxCore/Mesh.h index bcb40576..bad1a7fb 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Mesh.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Mesh.h @@ -100,6 +100,24 @@ namespace PolyVox template std::shared_ptr< Mesh > extractSubset(Mesh& inputMesh, std::set setMaterials); + + template + Mesh< Vertex< typename MeshType::VertexType::VoxelType > > decode(const MeshType& mesh) + { + Mesh< Vertex< typename MeshType::VertexType::VoxelType > > result; + result.m_vecVertices.reserve(mesh.m_vecVertices.size()); + + for(uint32_t ct = 0; ct < mesh.m_vecVertices.size(); ct++) + { + result.m_vecVertices[ct] = decode(mesh.m_vecVertices[ct]); + } + + result.m_vecTriangleIndices = mesh.m_vecTriangleIndices; + + result.m_Region = mesh.m_Region; + + return result; + } } #include "PolyVoxCore/Mesh.inl" diff --git a/library/PolyVoxCore/include/PolyVoxCore/PolyVoxForwardDeclarations.h b/library/PolyVoxCore/include/PolyVoxCore/PolyVoxForwardDeclarations.h index 3d9ef742..49bd882c 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/PolyVoxForwardDeclarations.h +++ b/library/PolyVoxCore/include/PolyVoxCore/PolyVoxForwardDeclarations.h @@ -81,7 +81,7 @@ namespace PolyVox //////////////////////////////////////////////////////////////////////////////// // CubicVertex //////////////////////////////////////////////////////////////////////////////// - template class CubicVertex; + template struct CubicVertex; //////////////////////////////////////////////////////////////////////////////// // Density @@ -111,7 +111,7 @@ namespace PolyVox //////////////////////////////////////////////////////////////////////////////// // MarchingCubesVertex //////////////////////////////////////////////////////////////////////////////// - template class MarchingCubesVertex; + template struct MarchingCubesVertex; //////////////////////////////////////////////////////////////////////////////// // Material diff --git a/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h b/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h index e8d938ae..1f421fd9 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h +++ b/library/PolyVoxCore/include/PolyVoxCore/VertexTypes.h @@ -32,134 +32,72 @@ freely, subject to the following restrictions: #include namespace PolyVox -{ -#ifdef SWIG - class CubicVertex +{ + #ifdef SWIG + struct Vertex #else template - class POLYVOX_API CubicVertex + struct POLYVOX_API Vertex #endif { - public: - typedef _VoxelType VoxelType; - CubicVertex() - { - } - - CubicVertex(Vector3DFloat positionToSet, VoxelType materialToSet) - :position(positionToSet) - ,material(materialToSet) - { - } - - CubicVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet, VoxelType materialToSet) - :position(positionToSet) - ,normal(normalToSet) - ,material(materialToSet) - { - } - - VoxelType getMaterial(void) const - { - return material; - } - - const Vector3DFloat& getNormal(void) const - { - return normal; - } - - const Vector3DFloat& getPosition(void) const - { - return position; - } - - void setMaterial(VoxelType materialToSet) - { - material = materialToSet; - } - - void setNormal(const Vector3DFloat& normalToSet) - { - normal = normalToSet; - } - - void setPosition(const Vector3DFloat& positionToSet) - { - position = positionToSet; - } - - public: Vector3DFloat position; Vector3DFloat normal; VoxelType material; }; #ifdef SWIG - class MarchingCubesVertex + struct CubicVertex #else template - class POLYVOX_API MarchingCubesVertex + struct POLYVOX_API CubicVertex #endif { - public: - typedef _VoxelType VoxelType; - MarchingCubesVertex() - { - } - - MarchingCubesVertex(Vector3DFloat positionToSet, VoxelType materialToSet) - :position(positionToSet) - , material(materialToSet) - { - } - - MarchingCubesVertex(Vector3DFloat positionToSet, Vector3DFloat normalToSet, VoxelType materialToSet) - :position(positionToSet) - , normal(normalToSet) - , material(materialToSet) - { - } - - VoxelType getMaterial(void) const - { - return material; - } - - const Vector3DFloat& getNormal(void) const - { - return normal; - } - - const Vector3DFloat& getPosition(void) const - { - return position; - } - - void setMaterial(VoxelType materialToSet) - { - material = materialToSet; - } - - void setNormal(const Vector3DFloat& normalToSet) - { - normal = normalToSet; - } - - void setPosition(const Vector3DFloat& positionToSet) - { - position = positionToSet; - } - - public: Vector3DFloat position; Vector3DFloat normal; VoxelType material; }; + +#ifdef SWIG + struct MarchingCubesVertex +#else + template + struct POLYVOX_API MarchingCubesVertex +#endif + { + typedef _VoxelType VoxelType; + + Vector3DFloat position; + Vector3DFloat normal; + VoxelType material; + }; + + // Hopefully the compiler will implement the 'Return value optimization' here, but + // performance critical code will most likely decode the vertices in a shader anyway. + template + Vertex decode(const CubicVertex& cubicVertex) + { + Vertex result; + result.position = cubicVertex.position; + result.normal = cubicVertex.normal; + result.material = cubicVertex.material; + return result; + } + + // Hopefully the compiler will implement the 'Return value optimization' here, but + // performance critical code will most likely decode the vertices in a shader anyway. + template + Vertex decode(const MarchingCubesVertex& cubicVertex) + { + Vertex result; + result.position = cubicVertex.position; + result.normal = cubicVertex.normal; + result.material = cubicVertex.material; + return result; + } } #endif diff --git a/tests/TestSurfaceExtractor.cpp b/tests/TestSurfaceExtractor.cpp index 2189b9c2..e621fa99 100644 --- a/tests/TestSurfaceExtractor.cpp +++ b/tests/TestSurfaceExtractor.cpp @@ -172,58 +172,58 @@ void TestSurfaceExtractor::testExecute() } QCOMPARE(mesh.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh1 = testForType(); QCOMPARE(mesh1.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh1.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh1.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh1.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh2 = testForType(); QCOMPARE(mesh2.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh2.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh2.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh2.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh3 = testForType(); QCOMPARE(mesh3.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh3.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh3.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh3.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh4 = testForType(); QCOMPARE(mesh4.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh4.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh4.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh4.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh5 = testForType(); QCOMPARE(mesh5.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh5.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh5.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh5.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh6 = testForType(); QCOMPARE(mesh6.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh6.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh6.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh6.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh7 = testForType(); QCOMPARE(mesh7.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh7.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh7.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh7.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh8 = testForType(); QCOMPARE(mesh8.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh8.getNoOfIndices(), uExpectedIndices); - QCOMPARE(mesh8.getVertices()[uMaterialToCheck].getMaterial(), static_cast(fNoMaterial)); + QCOMPARE(mesh8.getVertices()[uMaterialToCheck].material, static_cast(fNoMaterial)); auto mesh9 = testForType(); QCOMPARE(mesh9.getNoOfVertices(), uExpectedVertices); QCOMPARE(mesh9.getNoOfIndices(), uExpectedIndices); - //QCOMPARE(mesh9.getVertices()[uMaterialToCheck].getMaterial(), fExpectedMaterial); + //QCOMPARE(mesh9.getVertices()[uMaterialToCheck].material, fExpectedMaterial); //Test whether the CustomSurfaceExtractor works. /*testCustomController(floatMesh); QCOMPARE(floatMesh.getNoOfVertices(), uExpectedVertices); QCOMPARE(floatMesh.getNoOfIndices(), uExpectedIndices); - QCOMPARE(floatMesh.getVertices()[uMaterialToCheck].getMaterial(), fNoMaterial);*/ + QCOMPARE(floatMesh.getVertices()[uMaterialToCheck].material, fNoMaterial);*/ } QTEST_MAIN(TestSurfaceExtractor)