WindingNumberTree.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 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. #ifndef IGL_WINDINGNUMBERTREE_H
  9. #define IGL_WINDINGNUMBERTREE_H
  10. #include <list>
  11. #include <map>
  12. #include <Eigen/Dense>
  13. #include "WindingNumberMethod.h"
  14. namespace igl
  15. {
  16. // Space partitioning tree for computing winding number hierarchically.
  17. //
  18. // Templates:
  19. // Point type for points in space, e.g. Eigen::Vector3d
  20. template <
  21. typename Point,
  22. typename DerivedV,
  23. typename DerivedF >
  24. class WindingNumberTree
  25. {
  26. public:
  27. // Method to use (see enum above)
  28. //static double min_max_w;
  29. static std::map<
  30. std::pair<const WindingNumberTree*,const WindingNumberTree*>,
  31. typename DerivedV::Scalar>
  32. cached;
  33. // This is only need to fill in references, it should never actually be touched
  34. // and shouldn't cause race conditions. (This is a hack, but I think it's "safe")
  35. static DerivedV dummyV;
  36. protected:
  37. WindingNumberMethod method;
  38. const WindingNumberTree * parent;
  39. std::list<WindingNumberTree * > children;
  40. typedef
  41. Eigen::Matrix<typename DerivedV::Scalar,Eigen::Dynamic,Eigen::Dynamic>
  42. MatrixXS;
  43. typedef
  44. Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,Eigen::Dynamic>
  45. MatrixXF;
  46. //// List of boundary edges (recall edges are vertices in 2d)
  47. //const Eigen::MatrixXi boundary;
  48. // Base mesh vertices
  49. DerivedV & V;
  50. // Base mesh vertices with duplicates removed
  51. MatrixXS SV;
  52. // Facets in this bounding volume
  53. MatrixXF F;
  54. // Tessellated boundary curve
  55. MatrixXF cap;
  56. // Upper Bound on radius of enclosing ball
  57. typename DerivedV::Scalar radius;
  58. // (Approximate) center (of mass)
  59. Point center;
  60. public:
  61. inline WindingNumberTree();
  62. // For root
  63. inline WindingNumberTree(
  64. const Eigen::MatrixBase<DerivedV> & V,
  65. const Eigen::MatrixBase<DerivedF> & F);
  66. // For chilluns
  67. inline WindingNumberTree(
  68. const WindingNumberTree<Point,DerivedV,DerivedF> & parent,
  69. const Eigen::MatrixBase<DerivedF> & F);
  70. inline virtual ~WindingNumberTree();
  71. inline void delete_children();
  72. inline virtual void set_mesh(
  73. const Eigen::MatrixBase<DerivedV> & V,
  74. const Eigen::MatrixBase<DerivedF> & F);
  75. // Set method
  76. inline void set_method( const WindingNumberMethod & m);
  77. public:
  78. inline const DerivedV & getV() const;
  79. inline const MatrixXF & getF() const;
  80. inline const MatrixXF & getcap() const;
  81. // Grow the Tree recursively
  82. inline virtual void grow();
  83. // Determine whether a given point is inside the bounding
  84. //
  85. // Inputs:
  86. // p query point
  87. // Returns true if the point p is inside this bounding volume
  88. inline virtual bool inside(const Point & p) const;
  89. // Compute the (partial) winding number of a given point p
  90. // According to method
  91. //
  92. // Inputs:
  93. // p query point
  94. // Returns winding number
  95. inline typename DerivedV::Scalar winding_number(const Point & p) const;
  96. // Same as above, but always computes winding number using exact method
  97. // (sum over every facet)
  98. inline typename DerivedV::Scalar winding_number_all(const Point & p) const;
  99. // Same as above, but always computes using sum over tessllated boundary
  100. inline typename DerivedV::Scalar winding_number_boundary(const Point & p) const;
  101. //// Same as winding_number above, but if max_simple_abs_winding_number is
  102. //// less than some threshold min_max_w just return 0 (colloquially the "fast
  103. //// multipole method)
  104. ////
  105. ////
  106. //// Inputs:
  107. //// p query point
  108. //// min_max_w minimum max simple w to be processed
  109. //// Returns approximate winding number
  110. //double winding_number_approx_simple(
  111. // const Point & p,
  112. // const double min_max_w);
  113. // Print contents of Tree
  114. //
  115. // Optional input:
  116. // tab tab to show depth
  117. inline void print(const char * tab="");
  118. // Determine max absolute winding number
  119. //
  120. // Inputs:
  121. // p query point
  122. // Returns max winding number of
  123. inline virtual typename DerivedV::Scalar max_abs_winding_number(const Point & p) const;
  124. // Same as above, but stronger assumptions on (V,F). Assumes (V,F) is a
  125. // simple polyhedron
  126. inline virtual typename DerivedV::Scalar max_simple_abs_winding_number(const Point & p) const;
  127. // Compute or read cached winding number for point p with respect to mesh
  128. // in bounding box, recursing according to approximation criteria
  129. //
  130. // Inputs:
  131. // p query point
  132. // that WindingNumberTree containing mesh w.r.t. which we're computing w.n.
  133. // Returns cached winding number
  134. inline virtual typename DerivedV::Scalar cached_winding_number(const WindingNumberTree & that, const Point & p) const;
  135. };
  136. }
  137. // Implementation
  138. #include "WindingNumberTree.h"
  139. #include "winding_number.h"
  140. #include "triangle_fan.h"
  141. #include "exterior_edges.h"
  142. #include <igl/PI.h>
  143. #include <igl/remove_duplicate_vertices.h>
  144. #include <iostream>
  145. #include <limits>
  146. //template <typename Point, typename DerivedV, typename DerivedF>
  147. //WindingNumberMethod WindingNumberTree<Point,DerivedV,DerivedF>::method = EXACT_WINDING_NUMBER_METHOD;
  148. //template <typename Point, typename DerivedV, typename DerivedF>
  149. //double WindingNumberTree<Point,DerivedV,DerivedF>::min_max_w = 0;
  150. template <typename Point, typename DerivedV, typename DerivedF>
  151. std::map< std::pair<const igl::WindingNumberTree<Point,DerivedV,DerivedF>*,const igl::WindingNumberTree<Point,DerivedV,DerivedF>*>, typename DerivedV::Scalar>
  152. igl::WindingNumberTree<Point,DerivedV,DerivedF>::cached;
  153. template <typename Point, typename DerivedV, typename DerivedF>
  154. inline igl::WindingNumberTree<Point,DerivedV,DerivedF>::WindingNumberTree():
  155. method(EXACT_WINDING_NUMBER_METHOD),
  156. parent(NULL),
  157. V(dummyV),
  158. SV(),
  159. F(),
  160. cap(),
  161. radius(std::numeric_limits<typename DerivedV::Scalar>::infinity()),
  162. center(0,0,0)
  163. {
  164. }
  165. template <typename Point, typename DerivedV, typename DerivedF>
  166. inline igl::WindingNumberTree<Point,DerivedV,DerivedF>::WindingNumberTree(
  167. const Eigen::MatrixBase<DerivedV> & _V,
  168. const Eigen::MatrixBase<DerivedF> & _F):
  169. method(EXACT_WINDING_NUMBER_METHOD),
  170. parent(NULL),
  171. V(dummyV),
  172. SV(),
  173. F(),
  174. cap(),
  175. radius(std::numeric_limits<typename DerivedV::Scalar>::infinity()),
  176. center(0,0,0)
  177. {
  178. set_mesh(_V,_F);
  179. }
  180. template <typename Point, typename DerivedV, typename DerivedF>
  181. inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::set_mesh(
  182. const Eigen::MatrixBase<DerivedV> & _V,
  183. const Eigen::MatrixBase<DerivedF> & _F)
  184. {
  185. using namespace std;
  186. // Remove any exactly duplicate vertices
  187. // Q: Can this ever increase the complexity of the boundary?
  188. // Q: Would we gain even more by remove almost exactly duplicate vertices?
  189. MatrixXF SF,SVI,SVJ;
  190. igl::remove_duplicate_vertices(_V,_F,0.0,SV,SVI,SVJ,F);
  191. triangle_fan(igl::exterior_edges(F),cap);
  192. V = SV;
  193. }
  194. template <typename Point, typename DerivedV, typename DerivedF>
  195. inline igl::WindingNumberTree<Point,DerivedV,DerivedF>::WindingNumberTree(
  196. const igl::WindingNumberTree<Point,DerivedV,DerivedF> & parent,
  197. const Eigen::MatrixBase<DerivedF> & _F):
  198. method(parent.method),
  199. parent(&parent),
  200. V(parent.V),
  201. SV(),
  202. F(_F),
  203. cap(triangle_fan(igl::exterior_edges(_F)))
  204. {
  205. }
  206. template <typename Point, typename DerivedV, typename DerivedF>
  207. inline igl::WindingNumberTree<Point,DerivedV,DerivedF>::~WindingNumberTree()
  208. {
  209. delete_children();
  210. }
  211. template <typename Point, typename DerivedV, typename DerivedF>
  212. inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::delete_children()
  213. {
  214. using namespace std;
  215. // Delete children
  216. typename list<WindingNumberTree<Point,DerivedV,DerivedF>* >::iterator cit = children.begin();
  217. while(cit != children.end())
  218. {
  219. // clear the memory of this item
  220. delete (* cit);
  221. // erase from list, returns next element in iterator
  222. cit = children.erase(cit);
  223. }
  224. }
  225. template <typename Point, typename DerivedV, typename DerivedF>
  226. inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::set_method(const WindingNumberMethod & m)
  227. {
  228. this->method = m;
  229. for(auto child : children)
  230. {
  231. child->set_method(m);
  232. }
  233. }
  234. template <typename Point, typename DerivedV, typename DerivedF>
  235. inline const DerivedV & igl::WindingNumberTree<Point,DerivedV,DerivedF>::getV() const
  236. {
  237. return V;
  238. }
  239. template <typename Point, typename DerivedV, typename DerivedF>
  240. inline const typename igl::WindingNumberTree<Point,DerivedV,DerivedF>::MatrixXF&
  241. igl::WindingNumberTree<Point,DerivedV,DerivedF>::getF() const
  242. {
  243. return F;
  244. }
  245. template <typename Point, typename DerivedV, typename DerivedF>
  246. inline const typename igl::WindingNumberTree<Point,DerivedV,DerivedF>::MatrixXF&
  247. igl::WindingNumberTree<Point,DerivedV,DerivedF>::getcap() const
  248. {
  249. return cap;
  250. }
  251. template <typename Point, typename DerivedV, typename DerivedF>
  252. inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::grow()
  253. {
  254. // Don't grow
  255. return;
  256. }
  257. template <typename Point, typename DerivedV, typename DerivedF>
  258. inline bool igl::WindingNumberTree<Point,DerivedV,DerivedF>::inside(const Point & /*p*/) const
  259. {
  260. return true;
  261. }
  262. template <typename Point, typename DerivedV, typename DerivedF>
  263. inline typename DerivedV::Scalar
  264. igl::WindingNumberTree<Point,DerivedV,DerivedF>::winding_number(const Point & p) const
  265. {
  266. using namespace std;
  267. //cout<<"+"<<boundary.rows();
  268. // If inside then we need to be careful
  269. if(inside(p))
  270. {
  271. // If not a leaf then recurse
  272. if(children.size()>0)
  273. {
  274. // Recurse on each child and accumulate
  275. typename DerivedV::Scalar sum = 0;
  276. for(
  277. typename list<WindingNumberTree<Point,DerivedV,DerivedF>* >::const_iterator cit = children.begin();
  278. cit != children.end();
  279. cit++)
  280. {
  281. switch(method)
  282. {
  283. case EXACT_WINDING_NUMBER_METHOD:
  284. sum += (*cit)->winding_number(p);
  285. break;
  286. case APPROX_SIMPLE_WINDING_NUMBER_METHOD:
  287. case APPROX_CACHE_WINDING_NUMBER_METHOD:
  288. //if((*cit)->max_simple_abs_winding_number(p) > min_max_w)
  289. //{
  290. sum += (*cit)->winding_number(p);
  291. //}
  292. break;
  293. default:
  294. assert(false);
  295. break;
  296. }
  297. }
  298. return sum;
  299. }else
  300. {
  301. return winding_number_all(p);
  302. }
  303. }else{
  304. // Otherwise we can just consider boundary
  305. // Q: If we using the "multipole" method should we also subdivide the
  306. // boundary case?
  307. if((cap.rows() - 2) < F.rows())
  308. {
  309. switch(method)
  310. {
  311. case EXACT_WINDING_NUMBER_METHOD:
  312. return winding_number_boundary(p);
  313. case APPROX_SIMPLE_WINDING_NUMBER_METHOD:
  314. {
  315. typename DerivedV::Scalar dist = (p-center).norm();
  316. // Radius is already an overestimate of inside
  317. if(dist>1.0*radius)
  318. {
  319. return 0;
  320. }else
  321. {
  322. return winding_number_boundary(p);
  323. }
  324. }
  325. case APPROX_CACHE_WINDING_NUMBER_METHOD:
  326. {
  327. return parent->cached_winding_number(*this,p);
  328. }
  329. default: assert(false);break;
  330. }
  331. }else
  332. {
  333. // doesn't pay off to use boundary
  334. return winding_number_all(p);
  335. }
  336. }
  337. return 0;
  338. }
  339. template <typename Point, typename DerivedV, typename DerivedF>
  340. inline typename DerivedV::Scalar
  341. igl::WindingNumberTree<Point,DerivedV,DerivedF>::winding_number_all(const Point & p) const
  342. {
  343. return igl::winding_number(V,F,p);
  344. }
  345. template <typename Point, typename DerivedV, typename DerivedF>
  346. inline typename DerivedV::Scalar
  347. igl::WindingNumberTree<Point,DerivedV,DerivedF>::winding_number_boundary(const Point & p) const
  348. {
  349. using namespace Eigen;
  350. using namespace std;
  351. return igl::winding_number(V,cap,p);
  352. }
  353. //template <typename Point, typename DerivedV, typename DerivedF>
  354. //inline double igl::WindingNumberTree<Point,DerivedV,DerivedF>::winding_number_approx_simple(
  355. // const Point & p,
  356. // const double min_max_w)
  357. //{
  358. // using namespace std;
  359. // if(max_simple_abs_winding_number(p) > min_max_w)
  360. // {
  361. // return winding_number(p);
  362. // }else
  363. // {
  364. // cout<<"Skipped! "<<max_simple_abs_winding_number(p)<<"<"<<min_max_w<<endl;
  365. // return 0;
  366. // }
  367. //}
  368. template <typename Point, typename DerivedV, typename DerivedF>
  369. inline void igl::WindingNumberTree<Point,DerivedV,DerivedF>::print(const char * tab)
  370. {
  371. using namespace std;
  372. // Print all facets
  373. cout<<tab<<"["<<endl<<F<<endl<<"]";
  374. // Print children
  375. for(
  376. typename list<WindingNumberTree<Point,DerivedV,DerivedF>* >::iterator cit = children.begin();
  377. cit != children.end();
  378. cit++)
  379. {
  380. cout<<","<<endl;
  381. (*cit)->print((string(tab)+"").c_str());
  382. }
  383. }
  384. template <typename Point, typename DerivedV, typename DerivedF>
  385. inline typename DerivedV::Scalar
  386. igl::WindingNumberTree<Point,DerivedV,DerivedF>::max_abs_winding_number(const Point & /*p*/) const
  387. {
  388. return std::numeric_limits<typename DerivedV::Scalar>::infinity();
  389. }
  390. template <typename Point, typename DerivedV, typename DerivedF>
  391. inline typename DerivedV::Scalar
  392. igl::WindingNumberTree<Point,DerivedV,DerivedF>::max_simple_abs_winding_number(
  393. const Point & /*p*/) const
  394. {
  395. using namespace std;
  396. return numeric_limits<typename DerivedV::Scalar>::infinity();
  397. }
  398. template <typename Point, typename DerivedV, typename DerivedF>
  399. inline typename DerivedV::Scalar
  400. igl::WindingNumberTree<Point,DerivedV,DerivedF>::cached_winding_number(
  401. const igl::WindingNumberTree<Point,DerivedV,DerivedF> & that,
  402. const Point & p) const
  403. {
  404. using namespace std;
  405. // Simple metric for `is_far`
  406. //
  407. // this that
  408. // --------
  409. // ----- / | \ .
  410. // / r \ / R \ .
  411. // | p ! | | ! |
  412. // \_____/ \ /
  413. // \________/
  414. //
  415. //
  416. // a = angle formed by trapazoid formed by raising sides with lengths r and R
  417. // at respective centers.
  418. //
  419. // a = atan2(R-r,d), where d is the distance between centers
  420. // That should be bigger (what about parent? what about sister?)
  421. bool is_far = this->radius<that.radius;
  422. if(is_far)
  423. {
  424. typename DerivedV::Scalar a = atan2(
  425. that.radius - this->radius,
  426. (that.center - this->center).norm());
  427. assert(a>0);
  428. is_far = (a<PI/8.0);
  429. }
  430. if(is_far)
  431. {
  432. // Not implemented yet
  433. pair<const WindingNumberTree*,const WindingNumberTree*> this_that(this,&that);
  434. // Need to compute it for first time?
  435. if(cached.count(this_that)==0)
  436. {
  437. cached[this_that] =
  438. that.winding_number_boundary(this->center);
  439. }
  440. return cached[this_that];
  441. }else if(children.size() == 0)
  442. {
  443. // not far and hierarchy ended too soon: can't use cache
  444. return that.winding_number_boundary(p);
  445. }else
  446. {
  447. for(
  448. typename list<WindingNumberTree<Point,DerivedV,DerivedF>* >::const_iterator cit = children.begin();
  449. cit != children.end();
  450. cit++)
  451. {
  452. if((*cit)->inside(p))
  453. {
  454. return (*cit)->cached_winding_number(that,p);
  455. }
  456. }
  457. // Not inside any children? This can totally happen because bounding boxes
  458. // are set to bound contained facets. So sibilings may overlap and their
  459. // union may not contain their parent (though, their union is certainly a
  460. // subset of their parent).
  461. assert(false);
  462. }
  463. return 0;
  464. }
  465. // Explicit instantiation of static variable
  466. template <
  467. typename Point,
  468. typename DerivedV,
  469. typename DerivedF >
  470. DerivedV igl::WindingNumberTree<Point,DerivedV,DerivedF>::dummyV;
  471. #endif