libigl / tetgen

This is a mirror of the latest stable version of Tetgen.
Other
90 stars 41 forks source link

Becomes extremely slow when using tetgen::tetrahedralize #11

Closed yutanoma closed 11 months ago

yutanoma commented 11 months ago

Hi! I came across with a bug (?) that it becomes extremely slow when you call the igl::copyleft::tetgen::tetrahedralize function:

    // takes 1684ms
    Eigen::MatrixXd T_V;
    Eigen::MatrixXi _, __;
    std::string flags = "S0cQ";
    igl::copyleft::tetgen::tetrahedralize(V, _, flags, T_V, T, __);

whereas it is extremely faster when you directly call tetgen:

    // takes 49ms
    tetgenio in, out;

    in.firstnumber = 0;

    in.numberofpoints = V.rows();
    in.pointlist = new REAL[in.numberofpoints * 3];
    // loop over points
    for(int i = 0; i < V.rows(); i++)
    {
      in.pointlist[i*3+0] = V(i, 0);
      in.pointlist[i*3+1] = V(i, 1);
      in.pointlist[i*3+2] = V(i, 2);
    }
    in.numberoffacets = 0;
    in.facetlist = new tetgenio::facet[in.numberoffacets];
    in.facetmarkerlist = new int[in.numberoffacets];

    char* flags = "S0cQ";

    tetrahedralize(flags, &in, &out);

    assert(out.numberofpoints == V.rows());
    assert(out.numberofcorners == 4);

    T.resize(out.numberoftetrahedra, 4);
    int min_index = 1e7;
    int max_index = -1e7;
    // loop over tetrahedra
    for(int i = 0; i < out.numberoftetrahedra; i++)
    {
      for(int j = 0; j<out.numberofcorners; j++)
      {
        int index = out.tetrahedronlist[i * out.numberofcorners + j];
        T(i, j) = index;
        min_index = (min_index > index ? index : min_index);
        max_index = (max_index < index ? index : max_index);
      }
    }
alecjacobson commented 11 months ago

Would need your inputs to confirm. Are you compiling in release with NDEBUG defined?

yutanoma commented 11 months ago

No, I compiled with CMAKE_BUILD_TYPE=Debug.

I now also tried CMAKE_BUILD_TYPE=Release. For the same V matrix with 1683 rows, when using igl::copyleft::tetgen::tetrahedralize it took 40ms, whereas when calling directly it took 5ms. The input of the V matrix is here: v_matrix.txt

yutanoma commented 11 months ago

and when built under CMAKE_BUILD_TYPE=Release I measured the time to process each steps in igl::copyleft::tetgen::tetrahedralize. I found out that it was boundary_facets(TT, TF) that makes the slight difference between the two implementations above.

matrix_to_list: 0ms
matrix_to_list: 0ms
mesh_to_tetgenio: 0ms
tetrahedralize: 6ms
tetgenio_to_tetmesh: 0ms
boundary_facets: 8ms

I think this is not a bug after all, thank you.