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. // Remove any exactly duplicate vertices
  160. // Q: Can this ever increase the complexity of the boundary?
  161. // Q: Would we gain even more by remove almost exactly duplicate vertices?
  162. Eigen::MatrixXi SF,SVI,SVJ;
  163. igl::remove_duplicate_vertices(_V,_F,0.0,SV,SVI,SVJ,F);
  164. triangle_fan(exterior_edges(F),cap);
  165. V = SV;
  166. }
  167. template <typename Point>
  168. inline igl::WindingNumberTree<Point>::WindingNumberTree(
  169. const igl::WindingNumberTree<Point> & parent,
  170. const Eigen::MatrixXi & _F):
  171. method(parent.method),
  172. parent(&parent),
  173. V(parent.V),
  174. SV(),
  175. F(_F),
  176. cap(triangle_fan(exterior_edges(_F)))
  177. {
  178. }
  179. template <typename Point>
  180. inline igl::WindingNumberTree<Point>::~WindingNumberTree()
  181. {
  182. using namespace std;
  183. // Delete children
  184. typename list<WindingNumberTree<Point>* >::iterator cit = children.begin();
  185. while(cit != children.end())
  186. {
  187. // clear the memory of this item
  188. delete (* cit);
  189. // erase from list, returns next element in iterator
  190. cit = children.erase(cit);
  191. }
  192. }
  193. template <typename Point>
  194. inline void igl::WindingNumberTree<Point>::set_method(const WindingNumberMethod & m)
  195. {
  196. this->method = m;
  197. for(auto child : children)
  198. {
  199. child->set_method(m);
  200. }
  201. }
  202. template <typename Point>
  203. inline const Eigen::MatrixXd & igl::WindingNumberTree<Point>::getV() const
  204. {
  205. return V;
  206. }
  207. template <typename Point>
  208. inline const Eigen::MatrixXi & igl::WindingNumberTree<Point>::getF() const
  209. {
  210. return F;
  211. }
  212. template <typename Point>
  213. inline const Eigen::MatrixXi & igl::WindingNumberTree<Point>::getcap() const
  214. {
  215. return cap;
  216. }
  217. template <typename Point>
  218. inline void igl::WindingNumberTree<Point>::grow()
  219. {
  220. // Don't grow
  221. return;
  222. }
  223. template <typename Point>
  224. inline bool igl::WindingNumberTree<Point>::inside(const Point & /*p*/) const
  225. {
  226. return true;
  227. }
  228. template <typename Point>
  229. inline double igl::WindingNumberTree<Point>::winding_number(const Point & p) const
  230. {
  231. using namespace std;
  232. //cout<<"+"<<boundary.rows();
  233. // If inside then we need to be careful
  234. if(inside(p))
  235. {
  236. // If not a leaf then recurse
  237. if(children.size()>0)
  238. {
  239. // Recurse on each child and accumulate
  240. double sum = 0;
  241. for(
  242. typename list<WindingNumberTree<Point>* >::const_iterator cit = children.begin();
  243. cit != children.end();
  244. cit++)
  245. {
  246. switch(method)
  247. {
  248. case EXACT_WINDING_NUMBER_METHOD:
  249. sum += (*cit)->winding_number(p);
  250. break;
  251. case APPROX_SIMPLE_WINDING_NUMBER_METHOD:
  252. case APPROX_CACHE_WINDING_NUMBER_METHOD:
  253. //if((*cit)->max_simple_abs_winding_number(p) > min_max_w)
  254. //{
  255. sum += (*cit)->winding_number(p);
  256. //}
  257. break;
  258. default:
  259. assert(false);
  260. break;
  261. }
  262. }
  263. return sum;
  264. }else
  265. {
  266. return winding_number_all(p);
  267. }
  268. }else{
  269. // Otherwise we can just consider boundary
  270. // Q: If we using the "multipole" method should we also subdivide the
  271. // boundary case?
  272. if((cap.rows() - 2) < F.rows())
  273. {
  274. switch(method)
  275. {
  276. case EXACT_WINDING_NUMBER_METHOD:
  277. return winding_number_boundary(p);
  278. case APPROX_SIMPLE_WINDING_NUMBER_METHOD:
  279. {
  280. double dist = (p-center).norm();
  281. // Radius is already an overestimate of inside
  282. if(dist>1.0*radius)
  283. {
  284. return 0;
  285. }else
  286. {
  287. return winding_number_boundary(p);
  288. }
  289. }
  290. case APPROX_CACHE_WINDING_NUMBER_METHOD:
  291. {
  292. return parent->cached_winding_number(*this,p);
  293. }
  294. default: assert(false);break;
  295. }
  296. }else
  297. {
  298. // doesn't pay off to use boundary
  299. return winding_number_all(p);
  300. }
  301. }
  302. return 0;
  303. }
  304. template <typename Point>
  305. inline double igl::WindingNumberTree<Point>::winding_number_all(const Point & p) const
  306. {
  307. double w = 0;
  308. igl::winding_number_3(
  309. V.data(),
  310. V.rows(),
  311. F.data(),
  312. F.rows(),
  313. p.data(),
  314. 1,
  315. &w);
  316. return w;
  317. }
  318. template <typename Point>
  319. inline double igl::WindingNumberTree<Point>::winding_number_boundary(const Point & p) const
  320. {
  321. using namespace Eigen;
  322. using namespace std;
  323. double w = 0;
  324. // `cap` is already flipped inside out, so we don't need to flip sign of w
  325. igl::winding_number_3(
  326. V.data(),
  327. V.rows(),
  328. cap.data(),
  329. cap.rows(),
  330. &p[0],
  331. 1,
  332. &w);
  333. return w;
  334. }
  335. //template <typename Point>
  336. //inline double igl::WindingNumberTree<Point>::winding_number_approx_simple(
  337. // const Point & p,
  338. // const double min_max_w)
  339. //{
  340. // using namespace std;
  341. // if(max_simple_abs_winding_number(p) > min_max_w)
  342. // {
  343. // return winding_number(p);
  344. // }else
  345. // {
  346. // cout<<"Skipped! "<<max_simple_abs_winding_number(p)<<"<"<<min_max_w<<endl;
  347. // return 0;
  348. // }
  349. //}
  350. template <typename Point>
  351. inline void igl::WindingNumberTree<Point>::print(const char * tab)
  352. {
  353. using namespace std;
  354. // Print all facets
  355. cout<<tab<<"["<<endl<<F<<endl<<"]";
  356. // Print children
  357. for(
  358. typename list<WindingNumberTree<Point>* >::iterator cit = children.begin();
  359. cit != children.end();
  360. cit++)
  361. {
  362. cout<<","<<endl;
  363. (*cit)->print((string(tab)+"").c_str());
  364. }
  365. }
  366. template <typename Point>
  367. inline double igl::WindingNumberTree<Point>::max_abs_winding_number(const Point & p) const
  368. {
  369. return std::numeric_limits<double>::infinity();
  370. }
  371. template <typename Point>
  372. inline double igl::WindingNumberTree<Point>::max_simple_abs_winding_number(const Point & p) const
  373. {
  374. using namespace std;
  375. return numeric_limits<double>::infinity();
  376. }
  377. template <typename Point>
  378. inline double igl::WindingNumberTree<Point>::cached_winding_number(
  379. const igl::WindingNumberTree<Point> & that,
  380. const Point & p) const
  381. {
  382. using namespace std;
  383. using namespace igl;
  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