-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm2.cpp
More file actions
2541 lines (2054 loc) · 88 KB
/
m2.cpp
File metadata and controls
2541 lines (2054 loc) · 88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "m1.h"
#include "m2.h"
#include "m3.h"
#include "graphics.h"
#include "SearchBar.cpp"
#include <string>
#include <sstream>
#include "Global.h"
#include "graphics_types.h"
#include <X11/keysym.h>
#include <unordered_map>
#include <vector>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <iterator>
#include <climits>
#include "OSMEntity.h"
#include "OSMNode.h"
#include "OSMWay.h"
#include "OSMRelation.h"
#include "OSMDatabaseAPI.h"
// rtree libs + initialization
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <boost/foreach.hpp>
// namespaces for rtree's
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
// rtree setup for Intersection & POI
typedef bg::model::point <double, 2, bg::cs::cartesian> point2;
typedef bg::model::box<point2> box2;
typedef std::pair <point2, unsigned> value2;
typedef bgi::rtree <value2, bgi::rstar < 8 >> rtree2;
rtree2 *g_IntersectionTree;
rtree2 *g_POITree;
// rtree setup for Features
typedef bg::model::polygon<point2, false, false> polygon;
typedef std::pair <box2, unsigned> value_polygon;
typedef bgi::rtree <value_polygon, bgi::rstar < 16, 4 >> polygonTree;
std::vector<polygon> *polygons;
polygonTree *g_PolygonTree;
// rtree setup for Street Segments
typedef std::pair <point2, unsigned> segmentValue;
typedef bgi::rtree <segmentValue, bgi::rstar < 8 >> streetSegmentTree;
streetSegmentTree *g_StreetSegmentTree;
//M1_Data *data2;
std::vector <std::vector<double>> intersection_id_to_cartesian_point; // intersectionID -> cartesian point
//double average_lat = 0;
double original_world_area = 0;
bool g_find_intersection = false;
std::vector<unsigned> intersection_id_found;
t_bound_box world_box = get_visible_world();
bool click_intersection = false;
bool click_intersection_right = false;
unsigned click_intersection_id = 0;
unsigned click_intersection_id_right = 0;
bool click_POI = false;
unsigned click_POI_id = 0;
std::vector<double> *feature_areas;
std::vector<double> *feature_lengths;
double feature_areas_median = 0; // median feature area
double feature_lengths_median = 0; // median feature length
double feature_areas_mean = 0;
double feature_lengths_mean = 0;
Surface map_marker = load_png_from_file("/nfs/ug/homes-4/k/kornyeye/ece297/work/mapper/libstreetmap/resources/Marker20x20.png");
Surface one_way_marker = load_png_from_file("/nfs/ug/homes-4/k/kornyeye/ece297/work/mapper/libstreetmap/resources/Marker20x20.png");
Surface start_marker = load_png_from_file("/nfs/ug/homes-4/k/kornyeye/ece297/work/mapper/libstreetmap/resources/end.png");
Surface end_marker = load_png_from_file("/nfs/ug/homes-4/k/kornyeye/ece297/work/mapper/libstreetmap/resources/start.png");
double speed_limit_mean = 0;
// all street segment indexes for each street type
std::unordered_map<unsigned, std::string> *segment_id_to_street_type;
std::unordered_map<OSMID, unsigned> *OSMID_way_map;
bool first_time = true;
bool is_help = false;
//-------------------------------------------------//
//---------------FUNCTION PROTOTYPES---------------//
//-------------------------------------------------//
// Callback funcs
void drawscreen(void);
void act_on_mouse_button(float, float, t_event_buttonPressed);
void act_on_keypress(char c, int keysym);
// Drawing streets/segments
void draw_curve(unsigned);
void draw_street_segment(unsigned street_segment_id);
void draw_all_streets_in_box(t_bound_box t_box);
//Drawing street names
void draw_all_street_names(double zoom_level);
void draw_street_name(unsigned street_segment_id, double zoom_level,
unsigned speed_limit, t_bound_box segment_box);
// Drawing intersections
void highlight_intersection(unsigned);
void draw_intersection(unsigned);
void draw_all_intersections();
void draw_all_intersections_in_box(t_bound_box t_box);
// Drawing features
void draw_feature(unsigned);
void draw_all_features_in_box(t_bound_box t_box);
void draw_all_features();
// Drawing POI
void draw_POI(unsigned);
void draw_all_POI();
void draw_all_POI_in_box(t_bound_box t_box);
void draw_all_POI_names(double zoom_level);
// Helper func(s)
//Converts LatLon point to Cartesian coords
//Returns vector of x, y coords
std::vector<double>to_cartesian(LatLon point_);
std::vector<double> get_polygon_area_or_length(unsigned feature_id);
LatLon to_LatLon(float x, float y);
void find_func(void (*drawscreen) (void));
void act_on_find_button_func(void (*drawscreen_ptr) (void));
void act_on_switch_button_func(void (*drawscreen_ptr) (void));
void act_on_find_path_button_func(void (*drawscreen_ptr) (void));
void act_on_help_button_func(void (*drawscreen_ptr) (void));
void fill_OSMID_way_map();
std::string type_of_highway_street(unsigned street_segment_id);
void insert_streets_into_vectors();
void draw_intersection_info(unsigned intersection_id);
void draw_POI_info(unsigned click_POI_id);
void highlight_street(std::vector<unsigned> path);
std::vector<unsigned> shortest_path;
bool path_find = false;
bool searchbar = true;
bool first_string = true;
bool second_string = false;
bool ready_to_search = false;
std::string first_name;
std::string second_name;
unsigned enter_times = 0;
unsigned first_inter_id = 0;
unsigned second_inter_id = 0;
void draw_search_bar();
void show_path();
void print_directions();
std::vector< char > search_string_first;
std::vector< char > search_string_second;
void draw_search_bar_text();
t_bound_box search_box_blank;
bool poi_path = false;
std::vector<std::string> map_paths = {
"/cad2/ece297s/public/maps/beijing_china.streets.bin",
"/cad2/ece297s/public/maps/cape-town_south-africa.streets.bin",
"/cad2/ece297s/public/maps/hamilton_canada.streets.bin",
"/cad2/ece297s/public/maps/tokyo_japan.streets.bin",
"/cad2/ece297s/public/maps/hong-kong_china.streets.bin",
"/cad2/ece297s/public/maps/interlaken_switzerland.streets.bin",
"/cad2/ece297s/public/maps/london_england.streets.bin",
"/cad2/ece297s/public/maps/moscow_russia.streets.bin",
"/cad2/ece297s/public/maps/new-dehli_india.streets.bin",
"/cad2/ece297s/public/maps/new-york_usa.streets.bin",
"/cad2/ece297s/public/maps/rio-de-janeiro_brazil.streets.bin",
"/cad2/ece297s/public/maps/singapore.streets.bin",
"/cad2/ece297s/public/maps/sydney_australia.streets.bin",
"/cad2/ece297s/public/maps/tehran_iran.streets.bin",
"/cad2/ece297s/public/maps/cairo_egypt.streets.bin",
"/cad2/ece297s/public/maps/golden-horseshoe_canada.streets.bin",
"/cad2/ece297s/public/maps/iceland.streets.bin",
"/cad2/ece297s/public/maps/saint-helena.streets.bin",
"/cad2/ece297s/public/maps/toronto_canada.streets.bin"
};
int current_map_index = map_paths.size() - 1;
//----------------------------------------------------------------//
//--------------------FUNCTION IMPLEMENTATIONS--------------------//
//----------------------------------------------------------------//
/* respond to key_pressed (could be arrow keys) */
//Loads all map information data structures
//Initializes graphics
//Closes map and OSM database when finished
void
draw_map() {
/**
@param:
@func: (1) set up OSM data for given map
(2) intialize required data structures (e.g. all rtree's)
(3) initilize many of the global vars
(4) set up graphics window
(5) clear/delete many data structs utilized
*/
std::string osm_bin_path = map_paths[current_map_index].erase(map_paths[current_map_index].size() - 11, 11);
osm_bin_path.append("osm.bin");
std::cout << osm_bin_path << '\n';
loadOSMDatabaseBIN(osm_bin_path);
//----------------------------------------------------//
//----- SETUP: average_lat & original world view -----//
//----------------------------------------------------//
double max_lat = getIntersectionPosition(0).lat(); // lat - y
double min_lat = max_lat;
double max_lon = getIntersectionPosition(0).lon(); // lon - x
double min_lon = max_lon;
for (unsigned i = 0; i < getNumberOfIntersections(); ++i) {
max_lat = std::max(max_lat, getIntersectionPosition(i).lat()); // lat
min_lat = std::min(min_lat, getIntersectionPosition(i).lat());
max_lon = std::max(max_lon, getIntersectionPosition(i).lon()); // lon
min_lon = std::min(min_lon, getIntersectionPosition(i).lon());
}
average_lat = 0.5 * (max_lat * DEG_TO_RAD + min_lat * DEG_TO_RAD);
unsigned num_of_intersections = getNumberOfIntersections();
for (unsigned i = 0; i < num_of_intersections; ++i)
intersection_id_to_cartesian_point.push_back(to_cartesian(getIntersectionPosition(i)));
//---------------------------------//
//----- SETUP: M1_Data Object -----//
//---------------------------------//
unsigned num_of_streets = getNumberOfStreets();
unsigned num_of_street_segments = getNumberOfStreetSegments();
//data2 = new M1_Data; // must be initialized here since can't initialize it before the map data has been loaded
g_IntersectionTree = new rtree2;
g_POITree = new rtree2;
g_PolygonTree = new polygonTree;
g_StreetSegmentTree = new streetSegmentTree;
segment_id_to_street_type = new std::unordered_map<unsigned, std::string>;
OSMID_way_map = new std::unordered_map<OSMID, unsigned>;
feature_areas = new std::vector<double>;
feature_lengths = new std::vector<double>;
polygons = new std::vector<polygon>;
//-----------------------------------------------------//
//--------------------SETUP: rtrees--------------------//
//-----------------------------------------------------//
//Inserts all intersection positions and IDs into rtree
for (unsigned i = 0; i < num_of_intersections; ++i) {
std::vector<double> points_xy = to_cartesian(getIntersectionPosition(i));
point2 p(points_xy[0], points_xy[1]);
g_IntersectionTree->insert(std::make_pair(p, i));
}
//Inserts all POI positions and IDs into rtree
unsigned num_of_POI = getNumberOfPointsOfInterest();
for (unsigned i = 0; i < num_of_POI; ++i) {
std::vector<double> points_xy = to_cartesian(getPointOfInterestPosition(i));
point2 p(points_xy[0], points_xy[1]);
g_POITree->insert(std::make_pair(p, i));
}
//Inserts all street segments position and IDs into rtree
for (unsigned i = 0; i < num_of_street_segments; ++i) {
//Gets info of the street segment
StreetSegmentInfo segment_info = getStreetSegmentInfo(i);
unsigned intersection_from = segment_info.from;
std::vector<double> from_xy = to_cartesian(getIntersectionPosition(intersection_from));
point2 p(from_xy[0], from_xy[1]);
g_StreetSegmentTree->insert(std::make_pair(p, i));
}
//-----------------------------------------------------------------//
//--------------------SETUP: rtree for polygons--------------------//
//-----------------------------------------------------------------//
unsigned num_of_features = getNumberOfFeatures();
feature_areas->resize(num_of_features);
feature_lengths->resize(num_of_features);
std::fill(feature_areas->begin(), feature_areas->end(), 0);
std::fill(feature_lengths->begin(), feature_lengths->end(), 0);
std::vector<double> temp_areas;
std::vector<double> temp_lengths;
//Inserts vector of points of feature locations and IDs into rtree
for (unsigned i = 0; i < num_of_features; ++i) {
unsigned num_of_points = getFeaturePointCount(i);
std::vector<LatLon> coordinates;
polygon p;
for (unsigned j = 0; j < num_of_points; ++j)
coordinates.push_back(getFeaturePoint(i, j));
for (unsigned j = 0; j < num_of_points; ++j)
p.outer().push_back(point2(to_cartesian(coordinates[j])[0],
to_cartesian(coordinates[j])[1]));
polygons->push_back(p);
//---------------//
std::vector<double> area_length = get_polygon_area_or_length(i);
// fill feature_areas & feature_lengths vectors
// feature_area/feature_lengths vector index corresponds to feature area/length
if (area_length[0] != 0) {
(*feature_areas)[i] = area_length[0];
temp_areas.push_back(area_length[0]);
} else {
(*feature_lengths)[i] = area_length[1];
temp_lengths.push_back(area_length[1]);
}
}
for (unsigned i = 0; i < polygons->size(); ++i) // i corresponds to featureID
{
box2 b = bg::return_envelope<box2>((*polygons)[i]);
g_PolygonTree->insert(std::make_pair(b, i));
}
//
//
//
unsigned temp_areas_size = temp_areas.size();
for (unsigned i = 0; i < temp_areas_size; ++i)
feature_areas_mean += temp_areas[i];
feature_areas_mean /= temp_areas_size;
unsigned temp_lengths_size = temp_lengths.size();
for (unsigned i = 0; i < temp_lengths_size; ++i)
feature_lengths_mean += temp_lengths[i];
feature_lengths_mean /= temp_lengths_size;
//Average speed limit of street segments
for (unsigned i = 0; i < num_of_street_segments; ++i) {
StreetSegmentInfo segment_info = getStreetSegmentInfo(i);
speed_limit_mean += segment_info.speedLimit;
}
speed_limit_mean = speed_limit_mean / num_of_street_segments;
//------------------------------------------------------------------------//
//-----------------------------------------------------//
//---------------INITIALIZE THE GRAPHICS---------------//
//-----------------------------------------------------//
// initialize graphics window with title "Campus Map" and background
if (first_time) {
t_color background_colour(220, 220, 220, 255);
init_graphics("Campus Maps", background_colour);
update_message("Interactive graphics");
// create "Find" button & "Switch Map" button
create_button("Window", "Find", act_on_find_button_func);
create_button("Find", "Switch", act_on_switch_button_func);
create_button("Find", "POI Path", act_on_find_path_button_func);
create_button("Find", "Help", act_on_help_button_func);
first_time = false;
}
//Finds bounds of visible map
double max_y = max_lat * DEG_TO_RAD;
double max_x = max_lon * cos(average_lat) * DEG_TO_RAD;
double min_y = min_lat * DEG_TO_RAD;
double min_x = min_lon * cos(average_lat) * DEG_TO_RAD;
// setup original world view
set_visible_world(min_x, min_y, max_x, max_y);
original_world_area = get_visible_world().area();
//
//
//
fill_OSMID_way_map();
insert_streets_into_vectors();
//
//
//
set_keypress_input(searchbar);
if (!first_time) drawscreen();
// calls <= 4 functions repeatedly in a loop (returns when user exits)
event_loop(act_on_mouse_button, NULL, act_on_keypress, drawscreen);
destroy_button("Switch");
destroy_button("Find");
destroy_button("POI Path");
destroy_button("Help");
//delete data2;
delete g_IntersectionTree;
delete g_POITree;
delete g_PolygonTree;
delete g_StreetSegmentTree;
delete segment_id_to_street_type;
delete OSMID_way_map;
delete feature_areas;
delete feature_lengths;
delete polygons;
closeOSMDatabase();
// close graphics window
close_graphics();
}
//----------------------------//
//-----CALLBACK FUNCTIONS-----//
//----------------------------//
void act_on_keypress(char c, int keysym) {
// function to handle keyboard press event, the ASCII character is returned
// along with an extended code (keysym) on X11 to represent non-ASCII
// characters like the arrow keys.
//#ifdef X11 // Extended keyboard codes only supported for X11 for now
switch (keysym) {
case XK_Left:
std::cout << "Left Arrow" << std::endl;
break;
case XK_Right:
std::cout << "Right Arrow" << std::endl;
break;
case XK_Up:
std::cout << "Up Arrow" << std::endl;
break;
case XK_Down:
std::cout << "Down Arrow" << std::endl;
break;
case XK_Return:
std::cout << "Enter button" << std::endl;
enter_times = enter_times + 1;
if (enter_times % 2 == 1) {
first_string = false;
second_string = true;
ready_to_search = false;
std::string search_text_temp(search_string_first.begin(), search_string_first.end());
first_name = search_text_temp;
search_string_first.clear();
} else {
first_string = true;
second_string = false;
ready_to_search = true;
std::string search_text_temp(search_string_second.begin(), search_string_second.end());
second_name = search_text_temp;
search_string_second.clear();
}
std::cout << enter_times << std::endl;
drawscreen();
// std::string first_temp(search_string.begin(), search_string.end());
// first_name=first_temp;
break;
case XK_Caps_Lock:
break;
case XK_Shift_L:
break;
case XK_Shift_R:
break;
/* case XK_KP_Space:
first_string=true;
second_string=false;
ready_to_search=true;
drawscreen();
break;*/
case XK_BackSpace:
if (first_string) {
search_string_first.erase(search_string_first.begin() + search_string_first.size() - 1);
}
if (second_string) {
search_string_second.erase(search_string_second.begin() + search_string_second.size() - 1);
}
drawscreen();
break;
default:
ready_to_search = false;
if (first_string) {
search_string_first.push_back(c);
for (unsigned i = 0; i < search_string_first.size(); ++i) {
std::cout << search_string_first[i];
}
}
if (second_string) {
search_string_second.push_back(c);
for (unsigned j = 0; j < search_string_second.size(); ++j) {
std::cout << search_string_second[j];
}
}
drawscreen();
std::cout << "Key press: char is " << c << std::endl;
break;
}
//#endif
}
// highlight the path when the shortest path is given as street segments id
void draw_search_bar() {
//set_coordinate_system(GL_WORLD);
setcolor(WHITE);
/*t_bound_box current(get_visible_world());
float x1, y1, x2, y2;
x1 = current.bottom_left().x;
y1 = current.get_ycenter();
x2 = current.get_xcenter();
y2 = current.top_right().y;
t_bound_box search_box1(x1, y1, x2, y2);
// unsigned y3= search_box1.get_ycenter();
t_bound_box search_box2(x1, search_box1.get_ycenter(), x2, y2);
// unsigned y4= search_box2.get_ycenter();
t_bound_box search_box3(x1, search_box2.get_ycenter(), x2, y2);
// unsigned y5= search_box3.get_ycenter();
t_bound_box search_box4(x1, search_box3.get_ycenter(), x2, y2);
fillrect(search_box4);
search_box_blank = search_box4;*/
t_point point1(0, 50); // bottom_left
t_point xy1 = scrn_to_world(point1);
t_point point2(450, 0); // top-right
t_point xy2 = scrn_to_world(point2);
t_bound_box search_bar_box(point1, point2);
// setcolor(WHITE);
fillrect(xy1.x, xy1.y, xy2.x, xy2.y);
std::cout << "finish blank search bar\n";
set_coordinate_system(GL_SCREEN);
setcolor(BLACK);
setfontsize(16);
t_bound_box current(get_visible_world());
if (first_string) {
std::string search_text(search_string_first.begin(), search_string_first.end());
drawtext(150, 30, search_text);
std::cout << "first text\n";
}// std:: cout<< search_text<< std:: endl;
else if (second_string) {
std::string search_text_second(search_string_second.begin(), search_string_second.end());
drawtext(150, 30, search_text_second);
std::cout << "second text\n";
}
set_coordinate_system(GL_WORLD);
}
void show_path() {
if (!poi_path) {
std::string street_name11;
std::string temp;
std::string street_name12;
std::string street_name21;
std::string street_name22;
std::stringstream linestream1(first_name);
std::stringstream linestream2(second_name);
// linestream1 >> street_name11;
street_name11.clear();
std::cout << "in show path\n";
bool first = true;
linestream1 >> temp;
// while not the last element
while (linestream1.tellg() != -1) {
std::cout << " iN1\n";
// linestream1 >> temp;
if (temp != "&" && first) {
if (street_name11 == "") {
street_name11 = street_name11 + temp;
linestream1 >> temp;
} else {
street_name11 = street_name11 + " " + temp;
linestream1 >> temp;
}
} else if (temp != "&" && !first) {
if (street_name12 == "") {
street_name12 = street_name12 + temp;
linestream1 >> temp;
} else {
street_name12 = street_name12 + " " + temp;
linestream1 >> temp;
}
} else {
first = false;
linestream1>> temp;
}
}
street_name12 = street_name12 + " " + temp;
temp.clear();
std::cout << " iN2\n";
first = true;
// linestream2 >> street_name21;
linestream2 >> temp;
while (linestream2.tellg() != -1) {
// linestream2 >> temp;
if (temp != "&" && first) {
if (street_name21 == "") {
street_name21 = street_name21 + temp;
linestream2 >> temp;
} else {
street_name21 = street_name21 + " " + temp;
linestream2 >> temp;
}
} else if (temp != "&" && !first) {
if (street_name22 == "") {
street_name22 = street_name22 + temp;
linestream2 >> temp;
} else {
street_name22 = street_name22 + " " + temp;
linestream2 >> temp;
}
}//when temp = &
else {
first = false;
linestream2>> temp;
}
}
street_name22 = street_name22 + " " + temp;
temp.clear();
std::cout << street_name11 << std::endl;
std::cout << street_name12 << std::endl;
std::cout << street_name21 << std::endl;
std::cout << street_name22 << std::endl;
// find the start intersection id and end intersection id
std::vector<unsigned> start_intersection = find_intersection_ids_from_street_names(street_name11, street_name12);
std::vector<unsigned> end_intersection = find_intersection_ids_from_street_names(street_name21, street_name22);
//check if there is just one path
std::cout << start_intersection.size() << std::endl;
std::cout << end_intersection.size() << std::endl;
std::vector<double> xy1 = intersection_id_to_cartesian_point[start_intersection[0]];
double right, left, top, bottom;
double x1 = xy1[0], y1 = xy1[1];
double off = 0.0002;
std::vector<double> xy2 = intersection_id_to_cartesian_point[end_intersection[0]];
double x2 = xy2[0], y2 = xy2[1];
if (x1 > x2) {
right = x1;
left = x2;
} else {
right = x2;
left = x1;
}
if (y1 > y2) {
bottom = y1;
top = y2;
} else {
bottom = y2;
top = y1;
}
set_visible_world(left - off, bottom - off, right + off, top + off);
if (start_intersection.size() == 1 && end_intersection.size() == 1) {
first_inter_id = start_intersection[0];
second_inter_id = end_intersection[0];
shortest_path = find_path_between_intersections(start_intersection[0], end_intersection[0], 0);
std::cout << "11111111111111111111111 shortest path change" << std::endl;
path_find = true;
//highlight_street(shortest_path);
drawscreen();
} else if (start_intersection.size() == 0)
std::cout << "Error: No start intersection found \n";
else if (end_intersection.size() == 0)
std::cout << "Error: No end intersection found \n";
else
std::cout << "Error: multiple path because of more than 2 intersections \n";
}
else {
std::string street_name11;
std::string temp;
std::string street_name12;
std::string poi_name;
std::stringstream linestream1(first_name);
// linestream1 >> street_name11;
street_name11.clear();
bool first = true;
linestream1 >> temp;
// while not the last element
while (linestream1.tellg() != -1) {
// linestream1 >> temp;
if (temp != "&" && first) {
if (street_name11 == "") {
street_name11 = street_name11 + temp;
linestream1 >> temp;
} else {
street_name11 = street_name11 + " " + temp;
linestream1 >> temp;
}
} else if (temp != "&" && !first) {
if (street_name12 == "") {
street_name12 = street_name12 + temp;
linestream1 >> temp;
} else {
street_name12 = street_name12 + " " + temp;
linestream1 >> temp;
}
} else {
first = false;
linestream1>> temp;
}
}
street_name12 = street_name12 + " " + temp;
temp.clear();
poi_name = second_name;
std::cout << street_name11 << std::endl;
std::cout << street_name12 << std::endl;
std::cout << poi_name << std::endl;
// find the start intersection id and end intersection id
std::vector<unsigned> start_intersection = find_intersection_ids_from_street_names(street_name11, street_name12);
std::vector<double> xy1 = intersection_id_to_cartesian_point[start_intersection[0]];
double x = xy1[0], y = xy1[1];
double off = 0.0002;
set_visible_world(x - off, y - off, x + off, y + off);
//check if there is just one path
if (start_intersection.size() == 0) {
std::cout << "Error: No start intersection found \n";
} else if (start_intersection.size() == 1) {
shortest_path = find_path_to_point_of_interest(start_intersection[0], poi_name, 0);
path_find = true;
drawscreen();
} else {
std::cout << "ERROR" << std::endl;
}
}
}
void highlight_street(std::vector<unsigned> path) {
t_color path_colour(0, 153, 204, 255);
std::cout << "in highlightstreet \n";
for (unsigned i = 0; i < path.size(); i++) {
setcolor(path_colour);
draw_street_segment(path[i]);
// print_directions();
}
}
void
draw_start_intersection(unsigned intersection_id) {
/**
@param: unique intersection id
@func: draw the intersection corresponding to @param
*/
std:: cout<<"before load image\n";
LatLon intersection_position = getIntersectionPosition(intersection_id);
std::vector<double>xy = to_cartesian(intersection_position);
double x = xy[0], y = xy[1];
std:: cout<<"in load image\n";
// since (x,y) -> top left corner of png
// shift Marker20x20.png over 10 pixels left and 20 pixels up
// so bottom of Marker points to POI
t_point world_point(x, y);
t_point screen_coords = world_to_scrn(world_point);
t_point new_screen_coords(screen_coords.x - 25 , screen_coords.y - 47);
t_point new_world_coords = scrn_to_world(new_screen_coords);
draw_surface(start_marker, new_world_coords.x, new_world_coords.y);
}
void
draw_end_intersection(unsigned intersection_id) {
/**
@param: unique intersection id
@func: draw the intersection corresponding to @param
*/
std:: cout<<"before load image\n";
LatLon intersection_position = getIntersectionPosition(intersection_id);
std::vector<double>xy = to_cartesian(intersection_position);
double x = xy[0], y = xy[1];
std:: cout<<"in load image\n";
// since (x,y) -> top left corner of png
// shift Marker20x20.png over 10 pixels left and 20 pixels up
// so bottom of Marker points to POI
t_point world_point(x, y);
t_point screen_coords = world_to_scrn(world_point);
t_point new_screen_coords(screen_coords.x - 25 , screen_coords.y - 47);
t_point new_world_coords = scrn_to_world(new_screen_coords);
draw_surface(end_marker, new_world_coords.x, new_world_coords.y);
}
void
act_on_find_path_button_func(void (*drawscreen_ptr) (void)) {
/**
@param: void (*drawscreen_ptr) (void)
@func: Gets user input and zooms to the intersection
*/
// Callback function for the new button we created. This function will be called
// when the user clicks on the button. It just counts how many
// times you have clicked the button.
poi_path = true;
std::cout << "poi set to true\n";
/*
unsigned intersect1, intersect2;
double turn_penalty;
std::string street1_intersection1, street2_intersection1;
std::string street1_intersection2, street2_intersection2;
std::cout << "Please type in the first street name for the start intersection: \n";
std::getline(std::cin, street1_intersection1);
std::cout << "Please type in the second street name for the start intersection: \n";
std::getline(std::cin, street2_intersection1);
std::cout << "Please type in the first street name for the end intersection: \n";
std::getline(std::cin, street1_intersection2);
std::cout << "Please type in the second street name for the end intersection: \n";
std::getline(std::cin, street2_intersection2);
std::cout << "Please type in turn penalty \n";
std::cin >> turn_penalty;
path_find = true;
// find the start intersection id and end intersection id
std::vector<unsigned> start_intersection = find_intersection_ids_from_street_names(street1_intersection1, street2_intersection1);
std::vector<unsigned> end_intersection = find_intersection_ids_from_street_names(street1_intersection2, street2_intersection2);
//check if there is just one path
if (start_intersection.size() == 1 && end_intersection.size() == 1) {
shortest_path = find_path_between_intersections(start_intersection[0], end_intersection[0], turn_penalty);
std::cout << "1" << std::endl;
//highlight_street(shortest_path);
drawscreen();
} else if (start_intersection.size() == 0)
std::cout << "Error: No start intersection found \n";
else if (end_intersection.size() == 0)
std::cout << "Error: No end intersection found \n";
else
std::cout << "Error: multiple path because of more than 2 intersections \n";
*/
}
void print_directions() {
t_bound_box current_box = get_visible_world();
//Calculates direction dimensions on current view
double width = current_box.right() - current_box.left();
double height = current_box.top() - current_box.bottom();
double xcenter = current_box.left() + width / 5;
double ycenter = current_box.bottom() + height * 0.9;
t_point center(xcenter, ycenter);
setcolor(BLUE);
setfontsize(12);
drawtext(center, "DIRECTIONS:", current_box);
center.offset(0, -height / 30);
std::string current_street;
std::string previous_street = "";
double street_distance = 0;
double street_segment_distance = 0;
unsigned shortest_path_size = shortest_path.size();
//Loops through all the street segments in the shortest_path vector
for (unsigned i = 0; i < shortest_path_size; i++) {
current_street = street_segment_id_to_street_name[shortest_path[i]];
std::vector<double> curve_point_lengths = street_segment_id_to_curve_point_lengths[shortest_path[i]];
//Finds distance of the current street segment
for (unsigned j = 0; j < curve_point_lengths.size(); j++){
street_segment_distance += curve_point_lengths[j];
}
street_distance += street_segment_distance;
//If we are now traveling on a new street
if (current_street != previous_street && current_street != "<unknown>") {
drawtext(center, "Turn and continue on " + current_street + " for " + std::to_string((int)round(street_distance)) + " meter(s)", current_box);
center.offset(0, -height / 30);
street_distance = 0;
}
street_segment_distance = 0;
previous_street = current_street;
}
}
void
act_on_help_button_func(void (*drawscreen_ptr) (void)) {
is_help = true;
drawscreen();
is_help = false;
}
//Draws an intersection and its information at the location
void
draw_intersection_info(unsigned intersection_id) {
/**
@param: unsigned intersection_id
@func: Gets information from location vector and draws at location
*/
/*** Make white box ***/
std::vector<double> intersection_position = intersection_id_to_cartesian_point[click_intersection_id];
std::vector<std::string> street_names;
std::string intersection_name = getIntersectionName(intersection_id);
street_names = find_intersection_street_names(click_intersection_id);
std::sort(street_names.begin(), street_names.end());
street_names.erase(std::unique(street_names.begin(), street_names.end()), street_names.end());
t_bound_box current(get_visible_world());
float width = current.get_width();
float height = current.get_height();
//make the white windowintersecetion_position.x
setcolor(WHITE);
fillrect(intersection_position[0], intersection_position[1], intersection_position[0] + width / 4, intersection_position[1] + height / 6);
//print the info for the intersection
setcolor(BLACK);
setfontsize(10);
t_point intersecetion_position(intersection_position [0], intersection_position[1]);
t_point center = t_point(intersecetion_position.x + width / 20, intersecetion_position.y + height / 7);
drawtext(center, "Intersection Name :", current);
center = t_point(intersecetion_position.x + width / 8, intersecetion_position.y + height / 9);
drawtext(center, intersection_name, current);