boundary_loop.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. if(F.rows() == 0)
  23. return;
  24. MatrixXd Vdummy(F.maxCoeff()+1,1);
  25. MatrixXi TT,TTi;
  26. vector<std::vector<int> > VF, VFi;
  27. triangle_triangle_adjacency(Vdummy,F,TT,TTi);
  28. vertex_triangle_adjacency(Vdummy,F,VF,VFi);
  29. vector<bool> unvisited = is_border_vertex(Vdummy,F);
  30. set<int> unseen;
  31. for (size_t i = 0; i < unvisited.size(); ++i)
  32. {
  33. if (unvisited[i])
  34. unseen.insert(unseen.end(),i);
  35. }
  36. while (!unseen.empty())
  37. {
  38. vector<Index> l;
  39. // Get first vertex of loop
  40. int start = *unseen.begin();
  41. unseen.erase(unseen.begin());
  42. unvisited[start] = false;
  43. l.push_back(start);
  44. bool done = false;
  45. while (!done)
  46. {
  47. // Find next vertex
  48. bool newBndEdge = false;
  49. int v = l[l.size()-1];
  50. int next;
  51. for (int i = 0; i < (int)VF[v].size() && !newBndEdge; i++)
  52. {
  53. int fid = VF[v][i];
  54. if (TT.row(fid).minCoeff() < 0.) // Face contains boundary edge
  55. {
  56. int vLoc = -1;
  57. if (F(fid,0) == v) vLoc = 0;
  58. if (F(fid,1) == v) vLoc = 1;
  59. if (F(fid,2) == v) vLoc = 2;
  60. int vNext = F(fid,(vLoc + 1) % F.cols());
  61. newBndEdge = false;
  62. if (unvisited[vNext] && TT(fid,vLoc) < 0)
  63. {
  64. next = vNext;
  65. newBndEdge = true;
  66. }
  67. }
  68. }
  69. if (newBndEdge)
  70. {
  71. l.push_back(next);
  72. unseen.erase(next);
  73. unvisited[next] = false;
  74. }
  75. else
  76. done = true;
  77. }
  78. L.push_back(l);
  79. }
  80. }
  81. template <typename DerivedF, typename Index>
  82. IGL_INLINE void igl::boundary_loop(
  83. const Eigen::PlainObjectBase<DerivedF>& F,
  84. std::vector<Index>& L)
  85. {
  86. using namespace Eigen;
  87. using namespace std;
  88. if(F.rows() == 0)
  89. return;
  90. vector<vector<int> > Lall;
  91. boundary_loop(F,Lall);
  92. int idxMax = -1;
  93. size_t maxLen = 0;
  94. for (size_t i = 0; i < Lall.size(); ++i)
  95. {
  96. if (Lall[i].size() > maxLen)
  97. {
  98. maxLen = Lall[i].size();
  99. idxMax = i;
  100. }
  101. }
  102. L.resize(Lall[idxMax].size());
  103. for (size_t i = 0; i < Lall[idxMax].size(); ++i)
  104. {
  105. L[i] = Lall[idxMax][i];
  106. }
  107. }
  108. template <typename DerivedF, typename DerivedL>
  109. IGL_INLINE void igl::boundary_loop(
  110. const Eigen::PlainObjectBase<DerivedF>& F,
  111. Eigen::PlainObjectBase<DerivedL>& L)
  112. {
  113. using namespace Eigen;
  114. using namespace std;
  115. if(F.rows() == 0)
  116. return;
  117. vector<int> Lvec;
  118. boundary_loop(F,Lvec);
  119. L.resize(Lvec.size());
  120. for (size_t i = 0; i < Lvec.size(); ++i)
  121. L(i) = Lvec[i];
  122. }
  123. #ifdef IGL_STATIC_LIBRARY
  124. // Explicit template specialization
  125. template void igl::boundary_loop<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  126. #endif