ann_sample.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <cstdlib>
00022 #include <cstdio>
00023 #include <cstring>
00024 #include <fstream>
00025 #include <ANN/ANN.h>
00026
00027 using namespace std;
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 void getArgs(int argc, char **argv);
00052
00053 int k = 1;
00054 int dim = 2;
00055 double eps = 0;
00056 int maxPts = 1000;
00057
00058 istream* dataIn = NULL;
00059 istream* queryIn = NULL;
00060
00061 bool readPt(istream &in, ANNpoint p)
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)
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;
00081 ANNpointArray dataPts;
00082 ANNpoint queryPt;
00083 ANNidxArray nnIdx;
00084 ANNdistArray dists;
00085 ANNkd_tree* kdTree;
00086
00087 getArgs(argc, argv);
00088
00089 queryPt = annAllocPt(dim);
00090 dataPts = annAllocPts(maxPts, dim);
00091 nnIdx = new ANNidx[k];
00092 dists = new ANNdist[k];
00093
00094 nPts = 0;
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(
00103 dataPts,
00104 nPts,
00105 dim);
00106
00107 while (readPt(*queryIn, queryPt)) {
00108 cout << "Query point: ";
00109 printPt(cout, queryPt);
00110
00111 kdTree->annkSearch(
00112 queryPt,
00113 k,
00114 nnIdx,
00115 dists,
00116 eps);
00117
00118 cout << "\tNN:\tIndex\tDistance\n";
00119 for (int i = 0; i < k; i++) {
00120 dists[i] = sqrt(dists[i]);
00121 cout << "\t" << i << "\t" << nnIdx[i] << "\t" << dists[i] << "\n";
00122 }
00123 }
00124 delete [] nnIdx;
00125 delete [] dists;
00126 delete kdTree;
00127 annClose();
00128
00129 return EXIT_SUCCESS;
00130 }
00131
00132
00133
00134
00135
00136 void getArgs(int argc, char **argv)
00137 {
00138 static ifstream dataStream;
00139 static ifstream queryStream;
00140
00141 if (argc <= 1) {
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) {
00160 if (!strcmp(argv[i], "-d")) {
00161 dim = atoi(argv[++i]);
00162 }
00163 else if (!strcmp(argv[i], "-max")) {
00164 maxPts = atoi(argv[++i]);
00165 }
00166 else if (!strcmp(argv[i], "-nn")) {
00167 k = atoi(argv[++i]);
00168 }
00169 else if (!strcmp(argv[i], "-e")) {
00170 sscanf(argv[++i], "%lf", &eps);
00171 }
00172 else if (!strcmp(argv[i], "-df")) {
00173 dataStream.open(argv[++i], ios::in);
00174 if (!dataStream) {
00175 cerr << "Cannot open data file\n";
00176 exit(1);
00177 }
00178 dataIn = &dataStream;
00179 }
00180 else if (!strcmp(argv[i], "-qf")) {
00181 queryStream.open(argv[++i], ios::in);
00182 if (!queryStream) {
00183 cerr << "Cannot open query file\n";
00184 exit(1);
00185 }
00186 queryIn = &queryStream;
00187 }
00188 else {
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 }