174
|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Text; |
|
5 |
using System.Windows.Input; |
|
6 |
using System.Windows; |
|
7 |
|
|
8 |
namespace GestureControl |
|
9 |
{ |
|
10 |
/// <summary> |
|
11 |
/// Take a list of points and try to recognize a pattern |
|
12 |
/// </summary> |
|
13 |
public class SurfaceGesture : List<SurfaceGestureVector> |
|
14 |
{ |
|
15 |
|
|
16 |
#region Prop |
|
17 |
/// <summary> |
|
18 |
/// Allow some variation without log a direction |
|
19 |
/// </summary> |
|
20 |
public int Precision { get; set; } |
|
21 |
#endregion |
176
|
22 |
|
174
|
23 |
#region Constructor |
|
24 |
/// <summary> |
|
25 |
/// load know patterns and generate the vector list from a list of points with a default 20 precision factor |
|
26 |
/// </summary> |
|
27 |
/// <param name="list"></param> |
|
28 |
public SurfaceGesture(List<SurfaceGesturePoint> list) |
|
29 |
{ |
|
30 |
this.Precision = 20; |
|
31 |
this.Generate(list); |
|
32 |
} |
|
33 |
/// <summary> |
|
34 |
/// load know patterns and generate the vector list from a list of points with a precision factor |
|
35 |
/// </summary> |
|
36 |
/// <param name="list"></param> |
|
37 |
/// <param name="precision"></param> |
|
38 |
public SurfaceGesture(List<SurfaceGesturePoint> list, int precision) |
|
39 |
{ |
|
40 |
this.Precision = precision; |
|
41 |
this.Generate(list); |
|
42 |
} |
|
43 |
#endregion |
176
|
44 |
|
174
|
45 |
#region GenerateVector |
|
46 |
/// <summary> |
|
47 |
/// Generate list of vector from courbe mouvements, filter with the pas value |
|
48 |
/// </summary> |
|
49 |
/// <param name="list"></param> |
|
50 |
/// <param name="pas"></param> |
|
51 |
/// <returns></returns> |
|
52 |
private bool GenerateCourb(List<SurfaceGesturePoint> list, int pas) |
|
53 |
{ |
176
|
54 |
//List<SurfaceGesturePattern> tmp = new List<SurfaceGesturePattern>(); |
174
|
55 |
int sep = list.Count / pas; |
|
56 |
double count = 0; ; |
|
57 |
SurfaceGesturePoint past = new SurfaceGesturePoint() { X = 0, Y = 0 }; |
|
58 |
double y = 0; |
|
59 |
|
|
60 |
for (int i = 0; i < list.Count - 1; i++) |
|
61 |
{ |
|
62 |
if (i % pas != 0) |
|
63 |
{ |
|
64 |
count += Math.Atan(list[i + 1].Y / list[i + 1].X) - Math.Atan(list[i].Y / list[i].X); |
|
65 |
} |
|
66 |
else |
|
67 |
{ |
|
68 |
count /= pas; |
|
69 |
if (count == 0 || this.GetDistancePoints(past, list[i + 1]) < 5) |
|
70 |
{ |
|
71 |
y = list[i + 1].Y; |
|
72 |
past.X = count; |
|
73 |
continue; |
|
74 |
} |
|
75 |
if (y > list[i + 1].Y) |
|
76 |
{ |
|
77 |
if (past.X > count) |
|
78 |
this.AddDirection(SurfaceGestureVectorDirection.UPRIGHT); |
|
79 |
else |
|
80 |
this.AddDirection(SurfaceGestureVectorDirection.UPLEFT); |
|
81 |
} |
|
82 |
else |
|
83 |
{ |
|
84 |
if (past.X > count) |
|
85 |
this.AddDirection(SurfaceGestureVectorDirection.DOWNRIGHT); |
|
86 |
else |
|
87 |
this.AddDirection(SurfaceGestureVectorDirection.DOWNLEFT); |
|
88 |
} |
|
89 |
y = list[i + 1].Y; |
|
90 |
past.X = count; |
|
91 |
} |
|
92 |
} |
|
93 |
Console.Write(this); |
176
|
94 |
/*if (this.GetPattern() != "None") |
174
|
95 |
return true; |
176
|
96 |
else*/ |
174
|
97 |
return false; |
|
98 |
} |
|
99 |
/// <summary> |
|
100 |
/// Get distance between two points |
|
101 |
/// </summary> |
|
102 |
/// <param name="p1"></param> |
|
103 |
/// <param name="p2"></param> |
|
104 |
/// <returns></returns> |
|
105 |
private int GetDistancePoints(SurfaceGesturePoint p1, SurfaceGesturePoint p2) |
|
106 |
{ |
|
107 |
return (int)Math.Sqrt(Math.Pow((p2.X - p1.X), 2) + Math.Pow((p1.Y - p2.Y), 2)); |
|
108 |
} |
|
109 |
/// <summary> |
|
110 |
/// add a direction in the vector list if past who not the same |
|
111 |
/// </summary> |
|
112 |
/// <param name="type"></param> |
|
113 |
private void AddDirection(SurfaceGestureVectorDirection type) |
|
114 |
{ |
|
115 |
if (this.Count == 0) |
|
116 |
{ |
176
|
117 |
this.Add(new SurfaceGestureVector { Direction = type, Lenght = 42 }); |
|
118 |
return; |
174
|
119 |
} |
|
120 |
if (this[this.Count - 1].Direction != type) |
|
121 |
this.Add(new SurfaceGestureVector { Direction = type, Lenght = 42 }); |
|
122 |
} |
|
123 |
/// <summary> |
|
124 |
/// generate list of vector |
|
125 |
/// </summary> |
|
126 |
/// <param name="list"></param> |
|
127 |
private void Generate(List<SurfaceGesturePoint> list) |
|
128 |
{ |
|
129 |
if (list.Count < 2) |
176
|
130 |
return; |
174
|
131 |
SurfaceGestureVectorDirection lastDirection = SurfaceGestureVectorDirection.NONE; |
|
132 |
int lastPoint = 0; |
|
133 |
SurfaceGesturePoint LastChange = list[0]; |
|
134 |
|
|
135 |
/////// TEST/////////// |
|
136 |
if (this.GenerateCourb(list, 5) == true) |
|
137 |
return; |
|
138 |
this.Clear(); |
|
139 |
/////////////////////// |
|
140 |
|
|
141 |
for (int i = 0; i < list.Count - 1; i++) |
|
142 |
{ |
|
143 |
if (GetHorizontal(list[lastPoint], list[i + 1]) == SurfaceGestureVectorDirection.UP && lastDirection != SurfaceGestureVectorDirection.UP) |
|
144 |
{ |
|
145 |
this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.UP, Lenght = (Math.Abs(LastChange.Y - list[i + 1].Y)) }); |
|
146 |
LastChange = list[i + 1]; |
|
147 |
lastDirection = SurfaceGestureVectorDirection.UP; |
|
148 |
} |
|
149 |
else if (GetHorizontal(list[lastPoint], list[i + 1]) == SurfaceGestureVectorDirection.DOWN && lastDirection != SurfaceGestureVectorDirection.DOWN) |
|
150 |
{ |
|
151 |
this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.DOWN, Lenght = (Math.Abs(LastChange.Y - list[i + 1].Y)) }); |
|
152 |
LastChange = list[i + 1]; |
|
153 |
lastDirection = SurfaceGestureVectorDirection.DOWN; |
|
154 |
} |
|
155 |
else if (GetVertical(list[lastPoint], list[i + 1]) == SurfaceGestureVectorDirection.LEFT && lastDirection != SurfaceGestureVectorDirection.LEFT) |
|
156 |
{ |
|
157 |
this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.LEFT, Lenght = (Math.Abs(LastChange.X - list[i + 1].X)) }); |
|
158 |
LastChange = list[i + 1]; |
|
159 |
lastDirection = SurfaceGestureVectorDirection.LEFT; |
|
160 |
} |
|
161 |
else if (GetVertical(list[lastPoint], list[i + 1]) == SurfaceGestureVectorDirection.RIGHT && lastDirection != SurfaceGestureVectorDirection.RIGHT) |
|
162 |
{ |
|
163 |
this.Add(new SurfaceGestureVector { Direction = SurfaceGestureVectorDirection.RIGHT, Lenght = (Math.Abs(LastChange.X - list[i + 1].X)) }); |
|
164 |
LastChange = list[i + 1]; |
|
165 |
lastDirection = SurfaceGestureVectorDirection.RIGHT; |
|
166 |
} |
|
167 |
++lastPoint; |
|
168 |
} |
|
169 |
} |
|
170 |
private SurfaceGestureVectorDirection GetHorizontal(SurfaceGesturePoint p1, SurfaceGesturePoint p2) |
|
171 |
{ |
|
172 |
if (p1.Y < p2.Y) |
|
173 |
{ |
|
174 |
// go up |
|
175 |
if (Math.Abs(p2.Y - p1.Y) > this.Precision && Math.Abs(p2.X - p1.X) < Math.Abs(p2.Y - p1.Y)) |
|
176 |
return SurfaceGestureVectorDirection.DOWN; |
|
177 |
} |
|
178 |
else |
|
179 |
{ |
|
180 |
// go down |
|
181 |
if (Math.Abs(p1.Y - p2.Y) > this.Precision && Math.Abs(p1.X - p2.X) < Math.Abs(p1.Y - p2.Y)) |
|
182 |
return SurfaceGestureVectorDirection.UP; |
|
183 |
} |
|
184 |
return SurfaceGestureVectorDirection.NONE; |
|
185 |
} |
|
186 |
private SurfaceGestureVectorDirection GetVertical(SurfaceGesturePoint p1, SurfaceGesturePoint p2) |
|
187 |
{ |
|
188 |
if (p1.X < p2.X) |
|
189 |
{ |
|
190 |
// go left |
|
191 |
if (Math.Abs(p2.X - p1.X) > this.Precision && Math.Abs(p2.Y - p1.Y) < Math.Abs(p2.X - p1.X)) |
|
192 |
return SurfaceGestureVectorDirection.RIGHT; |
|
193 |
} |
|
194 |
else |
|
195 |
{ |
|
196 |
// go right |
|
197 |
if (Math.Abs(p1.X - p2.X) > this.Precision && Math.Abs(p1.Y - p2.Y) < Math.Abs(p1.X - p2.X)) |
|
198 |
return SurfaceGestureVectorDirection.LEFT; |
|
199 |
} |
|
200 |
return SurfaceGestureVectorDirection.NONE; |
|
201 |
} |
|
202 |
#endregion |
176
|
203 |
|
174
|
204 |
#region Override |
|
205 |
public override String ToString() |
|
206 |
{ |
|
207 |
String ret = ""; |
|
208 |
|
|
209 |
foreach (SurfaceGestureVector v in this) |
|
210 |
{ |
|
211 |
ret += (v.Direction + ", lenght:" + v.Lenght + ", precision:" + this.Precision + "\n"); |
|
212 |
//Console.WriteLine(v.Direction + ", lenght:" + v.Lenght + ", precision:" + this.Precision); |
|
213 |
} |
|
214 |
return ret; |
|
215 |
} |
|
216 |
#endregion |
|
217 |
} |
|
218 |
} |