From 43a1d0efa0db4c8bca82192787d7282ba6d6b470 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 29 Aug 2010 21:43:37 +0000 Subject: [PATCH] Initial version of cubic surface extractor. --- library/PolyVoxCore/CMakeLists.txt | 2 + .../include/CubicSurfaceExtractor.h | 62 ++++++++ .../include/CubicSurfaceExtractor.inl | 132 ++++++++++++++++++ .../include/PolyVoxForwardDeclarations.h | 1 + 4 files changed, 197 insertions(+) create mode 100644 library/PolyVoxCore/include/CubicSurfaceExtractor.h create mode 100644 library/PolyVoxCore/include/CubicSurfaceExtractor.inl diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index ea0363ab..367151b4 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -22,6 +22,8 @@ SET(CORE_INC_FILES include/Array.h include/Array.inl include/ArraySizes.h + include/CubicSurfaceExtractor.h + include/CubicSurfaceExtractor.inl include/Filters.h include/Filters.inl include/GradientEstimators.inl diff --git a/library/PolyVoxCore/include/CubicSurfaceExtractor.h b/library/PolyVoxCore/include/CubicSurfaceExtractor.h new file mode 100644 index 00000000..007da2ca --- /dev/null +++ b/library/PolyVoxCore/include/CubicSurfaceExtractor.h @@ -0,0 +1,62 @@ +#pragma region License +/******************************************************************************* +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. +*******************************************************************************/ +#pragma endregion + +#ifndef __PolyVox_CubicSurfaceExtractor_H__ +#define __PolyVox_CubicSurfaceExtractor_H__ + +#pragma region Headers +#include "PolyVoxForwardDeclarations.h" +#include "VolumeSampler.h" + +#include "PolyVoxImpl/TypeDef.h" +#pragma endregion + +namespace PolyVox +{ + template + class CubicSurfaceExtractor + { + public: + CubicSurfaceExtractor(Volume* volData, Region region, SurfaceMesh* result); + + void execute(); + + private: + //The volume data and a sampler to access it. + Volume* m_volData; + VolumeSampler m_sampVolume; + + //The surface patch we are currently filling. + SurfaceMesh* m_meshCurrent; + + //Information about the region we are currently processing + Region m_regSizeInVoxels; + Region m_regSizeInCells; + }; +} + +#include "CubicSurfaceExtractor.inl" + +#endif diff --git a/library/PolyVoxCore/include/CubicSurfaceExtractor.inl b/library/PolyVoxCore/include/CubicSurfaceExtractor.inl new file mode 100644 index 00000000..58601420 --- /dev/null +++ b/library/PolyVoxCore/include/CubicSurfaceExtractor.inl @@ -0,0 +1,132 @@ +/******************************************************************************* +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. +*******************************************************************************/ + +#include "Array.h" +#include "MaterialDensityPair.h" +#include "SurfaceMesh.h" +#include "PolyVoxImpl/MarchingCubesTables.h" +#include "SurfaceVertex.h" + +namespace PolyVox +{ + template + CubicSurfaceExtractor::CubicSurfaceExtractor(Volume* volData, Region region, SurfaceMesh* result) + :m_volData(volData) + ,m_sampVolume(volData) + ,m_regSizeInVoxels(region) + ,m_meshCurrent(result) + { + m_regSizeInVoxels.cropTo(m_volData->getEnclosingRegion()); + m_regSizeInCells = m_regSizeInVoxels; + m_regSizeInCells.setUpperCorner(m_regSizeInCells.getUpperCorner() - Vector3DInt16(1,1,1)); + + m_meshCurrent->clear(); + } + + template + void CubicSurfaceExtractor::execute() + { + for(uint16_t z = m_regSizeInVoxels.getLowerCorner().getZ(); z < m_regSizeInVoxels.getUpperCorner().getZ(); z++) + { + for(uint16_t y = m_regSizeInVoxels.getLowerCorner().getY(); y < m_regSizeInVoxels.getUpperCorner().getY(); y++) + { + for(uint16_t x = m_regSizeInVoxels.getLowerCorner().getX(); x < m_regSizeInVoxels.getUpperCorner().getX(); x++) + { + int currentVoxel = m_volData->getVoxelAt(x,y,z).getDensity() >= VoxelType::getThreshold(); + + int plusXVoxel = m_volData->getVoxelAt(x+1,y,z).getDensity() >= VoxelType::getThreshold(); + if(currentVoxel > plusXVoxel) + { + uint32_t v0 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y - 0.5f, z - 0.5f), Vector3DFloat(1.0f, 0.0f, 0.0f), 1)); + uint32_t v1 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y - 0.5f, z + 0.5f), Vector3DFloat(1.0f, 0.0f, 0.0f), 1)); + uint32_t v2 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z - 0.5f), Vector3DFloat(1.0f, 0.0f, 0.0f), 1)); + uint32_t v3 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(1.0f, 0.0f, 0.0f), 1)); + + m_meshCurrent->addTriangle(v0,v1,v2); + m_meshCurrent->addTriangle(v1,v3,v2); + } + if(currentVoxel < plusXVoxel) + { + uint32_t v0 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y - 0.5f, z - 0.5f), Vector3DFloat(-1.0f, 0.0f, 0.0f), 1)); + uint32_t v1 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y - 0.5f, z + 0.5f), Vector3DFloat(-1.0f, 0.0f, 0.0f), 1)); + uint32_t v2 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z - 0.5f), Vector3DFloat(-1.0f, 0.0f, 0.0f), 1)); + uint32_t v3 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(-1.0f, 0.0f, 0.0f), 1)); + + m_meshCurrent->addTriangle(v0,v1,v2); + m_meshCurrent->addTriangle(v1,v3,v2); + } + + int plusYVoxel = m_volData->getVoxelAt(x,y+1,z).getDensity() >= VoxelType::getThreshold(); + if(currentVoxel > plusYVoxel) + { + uint32_t v0 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x - 0.5f, y + 0.5f, z - 0.5f), Vector3DFloat(0.0f, 1.0f, 0.0f), 1)); + uint32_t v1 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x - 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(0.0f, 1.0f, 0.0f), 1)); + uint32_t v2 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z - 0.5f), Vector3DFloat(0.0f, 1.0f, 0.0f), 1)); + uint32_t v3 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(0.0f, 1.0f, 0.0f), 1)); + + m_meshCurrent->addTriangle(v0,v1,v2); + m_meshCurrent->addTriangle(v1,v3,v2); + } + if(currentVoxel < plusYVoxel) + { + uint32_t v0 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x - 0.5f, y + 0.5f, z - 0.5f), Vector3DFloat(0.0f, -1.0f, 0.0f), 1)); + uint32_t v1 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x - 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(0.0f, -1.0f, 0.0f), 1)); + uint32_t v2 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z - 0.5f), Vector3DFloat(0.0f, -1.0f, 0.0f), 1)); + uint32_t v3 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(0.0f, -1.0f, 0.0f), 1)); + + m_meshCurrent->addTriangle(v0,v1,v2); + m_meshCurrent->addTriangle(v1,v3,v2); + } + + int plusZVoxel = m_volData->getVoxelAt(x,y,z+1).getDensity() >= VoxelType::getThreshold(); + if(currentVoxel > plusZVoxel) + { + uint32_t v0 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x - 0.5f, y - 0.5f, z + 0.5f), Vector3DFloat(0.0f, 0.0f, 1.0f), 1)); + uint32_t v1 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x - 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(0.0f, 0.0f, 1.0f), 1)); + uint32_t v2 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y - 0.5f, z + 0.5f), Vector3DFloat(0.0f, 0.0f, 1.0f), 1)); + uint32_t v3 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(0.0f, 0.0f, 1.0f), 1)); + + m_meshCurrent->addTriangle(v0,v1,v2); + m_meshCurrent->addTriangle(v1,v3,v2); + } + if(currentVoxel < plusZVoxel) + { + uint32_t v0 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x - 0.5f, y - 0.5f, z + 0.5f), Vector3DFloat(0.0f, 0.0f, -1.0f), 1)); + uint32_t v1 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x - 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(0.0f, 0.0f, -1.0f), 1)); + uint32_t v2 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y - 0.5f, z + 0.5f), Vector3DFloat(0.0f, 0.0f, -1.0f), 1)); + uint32_t v3 = m_meshCurrent->addVertex(SurfaceVertex(Vector3DFloat(x + 0.5f, y + 0.5f, z + 0.5f), Vector3DFloat(0.0f, 0.0f, -1.0f), 1)); + + m_meshCurrent->addTriangle(v0,v1,v2); + m_meshCurrent->addTriangle(v1,v3,v2); + } + } + } + } + + m_meshCurrent->m_vecLodRecords.clear(); + LodRecord lodRecord; + lodRecord.beginIndex = 0; + lodRecord.endIndex = m_meshCurrent->getNoOfIndices(); + m_meshCurrent->m_vecLodRecords.push_back(lodRecord); + } +} \ No newline at end of file diff --git a/library/PolyVoxCore/include/PolyVoxForwardDeclarations.h b/library/PolyVoxCore/include/PolyVoxForwardDeclarations.h index c5c3835e..9ce83584 100644 --- a/library/PolyVoxCore/include/PolyVoxForwardDeclarations.h +++ b/library/PolyVoxCore/include/PolyVoxForwardDeclarations.h @@ -82,6 +82,7 @@ namespace PolyVox class SurfaceMesh; class Region; class SurfaceVertex; + template class CubicSurfaceExtractor; template class SurfaceExtractor; //---------- Vector ----------