is_edge_manifold.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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_edge_manifold.h"
  9. #include "all_edges.h"
  10. #include "unique_simplices.h"
  11. #include <algorithm>
  12. #include <vector>
  13. template <
  14. typename DerivedF,
  15. typename DerivedBF,
  16. typename DerivedE,
  17. typename DerivedEMAP,
  18. typename DerivedBE>
  19. IGL_INLINE bool igl::is_edge_manifold(
  20. const Eigen::PlainObjectBase<DerivedF>& F,
  21. Eigen::PlainObjectBase<DerivedBF>& BF,
  22. Eigen::PlainObjectBase<DerivedE>& E,
  23. Eigen::PlainObjectBase<DerivedEMAP>& EMAP,
  24. Eigen::PlainObjectBase<DerivedBE>& BE)
  25. {
  26. using namespace Eigen;
  27. typedef typename DerivedF::Index Index;
  28. typedef Matrix<typename DerivedF::Scalar,Dynamic,1> VectorXF;
  29. typedef Matrix<typename DerivedF::Scalar,Dynamic,2> MatrixXF2;
  30. MatrixXF2 allE;
  31. all_edges(F,allE);
  32. // Find unique undirected edges and mapping
  33. VectorXF _;
  34. unique_simplices(allE,E,_,EMAP);
  35. std::vector<typename DerivedF::Index> count(E.rows(),0);
  36. for(Index e = 0;e<EMAP.rows();e++)
  37. {
  38. count[EMAP[e]]++;
  39. }
  40. const Index m = F.rows();
  41. BF.resize(m,3);
  42. BE.resize(E.rows(),1);
  43. bool all = true;
  44. for(Index e = 0;e<EMAP.rows();e++)
  45. {
  46. const bool manifold = count[EMAP[e]] <= 2;
  47. all &= BF(e%m,e/m) = manifold;
  48. BE(EMAP[e]) = manifold;
  49. }
  50. return all;
  51. }
  52. template <typename DerivedV, typename DerivedF>
  53. IGL_INLINE bool igl::is_edge_manifold(
  54. const Eigen::PlainObjectBase<DerivedV>& /*V*/,
  55. const Eigen::PlainObjectBase<DerivedF>& F)
  56. {
  57. // List of edges (i,j,f,c) where edge i<j is associated with corner i of face
  58. // f
  59. std::vector<std::vector<int> > TTT;
  60. for(int f=0;f<F.rows();++f)
  61. for (int i=0;i<3;++i)
  62. {
  63. // v1 v2 f ei
  64. int v1 = F(f,i);
  65. int v2 = F(f,(i+1)%3);
  66. if (v1 > v2) std::swap(v1,v2);
  67. std::vector<int> r(4);
  68. r[0] = v1; r[1] = v2;
  69. r[2] = f; r[3] = i;
  70. TTT.push_back(r);
  71. }
  72. // Sort lexicographically
  73. std::sort(TTT.begin(),TTT.end());
  74. for(int i=2;i<(int)TTT.size();++i)
  75. {
  76. // Check any edges occur 3 times
  77. std::vector<int>& r1 = TTT[i-2];
  78. std::vector<int>& r2 = TTT[i-1];
  79. std::vector<int>& r3 = TTT[i];
  80. if ( (r1[0] == r2[0] && r2[0] == r3[0])
  81. &&
  82. (r1[1] == r2[1] && r2[1] == r3[1]) )
  83. {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. #ifdef IGL_STATIC_LIBRARY
  90. // Explicit template specialization
  91. // generated by autoexplicit.sh
  92. template bool igl::is_edge_manifold<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&);
  93. // generated by autoexplicit.sh
  94. //template bool igl::is_edge_manifold<double>(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&);
  95. template bool igl::is_edge_manifold<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  96. template bool igl::is_edge_manifold<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&);
  97. #endif