outer_hull.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. #include "outer_hull.h"
  2. #include "outer_facet.h"
  3. #include "sortrows.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 "writePLY.h"
  11. #include <Eigen/Geometry>
  12. #include <vector>
  13. #include <map>
  14. #include <queue>
  15. #include <iostream>
  16. //#define IGL_OUTER_HULL_DEBUG
  17. template <
  18. typename DerivedV,
  19. typename DerivedF,
  20. typename DerivedN,
  21. typename DerivedG,
  22. typename DerivedJ,
  23. typename Derivedflip>
  24. IGL_INLINE void igl::outer_hull(
  25. const Eigen::PlainObjectBase<DerivedV> & V,
  26. const Eigen::PlainObjectBase<DerivedF> & F,
  27. const Eigen::PlainObjectBase<DerivedN> & N,
  28. Eigen::PlainObjectBase<DerivedG> & G,
  29. Eigen::PlainObjectBase<DerivedJ> & J,
  30. Eigen::PlainObjectBase<Derivedflip> & flip)
  31. {
  32. #ifdef IGL_OUTER_HULL_DEBUG
  33. std::cerr << "Extracting outer hull" << std::endl;
  34. std::cerr << F << std::endl;
  35. writePLY("outer_hull_input.ply", V, F);
  36. #endif
  37. using namespace Eigen;
  38. using namespace std;
  39. typedef typename DerivedF::Index Index;
  40. Matrix<Index,DerivedF::RowsAtCompileTime,1> C;
  41. typedef Matrix<typename DerivedV::Scalar,Dynamic,DerivedV::ColsAtCompileTime> MatrixXV;
  42. typedef Matrix<typename DerivedF::Scalar,Dynamic,DerivedF::ColsAtCompileTime> MatrixXF;
  43. typedef Matrix<typename DerivedG::Scalar,Dynamic,DerivedG::ColsAtCompileTime> MatrixXG;
  44. typedef Matrix<typename DerivedJ::Scalar,Dynamic,DerivedJ::ColsAtCompileTime> MatrixXJ;
  45. typedef Matrix<typename DerivedN::Scalar,1,3> RowVector3N;
  46. const Index m = F.rows();
  47. const auto & duplicate_simplex = [&F](const int f, const int g)->bool
  48. {
  49. return
  50. (F(f,0) == F(g,0) && F(f,1) == F(g,1) && F(f,2) == F(g,2)) ||
  51. (F(f,1) == F(g,0) && F(f,2) == F(g,1) && F(f,0) == F(g,2)) ||
  52. (F(f,2) == F(g,0) && F(f,0) == F(g,1) && F(f,1) == F(g,2)) ||
  53. (F(f,0) == F(g,2) && F(f,1) == F(g,1) && F(f,2) == F(g,0)) ||
  54. (F(f,1) == F(g,2) && F(f,2) == F(g,1) && F(f,0) == F(g,0)) ||
  55. (F(f,2) == F(g,2) && F(f,0) == F(g,1) && F(f,1) == F(g,0));
  56. };
  57. #ifdef IGL_OUTER_HULL_DEBUG
  58. cout<<"outer hull..."<<endl;
  59. #endif
  60. #ifdef IGL_OUTER_HULL_DEBUG
  61. cout<<"edge map..."<<endl;
  62. #endif
  63. typedef Matrix<typename DerivedF::Scalar,Dynamic,2> MatrixX2I;
  64. typedef Matrix<typename DerivedF::Index,Dynamic,1> VectorXI;
  65. typedef Matrix<typename DerivedV::Scalar, 3, 1> Vector3F;
  66. MatrixX2I E,uE;
  67. VectorXI EMAP;
  68. vector<vector<typename DerivedF::Index> > uE2E;
  69. unique_edge_map(F,E,uE,EMAP,uE2E);
  70. #ifdef IGL_OUTER_HULL_DEBUG
  71. for (size_t ui=0; ui<uE.rows(); ui++) {
  72. std::cout << ui << ": " << uE2E[ui].size() << " -- (";
  73. for (size_t i=0; i<uE2E[ui].size(); i++) {
  74. std::cout << uE2E[ui][i] << ", ";
  75. }
  76. std::cout << ")" << std::endl;
  77. }
  78. #endif
  79. // TODO:
  80. // uE --> face-edge index, sorted CCW around edge according to normal
  81. // uE --> sorted order index
  82. // uE --> bool, whether needed to flip face to make "consistent" with unique
  83. // edge
  84. // Place order of each half-edge in its corresponding sorted list around edge
  85. VectorXI diIM(3*m);
  86. // Whether face's edge used for sorting is consistent with unique edge
  87. VectorXI dicons(3*m);
  88. // dihedral angles of faces around edge with face of edge in dicons
  89. vector<vector<typename DerivedV::Scalar> > di(uE2E.size());
  90. // For each list of face-edges incide on a unique edge
  91. for(size_t ui = 0;ui<(size_t)uE.rows();ui++)
  92. {
  93. // Base normal vector to orient against
  94. const auto fe0 = uE2E[ui][0];
  95. const RowVector3N & eVp = N.row(fe0%m);
  96. MatrixXd di_I(uE2E[ui].size(),2);
  97. const typename DerivedF::Scalar o = F(fe0%m, fe0/m);
  98. const typename DerivedF::Scalar d = F(fe0%m,((fe0/m)+2)%3);
  99. const typename DerivedF::Scalar s = F(fe0%m,((fe0/m)+1)%3);
  100. // Edge vector
  101. auto eV = (V.row(d)-V.row(s)).normalized();
  102. auto edge_len = (V.row(d) - V.row(s)).norm();
  103. auto edge_valance = uE2E[ui].size();
  104. assert(edge_valance % 2 == 0);
  105. bool degenerated = !eV.allFinite() || edge_len < 1e-12;
  106. #ifdef IGL_OUTER_HULL_DEBUG
  107. if (degenerated && edge_valance > 2) {
  108. cerr.precision(30);
  109. std::cerr << ui << ": " << (V.row(d) - V.row(s)).norm() << std::endl;
  110. std::cerr << "Edge valance: " << edge_valance << std::endl;
  111. std::cerr << V.row(d) << std::endl;
  112. std::cerr << V.row(s) << std::endl;
  113. }
  114. #endif
  115. if (degenerated) {
  116. const size_t num_adj_faces = uE2E[ui].size();
  117. Eigen::Matrix<typename DerivedV::Scalar, Eigen::Dynamic, 3>
  118. normals(num_adj_faces, 3);
  119. for (size_t fei=0; fei<num_adj_faces; fei++) {
  120. const auto & fe = uE2E[ui][fei];
  121. const auto f = fe % m;
  122. const RowVector3N & n = N.row(f);
  123. normals.row(fei) = n;
  124. }
  125. for (size_t i=0; i<num_adj_faces; i++) {
  126. size_t j = (i+1) % num_adj_faces;
  127. eV = normals.row(i).cross(normals.row(j));
  128. auto length = eV.norm();
  129. if (length > 1e-12) {
  130. eV /= length;
  131. break;
  132. }
  133. }
  134. }
  135. if (!eV.allFinite() || eV.norm() < 1e-12) {
  136. //cerr << "This is bad... all adj face normals are colinear" << std::endl;
  137. eV.setZero();
  138. }
  139. if (degenerated){
  140. // Adjust edge direction.
  141. Vector3F in_face_vec = V.row(o) - V.row(s);
  142. Vector3F edge = eV;
  143. if (edge.cross(in_face_vec).dot(eVp) < 0) {
  144. #ifdef IGL_OUTER_HULL_DEBUG
  145. cerr << "Flipping edge..." << std::endl;
  146. #endif
  147. eV *= -1;
  148. }
  149. //cerr << "Resolved: " << eV << std::endl;
  150. }
  151. vector<bool> cons(uE2E[ui].size());
  152. // Loop over incident face edges
  153. for(size_t fei = 0;fei<uE2E[ui].size();fei++)
  154. {
  155. const auto & fe = uE2E[ui][fei];
  156. const auto f = fe % m;
  157. const auto c = fe / m;
  158. // source should match destination to be consistent
  159. cons[fei] = (d == F(f,(c+1)%3));
  160. assert( cons[fei] || (d == F(f,(c+2)%3)));
  161. assert(!cons[fei] || (s == F(f,(c+2)%3)));
  162. assert(!cons[fei] || (d == F(f,(c+1)%3)));
  163. // Angle between n and f
  164. const RowVector3N & n = N.row(f);
  165. //di_I(fei,0) = M_PI - atan2( eVp.cross(n).dot(eV), eVp.dot(n));
  166. di_I(fei,0) = -atan2( eVp.cross(n).dot(eV), eVp.dot(n));
  167. #ifdef IGL_OUTER_HULL_DEBUG
  168. if(di_I(fei,0) != di_I(fei,0) )
  169. {
  170. cout<<"NaN from face: "<<(f+1)<<endl;
  171. cout<<" n: "<<n<<endl;
  172. cout<<" eVp: "<<eVp<<endl;
  173. cout<<" eV: "<<eV<<endl;
  174. cout<<" eVp x n . eV: "<<(eVp.cross(n).dot(eV))<<endl;
  175. cout<<" eVp . n: "<<(eVp.dot(n))<<endl;
  176. }
  177. #endif
  178. assert(di_I(fei,0) == di_I(fei,0) && "NaN Alert!");
  179. //if(!cons[fei])
  180. //{
  181. // di_I(fei,0) = di_I(fei,0) + M_PI;
  182. // if(di_I(fei,0)>=2.*M_PI)
  183. // {
  184. // di_I(fei,0) = di_I(fei,0) - 2.*M_PI;
  185. // }
  186. // std::cout << " + M_PI";
  187. //}
  188. if (cons[fei]) {
  189. di_I(fei, 0) -= M_PI;
  190. }
  191. // This signing is very important to make sure different edges sort
  192. // duplicate faces the same way, regardless of their orientations
  193. di_I(fei,1) = (cons[fei]?1.:-1.)*(f+1);
  194. }
  195. #if 0
  196. // Despite the effort to get stable normals the atan2 up doesn't
  197. // compute (exactly) -θ for -n if it computes θ for n. So just
  198. // explicitly check if there's a duplicate face
  199. // Shitty O(val^2) implementation
  200. for(size_t fei = 0;fei<uE2E[ui].size();fei++)
  201. {
  202. const auto & fe = uE2E[ui][fei];
  203. const auto f = fe % m;
  204. for(size_t gei = fei+1;gei<uE2E[ui].size();gei++)
  205. {
  206. const auto & ge = uE2E[ui][gei];
  207. const auto g = ge % m;
  208. if(duplicate_simplex(f,g))
  209. {
  210. #ifdef IGL_OUTER_HULL_DEBUG
  211. cout<<"Forcing duplicate: "<<(f+1)<<","<<(g+1)<<endl;
  212. #endif
  213. di_I(gei,0) = di_I(fei,0);
  214. }
  215. }
  216. }
  217. #endif
  218. VectorXi IM;
  219. //igl::sort(di[ui],true,di[ui],IM);
  220. // Sort, but break ties using "signed index" to ensure that duplicates
  221. // always show up in same order.
  222. MatrixXd s_di_I;
  223. igl::sortrows(di_I,true,s_di_I,IM);
  224. di[ui].resize(uE2E[ui].size());
  225. for(size_t i = 0;i<di[ui].size();i++)
  226. {
  227. di[ui][i] = s_di_I(i,0);
  228. }
  229. // copy old list
  230. vector<typename DerivedF::Index> temp = uE2E[ui];
  231. for(size_t fei = 0;fei<uE2E[ui].size();fei++)
  232. {
  233. uE2E[ui][fei] = temp[IM(fei)];
  234. const auto & fe = uE2E[ui][fei];
  235. diIM(fe) = fei;
  236. dicons(fe) = cons[IM(fei)];
  237. }
  238. }
  239. vector<vector<vector<Index > > > TT,_1;
  240. triangle_triangle_adjacency(E,EMAP,uE2E,false,TT,_1);
  241. VectorXI counts;
  242. #ifdef IGL_OUTER_HULL_DEBUG
  243. cout<<"facet components..."<<endl;
  244. #endif
  245. facet_components(TT,C,counts);
  246. assert(C.maxCoeff()+1 == counts.rows());
  247. const size_t ncc = counts.rows();
  248. G.resize(0,F.cols());
  249. J.resize(0,1);
  250. flip.setConstant(m,1,false);
  251. #ifdef IGL_OUTER_HULL_DEBUG
  252. cout<<"reindex..."<<endl;
  253. #endif
  254. // H contains list of faces on outer hull;
  255. vector<bool> FH(m,false);
  256. vector<bool> EH(3*m,false);
  257. vector<MatrixXG> vG(ncc);
  258. vector<MatrixXJ> vJ(ncc);
  259. vector<MatrixXJ> vIM(ncc);
  260. for(size_t id = 0;id<ncc;id++)
  261. {
  262. vIM[id].resize(counts[id],1);
  263. }
  264. // current index into each IM
  265. vector<size_t> g(ncc,0);
  266. // place order of each face in its respective component
  267. for(Index f = 0;f<m;f++)
  268. {
  269. vIM[C(f)](g[C(f)]++) = f;
  270. }
  271. #ifdef IGL_OUTER_HULL_DEBUG
  272. cout<<"barycenters..."<<endl;
  273. #endif
  274. // assumes that "resolve" has handled any coplanar cases correctly and nearly
  275. // coplanar cases can be sorted based on barycenter.
  276. MatrixXV BC;
  277. barycenter(V,F,BC);
  278. #ifdef IGL_OUTER_HULL_DEBUG
  279. cout<<"loop over CCs (="<<ncc<<")..."<<endl;
  280. #endif
  281. for(Index id = 0;id<(Index)ncc;id++)
  282. {
  283. auto & IM = vIM[id];
  284. // starting face that's guaranteed to be on the outer hull and in this
  285. // component
  286. int f;
  287. bool f_flip;
  288. #ifdef IGL_OUTER_HULL_DEBUG
  289. cout<<"outer facet..."<<endl;
  290. #endif
  291. outer_facet(V,F,N,IM,f,f_flip);
  292. #ifdef IGL_OUTER_HULL_DEBUG
  293. cout<<"outer facet: "<<f<<endl;
  294. #endif
  295. int FHcount = 1;
  296. FH[f] = true;
  297. // Q contains list of face edges to continue traversing upong
  298. queue<int> Q;
  299. Q.push(f+0*m);
  300. Q.push(f+1*m);
  301. Q.push(f+2*m);
  302. flip(f) = f_flip;
  303. //cout<<"flip("<<f<<") = "<<(flip(f)?"true":"false")<<endl;
  304. #ifdef IGL_OUTER_HULL_DEBUG
  305. cout<<"BFS..."<<endl;
  306. #endif
  307. while(!Q.empty())
  308. {
  309. // face-edge
  310. const int e = Q.front();
  311. Q.pop();
  312. // face
  313. const int f = e%m;
  314. // corner
  315. const int c = e/m;
  316. // Should never see edge again...
  317. if(EH[e] == true)
  318. {
  319. continue;
  320. }
  321. EH[e] = true;
  322. // source of edge according to f
  323. const int fs = flip(f)?F(f,(c+2)%3):F(f,(c+1)%3);
  324. // destination of edge according to f
  325. const int fd = flip(f)?F(f,(c+1)%3):F(f,(c+2)%3);
  326. // edge valence
  327. const size_t val = uE2E[EMAP(e)].size();
  328. //// find overlapping face-edges
  329. //const auto & neighbors = uE2E[EMAP(e)];
  330. //// normal after possible flipping
  331. //const auto & fN = (flip(f)?-1.:1.)*N.row(f);
  332. //// Edge vector according to f's (flipped) orientation.
  333. ////const auto & eV = (V.row(fd)-V.row(fs)).normalized();
  334. //#warning "EXPERIMENTAL, DO NOT USE"
  335. //// THIS IS WRONG! The first face is---after sorting---no longer the face
  336. //// used for orienting the sort.
  337. //const auto ui = EMAP(e);
  338. //const auto fe0 = uE2E[ui][0];
  339. //const auto es = F(fe0%m,((fe0/m)+1)%3);
  340. // is edge consistent with edge of face used for sorting
  341. const int e_cons = (dicons(e) ? 1: -1);
  342. int nfei = -1;
  343. // Loop once around trying to find suitable next face
  344. for(size_t step = 1; step<val+2;step++)
  345. {
  346. const int nfei_new = (diIM(e) + 2*val + e_cons*step*(flip(f)?-1:1))%val;
  347. const int nf = uE2E[EMAP(e)][nfei_new] % m;
  348. // Don't consider faces with identical dihedral angles
  349. if((di[EMAP(e)][diIM(e)] != di[EMAP(e)][nfei_new]))
  350. //#warning "THIS IS HACK, FIX ME"
  351. // if( abs(di[EMAP(e)][diIM(e)] - di[EMAP(e)][nfei_new]) < 1e-16 )
  352. {
  353. #ifdef IGL_OUTER_HULL_DEBUG
  354. cout<<"Next facet: "<<(f+1)<<" --> "<<(nf+1)<<", |"<<
  355. di[EMAP(e)][diIM(e)]<<" - "<<di[EMAP(e)][nfei_new]<<"| = "<<
  356. abs(di[EMAP(e)][diIM(e)] - di[EMAP(e)][nfei_new])
  357. <<endl;
  358. #endif
  359. // Only use this face if not already seen
  360. if(!FH[nf])
  361. {
  362. nfei = nfei_new;
  363. }
  364. break;
  365. }
  366. //#ifdef IGL_OUTER_HULL_DEBUG
  367. // cout<<"Skipping co-planar facet: "<<(f+1)<<" --> "<<(nf+1)<<endl;
  368. //#endif
  369. }
  370. int max_ne = -1;
  371. //// Loop over and find max dihedral angle
  372. //typename DerivedV::Scalar max_di = -1;
  373. //for(const auto & ne : neighbors)
  374. //{
  375. // const int nf = ne%m;
  376. // if(nf == f)
  377. // {
  378. // continue;
  379. // }
  380. // // Corner of neighbor
  381. // const int nc = ne/m;
  382. // // Is neighbor oriented consistently with (flipped) f?
  383. // //const int ns = F(nf,(nc+1)%3);
  384. // const int nd = F(nf,(nc+2)%3);
  385. // const bool cons = (flip(f)?fd:fs) == nd;
  386. // // Normal after possibly flipping to match flip or orientation of f
  387. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  388. // // Angle between n and f
  389. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  390. // if(ndi>=max_di)
  391. // {
  392. // max_ne = ne;
  393. // max_di = ndi;
  394. // }
  395. //}
  396. ////cout<<(max_ne != max_ne_2)<<" =?= "<<e_cons<<endl;
  397. //if(max_ne != max_ne_2)
  398. //{
  399. // cout<<(f+1)<<" ---> "<<(max_ne%m)+1<<" != "<<(max_ne_2%m)+1<<" ... "<<e_cons<<" "<<flip(f)<<endl;
  400. // typename DerivedV::Scalar max_di = -1;
  401. // for(size_t nei = 0;nei<neighbors.size();nei++)
  402. // {
  403. // const auto & ne = neighbors[nei];
  404. // const int nf = ne%m;
  405. // if(nf == f)
  406. // {
  407. // cout<<" "<<(ne%m)+1<<":\t"<<0<<"\t"<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  408. // continue;
  409. // }
  410. // // Corner of neighbor
  411. // const int nc = ne/m;
  412. // // Is neighbor oriented consistently with (flipped) f?
  413. // //const int ns = F(nf,(nc+1)%3);
  414. // const int nd = F(nf,(nc+2)%3);
  415. // const bool cons = (flip(f)?fd:fs) == nd;
  416. // // Normal after possibly flipping to match flip or orientation of f
  417. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  418. // // Angle between n and f
  419. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  420. // cout<<" "<<(ne%m)+1<<":\t"<<ndi<<"\t"<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  421. // if(ndi>=max_di)
  422. // {
  423. // max_ne = ne;
  424. // max_di = ndi;
  425. // }
  426. // }
  427. //}
  428. if(nfei >= 0)
  429. {
  430. max_ne = uE2E[EMAP(e)][nfei];
  431. }
  432. if(max_ne>=0)
  433. {
  434. // face of neighbor
  435. const int nf = max_ne%m;
  436. #ifdef IGL_OUTER_HULL_DEBUG
  437. if(!FH[nf])
  438. {
  439. // first time seeing face
  440. cout<<(f+1)<<" --> "<<(nf+1)<<endl;
  441. }
  442. #endif
  443. FH[nf] = true;
  444. FHcount++;
  445. // corner of neighbor
  446. const int nc = max_ne/m;
  447. const int nd = F(nf,(nc+2)%3);
  448. const bool cons = (flip(f)?fd:fs) == nd;
  449. flip(nf) = (cons ? flip(f) : !flip(f));
  450. //cout<<"flip("<<nf<<") = "<<(flip(nf)?"true":"false")<<endl;
  451. const int ne1 = nf+((nc+1)%3)*m;
  452. const int ne2 = nf+((nc+2)%3)*m;
  453. if(!EH[ne1])
  454. {
  455. Q.push(ne1);
  456. }
  457. if(!EH[ne2])
  458. {
  459. Q.push(ne2);
  460. }
  461. }
  462. }
  463. {
  464. vG[id].resize(FHcount,3);
  465. vJ[id].resize(FHcount,1);
  466. //nG += FHcount;
  467. size_t h = 0;
  468. assert(counts(id) == IM.rows());
  469. for(int i = 0;i<counts(id);i++)
  470. {
  471. const size_t f = IM(i);
  472. //if(f_flip)
  473. //{
  474. // flip(f) = !flip(f);
  475. //}
  476. if(FH[f])
  477. {
  478. vG[id].row(h) = (flip(f)?F.row(f).reverse().eval():F.row(f));
  479. vJ[id](h,0) = f;
  480. h++;
  481. }
  482. }
  483. assert((int)h == FHcount);
  484. }
  485. }
  486. // Is A inside B? Assuming A and B are consistently oriented but closed and
  487. // non-intersecting.
  488. const auto & is_component_inside_other = [](
  489. const Eigen::PlainObjectBase<DerivedV> & V,
  490. const MatrixXV & BC,
  491. const MatrixXG & A,
  492. const MatrixXJ & AJ,
  493. const MatrixXG & B)->bool
  494. {
  495. const auto & bounding_box = [](
  496. const Eigen::PlainObjectBase<DerivedV> & V,
  497. const MatrixXG & F)->
  498. MatrixXV
  499. {
  500. MatrixXV BB(2,3);
  501. BB<<
  502. 1e26,1e26,1e26,
  503. -1e26,-1e26,-1e26;
  504. const size_t m = F.rows();
  505. for(size_t f = 0;f<m;f++)
  506. {
  507. for(size_t c = 0;c<3;c++)
  508. {
  509. const auto & vfc = V.row(F(f,c));
  510. BB.row(0) = BB.row(0).array().min(vfc.array()).eval();
  511. BB.row(1) = BB.row(1).array().max(vfc.array()).eval();
  512. }
  513. }
  514. return BB;
  515. };
  516. // A lot of the time we're dealing with unrelated, distant components: cull
  517. // them.
  518. MatrixXV ABB = bounding_box(V,A);
  519. MatrixXV BBB = bounding_box(V,B);
  520. if( (BBB.row(0)-ABB.row(1)).maxCoeff()>0 ||
  521. (ABB.row(0)-BBB.row(1)).maxCoeff()>0 )
  522. {
  523. // bounding boxes do not overlap
  524. return false;
  525. }
  526. ////////////////////////////////////////////////////////////////////////
  527. // POTENTIAL ROBUSTNESS WEAK AREA
  528. ////////////////////////////////////////////////////////////////////////
  529. //
  530. // q could be so close (<~1e-16) to B that the winding number is not a robust way to
  531. // determine inside/outsideness. We could try to find a _better_ q which is
  532. // farther away, but couldn't they all be bad?
  533. MatrixXV q = BC.row(AJ(0));
  534. // In a perfect world, it's enough to test a single point.
  535. double w;
  536. // winding_number_3 expects colmajor
  537. const typename DerivedV::Scalar * Vdata;
  538. Vdata = V.data();
  539. Matrix<
  540. typename DerivedV::Scalar,
  541. DerivedV::RowsAtCompileTime,
  542. DerivedV::ColsAtCompileTime,
  543. ColMajor> Vcol;
  544. if(DerivedV::IsRowMajor)
  545. {
  546. // copy to convert to colmajor
  547. Vcol = V;
  548. Vdata = Vcol.data();
  549. }
  550. winding_number_3(
  551. Vdata,V.rows(),
  552. B.data(),B.rows(),
  553. q.data(),1,&w);
  554. return fabs(w)>0.5;
  555. };
  556. // Reject components which are completely inside other components
  557. vector<bool> keep(ncc,true);
  558. size_t nG = 0;
  559. // This is O( ncc * ncc * m)
  560. for(size_t id = 0;id<ncc;id++)
  561. {
  562. for(size_t oid = 0;oid<ncc;oid++)
  563. {
  564. if(id == oid)
  565. {
  566. continue;
  567. }
  568. const bool inside = is_component_inside_other(V,BC,vG[id],vJ[id],vG[oid]);
  569. #ifdef IGL_OUTER_HULL_DEBUG
  570. cout<<id<<" is inside "<<oid<<" ? "<<inside<<endl;
  571. #endif
  572. keep[id] = keep[id] && !inside;
  573. }
  574. if(keep[id])
  575. {
  576. nG += vJ[id].rows();
  577. }
  578. }
  579. // collect G and J across components
  580. G.resize(nG,3);
  581. J.resize(nG,1);
  582. {
  583. size_t off = 0;
  584. for(Index id = 0;id<(Index)ncc;id++)
  585. {
  586. if(keep[id])
  587. {
  588. assert(vG[id].rows() == vJ[id].rows());
  589. G.block(off,0,vG[id].rows(),vG[id].cols()) = vG[id];
  590. J.block(off,0,vJ[id].rows(),vJ[id].cols()) = vJ[id];
  591. off += vG[id].rows();
  592. }
  593. }
  594. }
  595. }
  596. template <
  597. typename DerivedV,
  598. typename DerivedF,
  599. typename DerivedG,
  600. typename DerivedJ,
  601. typename Derivedflip>
  602. IGL_INLINE void igl::outer_hull(
  603. const Eigen::PlainObjectBase<DerivedV> & V,
  604. const Eigen::PlainObjectBase<DerivedF> & F,
  605. Eigen::PlainObjectBase<DerivedG> & G,
  606. Eigen::PlainObjectBase<DerivedJ> & J,
  607. Eigen::PlainObjectBase<Derivedflip> & flip)
  608. {
  609. Eigen::Matrix<typename DerivedV::Scalar,DerivedF::RowsAtCompileTime,3> N;
  610. per_face_normals_stable(V,F,N);
  611. return outer_hull(V,F,N,G,J,flip);
  612. }
  613. #ifdef IGL_STATIC_LIBRARY
  614. // Explicit template specialization
  615. 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> >&);
  616. 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> >&);
  617. 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> >&);
  618. #endif