23#ifndef VULPES_DATASTRUCTURE_KDTREE_HPP_
24#define VULPES_DATASTRUCTURE_KDTREE_HPP_
82 explicit KDTree(
const std::vector<std::array<double, Dim>>& data)
85 for (
const auto& pt : data) {
106 explicit KDTree(
const double* data, int64_t size)
108 assert(size % Dim == 0);
123 explicit KDTree(
const double* data,
const std::vector<int64_t>& indices)
142 void build(int64_t leafsize,
bool median,
bool compact) {
144 m_tree.raw_boxsize_data =
nullptr;
166 const std::array<double, 2 * Dim>& boxsize,
195 template <
double PNorm,
bool Periodic = false>
196 std::pair<Eigen::MatrixXd, Eigen::Matrix<int64_t, -1, -1>>
queryKnn(
197 const double* points,
201 double distance_upper_bound = std::numeric_limits<double>::max())
const {
204 Eigen::MatrixXd distances(k, n_points);
205 Eigen::Matrix<int64_t, -1, -1> indices(k, n_points);
207 std::vector<int64_t> ks(k);
208 std::iota(ks.begin(), ks.end(), 1);
211 &
m_tree, distances.data(), indices.data(), points, n_points, ks.data(),
212 k, k, eps, distance_upper_bound);
214 return {distances, indices};
233 template <
double PNorm,
bool Periodic = false>
234 std::pair<Eigen::MatrixXd, Eigen::Matrix<int64_t, -1, -1>>
queryKnn(
235 const std::vector<std::array<double, Dim>>& points,
238 double distance_upper_bound = std::numeric_limits<double>::max())
const {
241 &points[0][0], points.size(), k, eps, distance_upper_bound);
261 template <
double PNorm,
bool Periodic = false>
262 std::vector<std::vector<scipy::ckdtree_intp_t>>
queryBall(
263 const double* points,
264 const double* radiuses,
267 bool return_length =
false,
268 bool sort_output =
true)
const {
270 std::vector<std::vector<scipy::ckdtree_intp_t>> results(n_queries);
272 &
m_tree, points, radiuses, eps, n_queries, results.data(),
273 return_length, sort_output);
293 template <
double PNorm,
bool Periodic = false>
294 std::vector<std::vector<scipy::ckdtree_intp_t>>
queryBall(
295 const std::vector<std::array<double, Dim>>& points,
296 const std::vector<double>& radiuses,
298 bool return_length =
false,
299 bool sort_output =
true)
const {
301 assert(points.size() == radiuses.size());
302 std::vector<std::vector<scipy::ckdtree_intp_t>> results(points.size());
304 &
m_tree, &points[0][0], radiuses.data(), eps, points.size(),
305 results.data(), return_length, sort_output);
322 template <
double PNorm,
bool Periodic = false>
324 double r,
double eps = 0.0)
const {
326 std::vector<scipy::ordered_pair> result;
346 template <
double PNorm,
bool Periodic = false>
348 const KDTree<Dim>& other,
double r,
double eps = 0.0)
const {
350 std::vector<std::vector<scipy::ckdtree_intp_t>> result(
m_tree.n);
405 node->
less =
nullptr;
410 node->
less = root +
reinterpret_cast<int_t
>(node->
less);
426 for (int64_t i = 0; i < Dim; ++i) {
431 for (int64_t i = 1; i < size; ++i) {
432 for (int64_t d = 0; d < Dim; ++d) {
456 std::array<double, Dim>
m_maxs{{-std::numeric_limits<double>::max()}};
458 std::array<double, Dim>
m_mins{{std::numeric_limits<double>::max()}};
int64_t m_raw_data_size
Definition kdtree.hpp:453
std::pair< Eigen::MatrixXd, Eigen::Matrix< int64_t, -1, -1 > > queryKnn(const std::vector< std::array< double, Dim > > &points, int64_t k, double eps=0, double distance_upper_bound=std::numeric_limits< double >::max()) const
Queries the KDTree for k-nearest neighbors (KNN) of a set of points.
Definition kdtree.hpp:234
std::vector< scipy::ordered_pair > queryPairs(double r, double eps=0.0) const
Finds all pairs of points whose distance is within a given radius.
Definition kdtree.hpp:323
scipy::ckdtree< Dim > m_tree
Definition kdtree.hpp:462
void build(int64_t leafsize, bool median, bool compact)
Builds the KDTree.
Definition kdtree.hpp:142
void buildRun(bool median, bool compact)
Internal function to build the KDTree.
Definition kdtree.hpp:368
KDTree(const std::vector< std::array< double, Dim > > &data)
Constructs a KDTree from a vector of data points.
Definition kdtree.hpp:82
void compututeBounds()
Computes the bounding box (max and min values) for the data points.
Definition kdtree.hpp:424
std::vector< scipy::ckdtreenode > m_tree_buffer
Definition kdtree.hpp:445
std::vector< std::vector< scipy::ckdtree_intp_t > > queryBall(const std::vector< std::array< double, Dim > > &points, const std::vector< double > &radiuses, double eps=0.0, bool return_length=false, bool sort_output=true) const
Queries the KDTree for all points within a given radius of a set of input points.
Definition kdtree.hpp:294
const double * m_raw_data
Definition kdtree.hpp:451
KDTree(const double *data, int64_t size)
Constructs a KDTree using a raw pointer to the data.
Definition kdtree.hpp:106
bool m_is_built
Definition kdtree.hpp:441
std::vector< int64_t > m_indices
Definition kdtree.hpp:449
void build(const std::array< double, 2 *Dim > &boxsize, int64_t leafsize, bool median, bool compact)
Builds the KDTree with specified bounding box sizes.
Definition kdtree.hpp:165
std::vector< double > m_data
Definition kdtree.hpp:447
std::pair< Eigen::MatrixXd, Eigen::Matrix< int64_t, -1, -1 > > queryKnn(const double *points, int64_t n_points, int64_t k, double eps=0, double distance_upper_bound=std::numeric_limits< double >::max()) const
Queries the KDTree for k-nearest neighbors (KNN) of a set of points.
Definition kdtree.hpp:196
std::array< double, 2 *Dim > m_boxsize
Definition kdtree.hpp:460
std::array< double, Dim > m_mins
Definition kdtree.hpp:458
KDTree(const double *data, const std::vector< int64_t > &indices)
Constructs a KDTree using a raw pointer to the data.
Definition kdtree.hpp:123
std::vector< std::vector< scipy::ckdtree_intp_t > > queryBall(const double *points, const double *radiuses, int64_t n_queries, double eps=0.0, bool return_length=false, bool sort_output=true) const
Queries the KDTree for all points within a given radius of a set of input points.
Definition kdtree.hpp:262
std::vector< std::vector< scipy::ckdtree_intp_t > > queryBallTree(const KDTree< Dim > &other, double r, double eps=0.0) const
Finds all points within a given radius from another KDTree.
Definition kdtree.hpp:347
int64_t m_leafsize
Definition kdtree.hpp:443
void postInitTraverse(scipy::ckdtreenode *root, scipy::ckdtreenode *node)
Performs post-initialization traversal of the tree to adjust node pointers.
Definition kdtree.hpp:403
std::array< double, Dim > m_maxs
Definition kdtree.hpp:456
Definition crs_map.hpp:36
static ckdtree_intp_t build_ckdtree(ckdtree< Dim > *self, ckdtree_intp_t start_idx, intptr_t end_idx, double *maxes, double *mins, const int _median, const int _compact)
Definition ckdtree_decl.hpp:141
int query_ball_point(const ckdtree< Dim > *self, const double *x, const double *r, const double eps, const ckdtree_intp_t n_queries, std::vector< ckdtree_intp_t > *results, const bool return_length, const bool sort_output)
Definition ckdtree_decl.hpp:1699
int query_ball_tree(const ckdtree< Dim > *self, const ckdtree< Dim > *other, const double r, const double eps, std::vector< ckdtree_intp_t > *results)
Definition ckdtree_decl.hpp:2151
intptr_t ckdtree_intp_t
Definition ckdtree_decl.hpp:82
int query_knn(const ckdtree< Dim > *self, double *dd, ckdtree_intp_t *ii, const double *xx, const ckdtree_intp_t n, const ckdtree_intp_t *k, const ckdtree_intp_t nk, const ckdtree_intp_t kmax, const double eps, const double distance_upper_bound)
Definition ckdtree_decl.hpp:1545
int query_pairs(const ckdtree< Dim > *self, const double r, const double eps, std::vector< ordered_pair > *results)
Definition ckdtree_decl.hpp:1944
Definition cell_manager.hpp:38
Definition ckdtree_decl.hpp:123
Definition ckdtree_decl.hpp:112
ckdtree_intp_t split_dim
Definition ckdtree_decl.hpp:113
ckdtreenode * greater
Definition ckdtree_decl.hpp:119
ckdtreenode * less
Definition ckdtree_decl.hpp:118