42#include "../2geom/orphan-code/rtree.cpp"
52const string search_str =
"Mode: Search (Area: Click whitespace and Drag)";
53const string update_str =
"Mode: Update (Click Bounding Box (dark gray) and Drag) NOT implemented";
54const string insert_str =
"Mode: Insert (Click whitespace and Drag)" ;
55const string erase_str =
"Mode: Delete (Click on Bounding Box (dark gray))";
56const string help_str =
"'I': Insert, 'U': Update, 'S': Search, 'D': Delete";
61class RTreeToy:
public Toy
66 std::vector< RectHandle > rectangles;
73 colour color_shape, color_shape_guide;
74 colour color_select_area, color_select_area_guide;
83 std::vector< std::vector< Rect > > rects_level;
84 std::vector<colour> color_rtree_level;
85 unsigned drawBB_color;
86 bool drawBB_color_all;
95 string out_str, drawBB_str, drawBB_color_str;
99 static const int label_size = 15 ;
124 static const char* menu_items[TOTAL_ITEMS];
125 static const char keys[TOTAL_ITEMS];
129 void draw(
cairo_t *cr, std::ostringstream *notify,
int width,
int height,
bool save, std::ostringstream *timer_stream )
override {
130 cairo_set_line_width( cr, 1 );
135 for(
auto & rectangle : rectangles){
136 rectangle.draw( cr,
true );
143 dummy_draw =
Rect( starting_point, ending_point );
145 if(
mode == INSERT_MODE ){
148 else if(
mode == SEARCH_MODE ){
160 <<
"'T': Bounding Boxes: " << drawBB_str <<
", '0'-'" <<
no_of_colors <<
"', 'P': Show Layer: " << drawBB_color_str <<
"\n"
164 for(
unsigned color = 0 ; color < rects_level.size() ; color++ ){
165 if( drawBB_color == color || drawBB_color_all ){
166 for(
auto & j : rects_level){
180 (
mode == INSERT_MODE ||
mode == SEARCH_MODE ) )
183 ending_point =
Point( e->x, e->y );
190 if(
mode == INSERT_MODE ){
191 starting_point =
Point( e->x, e->y );
192 ending_point = starting_point;
195 else if(
mode == SEARCH_MODE ){
196 starting_point =
Point( e->x, e->y );
197 ending_point = starting_point;
200 else if(
mode == DELETE_MODE ) {
203 for( i = 0; i < rectangles.size(); i++) {
204 hit = rectangles[i].hit(mouse);
211 stringstream shape_id( rectangles[i].
name );
212 unsigned shape_id_int;
213 shape_id >> shape_id_int;
215 rtree.
erase( rectangles[i].pos, shape_id_int );
216 rectangles.erase( rectangles.begin() + i );
224 else if( e->button == 2 ){
226 else if( e->button == 3 ){
232 if( e->button == 1 ) {
233 if(
mode == INSERT_MODE ) {
235 ending_point =
Point( e->x, e->y );
238 std::stringstream out;
241 rectangles.push_back( t );
244 insert_in_tree_the_last_rect();
245 find_rtree_subtrees_bounding_boxes( rtree );
246 add_new_rect =
false;
249 else if(
mode == SEARCH_MODE ){
251 ending_point =
Point( e->x, e->y );
252 rect_chosen =
Rect( starting_point, ending_point );
254 std::vector< int >
result(0);
259 std::cout <<
"Search results: " <<
result.size() << std::endl;
261 std::cout << i <<
", " ;
263 std::cout << std::endl;
265 add_new_rect =
false;
268 else if(
mode == DELETE_MODE ) {
272 find_rtree_subtrees_bounding_boxes( rtree );
274 std::cout <<
" \nTree:\n" << std::endl;
276 std::cout <<
"...done\n" << std::endl;
280 else if( e->button == 2 ){
282 else if( e->button == 3 ){
287 void key_hit( GdkEventKey *e )
override
289 char choice = std::toupper( keyval );
319 drawBB_color_all =
true;
321 drawBB_color_str =
"all";
324 drawBB_color_all =
false;
326 drawBB_color_str =
"0";
329 drawBB_color_all =
false;
331 drawBB_color_str =
"1";
334 drawBB_color_all =
false;
336 drawBB_color_str =
"2";
339 drawBB_color_all =
false;
341 drawBB_color_str =
"3";
344 drawBB_color_all =
false;
346 drawBB_color_str =
"4";
349 drawBB_color_all =
false;
351 drawBB_color_str =
"5";
354 drawBB_color_all =
false;
356 drawBB_color_str =
"6";
359 drawBB_color_all =
false;
361 drawBB_color_str =
"7";
368 void insert_in_tree_the_last_rect(){
369 unsigned i = rectangles.size() - 1;
370 Rect r1 = rectangles[i].pos;
372 stringstream shape_id( rectangles[i].
name );
373 unsigned shape_id_int;
374 shape_id >> shape_id_int;
377 rtree.
insert( r1, shape_id_int );
378 std::cout <<
" \nTree:\n" << std::endl;
380 std::cout <<
"...done\n" << std::endl;
387 for(
auto & color : rects_level){
390 save_bb(
tree.root, 0);
401 Rect r1( children_node.bounding_box );
402 rects_level[ depth ].push_back( r1 );
405 save_bb( children_node.data, 0);
408 save_bb( children_node.data, depth+1);
418 RTreeToy(
unsigned rmin,
unsigned rmax,
char ):
420 color_shape(0, 0, 0, 0.9), color_shape_guide(1, 0, 0, 1),
421 color_select_area(1, 0, 0, 0.6 ), color_select_area_guide(1, 0, 0, 1 ),
422 add_new_rect(
false ), delete_rect(
false ),
423 rect_chosen(), dummy_draw(),
426 drawBB_color(9), drawBB_color_all(
true),
427 mode( INSERT_MODE ), drawBB(
true),
429 drawBB_str(
"ON"), drawBB_color_str(
"all"),
431 hit( 0 ), rect_id( 0 )
434 color_rtree_level[0] =
colour(0, 0.80, 1, 1);
435 color_rtree_level[1] =
colour(0, 0.85, 0, 1);
436 color_rtree_level[2] =
colour(0.75, 0, 0.75, 1);
437 color_rtree_level[3] =
colour(0, 0, 1, 1);
438 color_rtree_level[4] =
colour(1, 0.62, 0, 1);
439 color_rtree_level[5] =
colour(1, 0, 0.8, 1);
440 color_rtree_level[6] =
colour(0.47, 0.26, 0.12, 1);
441 color_rtree_level[7] =
colour(1, 0.90, 0, 1);
448int main(
int argc,
char **argv) {
450 char* min_arg = NULL;
451 char* max_arg = NULL;
459 static struct option long_options[] =
464 {
"min-nodes", required_argument, 0,
'n'},
465 {
"max-nodes", required_argument, 0,
'm'},
466 {
"help", no_argument, 0,
'h'},
470 int option_index = 0;
472 c = getopt_long (argc, argv,
"n:m:h",
473 long_options, &option_index);
495 std::cerr <<
"Usage: " << argv[0] <<
" options\n" << std::endl ;
497 " -n --min-nodes=NUMBER minimum number in node.\n" <<
498 " -m --max-nodes=NUMBER maximum number in node.\n" <<
499 " -h --help Print this help.\n" << std::endl;
516 if( set_min_max == 3 ){
517 stringstream s1( min_arg );
520 stringstream s2( max_arg );
522 if( rmax <= rmin || rmax < 2 || rmin < 1 ){
523 std::cerr <<
"Rtree set to 2, 3" << std::endl ;
529 std::cerr <<
"Rtree set to 2, 3 ." << std::endl ;
535 char handlefile =
'T';
536 std::cout <<
"rmin: " << rmin <<
" rmax:" << rmax << std::endl;
537 init(argc, argv,
new RTreeToy( rmin, rmax, handlefile) );
544const char* RTreeToy::menu_items[] =
546 "Insert / Alter Rectangle",
552const char RTreeToy::keys[] =
554 'I',
'U',
'S',
'D',
'T',
555 '0',
'1',
'2',
'3',
'4',
'5',
'P'
Two-dimensional point that doubles as a vector.
std::vector< RTreeRecord_NonLeaf > children_nodes
int erase(const Rect &search_area, const int shape_to_delete)
void insert(Rect const &r, unsigned shape)
void print_tree(RTreeNode *subtree_root, int depth) const
void search(const Rect &search_area, std::vector< int > *result, const RTreeNode *subtree) const
Axis aligned, non-empty rectangle.
virtual void mouse_pressed(Geom::Point const &pos, unsigned button, unsigned modifiers)
virtual void mouse_moved(Geom::Point const &pos, unsigned modifiers)
virtual void mouse_released(Geom::Point const &pos, unsigned button, unsigned modifiers)
virtual void save(FILE *f)
virtual void key_hit(unsigned keyval, unsigned modifiers)
virtual void draw(cairo_t *cr, std::ostringstream *notify, int w, int h, bool save, std::ostringstream *timing_stream)
Various utility functions.
void cairo_rectangle(cairo_t *cr, Geom::Rect const &r)
const char * program_name
Implementation of Red-Black Tree as described in Intorduction to Algorithms.
void cairo_set_source_rgba(cairo_t *cr, colour c)
void init(int argc, char **argv, Toy *t, int width=600, int height=600)