decimate.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <[email protected]>
  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 "decimate.h"
  9. #include "collapse_edge.h"
  10. #include "edge_flaps.h"
  11. #include "is_edge_manifold.h"
  12. #include "remove_unreferenced.h"
  13. #include "slice_mask.h"
  14. #include "slice.h"
  15. #include "connect_boundary_to_infinity.h"
  16. #include "max_faces_stopping_condition.h"
  17. #include "shortest_edge_and_midpoint.h"
  18. IGL_INLINE bool igl::decimate(
  19. const Eigen::MatrixXd & V,
  20. const Eigen::MatrixXi & F,
  21. const size_t max_m,
  22. Eigen::MatrixXd & U,
  23. Eigen::MatrixXi & G,
  24. Eigen::VectorXi & J,
  25. Eigen::VectorXi & I)
  26. {
  27. // Original number of faces
  28. const int orig_m = F.rows();
  29. // Tracking number of faces
  30. int m = F.rows();
  31. typedef Eigen::MatrixXd DerivedV;
  32. typedef Eigen::MatrixXi DerivedF;
  33. DerivedV VO;
  34. DerivedF FO;
  35. igl::connect_boundary_to_infinity(V,F,VO,FO);
  36. // decimate will not work correctly on non-edge-manifold meshes. By extension
  37. // this includes meshes with non-manifold vertices on the boundary since these
  38. // will create a non-manifold edge when connected to infinity.
  39. if(!is_edge_manifold(FO))
  40. {
  41. return false;
  42. }
  43. bool ret = decimate(
  44. VO,
  45. FO,
  46. shortest_edge_and_midpoint,
  47. max_faces_stopping_condition(m,orig_m,max_m),
  48. U,
  49. G,
  50. J,
  51. I);
  52. const Eigen::Array<bool,Eigen::Dynamic,1> keep = (J.array()<orig_m);
  53. igl::slice_mask(Eigen::MatrixXi(G),keep,1,G);
  54. igl::slice_mask(Eigen::VectorXi(J),keep,1,J);
  55. Eigen::VectorXi _1,I2;
  56. igl::remove_unreferenced(Eigen::MatrixXd(U),Eigen::MatrixXi(G),U,G,_1,I2);
  57. igl::slice(Eigen::VectorXi(I),I2,1,I);
  58. return ret;
  59. }
  60. IGL_INLINE bool igl::decimate(
  61. const Eigen::MatrixXd & V,
  62. const Eigen::MatrixXi & F,
  63. const size_t max_m,
  64. Eigen::MatrixXd & U,
  65. Eigen::MatrixXi & G,
  66. Eigen::VectorXi & J)
  67. {
  68. Eigen::VectorXi I;
  69. return igl::decimate(V,F,max_m,U,G,J,I);
  70. }
  71. IGL_INLINE bool igl::decimate(
  72. const Eigen::MatrixXd & OV,
  73. const Eigen::MatrixXi & OF,
  74. const decimate_cost_and_placement_func & cost_and_placement,
  75. const decimate_stopping_condition_func & stopping_condition,
  76. Eigen::MatrixXd & U,
  77. Eigen::MatrixXi & G,
  78. Eigen::VectorXi & J,
  79. Eigen::VectorXi & I
  80. )
  81. {
  82. const auto always_try = [](
  83. const Eigen::MatrixXd & ,/*V*/
  84. const Eigen::MatrixXi & ,/*F*/
  85. const Eigen::MatrixXi & ,/*E*/
  86. const Eigen::VectorXi & ,/*EMAP*/
  87. const Eigen::MatrixXi & ,/*EF*/
  88. const Eigen::MatrixXi & ,/*EI*/
  89. const std::set<std::pair<double,int> > & ,/*Q*/
  90. const std::vector<std::set<std::pair<double,int> >::iterator > &,/*Qit*/
  91. const Eigen::MatrixXd & ,/*C*/
  92. const int /*e*/
  93. ) -> bool { return true;};
  94. const auto never_care = [](
  95. const Eigen::MatrixXd & , /*V*/
  96. const Eigen::MatrixXi & , /*F*/
  97. const Eigen::MatrixXi & , /*E*/
  98. const Eigen::VectorXi & ,/*EMAP*/
  99. const Eigen::MatrixXi & , /*EF*/
  100. const Eigen::MatrixXi & , /*EI*/
  101. const std::set<std::pair<double,int> > & , /*Q*/
  102. const std::vector<std::set<std::pair<double,int> >::iterator > &, /*Qit*/
  103. const Eigen::MatrixXd & , /*C*/
  104. const int , /*e*/
  105. const int , /*e1*/
  106. const int , /*e2*/
  107. const int , /*f1*/
  108. const int , /*f2*/
  109. const bool /*collapsed*/
  110. )-> void { };
  111. return igl::decimate(
  112. OV,OF,cost_and_placement,stopping_condition,always_try,never_care,U,G,J,I);
  113. }
  114. IGL_INLINE bool igl::decimate(
  115. const Eigen::MatrixXd & OV,
  116. const Eigen::MatrixXi & OF,
  117. const decimate_cost_and_placement_func & cost_and_placement,
  118. const decimate_stopping_condition_func & stopping_condition,
  119. const decimate_pre_collapse_func & pre_collapse,
  120. const decimate_post_collapse_func & post_collapse,
  121. Eigen::MatrixXd & U,
  122. Eigen::MatrixXi & G,
  123. Eigen::VectorXi & J,
  124. Eigen::VectorXi & I
  125. )
  126. {
  127. using namespace Eigen;
  128. using namespace std;
  129. VectorXi EMAP;
  130. MatrixXi E,EF,EI;
  131. edge_flaps(OF,E,EMAP,EF,EI);
  132. return igl::decimate(
  133. OV,OF,
  134. cost_and_placement,stopping_condition,pre_collapse,post_collapse,
  135. E,EMAP,EF,EI,
  136. U,G,J,I);
  137. }
  138. IGL_INLINE bool igl::decimate(
  139. const Eigen::MatrixXd & OV,
  140. const Eigen::MatrixXi & OF,
  141. const decimate_cost_and_placement_func & cost_and_placement,
  142. const decimate_stopping_condition_func & stopping_condition,
  143. const decimate_pre_collapse_func & pre_collapse,
  144. const decimate_post_collapse_func & post_collapse,
  145. const Eigen::MatrixXi & OE,
  146. const Eigen::VectorXi & OEMAP,
  147. const Eigen::MatrixXi & OEF,
  148. const Eigen::MatrixXi & OEI,
  149. Eigen::MatrixXd & U,
  150. Eigen::MatrixXi & G,
  151. Eigen::VectorXi & J,
  152. Eigen::VectorXi & I
  153. )
  154. {
  155. // Decimate 1
  156. using namespace Eigen;
  157. using namespace std;
  158. // Working copies
  159. Eigen::MatrixXd V = OV;
  160. Eigen::MatrixXi F = OF;
  161. Eigen::MatrixXi E = OE;
  162. Eigen::VectorXi EMAP = OEMAP;
  163. Eigen::MatrixXi EF = OEF;
  164. Eigen::MatrixXi EI = OEI;
  165. typedef std::set<std::pair<double,int> > PriorityQueue;
  166. PriorityQueue Q;
  167. std::vector<PriorityQueue::iterator > Qit;
  168. Qit.resize(E.rows());
  169. // If an edge were collapsed, we'd collapse it to these points:
  170. MatrixXd C(E.rows(),V.cols());
  171. for(int e = 0;e<E.rows();e++)
  172. {
  173. double cost = e;
  174. RowVectorXd p(1,3);
  175. cost_and_placement(e,V,F,E,EMAP,EF,EI,cost,p);
  176. C.row(e) = p;
  177. Qit[e] = Q.insert(std::pair<double,int>(cost,e)).first;
  178. }
  179. int prev_e = -1;
  180. bool clean_finish = false;
  181. while(true)
  182. {
  183. if(Q.empty())
  184. {
  185. break;
  186. }
  187. if(Q.begin()->first == std::numeric_limits<double>::infinity())
  188. {
  189. // min cost edge is infinite cost
  190. break;
  191. }
  192. int e,e1,e2,f1,f2;
  193. if(collapse_edge(
  194. cost_and_placement, pre_collapse, post_collapse,
  195. V,F,E,EMAP,EF,EI,Q,Qit,C,e,e1,e2,f1,f2))
  196. {
  197. if(stopping_condition(V,F,E,EMAP,EF,EI,Q,Qit,C,e,e1,e2,f1,f2))
  198. {
  199. clean_finish = true;
  200. break;
  201. }
  202. }else
  203. {
  204. if(prev_e == e)
  205. {
  206. assert(false && "Edge collapse no progress... bad stopping condition?");
  207. break;
  208. }
  209. // Edge was not collapsed... must have been invalid. collapse_edge should
  210. // have updated its cost to inf... continue
  211. }
  212. prev_e = e;
  213. }
  214. // remove all IGL_COLLAPSE_EDGE_NULL faces
  215. MatrixXi F2(F.rows(),3);
  216. J.resize(F.rows());
  217. int m = 0;
  218. for(int f = 0;f<F.rows();f++)
  219. {
  220. if(
  221. F(f,0) != IGL_COLLAPSE_EDGE_NULL ||
  222. F(f,1) != IGL_COLLAPSE_EDGE_NULL ||
  223. F(f,2) != IGL_COLLAPSE_EDGE_NULL)
  224. {
  225. F2.row(m) = F.row(f);
  226. J(m) = f;
  227. m++;
  228. }
  229. }
  230. F2.conservativeResize(m,F2.cols());
  231. J.conservativeResize(m);
  232. VectorXi _1;
  233. remove_unreferenced(V,F2,U,G,_1,I);
  234. return clean_finish;
  235. }