AABB.h 36 KB

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