collapse_edge.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 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 "collapse_edge.h"
  9. #include "circulation.h"
  10. #include "edge_collapse_is_valid.h"
  11. #include <vector>
  12. IGL_INLINE bool igl::collapse_edge(
  13. const int e,
  14. const Eigen::RowVectorXd & p,
  15. Eigen::MatrixXd & V,
  16. Eigen::MatrixXi & F,
  17. Eigen::MatrixXi & E,
  18. Eigen::VectorXi & EMAP,
  19. Eigen::MatrixXi & EF,
  20. Eigen::MatrixXi & EI,
  21. int & a_e1,
  22. int & a_e2,
  23. int & a_f1,
  24. int & a_f2)
  25. {
  26. // Assign this to 0 rather than, say, -1 so that deleted elements will get
  27. // draw as degenerate elements at vertex 0 (which should always exist and
  28. // never get collapsed to anything else since it is the smallest index)
  29. using namespace Eigen;
  30. using namespace std;
  31. const int eflip = E(e,0)>E(e,1);
  32. // source and destination
  33. const int s = eflip?E(e,1):E(e,0);
  34. const int d = eflip?E(e,0):E(e,1);
  35. if(!edge_collapse_is_valid(e,F,E,EMAP,EF,EI))
  36. {
  37. return false;
  38. }
  39. // Important to grab neighbors of d before monkeying with edges
  40. const std::vector<int> nV2Fd = circulation(e,!eflip,F,E,EMAP,EF,EI);
  41. // The following implementation strongly relies on s<d
  42. assert(s<d && "s should be less than d");
  43. // move source and destination to midpoint
  44. V.row(s) = p;
  45. V.row(d) = p;
  46. // Helper function to replace edge and associate information with NULL
  47. const auto & kill_edge = [&E,&EI,&EF](const int e)
  48. {
  49. E(e,0) = IGL_COLLAPSE_EDGE_NULL;
  50. E(e,1) = IGL_COLLAPSE_EDGE_NULL;
  51. EF(e,0) = IGL_COLLAPSE_EDGE_NULL;
  52. EF(e,1) = IGL_COLLAPSE_EDGE_NULL;
  53. EI(e,0) = IGL_COLLAPSE_EDGE_NULL;
  54. EI(e,1) = IGL_COLLAPSE_EDGE_NULL;
  55. };
  56. // update edge info
  57. // for each flap
  58. const int m = F.rows();
  59. for(int side = 0;side<2;side++)
  60. {
  61. const int f = EF(e,side);
  62. const int v = EI(e,side);
  63. const int sign = (eflip==0?1:-1)*(1-2*side);
  64. // next edge emanating from d
  65. const int e1 = EMAP(f+m*((v+sign*1+3)%3));
  66. // prev edge pointing to s
  67. const int e2 = EMAP(f+m*((v+sign*2+3)%3));
  68. assert(E(e1,0) == d || E(e1,1) == d);
  69. assert(E(e2,0) == s || E(e2,1) == s);
  70. // face adjacent to f on e1, also incident on d
  71. const bool flip1 = EF(e1,1)==f;
  72. const int f1 = flip1 ? EF(e1,0) : EF(e1,1);
  73. assert(f1!=f);
  74. assert(F(f1,0)==d || F(f1,1)==d || F(f1,2) == d);
  75. // across from which vertex of f1 does e1 appear?
  76. const int v1 = flip1 ? EI(e1,0) : EI(e1,1);
  77. // Kill e1
  78. kill_edge(e1);
  79. // Kill f
  80. F(f,0) = IGL_COLLAPSE_EDGE_NULL;
  81. F(f,1) = IGL_COLLAPSE_EDGE_NULL;
  82. F(f,2) = IGL_COLLAPSE_EDGE_NULL;
  83. // map f1's edge on e1 to e2
  84. assert(EMAP(f1+m*v1) == e1);
  85. EMAP(f1+m*v1) = e2;
  86. // side opposite f2, the face adjacent to f on e2, also incident on s
  87. const int opp2 = (EF(e2,0)==f?0:1);
  88. assert(EF(e2,opp2) == f);
  89. EF(e2,opp2) = f1;
  90. EI(e2,opp2) = v1;
  91. // remap e2 from d to s
  92. E(e2,0) = E(e2,0)==d ? s : E(e2,0);
  93. E(e2,1) = E(e2,1)==d ? s : E(e2,1);
  94. if(side==0)
  95. {
  96. a_e1 = e1;
  97. a_f1 = f;
  98. }else
  99. {
  100. a_e2 = e1;
  101. a_f2 = f;
  102. }
  103. }
  104. // finally, reindex faces and edges incident on d. Do this last so asserts
  105. // make sense.
  106. //
  107. // Could actually skip first and last, since those are always the two
  108. // collpased faces.
  109. for(auto f : nV2Fd)
  110. {
  111. for(int v = 0;v<3;v++)
  112. {
  113. if(F(f,v) == d)
  114. {
  115. const int flip1 = (EF(EMAP(f+m*((v+1)%3)),0)==f)?1:0;
  116. const int flip2 = (EF(EMAP(f+m*((v+2)%3)),0)==f)?0:1;
  117. assert(
  118. E(EMAP(f+m*((v+1)%3)),flip1) == d ||
  119. E(EMAP(f+m*((v+1)%3)),flip1) == s);
  120. E(EMAP(f+m*((v+1)%3)),flip1) = s;
  121. assert(
  122. E(EMAP(f+m*((v+2)%3)),flip2) == d ||
  123. E(EMAP(f+m*((v+2)%3)),flip2) == s);
  124. E(EMAP(f+m*((v+2)%3)),flip2) = s;
  125. F(f,v) = s;
  126. break;
  127. }
  128. }
  129. }
  130. // Finally, "remove" this edge and its information
  131. kill_edge(e);
  132. return true;
  133. }
  134. IGL_INLINE bool igl::collapse_edge(
  135. const int e,
  136. const Eigen::RowVectorXd & p,
  137. Eigen::MatrixXd & V,
  138. Eigen::MatrixXi & F,
  139. Eigen::MatrixXi & E,
  140. Eigen::VectorXi & EMAP,
  141. Eigen::MatrixXi & EF,
  142. Eigen::MatrixXi & EI)
  143. {
  144. int e1,e2,f1,f2;
  145. return collapse_edge(e,p,V,F,E,EMAP,EF,EI,e1,e2,f1,f2);
  146. }
  147. IGL_INLINE bool igl::collapse_edge(
  148. const std::function<void(
  149. const int,
  150. const Eigen::MatrixXd &,
  151. const Eigen::MatrixXi &,
  152. const Eigen::MatrixXi &,
  153. const Eigen::VectorXi &,
  154. const Eigen::MatrixXi &,
  155. const Eigen::MatrixXi &,
  156. double &,
  157. Eigen::RowVectorXd &)> & cost_and_placement,
  158. Eigen::MatrixXd & V,
  159. Eigen::MatrixXi & F,
  160. Eigen::MatrixXi & E,
  161. Eigen::VectorXi & EMAP,
  162. Eigen::MatrixXi & EF,
  163. Eigen::MatrixXi & EI,
  164. std::set<std::pair<double,int> > & Q,
  165. std::vector<std::set<std::pair<double,int> >::iterator > & Qit,
  166. Eigen::MatrixXd & C)
  167. {
  168. int e,e1,e2,f1,f2;
  169. return
  170. collapse_edge(cost_and_placement,V,F,E,EMAP,EF,EI,Q,Qit,C,e,e1,e2,f1,f2);
  171. }
  172. IGL_INLINE bool igl::collapse_edge(
  173. const std::function<void(
  174. const int,
  175. const Eigen::MatrixXd &,
  176. const Eigen::MatrixXi &,
  177. const Eigen::MatrixXi &,
  178. const Eigen::VectorXi &,
  179. const Eigen::MatrixXi &,
  180. const Eigen::MatrixXi &,
  181. double &,
  182. Eigen::RowVectorXd &)> & cost_and_placement,
  183. Eigen::MatrixXd & V,
  184. Eigen::MatrixXi & F,
  185. Eigen::MatrixXi & E,
  186. Eigen::VectorXi & EMAP,
  187. Eigen::MatrixXi & EF,
  188. Eigen::MatrixXi & EI,
  189. std::set<std::pair<double,int> > & Q,
  190. std::vector<std::set<std::pair<double,int> >::iterator > & Qit,
  191. Eigen::MatrixXd & C,
  192. int & e,
  193. int & e1,
  194. int & e2,
  195. int & f1,
  196. int & f2)
  197. {
  198. using namespace Eigen;
  199. if(Q.empty())
  200. {
  201. // no edges to collapse
  202. return false;
  203. }
  204. std::pair<double,int> p = *(Q.begin());
  205. if(p.first == std::numeric_limits<double>::infinity())
  206. {
  207. // min cost edge is infinite cost
  208. return false;
  209. }
  210. Q.erase(Q.begin());
  211. e = p.second;
  212. Qit[e] = Q.end();
  213. std::vector<int> N = circulation(e, true,F,E,EMAP,EF,EI);
  214. std::vector<int> Nd = circulation(e,false,F,E,EMAP,EF,EI);
  215. N.insert(N.begin(),Nd.begin(),Nd.end());
  216. const bool collapsed =
  217. collapse_edge(e,C.row(e),V,F,E,EMAP,EF,EI,e1,e2,f1,f2);
  218. if(collapsed)
  219. {
  220. // Erase the two, other collapsed edges
  221. Q.erase(Qit[e1]);
  222. Qit[e1] = Q.end();
  223. Q.erase(Qit[e2]);
  224. Qit[e2] = Q.end();
  225. // update local neighbors
  226. // loop over original face neighbors
  227. for(auto n : N)
  228. {
  229. if(F(n,0) != IGL_COLLAPSE_EDGE_NULL ||
  230. F(n,1) != IGL_COLLAPSE_EDGE_NULL ||
  231. F(n,2) != IGL_COLLAPSE_EDGE_NULL)
  232. {
  233. for(int v = 0;v<3;v++)
  234. {
  235. // get edge id
  236. const int ei = EMAP(v*F.rows()+n);
  237. // erase old entry
  238. Q.erase(Qit[ei]);
  239. // compute cost and potential placement
  240. double cost;
  241. RowVectorXd place;
  242. cost_and_placement(ei,V,F,E,EMAP,EF,EI,cost,place);
  243. // Replace in queue
  244. Qit[ei] = Q.insert(std::pair<double,int>(cost,ei)).first;
  245. C.row(ei) = place;
  246. }
  247. }
  248. }
  249. }else
  250. {
  251. // reinsert with infinite weight (the provided cost function must **not**
  252. // have given this un-collapsable edge inf cost already)
  253. p.first = std::numeric_limits<double>::infinity();
  254. Qit[e] = Q.insert(p).first;
  255. }
  256. return collapsed;
  257. }