decimate.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #ifndef IGL_DECIMATE_H
  9. #define IGL_DECIMATE_H
  10. #include "igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <vector>
  13. #include <set>
  14. namespace igl
  15. {
  16. // Assumes (V,F) is a manifold mesh (possibly with boundary) Collapses edges
  17. // until desired number of faces is achieved. This uses default edge cost and
  18. // merged vertex placement functions {edge length, edge midpoint}.
  19. //
  20. // Inputs:
  21. // V #V by dim list of vertex positions
  22. // F #F by 3 list of face indices into V.
  23. // max_m desired number of output faces
  24. // Outputs:
  25. // U #U by dim list of output vertex posistions (can be same ref as V)
  26. // G #G by 3 list of output face indices into U (can be same ref as G)
  27. // J #G list of indices into F of birth face
  28. // I #U list of indices into V of birth vertices
  29. // Returns true if m was reached (otherwise #G > m)
  30. IGL_INLINE bool decimate(
  31. const Eigen::MatrixXd & V,
  32. const Eigen::MatrixXi & F,
  33. const size_t max_m,
  34. Eigen::MatrixXd & U,
  35. Eigen::MatrixXi & G,
  36. Eigen::VectorXi & J,
  37. Eigen::VectorXi & I);
  38. // Inputs:
  39. // V #V by dim list of vertex positions
  40. // F #F by 3 list of face indices into V.
  41. // max_m desired number of output faces
  42. // Outputs:
  43. // U #U by dim list of output vertex posistions (can be same ref as V)
  44. // G #G by 3 list of output face indices into U (can be same ref as G)
  45. // J #G list of indices into F of birth face
  46. // Returns true if m was reached (otherwise #G > m)
  47. IGL_INLINE bool decimate(
  48. const Eigen::MatrixXd & V,
  49. const Eigen::MatrixXi & F,
  50. const size_t max_m,
  51. Eigen::MatrixXd & U,
  52. Eigen::MatrixXi & G,
  53. Eigen::VectorXi & J);
  54. // Assumes a **closed** manifold mesh. See igl::connect_boundary_to_infinity
  55. // and igl::decimate in decimate.cpp
  56. // is handling meshes with boundary by connecting all boundary edges with
  57. // dummy facets to infinity **and** modifying the stopping criteria.
  58. //
  59. // Inputs:
  60. // cost_and_placement function computing cost of collapsing an edge and 3d
  61. // position where it should be placed:
  62. // cost_and_placement(V,F,E,EMAP,EF,EI,cost,placement);
  63. // stopping_condition function returning whether to stop collapsing edges
  64. // based on current state. Guaranteed to be called after _successfully_
  65. // collapsing edge e removing edges (e,e1,e2) and faces (f1,f2):
  66. // bool should_stop =
  67. // stopping_condition(V,F,E,EMAP,EF,EI,Q,Qit,C,e,e1,e2,f1,f2);
  68. IGL_INLINE bool decimate(
  69. const Eigen::MatrixXd & V,
  70. const Eigen::MatrixXi & F,
  71. const std::function<void(
  72. const int /*e*/,
  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. double & /*cost*/,
  80. Eigen::RowVectorXd & /*p*/
  81. )> & cost_and_placement,
  82. const std::function<bool(
  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. const int ,/*e1*/
  94. const int ,/*e2*/
  95. const int ,/*f1*/
  96. const int /*f2*/
  97. )> & stopping_condition,
  98. Eigen::MatrixXd & U,
  99. Eigen::MatrixXi & G,
  100. Eigen::VectorXi & J,
  101. Eigen::VectorXi & I);
  102. // Inputs:
  103. // pre_collapse callback called with index of edge whose collapse is about
  104. // to be attempted (see collapse_edge)
  105. // post_collapse callback called with index of edge whose collapse was
  106. // just attempted and a flag revealing whether this was successful (see
  107. // collapse_edge)
  108. IGL_INLINE bool decimate(
  109. const Eigen::MatrixXd & V,
  110. const Eigen::MatrixXi & F,
  111. const std::function<void(
  112. const int /*e*/,
  113. const Eigen::MatrixXd &/*V*/,
  114. const Eigen::MatrixXi &/*F*/,
  115. const Eigen::MatrixXi &/*E*/,
  116. const Eigen::VectorXi &/*EMAP*/,
  117. const Eigen::MatrixXi &/*EF*/,
  118. const Eigen::MatrixXi &/*EI*/,
  119. double & /*cost*/,
  120. Eigen::RowVectorXd & /*p*/
  121. )> & cost_and_placement,
  122. const std::function<bool(
  123. const Eigen::MatrixXd & ,/*V*/
  124. const Eigen::MatrixXi & ,/*F*/
  125. const Eigen::MatrixXi & ,/*E*/
  126. const Eigen::VectorXi & ,/*EMAP*/
  127. const Eigen::MatrixXi & ,/*EF*/
  128. const Eigen::MatrixXi & ,/*EI*/
  129. const std::set<std::pair<double,int> > & ,/*Q*/
  130. const std::vector<std::set<std::pair<double,int> >::iterator > &,/*Qit*/
  131. const Eigen::MatrixXd & ,/*C*/
  132. const int ,/*e*/
  133. const int ,/*e1*/
  134. const int ,/*e2*/
  135. const int ,/*f1*/
  136. const int /*f2*/
  137. )> & stopping_condition,
  138. const std::function<bool(
  139. const Eigen::MatrixXd & ,/*V*/
  140. const Eigen::MatrixXi & ,/*F*/
  141. const Eigen::MatrixXi & ,/*E*/
  142. const Eigen::VectorXi & ,/*EMAP*/
  143. const Eigen::MatrixXi & ,/*EF*/
  144. const Eigen::MatrixXi & ,/*EI*/
  145. const std::set<std::pair<double,int> > & ,/*Q*/
  146. const std::vector<std::set<std::pair<double,int> >::iterator > &,/*Qit*/
  147. const Eigen::MatrixXd & ,/*C*/
  148. const int /*e*/
  149. )> & pre_collapse,
  150. const std::function<void(
  151. const Eigen::MatrixXd & , /*V*/
  152. const Eigen::MatrixXi & , /*F*/
  153. const Eigen::MatrixXi & , /*E*/
  154. const Eigen::VectorXi & ,/*EMAP*/
  155. const Eigen::MatrixXi & , /*EF*/
  156. const Eigen::MatrixXi & , /*EI*/
  157. const std::set<std::pair<double,int> > & , /*Q*/
  158. const std::vector<std::set<std::pair<double,int> >::iterator > &, /*Qit*/
  159. const Eigen::MatrixXd & , /*C*/
  160. const int , /*e*/
  161. const int , /*e1*/
  162. const int , /*e2*/
  163. const int , /*f1*/
  164. const int , /*f2*/
  165. const bool /*collapsed*/
  166. )> & post_collapse,
  167. Eigen::MatrixXd & U,
  168. Eigen::MatrixXi & G,
  169. Eigen::VectorXi & J,
  170. Eigen::VectorXi & I);
  171. // Inputs:
  172. // EMAP #F*3 list of indices into E, mapping each directed edge to unique
  173. // unique edge in E
  174. // EF #E by 2 list of edge flaps, EF(e,0)=f means e=(i-->j) is the edge of
  175. // F(f,:) opposite the vth corner, where EI(e,0)=v. Similarly EF(e,1) "
  176. // e=(j->i)
  177. // EI #E by 2 list of edge flap corners (see above).
  178. IGL_INLINE bool decimate(
  179. const Eigen::MatrixXd & V,
  180. const Eigen::MatrixXi & F,
  181. const std::function<void(
  182. const int /*e*/,
  183. const Eigen::MatrixXd &/*V*/,
  184. const Eigen::MatrixXi &/*F*/,
  185. const Eigen::MatrixXi &/*E*/,
  186. const Eigen::VectorXi &/*EMAP*/,
  187. const Eigen::MatrixXi &/*EF*/,
  188. const Eigen::MatrixXi &/*EI*/,
  189. double & /*cost*/,
  190. Eigen::RowVectorXd & /*p*/
  191. )> & cost_and_placement,
  192. const std::function<bool(
  193. const Eigen::MatrixXd & ,/*V*/
  194. const Eigen::MatrixXi & ,/*F*/
  195. const Eigen::MatrixXi & ,/*E*/
  196. const Eigen::VectorXi & ,/*EMAP*/
  197. const Eigen::MatrixXi & ,/*EF*/
  198. const Eigen::MatrixXi & ,/*EI*/
  199. const std::set<std::pair<double,int> > & ,/*Q*/
  200. const std::vector<std::set<std::pair<double,int> >::iterator > &,/*Qit*/
  201. const Eigen::MatrixXd & ,/*C*/
  202. const int ,/*e*/
  203. const int ,/*e1*/
  204. const int ,/*e2*/
  205. const int ,/*f1*/
  206. const int /*f2*/
  207. )> & stopping_condition,
  208. const std::function<bool(
  209. const Eigen::MatrixXd & ,/*V*/
  210. const Eigen::MatrixXi & ,/*F*/
  211. const Eigen::MatrixXi & ,/*E*/
  212. const Eigen::VectorXi & ,/*EMAP*/
  213. const Eigen::MatrixXi & ,/*EF*/
  214. const Eigen::MatrixXi & ,/*EI*/
  215. const std::set<std::pair<double,int> > & ,/*Q*/
  216. const std::vector<std::set<std::pair<double,int> >::iterator > &,/*Qit*/
  217. const Eigen::MatrixXd & ,/*C*/
  218. const int /*e*/
  219. )> & pre_collapse,
  220. const std::function<void(
  221. const Eigen::MatrixXd & , /*V*/
  222. const Eigen::MatrixXi & , /*F*/
  223. const Eigen::MatrixXi & , /*E*/
  224. const Eigen::VectorXi & ,/*EMAP*/
  225. const Eigen::MatrixXi & , /*EF*/
  226. const Eigen::MatrixXi & , /*EI*/
  227. const std::set<std::pair<double,int> > & , /*Q*/
  228. const std::vector<std::set<std::pair<double,int> >::iterator > &, /*Qit*/
  229. const Eigen::MatrixXd & , /*C*/
  230. const int , /*e*/
  231. const int , /*e1*/
  232. const int , /*e2*/
  233. const int , /*f1*/
  234. const int , /*f2*/
  235. const bool /*collapsed*/
  236. )> & post_collapse,
  237. const Eigen::MatrixXi & E,
  238. const Eigen::VectorXi & EMAP,
  239. const Eigen::MatrixXi & EF,
  240. const Eigen::MatrixXi & EI,
  241. Eigen::MatrixXd & U,
  242. Eigen::MatrixXi & G,
  243. Eigen::VectorXi & J,
  244. Eigen::VectorXi & I);
  245. }
  246. #ifndef IGL_STATIC_LIBRARY
  247. # include "decimate.cpp"
  248. #endif
  249. #endif