-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmap.py
More file actions
347 lines (275 loc) · 8.97 KB
/
map.py
File metadata and controls
347 lines (275 loc) · 8.97 KB
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
# import json
# import matplotlib.pyplot as plt
# # Load the JSON data
# with open('map.json') as f:
# data = json.load(f)
# border_nums = data['umf']['maps'][0]['borders'][0]['geometry']['ids']
# border_layer = data['umf']['maps'][0]['points2d']
# # Create a dictionary mapping border IDs to coordinates
# id_to_coord = {}
# for point in border_layer:
# id_to_coord[point['id']] = point['coordinates']
# # Get the coordinates associated with border_nums IDs
# border_coords = [id_to_coord[id] for id in border_nums[0]]
# # Loop through all layers in the map
# for layer in data['umf']['maps'][0]['layers']:
# # Check the layer type and get the corresponding coordinates
# if layer['layer_type'] == 'coverage':
# coverage_layer = layer['geometry']['coordinates']
# elif layer['layer_type'] == 'coverage_poly':
# coverage_poly_layer = layer['geometry']['coordinates']
# coverage_poly_layer = coverage_poly_layer[0][0] # Get the first set of coordinates
# print('Coverage poly layer coordinates:', coverage_poly_layer)
# print("cov layer\n\n",coverage_layer)
# print ("cov_poly\n\n",coverage_poly_layer)
# print("border \n\n",border_nums)
# print("border \n\n",border_coords)
# # Create a figure and axes
# fig, ax = plt.subplots()
# # Plot the border coordinates
# x, y = zip(*border_coords)
# ax.plot(x, y,label='Border')
# # Plot the coverage layer coordinates
# x, y = zip(*coverage_poly_layer)
# ax.scatter(x, y, label='Coverage')
# # Show the plot
# plt.show()
# import json
# import matplotlib.pyplot as plt
# import numpy as np
# # Load JSON data
# with open('mapData.json') as f:
# data = json.load(f)
# # Extract coordinates
# outer_points = [(p['coordinates'][0], p['coordinates'][1]) for p in data['maps'][0]['points2d'] if p['id'].startswith('10000')]
# rug_points = [(p['coordinates'][0], p['coordinates'][1]) for p in data['maps'][0]['points2d'] if p['id'].startswith('1000')]
# start_pose = data['maps'][0]['poses2d'][0]
# # Create plot
# fig, ax = plt.subplots()
# # Draw outer boundary
# for i in range(len(outer_points)-1):
# ax.plot([outer_points[i][0], outer_points[i+1][0]],
# [outer_points[i][1], outer_points[i+1][1]], 'black')
# # Fill floor
# ax.fill([p[0] for p in outer_points],
# [p[1] for p in outer_points],
# 'tan')
# #Draw rug
# for i in range(len(rug_points)-1):
# ax.plot([rug_points[i][0], rug_points[i+1][0]],
# [rug_points[i][1], rug_points[i+1][1]], 'darkgreen')
# # Draw start pose
# ax.plot(start_pose['coordinates'][0], start_pose['coordinates'][1], 'bv')
# ax.arrow(start_pose['coordinates'][0], start_pose['coordinates'][1],
# 0.3 * np.cos(start_pose['ori_rad']),
# 0.3 * np.sin(start_pose['ori_rad']),
# head_width=0.05, head_length=0.1, fc='b', ec='b')
# plt.axis('equal')
# plt.show()
# import matplotlib.pyplot as plt
# import matplotlib.patches as patches
# from collections import defaultdict
# points2d = [
# [100001, [-1.9838, -0.116]],
# [100000, [-1.9838, 1.5575]],
# [100011, [-1.7849, -0.7349]],
# [100010, [-1.7849, 1.5749]],
# [100025, [-1.7783, 0.1479]],
# [100024, [-1.7783, 1.2893]],
# [100002, [-1.6344, -0.4655]],
# [100014, [-1.2982, -0.1292]],
# [100023, [-0.9274, 1.2893]],
# [100022, [-0.9274, 1.5575]],
# [100003, [-0.8537, 0.3149]],
# [100005, [-0.7382, -0.8399]],
# [100015, [-0.7382, -0.4526]],
# [100004, [-0.7382, 0.3149]],
# [100006, [-0.1875, -0.8399]],
# [100016, [-0.1875, -0.7705]],
# [100007, [-0.1875, -0.2099]],
# [100020, [-0.1134, 1.1651]],
# [100021, [-0.1134, 1.5575]],
# [100017, [0.197, -0.2099]],
# [100012, [0.2099, -0.7349]],
# [100013, [0.2099, 1.5749]],
# [100008, [0.2362, -0.2099]],
# [100018, [0.2362, -0.142]],
# [100019, [0.2362, 1.1651]],
# [100009, [0.2362, 1.5575]]
# ]
# borders = [
# [0, [100000, 100001, 100002, 100003, 100004, 100005, 100006, 100007, 100008, 100009, 100000]]
# ]
# # Create a mapping from ids to coordinates for easy access
# id_to_coords = {id: coords for id, coords in points2d}
# # Create figure and axes
# fig, ax = plt.subplots()
# # Plot each point
# for id, coords in points2d:
# ax.scatter(*coords)
# # Plot each border
# for id, border_ids in borders:
# # Get the coordinates corresponding to each border ID
# border_coords = [id_to_coords[id] for id in border_ids]
# # Create the polygon
# polygon = patches.Polygon(border_coords, fill=False)
# aggregate_floor_type = {
# "floor_type": "carpet",
# "geometry": [
# [
# [-1.2981, -0.1293],
# [-1.7784, 0.1479],
# [-1.7784, 1.2894],
# [-0.9274, 1.2894],
# [-0.9274, 1.5575],
# [-0.1134, 1.5575],
# [-0.1134, 1.1651],
# [0.2362, 1.1651],
# [0.2362, -0.1421],
# [0.197, -0.2099],
# [-0.1874, -0.2099],
# [-0.1874, -0.7705],
# [-0.7382, -0.4525],
# [-0.7382, 0.3149],
# [-0.8538, 0.3149],
# [-1.2981, -0.1293]
# ]
# ]
# }
# # Plot floor type regions
# for region in aggregate_floor_type["geometry"]:
# # Create the polygon
# polygon = patches.Polygon(region, color='lightgrey' if aggregate_floor_type["floor_type"] == 'carpet' else 'white', alpha=0.5)
# # Add the polygon to the plot
# ax.add_patch(polygon)
# # Add the polygon to the plot
# # ax.add_patch(polygon)
# # Display the plot
# plt.show()
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
points2d = {
100001: [-1.9838, -0.116],
100000: [-1.9838, 1.5575],
100011: [-1.7849, -0.7349],
100010: [-1.7849, 1.5749],
100025: [-1.7783, 0.1479],
100024: [-1.7783, 1.2893],
100002: [-1.6344, -0.4655],
100014: [-1.2982, -0.1292],
100023: [-0.9274, 1.2893],
100022: [-0.9274, 1.5575],
100003: [-0.8537, 0.3149],
100005: [-0.7382, -0.8399],
100015: [-0.7382, -0.4526],
100004: [-0.7382, 0.3149],
100006: [-0.1875, -0.8399],
100016: [-0.1875, -0.7705],
100007: [-0.1875, -0.2099],
100020: [-0.1134, 1.1651],
100021: [-0.1134, 1.5575],
100017: [0.197, -0.2099],
100012: [0.2099, -0.7349],
100013: [0.2099, 1.5749],
100008: [0.2362, -0.2099],
100018: [0.2362, -0.142],
100019: [0.2362, 1.1651],
100009: [0.2362, 1.5575]
}
borders = [
{
"free_type": "free",
"geometry": {
"ids": [[100000, 100001, 100002, 100003, 100004, 100005, 100006, 100007, 100008, 100009, 100000]],
"type": "polygon"
},
"id": 0
}
]
regions = [
{
"floor_type": "carpet",
"geometry": {
"ids": [[100001, 100002, 100003, 100004, 100005]],
"type": "polygon"
},
"id": 0
},
# Add more regions as necessary
]
floor_type_colors = {
"carpet": "tan",
# Add more floor types and corresponding colors as necessary
}
poses2d = [
{
"coordinates": [-0.0485, 0.0313],
"id": 100001,
"ori_rad": -1.0362
},
{
"coordinates": [0, 0],
"id": 100000,
"ori_rad": 0
}
]
typed_poses = {
"dock_poses": {
"geometry": {
"ids": []
},
"type": "multipose"
},
"end_pose": {
"geometry": {
"ids": [100001]
},
"type": "multipose"
},
"start_pose": {
"geometry": {
"ids": [100000]
},
"type": "multipose"
}
}
pose_data = {
"type": "pose2d_concise",
"list": [[0.3185, 3.3856, 1.6119]]
}
converted_pose = {
'coordinates': pose_data['list'][0][:2],
'ori_rad': pose_data['list'][0][2]
}
poses2d = [converted_pose]
def plot_map(points2d, borders, poses2d):
# Create figure and axes
fig, ax = plt.subplots()
# Prepare to plot borders
for border in borders:
border_coordinates = [points2d[id] for id in border['geometry']['ids'][0]]
border_polygon = patches.Polygon(border_coordinates, fill=True, alpha=0.3)
ax.add_patch(border_polygon)
# Prepare to plot poses
for pose in poses2d:
x, y = pose['coordinates']
ori_rad = pose['ori_rad']
dx, dy = np.cos(ori_rad), np.sin(ori_rad)
ax.arrow(x, y, dx, dy, head_width=0.05, head_length=0.1, fc='black', ec='black')
# Prepare to plot typed poses
start_pose_ids = typed_poses['start_pose']['geometry']['ids']
end_pose_ids = typed_poses['end_pose']['geometry']['ids']
dock_pose_ids = typed_poses['dock_poses']['geometry']['ids']
for id in start_pose_ids:
x, y = points2d[id]
ax.plot(x, y, 'go') # start poses in green
for id in end_pose_ids:
x, y = points2d[id]
ax.plot(x, y, 'ro') # end poses in red
for id in dock_pose_ids:
x, y = points2d[id]
ax.plot(x, y, 'bo') # dock poses in blue
ax.set_aspect('equal', adjustable='datalim')
plt.show()
plot_map(points2d, borders, poses2d)