outer_hull.cpp 14 KB

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