AABB.cpp 30 KB

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