-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavmesh.h
342 lines (291 loc) · 5.55 KB
/
navmesh.h
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
#ifndef NAVMESH_H
#define NAVMESH_H
#include <vector>
#include <map>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef __cplusplus
extern "C" {
#endif
class dlist;
struct dnode{
dnode():next(NULL),pre(NULL),_dlist(NULL){}
dnode *next;
dnode *pre;
dlist *_dlist;
};
class dlist{
public:
dlist():size(0){
head.next = &tail;
tail.pre = &head;
}
size_t Size(){
return size;
}
bool Empty(){
return size == 0;
}
dnode *Begin(){
if(Empty())return &tail;
return head.next;
}
dnode *End(){
return &tail;
}
void Push(dnode *n){
if(n->_dlist || n->next || n->pre) return;
tail.pre->next = n;
n->pre = tail.pre;
tail.pre = n;
n->_dlist = this;
n->next = &tail;
++size;
}
void Remove(dnode *n)
{
if(n->_dlist != this || (!n->pre && !n->next))
return;
n->pre->next = n->next;
n->next->pre = n->pre;
n->pre = n->next = NULL;
n->_dlist = NULL;
--size;
}
dnode* Pop(){
if(Empty())
return NULL;
else
{
dnode *n = head.next;
Remove(n);
return n;
}
}
private:
dnode head;
dnode tail;
size_t size;
};
struct heapele
{
int index;//index in minheap;
};
struct mapnode : public heapele,public dnode
{
mapnode *parent;
double G;//从初始点到当前点的开销
double H;//从当前点到目标点的估计开销
double F;
float x;
float y;
int i;
double v[3];
int links[3];
int z;
};
struct minheap{
private:
int size;
int max_size;
typedef bool (*less)(struct heapele*l,struct heapele*r);
typedef void (*clear)(struct heapele*);
less cmp_function;
clear clear_function;
heapele **elements;
private:
void swap(int idx1,int idx2)
{
heapele *ele = elements[idx1];
elements[idx1] = elements[idx2];
elements[idx2] = ele;
elements[idx1]->index = idx1;
elements[idx2]->index = idx2;
}
int parent(int idx){
return idx/2;
}
int left(int idx){
return idx*2;
}
inline int right(int idx){
return idx*2+1;
}
void up(int idx){
int p = parent(idx);
while(p)
{
assert(elements[idx]);
assert(elements[p]);
if(cmp_function(elements[idx],elements[p]))
{
swap(idx,p);
idx = p;
p = parent(idx);
}
else
break;
}
}
void down(int idx){
int l = left(idx);
int r = right(idx);
int min = idx;
if(l <= size)
{
assert(elements[l]);
assert(elements[idx]);
}
if(l <= size && cmp_function(elements[l],elements[idx]))
min = l;
if(r <= size)
{
assert(elements[r]);
assert(elements[min]);
}
if(r <= size && cmp_function(elements[r],elements[min]))
min = r;
if(min != idx)
{
swap(idx,min);
down(min);
}
}
public:
minheap(int size,less cmp_func,clear clear_func):size(0),max_size(size),cmp_function(cmp_func),clear_function(clear_func){
elements = (heapele **)calloc(size,sizeof(*elements));
}
~minheap(){
free(elements);
}
void change(heapele *e)
{
int idx = e->index;
down(idx);
if(idx == e->index)
up(idx);
}
void insert(heapele *e)
{
if(e->index)
return change(e);
if(size >= max_size-1)
{
//expand the heap
unsigned int new_size = max_size*2;
heapele** tmp = (heapele**)calloc(new_size,sizeof(*tmp));
if(!tmp) return;
memcpy(tmp,elements,max_size*sizeof(*tmp));
free(elements);
elements = tmp;
max_size = new_size;
}
++size;
elements[size] = e;
e->index = size;
up(e->index);
}
/*void remove(heapele *e)
{
heapele **back = (heapele**)calloc(1,sizeof(*back)*(size-1));
int i = 1;
int c = 1;
for( ; i <= size;++i)
{
elements[i]->index = 0;
if(elements[i] != e)
back[c++] = elements[i];
}
memset(&(elements[0]),0,sizeof(*back)*max_size);
size = 0;
i = 1;
for(; i < c; ++i) insert(back[i]);
free(back);
}*/
heapele* minheap_min()
{
if(!size) return NULL;
return elements[1];
}
//return the min element and remove it
heapele* popmin()
{
if(size)
{
heapele *e = elements[1];
swap(1,size);
elements[size] = NULL;
--size;
down(1);
e->index = 0;
return e;
}
return NULL;
}
void Clear()
{
for(int i = 1; i <= size; ++i){
elements[i]->index = 0;
clear_function(elements[i]);
}
size = 0;
}
int Size()
{
return size;
}
};
struct Navmesh
{
float* verts;
int nverts;
unsigned short* tris;
int ntris;
mapnode* defaultMap;
dlist* close_list;
minheap* open_list;
};
// Creates navmesh from a polygon.
struct Navmesh* navmeshCreateEx(std::vector<int> const& trisList, std::vector<std::pair<float, float> > const& pointList);
// Find nearest triangle
int navmeshFindNearestTri(struct Navmesh* nav, const float* pos, float* nearest);
// Find path
int navmeshFindPath(struct Navmesh* nav, const float* start, const float* end, unsigned short* path, const int maxpath);
// optimal Find path
int navmeshFindBestPath(struct Navmesh* nav, const float* start, const float* end, unsigned short* path);
// Find tight rope path
int navmeshStringPull(struct Navmesh* nav, const float* start, const float* end,
const unsigned short* path, const int npath,
float* pts, const int maxpts);
// Deletes navmesh.
void navmeshDelete(struct Navmesh* nav);
#define MAX_CORNERS 64
#define AGENT_MAX_TRAIL 64
#define AGENT_MAX_PATH 128
#define VEL_HIST_SIZE 6
struct NavmeshAgent
{
float pos[2];
float target[2];
float oldpos[2];
float corners[MAX_CORNERS];
int ncorners;
unsigned short path[AGENT_MAX_PATH];
int npath;
};
struct MemPool
{
unsigned char* buf;
unsigned int cap;
unsigned int size;
};
//对外查找接口
int FindPath(struct Navmesh* nav, struct NavmeshAgent* agent);
void agentInit(struct NavmeshAgent* agent);
#ifdef __cplusplus
}
#endif
#endif