我一直在尝试用 C++ 和 Qt 实现行进立方体算法。不管怎样,到目前为止所有的步骤都已经写好了,但我得到的结果非常糟糕。我正在寻找有关可能出现问题的指导或建议。我怀疑问题之一可能与体素概念有关,特别是关于哪个顶点位于哪个角(0, 1, ..., 7)。另外,我不能 100% 确定如何解释算法的输入(我正在使用数据集)。 我应该按照 ZYX 顺序读取它并以相同的方式移动行进立方体还是根本不重要? (不考虑并非每个维度都必须具有相同大小的事实)。
这就是我所得到的反对它应该是什么样子的......
最佳答案
http://en.wikipedia.org/wiki/Marching_cubes
http://en.wikipedia.org/wiki/Marching_cubes#External_links
保罗·伯克。 “概述和源代码”。
http://paulbourke.net/geometry/polygonise/
Qt_MARCHING_CUBES.zip:Qt/OpenGL 示例由 Klaus Miltenberger 博士提供。
http://paulbourke.net/geometry/polygonise/Qt_MARCHING_CUBES.zip
该示例需要增强,但看起来应该可以工作。
在他的示例中,marchingcubes.cpp
中有一些用于计算行进立方体的不同方法:vMarchCube1
和 vMarchCube2
。
在评论中,它说 vMarchCube2
通过对 vMarchTetrahedron 进行六次调用,在单个立方体上执行 Marching Tetrahedrons 算法。
以下是第一个 vMarchCube1
的源代码:
//vMarchCube1 performs the Marching Cubes algorithm on a single cube
GLvoid GL_Widget::vMarchCube1(const GLfloat &fX, const GLfloat &fY, const GLfloat &fZ, const GLfloat &fScale, const GLfloat &fTv)
{
GLint iCorner, iVertex, iVertexTest, iEdge, iTriangle, iFlagIndex, iEdgeFlags;
GLfloat fOffset;
GLvector sColor;
GLfloat afCubeValue[8];
GLvector asEdgeVertex[12];
GLvector asEdgeNorm[12];
//Make a local copy of the values at the cube's corners
for(iVertex = 0; iVertex < 8; iVertex++)
{
afCubeValue[iVertex] = (this->*fSample)(fX + a2fVertexOffset[iVertex][0]*fScale,fY + a2fVertexOffset[iVertex][1]*fScale,fZ + a2fVertexOffset[iVertex][2]*fScale);
}
//Find which vertices are inside of the surface and which are outside
iFlagIndex = 0;
for(iVertexTest = 0; iVertexTest < 8; iVertexTest++)
{
if(afCubeValue[iVertexTest] <= fTv) iFlagIndex |= 1<<iVertexTest;
}
//Find which edges are intersected by the surface
iEdgeFlags = aiCubeEdgeFlags[iFlagIndex];
//If the cube is entirely inside or outside of the surface, then there will be no intersections
if(iEdgeFlags == 0)
{
return;
}
//Find the point of intersection of the surface with each edge
//Then find the normal to the surface at those points
for(iEdge = 0; iEdge < 12; iEdge++)
{
//if there is an intersection on this edge
if(iEdgeFlags & (1<<iEdge))
{
fOffset = fGetOffset(afCubeValue[ a2iEdgeConnection[iEdge][0] ],afCubeValue[ a2iEdgeConnection[iEdge][1] ], fTv);
asEdgeVertex[iEdge].fX = fX + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][0] + fOffset * a2fEdgeDirection[iEdge][0]) * fScale;
asEdgeVertex[iEdge].fY = fY + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][1] + fOffset * a2fEdgeDirection[iEdge][1]) * fScale;
asEdgeVertex[iEdge].fZ = fZ + (a2fVertexOffset[ a2iEdgeConnection[iEdge][0] ][2] + fOffset * a2fEdgeDirection[iEdge][2]) * fScale;
vGetNormal(asEdgeNorm[iEdge], asEdgeVertex[iEdge].fX, asEdgeVertex[iEdge].fY, asEdgeVertex[iEdge].fZ);
}
}
//Draw the triangles that were found. There can be up to five per cube
for(iTriangle = 0; iTriangle < 5; iTriangle++)
{
if(a2iTriangleConnectionTable[iFlagIndex][3*iTriangle] < 0) break;
for(iCorner = 0; iCorner < 3; iCorner++)
{
iVertex = a2iTriangleConnectionTable[iFlagIndex][3*iTriangle+iCorner];
vGetColor(sColor, asEdgeVertex[iVertex], asEdgeNorm[iVertex]);
glColor4f(sColor.fX, sColor.fY, sColor.fZ, 0.6);
glNormal3f(asEdgeNorm[iVertex].fX, asEdgeNorm[iVertex].fY, asEdgeNorm[iVertex].fZ);
glVertex3f(asEdgeVertex[iVertex].fX, asEdgeVertex[iVertex].fY, asEdgeVertex[iVertex].fZ);
}
}
}
更新:Github 工作示例,已测试
https://github.com/peteristhegreat/qt-marching-cubes
希望有帮助。
关于c++ - 行进立方体问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28030366/