Initial version of cubic surface extractor.
This commit is contained in:
parent
1123999d93
commit
43a1d0efa0
@ -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
|
||||
|
62
library/PolyVoxCore/include/CubicSurfaceExtractor.h
Normal file
62
library/PolyVoxCore/include/CubicSurfaceExtractor.h
Normal file
@ -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 <typename VoxelType>
|
||||
class CubicSurfaceExtractor
|
||||
{
|
||||
public:
|
||||
CubicSurfaceExtractor(Volume<VoxelType>* volData, Region region, SurfaceMesh* result);
|
||||
|
||||
void execute();
|
||||
|
||||
private:
|
||||
//The volume data and a sampler to access it.
|
||||
Volume<VoxelType>* m_volData;
|
||||
VolumeSampler<VoxelType> 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
|
132
library/PolyVoxCore/include/CubicSurfaceExtractor.inl
Normal file
132
library/PolyVoxCore/include/CubicSurfaceExtractor.inl
Normal file
@ -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 <typename VoxelType>
|
||||
CubicSurfaceExtractor<VoxelType>::CubicSurfaceExtractor(Volume<VoxelType>* 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 <typename VoxelType>
|
||||
void CubicSurfaceExtractor<VoxelType>::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);
|
||||
}
|
||||
}
|
@ -82,6 +82,7 @@ namespace PolyVox
|
||||
class SurfaceMesh;
|
||||
class Region;
|
||||
class SurfaceVertex;
|
||||
template <typename VoxelType> class CubicSurfaceExtractor;
|
||||
template <typename VoxelType> class SurfaceExtractor;
|
||||
|
||||
//---------- Vector ----------
|
||||
|
Loading…
x
Reference in New Issue
Block a user