is_vertex_manifold.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@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 "is_vertex_manifold.h"
  9. #include "triangle_triangle_adjacency.h"
  10. #include "vertex_triangle_adjacency.h"
  11. #include "unique.h"
  12. #include <vector>
  13. #include <cassert>
  14. #include <map>
  15. #include <queue>
  16. #include <iostream>
  17. template <typename DerivedF,typename DerivedB>
  18. IGL_INLINE bool igl::is_vertex_manifold(
  19. const Eigen::PlainObjectBase<DerivedF>& F,
  20. Eigen::PlainObjectBase<DerivedB>& B)
  21. {
  22. using namespace std;
  23. using namespace Eigen;
  24. assert(F.cols() == 3 && "F must contain triangles");
  25. typedef typename DerivedF::Scalar Index;
  26. typedef typename DerivedF::Index FIndex;
  27. const FIndex m = F.rows();
  28. const Index n = F.maxCoeff()+1;
  29. vector<vector<vector<FIndex > > > TT;
  30. vector<vector<vector<FIndex > > > TTi;
  31. triangle_triangle_adjacency(F,TT,TTi);
  32. vector<vector<FIndex > > V2F,_1;
  33. vertex_triangle_adjacency(n,F,V2F,_1);
  34. const auto & check_vertex = [&](const Index v)->bool
  35. {
  36. vector<FIndex> uV2Fv;
  37. {
  38. vector<size_t> _1,_2;
  39. unique(V2F[v],uV2Fv,_1,_2);
  40. }
  41. const FIndex one_ring_size = uV2Fv.size();
  42. if(one_ring_size == 0)
  43. {
  44. return false;
  45. }
  46. const FIndex g = uV2Fv[0];
  47. queue<Index> Q;
  48. Q.push(g);
  49. map<FIndex,bool> seen;
  50. while(!Q.empty())
  51. {
  52. const FIndex f = Q.front();
  53. Q.pop();
  54. if(seen.count(f)==1)
  55. {
  56. continue;
  57. }
  58. seen[f] = true;
  59. // Face f's neighbor lists opposite opposite each corner
  60. for(const auto & c : TT[f])
  61. {
  62. // Each neighbor
  63. for(const auto & n : c)
  64. {
  65. bool contains_v = false;
  66. for(Index nc = 0;nc<F.cols();nc++)
  67. {
  68. if(F(n,nc) == v)
  69. {
  70. contains_v = true;
  71. break;
  72. }
  73. }
  74. if(seen.count(n)==0 && contains_v)
  75. {
  76. Q.push(n);
  77. }
  78. }
  79. }
  80. }
  81. return one_ring_size == (FIndex) seen.size();
  82. };
  83. // Unreferenced vertices are considered non-manifold
  84. B.setConstant(n,1,false);
  85. // Loop over all vertices touched by F
  86. bool all = true;
  87. for(Index v = 0;v<n;v++)
  88. {
  89. all &= B(v) = check_vertex(v);
  90. }
  91. return all;
  92. }
  93. #ifdef IGL_STATIC_LIBRARY
  94. template bool igl::is_vertex_manifold<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> >&);
  95. #endif