GestureRecognitionToolkit  Version: 0.1.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
RangeTracker.cpp
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
6 and associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or substantial
12 portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
18 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 */
20 
21 #include "RangeTracker.h"
22 
23 GRT_BEGIN_NAMESPACE
24 
26  trackData = true;
27  numDimensions = 0;
28  totalNumSamplesViewed = 0;
29 }
30 
31 RangeTracker::RangeTracker(UINT numDimensions){
32  trackData = true;
33  setNumDimensions( numDimensions );
34 }
35 
37  this->trackData = rhs.trackData;
38  this->numDimensions = rhs.numDimensions;
39  this->totalNumSamplesViewed = rhs.totalNumSamplesViewed;
40  this->ranges = rhs.ranges;
41 }
42 
44 
46  totalNumSamplesViewed = 0;
47  ranges.clear();
48  if( numDimensions > 0 )
49  ranges.resize(numDimensions,MinMax(BIG_POSITIVE_VALUE,BIG_NEGATIVE_VALUE));
50 }
51 
52 bool RangeTracker::setNumDimensions(UINT numDimensions){
53 
54  if( numDimensions > 0 ){
55  //Set the dimensionality of the data
56  this->numDimensions = numDimensions;
57 
58  //Reset the ranges values
59  clear();
60 
61  return true;
62  }
63  return false;
64 }
65 
67  if( sample.size() != numDimensions ) return false;
68 
69  if( !trackData ) return true;
70 
71  totalNumSamplesViewed++;
72  for(UINT j=0; j<numDimensions; j++){
73  if( sample[j] < ranges[j].minValue ){ ranges[j].minValue = sample[j]; }
74  else if( sample[j] > ranges[j].maxValue ){ ranges[j].maxValue = sample[j]; }
75  }
76 
77  return true;
78 }
79 
80 bool RangeTracker::saveRangeDataToFile(std::string filename){
81 
82  std::fstream file;
83  file.open(filename.c_str(), std::ios::out);
84 
85  if( !file.is_open() ){
86  return false;
87  }
88 
89  file << "GRT_RANGE_TRACKER_DATA_FILE_V1.0\n";
90  file << "NumDimensions: " << numDimensions << std::endl;
91  file << "TotalNumSamplesViewed: " << totalNumSamplesViewed << std::endl;
92  file << "Ranges: " << std::endl;
93 
94  for(UINT i=0; i<ranges.getSize(); i++){
95  file << ranges[i].minValue << "\t" << ranges[i].maxValue << std::endl;
96  }
97 
98  file.close();
99  return true;
100 }
101 
102 bool RangeTracker::loadRangeDataFromFile(std::string filename){
103 
104  std::fstream file;
105  file.open(filename.c_str(), std::ios::in);
106  clear();
107 
108  if( !file.is_open() ){
109  std::cout << "FILE NOT FOUND\n";
110  return false;
111  }
112 
113  std::string word;
114 
115  //Check to make sure this is a file with the correct file format
116  file >> word;
117  if(word != "GRT_RANGE_TRACKER_DATA_FILE_V1.0"){
118  file.close();
119  return false;
120  }
121 
122  //Get the number of dimensions in the data
123  file >> word;
124  if(word != "NumDimensions:"){
125  file.close();
126  return false;
127  }
128  file >> numDimensions;
129 
130  //Get the total number of training examples in the training data
131  file >> word;
132  if(word != "TotalNumSamplesViewed:"){
133  file.close();
134  return false;
135  }
136  file >> totalNumSamplesViewed;
137 
138 
139  //Load the ranges
140  file >> word;
141  if(word != "Ranges:"){
142  file.close();
143  return false;
144  }
145 
146  ranges.resize(numDimensions);
147  for(UINT i=0; i<ranges.size(); i++){
148  file >> ranges[i].minValue;
149  file >> ranges[i].maxValue;
150  }
151 
152  file.close();
153  return true;
154 }
155 
157  return ranges;
158 }
159 
160 GRT_END_NAMESPACE
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
bool saveRangeDataToFile(std::string filename)
unsigned int getSize() const
Definition: Vector.h:193
bool update(VectorFloat sample)
bool loadRangeDataFromFile(std::string filename)
Definition: MinMax.h:29
bool setNumDimensions(UINT numDimensions)
The RangeTracker can be used to keep track of the expected ranges that might occur in a dataset...
Vector< MinMax > getRanges()