boundary_loop.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "igl/boundary_facets.h"
  10. #include "igl/slice.h"
  11. #include <set>
  12. template <typename DerivedF, typename Index>
  13. IGL_INLINE void igl::boundary_loop(
  14. const Eigen::PlainObjectBase<DerivedF> & F,
  15. std::vector<std::vector<Index> >& L)
  16. {
  17. using namespace std;
  18. using namespace Eigen;
  19. MatrixXi E;
  20. boundary_facets(F, E);
  21. set<int> unseen;
  22. for (int i = 0; i < E.rows(); ++i)
  23. {
  24. unseen.insert(unseen.end(),i);
  25. }
  26. while (!unseen.empty())
  27. {
  28. vector<Index> l;
  29. // Get first vertex of loop
  30. int startEdge = *unseen.begin();
  31. unseen.erase(unseen.begin());
  32. int start = E(startEdge,0);
  33. int next = E(startEdge,1);
  34. l.push_back(start);
  35. while (start != next)
  36. {
  37. l.push_back(next);
  38. // Find next edge
  39. int nextEdge;
  40. set<int>::iterator it;
  41. for (it=unseen.begin(); it != unseen.end() ; ++it)
  42. {
  43. if (E(*it,0) == next || E(*it,1) == next)
  44. {
  45. nextEdge = *it;
  46. break;
  47. }
  48. }
  49. unseen.erase(nextEdge);
  50. next = (E(nextEdge,0) == next) ? E(nextEdge,1) : E(nextEdge,0);
  51. }
  52. L.push_back(l);
  53. }
  54. }
  55. template <typename DerivedF, typename Index>
  56. IGL_INLINE void igl::boundary_loop(
  57. const Eigen::PlainObjectBase<DerivedF>& F,
  58. std::vector<Index>& L)
  59. {
  60. using namespace Eigen;
  61. using namespace std;
  62. vector<vector<int> > Lall;
  63. boundary_loop(F,Lall);
  64. int idxMax = -1;
  65. int maxLen = 0;
  66. for (int i = 0; i < Lall.size(); ++i)
  67. {
  68. if (Lall[i].size() > maxLen)
  69. {
  70. maxLen = Lall[i].size();
  71. idxMax = i;
  72. }
  73. }
  74. L.resize(Lall[idxMax].size());
  75. for (int i = 0; i < Lall[idxMax].size(); ++i)
  76. L[i] = Lall[idxMax][i];
  77. }
  78. template <typename DerivedF, typename DerivedL>
  79. IGL_INLINE void igl::boundary_loop(
  80. const Eigen::PlainObjectBase<DerivedF>& F,
  81. Eigen::PlainObjectBase<DerivedL>& L)
  82. {
  83. using namespace Eigen;
  84. using namespace std;
  85. vector<int> Lvec;
  86. boundary_loop(F,Lvec);
  87. L.resize(Lvec.size());
  88. for (int i = 0; i < Lvec.size(); ++i)
  89. L(i) = Lvec[i];
  90. }