usage.cpp
Go to the documentation of this file.
1 // This example is in the public domain
2 
3 #include "nabo/nabo.h"
4 #include <iostream>
5 
6 int main()
7 {
8  using namespace Nabo;
9  using namespace Eigen;
10  using namespace std;
11 
12  // 100 points in 3D
13  MatrixXf M = MatrixXf::Random(3, 100);
14  // 50 query points
15  MatrixXf q = MatrixXf::Random(3, 50);
16 
17  // Create a kd-tree for M, note that M must stay valid during the lifetime of the kd-tree.
19 
20  // The output of the query are a matrix of indices to columns of M and
21  // a matrix of squared distances corresponding to these indices.
22  // These matrix must have the correct size when passed to the search function.
23  MatrixXi indices;
24  MatrixXf dists2;
25 
26  // Look for the 5 nearest neighbours of each query point,
27  // We do not want approximations but we want to sort by the distance,
28  indices.resize(5, q.cols());
29  dists2.resize(5, q.cols());
30  nns->knn(q, indices, dists2, 5, 0, NNSearchF::SORT_RESULTS);
31 
32  // Look for the 3 nearest neighbours of each query point, use the data themselves for the query.
33  // We do not want approximations but we want to sort by the distance,
34  // and we want to allow self-match.
35  indices.resize(3, M.cols());
36  dists2.resize(3, M.cols());
37  nns->knn(M, indices, dists2, 3, 0, NNSearchF::SORT_RESULTS | NNSearchF::ALLOW_SELF_MATCH);
38 
39  // Make sure that the closest return points correspond to the points from M.
40  for (int i = 0; i < 100; ++i)
41  {
42  // The query is the data itself and we allow self-match.
43  if (indices.coeff(0, i) != i)
44  cerr << "Oups, something went wrong: " << indices.coeff(0, i) << " " << i << endl;
45  }
46 
47  // Now look for the 2 nearset neighbours of each query point.
48  // We do allow 10% approximation but do not want to allow self-match.
49  // We do not care about sorting either.
50  indices.resize(2, M.cols());
51  dists2.resize(2, M.cols());
52  nns->knn(M, indices, dists2, 2, 0.1, 0);
53  for (int i = 0; i < 50; ++i)
54  {
55  // The query is the data itself but we forbide self-match.
56  if (indices.coeff(0, i) == i)
57  cerr << "Oups, something went wrong: " << indices.coeff(0, i) << " " << i << endl;
58  }
59 
60  // Cleanup the kd-tree.
61  delete nns;
62 
63  return 0;
64 }
Nabo
Namespace for Nabo.
Definition: experimental/kdtree_cpu.cpp:40
nabo.h
public interface
Nabo::NearestNeighbourSearch::ALLOW_SELF_MATCH
@ ALLOW_SELF_MATCH
allows the return of the same point as the query, if this point is in the data cloud; forbidden by de...
Definition: nabo.h:310
main
int main()
Definition: usage.cpp:6
Nabo::NearestNeighbourSearch::createKDTreeLinearHeap
static NearestNeighbourSearch * createKDTreeLinearHeap(const CloudType &cloud, const Index dim=std::numeric_limits< Index >::max(), const unsigned creationOptionFlags=0, const Parameters &additionalParameters=Parameters())
Create a nearest-neighbour search, using a kd-tree with linear heap, good for small k (~up to 30)
Definition: nabo/nabo.cpp:166
Nabo::NearestNeighbourSearch
Nearest neighbour search interface, templatized on scalar type.
Definition: nabo.h:258
test.nns
nns
Definition: test.py:7
std
Definition: any.hpp:450
test.q
q
Definition: test.py:8
Nabo::NearestNeighbourSearch::SORT_RESULTS
@ SORT_RESULTS
sort points by distances, when k > 1; do not sort by default
Definition: nabo.h:311


libnabo
Author(s): Stéphane Magnenat
autogenerated on Fri Feb 2 2024 03:51:44