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