boundary_loop.cpp 3.7 KB

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