-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer.py
414 lines (330 loc) · 16.3 KB
/
visualizer.py
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from panda3d.core import *
from direct.showbase.ShowBase import ShowBase
from panda3d.core import AmbientLight, DirectionalLight
from panda3d.core import Geom, GeomNode, GeomTriangles, GeomVertexFormat, GeomVertexData, GeomVertexWriter
from panda3d.core import NodePath
from direct.task import Task
from direct.gui.OnscreenText import OnscreenText
from math import pi, sin, cos, radians, sqrt
class PlatooningVisualizer(ShowBase):
def __init__(self, env):
ShowBase.__init__(self)
self.env = env
self.episode = 1
self.total_reward = 0
self.avg_reward = 0
self.instant_rewards = []
self.road_width = 8
self.road_length = 800
self.road_color = (0.6, 0.6, 0.6, 1) # Grigio
self.terrain_width = 10000
self.terrain_length = 10000
self.terrain_color = (0, 0.8, 0.35, 1) # Verde
self.sidewalk_width = 3
self.sidewalk_height = 0.25
self.sidewalk_color = (0.9, 0.9, 0.9, 1) # Grigio chiaro
dash_length = 20
gap_length = 0
self.car_height_offset = 0
self.leader_starting_pos = -self.road_length/2 + 50
# Disabilita i controlli di camera predefiniti
self.disableMouse()
self.camera_control = CameraControl(self)
self.leader = self.loader.loadModel("Panda3D/Models/road/sedan.glb")
self.leader.setHpr(180, 90, 0)
self.leader.setScale(1.55)
self.leader.reparentTo(self.render)
self.follower = self.loader.loadModel("Panda3D/Models/road/police.glb")
self.follower.setHpr(180, 90, 0)
self.follower.setScale(1.55)
self.follower.reparentTo(self.render)
# Crea la strada
self.road = self.create_plane(self.road_width, self.road_length, self.road_color)
self.road.setColor(self.road_color)
self.road.setPos(0, 0, 0)
self.road.reparentTo(self.render)
# Crea il terreno
self.terrain = self.create_plane(self.terrain_width, self.terrain_length, self.terrain_color)
self.terrain.setColor(self.terrain_color)
self.terrain.setPos(0, 0, -0.1)
self.terrain.reparentTo(self.render)
# Crea i marciapiedi
self.left_sidewalk = self.loader.loadModel("models/box")
self.left_sidewalk.setScale(self.sidewalk_width, self.road_length, self.sidewalk_height)
self.left_sidewalk.setPos(-self.road_width/2 - self.sidewalk_width, -self.road_length/2, 0)
self.left_sidewalk.setColor(self.sidewalk_color)
#self.left_sidewalk.reparentTo(self.render)
self.right_sidewalk = self.loader.loadModel("models/box")
self.right_sidewalk.setScale(self.sidewalk_width, self.road_length, self.sidewalk_height)
self.right_sidewalk.setPos(self.road_width/2, -self.road_length/2, 0)
self.right_sidewalk.setColor(self.sidewalk_color)
#self.right_sidewalk.reparentTo(self.render)
self.building1sx = self.loader.loadModel("Panda3D/Models/buildings/skyscraperA.glb")
self.building1sx.setScale(10)
self.building1sx.setHpr(90, 0, 0)
self.building1sx.setPos(-self.road_width/2 - 20, 400, 0)
#self.building1sx.reparentTo(self.render)
self.building2sx = self.loader.loadModel("Panda3D/Models/buildings/large_buildingA.glb")
self.building2sx.setScale(10)
self.building2sx.setHpr(90, 0, 0)
self.building2sx.setPos(-self.road_width/2 - 20, 350, 0)
#self.building2sx.reparentTo(self.render)
self.building3sx = self.loader.loadModel("Panda3D/Models/buildings/low_wideA.glb")
self.building3sx.setScale(10)
self.building3sx.setHpr(90, 0, 0)
self.building3sx.setPos(-self.road_width/2 - 20, 300, 0)
#self.building3sx.reparentTo(self.render)
self.building4sx = self.loader.loadModel("Panda3D/Models/buildings/low_buildingA.glb")
self.building4sx.setScale(10)
self.building4sx.setHpr(90, 0, 0)
self.building4sx.setPos(-self.road_width/2 - 20, 250, 0)
#self.building4sx.reparentTo(self.render)
self.building1dx = self.loader.loadModel("Panda3D/Models/buildings/large_buildingB.glb")
self.building1dx.setScale(10)
self.building1dx.setHpr(-90, 0, 0)
self.building1dx.setPos(self.road_width/2 + 20, 250, 0)
#self.building1dx.reparentTo(self.render)
self.building2dx = self.loader.loadModel("Panda3D/Models/buildings/large_buildingC.glb")
self.building2dx.setScale(10)
self.building2dx.setHpr(-90, 0, 0)
self.building2dx.setPos(self.road_width/2 + 20, 300, 0)
#self.building2dx.reparentTo(self.render)
self.building3dx = self.loader.loadModel("Panda3D/Models/buildings/skyscraperE.glb")
self.building3dx.setScale(10)
self.building3dx.setHpr(-90, 0, 0)
self.building3dx.setPos(self.road_width/2 + 20, 350, 0)
#self.building3dx.reparentTo(self.render)
self.building4dx = self.loader.loadModel("Panda3D/Models/buildings/low_wideB.glb")
self.building4dx.setScale(10)
self.building4dx.setHpr(-90, 0, 0)
self.building4dx.setPos(self.road_width/2 + 20, 400, 0)
#self.building4dx.reparentTo(self.render)
self.buildingct = self.loader.loadModel("Panda3D/Models/buildings/skyscraperD.glb")
self.buildingct.setScale(10)
self.buildingct.setHpr(-90, 0, 0)
self.buildingct.setPos(0, 400, 0)
#self.buildingct.reparentTo(self.render)
# Crea la linea tratteggiata
self.center_line = self.create_dashed_line(self.road_length, (1, 1, 1, 1))
self.center_line.setPos(0, -self.road_length/2, 0.01)
self.center_line.reparentTo(self.render)
self.left_line = self.create_dashed_line(self.road_length, (1, 1, 1, 1), dash_length, gap_length)
self.left_line.setPos(-self.road_width/2 + 0.5, -self.road_length/2, 0.01)
self.left_line.reparentTo(self.render)
self.right_line = self.create_dashed_line(self.road_length, (1, 1, 1, 1), dash_length, gap_length)
self.right_line.setPos(self.road_width/2 - 0.5, -self.road_length/2, 0.01)
self.right_line.reparentTo(self.render)
self.desired_distance_line = self.create_line((1, 0, 0, 1)) # Rosso
self.actual_distance_line = self.create_line((0, 0, 1, 1)) # Blu
self.desired_distance_line.reparentTo(self.render)
self.actual_distance_line.reparentTo(self.render)
self.desired_text = self.create_text("DESIRED", (1, 0, 0, 1)) # Rosso
self.desired_text.reparentTo(self.render)
# Aggiungi illuminazione
alight = AmbientLight('alight')
alight.setColor((0.2, 0.2, 0.2, 1))
alnp = self.render.attachNewNode(alight)
self.render.setLight(alnp)
dlight = DirectionalLight('dlight')
dlight.setColor((0.8, 0.8, 0.8, 1))
dlnp = self.render.attachNewNode(dlight)
dlnp.setHpr(0, -60, 0)
self.render.setLight(dlnp)
self.info_display = self.create_info_display()
# Imposta la telecamera
self.camera.setPos(0,0,0)
self.camera.lookAt(0,0,0)
self.is_visualizing = False
self.paused = False
self.taskMgr.add(self.update_camera, "UpdateCameraTask")
self.total_distance = 0
def create_line(self, color):
line_segs = LineSegs()
line_segs.setThickness(2)
line_segs.setColor(color)
line_segs.moveTo(-1.5, 0, 0.02) # Inizio linea
line_segs.drawTo(1.5, 0, 0.02) # Fine linea
return self.render.attachNewNode(line_segs.create())
def create_text(self, text, color):
text_node = TextNode('road_text')
text_node.setText(text)
text_node.setTextColor(color)
text_node.setAlign(TextNode.ACenter)
text_path = self.render.attachNewNode(text_node)
text_path.setScale(0.5) # Regola la dimensione del testo
text_path.setHpr(0, -90, 0) # Ruota il testo per essere parallelo alla strada
return text_path
def create_plane(self, width, length, color):
format = GeomVertexFormat.getV3n3c4()
vdata = GeomVertexData('plane', format, Geom.UHStatic)
vertex = GeomVertexWriter(vdata, 'vertex')
normal = GeomVertexWriter(vdata, 'normal')
color_writer = GeomVertexWriter(vdata, 'color') # Rinominato per evitare conflitti
# Define the vertices
vertex.addData3(-width/2, -length/2, 0)
vertex.addData3(width/2, -length/2, 0)
vertex.addData3(width/2, length/2, 0)
vertex.addData3(-width/2, length/2, 0)
for _ in range(4):
normal.addData3(0, 0, 1)
color_writer.addData4(*color) # Usa il colore passato come argomento
prim = GeomTriangles(Geom.UHStatic)
prim.addVertices(0, 1, 2)
prim.addVertices(0, 2, 3)
geom = Geom(vdata)
geom.addPrimitive(prim)
node = GeomNode('plane')
node.addGeom(geom)
return NodePath(node)
def create_dashed_line(self, length, color, dash_length=3, gap_length=4, line_width=0.2):
line_root = NodePath("dashed_line_root")
for i in range(0, int(length), dash_length + gap_length):
line = self.create_plane(line_width, dash_length, color)
line.reparentTo(line_root)
line.setPos(0, i, 0)
return line_root
def create_info_display(self):
info_text = OnscreenText(
text="",
style=1,
fg=(0, 0, 0, 1),
pos=(-1.25, -0.3), # Posizione in basso a sinistra
align=TextNode.ALeft,
scale=0.05, # Dimensione del testo
mayChange=True
)
return info_text
def reset_episode(self, episode):
self.episode = episode + 1
self.total_distance = 0
self.leader.setPos(1.9, self.leader_starting_pos, self.car_height_offset)
self.follower.setPos(1.9, self.leader_starting_pos - self.env.actual_distance, self.car_height_offset)
self.camera_control.reset()
self.is_visualizing = True
self.paused = False
self.instant_rewards = []
def update_positions(self):
if self.paused:
return
leader_pos = self.total_distance + self.leader_starting_pos
follower_pos = leader_pos - self.env.actual_distance
self.leader.setPos(1.9, leader_pos, self.car_height_offset)
self.follower.setPos(1.9, follower_pos, self.car_height_offset)
# Aggiorna il punto medio tra i veicoli
midpoint = (leader_pos + follower_pos) / 2
self.camera_control.update_target(Vec3(0, midpoint, self.car_height_offset))
def update_distance_lines(self):
leader_pos = self.leader.getPos()
follower_pos = self.follower.getPos()
# Aggiorna la linea della desired_distance
desired_pos = leader_pos - Vec3(0, self.env.desired_distance, 0)
self.desired_distance_line.setPos(desired_pos.x, desired_pos.y + 2.1, 0.1)
self.desired_text.setPos(desired_pos.x, desired_pos.y + 2.3, 0.1)
# Aggiorna la linea della actual_distance
self.actual_distance_line.setPos(follower_pos.x, follower_pos.y + 2.1, 0.1)
def update_info_display(self, ep, ev, acc, leader_vel, agent_vel, actual_distance, desired_distance):
info_text = (
f"Episode: {self.episode}\n"
f"Position gap (ep): {ep:.2f} m\n"
f"Velocity gap (ev): {ev:.2f} m/s\n"
f"Acceleration gap (acc): {acc:.2f} m/s^2\n"
f"Leader velocity: {(leader_vel * 3.6):.2f} km/h\n"
f"Agent velocity: {(agent_vel * 3.6):.2f} km/h\n"
f"Actual distance: {(actual_distance - self.env.vehicles_length):.2f} m\n"
f"Desired distance: {(desired_distance - self.env.vehicles_length):.2f} m\n\n"
f"Total Reward: {self.total_reward:.4f}\n"
f"Avg Reward (last 100): {self.avg_reward:.4f}\n"
f"Instant Reward: {self.instant_rewards[-1]:.4f}\n\n"
)
self.info_display.setText(info_text)
def update(self, env):
if not self.is_visualizing:
self.episode += 1
return
self.env = env
self.update_positions()
self.update_distance_lines()
ep = self.env.state[0]
ev = self.env.state[1]
acc = self.env.state[2]
leader_vel = self.env.leader_velocity
agent_vel = self.env.agent_velocity
actual_distance = self.env.actual_distance
desired_distance = self.env.desired_distance
self.update_info_display(ep, ev, acc, leader_vel, agent_vel, actual_distance, desired_distance)
def update_camera(self, task):
self.camera_control.update()
return Task.cont
def toggle_pause(self):
self.paused = not self.paused
def stop_visualizing(self):
self.is_visualizing = False
self.paused = True
class CameraControl:
def __init__(self, base):
self.base = base
self.camera = base.camera
self.last_mouse_x = 0
self.last_mouse_y = 0
self.mouse_sensitivity = 0.8
self.zoom_speed = 1.5
self.min_height = 0.5
self.camera_distance = 40
self.camera_pitch = 20
self.camera_yaw = 0
self.target = Vec3(0, 0, 0)
self.offset = Vec3(-40, -40, 70)
# Accetta gli input del mouse
self.base.accept("mouse3", self.start_drag)
self.base.accept("mouse3-up", self.stop_drag)
self.base.accept("wheel_up", self.zoom_in)
self.base.accept("wheel_down", self.zoom_out)
self.dragging = False
def start_drag(self):
self.dragging = True
self.last_mouse_x = self.base.mouseWatcherNode.getMouseX()
self.last_mouse_y = self.base.mouseWatcherNode.getMouseY()
def stop_drag(self):
self.dragging = False
def zoom_in(self):
self.camera_distance = max(5, self.camera_distance - self.zoom_speed)
def zoom_out(self):
self.camera_distance = min(100, self.camera_distance + self.zoom_speed)
def update_target(self, new_target):
self.target = new_target
def update(self):
if self.dragging and self.base.mouseWatcherNode.hasMouse():
mouse_x = self.base.mouseWatcherNode.getMouseX()
mouse_y = self.base.mouseWatcherNode.getMouseY()
dx = mouse_x - self.last_mouse_x
dy = mouse_y - self.last_mouse_y
self.camera_yaw -= dx * self.mouse_sensitivity * 100
self.camera_pitch += dy * self.mouse_sensitivity * 100
self.camera_pitch = max(-89, min(89, self.camera_pitch))
self.last_mouse_x = mouse_x
self.last_mouse_y = mouse_y
# Calcola la nuova posizione della camera relativa al target
relative_x = self.camera_distance * -sin(radians(self.camera_yaw)) * cos(radians(self.camera_pitch))
relative_y = self.camera_distance * -cos(radians(self.camera_yaw)) * cos(radians(self.camera_pitch))
relative_z = self.camera_distance * sin(radians(self.camera_pitch))
# Calcola la nuova posizione della camera
new_pos = self.target + Vec3(relative_x, relative_y, relative_z)
# Controlla se la nuova posizione è sotto il terreno
if new_pos.z < self.min_height:
# Calcola l'altezza corretta mantenendo la distanza dal target
distance_xy = sqrt(relative_x**2 + relative_y**2)
max_distance_xy = sqrt(self.camera_distance**2 - self.min_height**2)
if distance_xy > max_distance_xy:
scale = max_distance_xy / distance_xy
relative_x *= scale
relative_y *= scale
relative_z = self.min_height
new_pos = self.target + Vec3(relative_x, relative_y, relative_z)
# Aggiorna la posizione e l'orientamento della camera
self.camera.setPos(new_pos)
self.camera.lookAt(self.target)
def reset(self):
self.camera_distance = 40
self.camera_pitch = 20
self.camera_yaw = 0
self.target = Vec3(0, 0, 0)