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.
PreProcessing.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 "PreProcessing.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 PreProcessing::StringPreProcessingMap* PreProcessing::stringPreProcessingMap = NULL;
27 UINT PreProcessing::numPreProcessingInstances = 0;
28 
29 PreProcessing* PreProcessing::createInstanceFromString( const std::string &preProcessingType ){
30 
31  StringPreProcessingMap::iterator iter = getMap()->find( preProcessingType );
32  if( iter == getMap()->end() ){
33  return NULL;
34  }
35  return iter->second();
36 }
37 
39  preProcessingType = "NOT_SET";
40  initialized = false;
41  numInputDimensions = 0;
42  numOutputDimensions = 0;
43  numPreProcessingInstances++;
44 }
45 
47  if( --numPreProcessingInstances == 0 ){
48  delete stringPreProcessingMap;
49  stringPreProcessingMap = NULL;
50  }
51 }
52 
53 bool PreProcessing::copyBaseVariables(const PreProcessing *preProcessingModule){
54 
55  if( preProcessingModule == NULL ){
56  errorLog << "copyBaseVariables(const PreProcessing *preProcessingModule) - preProcessingModule pointer is NULL!" << std::endl;
57  return false;
58  }
59 
60  if( !this->copyMLBaseVariables( preProcessingModule ) ){
61  return false;
62  }
63 
64  this->preProcessingType = preProcessingModule->preProcessingType;
65  this->initialized = preProcessingModule->initialized;
66  this->numInputDimensions = preProcessingModule->numInputDimensions;
67  this->numOutputDimensions = preProcessingModule->numOutputDimensions;
68  this->processedData = preProcessingModule->processedData;
69  this->debugLog = preProcessingModule->debugLog;
70  this->errorLog = preProcessingModule->errorLog;
71  this->warningLog = preProcessingModule->warningLog;
72 
73  return true;
74 }
75 
77 
78  //Reset the processed data vector
79  if( processedData.size() > 0 )
80  fill(processedData.begin(),processedData.end(),0);
81 
82  return true;
83 }
84 
86  initialized = false;
87  numInputDimensions = 0;
88  numOutputDimensions = 0;
89  processedData.clear();
90  return true;
91 }
92 
94 
95  if( numOutputDimensions == 0 ){
96  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << std::endl;
97  initialized = false;
98  return false;
99  }
100 
101  //Setup the output vector
102  processedData.resize( numOutputDimensions );
103 
104  //Flag the module has been initialized
105  initialized = true;
106 
107  return true;
108 }
109 
110 bool PreProcessing::savePreProcessingSettingsToFile(std::fstream &file) const{
111 
112  if( !file.is_open() ){
113  errorLog << "savePreProcessingSettingsToFile(fstream &file) - The file is not open!" << std::endl;
114  return false;
115  }
116 
117  if( !MLBase::saveBaseSettingsToFile( file ) ){
118  errorLog << "savePreProcessingSettingsToFile(fstream &file) - Failed to save base settings to file!" << std::endl;
119  return false;
120  }
121 
122  file << "Initialized: " << initialized << std::endl;
123 
124  return true;
125 }
126 
128 
129  if( !file.is_open() ){
130  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
131  return false;
132  }
133 
134  //Try and load the base settings from the file
135  if( !MLBase::loadBaseSettingsFromFile( file ) ){
136  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to load base settings from file!" << std::endl;
137  return false;
138  }
139 
140  std::string word;
141 
142  //Load if the filter has been initialized
143  file >> word;
144  if( word != "Initialized:" ){
145  errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
146  clear();
147  return false;
148  }
149  file >> initialized;
150 
151  //If the module has been initalized then call the init function to setup the processed data vector
152  if( initialized ){
153  return init();
154  }
155 
156  return true;
157 }
158 
160  return createInstanceFromString(preProcessingType);
161 }
162 
164  return preProcessingType;
165 }
166 
168  return numInputDimensions;
169 }
170 
172  return numOutputDimensions;
173 }
174 
176  return initialized;
177 }
178 
180  return processedData;
181 }
182 
183 GRT_END_NAMESPACE
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:375
virtual ~PreProcessing(void)
UINT getNumInputDimensions() const
UINT getNumOutputDimensions() const
bool savePreProcessingSettingsToFile(std::fstream &file) const
bool loadPreProcessingSettingsFromFile(std::fstream &file)
PreProcessing * createNewInstance() const
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
static PreProcessing * createInstanceFromString(std::string const &preProcessingType)
virtual bool reset()
std::string getPreProcessingType() const
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:50
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:398
VectorFloat getProcessedData() const
bool copyBaseVariables(const PreProcessing *preProcessingModule)
virtual bool clear()
This is the main base class that all GRT PreProcessing algorithms should inherit from.
std::map< std::string, PreProcessing *(*)() > StringPreProcessingMap
bool getInitialized() const