outer_hull.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "outer_hull.h"
  9. #include "extract_cells.h"
  10. #include "remesh_self_intersections.h"
  11. #include "../../remove_unreferenced.h"
  12. #include <CGAL/AABB_tree.h>
  13. #include <CGAL/AABB_traits.h>
  14. #include <CGAL/AABB_triangle_primitive.h>
  15. #include <CGAL/intersections.h>
  16. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  17. template <
  18. typename DerivedV,
  19. typename DerivedF,
  20. typename DerivedHV,
  21. typename DerivedHF,
  22. typename DerivedJ,
  23. typename Derivedflip>
  24. IGL_INLINE void igl::copyleft::cgal::outer_hull(
  25. const Eigen::PlainObjectBase<DerivedV> & V,
  26. const Eigen::PlainObjectBase<DerivedF> & F,
  27. Eigen::PlainObjectBase<DerivedHV> & HV,
  28. Eigen::PlainObjectBase<DerivedHF> & HF,
  29. Eigen::PlainObjectBase<DerivedJ> & J,
  30. Eigen::PlainObjectBase<Derivedflip> & flip)
  31. {
  32. // Exact types
  33. typedef CGAL::Epeck Kernel;
  34. typedef Kernel::FT ExactScalar;
  35. typedef
  36. Eigen::Matrix<
  37. ExactScalar,
  38. Eigen::Dynamic,
  39. Eigen::Dynamic,
  40. DerivedHV::IsRowMajor>
  41. MatrixXES;
  42. // Remesh self-intersections
  43. MatrixXES Vr;
  44. DerivedHF Fr;
  45. DerivedJ Jr;
  46. {
  47. RemeshSelfIntersectionsParam params;
  48. params.stitch_all = true;
  49. Eigen::VectorXi I;
  50. Eigen::MatrixXi IF;
  51. remesh_self_intersections(V, F, params, Vr, Fr, IF, Jr, I);
  52. // Merge coinciding vertices into non-manifold vertices.
  53. std::for_each(Fr.data(), Fr.data()+Fr.size(),
  54. [&I](typename DerivedHF::Scalar& a) { a=I[a]; });
  55. // Remove unreferenced vertices.
  56. Eigen::VectorXi UIM;
  57. remove_unreferenced(MatrixXES(Vr),DerivedHF(Fr), Vr, Fr, UIM);
  58. }
  59. // Extract cells for each face
  60. Eigen::MatrixXi C;
  61. extract_cells(Vr,Fr,C);
  62. // Extract faces on ambient cell
  63. int num_outer = 0;
  64. for(int i = 0;i<C.rows();i++)
  65. {
  66. num_outer += ( C(i,0) == 0 || C(i,1) == 0 ) ? 1 : 0;
  67. }
  68. HF.resize(num_outer,3);
  69. J.resize(num_outer,1);
  70. flip.resize(num_outer,1);
  71. {
  72. int h = 0;
  73. for(int i = 0;i<C.rows();i++)
  74. {
  75. if(C(i,0)==0)
  76. {
  77. HF.row(h) = Fr.row(i);
  78. J(h) = Jr(i);
  79. flip(h) = false;
  80. h++;
  81. }else if(C(i,1) == 0)
  82. {
  83. HF.row(h) = Fr.row(i).reverse();
  84. J(h) = Jr(i);
  85. flip(h) = true;
  86. h++;
  87. }
  88. }
  89. assert(h == num_outer);
  90. }
  91. // Remove unreferenced vertices and re-index faces
  92. {
  93. // Cast to output type
  94. DerivedHV Vr_cast(Vr.rows(),Vr.cols());
  95. for(int i = 0;i<Vr.rows();i++)
  96. {
  97. for(int j = 0;j<Vr.cols();j++)
  98. {
  99. assign_scalar(Vr(i,j), Vr_cast(i,j));
  100. }
  101. }
  102. Eigen::VectorXi I;
  103. remove_unreferenced(Vr_cast,DerivedHF(HF),HV,HF,I);
  104. }
  105. }
  106. #include "points_inside_component.h"
  107. #include "order_facets_around_edges.h"
  108. #include "outer_facet.h"
  109. #include "../../sortrows.h"
  110. #include "../../facet_components.h"
  111. #include "../../winding_number.h"
  112. #include "../../triangle_triangle_adjacency.h"
  113. #include "../../unique_edge_map.h"
  114. #include "../../barycenter.h"
  115. #include "../../per_face_normals.h"
  116. #include "../../sort_angles.h"
  117. #include <Eigen/Geometry>
  118. #include <vector>
  119. #include <map>
  120. #include <queue>
  121. #include <iostream>
  122. #include <type_traits>
  123. #include <CGAL/number_utils.h>
  124. //#define IGL_OUTER_HULL_DEBUG
  125. template <
  126. typename DerivedV,
  127. typename DerivedF,
  128. typename DerivedG,
  129. typename DerivedJ,
  130. typename Derivedflip>
  131. IGL_INLINE void igl::copyleft::cgal::outer_hull_legacy(
  132. const Eigen::PlainObjectBase<DerivedV> & V,
  133. const Eigen::PlainObjectBase<DerivedF> & F,
  134. Eigen::PlainObjectBase<DerivedG> & G,
  135. Eigen::PlainObjectBase<DerivedJ> & J,
  136. Eigen::PlainObjectBase<Derivedflip> & flip)
  137. {
  138. #ifdef IGL_OUTER_HULL_DEBUG
  139. std::cerr << "Extracting outer hull" << std::endl;
  140. #endif
  141. using namespace Eigen;
  142. using namespace std;
  143. typedef typename DerivedF::Index Index;
  144. Matrix<Index,DerivedF::RowsAtCompileTime,1> C;
  145. typedef Matrix<typename DerivedV::Scalar,Dynamic,DerivedV::ColsAtCompileTime> MatrixXV;
  146. //typedef Matrix<typename DerivedF::Scalar,Dynamic,DerivedF::ColsAtCompileTime> MatrixXF;
  147. typedef Matrix<typename DerivedG::Scalar,Dynamic,DerivedG::ColsAtCompileTime> MatrixXG;
  148. typedef Matrix<typename DerivedJ::Scalar,Dynamic,DerivedJ::ColsAtCompileTime> MatrixXJ;
  149. const Index m = F.rows();
  150. // UNUSED:
  151. //const auto & duplicate_simplex = [&F](const int f, const int g)->bool
  152. //{
  153. // return
  154. // (F(f,0) == F(g,0) && F(f,1) == F(g,1) && F(f,2) == F(g,2)) ||
  155. // (F(f,1) == F(g,0) && F(f,2) == F(g,1) && F(f,0) == F(g,2)) ||
  156. // (F(f,2) == F(g,0) && F(f,0) == F(g,1) && F(f,1) == F(g,2)) ||
  157. // (F(f,0) == F(g,2) && F(f,1) == F(g,1) && F(f,2) == F(g,0)) ||
  158. // (F(f,1) == F(g,2) && F(f,2) == F(g,1) && F(f,0) == F(g,0)) ||
  159. // (F(f,2) == F(g,2) && F(f,0) == F(g,1) && F(f,1) == F(g,0));
  160. //};
  161. #ifdef IGL_OUTER_HULL_DEBUG
  162. cout<<"outer hull..."<<endl;
  163. #endif
  164. #ifdef IGL_OUTER_HULL_DEBUG
  165. cout<<"edge map..."<<endl;
  166. #endif
  167. typedef Matrix<typename DerivedF::Scalar,Dynamic,2> MatrixX2I;
  168. typedef Matrix<typename DerivedF::Index,Dynamic,1> VectorXI;
  169. //typedef Matrix<typename DerivedV::Scalar, 3, 1> Vector3F;
  170. MatrixX2I E,uE;
  171. VectorXI EMAP;
  172. vector<vector<typename DerivedF::Index> > uE2E;
  173. unique_edge_map(F,E,uE,EMAP,uE2E);
  174. #ifdef IGL_OUTER_HULL_DEBUG
  175. for (size_t ui=0; ui<uE.rows(); ui++) {
  176. std::cout << ui << ": " << uE2E[ui].size() << " -- (";
  177. for (size_t i=0; i<uE2E[ui].size(); i++) {
  178. std::cout << uE2E[ui][i] << ", ";
  179. }
  180. std::cout << ")" << std::endl;
  181. }
  182. #endif
  183. std::vector<std::vector<typename DerivedF::Index> > uE2oE;
  184. std::vector<std::vector<bool> > uE2C;
  185. order_facets_around_edges(V, F, uE, uE2E, uE2oE, uE2C);
  186. uE2E = uE2oE;
  187. VectorXI diIM(3*m);
  188. for (auto ue : uE2E) {
  189. for (size_t i=0; i<ue.size(); i++) {
  190. auto fe = ue[i];
  191. diIM[fe] = i;
  192. }
  193. }
  194. vector<vector<vector<Index > > > TT,_1;
  195. triangle_triangle_adjacency(E,EMAP,uE2E,false,TT,_1);
  196. VectorXI counts;
  197. #ifdef IGL_OUTER_HULL_DEBUG
  198. cout<<"facet components..."<<endl;
  199. #endif
  200. facet_components(TT,C,counts);
  201. assert(C.maxCoeff()+1 == counts.rows());
  202. const size_t ncc = counts.rows();
  203. G.resize(0,F.cols());
  204. J.resize(0,1);
  205. flip.setConstant(m,1,false);
  206. #ifdef IGL_OUTER_HULL_DEBUG
  207. cout<<"reindex..."<<endl;
  208. #endif
  209. // H contains list of faces on outer hull;
  210. vector<bool> FH(m,false);
  211. vector<bool> EH(3*m,false);
  212. vector<MatrixXG> vG(ncc);
  213. vector<MatrixXJ> vJ(ncc);
  214. vector<MatrixXJ> vIM(ncc);
  215. //size_t face_count = 0;
  216. for(size_t id = 0;id<ncc;id++)
  217. {
  218. vIM[id].resize(counts[id],1);
  219. }
  220. // current index into each IM
  221. vector<size_t> g(ncc,0);
  222. // place order of each face in its respective component
  223. for(Index f = 0;f<m;f++)
  224. {
  225. vIM[C(f)](g[C(f)]++) = f;
  226. }
  227. #ifdef IGL_OUTER_HULL_DEBUG
  228. cout<<"barycenters..."<<endl;
  229. #endif
  230. // assumes that "resolve" has handled any coplanar cases correctly and nearly
  231. // coplanar cases can be sorted based on barycenter.
  232. MatrixXV BC;
  233. barycenter(V,F,BC);
  234. #ifdef IGL_OUTER_HULL_DEBUG
  235. cout<<"loop over CCs (="<<ncc<<")..."<<endl;
  236. #endif
  237. for(Index id = 0;id<(Index)ncc;id++)
  238. {
  239. auto & IM = vIM[id];
  240. // starting face that's guaranteed to be on the outer hull and in this
  241. // component
  242. int f;
  243. bool f_flip;
  244. #ifdef IGL_OUTER_HULL_DEBUG
  245. cout<<"outer facet..."<<endl;
  246. #endif
  247. igl::copyleft::cgal::outer_facet(V,F,IM,f,f_flip);
  248. #ifdef IGL_OUTER_HULL_DEBUG
  249. cout<<"outer facet: "<<f<<endl;
  250. //cout << V.row(F(f, 0)) << std::endl;
  251. //cout << V.row(F(f, 1)) << std::endl;
  252. //cout << V.row(F(f, 2)) << std::endl;
  253. #endif
  254. int FHcount = 1;
  255. FH[f] = true;
  256. // Q contains list of face edges to continue traversing upong
  257. queue<int> Q;
  258. Q.push(f+0*m);
  259. Q.push(f+1*m);
  260. Q.push(f+2*m);
  261. flip(f) = f_flip;
  262. //std::cout << "face " << face_count++ << ": " << f << std::endl;
  263. //std::cout << "f " << F.row(f).array()+1 << std::endl;
  264. //cout<<"flip("<<f<<") = "<<(flip(f)?"true":"false")<<endl;
  265. #ifdef IGL_OUTER_HULL_DEBUG
  266. cout<<"BFS..."<<endl;
  267. #endif
  268. while(!Q.empty())
  269. {
  270. // face-edge
  271. const int e = Q.front();
  272. Q.pop();
  273. // face
  274. const int f = e%m;
  275. // corner
  276. const int c = e/m;
  277. #ifdef IGL_OUTER_HULL_DEBUG
  278. std::cout << "edge: " << e << ", ue: " << EMAP(e) << std::endl;
  279. std::cout << "face: " << f << std::endl;
  280. std::cout << "corner: " << c << std::endl;
  281. std::cout << "consistent: " << uE2C[EMAP(e)][diIM[e]] << std::endl;
  282. #endif
  283. // Should never see edge again...
  284. if(EH[e] == true)
  285. {
  286. continue;
  287. }
  288. EH[e] = true;
  289. // source of edge according to f
  290. const int fs = flip(f)?F(f,(c+2)%3):F(f,(c+1)%3);
  291. // destination of edge according to f
  292. const int fd = flip(f)?F(f,(c+1)%3):F(f,(c+2)%3);
  293. // edge valence
  294. const size_t val = uE2E[EMAP(e)].size();
  295. #ifdef IGL_OUTER_HULL_DEBUG
  296. //std::cout << "vd: " << V.row(fd) << std::endl;
  297. //std::cout << "vs: " << V.row(fs) << std::endl;
  298. //std::cout << "edge: " << V.row(fd) - V.row(fs) << std::endl;
  299. for (size_t i=0; i<val; i++) {
  300. if (i == diIM(e)) {
  301. std::cout << "* ";
  302. } else {
  303. std::cout << " ";
  304. }
  305. std::cout << i << ": "
  306. << " (e: " << uE2E[EMAP(e)][i] << ", f: "
  307. << uE2E[EMAP(e)][i] % m * (uE2C[EMAP(e)][i] ? 1:-1) << ")" << std::endl;
  308. }
  309. #endif
  310. // is edge consistent with edge of face used for sorting
  311. const int e_cons = (uE2C[EMAP(e)][diIM(e)] ? 1: -1);
  312. int nfei = -1;
  313. // Loop once around trying to find suitable next face
  314. for(size_t step = 1; step<val+2;step++)
  315. {
  316. const int nfei_new = (diIM(e) + 2*val + e_cons*step*(flip(f)?-1:1))%val;
  317. const int nf = uE2E[EMAP(e)][nfei_new] % m;
  318. {
  319. #ifdef IGL_OUTER_HULL_DEBUG
  320. //cout<<"Next facet: "<<(f+1)<<" --> "<<(nf+1)<<", |"<<
  321. // di[EMAP(e)][diIM(e)]<<" - "<<di[EMAP(e)][nfei_new]<<"| = "<<
  322. // abs(di[EMAP(e)][diIM(e)] - di[EMAP(e)][nfei_new])
  323. // <<endl;
  324. #endif
  325. // Only use this face if not already seen
  326. if(!FH[nf])
  327. {
  328. nfei = nfei_new;
  329. //} else {
  330. // std::cout << "skipping face " << nfei_new << " because it is seen before"
  331. // << std::endl;
  332. }
  333. break;
  334. //} else {
  335. // std::cout << di[EMAP(e)][diIM(e)].transpose() << std::endl;
  336. // std::cout << di[EMAP(e)][diIM(nfei_new)].transpose() << std::endl;
  337. // std::cout << "skipping face " << nfei_new << " with identical dihedral angle"
  338. // << std::endl;
  339. }
  340. //#ifdef IGL_OUTER_HULL_DEBUG
  341. // cout<<"Skipping co-planar facet: "<<(f+1)<<" --> "<<(nf+1)<<endl;
  342. //#endif
  343. }
  344. int max_ne = -1;
  345. if(nfei >= 0)
  346. {
  347. max_ne = uE2E[EMAP(e)][nfei];
  348. }
  349. if(max_ne>=0)
  350. {
  351. // face of neighbor
  352. const int nf = max_ne%m;
  353. #ifdef IGL_OUTER_HULL_DEBUG
  354. if(!FH[nf])
  355. {
  356. // first time seeing face
  357. cout<<(f+1)<<" --> "<<(nf+1)<<endl;
  358. }
  359. #endif
  360. FH[nf] = true;
  361. //std::cout << "face " << face_count++ << ": " << nf << std::endl;
  362. //std::cout << "f " << F.row(nf).array()+1 << std::endl;
  363. FHcount++;
  364. // corner of neighbor
  365. const int nc = max_ne/m;
  366. const int nd = F(nf,(nc+2)%3);
  367. const bool cons = (flip(f)?fd:fs) == nd;
  368. flip(nf) = (cons ? flip(f) : !flip(f));
  369. //cout<<"flip("<<nf<<") = "<<(flip(nf)?"true":"false")<<endl;
  370. const int ne1 = nf+((nc+1)%3)*m;
  371. const int ne2 = nf+((nc+2)%3)*m;
  372. if(!EH[ne1])
  373. {
  374. Q.push(ne1);
  375. }
  376. if(!EH[ne2])
  377. {
  378. Q.push(ne2);
  379. }
  380. }
  381. }
  382. {
  383. vG[id].resize(FHcount,3);
  384. vJ[id].resize(FHcount,1);
  385. //nG += FHcount;
  386. size_t h = 0;
  387. assert(counts(id) == IM.rows());
  388. for(int i = 0;i<counts(id);i++)
  389. {
  390. const size_t f = IM(i);
  391. //if(f_flip)
  392. //{
  393. // flip(f) = !flip(f);
  394. //}
  395. if(FH[f])
  396. {
  397. vG[id].row(h) = (flip(f)?F.row(f).reverse().eval():F.row(f));
  398. vJ[id](h,0) = f;
  399. h++;
  400. }
  401. }
  402. assert((int)h == FHcount);
  403. }
  404. }
  405. // Is A inside B? Assuming A and B are consistently oriented but closed and
  406. // non-intersecting.
  407. const auto & has_overlapping_bbox = [](
  408. const Eigen::PlainObjectBase<DerivedV> & V,
  409. const MatrixXG & A,
  410. const MatrixXG & B)->bool
  411. {
  412. const auto & bounding_box = [](
  413. const Eigen::PlainObjectBase<DerivedV> & V,
  414. const MatrixXG & F)->
  415. DerivedV
  416. {
  417. DerivedV BB(2,3);
  418. BB<<
  419. 1e26,1e26,1e26,
  420. -1e26,-1e26,-1e26;
  421. const size_t m = F.rows();
  422. for(size_t f = 0;f<m;f++)
  423. {
  424. for(size_t c = 0;c<3;c++)
  425. {
  426. const auto & vfc = V.row(F(f,c)).eval();
  427. BB(0,0) = std::min(BB(0,0), vfc(0,0));
  428. BB(0,1) = std::min(BB(0,1), vfc(0,1));
  429. BB(0,2) = std::min(BB(0,2), vfc(0,2));
  430. BB(1,0) = std::max(BB(1,0), vfc(0,0));
  431. BB(1,1) = std::max(BB(1,1), vfc(0,1));
  432. BB(1,2) = std::max(BB(1,2), vfc(0,2));
  433. }
  434. }
  435. return BB;
  436. };
  437. // A lot of the time we're dealing with unrelated, distant components: cull
  438. // them.
  439. DerivedV ABB = bounding_box(V,A);
  440. DerivedV BBB = bounding_box(V,B);
  441. if( (BBB.row(0)-ABB.row(1)).maxCoeff()>0 ||
  442. (ABB.row(0)-BBB.row(1)).maxCoeff()>0 )
  443. {
  444. // bounding boxes do not overlap
  445. return false;
  446. } else {
  447. return true;
  448. }
  449. };
  450. // Reject components which are completely inside other components
  451. vector<bool> keep(ncc,true);
  452. size_t nG = 0;
  453. // This is O( ncc * ncc * m)
  454. for(size_t id = 0;id<ncc;id++)
  455. {
  456. if (!keep[id]) continue;
  457. std::vector<size_t> unresolved;
  458. for(size_t oid = 0;oid<ncc;oid++)
  459. {
  460. if(id == oid || !keep[oid])
  461. {
  462. continue;
  463. }
  464. if (has_overlapping_bbox(V, vG[id], vG[oid])) {
  465. unresolved.push_back(oid);
  466. }
  467. }
  468. const size_t num_unresolved_components = unresolved.size();
  469. DerivedV query_points(num_unresolved_components, 3);
  470. for (size_t i=0; i<num_unresolved_components; i++) {
  471. const size_t oid = unresolved[i];
  472. DerivedF f = vG[oid].row(0);
  473. query_points(i,0) = (V(f(0,0), 0) + V(f(0,1), 0) + V(f(0,2), 0))/3.0;
  474. query_points(i,1) = (V(f(0,0), 1) + V(f(0,1), 1) + V(f(0,2), 1))/3.0;
  475. query_points(i,2) = (V(f(0,0), 2) + V(f(0,1), 2) + V(f(0,2), 2))/3.0;
  476. }
  477. Eigen::VectorXi inside;
  478. igl::copyleft::cgal::points_inside_component(V, vG[id], query_points, inside);
  479. assert((size_t)inside.size() == num_unresolved_components);
  480. for (size_t i=0; i<num_unresolved_components; i++) {
  481. if (inside(i, 0)) {
  482. const size_t oid = unresolved[i];
  483. keep[oid] = false;
  484. }
  485. }
  486. }
  487. for (size_t id = 0; id<ncc; id++) {
  488. if (keep[id]) {
  489. nG += vJ[id].rows();
  490. }
  491. }
  492. // collect G and J across components
  493. G.resize(nG,3);
  494. J.resize(nG,1);
  495. {
  496. size_t off = 0;
  497. for(Index id = 0;id<(Index)ncc;id++)
  498. {
  499. if(keep[id])
  500. {
  501. assert(vG[id].rows() == vJ[id].rows());
  502. G.block(off,0,vG[id].rows(),vG[id].cols()) = vG[id];
  503. J.block(off,0,vJ[id].rows(),vJ[id].cols()) = vJ[id];
  504. off += vG[id].rows();
  505. }
  506. }
  507. }
  508. }
  509. #ifdef IGL_STATIC_LIBRARY
  510. // Explicit template specialization
  511. #endif