forked from drwiner/PyDPOCL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_worldmodel.py
More file actions
143 lines (124 loc) · 4.91 KB
/
Copy pathvisualize_worldmodel.py
File metadata and controls
143 lines (124 loc) · 4.91 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
import sys
import json
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon as MplPolygon
objcolors = ["red",
"blue",
"green",
"yellow",
"purple",
"cyan",
"orange",
"white"]
# Load the worldmodel JSON file
def load_worldmodel(filename):
with open(filename, 'r') as f:
return json.load(f)
def plot_area(ax, area, color='lightgray', edgecolor='black', alpha=0.5, label=None):
coords = area['coords']
poly = MplPolygon(coords, closed=True, facecolor=color, edgecolor=edgecolor, alpha=alpha, label=label)
ax.add_patch(poly)
if label:
# Place label at centroid
xs, ys = zip(*coords)
centroid = (sum(xs)/len(xs), sum(ys)/len(ys))
ax.text(centroid[0], centroid[1], label, ha='center', va='center', fontsize=12)
def plot_object(ax, obj, color='orange', edgecolor='black', alpha=0.9):
x, y = obj['initial_pose']
w = obj['width']
l = obj['length']
# Rectangle centered at (x, y)
rect = [
[x - w/2, y - l/2],
[x - w/2, y + l/2],
[x + w/2, y + l/2],
[x + w/2, y - l/2],
]
poly = MplPolygon(rect, closed=True, facecolor=color, edgecolor=edgecolor, alpha=alpha)
ax.add_patch(poly)
ax.text(x, y, obj['name'], ha='center', va='center', fontsize=12, color='black')
def plot_arrow(ax, start_obj, end_area, color='black'):
x, y = start_obj['initial_pose']
coords = end_area['coords']
xs, ys = zip(*coords)
end_x = sum(xs)/len(xs)
end_y = sum(ys)/len(ys)
ax.annotate('', xy=(end_x, end_y), xytext=(x, y),
arrowprops=dict(arrowstyle='->', color='black', lw=2))
def main():
filenames = ['domains/manipulation-domain-batch/test_0_worldmodel.json',
'domains/manipulation-domain-batch/test_1_worldmodel.json',
'domains/manipulation-domain-batch/test_2_worldmodel.json',
'domains/manipulation-domain-batch/test_3_worldmodel.json',
'domains/manipulation-domain-batch/test_4_worldmodel.json',
'domains/manipulation-domain-batch/test_5_worldmodel.json',
'domains/manipulation-domain-batch/test_6_worldmodel.json',
'domains/manipulation-domain-batch/test_7_worldmodel.json',
'domains/manipulation-domain-batch/test_8_worldmodel.json',
'domains/manipulation-domain-batch/test_9_worldmodel.json',
]
num_args = len(sys.argv)
if num_args > 1:
filenames = [f'domains/manipulation-domain-batch/test_{i}_worldmodel.json' for i in sys.argv[1:]]
for filename in filenames:
data = load_worldmodel(filename)
fig, ax = plt.subplots(figsize=(8, 6))
def find_area_by_name(name):
for area in data['areas']:
if area['name'] == name:
return area
return None
def find_object_by_name(name):
for obj in data['objects']:
if obj['name'] == name:
return obj
return None
# Plot areas
plot_area(ax, find_area_by_name('table'))
plot_area(ax, find_area_by_name('reach_robot_0'))
plot_area(ax, find_area_by_name('reach_robot_1'))
# Plot goal regions
i = 0
while True:
goal_name = f'goal_{i}'
goal_area = find_area_by_name(goal_name)
if goal_area is None:
break
obj_color = objcolors[i]
plot_area(ax, goal_area, color=obj_color, edgecolor='red', label=goal_name)
i += 1
# Plot objects
i = 0
while True:
obj_name = f'box_{i}'
goal_name = f'goal_{i}'
obj = find_object_by_name(obj_name)
goal_area = find_area_by_name(goal_name)
if obj is None:
break
obj_color = objcolors[i] if goal_area is not None else 'gray'
plot_object(ax, obj, color=obj_color)
if goal_area is not None:
#plot_arrow(ax, obj, goal_area)
pass
i += 1
#for area in data['areas']:
# if area['name'] == 'table':
# plot_area(ax, area, color='lightgrey', label=None)
# elif area['name'] == 'reach_robot_0':
# plot_area(ax, area, color='lightgrey', label=None)
# elif area['name'] == 'reach_robot_1':
# plot_area(ax, area, color='lightgrey', label=None)
# else:
# plot_area(ax, area, color='blue', label=area['name'])
# Plot objects
#for obj in data['objects']:
# plot_object(ax, obj)
ax.set_aspect('equal')
ax.autoscale()
ax.set_title(f'Worldmodel Visualization: {filename}')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
if __name__ == '__main__':
main()