-
Notifications
You must be signed in to change notification settings - Fork 886
Expand file tree
/
Copy pathmaneuversbuilder.cc
More file actions
4100 lines (3634 loc) · 168 KB
/
maneuversbuilder.cc
File metadata and controls
4100 lines (3634 loc) · 168 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 "odin/maneuversbuilder.h"
#include "baldr/graphconstants.h"
#include "baldr/streetname.h"
#include "baldr/streetnames.h"
#include "baldr/streetnames_factory.h"
#include "baldr/turn.h"
#include "baldr/turnlanes.h"
#include "baldr/verbal_text_formatter_factory.h"
#include "exceptions.h"
#include "midgard/logging.h"
#include "midgard/util.h"
#include "odin/sign.h"
#include "odin/signs.h"
#include "odin/util.h"
#include "proto/directions.pb.h"
#include "proto/options.pb.h"
#ifdef LOGGING_LEVEL_DEBUG
#include "midgard/encoded.h"
#include "midgard/pointll.h"
#endif
#include <boost/format.hpp>
#include <algorithm>
#include <cstdint>
#include <iterator>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
using namespace valhalla::midgard;
using namespace valhalla::baldr;
using namespace valhalla::odin;
namespace {
constexpr uint32_t kRelativeStraightTurnDegreeLowerBound = 330;
constexpr uint32_t kRelativeStraightTurnDegreeUpperBound = 30;
constexpr uint32_t kTurnChannelTurnDegreeLowerBound = 290;
constexpr uint32_t kTurnChannelTurnDegreeUpperBound = 70;
constexpr float kShortTurnChannelThreshold = 0.036f; // Kilometers
constexpr float kShortForkThreshold = 0.05f; // Kilometers
// Kilometers - picked since the next rounded maneuver announcement will happen
// in a quarter mile or 400 meters
constexpr float kShortContinueThreshold = 0.6f;
// Maximum number of edges to look for matching overlay
constexpr uint32_t kOverlaySignBoardEdgeMax = 5;
constexpr float kUpcomingLanesThreshold = 3.f; // Kilometers
// Small end ramp fork threshold in kilometers
constexpr float kSmallEndRampForkThreshold = 0.125f;
// Thresholds for succinct phrase usage
constexpr uint32_t kMaxWordCount = 5;
constexpr uint32_t kMaxStreetNameLength = 25;
std::vector<std::string> split(const std::string& source, char delimiter) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(source);
while (std::getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
bool is_pair(const std::vector<std::string>& tokens) {
return (tokens.size() == 2);
}
bool has_level_changes(
const ::google::protobuf::RepeatedPtrField<valhalla::TripLeg_Edge_Level>& levels) {
return levels.size() == 1 ? levels[0].start() != levels[0].end() : levels.size() != 0;
}
} // namespace
namespace valhalla {
namespace odin {
ManeuversBuilder::ManeuversBuilder(const Options& options, EnhancedTripLeg* etp)
: options_(options), trip_path_(etp) {
}
std::list<Maneuver> ManeuversBuilder::Build() {
// Create the maneuvers
std::list<Maneuver> maneuvers = Produce();
#ifdef LOGGING_LEVEL_TRACE
int man_id = 1;
LOG_TRACE("############################################");
LOG_TRACE("INITIAL MANEUVERS");
for (const Maneuver& maneuver : maneuvers) {
LOG_TRACE("---------------------------------------------");
LOG_TRACE(std::to_string(man_id++) + ": ");
LOG_TRACE(std::string(" maneuver_PARAMETERS=") + maneuver.ToParameterString());
LOG_TRACE(std::string(" maneuver=") + maneuver.ToString());
}
#endif
// Combine maneuvers
Combine(maneuvers);
// Calculate the consecutive exit sign count and then sort
CountAndSortSigns(maneuvers);
// Confirm maneuver type assignment
ConfirmManeuverTypeAssignment(maneuvers);
// Mark the maneuvers that have traversable outbound intersecting edges
SetTraversableOutboundIntersectingEdgeFlags(maneuvers);
// Process roundabouts
ProcessRoundabouts(maneuvers);
// Process the 'to stay on' attribute
SetToStayOnAttribute(maneuvers);
// Enhance signless interchanges
EnhanceSignlessInterchnages(maneuvers);
// Process the guidance view junctions and signboards
ProcessGuidanceViews(maneuvers);
// Update the maneuver placement for internal intersection turns
UpdateManeuverPlacementForInternalIntersectionTurns(maneuvers);
// Collapse small end ramp fork maneuvers to reduce verbose instructions
// Must happen after updating maneuver placement for internal edges
CollapseSmallEndRampFork(maneuvers);
// Collapse merge maneuvers to reduce obvious instructions
CollapseMergeManeuvers(maneuvers);
// Process the turn lanes. Must happen after updating maneuver placement for internal edges so we
// activate the correct lanes.
ProcessTurnLanes(maneuvers);
// Add landmarks to maneuvers as direction guidance support
// Each maneuver should get the landmarks associated with edges in the previous maneuver
AddLandmarksFromTripLegToManeuvers(maneuvers);
ProcessVerbalSuccinctTransitionInstruction(maneuvers);
#ifdef LOGGING_LEVEL_TRACE
int final_man_id = 1;
LOG_TRACE("############################################");
LOG_TRACE("FINAL MANEUVERS");
for (const Maneuver& maneuver : maneuvers) {
LOG_TRACE("---------------------------------------------");
LOG_TRACE(std::to_string(final_man_id++) + ": ");
LOG_TRACE(std::string(" maneuver_PARAMETERS=") + maneuver.ToParameterString());
LOG_TRACE(std::string(" maneuver=") + maneuver.ToString());
}
#endif
#ifdef LOGGING_LEVEL_DEBUG
std::vector<PointLL> shape = midgard::decode<std::vector<PointLL>>(trip_path_->shape());
// Shape by index
// int i = 0;
// for (PointLL ll : shape) {
// LOG_TRACE(std::string("shape lng/lat[") + std::to_string(i++) + "]=" +
// std::to_string(ll.lng()) + "," + std::to_string(ll.lat()));
// }
// Shape by lat/lon pairs
// std::string shape_json("\"shape\":[");
// for (PointLL ll : shape) {
// shape_json += "{\"lat\":" + std::to_string(ll.lat()) + ",\"lon\":" +
// std::to_string(ll.lng()) + "},";
// }
// shape_json.pop_back();
// shape_json += "]";
// LOG_TRACE(shape_json);
if (shape.empty() || (trip_path_->node_size() < 2))
throw valhalla_exception_t{213};
const auto& orig = trip_path_->GetOrigin();
const auto& dest = trip_path_->GetDestination();
std::string first_name = (trip_path_->GetCurrEdge(0)->name_size() == 0)
? ""
: trip_path_->GetCurrEdge(0)->name(0).value();
auto last_node_index = (trip_path_->node_size() - 2);
std::string last_name = (trip_path_->GetCurrEdge(last_node_index)->name_size() == 0)
? ""
: trip_path_->GetCurrEdge(last_node_index)->name(0).value();
std::string units = options_.units() == valhalla::Options::kilometers ? "kilometers" : "miles";
LOG_DEBUG((boost::format("ROUTE_REQUEST|-j "
"'{\"locations\":[{\"lat\":%1$.6f,\"lon\":%2$.6f,\"street\":\"%3%\"},{"
"\"lat\":%4$.6f,\"lon\":%5$.6f,\"street\":\"%6%\"}],\"costing\":"
"\"auto\",\"units\":\"%7%\"}'") %
orig.ll().lat() % orig.ll().lng() % first_name % dest.ll().lat() % dest.ll().lng() %
last_name % units)
.str());
#endif
return maneuvers;
}
std::list<Maneuver> ManeuversBuilder::Produce() {
std::list<Maneuver> maneuvers;
// Validate trip path node list
if (trip_path_->node_size() < 1) {
throw valhalla_exception_t{210};
}
// Check for a single node
if (trip_path_->node_size() == 1) {
// TODO - handle origin and destination are the same
throw valhalla_exception_t{211};
}
// Validate location count
if (trip_path_->location_size() < 2) {
throw valhalla_exception_t{212};
}
// Process the Destination maneuver
maneuvers.emplace_front();
CreateDestinationManeuver(maneuvers.front());
// TODO - handle no edges
// Initialize maneuver prior to loop
maneuvers.emplace_front();
InitializeManeuver(maneuvers.front(), trip_path_->GetLastNodeIndex());
#ifdef LOGGING_LEVEL_TRACE
LOG_TRACE("=============================================");
LOG_TRACE(std::string("osm_changeset=") + std::to_string(trip_path_->osm_changeset()));
#endif
// Step through nodes in reverse order to produce maneuvers
// excluding the last and first nodes
for (int i = (trip_path_->GetLastNodeIndex() - 1); i > 0; --i) {
auto node = trip_path_->GetEnhancedNode(i);
#ifdef LOGGING_LEVEL_TRACE
auto prev_edge = trip_path_->GetPrevEdge(i);
auto curr_edge = trip_path_->GetCurrEdge(i);
auto next_edge = trip_path_->GetNextEdge(i);
auto prev2curr_turn_degree = GetTurnDegree(prev_edge->end_heading(), curr_edge->begin_heading());
LOG_TRACE("---------------------------------------------");
LOG_TRACE(std::to_string(i) + ": ");
LOG_TRACE(std::string(" curr_edge_PARAMETERS=") +
(curr_edge ? curr_edge->ToParameterString() : "NONE"));
LOG_TRACE(std::string(" curr_edge=") + (curr_edge ? curr_edge->ToString() : "NONE"));
LOG_TRACE(std::string(" prev2curr_turn_degree=") + std::to_string(prev2curr_turn_degree) +
" is a " + Turn::GetTypeString(Turn::GetType(prev2curr_turn_degree)));
for (int z = 0; z < node->intersecting_edge_size(); ++z) {
auto intersecting_edge = node->GetIntersectingEdge(z);
auto xturn_degree = GetTurnDegree(prev_edge->end_heading(), intersecting_edge->begin_heading());
LOG_TRACE(std::string(" intersectingEdge=") + intersecting_edge->ToString());
LOG_TRACE(std::string(" prev2int_turn_degree=") + std::to_string(xturn_degree) + " is a " +
Turn::GetTypeString(Turn::GetType(xturn_degree)));
}
LOG_TRACE(std::string(" node=") + node->ToString());
IntersectingEdgeCounts xedge_counts;
node->CalculateRightLeftIntersectingEdgeCounts(prev_edge->end_heading(), prev_edge->travel_mode(),
xedge_counts);
LOG_TRACE(std::string(" right=") + std::to_string(xedge_counts.right) +
std::string(" | right_similar=") + std::to_string(xedge_counts.right_similar) +
std::string(" | right_traversable_outbound=") +
std::to_string(xedge_counts.right_traversable_outbound) +
std::string(" | right_similar_traversable_outbound=") +
std::to_string(xedge_counts.right_similar_traversable_outbound));
LOG_TRACE(std::string(" left =") + std::to_string(xedge_counts.left) +
std::string(" | left_similar =") + std::to_string(xedge_counts.left_similar) +
std::string(" | left_traversable_outbound =") +
std::to_string(xedge_counts.left_traversable_outbound) +
std::string(" | left_similar_traversable_outbound =") +
std::to_string(xedge_counts.left_similar_traversable_outbound));
#endif
if (trip_path_->GetCurrEdge(i)->pedestrian_type() == PedestrianType::kBlind) {
switch (node->type()) {
case TripLeg_Node_Type_kStreetIntersection: {
std::vector<std::pair<std::string, bool>> name_list;
for (int z = 0; z < node->intersecting_edge_size(); ++z) {
auto intersecting_edge = node->GetIntersectingEdge(z);
for (const auto& name : intersecting_edge->name()) {
std::pair<std::string, bool> cur_street = {name.value(), name.is_route_number()};
if (std::find(name_list.begin(), name_list.end(), cur_street) == name_list.end())
name_list.push_back(cur_street);
}
}
if (!name_list.empty()) {
maneuvers.front().set_cross_street_names(name_list);
maneuvers.front().set_node_type(node->type());
if (node->traffic_signal())
maneuvers.front().set_traffic_signal(true);
}
break;
}
case TripLeg_Node_Type_kGate:
case TripLeg_Node_Type_kBollard:
maneuvers.front().set_node_type(node->type());
break;
default:
break;
}
}
if (CanManeuverIncludePrevEdge(maneuvers.front(), i)) {
UpdateManeuver(maneuvers.front(), i);
} else {
// Finalize current maneuver
FinalizeManeuver(maneuvers.front(), i);
// Initialize new maneuver
maneuvers.emplace_front();
InitializeManeuver(maneuvers.front(), i);
}
}
#ifdef LOGGING_LEVEL_TRACE
auto curr_edge = trip_path_->GetCurrEdge(0);
LOG_TRACE("---------------------------------------------");
LOG_TRACE(std::string("0") + ": ");
LOG_TRACE(std::string(" curr_edge_PARAMETERS=") +
(curr_edge ? curr_edge->ToParameterString() : "NONE"));
LOG_TRACE(std::string(" curr_edge=") + (curr_edge ? curr_edge->ToString() : "NONE"));
auto node = trip_path_->GetEnhancedNode(0);
for (int z = 0; z < node->intersecting_edge_size(); ++z) {
auto intersecting_edge = node->GetIntersectingEdge(z);
LOG_TRACE(std::string(" intersectingEdge=") + intersecting_edge->ToString());
}
LOG_TRACE("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
for (int z = 0; z < trip_path_->admin_size(); ++z) {
auto admin = trip_path_->GetAdmin(z);
LOG_TRACE("ADMIN " + std::to_string(z) + ": " + admin->ToString());
}
#endif
// Process the Start maneuver
CreateStartManeuver(maneuvers.front());
return maneuvers;
}
void ManeuversBuilder::Combine(std::list<Maneuver>& maneuvers) {
bool maneuvers_have_been_combined = true;
// Continue trying to combine maneuvers until no maneuvers have been combined
while (maneuvers_have_been_combined) {
maneuvers_have_been_combined = false;
auto prev_man = maneuvers.begin();
auto curr_man = maneuvers.begin();
auto next_man = maneuvers.begin();
if (next_man != maneuvers.end()) {
++next_man;
}
while (next_man != maneuvers.end()) {
// Process common base names
std::unique_ptr<StreetNames> common_base_names =
curr_man->street_names().FindCommonBaseNames(next_man->street_names());
// Get the begin edge of the next maneuver
auto next_man_begin_edge = trip_path_->GetCurrEdge(next_man->begin_node_index());
bool is_first_man = (curr_man == maneuvers.begin());
LOG_TRACE("+++ Combine TOP ++++++++++++++++++++++++++++++++++++++++++++");
// Collapse the TransitConnectionStart Maneuver
// if the transit connection stop is a simple stop (not a station)
if ((curr_man->type() == DirectionsLeg_Maneuver_Type_kTransitConnectionStart) &&
next_man->IsTransit() &&
curr_man->transit_connection_platform_info().type() == TransitPlatformInfo_Type_kStop) {
LOG_TRACE("+++ Combine: Collapse the TransitConnectionStart Maneuver +++");
curr_man = CollapseTransitConnectionStartManeuver(maneuvers, curr_man, next_man);
maneuvers_have_been_combined = true;
++next_man;
}
// Collapse the TransitConnectionDestination Maneuver
// if the transit connection stop is a simple stop (not a station)
else if ((next_man->type() == DirectionsLeg_Maneuver_Type_kTransitConnectionDestination) &&
curr_man->IsTransit() &&
next_man->transit_connection_platform_info().type() ==
TransitPlatformInfo_Type_kStop) {
LOG_TRACE("+++ Combine: Collapse the TransitConnectionDestination Maneuver +++");
next_man = CollapseTransitConnectionDestinationManeuver(maneuvers, curr_man, next_man);
maneuvers_have_been_combined = true;
}
// Do not combine
// if current or next maneuver is an elevator
else if (curr_man->elevator() || next_man->elevator()) {
LOG_TRACE("+++ Do Not Combine: if current or next maneuver is an elevator +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if current or next maneuver is indoor steps
else if (curr_man->indoor_steps() || next_man->indoor_steps()) {
LOG_TRACE("+++ Do Not Combine: if current or next maneuver is indoor steps +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if current or next maneuver is an escalator
else if (curr_man->escalator() || next_man->escalator()) {
LOG_TRACE("+++ Do Not Combine: if current or next maneuver is an escalator +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
} else if (curr_man->has_level_changes() != next_man->has_level_changes()) {
LOG_TRACE("+++ Do Not Combine: if only one maneuver has level changes +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if current or next maneuver is a building entrance
else if (curr_man->building_enter() || next_man->building_enter() ||
curr_man->building_exit() || next_man->building_exit()) {
LOG_TRACE("+++ Do Not Combine: if current or next maneuver is a building entrance +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if any transit connection maneuvers
else if (curr_man->transit_connection() || next_man->transit_connection()) {
LOG_TRACE("+++ Do Not Combine: if any transit connection maneuvers +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if driving side is different
else if (curr_man->drive_on_right() != next_man->drive_on_right()) {
LOG_TRACE("+++ Do Not Combine: if driving side is different +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if travel mode is different
// OR next maneuver is destination
else if ((curr_man->travel_mode() != next_man->travel_mode()) ||
(next_man->type() == DirectionsLeg_Maneuver_Type_kDestination)) {
LOG_TRACE(
"+++ Do Not Combine: if travel mode is different or next maneuver is destination +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Combine current left unspecified internal maneuver with next left maneuver
else if (PossibleUnspecifiedInternalManeuver(prev_man, curr_man, next_man) &&
prev_man->HasSimilarNames(&(*next_man), true) &&
(ManeuversBuilder::DetermineRelativeDirection(curr_man->turn_degree()) ==
Maneuver::RelativeDirection::kLeft) &&
(ManeuversBuilder::DetermineRelativeDirection(next_man->turn_degree()) ==
Maneuver::RelativeDirection::kLeft) &&
(ManeuversBuilder::DetermineRelativeDirection(
GetTurnDegree(prev_man->end_heading(), next_man->begin_heading())) ==
Maneuver::RelativeDirection::KReverse)) {
LOG_TRACE(
"+++ Combine: double L turns with short unspecified internal intersection as ManeuverType=UTURN_LEFT +++");
curr_man = CombineUnspecifiedInternalManeuver(maneuvers, prev_man, curr_man, next_man,
DirectionsLeg_Maneuver_Type_kUturnLeft);
maneuvers_have_been_combined = true;
++next_man;
}
// Combine current right unspecified internal maneuver with next right maneuver
else if (PossibleUnspecifiedInternalManeuver(prev_man, curr_man, next_man) &&
prev_man->HasSimilarNames(&(*next_man), true) &&
(ManeuversBuilder::DetermineRelativeDirection(curr_man->turn_degree()) ==
Maneuver::RelativeDirection::kRight) &&
(ManeuversBuilder::DetermineRelativeDirection(next_man->turn_degree()) ==
Maneuver::RelativeDirection::kRight) &&
(ManeuversBuilder::DetermineRelativeDirection(
GetTurnDegree(prev_man->end_heading(), next_man->begin_heading())) ==
Maneuver::RelativeDirection::KReverse)) {
LOG_TRACE(
"+++ Combine: double R turns with short unspecified internal intersection as ManeuverType=UTURN_RIGHT +++");
curr_man = CombineUnspecifiedInternalManeuver(maneuvers, prev_man, curr_man, next_man,
DirectionsLeg_Maneuver_Type_kUturnRight);
maneuvers_have_been_combined = true;
++next_man;
}
// Do not combine
// if next maneuver is a fork or a tee
else if (next_man->fork() || next_man->tee()) {
LOG_TRACE("+++ Do Not Combine: if next maneuver is a fork or a tee +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if current or next maneuver is a ferry
else if (curr_man->ferry() || next_man->ferry()) {
LOG_TRACE("+++ Do Not Combine: if current or next maneuver is a ferry +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Combine current internal maneuver with next maneuver
else if (curr_man->internal_intersection() && (curr_man != next_man) &&
!next_man->IsDestinationType()) {
LOG_TRACE("+++ Combine: current internal maneuver with next maneuver +++");
curr_man = CombineInternalManeuver(maneuvers, prev_man, curr_man, next_man, is_first_man);
if (is_first_man) {
prev_man = curr_man;
}
maneuvers_have_been_combined = true;
++next_man;
}
// Combine current turn channel maneuver with next maneuver
else if (IsTurnChannelManeuverCombinable(prev_man, curr_man, next_man, is_first_man)) {
LOG_TRACE("+++ Combine: current turn channel maneuver with next maneuver +++");
curr_man = CombineTurnChannelManeuver(maneuvers, prev_man, curr_man, next_man, is_first_man);
if (is_first_man) {
prev_man = curr_man;
}
maneuvers_have_been_combined = true;
++next_man;
}
// Do not combine
// if next maneuver has an intersecting forward link
else if (next_man->intersecting_forward_edge()) {
LOG_TRACE("+++ Do Not Combine: if next maneuver has an intersecting forward link +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if has node_type
else if ((curr_man->pedestrian_type() == PedestrianType::kBlind &&
next_man->pedestrian_type() == PedestrianType::kBlind) &&
(curr_man->has_node_type() || next_man->has_node_type() || curr_man->is_steps() ||
next_man->is_steps() || curr_man->is_bridge() || next_man->is_bridge() ||
curr_man->is_tunnel() || next_man->is_tunnel())) {
LOG_TRACE("+++ Do Not Combine: if has node type+++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// Do not combine
// if trail type is different (unnamed/named pedestrian/bike/mtb)
else if (curr_man->trail_type() != next_man->trail_type()) {
LOG_TRACE("+++ Do Not Combine: if trail type is different +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
++next_man;
}
// NOTE: Logic may have to be adjusted depending on testing
// Maybe not intersecting forward link
// Maybe first edge in next is internal
// Maybe no signs
// Combine the 'same name straight' next maneuver with the current maneuver
// if begin edge of next maneuver is not a turn channel
// and the next maneuver is not an internal intersection maneuver
// and the current maneuver is not a ramp
// and the next maneuver is not a ramp
// and current and next maneuvers have a common base name
else if ((next_man->begin_relative_direction() == Maneuver::RelativeDirection::kKeepStraight) &&
(next_man_begin_edge && !next_man_begin_edge->IsTurnChannelUse()) &&
!next_man->internal_intersection() && !curr_man->ramp() && !next_man->ramp() &&
!curr_man->roundabout() && !next_man->roundabout() && !common_base_names->empty()) {
LOG_TRACE("+++ Combine: Several factors +++");
// If needed, set the begin street names
if (!curr_man->HasBeginStreetNames() && !curr_man->portions_highway() &&
(curr_man->street_names().size() > common_base_names->size())) {
curr_man->set_begin_street_names(curr_man->street_names().clone());
}
// Update current maneuver street names
curr_man->set_street_names(std::move(common_base_names));
next_man = CombineManeuvers(maneuvers, curr_man, next_man);
maneuvers_have_been_combined = true;
}
// Combine unnamed straight maneuvers
else if ((next_man->begin_relative_direction() == Maneuver::RelativeDirection::kKeepStraight) &&
!curr_man->HasStreetNames() && !next_man->HasStreetNames() && !curr_man->IsTransit() &&
!next_man->IsTransit() &&
(next_man_begin_edge && !next_man_begin_edge->IsTurnChannelUse()) &&
!next_man->internal_intersection() && !curr_man->ramp() && !next_man->ramp() &&
!curr_man->roundabout() && !next_man->roundabout()) {
LOG_TRACE("+++ Combine: unnamed straight maneuvers +++");
next_man = CombineManeuvers(maneuvers, curr_man, next_man);
maneuvers_have_been_combined = true;
}
// Combine ramp maneuvers
else if (AreRampManeuversCombinable(curr_man, next_man)) {
LOG_TRACE("+++ Combine: ramp maneuvers +++");
next_man = CombineManeuvers(maneuvers, curr_man, next_man);
maneuvers_have_been_combined = true;
}
// Combine obvious maneuver
else if (IsNextManeuverObvious(maneuvers, curr_man, next_man)) {
// If current maneuver does not have street names then use the next maneuver street names
if (!curr_man->HasStreetNames() && next_man->HasStreetNames()) {
curr_man->set_street_names(next_man->street_names().clone());
}
// Mark that the current maneuver contains an obvious maneuver
curr_man->set_contains_obvious_maneuver(true);
// Disable turn channel
curr_man->set_turn_channel(false);
LOG_TRACE("+++ Combine: obvious maneuver +++");
next_man = CombineManeuvers(maneuvers, curr_man, next_man);
maneuvers_have_been_combined = true;
}
// Combine current short length non-internal edges (left or right) with next maneuver that is a
// kRampStraight NOTE: This should already be marked internal for OSM data so shouldn't happen
// for OSM
else if (PossibleUnspecifiedInternalManeuver(prev_man, curr_man, next_man) &&
((ManeuversBuilder::DetermineRelativeDirection(curr_man->turn_degree()) ==
Maneuver::RelativeDirection::kLeft) ||
(ManeuversBuilder::DetermineRelativeDirection(curr_man->turn_degree()) ==
Maneuver::RelativeDirection::kRight)) &&
next_man->type() == DirectionsLeg_Maneuver_Type_kRampStraight) {
LOG_TRACE(
"+++ Combine: current non-internal turn that is very short in length with the next straight ramp maneuver +++");
curr_man = CombineUnspecifiedInternalManeuver(maneuvers, prev_man, curr_man, next_man,
DirectionsLeg_Maneuver_Type_kNone);
maneuvers_have_been_combined = true;
++next_man;
} else {
LOG_TRACE("+++ Do Not Combine +++");
// Update with no combine
prev_man = curr_man;
curr_man = next_man;
assert(next_man != maneuvers.end());
++next_man;
}
LOG_TRACE("+++ Combine BOTTOM +++++++++++++++++++++++++++++++++++++++++");
}
}
}
std::list<Maneuver>::iterator
ManeuversBuilder::CollapseTransitConnectionStartManeuver(std::list<Maneuver>& maneuvers,
std::list<Maneuver>::iterator curr_man,
std::list<Maneuver>::iterator next_man) {
// Set begin node index
next_man->set_begin_node_index(curr_man->begin_node_index());
// Set begin shape index
next_man->set_begin_shape_index(curr_man->begin_shape_index());
return maneuvers.erase(curr_man);
}
std::list<Maneuver>::iterator ManeuversBuilder::CollapseTransitConnectionDestinationManeuver(
std::list<Maneuver>& maneuvers,
std::list<Maneuver>::iterator curr_man,
std::list<Maneuver>::iterator next_man) {
// Set end node index
curr_man->set_end_node_index(next_man->end_node_index());
// Set end shape index
curr_man->set_end_shape_index(next_man->end_shape_index());
return maneuvers.erase(next_man);
}
bool ManeuversBuilder::PossibleUnspecifiedInternalManeuver(std::list<Maneuver>::iterator prev_man,
std::list<Maneuver>::iterator curr_man,
std::list<Maneuver>::iterator next_man) {
if (!curr_man->internal_intersection() && curr_man->travel_mode() == TravelMode::kDrive &&
!prev_man->roundabout() && !curr_man->roundabout() && !next_man->roundabout() &&
(curr_man->length(Options::kilometers) <= (kMaxInternalLength * kKmPerMeter)) &&
curr_man != next_man && !curr_man->IsStartType() && !next_man->IsDestinationType()) {
return true;
}
return false;
}
// Collapses unspecified internal edge maneuvers
// TODO: Future refactor to pull out common code
std::list<Maneuver>::iterator ManeuversBuilder::CombineUnspecifiedInternalManeuver(
std::list<Maneuver>& maneuvers,
std::list<Maneuver>::iterator prev_man,
std::list<Maneuver>::iterator curr_man,
std::list<Maneuver>::iterator next_man,
const DirectionsLeg_Maneuver_Type& maneuver_type) {
// Determine turn degree based on previous maneuver and next maneuver
next_man->set_turn_degree(GetTurnDegree(prev_man->end_heading(), next_man->begin_heading()));
// Set the cross street names
if (curr_man->HasStreetNames()) {
next_man->set_cross_street_names(curr_man->street_names().clone());
}
// Set relative direction
next_man->set_begin_relative_direction(
ManeuversBuilder::DetermineRelativeDirection(next_man->turn_degree()));
// Add distance
next_man->set_length(next_man->length() + curr_man->length());
// Add time
next_man->set_time(next_man->time() + curr_man->time());
// Add basic time
next_man->set_basic_time(next_man->basic_time() + curr_man->basic_time());
// TODO - heading?
// Set begin node index
next_man->set_begin_node_index(curr_man->begin_node_index());
// Set begin shape index
next_man->set_begin_shape_index(curr_man->begin_shape_index());
// Set maneuver type to specified argument
next_man->set_type(maneuver_type);
return maneuvers.erase(curr_man);
}
std::list<Maneuver>::iterator
ManeuversBuilder::CombineInternalManeuver(std::list<Maneuver>& maneuvers,
std::list<Maneuver>::iterator prev_man,
std::list<Maneuver>::iterator curr_man,
std::list<Maneuver>::iterator next_man,
bool start_man) {
if (start_man) {
// Determine turn degree current maneuver and next maneuver
next_man->set_turn_degree(GetTurnDegree(curr_man->end_heading(), next_man->begin_heading()));
} else {
// Determine turn degree based on previous maneuver and next maneuver
next_man->set_turn_degree(GetTurnDegree(prev_man->end_heading(), next_man->begin_heading()));
}
// Set the cross street names
if (curr_man->HasUsableInternalIntersectionName()) {
next_man->set_cross_street_names(curr_man->street_names().clone());
}
// Set the right and left internal turn counts
next_man->set_internal_right_turn_count(curr_man->internal_right_turn_count());
next_man->set_internal_left_turn_count(curr_man->internal_left_turn_count());
// Set relative direction
next_man->set_begin_relative_direction(
ManeuversBuilder::DetermineRelativeDirection(next_man->turn_degree()));
// If the relative direction is straight
// and both internal left and right turns exist
// then update the relative direction
if ((next_man->begin_relative_direction() == Maneuver::RelativeDirection::kKeepStraight) &&
(curr_man->internal_left_turn_count() > 0) && (curr_man->internal_right_turn_count() > 0)) {
LOG_TRACE("both left and right internal turn counts are > 0");
next_man->set_begin_relative_direction(ManeuversBuilder::DetermineRelativeDirection(
GetTurnDegree(prev_man->end_heading(), curr_man->end_heading())));
}
// Add distance
next_man->set_length(next_man->length() + curr_man->length());
// Add time
next_man->set_time(next_man->time() + curr_man->time());
// Add basic time
next_man->set_basic_time(next_man->basic_time() + curr_man->basic_time());
// TODO - heading?
// Set begin node index
next_man->set_begin_node_index(curr_man->begin_node_index());
// Set begin shape index
next_man->set_begin_shape_index(curr_man->begin_shape_index());
// NOTE: Do not copy signs from internal maneuver
// It would produce invalid results
if (start_man) {
next_man->set_type(DirectionsLeg_Maneuver_Type_kStart);
} else {
// Set maneuver type to 'none' so the type will be processed again
next_man->set_type(DirectionsLeg_Maneuver_Type_kNone);
SetManeuverType(*(next_man));
}
return maneuvers.erase(curr_man);
}
std::list<Maneuver>::iterator
ManeuversBuilder::CombineTurnChannelManeuver(std::list<Maneuver>& maneuvers,
std::list<Maneuver>::iterator prev_man,
std::list<Maneuver>::iterator curr_man,
std::list<Maneuver>::iterator next_man,
bool start_man) {
if (start_man) {
// Determine turn degree current maneuver and next maneuver
next_man->set_turn_degree(GetTurnDegree(curr_man->end_heading(), next_man->begin_heading()));
} else {
// Determine turn degree based on previous maneuver and next maneuver
next_man->set_turn_degree(GetTurnDegree(prev_man->end_heading(), next_man->begin_heading()));
}
// Set relative direction
next_man->set_begin_relative_direction(curr_man->begin_relative_direction());
// Add distance
next_man->set_length(next_man->length() + curr_man->length());
// Add time
next_man->set_time(next_man->time() + curr_man->time());
// Add basic time
next_man->set_basic_time(next_man->basic_time() + curr_man->basic_time());
// TODO - heading?
// Set begin node index
next_man->set_begin_node_index(curr_man->begin_node_index());
// Set begin shape index
next_man->set_begin_shape_index(curr_man->begin_shape_index());
// Set signs, if needed
if (curr_man->HasSigns() && !next_man->HasSigns()) {
*(next_man->mutable_signs()) = curr_man->signs();
}
if (start_man) {
next_man->set_type(DirectionsLeg_Maneuver_Type_kStart);
} else {
// Set maneuver type to 'none' so the type will be processed again
next_man->set_type(DirectionsLeg_Maneuver_Type_kNone);
SetManeuverType(*(next_man));
}
return maneuvers.erase(curr_man);
}
std::list<Maneuver>::iterator
ManeuversBuilder::CombineManeuvers(std::list<Maneuver>& maneuvers,
std::list<Maneuver>::iterator curr_man,
std::list<Maneuver>::iterator next_man) {
// Add distance
curr_man->set_length(curr_man->length() + next_man->length());
// Add time
curr_man->set_time(curr_man->time() + next_man->time());
// Add basic time
curr_man->set_basic_time(curr_man->basic_time() + next_man->basic_time());
// Update end heading
curr_man->set_end_heading(next_man->end_heading());
// Update end node index
curr_man->set_end_node_index(next_man->end_node_index());
// Update end shape index
curr_man->set_end_shape_index(next_man->end_shape_index());
// Update end level
curr_man->set_end_level_ref(next_man->end_level_ref());
// If needed, set elevator
if (next_man->elevator()) {
curr_man->set_elevator(true);
}
// If needed, set indoor steps
if (next_man->indoor_steps()) {
curr_man->set_indoor_steps(true);
}
// If needed, set steps
if (next_man->is_steps()) {
curr_man->set_steps(true);
}
// If needed, set escalator
if (next_man->escalator()) {
curr_man->set_escalator(true);
}
if (next_man->has_level_changes()) {
curr_man->set_has_level_changes(true);
}
// If needed, set ramp
if (next_man->ramp()) {
curr_man->set_ramp(true);
}
// If needed, set ferry
if (next_man->ferry()) {
curr_man->set_ferry(true);
}
// If needed, set rail_ferry
if (next_man->rail_ferry()) {
curr_man->set_rail_ferry(true);
}
// If needed, set roundabout
if (next_man->roundabout()) {
curr_man->set_roundabout(true);
}
// If needed, set portions_toll
if (next_man->portions_toll()) {
curr_man->set_portions_toll(true);
}
if (next_man->has_time_restrictions()) {
curr_man->set_has_time_restrictions(true);
}
// If needed, set portions_unpaved
if (next_man->portions_unpaved()) {
curr_man->set_portions_unpaved(true);
}
// If needed, set portions_highway
if (next_man->portions_highway()) {
curr_man->set_portions_highway(true);
}
// If needed, set contains_obvious_maneuver
if (next_man->contains_obvious_maneuver()) {
curr_man->set_contains_obvious_maneuver(true);
}
return maneuvers.erase(next_man);
}
void ManeuversBuilder::CountAndSortSigns(std::list<Maneuver>& maneuvers) {
auto prev_man = maneuvers.rbegin();
auto curr_man = maneuvers.rbegin();
if (prev_man != maneuvers.rend()) {
++prev_man;
}
// Rank the exit signs
while (prev_man != maneuvers.rend()) {
// Increase the branch exit sign consecutive count
// if it matches the succeeding named maneuver
if (prev_man->HasExitBranchSign() && !curr_man->HasExitSign() && curr_man->HasStreetNames()) {
for (Sign& sign : *(prev_man->mutable_signs()->mutable_exit_branch_list())) {
for (const auto& street_name : curr_man->street_names()) {
if (sign.text() == street_name->value()) {
sign.set_consecutive_count(sign.consecutive_count() + 1);
}
}
}
Signs::Sort(prev_man->mutable_signs()->mutable_exit_branch_list());
}
// Increase the branch guide sign consecutive count
// if it matches the succeeding named maneuver
else if (prev_man->HasGuideBranchSign() && !curr_man->HasGuideSign() &&
curr_man->HasStreetNames()) {
for (Sign& sign : *(prev_man->mutable_signs()->mutable_guide_branch_list())) {
for (const auto& street_name : curr_man->street_names()) {
if (sign.text() == street_name->value()) {
sign.set_consecutive_count(sign.consecutive_count() + 1);
}
}
}
Signs::Sort(prev_man->mutable_signs()->mutable_guide_branch_list());
}