is_boundary_edge.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_boundary_edge.h"
  9. #include "unique.h"
  10. #include "sort.h"
  11. template <
  12. typename DerivedF,
  13. typename DerivedE,
  14. typename DerivedB>
  15. void igl::is_boundary_edge(
  16. const Eigen::PlainObjectBase<DerivedE> & E,
  17. const Eigen::PlainObjectBase<DerivedF> & F,
  18. Eigen::PlainObjectBase<DerivedB> & B)
  19. {
  20. using namespace Eigen;
  21. using namespace std;
  22. // Should be triangles
  23. assert(F.cols() == 3);
  24. // Should be edges
  25. assert(E.cols() == 2);
  26. // number of faces
  27. const int m = F.rows();
  28. // Collect all directed edges after E
  29. MatrixXi EallE(E.rows()+3*m,2);
  30. EallE.block(0,0,E.rows(),E.cols()) = E;
  31. for(int e = 0;e<3;e++)
  32. {
  33. for(int f = 0;f<m;f++)
  34. {
  35. for(int c = 0;c<2;c++)
  36. {
  37. // 12 20 01
  38. EallE(E.rows()+m*e+f,c) = F(f,(c+1+e)%3);
  39. }
  40. }
  41. }
  42. // sort directed edges into undirected edges
  43. MatrixXi sorted_EallE,_;
  44. sort(EallE,2,true,sorted_EallE,_);
  45. // Determine unique undirected edges E and map to directed edges EMAP
  46. MatrixXi uE;
  47. VectorXi EMAP;
  48. unique_rows(sorted_EallE,uE,_,EMAP);
  49. // Counts of occurances
  50. VectorXi N = VectorXi::Zero(uE.rows());
  51. for(int e = 0;e<EMAP.rows();e++)
  52. {
  53. N(EMAP(e))++;
  54. }
  55. B.resize(E.rows());
  56. // Look of occurances of 2: one for original and another for boundary
  57. for(int e = 0;e<E.rows();e++)
  58. {
  59. B(e) = (N(EMAP(e)) == 2);
  60. }
  61. }
  62. template <
  63. typename DerivedF,
  64. typename DerivedE,
  65. typename DerivedB,
  66. typename DerivedEMAP>
  67. void igl::is_boundary_edge(
  68. const Eigen::PlainObjectBase<DerivedF> & F,
  69. Eigen::PlainObjectBase<DerivedB> & B,
  70. Eigen::PlainObjectBase<DerivedE> & E,
  71. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  72. {
  73. using namespace Eigen;
  74. using namespace std;
  75. // Should be triangles
  76. assert(F.cols() == 3);
  77. // number of faces
  78. const int m = F.rows();
  79. // Collect all directed edges after E
  80. MatrixXi allE(3*m,2);
  81. for(int e = 0;e<3;e++)
  82. {
  83. for(int f = 0;f<m;f++)
  84. {
  85. for(int c = 0;c<2;c++)
  86. {
  87. // 12 20 01
  88. allE(m*e+f,c) = F(f,(c+1+e)%3);
  89. }
  90. }
  91. }
  92. // sort directed edges into undirected edges
  93. MatrixXi sorted_allE,_;
  94. sort(allE,2,true,sorted_allE,_);
  95. // Determine unique undirected edges E and map to directed edges EMAP
  96. unique_rows(sorted_allE,E,_,EMAP);
  97. // Counts of occurances
  98. VectorXi N = VectorXi::Zero(E.rows());
  99. for(int e = 0;e<EMAP.rows();e++)
  100. {
  101. N(EMAP(e))++;
  102. }
  103. B.resize(E.rows());
  104. // Look of occurances of 1
  105. for(int e = 0;e<E.rows();e++)
  106. {
  107. B(e) = N(e) == 1;
  108. }
  109. }
  110. #ifdef IGL_STATIC_LIBRARY
  111. // Explicit instanciation:
  112. template void igl::is_boundary_edge<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  113. template void igl::is_boundary_edge<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<bool, -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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  114. template void igl::is_boundary_edge<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  115. template void igl::is_boundary_edge<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<bool, -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<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  116. template void igl::is_boundary_edge<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  117. template void igl::is_boundary_edge<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -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<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  118. #endif