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