exterior_edges.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "exterior_edges.h"
  2. #include "all_edges.h"
  3. #include <cassert>
  4. #include <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. // Count occurances looking only at pairs (i,j) where i<j, so we count and
  39. // edge i-->j as +1 and j-->i as -1
  40. map<pair<const int,const int>,int> C;
  41. // Loop over edges
  42. for(int e = 0;e<all_E.rows();e++)
  43. {
  44. int i = all_E(e,0);
  45. int j = all_E(e,1);
  46. if(i<j)
  47. {
  48. // Forward direction --> +1
  49. C[pair<const int,const int>(i,j)]++;
  50. }else
  51. {
  52. // Backward direction --> -1
  53. C[pair<const int,const int>(j,i)]--;
  54. }
  55. }
  56. // Q: Why mod off factors of 2? +1/-1 already takes care of interior edges?
  57. //// Mod off any factors of 2
  58. //for_each(C.begin(),C.end(),mod2);
  59. //int zeros = (int) count(C.begin(),C.end(),Compare(0));
  60. //E.resize(C.size() - zeros,2);
  61. E.resize(all_E.rows(),all_E.cols());
  62. int e = 0;
  63. // Find all edges with -1 or 1 occurances, flipping direction for -1
  64. for(
  65. map<pair<const int, const int>,int>::const_iterator cit=C.begin();
  66. cit!=C.end();
  67. cit++)
  68. {
  69. int i,j;
  70. if(cit->second > 0)
  71. {
  72. i = cit->first.first;
  73. j = cit->first.second;
  74. } else if(cit->second < 0)
  75. {
  76. i = cit->first.second;
  77. j = cit->first.first;
  78. } else if(cit->second == 0)
  79. {
  80. continue;
  81. }
  82. for(int k = 0;k<abs(cit->second);k++)
  83. {
  84. E(e,0) = i;
  85. E(e,1) = j;
  86. e++;
  87. }
  88. }
  89. E.conservativeResize(e,2);
  90. assert(e == E.rows());
  91. }
  92. IGL_INLINE Eigen::MatrixXi igl::exterior_edges( const Eigen::MatrixXi & F)
  93. {
  94. using namespace Eigen;
  95. MatrixXi E;
  96. exterior_edges(F,E);
  97. return E;
  98. }