-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder.cpp
More file actions
219 lines (203 loc) · 8.62 KB
/
Copy pathPathFinder.cpp
File metadata and controls
219 lines (203 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! \addtogroup 0007 Pathfinding
//! \brief A pathfinding module
//!
//! A pathfinding module that can be used in the R2D2 project.
//! The module is currently based on the A star algorithm.
//!
//! \file PathFinder.cpp
//! \author Jasper Schoenmaker 1661818
//! \author Chiel Douwes 1666311
//! \author Ole Achterberg 1651981
//! \date Created: 01-04-2016
//! \date Last Modified: 15-4-2016
//! \brief Find the path and returns the path
//!
//! Takes a starting point and end point and a reference to a path.
//! If a path is possible it returns a path to the end point. If no path is
//! possible it will return false.
//!
//! \copyright Copyright © 2016, HU University of Applied Sciences Utrecht.
//! All rights reserved.
//!
//! License: newBSD
//!
//! Redistribution and use in source and binary forms,
//! with or without modification, are permitted provided that
//! the following conditions are met:
//! - Redistributions of source code must retain the above copyright notice,
//! this list of conditions and the following disclaimer.
//! - Redistributions in binary form must reproduce the above copyright notice,
//! this list of conditions and the following disclaimer in the documentation
//! and/or other materials provided with the distribution.
//! - Neither the name of the HU University of Applied Sciences Utrecht
//! nor the names of its contributors may be used to endorse or promote
//! products derived from this software without specific prior written
//! permission.
//!
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
//! BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
//! AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//! IN NO EVENT SHALL THE HU UNIVERSITY OF APPLIED SCIENCES UTRECHT
//! BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
//! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
//! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//! EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ~< HEADER_VERSION 2016 04 12 >~
#include "../include/PathFinder.hpp"
namespace r2d2 {
PathFinder::PathFinder(Map &map, Translation robotBox) :
map(map),
robotBox(robotBox) {
}
bool PathFinder::get_path_to_coordinate(Coordinate start,
Coordinate goal,
std::vector<Coordinate>
&path) {
// check for the goal node being at the same coordinate as the start node
if (overlaps(start, goal)) {
path.clear();
return true;
}
// do a check for end node accessibility before starting the search
if (!can_travel(goal, goal)) {
return false;
}
// parent is at this point unknown for the start node,
// so construct it as unknown
CoordNode endNode{*this, goal, start, 0 * Length::METER},
startNode{*this,
start,
start};
AStarSearch<CoordNode> search{endNode};
std::shared_ptr<CoordNode> foundStart = search.search(startNode);
if (foundStart == nullptr) {
return false;
} else {
std::vector<CoordNode> foundPath{get_path(foundStart)};
path.clear();
for (CoordNode &node : foundPath) {
path.push_back(node.coord);
}
smooth_path(path, start);
return true;
}
}
PathFinder::CoordNode::CoordNode(
PathFinder &pathFinder, Coordinate coord,
Coordinate &startCoord, Length g,
std::weak_ptr<CoordNode> parent) :
Node{g, PathFinder::get_heuristic(startCoord - coord),
parent},
pathFinder(pathFinder),
coord(coord),
startNodeCoord(startCoord) {
}
std::vector<PathFinder::CoordNode>
PathFinder::CoordNode::get_available_nodes(
std::shared_ptr<PathFinder::CoordNode> &self) {
std::vector<CoordNode> children;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if (x != 0 || y != 0) {
// the grid will be relative to the end position of the search
Coordinate childPos{
coord + (Translation{
x * pathFinder.robotBox.get_x(),
y * pathFinder.robotBox.get_y(),
0 * Length::METER
} / SQUARES_PER_ROBOT)};
//check whether the successor is the end node
if (pathFinder.overlaps(childPos, startNodeCoord)) {
childPos = {startNodeCoord};
}
// can_travel is used so that it can be ensured that there is no
// obstacle in the path
if (pathFinder.can_travel(coord, childPos)) {
children.push_back(
CoordNode{pathFinder, childPos, startNodeCoord,
g + PathFinder::get_heuristic(
childPos - coord
), // distance from the search begin
self});
}
}
}
}
return children;
}
bool PathFinder::CoordNode::operator==(
const PathFinder::CoordNode &lhs) const {
return (coord - lhs.coord).get_length() / Length::METER == 0;
}
bool PathFinder::can_travel(const Coordinate &from,
const Coordinate &to) {
Coordinate minCoord{
(from.get_x() < to.get_x() ? from.get_x() : to.get_x()),
(from.get_y() < to.get_y() ? from.get_y() : to.get_y()),
0 * Length::METER};
Translation size{(Coordinate{
(from.get_x() > to.get_x() ? from.get_x() : to.get_x()),
(from.get_y() > to.get_y() ? from.get_y() : to.get_y()),
0 * Length::METER} - minCoord) + robotBox};
return !map.has_obstacle(minCoord - (robotBox / 2), size);
}
bool PathFinder::overlaps(const Coordinate &c1,
const Coordinate &c2) {
Translation diff = c1 - c2;
return (diff.get_x() < 0 * Length::METER ?
0 * Length::METER - diff.get_x() :
diff.get_x()
) < robotBox.get_x() / 2 &&
(diff.get_y() < 0 * Length::METER ?
0 * Length::METER - diff.get_y() :
diff.get_y()
) < robotBox.get_y() / 2;
}
// define a constant as to speed the calculation up,
// in this case 10 digits is "good enough"
#define SQ_ROOT_2 1.414213562f
Length PathFinder::get_heuristic(Translation coord) {
// diagonal distance
Length xDist = (coord.get_x() < 0 * Length::METER) ?
(0 * Length::METER - coord.get_x()) : coord.get_x();
Length yDist = (coord.get_y() < 0 * Length::METER) ?
(0 * Length::METER - coord.get_y()) : coord.get_y();
Length shortDist, longDist;
if (xDist < yDist) {
shortDist = xDist;
longDist = yDist;
} else {
shortDist = yDist;
longDist = xDist;
}
return (shortDist * SQ_ROOT_2) + (longDist - shortDist);
}
std::vector<PathFinder::CoordNode> PathFinder::get_path(
std::shared_ptr<PathFinder::CoordNode> start) {
std::shared_ptr<CoordNode> curNode = start;
std::vector<CoordNode> path;
while (!curNode->parent.expired()) {
curNode = std::shared_ptr<CoordNode>(curNode->parent);
path.emplace_back(*curNode);
}
return path;
}
void PathFinder::smooth_path(std::vector<Coordinate> &path,
Coordinate start) {
Coordinate lastPos = start;
auto it = path.begin();
auto current = it++;
while (it != path.end()) {
if (can_travel(lastPos, *it)) {
path.erase(current);
} else {
lastPos = *it;
current = it++;
}
}
}
}