mesh_with_skeleton.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "mesh_with_skeleton.h"
  2. #include <igl/sample_edges.h>
  3. #include <igl/cat.h>
  4. #include <igl/tetgen/tetrahedralize.h>
  5. #include <igl/writeOFF.h>
  6. #include <iostream>
  7. bool igl::mesh_with_skeleton(
  8. const Eigen::MatrixXd & V,
  9. const Eigen::MatrixXi & F,
  10. const Eigen::MatrixXd & C,
  11. const Eigen::VectorXi & /*P*/,
  12. const Eigen::MatrixXi & BE,
  13. const Eigen::MatrixXi & CE,
  14. const int samples_per_bone,
  15. Eigen::MatrixXd & VV,
  16. Eigen::MatrixXi & TT,
  17. Eigen::MatrixXi & FF)
  18. {
  19. using namespace Eigen;
  20. using namespace igl;
  21. using namespace std;
  22. // Collect all edges that need samples:
  23. MatrixXi BECE = cat(1,BE,CE);
  24. MatrixXd S;
  25. // Sample each edge with 10 samples. (Choice of 10 doesn't seem to matter so
  26. // much, but could under some circumstances)
  27. sample_edges(C,BECE,samples_per_bone,S);
  28. // Vertices we'll constrain tet mesh to meet
  29. MatrixXd VS = cat(1,V,S);
  30. // Boundary faces
  31. MatrixXi BF;
  32. // Use tetgen to mesh the interior of surface, this assumes surface:
  33. // * has no holes
  34. // * has no non-manifold edges or vertices
  35. // * has consistent orientation
  36. // * has no self-intersections
  37. // * has no 0-volume pieces
  38. // Default settings pq100 tell tetgen to mesh interior of triangle mesh and
  39. // to produce a graded tet mesh
  40. //writeOFF("mesh_with_skeleton.off",VS,F);
  41. cerr<<"tetgen begin()"<<endl;
  42. int status = tetrahedralize( VS,F,"pq100Y",VV,TT,FF);
  43. cerr<<"tetgen end()"<<endl;
  44. if(FF.rows() != F.rows())
  45. {
  46. // Issue a warning if the surface has changed
  47. cerr<<"mesh_with_skeleton: Warning: boundary faces != input faces"<<endl;
  48. }
  49. if(status != 0)
  50. {
  51. cerr<<
  52. "***************************************************************"<<endl<<
  53. "***************************************************************"<<endl<<
  54. "***************************************************************"<<endl<<
  55. "***************************************************************"<<endl<<
  56. "* mesh_with_skeleton: tetgen failed. Just meshing convex hull *"<<endl<<
  57. "***************************************************************"<<endl<<
  58. "***************************************************************"<<endl<<
  59. "***************************************************************"<<endl<<
  60. "***************************************************************"<<endl;
  61. // If meshing convex hull then use more regular mesh
  62. status = tetrahedralize(VS,F,"q1.414",VV,TT,FF);
  63. // I suppose this will fail if the skeleton is outside the mesh
  64. assert(FF.maxCoeff() < VV.rows());
  65. if(status != 0)
  66. {
  67. cerr<<"mesh_with_skeleton: tetgen failed again."<<endl;
  68. return false;
  69. }
  70. }
  71. return true;
  72. }