00001 import math,traceback
00002
00003 from PyQt4 import QtCore
00004
00005 import ost
00006 from ost import gfx, gui
00007
00008 class SpacenavControl(QtCore.QObject):
00009 def __init__(self, spnav,
00010 parent=None):
00011 QtCore.QObject.__init__(self, parent)
00012 QtCore.QObject.connect(spnav,QtCore.SIGNAL("deviceTransformed(int,int,int,int,int,int)"), self.Changed)
00013 QtCore.QObject.connect(spnav,QtCore.SIGNAL("deviceButtonPressed(int)"), self.Toggle)
00014
00015 self.trans = True
00016 self.rot = True
00017 self.speed = 480.0
00018
00019 def Changed(self, tx,ty,tz,rx,ry,rz):
00020 scene=gfx.Scene()
00021 tf = scene.transform
00022 def d(r):
00023 if r==0.0:
00024 return 0.0
00025 rr=r/abs(r)*max(0.0,abs(r)-0.9)
00026 if abs(rr)>0:
00027 return rr/abs(rr)*(math.pow(1.01,abs(rr))-1.0)*40.0/self.speed
00028 return 0.0
00029 if(self.trans):
00030 tf.ApplyXAxisTranslation(d(tx))
00031 tf.ApplyYAxisTranslation(d(ty))
00032
00033 currz=tf.trans[2];
00034 delta=currz*math.pow(1.01,d(tz))-currz;
00035 tf.ApplyZAxisTranslation(delta);
00036
00037 scene.SetNearFar(scene.near-delta,scene.far-delta);
00038 if(self.rot):
00039 tf.ApplyXAxisRotation(d(rx))
00040 tf.ApplyYAxisRotation(d(ry))
00041 tf.ApplyZAxisRotation(d(rz))
00042
00043 scene.transform=tf
00044 scene.RequestRedraw()
00045
00046 def Toggle(self, button):
00047 if button == 0:
00048 self.trans = not self.trans
00049 ost.LogVerbose("SpaceNav: translation %s" % (self.strans and "enabled" or "disabled"))
00050 elif button == 1:
00051 self.rot = not self.rot
00052
00053 ost.LogVerbose("SpaceNav: rotation %s"%(self.rot and "enabled" or "disabled"))
00054 elif button == 12:
00055 if self.speed > 20:
00056 self.speed *= 0.8
00057 ost.LogVerbose("SpaceNav: speed increased to "+str(self.speed))
00058 elif button == 13:
00059 self.speed /= 0.8
00060 ost.LogVerbose("SpaceNav: speed reduced to "+str(self.speed))
00061 else:
00062 ost.LogVerbose("SpaceNav: unmapped button press ["+str(button)+"]")
00063
00064
00065 def _InitSpaceNav(app):
00066 try:
00067 spnav = gui.SpnavInput.GetQThread()
00068 if spnav:
00069 spnav.start()
00070 parent = app.gl_win.qobject
00071 SpacenavControl(spnav,parent)
00072 ost.LogInfo("SpaceNav: device found and connected")
00073 else:
00074 ost.LogInfo("SpaceNav: no device found, or could not connect to device socket")
00075 except:
00076 ost.LogInfo("SpaceNav: caught exception during initialization: %s"%(traceback.format_exc()))