outer_hull.cpp 14 KB

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