GestureRecognitionToolkit  Version: 0.2.5
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 #define GRT_DLL_EXPORTS
22 #include "RangeTracker.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
27  trackData = true;
28  numDimensions = 0;
29  totalNumSamplesViewed = 0;
30 }
31 
32 RangeTracker::RangeTracker(UINT numDimensions){
33  trackData = true;
34  setNumDimensions( numDimensions );
35 }
36 
38  this->trackData = rhs.trackData;
39  this->numDimensions = rhs.numDimensions;
40  this->totalNumSamplesViewed = rhs.totalNumSamplesViewed;
41  this->ranges = rhs.ranges;
42 }
43 
45 
47  totalNumSamplesViewed = 0;
48  ranges.clear();
49  if( numDimensions > 0 )
50  ranges.resize(numDimensions,MinMax(BIG_POSITIVE_VALUE,BIG_NEGATIVE_VALUE));
51 }
52 
53 bool RangeTracker::setNumDimensions(UINT numDimensions){
54 
55  if( numDimensions > 0 ){
56  //Set the dimensionality of the data
57  this->numDimensions = numDimensions;
58 
59  //Reset the ranges values
60  clear();
61 
62  return true;
63  }
64  return false;
65 }
66 
68  if( sample.size() != numDimensions ) return false;
69 
70  if( !trackData ) return true;
71 
72  totalNumSamplesViewed++;
73  for(UINT j=0; j<numDimensions; j++){
74  if( sample[j] < ranges[j].minValue ){ ranges[j].minValue = sample[j]; }
75  else if( sample[j] > ranges[j].maxValue ){ ranges[j].maxValue = sample[j]; }
76  }
77 
78  return true;
79 }
80 
81 bool RangeTracker::saveRangeDataToFile(std::string filename){
82 
83  std::fstream file;
84  file.open(filename.c_str(), std::ios::out);
85 
86  if( !file.is_open() ){
87  return false;
88  }
89 
90  file << "GRT_RANGE_TRACKER_DATA_FILE_V1.0\n";
91  file << "NumDimensions: " << numDimensions << std::endl;
92  file << "TotalNumSamplesViewed: " << totalNumSamplesViewed << std::endl;
93  file << "Ranges: " << std::endl;
94 
95  for(UINT i=0; i<ranges.getSize(); i++){
96  file << ranges[i].minValue << "\t" << ranges[i].maxValue << std::endl;
97  }
98 
99  file.close();
100  return true;
101 }
102 
103 bool RangeTracker::loadRangeDataFromFile(std::string filename){
104 
105  std::fstream file;
106  file.open(filename.c_str(), std::ios::in);
107  clear();
108 
109  if( !file.is_open() ){
110  std::cout << "FILE NOT FOUND\n";
111  return false;
112  }
113 
114  std::string word;
115 
116  //Check to make sure this is a file with the correct file format
117  file >> word;
118  if(word != "GRT_RANGE_TRACKER_DATA_FILE_V1.0"){
119  file.close();
120  return false;
121  }
122 
123  //Get the number of dimensions in the data
124  file >> word;
125  if(word != "NumDimensions:"){
126  file.close();
127  return false;
128  }
129  file >> numDimensions;
130 
131  //Get the total number of training examples in the training data
132  file >> word;
133  if(word != "TotalNumSamplesViewed:"){
134  file.close();
135  return false;
136  }
137  file >> totalNumSamplesViewed;
138 
139 
140  //Load the ranges
141  file >> word;
142  if(word != "Ranges:"){
143  file.close();
144  return false;
145  }
146 
147  ranges.resize(numDimensions);
148  for(UINT i=0; i<ranges.size(); i++){
149  file >> ranges[i].minValue;
150  file >> ranges[i].maxValue;
151  }
152 
153  file.close();
154  return true;
155 }
156 
158  return ranges;
159 }
160 
161 GRT_END_NAMESPACE
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
bool saveRangeDataToFile(std::string filename)
UINT getSize() const
Definition: Vector.h:201
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()