WindingNumberTree.h 14 KB

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