simplify_polyhedron.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <alecjacobson@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. #include "simplify_polyhedron.h"
  9. #include "decimate.h"
  10. #include "circulation.h"
  11. #include "per_face_normals.h"
  12. #include "infinite_cost_stopping_condition.h"
  13. #include <functional>
  14. IGL_INLINE void igl::simplify_polyhedron(
  15. const Eigen::MatrixXd & OV,
  16. const Eigen::MatrixXi & OF,
  17. Eigen::MatrixXd & V,
  18. Eigen::MatrixXi & F,
  19. Eigen::VectorXi & J)
  20. {
  21. // TODO: to generalize to open meshes, 0-cost should keep all incident
  22. // boundary edges on their original lines. (for non-manifold meshes,
  23. // igl::decimate needs to be generalized)
  24. Eigen::MatrixXd N;
  25. // Function for computing cost of collapsing edge (0 if at least one
  26. // direction doesn't change pointset, inf otherwise) and placement (in lowest
  27. // cost direction).
  28. const auto & perfect= [&N](
  29. const int e,
  30. const Eigen::MatrixXd & V,
  31. const Eigen::MatrixXi & F,
  32. const Eigen::MatrixXi & E,
  33. const Eigen::VectorXi & EMAP,
  34. const Eigen::MatrixXi & EF,
  35. const Eigen::MatrixXi & EI,
  36. double & cost,
  37. Eigen::RowVectorXd & p)
  38. {
  39. // Function for ocmputing cost (0 or inf) of collapsing edge by placing
  40. // vertex at `positive` end of edge.
  41. const auto & perfect_directed = [&N](
  42. const int e,
  43. const bool positive,
  44. const Eigen::MatrixXd & V,
  45. const Eigen::MatrixXi & F,
  46. const Eigen::MatrixXi & E,
  47. const Eigen::VectorXi & EMAP,
  48. const Eigen::MatrixXi & EF,
  49. const Eigen::MatrixXi & EI,
  50. double & cost,
  51. Eigen::RowVectorXd & p)
  52. {
  53. const auto vi = E(e,positive);
  54. const auto vj = E(e,!positive);
  55. p = V.row(vj);
  56. std::vector<int> faces = igl::circulation(e,positive,F,E,EMAP,EF,EI);
  57. cost = 0;
  58. for(auto f : faces)
  59. {
  60. // Skip the faces being collapsed
  61. if(f == EF(e,0) || f == EF(e,1))
  62. {
  63. continue;
  64. }
  65. const Eigen::RowVectorXd nbefore = N.row(f);
  66. // Face with vi replaced with vj
  67. const Eigen::RowVector3i fafter(
  68. F(f,0) == vi ? vj : F(f,0),
  69. F(f,1) == vi ? vj : F(f,1),
  70. F(f,2) == vi ? vj : F(f,2));
  71. Eigen::RowVectorXd nafter;
  72. igl::per_face_normals(V,fafter,nafter);
  73. const double epsilon = 1e-10;
  74. // if normal changed then not feasible, break
  75. if((nbefore-nafter).norm() > epsilon)
  76. {
  77. cost = std::numeric_limits<double>::infinity();
  78. break;
  79. }
  80. }
  81. };
  82. p.resize(3);
  83. double cost0, cost1;
  84. Eigen::RowVectorXd p0, p1;
  85. perfect_directed(e,false,V,F,E,EMAP,EF,EI,cost0,p0);
  86. perfect_directed(e,true,V,F,E,EMAP,EF,EI,cost1,p1);
  87. if(cost0 < cost1)
  88. {
  89. cost = cost0;
  90. p = p0;
  91. }else
  92. {
  93. cost = cost1;
  94. p = p1;
  95. }
  96. };
  97. igl::per_face_normals(OV,OF,N);
  98. igl::decimate(
  99. OV,OF,perfect,igl::infinite_cost_stopping_condition(perfect),V,F,J);
  100. }