outer_hull.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #ifdef IGL_OUTER_HULL_DEBUG
  166. cout<<"outer facet: "<<f<<endl;
  167. #endif
  168. int FHcount = 0;
  169. // Q contains list of face edges to continue traversing upong
  170. queue<int> Q;
  171. Q.push(f+0*m);
  172. Q.push(f+1*m);
  173. Q.push(f+2*m);
  174. flip(f) = f_flip;
  175. //cout<<"flip("<<f<<") = "<<(flip(f)?"true":"false")<<endl;
  176. #ifdef IGL_OUTER_HULL_DEBUG
  177. cout<<"BFS..."<<endl;
  178. #endif
  179. while(!Q.empty())
  180. {
  181. // face-edge
  182. const int e = Q.front();
  183. Q.pop();
  184. // face
  185. const int f = e%m;
  186. // corner
  187. const int c = e/m;
  188. // Should never see edge again...
  189. if(EH[e] == true)
  190. {
  191. continue;
  192. }
  193. EH[e] = true;
  194. // first time seeing face
  195. if(!FH[f])
  196. {
  197. FH[f] = true;
  198. FHcount++;
  199. }
  200. // find overlapping face-edges
  201. const auto & neighbors = uE2E[EMAP(e)];
  202. // normal after possible flipping
  203. const auto & fN = (flip(f)?-1.:1.)*N.row(f);
  204. // source of edge according to f
  205. const int fs = flip(f)?F(f,(c+2)%3):F(f,(c+1)%3);
  206. // destination of edge according to f
  207. const int fd = flip(f)?F(f,(c+1)%3):F(f,(c+2)%3);
  208. // Edge vector according to f's (flipped) orientation.
  209. const auto & eV = (V.row(fd)-V.row(fs)).normalized();
  210. // edge valence
  211. const size_t val = uE2E[EMAP(e)].size();
  212. //#warning "EXPERIMENTAL, DO NOT USE"
  213. //// THIS IS WRONG! The first face is---after sorting---no longer the face
  214. //// used for orienting the sort.
  215. //const auto ui = EMAP(e);
  216. //const auto fe0 = uE2E[ui][0];
  217. //const auto es = F(fe0%m,((fe0/m)+1)%3);
  218. const int e_cons = (dicons(e) ? 1: -1);
  219. const int nfei = (diIM(e) + val + e_cons*(flip(f)?-1:1))%val;
  220. const int max_ne_2 = uE2E[EMAP(e)][nfei];
  221. int max_ne = -1;
  222. //// Loop over and find max dihedral angle
  223. //typename DerivedV::Scalar max_di = -1;
  224. //for(const auto & ne : neighbors)
  225. //{
  226. // const int nf = ne%m;
  227. // if(nf == f)
  228. // {
  229. // continue;
  230. // }
  231. // // Corner of neighbor
  232. // const int nc = ne/m;
  233. // // Is neighbor oriented consistently with (flipped) f?
  234. // //const int ns = F(nf,(nc+1)%3);
  235. // const int nd = F(nf,(nc+2)%3);
  236. // const bool cons = (flip(f)?fd:fs) == nd;
  237. // // Normal after possibly flipping to match flip or orientation of f
  238. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  239. // // Angle between n and f
  240. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  241. // if(ndi>=max_di)
  242. // {
  243. // max_ne = ne;
  244. // max_di = ndi;
  245. // }
  246. //}
  247. ////cout<<(max_ne != max_ne_2)<<" =?= "<<e_cons<<endl;
  248. //if(max_ne != max_ne_2)
  249. //{
  250. // cout<<(f+1)<<" ---> "<<(max_ne%m)+1<<" != "<<(max_ne_2%m)+1<<" ... "<<e_cons<<" "<<flip(f)<<endl;
  251. // typename DerivedV::Scalar max_di = -1;
  252. // for(size_t nei = 0;nei<neighbors.size();nei++)
  253. // {
  254. // const auto & ne = neighbors[nei];
  255. // const int nf = ne%m;
  256. // if(nf == f)
  257. // {
  258. // cout<<" "<<(ne%m)+1<<":\t"<<0<<"\t"<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  259. // continue;
  260. // }
  261. // // Corner of neighbor
  262. // const int nc = ne/m;
  263. // // Is neighbor oriented consistently with (flipped) f?
  264. // //const int ns = F(nf,(nc+1)%3);
  265. // const int nd = F(nf,(nc+2)%3);
  266. // const bool cons = (flip(f)?fd:fs) == nd;
  267. // // Normal after possibly flipping to match flip or orientation of f
  268. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  269. // // Angle between n and f
  270. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  271. // cout<<" "<<(ne%m)+1<<":\t"<<ndi<<"\t"<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  272. // if(ndi>=max_di)
  273. // {
  274. // max_ne = ne;
  275. // max_di = ndi;
  276. // }
  277. // }
  278. //}
  279. max_ne = max_ne_2;
  280. if(max_ne>=0)
  281. {
  282. // face of neighbor
  283. const int nf = max_ne%m;
  284. // corner of neighbor
  285. const int nc = max_ne/m;
  286. const int nd = F(nf,(nc+2)%3);
  287. const bool cons = (flip(f)?fd:fs) == nd;
  288. flip(nf) = (cons ? flip(f) : !flip(f));
  289. //cout<<"flip("<<nf<<") = "<<(flip(nf)?"true":"false")<<endl;
  290. const int ne1 = nf+((nc+1)%3)*m;
  291. const int ne2 = nf+((nc+2)%3)*m;
  292. if(!EH[ne1])
  293. {
  294. Q.push(ne1);
  295. }
  296. if(!EH[ne2])
  297. {
  298. Q.push(ne2);
  299. }
  300. }
  301. }
  302. {
  303. vG[id].resize(FHcount,3);
  304. vJ[id].resize(FHcount,1);
  305. //nG += FHcount;
  306. size_t h = 0;
  307. assert(counts(id) == IM.rows());
  308. for(int i = 0;i<counts(id);i++)
  309. {
  310. const size_t f = IM(i);
  311. //if(f_flip)
  312. //{
  313. // flip(f) = !flip(f);
  314. //}
  315. if(FH[f])
  316. {
  317. vG[id].row(h) = (flip(f)?F.row(f).reverse().eval():F.row(f));
  318. vJ[id](h,0) = f;
  319. h++;
  320. }
  321. }
  322. assert((int)h == FHcount);
  323. }
  324. }
  325. // Is A inside B? Assuming A and B are consistently oriented but closed and
  326. // non-intersecting.
  327. const auto & is_component_inside_other = [](
  328. const Eigen::PlainObjectBase<DerivedV> & V,
  329. const MatrixXV & BC,
  330. const MatrixXG & A,
  331. const MatrixXJ & AJ,
  332. const MatrixXG & B)->bool
  333. {
  334. const auto & bounding_box = [](
  335. const Eigen::PlainObjectBase<DerivedV> & V,
  336. const MatrixXG & F)->
  337. MatrixXV
  338. {
  339. MatrixXV BB(2,3);
  340. BB<<
  341. 1e26,1e26,1e26,
  342. -1e26,-1e26,-1e26;
  343. const size_t m = F.rows();
  344. for(size_t f = 0;f<m;f++)
  345. {
  346. for(size_t c = 0;c<3;c++)
  347. {
  348. const auto & vfc = V.row(F(f,c));
  349. BB.row(0) = BB.row(0).array().min(vfc.array()).eval();
  350. BB.row(1) = BB.row(1).array().max(vfc.array()).eval();
  351. }
  352. }
  353. return BB;
  354. };
  355. // A lot of the time we're dealing with unrelated, distant components: cull
  356. // them.
  357. MatrixXV ABB = bounding_box(V,A);
  358. MatrixXV BBB = bounding_box(V,B);
  359. if( (BBB.row(0)-ABB.row(1)).maxCoeff()>0 ||
  360. (ABB.row(0)-BBB.row(1)).maxCoeff()>0 )
  361. {
  362. // bounding boxes do not overlap
  363. return false;
  364. }
  365. ////////////////////////////////////////////////////////////////////////
  366. // POTENTIAL ROBUSTNESS WEAK AREA
  367. ////////////////////////////////////////////////////////////////////////
  368. //
  369. // q could be so close (<~1e-16) to B that the winding number is not a robust way to
  370. // determine inside/outsideness. We could try to find a _better_ q which is
  371. // farther away, but couldn't they all be bad?
  372. MatrixXV q = BC.row(AJ(0));
  373. // In a perfect world, it's enough to test a single point.
  374. double w;
  375. // winding_number_3 expects colmajor
  376. const typename DerivedV::Scalar * Vdata;
  377. Vdata = V.data();
  378. Matrix<
  379. typename DerivedV::Scalar,
  380. DerivedV::RowsAtCompileTime,
  381. DerivedV::ColsAtCompileTime,
  382. ColMajor> Vcol;
  383. if(DerivedV::IsRowMajor)
  384. {
  385. // copy to convert to colmajor
  386. Vcol = V;
  387. Vdata = Vcol.data();
  388. }
  389. winding_number_3(
  390. Vdata,V.rows(),
  391. B.data(),B.rows(),
  392. q.data(),1,&w);
  393. return fabs(w)>0.5;
  394. };
  395. // Reject components which are completely inside other components
  396. vector<bool> keep(ncc,true);
  397. size_t nG = 0;
  398. // This is O( ncc * ncc * m)
  399. for(size_t id = 0;id<ncc;id++)
  400. {
  401. for(size_t oid = 0;oid<ncc;oid++)
  402. {
  403. if(id == oid)
  404. {
  405. continue;
  406. }
  407. const bool inside = is_component_inside_other(V,BC,vG[id],vJ[id],vG[oid]);
  408. #ifdef IGL_OUTER_HULL_DEBUG
  409. cout<<id<<" is inside "<<oid<<" ? "<<inside<<endl;
  410. #endif
  411. keep[id] = keep[id] && !inside;
  412. }
  413. if(keep[id])
  414. {
  415. nG += vJ[id].rows();
  416. }
  417. }
  418. // collect G and J across components
  419. G.resize(nG,3);
  420. J.resize(nG,1);
  421. {
  422. size_t off = 0;
  423. for(Index id = 0;id<(Index)ncc;id++)
  424. {
  425. if(keep[id])
  426. {
  427. assert(vG[id].rows() == vJ[id].rows());
  428. G.block(off,0,vG[id].rows(),vG[id].cols()) = vG[id];
  429. J.block(off,0,vJ[id].rows(),vJ[id].cols()) = vJ[id];
  430. off += vG[id].rows();
  431. }
  432. }
  433. }
  434. }
  435. template <
  436. typename DerivedV,
  437. typename DerivedF,
  438. typename DerivedG,
  439. typename DerivedJ,
  440. typename Derivedflip>
  441. IGL_INLINE void igl::outer_hull(
  442. const Eigen::PlainObjectBase<DerivedV> & V,
  443. const Eigen::PlainObjectBase<DerivedF> & F,
  444. Eigen::PlainObjectBase<DerivedG> & G,
  445. Eigen::PlainObjectBase<DerivedJ> & J,
  446. Eigen::PlainObjectBase<Derivedflip> & flip)
  447. {
  448. Eigen::Matrix<typename DerivedV::Scalar,DerivedF::RowsAtCompileTime,3> N;
  449. per_face_normals(V,F,N);
  450. return outer_hull(V,F,N,G,J,flip);
  451. }
  452. #ifdef IGL_STATIC_LIBRARY
  453. // Explicit template specialization
  454. 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> >&);
  455. 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> >&);
  456. 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> >&);
  457. #endif