qslim.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "qslim.h"
  2. #include "collapse_edge.h"
  3. #include "connect_boundary_to_infinity.h"
  4. #include "decimate.h"
  5. #include "edge_flaps.h"
  6. #include "max_faces_stopping_condition.h"
  7. #include "per_vertex_point_to_plane_quadrics.h"
  8. #include "qslim_optimal_collapse_edge_callbacks.h"
  9. #include "quadric_binary_plus_operator.h"
  10. #include "remove_unreferenced.h"
  11. #include "slice.h"
  12. #include "slice_mask.h"
  13. IGL_INLINE bool igl::qslim(
  14. const Eigen::MatrixXd & V,
  15. const Eigen::MatrixXi & F,
  16. const size_t max_m,
  17. Eigen::MatrixXd & U,
  18. Eigen::MatrixXi & G,
  19. Eigen::VectorXi & J,
  20. Eigen::VectorXi & I)
  21. {
  22. using namespace igl;
  23. // Original number of faces
  24. const int orig_m = F.rows();
  25. // Tracking number of faces
  26. int m = F.rows();
  27. typedef Eigen::MatrixXd DerivedV;
  28. typedef Eigen::MatrixXi DerivedF;
  29. DerivedV VO;
  30. DerivedF FO;
  31. igl::connect_boundary_to_infinity(V,F,VO,FO);
  32. Eigen::VectorXi EMAP;
  33. Eigen::MatrixXi E,EF,EI;
  34. edge_flaps(FO,E,EMAP,EF,EI);
  35. // Quadrics per vertex
  36. typedef std::tuple<Eigen::MatrixXd,Eigen::RowVectorXd,double> Quadric;
  37. std::vector<Quadric> quadrics;
  38. per_vertex_point_to_plane_quadrics(VO,FO,EMAP,EF,EI,quadrics);
  39. // State variables keeping track of edge we just collapsed
  40. int v1 = -1;
  41. int v2 = -1;
  42. // Callbacks for computing and updating metric
  43. std::function<void(
  44. const int e,
  45. const Eigen::MatrixXd &,
  46. const Eigen::MatrixXi &,
  47. const Eigen::MatrixXi &,
  48. const Eigen::VectorXi &,
  49. const Eigen::MatrixXi &,
  50. const Eigen::MatrixXi &,
  51. double &,
  52. Eigen::RowVectorXd &)> cost_and_placement;
  53. std::function<bool(
  54. const Eigen::MatrixXd & ,/*V*/
  55. const Eigen::MatrixXi & ,/*F*/
  56. const Eigen::MatrixXi & ,/*E*/
  57. const Eigen::VectorXi & ,/*EMAP*/
  58. const Eigen::MatrixXi & ,/*EF*/
  59. const Eigen::MatrixXi & ,/*EI*/
  60. const std::set<std::pair<double,int> > & ,/*Q*/
  61. const std::vector<std::set<std::pair<double,int> >::iterator > &,/*Qit*/
  62. const Eigen::MatrixXd & ,/*C*/
  63. const int /*e*/
  64. )> pre_collapse;
  65. std::function<void(
  66. const Eigen::MatrixXd & , /*V*/
  67. const Eigen::MatrixXi & , /*F*/
  68. const Eigen::MatrixXi & , /*E*/
  69. const Eigen::VectorXi & ,/*EMAP*/
  70. const Eigen::MatrixXi & , /*EF*/
  71. const Eigen::MatrixXi & , /*EI*/
  72. const std::set<std::pair<double,int> > & , /*Q*/
  73. const std::vector<std::set<std::pair<double,int> >::iterator > &, /*Qit*/
  74. const Eigen::MatrixXd & , /*C*/
  75. const int , /*e*/
  76. const int , /*e1*/
  77. const int , /*e2*/
  78. const int , /*f1*/
  79. const int , /*f2*/
  80. const bool /*collapsed*/
  81. )> post_collapse;
  82. qslim_optimal_collapse_edge_callbacks(
  83. E,quadrics,v1,v2, cost_and_placement, pre_collapse,post_collapse);
  84. // Call to greedy decimator
  85. bool ret = decimate(
  86. VO, FO,
  87. cost_and_placement,
  88. max_faces_stopping_condition(m,orig_m,max_m),
  89. pre_collapse,
  90. post_collapse,
  91. E, EMAP, EF, EI,
  92. U, G, J, I);
  93. // Remove phony boundary faces and clean up
  94. const Eigen::Array<bool,Eigen::Dynamic,1> keep = (J.array()<orig_m);
  95. igl::slice_mask(Eigen::MatrixXi(G),keep,1,G);
  96. igl::slice_mask(Eigen::VectorXi(J),keep,1,J);
  97. Eigen::VectorXi _1,I2;
  98. igl::remove_unreferenced(Eigen::MatrixXd(U),Eigen::MatrixXi(G),U,G,_1,I2);
  99. igl::slice(Eigen::VectorXi(I),I2,1,I);
  100. return ret;
  101. }