WindingNumberTree.h 12 KB

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