-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiTouch.py
More file actions
77 lines (64 loc) · 2.35 KB
/
MultiTouch.py
File metadata and controls
77 lines (64 loc) · 2.35 KB
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
'''
CursorTracker.py will maintain a real-time list of all cursors
and their respective positions. It is updated via a tuio "thread"
Modified from Python Input Module for NumptyPhysics
'''
import sys
import collections
import math
WIDTH = 680
HEIGHT = 460
class CursorTracker(object):
def __init__(self, max_cursors):
self._seen = {}
self._coords = {}
self._prevCoords = {}
self._startCoords = {}
self._freeslots = collections.deque(range(max_cursors))
'''
FINGER DETECTION
'''
def update(self, cursors):
vanished = list(self._seen)
for cursor in cursors:
id, x, y = cursor.sessionid, cursor.xpos, cursor.ypos
x, y = self.convert_coords(x, y)
if id in self._seen:
# cursor still active
vanished.remove(id)
if self._coords[id] != (x, y):
self._prevCoords[id] = self._coords[id]
self._coords[id] = (x, y)
else:
self._prevCoords[id] = self._coords[id]
else:
# found a new cursor
taken_slot = self.grabslot()
if taken_slot is None:
print 'IGNORING EXCESSIVE CURSOR'
continue
self._seen[id] = taken_slot
self._startCoords[id] = (x,y)
self._prevCoords[id] = (x,y)
self._coords[id] = (x, y)
for id in vanished:
# the cursor vanished
self._startCoords[id] = (self._startCoords[id][0]+2, self._startCoords[id][1]+2)
self._prevCoords[id] = (self._prevCoords[id][0]+2, self._prevCoords[id][1]+2)
self._coords[id] = (self._coords[id][0]+2, self._coords[id][1]+2)
self.freeslot(self._seen[id])
del self._startCoords[id]
del self._prevCoords[id]
del self._coords[id]
del self._seen[id]
def grabslot(self):
try:
return self._freeslots.pop()
except IndexError:
return None
def freeslot(self, slot):
self._freeslots.appendleft(slot)
def convert_coords(self, x, y):
return (int(x*WIDTH), int(HEIGHT-y*HEIGHT))
def fingers_detected(self):
return len(self._seen)