forked from lanceulmer/dollar.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gestures.lua
321 lines (266 loc) · 7.88 KB
/
gestures.lua
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
local m = {}
m.__index = m
local atan2, cos, sin, atan, acos = math.atan2, math.cos, math.sin, math.atan, math.acos
local sqrt, abs, min, max, huge = math.sqrt, math.abs, math.min, math.max, math.huge
local ORIGIN = {0, 0}
local POINT_COUNT = 64
local HALF_DIAGONAL = 0.5 * math.sqrt(2) -- of a unit square
local ANGLE_RANGE = math.pi / 4
local ANGLE_PRECISION = math.pi / 90 -- 2 degrees
local PHI = 0.5 * (-1.0 + math.sqrt(5.0)) -- golden ratio
local function centroid(points)
local x, y = 0.0, 0.0
for i = 1, #points do
x = x + points[i][1]
y = y + points[i][2]
end
return {x / #points, y / #points}
end
local function rotateby(points, radians)
local c = centroid(points)
local cos_r = cos(radians)
local sin_r = sin(radians)
local newpoints = {}
for i = 1, #points do
local qx = (points[i][1] - c[1]) * cos_r - (points[i][2] - c[2]) * sin_r + c[1]
local qy = (points[i][1] - c[1]) * sin_r + (points[i][2] - c[2]) * cos_r + c[2]
newpoints[i] = {qx, qy}
end
return newpoints
end
local function distance(p1, p2)
local dx = p2[1] - p1[1]
local dy = p2[2] - p1[2]
return sqrt(dx * dx + dy * dy)
end
local function distanceAtAngle(points, template, radians)
local newpoints = rotateby(points, radians)
local d = 0.0
for i = 1, #newpoints do
d = d + distance(newpoints[i], template.points[i])
end
return d / #points
end
local function pathlength(points)
local d = 0.0
for i = 2, #points do
d = d + distance(points[i - 1], points[i])
end
return d
end
local function boundingbox(points)
local minX, maxX, minY, maxY = huge, -huge, huge, -huge
for i = 1, #points do
minX = min(minX, points[i][1])
maxX = max(maxX, points[i][1])
minY = min(minY, points[i][2])
maxY = max(maxY, points[i][2])
end
return {minX, minY, maxX - minX, maxY - minY} -- x, y, width, height
end
local function resample(points, n)
if type(points[1]) == 'number' then
local flatpoints = points
points = {}
for i = 1, #flatpoints / 2 do
points[i] = {flatpoints[i * 2 - 1], flatpoints[i * 2]}
end
end
local I = pathlength(points) / (n - 1)
local D = 0.0
local newpoints = {points[1]}
local i = 2
while i <= #points do
local d = distance(points[i - 1], points[i])
if (D + d) >= I then
local qx = points[i - 1][1] + ((I - D) / d) * (points[i][1] - points[i - 1][1])
local qy = points[i - 1][2] + ((I - D) / d) * (points[i][2] - points[i - 1][2])
table.insert(newpoints, {qx, qy})
points[i - 1] = {qx, qy}
D = 0.0
else
D = D + d
i = i + 1
end
end
if #newpoints == n - 1 then
table.insert(newpoints, {points[#points][1], points[#points][2]})
end
return newpoints
end
local function scalePoints(points, size, uniform)
local bbox = boundingbox(points)
local newpoints = {}
for i = 1, #points do
local qx, qy
if uniform then
local scale = max(bbox[3], bbox[4])
qx = points[i][1] * (size / scale)
qy = points[i][2] * (size / scale)
else
qx = points[i][1] * (size / bbox[3])
qy = points[i][2] * (size / bbox[4])
end
newpoints[i] = {qx, qy}
end
return newpoints
end
local function translatePoints(points, pt)
local c = centroid(points)
local newpoints = {}
for i = 1, #points do
newpoints[i] = {
points[i][1] + pt[1] - c[1],
points[i][2] + pt[2] - c[2]
}
end
return newpoints
end
local function vectorizePoints(points)
local sum = 0.0
local vector = {}
for i = 1, #points do
vector[#vector + 1] = points[i][1]
vector[#vector + 1] = points[i][2]
sum = sum + points[i][1] * points[i][1] + points[i][2] * points[i][2]
end
local magnitude = sqrt(sum)
for i = 1, #vector do
vector[i] = vector[i] / magnitude
end
return vector
end
local function distanceAtBestAngle(points, template)
local a = -ANGLE_RANGE
local b = ANGLE_RANGE
local x1 = PHI * a + (1.0 - PHI) * b
local f1 = distanceAtAngle(points, template, x1)
local x2 = (1.0 - PHI) * a + PHI * b
local f2 = distanceAtAngle(points, template, x2)
while abs(b - a) > ANGLE_PRECISION do
if f1 < f2 then
b = x2
x2 = x1
f2 = f1
x1 = PHI * a + (1.0 - PHI) * b
f1 = distanceAtAngle(points, template, x1)
else
a = x1
x1 = x2
f1 = f2
x2 = (1.0 - PHI) * a + PHI * b
f2 = distanceAtAngle(points, template, x2)
end
end
return min(f1, f2)
end
local function optimalCosineDist(v1, v2, oriented)
local a, b = 0.0, 0.0
for i = 1, #v1, 2 do
a = a + v1[i] * v2[i] + v1[i + 1] * v2[i + 1]
b = b + v1[i] * v2[i + 1] - v1[i + 1] * v2[i]
end
local angle = atan(b / a)
local d = acos(a * cos(angle) + b * sin(angle))
if oriented and abs(angle) > ANGLE_RANGE then d = d + 1 end
return d
end
function m.new(oriented, uniform, protractor)
local self = setmetatable({
templates = {},
oriented = oriented ~= false, -- rotation-sensitive gestures
uniform = uniform ~= false, -- gestures uniformly scaled or left unscaled
protractor = protractor ~= false, -- use improved faster algorithm
capturing = {},
}, m)
return self
end
function m:add(name, points)
local template = {
name = name,
points = resample(points, POINT_COUNT),
}
if not self.oriented then
local radians = atan2(centroid(template.points)[2] - template.points[1][2],
centroid(template.points)[1] - template.points[1][1])
template.points = rotateby(template.points, -radians)
end
template.points = scalePoints(template.points, 1, self.uniform)
template.points = translatePoints(template.points, ORIGIN)
template.vector = vectorizePoints(template.points)
table.insert(self.templates, template)
local count = 0
for _, t in ipairs(self.templates) do
if t.name == name then count = count + 1 end
end
return count
end
function m:remove(name)
local count = 0
for i = #self.templates, 1, -1 do
if self.templates[i].name == name then
count = count + 1
table.remove(self.templates, i)
end
end
return count
end
function m:clear()
self.templates = {}
end
function m:capture(x, y)
table.insert(self.capturing, {x, y})
end
function m:recognize(points)
if not points then
points = self.capturing
self.capturing = {}
end
points = resample(points, POINT_COUNT)
if #points ~= POINT_COUNT then return nil, 0 end
if not self.oriented then
local radians = atan2(centroid(points)[2] - points[1][2],
centroid(points)[1] - points[1][1])
points = rotateby(points, -radians)
end
points = scalePoints(points, 1, self.uniform)
points = translatePoints(points, ORIGIN)
local vector = vectorizePoints(points)
local bestDistance = huge
local bestIndex = 1
for i, template in ipairs(self.templates) do
local d
if self.protractor then
d = optimalCosineDist(template.vector, vector, self.oriented)
else
d = distanceAtBestAngle(points, template)
end
if d < bestDistance then
bestDistance = d
bestIndex = i
end
end
local name = self.templates[bestIndex] and self.templates[bestIndex].name
local score = self.protractor and 1.0 / bestDistance or 1.0 - bestDistance / HALF_DIAGONAL
return name, score, bestIndex
end
function m:toString()
local lines = {}
for _, template in ipairs(self.templates) do
local points = {}
for _, point in ipairs(template.points) do
table.insert(points, string.format('{%.2f, %.2f}', point[1], point[2]))
end
local line = string.format("{ name = '%s', points = {%s} }", tostring(template.name), table.concat(points, ', '))
table.insert(lines, line)
end
return '{ ' .. table.concat(lines, ',\n') .. ' }'
end
function m:fromString(gesture_definitions)
local func = assert(load("return " .. gesture_definitions))
local deserializedData = func()
for _, entry in ipairs(deserializedData) do
self:add(entry.name, entry.points)
end
end
return m