knn.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2018 Gavin Barill <[email protected]>
  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_KNN_H
  9. #define IGL_KNN_H
  10. #include "igl_inline.h"
  11. #include <Eigen/Core>
  12. #include <vector>
  13. namespace igl
  14. {
  15. /// Given a 3D set of points P, an whole number k, and an octree
  16. /// find the indicies of the k nearest neighbors for each point in P.
  17. /// Note that each point is its own neighbor.
  18. ///
  19. /// The octree data structures used in this function are intended to be the
  20. /// same ones output from igl::octree
  21. ///
  22. /// @param[in] P #P by 3 list of point locations
  23. /// @param[in] k number of neighbors to find
  24. /// @param[in] point_indices a vector of vectors, where the ith entry is a vector of
  25. /// the indices into P that are the ith octree cell's points
  26. /// @param[in] CH #OctreeCells by 8, where the ith row is the indices of
  27. /// the ith octree cell's children
  28. /// @param[in] CN #OctreeCells by 3, where the ith row is a 3d row vector
  29. /// representing the position of the ith cell's center
  30. /// @param[in] W #OctreeCells, a vector where the ith entry is the width
  31. /// of the ith octree cell
  32. /// @param[out] I #P by k list of k-nearest-neighbor indices into P
  33. template <
  34. typename DerivedP,
  35. typename IndexType,
  36. typename DerivedCH,
  37. typename DerivedCN,
  38. typename DerivedW,
  39. typename DerivedI>
  40. IGL_INLINE void knn(
  41. const Eigen::MatrixBase<DerivedP>& P,
  42. size_t k,
  43. const std::vector<std::vector<IndexType> > & point_indices,
  44. const Eigen::MatrixBase<DerivedCH>& CH,
  45. const Eigen::MatrixBase<DerivedCN>& CN,
  46. const Eigen::MatrixBase<DerivedW>& W,
  47. Eigen::PlainObjectBase<DerivedI> & I);
  48. /// \overload
  49. /// \brief only neighbors found in V
  50. /// @param[in] V #V by 3 list of point locations for which may be neighbors
  51. /// @param[out] I #P by k list of k-nearest-neighbor indices into V
  52. template <
  53. typename DerivedP,
  54. typename DerivedV,
  55. typename IndexType,
  56. typename DerivedCH,
  57. typename DerivedCN,
  58. typename DerivedW,
  59. typename DerivedI>
  60. IGL_INLINE void knn(
  61. const Eigen::MatrixBase<DerivedP>& P,
  62. const Eigen::MatrixBase<DerivedV>& V,
  63. size_t k,
  64. const std::vector<std::vector<IndexType> > & point_indices,
  65. const Eigen::MatrixBase<DerivedCH>& CH,
  66. const Eigen::MatrixBase<DerivedCN>& CN,
  67. const Eigen::MatrixBase<DerivedW>& W,
  68. Eigen::PlainObjectBase<DerivedI> & I);
  69. }
  70. #ifndef IGL_STATIC_LIBRARY
  71. # include "knn.cpp"
  72. #endif
  73. #endif