boundary_facets.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "boundary_facets.h"
  9. #include "face_occurrences.h"
  10. // IGL includes
  11. #include "sort.h"
  12. // STL includes
  13. #include <map>
  14. #include <iostream>
  15. template <typename IntegerT, typename IntegerF>
  16. IGL_INLINE void igl::boundary_facets(
  17. const std::vector<std::vector<IntegerT> > & T,
  18. std::vector<std::vector<IntegerF> > & F)
  19. {
  20. using namespace std;
  21. if(T.size() == 0)
  22. {
  23. F.clear();
  24. return;
  25. }
  26. int simplex_size = T[0].size();
  27. // Get a list of all faces
  28. vector<vector<IntegerF> > allF(
  29. T.size()*simplex_size,
  30. vector<IntegerF>(simplex_size-1));
  31. // Gather faces, loop over tets
  32. for(int i = 0; i< (int)T.size();i++)
  33. {
  34. assert((int)T[i].size() == simplex_size);
  35. switch(simplex_size)
  36. {
  37. case 4:
  38. // get face in correct order
  39. allF[i*simplex_size+0][0] = T[i][1];
  40. allF[i*simplex_size+0][1] = T[i][3];
  41. allF[i*simplex_size+0][2] = T[i][2];
  42. // get face in correct order
  43. allF[i*simplex_size+1][0] = T[i][0];
  44. allF[i*simplex_size+1][1] = T[i][2];
  45. allF[i*simplex_size+1][2] = T[i][3];
  46. // get face in correct order
  47. allF[i*simplex_size+2][0] = T[i][0];
  48. allF[i*simplex_size+2][1] = T[i][3];
  49. allF[i*simplex_size+2][2] = T[i][1];
  50. // get face in correct order
  51. allF[i*simplex_size+3][0] = T[i][0];
  52. allF[i*simplex_size+3][1] = T[i][1];
  53. allF[i*simplex_size+3][2] = T[i][2];
  54. break;
  55. case 3:
  56. allF[i*simplex_size+0][0] = T[i][1];
  57. allF[i*simplex_size+0][1] = T[i][2];
  58. allF[i*simplex_size+1][0] = T[i][2];
  59. allF[i*simplex_size+1][1] = T[i][0];
  60. allF[i*simplex_size+2][0] = T[i][0];
  61. allF[i*simplex_size+2][1] = T[i][1];
  62. break;
  63. }
  64. }
  65. // Counts
  66. vector<int> C;
  67. face_occurrences(allF,C);
  68. // Q: Why not just count the number of ones?
  69. // A: because we are including non-manifold edges as boundary edges
  70. int twos = (int) count(C.begin(),C.end(),2);
  71. //int ones = (int) count(C.begin(),C.end(),1);
  72. // Resize output to fit number of ones
  73. F.resize(allF.size() - twos);
  74. //F.resize(ones);
  75. int k = 0;
  76. for(int i = 0;i< (int)allF.size();i++)
  77. {
  78. if(C[i] != 2)
  79. {
  80. assert(k<(int)F.size());
  81. F[k] = allF[i];
  82. k++;
  83. }
  84. }
  85. assert(k==(int)F.size());
  86. //if(k != F.size())
  87. //{
  88. // printf("%d =? %d\n",k,F.size());
  89. //}
  90. }
  91. #include "list_to_matrix.h"
  92. #include "matrix_to_list.h"
  93. template <typename DerivedT, typename DerivedF>
  94. IGL_INLINE void igl::boundary_facets(
  95. const Eigen::PlainObjectBase<DerivedT>& T,
  96. Eigen::PlainObjectBase<DerivedF>& F)
  97. {
  98. assert(T.cols() == 0 || T.cols() == 4 || T.cols() == 3);
  99. using namespace std;
  100. using namespace Eigen;
  101. // Cop out: use vector of vectors version
  102. vector<vector<typename Eigen::PlainObjectBase<DerivedT>::Scalar> > vT;
  103. matrix_to_list(T,vT);
  104. vector<vector<typename Eigen::PlainObjectBase<DerivedF>::Scalar> > vF;
  105. boundary_facets(vT,vF);
  106. list_to_matrix(vF,F);
  107. }
  108. template <typename DerivedT, typename Ret>
  109. Ret igl::boundary_facets(
  110. const Eigen::PlainObjectBase<DerivedT>& T)
  111. {
  112. Ret F;
  113. igl::boundary_facets(T,F);
  114. return F;
  115. }
  116. #ifdef IGL_STATIC_LIBRARY
  117. // Explicit template specialization
  118. // generated by autoexplicit.sh
  119. template void igl::boundary_facets<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >&);
  120. // generated by autoexplicit.sh
  121. template void igl::boundary_facets<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  122. template void igl::boundary_facets<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> >&);
  123. template void igl::boundary_facets<int, int>(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&);
  124. //template Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > igl::boundary_facets(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  125. template Eigen::Matrix<int, -1, -1, 0, -1, -1> igl::boundary_facets<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&);
  126. #endif