//test.cc /* * Contributed to libgist by : * Dr. Thomas Hamelryck Vrije Universiteit Brussel (VUB) * Intitute for Molecular Biology ULTR Department * Paardestraat 65 1640 Sint-Gensius-Rode, Belgium * http://ultr16.vub.ac.be/~thomas * * November 2001 */ #include using namespace std; #include "gist.h" #include "gist_extensions.h" #include "gist_defs.h" //#include "gist_compat.h" #include "gist_cursor.h" #include "gist_cursorext.h" #include "gist_srtree.h" #include "gist_rtreecext.h" #include "gist_rtpred_point.h" #include "gist_rtpred_rect.h" int main() { gist sr_index; // create a new index file, using the given extension // (here an rtree, specified by &rt_point_ext) sr_index.create("rtree-file", &sr_point_ext); // two coordinates & matching labels double coord1[]={50.0, 60.0}; int label1=1; double coord2[]={10.0, 20.0}; int label2=2; // insert above two coordinates & labels in the rtree sr_index.insert((void *) coord1, sizeof(coord1), (void *) &label1, sizeof(label1)); sr_index.insert((void *) coord2, sizeof(coord2), (void *) &label2, sizeof(label2)); // flush the tree sr_index.flush(); // specify the query // next 3 lines specify a rectangular search area //double rect1[] = {5.0, 15.0, 15.0, 25.0}; //rt_rect r(2, rect1); //rt_query_t q(rt_query_t::rt_overlap, rt_query_t::rt_rectarg, &r); // next 3 lines specify a nearest neighbor search double c[]={10, 40}; // This segfaults on exit for some reason // rt_point p(2, c) // Next line seems to work rt_point *p=new rt_point(2, c); rt_query_t q(rt_query_t::rt_nearest, rt_query_t::rt_pointarg, p); // create a cursor (or iterator) based on the specified query gist_cursor_t cursor; // Max 100 neighbors int k=100; if(sr_index.fetch_init(cursor, &q, k)!=RCOK) { cerr << "Can't initialize cursor." << endl; return(eERROR); } // now do the lookup itself bool eof=false; // found coordinates & corresponding labels will be put in these variables char label[gist_p::max_tup_sz]; char coord[gist_p::max_tup_sz]; // used to specify the size of coordinates & labels smsize_t coord_len, label_len; while(!eof) { coord_len=gist_p::max_tup_sz; label_len=gist_p::max_tup_sz; // get Nth nearest coordinate & its label until done (eof) if(sr_index.fetch(cursor, (void *) coord, coord_len, (void *) &label, label_len, eof)!=RCOK) { cerr << "Can't fetch from cursor." << endl; return(eERROR); } if(!eof) { // process key and data.. double x, y; int i; // cast char data back to coordinates & labels x=*((double *) coord); y=*(((double *) coord)+1); i=*((int *) label); // print what we have found cout << "Found coordinate (" << x << "," << y << ") "; cout << "with label " << i << "." << endl; } else { // That's it! cout << "Done.\n"; sr_index.close(); // clean up query point delete p; return 0; } } }