edgetopology.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // IGL Lib - Simple C++ mesh library
  3. //
  4. // Copyright 2011, Daniele Panozzo. All rights reserved.
  5. #include "edgetopology.h"
  6. #include <algorithm>
  7. #include "is_manifold.h"
  8. IGL_INLINE void igl::edgetopology(
  9. const Eigen::MatrixXd& V,
  10. const Eigen::MatrixXi& F,
  11. Eigen::MatrixXi& EV,
  12. Eigen::MatrixXi& FE,
  13. Eigen::MatrixXi& EF)
  14. {
  15. assert(igl::is_manifold(V,F));
  16. std::vector<std::vector<int> > ETT;
  17. for(int f=0;f<F.rows();++f)
  18. for (int i=0;i<3;++i)
  19. {
  20. // v1 v2 f vi
  21. int v1 = F(f,i);
  22. int v2 = F(f,(i+1)%3);
  23. if (v1 > v2) std::swap(v1,v2);
  24. std::vector<int> r(4);
  25. r[0] = v1; r[1] = v2;
  26. r[2] = f; r[3] = i;
  27. ETT.push_back(r);
  28. }
  29. std::sort(ETT.begin(),ETT.end());
  30. // count the number of edges (assume manifoldness)
  31. int En = 1; // the last is always counted
  32. for(unsigned i=0;i<ETT.size()-1;++i)
  33. if (!((ETT[i][0] == ETT[i+1][0]) && (ETT[i][1] == ETT[i+1][1])))
  34. ++En;
  35. EV = Eigen::MatrixXi::Constant((int)(En),2,-1);
  36. FE = Eigen::MatrixXi::Constant((int)(F.rows()),3,-1);
  37. EF = Eigen::MatrixXi::Constant((int)(En),2,-1);
  38. En = 0;
  39. for(unsigned i=0;i<ETT.size();++i)
  40. {
  41. if (i == ETT.size()-1 ||
  42. !((ETT[i][0] == ETT[i+1][0]) && (ETT[i][1] == ETT[i+1][1]))
  43. )
  44. {
  45. // Border edge
  46. std::vector<int>& r1 = ETT[i];
  47. EV(En,0) = r1[0];
  48. EV(En,1) = r1[1];
  49. EF(En,0) = r1[2];
  50. FE(r1[2],r1[3]) = En;
  51. }
  52. else
  53. {
  54. std::vector<int>& r1 = ETT[i];
  55. std::vector<int>& r2 = ETT[i+1];
  56. EV(En,0) = r1[0];
  57. EV(En,1) = r1[1];
  58. EF(En,0) = r1[2];
  59. EF(En,1) = r2[2];
  60. FE(r1[2],r1[3]) = En;
  61. FE(r2[2],r2[3]) = En;
  62. ++i; // skip the next one
  63. }
  64. ++En;
  65. }
  66. // Sort the relation EF, accordingly to EV
  67. // the first one is the face on the left of the edge
  68. for(unsigned i=0; i<EF.rows(); ++i)
  69. {
  70. int fid = EF(i,0);
  71. bool flip = true;
  72. // search for edge EV.row(i)
  73. for (unsigned j=0; j<3; ++j)
  74. {
  75. if ((F(fid,j) == EV(i,0)) && (F(fid,(j+1)%3) == EV(i,1)))
  76. flip = false;
  77. }
  78. if (flip)
  79. {
  80. int tmp = EF(i,0);
  81. EF(i,0) = EF(i,1);
  82. EF(i,1) = tmp;
  83. }
  84. }
  85. }
  86. #ifndef IGL_HEADER_ONLY
  87. // Explicit template specialization
  88. #endif