AABB.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_max_sqr_d;
  39. //int m_depth;
  40. AABB():
  41. m_left(NULL), m_right(NULL),
  42. m_box(), m_primitive(-1)
  43. //m_max_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_max_sqr_d(other.m_max_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_max_sqr_d,second.m_max_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 <typename Derivedbb_mins, typename Derivedbb_maxs>
  108. IGL_INLINE void init(
  109. const Eigen::PlainObjectBase<DerivedV> & V,
  110. const Eigen::MatrixXi & Ele,
  111. const Eigen::PlainObjectBase<Derivedbb_mins> & bb_mins,
  112. const Eigen::PlainObjectBase<Derivedbb_maxs> & bb_maxs,
  113. const Eigen::VectorXi & elements,
  114. const int i = 0);
  115. // Wrapper for root with empty serialization
  116. IGL_INLINE void init(
  117. const Eigen::PlainObjectBase<DerivedV> & V,
  118. const Eigen::MatrixXi & Ele);
  119. // Build an Axis-Aligned Bounding Box tree for a given mesh.
  120. //
  121. // Inputs:
  122. // V #V by dim list of mesh vertex positions.
  123. // Ele #Ele by dim+1 list of mesh indices into #V.
  124. // SI #Ele by dim list revealing for each coordinate where Ele's
  125. // barycenters would be sorted: SI(e,d) = i --> the dth coordinate of
  126. // the barycenter of the eth element would be placed at position i in a
  127. // sorted list.
  128. // I #I list of indices into Ele of elements to include (for recursive
  129. // calls)
  130. //
  131. IGL_INLINE void init(
  132. const Eigen::PlainObjectBase<DerivedV> & V,
  133. const Eigen::MatrixXi & Ele,
  134. const Eigen::MatrixXi & SI,
  135. const Eigen::VectorXi & I);
  136. // Return whether at leaf node
  137. IGL_INLINE bool is_leaf() const;
  138. // Find the indices of elements containing given point: this makes sense
  139. // when Ele is a co-dimension 0 simplex (tets in 3D, triangles in 2D).
  140. //
  141. // Inputs:
  142. // V #V by dim list of mesh vertex positions. **Should be same as used to
  143. // construct mesh.**
  144. // Ele #Ele by dim+1 list of mesh indices into #V. **Should be same as used to
  145. // construct mesh.**
  146. // q dim row-vector query position
  147. // first whether to only return first element containing q
  148. // Returns:
  149. // list of indices of elements containing q
  150. template <typename Derivedq>
  151. IGL_INLINE std::vector<int> find(
  152. const Eigen::PlainObjectBase<DerivedV> & V,
  153. const Eigen::MatrixXi & Ele,
  154. const Eigen::PlainObjectBase<Derivedq> & q,
  155. const bool first=false) const;
  156. // If number of elements m then total tree size should be 2*h where h is
  157. // the deepest depth 2^ceil(log(#Ele*2-1))
  158. IGL_INLINE int subtree_size() const;
  159. // Serialize this class into 3 arrays (so we can pass it pack to matlab)
  160. //
  161. // Outputs:
  162. // bb_mins max_tree by dim list of bounding box min corner positions
  163. // bb_maxs max_tree by dim list of bounding box max corner positions
  164. // elements max_tree list of element or (not leaf id) indices into Ele
  165. // i recursive call index into these arrays {0}
  166. template <typename Derivedbb_mins, typename Derivedbb_maxs>
  167. IGL_INLINE void serialize(
  168. Eigen::PlainObjectBase<Derivedbb_mins> & bb_mins,
  169. Eigen::PlainObjectBase<Derivedbb_maxs> & bb_maxs,
  170. Eigen::VectorXi & elements,
  171. const int i = 0) const;
  172. // Compute squared distance to a query point
  173. //
  174. // Inputs:
  175. // V #V by dim list of vertex positions
  176. // Ele #Ele by dim list of simplex indices
  177. // P 3 list of query point coordinates
  178. // min_sqr_d current minimum squared distance (only find distances
  179. // less than this)
  180. // Outputs:
  181. // I #P list of facet indices corresponding to smallest distances
  182. // C #P by 3 list of closest points
  183. // Returns squared distance
  184. //
  185. // Known bugs: currently assumes Elements are triangles regardless of
  186. // dimension.
  187. IGL_INLINE Scalar squared_distance(
  188. const Eigen::PlainObjectBase<DerivedV> & V,
  189. const Eigen::MatrixXi & Ele,
  190. const RowVectorDIMS & p,
  191. int & i,
  192. RowVectorDIMS & c) const;
  193. //private:
  194. IGL_INLINE Scalar squared_distance(
  195. const Eigen::PlainObjectBase<DerivedV> & V,
  196. const Eigen::MatrixXi & Ele,
  197. const RowVectorDIMS & p,
  198. const Scalar min_sqr_d,
  199. int & i,
  200. RowVectorDIMS & c) const;
  201. // All hits
  202. IGL_INLINE bool intersect_ray(
  203. const Eigen::PlainObjectBase<DerivedV> & V,
  204. const Eigen::MatrixXi & Ele,
  205. const RowVectorDIMS & origin,
  206. const RowVectorDIMS & dir,
  207. std::vector<igl::Hit> & hits) const;
  208. // First hit
  209. IGL_INLINE bool intersect_ray(
  210. const Eigen::PlainObjectBase<DerivedV> & V,
  211. const Eigen::MatrixXi & Ele,
  212. const RowVectorDIMS & origin,
  213. const RowVectorDIMS & dir,
  214. igl::Hit & hit) const;
  215. //private:
  216. IGL_INLINE bool intersect_ray(
  217. const Eigen::PlainObjectBase<DerivedV> & V,
  218. const Eigen::MatrixXi & Ele,
  219. const RowVectorDIMS & origin,
  220. const RowVectorDIMS & dir,
  221. const Scalar min_t,
  222. igl::Hit & hit) const;
  223. public:
  224. template <
  225. typename DerivedP,
  226. typename DerivedsqrD,
  227. typename DerivedI,
  228. typename DerivedC>
  229. IGL_INLINE void squared_distance(
  230. const Eigen::PlainObjectBase<DerivedV> & V,
  231. const Eigen::MatrixXi & Ele,
  232. const Eigen::PlainObjectBase<DerivedP> & P,
  233. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  234. Eigen::PlainObjectBase<DerivedI> & I,
  235. Eigen::PlainObjectBase<DerivedC> & C) const;
  236. template <
  237. typename Derivedother_V,
  238. typename DerivedsqrD,
  239. typename DerivedI,
  240. typename DerivedC>
  241. IGL_INLINE void squared_distance(
  242. const Eigen::PlainObjectBase<DerivedV> & V,
  243. const Eigen::MatrixXi & Ele,
  244. const AABB<Derivedother_V,DIM> & other,
  245. const Eigen::PlainObjectBase<Derivedother_V> & other_V,
  246. const Eigen::MatrixXi & other_Ele,
  247. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  248. Eigen::PlainObjectBase<DerivedI> & I,
  249. Eigen::PlainObjectBase<DerivedC> & C) const;
  250. private:
  251. template <
  252. typename Derivedother_V,
  253. typename DerivedsqrD,
  254. typename DerivedI,
  255. typename DerivedC>
  256. IGL_INLINE Scalar squared_distance_helper(
  257. const Eigen::PlainObjectBase<DerivedV> & V,
  258. const Eigen::MatrixXi & Ele,
  259. const AABB<Derivedother_V,DIM> * other,
  260. const Eigen::PlainObjectBase<Derivedother_V> & other_V,
  261. const Eigen::MatrixXi & other_Ele,
  262. const Scalar min_sqr_d,
  263. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  264. Eigen::PlainObjectBase<DerivedI> & I,
  265. Eigen::PlainObjectBase<DerivedC> & C) const;
  266. // Helper function for leaves: works in-place on sqr_d
  267. IGL_INLINE void leaf_squared_distance(
  268. const Eigen::PlainObjectBase<DerivedV> & V,
  269. const Eigen::MatrixXi & Ele,
  270. const RowVectorDIMS & p,
  271. Scalar & sqr_d,
  272. int & i,
  273. RowVectorDIMS & c) const;
  274. IGL_INLINE void set_min(
  275. const RowVectorDIMS & p,
  276. const Scalar sqr_d_candidate,
  277. const int i_candidate,
  278. const RowVectorDIMS & c_candidate,
  279. Scalar & sqr_d,
  280. int & i,
  281. RowVectorDIMS & c) const;
  282. public:
  283. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  284. };
  285. }
  286. #ifndef IGL_STATIC_LIBRARY
  287. # include "AABB.cpp"
  288. #endif
  289. #endif