is_edge_manifold.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "oriented_facets.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::MatrixBase<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. oriented_facets(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 DerivedF>
  53. IGL_INLINE bool igl::is_edge_manifold(
  54. const Eigen::MatrixBase<DerivedF>& F)
  55. {
  56. // TODO: It's bothersome that this is not calling/reusing the code from the
  57. // overload above. This could result in disagreement.
  58. // List of edges (i,j,f,c) where edge i<j is associated with corner i of face
  59. // f
  60. std::vector<std::vector<int> > TTT;
  61. for(int f=0;f<F.rows();++f)
  62. for (int i=0;i<3;++i)
  63. {
  64. // v1 v2 f ei
  65. int v1 = F(f,i);
  66. int v2 = F(f,(i+1)%3);
  67. if (v1 > v2) std::swap(v1,v2);
  68. std::vector<int> r(4);
  69. r[0] = v1; r[1] = v2;
  70. r[2] = f; r[3] = i;
  71. TTT.push_back(r);
  72. }
  73. // Sort lexicographically
  74. std::sort(TTT.begin(),TTT.end());
  75. for(int i=2;i<(int)TTT.size();++i)
  76. {
  77. // Check any edges occur 3 times
  78. std::vector<int>& r1 = TTT[i-2];
  79. std::vector<int>& r2 = TTT[i-1];
  80. std::vector<int>& r3 = TTT[i];
  81. if ( (r1[0] == r2[0] && r2[0] == r3[0])
  82. &&
  83. (r1[1] == r2[1] && r2[1] == r3[1]) )
  84. {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. #ifdef IGL_STATIC_LIBRARY
  91. // Explicit template instantiation
  92. // generated by autoexplicit.sh
  93. template bool igl::is_edge_manifold<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&);
  94. // generated by autoexplicit.sh
  95. //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&);
  96. template bool igl::is_edge_manifold<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  97. template bool igl::is_edge_manifold<Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&);
  98. #endif