WindingNumberTree.h 16 KB

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