tt.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "tt.h"
  2. #include "is_manifold.h"
  3. #include <algorithm>
  4. template <typename DerivedV, typename DerivedF>
  5. IGL_INLINE void igl::tt_preprocess(const Eigen::PlainObjectBase<DerivedV>& /*V*/,
  6. const Eigen::PlainObjectBase<DerivedF>& F,
  7. std::vector<std::vector<int> >& TTT)
  8. {
  9. for(int f=0;f<F.rows();++f)
  10. for (int i=0;i<F.cols();++i)
  11. {
  12. // v1 v2 f ei
  13. int v1 = F(f,i);
  14. int v2 = F(f,(i+1)%F.cols());
  15. if (v1 > v2) std::swap(v1,v2);
  16. std::vector<int> r(4);
  17. r[0] = v1; r[1] = v2;
  18. r[2] = f; r[3] = i;
  19. TTT.push_back(r);
  20. }
  21. std::sort(TTT.begin(),TTT.end());
  22. }
  23. // Extract the face adjacencies
  24. template <typename DerivedF, typename DerivedTT>
  25. IGL_INLINE void igl::tt_extractTT(const Eigen::PlainObjectBase<DerivedF>& F,
  26. std::vector<std::vector<int> >& TTT,
  27. Eigen::PlainObjectBase<DerivedTT>& TT)
  28. {
  29. TT = Eigen::PlainObjectBase<DerivedTT>::Constant((int)(F.rows()),F.cols(),-1);
  30. for(int i=1;i<(int)TTT.size();++i)
  31. {
  32. std::vector<int>& r1 = TTT[i-1];
  33. std::vector<int>& r2 = TTT[i];
  34. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  35. {
  36. TT(r1[2],r1[3]) = r2[2];
  37. TT(r2[2],r2[3]) = r1[2];
  38. }
  39. }
  40. }
  41. // Extract the face adjacencies indices (needed for fast traversal)
  42. template <typename DerivedF, typename DerivedTT>
  43. IGL_INLINE void igl::tt_extractTTi(const Eigen::PlainObjectBase<DerivedF>& F,
  44. std::vector<std::vector<int> >& TTT,
  45. Eigen::PlainObjectBase<DerivedTT>& TTi)
  46. {
  47. TTi = Eigen::PlainObjectBase<DerivedTT>::Constant((int)(F.rows()),F.cols(),-1);
  48. for(int i=1;i<(int)TTT.size();++i)
  49. {
  50. std::vector<int>& r1 = TTT[i-1];
  51. std::vector<int>& r2 = TTT[i];
  52. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  53. {
  54. TTi(r1[2],r1[3]) = r2[3];
  55. TTi(r2[2],r2[3]) = r1[3];
  56. }
  57. }
  58. }
  59. // Compute triangle-triangle adjacency
  60. template <typename DerivedV, typename DerivedF, typename DerivedTT>
  61. IGL_INLINE void igl::tt(const Eigen::PlainObjectBase<DerivedV>& V,
  62. const Eigen::PlainObjectBase<DerivedF>& F,
  63. Eigen::PlainObjectBase<DerivedTT>& TT)
  64. {
  65. assert(igl::is_manifold(V,F));
  66. std::vector<std::vector<int> > TTT;
  67. tt_preprocess(V,F,TTT);
  68. tt_extractTT(F,TTT,TT);
  69. }
  70. // Compute triangle-triangle adjacency with indices
  71. template <typename DerivedV, typename DerivedF, typename DerivedTT>
  72. IGL_INLINE void igl::tt(const Eigen::PlainObjectBase<DerivedV>& V,
  73. const Eigen::PlainObjectBase<DerivedF>& F,
  74. Eigen::PlainObjectBase<DerivedTT>& TT,
  75. Eigen::PlainObjectBase<DerivedTT>& TTi)
  76. {
  77. assert(igl::is_manifold(V,F));
  78. std::vector<std::vector<int> > TTT;
  79. tt_preprocess(V,F,TTT);
  80. tt_extractTT(F,TTT,TT);
  81. tt_extractTTi(F,TTT,TTi);
  82. }
  83. #ifndef IGL_HEADER_ONLY
  84. // Explicit template specialization
  85. // generated by autoexplicit.sh
  86. template void igl::tt<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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  87. template void igl::tt<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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  88. #endif