Skip to content

Commit 15f8e29

Browse files
committed
try to fix code smell
1 parent db4d4ba commit 15f8e29

1 file changed

Lines changed: 46 additions & 47 deletions

File tree

src/robotics/astar_variant/src/astar_variant_base.cpp

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -67,65 +67,64 @@ bool AstarVariantBase::SetStartAndDestination(const Coordinate &start,
6767

6868
std::optional<std::vector<Coordinate>> AstarVariantBase::StepOverPathFinding() {
6969
if (found_path_) return std::nullopt;
70-
if (start_and_end_set_) {
71-
if (!traverse_path_.empty()) {
72-
const auto travelled_path = traverse_path_.begin();
73-
traverse_path_.erase(traverse_path_.begin());
70+
if (start_and_end_set_ && !traverse_path_.empty()) {
71+
const auto travelled_path = traverse_path_.begin();
72+
traverse_path_.erase(traverse_path_.begin());
7473

75-
const auto [i, j] = travelled_path->second;
74+
const auto [i, j] = travelled_path->second;
7675

77-
// mark node as visited
78-
visited_map_[i][j] = true;
76+
// mark node as visited
77+
visited_map_[i][j] = true;
7978

80-
auto &map = map_storage_->GetMap();
79+
auto &map = map_storage_->GetMap();
80+
81+
std::vector<Coordinate> expanded_nodes;
8182

82-
std::vector<Coordinate> expanded_nodes;
83+
for (size_t k = 0; k < motion_constraint_.size(); ++k) {
84+
Coordinate coordinate;
85+
coordinate.x = i + motion_constraint_.dx[k];
86+
coordinate.y = j + motion_constraint_.dy[k];
87+
if (!map_storage_->Contains(coordinate)) continue;
8388

84-
for (size_t k = 0; k < motion_constraint_.size(); ++k) {
85-
Coordinate coordinate;
86-
coordinate.x = i + motion_constraint_.dx[k];
87-
coordinate.y = j + motion_constraint_.dy[k];
88-
if (!map_storage_->Contains(coordinate)) continue;
89+
if (coordinate == dest_) {
90+
map[coordinate.x][coordinate.y].parent_coordinate = {i, j};
91+
found_path_ = true;
92+
return std::nullopt;
93+
}
8994

90-
if (coordinate == dest_) {
95+
// not occupied
96+
if (!map[coordinate.x][coordinate.y].occupied &&
97+
!visited_map_[coordinate.x][coordinate.y]) {
98+
const auto dist_travelled =
99+
sqrt(abs(motion_constraint_.dx[k]) + abs(motion_constraint_.dy[k]));
100+
101+
const auto astar_variant_spec = GetAstarVariantSpec();
102+
103+
const auto new_g = map[i][j].g + dist_travelled;
104+
const auto new_h = astar_variant_spec.heuristic_func(
105+
coordinate.x - dest_.x, coordinate.y - dest_.y);
106+
const auto new_f =
107+
astar_variant_spec.w1 * new_g + astar_variant_spec.w2 * new_h;
108+
109+
if (map[coordinate.x][coordinate.y].f ==
110+
std::numeric_limits<MapStorage::CostDataType>::max() ||
111+
map[coordinate.x][coordinate.y].f >= new_f) {
112+
map[coordinate.x][coordinate.y].f = new_f;
113+
map[coordinate.x][coordinate.y].g = new_g;
114+
map[coordinate.x][coordinate.y].h = new_h;
91115
map[coordinate.x][coordinate.y].parent_coordinate = {i, j};
92-
found_path_ = true;
93-
return std::nullopt;
94-
}
95116

96-
// not occupied
97-
if (!map[coordinate.x][coordinate.y].occupied &&
98-
!visited_map_[coordinate.x][coordinate.y]) {
99-
const auto dist_travelled = sqrt(abs(motion_constraint_.dx[k]) +
100-
abs(motion_constraint_.dy[k]));
101-
102-
const auto astar_variant_spec = GetAstarVariantSpec();
103-
104-
const auto new_g = map[i][j].g + dist_travelled;
105-
const auto new_h = astar_variant_spec.heuristic_func(
106-
coordinate.x - dest_.x, coordinate.y - dest_.y);
107-
const auto new_f =
108-
astar_variant_spec.w1 * new_g + astar_variant_spec.w2 * new_h;
109-
110-
if (map[coordinate.x][coordinate.y].f ==
111-
std::numeric_limits<MapStorage::CostDataType>::max() ||
112-
map[coordinate.x][coordinate.y].f >= new_f) {
113-
map[coordinate.x][coordinate.y].f = new_f;
114-
map[coordinate.x][coordinate.y].g = new_g;
115-
map[coordinate.x][coordinate.y].h = new_h;
116-
map[coordinate.x][coordinate.y].parent_coordinate = {i, j};
117-
118-
traverse_path_.insert(std::make_pair(
119-
new_f, std::make_pair(coordinate.x, coordinate.y)));
120-
121-
expanded_nodes.push_back(coordinate);
122-
}
117+
traverse_path_.insert(std::make_pair(
118+
new_f, std::make_pair(coordinate.x, coordinate.y)));
119+
120+
expanded_nodes.push_back(coordinate);
123121
}
124122
}
125-
return expanded_nodes;
126123
}
124+
return expanded_nodes;
127125
}
128-
return std::nullopt;
126+
}
127+
return std::nullopt;
129128
}
130129
bool AstarVariantBase::FindPath() {
131130
if (!start_and_end_set_) return false;

0 commit comments

Comments
 (0)