-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunistroke_recognizer.js
323 lines (319 loc) · 14.3 KB
/
unistroke_recognizer.js
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
322
323
//////////////////////////////////////////////////////////////////////////////
/**
* The $1 Unistroke Recognizer (JavaScript version)
* from https://depts.washington.edu/acelab/proj/dollar/
*
* Wobbrock, J.O., Wilson, A.D. and Li, Y. (2007). Gestures without
* libraries, toolkits or training: A $1 recognizer for user interface
* prototypes. Proceedings of the ACM Symposium on User Interface
* Software and Technology (UIST '07). Newport, Rhode Island (October
* 7-10, 2007). New York: ACM Press, pp. 159-168.
* https://dl.acm.org/citation.cfm?id=1294238
*
* This software is distributed under the "New BSD License" agreement:
*
* Copyright (C) 2007-2012, Jacob O. Wobbrock, Andrew D. Wilson and Yang Li.
* All rights reserved. Last updated July 14, 2018.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of the University of Washington nor Microsoft,
* nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Jacob O. Wobbrock OR Andrew D. Wilson
* OR Yang Li BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
//
// Point class
//
function Point(x, y) // constructor
{
this.X = x;
this.Y = y;
}
//
// Rectangle class
//
function Rectangle(x, y, width, height) // constructor
{
this.X = x;
this.Y = y;
this.Width = width;
this.Height = height;
}
//
// Unistroke class: a unistroke template
//
function Unistroke(name, points) // constructor
{
this.Name = name;
this.Points = Resample(points, NumPoints);
var radians = IndicativeAngle(this.Points);
this.Points = RotateBy(this.Points, -radians);
this.Points = ScaleTo(this.Points, SquareSize);
this.Points = TranslateTo(this.Points, Origin);
this.Vector = Vectorize(this.Points); // for Protractor
}
//
// Result class
//
function Result(name, score, ms) // constructor
{
this.Name = name;
this.Score = score;
this.Time = ms;
}
//
// DollarRecognizer constants
//
const NumUnistrokes = 9; //reduced from 16
const NumPoints = 64;
const SquareSize = 250.0; //TODO: should this change?
const Origin = new Point(0,0);
const Diagonal = Math.sqrt(SquareSize * SquareSize + SquareSize * SquareSize);
const HalfDiagonal = 0.5 * Diagonal;
const AngleRange = Deg2Rad(45.0);
const AnglePrecision = Deg2Rad(2.0);
const Phi = 0.5 * (-1.0 + Math.sqrt(5.0)); // Golden Ratio
//
// DollarRecognizer class
//
function DollarRecognizer() // constructor
{
//
// one built-in unistroke per gesture type
//
//"x" reset speed
//"caret" speed up
//"v" slow down
//"circle" switch between emoji and text
//"point" select letter
this.Unistrokes = new Array(NumUnistrokes);
this.Unistrokes[0] = new Unistroke("z", new Array(new Point(7,8), new Point(12,8), new Point(17,8), new Point(22,8), new Point(27,8), new Point(32,8), new Point(37,8), new Point(42,8), new Point(47,8), new Point(46,9), new Point(42,11), new Point(39,14), new Point(35,17), new Point(32,19), new Point(27,21), new Point(24,23), new Point(20,26), new Point(18,29), new Point(14,30), new Point(12,35), new Point(9,41), new Point(14,41), new Point(19,41), new Point(24,41), new Point(29,41), new Point(34,41), new Point(39,41), new Point(44,41)));
this.Unistrokes[1] = new Unistroke("circle", new Array(new Point(127,141),new Point(124,140),new Point(120,139),new Point(118,139),new Point(116,139),new Point(111,140),new Point(109,141),new Point(104,144),new Point(100,147),new Point(96,152),new Point(93,157),new Point(90,163),new Point(87,169),new Point(85,175),new Point(83,181),new Point(82,190),new Point(82,195),new Point(83,200),new Point(84,205),new Point(88,213),new Point(91,216),new Point(96,219),new Point(103,222),new Point(108,224),new Point(111,224),new Point(120,224),new Point(133,223),new Point(142,222),new Point(152,218),new Point(160,214),new Point(167,210),new Point(173,204),new Point(178,198),new Point(179,196),new Point(182,188),new Point(182,177),new Point(178,167),new Point(170,150),new Point(163,138),new Point(152,130),new Point(143,129),new Point(140,131),new Point(129,136),new Point(126,139)));
this.Unistrokes[2] = new Unistroke("caret", new Array(new Point(79,245),new Point(79,242),new Point(79,239),new Point(80,237),new Point(80,234),new Point(81,232),new Point(82,230),new Point(84,224),new Point(86,220),new Point(86,218),new Point(87,216),new Point(88,213),new Point(90,207),new Point(91,202),new Point(92,200),new Point(93,194),new Point(94,192),new Point(96,189),new Point(97,186),new Point(100,179),new Point(102,173),new Point(105,165),new Point(107,160),new Point(109,158),new Point(112,151),new Point(115,144),new Point(117,139),new Point(119,136),new Point(119,134),new Point(120,132),new Point(121,129),new Point(122,127),new Point(124,125),new Point(126,124),new Point(129,125),new Point(131,127),new Point(132,130),new Point(136,139),new Point(141,154),new Point(145,166),new Point(151,182),new Point(156,193),new Point(157,196),new Point(161,209),new Point(162,211),new Point(167,223),new Point(169,229),new Point(170,231),new Point(173,237),new Point(176,242),new Point(177,244),new Point(179,250),new Point(181,255),new Point(182,257)));
this.Unistrokes[3] = new Unistroke("v", new Array(new Point(89,164),new Point(90,162),new Point(92,162),new Point(94,164),new Point(95,166),new Point(96,169),new Point(97,171),new Point(99,175),new Point(101,178),new Point(103,182),new Point(106,189),new Point(108,194),new Point(111,199),new Point(114,204),new Point(117,209),new Point(119,214),new Point(122,218),new Point(124,222),new Point(126,225),new Point(128,228),new Point(130,229),new Point(133,233),new Point(134,236),new Point(136,239),new Point(138,240),new Point(139,242),new Point(140,244),new Point(142,242),new Point(142,240),new Point(142,237),new Point(143,235),new Point(143,233),new Point(145,229),new Point(146,226),new Point(148,217),new Point(149,208),new Point(149,205),new Point(151,196),new Point(151,193),new Point(153,182),new Point(155,172),new Point(157,165),new Point(159,160),new Point(162,155),new Point(164,150),new Point(165,148),new Point(166,146)));
this.Unistrokes[4] = new Unistroke("space", new Array(new Point(4,27), new Point(21,27),new Point(28,27),new Point(35,27),new Point(39,27), new Point(41,27), new Point(44,28), new Point(44,18), new Point(44,8)));
this.Unistrokes[5] = new Unistroke("backspace", new Array(new Point(4,4), new Point(4,10), new Point(4,18), new Point(5,25), new Point(6,27), new Point(21,27),new Point(28,27),new Point(35,27),new Point(39,27), new Point(44,27)));
this.Unistrokes[6] = new Unistroke("zigzag", new Array(new Point(307,216),new Point(333,186),new Point(356,215),new Point(375,186),new Point(399,216),new Point(418,186)));
this.Unistrokes[7] = new Unistroke("smile", new Array(new Point(7,23), new Point(7,27), new Point(8,31), new Point(9,35), new Point(12,39), new Point(16,43), new Point(21,44), new Point(25,45), new Point(29,45), new Point(33,44), new Point(37,43), new Point(40,42), new Point(41,38), new Point(45,34), new Point(46,30), new Point(48,25)));
this.Unistrokes[8] = new Unistroke("frown", new Array(new Point(6,24), new Point(8,19), new Point(8,15), new Point(12,11), new Point(15,8), new Point(18,6), new Point(23,6), new Point(27,6), new Point(31,7), new Point(34,10), new Point(38,12), new Point(41,15), new Point(41,20), new Point(41,25), new Point(42,26)));
// this.Unistrokes[9] = new Unistroke("angry", new Array(new Point(50,130), new Point(250,130)));
//
// The $1 Gesture Recognizer API begins here -- 3 methods: Recognize(), AddGesture(), and DeleteUserGestures()
//
this.Recognize = function(points, useProtractor)
{
var t0 = Date.now();
var candidate = new Unistroke("", points);
var u = -1;
var b = +Infinity;
for (var i = 0; i < this.Unistrokes.length; i++) // for each unistroke template
{
var d;
if (useProtractor)
d = OptimalCosineDistance(this.Unistrokes[i].Vector, candidate.Vector); // Protractor
else
d = DistanceAtBestAngle(candidate.Points, this.Unistrokes[i], -AngleRange, +AngleRange, AnglePrecision); // Golden Section Search (original $1)
if (d < b) {
b = d; // best (least) distance
u = i; // unistroke index
}
}
var t1 = Date.now();
return (u == -1) ? new Result("No match.", 0.0, t1-t0) : new Result(this.Unistrokes[u].Name, useProtractor ? (1.0 - b) : (1.0 - b / HalfDiagonal), t1-t0);
}
this.AddGesture = function(name, points)
{
this.Unistrokes[this.Unistrokes.length] = new Unistroke(name, points); // append new unistroke
var num = 0;
for (var i = 0; i < this.Unistrokes.length; i++) {
if (this.Unistrokes[i].Name == name)
num++;
}
return num;
}
this.DeleteUserGestures = function()
{
this.Unistrokes.length = NumUnistrokes; // clear any beyond the original set
return NumUnistrokes;
}
}
//
// Private helper functions from here on down
//
function Resample(points, n)
{
var I = PathLength(points) / (n - 1); // interval length
var D = 0.0;
var newpoints = new Array(points[0]);
for (var i = 1; i < points.length; i++)
{
var d = Distance(points[i-1], points[i]);
if ((D + d) >= I)
{
var qx = points[i-1].X + ((I - D) / d) * (points[i].X - points[i-1].X);
var qy = points[i-1].Y + ((I - D) / d) * (points[i].Y - points[i-1].Y);
var q = new Point(qx, qy);
newpoints[newpoints.length] = q; // append new point 'q'
points.splice(i, 0, q); // insert 'q' at position i in points s.t. 'q' will be the next i
D = 0.0;
}
else D += d;
}
if (newpoints.length == n - 1) // somtimes we fall a rounding-error short of adding the last point, so add it if so
newpoints[newpoints.length] = new Point(points[points.length - 1].X, points[points.length - 1].Y);
return newpoints;
}
function IndicativeAngle(points)
{
var c = Centroid(points);
return Math.atan2(c.Y - points[0].Y, c.X - points[0].X);
}
function RotateBy(points, radians) // rotates points around centroid
{
var c = Centroid(points);
var cos = Math.cos(radians);
var sin = Math.sin(radians);
var newpoints = new Array();
for (var i = 0; i < points.length; i++) {
var qx = (points[i].X - c.X) * cos - (points[i].Y - c.Y) * sin + c.X
var qy = (points[i].X - c.X) * sin + (points[i].Y - c.Y) * cos + c.Y;
newpoints[newpoints.length] = new Point(qx, qy);
}
return newpoints;
}
function ScaleTo(points, size) // non-uniform scale; assumes 2D gestures (i.e., no lines)
{
var B = BoundingBox(points);
var newpoints = new Array();
for (var i = 0; i < points.length; i++) {
var qx = points[i].X * (size / B.Width);
var qy = points[i].Y * (size / B.Height);
newpoints[newpoints.length] = new Point(qx, qy);
}
return newpoints;
}
function TranslateTo(points, pt) // translates points' centroid
{
var c = Centroid(points);
var newpoints = new Array();
for (var i = 0; i < points.length; i++) {
var qx = points[i].X + pt.X - c.X;
var qy = points[i].Y + pt.Y - c.Y;
newpoints[newpoints.length] = new Point(qx, qy);
}
return newpoints;
}
function Vectorize(points) // for Protractor
{
var sum = 0.0;
var vector = new Array();
for (var i = 0; i < points.length; i++) {
vector[vector.length] = points[i].X;
vector[vector.length] = points[i].Y;
sum += points[i].X * points[i].X + points[i].Y * points[i].Y;
}
var magnitude = Math.sqrt(sum);
for (var i = 0; i < vector.length; i++)
vector[i] /= magnitude;
return vector;
}
function OptimalCosineDistance(v1, v2) // for Protractor
{
var a = 0.0;
var b = 0.0;
for (var i = 0; i < v1.length; i += 2) {
a += v1[i] * v2[i] + v1[i+1] * v2[i+1];
b += v1[i] * v2[i+1] - v1[i+1] * v2[i];
}
var angle = Math.atan(b / a);
return Math.acos(a * Math.cos(angle) + b * Math.sin(angle));
}
function DistanceAtBestAngle(points, T, a, b, threshold)
{
var x1 = Phi * a + (1.0 - Phi) * b;
var f1 = DistanceAtAngle(points, T, x1);
var x2 = (1.0 - Phi) * a + Phi * b;
var f2 = DistanceAtAngle(points, T, x2);
while (Math.abs(b - a) > threshold)
{
if (f1 < f2) {
b = x2;
x2 = x1;
f2 = f1;
x1 = Phi * a + (1.0 - Phi) * b;
f1 = DistanceAtAngle(points, T, x1);
} else {
a = x1;
x1 = x2;
f1 = f2;
x2 = (1.0 - Phi) * a + Phi * b;
f2 = DistanceAtAngle(points, T, x2);
}
}
return Math.min(f1, f2);
}
function DistanceAtAngle(points, T, radians)
{
var newpoints = RotateBy(points, radians);
return PathDistance(newpoints, T.Points);
}
function Centroid(points)
{
var x = 0.0, y = 0.0;
for (var i = 0; i < points.length; i++) {
x += points[i].X;
y += points[i].Y;
}
x /= points.length;
y /= points.length;
return new Point(x, y);
}
function BoundingBox(points)
{
var minX = +Infinity, maxX = -Infinity, minY = +Infinity, maxY = -Infinity;
for (var i = 0; i < points.length; i++) {
minX = Math.min(minX, points[i].X);
minY = Math.min(minY, points[i].Y);
maxX = Math.max(maxX, points[i].X);
maxY = Math.max(maxY, points[i].Y);
}
return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}
function PathDistance(pts1, pts2)
{
var d = 0.0;
for (var i = 0; i < pts1.length; i++) // assumes pts1.length == pts2.length
d += Distance(pts1[i], pts2[i]);
return d / pts1.length;
}
function PathLength(points)
{
var d = 0.0;
for (var i = 1; i < points.length; i++)
d += Distance(points[i - 1], points[i]);
return d;
}
function Distance(p1, p2)
{
var dx = p2.X - p1.X;
var dy = p2.Y - p1.Y;
return Math.sqrt(dx * dx + dy * dy);
}
function Deg2Rad(d) { return (d * Math.PI / 180.0); }