build_octree.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2018 Gavin Barill <gavinpcb@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_BUILD_OCTREE
  9. #define IGL_BUILD_OCTREE
  10. #include "igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <vector>
  13. namespace igl
  14. {
  15. // Given a set of 3D points P, generate data structures for a pointerless
  16. // octree. Each cell stores its points, children, center location and width.
  17. // Our octree is not dense. We use the following rule: if the current cell
  18. // has any number of points, it will have all 8 children. A leaf cell will
  19. // have -1's as its list of child indices.
  20. //
  21. // We use a binary numbering of children. Treating the parent cell's center
  22. // as the origin, we number the octants in the following manner:
  23. // The first bit is 1 iff the octant's x coordinate is positive
  24. // The second bit is 1 iff the octant's y coordinate is positive
  25. // The third bit is 1 iff the octant's z coordinate is positive
  26. //
  27. // For example, the octant with negative x, positive y, positive z is:
  28. // 110 binary = 6 decimal
  29. //
  30. // Inputs:
  31. // P #P by 3 list of point locations
  32. //
  33. // Outputs:
  34. // point_indices a vector of vectors, where the ith entry is a vector of
  35. // the indices into P that are the ith octree cell's points
  36. // children a vector of vectors, where the ith entry is a vector of
  37. // the ith octree cell's of octree children
  38. // centers a vector where the ith entry is a 3d row vector
  39. // representing the position of the ith cell's center
  40. // widths a vector where the ith entry is the width of the ith
  41. // octree cell
  42. //
  43. template <typename DerivedP, typename IndexType, typename CentersType,
  44. typename WidthsType>
  45. IGL_INLINE void build_octree(const Eigen::MatrixBase<DerivedP>& P,
  46. std::vector<std::vector<IndexType> > & point_indices,
  47. std::vector<Eigen::Matrix<IndexType,8,1>,
  48. Eigen::aligned_allocator<Eigen::Matrix<IndexType,8,1> > > & children,
  49. std::vector<Eigen::Matrix<CentersType,1,3>,
  50. Eigen::aligned_allocator<Eigen::Matrix<CentersType,1,3> > > & centers,
  51. std::vector<WidthsType> & widths);
  52. }
  53. #ifndef IGL_STATIC_LIBRARY
  54. # include "build_octree.cpp"
  55. #endif
  56. #endif