WindingNumberAABB.h 9.4 KB

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