Advertisement
Guest User

ANN-Sample

a guest
Aug 16th, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <fstream>
  5. #include <ANN/ANN.h>
  6.  
  7. using namespace std;
  8. void getArgs(int argc, char **argv);
  9. int k = 1;
  10. int dim = 2;
  11. double eps = 0;
  12. int maxPts = 1000;
  13.  
  14. istream* dataIn = NULL;
  15. istream* queryIn = NULL;
  16.  
  17. bool readPt(istream &in, ANNpoint p)
  18. {
  19.  for (int i = 0; i < dim; i++) {
  20.  if(!(in >> p[i])) return false;
  21.  }
  22.  return true;
  23. }
  24.  
  25. void printPt(ostream &out, ANNpoint p)
  26. {
  27.  out << "(" << p[0];
  28.  for (int i = 1; i < dim; i++) {
  29.  out << ", " << p[i];
  30.  }
  31.  out << ")\n";
  32. }
  33.  
  34. int main(int argc, char **argv)
  35. {
  36.  int nPts;
  37.  ANNpointArray dataPts;
  38.  ANNpoint queryPt;
  39.  ANNidxArray nnIdx;
  40.  ANNdistArray dists;
  41.  ANNkd_tree* kdTree;
  42.  
  43.  getArgs(argc, argv);
  44.  
  45.  queryPt = annAllocPt(dim);
  46.  dataPts = annAllocPts(maxPts, dim);
  47.  nnIdx = new ANNidx[k];
  48.  dists = new ANNdist[k];
  49.  
  50.  nPts = 0;
  51.  
  52.  cout << "Data Points:\n";
  53.  while (nPts < maxPts && readPt(*dataIn, dataPts[nPts])) {
  54.  printPt(cout, dataPts[nPts]);
  55.  nPts++;
  56.  }
  57.  
  58.  kdTree = new ANNkd_tree(
  59.  dataPts,
  60.  nPts,
  61.  dim);
  62.  
  63.  while (readPt(*queryIn, queryPt)) {
  64.  cout << "Query point: ";
  65.  printPt(cout, queryPt);
  66.  
  67.  kdTree->annkSearch(
  68.  queryPt,
  69.  k,
  70.  nnIdx,
  71.  dists,
  72.  eps);
  73.  
  74.  cout << "\tNN:\tIndex\tDistance\n";
  75.  for (int i = 0; i < k; i++) {
  76.  dists[i] = sqrt(dists[i]);
  77.  cout << "\t" << i << "\t" << nnIdx[i] << "\t" << dists[i] << "\n";
  78.  }
  79.  }
  80.  delete [] nnIdx;
  81.  delete [] dists;
  82.  delete kdTree;
  83.  annClose();
  84.  
  85.  return EXIT_SUCCESS;
  86. }void getArgs(int argc, char **argv)
  87. {
  88.  static ifstream dataStream;
  89.  static ifstream queryStream;
  90.  
  91.  if (argc <= 1) {
  92.  cerr << "Usage:\n\n"
  93.  << " ann_sample [-d dim] [-max m] [-nn k] [-e eps] [-df data]"
  94.  " [-qf query]\n\n"
  95.  << " where:\n"
  96.  << " dim dimension of the space (default = 2)\n"
  97.  << " m maximum number of data points (default = 1000)\n"
  98.  << " k number of nearest neighbors per query (default 1)\n"
  99.  << " eps the error bound (default = 0.0)\n"
  100.  << " data name of file containing data points\n"
  101.  << " query name of file containing query points\n\n"
  102.  << " Results are sent to the standard output.\n"
  103.  << "\n"
  104.  << " To run this demo use:\n"
  105.  << " ann_sample -df data.pts -qf query.pts\n";
  106.  exit(0);
  107.  }
  108.  int i = 1;
  109.  while (i < argc) {
  110.  if (!strcmp(argv[i], "-d")) {
  111.  dim = atoi(argv[++i]);
  112.  }
  113.  else if (!strcmp(argv[i], "-max")) {
  114.  maxPts = atoi(argv[++i]);
  115.  }
  116.  else if (!strcmp(argv[i], "-nn")) {
  117.  k = atoi(argv[++i]);
  118.  }
  119.  else if (!strcmp(argv[i], "-e")) {
  120.  sscanf(argv[++i], "%lf", &eps);
  121.  }
  122.  else if (!strcmp(argv[i], "-df")) {
  123.  dataStream.open(argv[++i], ios::in);
  124.  if (!dataStream) {
  125.  cerr << "Cannot open data file\n";
  126.  exit(1);
  127.  }
  128.  dataIn = &dataStream;
  129.  }
  130.  else if (!strcmp(argv[i], "-qf")) {
  131.  queryStream.open(argv[++i], ios::in);
  132.  if (!queryStream) {
  133.  cerr << "Cannot open query file\n";
  134.  exit(1);
  135.  }
  136.  queryIn = &queryStream;
  137.  }
  138.  else {
  139.  cerr << "Unrecognized option.\n";
  140.  exit(1);
  141.  }
  142.  i++;
  143.  }
  144.  if (dataIn == NULL || queryIn == NULL) {
  145.  cerr << "-df and -qf options must be specified\n";
  146.  exit(1);
  147.  }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement