tetgenio_to_tetmesh.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "tetgenio_to_tetmesh.h"
  9. // IGL includes
  10. #include "../../list_to_matrix.h"
  11. // STL includes
  12. #include <iostream>
  13. template <
  14. typename DerivedV,
  15. typename DerivedT,
  16. typename DerivedF,
  17. typename DerivedTM,
  18. typename DerivedR,
  19. typename DerivedN,
  20. typename DerivedPT,
  21. typename DerivedFT>
  22. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  23. const tetgenio & out,
  24. Eigen::PlainObjectBase<DerivedV>& V,
  25. Eigen::PlainObjectBase<DerivedT>& T,
  26. Eigen::PlainObjectBase<DerivedF>& F,
  27. Eigen::PlainObjectBase<DerivedTM>& TM,
  28. Eigen::PlainObjectBase<DerivedR>& R,
  29. Eigen::PlainObjectBase<DerivedN>& N,
  30. Eigen::PlainObjectBase<DerivedPT>& PT,
  31. Eigen::PlainObjectBase<DerivedFT>& FT,
  32. int & num_regions)
  33. {
  34. // process points
  35. if(out.pointlist == NULL)
  36. {
  37. std::cerr<<"^tetgenio_to_tetmesh Error: point list is NULL\n"<<std::endl;
  38. return false;
  39. }
  40. V.resize(out.numberofpoints,3);
  41. // loop over points
  42. for(int i = 0;i < out.numberofpoints; i++)
  43. {
  44. V(i,0) = out.pointlist[i*3+0];
  45. V(i,1) = out.pointlist[i*3+1];
  46. V(i,2) = out.pointlist[i*3+2];
  47. }
  48. // process tets
  49. if(out.tetrahedronlist == NULL)
  50. {
  51. std::cerr<<"^tetgenio_to_tetmesh Error: tet list is NULL\n"<<std::endl;
  52. return false;
  53. }
  54. // When would this not be 4?
  55. assert(out.numberofcorners == 4);
  56. T.resize(out.numberoftetrahedra,out.numberofcorners);
  57. // loop over tetrahedra
  58. for(int i = 0; i < out.numberoftetrahedra; i++)
  59. {
  60. for(int j = 0; j<out.numberofcorners; j++)
  61. {
  62. T(i,j) = out.tetrahedronlist[i * out.numberofcorners + j];
  63. }
  64. }
  65. assert(T.maxCoeff() >= 0);
  66. assert(T.minCoeff() >= 0);
  67. assert(T.maxCoeff() < V.rows());
  68. F.resize(out.numberoftrifaces,3);
  69. // loop over tetrahedra
  70. for(int i = 0; i < out.numberoftrifaces; i++)
  71. {
  72. F(i,0) = out.trifacelist[i * 3 + 0];
  73. F(i,1) = out.trifacelist[i * 3 + 1];
  74. F(i,2) = out.trifacelist[i * 3 + 2];
  75. }
  76. if(out.pointmarkerlist)
  77. {
  78. assert(out.numberofpoints == out.numberofpointmarkers);
  79. TM.resize(out.numberofpoints);
  80. for (int i = 0; i < out.numberofpoints; ++i)
  81. {
  82. TM(i) = out.pointmarkerlist[i];
  83. }
  84. }
  85. if(out.tetrahedronattributelist)
  86. {
  87. R.resize(out.numberoftetrahedra);
  88. std::unordered_map<REAL, REAL> hashUniqueRegions;
  89. for(int i = 0; i < out.numberoftetrahedra; i++)
  90. {
  91. R(i) = out.tetrahedronattributelist[i];
  92. hashUniqueRegions[R(i)] = i;
  93. }
  94. // extract region marks
  95. num_regions = hashUniqueRegions.size();
  96. }else
  97. {
  98. num_regions = 0;
  99. }
  100. // extract neighbor list
  101. if(out.neighborlist)
  102. {
  103. N.resize(out.numberoftetrahedra, 4);
  104. for (int i = 0; i < out.numberoftetrahedra; i++)
  105. {
  106. for (int j = 0; j < 4; j++)
  107. {
  108. N(i,j) = out.neighborlist[i * 4 + j];
  109. }
  110. }
  111. }
  112. // extract point 2 tetrahedron list
  113. if(out.point2tetlist)
  114. {
  115. PT.resize(out.numberofpoints);
  116. for (int i = 0; i < out.numberofpoints; i++)
  117. {
  118. PT(i) = out.point2tetlist[i];
  119. }
  120. }
  121. //extract face to tetrahedron list
  122. if(out.face2tetlist)
  123. {
  124. FT.resize(out.numberoftrifaces,2);
  125. int triface;
  126. for (int i = 0; i < out.numberoftrifaces; i++)
  127. {
  128. for (int j = 0; j < 2; j++)
  129. {
  130. FT(i,j) = out.face2tetlist[i * 2 + j];
  131. }
  132. }
  133. }
  134. return true;
  135. }
  136. #ifdef IGL_STATIC_LIBRARY
  137. // Explicit template instantiation
  138. // generated by autoexplicit.sh
  139. template bool igl::copyleft::tetgen::tetgenio_to_tetmesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>>(tetgenio const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1>>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1>>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1>>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1>>&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>>&, int&);
  140. #endif