WindingNumberTree.h 13 KB

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