outer_hull.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "outer_hull.h"
  2. #include "outer_facet.h"
  3. #include "facet_components.h"
  4. #include "winding_number.h"
  5. #include "triangle_triangle_adjacency.h"
  6. #include "unique_edge_map.h"
  7. #include "barycenter.h"
  8. #include "per_face_normals.h"
  9. #include "all_edges.h"
  10. #include "colon.h"
  11. #include "get_seconds.h"
  12. #include <Eigen/Geometry>
  13. #include <vector>
  14. #include <map>
  15. #include <queue>
  16. #include <iostream>
  17. //#define IGL_OUTER_HULL_DEBUG
  18. template <
  19. typename DerivedV,
  20. typename DerivedF,
  21. typename DerivedG,
  22. typename DerivedJ,
  23. typename Derivedflip>
  24. IGL_INLINE void igl::outer_hull(
  25. const Eigen::PlainObjectBase<DerivedV> & V,
  26. const Eigen::PlainObjectBase<DerivedF> & F,
  27. Eigen::PlainObjectBase<DerivedG> & G,
  28. Eigen::PlainObjectBase<DerivedJ> & J,
  29. Eigen::PlainObjectBase<Derivedflip> & flip)
  30. {
  31. using namespace Eigen;
  32. using namespace std;
  33. using namespace igl;
  34. typedef typename DerivedF::Index Index;
  35. Matrix<Index,DerivedF::RowsAtCompileTime,1> C;
  36. typedef Matrix<typename DerivedV::Scalar,Dynamic,DerivedV::ColsAtCompileTime> MatrixXV;
  37. typedef Matrix<typename DerivedF::Scalar,Dynamic,DerivedF::ColsAtCompileTime> MatrixXF;
  38. typedef Matrix<typename DerivedG::Scalar,Dynamic,DerivedG::ColsAtCompileTime> MatrixXG;
  39. typedef Matrix<typename DerivedJ::Scalar,Dynamic,DerivedJ::ColsAtCompileTime> MatrixXJ;
  40. const Index m = F.rows();
  41. #ifdef IGL_OUTER_HULL_DEBUG
  42. cout<<"outer hull..."<<endl;
  43. #endif
  44. #ifdef IGL_OUTER_HULL_DEBUG
  45. cout<<"edge map..."<<endl;
  46. #endif
  47. typedef Matrix<typename DerivedF::Scalar,Dynamic,2> MatrixX2I;
  48. typedef Matrix<typename DerivedF::Index,Dynamic,1> VectorXI;
  49. MatrixX2I E,uE;
  50. VectorXI EMAP;
  51. vector<vector<typename DerivedF::Index> > uE2E;
  52. unique_edge_map(F,E,uE,EMAP,uE2E);
  53. vector<vector<vector<Index > > > TT,_1;
  54. triangle_triangle_adjacency(E,EMAP,uE2E,false,TT,_1);
  55. VectorXI counts;
  56. #ifdef IGL_OUTER_HULL_DEBUG
  57. cout<<"facet components..."<<endl;
  58. #endif
  59. facet_components(TT,C,counts);
  60. assert(C.maxCoeff()+1 == counts.rows());
  61. const size_t ncc = counts.rows();
  62. G.resize(0,F.cols());
  63. J.resize(0,1);
  64. flip.setConstant(m,1,false);
  65. // precompute face normals
  66. typename Eigen::Matrix<
  67. typename DerivedV::Scalar,
  68. DerivedF::RowsAtCompileTime,
  69. 3> N;
  70. #ifdef IGL_OUTER_HULL_DEBUG
  71. cout<<"normals..."<<endl;
  72. #endif
  73. per_face_normals(V,F,N);
  74. #ifdef IGL_OUTER_HULL_DEBUG
  75. cout<<"reindex..."<<endl;
  76. #endif
  77. // H contains list of faces on outer hull;
  78. vector<bool> FH(m,false);
  79. vector<bool> EH(3*m,false);
  80. vector<MatrixXG> vG(ncc);
  81. vector<MatrixXJ> vJ(ncc);
  82. vector<MatrixXJ> vIM(ncc);
  83. for(size_t id = 0;id<ncc;id++)
  84. {
  85. vIM[id].resize(counts[id],1);
  86. }
  87. // current index into each IM
  88. vector<size_t> g(ncc,0);
  89. // place order of each face in its respective component
  90. for(Index f = 0;f<m;f++)
  91. {
  92. vIM[C(f)](g[C(f)]++) = f;
  93. }
  94. #ifdef IGL_OUTER_HULL_DEBUG
  95. cout<<"barycenters..."<<endl;
  96. #endif
  97. // assumes that "resolve" has handled any coplanar cases correctly and nearly
  98. // coplanar cases can be sorted based on barycenter.
  99. MatrixXV BC;
  100. barycenter(V,F,BC);
  101. #ifdef IGL_OUTER_HULL_DEBUG
  102. cout<<"loop over CCs..."<<endl;
  103. #endif
  104. for(Index id = 0;id<(Index)ncc;id++)
  105. {
  106. auto & IM = vIM[id];
  107. // starting face that's guaranteed to be on the outer hull and in this
  108. // component
  109. int f;
  110. bool f_flip;
  111. #ifdef IGL_OUTER_HULL_DEBUG
  112. cout<<"outer facet..."<<endl;
  113. #endif
  114. outer_facet(V,F,N,IM,f,f_flip);
  115. int FHcount = 0;
  116. // Q contains list of face edges to continue traversing upong
  117. queue<int> Q;
  118. Q.push(f+0*m);
  119. Q.push(f+1*m);
  120. Q.push(f+2*m);
  121. flip(f) = f_flip;
  122. #ifdef IGL_OUTER_HULL_DEBUG
  123. cout<<"BFS..."<<endl;
  124. #endif
  125. while(!Q.empty())
  126. {
  127. // face-edge
  128. const int e = Q.front();
  129. Q.pop();
  130. // face
  131. const int f = e%m;
  132. // corner
  133. const int c = e/m;
  134. // Should never see edge again...
  135. if(EH[e] == true)
  136. {
  137. continue;
  138. }
  139. EH[e] = true;
  140. // first time seeing face
  141. if(!FH[f])
  142. {
  143. FH[f] = true;
  144. FHcount++;
  145. }
  146. // find overlapping face-edges
  147. const auto & neighbors = uE2E[EMAP(e)];
  148. const auto & fN = (flip(f)?-1.:1.)*N.row(f);
  149. // source of edge according to f
  150. const int fs = flip(f)?F(f,(c+2)%3):F(f,(c+1)%3);
  151. // destination of edge according to f
  152. const int fd = flip(f)?F(f,(c+1)%3):F(f,(c+2)%3);
  153. const auto & eV = (V.row(fd)-V.row(fs)).normalized();
  154. // Loop over and find max dihedral angle
  155. typename DerivedV::Scalar max_di = -1;
  156. int max_ne = -1;
  157. typename Eigen::Matrix< typename DerivedV::Scalar, 1, 3> max_nN;
  158. for(const auto & ne : neighbors)
  159. {
  160. const int nf = ne%m;
  161. if(nf == f)
  162. {
  163. continue;
  164. }
  165. const int nc = ne/m;
  166. // are faces consistently oriented
  167. //const int ns = F(nf,(nc+1)%3);
  168. const int nd = F(nf,(nc+2)%3);
  169. const bool cons = (flip(f)?fd:fs) == nd;
  170. const auto & nN = (cons? (flip(f)?-1:1.) : (flip(f)?1.:-1.) )*N.row(nf);
  171. const auto & ndi = M_PI - atan2( fN.cross(nN).dot(eV), fN.dot(nN));
  172. if(ndi>=max_di)
  173. {
  174. max_ne = ne;
  175. max_di = ndi;
  176. max_nN = nN;
  177. }
  178. }
  179. if(max_ne>=0)
  180. {
  181. const int nf = max_ne%m;
  182. const int nc = max_ne/m;
  183. const int nd = F(nf,(nc+2)%3);
  184. const bool cons = (flip(f)?fd:fs) == nd;
  185. flip(nf) = (cons ? flip(f) : !flip(f));
  186. const int ne1 = nf+((nc+1)%3)*m;
  187. const int ne2 = nf+((nc+2)%3)*m;
  188. if(!EH[ne1])
  189. {
  190. Q.push(ne1);
  191. }
  192. if(!EH[ne2])
  193. {
  194. Q.push(ne2);
  195. }
  196. }
  197. }
  198. {
  199. vG[id].resize(FHcount,3);
  200. vJ[id].resize(FHcount,1);
  201. //nG += FHcount;
  202. size_t h = 0;
  203. assert(counts(id) == IM.rows());
  204. for(int i = 0;i<counts(id);i++)
  205. {
  206. const size_t f = IM(i);
  207. //if(f_flip)
  208. //{
  209. // flip(f) = !flip(f);
  210. //}
  211. if(FH[f])
  212. {
  213. vG[id].row(h) = (flip(f)?F.row(f).reverse().eval():F.row(f));
  214. vJ[id](h,0) = f;
  215. h++;
  216. }
  217. }
  218. assert((int)h == FHcount);
  219. }
  220. }
  221. // Is A inside B? Assuming A and B are consistently oriented but closed and
  222. // non-intersecting.
  223. const auto & is_component_inside_other = [](
  224. const Eigen::PlainObjectBase<DerivedV> & V,
  225. const MatrixXV & BC,
  226. const MatrixXG & A,
  227. const MatrixXJ & AJ,
  228. const MatrixXG & B)->bool
  229. {
  230. const auto & bounding_box = [](
  231. const Eigen::PlainObjectBase<DerivedV> & V,
  232. const MatrixXG & F)->
  233. MatrixXV
  234. {
  235. MatrixXV BB(2,3);
  236. BB<<
  237. 1e26,1e26,1e26,
  238. -1e26,-1e26,-1e26;
  239. const size_t m = F.rows();
  240. for(size_t f = 0;f<m;f++)
  241. {
  242. for(size_t c = 0;c<3;c++)
  243. {
  244. const auto & vfc = V.row(F(f,c));
  245. BB.row(0) = BB.row(0).array().min(vfc.array()).eval();
  246. BB.row(1) = BB.row(1).array().max(vfc.array()).eval();
  247. }
  248. }
  249. return BB;
  250. };
  251. // A lot of the time we're dealing with unrelated, distant components: cull
  252. // them.
  253. MatrixXV ABB = bounding_box(V,A);
  254. MatrixXV BBB = bounding_box(V,B);
  255. if( (BBB.row(0)-ABB.row(1)).maxCoeff()>0 ||
  256. (ABB.row(0)-BBB.row(1)).maxCoeff()>0 )
  257. {
  258. // bounding boxes do not overlap
  259. return false;
  260. }
  261. ////////////////////////////////////////////////////////////////////////
  262. // POTENTIAL ROBUSTNESS WEAK AREA
  263. ////////////////////////////////////////////////////////////////////////
  264. //
  265. // q could be so close (<~1e-16) to B that the winding number is not a robust way to
  266. // determine inside/outsideness. We could try to find a _better_ q which is
  267. // farther away, but couldn't they all be bad?
  268. MatrixXV q = BC.row(AJ(0));
  269. // In a perfect world, it's enough to test a single point.
  270. double w;
  271. winding_number_3(
  272. V.data(),V.rows(),
  273. B.data(),B.rows(),
  274. q.data(),1,&w);
  275. return fabs(w)>0.5;
  276. };
  277. // Reject components which are completely inside other components
  278. vector<bool> keep(ncc,true);
  279. size_t nG = 0;
  280. // This is O( ncc * ncc * m)
  281. for(size_t id = 0;id<ncc;id++)
  282. {
  283. for(size_t oid = 0;oid<ncc;oid++)
  284. {
  285. if(id == oid)
  286. {
  287. continue;
  288. }
  289. keep[id] = keep[id] &&
  290. !is_component_inside_other(V,BC,vG[id],vJ[id],vG[oid]);
  291. }
  292. if(keep[id])
  293. {
  294. nG += vJ[id].rows();
  295. }
  296. }
  297. // collect G and J across components
  298. G.resize(nG,3);
  299. J.resize(nG,1);
  300. {
  301. size_t off = 0;
  302. for(Index id = 0;id<(Index)ncc;id++)
  303. {
  304. if(keep[id])
  305. {
  306. assert(vG[id].rows() == vJ[id].rows());
  307. G.block(off,0,vG[id].rows(),vG[id].cols()) = vG[id];
  308. J.block(off,0,vJ[id].rows(),vJ[id].cols()) = vJ[id];
  309. off += vG[id].rows();
  310. }
  311. }
  312. }
  313. }
  314. #ifdef IGL_STATIC_LIBRARY
  315. // Explicit template specialization
  316. template void igl::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> >&);
  317. #endif