boundary_loop.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Stefan Brugger <stefanbrugger@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 "boundary_loop.h"
  9. #include "slice.h"
  10. #include "triangle_triangle_adjacency.h"
  11. #include "vertex_triangle_adjacency.h"
  12. #include "is_border_vertex.h"
  13. #include <set>
  14. template <typename DerivedF, typename Index>
  15. IGL_INLINE void igl::boundary_loop(
  16. const Eigen::PlainObjectBase<DerivedF> & F,
  17. std::vector<std::vector<Index> >& L)
  18. {
  19. using namespace std;
  20. using namespace Eigen;
  21. using namespace igl;
  22. MatrixXd Vdummy(F.maxCoeff(),1);
  23. MatrixXi TT,TTi;
  24. vector<std::vector<int> > VF, VFi;
  25. triangle_triangle_adjacency(Vdummy,F,TT,TTi);
  26. vertex_triangle_adjacency(Vdummy,F,VF,VFi);
  27. vector<bool> unvisited = is_border_vertex(Vdummy,F);
  28. set<int> unseen;
  29. for (int i = 0; i < unvisited.size(); ++i)
  30. {
  31. if (unvisited[i])
  32. unseen.insert(unseen.end(),i);
  33. }
  34. while (!unseen.empty())
  35. {
  36. vector<Index> l;
  37. // Get first vertex of loop
  38. int start = *unseen.begin();
  39. unseen.erase(unseen.begin());
  40. unvisited[start] = false;
  41. l.push_back(start);
  42. bool done = false;
  43. while (!done)
  44. {
  45. // Find next vertex
  46. bool newBndEdge = false;
  47. int v = l[l.size()-1];
  48. int next;
  49. for (int i = 0; i < (int)VF[v].size() && !newBndEdge; i++)
  50. {
  51. int fid = VF[v][i];
  52. if (TT.row(fid).minCoeff() < 0.) // Face contains boundary edge
  53. {
  54. int vLoc;
  55. if (F(fid,0) == v) vLoc = 0;
  56. if (F(fid,1) == v) vLoc = 1;
  57. if (F(fid,2) == v) vLoc = 2;
  58. int vNext = F(fid,(vLoc + 1) % F.cols());
  59. newBndEdge = false;
  60. if (unvisited[vNext] && TT(fid,vLoc) < 0)
  61. {
  62. next = vNext;
  63. newBndEdge = true;
  64. }
  65. }
  66. }
  67. if (newBndEdge)
  68. {
  69. l.push_back(next);
  70. unseen.erase(next);
  71. unvisited[next] = false;
  72. }
  73. else
  74. done = true;
  75. }
  76. L.push_back(l);
  77. }
  78. }
  79. template <typename DerivedF, typename Index>
  80. IGL_INLINE void igl::boundary_loop(
  81. const Eigen::PlainObjectBase<DerivedF>& F,
  82. std::vector<Index>& L)
  83. {
  84. using namespace Eigen;
  85. using namespace std;
  86. vector<vector<int> > Lall;
  87. boundary_loop(F,Lall);
  88. int idxMax = -1;
  89. int maxLen = 0;
  90. for (int i = 0; i < Lall.size(); ++i)
  91. {
  92. if (Lall[i].size() > maxLen)
  93. {
  94. maxLen = Lall[i].size();
  95. idxMax = i;
  96. }
  97. }
  98. L.resize(Lall[idxMax].size());
  99. for (int i = 0; i < Lall[idxMax].size(); ++i)
  100. L[i] = Lall[idxMax][i];
  101. }
  102. template <typename DerivedF, typename DerivedL>
  103. IGL_INLINE void igl::boundary_loop(
  104. const Eigen::PlainObjectBase<DerivedF>& F,
  105. Eigen::PlainObjectBase<DerivedL>& L)
  106. {
  107. using namespace Eigen;
  108. using namespace std;
  109. vector<int> Lvec;
  110. boundary_loop(F,Lvec);
  111. L.resize(Lvec.size());
  112. for (int i = 0; i < Lvec.size(); ++i)
  113. L(i) = Lvec[i];
  114. }