outer_hull.cpp 15 KB

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