relabel_small_immersed_cells.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "relabel_small_immersed_cells.h"
  10. #include "../../centroid.h"
  11. #include "assign.h"
  12. #include "cell_adjacency.h"
  13. #include <vector>
  14. template<
  15. typename DerivedV,
  16. typename DerivedF,
  17. typename DerivedP,
  18. typename DerivedC,
  19. typename FT,
  20. typename DerivedW>
  21. IGL_INLINE void igl::copyleft::cgal::relabel_small_immersed_cells(
  22. const Eigen::PlainObjectBase<DerivedV>& V,
  23. const Eigen::PlainObjectBase<DerivedF>& F,
  24. const size_t num_patches,
  25. const Eigen::PlainObjectBase<DerivedP>& P,
  26. const size_t num_cells,
  27. const Eigen::PlainObjectBase<DerivedC>& C,
  28. const FT vol_threashold,
  29. Eigen::PlainObjectBase<DerivedW>& W)
  30. {
  31. const size_t num_vertices = V.rows();
  32. const size_t num_faces = F.rows();
  33. typedef std::tuple<typename DerivedC::Scalar, bool, size_t> CellConnection;
  34. std::vector<std::set<CellConnection> > cell_adj;
  35. igl::copyleft::cgal::cell_adjacency(C, num_cells, cell_adj);
  36. Eigen::MatrixXd VV;
  37. assign(V,VV);
  38. auto compute_cell_volume = [&](size_t cell_id) {
  39. std::vector<short> is_involved(num_patches, 0);
  40. for (size_t i=0; i<num_patches; i++) {
  41. if (C(i,0) == cell_id) {
  42. // cell is on positive side of patch i.
  43. is_involved[i] = 1;
  44. }
  45. if (C(i,1) == cell_id) {
  46. // cell is on negative side of patch i.
  47. is_involved[i] = -1;
  48. }
  49. }
  50. std::vector<size_t> involved_positive_faces;
  51. std::vector<size_t> involved_negative_faces;
  52. for (size_t i=0; i<num_faces; i++) {
  53. switch (is_involved[P[i]]) {
  54. case 1:
  55. involved_negative_faces.push_back(i);
  56. break;
  57. case -1:
  58. involved_positive_faces.push_back(i);
  59. break;
  60. }
  61. }
  62. const size_t num_positive_faces = involved_positive_faces.size();
  63. const size_t num_negative_faces = involved_negative_faces.size();
  64. DerivedF selected_faces(num_positive_faces + num_negative_faces, 3);
  65. for (size_t i=0; i<num_positive_faces; i++) {
  66. selected_faces.row(i) = F.row(involved_positive_faces[i]);
  67. }
  68. for (size_t i=0; i<num_negative_faces; i++) {
  69. selected_faces.row(num_positive_faces+i) =
  70. F.row(involved_negative_faces[i]).reverse();
  71. }
  72. Eigen::VectorXd c(3);
  73. double vol;
  74. igl::centroid(VV, selected_faces, c, vol);
  75. return vol;
  76. };
  77. std::vector<typename DerivedV::Scalar> cell_volumes(num_cells);
  78. for (size_t i=0; i<num_cells; i++) {
  79. cell_volumes[i] = compute_cell_volume(i);
  80. }
  81. std::vector<typename DerivedW::Scalar> cell_values(num_cells);
  82. for (size_t i=0; i<num_faces; i++) {
  83. cell_values[C(P[i], 0)] = W(i, 0);
  84. cell_values[C(P[i], 1)] = W(i, 1);
  85. }
  86. for (size_t i=1; i<num_cells; i++) {
  87. std::cout << cell_volumes[i] << std::endl;
  88. if (cell_volumes[i] >= vol_threashold) continue;
  89. std::set<typename DerivedW::Scalar> neighbor_values;
  90. const auto neighbors = cell_adj[i];
  91. for (const auto& entry : neighbors) {
  92. const auto& j = std::get<0>(entry);
  93. neighbor_values.insert(cell_values[j]);
  94. }
  95. // If cell i is immersed, assign its value to be the immersed value.
  96. if (neighbor_values.size() == 1) {
  97. cell_values[i] = *neighbor_values.begin();
  98. }
  99. }
  100. for (size_t i=0; i<num_faces; i++) {
  101. W(i,0) = cell_values[C(P[i], 0)];
  102. W(i,1) = cell_values[C(P[i], 1)];
  103. }
  104. }