resolve_duplicated_faces.cpp 3.6 KB

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