AABB.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 dim-long query point
  178. // Outputs:
  179. // i facet index corresponding to smallest distances
  180. // c closest point
  181. // Returns squared distance
  182. //
  183. // Known bugs: currently assumes Elements are triangles regardless of
  184. // dimension.
  185. IGL_INLINE Scalar squared_distance(
  186. const Eigen::PlainObjectBase<DerivedV> & V,
  187. const Eigen::MatrixXi & Ele,
  188. const RowVectorDIMS & p,
  189. int & i,
  190. RowVectorDIMS & c) const;
  191. //private:
  192. // Compute squared distance to a query point
  193. //
  194. // Inputs:
  195. // V #V by dim list of vertex positions
  196. // Ele #Ele by dim list of simplex indices
  197. // p dim-long query point
  198. // min_sqr_d current minimum squared distance (only consider distances
  199. // less than this), see output.
  200. // Outputs:
  201. // min_sqr_d updated current minimum squared distance
  202. // i facet index corresponding to smallest distances
  203. // c closest point
  204. // Returns squared distance
  205. //
  206. // Known bugs: currently assumes Elements are triangles regardless of
  207. // dimension.
  208. IGL_INLINE Scalar squared_distance(
  209. const Eigen::PlainObjectBase<DerivedV> & V,
  210. const Eigen::MatrixXi & Ele,
  211. const RowVectorDIMS & p,
  212. const Scalar min_sqr_d,
  213. int & i,
  214. RowVectorDIMS & c) const;
  215. // All hits
  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. std::vector<igl::Hit> & hits) const;
  222. // First hit
  223. IGL_INLINE bool intersect_ray(
  224. const Eigen::PlainObjectBase<DerivedV> & V,
  225. const Eigen::MatrixXi & Ele,
  226. const RowVectorDIMS & origin,
  227. const RowVectorDIMS & dir,
  228. igl::Hit & hit) const;
  229. //private:
  230. IGL_INLINE bool intersect_ray(
  231. const Eigen::PlainObjectBase<DerivedV> & V,
  232. const Eigen::MatrixXi & Ele,
  233. const RowVectorDIMS & origin,
  234. const RowVectorDIMS & dir,
  235. const Scalar min_t,
  236. igl::Hit & hit) const;
  237. public:
  238. // Compute the squared distance from all query points in P to the
  239. // _closest_ points on the primitives stored in the AABB hierarchy for
  240. // the mesh (V,Ele).
  241. //
  242. // Inputs:
  243. // V #V by dim list of vertex positions
  244. // Ele #Ele by dim list of simplex indices
  245. // P #P by dim list of query points
  246. // Outputs:
  247. // sqrD #P list of squared distances
  248. // I #P list of indices into Ele of closest primitives
  249. // C #P by dim list of closest points
  250. template <
  251. typename DerivedP,
  252. typename DerivedsqrD,
  253. typename DerivedI,
  254. typename DerivedC>
  255. IGL_INLINE void squared_distance(
  256. const Eigen::PlainObjectBase<DerivedV> & V,
  257. const Eigen::MatrixXi & Ele,
  258. const Eigen::PlainObjectBase<DerivedP> & P,
  259. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  260. Eigen::PlainObjectBase<DerivedI> & I,
  261. Eigen::PlainObjectBase<DerivedC> & C) const;
  262. // Compute the squared distance from all query points in P already stored
  263. // in its own AABB hierarchy to the _closest_ points on the primitives
  264. // stored in the AABB hierarchy for the mesh (V,Ele).
  265. //
  266. // Inputs:
  267. // V #V by dim list of vertex positions
  268. // Ele #Ele by dim list of simplex indices
  269. // other AABB hierarchy of another set of primitives (must be points)
  270. // other_V #other_V by dim list of query points
  271. // other_Ele #other_Ele by ss list of simplex indices into other_V
  272. // (must be simple list of points: ss == 1)
  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 Derivedother_V,
  279. typename DerivedsqrD,
  280. typename DerivedI,
  281. typename DerivedC>
  282. IGL_INLINE void squared_distance(
  283. const Eigen::PlainObjectBase<DerivedV> & V,
  284. const Eigen::MatrixXi & Ele,
  285. const AABB<Derivedother_V,DIM> & other,
  286. const Eigen::PlainObjectBase<Derivedother_V> & other_V,
  287. const Eigen::MatrixXi & other_Ele,
  288. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  289. Eigen::PlainObjectBase<DerivedI> & I,
  290. Eigen::PlainObjectBase<DerivedC> & C) const;
  291. private:
  292. template <
  293. typename Derivedother_V,
  294. typename DerivedsqrD,
  295. typename DerivedI,
  296. typename DerivedC>
  297. IGL_INLINE Scalar squared_distance_helper(
  298. const Eigen::PlainObjectBase<DerivedV> & V,
  299. const Eigen::MatrixXi & Ele,
  300. const AABB<Derivedother_V,DIM> * other,
  301. const Eigen::PlainObjectBase<Derivedother_V> & other_V,
  302. const Eigen::MatrixXi & other_Ele,
  303. const Scalar min_sqr_d,
  304. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  305. Eigen::PlainObjectBase<DerivedI> & I,
  306. Eigen::PlainObjectBase<DerivedC> & C) const;
  307. // Compute the squared distance to the primitive in this node: assumes
  308. // that this is indeed a leaf node.
  309. //
  310. // Inputs:
  311. // V #V by dim list of vertex positions
  312. // Ele #Ele by dim list of simplex indices
  313. // p dim-long query point
  314. // sqr_d current minimum distance for this query, see output
  315. // i current index into Ele of closest point, see output
  316. // c dim-long current closest point, see output
  317. // Outputs:
  318. // sqr_d minimum of initial value and squared distance to this
  319. // primitive
  320. // i possibly updated index into Ele of closest point
  321. // c dim-long possibly updated closest point
  322. IGL_INLINE void leaf_squared_distance(
  323. const Eigen::PlainObjectBase<DerivedV> & V,
  324. const Eigen::MatrixXi & Ele,
  325. const RowVectorDIMS & p,
  326. Scalar & sqr_d,
  327. int & i,
  328. RowVectorDIMS & c) const;
  329. // If new distance (sqr_d_candidate) is less than current distance
  330. // (sqr_d), then update this distance and its associated values
  331. // _in-place_:
  332. //
  333. // Inputs:
  334. // p dim-long query point (only used in DEBUG mode)
  335. // sqr_d candidate minimum distance for this query, see output
  336. // i candidate index into Ele of closest point, see output
  337. // c dim-long candidate closest point, see output
  338. // sqr_d current minimum distance for this query, see output
  339. // i current index into Ele of closest point, see output
  340. // c dim-long current closest point, see output
  341. // Outputs:
  342. // sqr_d minimum of initial value and squared distance to this
  343. // primitive
  344. // i possibly updated index into Ele of closest point
  345. // c dim-long possibly updated closest point
  346. IGL_INLINE void set_min(
  347. const RowVectorDIMS & p,
  348. const Scalar sqr_d_candidate,
  349. const int i_candidate,
  350. const RowVectorDIMS & c_candidate,
  351. Scalar & sqr_d,
  352. int & i,
  353. RowVectorDIMS & c) const;
  354. public:
  355. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  356. };
  357. }
  358. #ifndef IGL_STATIC_LIBRARY
  359. # include "AABB.cpp"
  360. #endif
  361. #endif