Krotos Modules 3
Loading...
Searching...
No Matches
KDTreeVectorOfVectorsAdaptor.h
Go to the documentation of this file.
1/***********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright 2011-16 Jose Luis Blanco (joseluisblancoc@gmail.com).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *************************************************************************/
28
29#pragma once
30
31#include "nanoflann.hpp"
32#include <vector>
33
34// ===== This example shows how to use nanoflann with these types of containers:
35// using my_vector_of_vectors_t = std::vector<std::vector<double> > ;
36//
37// The next one requires #include <Eigen/Dense>
38// using my_vector_of_vectors_t = std::vector<Eigen::VectorXd> ;
39// =============================================================================
40
52template <class VectorOfVectorsType, typename num_t = double, int DIM = -1, class Distance = nanoflann::metric_L2,
53 typename IndexType = size_t>
55{
57 using metric_t = typename Distance::template traits<num_t, self_t>::distance_t;
59 IndexType>; // nanoflann::KDTreeSingleIndexAdaptor,
60 // nanoflann::KDTreeSingleIndexDynamicAdaptor
61
64 index_t* index = nullptr;
65
68 KDTreeVectorOfVectorsAdaptor(const size_t /* dimensionality */, const VectorOfVectorsType& mat,
69 const int leaf_max_size = 10)
70 : m_data(mat)
71 {
72 assert(mat.size() != 0 && mat[0].size() != 0);
73 const size_t dims = mat[0].size();
74 if (DIM > 0 && static_cast<int>(dims) != DIM)
75 throw std::runtime_error("Data set dimensionality does not match the 'DIM' template "
76 "argument");
77 index = new index_t(static_cast<int>(dims), *this /* adaptor */,
79 }
80
82
83 const VectorOfVectorsType& m_data;
84
90 inline void query(const num_t* query_point, const size_t num_closest, IndexType* out_indices,
91 num_t* out_distances_sq) const
92 {
93 nanoflann::KNNResultSet<num_t, IndexType> resultSet(num_closest);
94 resultSet.init(out_indices, out_distances_sq);
95 index->findNeighbors(resultSet, query_point);
96 }
97
101 const self_t& derived() const { return *this; }
102 self_t& derived() { return *this; }
103
104 // Must return the number of data points
105 inline size_t kdtree_get_point_count() const { return m_data.size(); }
106
107 // Returns the dim'th component of the idx'th point in the class:
108 inline num_t kdtree_get_pt(const size_t idx, const size_t dim) const { return m_data[idx][dim]; }
109
110 // Optional bounding-box computation: return false to default to a standard
111 // bbox computation loop.
112 // Return true if the BBOX was already computed by the class and returned
113 // in "bb" so it can be avoided to redo it again. Look at bb.size() to
114 // find out the expected dimensionality (e.g. 2 or 3 for point clouds)
115 template <class BBOX> bool kdtree_get_bbox(BBOX& /*bb*/) const { return false; }
116
119}; // end of KDTreeVectorOfVectorsAdaptor
Definition nanoflann.hpp:1226
bool findNeighbors(RESULTSET &result, const ElementType *vec, const SearchParameters &searchParams={}) const
Definition nanoflann.hpp:1353
Definition nanoflann.hpp:148
void init(IndexType *indices_, DistanceType *dists_)
Definition nanoflann.hpp:163
Definition KDTreeVectorOfVectorsAdaptor.h:55
nanoflann::KDTreeSingleIndexAdaptor< metric_t, self_t, DIM, IndexType > index_t
Definition KDTreeVectorOfVectorsAdaptor.h:58
void query(const num_t *query_point, const size_t num_closest, IndexType *out_indices, num_t *out_distances_sq) const
Definition KDTreeVectorOfVectorsAdaptor.h:90
num_t kdtree_get_pt(const size_t idx, const size_t dim) const
Definition KDTreeVectorOfVectorsAdaptor.h:108
size_t kdtree_get_point_count() const
Definition KDTreeVectorOfVectorsAdaptor.h:105
const self_t & derived() const
Definition KDTreeVectorOfVectorsAdaptor.h:101
const VectorOfVectorsType & m_data
Definition KDTreeVectorOfVectorsAdaptor.h:83
KDTreeVectorOfVectorsAdaptor< VectorOfVectorsType, num_t, DIM, Distance > self_t
Definition KDTreeVectorOfVectorsAdaptor.h:56
typename Distance::template traits< num_t, self_t >::distance_t metric_t
Definition KDTreeVectorOfVectorsAdaptor.h:57
self_t & derived()
Definition KDTreeVectorOfVectorsAdaptor.h:102
KDTreeVectorOfVectorsAdaptor(const size_t, const VectorOfVectorsType &mat, const int leaf_max_size=10)
Definition KDTreeVectorOfVectorsAdaptor.h:68
bool kdtree_get_bbox(BBOX &) const
Definition KDTreeVectorOfVectorsAdaptor.h:115
index_t * index
Definition KDTreeVectorOfVectorsAdaptor.h:64
~KDTreeVectorOfVectorsAdaptor()
Definition KDTreeVectorOfVectorsAdaptor.h:81
Definition nanoflann.hpp:618
Definition nanoflann.hpp:566