-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.cpp
187 lines (147 loc) · 4.47 KB
/
map.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
#include "map.h"
#include <iostream>
using namespace std;
map::map()
{
//this->StaticObj = NULL;
//this->NavMeshes = NULL;
this->NumOfPolygons = 0;
this->NumOfObstacles = 0;
}
map::~map()
{
/*if (this->StaticObj != NULL)
delete [] StaticObj;
if (this->NavMeshes != NULL)
delete [] NavMeshes;*/
}
/*bool map::initialize(obstacle* StaticObj, int NumOfObstacles, ConvexPolygon* NavMeshes, int NumOfPolygons)
{
this->StaticObj = StaticObj;
this->NumOfObstacles = NumOfObstacles;
this->NavMeshes = NavMeshes;
this->NumOfPolygons = NumOfPolygons;
if (NavMeshes != NULL)
createGraphFromMesh();
return true;
}*/
bool map::initialize()
{
createGraphFromMesh();
/*ArrayList<int> edges; int size, i, j;
for (i = 0; i < NumOfPolygons; i++)
{
cout << i << ": ";
edges = Graph[i].getAllEdgeLeftToRight(size);
for (j = 0; j < size - 1; j++)
{
cout << edges[j] << ", ";
}
cout << edges[j] << "\n";
}*/
return true;
}
void map::addObstacle(obstacle* obj)
{
StaticObj[NumOfObstacles++] = *obj;
}
void map::addNavMesh(ConvexPolygon* poly)
{
NavMeshes[NumOfPolygons++] = *poly;
}
void map::render()
{
int i;
for (i = 0; i < NumOfObstacles; i++)
StaticObj[i].render();
glColor3f(1.0f, 0, 1.0f);
for (i = 0; i < NumOfPolygons; i++)
NavMeshes[i].render();
}
stack<Point2D>* map::getOptimalPath(Point2D StartPos, Point2D FinishPos)
{
int StartIndex = -1, FinishIndex = -1, CurrentIndex;
int i;
for (i = 0; i < NumOfPolygons; i++)
{
if (NavMeshes[i].inclusionOfPoint(StartPos))
StartIndex = i;
if (NavMeshes[i].inclusionOfPoint(FinishPos))
FinishIndex = i;
}
if (StartIndex == -1 || FinishIndex == -1)
return NULL;
else
{
stack<Point2D>* path = new stack<Point2D>;
if (StartIndex == FinishIndex)
{
path->push(FinishPos);
path->push(StartPos);
return path;
}
ArrayList<Vertex> temp_graph = Graph;
explore(temp_graph, StartPos, StartIndex, FinishPos, FinishIndex);
CurrentIndex = temp_graph[FinishIndex].getParent();
path->push(FinishPos);
while (CurrentIndex != StartIndex)
{
path->push(NavMeshes[CurrentIndex].getCentroid());
CurrentIndex = temp_graph[CurrentIndex].getParent();
}
path->push(StartPos);
return path;
}
}
void map::explore(ArrayList<Vertex>& temp_graph, Point2D StartPos, int StartIndex, Point2D FinishPos, int FinishIndex)
{
int CurrentIndex = StartIndex, NumOfEdges, i, NextIndex;
GLfloat new_g_value;
PriorityQueue<float> PQ;
ArrayList<int> edges;
temp_graph[StartIndex].setCostSoFar(0.0);
temp_graph[StartIndex].setVisitState(true);
PQ.insert(StartIndex, getDistance(StartPos, FinishPos));
while(! PQ.isEmpty())
{
CurrentIndex = PQ.remove();
if (CurrentIndex == FinishIndex)
return;
else
{
edges = temp_graph[CurrentIndex].getAllEdgeLeftToRight(NumOfEdges);
for (i = 0; i < NumOfEdges; i++)
{
NextIndex = edges[i];
new_g_value = temp_graph[CurrentIndex].getCostSoFar() + getDistance(NavMeshes[CurrentIndex].getCentroid(), NavMeshes[NextIndex].getCentroid());
if ((!temp_graph[NextIndex].isVisited()) || new_g_value < temp_graph[NextIndex].getCostSoFar())
{
temp_graph[NextIndex].setCostSoFar(new_g_value);
temp_graph[NextIndex].setParent(CurrentIndex);
if (!temp_graph[NextIndex].isVisited())
temp_graph[NextIndex].setVisitState(true);
PQ.insert(NextIndex, temp_graph[NextIndex].getCostSoFar() + getDistance(NavMeshes[NextIndex].getCentroid(), FinishPos));
}
}
}
}
}
void map::createGraphFromMesh()
{
int i, j;
for (i = 0; i < NumOfPolygons; i++)
{
for (j = 0; j < NumOfPolygons; j++)
{
if (NavMeshes[i].isNeighbor(&(NavMeshes[j])) && j != i)
{
Graph[i].insertEdge(j);
//Graph[j].insertEdge(i);
}
}
}
}
GLfloat map::getDistance(Point2D StartPos, Point2D FinishPos)
{
return (GLfloat)(sqrt(pow(StartPos.x - FinishPos.x, 2) + pow(StartPos.y - FinishPos.y, 2)));
}