build_octree.h 2.4 KB

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