outer_hull.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #include "outer_hull.h"
  2. #include "outer_facet.h"
  3. #include "sort.h"
  4. #include "facet_components.h"
  5. #include "winding_number.h"
  6. #include "triangle_triangle_adjacency.h"
  7. #include "unique_edge_map.h"
  8. #include "barycenter.h"
  9. #include "per_face_normals.h"
  10. #include "all_edges.h"
  11. #include "colon.h"
  12. #include "get_seconds.h"
  13. #include <Eigen/Geometry>
  14. #include <vector>
  15. #include <map>
  16. #include <queue>
  17. #include <iostream>
  18. //#define IGL_OUTER_HULL_DEBUG
  19. template <
  20. typename DerivedV,
  21. typename DerivedF,
  22. typename DerivedN,
  23. typename DerivedG,
  24. typename DerivedJ,
  25. typename Derivedflip>
  26. IGL_INLINE void igl::outer_hull(
  27. const Eigen::PlainObjectBase<DerivedV> & V,
  28. const Eigen::PlainObjectBase<DerivedF> & F,
  29. const Eigen::PlainObjectBase<DerivedN> & N,
  30. Eigen::PlainObjectBase<DerivedG> & G,
  31. Eigen::PlainObjectBase<DerivedJ> & J,
  32. Eigen::PlainObjectBase<Derivedflip> & flip)
  33. {
  34. using namespace Eigen;
  35. using namespace std;
  36. using namespace igl;
  37. typedef typename DerivedF::Index Index;
  38. Matrix<Index,DerivedF::RowsAtCompileTime,1> C;
  39. typedef Matrix<typename DerivedV::Scalar,Dynamic,DerivedV::ColsAtCompileTime> MatrixXV;
  40. typedef Matrix<typename DerivedF::Scalar,Dynamic,DerivedF::ColsAtCompileTime> MatrixXF;
  41. typedef Matrix<typename DerivedG::Scalar,Dynamic,DerivedG::ColsAtCompileTime> MatrixXG;
  42. typedef Matrix<typename DerivedJ::Scalar,Dynamic,DerivedJ::ColsAtCompileTime> MatrixXJ;
  43. const Index m = F.rows();
  44. #ifdef IGL_OUTER_HULL_DEBUG
  45. cout<<"outer hull..."<<endl;
  46. #endif
  47. #ifdef IGL_OUTER_HULL_DEBUG
  48. cout<<"edge map..."<<endl;
  49. #endif
  50. typedef Matrix<typename DerivedF::Scalar,Dynamic,2> MatrixX2I;
  51. typedef Matrix<typename DerivedF::Index,Dynamic,1> VectorXI;
  52. MatrixX2I E,uE;
  53. VectorXI EMAP;
  54. vector<vector<typename DerivedF::Index> > uE2E;
  55. unique_edge_map(F,E,uE,EMAP,uE2E);
  56. // TODO:
  57. // uE --> face-edge index, sorted CCW around edge according to normal
  58. // uE --> sorted order index
  59. // uE --> bool, whether needed to flip face to make "consistent" with unique
  60. // edge
  61. VectorXI diIM(3*m);
  62. vector<vector<typename DerivedV::Scalar> > di(uE2E.size());
  63. // For each list of face-edges incide on a unique edge
  64. for(size_t ui = 0;ui<(size_t)uE.rows();ui++)
  65. {
  66. const typename DerivedF::Scalar ud = uE(ui,1);
  67. const typename DerivedF::Scalar us = uE(ui,0);
  68. // Base normal vector to orient against
  69. const auto fe0 = uE2E[ui][0];
  70. const auto & eVp = N.row(fe0%m);
  71. di[ui].resize(uE2E[ui].size());
  72. const typename DerivedF::Scalar d = F(fe0%m,((fe0/m)+2)%3);
  73. const typename DerivedF::Scalar s = F(fe0%m,((fe0/m)+1)%3);
  74. // Edge vector
  75. //const auto & eV = (V.row(ud)-V.row(us)).normalized();
  76. const auto & eV = (V.row(d)-V.row(s)).normalized();
  77. // Loop over incident face edges
  78. for(size_t fei = 0;fei<uE2E[ui].size();fei++)
  79. {
  80. const auto & fe = uE2E[ui][fei];
  81. const auto f = fe % m;
  82. const auto c = fe / m;
  83. // source should match destination to be consistent
  84. const bool cons = (d == F(f,(c+1)%3));
  85. assert(cons || (d == F(f,(c+2)%3)));
  86. assert(!cons || (s == F(f,(c+2)%3)));
  87. assert(!cons || (d == F(f,(c+1)%3)));
  88. // Angle between n and f
  89. const auto & n = N.row(f);
  90. di[ui][fei] = M_PI - atan2( eVp.cross(n).dot(eV), eVp.dot(n));
  91. if(!cons)
  92. {
  93. di[ui][fei] = di[ui][fei] + M_PI;
  94. if(di[ui][fei]>2.*M_PI)
  95. {
  96. di[ui][fei] = di[ui][fei] - 2.*M_PI;
  97. }
  98. }
  99. }
  100. vector<size_t> IM;
  101. igl::sort(di[ui],true,di[ui],IM);
  102. // copy old list
  103. vector<typename DerivedF::Index> temp = uE2E[ui];
  104. for(size_t fei = 0;fei<uE2E[ui].size();fei++)
  105. {
  106. uE2E[ui][fei] = temp[IM[fei]];
  107. const auto & fe = uE2E[ui][fei];
  108. diIM(fe) = fei;
  109. }
  110. }
  111. vector<vector<vector<Index > > > TT,_1;
  112. triangle_triangle_adjacency(E,EMAP,uE2E,false,TT,_1);
  113. VectorXI counts;
  114. #ifdef IGL_OUTER_HULL_DEBUG
  115. cout<<"facet components..."<<endl;
  116. #endif
  117. facet_components(TT,C,counts);
  118. assert(C.maxCoeff()+1 == counts.rows());
  119. const size_t ncc = counts.rows();
  120. G.resize(0,F.cols());
  121. J.resize(0,1);
  122. flip.setConstant(m,1,false);
  123. #ifdef IGL_OUTER_HULL_DEBUG
  124. cout<<"reindex..."<<endl;
  125. #endif
  126. // H contains list of faces on outer hull;
  127. vector<bool> FH(m,false);
  128. vector<bool> EH(3*m,false);
  129. vector<MatrixXG> vG(ncc);
  130. vector<MatrixXJ> vJ(ncc);
  131. vector<MatrixXJ> vIM(ncc);
  132. for(size_t id = 0;id<ncc;id++)
  133. {
  134. vIM[id].resize(counts[id],1);
  135. }
  136. // current index into each IM
  137. vector<size_t> g(ncc,0);
  138. // place order of each face in its respective component
  139. for(Index f = 0;f<m;f++)
  140. {
  141. vIM[C(f)](g[C(f)]++) = f;
  142. }
  143. #ifdef IGL_OUTER_HULL_DEBUG
  144. cout<<"barycenters..."<<endl;
  145. #endif
  146. // assumes that "resolve" has handled any coplanar cases correctly and nearly
  147. // coplanar cases can be sorted based on barycenter.
  148. MatrixXV BC;
  149. barycenter(V,F,BC);
  150. #ifdef IGL_OUTER_HULL_DEBUG
  151. cout<<"loop over CCs (="<<ncc<<")..."<<endl;
  152. #endif
  153. for(Index id = 0;id<(Index)ncc;id++)
  154. {
  155. auto & IM = vIM[id];
  156. // starting face that's guaranteed to be on the outer hull and in this
  157. // component
  158. int f;
  159. bool f_flip;
  160. #ifdef IGL_OUTER_HULL_DEBUG
  161. cout<<"outer facet..."<<endl;
  162. #endif
  163. outer_facet(V,F,N,IM,f,f_flip);
  164. int FHcount = 0;
  165. // Q contains list of face edges to continue traversing upong
  166. queue<int> Q;
  167. Q.push(f+0*m);
  168. Q.push(f+1*m);
  169. Q.push(f+2*m);
  170. flip(f) = f_flip;
  171. //cout<<"flip("<<f<<") = "<<(flip(f)?"true":"false")<<endl;
  172. #ifdef IGL_OUTER_HULL_DEBUG
  173. cout<<"BFS..."<<endl;
  174. #endif
  175. while(!Q.empty())
  176. {
  177. // face-edge
  178. const int e = Q.front();
  179. Q.pop();
  180. // face
  181. const int f = e%m;
  182. // corner
  183. const int c = e/m;
  184. // Should never see edge again...
  185. if(EH[e] == true)
  186. {
  187. continue;
  188. }
  189. EH[e] = true;
  190. // first time seeing face
  191. if(!FH[f])
  192. {
  193. FH[f] = true;
  194. FHcount++;
  195. }
  196. // find overlapping face-edges
  197. const auto & neighbors = uE2E[EMAP(e)];
  198. // normal after possible flipping
  199. const auto & fN = (flip(f)?-1.:1.)*N.row(f);
  200. // source of edge according to f
  201. const int fs = flip(f)?F(f,(c+2)%3):F(f,(c+1)%3);
  202. // destination of edge according to f
  203. const int fd = flip(f)?F(f,(c+1)%3):F(f,(c+2)%3);
  204. // Edge vector according to f's (flipped) orientation.
  205. const auto & eV = (V.row(fd)-V.row(fs)).normalized();
  206. // edge valence
  207. const size_t val = uE2E[EMAP(e)].size();
  208. const auto ui = EMAP(e);
  209. const auto fe0 = uE2E[ui][0];
  210. const auto es = F(fe0%m,((fe0/m)+1)%3);
  211. const int e_cons = (fs == es?-1:1);
  212. const int nfei = (diIM(e) + val + e_cons*(flip(f)?-1:1))%val;
  213. const int max_ne_2 = uE2E[EMAP(e)][nfei];
  214. // Loop over and find max dihedral angle
  215. int max_ne = -1;
  216. // // SAVE THIS OLD IMPLEMENTATION FOR NOW
  217. //typename DerivedV::Scalar max_di = -1;
  218. //for(const auto & ne : neighbors)
  219. //{
  220. // const int nf = ne%m;
  221. // if(nf == f)
  222. // {
  223. // continue;
  224. // }
  225. // // Corner of neighbor
  226. // const int nc = ne/m;
  227. // // Is neighbor oriented consistently with (flipped) f?
  228. // //const int ns = F(nf,(nc+1)%3);
  229. // const int nd = F(nf,(nc+2)%3);
  230. // const bool cons = (flip(f)?fd:fs) == nd;
  231. // // Normal after possibly flipping to match flip or orientation of f
  232. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  233. // // Angle between n and f
  234. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  235. // if(ndi>=max_di)
  236. // {
  237. // max_ne = ne;
  238. // max_di = ndi;
  239. // }
  240. //}
  241. ////cout<<(max_ne != max_ne_2)<<" =?= "<<e_cons<<endl;
  242. //if(max_ne != max_ne_2)
  243. //{
  244. // cout<<(f+1)<<" ---> "<<(max_ne%m)+1<<" != "<<(max_ne_2%m)+1<<" ... "<<e_cons<<endl;
  245. // typename DerivedV::Scalar max_di = -1;
  246. // for(size_t nei = 0;nei<neighbors.size();nei++)
  247. // {
  248. // const auto & ne = neighbors[nei];
  249. // const int nf = ne%m;
  250. // if(nf == f)
  251. // {
  252. // cout<<" "<<(ne%m)+1<<": "<<0<<" "<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  253. // continue;
  254. // }
  255. // // Corner of neighbor
  256. // const int nc = ne/m;
  257. // // Is neighbor oriented consistently with (flipped) f?
  258. // //const int ns = F(nf,(nc+1)%3);
  259. // const int nd = F(nf,(nc+2)%3);
  260. // const bool cons = (flip(f)?fd:fs) == nd;
  261. // // Normal after possibly flipping to match flip or orientation of f
  262. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  263. // // Angle between n and f
  264. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  265. // cout<<" "<<(ne%m)+1<<": "<<ndi<<" "<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  266. // if(ndi>=max_di)
  267. // {
  268. // max_ne = ne;
  269. // max_di = ndi;
  270. // }
  271. // }
  272. //}
  273. max_ne = max_ne_2;
  274. if(max_ne>=0)
  275. {
  276. // face of neighbor
  277. const int nf = max_ne%m;
  278. // corner of neighbor
  279. const int nc = max_ne/m;
  280. const int nd = F(nf,(nc+2)%3);
  281. const bool cons = (flip(f)?fd:fs) == nd;
  282. flip(nf) = (cons ? flip(f) : !flip(f));
  283. //cout<<"flip("<<nf<<") = "<<(flip(nf)?"true":"false")<<endl;
  284. const int ne1 = nf+((nc+1)%3)*m;
  285. const int ne2 = nf+((nc+2)%3)*m;
  286. if(!EH[ne1])
  287. {
  288. Q.push(ne1);
  289. }
  290. if(!EH[ne2])
  291. {
  292. Q.push(ne2);
  293. }
  294. }
  295. }
  296. {
  297. vG[id].resize(FHcount,3);
  298. vJ[id].resize(FHcount,1);
  299. //nG += FHcount;
  300. size_t h = 0;
  301. assert(counts(id) == IM.rows());
  302. for(int i = 0;i<counts(id);i++)
  303. {
  304. const size_t f = IM(i);
  305. //if(f_flip)
  306. //{
  307. // flip(f) = !flip(f);
  308. //}
  309. if(FH[f])
  310. {
  311. vG[id].row(h) = (flip(f)?F.row(f).reverse().eval():F.row(f));
  312. vJ[id](h,0) = f;
  313. h++;
  314. }
  315. }
  316. assert((int)h == FHcount);
  317. }
  318. }
  319. // Is A inside B? Assuming A and B are consistently oriented but closed and
  320. // non-intersecting.
  321. const auto & is_component_inside_other = [](
  322. const Eigen::PlainObjectBase<DerivedV> & V,
  323. const MatrixXV & BC,
  324. const MatrixXG & A,
  325. const MatrixXJ & AJ,
  326. const MatrixXG & B)->bool
  327. {
  328. const auto & bounding_box = [](
  329. const Eigen::PlainObjectBase<DerivedV> & V,
  330. const MatrixXG & F)->
  331. MatrixXV
  332. {
  333. MatrixXV BB(2,3);
  334. BB<<
  335. 1e26,1e26,1e26,
  336. -1e26,-1e26,-1e26;
  337. const size_t m = F.rows();
  338. for(size_t f = 0;f<m;f++)
  339. {
  340. for(size_t c = 0;c<3;c++)
  341. {
  342. const auto & vfc = V.row(F(f,c));
  343. BB.row(0) = BB.row(0).array().min(vfc.array()).eval();
  344. BB.row(1) = BB.row(1).array().max(vfc.array()).eval();
  345. }
  346. }
  347. return BB;
  348. };
  349. // A lot of the time we're dealing with unrelated, distant components: cull
  350. // them.
  351. MatrixXV ABB = bounding_box(V,A);
  352. MatrixXV BBB = bounding_box(V,B);
  353. if( (BBB.row(0)-ABB.row(1)).maxCoeff()>0 ||
  354. (ABB.row(0)-BBB.row(1)).maxCoeff()>0 )
  355. {
  356. // bounding boxes do not overlap
  357. return false;
  358. }
  359. ////////////////////////////////////////////////////////////////////////
  360. // POTENTIAL ROBUSTNESS WEAK AREA
  361. ////////////////////////////////////////////////////////////////////////
  362. //
  363. // q could be so close (<~1e-16) to B that the winding number is not a robust way to
  364. // determine inside/outsideness. We could try to find a _better_ q which is
  365. // farther away, but couldn't they all be bad?
  366. MatrixXV q = BC.row(AJ(0));
  367. // In a perfect world, it's enough to test a single point.
  368. double w;
  369. winding_number_3(
  370. V.data(),V.rows(),
  371. B.data(),B.rows(),
  372. q.data(),1,&w);
  373. return fabs(w)>0.5;
  374. };
  375. // Reject components which are completely inside other components
  376. vector<bool> keep(ncc,true);
  377. size_t nG = 0;
  378. // This is O( ncc * ncc * m)
  379. for(size_t id = 0;id<ncc;id++)
  380. {
  381. for(size_t oid = 0;oid<ncc;oid++)
  382. {
  383. if(id == oid)
  384. {
  385. continue;
  386. }
  387. keep[id] = keep[id] &&
  388. !is_component_inside_other(V,BC,vG[id],vJ[id],vG[oid]);
  389. }
  390. if(keep[id])
  391. {
  392. nG += vJ[id].rows();
  393. }
  394. }
  395. // collect G and J across components
  396. G.resize(nG,3);
  397. J.resize(nG,1);
  398. {
  399. size_t off = 0;
  400. for(Index id = 0;id<(Index)ncc;id++)
  401. {
  402. if(keep[id])
  403. {
  404. assert(vG[id].rows() == vJ[id].rows());
  405. G.block(off,0,vG[id].rows(),vG[id].cols()) = vG[id];
  406. J.block(off,0,vJ[id].rows(),vJ[id].cols()) = vJ[id];
  407. off += vG[id].rows();
  408. }
  409. }
  410. }
  411. }
  412. template <
  413. typename DerivedV,
  414. typename DerivedF,
  415. typename DerivedG,
  416. typename DerivedJ,
  417. typename Derivedflip>
  418. IGL_INLINE void igl::outer_hull(
  419. const Eigen::PlainObjectBase<DerivedV> & V,
  420. const Eigen::PlainObjectBase<DerivedF> & F,
  421. Eigen::PlainObjectBase<DerivedG> & G,
  422. Eigen::PlainObjectBase<DerivedJ> & J,
  423. Eigen::PlainObjectBase<Derivedflip> & flip)
  424. {
  425. Eigen::Matrix<typename DerivedV::Scalar,DerivedF::RowsAtCompileTime,3> N;
  426. per_face_normals(V,F,N);
  427. return outer_hull(V,F,N,G,J,flip);
  428. }
  429. #ifdef IGL_STATIC_LIBRARY
  430. // Explicit template specialization
  431. template void igl::outer_hull<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  432. #endif