mesh_with_skeleton.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <iostream>
  13. // Default settings pq2Y tell tetgen to mesh interior of triangle mesh and
  14. // to produce a graded tet mesh
  15. const static std::string DEFAULT_TETGEN_FLAGS = "pq2Y";
  16. IGL_INLINE bool igl::copyleft::tetgen::mesh_with_skeleton(
  17. const Eigen::MatrixXd & V,
  18. const Eigen::MatrixXi & F,
  19. const Eigen::MatrixXd & C,
  20. const Eigen::VectorXi & /*P*/,
  21. const Eigen::MatrixXi & BE,
  22. const Eigen::MatrixXi & CE,
  23. const int samples_per_bone,
  24. const std::string & tetgen_flags,
  25. Eigen::MatrixXd & VV,
  26. Eigen::MatrixXi & TT,
  27. Eigen::MatrixXi & FF)
  28. {
  29. using namespace Eigen;
  30. using namespace std;
  31. const string eff_tetgen_flags =
  32. (tetgen_flags.length() == 0?DEFAULT_TETGEN_FLAGS:tetgen_flags);
  33. // Collect all edges that need samples:
  34. MatrixXi BECE = cat(1,BE,CE);
  35. MatrixXd S;
  36. // Sample each edge with 10 samples. (Choice of 10 doesn't seem to matter so
  37. // much, but could under some circumstances)
  38. sample_edges(C,BECE,samples_per_bone,S);
  39. // Vertices we'll constrain tet mesh to meet
  40. MatrixXd VS = cat(1,V,S);
  41. // Use tetgen to mesh the interior of surface, this assumes surface:
  42. // * has no holes
  43. // * has no non-manifold edges or vertices
  44. // * has consistent orientation
  45. // * has no self-intersections
  46. // * has no 0-volume pieces
  47. cerr<<"tetgen begin()"<<endl;
  48. int status = tetrahedralize( VS,F,eff_tetgen_flags,VV,TT,FF);
  49. cerr<<"tetgen end()"<<endl;
  50. if(FF.rows() != F.rows())
  51. {
  52. // Issue a warning if the surface has changed
  53. cerr<<"mesh_with_skeleton: Warning: boundary faces != input faces"<<endl;
  54. }
  55. if(status != 0)
  56. {
  57. cerr<<
  58. "***************************************************************"<<endl<<
  59. "***************************************************************"<<endl<<
  60. "***************************************************************"<<endl<<
  61. "***************************************************************"<<endl<<
  62. "* mesh_with_skeleton: tetgen failed. Just meshing convex hull *"<<endl<<
  63. "***************************************************************"<<endl<<
  64. "***************************************************************"<<endl<<
  65. "***************************************************************"<<endl<<
  66. "***************************************************************"<<endl;
  67. // If meshing convex hull then use more regular mesh
  68. status = tetrahedralize(VS,F,"q1.414",VV,TT,FF);
  69. // I suppose this will fail if the skeleton is outside the mesh
  70. assert(FF.maxCoeff() < VV.rows());
  71. if(status != 0)
  72. {
  73. cerr<<"mesh_with_skeleton: tetgen failed again."<<endl;
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. IGL_INLINE bool igl::copyleft::tetgen::mesh_with_skeleton(
  80. const Eigen::MatrixXd & V,
  81. const Eigen::MatrixXi & F,
  82. const Eigen::MatrixXd & C,
  83. const Eigen::VectorXi & P,
  84. const Eigen::MatrixXi & BE,
  85. const Eigen::MatrixXi & CE,
  86. const int samples_per_bone,
  87. Eigen::MatrixXd & VV,
  88. Eigen::MatrixXi & TT,
  89. Eigen::MatrixXi & FF)
  90. {
  91. return mesh_with_skeleton(
  92. V,F,C,P,BE,CE,samples_per_bone,DEFAULT_TETGEN_FLAGS,VV,TT,FF);
  93. }
  94. #ifdef IGL_STATIC_LIBRARY
  95. // Explicit template instantiation
  96. #endif