2016-03-09 19:52:35 +01:00
|
|
|
'''
|
|
|
|
This is the visualization backend for bare_sim module
|
|
|
|
'''
|
2016-04-28 18:14:38 +02:00
|
|
|
try:
|
|
|
|
import visual as vs
|
|
|
|
from visual import vector as v
|
|
|
|
except ImportError:
|
|
|
|
from vector import Vector as v
|
|
|
|
|
2016-03-09 19:52:35 +01:00
|
|
|
from geom import Camera
|
|
|
|
|
|
|
|
from util import drawAxes
|
|
|
|
|
|
|
|
def drawCameraFrame(cam): # create frame and draw its contents
|
|
|
|
c = v(cam.t)
|
|
|
|
cam.center = vs.sphere(pos=c,
|
|
|
|
radius=Camera.default_radius,
|
|
|
|
color=vs.color.green)
|
|
|
|
cam.dir = vs.arrow(pos=c,
|
|
|
|
axis=v(cam.direction) * cam.f,
|
|
|
|
shaftwidth=1.0)
|
|
|
|
# TODO: check for orientation of the camera, rotate plane accordingly
|
|
|
|
cam.img_plane = vs.box(pos=c + cam.dir.axis,
|
|
|
|
length=cam.image_width,
|
|
|
|
width=0.5,
|
|
|
|
height=cam.image_width,
|
|
|
|
color=vs.color.white,
|
|
|
|
opacity=0.5)
|
|
|
|
|
|
|
|
def castRays(cam, pts, frame = None):
|
|
|
|
'''
|
|
|
|
Cast rays from camera center towards pts
|
|
|
|
'''
|
|
|
|
for point in pts:
|
|
|
|
pos = v(cam.t)
|
|
|
|
diff = v(point) - pos
|
|
|
|
drawLine(frame, pos, diff.mag, diff.norm())
|
|
|
|
|
|
|
|
class Ray:
|
|
|
|
def __init__(self, position, length, direction, color = vs.color.white):
|
|
|
|
self.position = position
|
|
|
|
self.length = length
|
|
|
|
self.direction = direction
|
|
|
|
self.color = color
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# a Simulation object which extends this class can be visualizable
|
|
|
|
class Visualizable:
|
|
|
|
display_width = 800
|
|
|
|
display_height = 600
|
|
|
|
|
|
|
|
# no code interaction is possible if this is true
|
|
|
|
# press esc to cancel visualization, change model parameters, then visualize again
|
|
|
|
visualizing = False
|
|
|
|
|
|
|
|
def visualize(self):
|
|
|
|
self.visualizing = True
|
|
|
|
# self.win = vs.window(width=self.display_width, height=self.display_height, title='')
|
|
|
|
# self.scene = vs.display(window=self.win, width=self.display_width, height=self.display_height)
|
|
|
|
# self.scene.bind('keydown', self.__keyInput__)
|
|
|
|
vs.scene.bind('keydown', self.__keyInput__)
|
|
|
|
|
|
|
|
print 'Visualizing model...'
|
|
|
|
print '(Modifications are not allowed)'
|
|
|
|
print 'Press Esc to stop visualization...'
|
|
|
|
while self.visualizing:
|
|
|
|
vs.rate(1)
|
|
|
|
self.draw()
|
|
|
|
|
|
|
|
def __keyInput__(self, evt):
|
|
|
|
s = evt.key
|
|
|
|
if s == 'esc':
|
|
|
|
print 'You can modify model parameters now...'
|
|
|
|
self.visualizing = False;
|
|
|
|
# vs.exit()
|
|
|
|
|
|
|
|
def draw(self):
|
|
|
|
'''
|
|
|
|
This method actually does some drawing in the scene
|
|
|
|
'''
|
|
|
|
# vs.sphere(pos = (0, 0, 0), radius = self.r, color=self.clr)
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|