outer_hull.cpp 14 KB

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