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
/
Copy pathGesture.cpp
281 lines (214 loc) · 8.69 KB
/
Gesture.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
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "Config.h"
#include "Gesture.h"
Gesture::Gesture() :
id(0) {
}
void Gesture::setId(unsigned int id) {
this->id = id;
}
unsigned int Gesture::getId() {
return id;
}
void Gesture::addAccelerationData(AccelerationData& accelerationData) {
sampler.addAccelerationData(accelerationData);
if (!sampler.isReady()) {
return;
}
addSampledDataToStore();
}
void Gesture::addSampledDataToStore(void) {
if (!(sampler.hasRemainingData() || sampler.isReady())) {
return;
}
AccelerationData& sample = sampler.getSample();
//printf("sampled, x:%6hd, y:%6hd, z:%6hd\n", sample.x, sample.y, sample.z);
#ifdef USE_DATA_QUANTIZER
AccelerationData quantizedData;
quantizedData.x = dataQuantizer.quantize(sample.x);
quantizedData.y = dataQuantizer.quantize(sample.y);
quantizedData.z = dataQuantizer.quantize(sample.z);
//printf("quantized, x:%6hd, y:%6hd, z:%6hd\n", quantizedData.x, quantizedData.y, quantizedData.z);
accelerationDataStore.add(quantizedData);
#else
sample.scale(100);
//printf("scaled, x:%6hd, y:%6hd, z:%6hd\n", sample.x, sample.y, sample.z);
accelerationDataStore.add(sample);
#endif
sampler.prepareForUpdate();
}
void Gesture::import(void) {
FILE *fp;
char filename[10];
AccelerationData accelerationData;
sprintf(filename, "%d.uwv", id);
printf("Importing %s ... ", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed, cannot open file\n");
return;
}
int warnings = 0;
while (!feof(fp)) {
int itemsRead = fscanf(fp, "%hd %hd %hd \n", &accelerationData.x, &accelerationData.y, &accelerationData.z);
//printf("itemsRead: %d, x: %d, y: %d, z: %d\n", itemsRead, accelerationData.x, accelerationData.y, accelerationData.z);
if (itemsRead != 3) {
warnings++;
break;
}
addAccelerationData(accelerationData);
}
addSampledDataToStore();
fclose(fp);
if (warnings) {
printf("Warning, incorrectly formatted lines detected: %d\n", warnings);
return;
}
printf("OK\n");
}
void Gesture::load(void) {
char filename[10];
sprintf(filename, "%d.ges", id);
load(filename);
}
void Gesture::load(const char *filename) {
FILE *fp;
AccelerationData accelerationData;
printf("Loading %s ... ", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed, cannot open file\n");
return;
}
int warnings = 0;
while (!feof(fp)) {
int itemsRead = fscanf(fp, "%hd,%hd,%hd\n", &accelerationData.x, &accelerationData.y, &accelerationData.z);
//printf("itemsRead: %d, x: %d, y: %d, z: %d\n", itemsRead, accelerationData.x, accelerationData.y, accelerationData.z);
if (itemsRead != 3) {
warnings++;
break;
}
accelerationDataStore.add(accelerationData);
}
fclose(fp);
if (warnings) {
printf("Warning, incorrectly formatted lines detected: %d\n", warnings);
return;
}
printf("OK\n");
}
void Gesture::save(void) {
FILE *fp;
char filename[10];
sprintf(filename, "record.ges");
printf("Saving %s ... ", filename);
fp = fopen(filename, "w");
if (fp == NULL) {
printf("Failed, cannot write file\n");
return;
}
AccelerationData *accelerationData = accelerationDataStore.getAccelerationData();
for (unsigned int index = 0; index < accelerationDataStore.getTotalAccelerationDataItems(); index++) {
fprintf(fp, "%hd,%hd,%hd\n", accelerationData[index].x, accelerationData[index].y, accelerationData[index].z);
}
fclose(fp);
printf("OK\n");
}
bool Gesture::isValid(void) {
return accelerationDataStore.getTotalAccelerationDataItems() > 0;
}
long Gesture::calculateDistanceBetween(Gesture& other) {
if (!(isValid() && other.isValid())) {
return INT_MAX;
}
AccelerationDataStore& otherAccelerationDataStore = other.getAccelerationDataStore();
AccelerationData *otherAccelerationData = otherAccelerationDataStore.getAccelerationData();
unsigned int otherTotalAccelerationDataItems = otherAccelerationDataStore.getTotalAccelerationDataItems();
long* table = buildTable(otherTotalAccelerationDataItems);
if (!table) {
return INT_MAX;
}
unsigned int totalAccelerationDataItems = accelerationDataStore.getTotalAccelerationDataItems();
long distance = calculateDTWDistance(otherAccelerationData, otherTotalAccelerationDataItems, totalAccelerationDataItems - 1,
otherTotalAccelerationDataItems - 1, table);
distance /= (totalAccelerationDataItems + otherTotalAccelerationDataItems);
//printf("otherTotalAccelerationDataItems: %d, totalAccelerationDataItems: %d\n", otherTotalAccelerationDataItems, totalAccelerationDataItems);
free(table);
return distance;
}
long *Gesture::buildTable(unsigned int itemsInOtherGestureToCompare) {
unsigned int totalAccelerationDataItems = accelerationDataStore.getTotalAccelerationDataItems();
unsigned int tableSize = totalAccelerationDataItems * itemsInOtherGestureToCompare;
unsigned int allocSize = tableSize * sizeof(long);
//printf("tableSize: %d, allocSize: %d\n", tableSize, allocSize);
long* table = (long*) malloc(allocSize);
if (!table) {
return NULL;
}
for (unsigned int index = 0; index < tableSize; index++) {
table[index] = -1L;
}
return table;
}
AccelerationDataStore& Gesture::getAccelerationDataStore(void) {
return accelerationDataStore;
}
/**
* Due to the size of the numbers generated by this the acceleration data values should be small, x/y/z values of ~32768, cause infinite loops.
*/
long Gesture::calculateDTWDistance(AccelerationData *otherAccelerationData, unsigned int otherTotalAccelerationDataItems, int compareIndex,
int otherCompareIndex, long *table) {
//printf("compareIndex: %d, otherCompareIndex: %d\n", compareIndex, otherCompareIndex);
AccelerationData *accelerationData = accelerationDataStore.getAccelerationData();
if (compareIndex < 0 || otherCompareIndex < 0) {
return INT_MAX;
}
int tableWidth = otherTotalAccelerationDataItems;
long localDistance = 0;
for (unsigned int axis = 0; axis < 3; axis++) {
int16_t a = accelerationData[compareIndex].getAxisValue((Axis) axis);
int16_t b = otherAccelerationData[otherCompareIndex].getAxisValue((Axis) axis);
localDistance += ((a - b) * (a - b));
}
//printf("localDistance: %ld\n", localDistance);
long sdistance = 0;
long s1, s2, s3;
if (compareIndex == 0 && otherCompareIndex == 0) {
if (table[compareIndex * tableWidth + otherCompareIndex] < 0) {
table[compareIndex * tableWidth + otherCompareIndex] = localDistance;
}
//printf("distance (a): %ld\n", table[compareIndex * tableWidth + otherCompareIndex]);
return localDistance;
}
if (compareIndex == 0) {
if (table[compareIndex * tableWidth + (otherCompareIndex - 1)] < 0)
sdistance = calculateDTWDistance(otherAccelerationData, otherTotalAccelerationDataItems, compareIndex, otherCompareIndex - 1, table);
else
sdistance = table[compareIndex * tableWidth + otherCompareIndex - 1];
} else if (otherCompareIndex == 0) {
if (table[(compareIndex - 1) * tableWidth + otherCompareIndex] < 0)
sdistance = calculateDTWDistance(otherAccelerationData, otherTotalAccelerationDataItems, compareIndex - 1, otherCompareIndex, table);
else
sdistance = table[(compareIndex - 1) * tableWidth + otherCompareIndex];
} else {
if (table[compareIndex * tableWidth + (otherCompareIndex - 1)] < 0)
s1 = calculateDTWDistance(otherAccelerationData, otherTotalAccelerationDataItems, compareIndex, otherCompareIndex - 1, table);
else
s1 = table[compareIndex * tableWidth + (otherCompareIndex - 1)];
if (table[(compareIndex - 1) * tableWidth + otherCompareIndex] < 0)
s2 = calculateDTWDistance(otherAccelerationData, otherTotalAccelerationDataItems, compareIndex - 1, otherCompareIndex, table);
else
s2 = table[(compareIndex - 1) * tableWidth + otherCompareIndex];
if (table[(compareIndex - 1) * tableWidth + otherCompareIndex - 1] < 0)
s3 = calculateDTWDistance(otherAccelerationData, otherTotalAccelerationDataItems, compareIndex - 1, otherCompareIndex - 1, table);
else
s3 = table[(compareIndex - 1) * tableWidth + otherCompareIndex - 1];
sdistance = s1 < s2 ? s1 : s2;
sdistance = sdistance < s3 ? sdistance : s3;
}
table[compareIndex * tableWidth + otherCompareIndex] = localDistance + sdistance;
//printf("distance (b): %ld\n", table[compareIndex * tableWidth + otherCompareIndex]);
return table[compareIndex * tableWidth + otherCompareIndex];
}