5
5
author: Atsushi Sakai
6
6
7
7
"""
8
-
8
+ from functools import total_ordering
9
9
import matplotlib .pyplot as plt
10
10
import numpy as np
11
11
12
12
13
+ @total_ordering
14
+ class FloatGrid :
15
+
16
+ def __init__ (self , init_val = 0.0 ):
17
+ self .data = init_val
18
+
19
+ def get_float_data (self ):
20
+ return self .data
21
+
22
+ def __eq__ (self , other ):
23
+ if not isinstance (other , FloatGrid ):
24
+ return NotImplemented
25
+ return self .get_float_data () == other .get_float_data ()
26
+
27
+ def __lt__ (self , other ):
28
+ if not isinstance (other , FloatGrid ):
29
+ return NotImplemented
30
+ return self .get_float_data () < other .get_float_data ()
31
+
32
+
13
33
class GridMap :
14
34
"""
15
35
GridMap class
16
36
"""
17
37
18
38
def __init__ (self , width , height , resolution ,
19
- center_x , center_y , init_val = 0.0 ):
39
+ center_x , center_y , init_val = FloatGrid ( 0.0 ) ):
20
40
"""__init__
21
41
22
42
:param width: number of grid for width
23
- :param height: number of grid for heigt
43
+ :param height: number of grid for height
24
44
:param resolution: grid resolution [m]
25
45
:param center_x: center x position [m]
26
46
:param center_y: center y position [m]
@@ -35,8 +55,9 @@ def __init__(self, width, height, resolution,
35
55
self .left_lower_x = self .center_x - self .width / 2.0 * self .resolution
36
56
self .left_lower_y = self .center_y - self .height / 2.0 * self .resolution
37
57
38
- self .ndata = self .width * self .height
39
- self .data = [init_val ] * self .ndata
58
+ self .n_data = self .width * self .height
59
+ self .data = [init_val ] * self .n_data
60
+ self .data_type = type (init_val )
40
61
41
62
def get_value_from_xy_index (self , x_ind , y_ind ):
42
63
"""get_value_from_xy_index
@@ -49,7 +70,7 @@ def get_value_from_xy_index(self, x_ind, y_ind):
49
70
50
71
grid_ind = self .calc_grid_index_from_xy_index (x_ind , y_ind )
51
72
52
- if 0 <= grid_ind < self .ndata :
73
+ if 0 <= grid_ind < self .n_data :
53
74
return self .data [grid_ind ]
54
75
else :
55
76
return None
@@ -101,7 +122,7 @@ def set_value_from_xy_index(self, x_ind, y_ind, val):
101
122
102
123
grid_ind = int (y_ind * self .width + x_ind )
103
124
104
- if 0 <= grid_ind < self .ndata :
125
+ if 0 <= grid_ind < self .n_data and isinstance ( val , self . data_type ) :
105
126
self .data [grid_ind ] = val
106
127
return True # OK
107
128
else :
@@ -138,6 +159,27 @@ def calc_grid_index_from_xy_index(self, x_ind, y_ind):
138
159
grid_ind = int (y_ind * self .width + x_ind )
139
160
return grid_ind
140
161
162
+ def calc_xy_index_from_grid_index (self , grid_ind ):
163
+ y_ind , x_ind = divmod (grid_ind , self .width )
164
+ return x_ind , y_ind
165
+
166
+ def calc_grid_index_from_xy_pos (self , x_pos , y_pos ):
167
+ """get_xy_index_from_xy_pos
168
+
169
+ :param x_pos: x position [m]
170
+ :param y_pos: y position [m]
171
+ """
172
+ x_ind = self .calc_xy_index_from_position (
173
+ x_pos , self .left_lower_x , self .width )
174
+ y_ind = self .calc_xy_index_from_position (
175
+ y_pos , self .left_lower_y , self .height )
176
+
177
+ return self .calc_grid_index_from_xy_index (x_ind , y_ind )
178
+
179
+ def calc_grid_central_xy_position_from_grid_index (self , grid_ind ):
180
+ x_ind , y_ind = self .calc_xy_index_from_grid_index (grid_ind )
181
+ return self .calc_grid_central_xy_position_from_xy_index (x_ind , y_ind )
182
+
141
183
def calc_grid_central_xy_position_from_xy_index (self , x_ind , y_ind ):
142
184
x_pos = self .calc_grid_central_xy_position_from_index (
143
185
x_ind , self .left_lower_x )
@@ -156,39 +198,40 @@ def calc_xy_index_from_position(self, pos, lower_pos, max_index):
156
198
else :
157
199
return None
158
200
159
- def check_occupied_from_xy_index (self , xind , yind , occupied_val = 1.0 ):
201
+ def check_occupied_from_xy_index (self , x_ind , y_ind , occupied_val ):
160
202
161
- val = self .get_value_from_xy_index (xind , yind )
203
+ val = self .get_value_from_xy_index (x_ind , y_ind )
162
204
163
205
if val is None or val >= occupied_val :
164
206
return True
165
207
else :
166
208
return False
167
209
168
- def expand_grid (self ):
169
- xinds , yinds = [], []
210
+ def expand_grid (self , occupied_val = FloatGrid ( 1.0 ) ):
211
+ x_inds , y_inds , values = [], [], []
170
212
171
213
for ix in range (self .width ):
172
214
for iy in range (self .height ):
173
- if self .check_occupied_from_xy_index (ix , iy ):
174
- xinds .append (ix )
175
- yinds .append (iy )
176
-
177
- for (ix , iy ) in zip (xinds , yinds ):
178
- self .set_value_from_xy_index (ix + 1 , iy , val = 1.0 )
179
- self .set_value_from_xy_index (ix , iy + 1 , val = 1.0 )
180
- self .set_value_from_xy_index (ix + 1 , iy + 1 , val = 1.0 )
181
- self .set_value_from_xy_index (ix - 1 , iy , val = 1.0 )
182
- self .set_value_from_xy_index (ix , iy - 1 , val = 1.0 )
183
- self .set_value_from_xy_index (ix - 1 , iy - 1 , val = 1.0 )
215
+ if self .check_occupied_from_xy_index (ix , iy , occupied_val ):
216
+ x_inds .append (ix )
217
+ y_inds .append (iy )
218
+ values .append (self .get_value_from_xy_index (ix , iy ))
219
+
220
+ for (ix , iy , value ) in zip (x_inds , y_inds , values ):
221
+ self .set_value_from_xy_index (ix + 1 , iy , val = value )
222
+ self .set_value_from_xy_index (ix , iy + 1 , val = value )
223
+ self .set_value_from_xy_index (ix + 1 , iy + 1 , val = value )
224
+ self .set_value_from_xy_index (ix - 1 , iy , val = value )
225
+ self .set_value_from_xy_index (ix , iy - 1 , val = value )
226
+ self .set_value_from_xy_index (ix - 1 , iy - 1 , val = value )
184
227
185
228
@staticmethod
186
229
def check_inside_polygon (iox , ioy , x , y ):
187
230
188
- npoint = len (x ) - 1
231
+ n_point = len (x ) - 1
189
232
inside = False
190
- for i1 in range (npoint ):
191
- i2 = (i1 + 1 ) % (npoint + 1 )
233
+ for i1 in range (n_point ):
234
+ i2 = (i1 + 1 ) % (n_point + 1 )
192
235
193
236
if x [i1 ] >= x [i2 ]:
194
237
min_x , max_x = x [i2 ], x [i1 ]
@@ -211,52 +254,54 @@ def print_grid_map_info(self):
211
254
print ("center_y:" , self .center_y )
212
255
print ("left_lower_x:" , self .left_lower_x )
213
256
print ("left_lower_y:" , self .left_lower_y )
214
- print ("ndata :" , self .ndata )
257
+ print ("n_data :" , self .n_data )
215
258
216
259
def plot_grid_map (self , ax = None ):
217
-
218
- grid_data = np .reshape (np . array ( self . data ) , (self .height , self .width ))
260
+ float_data_array = np . array ([ d . get_float_data () for d in self . data ])
261
+ grid_data = np .reshape (float_data_array , (self .height , self .width ))
219
262
if not ax :
220
263
fig , ax = plt .subplots ()
221
264
heat_map = ax .pcolor (grid_data , cmap = "Blues" , vmin = 0.0 , vmax = 1.0 )
222
265
plt .axis ("equal" )
223
- # plt.show()
224
266
225
267
return heat_map
226
268
227
269
228
- def test_polygon_set ():
270
+ def polygon_set_demo ():
229
271
ox = [0.0 , 4.35 , 20.0 , 50.0 , 100.0 , 130.0 , 40.0 ]
230
272
oy = [0.0 , - 4.15 , - 20.0 , 0.0 , 30.0 , 60.0 , 80.0 ]
231
273
232
274
grid_map = GridMap (600 , 290 , 0.7 , 60.0 , 30.5 )
233
275
234
- grid_map .set_value_from_polygon (ox , oy , 1.0 , inside = False )
276
+ grid_map .set_value_from_polygon (ox , oy , FloatGrid ( 1.0 ) , inside = False )
235
277
236
278
grid_map .plot_grid_map ()
237
279
238
280
plt .axis ("equal" )
239
281
plt .grid (True )
240
282
241
283
242
- def test_position_set ():
284
+ def position_set_demo ():
243
285
grid_map = GridMap (100 , 120 , 0.5 , 10.0 , - 0.5 )
244
286
245
- grid_map .set_value_from_xy_pos (10.1 , - 1.1 , 1.0 )
246
- grid_map .set_value_from_xy_pos (10.1 , - 0.1 , 1.0 )
247
- grid_map .set_value_from_xy_pos (10.1 , 1.1 , 1.0 )
248
- grid_map .set_value_from_xy_pos (11.1 , 0.1 , 1.0 )
249
- grid_map .set_value_from_xy_pos (10.1 , 0.1 , 1.0 )
250
- grid_map .set_value_from_xy_pos (9.1 , 0.1 , 1.0 )
287
+ grid_map .set_value_from_xy_pos (10.1 , - 1.1 , FloatGrid ( 1.0 ) )
288
+ grid_map .set_value_from_xy_pos (10.1 , - 0.1 , FloatGrid ( 1.0 ) )
289
+ grid_map .set_value_from_xy_pos (10.1 , 1.1 , FloatGrid ( 1.0 ) )
290
+ grid_map .set_value_from_xy_pos (11.1 , 0.1 , FloatGrid ( 1.0 ) )
291
+ grid_map .set_value_from_xy_pos (10.1 , 0.1 , FloatGrid ( 1.0 ) )
292
+ grid_map .set_value_from_xy_pos (9.1 , 0.1 , FloatGrid ( 1.0 ) )
251
293
252
294
grid_map .plot_grid_map ()
253
295
296
+ plt .axis ("equal" )
297
+ plt .grid (True )
298
+
254
299
255
300
def main ():
256
301
print ("start!!" )
257
302
258
- test_position_set ()
259
- test_polygon_set ()
303
+ position_set_demo ()
304
+ polygon_set_demo ()
260
305
261
306
plt .show ()
262
307
0 commit comments