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.
DeadZone.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 "DeadZone.h"
22 
23 GRT_BEGIN_NAMESPACE
24 
25 //Register the DeadZone module with the PreProcessing base class
26 RegisterPreProcessingModule< DeadZone > DeadZone::registerModule("DeadZone");
27 
28 DeadZone::DeadZone(Float lowerLimit,Float upperLimit,UINT numDimensions){
29  classType = "DeadZone";
30  preProcessingType = classType;
31  debugLog.setProceedingText("[DEBUG DeadZone]");
32  errorLog.setProceedingText("[ERROR DeadZone]");
33  warningLog.setProceedingText("[WARNING DeadZone]");
34  init(lowerLimit,upperLimit,numDimensions);
35 }
36 
38  this->lowerLimit = rhs.lowerLimit;
39  this->upperLimit = rhs.upperLimit;
40  classType = "DeadZone";
41  preProcessingType = classType;
42  debugLog.setProceedingText("[DEBUG DeadZone]");
43  errorLog.setProceedingText("[ERROR DeadZone]");
44  warningLog.setProceedingText("[WARNING DeadZone]");
46 }
47 
49 
50 }
51 
53  if(this!=&rhs){
54  this->lowerLimit = rhs.lowerLimit;
55  this->upperLimit = rhs.upperLimit;
57  }
58  return *this;
59 }
60 
61 bool DeadZone::deepCopyFrom(const PreProcessing *preProcessing){
62 
63  if( preProcessing == NULL ) return false;
64 
65  if( this->getPreProcessingType() == preProcessing->getPreProcessingType() ){
66 
67  DeadZone *ptr = (DeadZone*)preProcessing;
68  //Clone the DeadZone values
69  this->lowerLimit = ptr->lowerLimit;
70  this->upperLimit = ptr->upperLimit;
71 
72  //Clone the base class variables
73  return copyBaseVariables( preProcessing );
74  }
75 
76  errorLog << "clone(const PreProcessing *preProcessing) - PreProcessing Types Do Not Match!" << std::endl;
77 
78  return false;
79 }
80 
81 bool DeadZone::process(const VectorFloat &inputVector){
82 
83  if( !initialized ){
84  errorLog << "process(const VectorFloat &inputVector) - Not initialized!" << std::endl;
85  return false;
86  }
87 
88  if( inputVector.size() != numInputDimensions ){
89  errorLog << "process(const VectorFloat &inputVector) - The size of the inputVector (" << inputVector.size() << ") does not match that of the filter (" << numInputDimensions << ")!" << std::endl;
90  return false;
91  }
92 
93  filter( inputVector );
94 
95  if( processedData.size() == numOutputDimensions ) return true;
96  return false;
97 }
98 
100  return true;
101 }
102 
103 bool DeadZone::saveModelToFile(std::string filename) const{
104 
105  if( !initialized ){
106  errorLog << "saveModelToFile(std::string filename) - The DeadZone has not been initialized" << std::endl;
107  return false;
108  }
109 
110  std::fstream file;
111  file.open(filename.c_str(), std::ios::out);
112 
113  if( !saveModelToFile( file ) ){
114  file.close();
115  return false;
116  }
117 
118  file.close();
119 
120  return true;
121 }
122 
123 bool DeadZone::saveModelToFile(std::fstream &file) const{
124 
125  if( !file.is_open() ){
126  errorLog << "saveModelToFile(fstream &file) - The file is not open!" << std::endl;
127  return false;
128  }
129 
130  file << "GRT_DEAD_ZONE_FILE_V1.0" << std::endl;
131 
132  file << "NumInputDimensions: " << numInputDimensions << std::endl;
133  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
134  file << "LowerLimit: " << lowerLimit << std::endl;
135  file << "UpperLimit: " << upperLimit << std::endl;
136 
137  return true;
138 }
139 
140 bool DeadZone::loadModelFromFile(std::string filename){
141 
142  std::fstream file;
143  file.open(filename.c_str(), std::ios::in);
144 
145  if( !loadModelFromFile( file ) ){
146  file.close();
147  initialized = false;
148  return false;
149  }
150 
151  file.close();
152 
153  return true;
154 }
155 
156 bool DeadZone::loadModelFromFile(std::fstream &file){
157 
158  if( !file.is_open() ){
159  errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
160  return false;
161  }
162 
163  std::string word;
164 
165  //Load the header
166  file >> word;
167 
168  if( word != "GRT_DEAD_ZONE_FILE_V1.0" ){
169  errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
170  return false;
171  }
172 
173  //Load the number of input dimensions
174  file >> word;
175  if( word != "NumInputDimensions:" ){
176  errorLog << "loadModelFromFile(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
177  return false;
178  }
179  file >> numInputDimensions;
180 
181  //Load the number of output dimensions
182  file >> word;
183  if( word != "NumOutputDimensions:" ){
184  errorLog << "loadModelFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
185  return false;
186  }
187  file >> numOutputDimensions;
188 
189  //Load the lower limit
190  file >> word;
191  if( word != "LowerLimit:" ){
192  errorLog << "loadModelFromFile(fstream &file) - Failed to read LowerLimit header!" << std::endl;
193  return false;
194  }
195  file >> lowerLimit;
196 
197  file >> word;
198  if( word != "UpperLimit:" ){
199  errorLog << "loadModelFromFile(fstream &file) - Failed to read UpperLimit header!" << std::endl;
200  return false;
201  }
202  file >> upperLimit;
203 
204  //Init the deadzone module to ensure everything is initialized correctly
205  return init(lowerLimit,upperLimit,numInputDimensions);
206 }
207 
208 bool DeadZone::init(Float lowerLimit,Float upperLimit,UINT numDimensions){
209 
210  initialized = false;
211 
212  if( numDimensions == 0 ){
213  errorLog << "init(Float lowerLimit,Float upperLimit,UINT numDimensions) - NumDimensions must be greater than 0!" << std::endl;
214  return false;
215  }
216 
217  if( lowerLimit >= upperLimit ){
218  errorLog << "init(Float lowerLimit,Float upperLimit,UINT numDimensions) - The lower limit must be less than the upperlimit!" << std::endl;
219  return false;
220  }
221 
222  this->lowerLimit = lowerLimit;
223  this->upperLimit = upperLimit;
224  this->numInputDimensions = numDimensions;
225  this->numOutputDimensions = numDimensions;
226  processedData.clear();
227  processedData.resize(numOutputDimensions,0);
228  initialized = true;
229 
230  return true;
231 }
232 
233 Float DeadZone::filter(const Float x){
234  VectorFloat y = filter(VectorFloat(1,x));
235  if( y.getSize() == 0 ) return 0;
236  return y[0];
237 }
238 
240 
241  if( !initialized ){
242  errorLog << "filter(const VectorFloat &x) - Not Initialized!" << std::endl;
243  return VectorFloat();
244  }
245 
246  if( x.getSize() != numInputDimensions ){
247  errorLog << "filter(const VectorFloat &x) - The Number Of Input Dimensions (" << numInputDimensions << ") does not match the size of the input vector (" << x.size() << ")!" << std::endl;
248  return VectorFloat();
249  }
250 
251  for(UINT n=0; n<numInputDimensions; n++){
252  if( x[n] > lowerLimit && x[n] < upperLimit ){
253  processedData[n] = 0;
254  }else{
255  if( x[n] >= upperLimit ) processedData[n] = x[n] - upperLimit;
256  else processedData[n] = x[n] - lowerLimit;
257  }
258  }
259  return processedData;
260 }
261 
262 bool DeadZone::setLowerLimit(Float lowerLimit){
263  this->lowerLimit = lowerLimit;
264  return true;
265 }
266 
267 bool DeadZone::setUpperLimit(Float upperLimit){
268  this->upperLimit = upperLimit;
269  return true;
270 }
271 
272 GRT_END_NAMESPACE
virtual bool saveModelToFile(std::string filename) const
Definition: DeadZone.cpp:103
Float upperLimit
The upper limit of the dead-zone region.
Definition: DeadZone.h:193
DeadZone & operator=(const DeadZone &rhs)
Definition: DeadZone.cpp:52
virtual bool process(const VectorFloat &inputVector)
Definition: DeadZone.cpp:81
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
Float filter(const Float x)
Definition: DeadZone.cpp:233
bool setUpperLimit(Float upperLimit)
Definition: DeadZone.cpp:267
unsigned int getSize() const
Definition: Vector.h:193
virtual bool deepCopyFrom(const PreProcessing *preProcessing)
Definition: DeadZone.cpp:61
virtual ~DeadZone()
Definition: DeadZone.cpp:48
virtual bool reset()
Definition: DeadZone.cpp:99
std::string getPreProcessingType() const
Float lowerLimit
The lower limit of the dead-zone region.
Definition: DeadZone.h:192
bool setLowerLimit(Float lowerLimit)
Definition: DeadZone.cpp:262
DeadZone(Float lowerLimit=-0.1, Float upperLimit=0.1, UINT numDimensions=1)
Definition: DeadZone.cpp:28
The DeadZone class sets any values in the input signal that fall within the dead-zone region to zero...
bool copyBaseVariables(const PreProcessing *preProcessingModule)
virtual bool loadModelFromFile(std::string filename)
Definition: DeadZone.cpp:140