-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththeta.cpp
157 lines (147 loc) · 4.58 KB
/
theta.cpp
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
#include "theta.h"
Node Theta::resetParent(Node current, Node parent, const Map &map)
{
if (parent.parent == nullptr)
return current;
if(current == *parent.parent)
return current;
if (map.checkLine(parent.parent->i, parent.parent->j, current.i, current.j)) {
current.g = parent.parent->g + distance(parent.parent->i, parent.parent->j, current.i, current.j);
current.parent = parent.parent;
return current;
}
return current;
}
double Theta::distance(int i1, int j1, int i2, int j2)
{
return sqrt(pow(i1 - i2, 2) + pow(j1 - j2, 2));
}
SearchResult Theta::startSearch(const Map &map)
{
open.resize(map.getHeight());
Node curNode;
curNode.i = map.getStart_i();
curNode.j = map.getStart_j();
curNode.g = 0;
curNode.H = distance(curNode.i, curNode.j, map.getGoal_i(), map.getGoal_j());
curNode.F = curNode.H;
curNode.parent = nullptr;
addOpen(curNode);
int closeSize = 0;
bool pathfound = false;
while (openSize > 0) {
curNode = findMin();
close.insert({curNode.i * map.getWidth() + curNode.j, curNode});
closeSize++;
open[curNode.i].pop_front();
openSize--;
if (curNode.i == map.getGoal_i() && curNode.j == map.getGoal_j()) {
pathfound = true;
break;
}
std::list<Node> successors = findSuccessors(curNode, map);
std::list<Node>::iterator it = successors.begin();
auto parent = &(close.find(curNode.i * map.getWidth() + curNode.j)->second);
while (it != successors.end()) {
it->parent = parent;
it->H = distance(it->i, it->j, map.getGoal_i(), map.getGoal_j());
*it = resetParent(*it, *it->parent, map);
it->F = it->g + it->H;
addOpen(*it);
it++;
}
}
if (pathfound) {
sresult.pathfound = true;
makePrimaryPath(curNode);
sresult.pathlength = curNode.g;
}
else
sresult.pathfound = false;
sresult.hppath = hppath;
return sresult;
}
Node Theta::findMin()
{
Node min;
min.F = std::numeric_limits<double>::infinity();
for (int i = 0; i < open.size(); i++)
if (!open[i].empty() && open[i].begin()->F <= min.F)
if (open[i].begin()->F == min.F){
if(open[i].begin()->g >= min.g)
min = *open[i].begin();
}
else
min = *open[i].begin();
return min;
}
std::list<Node> Theta::findSuccessors(Node curNode, const Map &map)
{
Node newNode;
std::list<Node> successors;
for (int i = -1; i <= +1; i++)
for (int j = -1; j <= +1; j++)
if ((i != 0 || j != 0) && map.checkTraversability(curNode.i + i, curNode.j + j)) {
if (close.find((curNode.i + i) * map.getWidth() + curNode.j + j) == close.end()) {
newNode.i = curNode.i + i;
newNode.j = curNode.j + j;
if(i == 0 || j == 0)
newNode.g = curNode.g + 1;
else
newNode.g = curNode.g + sqrt(2);
successors.push_front(newNode);
}
}
return successors;
}
void Theta::addOpen(Node newNode)
{
std::list<Node>::iterator iter, pos;
if (open[newNode.i].empty()) {
open[newNode.i].push_back(newNode);
openSize++;
return;
}
pos = open[newNode.i].end();
bool posFound = false;
for (iter = open[newNode.i].begin(); iter != open[newNode.i].end(); ++iter) {
if (!posFound && iter->F >= newNode.F)
if (iter->F == newNode.F) {
if(newNode.g >= iter->g){
pos = iter;
posFound = true;
}
}
else {
pos = iter;
posFound = true;
}
if (iter->j == newNode.j) {
if (newNode.F >= iter->F)
return;
else {
if (pos == iter) {
iter->F = newNode.F;
iter->g = newNode.g;
iter->parent = newNode.parent;
return;
}
open[newNode.i].erase(iter);
openSize--;
break;
}
}
}
openSize++;
open[newNode.i].insert(pos, newNode);
}
void Theta::makePrimaryPath(Node curNode)
{
Node current = curNode;
hppath.clear();
while(current.parent) {
hppath.push_back(current);
current = *current.parent;
}
//hppath.push_back(current);
}