InElementAABB.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #ifndef IGL_IN_ELEMENT_AABB_H
  2. #define IGL_IN_ELEMENT_AABB_H
  3. #include <Eigen/Core>
  4. #include <memory>
  5. #include <vector>
  6. namespace igl
  7. {
  8. class InElementAABB
  9. {
  10. public:
  11. std::shared_ptr<InElementAABB> m_left, m_right;
  12. Eigen::RowVectorXd m_bb_min,m_bb_max;
  13. // -1 non-leaf
  14. int m_element;
  15. InElementAABB():
  16. m_left(NULL), m_right(NULL),
  17. m_bb_min(), m_bb_max(), m_element(-1)
  18. {}
  19. // Build an Axis-Aligned Bounding Box tree for a given mesh and given
  20. // serialization of a previous AABB tree.
  21. //
  22. // Inputs:
  23. // V #V by dim list of mesh vertex positions.
  24. // Ele #Ele by dim+1 list of mesh indices into #V.
  25. // bb_mins max_tree by dim list of bounding box min corner positions
  26. // bb_maxs max_tree by dim list of bounding box max corner positions
  27. // elements max_tree list of element or (not leaf id) indices into Ele
  28. // i recursive call index {0}
  29. inline void init(
  30. const Eigen::MatrixXd & V,
  31. const Eigen::MatrixXi & Ele,
  32. const Eigen::MatrixXd & bb_mins,
  33. const Eigen::MatrixXd & bb_maxs,
  34. const Eigen::VectorXi & elements,
  35. const int i = 0);
  36. // Wrapper for root with empty serialization
  37. inline void init(
  38. const Eigen::MatrixXd & V,
  39. const Eigen::MatrixXi & Ele);
  40. // Build an Axis-Aligned Bounding Box tree for a given mesh.
  41. //
  42. // Inputs:
  43. // V #V by dim list of mesh vertex positions.
  44. // Ele #Ele by dim+1 list of mesh indices into #V.
  45. // SI #Ele by dim list revealing for each coordinate where Ele's
  46. // barycenters would be sorted: SI(e,d) = i --> the dth coordinate of
  47. // the barycenter of the eth element would be placed at position i in a
  48. // sorted list.
  49. // I #I list of indices into Ele of elements to include (for recursive
  50. // calls)
  51. //
  52. inline void init(
  53. const Eigen::MatrixXd & V,
  54. const Eigen::MatrixXi & Ele,
  55. const Eigen::MatrixXi & SI,
  56. const Eigen::VectorXi & I);
  57. // Find the indices of elements containing given point.
  58. //
  59. // Inputs:
  60. // V #V by dim list of mesh vertex positions. **Should be same as used to
  61. // construct mesh.**
  62. // Ele #Ele by dim+1 list of mesh indices into #V. **Should be same as used to
  63. // construct mesh.**
  64. // q dim row-vector query position
  65. // first whether to only return first element containing q
  66. // Returns:
  67. // list of indices of elements containing q
  68. inline std::vector<int> find(
  69. const Eigen::MatrixXd & V,
  70. const Eigen::MatrixXi & Ele,
  71. const Eigen::RowVectorXd & q,
  72. const bool first=false) const;
  73. // If number of elements m then total tree size should be 2*h where h is
  74. // the deepest depth 2^ceil(log(#Ele*2-1))
  75. inline int subtree_size();
  76. // Serialize this class into 3 arrays (so we can pass it pack to matlab)
  77. //
  78. // Outputs:
  79. // bb_mins max_tree by dim list of bounding box min corner positions
  80. // bb_maxs max_tree by dim list of bounding box max corner positions
  81. // elements max_tree list of element or (not leaf id) indices into Ele
  82. // i recursive call index into these arrays {0}
  83. inline void serialize(
  84. Eigen::MatrixXd & bb_mins,
  85. Eigen::MatrixXd & bb_maxs,
  86. Eigen::VectorXi & elements,
  87. const int i = 0);
  88. };
  89. }
  90. // Implementation
  91. #include <igl/volume.h>
  92. #include <igl/colon.h>
  93. #include <igl/doublearea.h>
  94. #include <igl/matlab_format.h>
  95. #include <igl/colon.h>
  96. #include <igl/sort.h>
  97. #include <igl/barycenter.h>
  98. #include <iostream>
  99. inline void igl::InElementAABB::init(
  100. const Eigen::MatrixXd & V,
  101. const Eigen::MatrixXi & Ele,
  102. const Eigen::MatrixXd & bb_mins,
  103. const Eigen::MatrixXd & bb_maxs,
  104. const Eigen::VectorXi & elements,
  105. const int i)
  106. {
  107. using namespace std;
  108. using namespace Eigen;
  109. if(bb_mins.size() > 0)
  110. {
  111. assert(bb_mins.rows() == bb_maxs.rows() && "Serial tree arrays must match");
  112. assert(bb_mins.cols() == V.cols() && "Serial tree array dim must match V");
  113. assert(bb_mins.cols() == bb_maxs.cols() && "Serial tree arrays must match");
  114. assert(bb_mins.rows() == elements.rows() &&
  115. "Serial tree arrays must match");
  116. // construct from serialization
  117. m_bb_min = bb_mins.row(i);
  118. m_bb_max = bb_maxs.row(i);
  119. m_element = elements(i);
  120. // Not leaf then recurse
  121. if(m_element == -1)
  122. {
  123. m_left = make_shared<InElementAABB>();
  124. m_left->init( V,Ele,bb_mins,bb_maxs,elements,2*i+1);
  125. m_right = make_shared<InElementAABB>();
  126. m_right->init( V,Ele,bb_mins,bb_maxs,elements,2*i+2);
  127. }
  128. }else
  129. {
  130. VectorXi allI = colon<int>(0,Ele.rows()-1);
  131. MatrixXd BC;
  132. barycenter(V,Ele,BC);
  133. MatrixXi SI(BC.rows(),BC.cols());
  134. {
  135. MatrixXd _;
  136. MatrixXi IS;
  137. igl::sort(BC,1,true,_,IS);
  138. // Need SI(i) to tell which place i would be sorted into
  139. const int dim = IS.cols();
  140. for(int i = 0;i<IS.rows();i++)
  141. {
  142. for(int d = 0;d<dim;d++)
  143. {
  144. SI(IS(i,d),d) = i;
  145. }
  146. }
  147. }
  148. init(V,Ele,SI,allI);
  149. }
  150. }
  151. inline void igl::InElementAABB::init(
  152. const Eigen::MatrixXd & V,
  153. const Eigen::MatrixXi & Ele)
  154. {
  155. using namespace Eigen;
  156. return init(V,Ele,MatrixXd(),MatrixXd(),VectorXi(),0);
  157. }
  158. inline void igl::InElementAABB::init(
  159. const Eigen::MatrixXd & V,
  160. const Eigen::MatrixXi & Ele,
  161. const Eigen::MatrixXi & SI,
  162. const Eigen::VectorXi & I)
  163. {
  164. using namespace Eigen;
  165. using namespace std;
  166. const int dim = V.cols();
  167. const double inf = numeric_limits<double>::infinity();
  168. m_bb_min.setConstant(1,dim, inf);
  169. m_bb_max.setConstant(1,dim,-inf);
  170. // Compute bounding box
  171. for(int i = 0;i<I.rows();i++)
  172. {
  173. for(int c = 0;c<Ele.cols();c++)
  174. {
  175. for(int d = 0;d<dim;d++)
  176. {
  177. m_bb_min(d) = min(m_bb_min(d),V(Ele(I(i),c),d));
  178. m_bb_max(d) = max(m_bb_max(d),V(Ele(I(i),c),d));
  179. }
  180. }
  181. }
  182. switch(I.size())
  183. {
  184. case 0:
  185. {
  186. assert(false);
  187. }
  188. case 1:
  189. {
  190. m_element = I(0);
  191. break;
  192. }
  193. default:
  194. {
  195. // Compute longest direction
  196. int max_d = -1;
  197. double max_len = -inf;
  198. for(int d = 0;d<dim;d++)
  199. {
  200. const auto diff = (m_bb_max[d] - m_bb_min[d]);
  201. if( diff > max_len )
  202. {
  203. max_len = diff;
  204. max_d = d;
  205. }
  206. }
  207. // Can't use median on BC directly because many may have same value,
  208. // but can use median on sorted BC indices
  209. VectorXi SIdI(I.rows());
  210. for(int i = 0;i<I.rows();i++)
  211. {
  212. SIdI(i) = SI(I(i),max_d);
  213. }
  214. // Since later I use <= I think I don't need to worry about odd/even
  215. // Pass by copy to avoid changing input
  216. const auto median = [](VectorXi A)->double
  217. {
  218. size_t n = A.size()/2;
  219. nth_element(A.data(),A.data()+n,A.data()+A.size());
  220. if(A.rows() % 2 == 1)
  221. {
  222. return A(n);
  223. }else
  224. {
  225. nth_element(A.data(),A.data()+n-1,A.data()+A.size());
  226. return 0.5*(A(n)+A(n-1));
  227. }
  228. };
  229. const double med = median(SIdI);
  230. VectorXi LI((I.rows()+1)/2),RI(I.rows()/2);
  231. assert(LI.rows()+RI.rows() == I.rows());
  232. // Distribute left and right
  233. {
  234. int li = 0;
  235. int ri = 0;
  236. for(int i = 0;i<I.rows();i++)
  237. {
  238. if(SIdI(i)<=med)
  239. {
  240. LI(li++) = I(i);
  241. }else
  242. {
  243. RI(ri++) = I(i);
  244. }
  245. }
  246. }
  247. if(LI.rows()>0)
  248. {
  249. m_left = make_shared<InElementAABB>();
  250. m_left->init(V,Ele,SI,LI);
  251. }
  252. if(RI.rows()>0)
  253. {
  254. m_right = make_shared<InElementAABB>();
  255. m_right->init(V,Ele,SI,RI);
  256. }
  257. }
  258. }
  259. }
  260. inline std::vector<int> igl::InElementAABB::find(
  261. const Eigen::MatrixXd & V,
  262. const Eigen::MatrixXi & Ele,
  263. const Eigen::RowVectorXd & q,
  264. const bool first) const
  265. {
  266. using namespace std;
  267. using namespace Eigen;
  268. bool inside = true;
  269. const int dim = m_bb_max.size();
  270. assert(q.size() == m_bb_max.size());
  271. const double epsilon = 1e-14;
  272. // Check if outside bounding box
  273. for(int d = 0;d<q.size()&&inside;d++)
  274. {
  275. inside &= (q(d)-m_bb_min(d))>=epsilon;
  276. inside &= (m_bb_max(d)-q(d))>=epsilon;
  277. }
  278. cout<<"searching..."<<endl;
  279. if(!inside)
  280. {
  281. cout<<"not in bb"<<endl;
  282. return std::vector<int>();
  283. }
  284. if(m_element != -1)
  285. {
  286. // Initialize to some value > -epsilon
  287. double a1=1,a2=1,a3=1,a4=1;
  288. switch(dim)
  289. {
  290. case 3:
  291. {
  292. // Barycentric coordinates
  293. const RowVector3d V1 = V.row(Ele(m_element,0));
  294. const RowVector3d V2 = V.row(Ele(m_element,1));
  295. const RowVector3d V3 = V.row(Ele(m_element,2));
  296. const RowVector3d V4 = V.row(Ele(m_element,3));
  297. a1 = volume_single(V2,V4,V3,(RowVector3d)q);
  298. a2 = volume_single(V1,V3,V4,(RowVector3d)q);
  299. a3 = volume_single(V1,V4,V2,(RowVector3d)q);
  300. a4 = volume_single(V1,V2,V3,(RowVector3d)q);
  301. break;
  302. }
  303. case 2:
  304. {
  305. // Barycentric coordinates
  306. const Vector2d V1 = V.row(Ele(m_element,0));
  307. const Vector2d V2 = V.row(Ele(m_element,1));
  308. const Vector2d V3 = V.row(Ele(m_element,2));
  309. double a0 = doublearea_single(V1,V2,V3);
  310. a1 = doublearea_single(V1,V2,(Vector2d)q);
  311. a2 = doublearea_single(V2,V3,(Vector2d)q);
  312. a3 = doublearea_single(V3,V1,(Vector2d)q);
  313. cout<<
  314. a0<<" "<<
  315. a1<<" "<<
  316. a2<<" "<<
  317. a3<<" "<<
  318. endl;
  319. break;
  320. }
  321. default:assert(false);
  322. }
  323. if(
  324. a1>=-epsilon &&
  325. a2>=-epsilon &&
  326. a3>=-epsilon &&
  327. a4>=-epsilon)
  328. {
  329. return std::vector<int>(1,m_element);
  330. }else
  331. {
  332. return std::vector<int>();
  333. }
  334. }
  335. std::vector<int> left = m_left->find(V,Ele,q,first);
  336. if(first && !left.empty())
  337. {
  338. return left;
  339. }
  340. std::vector<int> right = m_right->find(V,Ele,q,first);
  341. if(first)
  342. {
  343. return right;
  344. }
  345. left.insert(left.end(),right.begin(),right.end());
  346. return left;
  347. }
  348. inline int igl::InElementAABB::subtree_size()
  349. {
  350. // 1 for self
  351. int n = 1;
  352. int n_left = 0,n_right = 0;
  353. if(m_left != NULL)
  354. {
  355. n_left = m_left->subtree_size();
  356. }
  357. if(m_right != NULL)
  358. {
  359. n_right = m_right->subtree_size();
  360. }
  361. n += 2*std::max(n_left,n_right);
  362. return n;
  363. }
  364. inline void igl::InElementAABB::serialize(
  365. Eigen::MatrixXd & bb_mins,
  366. Eigen::MatrixXd & bb_maxs,
  367. Eigen::VectorXi & elements,
  368. const int i)
  369. {
  370. using namespace std;
  371. // Calling for root then resize output
  372. if(i==0)
  373. {
  374. const int m = subtree_size();
  375. //cout<<"m: "<<m<<endl;
  376. bb_mins.resize(m,m_bb_min.size());
  377. bb_maxs.resize(m,m_bb_max.size());
  378. elements.resize(m,1);
  379. }
  380. //cout<<i<<" ";
  381. bb_mins.row(i) = m_bb_min;
  382. bb_maxs.row(i) = m_bb_max;
  383. elements(i) = m_element;
  384. if(m_left != NULL)
  385. {
  386. m_left->serialize(bb_mins,bb_maxs,elements,2*i+1);
  387. }
  388. if(m_right != NULL)
  389. {
  390. m_right->serialize(bb_mins,bb_maxs,elements,2*i+2);
  391. }
  392. }
  393. #endif