mickelangelo / carve

Automatically exported from code.google.com/p/carve
Other
0 stars 0 forks source link

Crash in reading OFF file #54

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Dear all,

Could anyone help me to take a look at the following code to read OFF file? The 
code crashed at this line:
  carve::mesh::MeshSet<3> poly1(faces);

In addition, could anyone tell me how to write the code to write OFF file (the 
function 'write_off')?

Thanks,
Tang Laoya

/// file: test_carve_polyhedrons_2.cpp
// Begin License:
// Copyright (C) 2006-2011 Tobias Sargeant (tobias.sargeant@gmail.com).
// All rights reserved.
//
// This file is part of the Carve CSG Library (http://carve-csg.com/)
//
// This file may be used under the terms of the GNU General Public
// License version 2.0 as published by the Free Software Foundation
// and appearing in the file LICENSE.GPL2 included in the packaging of
// this file.
//
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
// End:

#include <carve/csg.hpp>
#include <carve/poly.hpp>
#include <carve/geom.hpp>

#include <vector>
#include <iostream>

void read_off(std::vector<carve::mesh::MeshSet<3>::vertex_t> *verts, 
std::vector<carve::mesh::MeshSet<3>::face_t *> *faces,char* file)
{
    FILE *fp=fopen(file,"r");
    char off[10];
    int np, nf, nv, v;
    double x,y,z;
  std::vector<carve::mesh::MeshSet<3>::vertex_t *> corners;
    fscanf(fp,"%s\n",&off);
    fscanf(fp,"%d %d\n",&np,&nf);
    for (int i=0;i<np;i++)
    {
        fscanf(fp,"%e %e %e\n",&x,&y,&z);
        verts->push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(x,y,z)));
    }
    for (int i=0;i<nf;i++)
    {
        fscanf(fp,"%d",&nv);
        for (int j=0;j<nv;j++)
        {
            fscanf(fp,"%d",&v);
            corners.push_back(&(*verts)[v]);
        }
        faces->push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));
        corners.clear();
    }
    fclose(fp);
}

void write_off(carve::mesh::MeshSet<3> poly,char *file)
{

}

int main()
{
  //create a tetrahedron
#define mytest
#ifdef mytest
  std::vector<carve::mesh::MeshSet<3>::vertex_t> verts;
  std::vector<carve::mesh::MeshSet<3>::face_t *> faces;
    char *file="E:\\Users\\tang\\tang\\mesh\\CSG\\carve-clone\\mytest\\a1.off";
    read_off(&verts,&faces,file);
  carve::mesh::MeshSet<3> poly1(faces);
    file="E:\\Users\\tang\\tang\\mesh\\CSG\\carve-clone\\mytest\\a2.off";
    read_off(&verts,&faces,file);
  carve::mesh::MeshSet<3> poly2(faces);
  carve::mesh::MeshSet<3> *is_poly = carve::csg::CSG().compute(&poly1,
                                                               &poly2,
                                                               carve::csg::CSG::A_MINUS_B);
#else
  std::vector<carve::mesh::MeshSet<3>::vertex_t> tet_verts;
  std::vector<carve::mesh::MeshSet<3>::face_t *> tet_faces;
  std::vector<carve::mesh::MeshSet<3>::vertex_t *> corners;

  tet_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(0.0, 0.0, 0.0)));
  tet_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(1.0, 0.0, 0.0)));
  tet_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(0.0, 1.0, 0.0)));
  tet_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(0.0, 0.0, 1.0)));

  corners.push_back(&tet_verts[0]);
  corners.push_back(&tet_verts[2]);
  corners.push_back(&tet_verts[1]);
  tet_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

  corners.clear();
  corners.push_back(&tet_verts[0]);
  corners.push_back(&tet_verts[1]);
  corners.push_back(&tet_verts[3]);
  tet_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

  corners.clear();
  corners.push_back(&tet_verts[0]);
  corners.push_back(&tet_verts[3]);
  corners.push_back(&tet_verts[2]);
  tet_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

  corners.clear();
  corners.push_back(&tet_verts[1]);
  corners.push_back(&tet_verts[2]);
  corners.push_back(&tet_verts[3]);
  tet_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

  carve::mesh::MeshSet<3> tetrahedron(tet_faces);

  //create a triangle
  std::vector<carve::mesh::MeshSet<3>::vertex_t> tri_verts;
  std::vector<carve::mesh::MeshSet<3>::face_t *> tri_faces;

  //Vertices
  //crashes if last coordinate set to 1e-8, but ok for 1e-7
  //tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(-0.3, 0.0, 1e-8)));
  //tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(1.0, 0.0, 1.1e-8)));
  //tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(-0.3, 1.0, 1.1e-8)));
  tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(-0.1, -0.1, 0.5)));
  tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(1.1, -0.1, 0.5)));
  tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(-0.1, 1.1, 0.5)));

  //Face
  corners.clear();
  corners.push_back(&tri_verts[0]);
  corners.push_back(&tri_verts[2]);
  corners.push_back(&tri_verts[1]);
  tri_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

