mesh_with_skeleton.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <igl/sample_edges.h>
  10. #include <igl/cat.h>
  11. #include <igl/tetgen/tetrahedralize.h>
  12. #include <igl/writeOFF.h>
  13. #include <iostream>
  14. // Default settings pq2Y tell tetgen to mesh interior of triangle mesh and
  15. // to produce a graded tet mesh
  16. const static std::string DEFAULT_TETGEN_FLAGS = "pq2Y";
  17. bool igl::mesh_with_skeleton(
  18. const Eigen::MatrixXd & V,
  19. const Eigen::MatrixXi & F,
  20. const Eigen::MatrixXd & C,
  21. const Eigen::VectorXi & /*P*/,
  22. const Eigen::MatrixXi & BE,
  23. const Eigen::MatrixXi & CE,
  24. const int samples_per_bone,
  25. const std::string & tetgen_flags,
  26. Eigen::MatrixXd & VV,
  27. Eigen::MatrixXi & TT,
  28. Eigen::MatrixXi & FF)
  29. {
  30. using namespace Eigen;
  31. using namespace igl;
  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. // Boundary faces
  44. MatrixXi BF;
  45. // Use tetgen to mesh the interior of surface, this assumes surface:
  46. // * has no holes
  47. // * has no non-manifold edges or vertices
  48. // * has consistent orientation
  49. // * has no self-intersections
  50. // * has no 0-volume pieces
  51. //writeOFF("mesh_with_skeleton.off",VS,F);
  52. cerr<<"tetgen begin()"<<endl;
  53. int status = tetrahedralize( VS,F,eff_tetgen_flags,VV,TT,FF);
  54. cerr<<"tetgen end()"<<endl;
  55. if(FF.rows() != F.rows())
  56. {
  57. // Issue a warning if the surface has changed
  58. cerr<<"mesh_with_skeleton: Warning: boundary faces != input faces"<<endl;
  59. }
  60. if(status != 0)
  61. {
  62. cerr<<
  63. "***************************************************************"<<endl<<
  64. "***************************************************************"<<endl<<
  65. "***************************************************************"<<endl<<
  66. "***************************************************************"<<endl<<
  67. "* mesh_with_skeleton: tetgen failed. Just meshing convex hull *"<<endl<<
  68. "***************************************************************"<<endl<<
  69. "***************************************************************"<<endl<<
  70. "***************************************************************"<<endl<<
  71. "***************************************************************"<<endl;
  72. // If meshing convex hull then use more regular mesh
  73. status = tetrahedralize(VS,F,"q1.414",VV,TT,FF);
  74. // I suppose this will fail if the skeleton is outside the mesh
  75. assert(FF.maxCoeff() < VV.rows());
  76. if(status != 0)
  77. {
  78. cerr<<"mesh_with_skeleton: tetgen failed again."<<endl;
  79. return false;
  80. }
  81. }
  82. return true;
  83. }
  84. bool igl::mesh_with_skeleton(
  85. const Eigen::MatrixXd & V,
  86. const Eigen::MatrixXi & F,
  87. const Eigen::MatrixXd & C,
  88. const Eigen::VectorXi & P,
  89. const Eigen::MatrixXi & BE,
  90. const Eigen::MatrixXi & CE,
  91. const int samples_per_bone,
  92. Eigen::MatrixXd & VV,
  93. Eigen::MatrixXi & TT,
  94. Eigen::MatrixXi & FF)
  95. {
  96. return igl::mesh_with_skeleton(
  97. V,F,C,P,BE,CE,samples_per_bone,DEFAULT_TETGEN_FLAGS,VV,TT,FF);
  98. }
  99. #ifndef IGL_HEADER_ONLY
  100. // Explicit template instanciation
  101. #endif