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