AABB.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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. deinit();
  318. if(bb_mins.size() > 0)
  319. {
  320. assert(bb_mins.rows() == bb_maxs.rows() && "Serial tree arrays must match");
  321. assert(bb_mins.cols() == V.cols() && "Serial tree array dim must match V");
  322. assert(bb_mins.cols() == bb_maxs.cols() && "Serial tree arrays must match");
  323. assert(bb_mins.rows() == elements.rows() &&
  324. "Serial tree arrays must match");
  325. // construct from serialization
  326. m_box.extend(bb_mins.row(i).transpose());
  327. m_box.extend(bb_maxs.row(i).transpose());
  328. m_primitive = elements(i);
  329. // Not leaf then recurse
  330. if(m_primitive == -1)
  331. {
  332. m_left = new AABB();
  333. m_left->init( V,Ele,bb_mins,bb_maxs,elements,2*i+1);
  334. m_right = new AABB();
  335. m_right->init( V,Ele,bb_mins,bb_maxs,elements,2*i+2);
  336. //m_depth = std::max( m_left->m_depth, m_right->m_depth)+1;
  337. }
  338. }else
  339. {
  340. VectorXi allI = colon<int>(0,Ele.rows()-1);
  341. MatrixXDIMS BC;
  342. if(Ele.cols() == 1)
  343. {
  344. // points
  345. BC = V;
  346. }else
  347. {
  348. // Simplices
  349. barycenter(V,Ele,BC);
  350. }
  351. MatrixXi SI(BC.rows(),BC.cols());
  352. {
  353. MatrixXDIMS _;
  354. MatrixXi IS;
  355. igl::sort(BC,1,true,_,IS);
  356. // Need SI(i) to tell which place i would be sorted into
  357. const int dim = IS.cols();
  358. for(int i = 0;i<IS.rows();i++)
  359. {
  360. for(int d = 0;d<dim;d++)
  361. {
  362. SI(IS(i,d),d) = i;
  363. }
  364. }
  365. }
  366. init(V,Ele,SI,allI);
  367. }
  368. }
  369. template <typename DerivedV, int DIM>
  370. inline void igl::AABB<DerivedV,DIM>::init(
  371. const Eigen::PlainObjectBase<DerivedV> & V,
  372. const Eigen::MatrixXi & Ele)
  373. {
  374. using namespace Eigen;
  375. // deinit will be immediately called...
  376. return init(V,Ele,MatrixXDIMS(),MatrixXDIMS(),VectorXi(),0);
  377. }
  378. template <typename DerivedV, int DIM>
  379. inline void igl::AABB<DerivedV,DIM>::init(
  380. const Eigen::PlainObjectBase<DerivedV> & V,
  381. const Eigen::MatrixXi & Ele,
  382. const Eigen::MatrixXi & SI,
  383. const Eigen::VectorXi & I)
  384. {
  385. using namespace Eigen;
  386. using namespace std;
  387. deinit();
  388. if(V.size() == 0 || Ele.size() == 0 || I.size() == 0)
  389. {
  390. return;
  391. }
  392. assert(DIM == V.cols() && "V.cols() should matched declared dimension");
  393. //const Scalar inf = numeric_limits<Scalar>::infinity();
  394. m_box = AlignedBox<Scalar,DIM>();
  395. // Compute bounding box
  396. for(int i = 0;i<I.rows();i++)
  397. {
  398. for(int c = 0;c<Ele.cols();c++)
  399. {
  400. m_box.extend(V.row(Ele(I(i),c)).transpose());
  401. m_box.extend(V.row(Ele(I(i),c)).transpose());
  402. }
  403. }
  404. switch(I.size())
  405. {
  406. case 0:
  407. {
  408. assert(false);
  409. }
  410. case 1:
  411. {
  412. m_primitive = I(0);
  413. break;
  414. }
  415. default:
  416. {
  417. // Compute longest direction
  418. int max_d = -1;
  419. m_box.diagonal().maxCoeff(&max_d);
  420. // Can't use median on BC directly because many may have same value,
  421. // but can use median on sorted BC indices
  422. VectorXi SIdI(I.rows());
  423. for(int i = 0;i<I.rows();i++)
  424. {
  425. SIdI(i) = SI(I(i),max_d);
  426. }
  427. // Since later I use <= I think I don't need to worry about odd/even
  428. // Pass by copy to avoid changing input
  429. const auto median = [](VectorXi A)->Scalar
  430. {
  431. size_t n = A.size()/2;
  432. nth_element(A.data(),A.data()+n,A.data()+A.size());
  433. if(A.rows() % 2 == 1)
  434. {
  435. return A(n);
  436. }else
  437. {
  438. nth_element(A.data(),A.data()+n-1,A.data()+A.size());
  439. return 0.5*(A(n)+A(n-1));
  440. }
  441. };
  442. const Scalar med = median(SIdI);
  443. VectorXi LI((I.rows()+1)/2),RI(I.rows()/2);
  444. assert(LI.rows()+RI.rows() == I.rows());
  445. // Distribute left and right
  446. {
  447. int li = 0;
  448. int ri = 0;
  449. for(int i = 0;i<I.rows();i++)
  450. {
  451. if(SIdI(i)<=med)
  452. {
  453. LI(li++) = I(i);
  454. }else
  455. {
  456. RI(ri++) = I(i);
  457. }
  458. }
  459. }
  460. //m_depth = 0;
  461. if(LI.rows()>0)
  462. {
  463. m_left = new AABB();
  464. m_left->init(V,Ele,SI,LI);
  465. //m_depth = std::max(m_depth, m_left->m_depth+1);
  466. }
  467. if(RI.rows()>0)
  468. {
  469. m_right = new AABB();
  470. m_right->init(V,Ele,SI,RI);
  471. //m_depth = std::max(m_depth, m_right->m_depth+1);
  472. }
  473. }
  474. }
  475. }
  476. template <typename DerivedV, int DIM>
  477. inline bool igl::AABB<DerivedV,DIM>::is_leaf() const
  478. {
  479. return m_primitive != -1;
  480. }
  481. template <typename DerivedV, int DIM>
  482. template <typename Derivedq>
  483. inline std::vector<int> igl::AABB<DerivedV,DIM>::find(
  484. const Eigen::PlainObjectBase<DerivedV> & V,
  485. const Eigen::MatrixXi & Ele,
  486. const Eigen::PlainObjectBase<Derivedq> & q,
  487. const bool first) const
  488. {
  489. using namespace std;
  490. using namespace Eigen;
  491. assert(q.size() == DIM &&
  492. "Query dimension should match aabb dimension");
  493. assert(Ele.cols() == V.cols()+1 &&
  494. "AABB::find only makes sense for (d+1)-simplices");
  495. const Scalar epsilon = igl::EPS<Scalar>();
  496. // Check if outside bounding box
  497. bool inside = m_box.contains(q.transpose());
  498. if(!inside)
  499. {
  500. return std::vector<int>();
  501. }
  502. assert(m_primitive==-1 || (m_left == NULL && m_right == NULL));
  503. if(is_leaf())
  504. {
  505. // Initialize to some value > -epsilon
  506. Scalar a1=0,a2=0,a3=0,a4=0;
  507. switch(DIM)
  508. {
  509. case 3:
  510. {
  511. // Barycentric coordinates
  512. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  513. const RowVector3S V1 = V.row(Ele(m_primitive,0));
  514. const RowVector3S V2 = V.row(Ele(m_primitive,1));
  515. const RowVector3S V3 = V.row(Ele(m_primitive,2));
  516. const RowVector3S V4 = V.row(Ele(m_primitive,3));
  517. a1 = volume_single(V2,V4,V3,(RowVector3S)q);
  518. a2 = volume_single(V1,V3,V4,(RowVector3S)q);
  519. a3 = volume_single(V1,V4,V2,(RowVector3S)q);
  520. a4 = volume_single(V1,V2,V3,(RowVector3S)q);
  521. break;
  522. }
  523. case 2:
  524. {
  525. // Barycentric coordinates
  526. typedef Eigen::Matrix<Scalar,2,1> Vector2S;
  527. const Vector2S V1 = V.row(Ele(m_primitive,0));
  528. const Vector2S V2 = V.row(Ele(m_primitive,1));
  529. const Vector2S V3 = V.row(Ele(m_primitive,2));
  530. // Hack for now to keep templates simple. If becomes bottleneck
  531. // consider using std::enable_if_t
  532. const Vector2S q2 = q.head(2);
  533. a1 = doublearea_single(V1,V2,q2);
  534. a2 = doublearea_single(V2,V3,q2);
  535. a3 = doublearea_single(V3,V1,q2);
  536. break;
  537. }
  538. default:assert(false);
  539. }
  540. // Normalization is important for correcting sign
  541. Scalar sum = a1+a2+a3+a4;
  542. a1 /= sum;
  543. a2 /= sum;
  544. a3 /= sum;
  545. a4 /= sum;
  546. if(
  547. a1>=-epsilon &&
  548. a2>=-epsilon &&
  549. a3>=-epsilon &&
  550. a4>=-epsilon)
  551. {
  552. return std::vector<int>(1,m_primitive);
  553. }else
  554. {
  555. return std::vector<int>();
  556. }
  557. }
  558. std::vector<int> left = m_left->find(V,Ele,q,first);
  559. if(first && !left.empty())
  560. {
  561. return left;
  562. }
  563. std::vector<int> right = m_right->find(V,Ele,q,first);
  564. if(first)
  565. {
  566. return right;
  567. }
  568. left.insert(left.end(),right.begin(),right.end());
  569. return left;
  570. }
  571. template <typename DerivedV, int DIM>
  572. inline int igl::AABB<DerivedV,DIM>::subtree_size() const
  573. {
  574. // 1 for self
  575. int n = 1;
  576. int n_left = 0,n_right = 0;
  577. if(m_left != NULL)
  578. {
  579. n_left = m_left->subtree_size();
  580. }
  581. if(m_right != NULL)
  582. {
  583. n_right = m_right->subtree_size();
  584. }
  585. n += 2*std::max(n_left,n_right);
  586. return n;
  587. }
  588. template <typename DerivedV, int DIM>
  589. template <typename Derivedbb_mins, typename Derivedbb_maxs>
  590. inline void igl::AABB<DerivedV,DIM>::serialize(
  591. Eigen::PlainObjectBase<Derivedbb_mins> & bb_mins,
  592. Eigen::PlainObjectBase<Derivedbb_maxs> & bb_maxs,
  593. Eigen::VectorXi & elements,
  594. const int i) const
  595. {
  596. using namespace std;
  597. using namespace Eigen;
  598. // Calling for root then resize output
  599. if(i==0)
  600. {
  601. const int m = subtree_size();
  602. //cout<<"m: "<<m<<endl;
  603. bb_mins.resize(m,DIM);
  604. bb_maxs.resize(m,DIM);
  605. elements.resize(m,1);
  606. }
  607. //cout<<i<<" ";
  608. bb_mins.row(i) = m_box.min();
  609. bb_maxs.row(i) = m_box.max();
  610. elements(i) = m_primitive;
  611. if(m_left != NULL)
  612. {
  613. m_left->serialize(bb_mins,bb_maxs,elements,2*i+1);
  614. }
  615. if(m_right != NULL)
  616. {
  617. m_right->serialize(bb_mins,bb_maxs,elements,2*i+2);
  618. }
  619. }
  620. template <typename DerivedV, int DIM>
  621. inline typename igl::AABB<DerivedV,DIM>::Scalar
  622. igl::AABB<DerivedV,DIM>::squared_distance(
  623. const Eigen::PlainObjectBase<DerivedV> & V,
  624. const Eigen::MatrixXi & Ele,
  625. const RowVectorDIMS & p,
  626. int & i,
  627. RowVectorDIMS & c) const
  628. {
  629. return squared_distance(V,Ele,p,std::numeric_limits<Scalar>::infinity(),i,c);
  630. }
  631. template <typename DerivedV, int DIM>
  632. inline typename igl::AABB<DerivedV,DIM>::Scalar
  633. igl::AABB<DerivedV,DIM>::squared_distance(
  634. const Eigen::PlainObjectBase<DerivedV> & V,
  635. const Eigen::MatrixXi & Ele,
  636. const RowVectorDIMS & p,
  637. Scalar min_sqr_d,
  638. int & i,
  639. RowVectorDIMS & c) const
  640. {
  641. using namespace Eigen;
  642. using namespace std;
  643. Scalar sqr_d = min_sqr_d;
  644. //assert(DIM == 3 && "Code has only been tested for DIM == 3");
  645. assert((Ele.cols() == 3 || Ele.cols() == 2 || Ele.cols() == 1)
  646. && "Code has only been tested for simplex sizes 3,2,1");
  647. assert(m_primitive==-1 || (m_left == NULL && m_right == NULL));
  648. if(is_leaf())
  649. {
  650. leaf_squared_distance(V,Ele,p,sqr_d,i,c);
  651. }else
  652. {
  653. bool looked_left = false;
  654. bool looked_right = false;
  655. const auto & look_left = [&]()
  656. {
  657. int i_left;
  658. RowVectorDIMS c_left = c;
  659. Scalar sqr_d_left = m_left->squared_distance(V,Ele,p,sqr_d,i_left,c_left);
  660. set_min(p,sqr_d_left,i_left,c_left,sqr_d,i,c);
  661. looked_left = true;
  662. };
  663. const auto & look_right = [&]()
  664. {
  665. int i_right;
  666. RowVectorDIMS c_right = c;
  667. Scalar sqr_d_right = m_right->squared_distance(V,Ele,p,sqr_d,i_right,c_right);
  668. set_min(p,sqr_d_right,i_right,c_right,sqr_d,i,c);
  669. looked_right = true;
  670. };
  671. // must look left or right if in box
  672. if(m_left->m_box.contains(p.transpose()))
  673. {
  674. look_left();
  675. }
  676. if(m_right->m_box.contains(p.transpose()))
  677. {
  678. look_right();
  679. }
  680. // if haven't looked left and could be less than current min, then look
  681. Scalar left_min_sqr_d = m_left->m_box.squaredExteriorDistance(p.transpose());
  682. Scalar right_min_sqr_d = m_right->m_box.squaredExteriorDistance(p.transpose());
  683. if(left_min_sqr_d < right_min_sqr_d)
  684. {
  685. if(!looked_left && left_min_sqr_d<sqr_d)
  686. {
  687. look_left();
  688. }
  689. if( !looked_right && right_min_sqr_d<sqr_d)
  690. {
  691. look_right();
  692. }
  693. }else
  694. {
  695. if( !looked_right && right_min_sqr_d<sqr_d)
  696. {
  697. look_right();
  698. }
  699. if(!looked_left && left_min_sqr_d<sqr_d)
  700. {
  701. look_left();
  702. }
  703. }
  704. }
  705. return sqr_d;
  706. }
  707. template <typename DerivedV, int DIM>
  708. template <
  709. typename DerivedP,
  710. typename DerivedsqrD,
  711. typename DerivedI,
  712. typename DerivedC>
  713. inline void igl::AABB<DerivedV,DIM>::squared_distance(
  714. const Eigen::PlainObjectBase<DerivedV> & V,
  715. const Eigen::MatrixXi & Ele,
  716. const Eigen::PlainObjectBase<DerivedP> & P,
  717. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  718. Eigen::PlainObjectBase<DerivedI> & I,
  719. Eigen::PlainObjectBase<DerivedC> & C) const
  720. {
  721. assert(P.cols() == V.cols() && "cols in P should match dim of cols in V");
  722. sqrD.resize(P.rows(),1);
  723. I.resize(P.rows(),1);
  724. C.resize(P.rows(),P.cols());
  725. for(int p = 0;p<P.rows();p++)
  726. {
  727. RowVectorDIMS Pp = P.row(p), c;
  728. int Ip;
  729. sqrD(p) = squared_distance(V,Ele,Pp,Ip,c);
  730. I(p) = Ip;
  731. C.row(p).head(DIM) = c;
  732. }
  733. }
  734. template <typename DerivedV, int DIM>
  735. template <
  736. typename Derivedother_V,
  737. typename DerivedsqrD,
  738. typename DerivedI,
  739. typename DerivedC>
  740. inline void igl::AABB<DerivedV,DIM>::squared_distance(
  741. const Eigen::PlainObjectBase<DerivedV> & V,
  742. const Eigen::MatrixXi & Ele,
  743. const AABB<Derivedother_V,DIM> & other,
  744. const Eigen::PlainObjectBase<Derivedother_V> & other_V,
  745. const Eigen::MatrixXi & other_Ele,
  746. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  747. Eigen::PlainObjectBase<DerivedI> & I,
  748. Eigen::PlainObjectBase<DerivedC> & C) const
  749. {
  750. assert(other_Ele.cols() == 1 &&
  751. "Only implemented for other as list of points");
  752. assert(other_V.cols() == V.cols() && "other must match this dimension");
  753. sqrD.setConstant(other_Ele.rows(),1,std::numeric_limits<double>::infinity());
  754. I.resize(other_Ele.rows(),1);
  755. C.resize(other_Ele.rows(),other_V.cols());
  756. // All points in other_V currently think they need to check against root of
  757. // this. The point of using another AABB is to quickly prune chunks of
  758. // other_V so that most points just check some subtree of this.
  759. // This holds a conservative estimate of max(sqr_D) where sqr_D is the
  760. // current best minimum squared distance for all points in this subtree
  761. double min_sqr_d = std::numeric_limits<double>::infinity();
  762. squared_distance_helper(
  763. V,Ele,&other,other_V,other_Ele,min_sqr_d,sqrD,I,C);
  764. }
  765. template <typename DerivedV, int DIM>
  766. template <
  767. typename Derivedother_V,
  768. typename DerivedsqrD,
  769. typename DerivedI,
  770. typename DerivedC>
  771. inline typename igl::AABB<DerivedV,DIM>::Scalar igl::AABB<DerivedV,DIM>::squared_distance_helper(
  772. const Eigen::PlainObjectBase<DerivedV> & V,
  773. const Eigen::MatrixXi & Ele,
  774. const AABB<Derivedother_V,DIM> * other,
  775. const Eigen::PlainObjectBase<Derivedother_V> & other_V,
  776. const Eigen::MatrixXi & other_Ele,
  777. const Scalar /*min_sqr_d*/,
  778. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  779. Eigen::PlainObjectBase<DerivedI> & I,
  780. Eigen::PlainObjectBase<DerivedC> & C) const
  781. {
  782. using namespace std;
  783. using namespace Eigen;
  784. // This implementation is a bit disappointing. There's no major speed up. Any
  785. // performance gains seem to come from accidental cache coherency and
  786. // diminish for larger "other" (the opposite of what was intended).
  787. // Base case
  788. if(other->is_leaf() && this->is_leaf())
  789. {
  790. Scalar sqr_d = sqrD(other->m_primitive);
  791. int i = I(other->m_primitive);
  792. RowVectorDIMS c = C.row( other->m_primitive);
  793. RowVectorDIMS p = other_V.row(other->m_primitive);
  794. leaf_squared_distance(V,Ele,p,sqr_d,i,c);
  795. sqrD( other->m_primitive) = sqr_d;
  796. I( other->m_primitive) = i;
  797. C.row(other->m_primitive) = c;
  798. //cout<<"leaf: "<<sqr_d<<endl;
  799. //other->m_max_sqr_d = sqr_d;
  800. return sqr_d;
  801. }
  802. if(other->is_leaf())
  803. {
  804. Scalar sqr_d = sqrD(other->m_primitive);
  805. int i = I(other->m_primitive);
  806. RowVectorDIMS c = C.row( other->m_primitive);
  807. RowVectorDIMS p = other_V.row(other->m_primitive);
  808. sqr_d = squared_distance(V,Ele,p,sqr_d,i,c);
  809. sqrD( other->m_primitive) = sqr_d;
  810. I( other->m_primitive) = i;
  811. C.row(other->m_primitive) = c;
  812. //other->m_max_sqr_d = sqr_d;
  813. return sqr_d;
  814. }
  815. //// Exact minimum squared distance between arbitary primitives inside this and
  816. //// othre's bounding boxes
  817. //const auto & min_squared_distance = [&](
  818. // const AABB<DerivedV,DIM> * A,
  819. // const AABB<Derivedother_V,DIM> * B)->Scalar
  820. //{
  821. // return A->m_box.squaredExteriorDistance(B->m_box);
  822. //};
  823. if(this->is_leaf())
  824. {
  825. //if(min_squared_distance(this,other) < other->m_max_sqr_d)
  826. if(true)
  827. {
  828. this->squared_distance_helper(
  829. V,Ele,other->m_left,other_V,other_Ele,0,sqrD,I,C);
  830. this->squared_distance_helper(
  831. V,Ele,other->m_right,other_V,other_Ele,0,sqrD,I,C);
  832. }else
  833. {
  834. // This is never reached...
  835. }
  836. //// we know other is not a leaf
  837. //other->m_max_sqr_d = std::max(other->m_left->m_max_sqr_d,other->m_right->m_max_sqr_d);
  838. return 0;
  839. }
  840. // FORCE DOWN TO OTHER LEAF EVAL
  841. //if(min_squared_distance(this,other) < other->m_max_sqr_d)
  842. if(true)
  843. {
  844. if(true)
  845. {
  846. this->squared_distance_helper(
  847. V,Ele,other->m_left,other_V,other_Ele,0,sqrD,I,C);
  848. this->squared_distance_helper(
  849. V,Ele,other->m_right,other_V,other_Ele,0,sqrD,I,C);
  850. }else // this direction never seems to be faster
  851. {
  852. this->m_left->squared_distance_helper(
  853. V,Ele,other,other_V,other_Ele,0,sqrD,I,C);
  854. this->m_right->squared_distance_helper(
  855. V,Ele,other,other_V,other_Ele,0,sqrD,I,C);
  856. }
  857. }else
  858. {
  859. // this is never reached ... :-(
  860. }
  861. //// we know other is not a leaf
  862. //other->m_max_sqr_d = std::max(other->m_left->m_max_sqr_d,other->m_right->m_max_sqr_d);
  863. return 0;
  864. #if 0 // False
  865. // _Very_ conservative approximation of maximum squared distance between
  866. // primitives inside this and other's bounding boxes
  867. const auto & max_squared_distance = [](
  868. const AABB<DerivedV,DIM> * A,
  869. const AABB<Derivedother_V,DIM> * B)->Scalar
  870. {
  871. AlignedBox<Scalar,DIM> combo = A->m_box;
  872. combo.extend(B->m_box);
  873. return combo.diagonal().squaredNorm();
  874. };
  875. //// other base-case
  876. //if(other->is_leaf())
  877. //{
  878. // double sqr_d = sqrD(other->m_primitive);
  879. // int i = I(other->m_primitive);
  880. // RowVectorDIMS c = C.row(m_primitive);
  881. // RowVectorDIMS p = other_V.row(m_primitive);
  882. // leaf_squared_distance(V,Ele,p,sqr_d,i,c);
  883. // sqrD(other->m_primitive) = sqr_d;
  884. // I(other->m_primitive) = i;
  885. // C.row(m_primitive) = c;
  886. // return;
  887. //}
  888. std::vector<const AABB<DerivedV,DIM> * > this_list;
  889. if(this->is_leaf())
  890. {
  891. this_list.push_back(this);
  892. }else
  893. {
  894. assert(this->m_left);
  895. this_list.push_back(this->m_left);
  896. assert(this->m_right);
  897. this_list.push_back(this->m_right);
  898. }
  899. std::vector<AABB<Derivedother_V,DIM> *> other_list;
  900. if(other->is_leaf())
  901. {
  902. other_list.push_back(other);
  903. }else
  904. {
  905. assert(other->m_left);
  906. other_list.push_back(other->m_left);
  907. assert(other->m_right);
  908. other_list.push_back(other->m_right);
  909. }
  910. //const std::function<Scalar(
  911. // const AABB<Derivedother_V,DIM> * other)
  912. // > max_sqr_d = [&sqrD,&max_sqr_d](const AABB<Derivedother_V,DIM> * other)->Scalar
  913. // {
  914. // if(other->is_leaf())
  915. // {
  916. // return sqrD(other->m_primitive);
  917. // }else
  918. // {
  919. // return std::max(max_sqr_d(other->m_left),max_sqr_d(other->m_right));
  920. // }
  921. // };
  922. //// Potentially recurse on all pairs, if minimum distance is less than running
  923. //// bound
  924. //Eigen::Matrix<Scalar,Eigen::Dynamic,1> other_max_sqr_d =
  925. // Eigen::Matrix<Scalar,Eigen::Dynamic,1>::Constant(other_list.size(),1,min_sqr_d);
  926. for(size_t child = 0;child<other_list.size();child++)
  927. {
  928. auto other_tree = other_list[child];
  929. Eigen::Matrix<Scalar,Eigen::Dynamic,1> this_max_sqr_d(this_list.size(),1);
  930. for(size_t t = 0;t<this_list.size();t++)
  931. {
  932. const auto this_tree = this_list[t];
  933. this_max_sqr_d(t) = max_squared_distance(this_tree,other_tree);
  934. }
  935. if(this_list.size() ==2 &&
  936. ( this_max_sqr_d(0) > this_max_sqr_d(1))
  937. )
  938. {
  939. std::swap(this_list[0],this_list[1]);
  940. //std::swap(this_max_sqr_d(0),this_max_sqr_d(1));
  941. }
  942. const Scalar sqr_d = this_max_sqr_d.minCoeff();
  943. for(size_t t = 0;t<this_list.size();t++)
  944. {
  945. const auto this_tree = this_list[t];
  946. //const auto mm = max_sqr_d(other_tree);
  947. //const Scalar mc = other_max_sqr_d(child);
  948. //assert(mc == mm);
  949. // Only look left/right in this_list if can possible decrease somebody's
  950. // distance in this_tree.
  951. const Scalar min_this_other = min_squared_distance(this_tree,other_tree);
  952. if(
  953. min_this_other < sqr_d &&
  954. min_this_other < other_tree->m_max_sqr_d)
  955. {
  956. //cout<<"before: "<<other_max_sqr_d(child)<<endl;
  957. //other_max_sqr_d(child) = std::min(
  958. // other_max_sqr_d(child),
  959. // this_tree->squared_distance_helper(
  960. // V,Ele,other_tree,other_V,other_Ele,other_max_sqr_d(child),sqrD,I,C));
  961. //cout<<"after: "<<other_max_sqr_d(child)<<endl;
  962. this_tree->squared_distance_helper(
  963. V,Ele,other_tree,other_V,other_Ele,0,sqrD,I,C);
  964. }
  965. }
  966. }
  967. //const Scalar ret = other_max_sqr_d.maxCoeff();
  968. //const auto mm = max_sqr_d(other);
  969. //assert(mm == ret);
  970. //cout<<"non-leaf: "<<ret<<endl;
  971. //return ret;
  972. if(!other->is_leaf())
  973. {
  974. other->m_max_sqr_d = std::max(other->m_left->m_max_sqr_d,other->m_right->m_max_sqr_d);
  975. }
  976. return 0;
  977. #endif
  978. }
  979. template <typename DerivedV, int DIM>
  980. inline void igl::AABB<DerivedV,DIM>::leaf_squared_distance(
  981. const Eigen::PlainObjectBase<DerivedV> & V,
  982. const Eigen::MatrixXi & Ele,
  983. const RowVectorDIMS & p,
  984. Scalar & sqr_d,
  985. int & i,
  986. RowVectorDIMS & c) const
  987. {
  988. using namespace Eigen;
  989. using namespace std;
  990. RowVectorDIMS c_candidate;
  991. Scalar sqr_d_candidate;
  992. igl::point_simplex_squared_distance<DIM>(
  993. p,V,Ele,m_primitive,sqr_d_candidate,c_candidate);
  994. set_min(p,sqr_d_candidate,m_primitive,c_candidate,sqr_d,i,c);
  995. }
  996. template <typename DerivedV, int DIM>
  997. inline void igl::AABB<DerivedV,DIM>::set_min(
  998. const RowVectorDIMS &
  999. #ifndef NDEBUG
  1000. p
  1001. #endif
  1002. ,
  1003. const Scalar sqr_d_candidate,
  1004. const int i_candidate,
  1005. const RowVectorDIMS & c_candidate,
  1006. Scalar & sqr_d,
  1007. int & i,
  1008. RowVectorDIMS & c) const
  1009. {
  1010. #ifndef NDEBUG
  1011. //std::cout<<matlab_format(c_candidate,"c_candidate")<<std::endl;
  1012. const Scalar pc_norm = (p-c_candidate).squaredNorm();
  1013. const Scalar diff = fabs(sqr_d_candidate - pc_norm);
  1014. assert(diff<=1e-10 && "distance should match norm of difference");
  1015. #endif
  1016. if(sqr_d_candidate < sqr_d)
  1017. {
  1018. i = i_candidate;
  1019. c = c_candidate;
  1020. sqr_d = sqr_d_candidate;
  1021. }
  1022. }
  1023. template <typename DerivedV, int DIM>
  1024. inline bool
  1025. igl::AABB<DerivedV,DIM>::intersect_ray(
  1026. const Eigen::PlainObjectBase<DerivedV> & V,
  1027. const Eigen::MatrixXi & Ele,
  1028. const RowVectorDIMS & origin,
  1029. const RowVectorDIMS & dir,
  1030. std::vector<igl::Hit> & hits) const
  1031. {
  1032. hits.clear();
  1033. const Scalar t0 = 0;
  1034. const Scalar t1 = std::numeric_limits<Scalar>::infinity();
  1035. {
  1036. Scalar _1,_2;
  1037. if(!ray_box_intersect(origin,dir,m_box,t0,t1,_1,_2))
  1038. {
  1039. return false;
  1040. }
  1041. }
  1042. if(this->is_leaf())
  1043. {
  1044. // Actually process elements
  1045. assert((Ele.size() == 0 || Ele.cols() == 3) && "Elements should be triangles");
  1046. // Cheesecake way of hitting element
  1047. return ray_mesh_intersect(origin,dir,V,Ele.row(m_primitive),hits);
  1048. }
  1049. std::vector<igl::Hit> left_hits;
  1050. std::vector<igl::Hit> right_hits;
  1051. const bool left_ret = m_left->intersect_ray(V,Ele,origin,dir,left_hits);
  1052. const bool right_ret = m_right->intersect_ray(V,Ele,origin,dir,right_hits);
  1053. hits.insert(hits.end(),left_hits.begin(),left_hits.end());
  1054. hits.insert(hits.end(),right_hits.begin(),right_hits.end());
  1055. return left_ret || right_ret;
  1056. }
  1057. template <typename DerivedV, int DIM>
  1058. inline bool
  1059. igl::AABB<DerivedV,DIM>::intersect_ray(
  1060. const Eigen::PlainObjectBase<DerivedV> & V,
  1061. const Eigen::MatrixXi & Ele,
  1062. const RowVectorDIMS & origin,
  1063. const RowVectorDIMS & dir,
  1064. igl::Hit & hit) const
  1065. {
  1066. #if false
  1067. // BFS
  1068. std::queue<const AABB *> Q;
  1069. // Or DFS
  1070. //std::stack<const AABB *> Q;
  1071. Q.push(this);
  1072. bool any_hit = false;
  1073. hit.t = std::numeric_limits<Scalar>::infinity();
  1074. while(!Q.empty())
  1075. {
  1076. const AABB * tree = Q.front();
  1077. //const AABB * tree = Q.top();
  1078. Q.pop();
  1079. {
  1080. Scalar _1,_2;
  1081. if(!ray_box_intersect(
  1082. origin,dir,tree->m_box,Scalar(0),Scalar(hit.t),_1,_2))
  1083. {
  1084. continue;
  1085. }
  1086. }
  1087. if(tree->is_leaf())
  1088. {
  1089. // Actually process elements
  1090. assert((Ele.size() == 0 || Ele.cols() == 3) && "Elements should be triangles");
  1091. igl::Hit leaf_hit;
  1092. if(
  1093. ray_mesh_intersect(origin,dir,V,Ele.row(tree->m_primitive),leaf_hit)&&
  1094. leaf_hit.t < hit.t)
  1095. {
  1096. hit = leaf_hit;
  1097. }
  1098. continue;
  1099. }
  1100. // Add children to queue
  1101. Q.push(tree->m_left);
  1102. Q.push(tree->m_right);
  1103. }
  1104. return any_hit;
  1105. #else
  1106. // DFS
  1107. return intersect_ray(
  1108. V,Ele,origin,dir,std::numeric_limits<Scalar>::infinity(),hit);
  1109. #endif
  1110. }
  1111. template <typename DerivedV, int DIM>
  1112. inline bool
  1113. igl::AABB<DerivedV,DIM>::intersect_ray(
  1114. const Eigen::PlainObjectBase<DerivedV> & V,
  1115. const Eigen::MatrixXi & Ele,
  1116. const RowVectorDIMS & origin,
  1117. const RowVectorDIMS & dir,
  1118. const Scalar _min_t,
  1119. igl::Hit & hit) const
  1120. {
  1121. //// Naive, slow
  1122. //std::vector<igl::Hit> hits;
  1123. //intersect_ray(V,Ele,origin,dir,hits);
  1124. //if(hits.size() > 0)
  1125. //{
  1126. // hit = hits.front();
  1127. // return true;
  1128. //}else
  1129. //{
  1130. // return false;
  1131. //}
  1132. Scalar min_t = _min_t;
  1133. const Scalar t0 = 0;
  1134. {
  1135. Scalar _1,_2;
  1136. if(!ray_box_intersect(origin,dir,m_box,t0,min_t,_1,_2))
  1137. {
  1138. return false;
  1139. }
  1140. }
  1141. if(this->is_leaf())
  1142. {
  1143. // Actually process elements
  1144. assert((Ele.size() == 0 || Ele.cols() == 3) && "Elements should be triangles");
  1145. // Cheesecake way of hitting element
  1146. return ray_mesh_intersect(origin,dir,V,Ele.row(m_primitive),hit);
  1147. }
  1148. // Doesn't seem like smartly choosing left before/after right makes a
  1149. // differnce
  1150. igl::Hit left_hit;
  1151. igl::Hit right_hit;
  1152. bool left_ret = m_left->intersect_ray(V,Ele,origin,dir,min_t,left_hit);
  1153. if(left_ret && left_hit.t<min_t)
  1154. {
  1155. // It's scary that this line doesn't seem to matter....
  1156. min_t = left_hit.t;
  1157. hit = left_hit;
  1158. left_ret = true;
  1159. }else
  1160. {
  1161. left_ret = false;
  1162. }
  1163. bool right_ret = m_right->intersect_ray(V,Ele,origin,dir,min_t,right_hit);
  1164. if(right_ret && right_hit.t<min_t)
  1165. {
  1166. min_t = right_hit.t;
  1167. hit = right_hit;
  1168. right_ret = true;
  1169. }else
  1170. {
  1171. right_ret = false;
  1172. }
  1173. return left_ret || right_ret;
  1174. }
  1175. #endif