boundary_loop.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Stefan Brugger <[email protected]>
  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 "triangle_triangle_adjacency.h"
  10. #include "vertex_triangle_adjacency.h"
  11. #include "is_border_vertex.h"
  12. #include <set>
  13. template <typename DerivedF, typename Index>
  14. IGL_INLINE void igl::boundary_loop(
  15. const Eigen::MatrixBase<DerivedF> & F,
  16. std::vector<std::vector<Index> >& L)
  17. {
  18. using namespace std;
  19. using namespace Eigen;
  20. if(F.rows() == 0)
  21. return;
  22. VectorXd Vdummy(F.maxCoeff()+1,1);
  23. Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, Eigen::Dynamic> TT,TTi;
  24. vector<std::vector<int> > VF, VFi;
  25. triangle_triangle_adjacency(F,TT,TTi);
  26. vertex_triangle_adjacency(Vdummy,F,VF,VFi);
  27. vector<bool> unvisited = is_border_vertex(F);
  28. set<int> unseen;
  29. for (size_t 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 = -1;
  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::MatrixBase<DerivedF>& F,
  82. std::vector<Index>& L)
  83. {
  84. using namespace Eigen;
  85. using namespace std;
  86. if(F.rows() == 0)
  87. return;
  88. vector<vector<int> > Lall;
  89. boundary_loop(F,Lall);
  90. int idxMax = -1;
  91. size_t maxLen = 0;
  92. for (size_t i = 0; i < Lall.size(); ++i)
  93. {
  94. if (Lall[i].size() > maxLen)
  95. {
  96. maxLen = Lall[i].size();
  97. idxMax = i;
  98. }
  99. }
  100. //Check for meshes without boundary
  101. if (idxMax == -1)
  102. {
  103. L.clear();
  104. return;
  105. }
  106. L.resize(Lall[idxMax].size());
  107. for (size_t i = 0; i < Lall[idxMax].size(); ++i)
  108. {
  109. L[i] = Lall[idxMax][i];
  110. }
  111. }
  112. template <typename DerivedF, typename DerivedL>
  113. IGL_INLINE void igl::boundary_loop(
  114. const Eigen::MatrixBase<DerivedF>& F,
  115. Eigen::PlainObjectBase<DerivedL>& L)
  116. {
  117. using namespace Eigen;
  118. using namespace std;
  119. if(F.rows() == 0)
  120. return;
  121. vector<int> Lvec;
  122. boundary_loop(F,Lvec);
  123. L.resize(Lvec.size(), 1);
  124. for (size_t i = 0; i < Lvec.size(); ++i)
  125. L(i) = Lvec[i];
  126. }
  127. #ifdef IGL_STATIC_LIBRARY
  128. // Explicit template instantiation
  129. template void igl::boundary_loop<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  130. template void igl::boundary_loop<Eigen::Matrix<int, -1, -1, 0, -1, -1>, int>(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&);
  131. #endif