AABB.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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. #include "AABB.h"
  9. #include "EPS.h"
  10. #include "barycenter.h"
  11. #include "barycentric_coordinates.h"
  12. #include "colon.h"
  13. #include "doublearea.h"
  14. #include "matlab_format.h"
  15. #include "point_simplex_squared_distance.h"
  16. #include "project_to_line_segment.h"
  17. #include "sort.h"
  18. #include "volume.h"
  19. #include "ray_box_intersect.h"
  20. #include "ray_mesh_intersect.h"
  21. #include <iostream>
  22. #include <iomanip>
  23. #include <limits>
  24. #include <list>
  25. #include <queue>
  26. #include <stack>
  27. template <typename DerivedV, int DIM>
  28. template <typename Derivedbb_mins, typename Derivedbb_maxs>
  29. IGL_INLINE void igl::AABB<DerivedV,DIM>::init(
  30. const Eigen::PlainObjectBase<DerivedV> & V,
  31. const Eigen::MatrixXi & Ele,
  32. const Eigen::PlainObjectBase<Derivedbb_mins> & bb_mins,
  33. const Eigen::PlainObjectBase<Derivedbb_maxs> & bb_maxs,
  34. const Eigen::VectorXi & elements,
  35. const int i)
  36. {
  37. using namespace std;
  38. using namespace Eigen;
  39. deinit();
  40. if(bb_mins.size() > 0)
  41. {
  42. assert(bb_mins.rows() == bb_maxs.rows() && "Serial tree arrays must match");
  43. assert(bb_mins.cols() == V.cols() && "Serial tree array dim must match V");
  44. assert(bb_mins.cols() == bb_maxs.cols() && "Serial tree arrays must match");
  45. assert(bb_mins.rows() == elements.rows() &&
  46. "Serial tree arrays must match");
  47. // construct from serialization
  48. m_box.extend(bb_mins.row(i).transpose());
  49. m_box.extend(bb_maxs.row(i).transpose());
  50. m_primitive = elements(i);
  51. // Not leaf then recurse
  52. if(m_primitive == -1)
  53. {
  54. m_left = new AABB();
  55. m_left->init( V,Ele,bb_mins,bb_maxs,elements,2*i+1);
  56. m_right = new AABB();
  57. m_right->init( V,Ele,bb_mins,bb_maxs,elements,2*i+2);
  58. //m_depth = std::max( m_left->m_depth, m_right->m_depth)+1;
  59. }
  60. }else
  61. {
  62. VectorXi allI = colon<int>(0,Ele.rows()-1);
  63. MatrixXDIMS BC;
  64. if(Ele.cols() == 1)
  65. {
  66. // points
  67. BC = V;
  68. }else
  69. {
  70. // Simplices
  71. barycenter(V,Ele,BC);
  72. }
  73. MatrixXi SI(BC.rows(),BC.cols());
  74. {
  75. MatrixXDIMS _;
  76. MatrixXi IS;
  77. igl::sort(BC,1,true,_,IS);
  78. // Need SI(i) to tell which place i would be sorted into
  79. const int dim = IS.cols();
  80. for(int i = 0;i<IS.rows();i++)
  81. {
  82. for(int d = 0;d<dim;d++)
  83. {
  84. SI(IS(i,d),d) = i;
  85. }
  86. }
  87. }
  88. init(V,Ele,SI,allI);
  89. }
  90. }
  91. template <typename DerivedV, int DIM>
  92. void igl::AABB<DerivedV,DIM>::init(
  93. const Eigen::PlainObjectBase<DerivedV> & V,
  94. const Eigen::MatrixXi & Ele)
  95. {
  96. using namespace Eigen;
  97. // deinit will be immediately called...
  98. return init(V,Ele,MatrixXDIMS(),MatrixXDIMS(),VectorXi(),0);
  99. }
  100. template <typename DerivedV, int DIM>
  101. IGL_INLINE void igl::AABB<DerivedV,DIM>::init(
  102. const Eigen::PlainObjectBase<DerivedV> & V,
  103. const Eigen::MatrixXi & Ele,
  104. const Eigen::MatrixXi & SI,
  105. const Eigen::VectorXi & I)
  106. {
  107. using namespace Eigen;
  108. using namespace std;
  109. deinit();
  110. if(V.size() == 0 || Ele.size() == 0 || I.size() == 0)
  111. {
  112. return;
  113. }
  114. assert(DIM == V.cols() && "V.cols() should matched declared dimension");
  115. //const Scalar inf = numeric_limits<Scalar>::infinity();
  116. m_box = AlignedBox<Scalar,DIM>();
  117. // Compute bounding box
  118. for(int i = 0;i<I.rows();i++)
  119. {
  120. for(int c = 0;c<Ele.cols();c++)
  121. {
  122. m_box.extend(V.row(Ele(I(i),c)).transpose());
  123. m_box.extend(V.row(Ele(I(i),c)).transpose());
  124. }
  125. }
  126. switch(I.size())
  127. {
  128. case 0:
  129. {
  130. assert(false);
  131. }
  132. case 1:
  133. {
  134. m_primitive = I(0);
  135. break;
  136. }
  137. default:
  138. {
  139. // Compute longest direction
  140. int max_d = -1;
  141. m_box.diagonal().maxCoeff(&max_d);
  142. // Can't use median on BC directly because many may have same value,
  143. // but can use median on sorted BC indices
  144. VectorXi SIdI(I.rows());
  145. for(int i = 0;i<I.rows();i++)
  146. {
  147. SIdI(i) = SI(I(i),max_d);
  148. }
  149. // Since later I use <= I think I don't need to worry about odd/even
  150. // Pass by copy to avoid changing input
  151. const auto median = [](VectorXi A)->Scalar
  152. {
  153. size_t n = A.size()/2;
  154. nth_element(A.data(),A.data()+n,A.data()+A.size());
  155. if(A.rows() % 2 == 1)
  156. {
  157. return A(n);
  158. }else
  159. {
  160. nth_element(A.data(),A.data()+n-1,A.data()+A.size());
  161. return 0.5*(A(n)+A(n-1));
  162. }
  163. };
  164. const Scalar med = median(SIdI);
  165. VectorXi LI((I.rows()+1)/2),RI(I.rows()/2);
  166. assert(LI.rows()+RI.rows() == I.rows());
  167. // Distribute left and right
  168. {
  169. int li = 0;
  170. int ri = 0;
  171. for(int i = 0;i<I.rows();i++)
  172. {
  173. if(SIdI(i)<=med)
  174. {
  175. LI(li++) = I(i);
  176. }else
  177. {
  178. RI(ri++) = I(i);
  179. }
  180. }
  181. }
  182. //m_depth = 0;
  183. if(LI.rows()>0)
  184. {
  185. m_left = new AABB();
  186. m_left->init(V,Ele,SI,LI);
  187. //m_depth = std::max(m_depth, m_left->m_depth+1);
  188. }
  189. if(RI.rows()>0)
  190. {
  191. m_right = new AABB();
  192. m_right->init(V,Ele,SI,RI);
  193. //m_depth = std::max(m_depth, m_right->m_depth+1);
  194. }
  195. }
  196. }
  197. }
  198. template <typename DerivedV, int DIM>
  199. IGL_INLINE bool igl::AABB<DerivedV,DIM>::is_leaf() const
  200. {
  201. return m_primitive != -1;
  202. }
  203. template <typename DerivedV, int DIM>
  204. template <typename Derivedq>
  205. IGL_INLINE std::vector<int> igl::AABB<DerivedV,DIM>::find(
  206. const Eigen::PlainObjectBase<DerivedV> & V,
  207. const Eigen::MatrixXi & Ele,
  208. const Eigen::PlainObjectBase<Derivedq> & q,
  209. const bool first) const
  210. {
  211. using namespace std;
  212. using namespace Eigen;
  213. assert(q.size() == DIM &&
  214. "Query dimension should match aabb dimension");
  215. assert(Ele.cols() == V.cols()+1 &&
  216. "AABB::find only makes sense for (d+1)-simplices");
  217. const Scalar epsilon = igl::EPS<Scalar>();
  218. // Check if outside bounding box
  219. bool inside = m_box.contains(q.transpose());
  220. if(!inside)
  221. {
  222. return std::vector<int>();
  223. }
  224. assert(m_primitive==-1 || (m_left == NULL && m_right == NULL));
  225. if(is_leaf())
  226. {
  227. // Initialize to some value > -epsilon
  228. Scalar a1=0,a2=0,a3=0,a4=0;
  229. switch(DIM)
  230. {
  231. case 3:
  232. {
  233. // Barycentric coordinates
  234. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  235. const RowVector3S V1 = V.row(Ele(m_primitive,0));
  236. const RowVector3S V2 = V.row(Ele(m_primitive,1));
  237. const RowVector3S V3 = V.row(Ele(m_primitive,2));
  238. const RowVector3S V4 = V.row(Ele(m_primitive,3));
  239. a1 = volume_single(V2,V4,V3,(RowVector3S)q);
  240. a2 = volume_single(V1,V3,V4,(RowVector3S)q);
  241. a3 = volume_single(V1,V4,V2,(RowVector3S)q);
  242. a4 = volume_single(V1,V2,V3,(RowVector3S)q);
  243. break;
  244. }
  245. case 2:
  246. {
  247. // Barycentric coordinates
  248. typedef Eigen::Matrix<Scalar,2,1> Vector2S;
  249. const Vector2S V1 = V.row(Ele(m_primitive,0));
  250. const Vector2S V2 = V.row(Ele(m_primitive,1));
  251. const Vector2S V3 = V.row(Ele(m_primitive,2));
  252. // Hack for now to keep templates simple. If becomes bottleneck
  253. // consider using std::enable_if_t
  254. const Vector2S q2 = q.head(2);
  255. a1 = doublearea_single(V1,V2,q2);
  256. a2 = doublearea_single(V2,V3,q2);
  257. a3 = doublearea_single(V3,V1,q2);
  258. break;
  259. }
  260. default:assert(false);
  261. }
  262. // Normalization is important for correcting sign
  263. Scalar sum = a1+a2+a3+a4;
  264. a1 /= sum;
  265. a2 /= sum;
  266. a3 /= sum;
  267. a4 /= sum;
  268. if(
  269. a1>=-epsilon &&
  270. a2>=-epsilon &&
  271. a3>=-epsilon &&
  272. a4>=-epsilon)
  273. {
  274. return std::vector<int>(1,m_primitive);
  275. }else
  276. {
  277. return std::vector<int>();
  278. }
  279. }
  280. std::vector<int> left = m_left->find(V,Ele,q,first);
  281. if(first && !left.empty())
  282. {
  283. return left;
  284. }
  285. std::vector<int> right = m_right->find(V,Ele,q,first);
  286. if(first)
  287. {
  288. return right;
  289. }
  290. left.insert(left.end(),right.begin(),right.end());
  291. return left;
  292. }
  293. template <typename DerivedV, int DIM>
  294. IGL_INLINE int igl::AABB<DerivedV,DIM>::subtree_size() const
  295. {
  296. // 1 for self
  297. int n = 1;
  298. int n_left = 0,n_right = 0;
  299. if(m_left != NULL)
  300. {
  301. n_left = m_left->subtree_size();
  302. }
  303. if(m_right != NULL)
  304. {
  305. n_right = m_right->subtree_size();
  306. }
  307. n += 2*std::max(n_left,n_right);
  308. return n;
  309. }
  310. template <typename DerivedV, int DIM>
  311. template <typename Derivedbb_mins, typename Derivedbb_maxs>
  312. IGL_INLINE void igl::AABB<DerivedV,DIM>::serialize(
  313. Eigen::PlainObjectBase<Derivedbb_mins> & bb_mins,
  314. Eigen::PlainObjectBase<Derivedbb_maxs> & bb_maxs,
  315. Eigen::VectorXi & elements,
  316. const int i) const
  317. {
  318. using namespace std;
  319. using namespace Eigen;
  320. // Calling for root then resize output
  321. if(i==0)
  322. {
  323. const int m = subtree_size();
  324. //cout<<"m: "<<m<<endl;
  325. bb_mins.resize(m,DIM);
  326. bb_maxs.resize(m,DIM);
  327. elements.resize(m,1);
  328. }
  329. //cout<<i<<" ";
  330. bb_mins.row(i) = m_box.min();
  331. bb_maxs.row(i) = m_box.max();
  332. elements(i) = m_primitive;
  333. if(m_left != NULL)
  334. {
  335. m_left->serialize(bb_mins,bb_maxs,elements,2*i+1);
  336. }
  337. if(m_right != NULL)
  338. {
  339. m_right->serialize(bb_mins,bb_maxs,elements,2*i+2);
  340. }
  341. }
  342. template <typename DerivedV, int DIM>
  343. IGL_INLINE typename igl::AABB<DerivedV,DIM>::Scalar
  344. igl::AABB<DerivedV,DIM>::squared_distance(
  345. const Eigen::PlainObjectBase<DerivedV> & V,
  346. const Eigen::MatrixXi & Ele,
  347. const RowVectorDIMS & p,
  348. int & i,
  349. RowVectorDIMS & c) const
  350. {
  351. return squared_distance(V,Ele,p,std::numeric_limits<Scalar>::infinity(),i,c);
  352. }
  353. template <typename DerivedV, int DIM>
  354. IGL_INLINE typename igl::AABB<DerivedV,DIM>::Scalar
  355. igl::AABB<DerivedV,DIM>::squared_distance(
  356. const Eigen::PlainObjectBase<DerivedV> & V,
  357. const Eigen::MatrixXi & Ele,
  358. const RowVectorDIMS & p,
  359. Scalar min_sqr_d,
  360. int & i,
  361. RowVectorDIMS & c) const
  362. {
  363. using namespace Eigen;
  364. using namespace std;
  365. Scalar sqr_d = min_sqr_d;
  366. //assert(DIM == 3 && "Code has only been tested for DIM == 3");
  367. assert((Ele.cols() == 3 || Ele.cols() == 2 || Ele.cols() == 1)
  368. && "Code has only been tested for simplex sizes 3,2,1");
  369. assert(m_primitive==-1 || (m_left == NULL && m_right == NULL));
  370. if(is_leaf())
  371. {
  372. leaf_squared_distance(V,Ele,p,sqr_d,i,c);
  373. }else
  374. {
  375. bool looked_left = false;
  376. bool looked_right = false;
  377. const auto & look_left = [&]()
  378. {
  379. int i_left;
  380. RowVectorDIMS c_left = c;
  381. Scalar sqr_d_left = m_left->squared_distance(V,Ele,p,sqr_d,i_left,c_left);
  382. set_min(p,sqr_d_left,i_left,c_left,sqr_d,i,c);
  383. looked_left = true;
  384. };
  385. const auto & look_right = [&]()
  386. {
  387. int i_right;
  388. RowVectorDIMS c_right = c;
  389. Scalar sqr_d_right = m_right->squared_distance(V,Ele,p,sqr_d,i_right,c_right);
  390. set_min(p,sqr_d_right,i_right,c_right,sqr_d,i,c);
  391. looked_right = true;
  392. };
  393. // must look left or right if in box
  394. if(m_left->m_box.contains(p.transpose()))
  395. {
  396. look_left();
  397. }
  398. if(m_right->m_box.contains(p.transpose()))
  399. {
  400. look_right();
  401. }
  402. // if haven't looked left and could be less than current min, then look
  403. Scalar left_min_sqr_d = m_left->m_box.squaredExteriorDistance(p.transpose());
  404. Scalar right_min_sqr_d = m_right->m_box.squaredExteriorDistance(p.transpose());
  405. if(left_min_sqr_d < right_min_sqr_d)
  406. {
  407. if(!looked_left && left_min_sqr_d<sqr_d)
  408. {
  409. look_left();
  410. }
  411. if( !looked_right && right_min_sqr_d<sqr_d)
  412. {
  413. look_right();
  414. }
  415. }else
  416. {
  417. if( !looked_right && right_min_sqr_d<sqr_d)
  418. {
  419. look_right();
  420. }
  421. if(!looked_left && left_min_sqr_d<sqr_d)
  422. {
  423. look_left();
  424. }
  425. }
  426. }
  427. return sqr_d;
  428. }
  429. template <typename DerivedV, int DIM>
  430. template <
  431. typename DerivedP,
  432. typename DerivedsqrD,
  433. typename DerivedI,
  434. typename DerivedC>
  435. IGL_INLINE void igl::AABB<DerivedV,DIM>::squared_distance(
  436. const Eigen::PlainObjectBase<DerivedV> & V,
  437. const Eigen::MatrixXi & Ele,
  438. const Eigen::PlainObjectBase<DerivedP> & P,
  439. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  440. Eigen::PlainObjectBase<DerivedI> & I,
  441. Eigen::PlainObjectBase<DerivedC> & C) const
  442. {
  443. assert(P.cols() == V.cols() && "cols in P should match dim of cols in V");
  444. sqrD.resize(P.rows(),1);
  445. I.resize(P.rows(),1);
  446. C.resize(P.rows(),P.cols());
  447. for(int p = 0;p<P.rows();p++)
  448. {
  449. RowVectorDIMS Pp = P.row(p), c;
  450. int Ip;
  451. sqrD(p) = squared_distance(V,Ele,Pp,Ip,c);
  452. I(p) = Ip;
  453. C.row(p).head(DIM) = c;
  454. }
  455. }
  456. template <typename DerivedV, int DIM>
  457. template <
  458. typename Derivedother_V,
  459. typename DerivedsqrD,
  460. typename DerivedI,
  461. typename DerivedC>
  462. IGL_INLINE void igl::AABB<DerivedV,DIM>::squared_distance(
  463. const Eigen::PlainObjectBase<DerivedV> & V,
  464. const Eigen::MatrixXi & Ele,
  465. const AABB<Derivedother_V,DIM> & other,
  466. const Eigen::PlainObjectBase<Derivedother_V> & other_V,
  467. const Eigen::MatrixXi & other_Ele,
  468. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  469. Eigen::PlainObjectBase<DerivedI> & I,
  470. Eigen::PlainObjectBase<DerivedC> & C) const
  471. {
  472. assert(other_Ele.cols() == 1 &&
  473. "Only implemented for other as list of points");
  474. assert(other_V.cols() == V.cols() && "other must match this dimension");
  475. sqrD.setConstant(other_Ele.rows(),1,std::numeric_limits<double>::infinity());
  476. I.resize(other_Ele.rows(),1);
  477. C.resize(other_Ele.rows(),other_V.cols());
  478. // All points in other_V currently think they need to check against root of
  479. // this. The point of using another AABB is to quickly prune chunks of
  480. // other_V so that most points just check some subtree of this.
  481. // This holds a conservative estimate of max(sqr_D) where sqr_D is the
  482. // current best minimum squared distance for all points in this subtree
  483. double min_sqr_d = std::numeric_limits<double>::infinity();
  484. squared_distance_helper(
  485. V,Ele,&other,other_V,other_Ele,min_sqr_d,sqrD,I,C);
  486. }
  487. template <typename DerivedV, int DIM>
  488. template <
  489. typename Derivedother_V,
  490. typename DerivedsqrD,
  491. typename DerivedI,
  492. typename DerivedC>
  493. IGL_INLINE typename igl::AABB<DerivedV,DIM>::Scalar igl::AABB<DerivedV,DIM>::squared_distance_helper(
  494. const Eigen::PlainObjectBase<DerivedV> & V,
  495. const Eigen::MatrixXi & Ele,
  496. const AABB<Derivedother_V,DIM> * other,
  497. const Eigen::PlainObjectBase<Derivedother_V> & other_V,
  498. const Eigen::MatrixXi & other_Ele,
  499. const Scalar /*min_sqr_d*/,
  500. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  501. Eigen::PlainObjectBase<DerivedI> & I,
  502. Eigen::PlainObjectBase<DerivedC> & C) const
  503. {
  504. using namespace std;
  505. using namespace Eigen;
  506. // This implementation is a bit disappointing. There's no major speed up. Any
  507. // performance gains seem to come from accidental cache coherency and
  508. // diminish for larger "other" (the opposite of what was intended).
  509. // Base case
  510. if(other->is_leaf() && this->is_leaf())
  511. {
  512. Scalar sqr_d = sqrD(other->m_primitive);
  513. int i = I(other->m_primitive);
  514. RowVectorDIMS c = C.row( other->m_primitive);
  515. RowVectorDIMS p = other_V.row(other->m_primitive);
  516. leaf_squared_distance(V,Ele,p,sqr_d,i,c);
  517. sqrD( other->m_primitive) = sqr_d;
  518. I( other->m_primitive) = i;
  519. C.row(other->m_primitive) = c;
  520. //cout<<"leaf: "<<sqr_d<<endl;
  521. //other->m_max_sqr_d = sqr_d;
  522. return sqr_d;
  523. }
  524. if(other->is_leaf())
  525. {
  526. Scalar sqr_d = sqrD(other->m_primitive);
  527. int i = I(other->m_primitive);
  528. RowVectorDIMS c = C.row( other->m_primitive);
  529. RowVectorDIMS p = other_V.row(other->m_primitive);
  530. sqr_d = squared_distance(V,Ele,p,sqr_d,i,c);
  531. sqrD( other->m_primitive) = sqr_d;
  532. I( other->m_primitive) = i;
  533. C.row(other->m_primitive) = c;
  534. //other->m_max_sqr_d = sqr_d;
  535. return sqr_d;
  536. }
  537. //// Exact minimum squared distance between arbitary primitives inside this and
  538. //// othre's bounding boxes
  539. //const auto & min_squared_distance = [&](
  540. // const AABB<DerivedV,DIM> * A,
  541. // const AABB<Derivedother_V,DIM> * B)->Scalar
  542. //{
  543. // return A->m_box.squaredExteriorDistance(B->m_box);
  544. //};
  545. if(this->is_leaf())
  546. {
  547. //if(min_squared_distance(this,other) < other->m_max_sqr_d)
  548. if(true)
  549. {
  550. this->squared_distance_helper(
  551. V,Ele,other->m_left,other_V,other_Ele,0,sqrD,I,C);
  552. this->squared_distance_helper(
  553. V,Ele,other->m_right,other_V,other_Ele,0,sqrD,I,C);
  554. }else
  555. {
  556. // This is never reached...
  557. }
  558. //// we know other is not a leaf
  559. //other->m_max_sqr_d = std::max(other->m_left->m_max_sqr_d,other->m_right->m_max_sqr_d);
  560. return 0;
  561. }
  562. // FORCE DOWN TO OTHER LEAF EVAL
  563. //if(min_squared_distance(this,other) < other->m_max_sqr_d)
  564. if(true)
  565. {
  566. if(true)
  567. {
  568. this->squared_distance_helper(
  569. V,Ele,other->m_left,other_V,other_Ele,0,sqrD,I,C);
  570. this->squared_distance_helper(
  571. V,Ele,other->m_right,other_V,other_Ele,0,sqrD,I,C);
  572. }else // this direction never seems to be faster
  573. {
  574. this->m_left->squared_distance_helper(
  575. V,Ele,other,other_V,other_Ele,0,sqrD,I,C);
  576. this->m_right->squared_distance_helper(
  577. V,Ele,other,other_V,other_Ele,0,sqrD,I,C);
  578. }
  579. }else
  580. {
  581. // this is never reached ... :-(
  582. }
  583. //// we know other is not a leaf
  584. //other->m_max_sqr_d = std::max(other->m_left->m_max_sqr_d,other->m_right->m_max_sqr_d);
  585. return 0;
  586. #if 0 // False
  587. // _Very_ conservative approximation of maximum squared distance between
  588. // primitives inside this and other's bounding boxes
  589. const auto & max_squared_distance = [](
  590. const AABB<DerivedV,DIM> * A,
  591. const AABB<Derivedother_V,DIM> * B)->Scalar
  592. {
  593. AlignedBox<Scalar,DIM> combo = A->m_box;
  594. combo.extend(B->m_box);
  595. return combo.diagonal().squaredNorm();
  596. };
  597. //// other base-case
  598. //if(other->is_leaf())
  599. //{
  600. // double sqr_d = sqrD(other->m_primitive);
  601. // int i = I(other->m_primitive);
  602. // RowVectorDIMS c = C.row(m_primitive);
  603. // RowVectorDIMS p = other_V.row(m_primitive);
  604. // leaf_squared_distance(V,Ele,p,sqr_d,i,c);
  605. // sqrD(other->m_primitive) = sqr_d;
  606. // I(other->m_primitive) = i;
  607. // C.row(m_primitive) = c;
  608. // return;
  609. //}
  610. std::vector<const AABB<DerivedV,DIM> * > this_list;
  611. if(this->is_leaf())
  612. {
  613. this_list.push_back(this);
  614. }else
  615. {
  616. assert(this->m_left);
  617. this_list.push_back(this->m_left);
  618. assert(this->m_right);
  619. this_list.push_back(this->m_right);
  620. }
  621. std::vector<AABB<Derivedother_V,DIM> *> other_list;
  622. if(other->is_leaf())
  623. {
  624. other_list.push_back(other);
  625. }else
  626. {
  627. assert(other->m_left);
  628. other_list.push_back(other->m_left);
  629. assert(other->m_right);
  630. other_list.push_back(other->m_right);
  631. }
  632. //const std::function<Scalar(
  633. // const AABB<Derivedother_V,DIM> * other)
  634. // > max_sqr_d = [&sqrD,&max_sqr_d](const AABB<Derivedother_V,DIM> * other)->Scalar
  635. // {
  636. // if(other->is_leaf())
  637. // {
  638. // return sqrD(other->m_primitive);
  639. // }else
  640. // {
  641. // return std::max(max_sqr_d(other->m_left),max_sqr_d(other->m_right));
  642. // }
  643. // };
  644. //// Potentially recurse on all pairs, if minimum distance is less than running
  645. //// bound
  646. //Eigen::Matrix<Scalar,Eigen::Dynamic,1> other_max_sqr_d =
  647. // Eigen::Matrix<Scalar,Eigen::Dynamic,1>::Constant(other_list.size(),1,min_sqr_d);
  648. for(size_t child = 0;child<other_list.size();child++)
  649. {
  650. auto other_tree = other_list[child];
  651. Eigen::Matrix<Scalar,Eigen::Dynamic,1> this_max_sqr_d(this_list.size(),1);
  652. for(size_t t = 0;t<this_list.size();t++)
  653. {
  654. const auto this_tree = this_list[t];
  655. this_max_sqr_d(t) = max_squared_distance(this_tree,other_tree);
  656. }
  657. if(this_list.size() ==2 &&
  658. ( this_max_sqr_d(0) > this_max_sqr_d(1))
  659. )
  660. {
  661. std::swap(this_list[0],this_list[1]);
  662. //std::swap(this_max_sqr_d(0),this_max_sqr_d(1));
  663. }
  664. const Scalar sqr_d = this_max_sqr_d.minCoeff();
  665. for(size_t t = 0;t<this_list.size();t++)
  666. {
  667. const auto this_tree = this_list[t];
  668. //const auto mm = max_sqr_d(other_tree);
  669. //const Scalar mc = other_max_sqr_d(child);
  670. //assert(mc == mm);
  671. // Only look left/right in this_list if can possible decrease somebody's
  672. // distance in this_tree.
  673. const Scalar min_this_other = min_squared_distance(this_tree,other_tree);
  674. if(
  675. min_this_other < sqr_d &&
  676. min_this_other < other_tree->m_max_sqr_d)
  677. {
  678. //cout<<"before: "<<other_max_sqr_d(child)<<endl;
  679. //other_max_sqr_d(child) = std::min(
  680. // other_max_sqr_d(child),
  681. // this_tree->squared_distance_helper(
  682. // V,Ele,other_tree,other_V,other_Ele,other_max_sqr_d(child),sqrD,I,C));
  683. //cout<<"after: "<<other_max_sqr_d(child)<<endl;
  684. this_tree->squared_distance_helper(
  685. V,Ele,other_tree,other_V,other_Ele,0,sqrD,I,C);
  686. }
  687. }
  688. }
  689. //const Scalar ret = other_max_sqr_d.maxCoeff();
  690. //const auto mm = max_sqr_d(other);
  691. //assert(mm == ret);
  692. //cout<<"non-leaf: "<<ret<<endl;
  693. //return ret;
  694. if(!other->is_leaf())
  695. {
  696. other->m_max_sqr_d = std::max(other->m_left->m_max_sqr_d,other->m_right->m_max_sqr_d);
  697. }
  698. return 0;
  699. #endif
  700. }
  701. template <typename DerivedV, int DIM>
  702. IGL_INLINE void igl::AABB<DerivedV,DIM>::leaf_squared_distance(
  703. const Eigen::PlainObjectBase<DerivedV> & V,
  704. const Eigen::MatrixXi & Ele,
  705. const RowVectorDIMS & p,
  706. Scalar & sqr_d,
  707. int & i,
  708. RowVectorDIMS & c) const
  709. {
  710. using namespace Eigen;
  711. using namespace std;
  712. RowVectorDIMS c_candidate;
  713. Scalar sqr_d_candidate;
  714. igl::point_simplex_squared_distance<DIM>(
  715. p,V,Ele,m_primitive,sqr_d_candidate,c_candidate);
  716. set_min(p,sqr_d_candidate,m_primitive,c_candidate,sqr_d,i,c);
  717. }
  718. template <typename DerivedV, int DIM>
  719. IGL_INLINE void igl::AABB<DerivedV,DIM>::set_min(
  720. const RowVectorDIMS &
  721. #ifndef NDEBUG
  722. p
  723. #endif
  724. ,
  725. const Scalar sqr_d_candidate,
  726. const int i_candidate,
  727. const RowVectorDIMS & c_candidate,
  728. Scalar & sqr_d,
  729. int & i,
  730. RowVectorDIMS & c) const
  731. {
  732. #ifndef NDEBUG
  733. //std::cout<<matlab_format(c_candidate,"c_candidate")<<std::endl;
  734. const Scalar pc_norm = (p-c_candidate).squaredNorm();
  735. const Scalar diff = fabs(sqr_d_candidate - pc_norm);
  736. assert(diff<=1e-10 && "distance should match norm of difference");
  737. #endif
  738. if(sqr_d_candidate < sqr_d)
  739. {
  740. i = i_candidate;
  741. c = c_candidate;
  742. sqr_d = sqr_d_candidate;
  743. }
  744. }
  745. template <typename DerivedV, int DIM>
  746. IGL_INLINE bool
  747. igl::AABB<DerivedV,DIM>::intersect_ray(
  748. const Eigen::PlainObjectBase<DerivedV> & V,
  749. const Eigen::MatrixXi & Ele,
  750. const RowVectorDIMS & origin,
  751. const RowVectorDIMS & dir,
  752. std::vector<igl::Hit> & hits) const
  753. {
  754. hits.clear();
  755. const Scalar t0 = 0;
  756. const Scalar t1 = std::numeric_limits<Scalar>::infinity();
  757. {
  758. Scalar _1,_2;
  759. if(!ray_box_intersect(origin,dir,m_box,t0,t1,_1,_2))
  760. {
  761. return false;
  762. }
  763. }
  764. if(this->is_leaf())
  765. {
  766. // Actually process elements
  767. assert((Ele.size() == 0 || Ele.cols() == 3) && "Elements should be triangles");
  768. // Cheesecake way of hitting element
  769. return ray_mesh_intersect(origin,dir,V,Ele.row(m_primitive),hits);
  770. }
  771. std::vector<igl::Hit> left_hits;
  772. std::vector<igl::Hit> right_hits;
  773. const bool left_ret = m_left->intersect_ray(V,Ele,origin,dir,left_hits);
  774. const bool right_ret = m_right->intersect_ray(V,Ele,origin,dir,right_hits);
  775. hits.insert(hits.end(),left_hits.begin(),left_hits.end());
  776. hits.insert(hits.end(),right_hits.begin(),right_hits.end());
  777. return left_ret || right_ret;
  778. }
  779. template <typename DerivedV, int DIM>
  780. IGL_INLINE bool
  781. igl::AABB<DerivedV,DIM>::intersect_ray(
  782. const Eigen::PlainObjectBase<DerivedV> & V,
  783. const Eigen::MatrixXi & Ele,
  784. const RowVectorDIMS & origin,
  785. const RowVectorDIMS & dir,
  786. igl::Hit & hit) const
  787. {
  788. #if false
  789. // BFS
  790. std::queue<const AABB *> Q;
  791. // Or DFS
  792. //std::stack<const AABB *> Q;
  793. Q.push(this);
  794. bool any_hit = false;
  795. hit.t = std::numeric_limits<Scalar>::infinity();
  796. while(!Q.empty())
  797. {
  798. const AABB * tree = Q.front();
  799. //const AABB * tree = Q.top();
  800. Q.pop();
  801. {
  802. Scalar _1,_2;
  803. if(!ray_box_intersect(
  804. origin,dir,tree->m_box,Scalar(0),Scalar(hit.t),_1,_2))
  805. {
  806. continue;
  807. }
  808. }
  809. if(tree->is_leaf())
  810. {
  811. // Actually process elements
  812. assert((Ele.size() == 0 || Ele.cols() == 3) && "Elements should be triangles");
  813. igl::Hit leaf_hit;
  814. if(
  815. ray_mesh_intersect(origin,dir,V,Ele.row(tree->m_primitive),leaf_hit)&&
  816. leaf_hit.t < hit.t)
  817. {
  818. hit = leaf_hit;
  819. }
  820. continue;
  821. }
  822. // Add children to queue
  823. Q.push(tree->m_left);
  824. Q.push(tree->m_right);
  825. }
  826. return any_hit;
  827. #else
  828. // DFS
  829. return intersect_ray(
  830. V,Ele,origin,dir,std::numeric_limits<Scalar>::infinity(),hit);
  831. #endif
  832. }
  833. template <typename DerivedV, int DIM>
  834. IGL_INLINE bool
  835. igl::AABB<DerivedV,DIM>::intersect_ray(
  836. const Eigen::PlainObjectBase<DerivedV> & V,
  837. const Eigen::MatrixXi & Ele,
  838. const RowVectorDIMS & origin,
  839. const RowVectorDIMS & dir,
  840. const Scalar _min_t,
  841. igl::Hit & hit) const
  842. {
  843. //// Naive, slow
  844. //std::vector<igl::Hit> hits;
  845. //intersect_ray(V,Ele,origin,dir,hits);
  846. //if(hits.size() > 0)
  847. //{
  848. // hit = hits.front();
  849. // return true;
  850. //}else
  851. //{
  852. // return false;
  853. //}
  854. Scalar min_t = _min_t;
  855. const Scalar t0 = 0;
  856. {
  857. Scalar _1,_2;
  858. if(!ray_box_intersect(origin,dir,m_box,t0,min_t,_1,_2))
  859. {
  860. return false;
  861. }
  862. }
  863. if(this->is_leaf())
  864. {
  865. // Actually process elements
  866. assert((Ele.size() == 0 || Ele.cols() == 3) && "Elements should be triangles");
  867. // Cheesecake way of hitting element
  868. return ray_mesh_intersect(origin,dir,V,Ele.row(m_primitive),hit);
  869. }
  870. // Doesn't seem like smartly choosing left before/after right makes a
  871. // differnce
  872. igl::Hit left_hit;
  873. igl::Hit right_hit;
  874. bool left_ret = m_left->intersect_ray(V,Ele,origin,dir,min_t,left_hit);
  875. if(left_ret && left_hit.t<min_t)
  876. {
  877. // It's scary that this line doesn't seem to matter....
  878. min_t = left_hit.t;
  879. hit = left_hit;
  880. left_ret = true;
  881. }else
  882. {
  883. left_ret = false;
  884. }
  885. bool right_ret = m_right->intersect_ray(V,Ele,origin,dir,min_t,right_hit);
  886. if(right_ret && right_hit.t<min_t)
  887. {
  888. min_t = right_hit.t;
  889. hit = right_hit;
  890. right_ret = true;
  891. }else
  892. {
  893. right_ret = false;
  894. }
  895. return left_ret || right_ret;
  896. }
  897. #ifdef IGL_STATIC_LIBRARY
  898. // Explicit template specialization
  899. // generated by autoexplicit.sh
  900. template void igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 3>::init(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&);
  901. template void igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 2>::init(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&);
  902. template void igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 3>::squared_distance<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&) const;
  903. template void igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 2>::squared_distance<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&) const;
  904. template double igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 3>::squared_distance(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::Matrix<double, 1, 3, 1, 1, 3> const&, int&, Eigen::Matrix<double, 1, 3, 1, 1, 3>&) const;
  905. template double igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 2>::squared_distance(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::Matrix<double, 1, 2, 1, 1, 2> const&, int&, Eigen::Matrix<double, 1, 2, 1, 1, 2>&) const;
  906. template void igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 2>::squared_distance<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&) const;
  907. template void igl::AABB<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 3>::squared_distance<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&) const;
  908. #endif