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