usage.cpp
Go to the documentation of this file.
00001 // This example is in the public domain
00002 
00003 #include "nabo/nabo.h"
00004 #include <iostream>
00005 
00006 int main()
00007 {
00008         using namespace Nabo;
00009         using namespace Eigen;
00010         using namespace std;
00011         
00012         // 100 points in 3D
00013         MatrixXf M = MatrixXf::Random(3, 100);
00014         // 50 query points
00015         MatrixXf q = MatrixXf::Random(3, 50);
00016         
00017         // Create a kd-tree for M, note that M must stay valid during the lifetime of the kd-tree.
00018         NNSearchF* nns = NNSearchF::createKDTreeLinearHeap(M);
00019         
00020         // The output of the query are a matrix of indices to columns of M and 
00021         // a matrix of squared distances corresponding to these indices.
00022         // These matrix must have the correct size when passed to the search function.
00023         MatrixXi indices;
00024         MatrixXf dists2;
00025         
00026         // Look for the 5 nearest neighbours of each query point, 
00027         // We do not want approximations but we want to sort by the distance,
00028         indices.resize(5, q.cols());
00029         dists2.resize(5, q.cols());
00030         nns->knn(q, indices, dists2, 5, 0, NNSearchF::SORT_RESULTS);
00031         
00032         // Look for the 3 nearest neighbours of each query point, use the data themselves for the query.
00033         // We do not want approximations but we want to sort by the distance,
00034         // and we want to allow self-match.
00035         indices.resize(3, M.cols());
00036         dists2.resize(3, M.cols());
00037         nns->knn(M, indices, dists2, 3, 0, NNSearchF::SORT_RESULTS | NNSearchF::ALLOW_SELF_MATCH);
00038         
00039         // Make sure that the closest return points correspond to the points from M.
00040         for (int i = 0; i < 100; ++i)
00041         {
00042                 // The query is the data itself and we allow self-match.
00043                 if (indices.coeff(0, i) != i)
00044                         cerr << "Oups, something went wrong: " << indices.coeff(0, i) << " " <<  i << endl;
00045         }
00046         
00047         // Now look for the 2 nearset neighbours of each query point.
00048         // We do allow 10% approximation but do not want to allow self-match.
00049         // We do not care about sorting either.
00050         indices.resize(2, M.cols());
00051         dists2.resize(2, M.cols());
00052         nns->knn(M, indices, dists2, 2, 0.1, 0);
00053         for (int i = 0; i < 50; ++i)
00054         {
00055                 // The query is the data itself but we forbide self-match.
00056                 if (indices.coeff(0, i) == i)
00057                         cerr << "Oups, something went wrong: " << indices.coeff(0, i) << " " <<  i << endl;
00058         }
00059         
00060         // Cleanup the kd-tree.
00061         delete nns;
00062         
00063         return 0;
00064 }


libnabo
Author(s): Stéphane Magnenat
autogenerated on Thu Sep 10 2015 10:54:55