2014-01-26 26 views
0

我試圖運行此代碼以顯示3D網格輸出 出於某種奇怪的原因,輸出顯示nf和頂點數量,但在此之後出現分段錯誤;莫爾輸出線不寫入std。任何想法爲什麼?CGAL分段錯誤

#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; 
} 
+1

你有沒有試着用gdb調試呢? – BenC

回答

2

這個錯誤來自行

Halfedge_handle vc = it->vertex_begin(); 

您需要

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

至極來取代它是通過所有的halfedges事件迭代給定頂點的正確方法。

注意,你也有幾個在過剩代碼中的錯誤(沒有窗戶的創作...)

紀堯姆