outer_hull.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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<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. #warning "EXPERIMENTAL, DO NOT USE"
  209. const int e_cons = (fs == uE(EMAP(e))?-1:1);
  210. const int nfei = (diIM(e) + val + e_cons*(flip(f)?-1:1))%val;
  211. const int max_ne = uE2E[EMAP(e)][nfei];
  212. // Loop over and find max dihedral angle
  213. //int max_ne = -1;
  214. //typename DerivedV::Scalar max_di = -1;
  215. //for(const auto & ne : neighbors)
  216. //{
  217. // const int nf = ne%m;
  218. // if(nf == f)
  219. // {
  220. // continue;
  221. // }
  222. // // Corner of neighbor
  223. // const int nc = ne/m;
  224. // // Is neighbor oriented consistently with (flipped) f?
  225. // //const int ns = F(nf,(nc+1)%3);
  226. // const int nd = F(nf,(nc+2)%3);
  227. // const bool cons = (flip(f)?fd:fs) == nd;
  228. // // Normal after possibly flipping to match flip or orientation of f
  229. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  230. // // Angle between n and f
  231. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  232. // if(ndi>=max_di)
  233. // {
  234. // max_ne = ne;
  235. // max_di = ndi;
  236. // }
  237. //}
  238. //if(max_ne != max_ne_2)
  239. //{
  240. // cout<<(f+1)<<" ---> "<<(max_ne%m)+1<<" != "<<(max_ne_2%m)+1<<" ... "<<e_cons<<endl;
  241. // typename DerivedV::Scalar max_di = -1;
  242. // for(size_t nei = 0;nei<neighbors.size();nei++)
  243. // {
  244. // const auto & ne = neighbors[nei];
  245. // const int nf = ne%m;
  246. // if(nf == f)
  247. // {
  248. // cout<<" "<<(ne%m)+1<<": "<<0<<" "<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  249. // continue;
  250. // }
  251. // // Corner of neighbor
  252. // const int nc = ne/m;
  253. // // Is neighbor oriented consistently with (flipped) f?
  254. // //const int ns = F(nf,(nc+1)%3);
  255. // const int nd = F(nf,(nc+2)%3);
  256. // const bool cons = (flip(f)?fd:fs) == nd;
  257. // // Normal after possibly flipping to match flip or orientation of f
  258. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  259. // // Angle between n and f
  260. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  261. // cout<<" "<<(ne%m)+1<<": "<<ndi<<" "<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  262. // if(ndi>=max_di)
  263. // {
  264. // max_ne = ne;
  265. // max_di = ndi;
  266. // }
  267. // }
  268. //}
  269. if(max_ne>=0)
  270. {
  271. // face of neighbor
  272. const int nf = max_ne%m;
  273. // corner of neighbor
  274. const int nc = max_ne/m;
  275. const int nd = F(nf,(nc+2)%3);
  276. const bool cons = (flip(f)?fd:fs) == nd;
  277. flip(nf) = (cons ? flip(f) : !flip(f));
  278. //cout<<"flip("<<nf<<") = "<<(flip(nf)?"true":"false")<<endl;
  279. const int ne1 = nf+((nc+1)%3)*m;
  280. const int ne2 = nf+((nc+2)%3)*m;
  281. if(!EH[ne1])
  282. {
  283. Q.push(ne1);
  284. }
  285. if(!EH[ne2])
  286. {
  287. Q.push(ne2);
  288. }
  289. }
  290. }
  291. {
  292. vG[id].resize(FHcount,3);
  293. vJ[id].resize(FHcount,1);
  294. //nG += FHcount;
  295. size_t h = 0;
  296. assert(counts(id) == IM.rows());
  297. for(int i = 0;i<counts(id);i++)
  298. {
  299. const size_t f = IM(i);
  300. //if(f_flip)
  301. //{
  302. // flip(f) = !flip(f);
  303. //}
  304. if(FH[f])
  305. {
  306. vG[id].row(h) = (flip(f)?F.row(f).reverse().eval():F.row(f));
  307. vJ[id](h,0) = f;
  308. h++;
  309. }
  310. }
  311. assert((int)h == FHcount);
  312. }
  313. }
  314. // Is A inside B? Assuming A and B are consistently oriented but closed and
  315. // non-intersecting.
  316. const auto & is_component_inside_other = [](
  317. const Eigen::PlainObjectBase<DerivedV> & V,
  318. const MatrixXV & BC,
  319. const MatrixXG & A,
  320. const MatrixXJ & AJ,
  321. const MatrixXG & B)->bool
  322. {
  323. const auto & bounding_box = [](
  324. const Eigen::PlainObjectBase<DerivedV> & V,
  325. const MatrixXG & F)->
  326. MatrixXV
  327. {
  328. MatrixXV BB(2,3);
  329. BB<<
  330. 1e26,1e26,1e26,
  331. -1e26,-1e26,-1e26;
  332. const size_t m = F.rows();
  333. for(size_t f = 0;f<m;f++)
  334. {
  335. for(size_t c = 0;c<3;c++)
  336. {
  337. const auto & vfc = V.row(F(f,c));
  338. BB.row(0) = BB.row(0).array().min(vfc.array()).eval();
  339. BB.row(1) = BB.row(1).array().max(vfc.array()).eval();
  340. }
  341. }
  342. return BB;
  343. };
  344. // A lot of the time we're dealing with unrelated, distant components: cull
  345. // them.
  346. MatrixXV ABB = bounding_box(V,A);
  347. MatrixXV BBB = bounding_box(V,B);
  348. if( (BBB.row(0)-ABB.row(1)).maxCoeff()>0 ||
  349. (ABB.row(0)-BBB.row(1)).maxCoeff()>0 )
  350. {
  351. // bounding boxes do not overlap
  352. return false;
  353. }
  354. ////////////////////////////////////////////////////////////////////////
  355. // POTENTIAL ROBUSTNESS WEAK AREA
  356. ////////////////////////////////////////////////////////////////////////
  357. //
  358. // q could be so close (<~1e-16) to B that the winding number is not a robust way to
  359. // determine inside/outsideness. We could try to find a _better_ q which is
  360. // farther away, but couldn't they all be bad?
  361. MatrixXV q = BC.row(AJ(0));
  362. // In a perfect world, it's enough to test a single point.
  363. double w;
  364. winding_number_3(
  365. V.data(),V.rows(),
  366. B.data(),B.rows(),
  367. q.data(),1,&w);
  368. return fabs(w)>0.5;
  369. };
  370. // Reject components which are completely inside other components
  371. vector<bool> keep(ncc,true);
  372. size_t nG = 0;
  373. // This is O( ncc * ncc * m)
  374. for(size_t id = 0;id<ncc;id++)
  375. {
  376. for(size_t oid = 0;oid<ncc;oid++)
  377. {
  378. if(id == oid)
  379. {
  380. continue;
  381. }
  382. keep[id] = keep[id] &&
  383. !is_component_inside_other(V,BC,vG[id],vJ[id],vG[oid]);
  384. }
  385. if(keep[id])
  386. {
  387. nG += vJ[id].rows();
  388. }
  389. }
  390. // collect G and J across components
  391. G.resize(nG,3);
  392. J.resize(nG,1);
  393. {
  394. size_t off = 0;
  395. for(Index id = 0;id<(Index)ncc;id++)
  396. {
  397. if(keep[id])
  398. {
  399. assert(vG[id].rows() == vJ[id].rows());
  400. G.block(off,0,vG[id].rows(),vG[id].cols()) = vG[id];
  401. J.block(off,0,vJ[id].rows(),vJ[id].cols()) = vJ[id];
  402. off += vG[id].rows();
  403. }
  404. }
  405. }
  406. }
  407. template <
  408. typename DerivedV,
  409. typename DerivedF,
  410. typename DerivedG,
  411. typename DerivedJ,
  412. typename Derivedflip>
  413. IGL_INLINE void igl::outer_hull(
  414. const Eigen::PlainObjectBase<DerivedV> & V,
  415. const Eigen::PlainObjectBase<DerivedF> & F,
  416. Eigen::PlainObjectBase<DerivedG> & G,
  417. Eigen::PlainObjectBase<DerivedJ> & J,
  418. Eigen::PlainObjectBase<Derivedflip> & flip)
  419. {
  420. Eigen::Matrix<typename DerivedV::Scalar,DerivedF::RowsAtCompileTime,3> N;
  421. per_face_normals(V,F,N);
  422. return outer_hull(V,F,N,G,J,flip);
  423. }
  424. #ifdef IGL_STATIC_LIBRARY
  425. // Explicit template specialization
  426. 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> >&);
  427. #endif