ann_sample.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------
00002 //              File:                   ann_sample.cpp
00003 //              Programmer:             Sunil Arya and David Mount
00004 //              Last modified:  03/04/98 (Release 0.1)
00005 //              Description:    Sample program for ANN
00006 //----------------------------------------------------------------------
00007 // Copyright (c) 1997-2005 University of Maryland and Sunil Arya and
00008 // David Mount.  All Rights Reserved.
00009 // 
00010 // This software and related documentation is part of the Approximate
00011 // Nearest Neighbor Library (ANN).  This software is provided under
00012 // the provisions of the Lesser GNU Public License (LGPL).  See the
00013 // file ../ReadMe.txt for further information.
00014 // 
00015 // The University of Maryland (U.M.) and the authors make no
00016 // representations about the suitability or fitness of this software for
00017 // any purpose.  It is provided "as is" without express or implied
00018 // warranty.
00019 //----------------------------------------------------------------------
00020 
00021 #include <cstdlib>                                              // C standard library
00022 #include <cstdio>                                               // C I/O (for sscanf)
00023 #include <cstring>                                              // string manipulation
00024 #include <fstream>                                              // file I/O
00025 #include <ANN/ANN.h>                                    // ANN declarations
00026 
00027 using namespace std;                                    // make std:: accessible
00028 
00029 //----------------------------------------------------------------------
00030 // ann_sample
00031 //
00032 // This is a simple sample program for the ANN library.  After compiling,
00033 // it can be run as follows.
00034 // 
00035 // ann_sample [-d dim] [-max mpts] [-nn k] [-e eps] [-df data] [-qf query]
00036 //
00037 // where
00038 //              dim                             is the dimension of the space (default = 2)
00039 //              mpts                    maximum number of data points (default = 1000)
00040 //              k                               number of nearest neighbors per query (default 1)
00041 //              eps                             is the error bound (default = 0.0)
00042 //              data                    file containing data points
00043 //              query                   file containing query points
00044 //
00045 // Results are sent to the standard output.
00046 //----------------------------------------------------------------------
00047 
00048 //----------------------------------------------------------------------
00049 //      Parameters that are set in getArgs()
00050 //----------------------------------------------------------------------
00051 void getArgs(int argc, char **argv);                    // get command-line arguments
00052 
00053 int                             k                               = 1;                    // number of nearest neighbors
00054 int                             dim                             = 2;                    // dimension
00055 double                  eps                             = 0;                    // error bound
00056 int                             maxPts                  = 1000;                 // maximum number of data points
00057 
00058 istream*                dataIn                  = NULL;                 // input for data points
00059 istream*                queryIn                 = NULL;                 // input for query points
00060 
00061 bool readPt(istream &in, ANNpoint p)                    // read point (false on EOF)
00062 {
00063         for (int i = 0; i < dim; i++) {
00064                 if(!(in >> p[i])) return false;
00065         }
00066         return true;
00067 }
00068 
00069 void printPt(ostream &out, ANNpoint p)                  // print point
00070 {
00071         out << "(" << p[0];
00072         for (int i = 1; i < dim; i++) {
00073                 out << ", " << p[i];
00074         }
00075         out << ")\n";
00076 }
00077 
00078 int main(int argc, char **argv)
00079 {
00080         int                                     nPts;                                   // actual number of data points
00081         ANNpointArray           dataPts;                                // data points
00082         ANNpoint                        queryPt;                                // query point
00083         ANNidxArray                     nnIdx;                                  // near neighbor indices
00084         ANNdistArray            dists;                                  // near neighbor distances
00085         ANNkd_tree*                     kdTree;                                 // search structure
00086 
00087         getArgs(argc, argv);                                            // read command-line arguments
00088 
00089         queryPt = annAllocPt(dim);                                      // allocate query point
00090         dataPts = annAllocPts(maxPts, dim);                     // allocate data points
00091         nnIdx = new ANNidx[k];                                          // allocate near neigh indices
00092         dists = new ANNdist[k];                                         // allocate near neighbor dists
00093 
00094         nPts = 0;                                                                       // read data points
00095 
00096         cout << "Data Points:\n";
00097         while (nPts < maxPts && readPt(*dataIn, dataPts[nPts])) {
00098                 printPt(cout, dataPts[nPts]);
00099                 nPts++;
00100         }
00101 
00102         kdTree = new ANNkd_tree(                                        // build search structure
00103                                         dataPts,                                        // the data points
00104                                         nPts,                                           // number of points
00105                                         dim);                                           // dimension of space
00106 
00107         while (readPt(*queryIn, queryPt)) {                     // read query points
00108                 cout << "Query point: ";                                // echo query point
00109                 printPt(cout, queryPt);
00110 
00111                 kdTree->annkSearch(                                             // search
00112                                 queryPt,                                                // query point
00113                                 k,                                                              // number of near neighbors
00114                                 nnIdx,                                                  // nearest neighbors (returned)
00115                                 dists,                                                  // distance (returned)
00116                                 eps);                                                   // error bound
00117 
00118                 cout << "\tNN:\tIndex\tDistance\n";
00119                 for (int i = 0; i < k; i++) {                   // print summary
00120                         dists[i] = sqrt(dists[i]);                      // unsquare distance
00121                         cout << "\t" << i << "\t" << nnIdx[i] << "\t" << dists[i] << "\n";
00122                 }
00123         }
00124     delete [] nnIdx;                                                    // clean things up
00125     delete [] dists;
00126     delete kdTree;
00127         annClose();                                                                     // done with ANN
00128 
00129         return EXIT_SUCCESS;
00130 }
00131 
00132 //----------------------------------------------------------------------
00133 //      getArgs - get command line arguments
00134 //----------------------------------------------------------------------
00135 
00136 void getArgs(int argc, char **argv)
00137 {
00138         static ifstream dataStream;                                     // data file stream
00139         static ifstream queryStream;                            // query file stream
00140 
00141         if (argc <= 1) {                                                        // no arguments
00142                 cerr << "Usage:\n\n"
00143                 << "  ann_sample [-d dim] [-max m] [-nn k] [-e eps] [-df data]"
00144                    " [-qf query]\n\n"
00145                 << "  where:\n"
00146                 << "    dim      dimension of the space (default = 2)\n"
00147                 << "    m        maximum number of data points (default = 1000)\n"
00148                 << "    k        number of nearest neighbors per query (default 1)\n"
00149                 << "    eps      the error bound (default = 0.0)\n"
00150                 << "    data     name of file containing data points\n"
00151                 << "    query    name of file containing query points\n\n"
00152                 << " Results are sent to the standard output.\n"
00153                 << "\n"
00154                 << " To run this demo use:\n"
00155                 << "    ann_sample -df data.pts -qf query.pts\n";
00156                 exit(0);
00157         }
00158         int i = 1;
00159         while (i < argc) {                                                      // read arguments
00160                 if (!strcmp(argv[i], "-d")) {                   // -d option
00161                         dim = atoi(argv[++i]);                          // get dimension to dump
00162                 }
00163                 else if (!strcmp(argv[i], "-max")) {    // -max option
00164                         maxPts = atoi(argv[++i]);                       // get max number of points
00165                 }
00166                 else if (!strcmp(argv[i], "-nn")) {             // -nn option
00167                         k = atoi(argv[++i]);                            // get number of near neighbors
00168                 }
00169                 else if (!strcmp(argv[i], "-e")) {              // -e option
00170                         sscanf(argv[++i], "%lf", &eps);         // get error bound
00171                 }
00172                 else if (!strcmp(argv[i], "-df")) {             // -df option
00173                         dataStream.open(argv[++i], ios::in);// open data file
00174                         if (!dataStream) {
00175                                 cerr << "Cannot open data file\n";
00176                                 exit(1);
00177                         }
00178                         dataIn = &dataStream;                           // make this the data stream
00179                 }
00180                 else if (!strcmp(argv[i], "-qf")) {             // -qf option
00181                         queryStream.open(argv[++i], ios::in);// open query file
00182                         if (!queryStream) {
00183                                 cerr << "Cannot open query file\n";
00184                                 exit(1);
00185                         }
00186                         queryIn = &queryStream;                 // make this query stream
00187                 }
00188                 else {                                                                  // illegal syntax
00189                         cerr << "Unrecognized option.\n";
00190                         exit(1);
00191                 }
00192                 i++;
00193         }
00194         if (dataIn == NULL || queryIn == NULL) {
00195                 cerr << "-df and -qf options must be specified\n";
00196                 exit(1);
00197         }
00198 }

Generated on Wed Nov 23 18:59:54 2011 for FreeCAD by  doxygen 1.6.1