Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(static_obstacle_avoidance): remove redundant calculation related to lanelet functions #9355

Merged
merged 4 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <utility>
Expand Down Expand Up @@ -594,6 +595,13 @@ struct AvoidancePlanningData

double to_return_point{std::numeric_limits<double>::max()};

std::optional<double> distance_to_red_traffic_light{std::nullopt};

bool has_closest_lanelet{false};
lanelet::ConstLanelet closest_lanelet{};
satoshi-ota marked this conversation as resolved.
Show resolved Hide resolved

bool is_allowed_goal_modification{false};

bool request_operator{false};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,16 @@ class AvoidanceHelper
: std::max(shift_length, getRightShiftBound());
}

double getForwardDetectionRange() const
double getForwardDetectionRange(
const bool has_closest_lanelet, const lanelet::ConstLanelet & closest_lane) const
{
if (parameters_->use_static_detection_area) {
return parameters_->object_check_max_forward_distance;
}

const auto & route_handler = data_->route_handler;

lanelet::ConstLanelet closest_lane;
if (!route_handler->getClosestLaneletWithinRoute(getEgoPose(), &closest_lane)) {
if (!has_closest_lanelet) {
return parameters_->object_check_max_forward_distance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ double calcShiftLength(
const bool & is_object_on_right, const double & overhang_dist, const double & avoid_margin);

bool isWithinLanes(
const lanelet::ConstLanelets & lanelets, const std::shared_ptr<const PlannerData> & planner_data);
const bool has_closest_lanelet, const lanelet::ConstLanelet & closest_lanelet,
const std::shared_ptr<const PlannerData> & planner_data);

/**
* @brief check if the ego has to shift driving position.
Expand Down Expand Up @@ -261,12 +262,12 @@ DrivableLanes generateExpandedDrivableLanes(
double calcDistanceToReturnDeadLine(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters);
const std::shared_ptr<AvoidanceParameters> & parameters,
const std::optional<double> distance_to_red_traffic, const bool is_allowed_goal_modification);

double calcDistanceToAvoidStartLine(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters);
const lanelet::ConstLanelets & lanelets, const std::shared_ptr<AvoidanceParameters> & parameters,
const std::optional<double> distance_to_red_traffic);

/**
* @brief calculate error eclipse radius based on object pose covariance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 TIER IV, Inc.

Check notice on line 1 in planning/behavior_path_planner/autoware_behavior_path_static_obstacle_avoidance_module/src/scene.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1421 to 1427, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -210,86 +210,95 @@
data.extend_lanelets = utils::static_obstacle_avoidance::getExtendLanes(
data.current_lanelets, getEgoPose(), planner_data_);

data.has_closest_lanelet =
planner_data_->route_handler->getClosestLaneletWithinRoute(getEgoPose(), &data.closest_lanelet);

// expand drivable lanes
const auto is_within_current_lane =
utils::static_obstacle_avoidance::isWithinLanes(data.current_lanelets, planner_data_);
const auto is_within_current_lane = utils::static_obstacle_avoidance::isWithinLanes(
data.has_closest_lanelet, data.closest_lanelet, planner_data_);
const auto red_signal_lane_itr = std::find_if(
data.current_lanelets.begin(), data.current_lanelets.end(), [&](const auto & lanelet) {
if (utils::traffic_light::isTrafficSignalStop({lanelet}, planner_data_)) {
return true;
}
const auto next_lanes = planner_data_->route_handler->getNextLanelets(lanelet);
return utils::traffic_light::isTrafficSignalStop(next_lanes, planner_data_);
});
const auto not_use_adjacent_lane =
is_within_current_lane && red_signal_lane_itr != data.current_lanelets.end();

std::for_each(
data.current_lanelets.begin(), data.current_lanelets.end(), [&](const auto & lanelet) {
if (!not_use_adjacent_lane) {
data.drivable_lanes.push_back(
utils::static_obstacle_avoidance::generateExpandedDrivableLanes(
lanelet, planner_data_, parameters_));
} else if (red_signal_lane_itr->id() != lanelet.id()) {
data.drivable_lanes.push_back(
utils::static_obstacle_avoidance::generateExpandedDrivableLanes(
lanelet, planner_data_, parameters_));
} else {
data.drivable_lanes.push_back(
utils::static_obstacle_avoidance::generateNotExpandedDrivableLanes(lanelet));
data.red_signal_lane = lanelet;
}
});

// calc drivable bound
auto tmp_path = getPreviousModuleOutput().path;
const auto shorten_lanes = utils::cutOverlappedLanes(tmp_path, data.drivable_lanes);
const auto use_left_side_hatched_road_marking_area = [&]() {
if (!not_use_adjacent_lane) {
return true;
}
return !planner_data_->route_handler->getRoutingGraphPtr()->left(*red_signal_lane_itr);
}();
const auto use_right_side_hatched_road_marking_area = [&]() {
if (!not_use_adjacent_lane) {
return true;
}
return !planner_data_->route_handler->getRoutingGraphPtr()->right(*red_signal_lane_itr);
}();
data.left_bound = utils::calcBound(
getPreviousModuleOutput().path, planner_data_, shorten_lanes,
use_left_side_hatched_road_marking_area, parameters_->use_intersection_areas,
parameters_->use_freespace_areas, true);
data.right_bound = utils::calcBound(
getPreviousModuleOutput().path, planner_data_, shorten_lanes,
use_right_side_hatched_road_marking_area, parameters_->use_intersection_areas,
parameters_->use_freespace_areas, false);

// reference path
if (isDrivingSameLane(helper_->getPreviousDrivingLanes(), data.current_lanelets)) {
data.reference_path_rough = extendBackwardLength(getPreviousModuleOutput().path);
} else {
data.reference_path_rough = getPreviousModuleOutput().path;
RCLCPP_WARN(getLogger(), "Previous module lane is updated. Don't use latest reference path.");
}

// resampled reference path
data.reference_path = utils::resamplePathWithSpline(
data.reference_path_rough, parameters_->resample_interval_for_planning);

// closest index
data.ego_closest_path_index = planner_data_->findEgoIndex(data.reference_path.points);

// arclength from ego pose (used in many functions)
data.arclength_from_ego = utils::calcPathArcLengthArray(
data.reference_path, 0, data.reference_path.points.size(),
autoware::motion_utils::calcSignedArcLength(data.reference_path.points, getEgoPosition(), 0));

data.is_allowed_goal_modification =
utils::isAllowedGoalModification(planner_data_->route_handler);
data.distance_to_red_traffic_light = utils::traffic_light::calcDistanceToRedTrafficLight(
data.current_lanelets, data.reference_path_rough, planner_data_);

data.to_return_point = utils::static_obstacle_avoidance::calcDistanceToReturnDeadLine(
data.current_lanelets, data.reference_path_rough, planner_data_, parameters_);
data.current_lanelets, data.reference_path_rough, planner_data_, parameters_,
data.distance_to_red_traffic_light, data.is_allowed_goal_modification);

data.to_start_point = utils::static_obstacle_avoidance::calcDistanceToAvoidStartLine(
data.current_lanelets, data.reference_path_rough, planner_data_, parameters_);
data.current_lanelets, parameters_, data.distance_to_red_traffic_light);

Check warning on line 301 in planning/behavior_path_planner/autoware_behavior_path_static_obstacle_avoidance_module/src/scene.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Large Method

StaticObstacleAvoidanceModule::fillFundamentalData increases from 86 to 93 lines of code, threshold = 70. Large functions with many lines of code are generally harder to understand and lower the code health. Avoid adding more lines to this function.

// filter only for the latest detected objects.
fillAvoidanceTargetObjects(data, debug);
Expand Down Expand Up @@ -323,17 +332,16 @@
using utils::static_obstacle_avoidance::filterTargetObjects;
using utils::static_obstacle_avoidance::separateObjectsByPath;
using utils::static_obstacle_avoidance::updateRoadShoulderDistance;
using utils::traffic_light::calcDistanceToRedTrafficLight;

// Separate dynamic objects based on whether they are inside or outside of the expanded lanelets.
constexpr double MARGIN = 10.0;
const auto forward_detection_range = [&]() {
const auto to_traffic_light = calcDistanceToRedTrafficLight(
data.current_lanelets, helper_->getPreviousReferencePath(), planner_data_);
if (!to_traffic_light.has_value()) {
return helper_->getForwardDetectionRange();
if (!data.distance_to_red_traffic_light.has_value()) {
return helper_->getForwardDetectionRange(data.has_closest_lanelet, data.closest_lanelet);
}
return std::min(helper_->getForwardDetectionRange(), to_traffic_light.value());
return std::min(
helper_->getForwardDetectionRange(data.has_closest_lanelet, data.closest_lanelet),
data.distance_to_red_traffic_light.value());
}();

const auto [object_within_target_lane, object_outside_target_lane] = separateObjectsByPath(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 TIER IV, Inc.

Check notice on line 1 in planning/behavior_path_planner/autoware_behavior_path_static_obstacle_avoidance_module/src/utils.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Lines of Code in a Single File

The lines of code decreases from 2005 to 1999, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,6 +35,7 @@
#include <algorithm>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -1161,16 +1162,16 @@
}

bool isWithinLanes(
const lanelet::ConstLanelets & lanelets, const std::shared_ptr<const PlannerData> & planner_data)
const bool has_closest_lanelet, const lanelet::ConstLanelet & closest_lanelet,
const std::shared_ptr<const PlannerData> & planner_data)
{
const auto & rh = planner_data->route_handler;
const auto & ego_pose = planner_data->self_odometry->pose.pose;
const auto transform = autoware::universe_utils::pose2transform(ego_pose);
const auto footprint = autoware::universe_utils::transformVector(
planner_data->parameters.vehicle_info.createFootprint(), transform);

lanelet::ConstLanelet closest_lanelet{};
if (!lanelet::utils::query::getClosestLanelet(lanelets, ego_pose, &closest_lanelet)) {
if (!has_closest_lanelet) {
return true;
}

Expand Down Expand Up @@ -1950,13 +1951,11 @@
? autoware::motion_utils::calcSignedArcLength(
data.reference_path_rough.points, ego_idx, data.reference_path_rough.points.size() - 1)
: std::numeric_limits<double>::max();
const auto & is_allowed_goal_modification =
utils::isAllowedGoalModification(planner_data->route_handler);

for (auto & o : objects) {
if (!filtering_utils::isSatisfiedWithCommonCondition(
o, data.reference_path_rough, forward_detection_range, to_goal_distance,
planner_data->self_odometry->pose.pose.position, is_allowed_goal_modification,
planner_data->self_odometry->pose.pose.position, data.is_allowed_goal_modification,
parameters)) {
data.other_objects.push_back(o);
continue;
Expand Down Expand Up @@ -2556,9 +2555,8 @@
}

double calcDistanceToAvoidStartLine(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters)
const lanelet::ConstLanelets & lanelets, const std::shared_ptr<AvoidanceParameters> & parameters,
const std::optional<double> distance_to_red_traffic)
{
if (lanelets.empty()) {
return std::numeric_limits<double>::lowest();
Expand All @@ -2568,11 +2566,10 @@

// dead line stop factor(traffic light)
if (parameters->enable_dead_line_for_traffic_light) {
const auto to_traffic_light = calcDistanceToRedTrafficLight(lanelets, path, planner_data);
if (to_traffic_light.has_value()) {
if (distance_to_red_traffic.has_value()) {
distance_to_return_dead_line = std::max(
distance_to_return_dead_line,
to_traffic_light.value() + parameters->dead_line_buffer_for_traffic_light);
distance_to_red_traffic.value() + parameters->dead_line_buffer_for_traffic_light);
}
}

Expand All @@ -2582,28 +2579,26 @@
double calcDistanceToReturnDeadLine(
const lanelet::ConstLanelets & lanelets, const PathWithLaneId & path,
const std::shared_ptr<const PlannerData> & planner_data,
const std::shared_ptr<AvoidanceParameters> & parameters)
const std::shared_ptr<AvoidanceParameters> & parameters,
const std::optional<double> distance_to_red_traffic, const bool is_allowed_goal_modification)
{
if (lanelets.empty()) {
return std::numeric_limits<double>::max();
}

double distance_to_return_dead_line = std::numeric_limits<double>::max();

// dead line stop factor(traffic light)
if (parameters->enable_dead_line_for_traffic_light) {
const auto to_traffic_light = calcDistanceToRedTrafficLight(lanelets, path, planner_data);
if (to_traffic_light.has_value()) {
if (distance_to_red_traffic.has_value()) {
distance_to_return_dead_line = std::min(
distance_to_return_dead_line,
to_traffic_light.value() - parameters->dead_line_buffer_for_traffic_light);
distance_to_red_traffic.value() - parameters->dead_line_buffer_for_traffic_light);
}
}

// dead line for goal
if (
!utils::isAllowedGoalModification(planner_data->route_handler) &&
parameters->enable_dead_line_for_goal) {
if (!is_allowed_goal_modification && parameters->enable_dead_line_for_goal) {

Check notice on line 2601 in planning/behavior_path_planner/autoware_behavior_path_static_obstacle_avoidance_module/src/utils.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ New issue: Excess Number of Function Arguments

calcDistanceToReturnDeadLine has 6 arguments, threshold = 4. This function has too many arguments, indicating a lack of encapsulation. Avoid adding more arguments.
if (planner_data->route_handler->isInGoalRouteSection(lanelets.back())) {
const auto & ego_pos = planner_data->self_odometry->pose.pose.position;
const auto to_goal_distance =
Expand Down
Loading