relabel_small_immersed_cells.cpp 3.8 KB

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