GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Context.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 "Context.h"
22 namespace GRT{
23 
24 Context::StringContextMap* Context::stringContextMap = NULL;
25 UINT Context::numContextInstances = 0;
26 
27 Context* Context::createInstanceFromString(string const &contextType){
28 
29  StringContextMap::iterator iter = getMap()->find( contextType );
30  if( iter == getMap()->end() ){
31  return NULL;
32  }
33  return iter->second();
34 }
35 
37  return createInstanceFromString( contextType );
38 }
39 
41 
42  //Clear any previous data
43  data.clear();
44 
45  if( numOutputDimensions == 0 ){
46  errorLog << "init() - Failed to init module, the number of output dimensions is zero!" << endl;
47  initialized = false;
48  return false;
49  }
50 
51  //Flag that the context data is OK to continue
52  okToContinue = true;
53 
54  //Resize the feature vector
55  data.resize(numOutputDimensions,0);
56 
57  //Flag the module has been initialized
58  initialized = true;
59 
60  return true;
61 }
62 
63 bool Context::saveContextSettingsToFile(fstream &file) const{
64 
65  if( !file.is_open() ){
66  errorLog << "saveContextSettingsToFile(fstream &file) - The file is not open!" << endl;
67  return false;
68  }
69 
70  if( !MLBase::saveBaseSettingsToFile( file ) ) return false;
71 
72  file << "Initialized: " << initialized << endl;
73 
74  return true;
75 }
76 
78 
79  if( !file.is_open() ){
80  errorLog << "loadContextSettingsFromFile(fstream &file) - The file is not open!" << endl;
81  return false;
82  }
83 
84  //Try and load the base settings from the file
85  if( !MLBase::loadBaseSettingsFromFile( file ) ){
86  return false;
87  }
88 
89  string word;
90 
91  //Load if the filter has been initialized
92  file >> word;
93  if( word != "Initialized:" ){
94  errorLog << "loadContextSettingsFromFile(fstream &file) - Failed to read Initialized header!" << endl;
95  clear();
96  return false;
97  }
98  file >> initialized;
99 
100  //If the module has been initalized then call the init function to setup the feature data vector
101  if( initialized ){
102  return init();
103  }
104 
105  return true;
106 }
107 
108 } //End of namespace GRT
109 
virtual bool clear()
Definition: MLBase.cpp:118
bool loadBaseSettingsFromFile(fstream &file)
Definition: MLBase.cpp:357
Definition: AdaBoost.cpp:25
bool saveBaseSettingsToFile(fstream &file) const
Definition: MLBase.cpp:334
std::map< string, Context *(*)() > StringContextMap
Definition: Context.h:101
bool init()
Definition: Context.cpp:40
Context * createNewInstance() const
Definition: Context.cpp:36
static Context * createInstanceFromString(string const &contextType)
Definition: Context.cpp:27
bool loadContextSettingsFromFile(fstream &file)
Definition: Context.cpp:77
bool saveContextSettingsToFile(fstream &file) const
Definition: Context.cpp:63
This is the main base class that all GRT Feature Extraction algorithms should inherit from...