resolve_duplicated_faces.cpp 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Qingnan Zhou <[email protected]>
  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. //
  9. #include "resolve_duplicated_faces.h"
  10. #include "IGL_ASSERT.h"
  11. #include "placeholders.h"
  12. #include "unique_simplices.h"
  13. #include <vector>
  14. template<
  15. typename DerivedF1,
  16. typename DerivedF2,
  17. typename DerivedJ >
  18. IGL_INLINE void igl::resolve_duplicated_faces(
  19. const Eigen::MatrixBase<DerivedF1>& F1,
  20. Eigen::PlainObjectBase<DerivedF2>& F2,
  21. Eigen::PlainObjectBase<DerivedJ>& J) {
  22. //typedef typename DerivedF1::Scalar Index;
  23. Eigen::Matrix<typename DerivedF1::Scalar, Eigen::Dynamic, 1> IA,IC;
  24. DerivedF1 uF;
  25. igl::unique_simplices(F1,uF,IA,IC);
  26. const size_t num_faces = F1.rows();
  27. const size_t num_unique_faces = uF.rows();
  28. IGL_ASSERT((size_t) IA.rows() == num_unique_faces);
  29. // faces on top of each unique face
  30. std::vector<std::vector<int> > uF2F(num_unique_faces);
  31. // signed counts
  32. Eigen::VectorXi counts = Eigen::VectorXi::Zero(num_unique_faces);
  33. Eigen::VectorXi ucounts = Eigen::VectorXi::Zero(num_unique_faces);
  34. // loop over all faces
  35. for (size_t i=0; i<num_faces; i++) {
  36. const size_t ui = IC(i);
  37. const bool consistent =
  38. (F1(i,0) == uF(ui, 0) && F1(i,1) == uF(ui, 1) && F1(i,2) == uF(ui, 2)) ||
  39. (F1(i,0) == uF(ui, 1) && F1(i,1) == uF(ui, 2) && F1(i,2) == uF(ui, 0)) ||
  40. (F1(i,0) == uF(ui, 2) && F1(i,1) == uF(ui, 0) && F1(i,2) == uF(ui, 1));
  41. uF2F[ui].push_back(int(i+1) * (consistent?1:-1));
  42. counts(ui) += consistent ? 1:-1;
  43. ucounts(ui)++;
  44. }
  45. std::vector<size_t> kept_faces;
  46. for (size_t i=0; i<num_unique_faces; i++) {
  47. if (ucounts[i] == 1) {
  48. kept_faces.push_back(abs(uF2F[i][0])-1);
  49. continue;
  50. }
  51. if (counts[i] == 1) {
  52. bool found = false;
  53. for (auto fid : uF2F[i]) {
  54. if (fid > 0) {
  55. kept_faces.push_back(abs(fid)-1);
  56. found = true;
  57. break;
  58. }
  59. }
  60. IGL_ASSERT(found);
  61. } else if (counts[i] == -1) {
  62. bool found = false;
  63. for (auto fid : uF2F[i]) {
  64. if (fid < 0) {
  65. kept_faces.push_back(abs(fid)-1);
  66. found = true;
  67. break;
  68. }
  69. }
  70. IGL_ASSERT(found);
  71. } else {
  72. IGL_ASSERT(counts[i] == 0);
  73. }
  74. }
  75. const size_t num_kept = kept_faces.size();
  76. J.resize(num_kept, 1);
  77. std::copy(kept_faces.begin(), kept_faces.end(), J.data());
  78. F2 = F1(J.derived(),igl::placeholders::all);
  79. }
  80. #ifdef IGL_STATIC_LIBRARY
  81. // Explicit template instantiation
  82. // generated by autoexplicit.sh
  83. template void igl::resolve_duplicated_faces<Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  84. template void igl::resolve_duplicated_faces<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  85. template void igl::resolve_duplicated_faces<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<long, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&);
  86. #ifdef WIN32
  87. template void igl::resolve_duplicated_faces<class Eigen::Matrix<int, -1, -1, 0, -1, -1>, class Eigen::Matrix<int, -1, -1, 0, -1, -1>, class Eigen::Matrix<__int64, -1, 1, 0, -1, 1>>(class Eigen::MatrixBase<class Eigen::Matrix<int, -1, -1, 0, -1, -1>> const &, class Eigen::PlainObjectBase<class Eigen::Matrix<int, -1, -1, 0, -1, -1>> &, class Eigen::PlainObjectBase<class Eigen::Matrix<__int64, -1, 1, 0, -1, 1>> &);
  88. #endif
  89. #endif