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