WindingNumberAABB.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. // # MUTUAL DEPENDENCY ISSUE FOR HEADER ONLY VERSION
  9. // MUST INCLUDE winding_number.h first before guard:
  10. #include "winding_number.h"
  11. #ifndef IGL_WINDINGNUMBERAABB_H
  12. #define IGL_WINDINGNUMBERAABB_H
  13. #include "WindingNumberTree.h"
  14. namespace igl
  15. {
  16. template <typename Point>
  17. class WindingNumberAABB : public WindingNumberTree<Point>
  18. {
  19. protected:
  20. Point min_corner;
  21. Point max_corner;
  22. double total_positive_area;
  23. public:
  24. enum SplitMethod
  25. {
  26. CENTER_ON_LONGEST_AXIS = 0,
  27. MEDIAN_ON_LONGEST_AXIS = 1,
  28. NUM_SPLIT_METHODS = 2
  29. } split_method;
  30. public:
  31. inline WindingNumberAABB():
  32. total_positive_area(std::numeric_limits<double>::infinity()),
  33. split_method(MEDIAN_ON_LONGEST_AXIS)
  34. {}
  35. inline WindingNumberAABB(
  36. const Eigen::MatrixXd & V,
  37. const Eigen::MatrixXi & F);
  38. inline WindingNumberAABB(
  39. const WindingNumberTree<Point> & parent,
  40. const Eigen::MatrixXi & F);
  41. // Initialize some things
  42. inline void set_mesh(
  43. const Eigen::MatrixXd & V,
  44. const Eigen::MatrixXi & F);
  45. inline void init();
  46. inline bool inside(const Point & p) const;
  47. inline virtual void grow();
  48. // Compute min and max corners
  49. inline void compute_min_max_corners();
  50. inline double max_abs_winding_number(const Point & p) const;
  51. inline double max_simple_abs_winding_number(const Point & p) const;
  52. };
  53. }
  54. // Implementation
  55. #include "winding_number.h"
  56. #include "barycenter.h"
  57. #include "median.h"
  58. #include "doublearea.h"
  59. #include "per_face_normals.h"
  60. #include <limits>
  61. #include <vector>
  62. #include <iostream>
  63. // Minimum number of faces in a hierarchy element (this is probably dependent
  64. // on speed of machine and compiler optimization)
  65. #ifndef WindingNumberAABB_MIN_F
  66. # define WindingNumberAABB_MIN_F 100
  67. #endif
  68. template <typename Point>
  69. inline void igl::WindingNumberAABB<Point>::set_mesh(
  70. const Eigen::MatrixXd & V,
  71. const Eigen::MatrixXi & F)
  72. {
  73. igl::WindingNumberTree<Point>::set_mesh(V,F);
  74. init();
  75. }
  76. template <typename Point>
  77. inline void igl::WindingNumberAABB<Point>::init()
  78. {
  79. using namespace Eigen;
  80. assert(max_corner.size() == 3);
  81. assert(min_corner.size() == 3);
  82. compute_min_max_corners();
  83. VectorXd dblA;
  84. doublearea(this->getV(),this->getF(),dblA);
  85. total_positive_area = dblA.sum()/2.0;
  86. }
  87. template <typename Point>
  88. inline igl::WindingNumberAABB<Point>::WindingNumberAABB(
  89. const Eigen::MatrixXd & V,
  90. const Eigen::MatrixXi & F):
  91. WindingNumberTree<Point>(V,F),
  92. min_corner(),
  93. max_corner(),
  94. total_positive_area(std::numeric_limits<double>::infinity()),
  95. split_method(MEDIAN_ON_LONGEST_AXIS)
  96. {
  97. init();
  98. }
  99. template <typename Point>
  100. inline igl::WindingNumberAABB<Point>::WindingNumberAABB(
  101. const WindingNumberTree<Point> & parent,
  102. const Eigen::MatrixXi & F):
  103. WindingNumberTree<Point>(parent,F),
  104. min_corner(),
  105. max_corner(),
  106. total_positive_area(std::numeric_limits<double>::infinity()),
  107. split_method(MEDIAN_ON_LONGEST_AXIS)
  108. {
  109. init();
  110. }
  111. template <typename Point>
  112. inline void igl::WindingNumberAABB<Point>::grow()
  113. {
  114. using namespace std;
  115. using namespace Eigen;
  116. //cout<<"cap.rows(): "<<this->getcap().rows()<<endl;
  117. //cout<<"F.rows(): "<<this->getF().rows()<<endl;
  118. // Base cases
  119. if(
  120. this->getF().rows() <= (WindingNumberAABB_MIN_F>0?WindingNumberAABB_MIN_F:0) ||
  121. (this->getcap().rows() - 2) >= this->getF().rows())
  122. {
  123. // Don't grow
  124. return;
  125. }
  126. // Compute longest direction
  127. int max_d = -1;
  128. double max_len = -numeric_limits<double>::infinity();
  129. for(int d = 0;d<min_corner.size();d++)
  130. {
  131. if( (max_corner[d] - min_corner[d]) > max_len )
  132. {
  133. max_len = (max_corner[d] - min_corner[d]);
  134. max_d = d;
  135. }
  136. }
  137. // Compute facet barycenters
  138. MatrixXd BC;
  139. barycenter(this->getV(),this->getF(),BC);
  140. // Blerg, why is selecting rows so difficult
  141. double split_value;
  142. // Split in longest direction
  143. switch(split_method)
  144. {
  145. case MEDIAN_ON_LONGEST_AXIS:
  146. // Determine median
  147. median(BC.col(max_d),split_value);
  148. break;
  149. default:
  150. assert(false);
  151. case CENTER_ON_LONGEST_AXIS:
  152. split_value = 0.5*(max_corner[max_d] + min_corner[max_d]);
  153. break;
  154. }
  155. //cout<<"c: "<<0.5*(max_corner[max_d] + min_corner[max_d])<<" "<<
  156. // "m: "<<split_value<<endl;;
  157. vector<int> id( this->getF().rows());
  158. for(int i = 0;i<this->getF().rows();i++)
  159. {
  160. if(BC(i,max_d) <= split_value)
  161. {
  162. id[i] = 0; //left
  163. }else
  164. {
  165. id[i] = 1; //right
  166. }
  167. }
  168. const int lefts = (int) count(id.begin(),id.end(),0);
  169. const int rights = (int) count(id.begin(),id.end(),1);
  170. if(lefts == 0 || rights == 0)
  171. {
  172. // badly balanced base case (could try to recut)
  173. return;
  174. }
  175. assert(lefts+rights == this->getF().rows());
  176. MatrixXi leftF(lefts, this->getF().cols());
  177. MatrixXi rightF(rights,this->getF().cols());
  178. int left_i = 0;
  179. int right_i = 0;
  180. for(int i = 0;i<this->getF().rows();i++)
  181. {
  182. if(id[i] == 0)
  183. {
  184. leftF.row(left_i++) = this->getF().row(i);
  185. }else if(id[i] == 1)
  186. {
  187. rightF.row(right_i++) = this->getF().row(i);
  188. }else
  189. {
  190. assert(false);
  191. }
  192. }
  193. assert(right_i == rightF.rows());
  194. assert(left_i == leftF.rows());
  195. // Finally actually grow children and Recursively grow
  196. WindingNumberAABB<Point> * leftWindingNumberAABB = new WindingNumberAABB<Point>(*this,leftF);
  197. leftWindingNumberAABB->grow();
  198. this->children.push_back(leftWindingNumberAABB);
  199. WindingNumberAABB<Point> * rightWindingNumberAABB = new WindingNumberAABB<Point>(*this,rightF);
  200. rightWindingNumberAABB->grow();
  201. this->children.push_back(rightWindingNumberAABB);
  202. }
  203. template <typename Point>
  204. inline bool igl::WindingNumberAABB<Point>::inside(const Point & p) const
  205. {
  206. assert(p.size() == max_corner.size());
  207. assert(p.size() == min_corner.size());
  208. for(int i = 0;i<p.size();i++)
  209. {
  210. //// Perfect matching is **not** robust
  211. //if( p(i) < min_corner(i) || p(i) >= max_corner(i))
  212. // **MUST** be conservative
  213. if( p(i) < min_corner(i) || p(i) > max_corner(i))
  214. {
  215. return false;
  216. }
  217. }
  218. return true;
  219. }
  220. template <typename Point>
  221. inline void igl::WindingNumberAABB<Point>::compute_min_max_corners()
  222. {
  223. using namespace std;
  224. // initialize corners
  225. for(int d = 0;d<min_corner.size();d++)
  226. {
  227. min_corner[d] = numeric_limits<double>::infinity();
  228. max_corner[d] = -numeric_limits<double>::infinity();
  229. }
  230. this->center = Point(0,0,0);
  231. // Loop over facets
  232. for(int i = 0;i<this->getF().rows();i++)
  233. {
  234. for(int j = 0;j<this->getF().cols();j++)
  235. {
  236. for(int d = 0;d<min_corner.size();d++)
  237. {
  238. min_corner[d] =
  239. this->getV()(this->getF()(i,j),d) < min_corner[d] ?
  240. this->getV()(this->getF()(i,j),d) : min_corner[d];
  241. max_corner[d] =
  242. this->getV()(this->getF()(i,j),d) > max_corner[d] ?
  243. this->getV()(this->getF()(i,j),d) : max_corner[d];
  244. }
  245. // This is biased toward vertices incident on more than one face, but
  246. // perhaps that's good
  247. this->center += this->getV().row(this->getF()(i,j));
  248. }
  249. }
  250. // Average
  251. this->center.array() /= this->getF().size();
  252. //cout<<"min_corner: "<<this->min_corner.transpose()<<endl;
  253. //cout<<"Center: "<<this->center.transpose()<<endl;
  254. //cout<<"max_corner: "<<this->max_corner.transpose()<<endl;
  255. //cout<<"Diag center: "<<((this->max_corner + this->min_corner)*0.5).transpose()<<endl;
  256. //cout<<endl;
  257. this->radius = (max_corner-min_corner).norm()/2.0;
  258. }
  259. template <typename Point>
  260. inline double igl::WindingNumberAABB<Point>::max_abs_winding_number(const Point & p) const
  261. {
  262. using namespace std;
  263. // Only valid if not inside
  264. if(inside(p))
  265. {
  266. return numeric_limits<double>::infinity();
  267. }
  268. // Q: we know the total positive area so what's the most this could project
  269. // to? Remember it could be layered in the same direction.
  270. return numeric_limits<double>::infinity();
  271. }
  272. template <typename Point>
  273. inline double igl::WindingNumberAABB<Point>::max_simple_abs_winding_number(const Point & p) const
  274. {
  275. using namespace std;
  276. using namespace Eigen;
  277. // Only valid if not inside
  278. if(inside(p))
  279. {
  280. return numeric_limits<double>::infinity();
  281. }
  282. // Max simple is the same as sum of positive winding number contributions of
  283. // bounding box
  284. // begin precomputation
  285. //MatrixXd BV((int)pow(2,3),3);
  286. MatrixXd BV((int)(1<<3),3);
  287. BV <<
  288. min_corner[0],min_corner[1],min_corner[2],
  289. min_corner[0],min_corner[1],max_corner[2],
  290. min_corner[0],max_corner[1],min_corner[2],
  291. min_corner[0],max_corner[1],max_corner[2],
  292. max_corner[0],min_corner[1],min_corner[2],
  293. max_corner[0],min_corner[1],max_corner[2],
  294. max_corner[0],max_corner[1],min_corner[2],
  295. max_corner[0],max_corner[1],max_corner[2];
  296. MatrixXi BF(2*2*3,3);
  297. BF <<
  298. 0,6,4,
  299. 0,2,6,
  300. 0,3,2,
  301. 0,1,3,
  302. 2,7,6,
  303. 2,3,7,
  304. 4,6,7,
  305. 4,7,5,
  306. 0,4,5,
  307. 0,5,1,
  308. 1,5,7,
  309. 1,7,3;
  310. MatrixXd BFN;
  311. per_face_normals(BV,BF,BFN);
  312. // end of precomputation
  313. // Only keep those with positive dot products
  314. MatrixXi PBF(BF.rows(),BF.cols());
  315. int pbfi = 0;
  316. Point p2c = 0.5*(min_corner+max_corner)-p;
  317. for(int i = 0;i<BFN.rows();i++)
  318. {
  319. if(p2c.dot(BFN.row(i)) > 0)
  320. {
  321. PBF.row(pbfi++) = BF.row(i);
  322. }
  323. }
  324. PBF.conservativeResize(pbfi,PBF.cols());
  325. double w = numeric_limits<double>::infinity();
  326. igl::winding_number_3(
  327. BV.data(),
  328. BV.rows(),
  329. PBF.data(),
  330. PBF.rows(),
  331. p.data(),
  332. 1,
  333. &w);
  334. return w;
  335. }
  336. //// Explicit instanciation
  337. //template class igl::WindingNumberAABB<Eigen::Vector3d >;
  338. #endif