forked from gabrielDiaz-performlab/CatchB
/
gazeTools.py
350 lines (273 loc) · 13.7 KB
/
gazeTools.py
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import viz
import vizact
import vizshape
import smi
import vizconnect
import numpy as np
import math
class gazeVector():
def __init__(self, eyeTracker, eye, parentNode, renderToWindows = None,gazeVectorColor=viz.RED):
self.eye = eye
self.eyeTracker = eyeTracker
self.renderToWindows = renderToWindows
#Creating a line
viz.startLayer(viz.LINES)
viz.lineWidth(4)#Set the size of the lines.
viz.vertex(0,0,0)
viz.vertex(0,0,3)
viz.vertexColor(gazeVectorColor)
self.gazeLine = viz.endLayer()
self.gazeLine.visible(True)
#self.gazeLine.setScale(1,1,1)
if( self.renderToWindows ):
self.gazeLine.renderOnlyToWindows(self.renderToWindows)
self.gazeLine.setParent(parentNode)
def toggleUpdate(self):
def moveGazeVector():
gazeSamp = []
gazeSamp = self.eyeTracker.getLastSample()
if( gazeSamp is None ):
return
if( self.eye == viz.LEFT_EYE):
gazeSamp = gazeSamp.leftEye;
elif( self.eye == viz.RIGHT_EYE ):
gazeSamp = gazeSamp.rightEye;
#3D gaze is provided as a normalized gaze direction vector (gazeDirection) and a gaze base point (gazeBasePoint).
#Gaze base point is given in mm with respect to the origin of the eyetracker coordinate system.
# Note: you must flip X
viewPos_XYZ = np.array(viz.MainView.getPosition(), dtype = float)
gazeDir_XYZ = np.array([ -gazeSamp.gazeDirection.x, gazeSamp.gazeDirection.y, gazeSamp.gazeDirection.z], dtype = float)
pupilPos_XYZ = [-gazeSamp.pupilPosition.x, gazeSamp.pupilPosition.y, gazeSamp.pupilPosition.z]
pupilPos_XYZ = np.divide(pupilPos_XYZ, 1000)
# Create a node3D
#gazePoint_XYZ = [viewPos_XYZ[0] + gazeDir_XYZ[0], viewPos_XYZ[1] + gazeDir_XYZ[1], viewPos_XYZ[2] + gazeDir_XYZ[2]]
gazePoint_XYZ = [gazeDir_XYZ[0], gazeDir_XYZ[1], gazeDir_XYZ[2]]
#gazePoint_XYZ = np.multiply(1.0, gazePoint_XYZ)
#self.gazeLine.setVertex(0, pupilPos_XYZ[0], pupilPos_XYZ[1], pupilPos_XYZ[2], viz.ABS_PARENT)
self.gazeLine.setVertex(0, 0, 0, viz.ABS_PARENT)
self.gazeLine.setVertex(1, gazePoint_XYZ[0], gazePoint_XYZ[1], gazePoint_XYZ[2], viz.ABS_PARENT)
#print 'GazePoint=[', gazePoint_XYZ, '],[', pupilPos_XYZ,']'
# self.node3D.enable(viz.RENDERING)
self.updateAct = vizact.onupdate(viz.PRIORITY_INPUT+1,moveGazeVector)
class gazeSphere():
def __init__(self,eyeTracker,eye,parentNode,renderToWindows = None,sphereColor=viz.RED):
self.sizeInDegrees = 0.5
self.sphereDistance = 1
self.renderToWindows = renderToWindows
from math import tan, radians
self.radius = tan(radians(self.sizeInDegrees)) * self.sphereDistance
#with viz.cluster.MaskedContext(viz.CLIENT1):
self.node3D = vizshape.addSphere(radius=self.radius, color = sphereColor, alpha = 0.4)
if( self.renderToWindows ):
self.node3D.renderOnlyToWindows(self.renderToWindows)
self.node3D.disable(viz.RENDERING)
self.updateAct = []
self.eyeTracker = eyeTracker
self.eye = eye
self.node3D.setParent(parentNode)
def toggleUpdate(self):
def moveGazeSphere():
gazeSamp = []
#if( self.eye == viz.BOTH_EYE):
gazeSamp = self.eyeTracker.getLastSample()
if( gazeSamp is None ):
return
#timestamp = gazeSamp.timestamp
if( self.eye == viz.LEFT_EYE):
gazeSamp = gazeSamp.leftEye;
elif( self.eye == viz.RIGHT_EYE ):
gazeSamp = gazeSamp.rightEye;
#3D gaze is provided as a normalized gaze direction vector (gazeDirection) and a gaze base point (gazeBasePoint).
#Gaze base point is given in mm with respect to the origin of the eyetracker coordinate system.
# Note: you must flip X
gazeDirXYZ = [ -gazeSamp.gazeDirection.x, gazeSamp.gazeDirection.y, gazeSamp.gazeDirection.z]
gazePointXYZ = self.sphereDistance * gazeDirXYZ
#with viz.cluster.MaskedContext(viz.CLIENT1):# show
self.node3D.setPosition( gazePointXYZ,viz.ABS_PARENT)
self.node3D.enable(viz.RENDERING)
self.updateAct = vizact.onupdate(viz.PRIORITY_INPUT+1,moveGazeSphere)
class calibrationTools():
def __init__(self, parentNode, renderToWindows = None, cyclopEyeSphere = None, config = None, room = None):
self.config = config
self.room = room
self.calibrationInProgress = False
self.parentNode = parentNode
self.renderToWindows = renderToWindows
self.calibrationSphere = None
self.cyclopEyeSphere = cyclopEyeSphere
self.minimumAngle = float(self.config.expCfg['room']['minimumStimuliSize'])
self.minimumAngle = (self.minimumAngle * np.pi)/(60*180) # We want the calibration point subtend 15 arcmin to the subject's eye
self.localAction = None
self.initialValue = 0.02
self.calibrationSphereRadius = self.initialValue
self.calibrationBlockCounter = 900
self.calibrationCounter = 0
self.maximumAngularError = float(self.config.expCfg['room']['maximumAngularError'])
self.textObjectPosition = map(float,self.config.expCfg['room']['textObjectPosition'])
self.calibrationPositionRange_X = map(float,self.config.expCfg['room']['calibrationPointsRange_X'])
self.calibrationPositionRange_Y = map(float,self.config.expCfg['room']['calibrationPointsRange_Y'])
self.calibrationPositionRange_Z = map(float,self.config.expCfg['room']['calibrationPointsRange_Z'])
self.numberOfCalibrationPoints = float(self.config.expCfg['room']['calibrationPointPerPlane'])
def dotproduct(self, v1, v2):
return sum((a*b) for a, b in zip(v1, v2))
def length(self, v):
return math.sqrt(self.dotproduct(v, v))
def angle(self, v1, v2):
return math.acos(self.dotproduct(v1, v2) / (self.length(v1) * self.length(v2)))
def calculateAngularError(self, node1, node2, textObject):
vector1 = node1.getPosition(viz.ABS_PARENT)
#V1 = [a - b for a, b in zip(vector1, self.parentNode.getPosition(viz.ABS_GLOBAL))]
V1 = vector1
if (node2 == 0.0):
if ( self.calibrationCounter < 27 ):
V2 = self.calibrationPositions[self.calibrationCounter,:]# + self.parentNode.getPosition(viz.ABS_GLOBAL)
else:
V2 = self.calibrationSphere.getPosition()
else:
V2 = [b - a for a, b in zip(node1.getPosition(viz.ABS_GLOBAL), node2.getPosition(viz.ABS_GLOBAL))]
self.errorAngle = np.multiply(self.angle(V1,V2), 180/np.pi)
#print 'Angular Error = %.2f %c'%(errorAngle, u"\u00b0")
#print 'Angular Error = %.2f %c'%(self.errorAngle, u"\u00b0")
textObject.message('AE = %.1f %c'%(self.errorAngle, u"\u00b0"))
textObject.setPosition(self.textObjectPosition, viz.ABS_PARENT)
if ( self.errorAngle < self.maximumAngularError ):
textObject.color(self.errorAngle/self.maximumAngularError, 1 - self.errorAngle/self.maximumAngularError, 0)
else:
textObject.color(viz.RED)
def create3DCalibrationPositions(self, xRange, yRange, zRange, numberOfGridPoints):
x = np.linspace(xRange[0], xRange[1], numberOfGridPoints)
y = np.linspace(yRange[0], yRange[1], numberOfGridPoints)
z = np.linspace(zRange[0], zRange[1], numberOfGridPoints)
points = np.empty(shape = (1,3), dtype = float)
#points.resize(1,3)
for k in z:
for j in y:
for i in x:
points = np.vstack((points, [i,j,k]))
points = np.delete(points, 0, 0) # TODO: The first element is initialized by a random value!!? Why? Should be fixed later (KAMRAN)
self.calibrationPositions = points
self.numberOfCalibrationPoints = self.calibrationPositions.shape[0]
#print 'Number of Calibration Points =', self.numberOfCalibrationPoints
#print 'calibration points:\n', points
def toggleRoomWallsVisibility(self):
self.room.ceiling.node3D.visible(viz.TOGGLE)
self.room.floor.node3D.visible(viz.TOGGLE)
self.room.wall_PosZ.node3D.visible(viz.TOGGLE)
self.room.wall_NegZ.node3D.visible(viz.TOGGLE)
self.room.wall_PosX.node3D.visible(viz.TOGGLE)
self.room.wall_NegX.node3D.visible(viz.TOGGLE)
def checkActionDone(self):
distance = np.array(self.calibrationSphere.getPosition()) - np.array([-3.0,0.0,6.0])
#print 'Distance', distance
if(abs( self.length(distance)) < 0.1):
self.calibrationInProgress = False
self.localAction.remove()
self.text_object.remove()
self.calibrationCounter = 0
self.targetMovingAction.remove()
self.calibrationCounter = 100
self.calibrationSphere.remove()
self.toggleRoomWallsVisibility()
print 'Quit Dynamic Calibration!!'
def dynamicCalibrationMethod(self):
if ( self.calibrationInProgress == False ):
self.calibrationInProgress = True
self.toggleRoomWallsVisibility()
self.calibrationCounter = 27
self.calibrationSphereRadius = self.initialValue
self.calibrationSphere = vizshape.addSphere(self.calibrationSphereRadius, color = viz.PURPLE)
self.calibrationSphere.emissive(viz.PURPLE)
self.calibrationSphere.setParent(self.parentNode)
newPos = [-3,-.5,12]
self.setSphereRadius(self.parentNode.getPosition(viz.ABS_GLOBAL), newPos, 0)
self.calibrationSphere.setPosition(newPos[0], newPos[1], newPos[2],viz.ABS_PARENT)
self.targetMovingAction = vizact.onupdate(viz.PRIORITY_INPUT+1, self.checkActionDone)
self.text_object = viz.addText('')
self.text_object.setParent(self.calibrationSphere)
self.text_object.renderOnlyToWindows([self.renderToWindows])
self.localAction = vizact.onupdate(viz.PRIORITY_INPUT+1,self.calculateAngularError, self.cyclopEyeSphere.node3D, 0.0, self.text_object)#self.currentTrial.ballObj.node3D
print 'Dynamic Calibration Procedure Started'
#Use a moveTo action to move a node to the point [0,0,25] at 2 meters per second
self.moveAction = vizact.moveTo([-3,-0.5,12],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([3,-0.5,12],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([3,3,12],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([-3,3,12],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([-3,-0.5,12],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([-3,-0.5,6],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([3,-0.5,6],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([3,3,6],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([-3,3,6],speed=2)
self.calibrationSphere.addAction(self.moveAction)
self.moveAction = vizact.moveTo([-3,0,6],speed=2)
self.calibrationSphere.addAction(self.moveAction)
else:
self.localAction.remove()
self.text_object.remove()
self.calibrationInProgress = False
self.calibrationCounter = 0
self.calibrationSphere.remove()
self.toggleRoomWallsVisibility()
print 'Quit Dynamic Calibration!!'
def staticCalibrationMethod(self):
if ( self.calibrationInProgress == False ):
self.calibrationBlockCounter += 100
self.toggleRoomWallsVisibility()
self.calibrationInProgress = True
self.calibrationCounter = 0
self.calibrationSphereRadius = self.initialValue
self.calibrationSphere = vizshape.addSphere(self.calibrationSphereRadius, color = viz.PURPLE)
self.calibrationSphere.emissive(viz.PURPLE)
self.calibrationSphere.setParent(self.parentNode)
self.setSphereRadius(self.parentNode.getPosition(viz.ABS_GLOBAL), self.calibrationPositions[self.calibrationCounter,:], 0)
print 'FirstPos', self.calibrationPositions[self.calibrationCounter,:]
newPos = [self.calibrationPositions[self.calibrationCounter,0], self.calibrationPositions[self.calibrationCounter,1], self.calibrationPositions[self.calibrationCounter,2]]
self.calibrationSphere.setPosition(newPos[0], newPos[1], newPos[2],viz.ABS_PARENT)
self.text_object = viz.addText('')
self.text_object.setParent(self.calibrationSphere)
self.text_object.renderOnlyToWindows([self.renderToWindows])
self.localAction = vizact.onupdate(viz.PRIORITY_INPUT+1,self.calculateAngularError, self.cyclopEyeSphere.node3D, 0.0, self.text_object)#self.currentTrial.ballObj.node3D
print 'Static Calibration Started'
else:
self.calibrationInProgress = False
self.calibrationCounter = 0
self.calibrationSphere.remove()
self.localAction.remove()
self.text_object.remove()
self.toggleRoomWallsVisibility()
print 'Quit Static Calibration!!'
def updateCalibrationPoint(self):
if( self.calibrationInProgress == True ):
self.calibrationCounter +=1
if( self.calibrationCounter < self.numberOfCalibrationPoints ):
newPos = [self.calibrationPositions[self.calibrationCounter,0], self.calibrationPositions[self.calibrationCounter,1], self.calibrationPositions[self.calibrationCounter,2]]
self.calibrationSphere.setPosition(newPos[0], newPos[1], newPos[2],viz.ABS_PARENT)
self.setSphereRadius(self.parentNode.getPosition(viz.ABS_GLOBAL), self.calibrationPositions[self.calibrationCounter,:], 0)
print 'Calibratring for Point[%d]' %(self.calibrationCounter), 'at [%f %f %f]' % (newPos[0], newPos[1], newPos[2])
print 'Counter', self.calibrationBlockCounter + self.calibrationCounter
else:
self.calibrationInProgress = False
self.calibrationCounter = 0
self.calibrationSphere.remove()
self.localAction.remove()
self.text_object.remove()
self.toggleRoomWallsVisibility()
print 'Calibration Done Successfully'
def setSphereRadius(self, eyePos, targetPos, radius):
if ( radius == 0 ):
distance = np.linalg.norm(targetPos)
self.calibrationSphereRadius = distance * np.tan(self.minimumAngle)
#print 'Radius Changed to ', self.calibrationSphereRadius,' theta = ', 0.5 * 180 * np.arctan( self.calibrationSphereRadius/distance )/np.pi
else:
self.calibrationSphereRadius = radius
#print '==>Radius Changed to ', self.calibrationSphereRadius
ratio = self.calibrationSphereRadius/self.initialValue
self.calibrationSphere.setScale([ratio, ratio, ratio], viz.ABS_PARENT)
print 'SetSphere to :',self.calibrationSphereRadius