0

我尝试运行此代码以显示 3D 网格输出由于某种奇怪的原因,显示了 nf 和顶点数的输出,但之后它给出了分段错误;Moar 输出行未写入 std。知道为什么吗?

#include <CGAL/Cartesian.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/IO/Polyhedron_iostream.h>
#include <iostream>
#include <fstream>
#include <GL/glut.h>

typedef CGAL::Cartesian< double > Kernel;
typedef Kernel::Vector_3  Vector;
typedef Kernel::Point_3  Point;
typedef CGAL::Polyhedron_3< Kernel > Polyhedron;

typedef Polyhedron::Vertex  Vertex;
typedef Polyhedron::Vertex_handle  Vertex_handle;
typedef Polyhedron::Vertex_iterator  Vertex_iterator;
typedef Polyhedron::Halfedge_handle  Halfedge_handle;
typedef Polyhedron::Edge_iterator  Edge_iterator;
typedef Polyhedron::Facet_iterator  Facet_iterator;
typedef Polyhedron::Halfedge_around_facet_circulator Halfedge_facet_circulator;

GLdouble *vertices = NULL;
GLdouble *normals  = NULL;
GLuint *faces      = NULL;
int nf = 0;
using namespace std;
Polyhedron mesh;
void display()
{
    glShadeModel(GL_SMOOTH);
    //init_lights();

    if (vertices == NULL || normals == NULL || faces == NULL) {
        std::cerr << "Not prepared" << std::endl;
        exit(1);
    }

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glVertexPointer(3, GL_DOUBLE, 0, vertices);
    glNormalPointer(GL_DOUBLE, 0, normals);

    glDrawElements(GL_TRIANGLES, nf * 3, GL_UNSIGNED_INT, faces);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
}

int searchVertex(Vertex *haystack, int length, Point needle)
{
    for (int i = 0; i < length; ++i) {
        if (haystack[i].point() == needle)
            return i;
    }
    throw "Vertex not found";
}

Vector facet_normal(Polyhedron::Facet_handle facet)
{
    Halfedge_facet_circulator hfc = facet->facet_begin();
    CGAL_assertion( CGAL::circulator_size(hfc) >= 3 );

    Point v[3];
    for (int i = 0; i < 3; ++i, hfc++) {
        v[i] = hfc->vertex()->point();
    }
    Vector n = cross_product( v[ 1 ] - v[ 2 ], v[ 1 ] - v[ 0 ] );
    return ( 1.0/sqrt( n.squared_length() ) ) * n;
}

void loadMesh(const char *file)
{
    ifstream ifs(file);
    if ( ifs == NULL ) {
        cerr << "Cannot open the file : " << file << endl;
        exit(1);
    }

    ifs >> mesh;

    if(!ifs || !mesh.is_valid() || mesh.empty())
    {
        std::cerr << "Error: cannot read OFF file ";
        exit(1);
    }
    //cout<<mesh<<"\n";
    mesh.normalize_border();
    if ( mesh.size_of_border_edges() != 0 ) {
        cerr << "The input object has border edges. Cannot unfold." << endl;
        exit(1);
    }

    int vsize = mesh.size_of_vertices();
    nf = mesh.size_of_facets();
    cout<<mesh;

    cout << "Vsize: " << vsize << ", nf: " << nf <<"\n";
    cout << "Moar Output";
    int i = 0;

    Vertex *v = new Vertex[vsize];
    vertices = new double[vsize*3];
    normals  = new double[vsize*3];
    faces    = new GLuint[nf*3];

    Vertex_iterator itt = mesh.vertices_begin();
    for (Vertex_iterator it = mesh.vertices_begin(); it != mesh.vertices_end(); it++, i++) {
        v[i] = *it;
        Point p = it->point();
        vertices[i*3]     = p.x();
        vertices[i*3 + 1] = p.y();
        vertices[i*3 + 2] = p.z();

        Halfedge_handle vc = it->vertex_begin();
        Vector n;
        do {
            n = n + facet_normal(vc->facet());
        } while (++vc != it->vertex_begin());
        n = ( 1.0/sqrt( n.squared_length() ) ) * n;

        normals[i*3 + 0] = n.x();
        normals[i*3 + 1] = n.y();
        normals[i*3 + 2] = n.z();
    }
    cout<<"Here";
    i = 0;
    for (Facet_iterator it = mesh.facets_begin(); it != mesh.facets_end(); ++it, ++i) {
        Halfedge_facet_circulator hfc = it->facet_begin();
        CGAL_assertion(CGAL::circulator_size(hfc) == 3);
        for (int j = 0; j < 3; ++j, ++hfc) {
            faces[i*3 + j] = searchVertex(v, vsize, hfc->vertex()->point());
        }
    }
}

int main(int argc, char *argv[])
{
    loadMesh("cube.off");
    //glutInit(&argc, argv);
    glutDisplayFunc(display);
    glutMainLoop();

    if (vertices)
        delete [] vertices;
    if (normals)
        delete [] normals;
    if (faces)
        delete [] faces;

    return 0;
}
4

1 回答 1

2

错误来自行

  Halfedge_handle vc = it->vertex_begin();

您需要将其替换为

  Polyhedron::Halfedge_around_vertex_circulator vc = it->vertex_begin();

这是遍历给定顶点的所有半边的正确方法。

请注意,您在 glut 代码中也有几个错误(没有创建窗口......)

纪尧姆

于 2014-01-27T10:01:33.820 回答