Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cstdio>
- #include <cstring>
- #include <fstream>
- #include <ANN/ANN.h>
- using namespace std;
- void getArgs(int argc, char **argv);
- int k = 1;
- int dim = 2;
- double eps = 0;
- int maxPts = 1000;
- istream* dataIn = NULL;
- istream* queryIn = NULL;
- bool readPt(istream &in, ANNpoint p)
- {
- for (int i = 0; i < dim; i++) {
- if(!(in >> p[i])) return false;
- }
- return true;
- }
- void printPt(ostream &out, ANNpoint p)
- {
- out << "(" << p[0];
- for (int i = 1; i < dim; i++) {
- out << ", " << p[i];
- }
- out << ")\n";
- }
- int main(int argc, char **argv)
- {
- int nPts;
- ANNpointArray dataPts;
- ANNpoint queryPt;
- ANNidxArray nnIdx;
- ANNdistArray dists;
- ANNkd_tree* kdTree;
- getArgs(argc, argv);
- queryPt = annAllocPt(dim);
- dataPts = annAllocPts(maxPts, dim);
- nnIdx = new ANNidx[k];
- dists = new ANNdist[k];
- nPts = 0;
- cout << "Data Points:\n";
- while (nPts < maxPts && readPt(*dataIn, dataPts[nPts])) {
- printPt(cout, dataPts[nPts]);
- nPts++;
- }
- kdTree = new ANNkd_tree(
- dataPts,
- nPts,
- dim);
- while (readPt(*queryIn, queryPt)) {
- cout << "Query point: ";
- printPt(cout, queryPt);
- kdTree->annkSearch(
- queryPt,
- k,
- nnIdx,
- dists,
- eps);
- cout << "\tNN:\tIndex\tDistance\n";
- for (int i = 0; i < k; i++) {
- dists[i] = sqrt(dists[i]);
- cout << "\t" << i << "\t" << nnIdx[i] << "\t" << dists[i] << "\n";
- }
- }
- delete [] nnIdx;
- delete [] dists;
- delete kdTree;
- annClose();
- return EXIT_SUCCESS;
- }void getArgs(int argc, char **argv)
- {
- static ifstream dataStream;
- static ifstream queryStream;
- if (argc <= 1) {
- cerr << "Usage:\n\n"
- << " ann_sample [-d dim] [-max m] [-nn k] [-e eps] [-df data]"
- " [-qf query]\n\n"
- << " where:\n"
- << " dim dimension of the space (default = 2)\n"
- << " m maximum number of data points (default = 1000)\n"
- << " k number of nearest neighbors per query (default 1)\n"
- << " eps the error bound (default = 0.0)\n"
- << " data name of file containing data points\n"
- << " query name of file containing query points\n\n"
- << " Results are sent to the standard output.\n"
- << "\n"
- << " To run this demo use:\n"
- << " ann_sample -df data.pts -qf query.pts\n";
- exit(0);
- }
- int i = 1;
- while (i < argc) {
- if (!strcmp(argv[i], "-d")) {
- dim = atoi(argv[++i]);
- }
- else if (!strcmp(argv[i], "-max")) {
- maxPts = atoi(argv[++i]);
- }
- else if (!strcmp(argv[i], "-nn")) {
- k = atoi(argv[++i]);
- }
- else if (!strcmp(argv[i], "-e")) {
- sscanf(argv[++i], "%lf", &eps);
- }
- else if (!strcmp(argv[i], "-df")) {
- dataStream.open(argv[++i], ios::in);
- if (!dataStream) {
- cerr << "Cannot open data file\n";
- exit(1);
- }
- dataIn = &dataStream;
- }
- else if (!strcmp(argv[i], "-qf")) {
- queryStream.open(argv[++i], ios::in);
- if (!queryStream) {
- cerr << "Cannot open query file\n";
- exit(1);
- }
- queryIn = &queryStream;
- }
- else {
- cerr << "Unrecognized option.\n";
- exit(1);
- }
- i++;
- }
- if (dataIn == NULL || queryIn == NULL) {
- cerr << "-df and -qf options must be specified\n";
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement