mesh_with_skeleton.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "mesh_with_skeleton.h"
  9. #include "tetrahedralize.h"
  10. #include "../../sample_edges.h"
  11. #include "../../cat.h"
  12. #include "../../writeOFF.h"
  13. #include "../../writeOBJ.h"
  14. #include <iostream>
  15. // Default settings pq2Y tell tetgen to mesh interior of triangle mesh and
  16. // to produce a graded tet mesh
  17. const static std::string DEFAULT_TETGEN_FLAGS = "pq2Y";
  18. IGL_INLINE bool igl::copyleft::tetgen::mesh_with_skeleton(
  19. const Eigen::MatrixXd & V,
  20. const Eigen::MatrixXi & F,
  21. const Eigen::MatrixXd & C,
  22. const Eigen::VectorXi & /*P*/,
  23. const Eigen::MatrixXi & BE,
  24. const Eigen::MatrixXi & CE,
  25. const int samples_per_bone,
  26. const std::string & tetgen_flags,
  27. Eigen::MatrixXd & VV,
  28. Eigen::MatrixXi & TT,
  29. Eigen::MatrixXi & FF)
  30. {
  31. using namespace Eigen;
  32. using namespace std;
  33. const string eff_tetgen_flags =
  34. (tetgen_flags.length() == 0?DEFAULT_TETGEN_FLAGS:tetgen_flags);
  35. // Collect all edges that need samples:
  36. MatrixXi BECE = cat(1,BE,CE);
  37. MatrixXd S;
  38. // Sample each edge with 10 samples. (Choice of 10 doesn't seem to matter so
  39. // much, but could under some circumstances)
  40. sample_edges(C,BECE,samples_per_bone,S);
  41. // Vertices we'll constrain tet mesh to meet
  42. MatrixXd VS = cat(1,V,S);
  43. // Use tetgen to mesh the interior of surface, this assumes surface:
  44. // * has no holes
  45. // * has no non-manifold edges or vertices
  46. // * has consistent orientation
  47. // * has no self-intersections
  48. // * has no 0-volume pieces
  49. //writeOBJ("mesh_with_skeleton.obj",VS,F);
  50. cerr<<"tetgen begin()"<<endl;
  51. int status = tetrahedralize( VS,F,eff_tetgen_flags,VV,TT,FF);
  52. cerr<<"tetgen end()"<<endl;
  53. if(FF.rows() != F.rows())
  54. {
  55. // Issue a warning if the surface has changed
  56. cerr<<"mesh_with_skeleton: Warning: boundary faces != input faces"<<endl;
  57. }
  58. if(status != 0)
  59. {
  60. cerr<<
  61. "***************************************************************"<<endl<<
  62. "***************************************************************"<<endl<<
  63. "***************************************************************"<<endl<<
  64. "***************************************************************"<<endl<<
  65. "* mesh_with_skeleton: tetgen failed. Just meshing convex hull *"<<endl<<
  66. "***************************************************************"<<endl<<
  67. "***************************************************************"<<endl<<
  68. "***************************************************************"<<endl<<
  69. "***************************************************************"<<endl;
  70. // If meshing convex hull then use more regular mesh
  71. status = tetrahedralize(VS,F,"q1.414",VV,TT,FF);
  72. // I suppose this will fail if the skeleton is outside the mesh
  73. assert(FF.maxCoeff() < VV.rows());
  74. if(status != 0)
  75. {
  76. cerr<<"mesh_with_skeleton: tetgen failed again."<<endl;
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. IGL_INLINE bool igl::copyleft::tetgen::mesh_with_skeleton(
  83. const Eigen::MatrixXd & V,
  84. const Eigen::MatrixXi & F,
  85. const Eigen::MatrixXd & C,
  86. const Eigen::VectorXi & P,
  87. const Eigen::MatrixXi & BE,
  88. const Eigen::MatrixXi & CE,
  89. const int samples_per_bone,
  90. Eigen::MatrixXd & VV,
  91. Eigen::MatrixXi & TT,
  92. Eigen::MatrixXi & FF)
  93. {
  94. return mesh_with_skeleton(
  95. V,F,C,P,BE,CE,samples_per_bone,DEFAULT_TETGEN_FLAGS,VV,TT,FF);
  96. }
  97. #ifdef IGL_STATIC_LIBRARY
  98. // Explicit template specialization
  99. #endif