ITK 6.0.0
Insight Toolkit
 
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,
53 typename num_t = double,
54 int DIM = -1,
55 class Distance = nanoflann::metric_L2,
56 typename IndexType = size_t>
58{
60 using metric_t = typename Distance::template traits<num_t, self_t>::distance_t;
61 using index_t = nanoflann::KDTreeSingleIndexAdaptor<metric_t, self_t, DIM, IndexType>;
62
65 index_t * index = nullptr;
66
69 KDTreeVectorOfVectorsAdaptor(const size_t /* dimensionality */,
70 const VectorOfVectorsType & mat,
71 const int leaf_max_size = 10)
72 : m_data(mat)
73 {
74 assert(mat.size() != 0 && mat[0].size() != 0);
75 const size_t dims = mat[0].size();
76 if (DIM > 0 && static_cast<int>(dims) != DIM)
77 throw std::runtime_error("Data set dimensionality does not match the 'DIM' template "
78 "argument");
79 index = new index_t(
80 static_cast<int>(dims), *this /* adaptor */, nanoflann::KDTreeSingleIndexAdaptorParams(leaf_max_size));
81 }
82
84
85 // Non-copyable, non-movable: owns raw 'index' pointer.
92
93 const VectorOfVectorsType & m_data;
94
103 inline void
104 query(const num_t * query_point,
105 const size_t num_closest,
106 IndexType * out_indices,
107 num_t * out_distances_sq,
108 const int nChecks_IGNORED = 10) const
109 {
110 nanoflann::KNNResultSet<num_t, IndexType> resultSet(num_closest);
111 resultSet.init(out_indices, out_distances_sq);
112 index->findNeighbors(resultSet, query_point, nanoflann::SearchParams());
113 }
114
117
118 const self_t &
119 derived() const
120 {
121 return *this;
122 }
123 self_t &
125 {
126 return *this;
127 }
128
129 // Must return the number of data points
130 inline size_t
132 {
133 return m_data.size();
134 }
135
136 // Returns the dim'th component of the idx'th point in the class:
137 inline num_t
138 kdtree_get_pt(const size_t idx, const size_t dim) const
139 {
140 return m_data[idx][dim];
141 }
142
143 // Optional bounding-box computation: return false to default to a standard
144 // bbox computation loop.
145 // Return true if the BBOX was already computed by the class and returned
146 // in "bb" so it can be avoided to redo it again. Look at bb.size() to
147 // find out the expected dimensionality (e.g. 2 or 3 for point clouds)
148 template <class BBOX>
149 bool
150 kdtree_get_bbox(BBOX & /*bb*/) const
151 {
152 return false;
153 }
154
156
157}; // end of KDTreeVectorOfVectorsAdaptor
KDTreeVectorOfVectorsAdaptor & operator=(KDTreeVectorOfVectorsAdaptor &&)=delete
KDTreeVectorOfVectorsAdaptor(KDTreeVectorOfVectorsAdaptor &&)=delete
KDTreeVectorOfVectorsAdaptor(const vcl_size_t, const VectorOfVectorsType &mat, const int leaf_max_size=10)
KDTreeVectorOfVectorsAdaptor< VectorofVectorsT, double, DIM, nanoflann::metric_L2 > self_t
void query(const num_t *query_point, const vcl_size_t num_closest, IndexType *out_indices, num_t *out_distances_sq, const int nChecks_IGNORED=10) const
KDTreeVectorOfVectorsAdaptor & operator=(const KDTreeVectorOfVectorsAdaptor &)=delete
typename nanoflann::metric_L2::template traits< double, self_t >::distance_t metric_t
nanoflann::KDTreeSingleIndexAdaptor< metric_t, self_t, DIM, vcl_size_t > index_t
num_t kdtree_get_pt(const vcl_size_t idx, const vcl_size_t dim) const
KDTreeVectorOfVectorsAdaptor(const KDTreeVectorOfVectorsAdaptor &)=delete