outer_hull.cpp 15 KB

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