-
Notifications
You must be signed in to change notification settings - Fork 1
/
BimanualDexterity.py
154 lines (125 loc) · 6.24 KB
/
BimanualDexterity.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
import math
import vtk
from PythonMetricsCalculator import PerkEvaluatorMetric
# Adapted from: Hofstad et al., A study of psychomotor skills in minimally invasive surgery: what differentiates expert and nonexpert performance, Surgical Endoscopy, 2013.
class BimanualDexterity( PerkEvaluatorMetric ):
# Static methods
@staticmethod
def GetMetricName():
return "Bimanual Dexterity: Translational & Rotational"
@staticmethod
def GetMetricUnit():
return "rho"
@staticmethod
def IsShared():
return False
@staticmethod
def GetMetricShared():
return False
@staticmethod
def GetTransformRoles():
return [ "LeftTool", "RightTool" ]
# Instance methods
def __init__( self ):
self.prevLeftInverseMatrix = None
self.prevRightInverseMatrix = None
self.prevLeftTime = None
self.prevRightTime = None
self.currLeftRotationalSpeed = None
self.currRightRotationalSpeed = None
self.currLeftTranslationalSpeed = None
self.currRightTranslationalSpeed = None
self.leftRotationalSumSquares = 0.0
self.rightRotationalSumSquares = 0.0
self.leftRotationalSum = 0.0
self.rightRotationalSum = 0.0
self.rotationalSumProducts = 0.0
self.leftTranslationalSumSquares = 0.0
self.rightTranslationalSumSquares = 0.0
self.leftTranslationalSum = 0.0
self.rightTranslationalSum = 0.0
self.translationalSumProducts = 0.0
self.leftCount = 0
self.rightCount = 0
self.totalCount = 0
def AddAnatomyRole( self, role, node ):
pass
def AddTimestamp( self, time, matrix, point, role ):
prevInverseMatrix = None
if ( role == "LeftTool" ):
prevInverseMatrix = self.prevLeftInverseMatrix
if ( role == "RightTool" ):
prevInverseMatrix = self.prevRightInverseMatrix
angle = None
distance = None
if ( prevInverseMatrix is not None ):
changeTransform = vtk.vtkTransform()
changeTransform.Concatenate( matrix )
changeTransform.Concatenate( prevInverseMatrix ) # matrix * prevInverseMatrix
# Rotation
axisAngle = [ 0, 0, 0, 0 ]
changeTransform.GetOrientationWXYZ( axisAngle ) # This is in degrees
angle = axisAngle[ 0 ]
if ( angle > 180 ):
angle = angle - 360
# Translation
position = [ 0, 0, 0 ]
changeTransform.GetPosition( position )
distance = vtk.vtkMath.Norm( position )
if ( role == "LeftTool" ):
if ( self.prevLeftTime is not None and angle is not None and distance is not None ):
# Speeds
self.currLeftRotationalSpeed = abs( angle / ( time - self.prevLeftTime ) )
self.currLeftTranslationalSpeed = abs( distance / ( time - self.prevLeftTime ) )
# Update the sums
self.leftRotationalSum += self.currLeftRotationalSpeed
self.leftRotationalSumSquares += self.currLeftRotationalSpeed * self.currLeftRotationalSpeed
self.leftTranslationalSum += self.currLeftTranslationalSpeed
self.leftTranslationalSumSquares += self.currLeftTranslationalSpeed * self.currLeftTranslationalSpeed
# Increase count
self.leftCount += 1
# Update previous
self.prevLeftTime = time
self.prevLeftInverseMatrix = vtk.vtkMatrix4x4()
self.prevLeftInverseMatrix.DeepCopy( matrix )
self.prevLeftInverseMatrix.Invert()
if ( role == "RightTool" ):
if ( self.prevRightTime is not None and angle is not None and distance is not None ):
# Speeds
self.currRightRotationalSpeed = abs( angle / ( time - self.prevRightTime ) )
self.currRightTranslationalSpeed = abs( distance / ( time - self.prevRightTime ) )
# Update the sums
self.rightRotationalSum += self.currRightRotationalSpeed
self.rightRotationalSumSquares += self.currRightRotationalSpeed * self.currRightRotationalSpeed
self.rightTranslationalSum += self.currRightTranslationalSpeed
self.rightTranslationalSumSquares += self.currRightTranslationalSpeed * self.currRightTranslationalSpeed
# Increase count
self.rightCount += 1
# Update previous
self.prevRightTime = time
self.prevRightInverseMatrix = vtk.vtkMatrix4x4()
self.prevRightInverseMatrix.DeepCopy( matrix )
self.prevRightInverseMatrix.Invert()
if ( self.currLeftRotationalSpeed is None or self.currRightRotationalSpeed is None or self.currLeftTranslationalSpeed is None or self.currRightTranslationalSpeed is None ):
return
self.rotationalSumProducts += self.currLeftRotationalSpeed * self.rotationalSumProducts
self.translationalSumProducts += self.currLeftTranslationalSpeed * self.currRightTranslationalSpeed
self.totalCount += 1
def GetMetric( self ):
if ( self.totalCount == 0 or self.leftCount == 0 or self.rightCount == 0 ):
return 0
leftRotationalMean = self.leftRotationalSum / self.leftCount
leftTranslationalMean = self.leftTranslationalSum / self.leftCount
rightRotationalMean = self.rightRotationalSum / self.rightCount
rightTranslationalMean = self.rightTranslationalSum / self.rightCount
leftRotationalStdev = math.sqrt( self.leftRotationalSumSquares / self.leftCount - leftRotationalMean * leftRotationalMean )
leftTranslationalStdev = math.sqrt( self.leftTranslationalSumSquares / self.leftCount - leftTranslationalMean * leftTranslationalMean )
rightRotationalStdev = math.sqrt( self.rightRotationalSumSquares / self.rightCount - rightRotationalMean * rightRotationalMean )
rightTranslationalStdev = math.sqrt( self.rightTranslationalSumSquares / self.rightCount - rightTranslationalMean * rightTranslationalMean )
rotationalCovariance = self.rotationalSumProducts / self.totalCount - leftRotationalMean * rightRotationalMean
translationalCovariance = self.translationalSumProducts / self.totalCount - leftTranslationalMean * rightTranslationalMean
rotationalBimanualDexterity = rotationalCovariance / ( leftRotationalStdev * rightRotationalStdev )
translationalBimanualDexterity = translationalCovariance / ( leftTranslationalStdev * rightTranslationalStdev )
bimanualDexterity = [ translationalBimanualDexterity, rotationalBimanualDexterity ]
separator = "\t"
return separator.join( map( str, bimanualDexterity ) )