jps 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/jps/extconf.rb +3 -0
- data/ext/jps/jps.hh +1539 -0
- data/ext/jps/map_jps.cpp +230 -0
- data/ext/jps/map_jps.h +22 -0
- data/ext/jps/rjps.cpp +126 -0
- metadata +49 -0
data/ext/jps/map_jps.cpp
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
#include "jps.hh"
|
2
|
+
#include <iostream>
|
3
|
+
#include <algorithm>
|
4
|
+
#include <string>
|
5
|
+
#include <string.h>
|
6
|
+
#include <assert.h>
|
7
|
+
#include <vector>
|
8
|
+
#include "map_jps.h"
|
9
|
+
|
10
|
+
using namespace std;
|
11
|
+
|
12
|
+
Map *map = NULL;
|
13
|
+
vector<JPS::Position> *path = NULL;
|
14
|
+
// std::vector<std::string> out;
|
15
|
+
|
16
|
+
inline unsigned Map::operator()(unsigned x, unsigned y) const
|
17
|
+
{
|
18
|
+
unsigned canwalk = x < this->w && y < this->h;
|
19
|
+
if(canwalk)
|
20
|
+
canwalk = data[y][x] != 1;
|
21
|
+
|
22
|
+
return canwalk;
|
23
|
+
}
|
24
|
+
|
25
|
+
void Map::clear()
|
26
|
+
{
|
27
|
+
int i;
|
28
|
+
for(i = 0;i < this->h; ++i)
|
29
|
+
{
|
30
|
+
delete[] this->data[i];
|
31
|
+
}
|
32
|
+
delete[] this->data;
|
33
|
+
}
|
34
|
+
|
35
|
+
void Map::init(int width, int height)
|
36
|
+
{
|
37
|
+
this->w = width;
|
38
|
+
this->h = height;
|
39
|
+
this->data = new int*[height];
|
40
|
+
int i,j;
|
41
|
+
for(j = 0; j < height; ++j)
|
42
|
+
{
|
43
|
+
this->data[j] = new int[width];
|
44
|
+
for(i = 0; i < width; ++i)
|
45
|
+
{
|
46
|
+
this->data[j][i] = 0;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
void clearMap()
|
52
|
+
{
|
53
|
+
if(!map){
|
54
|
+
delete map;
|
55
|
+
map = NULL;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
void clearPath()
|
60
|
+
{
|
61
|
+
if(!path)
|
62
|
+
{
|
63
|
+
delete path;
|
64
|
+
path = NULL;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
vector<JPS::Position> * findPath(int startX, int startY, int stopX, int stopY)
|
69
|
+
{
|
70
|
+
if(!map || !path){
|
71
|
+
return NULL;
|
72
|
+
}
|
73
|
+
|
74
|
+
path->clear();
|
75
|
+
|
76
|
+
//开始和结束位置不能是障碍 不然找不到路径
|
77
|
+
//这里先设置成非障碍,查询结束后重置回去
|
78
|
+
int oldStartFlag = map->data[startY][startX];
|
79
|
+
int oldStopFlag = map->data[stopY][stopX];
|
80
|
+
map->data[startY][startX] = 0;
|
81
|
+
map->data[stopY][stopX] = 0;
|
82
|
+
|
83
|
+
JPS::PathVector waypoints;
|
84
|
+
JPS::PathVector result;
|
85
|
+
waypoints.push_back(JPS::Pos(JPS::PosType(startX), startY));
|
86
|
+
waypoints.push_back(JPS::Pos(JPS::PosType(stopX), stopY));
|
87
|
+
|
88
|
+
JPS::Searcher<Map> search(*map);
|
89
|
+
search.findPath(result, waypoints[0], waypoints[1], 1);
|
90
|
+
|
91
|
+
int curX = startX;
|
92
|
+
int curY = startY;
|
93
|
+
for(JPS::PathVector::iterator it = result.begin(); it != result.end(); ++it)
|
94
|
+
{
|
95
|
+
int x = it -> x;
|
96
|
+
int y = it -> y;
|
97
|
+
|
98
|
+
//不反回目标点
|
99
|
+
// if(x == stopX && y == stopY)
|
100
|
+
// {
|
101
|
+
// break;
|
102
|
+
// }
|
103
|
+
|
104
|
+
if(curX != x && curY != y)
|
105
|
+
{
|
106
|
+
JPS::Position pos1;
|
107
|
+
if(map->data[curY][x] == 0)
|
108
|
+
{
|
109
|
+
pos1.x = x;
|
110
|
+
pos1.y = curY;
|
111
|
+
path->push_back(pos1);
|
112
|
+
curX = x;
|
113
|
+
}
|
114
|
+
else if(map->data[y][curX] == 0)
|
115
|
+
{
|
116
|
+
pos1.x = curX;
|
117
|
+
pos1.y = y;
|
118
|
+
path->push_back(pos1);
|
119
|
+
curY = y;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
JPS::Position pos2;
|
124
|
+
pos2.x = x;
|
125
|
+
pos2.y = y;
|
126
|
+
path->push_back(pos2);
|
127
|
+
curX = x;
|
128
|
+
curY = y;
|
129
|
+
}
|
130
|
+
|
131
|
+
//查询结束后重置回去
|
132
|
+
map->data[startY][startX] = oldStartFlag;
|
133
|
+
map->data[stopY][stopX] = oldStopFlag;
|
134
|
+
return path;
|
135
|
+
}
|
136
|
+
|
137
|
+
JPS::Position findDstPos(int startX,int startY, int endX, int endY,int step)
|
138
|
+
{
|
139
|
+
JPS::Position dstPos;
|
140
|
+
if(!map){
|
141
|
+
return dstPos;
|
142
|
+
}
|
143
|
+
|
144
|
+
int oldStartFlag = map->data[startY][startX];
|
145
|
+
int oldStopFlag = map->data[endY][endX];
|
146
|
+
map->data[startY][startX] = 0;
|
147
|
+
map->data[endY][endX] = 0;
|
148
|
+
|
149
|
+
JPS::PathVector waypoints;
|
150
|
+
|
151
|
+
waypoints.push_back(JPS::Pos(startX, startY));
|
152
|
+
waypoints.push_back(JPS::Pos(endX, endY));
|
153
|
+
|
154
|
+
JPS::PathVector result;
|
155
|
+
JPS::Searcher<Map> search(*map);
|
156
|
+
search.findPath(result, waypoints[0], waypoints[1], 1);
|
157
|
+
|
158
|
+
int curStep = 0;
|
159
|
+
int curX = startX;
|
160
|
+
int curY = startY;
|
161
|
+
for(JPS::PathVector::iterator it = result.begin(); it != result.end(); ++it)
|
162
|
+
{
|
163
|
+
int y = it->y;
|
164
|
+
int x = it->x;
|
165
|
+
//到最后一个点的话使用倒数第二个点
|
166
|
+
if(x == endX && y == endY)
|
167
|
+
{
|
168
|
+
dstPos.x = curX;
|
169
|
+
dstPos.y = curY;
|
170
|
+
break;
|
171
|
+
}
|
172
|
+
|
173
|
+
if(curX != x && curY != y)
|
174
|
+
{
|
175
|
+
vector<int> v1;
|
176
|
+
if(map->data[curY][x] == 0)
|
177
|
+
curX = x;
|
178
|
+
else if(map->data[y][curX] == 0)
|
179
|
+
curY = y;
|
180
|
+
curStep++;
|
181
|
+
|
182
|
+
if(curStep == step)
|
183
|
+
{
|
184
|
+
dstPos.x = curX;
|
185
|
+
dstPos.y = curY;
|
186
|
+
break;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
curX = x;
|
191
|
+
curY = y;
|
192
|
+
curStep++;
|
193
|
+
|
194
|
+
if(curStep == step )
|
195
|
+
{
|
196
|
+
dstPos.x = curX;
|
197
|
+
dstPos.y = curY;
|
198
|
+
break;
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
map->data[startY][startX] = oldStartFlag;
|
203
|
+
map->data[endY][endX] = oldStopFlag;
|
204
|
+
|
205
|
+
return dstPos;
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
void initMap(int width,int height)
|
210
|
+
{
|
211
|
+
if(map != NULL){
|
212
|
+
clearMap();
|
213
|
+
}
|
214
|
+
if(path != NULL){
|
215
|
+
clearPath();
|
216
|
+
}
|
217
|
+
|
218
|
+
map = new Map();
|
219
|
+
map->init(width, height);
|
220
|
+
|
221
|
+
path = new vector<JPS::Position>();
|
222
|
+
}
|
223
|
+
|
224
|
+
void setMapPos(int posX, int posY,int flag)
|
225
|
+
{
|
226
|
+
if(!map){
|
227
|
+
return;
|
228
|
+
}
|
229
|
+
map->data[posY][posX] = flag;
|
230
|
+
}
|
data/ext/jps/map_jps.h
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#ifndef MAP_JPS_H_
|
2
|
+
#define MAP_JPS_H_
|
3
|
+
#include "jps.hh"
|
4
|
+
#include <vector>
|
5
|
+
#include <iostream>
|
6
|
+
using namespace std;
|
7
|
+
struct Map{
|
8
|
+
void init(int width, int height);
|
9
|
+
void clear();
|
10
|
+
unsigned operator()(unsigned x, unsigned y) const;
|
11
|
+
int w,h;
|
12
|
+
int **data;
|
13
|
+
};
|
14
|
+
|
15
|
+
void clearMap();
|
16
|
+
void clearPath();
|
17
|
+
void initMap(int width,int height);
|
18
|
+
vector<JPS::Position> * findPath(int startX, int startY, int stopX, int stopY);
|
19
|
+
JPS::Position findDstPos(int startX,int startY, int endX, int endY,int step);
|
20
|
+
void initMap(int width,int height);
|
21
|
+
void setMapPos(int posX, int posY,int flag);
|
22
|
+
#endif
|
data/ext/jps/rjps.cpp
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
|
3
|
+
#include "map_jps.h"
|
4
|
+
#include "jps.hh"
|
5
|
+
#include <vector>
|
6
|
+
|
7
|
+
extern vector<JPS::Position> *path;
|
8
|
+
extern Map *map;
|
9
|
+
|
10
|
+
|
11
|
+
VALUE find_path(VALUE self, VALUE start_x, VALUE start_y, VALUE stop_x, VALUE stop_y)
|
12
|
+
{
|
13
|
+
if(map == NULL)
|
14
|
+
return rb_str_new2("Map is null, plz init map first!");
|
15
|
+
if(path == NULL)
|
16
|
+
return rb_str_new2("path is null, plz init map first!");
|
17
|
+
|
18
|
+
int startX = NUM2INT(start_x);
|
19
|
+
int startY = NUM2INT(start_y);
|
20
|
+
int stopX = NUM2INT(stop_x);
|
21
|
+
int stopY = NUM2INT(stop_y);
|
22
|
+
|
23
|
+
vector<JPS::Position> * p;
|
24
|
+
p = findPath(startX, startY, stopX, stopY);
|
25
|
+
|
26
|
+
VALUE arr1 = rb_ary_new();
|
27
|
+
int i;
|
28
|
+
for(i=0; i< p->size(); ++i)
|
29
|
+
{
|
30
|
+
VALUE arr2 = rb_ary_new();
|
31
|
+
JPS::Position ret = (*path)[i];
|
32
|
+
rb_ary_push(arr2, INT2NUM(ret.x));
|
33
|
+
rb_ary_push(arr2, INT2NUM(ret.y));
|
34
|
+
rb_ary_push(arr1, arr2);
|
35
|
+
}
|
36
|
+
|
37
|
+
return arr1;
|
38
|
+
// return INT2FIX(p);
|
39
|
+
}
|
40
|
+
|
41
|
+
VALUE get_path_length(VALUE self)
|
42
|
+
{
|
43
|
+
return INT2NUM(path->size());
|
44
|
+
}
|
45
|
+
|
46
|
+
VALUE clear_map(VALUE self)
|
47
|
+
{
|
48
|
+
clearMap();
|
49
|
+
clearPath();
|
50
|
+
return INT2NUM(0);
|
51
|
+
}
|
52
|
+
|
53
|
+
VALUE find_dst_pos(VALUE self, VALUE start_x, VALUE start_y, VALUE end_x, VALUE end_y, VALUE step)
|
54
|
+
{
|
55
|
+
if(map == NULL)
|
56
|
+
return rb_str_new2("Map is null, plz init map first!");
|
57
|
+
if(path == NULL)
|
58
|
+
return rb_str_new2("path is null, plz init map first!");
|
59
|
+
|
60
|
+
int startX = NUM2INT(start_x);
|
61
|
+
int startY = NUM2INT(start_y);
|
62
|
+
int endX = NUM2INT(end_x);
|
63
|
+
int endY = NUM2INT(end_y);
|
64
|
+
int sp = NUM2INT(step);
|
65
|
+
|
66
|
+
if(startX < 0 || startX >= map->w)
|
67
|
+
return rb_str_new2("startX is overflow!");
|
68
|
+
if(startY < 0 || startY >= map->w)
|
69
|
+
return rb_str_new2("startY is overflow!");
|
70
|
+
if(endX < 0 || endX >= map->w)
|
71
|
+
return rb_str_new2("endX is overflow!");
|
72
|
+
if(endY < 0 || endY >= map->w)
|
73
|
+
return rb_str_new2("endY is overflow!");
|
74
|
+
|
75
|
+
JPS::Position ret = findDstPos(startX, startY, endX, endY, sp);
|
76
|
+
|
77
|
+
VALUE point = rb_ary_new();
|
78
|
+
rb_ary_push(point, INT2NUM(ret.x));
|
79
|
+
rb_ary_push(point, INT2NUM(ret.y));
|
80
|
+
return point;
|
81
|
+
|
82
|
+
}
|
83
|
+
|
84
|
+
VALUE get_path_point(VALUE self, VALUE idx)
|
85
|
+
{
|
86
|
+
int index = NUM2INT(idx);
|
87
|
+
JPS::Position ret = (*path)[index];
|
88
|
+
VALUE point = rb_ary_new();
|
89
|
+
rb_ary_push(point, INT2NUM(ret.x));
|
90
|
+
rb_ary_push(point, INT2NUM(ret.y));
|
91
|
+
return point;
|
92
|
+
}
|
93
|
+
|
94
|
+
VALUE set_map_pos(VALUE self, VALUE posX, VALUE posY, VALUE flag)
|
95
|
+
{
|
96
|
+
if(map == NULL)
|
97
|
+
return rb_str_new2("Map is null, plz init map first!");
|
98
|
+
int x = NUM2INT(posX);
|
99
|
+
int y = NUM2INT(posY);
|
100
|
+
int fg = NUM2INT(flag);
|
101
|
+
|
102
|
+
setMapPos(x, y, fg);
|
103
|
+
return INT2NUM(0);
|
104
|
+
}
|
105
|
+
|
106
|
+
VALUE init_map(VALUE self, VALUE width, VALUE height)
|
107
|
+
{
|
108
|
+
int w = NUM2INT(width);
|
109
|
+
int h = NUM2INT(height);
|
110
|
+
initMap(w, h);
|
111
|
+
return INT2NUM(0);
|
112
|
+
}
|
113
|
+
|
114
|
+
|
115
|
+
extern "C" void Init_rjps()
|
116
|
+
{
|
117
|
+
VALUE jps = rb_define_module("Jps");
|
118
|
+
rb_define_module_function(jps, "find_path", RUBY_METHOD_FUNC(find_path), 4);
|
119
|
+
rb_define_module_function(jps, "init_map", RUBY_METHOD_FUNC(init_map), 2);
|
120
|
+
rb_define_module_function(jps, "get_path_length", RUBY_METHOD_FUNC(get_path_length), 0);
|
121
|
+
rb_define_module_function(jps, "get_path_point", RUBY_METHOD_FUNC(get_path_point), 1);
|
122
|
+
rb_define_module_function(jps, "set_map_pos", RUBY_METHOD_FUNC(set_map_pos), 3);
|
123
|
+
rb_define_module_function(jps, "clear_map", RUBY_METHOD_FUNC(clear_map), 0);
|
124
|
+
rb_define_module_function(jps, "find_dst_pos", RUBY_METHOD_FUNC(find_dst_pos), 5);
|
125
|
+
}
|
126
|
+
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jps
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ljf
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: jps automated test application
|
14
|
+
email: xxxxxxx@xxxnets.com
|
15
|
+
executables: []
|
16
|
+
extensions:
|
17
|
+
- ext/jps/extconf.rb
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ext/jps/extconf.rb
|
21
|
+
- ext/jps/jps.hh
|
22
|
+
- ext/jps/map_jps.cpp
|
23
|
+
- ext/jps/map_jps.h
|
24
|
+
- ext/jps/rjps.cpp
|
25
|
+
homepage: http://172.17.2.44:9527/welcome/index
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.6.14
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: jps
|
49
|
+
test_files: []
|