WindingNumberTree.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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();
  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. method(EXACT_WINDING_NUMBER_METHOD),
  141. parent(NULL),
  142. V(dummyV),
  143. SV(),
  144. F(),
  145. //boundary(igl::boundary_facets<Eigen::MatrixXi,Eigen::MatrixXi>(F))
  146. cap(),
  147. radius(std::numeric_limits<double>::infinity()),
  148. center(0,0,0)
  149. {
  150. }
  151. template <typename Point>
  152. inline igl::WindingNumberTree<Point>::WindingNumberTree(
  153. const Eigen::MatrixXd & _V,
  154. const Eigen::MatrixXi & _F):
  155. method(EXACT_WINDING_NUMBER_METHOD),
  156. parent(NULL),
  157. V(dummyV),
  158. SV(),
  159. F(),
  160. //boundary(igl::boundary_facets<Eigen::MatrixXi,Eigen::MatrixXi>(F))
  161. cap(),
  162. radius(std::numeric_limits<double>::infinity()),
  163. center(0,0,0)
  164. {
  165. set_mesh(_V,_F);
  166. }
  167. template <typename Point>
  168. inline void igl::WindingNumberTree<Point>::set_mesh(
  169. const Eigen::MatrixXd & _V,
  170. const Eigen::MatrixXi & _F)
  171. {
  172. using namespace std;
  173. // Remove any exactly duplicate vertices
  174. // Q: Can this ever increase the complexity of the boundary?
  175. // Q: Would we gain even more by remove almost exactly duplicate vertices?
  176. Eigen::MatrixXi SF,SVI,SVJ;
  177. igl::remove_duplicate_vertices(_V,_F,0.0,SV,SVI,SVJ,F);
  178. triangle_fan(igl::exterior_edges(F),cap);
  179. V = SV;
  180. }
  181. template <typename Point>
  182. inline igl::WindingNumberTree<Point>::WindingNumberTree(
  183. const igl::WindingNumberTree<Point> & parent,
  184. const Eigen::MatrixXi & _F):
  185. method(parent.method),
  186. parent(&parent),
  187. V(parent.V),
  188. SV(),
  189. F(_F),
  190. cap(triangle_fan(igl::exterior_edges(_F)))
  191. {
  192. }
  193. template <typename Point>
  194. inline igl::WindingNumberTree<Point>::~WindingNumberTree()
  195. {
  196. using namespace std;
  197. // Delete children
  198. typename list<WindingNumberTree<Point>* >::iterator cit = children.begin();
  199. while(cit != children.end())
  200. {
  201. // clear the memory of this item
  202. delete (* cit);
  203. // erase from list, returns next element in iterator
  204. cit = children.erase(cit);
  205. }
  206. }
  207. template <typename Point>
  208. inline void igl::WindingNumberTree<Point>::set_method(const WindingNumberMethod & m)
  209. {
  210. this->method = m;
  211. for(auto child : children)
  212. {
  213. child->set_method(m);
  214. }
  215. }
  216. template <typename Point>
  217. inline const Eigen::MatrixXd & igl::WindingNumberTree<Point>::getV() const
  218. {
  219. return V;
  220. }
  221. template <typename Point>
  222. inline const Eigen::MatrixXi & igl::WindingNumberTree<Point>::getF() const
  223. {
  224. return F;
  225. }
  226. template <typename Point>
  227. inline const Eigen::MatrixXi & igl::WindingNumberTree<Point>::getcap() const
  228. {
  229. return cap;
  230. }
  231. template <typename Point>
  232. inline void igl::WindingNumberTree<Point>::grow()
  233. {
  234. // Don't grow
  235. return;
  236. }
  237. template <typename Point>
  238. inline bool igl::WindingNumberTree<Point>::inside(const Point & /*p*/) const
  239. {
  240. return true;
  241. }
  242. template <typename Point>
  243. inline double igl::WindingNumberTree<Point>::winding_number(const Point & p) const
  244. {
  245. using namespace std;
  246. //cout<<"+"<<boundary.rows();
  247. // If inside then we need to be careful
  248. if(inside(p))
  249. {
  250. // If not a leaf then recurse
  251. if(children.size()>0)
  252. {
  253. // Recurse on each child and accumulate
  254. double sum = 0;
  255. for(
  256. typename list<WindingNumberTree<Point>* >::const_iterator cit = children.begin();
  257. cit != children.end();
  258. cit++)
  259. {
  260. switch(method)
  261. {
  262. case EXACT_WINDING_NUMBER_METHOD:
  263. sum += (*cit)->winding_number(p);
  264. break;
  265. case APPROX_SIMPLE_WINDING_NUMBER_METHOD:
  266. case APPROX_CACHE_WINDING_NUMBER_METHOD:
  267. //if((*cit)->max_simple_abs_winding_number(p) > min_max_w)
  268. //{
  269. sum += (*cit)->winding_number(p);
  270. //}
  271. break;
  272. default:
  273. assert(false);
  274. break;
  275. }
  276. }
  277. return sum;
  278. }else
  279. {
  280. return winding_number_all(p);
  281. }
  282. }else{
  283. // Otherwise we can just consider boundary
  284. // Q: If we using the "multipole" method should we also subdivide the
  285. // boundary case?
  286. if((cap.rows() - 2) < F.rows())
  287. {
  288. switch(method)
  289. {
  290. case EXACT_WINDING_NUMBER_METHOD:
  291. return winding_number_boundary(p);
  292. case APPROX_SIMPLE_WINDING_NUMBER_METHOD:
  293. {
  294. double dist = (p-center).norm();
  295. // Radius is already an overestimate of inside
  296. if(dist>1.0*radius)
  297. {
  298. return 0;
  299. }else
  300. {
  301. return winding_number_boundary(p);
  302. }
  303. }
  304. case APPROX_CACHE_WINDING_NUMBER_METHOD:
  305. {
  306. return parent->cached_winding_number(*this,p);
  307. }
  308. default: assert(false);break;
  309. }
  310. }else
  311. {
  312. // doesn't pay off to use boundary
  313. return winding_number_all(p);
  314. }
  315. }
  316. return 0;
  317. }
  318. template <typename Point>
  319. inline double igl::WindingNumberTree<Point>::winding_number_all(const Point & p) const
  320. {
  321. double w = 0;
  322. igl::winding_number_3(
  323. V.data(),
  324. V.rows(),
  325. F.data(),
  326. F.rows(),
  327. p.data(),
  328. 1,
  329. &w);
  330. return w;
  331. }
  332. template <typename Point>
  333. inline double igl::WindingNumberTree<Point>::winding_number_boundary(const Point & p) const
  334. {
  335. using namespace Eigen;
  336. using namespace std;
  337. double w = 0;
  338. // `cap` is already flipped inside out, so we don't need to flip sign of w
  339. igl::winding_number_3(
  340. V.data(),
  341. V.rows(),
  342. cap.data(),
  343. cap.rows(),
  344. &p[0],
  345. 1,
  346. &w);
  347. return w;
  348. }
  349. //template <typename Point>
  350. //inline double igl::WindingNumberTree<Point>::winding_number_approx_simple(
  351. // const Point & p,
  352. // const double min_max_w)
  353. //{
  354. // using namespace std;
  355. // if(max_simple_abs_winding_number(p) > min_max_w)
  356. // {
  357. // return winding_number(p);
  358. // }else
  359. // {
  360. // cout<<"Skipped! "<<max_simple_abs_winding_number(p)<<"<"<<min_max_w<<endl;
  361. // return 0;
  362. // }
  363. //}
  364. template <typename Point>
  365. inline void igl::WindingNumberTree<Point>::print(const char * tab)
  366. {
  367. using namespace std;
  368. // Print all facets
  369. cout<<tab<<"["<<endl<<F<<endl<<"]";
  370. // Print children
  371. for(
  372. typename list<WindingNumberTree<Point>* >::iterator cit = children.begin();
  373. cit != children.end();
  374. cit++)
  375. {
  376. cout<<","<<endl;
  377. (*cit)->print((string(tab)+"").c_str());
  378. }
  379. }
  380. template <typename Point>
  381. inline double igl::WindingNumberTree<Point>::max_abs_winding_number(const Point & p) const
  382. {
  383. return std::numeric_limits<double>::infinity();
  384. }
  385. template <typename Point>
  386. inline double igl::WindingNumberTree<Point>::max_simple_abs_winding_number(const Point & p) const
  387. {
  388. using namespace std;
  389. return numeric_limits<double>::infinity();
  390. }
  391. template <typename Point>
  392. inline double igl::WindingNumberTree<Point>::cached_winding_number(
  393. const igl::WindingNumberTree<Point> & that,
  394. const Point & p) const
  395. {
  396. using namespace std;
  397. // Simple metric for "far".
  398. // this that
  399. // --------
  400. // ----- / | \ .
  401. // / r \ / R \ .
  402. // | p ! | | ! |
  403. // \_____/ \ /
  404. // \________/
  405. //
  406. //
  407. // a = angle formed by trapazoid formed by raising sides with lengths r and R
  408. // at respective centers.
  409. //
  410. // a = atan2(R-r,d), where d is the distance between centers
  411. // That should be bigger (what about parent? what about sister?)
  412. bool far = this->radius<that.radius;
  413. if(far)
  414. {
  415. double a = atan2(
  416. that.radius - this->radius,
  417. (that.center - this->center).norm());
  418. assert(a>0);
  419. far = (a<PI/8.0);
  420. }
  421. if(far)
  422. {
  423. // Not implemented yet
  424. pair<const WindingNumberTree*,const WindingNumberTree*> this_that(this,&that);
  425. // Need to compute it for first time?
  426. if(cached.count(this_that)==0)
  427. {
  428. cached[this_that] =
  429. that.winding_number_boundary(this->center);
  430. }
  431. return cached[this_that];
  432. }else if(children.size() == 0)
  433. {
  434. // not far and hierarchy ended too soon: can't use cache
  435. return that.winding_number_boundary(p);
  436. }else
  437. {
  438. for(
  439. typename list<WindingNumberTree<Point>* >::const_iterator cit = children.begin();
  440. cit != children.end();
  441. cit++)
  442. {
  443. if((*cit)->inside(p))
  444. {
  445. return (*cit)->cached_winding_number(that,p);
  446. }
  447. }
  448. // Not inside any children? This can totally happen because bounding boxes
  449. // are set to bound contained facets. So sibilings may overlap and their
  450. // union may not contain their parent (though, their union is certainly a
  451. // subset of their parent).
  452. assert(false);
  453. }
  454. return 0;
  455. }
  456. // Explicit instanciation
  457. //template class igl::WindingNumberTree<Eigen::Vector3d >;
  458. #endif