在CGAL 3D表面网格生成之后,法线可能不一致

时间:2015-12-05 04:54:33

标签: opengl cgal

我使用CGAL,3D Surface Mesh Generation的包。

http://doc.cgal.org/latest/Surface_mesher/index.html#Chapter_3D_Surface_Mesh_Generation

我从示例代码开始:

http://doc.cgal.org/latest/Surface_mesher/Surface_mesher_2mesh_an_implicit_function_8cpp-example.html

现在尝试从类型为C2t3的变量c2t3中提取相关的facets(=三角形)。

是一个很好的解释如何做到这一点

http://wiki.schmid.dk/wiki/index.php/CGAL

我在

中找到了一点修改后的修改

http://cgal-discuss.949826.n4.nabble.com/normal-vector-of-a-facet-td1580004.html

现在,当我通过下面的代码片段将三角形分配给OpenGL时,显示的表面是黄色(我的照明颜色)和黑色三角形的马赛克 - 我得出结论这是因为表面法线不一致。但那怎么可能呢?如果在上面的最后一个链接中跟随参数,那么它应该是正确的。有没有更好地了解CGAL和3D Surface Mesh Generation及其数据结构的人能给我一些指导吗? (我也尝试了下面代码的几个明显的替代方案,但没有正常工作)。

for (C2t3::Facet_iterator fit = c2t3.facets_begin(); fit != c2t3.facets_end(); ++fit) {

        const Point_3& p0 = fit->first->vertex((fit->second))->point();
        // points on the facet
        const Point_3& p1 = fit->first->vertex((fit->second+1)&3)->point();
        const Point_3& p2 = fit->first->vertex((fit->second+2)&3)->point();
        const Point_3& p3 = fit->first->vertex((fit->second+3)&3)->point();

        Vector_3 n = ( fit->second % 2 == 1) ?
                           CGAL::normal(p1, p2, p3) :
                           CGAL::normal(p1, p3, p2);

        n = n /sqrt(n * n);

        glNormal3d(n.x(), n.y(), n.z());
        glVertex3d(p1.x(), p1.y(), p1.z());
        glVertex3d(p2.x(), p2.y(), p2.z());
        glVertex3d(p3.x(), p3.y(), p3.z());

        ++cnt2;
    }

1 个答案:

答案 0 :(得分:1)

您提取构面的方式是正确的,并且会为您提供一致的方向方向,您可以从同一方面考虑这些方面"表面的。例如,考虑嵌入在c2t3中的球体。如果你只考虑在球体内使用四面体的面,那么你的函数将做你想要的。但是由于在facets上的迭代不能保证你不会在球体外面有四面体,你的函数会显示错误的方向面。 一个简单的解决方案是使用函数CGAL::output_surface_facets_to_polyhedron首先从c2t3中创建一个多面体并将其用于显示。 或者,您也可以查看不那么复杂且模仿已完成的实现。