mesh_with_skeleton.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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. const std::string eff_tetgen_flags =
  30. (tetgen_flags.length() == 0?DEFAULT_TETGEN_FLAGS:tetgen_flags);
  31. // Collect all edges that need samples:
  32. Eigen::MatrixXi BECE = cat(1,BE,CE);
  33. Eigen::MatrixXd S;
  34. // Sample each edge with 10 samples. (Choice of 10 doesn't seem to matter so
  35. // much, but could under some circumstances)
  36. sample_edges(C,BECE,samples_per_bone,S);
  37. // Vertices we'll constrain tet mesh to meet
  38. Eigen::MatrixXd VS = cat(1,V,S);
  39. // Use tetgen to mesh the interior of surface, this assumes surface:
  40. // * has no holes
  41. // * has no non-manifold edges or vertices
  42. // * has consistent orientation
  43. // * has no self-intersections
  44. // * has no 0-volume pieces
  45. int status = tetrahedralize( VS,F,eff_tetgen_flags,VV,TT,FF);
  46. if(FF.rows() != F.rows())
  47. {
  48. // Issue a warning if the surface has changed
  49. std::cerr<<"mesh_with_skeleton: Warning: boundary faces != input faces"<<std::endl;
  50. }
  51. if(status != 0)
  52. {
  53. std::cerr<<
  54. "***************************************************************"<<std::endl<<
  55. "***************************************************************"<<std::endl<<
  56. "***************************************************************"<<std::endl<<
  57. "***************************************************************"<<std::endl<<
  58. "* mesh_with_skeleton: tetgen failed. Just meshing convex hull *"<<std::endl<<
  59. "***************************************************************"<<std::endl<<
  60. "***************************************************************"<<std::endl<<
  61. "***************************************************************"<<std::endl<<
  62. "***************************************************************"<<std::endl;
  63. // If meshing convex hull then use more regular mesh
  64. status = tetrahedralize(VS,F,"q1.414",VV,TT,FF);
  65. // I suppose this will fail if the skeleton is outside the mesh
  66. assert(FF.maxCoeff() < VV.rows());
  67. if(status != 0)
  68. {
  69. std::cerr<<"mesh_with_skeleton: tetgen failed again."<<std::endl;
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. IGL_INLINE bool igl::copyleft::tetgen::mesh_with_skeleton(
  76. const Eigen::MatrixXd & V,
  77. const Eigen::MatrixXi & F,
  78. const Eigen::MatrixXd & C,
  79. const Eigen::VectorXi & P,
  80. const Eigen::MatrixXi & BE,
  81. const Eigen::MatrixXi & CE,
  82. const int samples_per_bone,
  83. Eigen::MatrixXd & VV,
  84. Eigen::MatrixXi & TT,
  85. Eigen::MatrixXi & FF)
  86. {
  87. return mesh_with_skeleton(
  88. V,F,C,P,BE,CE,samples_per_bone,DEFAULT_TETGEN_FLAGS,VV,TT,FF);
  89. }
  90. #ifdef IGL_STATIC_LIBRARY
  91. // Explicit template instantiation
  92. #endif