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.
Node.h
Go to the documentation of this file.
1 
10 /*
11 GRT MIT License
12 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
13 
14 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
15 and associated documentation files (the "Software"), to deal in the Software without restriction,
16 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
18 subject to the following conditions:
19 
20 The above copyright notice and this permission notice shall be included in all copies or substantial
21 portions of the Software.
22 
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29 
30 #ifndef GRT_NODE_HEADER
31 #define GRT_NODE_HEADER
32 
33 #include "../../CoreModules/MLBase.h"
34 
35 GRT_BEGIN_NAMESPACE
36 
37 class GRT_API Node : public MLBase{
38 public:
42  Node( const std::string id = "Node" );
43 
47  virtual ~Node();
48 
56  virtual bool predict_(VectorFloat &x) override;
57 
66  virtual bool predict_(VectorFloat &x,VectorFloat &y);
67 
75  virtual bool computeFeatureWeights( VectorFloat &weights ) const;
76 
84  virtual bool computeLeafNodeWeights( MatrixFloat &weights ) const;
85 
92  virtual bool clear() override;
93 
100  virtual bool print() const override;
101 
109  virtual bool getModel( std::ostream &stream ) const override;
110 
117  virtual bool save( std::fstream &file ) const override;
118 
125  virtual bool load( std::fstream &file ) override;
126 
133  virtual Node* deepCopy() const;
134 
140  std::string getNodeType() const;
141 
148  UINT getDepth() const;
149 
155  UINT getNodeID() const;
156 
162  UINT getPredictedNodeID() const;
163 
164  UINT getMaxDepth() const;
165 
171  bool getIsLeafNode() const;
172 
178  bool getHasParent() const;
179 
185  bool getHasLeftChild() const;
186 
192  bool getHasRightChild() const;
193 
194  Node *getParent() const { return parent; }
195  Node *getLeftChild() const { return leftChild; }
196  Node *getRightChild() const { return rightChild; }
197 
198  bool initNode(Node *parent,const UINT depth,const UINT nodeID,const bool isLeafNode = false);
199 
200  bool setParent(Node *parent);
201 
202  bool setLeftChild(Node *leftChild);
203 
204  bool setRightChild(Node *rightChild);
205 
206  bool setDepth(const UINT depth);
207 
208  bool setNodeID(const UINT nodeID);
209 
210  bool setIsLeafNode(const bool isLeafNode);
211 
215  typedef std::map< std::string, Node*(*)() > StringNodeMap;
216 
223  static Node* createInstanceFromString( std::string const &nodeType );
224 
230  Node* createNewInstance() const;
231 
232  using MLBase::save;
233  using MLBase::load;
234  using MLBase::predict;
235  using MLBase::predict_;
236 
237 protected:
238 
246  virtual bool saveParametersToFile( std::fstream &file ) const{ return true; }
247 
255  virtual bool loadParametersFromFile( std::fstream &file ){ return true; }
256 
257  std::string nodeType;
258  UINT depth;
259  UINT nodeID;
260  UINT predictedNodeID;
261  bool isLeafNode;
262  Node *parent;
263  Node *leftChild;
264  Node *rightChild;
265 
266  static StringNodeMap *getMap() {
267  if( !stringNodeMap ){ stringNodeMap = new StringNodeMap; }
268  return stringNodeMap;
269  }
270 
271 private:
272  static StringNodeMap *stringNodeMap;
273  static UINT numNodeInstances;
274 };
275 
276 //These two functions/classes are used to register any new Node with the Node base class
277 template< typename T > Node* getNewNodeInstance() { return new T; }
278 
279 template< typename T >
281  public:
282  RegisterNode(std::string const &newNodeName) {
283  getMap()->insert( std::pair< std::string, Node*(*)() >(newNodeName, &getNewNodeInstance< T > ) );
284  }
285 };
286 
287 GRT_END_NAMESPACE
288 
289 #endif //GRT_NODE_HEADER
290 
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:135
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:137
Definition: Node.h:37
virtual bool getModel(std::ostream &stream) const
Definition: MLBase.cpp:213
virtual bool save(const std::string &filename) const
Definition: MLBase.cpp:167
virtual bool loadParametersFromFile(std::fstream &file)
Definition: Node.h:255
virtual bool print() const
Definition: MLBase.cpp:165
virtual bool saveParametersToFile(std::fstream &file) const
Definition: Node.h:246
virtual bool clear()
Definition: MLBase.cpp:149
virtual bool load(const std::string &filename)
Definition: MLBase.cpp:190
This is the main base class that all GRT machine learning algorithms should inherit from...
Definition: MLBase.h:72
std::map< std::string, Node *(*)() > StringNodeMap
Definition: Node.h:215