-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOceanmap2d.cpp
361 lines (272 loc) · 10.1 KB
/
Oceanmap2d.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**--------------start Oceanmpa2d.cpp--------------------**/
/**cpp file where all the functions of Oceanmap are defined and initialized**/
/**if defined, printing for debugging is enabled**/
//#define _DEBUG_COST
//#define _DEBUG_COST_2
//#define _DEBUG_COST_3
//#define _DEBUG_NEIGHBORHOOD
//#define _DEBUG_WIND
//#define _DEBUG_WIND_SPEED
/**general includes**/
#include <ctype.h>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <vector>
#include <iostream>
#include "include/Grid2D.h"
#include "Oceanmap2d.h"
//for setIslands
#include <string>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include "Mapnode.h"
#include "micropather.h"
#include "avalon.h"
using namespace micropather;
void Oceanmap2d::SetNeighborhood8()
{
for (int i=-1; i<2; i++)
{
for (int j=-1; j<2; j++)
{
if ((i==0) && (j==0))
{
continue;
}
else
{
neighbor neighborNow;
neighborNow.x =i;
neighborNow.y =j;
neighborNow.traversedCells.push_back(std::pair<int,int>(i,j));
neighborNow.traversedCells.push_back(std::pair<int,int>(0,0));
neighborhood.push_back (neighborNow);
}
}
}
}
void Oceanmap2d::SetNeighborhood24()
{
int z,traversedCellSize;
double deltax, deltay;
int currentX, currentY;
for (int i=-2; i<3; i++)
{
for (int j=-2; j<3; j++)
{
#ifdef _DEBUG_NEIGHBORHOOD
printf("i = %d, j=%d \n",i,j);
#endif //debug neighborhood
if ((i==0) && (j==0))
{
continue;
}
else
{
neighbor neighborNow;
neighborNow.x = i;
neighborNow.y = j;
deltax = 0;
deltay = 0;
z=0;
while (z<5)
{
deltax += ((double)i)/5;
deltay += ((double)j)/5;
currentX = rint(deltax);
currentY = rint(deltay);
#ifdef _DEBUG_NEIGHBORHOOD
printf("deltax und deltay: %f,%f; rounded: %d,%d;\n",deltax,deltay,currentX,currentY);
#endif
if (z==0)
{
neighborNow.traversedCells.push_back(std::pair<int,int>(currentX,currentY));
z++;
}
else
{
traversedCellSize = neighborNow.traversedCells.size() - 1;
z++;
if (neighborNow.traversedCells[traversedCellSize].first==currentX && neighborNow.traversedCells[traversedCellSize].second==currentY)
{
continue;
}
else
{
neighborNow.traversedCells.push_back(std::pair<int,int>(currentX,currentY));
}
}
#ifdef _DEBUG_NEIGHBORHOOD
printf("grösse des traversed Cells - Vector: %d\n",neighborNow.traversedCells.size());
#endif //debug neighborhood
}
neighborhood.push_back (neighborNow);
}
}
}
}
float Oceanmap2d::LeastCostEstimate (void *stateStart, void *stateEnd)
{
int dx = ((Mapnode2d *) stateStart)->x - ((Mapnode2d *) stateEnd)->x;
int dy = ((Mapnode2d *) stateStart)->y - ((Mapnode2d *) stateEnd)->y;
return (float) sqrt ((double) (dx * dx) + (double) (dy * dy));
}
void Oceanmap2d::AdjacentCost (void *state,std::vector < micropather::StateCost > *adjacent)
{
/**for each of the surrounding nodes this routine calculates the cost**/
Mapnode2d current = *(Mapnode2d *) state;
int i,j,m,n;
double currentCost=0,dist,headingAngle,alpha, currentSpeed;
for (unsigned int k = 0; k<neighborhood.size();k++)
{
i= (signed)current.x + neighborhood[k].x;
j= (signed)current.y + neighborhood[k].y;
currentCost=0;
for (unsigned int l=0;l<neighborhood[k].traversedCells.size ();l++)
{
m=neighborhood[k].traversedCells[l].first + current.x;
n=neighborhood[k].traversedCells[l].second + current.y;
if (map.contains(m,n))
{
currentCost += map(m, n).cost;
}
}
if (!map.contains(i,j))
{
continue;
}
else
{
/**good cell, use currentCost to avoid obstacles!**/
//new version for calculating alpha:
dist = sqrt (pow (i,2) + pow (j,2));
headingAngle = atan2 (neighborhood[k].y, neighborhood[k].x);
windDirection = remainder( windDirection, 2*AV_PI);
alpha = fabs( remainder((headingAngle - windDirection),2*AV_PI));
/**calculating the currentSpeed**/
if (alpha<(AV_PI/4-0.05))
{
currentSpeed=0.0;
}
else
{
currentSpeed = ((-0.00482179 * pow (alpha, 4) + 0.0335788 * pow (alpha, 3) - 0.246088 * pow (alpha,2)
+ 0.746539 * alpha - 0.0110976) * (double) windSpeed);
}
//printf("expanding from %d,%d to %d,%d with total cost of: %f \n",(signed)current.x,(signed)current.y,i,j,(currentCost + dist/currentSpeed));
/**push back the newly calculated adjacent node**/
if(currentSpeed==0)
{
StateCost nodeCost = { (void *) &map (i, j), 20000};
adjacent->push_back (nodeCost);
}
else
{
StateCost nodeCost = { (void *) &map (i, j), currentCost + dist/currentSpeed};
adjacent->push_back (nodeCost);
}
}
assert (adjacent->size()>0);
}
}
void Oceanmap2d::PrintStateInfo (void *state)
{
((Mapnode2d *) state)->print (stdout);
}
void Oceanmap2d::Print (std::vector < void *>path)
{
for (unsigned int k = 0; k < path.size (); ++k)
{
((Mapnode2d *) path[k])->print (stdout);
printf ("vectorsize=%d\n",path.size());
}
}
Oceanmap2d::Oceanmap2d (unsigned int width, unsigned int height):map (0, 1, width, 0, 1, height) {
Grid2D < Mapnode2d >::iterator it;
for (it = map.begin (); it != map.end (); ++it)
{
it->x = map.xindex (it);
it->y = map.yindex (it);
}
if(NEIGHBORHOOD == 8)
SetNeighborhood8();
if(NEIGHBORHOOD == 24)
SetNeighborhood24();
}
Oceanmap2d::~Oceanmap2d(){
}
void Oceanmap2d::setIslands (const char *island, int xIsland, int yIsland,int xIslandLongitude,int yIslandLatitude, int nodemeters)
{
std::string line;
std::ifstream myfile (island);
int xSize,ySize,xi,yi,color,cx,cy;
int xCount=0,yCount=0;
char currentLine[10000], *ptr;
//printf("helle\n");
if (myfile.is_open())
{
//printf("hello2\n");
/**getting to know the size of the picture**/
myfile.getline (currentLine,100,'\n');
myfile.getline (currentLine,100,'\n');
//myfile.getline (currentLine,100, '\n');
//myfile.getline (currentLine,1000);
while(currentLine[0]=='#')
{
myfile.getline (currentLine,10,'\n');
//printf("hello3\n");
}
std::cout << "current Line: " << currentLine << std::endl;
xSize = strtol(currentLine,&ptr,0);
ySize = atoi(ptr);
//printf("xSize is %d and ySize = %d \n ",xSize,ySize);
myfile.getline (currentLine,10,'\n'); //to get to the max colour
FILE * testfile;
testfile = fopen ("testlog.txt","w+");
/**start the reading part**/
while (yCount<ySize)
{
xCount = 0;
while (xCount<xSize) //ga die einzelne Reihe in xrichtig düre
{
//printf("newline\n");
myfile.getline (currentLine,10000,'\n'); //to get tho the start of the image the first time!
//if (xCount==0) //to read out the color of the specific point
//{
color = strtol(currentLine,&ptr,10);
//printf("hello5a, color = %d\n",color);
//}
// else
// {
// color = strtol(ptr,&ptr,10);
// printf("hello5b, color = %ld\n",color);
// }
if (color==0)
{
for (cx=0;cx<(nodemeters/GRID_SIZE) ;cx++)
{
for (cy=0;cy<(nodemeters/GRID_SIZE) ;cy++)
{
xi=xIslandLongitude - (nodemeters/GRID_SIZE)*xIsland +(nodemeters/GRID_SIZE)*xCount + cx;
yi=yIslandLatitude + (nodemeters/GRID_SIZE)*yIsland -(nodemeters/GRID_SIZE)*yCount - cy;
if (map.contains(xi,yi))
{
//printf("black at %d,%d \n",xi,yi);
map(xi,yi).cost=1000;
fprintf(testfile, "%d %d\n", xi, yi);
}
}
}
}
xCount++;
}
yCount++;
}
fclose (testfile);
myfile.close();
}
else std::cout << "Unable to open file";
}
/**------------end Oceanmap2d.cpp------------------**/