AABB.cpp 47 KB

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