diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-04-28 16:01:13 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-04-28 18:43:08 +0200 |
| commit | 30b6637fe84813ad7119e92dd49df7e12692c694 (patch) | |
| tree | 324ad07ddf43596cd6ef65e321d7ea297459a9b3 /docs | |
| parent | 5423ccd321c1c84b0e7736b10b2fe9eeb1833e16 (diff) | |
Document the gesture frame logic
Add some notes on the math of the gesture frame computations.
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/gestures.txt | 58 | ||||
| -rw-r--r-- | docs/pivot.txt | 146 |
2 files changed, 204 insertions, 0 deletions
diff --git a/docs/gestures.txt b/docs/gestures.txt new file mode 100644 index 0000000..5a1eb45 --- /dev/null +++ b/docs/gestures.txt | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | Introduction | ||
| 2 | ------------ | ||
| 3 | |||
| 4 | This document describes how the gestures are extracted from multi-finger | ||
| 5 | actions. The process is divided into gestural transformations, gesture | ||
| 6 | recognition, and gesture instantiation. | ||
| 7 | |||
| 8 | Gestural Transformations | ||
| 9 | ------------------------ | ||
| 10 | |||
| 11 | All two-finger transformations are extracted. These are all exact, in the | ||
| 12 | sense that continuously transforming the original finger positions, frame | ||
| 13 | per frame, will exactly follow the actual finger positions. | ||
| 14 | |||
| 15 | In addition to two-finger transformations, a global gesture is also | ||
| 16 | extracted. The rotation and scaling is taken from the contact pair with the | ||
| 17 | longest distance between contacts. This approximates the behavior of a | ||
| 18 | region, such that complex transformations could, in principle, be happening | ||
| 19 | inside the region, but at a distance, the transformation will look like it | ||
| 20 | was performed with two fingers. | ||
| 21 | |||
| 22 | At each time step, a gestural transformation is goverened by rotation, | ||
| 23 | scaling and translation. The point around which rotation and scaling is | ||
| 24 | performed is called the pivot. To form as natural gestures as possible, | ||
| 25 | this point always lies within the area formed by the contacts themselves. | ||
| 26 | It is placed at the point which, after rotation and scaling, leaves the | ||
| 27 | transformed contacts as close to the actual positions as possible. The | ||
| 28 | translation, or drag, is always a fraction of the movement of the center | ||
| 29 | point. The fraction is called moveness, and is related to the distance | ||
| 30 | between the pivot and the center point. When the pivot is at the center | ||
| 31 | point, the moveness is one and the drag is the same as the center movement. | ||
| 32 | When the pivot is at one of the contacts, as in rotation around a finger, | ||
| 33 | the moveness is zero, and consequently the drag is zero. The relations | ||
| 34 | between the pivot, the moveness and the drag are detailed in the document | ||
| 35 | pivot.txt. | ||
| 36 | |||
| 37 | Gesture Recognition | ||
| 38 | ------------------- | ||
| 39 | |||
| 40 | Each gestural transformation can give rise to one or several gesture | ||
| 41 | primitives. Based on the rotation, scaling, moveness and drag values, the | ||
| 42 | gesture primitives drag, pinch and rotate are recognized. The onset of a | ||
| 43 | gesture primitive is governed by a threshold and a timeout. Basically, if | ||
| 44 | performed distinctly enough, the gesture will be triggered, and will remain | ||
| 45 | active until a finger is lifted or added. | ||
| 46 | |||
| 47 | Gesture Instantiation | ||
| 48 | --------------------- | ||
| 49 | |||
| 50 | Given a set of detected gesture primitives, only some will trigger actual | ||
| 51 | gesture events. First, the set of available primitives are matched against | ||
| 52 | available listeners. Primitives not listened for are dropped. The remaining | ||
| 53 | set is arranged according to priority. A pointer gesture has lower priority | ||
| 54 | than a two-finger gesture, which has lower priority than a tap, for | ||
| 55 | instance. As long as the gesture primitives of higher priority are expected | ||
| 56 | but not activated, all gestures are held back. Once one of the highest | ||
| 57 | priority primitives are activated, all primitives of lower priority are | ||
| 58 | cancelled, and events are emitted. | ||
diff --git a/docs/pivot.txt b/docs/pivot.txt new file mode 100644 index 0000000..b945dd6 --- /dev/null +++ b/docs/pivot.txt | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | The pivot, p, is defined as the point, within the convex hull of the | ||
| 2 | contacts, which, after rotation and scaling, leaves the transformed | ||
| 3 | contacts as close to the actual positions as possible. | ||
| 4 | |||
| 5 | Let r_i be the starting points and s_i the actual ending points in a | ||
| 6 | transformation. Let D be the scaling, and R the rotation. Then, minimizing | ||
| 7 | |||
| 8 | L(p) = sum_i |D R (r_i - p) + p - s_i|^2 / N | ||
| 9 | |||
| 10 | yields the pivot. Let | ||
| 11 | |||
| 12 | rm = sum_i r_i / N, | ||
| 13 | p = rm + u, | ||
| 14 | q_i = s_i - rm - D R (r_i - rm), | ||
| 15 | |||
| 16 | and we get | ||
| 17 | |||
| 18 | L(p) = sum_i |(1 - D R) u - q_i|^2 / N. | ||
| 19 | |||
| 20 | With | ||
| 21 | |||
| 22 | L0 = sum_i norm2(q_i) / N, | ||
| 23 | T = (1 - D R)' (1 - D R), | ||
| 24 | m = sum_i q_i / N, | ||
| 25 | |||
| 26 | we can write this as | ||
| 27 | |||
| 28 | L(p) = L0 + u' T u - 2 m' (1 - D R) u. | ||
| 29 | |||
| 30 | To handle the constraint, we can approximate the hull with a circle | ||
| 31 | centered at rm. If we pick the average radius, P, the constraint becomes | ||
| 32 | |||
| 33 | |u| < P. | ||
| 34 | |||
| 35 | Relaxing [1] the expression (h >= 0) yields | ||
| 36 | |||
| 37 | L(p, h) = L0 + u' T u - 2 m' (1 - D R) u + h (|u|^2 - P^2), | ||
| 38 | |||
| 39 | leading to the linear equation | ||
| 40 | |||
| 41 | (T + h) u = (1 - D R)' m. | ||
| 42 | |||
| 43 | Further, | ||
| 44 | |||
| 45 | sm = sum_i s_i / N, | ||
| 46 | m = sum_i (s_i - rm - D R (r_i - rm)) / N = sm - rm, | ||
| 47 | |||
| 48 | thus m is the average displacement. In words, the pivot is the average | ||
| 49 | position plus a correction depending on the average displacement. | ||
| 50 | |||
| 51 | * | ||
| 52 | |||
| 53 | Some algebra solves the equation, | ||
| 54 | |||
| 55 | D' = D, | ||
| 56 | [D, R] = 0, | ||
| 57 | R = S + C, | ||
| 58 | S' = -S, | ||
| 59 | C' = C, | ||
| 60 | R + R' = 2 C, | ||
| 61 | T = (1 - D R)' (1 - D R) = 1 + D^2 - 2 D C, | ||
| 62 | |||
| 63 | which is a simple diagonal scaling operator. With | ||
| 64 | |||
| 65 | a = 1 - D C, | ||
| 66 | b = D S, | ||
| 67 | |||
| 68 | we can write this as | ||
| 69 | |||
| 70 | T = (1 - DC)^2 + D^2(1 - C^2) = (1 - DC)^2 + D^2 S^2 = a^2 + b^2. | ||
| 71 | |||
| 72 | Similarly, we can write | ||
| 73 | |||
| 74 | (1 - D R)' = ((a, b), (-b, a)), | ||
| 75 | |||
| 76 | and thusly, | ||
| 77 | |||
| 78 | u = Q(h) m, | ||
| 79 | |||
| 80 | with | ||
| 81 | |||
| 82 | Q(h) = ((a, b), (-b, a)) / (a^2 + b^2 + h). | ||
| 83 | |||
| 84 | When D R = 1, it follows that a^2 + b^2 = 0, and the relaxation ensures | ||
| 85 | that u is finite. | ||
| 86 | |||
| 87 | * | ||
| 88 | |||
| 89 | The drag is found by minimizing | ||
| 90 | |||
| 91 | E(d) = sum_i | D R (r_i - p) + p + d - s_i |^2 / N, | ||
| 92 | E(d) = d^2 + 2 d' ((1 - D R) u - m) + E0, | ||
| 93 | |||
| 94 | which leads to the linear equation | ||
| 95 | |||
| 96 | d = m - (1 - D R) u. | ||
| 97 | |||
| 98 | Explicitly, | ||
| 99 | |||
| 100 | d = m - (a ux - b uy, a uy + b ux). | ||
| 101 | |||
| 102 | Inserting the expression for u yields, after some algebra, | ||
| 103 | |||
| 104 | d = m (1 - (a^2 + b^2) / (a^2 + b^2 + h)). | ||
| 105 | |||
| 106 | When h = 0, d = 0, as expected. | ||
| 107 | |||
| 108 | When a^2 + b^2 = 0, d = m, also as expected. | ||
| 109 | |||
| 110 | For constrained cases, the drag is a fraction of the average displacement. | ||
| 111 | |||
| 112 | * | ||
| 113 | |||
| 114 | Time to look at measures for the relaxation parameter. Since d depends on | ||
| 115 | h, we can write the correction u(h) in terms of d instead. After som | ||
| 116 | algebra, | ||
| 117 | |||
| 118 | |u(h)| = (|m| - |d|) / sqrt(a^2 + b^2). | ||
| 119 | |||
| 120 | Conversely, d(h) can be written in terms of the constrained u(h) as | ||
| 121 | |||
| 122 | d(h) = m (1 - sqrt(a^2 + b^2) |u(h)| / |m|). | ||
| 123 | |||
| 124 | Since |u(0)| = |m| / sqrt(a^2 + b^2), we obtain | ||
| 125 | |||
| 126 | d(h) = m (1 - |u(h)| / |u(0)|). | ||
| 127 | |||
| 128 | * | ||
| 129 | |||
| 130 | We can now write down an explicit recipe for determining the pivot (p) and | ||
| 131 | drag (d), given the transformation parameters a and b. | ||
| 132 | |||
| 133 | w = (a mx + b my, a my - b mx). | ||
| 134 | |||
| 135 | If |w| = 0, then u = 0. Consequently p = rm, d = m, and we are done. Else, | ||
| 136 | |||
| 137 | u = w |m|^2 / |w|^2, | ||
| 138 | |||
| 139 | t = P / |u|. | ||
| 140 | |||
| 141 | If t >= 1, then p = rm + u, d = 0, and we are done. Else, | ||
| 142 | |||
| 143 | p = rm + t u, | ||
| 144 | d = (1 - t) m. | ||
| 145 | |||
| 146 | [1] See Lagrange relaxation | ||
