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

fix(static_drivable_area_expansion): fix bound extraction logic #6006

Merged
Merged
Changes from all 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
@@ -1,4 +1,4 @@
// Copyright 2023 TIER IV, Inc.

Check notice on line 1 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.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 1494 to 1498, 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.

Check notice on line 1 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 10.31 to 10.03, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// 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 @@ -148,6 +148,40 @@

return offset_point ? offset_point.value() : points.at(nearest_segment_idx + 1);
}

std::vector<lanelet::ConstPoint3d> extractBoundFromPolygon(

Check warning on line 152 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

Codecov / codecov/patch

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

Added line #L152 was not covered by tests
const lanelet::ConstPolygon3d & polygon, const size_t start_idx, const size_t end_idx,
const bool clockwise)
{
std::vector<lanelet::ConstPoint3d> ret{};

Check warning on line 156 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

Codecov / codecov/patch

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

Added line #L156 was not covered by tests
for (size_t i = start_idx; i != end_idx; i = clockwise ? i + 1 : i - 1) {
ret.push_back(polygon[i]);

if (i + 1 == polygon.size() && clockwise) {
if (end_idx == 0) {
ret.push_back(polygon[end_idx]);
return ret;
}
i = 0;
ret.push_back(polygon[i]);
continue;

Check warning on line 167 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

Codecov / codecov/patch

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

Added line #L167 was not covered by tests
}

if (i == 0 && !clockwise) {
if (end_idx == polygon.size() - 1) {
ret.push_back(polygon[end_idx]);
return ret;
}
i = polygon.size() - 1;
ret.push_back(polygon[i]);
continue;

Check warning on line 177 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

Codecov / codecov/patch

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

Added line #L177 was not covered by tests
}
}

ret.push_back(polygon[end_idx]);

return ret;
}

Check warning on line 184 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

extractBoundFromPolygon has a cyclomatic complexity of 11, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check notice on line 184 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ New issue: Bumpy Road Ahead

extractBoundFromPolygon has 2 blocks with nested conditional logic. Any nesting of 2 or deeper is considered. Threshold is one single, nested block per function. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.

Check warning on line 184 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

Codecov / codecov/patch

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

Added line #L184 was not covered by tests
} // namespace

namespace behavior_path_planner::utils::drivable_area_processing
Expand Down Expand Up @@ -1260,83 +1294,53 @@
const std::vector<lanelet::ConstPoint3d> & original_bound,
const std::shared_ptr<RouteHandler> & route_handler,
const std::vector<DrivableLanes> & drivable_lanes, const bool is_left)
{
const auto extract_bound_from_polygon =
[](const auto & polygon, const auto start_idx, const auto end_idx, const auto clockwise) {
std::vector<lanelet::ConstPoint3d> ret{};
for (size_t i = start_idx; i != end_idx; i = clockwise ? i + 1 : i - 1) {
ret.push_back(polygon[i]);

if (i + 1 == polygon.size() && clockwise) {
if (end_idx == 0) {
ret.push_back(polygon[end_idx]);
return ret;
}
i = 0;
continue;
}

if (i == 0 && !clockwise) {
if (end_idx == polygon.size() - 1) {
ret.push_back(polygon[end_idx]);
return ret;
}
i = polygon.size() - 1;
continue;
}
}

ret.push_back(polygon[end_idx]);

return ret;
};

std::vector<lanelet::ConstPoint3d> expanded_bound = original_bound;

// expand drivable area by using intersection area.
for (const auto & drivable_lane : drivable_lanes) {
const auto edge_lanelet = is_left ? drivable_lane.left_lane : drivable_lane.right_lane;
const auto lanelet_bound = is_left ? edge_lanelet.leftBound3d() : edge_lanelet.rightBound3d();

if (lanelet_bound.size() < 2) {
continue;
}

const std::string id = edge_lanelet.attributeOr("intersection_area", "else");
if (id == "else") {
continue;
}

// Step1. extract intersection partial bound.
std::vector<lanelet::ConstPoint3d> intersection_bound{};
{
const auto polygon =
route_handler->getLaneletMapPtr()->polygonLayer.get(std::atoi(id.c_str()));

const auto is_clockwise_polygon =
boost::geometry::is_valid(lanelet::utils::to2D(polygon.basicPolygon()));
const auto is_clockwise_iteration = is_clockwise_polygon ? is_left : !is_left;

const auto intersection_bound_itr_init = std::find_if(
polygon.begin(), polygon.end(),
[&lanelet_bound](const auto & p) { return p.id() == lanelet_bound.front().id(); });

const auto intersection_bound_itr_last = std::find_if(
polygon.begin(), polygon.end(),
[&lanelet_bound](const auto & p) { return p.id() == lanelet_bound.back().id(); });

if (
intersection_bound_itr_init == polygon.end() ||
intersection_bound_itr_last == polygon.end()) {
continue;
}

// extract line strings between start_idx and end_idx.
const size_t start_idx = std::distance(polygon.begin(), intersection_bound_itr_init);
const size_t end_idx = std::distance(polygon.begin(), intersection_bound_itr_last);

intersection_bound =
extract_bound_from_polygon(polygon, start_idx, end_idx, is_clockwise_iteration);
extractBoundFromPolygon(polygon, start_idx, end_idx, is_clockwise_iteration);

Check notice on line 1343 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Complex Method

getBoundWithIntersectionAreas decreases in cyclomatic complexity from 30 to 20, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check notice on line 1343 in planning/behavior_path_planner_common/src/utils/drivable_area_expansion/static_drivable_area.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ No longer an issue: Bumpy Road Ahead

getBoundWithIntersectionAreas is no longer above the threshold for logical blocks with deeply nested code. The Bumpy Road code smell is a function that contains multiple chunks of nested conditional logic. The deeper the nesting and the more bumps, the lower the code health.
}

// Step2. check shared bound point.
Expand Down
Loading