outer_hull.cpp 23 KB

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