qslim.cpp 4.5 KB

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