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