AABB.h 16 KB

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