resolve_duplicated_faces.cpp 4.1 KB

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