gazesim/code/svis.py

92 lines
2.5 KiB
Python

'''
This is the visualization backend for bare_sim module
'''
try:
import visual as vs
from visual import vector as vv
except ImportError:
from vector import Vector as vv
# from vector import Vector as vector
pass
from vector import Vector as v
from geom import Camera
from util import drawAxes
# converts a vector object into a visual vector object
v_to_vv = lambda vec: vv(vec.x, vec.y, vec.z)
def drawCameraFrame(cam): # create frame and draw its contents
c = v(cam.t)
cam.center = vs.sphere(pos=v_to_vv(c),
radius=Camera.default_radius,
color=vs.color.green)
cam.dir = vs.arrow(pos=v_to_vv(c),
axis=v_to_vv(v(cam.direction)) * cam.f,
shaftwidth=1.0)
# TODO: check for orientation of the camera, rotate plane accordingly
cam.img_plane = vs.box(pos=v_to_vv(c) + v_to_vv(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