1 | #include "training.h" |
---|
2 | |
---|
3 | /** |
---|
4 | * Dataset destructor, which calls clear. |
---|
5 | */ |
---|
6 | Training::~Training() |
---|
7 | { |
---|
8 | clear(); |
---|
9 | } |
---|
10 | |
---|
11 | /** |
---|
12 | * Load a training stored in a file. |
---|
13 | * |
---|
14 | * @param training File stream of the contained training |
---|
15 | */ |
---|
16 | bool Training::loadTraining(ifstream& training) |
---|
17 | { |
---|
18 | string sample, cmd; |
---|
19 | |
---|
20 | // Get a sample |
---|
21 | getline(training, sample); |
---|
22 | |
---|
23 | // Parse every sample to get the log type |
---|
24 | istringstream sStr(sample); |
---|
25 | sStr >> cmd; |
---|
26 | |
---|
27 | while(!training.eof() && cmd != "END" && cmd != "") { |
---|
28 | if(cmd == "ACC") |
---|
29 | addSample(new AccSample(sStr.str())); |
---|
30 | else if(cmd == "GYRO") |
---|
31 | addSample(new GyroSample(sStr.str())); |
---|
32 | else { |
---|
33 | cout << "[Error] Bad log type." << endl; |
---|
34 | return false; |
---|
35 | } |
---|
36 | |
---|
37 | getline(training, sample); |
---|
38 | sStr.str(sample); |
---|
39 | sStr >> cmd; |
---|
40 | } |
---|
41 | |
---|
42 | return true; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Save the training into a file for training and recognition. |
---|
47 | * |
---|
48 | * @param out Stream of the destination file |
---|
49 | */ |
---|
50 | void Training::save(ofstream& out) const |
---|
51 | { |
---|
52 | // Training Header |
---|
53 | out << "START " << timestamp << endl; |
---|
54 | |
---|
55 | // Samples |
---|
56 | for(unsigned int i = 0 ; i < samples.size() ; i++) |
---|
57 | samples[i]->save(out); |
---|
58 | |
---|
59 | out << "END" << endl; |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Add a new training to the dataset. |
---|
64 | * |
---|
65 | * @param sample Add a sample to the training set. |
---|
66 | */ |
---|
67 | void Training::addSample(Sample* sample) |
---|
68 | { |
---|
69 | // We retrieve the overall gesture timestamp |
---|
70 | struct timeval t; |
---|
71 | gettimeofday(&t,0); |
---|
72 | unsigned long sampleTs = (t.tv_sec % 86400) * 1000 + t.tv_usec / 1000; |
---|
73 | |
---|
74 | // We compute the relative timestamp in msec |
---|
75 | unsigned long deltaT = sampleTs - timestamp; |
---|
76 | |
---|
77 | if(sample) { |
---|
78 | sample->setTimestampFromGestureStart(deltaT); |
---|
79 | samples.push_back(sample); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Delete all samples and clear the buffer. This method will take |
---|
85 | * care of freeing the memory of each sample in the training set, |
---|
86 | * hence you don't need to free them in your code. |
---|
87 | */ |
---|
88 | void Training::clear() |
---|
89 | { |
---|
90 | for(unsigned int i = 0 ; i < samples.size() ; i++) { |
---|
91 | if(samples[i]) { |
---|
92 | delete samples[i]; |
---|
93 | samples[i] = 0; |
---|
94 | } |
---|
95 | } |
---|
96 | samples.clear(); |
---|
97 | } |
---|