AABB.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "igl_inline.h"
  12. #include <Eigen/Core>
  13. #include <Eigen/Geometry>
  14. #include <vector>
  15. namespace igl
  16. {
  17. // Implementation of semi-general purpose axis-aligned bounding box hierarchy.
  18. // The mesh (V,Ele) is stored and managed by the caller and each routine here
  19. // simply takes it as references (it better not change between calls).
  20. //
  21. // It's a little annoying that the Dimension is a template parameter and not
  22. // picked up at run time from V. This leads to duplicated code for 2d/3d (up to
  23. // dim).
  24. template <typename DerivedV, int DIM>
  25. class AABB
  26. {
  27. public:
  28. typedef typename DerivedV::Scalar Scalar;
  29. typedef Eigen::Matrix<Scalar,1,DIM> RowVectorDIMS;
  30. typedef Eigen::Matrix<Scalar,DIM,1> VectorDIMS;
  31. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,DIM> MatrixXDIMS;
  32. // Shared pointers are slower...
  33. AABB * m_left;
  34. AABB * m_right;
  35. Eigen::AlignedBox<Scalar,DIM> m_box;
  36. // -1 non-leaf
  37. int m_primitive;
  38. //Scalar m_low_sqr_d;
  39. //int m_depth;
  40. AABB():
  41. m_left(NULL), m_right(NULL),
  42. m_box(), m_primitive(-1)
  43. //m_low_sqr_d(std::numeric_limits<double>::infinity()),
  44. //m_depth(0)
  45. {}
  46. // http://stackoverflow.com/a/3279550/148668
  47. AABB(const AABB& other):
  48. m_left(other.m_left ? new AABB(*other.m_left) : NULL),
  49. m_right(other.m_right ? new AABB(*other.m_right) : NULL),
  50. m_box(other.m_box),
  51. m_primitive(other.m_primitive)
  52. //m_low_sqr_d(other.m_low_sqr_d),
  53. //m_depth(std::max(
  54. // m_left ? m_left->m_depth + 1 : 0,
  55. // m_right ? m_right->m_depth + 1 : 0))
  56. {
  57. }
  58. // copy-swap idiom
  59. friend void swap(AABB& first, AABB& second)
  60. {
  61. // Enable ADL
  62. using std::swap;
  63. swap(first.m_left,second.m_left);
  64. swap(first.m_right,second.m_right);
  65. swap(first.m_box,second.m_box);
  66. swap(first.m_primitive,second.m_primitive);
  67. //swap(first.m_low_sqr_d,second.m_low_sqr_d);
  68. //swap(first.m_depth,second.m_depth);
  69. }
  70. AABB& operator=(const AABB &other)
  71. {
  72. this->deinit();
  73. m_left = other.m_left ? new AABB(*other.m_left) : NULL;
  74. m_right = other.m_right ? new AABB(*other.m_right) : NULL;
  75. m_box = other.m_box;
  76. m_primitive = other.m_primitive;
  77. return *this;
  78. }
  79. AABB(AABB&& other):
  80. // initialize via default constructor
  81. AABB()
  82. {
  83. swap(*this,other);
  84. }
  85. // Seems like there should have been an elegant solution to this using
  86. // the copy-swap idiom above:
  87. IGL_INLINE void deinit()
  88. {
  89. m_primitive = -1;
  90. m_box = Eigen::AlignedBox<Scalar,DIM>();
  91. delete m_left;
  92. m_left = NULL;
  93. delete m_right;
  94. m_right = NULL;
  95. }
  96. ~AABB()
  97. {
  98. deinit();
  99. }
  100. // Build an Axis-Aligned Bounding Box tree for a given mesh and given
  101. // serialization of a previous AABB tree.
  102. //
  103. // Inputs:
  104. // V #V by dim list of mesh vertex positions.
  105. // Ele #Ele by dim+1 list of mesh indices into #V.
  106. // bb_mins max_tree by dim list of bounding box min corner positions
  107. // bb_maxs max_tree by dim list of bounding box max corner positions
  108. // elements max_tree list of element or (not leaf id) indices into Ele
  109. // i recursive call index {0}
  110. template <
  111. typename DerivedEle,
  112. typename Derivedbb_mins,
  113. typename Derivedbb_maxs,
  114. typename Derivedelements>
  115. IGL_INLINE void init(
  116. const Eigen::MatrixBase<DerivedV> & V,
  117. const Eigen::MatrixBase<DerivedEle> & Ele,
  118. const Eigen::MatrixBase<Derivedbb_mins> & bb_mins,
  119. const Eigen::MatrixBase<Derivedbb_maxs> & bb_maxs,
  120. const Eigen::MatrixBase<Derivedelements> & elements,
  121. const int i = 0);
  122. // Wrapper for root with empty serialization
  123. template <typename DerivedEle>
  124. IGL_INLINE void init(
  125. const Eigen::MatrixBase<DerivedV> & V,
  126. const Eigen::MatrixBase<DerivedEle> & Ele);
  127. // Build an Axis-Aligned Bounding Box tree for a given mesh.
  128. //
  129. // Inputs:
  130. // V #V by dim list of mesh vertex positions.
  131. // Ele #Ele by dim+1 list of mesh indices into #V.
  132. // SI #Ele by dim list revealing for each coordinate where Ele's
  133. // barycenters would be sorted: SI(e,d) = i --> the dth coordinate of
  134. // the barycenter of the eth element would be placed at position i in a
  135. // sorted list.
  136. // I #I list of indices into Ele of elements to include (for recursive
  137. // calls)
  138. //
  139. template <typename DerivedEle, typename DerivedSI, typename DerivedI>
  140. IGL_INLINE void init(
  141. const Eigen::MatrixBase<DerivedV> & V,
  142. const Eigen::MatrixBase<DerivedEle> & Ele,
  143. const Eigen::MatrixBase<DerivedSI> & SI,
  144. const Eigen::MatrixBase<DerivedI>& I);
  145. // Return whether at leaf node
  146. IGL_INLINE bool is_leaf() const;
  147. // Find the indices of elements containing given point: this makes sense
  148. // when Ele is a co-dimension 0 simplex (tets in 3D, triangles in 2D).
  149. //
  150. // Inputs:
  151. // V #V by dim list of mesh vertex positions. **Should be same as used to
  152. // construct mesh.**
  153. // Ele #Ele by dim+1 list of mesh indices into #V. **Should be same as used to
  154. // construct mesh.**
  155. // q dim row-vector query position
  156. // first whether to only return first element containing q
  157. // Returns:
  158. // list of indices of elements containing q
  159. template <typename DerivedEle, typename Derivedq>
  160. IGL_INLINE std::vector<int> find(
  161. const Eigen::MatrixBase<DerivedV> & V,
  162. const Eigen::MatrixBase<DerivedEle> & Ele,
  163. const Eigen::MatrixBase<Derivedq> & q,
  164. const bool first=false) const;
  165. // If number of elements m then total tree size should be 2*h where h is
  166. // the deepest depth 2^ceil(log(#Ele*2-1))
  167. IGL_INLINE int subtree_size() const;
  168. // Serialize this class into 3 arrays (so we can pass it pack to matlab)
  169. //
  170. // Outputs:
  171. // bb_mins max_tree by dim list of bounding box min corner positions
  172. // bb_maxs max_tree by dim list of bounding box max corner positions
  173. // elements max_tree list of element or (not leaf id) indices into Ele
  174. // i recursive call index into these arrays {0}
  175. template <
  176. typename Derivedbb_mins,
  177. typename Derivedbb_maxs,
  178. typename Derivedelements>
  179. IGL_INLINE void serialize(
  180. Eigen::PlainObjectBase<Derivedbb_mins> & bb_mins,
  181. Eigen::PlainObjectBase<Derivedbb_maxs> & bb_maxs,
  182. Eigen::PlainObjectBase<Derivedelements> & elements,
  183. const int i = 0) const;
  184. // Compute squared distance to a query point
  185. //
  186. // Inputs:
  187. // V #V by dim list of vertex positions
  188. // Ele #Ele by dim list of simplex indices
  189. // p dim-long query point
  190. // Outputs:
  191. // i facet index corresponding to smallest distances
  192. // c closest point
  193. // Returns squared distance
  194. //
  195. // Known bugs: currently assumes Elements are triangles regardless of
  196. // dimension.
  197. template <typename DerivedEle>
  198. IGL_INLINE Scalar squared_distance(
  199. const Eigen::MatrixBase<DerivedV> & V,
  200. const Eigen::MatrixBase<DerivedEle> & Ele,
  201. const RowVectorDIMS & p,
  202. int & i,
  203. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  204. //private:
  205. // Compute squared distance to a query point
  206. //
  207. // Inputs:
  208. // V #V by dim list of vertex positions
  209. // Ele #Ele by dim list of simplex indices
  210. // p dim-long query point
  211. // low_sqr_d lower bound on squared distance, specified maximum squared
  212. // distance
  213. // up_sqr_d current upper bounded on squared distance, current minimum
  214. // squared distance (only consider distances less than this), see
  215. // output.
  216. // Outputs:
  217. // up_sqr_d updated current minimum squared distance
  218. // i facet index corresponding to smallest distances
  219. // c closest point
  220. // Returns squared distance
  221. //
  222. // Known bugs: currently assumes Elements are triangles regardless of
  223. // dimension.
  224. template <typename DerivedEle>
  225. IGL_INLINE Scalar squared_distance(
  226. const Eigen::MatrixBase<DerivedV> & V,
  227. const Eigen::MatrixBase<DerivedEle> & Ele,
  228. const RowVectorDIMS & p,
  229. const Scalar low_sqr_d,
  230. const Scalar up_sqr_d,
  231. int & i,
  232. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  233. // Default low_sqr_d
  234. template <typename DerivedEle>
  235. IGL_INLINE Scalar squared_distance(
  236. const Eigen::MatrixBase<DerivedV> & V,
  237. const Eigen::MatrixBase<DerivedEle> & Ele,
  238. const RowVectorDIMS & p,
  239. const Scalar up_sqr_d,
  240. int & i,
  241. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  242. // All hits
  243. template <typename DerivedEle>
  244. IGL_INLINE bool intersect_ray(
  245. const Eigen::MatrixBase<DerivedV> & V,
  246. const Eigen::MatrixBase<DerivedEle> & Ele,
  247. const RowVectorDIMS & origin,
  248. const RowVectorDIMS & dir,
  249. std::vector<igl::Hit> & hits) const;
  250. // First hit
  251. template <typename DerivedEle>
  252. IGL_INLINE bool intersect_ray(
  253. const Eigen::MatrixBase<DerivedV> & V,
  254. const Eigen::MatrixBase<DerivedEle> & Ele,
  255. const RowVectorDIMS & origin,
  256. const RowVectorDIMS & dir,
  257. igl::Hit & hit) const;
  258. //private:
  259. template <typename DerivedEle>
  260. IGL_INLINE bool intersect_ray(
  261. const Eigen::MatrixBase<DerivedV> & V,
  262. const Eigen::MatrixBase<DerivedEle> & Ele,
  263. const RowVectorDIMS & origin,
  264. const RowVectorDIMS & dir,
  265. const Scalar min_t,
  266. igl::Hit & hit) const;
  267. public:
  268. // Compute the squared distance from all query points in P to the
  269. // _closest_ points on the primitives stored in the AABB hierarchy for
  270. // the mesh (V,Ele).
  271. //
  272. // Inputs:
  273. // V #V by dim list of vertex positions
  274. // Ele #Ele by dim list of simplex indices
  275. // P #P by dim list of query points
  276. // Outputs:
  277. // sqrD #P list of squared distances
  278. // I #P list of indices into Ele of closest primitives
  279. // C #P by dim list of closest points
  280. template <
  281. typename DerivedEle,
  282. typename DerivedP,
  283. typename DerivedsqrD,
  284. typename DerivedI,
  285. typename DerivedC>
  286. IGL_INLINE void squared_distance(
  287. const Eigen::MatrixBase<DerivedV> & V,
  288. const Eigen::MatrixBase<DerivedEle> & Ele,
  289. const Eigen::MatrixBase<DerivedP> & P,
  290. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  291. Eigen::PlainObjectBase<DerivedI> & I,
  292. Eigen::PlainObjectBase<DerivedC> & C) const;
  293. // Compute the squared distance from all query points in P already stored
  294. // in its own AABB hierarchy to the _closest_ points on the primitives
  295. // stored in the AABB hierarchy for the mesh (V,Ele).
  296. //
  297. // Inputs:
  298. // V #V by dim list of vertex positions
  299. // Ele #Ele by dim list of simplex indices
  300. // other AABB hierarchy of another set of primitives (must be points)
  301. // other_V #other_V by dim list of query points
  302. // other_Ele #other_Ele by ss list of simplex indices into other_V
  303. // (must be simple list of points: ss == 1)
  304. // Outputs:
  305. // sqrD #P list of squared distances
  306. // I #P list of indices into Ele of closest primitives
  307. // C #P by dim list of closest points
  308. template <
  309. typename DerivedEle,
  310. typename Derivedother_V,
  311. typename Derivedother_Ele,
  312. typename DerivedsqrD,
  313. typename DerivedI,
  314. typename DerivedC>
  315. IGL_INLINE void squared_distance(
  316. const Eigen::MatrixBase<DerivedV> & V,
  317. const Eigen::MatrixBase<DerivedEle> & Ele,
  318. const AABB<Derivedother_V,DIM> & other,
  319. const Eigen::MatrixBase<Derivedother_V> & other_V,
  320. const Eigen::MatrixBase<Derivedother_Ele> & other_Ele,
  321. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  322. Eigen::PlainObjectBase<DerivedI> & I,
  323. Eigen::PlainObjectBase<DerivedC> & C) const;
  324. private:
  325. template <
  326. typename DerivedEle,
  327. typename Derivedother_V,
  328. typename Derivedother_Ele,
  329. typename DerivedsqrD,
  330. typename DerivedI,
  331. typename DerivedC>
  332. IGL_INLINE Scalar squared_distance_helper(
  333. const Eigen::MatrixBase<DerivedV> & V,
  334. const Eigen::MatrixBase<DerivedEle> & Ele,
  335. const AABB<Derivedother_V,DIM> * other,
  336. const Eigen::MatrixBase<Derivedother_V> & other_V,
  337. const Eigen::MatrixBase<Derivedother_Ele>& other_Ele,
  338. const Scalar up_sqr_d,
  339. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  340. Eigen::PlainObjectBase<DerivedI> & I,
  341. Eigen::PlainObjectBase<DerivedC> & C) const;
  342. // Compute the squared distance to the primitive in this node: assumes
  343. // that this is indeed a leaf node.
  344. //
  345. // Inputs:
  346. // V #V by dim list of vertex positions
  347. // Ele #Ele by dim list of simplex indices
  348. // p dim-long query point
  349. // sqr_d current minimum distance for this query, see output
  350. // i current index into Ele of closest point, see output
  351. // c dim-long current closest point, see output
  352. // Outputs:
  353. // sqr_d minimum of initial value and squared distance to this
  354. // primitive
  355. // i possibly updated index into Ele of closest point
  356. // c dim-long possibly updated closest point
  357. template <typename DerivedEle>
  358. IGL_INLINE void leaf_squared_distance(
  359. const Eigen::MatrixBase<DerivedV> & V,
  360. const Eigen::MatrixBase<DerivedEle> & Ele,
  361. const RowVectorDIMS & p,
  362. const Scalar low_sqr_d,
  363. Scalar & sqr_d,
  364. int & i,
  365. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  366. // Default low_sqr_d
  367. template <typename DerivedEle>
  368. IGL_INLINE void leaf_squared_distance(
  369. const Eigen::MatrixBase<DerivedV> & V,
  370. const Eigen::MatrixBase<DerivedEle> & Ele,
  371. const RowVectorDIMS & p,
  372. Scalar & sqr_d,
  373. int & i,
  374. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  375. // If new distance (sqr_d_candidate) is less than current distance
  376. // (sqr_d), then update this distance and its associated values
  377. // _in-place_:
  378. //
  379. // Inputs:
  380. // p dim-long query point (only used in DEBUG mode)
  381. // sqr_d candidate minimum distance for this query, see output
  382. // i candidate index into Ele of closest point, see output
  383. // c dim-long candidate closest point, see output
  384. // sqr_d current minimum distance for this query, see output
  385. // i current index into Ele of closest point, see output
  386. // c dim-long current closest point, see output
  387. // Outputs:
  388. // sqr_d minimum of initial value and squared distance to this
  389. // primitive
  390. // i possibly updated index into Ele of closest point
  391. // c dim-long possibly updated closest point
  392. IGL_INLINE void set_min(
  393. const RowVectorDIMS & p,
  394. const Scalar sqr_d_candidate,
  395. const int i_candidate,
  396. const RowVectorDIMS & c_candidate,
  397. Scalar & sqr_d,
  398. int & i,
  399. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  400. public:
  401. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  402. };
  403. }
  404. #ifndef IGL_STATIC_LIBRARY
  405. # include "AABB.cpp"
  406. #endif
  407. #endif