WindingNumberAABB.h 9.7 KB

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