Commit d0f9c981 authored by Ahmad Reza Cheraghi's avatar Ahmad Reza Cheraghi
Browse files

Replace vis.py

parent 3a01e144
......@@ -2,9 +2,9 @@ import datetime, math, os, time
from pyglet.gl import *
from pyglet.window import mouse
import pyglet.window.key as key
import importlib
import copy
black = 1
gray = 2
red = 3
......@@ -111,13 +111,15 @@ class View:
class VisWindow(pyglet.window.Window):
def __init__(self, world):
super().__init__(window_width, window_height, resizable=window_resizable, vsync=False, caption="Simulator")
def __init__(self, world, sim):
#super().__init__(world.get_world_x_size(), world.get_world_y_size(), resizable=window_resizable, vsync=False, caption="Simulator")
super().__init__(world.get_world_x_size(), world.get_world_y_size(), resizable=window_resizable, vsync=False, caption="Simulator")
self.window_active = True
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
self.world = world
self.sim = sim
self.init_tile_vertex_list()
self.init_particle_vertex_list()
self.view = View()
......@@ -162,7 +164,7 @@ class VisWindow(pyglet.window.Window):
if coords_coords not in self.world.tile_map:
# add tile and vertices
#tile = Tile(coords_coords[0], coords_coords[1])
self.world.create_tile(coords_coords[0], coords_coords[1])
self.world.add_tile(coords_coords[0], coords_coords[1])
tile = self.world.tile_map[float(coords_coords[0]), float(coords_coords[1])]
#self.tile_vertex_list.resize(4 * len(self.world.tiles), 4 * len(self.world.tiles))
self.tile_vertex_list.resize(4 * len(self.world.tiles), 8 * len(self.world.tiles))
......@@ -279,156 +281,74 @@ class VisWindow(pyglet.window.Window):
def update_tiles(self, update_all=False):
foreground = []
background = []
change_tile = False
if self.world.tiles_rm:
while self.world.tiles_rm:
rm_tile = self.world.tiles_rm.pop()
indices=[]
#self.tile_vertex_list.resize(4 * len(self.world.tiles), 8 * len(self.world.tiles))
if len(self.world.tiles) == 0:
#self.tile_vertex_list.delete()
pass
# self.tile_vertex_list.delete()
#self.tile_vertex_list.indices = list(range(4 * len(self.world.tiles), 8 * len(self.world.tiles)+8))
else:
self.tile_vertex_list.resize(4 * len(self.world.tiles), 8 * len(self.world.tiles))
self.tile_vertex_list.indices = list(range(0, 8 * len(self.world.tiles)+8))
#self.update_tile(len(self.world.tiles) - 1, tile)
for i, tile in enumerate(self.world.tiles):
for i, tile in enumerate(self.world.tiles):
if update_all or tile.modified:
self.update_tile(i, tile)
#indices += list(range(4 * i, 4 * i + 4))
#self.tile_vertex_list.indices = list(range(0, 8*len(self.world.tiles)))
elif self.world.new_tile_flag == True:
self.world.new_tile_flag = False
#indices += list(range(4 * i, 4 * i + 4))
if len(self.world.tiles) == 1:
print ("new size:", self.tile_vertex_list.get_size())
self.update_tile(len(self.world.tiles) - 1, self.world.new_tile)
self.tile_vertex_list.indices = list(range(0, 4 * len(self.world.tiles)))
else:
self.tile_vertex_list.resize(4 * len(self.world.tiles), 4* len(self.world.tiles))
print ("new size:", self.tile_vertex_list.get_size(), len(self.world.tiles))
self.update_tile(len(self.world.tiles) - 1, self.world.new_tile)
# self.tile_vertex_list.indices = list(range( 4* len(self.world.tiles), 4* len(self.world.tiles)))
self.tile_vertex_list.indices[ 4 * (len(self.world.tiles) - 1):
4 * (len(self.world.tiles) - 1) + 4] = range(
4 * (len(self.world.tiles) - 1), 4 * (len(self.world.tiles) - 1) + 4)
# self.tile_vertex_list.resize(4 * len(self.world.tiles), 4 * len(self.world.tiles))
# print ("new size:", self.tile_vertex_list.get_size())
# self.world.new_tile_flag = False
# indices = []
# # self.update_tile(i, tile)
# # for i, tile in enumerate(self.world.tiles):
# # count = 0
# # self.update_tile(i, tile)
# # indices += list(range(4 * i, 4 * i + 4))
# #self.tile_vertex_list.indices = list(range(0, 8*len(self.world.tiles)))
# self.tile_vertex_list.indices[4 * (len(self.world.tiles) - 1): 4 * (len(self.world.tiles) - 1) + 4] = range(
# 4 * (len(self.world.tiles) - 1), 4 * (len(self.world.tiles) - 1) + 4)
# self.update_tile(len(self.world.tiles) - 1, self.world.new_tile)
# count = 0
count = 0
for vertices in self.tile_vertex_list.vertices:
count += 1
print("NEWWWWWWNEW vertex ", vertices, "at ", count, " size ", self.tile_vertex_list.get_size())
else:
indices=[]
for i, tile in enumerate(self.world.tiles):
counter = 0
if update_all or tile.modified:
self.update_tile(i, tile)
tile.modified = False
for vertices in self.tile_vertex_list.vertices:
counter += 1
print(
"old vertex ", vertices, "at ", i, " and ", counter, " size ", self.tile_vertex_list.get_size())
indices += list(range(4 * i, 4 * i + 4))
if tile.get_tile_status:
foreground += indices
else:
background += indices
self.tile_vertex_list.indices = indices
#self.tile_vertex_list.resize(4 * len(self.world.tiles), 4 * len(self.world.tiles))
tile.modified = False
indices = list(range(4 * i, 4 * i + 4))
if tile.get_tile_status():
foreground += indices
else:
background += indices
self.tile_vertex_list.indices = background + foreground
def update_tile(self, i, tile):
weird = 256 / 220
pos = coords_to_world(tile.coords)
x = pos[0]
y = pos[1]
self.tile_vertex_list.vertices[8*i :8*i +8 ] = [x - weird, y - weird, x + weird, y - weird, x + weird,
y + weird, x - weird, y + weird]
self.tile_vertex_list.vertices[8 * i: 8 * i + 8] = [x - weird, y - weird, x + weird, y - weird, x + weird,
y + weird, x - weird, y + weird]
if tile.get_tile_status():
texLeft = 0/8
texRight = 1/8
texBottom = 5/8
texTop = 6/8
texLeft = 0 / 8
texRight = 1 / 8
texBottom = 5 / 8
texTop = 6 / 8
tile_alpha = 1
else:
texLeft = 7/8
texRight = 8/8
texBottom = 4/8
texTop = 5/8
texLeft = 7 / 8
texRight = 8 / 8
texBottom = 4 / 8
texTop = 5 / 8
tile_alpha = 0.5
self.tile_vertex_list.tex_coords[8*i :8*i +8 ] = [texLeft, texBottom, texRight, texBottom, texRight, texTop, texLeft, texTop]
self.tile_vertex_list.colors[16*i :16*i +16] = (color_map[tile.color] + [tile_alpha]) * 4
self.tile_vertex_list.tex_coords[8 * i: 8 * i + 8] = [texLeft, texBottom, texRight, texBottom, texRight, texTop,
texLeft, texTop]
self.tile_vertex_list.colors[16 * i: 16 * i + 16] = (color_map[tile.color] + [tile_alpha]) * 4
def update_particles(self, update_all = False):
foreground = []
background = []
change_particle = False
for i, particle in enumerate(self.world.particles):
if update_all or particle.modified:
self.update_particle(i, particle)
particle.modified = False
indices = list(range(4 * i, 4 * i + 4))
background += indices
change_particle = True
#if change_particle == True:
# self.particle_vertex_list.indices = background #+ foreground
# def update_particles(self, update_all = False):
# for i, particle in enumerate(self.world.particles):
# if update_all or particle.modified:
# self.update_particle(i, particle)
# particle.modified = False
def update_particle(self, i, particle):
weird = 256 / 220
#weird = 0,2
pos = coords_to_world(particle.coords)
x = pos[0]
y = pos[1]
self.particle_vertex_list.vertices[8 * i: 8 * i + 8] = [x - weird, y - weird, x + weird, y - weird, x + weird,
y + weird, x - weird, y + weird]
# self.particle_vertex_list.vertices[8 * i: 8 * i + 8] = [x + 1, y + 1,
# x + 1, y - 1,
# x - 1, y - 1,
# x - 1, y + 1]
# #here it reads from the svg file the needed graphic.
texLeft = 0/8 #the 8th column from left
texRight = 1/8 # I do not understand, but it takes last column from left and so the first column from right
texBottom = 0/8 # the fifth row from bottom
texTop = 1/8 # the fourth row from top
self.particle_vertex_list.tex_coords[8 * i: 8 * i + 8] = [texLeft, texBottom, texRight, texBottom, texRight, texTop, texLeft, texTop]
texLeft = 0/8
texRight = 1/8
texBottom = 0/8
texTop = 1/8
self.particle_vertex_list.tex_coords[8 * i: 8 * i + 8] = [texLeft, texBottom, texRight, texBottom,
texRight, texTop, texLeft, texTop]
self.particle_vertex_list.colors[16 * i: 16 * i + 16] = (color_map[particle.color] + [particle_alpha]) * 4
def update_locations(self, update_all=True):
for i, location in enumerate(self.world.locations):
if update_all or location.modified:
......@@ -444,17 +364,17 @@ class VisWindow(pyglet.window.Window):
target_frame_time = 1 / target_frame_rate
round_time = 1 / rounds_per_second
self.elapsed_frame_time = 0
mod = importlib.import_module('robotsOnTiles.scenarios.' + self.sim.get_scenario())
while self.window_active:
while self.world.get_actual_round() <= self.world.get_max_round(): #while actual simulation round is below max round
while self.sim.get_actual_round() <= self.sim.get_max_round(): #while actual simulation round is below max round
last_time = time.perf_counter()
while self.elapsed_frame_time >= round_time:
self.round()
mod.scenario(self.sim, self.world)
self.elapsed_frame_time -= round_time
if self.elapsed_frame_time >= round_time: #one simulation is finished then
self.world.inc_round_cnter() #increase simulation round counter by one.
if self.elapsed_frame_time <= round_time:
self.world.csv_round_writer.next_line(self.sim.get_actual_round())
self.sim.inc_round_cnter() # increase simulation round counter by one.
self.dispatch_events()
self.draw()
......@@ -468,9 +388,13 @@ class VisWindow(pyglet.window.Window):
if self.simulation_running:
self.elapsed_frame_time += target_frame_time
if print_frame_stats:
frame_time_delta = time.perf_counter() - last_time - target_frame_time
frame_time_ok = abs(frame_time_delta) <= 0.0001
print('frame time:', format(time.perf_counter() - last_time, '.6f'), '\tdelta:', format(frame_time_delta, '0.6f'), '\tok:', frame_time_ok)
self.world.csv_round_writer.aggregate_rounds() #After simulation is finished, aggregate everything
# return
\ No newline at end of file
self.world.csv_round_writer.aggregate_metrics () #After simulation is finished, aggregate everything
for particle in self.world.particles:
particle.csv_particle_writer.close_particle()
return
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment