vis3d.py 7.33 KB
Newer Older
Karol Actun's avatar
Karol Actun committed
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
import importlib
import time
from pprint import pprint

from PyQt5 import QtCore
from PyQt5.QtCore import QRect

import gui
import vispy
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QHBoxLayout, QGridLayout, QSpacerItem
from vispy import app, gloo
from lib.visualization.grid_models import CubicGrid
from lib.visualization.models import ParticleModel, TileModel, MarkerModel
from vispy.util.transforms import perspective, ortho, rotate, translate
from PyQt5.QtWidgets import QSizePolicy
import numpy as np

# important: because of the implementation and the gui module, vispy has to run on pyqt5 backend
app.use_app("pyqt5")
# gl+ = newest openGL version on the system.
vispy.config['gl_backend'] = "gl+"


window_width = 600
window_height = 800
scaling = 1


class Gui(QWidget):
    def __init__(self, visualization):
        super().__init__()
        self.visualization = visualization

        gui_interface = importlib.import_module('gui.' + self.visualization.world.config_data.gui)

        gui_widget = gui_interface.define_gui(self.visualization, self.visualization.world)

        gui_widget.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)

        wrapper_layout = QVBoxLayout()
        wrapper_layout.addWidget(gui_widget)
        wrapper_layout.addStretch(0)

        self.visualization.native.setLayout(wrapper_layout)

    def start(self):
        print("starting")
        self.visualization.running = not self.visualization.running


class Controller:
    def __init__(self, visualization):
        self.visualization = visualization
        self.theta = 0
        self.phi = 0
        self.xoffset = 0
        self.yoffset = 0
        self.radius = 10
        self.fov = 50
        self.drag_sensitivity = 1000
        self.rotate_sensitivity = 5
        # orth = orthographic projection, else = perspective
        self.projection_type = 'pers'
        # vispy somehow doesn't catch the closeEvent of PyQt5,
        # so it has to be done over the native interface
        self.visualization.native.closeEvent = self.on_close
        # the rest of the events work fine
        self.visualization.on_mouse_drag = self.on_mouse_drag
        self.visualization.on_mouse_move = self.on_mouse_move
        self.visualization.on_mouse_wheel = self.on_mouse_wheel
        self.visualization.on_resize = self.on_resize
        self.update_window()

    def start(self):
        self.visualization.show()

    def on_mouse_drag(self, delta, buttons):
        if 1 in buttons:
            self.theta -= delta[1] / self.rotate_sensitivity
            self.phi -= delta[0] / self.rotate_sensitivity
            if self.theta > 90:
                self.theta = 90
            if self.theta < -90:
                self.theta = -90
        if 2 in buttons:
            self.xoffset -= delta[0] / self.drag_sensitivity * self.radius
            self.yoffset += delta[1] / self.drag_sensitivity * self.radius
        self.update_window()

    def on_mouse_move(self, event):
        if event.is_dragging and event.last_event.is_dragging:
            self.on_mouse_drag(event.last_event.pos - event.pos, event.buttons)
        self.update_window()

    def on_mouse_wheel(self, event):
        self.radius -= event.delta[1] / 3
        if self.radius < 1:
            self.radius = 1
        self.update_window()

    def on_resize(self, event):
        gloo.set_viewport(0, 0, *event.physical_size)
        self.update_window()

    def update_window(self):
        ratio = self.visualization.physical_size[0] / self.visualization.physical_size[1]
        if self.projection_type == 'orth':
            proj = ortho(-self.radius * ratio, self.radius * ratio, -self.radius, self.radius, 0.01, 1000)
        else:
            proj = perspective(self.fov, ratio, 0.01, 1000.0)
        view = np.dot(np.dot(rotate(self.phi, (0, 1, 0)),
                             rotate(self.theta, (1, 0, 0))),
                      translate([self.xoffset, self.yoffset, -self.radius]))
        self.visualization.update_view(proj, view, proj, view)

    def on_close(self, event):
        exit(0)


class Visualization(app.Canvas):
    def __init__(self, world):
        app.Canvas.__init__(self, size=(window_width*scaling, window_height*scaling), title='Simulator', vsync=False)

        gloo.set_state(clear_color=(0.35, 0.35, 0.35, 1.00), depth_test=True,
                       blend_func=('src_alpha', 'one_minus_src_alpha'), blend=True, depth_mask=True,
                       cull_face=True)

        self.rounds_per_second = 10
        self.world = world
        self.particle_model = ParticleModel("lib/visualization/models/particle.obj", (1, 0, 0, 1))
        self.tile_model = TileModel("lib/visualization/models/tile.obj",
                                    "lib/visualization/textures/cubic_tile_tex.jpg")
        self.marker_model = MarkerModel("lib/visualization/models/marker.obj", (0, 1, 0, 1))

        self.grid = self.world.grid

        self.running = False
        self.controller = Controller(self)
        self.gui = Gui(self)
        self.controller.start()

    def on_draw(self, event):
        gloo.clear(color=True, depth=True)
        self.grid.draw_model()

        for p in self.world.particles:
            self.particle_model.translate_model(p.coordinates)
            self.particle_model.draw_model()

        for m in self.world.markers:
            self.marker_model.translate_model(m.coordinates)
            self.marker_model.draw_model()

        for t in self.world.tiles:
            if t.get_tile_status() == True:
                self.tile_model.translate_model((t.coordinates[0]+0.2,
                                                t.coordinates[1]+0.2,
                                                t.coordinates[2]+0.2))
                self.tile_model['scale'] = 0.5
                self.tile_model['model_color'] = (0, 0, 0.3, 1)
                self.tile_model.draw_model()
                self.tile_model['model_color'] = (0, 0, 1, 1)
                self.tile_model['scale'] = 1
            else:
                self.tile_model.translate_model(t.coordinates)
                self.tile_model.draw_model()



        self.particle_model.rotate_light(0.01)
        self.tile_model.rotate_light(0.01)
        self.marker_model.rotate_light(0.01)

    def update_view(self, proj, view, gridproj, gridview):
        self.particle_model['projection'] = proj
        self.particle_model['view'] = view
        self.tile_model['projection'] = proj
        self.tile_model['view'] = view
        self.marker_model['projection'] = proj
        self.marker_model['view'] = view
        self.grid['projection'] = gridproj
        self.grid['view'] = gridview
        self.update()


    def wait_till_running(self):
        while not self.running:
            app.process_events()
            self.update()


    def run(self, round_start_timestamp):

        # waiting until simulation starts
        self.wait_till_running()

        # waiting until enough time passed to do the next simulation round.
        waiting_time = (1 / self.rounds_per_second) / 100
        time_elapsed = time.perf_counter() - round_start_timestamp
        while time_elapsed < 1 / self.rounds_per_second:
            # waiting for 1/100 of the round_time
            time.sleep(waiting_time)
            self.wait_till_running()
            app.process_events()
            self.update()
            time_elapsed = time.perf_counter() - round_start_timestamp