//  corners.clear();
//  corners.push_back(&tri_verts[0]);
//  corners.push_back(&tri_verts[1]);
//  corners.push_back(&tri_verts[2]);
//  tri_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners));

  carve::mesh::MeshSet<3> triangle(tri_faces);

  //cut triangle with tetrahedron.
  carve::mesh::MeshSet<3> *is_poly = carve::csg::CSG().compute(&tetrahedron,
                                                               &triangle,
                                                               carve::csg::CSG::B_MINUS_A);
#endif
  // std::cout << "Tetrahedron is ... \n" << tetrahedron;
  // std::cout << "Triangle is ... \n" << triangle;
  // std::cout << "Intersection is ... \n" << *is_poly;

  return 0;
}

Original issue reported on code.google.com by tanglaoy...@gmail.com on 16 Apr 2014 at 2:57

GoogleCodeExporter commented 9 years ago
Sorry, the read_off function should be as follows:
void read_off(std::vector<carve::mesh::MeshSet<3>::vertex_t> *verts, 
std::vector<carve::mesh::MeshSet<3>::face_t *> *faces,char* file)
{
    FILE *fp=fopen(file,"r");
    char off[10];
    int np, nf, tmp, nv, v;
    double x,y,z;
  std::vector<carve::mesh::MeshSet<3>::vertex_t *> corners;
    fscanf(fp,"%s\n",&off);
    fscanf(fp,"%d %d %d\n",&np,&nf,&tmp);
    for (int i=0;i<np;i++)
    {
        fscanf(fp,"%le %le %le\n",&x,&y,&z);
        verts->push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(x,y,z)));
    }
    for (int i=0;i<nf;i++)
    {
        fscanf(fp,"%d",&nv);
        for (int j=0;j<nv;j++)
        {
            fscanf(fp,"%d",&v);
            corners.push_back(&(*verts)[v]);
        }
        faces->push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));
        corners.clear();
    }
    fclose(fp);
}

and I forgot to attach the OFF file:

/// a1.off
OFF
18 20 0
1 -2  -2
1 0   -2
1 2   -2
-2 -2 -2
-2 0  -2
-2 2  -2
1 -2   0
1 0    0
1 2    0
-2 -2  0
-2 0   0
-2 2   0
1 -2   2
1 0    2
1 2    2
-2 -2  2
-2 0   2
-2 2   2
4 0 1 7 6
4 1 2 8 7
4 6 7 13 12
4 7 8 14 13
4 2 5 11 8
4 8 11 17 14
4 4 10 11 5
4 3 9 10 4
4 9 15 16 10
4 10 16 17 11
4 0 6 9 3
4 6 12 15 9
4 0 3 4 1
4 1 4 5 2
4 12 13 16 15
4 13 14 17 16
4 6 7 10 9
4 7 8 11 10
4 7 10 16 13
4 1 4 10 7

/// a2.off
OFF
8 6 0
2 1 -1
2 1 1
2 -1 1
2 -1 -1
-1 1 -1
-1 1 1
-1 -1 1
-1 -1 -1
4 0 1 2 3
4 4 7 6 5
4 0 4 5 1
4 1 5 6 2
4 3 2 6 7
4 0 3 7 4

Thanks,
Tang Laoya

Original comment by tanglaoy...@gmail.com on 16 Apr 2014 at 3:28

GoogleCodeExporter commented 9 years ago
Sorry again. 

Now the crash problem is solved. But the following error message displayed:
uncontained hole loop does not share vertices with any face loop!

Could anyone help me to take a look at it?

In addition, could anyone tell me how to write the 'write_off' function?

Thanks,
Tang Laoya

// Begin License:
// Copyright (C) 2006-2011 Tobias Sargeant (tobias.sargeant@gmail.com).
// All rights reserved.
//
// This file is part of the Carve CSG Library (http://carve-csg.com/)
//
// This file may be used under the terms of the GNU General Public
// License version 2.0 as published by the Free Software Foundation
// and appearing in the file LICENSE.GPL2 included in the packaging of
// this file.
//
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
// End:

#include <carve/csg.hpp>
#include <carve/poly.hpp>
#include <carve/geom.hpp>

#include <vector>
#include <iostream>

