ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
kdtree_cuda_3d_index.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
5  * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
6  * Copyright 2011 Andreas Muetzel (amuetzel@uni-koblenz.de). All rights reserved.
7  *
8  * THE BSD LICENSE
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *************************************************************************/
31 
32 #ifndef FLANN_KDTREE_CUDA_3D_INDEX_H_
33 #define FLANN_KDTREE_CUDA_3D_INDEX_H_
34 
35 #include <algorithm>
36 #include <map>
37 #include <cassert>
38 #include <cstring>
39 #include "FLANN/general.h"
41 #include "FLANN/util/matrix.h"
42 #include "FLANN/util/result_set.h"
43 #include "FLANN/util/heap.h"
44 #include "FLANN/util/allocator.h"
45 #include "FLANN/util/random.h"
46 #include "FLANN/util/saving.h"
47 #include "FLANN/util/params.h"
48 
49 namespace flann
50 {
51 
53 {
54  KDTreeCuda3dIndexParams( int leaf_max_size = 64 )
55  {
56  (*this)["algorithm"] = FLANN_INDEX_KDTREE_CUDA;
57  (*this)["leaf_max_size"] = leaf_max_size;
58  (*this)["dim"] = 3;
59  }
60 };
61 
69 template <typename Distance>
70 class KDTreeCuda3dIndex : public NNIndex<Distance>
71 {
72 public:
73  typedef typename Distance::ElementType ElementType;
74  typedef typename Distance::ResultType DistanceType;
75 
77 
79 
80  typedef bool needs_kdtree_distance;
81 
90  Distance d = Distance() ) : BaseClass(params,d), dataset_(inputData), leaf_count_(0), visited_leafs(0), node_count_(0), current_node_count_(0)
91  {
92  size_ = dataset_.rows;
93  dim_ = dataset_.cols;
94 
95  int dim_param = get_param(params,"dim",-1);
96  if (dim_param>0) dim_ = dim_param;
97  leaf_max_size_ = get_param(params,"leaf_max_size",10);
98  assert( dim_ == 3 );
99  gpu_helper_=0;
100  }
101 
104 
109  {
110  delete[] data_.ptr();
111  clearGpuBuffers();
112  }
113 
114  BaseClass* clone() const
115  {
116  throw FLANNException("KDTreeCuda3dIndex cloning is not implemented");
117  }
118 
122  void buildIndex()
123  {
124  // Create a permutable array of indices to the input vectors.
125  vind_.resize(size_);
126  for (size_t i = 0; i < size_; i++) {
127  vind_[i] = i;
128  }
129 
130  leaf_count_=0;
131  node_count_=0;
132  // computeBoundingBox(root_bbox_);
133  // tree_.reserve(log2((double)size_/leaf_max_size_));
134  // divideTree(0, size_, root_bbox_,-1 ); // construct the tree
135 
136  delete[] data_.ptr();
137 
138  uploadTreeToGpu();
139  }
140 
142  {
144  }
145 
146 
147  void removePoint(size_t index)
148  {
149  throw FLANNException( "removePoint not implemented for this index type!" );
150  }
151 
152  ElementType* getPoint(size_t id)
153  {
154  return dataset_[id];
155  }
156 
157  void saveIndex(FILE* stream)
158  {
159  throw FLANNException( "Index saving not implemented!" );
160 
161  }
162 
163 
164  void loadIndex(FILE* stream)
165  {
166  throw FLANNException( "Index loading not implemented!" );
167  }
168 
169  size_t veclen() const
170  {
171  return dim_;
172  }
173 
179  int usedMemory() const
180  {
181  // return tree_.size()*sizeof(Node)+dataset_.rows*sizeof(int); // pool memory and vind array memory
182  return 0;
183  }
184 
185 
194  int knnSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, size_t knn, const SearchParams& params) const
195  {
196  knnSearchGpu(queries,indices, dists, knn, params);
197  return knn*queries.rows; // hack...
198  }
199 
208  int knnSearch(const Matrix<ElementType>& queries,
209  std::vector< std::vector<int> >& indices,
210  std::vector<std::vector<DistanceType> >& dists,
211  size_t knn,
212  const SearchParams& params) const
213  {
214  knnSearchGpu(queries,indices, dists, knn, params);
215  return knn*queries.rows; // hack...
216  }
217 
226  void knnSearchGpu(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists, size_t knn, const SearchParams& params) const;
227 
228  int knnSearchGpu(const Matrix<ElementType>& queries,
229  std::vector< std::vector<int> >& indices,
230  std::vector<std::vector<DistanceType> >& dists,
231  size_t knn,
232  const SearchParams& params) const
233  {
234  flann::Matrix<int> ind( new int[knn*queries.rows], queries.rows,knn);
235  flann::Matrix<DistanceType> dist( new DistanceType[knn*queries.rows], queries.rows,knn);
236  knnSearchGpu(queries,ind,dist,knn,params);
237  for( size_t i = 0; i<queries.rows; i++ ) {
238  indices[i].resize(knn);
239  dists[i].resize(knn);
240  for( size_t j=0; j<knn; j++ ) {
241  indices[i][j]=ind[i][j];
242  dists[i][j]=dist[i][j];
243  }
244  }
245  delete [] ind.ptr();
246  delete [] dist.ptr();
247  return knn*queries.rows; // hack...
248  }
249 
250  int radiusSearch(const Matrix<ElementType>& queries, Matrix<int>& indices, Matrix<DistanceType>& dists,
251  float radius, const SearchParams& params) const
252  {
253  return radiusSearchGpu(queries,indices, dists, radius, params);
254  }
255 
256  int radiusSearch(const Matrix<ElementType>& queries, std::vector< std::vector<int> >& indices,
257  std::vector<std::vector<DistanceType> >& dists, float radius, const SearchParams& params) const
258  {
259  return radiusSearchGpu(queries,indices, dists, radius, params);
260  }
261 
263  float radius, const SearchParams& params) const;
264 
265  int radiusSearchGpu(const Matrix<ElementType>& queries, std::vector< std::vector<int> >& indices,
266  std::vector<std::vector<DistanceType> >& dists, float radius, const SearchParams& params) const;
267 
272  void findNeighbors(ResultSet<DistanceType>& result, const ElementType* vec, const SearchParams& searchParams) const
273  {
274  }
275 
276 protected:
278  {
279  /* nothing to do here */
280  }
281 
282  void freeIndex()
283  {
284  /* nothing to do here */
285  }
286 
287 private:
288 
289  void uploadTreeToGpu( );
290 
291  void clearGpuBuffers( );
292 
293 
294 
295 
296 private:
297 
298  struct GpuHelper;
299 
300  GpuHelper* gpu_helper_;
301 
302  const Matrix<ElementType> dataset_;
303 
304  int leaf_max_size_;
305 
306  int leaf_count_;
307  int node_count_;
309  int current_node_count_;
310 
311 
315  std::vector<int> vind_;
316 
317  Matrix<ElementType> data_;
318 
319  size_t dim_;
320 
322 }; // class KDTreeCuda3dIndex
323 
324 
325 }
326 
327 #endif //FLANN_KDTREE_SINGLE_INDEX_H_
double Distance(const Point3D< Real > &p1, const Point3D< Real > &p2)
cmdLineReadable * params[]
core::Tensor result
Definition: VtkUtils.cpp:76
KDTreeCuda3dIndex(const KDTreeCuda3dIndex &other)
Distance::ElementType ElementType
void removePoint(size_t index)
void knnSearchGpu(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, size_t knn, const SearchParams &params) const
Perform k-nearest neighbor search.
KDTreeCuda3dIndex(const Matrix< ElementType > &inputData, const IndexParams &params=KDTreeCuda3dIndexParams(), Distance d=Distance())
int knnSearch(const Matrix< ElementType > &queries, std::vector< std::vector< int > > &indices, std::vector< std::vector< DistanceType > > &dists, size_t knn, const SearchParams &params) const
Perform k-nearest neighbor search.
ElementType * getPoint(size_t id)
void findNeighbors(ResultSet< DistanceType > &result, const ElementType *vec, const SearchParams &searchParams) const
int radiusSearch(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, float radius, const SearchParams &params) const
Distance::ResultType DistanceType
NNIndex< Distance > BaseClass
KDTreeCuda3dIndex operator=(KDTreeCuda3dIndex other)
flann_algorithm_t getType() const
int radiusSearchGpu(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, float radius, const SearchParams &params) const
int knnSearch(const Matrix< ElementType > &queries, Matrix< int > &indices, Matrix< DistanceType > &dists, size_t knn, const SearchParams &params) const
Perform k-nearest neighbor search.
int radiusSearch(const Matrix< ElementType > &queries, std::vector< std::vector< int > > &indices, std::vector< std::vector< DistanceType > > &dists, float radius, const SearchParams &params) const
int radiusSearchGpu(const Matrix< ElementType > &queries, std::vector< std::vector< int > > &indices, std::vector< std::vector< DistanceType > > &dists, float radius, const SearchParams &params) const
int knnSearchGpu(const Matrix< ElementType > &queries, std::vector< std::vector< int > > &indices, std::vector< std::vector< DistanceType > > &dists, size_t knn, const SearchParams &params) const
size_t cols
Definition: matrix.h:73
size_t rows
Definition: matrix.h:72
T * ptr() const
Definition: matrix.h:127
size_t size_
Definition: nn_index.h:836
flann_algorithm_t
Definition: defines.h:80
@ FLANN_INDEX_KDTREE_SINGLE
Definition: defines.h:85
static double dist(double x1, double y1, double x2, double y2)
Definition: lsd.c:207
T get_param(const IndexParams &params, std::string name, const T &default_value)
Definition: params.h:95
std::map< std::string, any > IndexParams
Definition: params.h:51
#define USING_BASECLASS_SYMBOLS
Definition: nn_index.h:887
KDTreeCuda3dIndexParams(int leaf_max_size=64)