AABB.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 <
  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. // min_sqr_d specified maximum squared distance
  209. // min_sqr_d current minimum squared distance (only consider distances
  210. // less than this), see output.
  211. // Outputs:
  212. // min_sqr_d updated current minimum squared distance
  213. // i facet index corresponding to smallest distances
  214. // c closest point
  215. // Returns squared distance
  216. //
  217. // Known bugs: currently assumes Elements are triangles regardless of
  218. // dimension.
  219. template <typename DerivedEle>
  220. IGL_INLINE Scalar squared_distance(
  221. const Eigen::MatrixBase<DerivedV> & V,
  222. const Eigen::MatrixBase<DerivedEle> & Ele,
  223. const RowVectorDIMS & p,
  224. const Scalar max_sqr_d,
  225. const Scalar min_sqr_d,
  226. int & i,
  227. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  228. // Default max_sqr_d
  229. template <typename DerivedEle>
  230. IGL_INLINE Scalar squared_distance(
  231. const Eigen::MatrixBase<DerivedV> & V,
  232. const Eigen::MatrixBase<DerivedEle> & Ele,
  233. const RowVectorDIMS & p,
  234. const Scalar min_sqr_d,
  235. int & i,
  236. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  237. // All hits
  238. template <typename DerivedEle>
  239. IGL_INLINE bool intersect_ray(
  240. const Eigen::MatrixBase<DerivedV> & V,
  241. const Eigen::MatrixBase<DerivedEle> & Ele,
  242. const RowVectorDIMS & origin,
  243. const RowVectorDIMS & dir,
  244. std::vector<igl::Hit> & hits) const;
  245. // First hit
  246. template <typename DerivedEle>
  247. IGL_INLINE bool intersect_ray(
  248. const Eigen::MatrixBase<DerivedV> & V,
  249. const Eigen::MatrixBase<DerivedEle> & Ele,
  250. const RowVectorDIMS & origin,
  251. const RowVectorDIMS & dir,
  252. igl::Hit & hit) const;
  253. //private:
  254. template <typename DerivedEle>
  255. IGL_INLINE bool intersect_ray(
  256. const Eigen::MatrixBase<DerivedV> & V,
  257. const Eigen::MatrixBase<DerivedEle> & Ele,
  258. const RowVectorDIMS & origin,
  259. const RowVectorDIMS & dir,
  260. const Scalar min_t,
  261. igl::Hit & hit) const;
  262. public:
  263. // Compute the squared distance from all query points in P to the
  264. // _closest_ points on the primitives stored in the AABB hierarchy for
  265. // the mesh (V,Ele).
  266. //
  267. // Inputs:
  268. // V #V by dim list of vertex positions
  269. // Ele #Ele by dim list of simplex indices
  270. // P #P by dim list of query points
  271. // Outputs:
  272. // sqrD #P list of squared distances
  273. // I #P list of indices into Ele of closest primitives
  274. // C #P by dim list of closest points
  275. template <
  276. typename DerivedEle,
  277. typename DerivedP,
  278. typename DerivedsqrD,
  279. typename DerivedI,
  280. typename DerivedC>
  281. IGL_INLINE void squared_distance(
  282. const Eigen::MatrixBase<DerivedV> & V,
  283. const Eigen::MatrixBase<DerivedEle> & Ele,
  284. const Eigen::MatrixBase<DerivedP> & P,
  285. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  286. Eigen::PlainObjectBase<DerivedI> & I,
  287. Eigen::PlainObjectBase<DerivedC> & C) const;
  288. // Compute the squared distance from all query points in P already stored
  289. // in its own AABB hierarchy to the _closest_ points on the primitives
  290. // stored in the AABB hierarchy for the mesh (V,Ele).
  291. //
  292. // Inputs:
  293. // V #V by dim list of vertex positions
  294. // Ele #Ele by dim list of simplex indices
  295. // other AABB hierarchy of another set of primitives (must be points)
  296. // other_V #other_V by dim list of query points
  297. // other_Ele #other_Ele by ss list of simplex indices into other_V
  298. // (must be simple list of points: ss == 1)
  299. // Outputs:
  300. // sqrD #P list of squared distances
  301. // I #P list of indices into Ele of closest primitives
  302. // C #P by dim list of closest points
  303. template <
  304. typename DerivedEle,
  305. typename Derivedother_V,
  306. typename Derivedother_Ele,
  307. typename DerivedsqrD,
  308. typename DerivedI,
  309. typename DerivedC>
  310. IGL_INLINE void squared_distance(
  311. const Eigen::MatrixBase<DerivedV> & V,
  312. const Eigen::MatrixBase<DerivedEle> & Ele,
  313. const AABB<Derivedother_V,DIM> & other,
  314. const Eigen::MatrixBase<Derivedother_V> & other_V,
  315. const Eigen::MatrixBase<Derivedother_Ele> & other_Ele,
  316. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  317. Eigen::PlainObjectBase<DerivedI> & I,
  318. Eigen::PlainObjectBase<DerivedC> & C) const;
  319. private:
  320. template <
  321. typename DerivedEle,
  322. typename Derivedother_V,
  323. typename Derivedother_Ele,
  324. typename DerivedsqrD,
  325. typename DerivedI,
  326. typename DerivedC>
  327. IGL_INLINE Scalar squared_distance_helper(
  328. const Eigen::MatrixBase<DerivedV> & V,
  329. const Eigen::MatrixBase<DerivedEle> & Ele,
  330. const AABB<Derivedother_V,DIM> * other,
  331. const Eigen::MatrixBase<Derivedother_V> & other_V,
  332. const Eigen::MatrixBase<Derivedother_Ele>& other_Ele,
  333. const Scalar min_sqr_d,
  334. Eigen::PlainObjectBase<DerivedsqrD> & sqrD,
  335. Eigen::PlainObjectBase<DerivedI> & I,
  336. Eigen::PlainObjectBase<DerivedC> & C) const;
  337. // Compute the squared distance to the primitive in this node: assumes
  338. // that this is indeed a leaf node.
  339. //
  340. // Inputs:
  341. // V #V by dim list of vertex positions
  342. // Ele #Ele by dim list of simplex indices
  343. // p dim-long query point
  344. // sqr_d current minimum distance for this query, see output
  345. // i current index into Ele of closest point, see output
  346. // c dim-long current closest point, see output
  347. // Outputs:
  348. // sqr_d minimum of initial value and squared distance to this
  349. // primitive
  350. // i possibly updated index into Ele of closest point
  351. // c dim-long possibly updated closest point
  352. template <typename DerivedEle>
  353. IGL_INLINE void leaf_squared_distance(
  354. const Eigen::MatrixBase<DerivedV> & V,
  355. const Eigen::MatrixBase<DerivedEle> & Ele,
  356. const RowVectorDIMS & p,
  357. const Scalar max_sqr_d,
  358. Scalar & sqr_d,
  359. int & i,
  360. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  361. // Default max_sqr_d
  362. template <typename DerivedEle>
  363. IGL_INLINE void leaf_squared_distance(
  364. const Eigen::MatrixBase<DerivedV> & V,
  365. const Eigen::MatrixBase<DerivedEle> & Ele,
  366. const RowVectorDIMS & p,
  367. Scalar & sqr_d,
  368. int & i,
  369. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  370. // If new distance (sqr_d_candidate) is less than current distance
  371. // (sqr_d), then update this distance and its associated values
  372. // _in-place_:
  373. //
  374. // Inputs:
  375. // p dim-long query point (only used in DEBUG mode)
  376. // sqr_d candidate minimum distance for this query, see output
  377. // i candidate index into Ele of closest point, see output
  378. // c dim-long candidate closest point, see output
  379. // sqr_d current minimum distance for this query, see output
  380. // i current index into Ele of closest point, see output
  381. // c dim-long current closest point, see output
  382. // Outputs:
  383. // sqr_d minimum of initial value and squared distance to this
  384. // primitive
  385. // i possibly updated index into Ele of closest point
  386. // c dim-long possibly updated closest point
  387. IGL_INLINE void set_min(
  388. const RowVectorDIMS & p,
  389. const Scalar sqr_d_candidate,
  390. const int i_candidate,
  391. const RowVectorDIMS & c_candidate,
  392. Scalar & sqr_d,
  393. int & i,
  394. Eigen::PlainObjectBase<RowVectorDIMS> & c) const;
  395. public:
  396. EIGEN_MAKE_ALIGNED_OPERATOR_NEW
  397. };
  398. }
  399. #ifndef IGL_STATIC_LIBRARY
  400. # include "AABB.cpp"
  401. #endif
  402. #endif