Skip to content

Commit eeed846

Browse files
authored
perf(bpp): reduce computational cost (#6054)
* pref(avoidance): don't use boost union_ Signed-off-by: satoshi-ota <satoshi.ota928@gmail.com> * perf(avoidance): reduce frequency to call calcSignedArcLength Signed-off-by: satoshi-ota <satoshi.ota928@gmail.com> * perf(turn_signal): reduce frequency to call calcSignedArcLength Signed-off-by: satoshi-ota <satoshi.ota928@gmail.com> * perf(static_drivable_area_expansion): don't use calcDistance2d Signed-off-by: satoshi-ota <satoshi.ota928@gmail.com> * fix(static_drivable_area_expansion): rename and fix return value consistency Signed-off-by: satoshi-ota <satoshi.ota928@gmail.com> --------- Signed-off-by: satoshi-ota <satoshi.ota928@gmail.com>
1 parent b60975f commit eeed846

File tree

7 files changed

+84
-80
lines changed

7 files changed

+84
-80
lines changed

planning/behavior_path_avoidance_module/include/behavior_path_avoidance_module/data_structs.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ struct ShiftLineData
588588
*/
589589
struct DebugData
590590
{
591-
geometry_msgs::msg::Polygon detection_area;
591+
std::vector<geometry_msgs::msg::Polygon> detection_areas;
592592

593593
lanelet::ConstLineStrings3d bounds;
594594

planning/behavior_path_avoidance_module/include/behavior_path_avoidance_module/scene.hpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,12 @@ class AvoidanceModule : public SceneModuleInterface
112112
*/
113113
void updateRegisteredRTCStatus(const PathWithLaneId & path)
114114
{
115-
const Point ego_position = planner_data_->self_odometry->pose.pose.position;
115+
const auto ego_idx = planner_data_->findEgoIndex(path.points);
116116

117117
for (const auto & left_shift : left_shift_array_) {
118118
const double start_distance =
119-
calcSignedArcLength(path.points, ego_position, left_shift.start_pose.position);
120-
const double finish_distance =
121-
calcSignedArcLength(path.points, ego_position, left_shift.finish_pose.position);
119+
calcSignedArcLength(path.points, ego_idx, left_shift.start_pose.position);
120+
const double finish_distance = start_distance + left_shift.relative_longitudinal;
122121
rtc_interface_ptr_map_.at("left")->updateCooperateStatus(
123122
left_shift.uuid, true, start_distance, finish_distance, clock_->now());
124123
if (finish_distance > -1.0e-03) {
@@ -130,9 +129,8 @@ class AvoidanceModule : public SceneModuleInterface
130129

131130
for (const auto & right_shift : right_shift_array_) {
132131
const double start_distance =
133-
calcSignedArcLength(path.points, ego_position, right_shift.start_pose.position);
134-
const double finish_distance =
135-
calcSignedArcLength(path.points, ego_position, right_shift.finish_pose.position);
132+
calcSignedArcLength(path.points, ego_idx, right_shift.start_pose.position);
133+
const double finish_distance = start_distance + right_shift.relative_longitudinal;
136134
rtc_interface_ptr_map_.at("right")->updateCooperateStatus(
137135
right_shift.uuid, true, start_distance, finish_distance, clock_->now());
138136
if (finish_distance > -1.0e-03) {
@@ -399,6 +397,7 @@ class AvoidanceModule : public SceneModuleInterface
399397
UUID uuid;
400398
Pose start_pose;
401399
Pose finish_pose;
400+
double relative_longitudinal{0.0};
402401
};
403402

404403
using RegisteredShiftLineArray = std::vector<RegisteredShiftLine>;

planning/behavior_path_avoidance_module/src/debug.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,15 @@ MarkerArray createDebugMarkerArray(
608608
addShiftGrad(debug.total_backward_grad, debug.total_shift, "grad_backward", 0.4, 0.2, 0.9);
609609
}
610610

611+
// detection area
612+
size_t i = 0;
613+
for (const auto & detection_area : debug.detection_areas) {
614+
add(createPolygonMarkerArray(detection_area, "detection_area", i++, 0.16, 1.0, 0.69, 0.1));
615+
}
616+
611617
// misc
612618
{
613619
add(createPathMarkerArray(path, "centerline_resampled", 0, 0.0, 0.9, 0.5));
614-
add(createPolygonMarkerArray(debug.detection_area, "detection_area", 0L, 0.16, 1.0, 0.69, 0.1));
615620
add(createDrivableBounds(data, "drivable_bound", 1.0, 0.0, 0.42));
616621
add(laneletsAsTriangleMarkerArray(
617622
"drivable_lanes", transformToLanelets(data.drivable_lanes),

planning/behavior_path_avoidance_module/src/scene.cpp

+37-25
Original file line numberDiff line numberDiff line change
@@ -703,18 +703,6 @@ bool AvoidanceModule::isSafePath(
703703
return true; // if safety check is disabled, it always return safe.
704704
}
705705

706-
const bool limit_to_max_velocity = false;
707-
const auto ego_predicted_path_params =
708-
std::make_shared<utils::path_safety_checker::EgoPredictedPathParams>(
709-
parameters_->ego_predicted_path_params);
710-
const size_t ego_seg_idx = planner_data_->findEgoSegmentIndex(shifted_path.path.points);
711-
const auto ego_predicted_path_for_front_object = utils::path_safety_checker::createPredictedPath(
712-
ego_predicted_path_params, shifted_path.path.points, getEgoPose(), getEgoSpeed(), ego_seg_idx,
713-
true, limit_to_max_velocity);
714-
const auto ego_predicted_path_for_rear_object = utils::path_safety_checker::createPredictedPath(
715-
ego_predicted_path_params, shifted_path.path.points, getEgoPose(), getEgoSpeed(), ego_seg_idx,
716-
false, limit_to_max_velocity);
717-
718706
const auto ego_idx = planner_data_->findEgoIndex(shifted_path.path.points);
719707
const auto is_right_shift = [&]() -> std::optional<bool> {
720708
for (size_t i = ego_idx; i < shifted_path.shift_length.size(); i++) {
@@ -741,6 +729,22 @@ bool AvoidanceModule::isSafePath(
741729
const auto safety_check_target_objects = utils::avoidance::getSafetyCheckTargetObjects(
742730
avoid_data_, planner_data_, parameters_, is_right_shift.value(), debug);
743731

732+
if (safety_check_target_objects.empty()) {
733+
return true;
734+
}
735+
736+
const bool limit_to_max_velocity = false;
737+
const auto ego_predicted_path_params =
738+
std::make_shared<utils::path_safety_checker::EgoPredictedPathParams>(
739+
parameters_->ego_predicted_path_params);
740+
const size_t ego_seg_idx = planner_data_->findEgoSegmentIndex(shifted_path.path.points);
741+
const auto ego_predicted_path_for_front_object = utils::path_safety_checker::createPredictedPath(
742+
ego_predicted_path_params, shifted_path.path.points, getEgoPose(), getEgoSpeed(), ego_seg_idx,
743+
true, limit_to_max_velocity);
744+
const auto ego_predicted_path_for_rear_object = utils::path_safety_checker::createPredictedPath(
745+
ego_predicted_path_params, shifted_path.path.points, getEgoPose(), getEgoSpeed(), ego_seg_idx,
746+
false, limit_to_max_velocity);
747+
744748
for (const auto & object : safety_check_target_objects) {
745749
auto current_debug_data = utils::path_safety_checker::createObjectDebug(object);
746750

@@ -793,15 +797,16 @@ PathWithLaneId AvoidanceModule::extendBackwardLength(const PathWithLaneId & orig
793797

794798
const auto longest_dist_to_shift_point = [&]() {
795799
double max_dist = 0.0;
796-
for (const auto & pnt : path_shifter_.getShiftLines()) {
797-
max_dist = std::max(
798-
max_dist, calcSignedArcLength(previous_path.points, pnt.start.position, getEgoPosition()));
800+
auto lines = path_shifter_.getShiftLines();
801+
if (lines.empty()) {
802+
return max_dist;
799803
}
800-
for (const auto & sp : generator_.getRawRegisteredShiftLine()) {
801-
max_dist = std::max(
802-
max_dist, calcSignedArcLength(previous_path.points, sp.start.position, getEgoPosition()));
803-
}
804-
return max_dist;
804+
std::sort(lines.begin(), lines.end(), [](const auto & a, const auto & b) {
805+
return a.start_idx < b.start_idx;
806+
});
807+
return std::max(
808+
max_dist,
809+
calcSignedArcLength(previous_path.points, lines.front().start.position, getEgoPosition()));
805810
}();
806811

807812
const auto extra_margin = 10.0; // Since distance does not consider arclength, but just line.
@@ -1029,11 +1034,14 @@ void AvoidanceModule::updatePathShifter(const AvoidLineArray & shift_lines)
10291034
const auto sl = helper_->getMainShiftLine(shift_lines);
10301035
const auto sl_front = shift_lines.front();
10311036
const auto sl_back = shift_lines.back();
1037+
const auto relative_longitudinal = sl_back.end_longitudinal - sl_front.start_longitudinal;
10321038

10331039
if (helper_->getRelativeShiftToPath(sl) > 0.0) {
1034-
left_shift_array_.push_back({uuid_map_.at("left"), sl_front.start, sl_back.end});
1040+
left_shift_array_.push_back(
1041+
{uuid_map_.at("left"), sl_front.start, sl_back.end, relative_longitudinal});
10351042
} else if (helper_->getRelativeShiftToPath(sl) < 0.0) {
1036-
right_shift_array_.push_back({uuid_map_.at("right"), sl_front.start, sl_back.end});
1043+
right_shift_array_.push_back(
1044+
{uuid_map_.at("right"), sl_front.start, sl_back.end, relative_longitudinal});
10371045
}
10381046

10391047
uuid_map_.at("left") = generateUUID();
@@ -1150,15 +1158,19 @@ bool AvoidanceModule::isValidShiftLine(
11501158
const size_t start_idx = shift_lines.front().start_idx;
11511159
const size_t end_idx = shift_lines.back().end_idx;
11521160

1161+
const auto path = shifter_for_validate.getReferencePath();
1162+
const auto left_bound = lanelet::utils::to2D(avoid_data_.left_bound);
1163+
const auto right_bound = lanelet::utils::to2D(avoid_data_.right_bound);
11531164
for (size_t i = start_idx; i <= end_idx; ++i) {
1154-
const auto p = getPoint(shifter_for_validate.getReferencePath().points.at(i));
1165+
const auto p = getPoint(path.points.at(i));
11551166
lanelet::BasicPoint2d basic_point{p.x, p.y};
11561167

11571168
const auto shift_length = proposed_shift_path.shift_length.at(i);
1158-
const auto bound = shift_length > 0.0 ? avoid_data_.left_bound : avoid_data_.right_bound;
11591169
const auto THRESHOLD = minimum_distance + std::abs(shift_length);
11601170

1161-
if (boost::geometry::distance(basic_point, lanelet::utils::to2D(bound)) < THRESHOLD) {
1171+
if (
1172+
boost::geometry::distance(basic_point, (shift_length > 0.0 ? left_bound : right_bound)) <
1173+
THRESHOLD) {
11621174
RCLCPP_DEBUG_THROTTLE(
11631175
getLogger(), *clock_, 1000,
11641176
"following latest new shift line may cause deviation from drivable area.");

planning/behavior_path_avoidance_module/src/utils.cpp

+16-29
Original file line numberDiff line numberDiff line change
@@ -1892,11 +1892,12 @@ std::pair<PredictedObjects, PredictedObjects> separateObjectsByPath(
18921892
max_offset = std::max(max_offset, offset);
18931893
}
18941894

1895+
const double MARGIN = is_running ? 1.0 : 0.0; // [m]
18951896
const auto detection_area =
1896-
createVehiclePolygon(planner_data->parameters.vehicle_info, max_offset);
1897+
createVehiclePolygon(planner_data->parameters.vehicle_info, max_offset + MARGIN);
18971898
const auto ego_idx = planner_data->findEgoIndex(path.points);
18981899

1899-
Polygon2d attention_area;
1900+
std::vector<Polygon2d> detection_areas;
19001901
for (size_t i = 0; i < path.points.size() - 1; ++i) {
19011902
const auto & p_ego_front = path.points.at(i).point.pose;
19021903
const auto & p_ego_back = path.points.at(i + 1).point.pose;
@@ -1906,41 +1907,27 @@ std::pair<PredictedObjects, PredictedObjects> separateObjectsByPath(
19061907
break;
19071908
}
19081909

1909-
const auto ego_one_step_polygon = createOneStepPolygon(p_ego_front, p_ego_back, detection_area);
1910-
1911-
std::vector<Polygon2d> unions;
1912-
boost::geometry::union_(attention_area, ego_one_step_polygon, unions);
1913-
if (!unions.empty()) {
1914-
attention_area = unions.front();
1915-
boost::geometry::correct(attention_area);
1916-
}
1910+
detection_areas.push_back(createOneStepPolygon(p_ego_front, p_ego_back, detection_area));
19171911
}
19181912

1919-
// expand detection area width only when the module is running.
1920-
if (is_running) {
1921-
constexpr int PER_CIRCLE = 36;
1922-
constexpr double MARGIN = 1.0; // [m]
1923-
boost::geometry::strategy::buffer::distance_symmetric<double> distance_strategy(MARGIN);
1924-
boost::geometry::strategy::buffer::join_round join_strategy(PER_CIRCLE);
1925-
boost::geometry::strategy::buffer::end_round end_strategy(PER_CIRCLE);
1926-
boost::geometry::strategy::buffer::point_circle circle_strategy(PER_CIRCLE);
1927-
boost::geometry::strategy::buffer::side_straight side_strategy;
1928-
boost::geometry::model::multi_polygon<Polygon2d> result;
1929-
// Create the buffer of a multi polygon
1930-
boost::geometry::buffer(
1931-
attention_area, result, distance_strategy, side_strategy, join_strategy, end_strategy,
1932-
circle_strategy);
1933-
if (!result.empty()) {
1934-
attention_area = result.front();
1913+
std::for_each(detection_areas.begin(), detection_areas.end(), [&](const auto & detection_area) {
1914+
debug.detection_areas.push_back(toMsg(detection_area, data.reference_pose.position.z));
1915+
});
1916+
1917+
const auto within_detection_area = [&](const auto & obj_polygon) {
1918+
for (const auto & detection_area : detection_areas) {
1919+
if (!boost::geometry::disjoint(obj_polygon, detection_area)) {
1920+
return true;
1921+
}
19351922
}
1936-
}
19371923

1938-
debug.detection_area = toMsg(attention_area, data.reference_pose.position.z);
1924+
return false;
1925+
};
19391926

19401927
const auto objects = planner_data->dynamic_object->objects;
19411928
std::for_each(objects.begin(), objects.end(), [&](const auto & object) {
19421929
const auto obj_polygon = tier4_autoware_utils::toPolygon2d(object);
1943-
if (boost::geometry::disjoint(obj_polygon, attention_area)) {
1930+
if (!within_detection_area(obj_polygon)) {
19441931
other_objects.objects.push_back(object);
19451932
} else {
19461933
target_objects.objects.push_back(object);

planning/behavior_path_planner_common/src/turn_signal_decider.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,7 @@ TurnSignalInfo TurnSignalDecider::use_prior_turn_signal(
415415

416416
const double dist_to_original_desired_start =
417417
get_distance(original_desired_start_point) - base_link2front_;
418-
const double dist_to_original_desired_end = get_distance(original_desired_end_point);
419-
const double dist_to_original_required_start =
420-
get_distance(original_required_start_point) - base_link2front_;
421-
const double dist_to_original_required_end = get_distance(original_required_end_point);
422418
const double dist_to_new_desired_start = get_distance(new_desired_start_point) - base_link2front_;
423-
const double dist_to_new_desired_end = get_distance(new_desired_end_point);
424-
const double dist_to_new_required_start =
425-
get_distance(new_required_start_point) - base_link2front_;
426-
const double dist_to_new_required_end = get_distance(new_required_end_point);
427419

428420
// If we still do not reach the desired front point we ignore it
429421
if (dist_to_original_desired_start > 0.0 && dist_to_new_desired_start > 0.0) {
@@ -435,6 +427,9 @@ TurnSignalInfo TurnSignalDecider::use_prior_turn_signal(
435427
return original_signal;
436428
}
437429

430+
const double dist_to_original_desired_end = get_distance(original_desired_end_point);
431+
const double dist_to_new_desired_end = get_distance(new_desired_end_point);
432+
438433
// If we already passed the desired end point, return the other signal
439434
if (dist_to_original_desired_end < 0.0 && dist_to_new_desired_end < 0.0) {
440435
TurnSignalInfo empty_signal_info;
@@ -445,6 +440,13 @@ TurnSignalInfo TurnSignalDecider::use_prior_turn_signal(
445440
return original_signal;
446441
}
447442

443+
const double dist_to_original_required_start =
444+
get_distance(original_required_start_point) - base_link2front_;
445+
const double dist_to_original_required_end = get_distance(original_required_end_point);
446+
const double dist_to_new_required_start =
447+
get_distance(new_required_start_point) - base_link2front_;
448+
const double dist_to_new_required_end = get_distance(new_required_end_point);
449+
448450
if (dist_to_original_desired_start <= dist_to_new_desired_start) {
449451
const auto enable_prior = use_prior_turn_signal(
450452
dist_to_original_required_start, dist_to_original_required_end, dist_to_new_required_start,

planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

+8-9
Original file line numberDiff line numberDiff line change
@@ -208,26 +208,25 @@ std::optional<std::pair<size_t, geometry_msgs::msg::Point>> intersectBound(
208208
return std::nullopt;
209209
}
210210

211-
double calcDistanceFromPointToSegment(
211+
double calcSquaredDistanceFromPointToSegment(
212212
const geometry_msgs::msg::Point & segment_start_point,
213213
const geometry_msgs::msg::Point & segment_end_point,
214214
const geometry_msgs::msg::Point & target_point)
215215
{
216+
using tier4_autoware_utils::calcSquaredDistance2d;
217+
216218
const auto & a = segment_start_point;
217219
const auto & b = segment_end_point;
218220
const auto & p = target_point;
219221

220222
const double dot_val = (b.x - a.x) * (p.x - a.x) + (b.y - a.y) * (p.y - a.y);
221-
const double squared_segment_length = tier4_autoware_utils::calcSquaredDistance2d(a, b);
223+
const double squared_segment_length = calcSquaredDistance2d(a, b);
222224
if (0 <= dot_val && dot_val <= squared_segment_length) {
223-
const double numerator = std::abs((p.x - a.x) * (a.y - b.y) - (p.y - a.y) * (a.x - b.x));
224-
const double denominator = std::sqrt(std::pow(a.x - b.x, 2) + std::pow(a.y - b.y, 2));
225-
return numerator / denominator;
225+
return calcSquaredDistance2d(p, a) - dot_val * dot_val / squared_segment_length;
226226
}
227227

228228
// target_point is outside the segment.
229-
return std::min(
230-
tier4_autoware_utils::calcDistance2d(a, p), tier4_autoware_utils::calcDistance2d(b, p));
229+
return std::min(calcSquaredDistance2d(a, p), calcSquaredDistance2d(b, p));
231230
}
232231

233232
PolygonPoint transformBoundFrenetCoordinate(
@@ -238,8 +237,8 @@ PolygonPoint transformBoundFrenetCoordinate(
238237
// find wrong nearest index.
239238
std::vector<double> dist_to_bound_segment_vec;
240239
for (size_t i = 0; i < bound_points.size() - 1; ++i) {
241-
const double dist_to_bound_segment =
242-
calcDistanceFromPointToSegment(bound_points.at(i), bound_points.at(i + 1), target_point);
240+
const double dist_to_bound_segment = calcSquaredDistanceFromPointToSegment(
241+
bound_points.at(i), bound_points.at(i + 1), target_point);
243242
dist_to_bound_segment_vec.push_back(dist_to_bound_segment);
244243
}
245244

0 commit comments

Comments
 (0)