boundary_loop.cpp 3.5 KB

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