exterior_edges.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "exterior_edges.h"
  2. #include "all_edges.h"
  3. #include <cassert>
  4. #include <unordered_map>
  5. #include <utility>
  6. //template <typename T> inline int sgn(T val) {
  7. // return (T(0) < val) - (val < T(0));
  8. //}
  9. //static void mod2(std::pair<const std::pair<const int, const int>, int>& p)
  10. //{
  11. // using namespace std;
  12. // // Be sure that sign of mod matches sign of argument
  13. // p.second = p.second%2 ? sgn(p.second) : 0;
  14. //}
  15. //// http://stackoverflow.com/a/5517869/148668
  16. //struct Compare
  17. //{
  18. // int i;
  19. // Compare(const int& i) : i(i) {}
  20. //};
  21. //bool operator==(const std::pair<std::pair<const int, const int>,int>&p, const Compare& c)
  22. //{
  23. // return c.i == p.second;
  24. //}
  25. //bool operator==(const Compare& c, const std::pair<std::pair<const int, const int>, int> &p)
  26. //{
  27. // return c.i == p.second;
  28. //}
  29. IGL_INLINE void igl::exterior_edges(
  30. const Eigen::MatrixXi & F,
  31. Eigen::MatrixXi & E)
  32. {
  33. using namespace Eigen;
  34. using namespace std;
  35. assert(F.cols() == 3);
  36. MatrixXi all_E;
  37. all_edges(F,all_E);
  38. long int n = F.maxCoeff()+1;
  39. int m = F.minCoeff();
  40. const auto & compress = [n,m](const int i, const int j)->long int
  41. {
  42. return n*(i-m)+(j-m);
  43. };
  44. const auto & decompress = [n,m](const long int l,int & i, int & j)
  45. {
  46. i = (l / n) + m;
  47. j = (l % n) + m;
  48. };
  49. // Count occurances looking only at pairs (i,j) where i<j, so we count and
  50. // edge i-->j as +1 and j-->i as -1
  51. unordered_map<long int,int> C;
  52. // Loop over edges
  53. for(int e = 0;e<all_E.rows();e++)
  54. {
  55. int i = all_E(e,0);
  56. int j = all_E(e,1);
  57. if(i<j)
  58. {
  59. // Forward direction --> +1
  60. C[compress(i,j)]++;
  61. }else
  62. {
  63. // Backward direction --> -1
  64. C[compress(j,i)]--;
  65. }
  66. }
  67. // Q: Why mod off factors of 2? +1/-1 already takes care of interior edges?
  68. //// Mod off any factors of 2
  69. //for_each(C.begin(),C.end(),mod2);
  70. //int zeros = (int) count(C.begin(),C.end(),Compare(0));
  71. //E.resize(C.size() - zeros,2);
  72. E.resize(all_E.rows(),all_E.cols());
  73. int e = 0;
  74. // Find all edges with -1 or 1 occurances, flipping direction for -1
  75. for(const auto & cit : C)
  76. {
  77. int i,j;
  78. if(cit.second > 0)
  79. {
  80. decompress(cit.first,i,j);
  81. } else if(cit.second < 0)
  82. {
  83. decompress(cit.first,j,i);
  84. } else if(cit.second == 0)
  85. {
  86. continue;
  87. }
  88. for(int k = 0;k<abs(cit.second);k++)
  89. {
  90. E(e,0) = i;
  91. E(e,1) = j;
  92. e++;
  93. }
  94. }
  95. E.conservativeResize(e,2);
  96. assert(e == E.rows());
  97. }
  98. IGL_INLINE Eigen::MatrixXi igl::exterior_edges( const Eigen::MatrixXi & F)
  99. {
  100. using namespace Eigen;
  101. MatrixXi E;
  102. exterior_edges(F,E);
  103. return E;
  104. }