outer_hull.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. // Should never see edge again...
  323. if(EH[e] == true)
  324. {
  325. continue;
  326. }
  327. EH[e] = true;
  328. // source of edge according to f
  329. const int fs = flip(f)?F(f,(c+2)%3):F(f,(c+1)%3);
  330. // destination of edge according to f
  331. const int fd = flip(f)?F(f,(c+1)%3):F(f,(c+2)%3);
  332. // edge valence
  333. const size_t val = uE2E[EMAP(e)].size();
  334. //// find overlapping face-edges
  335. //const auto & neighbors = uE2E[EMAP(e)];
  336. //// normal after possible flipping
  337. //const auto & fN = (flip(f)?-1.:1.)*N.row(f);
  338. //// Edge vector according to f's (flipped) orientation.
  339. ////const auto & eV = (V.row(fd)-V.row(fs)).normalized();
  340. //#warning "EXPERIMENTAL, DO NOT USE"
  341. //// THIS IS WRONG! The first face is---after sorting---no longer the face
  342. //// used for orienting the sort.
  343. //const auto ui = EMAP(e);
  344. //const auto fe0 = uE2E[ui][0];
  345. //const auto es = F(fe0%m,((fe0/m)+1)%3);
  346. // is edge consistent with edge of face used for sorting
  347. const int e_cons = (dicons(e) ? 1: -1);
  348. int nfei = -1;
  349. // Loop once around trying to find suitable next face
  350. for(size_t step = 1; step<val+2;step++)
  351. {
  352. const int nfei_new = (diIM(e) + 2*val + e_cons*step*(flip(f)?-1:1))%val;
  353. const int nf = uE2E[EMAP(e)][nfei_new] % m;
  354. // Don't consider faces with identical dihedral angles
  355. if ((di[EMAP(e)][diIM(e)].array() != di[EMAP(e)][nfei_new].array()).any())
  356. //if((di[EMAP(e)][diIM(e)] != di[EMAP(e)][nfei_new]))
  357. //#warning "THIS IS HACK, FIX ME"
  358. // if( abs(di[EMAP(e)][diIM(e)] - di[EMAP(e)][nfei_new]) < 1e-16 )
  359. {
  360. #ifdef IGL_OUTER_HULL_DEBUG
  361. //cout<<"Next facet: "<<(f+1)<<" --> "<<(nf+1)<<", |"<<
  362. // di[EMAP(e)][diIM(e)]<<" - "<<di[EMAP(e)][nfei_new]<<"| = "<<
  363. // abs(di[EMAP(e)][diIM(e)] - di[EMAP(e)][nfei_new])
  364. // <<endl;
  365. #endif
  366. // Only use this face if not already seen
  367. if(!FH[nf])
  368. {
  369. nfei = nfei_new;
  370. }
  371. break;
  372. }
  373. //#ifdef IGL_OUTER_HULL_DEBUG
  374. // cout<<"Skipping co-planar facet: "<<(f+1)<<" --> "<<(nf+1)<<endl;
  375. //#endif
  376. }
  377. int max_ne = -1;
  378. //// Loop over and find max dihedral angle
  379. //typename DerivedV::Scalar max_di = -1;
  380. //for(const auto & ne : neighbors)
  381. //{
  382. // const int nf = ne%m;
  383. // if(nf == f)
  384. // {
  385. // continue;
  386. // }
  387. // // Corner of neighbor
  388. // const int nc = ne/m;
  389. // // Is neighbor oriented consistently with (flipped) f?
  390. // //const int ns = F(nf,(nc+1)%3);
  391. // const int nd = F(nf,(nc+2)%3);
  392. // const bool cons = (flip(f)?fd:fs) == nd;
  393. // // Normal after possibly flipping to match flip or orientation of f
  394. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  395. // // Angle between n and f
  396. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  397. // if(ndi>=max_di)
  398. // {
  399. // max_ne = ne;
  400. // max_di = ndi;
  401. // }
  402. //}
  403. ////cout<<(max_ne != max_ne_2)<<" =?= "<<e_cons<<endl;
  404. //if(max_ne != max_ne_2)
  405. //{
  406. // cout<<(f+1)<<" ---> "<<(max_ne%m)+1<<" != "<<(max_ne_2%m)+1<<" ... "<<e_cons<<" "<<flip(f)<<endl;
  407. // typename DerivedV::Scalar max_di = -1;
  408. // for(size_t nei = 0;nei<neighbors.size();nei++)
  409. // {
  410. // const auto & ne = neighbors[nei];
  411. // const int nf = ne%m;
  412. // if(nf == f)
  413. // {
  414. // cout<<" "<<(ne%m)+1<<":\t"<<0<<"\t"<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  415. // continue;
  416. // }
  417. // // Corner of neighbor
  418. // const int nc = ne/m;
  419. // // Is neighbor oriented consistently with (flipped) f?
  420. // //const int ns = F(nf,(nc+1)%3);
  421. // const int nd = F(nf,(nc+2)%3);
  422. // const bool cons = (flip(f)?fd:fs) == nd;
  423. // // Normal after possibly flipping to match flip or orientation of f
  424. // const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  425. // // Angle between n and f
  426. // const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  427. // cout<<" "<<(ne%m)+1<<":\t"<<ndi<<"\t"<<di[EMAP[e]][nei]<<" "<<diIM(ne)<<endl;
  428. // if(ndi>=max_di)
  429. // {
  430. // max_ne = ne;
  431. // max_di = ndi;
  432. // }
  433. // }
  434. //}
  435. if(nfei >= 0)
  436. {
  437. max_ne = uE2E[EMAP(e)][nfei];
  438. }
  439. if(max_ne>=0)
  440. {
  441. // face of neighbor
  442. const int nf = max_ne%m;
  443. #ifdef IGL_OUTER_HULL_DEBUG
  444. if(!FH[nf])
  445. {
  446. // first time seeing face
  447. cout<<(f+1)<<" --> "<<(nf+1)<<endl;
  448. }
  449. #endif
  450. FH[nf] = true;
  451. FHcount++;
  452. // corner of neighbor
  453. const int nc = max_ne/m;
  454. const int nd = F(nf,(nc+2)%3);
  455. const bool cons = (flip(f)?fd:fs) == nd;
  456. flip(nf) = (cons ? flip(f) : !flip(f));
  457. //cout<<"flip("<<nf<<") = "<<(flip(nf)?"true":"false")<<endl;
  458. const int ne1 = nf+((nc+1)%3)*m;
  459. const int ne2 = nf+((nc+2)%3)*m;
  460. if(!EH[ne1])
  461. {
  462. Q.push(ne1);
  463. }
  464. if(!EH[ne2])
  465. {
  466. Q.push(ne2);
  467. }
  468. }
  469. }
  470. {
  471. vG[id].resize(FHcount,3);
  472. vJ[id].resize(FHcount,1);
  473. //nG += FHcount;
  474. size_t h = 0;
  475. assert(counts(id) == IM.rows());
  476. for(int i = 0;i<counts(id);i++)
  477. {
  478. const size_t f = IM(i);
  479. //if(f_flip)
  480. //{
  481. // flip(f) = !flip(f);
  482. //}
  483. if(FH[f])
  484. {
  485. vG[id].row(h) = (flip(f)?F.row(f).reverse().eval():F.row(f));
  486. vJ[id](h,0) = f;
  487. h++;
  488. }
  489. }
  490. assert((int)h == FHcount);
  491. }
  492. }
  493. // Is A inside B? Assuming A and B are consistently oriented but closed and
  494. // non-intersecting.
  495. const auto & is_component_inside_other = [](
  496. const Eigen::PlainObjectBase<DerivedV> & V,
  497. const MatrixXV & BC,
  498. const MatrixXG & A,
  499. const MatrixXJ & AJ,
  500. const MatrixXG & B)->bool
  501. {
  502. const auto & bounding_box = [](
  503. const Eigen::PlainObjectBase<DerivedV> & V,
  504. const MatrixXG & F)->
  505. MatrixXV
  506. {
  507. MatrixXV BB(2,3);
  508. BB<<
  509. 1e26,1e26,1e26,
  510. -1e26,-1e26,-1e26;
  511. const size_t m = F.rows();
  512. for(size_t f = 0;f<m;f++)
  513. {
  514. for(size_t c = 0;c<3;c++)
  515. {
  516. const auto & vfc = V.row(F(f,c));
  517. BB.row(0) = BB.row(0).array().min(vfc.array()).eval();
  518. BB.row(1) = BB.row(1).array().max(vfc.array()).eval();
  519. }
  520. }
  521. return BB;
  522. };
  523. // A lot of the time we're dealing with unrelated, distant components: cull
  524. // them.
  525. MatrixXV ABB = bounding_box(V,A);
  526. MatrixXV BBB = bounding_box(V,B);
  527. if( (BBB.row(0)-ABB.row(1)).maxCoeff()>0 ||
  528. (ABB.row(0)-BBB.row(1)).maxCoeff()>0 )
  529. {
  530. // bounding boxes do not overlap
  531. return false;
  532. }
  533. ////////////////////////////////////////////////////////////////////////
  534. // POTENTIAL ROBUSTNESS WEAK AREA
  535. ////////////////////////////////////////////////////////////////////////
  536. //
  537. // q could be so close (<~1e-16) to B that the winding number is not a robust way to
  538. // determine inside/outsideness. We could try to find a _better_ q which is
  539. // farther away, but couldn't they all be bad?
  540. MatrixXV q = BC.row(AJ(0));
  541. // In a perfect world, it's enough to test a single point.
  542. double w;
  543. // winding_number_3 expects colmajor
  544. const typename DerivedV::Scalar * Vdata;
  545. Vdata = V.data();
  546. Matrix<
  547. typename DerivedV::Scalar,
  548. DerivedV::RowsAtCompileTime,
  549. DerivedV::ColsAtCompileTime,
  550. ColMajor> Vcol;
  551. if(DerivedV::IsRowMajor)
  552. {
  553. // copy to convert to colmajor
  554. Vcol = V;
  555. Vdata = Vcol.data();
  556. }
  557. winding_number_3(
  558. Vdata,V.rows(),
  559. B.data(),B.rows(),
  560. q.data(),1,&w);
  561. return fabs(w)>0.5;
  562. };
  563. // Reject components which are completely inside other components
  564. vector<bool> keep(ncc,true);
  565. size_t nG = 0;
  566. // This is O( ncc * ncc * m)
  567. for(size_t id = 0;id<ncc;id++)
  568. {
  569. for(size_t oid = 0;oid<ncc;oid++)
  570. {
  571. if(id == oid)
  572. {
  573. continue;
  574. }
  575. const bool inside = is_component_inside_other(V,BC,vG[id],vJ[id],vG[oid]);
  576. #ifdef IGL_OUTER_HULL_DEBUG
  577. cout<<id<<" is inside "<<oid<<" ? "<<inside<<endl;
  578. #endif
  579. keep[id] = keep[id] && !inside;
  580. }
  581. if(keep[id])
  582. {
  583. nG += vJ[id].rows();
  584. }
  585. }
  586. // collect G and J across components
  587. G.resize(nG,3);
  588. J.resize(nG,1);
  589. {
  590. size_t off = 0;
  591. for(Index id = 0;id<(Index)ncc;id++)
  592. {
  593. if(keep[id])
  594. {
  595. assert(vG[id].rows() == vJ[id].rows());
  596. G.block(off,0,vG[id].rows(),vG[id].cols()) = vG[id];
  597. J.block(off,0,vJ[id].rows(),vJ[id].cols()) = vJ[id];
  598. off += vG[id].rows();
  599. }
  600. }
  601. }
  602. }
  603. template <
  604. typename DerivedV,
  605. typename DerivedF,
  606. typename DerivedG,
  607. typename DerivedJ,
  608. typename Derivedflip>
  609. IGL_INLINE void igl::outer_hull(
  610. const Eigen::PlainObjectBase<DerivedV> & V,
  611. const Eigen::PlainObjectBase<DerivedF> & F,
  612. Eigen::PlainObjectBase<DerivedG> & G,
  613. Eigen::PlainObjectBase<DerivedJ> & J,
  614. Eigen::PlainObjectBase<Derivedflip> & flip)
  615. {
  616. Eigen::Matrix<typename DerivedV::Scalar,DerivedF::RowsAtCompileTime,3> N;
  617. per_face_normals_stable(V,F,N);
  618. return outer_hull(V,F,N,G,J,flip);
  619. }
  620. #ifdef IGL_STATIC_LIBRARY
  621. // Explicit template specialization
  622. 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> >&);
  623. 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> >&);
  624. 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> >&);
  625. #endif