@@ -2864,7 +2864,14 @@ lanelet::ConstLanelets extendNextLane(
2864
2864
// Add next lane
2865
2865
const auto next_lanes = route_handler->getNextLanelets (extended_lanes.back ());
2866
2866
if (!next_lanes.empty ()) {
2867
- extended_lanes.push_back (next_lanes.front ());
2867
+ // use the next lane in route if it exists
2868
+ auto target_next_lane = next_lanes.front ();
2869
+ for (const auto & next_lane : next_lanes) {
2870
+ if (route_handler->isRouteLanelet (next_lane)) {
2871
+ target_next_lane = next_lane;
2872
+ }
2873
+ }
2874
+ extended_lanes.push_back (target_next_lane);
2868
2875
}
2869
2876
2870
2877
return extended_lanes;
@@ -2878,9 +2885,15 @@ lanelet::ConstLanelets extendPrevLane(
2878
2885
// Add previous lane
2879
2886
const auto prev_lanes = route_handler->getPreviousLanelets (extended_lanes.front ());
2880
2887
if (!prev_lanes.empty ()) {
2881
- extended_lanes.insert (extended_lanes.begin (), prev_lanes.front ());
2888
+ // use the previous lane in route if it exists
2889
+ auto target_prev_lane = prev_lanes.front ();
2890
+ for (const auto & prev_lane : prev_lanes) {
2891
+ if (route_handler->isRouteLanelet (prev_lane)) {
2892
+ target_prev_lane = prev_lane;
2893
+ }
2894
+ }
2895
+ extended_lanes.insert (extended_lanes.begin (), target_prev_lane);
2882
2896
}
2883
-
2884
2897
return extended_lanes;
2885
2898
}
2886
2899
@@ -2895,26 +2908,24 @@ lanelet::ConstLanelets extendLanes(
2895
2908
2896
2909
lanelet::ConstLanelets getExtendedCurrentLanes (
2897
2910
const std::shared_ptr<const PlannerData> & planner_data, const double backward_length,
2898
- const double forward_length)
2911
+ const double forward_length, const bool until_goal_lane )
2899
2912
{
2900
2913
auto lanes = getCurrentLanes (planner_data);
2901
2914
if (lanes.empty ()) return lanes;
2915
+ const auto start_lane = lanes.front ();
2902
2916
2903
2917
double forward_length_sum = 0.0 ;
2904
2918
double backward_length_sum = 0.0 ;
2905
2919
2906
- const auto is_loop = [&](const auto & target_lane) {
2907
- auto it = std::find_if (lanes.begin (), lanes.end (), [&](const lanelet::ConstLanelet & lane) {
2908
- return lane.id () == target_lane.id ();
2909
- });
2910
-
2911
- return it != lanes.end ();
2912
- };
2913
-
2914
2920
while (backward_length_sum < backward_length) {
2915
2921
auto extended_lanes = extendPrevLane (planner_data->route_handler , lanes);
2916
-
2917
- if (extended_lanes.empty () || is_loop (extended_lanes.front ())) {
2922
+ if (extended_lanes.empty ()) {
2923
+ return lanes;
2924
+ }
2925
+ // loop check
2926
+ // if current map lanes is looping and has a very large value for backward_length,
2927
+ // the extending process will not finish.
2928
+ if (extended_lanes.front ().id () == start_lane.id ()) {
2918
2929
return lanes;
2919
2930
}
2920
2931
@@ -2927,9 +2938,25 @@ lanelet::ConstLanelets getExtendedCurrentLanes(
2927
2938
}
2928
2939
2929
2940
while (forward_length_sum < forward_length) {
2930
- auto extended_lanes = extendNextLane (planner_data->route_handler , lanes);
2941
+ // stop extending if the goal lane is included
2942
+ // if forward_length is a very large value, set it to true,
2943
+ // as it may continue to extend lanes outside the route ahead of goal forever.
2944
+ if (until_goal_lane) {
2945
+ lanelet::ConstLanelet goal_lane;
2946
+ planner_data->route_handler ->getGoalLanelet (&goal_lane);
2947
+ if (lanes.back ().id () == goal_lane.id ()) {
2948
+ return lanes;
2949
+ }
2950
+ }
2931
2951
2932
- if (extended_lanes.empty () || is_loop (extended_lanes.back ())) {
2952
+ auto extended_lanes = extendNextLane (planner_data->route_handler , lanes);
2953
+ if (extended_lanes.empty ()) {
2954
+ return lanes;
2955
+ }
2956
+ // loop check
2957
+ // if current map lanes is looping and has a very large value for forward_length,
2958
+ // the extending process will not finish.
2959
+ if (extended_lanes.back ().id () == start_lane.id ()) {
2933
2960
return lanes;
2934
2961
}
2935
2962
0 commit comments