OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
init_spacenav.py
Go to the documentation of this file.
1 import math,traceback
2 
3 from PyQt4 import QtCore
4 
5 import ost
6 from ost import gfx, gui
7 
8 class SpacenavControl(QtCore.QObject):
9  def __init__(self, spnav,
10  parent=None):
11  QtCore.QObject.__init__(self, parent)
12  QtCore.QObject.connect(spnav,QtCore.SIGNAL("deviceTransformed(int,int,int,int,int,int)"), self.Changed)
13  QtCore.QObject.connect(spnav,QtCore.SIGNAL("deviceButtonPressed(int)"), self.Toggle)
14 
15  self.trans = True
16  self.rot = True
17  self.speed = 480.0
18 
19  def Changed(self, tx,ty,tz,rx,ry,rz):
20  scene=gfx.Scene()
21  tf = scene.transform
22  def d(r):
23  if r==0.0:
24  return 0.0
25  rr=r/abs(r)*max(0.0,abs(r)-0.9)
26  if abs(rr)>0:
27  return rr/abs(rr)*(math.pow(1.01,abs(rr))-1.0)*40.0/self.speed
28  return 0.0
29  if(self.trans):
30  tf.ApplyXAxisTranslation(d(tx))
31  tf.ApplyYAxisTranslation(d(ty))
32  # adjust translation speed to distance from viewpoint
33  currz=tf.trans[2];
34  delta=currz*math.pow(1.01,d(tz))-currz;
35  tf.ApplyZAxisTranslation(delta);
36  # adjust near and far clipping planes together with z trans
37  scene.SetNearFar(scene.near-delta,scene.far-delta);
38  if(self.rot):
39  tf.ApplyXAxisRotation(d(rx))
40  tf.ApplyYAxisRotation(d(ry))
41  tf.ApplyZAxisRotation(d(rz))
42 
43  scene.transform=tf
44  scene.RequestRedraw()
45 
46  def Toggle(self, button):
47  if button == 0:
48  self.trans = not self.trans
49  ost.LogVerbose("SpaceNav: translation %s" % (self.strans and "enabled" or "disabled"))
50  elif button == 1:
51  self.rot = not self.rot
52  #ost.LogVerbose("SpaceNav: rotation %s"%("enabled" if self.rot else "disabled"))
53  ost.LogVerbose("SpaceNav: rotation %s"%(self.rot and "enabled" or "disabled"))
54  elif button == 12:
55  if self.speed > 20:
56  self.speed *= 0.8
57  ost.LogVerbose("SpaceNav: speed increased to "+str(self.speed))
58  elif button == 13:
59  self.speed /= 0.8
60  ost.LogVerbose("SpaceNav: speed reduced to "+str(self.speed))
61  else:
62  ost.LogVerbose("SpaceNav: unmapped button press ["+str(button)+"]")
63 
64 
65 def _InitSpaceNav(app):
66  try:
67  spnav = gui.SpnavInput.GetQThread()
68  if spnav:
69  spnav.start()
70  parent = app.gl_win.qobject
71  SpacenavControl(spnav,parent)
72  ost.LogInfo("SpaceNav: device found and connected")
73  else:
74  ost.LogInfo("SpaceNav: no device found, or could not connect to device socket")
75  except:
76  ost.LogInfo("SpaceNav: caught exception during initialization: %s"%(traceback.format_exc()))