decimate.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef IGL_DECIMATE_H
  9. #define IGL_DECIMATE_H
  10. #include "igl_inline.h"
  11. #include "decimate_callback_types.h"
  12. #include "COLLAPSE_EDGE_NULL.h"
  13. #include <Eigen/Core>
  14. /// @file decimate.h
  15. ///
  16. /// igl::decimate implements a customizable greedy edge collapser using a priority-queue:
  17. ///
  18. /// Q ← {}
  19. /// for each edge e
  20. /// cost[e], placement[e] ← cost_and_placement(e)
  21. /// Q.update( e, cost[e] )
  22. ///
  23. /// while Q not empty
  24. /// e ← Q.pop()
  25. /// if cost[e] is finite
  26. /// collapse_happened_flag = false
  27. /// if pre_collapse( e )
  28. /// collapse_happened_flag = collapse_edge( e )
  29. /// post_collapse( e, collapse_happened_flag )
  30. /// if collapse_happened_flag
  31. /// for each neighbor edge f
  32. /// cost[f], placement[f] ← cost_and_placement(f)
  33. /// Q.update( f, cost[f] )
  34. /// if stopping_condition()
  35. /// break
  36. /// else
  37. /// cost[ e ] = ∞
  38. /// Q.update( e, cost[e] )
  39. ///
  40. ///
  41. /// There are four important callbacks that can be customized (decimate_callback_types.h). Note that any of these functions can capture user-defined book-keeping variables.
  42. ///
  43. /// `cost_and_placement` This function is called for every original edge before
  44. /// the queue processing starts and then on the “neighbors” (edges in the
  45. /// two-ring) of a successfully collapsed edge. The function should output a
  46. /// cost that would be paid if the given edge were to be collapsed to a single
  47. /// vertex and the positional placement of that new vertex. Outputting a cost of
  48. /// ∞ guarantees that the edge will not be collapsed.
  49. ///
  50. /// `pre_collapse` This function is called just before `collapse_edge` is
  51. /// attempted. If this function returns false then the collapse is aborted. This
  52. /// callback is an opportunity for callers to conduct a final check whether the
  53. /// collapse should really take place (e.g., based on the most current
  54. /// information, which may not be available at the time that
  55. /// `cost_and_placement` was called). This is rarely necessary and its preferred
  56. /// to use cost_and_placement to assign uncollapsible edges ∞ cost if possible.
  57. ///
  58. /// `post_collapse` This function is called after `collapse_edge` is attempted.
  59. /// Since this collapse may have failed (e.g., it would create a non-manifold
  60. /// mesh). This callback is provided a flag whether the attempted collapse
  61. /// actually occurred. This callback is an opportunity for callers to update any
  62. /// data-structures/book-keeping to acknowledge the successful (or failed)
  63. /// collapse.
  64. ///
  65. /// `stopping_condition` This function is called after a successful call to
  66. /// `collapse_edge`. If this function returns true then the entire
  67. /// queue-processing ends (e.g., if the number of remaining faces is below a
  68. /// user’s threshold).
  69. ///
  70. /// \see
  71. /// collapse_least_cost_edge
  72. /// collapse_edge
  73. /// qslim
  74. namespace igl
  75. {
  76. /// Assumes (V,F) is a manifold mesh (possibly with boundary) collapses edges
  77. /// until desired number of faces is achieved. This uses default edge cost and
  78. /// merged vertex placement functions {edge length, edge midpoint}.
  79. ///
  80. /// See \fileinfo for more details.
  81. ///
  82. /// @param[in] V #V by dim list of vertex positions
  83. /// @param[in] F #F by 3 list of face indices into V.
  84. /// @param[in] max_m desired number of output faces
  85. /// @param[in] block_intersections whether to block intersections (see
  86. /// intersection_blocking_collapse_edge_callbacks)
  87. /// @param[out] U #U by dim list of output vertex posistions (can be same ref as V)
  88. /// @param[out] G #G by 3 list of output face indices into U (can be same ref as G)
  89. /// @param[out] J #G list of indices into F of birth face
  90. /// @param[out] I #U list of indices into V of birth vertices
  91. /// @return true if m was reached (otherwise #G > m)
  92. IGL_INLINE bool decimate(
  93. const Eigen::MatrixXd & V,
  94. const Eigen::MatrixXi & F,
  95. const int max_m,
  96. const bool block_intersections,
  97. Eigen::MatrixXd & U,
  98. Eigen::MatrixXi & G,
  99. Eigen::VectorXi & J,
  100. Eigen::VectorXi & I);
  101. /// Collapses edges of a **closed manifold mesh** (V,F) using user defined
  102. /// callbacks in a priority queue. Functions control the cost and placement of each collapse the
  103. /// stopping criteria for queue processing and the callbacks for pre and post
  104. /// collapse operations. See the first implementation in decimate.cpp for an
  105. /// example of how to deal with open/non-manifold meshes and how to adjust
  106. /// cost and placement functions accordingly.
  107. ///
  108. /// See \fileinfo for more details.
  109. ///
  110. /// @param[in] V #V by dim list of vertex positions
  111. /// @param[in] F #F by 3 list of face indices into V.
  112. /// @param[in] cost_and_placement function computing cost of collapsing an edge and 3d
  113. /// position where it should be placed:
  114. /// cost_and_placement(V,F,E,EMAP,EF,EI,cost,placement);
  115. /// @param[in] stopping_condition function returning whether to stop collapsing edges
  116. /// based on current state. Guaranteed to be called after _successfully_
  117. /// collapsing edge e removing edges (e,e1,e2) and faces (f1,f2):
  118. /// bool should_stop =
  119. /// stopping_condition(V,F,E,EMAP,EF,EI,Q,Qit,C,e,e1,e2,f1,f2);
  120. /// @param[in] pre_collapse callback called with index of edge whose collapse is about
  121. /// to be attempted (see collapse_least_cost_edge)
  122. /// @param[in] post_collapse callback called with index of edge whose collapse was
  123. /// just attempted and a flag revealing whether this was successful (see
  124. /// collapse_least_cost_edge)
  125. /// @param[out] U #U by dim list of output vertex posistions (can be same ref as V)
  126. /// @param[out] G #G by 3 list of output face indices into U (can be same ref as G)
  127. /// @param[out] J #G list of indices into F of birth face
  128. /// @param[out] I #U list of indices into V of birth vertices
  129. /// @return true if m was reached (otherwise #G > m)
  130. ///
  131. /// \see connect_boundary_to_infinity
  132. ///
  133. /// \bug E,EMAP,EF,EI seem to be immediately (re)computed from F. These inputs
  134. /// are unused and it's not clear why.
  135. IGL_INLINE bool decimate(
  136. const Eigen::MatrixXd & V,
  137. const Eigen::MatrixXi & F,
  138. const decimate_cost_and_placement_callback & cost_and_placement,
  139. const decimate_stopping_condition_callback & stopping_condition,
  140. const decimate_pre_collapse_callback & pre_collapse,
  141. const decimate_post_collapse_callback & post_collapse,
  142. Eigen::MatrixXd & U,
  143. Eigen::MatrixXi & G,
  144. Eigen::VectorXi & J,
  145. Eigen::VectorXi & I);
  146. }
  147. #ifndef IGL_STATIC_LIBRARY
  148. # include "decimate.cpp"
  149. #endif
  150. #endif