forked from PathPlanning/AStar-JPS-ThetaStar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theta.cpp
185 lines (179 loc) · 6.21 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
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
#include "theta.h"
Theta::~Theta()
{
}
bool Theta::lineOfSight(int i1, int j1, int i2, int j2, const Map &map, bool cutcorners)
{
int delta_i = std::abs(i1 - i2);
int delta_j = std::abs(j1 - j2);
int step_i = (i1 < i2 ? 1 : -1);
int step_j = (j1 < j2 ? 1 : -1);
int error = 0;
int i = i1;
int j = j1;
if(delta_i == 0) {
for(; j != j2; j += step_j)
if(map.CellIsObstacle(i, j))
return false;
return true;
}
else if(delta_j == 0) {
for(; i != i2; i += step_i)
if(map.CellIsObstacle(i, j))
return false;
return true;
}
if(cutcorners) {
if (delta_i > delta_j) {
for (; i != i2; i += step_i) {
if (map.CellIsObstacle(i, j))
return false;
error += delta_j;
if ((error << 1) > delta_i) {
if(((error << 1) - delta_j) < delta_i && map.CellIsObstacle(i+step_i, j))
return false;
else if(((error << 1) - delta_j) > delta_i && map.CellIsObstacle(i, j+step_j))
return false;
j += step_j;
error -= delta_i;
}
}
}
else {
for (; j != j2; j += step_j) {
if (map.CellIsObstacle(i, j))
return false;
error += delta_i;
if ((error << 1) > delta_j) {
if(((error << 1) - delta_i) < delta_j && map.CellIsObstacle(i, j+step_j))
return false;
else if(((error << 1) - delta_i) > delta_j && map.CellIsObstacle(i+step_i, j))
return false;
i += step_i;
error -= delta_j;
}
}
}
}
else {
int sep_value = delta_i*delta_i + delta_j*delta_j;
if(delta_i > delta_j) {
for(; i != i2; i += step_i) {
if(map.CellIsObstacle(i, j))
return false;
if(map.CellIsObstacle(i, j + step_j))
return false;
error += delta_j;
if(error >= delta_i) {
if(((error << 1) - delta_i - delta_j)*((error << 1) - delta_i - delta_j) < sep_value)
if(map.CellIsObstacle(i + step_i,j))
return false;
if((3*delta_i - ((error << 1) - delta_j))*(3*delta_i - ((error << 1) - delta_j)) < sep_value)
if(map.CellIsObstacle(i, j + 2*step_j))
return false;
j += step_j;
error -= delta_i;
}
}
if(map.CellIsObstacle(i, j))
return false;
}
else {
for(; j != j2; j += step_j) {
if(map.CellIsObstacle(i, j))
return false;
if(map.CellIsObstacle(i + step_i, j))
return false;
error += delta_i;
if(error >= delta_j) {
if(((error << 1) - delta_i - delta_j)*((error << 1) - delta_i - delta_j) < (delta_i*delta_i + delta_j*delta_j))
if(map.CellIsObstacle(i, j + step_j))
return false;
if((3*delta_j - ((error << 1) - delta_i))*(3*delta_j - ((error << 1) - delta_i)) < (delta_i*delta_i + delta_j*delta_j))
if(map.CellIsObstacle(i + 2*step_i, j))
return false;
i += step_i;
error -= delta_j;
}
}
if(map.CellIsObstacle(i, j))
return false;
}
}
return true;
}
Node Theta::resetParent(Node current, Node parent, const Map &map, const EnvironmentOptions &options )
{
if (parent.parent == nullptr)
return current;
if(current == *parent.parent)
return current;
if (lineOfSight(parent.parent->i, parent.parent->j, current.i, current.j, map, options.cutcorners)) {
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));
}
void Theta::makeSecondaryPath()
{
Node *forpath = &*hppath.begin();
std::list<Node>::iterator iter = hppath.begin();
iter++;
Node *forpath2 = &*iter;
Node inpath;
lppath.push_back(*forpath);
while(forpath2 != &*hppath.end()) {
int i1 = forpath->i;
int j1 = forpath->j;
int i2 = forpath2->i;
int j2 = forpath2->j;
int delta_i = std::abs(i1 - i2);
int delta_j = std::abs(j1 - j2);
int step_i = (i1 < i2 ? 1 : -1);
int step_j = (j1 < j2 ? 1 : -1);
int error = 0;
if (delta_i > delta_j) {
int j = j1;
for (int i = i1; i != i2; i += step_i) {
inpath.i = i;
inpath.j = j;
lppath.push_back(inpath);
error += delta_j;
if ((error << 1) > delta_i) {
j += step_j;
error -= delta_i;
}
}
}
else {
int i = i1;
for (int j = j1; j != j2; j += step_j) {
inpath.i = i;
inpath.j = j;
lppath.push_back(inpath);
error += delta_i;
if ((error << 1) > delta_j) {
i += step_i;
error -= delta_j;
}
}
}
forpath = forpath2;
iter++;
forpath2 = &*iter;
}
}
void Theta::makePrimaryPath(Node curNode)
{
Node current = curNode;
while(current.parent) {
hppath.push_front(current);
current = *current.parent;
}
hppath.push_front(current);
}