outer_hull_legacy.cpp 15 KB

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