is_boundary_edge.cpp 5.3 KB

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