edge_topology.cpp 3.7 KB

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