From e1895aab7609a16011df95833bf93dc2e320a5d8 Mon Sep 17 00:00:00 2001 From: Adam Wulkiewicz Date: Thu, 29 Sep 2011 22:54:22 +0000 Subject: [PATCH] added simple commands (various queries) to GLUT example. [SVN r74615] --- tests/additional_glut_vis.cpp | 231 ++++++++++++++++++++++++++-------- 1 file changed, 182 insertions(+), 49 deletions(-) diff --git a/tests/additional_glut_vis.cpp b/tests/additional_glut_vis.cpp index 40090b369..b420f1f97 100644 --- a/tests/additional_glut_vis.cpp +++ b/tests/additional_glut_vis.cpp @@ -17,13 +17,16 @@ #include #include -typedef boost::geometry::model::point P; -typedef boost::geometry::model::box

B; -//boost::geometry::index::rtree t(2, 1); +namespace bg = boost::geometry; +namespace bgi = bg::index; -boost::geometry::index::rtree< +typedef bg::model::point P; +typedef bg::model::box

B; +//bgi::rtree t(2, 1); + +bgi::rtree< B, - boost::geometry::index::rstar<4, 2> > t; + bgi::rstar<4, 2> > t; std::vector vect; size_t found_count = 0; @@ -32,8 +35,101 @@ float min_distance = 20; float max_distance = 30; size_t count = 10; std::vector nearest_boxes; +B search_box; -void draw_search_area() +enum query_mode_type { + qm_knn, qm_c, qm_d, qm_i, qm_o, qm_w, qm_nc, qm_nd, qm_ni, qm_no, qm_nw +} query_mode = qm_knn; + +bool search_valid = false; + +void knn() +{ + float x = ( rand() % 1000 ) / 10.0f; + float y = ( rand() % 1000 ) / 10.0f; + + search_point = P(x, y); + nearest_boxes.clear(); + found_count = t.nearest( + bgi::bounded( + search_point, + bgi::far(min_distance), + bgi::near(max_distance)), + count, + std::back_inserter(nearest_boxes) + ); + + if ( found_count > 0 ) + { + std::cout << "search point: "; + bgi::detail::rtree::visitors::detail::print_indexable(std::cout, search_point); + std::cout << "\nfound: "; + for ( size_t i = 0 ; i < nearest_boxes.size() ; ++i ) + { + bgi::detail::rtree::visitors::detail::print_indexable(std::cout, nearest_boxes[i]); + std::cout << '\n'; + } + } + else + std::cout << "nearest not found\n"; +} + +template +void query() +{ + float x = ( rand() % 1000 ) / 10.0f; + float y = ( rand() % 1000 ) / 10.0f; + float w = 10 + ( rand() % 1000 ) / 100.0f; + float h = 10 + ( rand() % 1000 ) / 100.0f; + + search_box = B(P(x - w, y - h), P(x + w, y + h)); + nearest_boxes.clear(); + found_count = t.query(Predicate(search_box), std::back_inserter(nearest_boxes) ); + + if ( found_count > 0 ) + { + std::cout << "search box: "; + bgi::detail::rtree::visitors::detail::print_indexable(std::cout, search_box); + std::cout << "\nfound: "; + for ( size_t i = 0 ; i < nearest_boxes.size() ; ++i ) + { + bgi::detail::rtree::visitors::detail::print_indexable(std::cout, nearest_boxes[i]); + std::cout << '\n'; + } + } + else + std::cout << "nearest not found\n"; +} + +void search() +{ + if ( query_mode == qm_knn ) + knn(); + else if ( query_mode == qm_c ) + query< bgi::detail::covered_by >(); + else if ( query_mode == qm_d ) + query< bgi::detail::disjoint >(); + else if ( query_mode == qm_i ) + query< bgi::detail::intersects >(); + else if ( query_mode == qm_o ) + query< bgi::detail::overlaps >(); + else if ( query_mode == qm_w ) + query< bgi::detail::within >(); + else if ( query_mode == qm_nc ) + query< bgi::detail::not_covered_by >(); + else if ( query_mode == qm_nd ) + query< bgi::detail::not_disjoint >(); + else if ( query_mode == qm_ni ) + query< bgi::detail::not_intersects >(); + else if ( query_mode == qm_no ) + query< bgi::detail::not_overlaps >(); + else if ( query_mode == qm_nw ) + query< bgi::detail::not_within >(); + + search_valid = true; +} + +void draw_knn_area() { float x = boost::geometry::get<0>(search_point); float y = boost::geometry::get<1>(search_point); @@ -61,16 +157,37 @@ void draw_search_area() glEnd(); } +void draw_query_area() +{ + float x1 = boost::geometry::get(search_box); + float y1 = boost::geometry::get(search_box); + float x2 = boost::geometry::get(search_box); + float y2 = boost::geometry::get(search_box); + float z = t.depth(); + + // search box + glBegin(GL_LINE_LOOP); + glVertex3f(x1, y1, z); + glVertex3f(x2, y1, z); + glVertex3f(x2, y2, z); + glVertex3f(x1, y2, z); + glEnd(); +} + void render_scene(void) { glClear(GL_COLOR_BUFFER_BIT); boost::geometry::index::gl_draw(t); - if ( found_count > 0 ) + if ( search_valid ) { glColor3f(1.0f, 0.5f, 0.0f); - draw_search_area(); + + if ( query_mode == qm_knn ) + draw_knn_area(); + else + draw_query_area(); for ( size_t i = 0 ; i < nearest_boxes.size() ; ++i ) boost::geometry::index::detail::rtree::visitors::detail::gl_draw_indexable(nearest_boxes[i], t.depth()); @@ -95,16 +212,13 @@ void resize(int w, int h) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( - 150.0f, 150.0f, 150.0f, + 120.0f, 120.0f, 120.0f, 50.0f, 50.0f, -1.0f, 0.0f, 1.0f, 0.0f); } void mouse(int button, int state, int x, int y) { - namespace bg = boost::geometry; - namespace bgi = bg::index; - if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) { float x = ( rand() % 100 ); @@ -121,10 +235,11 @@ void mouse(int button, int state, int x, int y) bgi::detail::rtree::visitors::detail::print_indexable(std::cout, b); std::cout << '\n'; - std::cout << "\n" << t << "\n"; std::cout << ( bgi::are_boxes_ok(t) ? "boxes OK\n" : "WRONG BOXES!\n" ); std::cout << ( bgi::are_levels_ok(t) ? "levels OK\n" : "WRONG LEVELS!\n" ); std::cout << "\n"; + + search_valid = false; } else if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN ) { @@ -141,63 +256,81 @@ void mouse(int button, int state, int x, int y) bgi::detail::rtree::visitors::detail::print_indexable(std::cout, b); std::cout << '\n'; - std::cout << "\n" << t << "\n"; std::cout << ( bgi::are_boxes_ok(t) ? "boxes OK\n" : "WRONG BOXES!\n" ); std::cout << ( bgi::are_levels_ok(t) ? "levels OK\n" : "WRONG LEVELS!\n" ); std::cout << "\n"; + + search_valid = false; } else if ( button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN ) { - float x = ( rand() % 1000 ) / 10.0f; - float y = ( rand() % 1000 ) / 10.0f; - - search_point = P(x, y); - nearest_boxes.clear(); - found_count = t.nearest( - bgi::bounded( - search_point, - bgi::far(min_distance), - bgi::near(max_distance)), - count, - std::back_inserter(nearest_boxes) - ); - - if ( found_count > 0 ) - { - std::cout << "search point: "; - bgi::detail::rtree::visitors::detail::print_indexable(std::cout, search_point); - std::cout << "\nfound: "; - for ( size_t i = 0 ; i < nearest_boxes.size() ; ++i ) - { - bgi::detail::rtree::visitors::detail::print_indexable(std::cout, nearest_boxes[i]); - std::cout << '\n'; - } - } - else - std::cout << "nearest not found\n"; - - std::cout << "\n" << t << "\n"; - std::cout << ( bgi::are_boxes_ok(t) ? "boxes OK\n" : "WRONG BOXES!\n" ); - std::cout << ( bgi::are_levels_ok(t) ? "levels OK\n" : "WRONG LEVELS!\n" ); - std::cout << "\n"; + search(); } glutPostRedisplay(); } +std::string current_line; + +void keyboard(unsigned char key, int x, int y) +{ + if ( key == '\r' || key == '\n' ) + { + if ( current_line == "t" ) + { + std::cout << "\n" << t << "\n"; + } + else + { + if ( current_line == "knn" ) + query_mode = qm_knn; + else if ( current_line == "c" ) + query_mode = qm_c; + else if ( current_line == "d" ) + query_mode = qm_d; + else if ( current_line == "i" ) + query_mode = qm_i; + else if ( current_line == "o" ) + query_mode = qm_o; + else if ( current_line == "w" ) + query_mode = qm_w; + else if ( current_line == "nc" ) + query_mode = qm_nc; + else if ( current_line == "nd" ) + query_mode = qm_nd; + else if ( current_line == "ni" ) + query_mode = qm_ni; + else if ( current_line == "no" ) + query_mode = qm_no; + else if ( current_line == "nw" ) + query_mode = qm_nw; + + search(); + glutPostRedisplay(); + } + + current_line.clear(); + std::cout << '\n'; + } + else + { + current_line += key; + std::cout << key; + } +} + int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(800, 600); - glutCreateWindow("Mouse click to insert new value"); + glutCreateWindow("boost::geometry::index::rtree GLUT test"); glutDisplayFunc(render_scene); glutReshapeFunc(resize); glutMouseFunc(mouse); - - std::cout << "Mouse click to insert new value"; + glutKeyboardFunc(keyboard); glutMainLoop();