triangle_triangle_adjacency.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@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 "triangle_triangle_adjacency.h"
  9. #include "is_edge_manifold.h"
  10. #include "all_edges.h"
  11. #include <map>
  12. #include <algorithm>
  13. template <typename Scalar, typename Index>
  14. IGL_INLINE void igl::triangle_triangle_adjacency_preprocess(const Eigen::PlainObjectBase<Scalar>& /*V*/,
  15. const Eigen::PlainObjectBase<Index>& F,
  16. std::vector<std::vector<int> >& TTT)
  17. {
  18. for(int f=0;f<F.rows();++f)
  19. for (int i=0;i<F.cols();++i)
  20. {
  21. // v1 v2 f ei
  22. int v1 = F(f,i);
  23. int v2 = F(f,(i+1)%F.cols());
  24. if (v1 > v2) std::swap(v1,v2);
  25. std::vector<int> r(4);
  26. r[0] = v1; r[1] = v2;
  27. r[2] = f; r[3] = i;
  28. TTT.push_back(r);
  29. }
  30. std::sort(TTT.begin(),TTT.end());
  31. }
  32. // Extract the face adjacencies
  33. template <typename Index>
  34. IGL_INLINE void igl::triangle_triangle_adjacency_extractTT(const Eigen::PlainObjectBase<Index>& F,
  35. std::vector<std::vector<int> >& TTT,
  36. Eigen::PlainObjectBase<Index>& TT)
  37. {
  38. TT = Eigen::PlainObjectBase<Index>::Constant((int)(F.rows()),F.cols(),-1);
  39. for(int i=1;i<(int)TTT.size();++i)
  40. {
  41. std::vector<int>& r1 = TTT[i-1];
  42. std::vector<int>& r2 = TTT[i];
  43. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  44. {
  45. TT(r1[2],r1[3]) = r2[2];
  46. TT(r2[2],r2[3]) = r1[2];
  47. }
  48. }
  49. }
  50. // Extract the face adjacencies indices (needed for fast traversal)
  51. template <typename Index>
  52. IGL_INLINE void igl::triangle_triangle_adjacency_extractTTi(const Eigen::PlainObjectBase<Index>& F,
  53. std::vector<std::vector<int> >& TTT,
  54. Eigen::PlainObjectBase<Index>& TTi)
  55. {
  56. TTi = Eigen::PlainObjectBase<Index>::Constant((int)(F.rows()),F.cols(),-1);
  57. for(int i=1;i<(int)TTT.size();++i)
  58. {
  59. std::vector<int>& r1 = TTT[i-1];
  60. std::vector<int>& r2 = TTT[i];
  61. if ((r1[0] == r2[0]) && (r1[1] == r2[1]))
  62. {
  63. TTi(r1[2],r1[3]) = r2[3];
  64. TTi(r2[2],r2[3]) = r1[3];
  65. }
  66. }
  67. }
  68. // Compute triangle-triangle adjacency
  69. template <typename Scalar, typename Index>
  70. IGL_INLINE void igl::triangle_triangle_adjacency(const Eigen::PlainObjectBase<Scalar>& V,
  71. const Eigen::PlainObjectBase<Index>& F,
  72. Eigen::PlainObjectBase<Index>& TT)
  73. {
  74. //assert(igl::is_edge_manifold(V,F));
  75. std::vector<std::vector<int> > TTT;
  76. triangle_triangle_adjacency_preprocess(V,F,TTT);
  77. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  78. }
  79. // Compute triangle-triangle adjacency with indices
  80. template <typename Scalar, typename Index>
  81. IGL_INLINE void igl::triangle_triangle_adjacency(const Eigen::PlainObjectBase<Scalar>& V,
  82. const Eigen::PlainObjectBase<Index>& F,
  83. Eigen::PlainObjectBase<Index>& TT,
  84. Eigen::PlainObjectBase<Index>& TTi)
  85. {
  86. //assert(igl::is_edge_manifold(V,F));
  87. std::vector<std::vector<int> > TTT;
  88. triangle_triangle_adjacency_preprocess(V,F,TTT);
  89. triangle_triangle_adjacency_extractTT(F,TTT,TT);
  90. triangle_triangle_adjacency_extractTTi(F,TTT,TTi);
  91. }
  92. template <
  93. typename DerivedF,
  94. typename TTIndex,
  95. typename TTiIndex>
  96. IGL_INLINE void igl::triangle_triangle_adjacency(
  97. const Eigen::PlainObjectBase<DerivedF> & F,
  98. std::vector<std::vector<std::vector<TTIndex> > > & TT,
  99. std::vector<std::vector<std::vector<TTiIndex> > > & TTi)
  100. {
  101. using namespace Eigen;
  102. using namespace std;
  103. using namespace igl;
  104. assert(F.cols() == 3 && "Faces must be triangles");
  105. typedef typename DerivedF::Index Index;
  106. // number of faces
  107. const int m = F.rows();
  108. // All occurances of directed edges
  109. MatrixXi E;
  110. all_edges(F,E);
  111. assert(E.rows() == 3*m);
  112. // uE2E[i] --> {j,k,...} means unique edge i corresponds to face edges j and
  113. // k (where j-edge comes is the j/m edge of face j%m)
  114. map<pair<Index,Index>,vector<Index> > uE2E;
  115. for(int e = 0;e<E.rows();e++)
  116. {
  117. Index i = E(e,0);
  118. Index j = E(e,1);
  119. if(i<j)
  120. {
  121. uE2E[pair<Index,Index>(i,j)].push_back(e);
  122. }else
  123. {
  124. uE2E[pair<Index,Index>(j,i)].push_back(e);
  125. }
  126. }
  127. // E2E[i] --> {j,k,...} means face edge i corresponds to other faces edges j
  128. // and k
  129. TT.resize (m,vector<vector<Index> >(F.cols()));
  130. TTi.resize(m,vector<vector<Index> >(F.cols()));
  131. for(int e = 0;e<E.rows();e++)
  132. {
  133. const Index i = E(e,0);
  134. const Index j = E(e,1);
  135. const Index f = e%m;
  136. const Index c = e/m;
  137. const vector<Index> & N =
  138. i<j ? uE2E[pair<Index,Index>(i,j)] : uE2E[pair<Index,Index>(j,i)];
  139. for(const auto & ne : N)
  140. {
  141. const Index nf = ne%m;
  142. const Index nc = ne/m;
  143. TT[f][c].push_back(nf);
  144. TTi[f][c].push_back(nc);
  145. }
  146. }
  147. }
  148. #ifdef IGL_STATIC_LIBRARY
  149. // Explicit template specialization
  150. template void igl::triangle_triangle_adjacency<Eigen::Matrix<double, -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> >&);
  151. // generated by autoexplicit.sh
  152. template void igl::triangle_triangle_adjacency<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  153. template void igl::triangle_triangle_adjacency<Eigen::Matrix<double, -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> >&);
  154. #endif