collapse_edge.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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_COLLAPSE_EDGE_H
  9. #define IGL_COLLAPSE_EDGE_H
  10. #include "igl_inline.h"
  11. #include "COLLAPSE_EDGE_NULL.h"
  12. #include <Eigen/Core>
  13. #include <vector>
  14. namespace igl
  15. {
  16. /// Attempt to collapse a given edge of a mesh. Assumes (V,F) is a closed
  17. /// manifold mesh (except for previously collapsed faces which should be set
  18. /// to: [IGL_COLLAPSE_EDGE_NULL IGL_COLLAPSE_EDGE_NULL
  19. /// IGL_COLLAPSE_EDGE_NULL]. Collapses exactly two faces and exactly 3 edges
  20. /// from E (e and one side of each face gets collapsed to the other). This is
  21. /// implemented in a way that it can be repeatedly called until satisfaction
  22. /// and then the garbage in F can be collected by removing NULL faces.
  23. ///
  24. /// @param[in] e index into E of edge to try to collapse. E(e,:) = [s d] or [d s] so
  25. /// that s<d, then d is collapsed to s.
  26. /// @param[in] p dim list of vertex position where to place merged vertex
  27. /// [mesh inputs]
  28. /// @param[in,out] V #V by dim list of vertex positions, lesser index of E(e,:) will be set
  29. /// to midpoint of edge.
  30. /// @param[in,out] F #F by 3 list of face indices into V.
  31. /// @param[in,out] E #E by 2 list of edge indices into V.
  32. /// @param[in,out] EMAP #F*3 list of indices into E, mapping each directed edge to unique
  33. /// unique edge in E
  34. /// @param[in,out] EF #E by 2 list of edge flaps, EF(e,0)=f means e=(i-->j) is the edge of
  35. /// F(f,:) opposite the vth corner, where EI(e,0)=v. Similarly EF(e,1) "
  36. /// e=(j->i)
  37. /// @param[in,out] EI #E by 2 list of edge flap corners (see above).
  38. /// [mesh inputs]
  39. /// @param[out] e1 index into E of edge collpased on left
  40. /// @param[out] e2 index into E of edge collpased on right
  41. /// @param[out] f1 index into F of face collpased on left
  42. /// @param[out] f2 index into F of face collpased on right
  43. /// @return true if edge was collapsed
  44. ///
  45. ///
  46. /// Define [s,d] = sort(E(e,:)) so that s<d, then d is "detached" from
  47. /// connectivity meaning all faces/edges incident on d will now be incident on
  48. /// s. (This reduces fragmentation by preferring to collapse toward the start
  49. /// of V)¹. If E(e,1)==s then we say the edge is "flipped" (`eflip` true in
  50. /// the implementation).
  51. ///
  52. /// f1 is set to EF(e,0) and f2 is set to EF(e,1). Let v1 be EI(e,0) the
  53. /// corner of F(f1,:) opposite e. _If_ (s<d) then e1 will be the edge after e
  54. /// within f1:
  55. ///
  56. /// s<d
  57. /// ✅s----e-----d☠️
  58. /// \ ← /
  59. /// \ ↘f₁↗ /
  60. /// e₁ /
  61. /// \ /
  62. /// \/
  63. ///
  64. /// _If_ (s>d) then e1 will be the edge after e within f1:
  65. ///
  66. /// s>d
  67. /// ✅s----e-----d☠️
  68. /// \ ← /
  69. /// \ ↘f₁↗ /
  70. /// \ e₁
  71. /// \ /
  72. /// \/
  73. ///
  74. ///
  75. /// ¹Or at least it would if we templated these functions to allow using
  76. /// RowMajor V.
  77. ///
  78. /// It really seems that this callback should provide a meaningful edge on the
  79. /// _new_ mesh. Meanwhile – Oof – You can use this gross mechanism to find the faces incident on the
  80. /// collapsed vertex:
  81. ///
  82. /// ```cpp
  83. /// const auto survivors =
  84. /// [&F,&e,&EMAP](const int f1, const int e1, int & d1)
  85. /// {
  86. /// for(int c=0;c<3;c++)
  87. /// {
  88. /// d1 = EMAP(f1+c*F.rows());
  89. /// if((d1 != e) && (d1 != e1)) { break; }
  90. /// }
  91. /// };
  92. /// int d1,d2;
  93. /// survivors(f1,e1,d1);
  94. /// survivors(f2,e2,d2);
  95. /// // Will circulating by continuing in the CCW direction of E(d1,:)
  96. /// // encircle the common edge? That is, is E(d1,1) the common vertex?
  97. /// const bool ccw = E(d1,1) == E(d2,0) || E(d1,1) == E(d2,1);
  98. /// std::vector<int> Nf;
  99. /// {
  100. /// std::vector<int> Nv;
  101. /// igl::circulation(d1,ccw,F,EMAP,EF,EI,Nv,Nf);
  102. /// }
  103. /// ```
  104. template <
  105. typename Derivedp,
  106. typename DerivedV,
  107. typename DerivedF,
  108. typename DerivedE,
  109. typename DerivedEMAP,
  110. typename DerivedEF,
  111. typename DerivedEI>
  112. IGL_INLINE bool collapse_edge(
  113. const int e,
  114. const Eigen::MatrixBase<Derivedp> & p,
  115. Eigen::MatrixBase<DerivedV> & V,
  116. Eigen::MatrixBase<DerivedF> & F,
  117. Eigen::MatrixBase<DerivedE> & E,
  118. Eigen::MatrixBase<DerivedEMAP> & EMAP,
  119. Eigen::MatrixBase<DerivedEF> & EF,
  120. Eigen::MatrixBase<DerivedEI> & EI,
  121. int & e1,
  122. int & e2,
  123. int & f1,
  124. int & f2);
  125. /// \overload
  126. ///
  127. /// @param[in] Nsv #Nsv vertex circulation around s (see circulation)
  128. /// @param[in] Nsf #Nsf face circulation around s
  129. /// @param[in] Ndv #Ndv vertex circulation around d
  130. /// @param[in] Ndf #Ndf face circulation around d
  131. template
  132. <
  133. typename Derivedp,
  134. typename DerivedV,
  135. typename DerivedF,
  136. typename DerivedE,
  137. typename DerivedEMAP,
  138. typename DerivedEF,
  139. typename DerivedEI>
  140. IGL_INLINE bool collapse_edge(
  141. const int e,
  142. const Eigen::MatrixBase<Derivedp> & p,
  143. /*const*/ std::vector<int> & Nsv,
  144. const std::vector<int> & Nsf,
  145. /*const*/ std::vector<int> & Ndv,
  146. const std::vector<int> & Ndf,
  147. Eigen::MatrixBase<DerivedV> & V,
  148. Eigen::MatrixBase<DerivedF> & F,
  149. Eigen::MatrixBase<DerivedE> & E,
  150. Eigen::MatrixBase<DerivedEMAP> & EMAP,
  151. Eigen::MatrixBase<DerivedEF> & EF,
  152. Eigen::MatrixBase<DerivedEI> & EI,
  153. int & a_e1,
  154. int & a_e2,
  155. int & a_f1,
  156. int & a_f2);
  157. }
  158. #ifndef IGL_STATIC_LIBRARY
  159. # include "collapse_edge.cpp"
  160. #endif
  161. #endif