-
Notifications
You must be signed in to change notification settings - Fork 1
/
TranslationalActions.py
74 lines (55 loc) · 2.2 KB
/
TranslationalActions.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
import math
import vtk
from PythonMetricsCalculator import PerkEvaluatorMetric
class TranslationalActions( PerkEvaluatorMetric ):
# Note: An action is defined as a time period where the absolute velocity of motion is greater than some threshold
VELOCITY_THRESHOLD = 50 #mm/s
TIME_THRESHOLD = 0.2 #s
# Static methods
@staticmethod
def GetMetricName():
return "Translational Actions"
@staticmethod
def GetMetricUnit():
return "count"
# Instance methods
def __init__( self ):
PerkEvaluatorMetric.__init__( self )
self.numActions = 0
self.actionState = 0
self.completeActionTime = 0
self.timePrev = None
self.matrixPrev = None
def AddTimestamp( self, time, matrix, point, role ):
if ( time == self.timePrev ):
return
if ( self.timePrev == None or self.matrixPrev == None ):
self.timePrev = time
self.matrixPrev = vtk.vtkMatrix4x4()
self.matrixPrev.DeepCopy( matrix )
return
invertPrev = vtk.vtkMatrix4x4()
invertPrev.DeepCopy( self.matrixPrev )
invertPrev.Invert()
currChangeMatrix = vtk.vtkMatrix4x4()
vtk.vtkMatrix4x4().Multiply4x4( matrix, invertPrev, currChangeMatrix )
currChangeTransform = vtk.vtkTransform()
currChangeTransform.SetMatrix( currChangeMatrix )
currVelocity = [ 0, 0, 0 ]
currChangeTransform.GetPosition( currVelocity )
vtk.vtkMath().MultiplyScalar( currVelocity, 1 / ( time - self.timePrev ) )
currAbsVelocity = math.sqrt( currVelocity[ 0 ] * currVelocity[ 0 ] + currVelocity[ 1 ] * currVelocity[ 1 ] + currVelocity[ 2 ] * currVelocity[ 2 ] )
currentTestState = ( currAbsVelocity > TranslationalActions.VELOCITY_THRESHOLD )
if ( currentTestState == self.actionState ):
self.completeActionTime = time
else:
if ( ( time - self.completeActionTime ) > TranslationalActions.TIME_THRESHOLD ):
self.actionState = currentTestState
self.completeActionTime = time
if ( currentTestState == 1 ):
self.numActions += 1
self.timePrev = time
self.matrixPrev = vtk.vtkMatrix4x4()
self.matrixPrev.DeepCopy( matrix )
def GetMetric( self ):
return self.numActions