WindingNumberTree.h 14 KB

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