void read_off(std::vector<carve::mesh::MeshSet<3>::vertex_t> *verts, 
std::vector<carve::mesh::MeshSet<3>::face_t *> *faces,char* file)
{
    FILE *fp=fopen(file,"r");
    char off[10];
    int np, nf, tmp, nv, v;
    double x,y,z;
  std::vector<carve::mesh::MeshSet<3>::vertex_t *> corners;
    fscanf(fp,"%s\n",&off);
    fscanf(fp,"%d %d %d\n",&np,&nf,&tmp);
    for (int i=0;i<np;i++)
    {
        fscanf(fp,"%le %le %le\n",&x,&y,&z);
        verts->push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(x,y,z)));
    }
    for (int i=0;i<nf;i++)
    {
        fscanf(fp,"%d",&nv);
        for (int j=0;j<nv;j++)
        {
            fscanf(fp,"%d",&v);
            corners.push_back(&(*verts)[v]);
        }
        faces->push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));
        corners.clear();
    }
    fclose(fp);
}

void write_off(carve::mesh::MeshSet<3> poly,char *file)
{

}

int main()
{
  //create a tetrahedron
#define mytest
#ifdef mytest
  std::vector<carve::mesh::MeshSet<3>::vertex_t> verts;
  std::vector<carve::mesh::MeshSet<3>::face_t *> faces;
    char *file="E:\\Users\\tang\\tang\\mesh\\CSG\\carve-clone\\mytest\\a1.off";
    read_off(&verts,&faces,file);
  carve::mesh::MeshSet<3> poly1(faces);
    verts.clear();
    faces.clear();
    file="E:\\Users\\tang\\tang\\mesh\\CSG\\carve-clone\\mytest\\a2.off";
    read_off(&verts,&faces,file);
  carve::mesh::MeshSet<3> poly2(faces);
  carve::mesh::MeshSet<3> *is_poly = carve::csg::CSG().compute(&poly1,
                                                               &poly2,
                                                               carve::csg::CSG::A_MINUS_B);
    verts.clear();
    faces.clear();
#else
  std::vector<carve::mesh::MeshSet<3>::vertex_t> tet_verts;
  std::vector<carve::mesh::MeshSet<3>::face_t *> tet_faces;
  std::vector<carve::mesh::MeshSet<3>::vertex_t *> corners;

  tet_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(0.0, 0.0, 0.0)));
  tet_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(1.0, 0.0, 0.0)));
  tet_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(0.0, 1.0, 0.0)));
  tet_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(0.0, 0.0, 1.0)));

  corners.push_back(&tet_verts[0]);
  corners.push_back(&tet_verts[2]);
  corners.push_back(&tet_verts[1]);
  tet_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

  corners.clear();
  corners.push_back(&tet_verts[0]);
  corners.push_back(&tet_verts[1]);
  corners.push_back(&tet_verts[3]);
  tet_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

  corners.clear();
  corners.push_back(&tet_verts[0]);
  corners.push_back(&tet_verts[3]);
  corners.push_back(&tet_verts[2]);
  tet_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

  corners.clear();
  corners.push_back(&tet_verts[1]);
  corners.push_back(&tet_verts[2]);
  corners.push_back(&tet_verts[3]);
  tet_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

  carve::mesh::MeshSet<3> tetrahedron(tet_faces);

  //create a triangle
  std::vector<carve::mesh::MeshSet<3>::vertex_t> tri_verts;
  std::vector<carve::mesh::MeshSet<3>::face_t *> tri_faces;

  //Vertices
  //crashes if last coordinate set to 1e-8, but ok for 1e-7
  //tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(-0.3, 0.0, 1e-8)));
  //tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(1.0, 0.0, 1.1e-8)));
  //tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(-0.3, 1.0, 1.1e-8)));
  tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(-0.1, -0.1, 0.5)));
  tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(1.1, -0.1, 0.5)));
  tri_verts.push_back(carve::mesh::MeshSet<3>::vertex_t(carve::geom::VECTOR(-0.1, 1.1, 0.5)));

  //Face
  corners.clear();
  corners.push_back(&tri_verts[0]);
  corners.push_back(&tri_verts[2]);
  corners.push_back(&tri_verts[1]);
  tri_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners.begin(), corners.end()));

//  corners.clear();
//  corners.push_back(&tri_verts[0]);
//  corners.push_back(&tri_verts[1]);
//  corners.push_back(&tri_verts[2]);
//  tri_faces.push_back(new carve::mesh::MeshSet<3>::face_t(corners));

  carve::mesh::MeshSet<3> triangle(tri_faces);

  //cut triangle with tetrahedron.
  carve::mesh::MeshSet<3> *is_poly = carve::csg::CSG().compute(&tetrahedron,
                                                               &triangle,
                                                               carve::csg::CSG::B_MINUS_A);
#endif
  // std::cout << "Tetrahedron is ... \n" << tetrahedron;
  // std::cout << "Triangle is ... \n" << triangle;
  // std::cout << "Intersection is ... \n" << *is_poly;

  return 0;
}

Original comment by tanglaoy...@gmail.com on 16 Apr 2014 at 3:42

GoogleCodeExporter commented 9 years ago
Problem solved after got latest version.

Thanks

Original comment by tanglaoy...@gmail.com on 17 Apr 2014 at 5:08