AABB.h 36 KB

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