edgetopology.cpp 2.6 KB

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