tetgenio_to_tetmesh.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. TM.resize(out.numberofpoints);
  79. for (int i = 0; i < out.numberofpoints; ++i)
  80. {
  81. TM(i) = out.pointmarkerlist[i];
  82. }
  83. }
  84. if(out.tetrahedronattributelist)
  85. {
  86. R.resize(out.numberoftetrahedra);
  87. std::unordered_map<REAL, REAL> hashUniqueRegions;
  88. for(int i = 0; i < out.numberoftetrahedra; i++)
  89. {
  90. R(i) = out.tetrahedronattributelist[i];
  91. hashUniqueRegions[R(i)] = i;
  92. }
  93. // extract region marks
  94. num_regions = hashUniqueRegions.size();
  95. }else
  96. {
  97. num_regions = 0;
  98. }
  99. // extract neighbor list
  100. if(out.neighborlist)
  101. {
  102. N.resize(out.numberoftetrahedra, 4);
  103. for (int i = 0; i < out.numberoftetrahedra; i++)
  104. {
  105. for (int j = 0; j < 4; j++)
  106. {
  107. N(i,j) = out.neighborlist[i * 4 + j];
  108. }
  109. }
  110. }
  111. // extract point 2 tetrahedron list
  112. if(out.point2tetlist)
  113. {
  114. PT.resize(out.numberofpoints);
  115. for (int i = 0; i < out.numberofpoints; i++)
  116. {
  117. PT(i) = out.point2tetlist[i];
  118. }
  119. }
  120. //extract face to tetrahedron list
  121. if(out.face2tetlist)
  122. {
  123. FT.resize(out.numberoftrifaces,2);
  124. int triface;
  125. for (int i = 0; i < out.numberoftrifaces; i++)
  126. {
  127. for (int j = 0; j < 2; j++)
  128. {
  129. FT(i,j) = out.face2tetlist[i * 2 + j];
  130. }
  131. }
  132. }
  133. return true;
  134. }
  135. #ifdef IGL_STATIC_LIBRARY
  136. // Explicit template instantiation
  137. // generated by autoexplicit.sh
  138. 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&);
  139. #endif