WindingNumberTree.h 13 KB

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