-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeapClassPy3.py
205 lines (97 loc) · 4.91 KB
/
LeapClassPy3.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 08:44:58 2019
@author: GELab
"""
import time
import pickle
import collections as col
import sys
if sys.version_info.major == 2:
##only for python2 you would need Leap
sys.path.insert(0, "./LeapDeveloperKit_3.2.1_win/LeapDeveloperKit_3.2.1+45911_win/LeapSDK/lib")
sys.path.insert(1, "./LeapDeveloperKit_3.2.1_win/LeapDeveloperKit_3.2.1+45911_win/LeapSDK/lib/x64" )
import Leap
class CoOrdinates(object):
def __init__(self,cordList,dirOrPos = 'pos'):
self.x = None
self.y = None
self.z = None
self.pitch = None
self.yaw = None
self.roll = None
if dirOrPos == 'pos':
self._setPositionalAttributes(cordList)
if dirOrPos == 'dir':
self._setPositionalAttributes(cordList)
def _setPositionalAttributes(self,cordList, numElems = 3):
self.x = cordList[0]
self.y = cordList[1]
self.z = cordList[2]
return self
def _setDirectionalAttributes(self,cordList,numElems = 3):
self.pitch = cordList[0]
self.yaw = cordList[1]
self.roll = cordList[2]
return self
def __repr__(self):
string = '(x: {}, y: {}, z: {}), (pitch: {}, roll: {}, yaw: {}) '.format(self.x,self.y,self.z ,
self.pitch,self.yaw,self.roll
)
return string
class LeapFinger3(object):
def __init__(self):
self.type = None
self.typeName = None
self.relativeTipPosition = None
self.relativeTipDirection = None
self.absTipPosition = None
self.absTipDirection = None
pass
def __repr__(self):
typeName = self.typeName
string1 = str(self.absTipPosition)
string2 = str(self.relativeTipPosition)
return 'Finger {} @ abs position is {} \n and relative position is {} '.format(typeName,string1,string2)
class LeapHand3(object):
finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
def __init__(self,OrigHand):
self.fingers = []
self.handType = None ##Left or right
self.palmNormal = None
self.handDirection = None
self.palmPosition = None
self.arm_direction = None
self.wrist_position = None
self.setFingerTipAttributes(OrigHand)
self.setHandAttributes(OrigHand)
pass
def setFingerTipAttributes(self,origHand):
hand_x_basis = origHand.basis.x_basis
hand_y_basis = origHand.basis.y_basis
hand_z_basis = origHand.basis.z_basis
hand_origin = origHand.palm_position
hand_transform = Leap.Matrix(hand_x_basis, hand_y_basis, hand_z_basis, hand_origin)
hand_transform = hand_transform.rigid_inverse()
for finger in origHand.fingers:
newFinger = LeapFinger3()
newFinger.type = finger.type
newFinger.typeName = self.finger_names[finger.type]
fingtip_transformed_position = hand_transform.transform_point(finger.stabilized_tip_position)
fingtip_transformed_direction = hand_transform.transform_direction(finger.direction)
newFinger.absTipPosition = CoOrdinates(finger.stabilized_tip_position,'pos')
newFinger.absTipDirection = CoOrdinates(finger.direction,'dir')
newFinger.relativeTipPosition = CoOrdinates(fingtip_transformed_position,'pos')
newFinger.relativeTipDirection = CoOrdinates(fingtip_transformed_direction,'dir')
self.fingers.append(newFinger)
return self
def setHandAttributes(self,OrigHand):
self.handType = "Left_hand" if OrigHand.is_left else "Right_hand"
self.palmNormal = CoOrdinates(OrigHand.palm_normal,'pos')
self.handDirection = CoOrdinates(OrigHand.direction,'dir')
self.palmPosition = CoOrdinates(OrigHand.palm_position, 'pos')
self.arm_direction = CoOrdinates(OrigHand.arm.direction,'dir')
self.wrist_position = CoOrdinates(OrigHand.arm.wrist_position,'pos')
return self
def __repr__(self):
return 'Hand/Palm at position : {}'.format(self.palmPosition)