0
3D 메쉬 출력을 표시하기 위해이 코드를 실행하려고했습니다. 이상한 이유로 인해, nf 및 정점 수에 대한 출력이 표시되지만 이후에는 세그멘테이션 오류가 발생합니다. Moar 출력 행은 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;
}
당신이 GDB로 디버깅을 시도 했습니까? – BenC