This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
GestureRecognizer.cpp
76 lines (56 loc) · 1.99 KB
/
GestureRecognizer.cpp
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
#include <stdio.h>
#include <limits.h>
#include "Config.h"
#include "GestureRecognizer.h"
GestureRecognizer::GestureRecognizer() {
for (int index = 0; index < MAX_GESTURES; index++) {
Gesture& gesture = gestures[index];
gesture.setId(index);
}
}
void GestureRecognizer::load(const char *replayFilename) {
possibleGesture.load(replayFilename);
}
void GestureRecognizer::loadGestures(void) {
for (int index = 0; index < MAX_GESTURES; index++) {
Gesture& gesture = gestures[index];
gesture.load();
}
}
void GestureRecognizer::addAccelerationData(AccelerationData& accelerationData) {
//printf("raw, x:%6hd, y:%6hd, z: %6hd\n", accelerationData.x, accelerationData.y, accelerationData.z);
possibleGesture.addAccelerationData(accelerationData);
}
Gesture *GestureRecognizer::recognize(void) {
possibleGesture.save();
long shortestDistance = INT_MAX;
long longestDistance = 0;
Gesture *bestMatch = NULL;
long totalDistance = 0;
unsigned int gesturesChecked = 0;
for (unsigned int index = 0; index < MAX_GESTURES; index++) {
Gesture& gesture = gestures[index];
if (!gesture.isValid()) {
continue;
}
long distance = possibleGesture.calculateDistanceBetween(gesture);
if (distance < shortestDistance) {
shortestDistance = distance;
bestMatch = &gesture;
}
if (distance > longestDistance) {
longestDistance = distance;
}
totalDistance += distance;
gesturesChecked++;
printf("index: %u, distance: %ld\n", index, distance);
}
long averageDistance = totalDistance / gesturesChecked;
long variance = longestDistance - shortestDistance;
printf("averageDistance: %ld, shortestDistance: %ld, longestDistance: %ld, variance: %ld\n", averageDistance, shortestDistance, longestDistance, variance);
if (shortestDistance > averageDistance / 2) {
printf("almost matched %d\n", bestMatch->getId());
return NULL;
}
return bestMatch;
}