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