我正在实现一个基于 Paul Bourke 的行进立方体算法。进行了一些重大调整:
- 预计算标量场(浮点值)
- 使用 std::map 避免最终列表中的重复顶点
- 顶点存储以可视化 Ogre3D 中的最终网格
基本上我改了他近80%的代码。我生成的网格有一些丑陋的梯田,我不确定如何避免它们。我假设对标量场使用 float 就可以完成这项工作。这是一个普遍的影响吗?如何避免?
计算边上的顶点位置。 (cell.val[p1] 包含给定顶点的标量值):
//if there is an intersection on this edge
if (cell.iEdgeFlags & (1 << iEdge))
{
const int* edge = a2iEdgeConnection[iEdge];
int p1 = edge[0];
int p2 = edge[1];
//find the approx intersection point by linear interpolation between the two edges and the density value
float length = cell.val[p1] / (cell.val[p2] + cell.val[p1]);
asEdgeVertex[iEdge] = cell.p[p1] + length * (cell.p[p2] - cell.p[p1]);
}
您可以在这里找到完整的源代码:https://github.com/DieOptimistin/MarchingCubes我使用 Ogre3D 作为这个例子的库。
正如 Andy Newmann 所说,问题在于线性插值。正确的是:
float offset;
float delta = cell.val[p2] - cell.val[p1];
if (delta == 0) offset = 0.5;
else offset = (mTargetValue - cell.val[p1]) / delta;
asEdgeVertex[iEdge] = cell.p[p1] + offset* (cell.p[p2] - cell.p[p1]);
我是一名优秀的程序员,十分优秀!