simplify_polyhedron.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. assert(false && "This is incorrect. In all but simple cases, this could introduce self-intersections");
  22. // TODO: to generalize to non-manifold and open meshes, the cost should be 0
  23. // if moving the vertex kept all incident faces in their original planes and
  24. // kept all incident boundary edges on their original lines.
  25. Eigen::MatrixXd N;
  26. // Function for computing cost of collapsing edge (0 if at least one
  27. // direction doesn't change pointset, inf otherwise) and placement (in lowest
  28. // cost direction).
  29. const auto & perfect= [&N](
  30. const int e,
  31. const Eigen::MatrixXd & V,
  32. const Eigen::MatrixXi & F,
  33. const Eigen::MatrixXi & E,
  34. const Eigen::VectorXi & EMAP,
  35. const Eigen::MatrixXi & EF,
  36. const Eigen::MatrixXi & EI,
  37. double & cost,
  38. Eigen::RowVectorXd & p)
  39. {
  40. // Function for ocmputing cost (0 or inf) of collapsing edge by placing
  41. // vertex at `positive` end of edge.
  42. const auto & perfect_directed = [&N](
  43. const int e,
  44. const bool positive,
  45. const Eigen::MatrixXd & V,
  46. const Eigen::MatrixXi & F,
  47. const Eigen::MatrixXi & E,
  48. const Eigen::VectorXi & EMAP,
  49. const Eigen::MatrixXi & EF,
  50. const Eigen::MatrixXi & EI,
  51. double & cost,
  52. Eigen::RowVectorXd & p)
  53. {
  54. const auto vi = E(e,positive);
  55. const auto vj = E(e,!positive);
  56. p = V.row(vj);
  57. std::vector<int> faces = igl::circulation(e,positive,F,E,EMAP,EF,EI);
  58. assert(faces.size() >= 2);
  59. const Eigen::RowVectorXd nfront = N.row(faces.front());
  60. const Eigen::RowVectorXd nback = N.row(faces.back());
  61. // loop around faces incident on vi, beginning by matching normals to
  62. // front, then allow one switch to back normal
  63. bool matching_front = true;
  64. cost = 0;
  65. for(auto f : faces)
  66. {
  67. const Eigen::RowVectorXd nf = N.row(f);
  68. const double epsilon = 1e-10;
  69. if(matching_front && (nf-nfront).norm()>epsilon)
  70. {
  71. matching_front = false;
  72. }
  73. if(!matching_front && (nf-nback).norm()>epsilon)
  74. {
  75. cost = std::numeric_limits<double>::infinity();
  76. break;
  77. }
  78. }
  79. };
  80. p.resize(3);
  81. double cost0, cost1;
  82. Eigen::RowVectorXd p0, p1;
  83. perfect_directed(e,false,V,F,E,EMAP,EF,EI,cost0,p0);
  84. perfect_directed(e,true,V,F,E,EMAP,EF,EI,cost1,p1);
  85. if(cost0 < cost1)
  86. {
  87. cost = cost0;
  88. p = p0;
  89. }else
  90. {
  91. cost = cost1;
  92. p = p1;
  93. }
  94. };
  95. igl::per_face_normals(OV,OF,N);
  96. igl::decimate(
  97. OV,OF,perfect,igl::infinite_cost_stopping_condition(perfect),V,F,J);
  98. }