adjacency_list.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "adjacency_list.h"
  2. #include "verbose.h"
  3. template <typename T, typename M>
  4. IGL_INLINE void igl::adjacency_list(
  5. const M & F,
  6. std::vector<std::vector<T> >& A,
  7. bool sorted)
  8. {
  9. A.clear();
  10. A.resize(F.maxCoeff()+1);
  11. // Loop over faces
  12. for(int i = 0;i<F.rows();i++)
  13. {
  14. // Loop over this face
  15. for(int j = 0;j<F.cols();j++)
  16. {
  17. // Get indices of edge: s --> d
  18. int s = F(i,j);
  19. int d = F(i,(j+1)%F.cols());
  20. A.at(s).push_back(d);
  21. A.at(d).push_back(s);
  22. }
  23. }
  24. // Remove duplicates
  25. for(int i=0; i<(int)A.size();++i)
  26. {
  27. std::sort(A[i].begin(), A[i].end());
  28. A[i].erase(std::unique(A[i].begin(), A[i].end()), A[i].end());
  29. }
  30. // If needed, sort every VV
  31. if (sorted)
  32. {
  33. // Loop over faces
  34. // for every vertex v store a set of ordered edges not incident to v that belongs to triangle incident on v.
  35. std::vector<std::vector<std::vector<int> > > SR;
  36. SR.resize(A.size());
  37. for(int i = 0;i<F.rows();i++)
  38. {
  39. // Loop over this face
  40. for(int j = 0;j<F.cols();j++)
  41. {
  42. // Get indices of edge: s --> d
  43. int s = F(i,j);
  44. int d = F(i,(j+1)%F.cols());
  45. // Get index of opposing vertex v
  46. int v = F(i,(j+2)%F.cols());
  47. std::vector<int> e(2);
  48. e[0] = d;
  49. e[1] = v;
  50. SR[s].push_back(e);
  51. }
  52. }
  53. for(int v=0; v<(int)SR.size();++v)
  54. {
  55. std::vector<int>& vv = A.at(v);
  56. std::vector<std::vector<int> >& sr = SR[v];
  57. std::vector<std::vector<int> > pn = sr;
  58. // Compute previous/next for every element in sr
  59. for(int i=0;i<(int)sr.size();++i)
  60. {
  61. int a = sr[i][0];
  62. int b = sr[i][1];
  63. // search for previous
  64. int p = -1;
  65. for(int j=0;j<(int)sr.size();++j)
  66. if(sr[j][1] == a)
  67. p = j;
  68. pn[i][0] = p;
  69. // search for next
  70. int n = -1;
  71. for(int j=0;j<(int)sr.size();++j)
  72. if(sr[j][0] == b)
  73. n = j;
  74. pn[i][1] = n;
  75. }
  76. // assume manifoldness (look for beginning of a single chain)
  77. int c = 0;
  78. for(int j=0; j<=(int)sr.size();++j)
  79. if (pn[c][0] != -1)
  80. c = pn[c][0];
  81. if (pn[c][0] == -1) // border case
  82. {
  83. // finally produce the new vv relation
  84. for(int j=0; j<(int)sr.size();++j)
  85. {
  86. vv[j] = sr[c][0];
  87. if (pn[c][1] != -1)
  88. c = pn[c][1];
  89. }
  90. vv.back() = sr[c][1];
  91. }
  92. else
  93. {
  94. // finally produce the new vv relation
  95. for(int j=0; j<(int)sr.size();++j)
  96. {
  97. vv[j] = sr[c][0];
  98. c = pn[c][1];
  99. }
  100. }
  101. }
  102. }
  103. }
  104. #ifndef IGL_HEADER_ONLY
  105. // Explicit template specialization
  106. #endif