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.
Node.cpp
1 
21 #define GRT_DLL_EXPORTS
22 #include "Node.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
26 Node::StringNodeMap* Node::stringNodeMap = NULL;
27 UINT Node::numNodeInstances = 0;
28 
29 Node* Node::createInstanceFromString( std::string const &nodeType ){
30 
31  StringNodeMap::iterator iter = getMap()->find( nodeType );
32  if( iter == getMap()->end() ){
33  return NULL;
34  }
35 
36  return iter->second();
37 }
38 
40  return createInstanceFromString( nodeType );
41 }
42 
44  nodeType = "";
45  parent = NULL;
46  leftChild = NULL;
47  rightChild = NULL;
48  debugLog.setProceedingText("[DEBUG Node]");
49  errorLog.setProceedingText("[ERROR Node]");
50  trainingLog.setProceedingText("[TRAINING Node]");
51  testingLog.setProceedingText("[TESTING Node]");
52  warningLog.setProceedingText("[WARNING Node]");
53  clear();
54 }
55 
57  clear();
58 }
59 
60 bool Node::predict(const VectorFloat &x){
61  warningLog << "predict(const VectorFloat &x) - Base class not overwritten!" << std::endl;
62  return false;
63 }
64 
66  warningLog << "predict(const VectorFloat &x) - Base class not overwritten!" << std::endl;
67  return false;
68 }
69 
70 bool Node::clear(){
71 
72  //Set the parent pointer to null, this is safe as the parent pointer does not own the memory
73  parent = NULL;
74 
75  if( leftChild != NULL ){
76  //Recursively clean up the left child
77  leftChild->clear();
78 
79  //Clean up the left child
80  delete leftChild;
81  leftChild = NULL;
82  }
83 
84  if( rightChild != NULL ){
85  //Recursively clean up the right child
86  rightChild->clear();
87 
88  //Clean up the right child
89  delete rightChild;
90  rightChild = NULL;
91  }
92 
93  depth = 0;
94  nodeID = 0;
95  predictedNodeID = 0;
96  isLeafNode = false;
97 
98  return true;
99 }
100 
102  return false; //Base class always return false
103 }
104 
106  return false; //Base class always return false
107 }
108 
109 bool Node::print() const{
110 
111  std::ostringstream stream;
112  if( getModel( stream ) ){
113  std::cout << stream.str();
114  return true;
115  }
116 
117  return false;
118 }
119 
120 bool Node::getModel( std::ostream &stream ) const{
121 
122  std::string tab = "";
123  for(UINT i=0; i<depth; i++) tab += "\t";
124 
125  stream << tab << "depth: " << depth << " isLeafNode: " << isLeafNode << " nodeID: " << nodeID << std::endl;
126 
127  if( leftChild != NULL ){
128  stream << tab << "LeftChild: " << std::endl;
129  leftChild->getModel( stream );
130  }
131 
132  if( rightChild != NULL ){
133  stream << tab << "RightChild: " << std::endl;
134  rightChild->getModel( stream );
135  }
136 
137  return true;
138 }
139 
140 bool Node::save( std::fstream &file ) const{
141 
142  if(!file.is_open())
143  {
144  errorLog << "save(fstream &file) - File is not open!" << std::endl;
145  return false;
146  }
147 
148  file << "NodeType: " << nodeType << std::endl;
149  file << "Depth: " << depth << std::endl;
150  file << "NodeID: " << nodeID << std::endl;
151  file << "IsLeafNode: " << isLeafNode << std::endl;
152  file << "HasLeftChild: " << getHasLeftChild() << std::endl;
153  file << "HasRightChild: " << getHasRightChild() << std::endl;
154 
155  //If there is a left child then load the left child's data
156  if( getHasLeftChild() ){
157  file << "LeftChild\n";
158  if( !leftChild->save( file ) ){
159  errorLog << "save(fstream &file) - Failed to save left child at depth: " << depth << std::endl;
160  return false;
161  }
162  }
163 
164  //If there is a right child then load the right child's data
165  if( getHasRightChild() ){
166  file << "RightChild\n";
167  if( !rightChild->save( file ) ){
168  errorLog << "save(fstream &file) - Failed to save right child at depth: " << depth << std::endl;
169  return false;
170  }
171  }
172 
173  //Save the custom parameters to the file
174  if( !saveParametersToFile( file ) ){
175  errorLog << "save(fstream &file) - Failed to save parameters to file at depth: " << depth << std::endl;
176  return false;
177  }
178 
179  return true;
180 }
181 
182 bool Node::load( std::fstream &file ){
183 
184  //Clear any previous nodes
185  clear();
186 
187  if(!file.is_open())
188  {
189  errorLog << "load(fstream &file) - File is not open!" << std::endl;
190  return false;
191  }
192 
193  std::string word;
194  bool hasLeftChild = false;
195  bool hasRightChild = false;
196 
197  file >> word;
198  if( word != "NodeType:" ){
199  errorLog << "load(fstream &file) - Failed to find Node header!" << std::endl;
200  return false;
201  }
202  file >> nodeType;
203 
204  file >> word;
205  if( word != "Depth:" ){
206  errorLog << "load(fstream &file) - Failed to find Depth header!" << std::endl;
207  return false;
208  }
209  file >> depth;
210 
211  file >> word;
212  if( word != "NodeID:" ){
213  errorLog << "load(fstream &file) - Failed to find NodeID header!" << std::endl;
214  return false;
215  }
216  file >> nodeID;
217 
218  file >> word;
219  if( word != "IsLeafNode:" ){
220  errorLog << "load(fstream &file) - Failed to find IsLeafNode header!" << std::endl;
221  return false;
222  }
223  file >> isLeafNode;
224 
225  file >> word;
226  if( word != "HasLeftChild:" ){
227  errorLog << "load(fstream &file) - Failed to find HasLeftChild header!" << std::endl;
228  return false;
229  }
230  file >> hasLeftChild;
231 
232  file >> word;
233  if( word != "HasRightChild:" ){
234  errorLog << "load(fstream &file) - Failed to find HasRightChild header!" << std::endl;
235  return false;
236  }
237  file >> hasRightChild;
238 
239  if( hasLeftChild ){
240  file >> word;
241  if( word != "LeftChild" ){
242  errorLog << "load(fstream &file) - Failed to find LeftChild header!" << std::endl;
243  return false;
244  }
245  leftChild = createNewInstance();
246  leftChild->setParent( this );
247  if( !leftChild->load(file) ){
248  errorLog << "load(fstream &file) - Failed to load left child at depth: " << depth << std::endl;
249  return false;
250  }
251  }
252 
253  if( hasRightChild ){
254  file >> word;
255  if( word != "RightChild" ){
256  errorLog << "load(fstream &file) - Failed to find RightChild header!" << std::endl;
257  return false;
258  }
259  rightChild = createNewInstance();
260  rightChild->setParent( this );
261  if( !rightChild->load( file ) ){
262  errorLog << "load(fstream &file) - Failed to load right child at depth: " << depth << std::endl;
263  return false;
264  }
265  }
266 
267  //Load the custom parameters from a file
268  if( !loadParametersFromFile( file ) ){
269  errorLog << "loadParametersFromFile(fstream &file) - Failed to load parameters from file at depth: " << depth << std::endl;
270  return false;
271  }
272 
273  return true;
274 }
275 
277 
278  Node *node = createNewInstance();
279 
280  if( node == NULL ){
281  return NULL;
282  }
283 
284  //Copy this node into the node
285  node->setNodeID( nodeID );
286  node->setDepth( depth );
287  node->setIsLeafNode( isLeafNode );
288 
289  //Recursively deep copy the left child
290  if( getHasLeftChild() ){
291  node->setLeftChild( leftChild->deepCopyNode() );
292  node->leftChild->setParent( node );
293  }
294 
295  //Recursively deep copy the right child
296  if( getHasRightChild() ){
297  node->setRightChild( rightChild->deepCopyNode() );
298  node->rightChild->setParent( node );
299  }
300 
301  return node;
302 }
303 
304 std::string Node::getNodeType() const{
305  return nodeType;
306 }
307 
308 UINT Node::getDepth() const{
309  return depth;
310 }
311 
312 UINT Node::getNodeID() const{
313  return nodeID;
314 }
315 
317  return predictedNodeID;
318 }
319 
320 UINT Node::getMaxDepth() const {
321 
322  UINT maxDepth = depth;
323 
324  //Search for the maximum depth in the left child
325  if( getHasLeftChild() ){
326  UINT maxLeftDepth = leftChild->getMaxDepth();
327  if( maxLeftDepth > maxDepth ){
328  maxDepth = maxLeftDepth;
329  }
330  }
331 
332  //Search for the maximum depth in the right child
333  if( getHasRightChild() ){
334  UINT maxRightDepth = rightChild->getMaxDepth();
335  if( maxRightDepth > maxDepth ){
336  maxDepth = maxRightDepth;
337  }
338  }
339 
340  return maxDepth;
341 }
342 
343 bool Node::getIsLeafNode() const{
344  return isLeafNode;
345 }
346 
347 bool Node::getHasParent() const{
348  return (parent != NULL);
349 }
350 
351 bool Node::getHasLeftChild() const {
352  return (leftChild != NULL);
353 }
354 
356  return (rightChild != NULL);
357 }
358 
359 bool Node::initNode(Node *parent,const UINT depth,const UINT nodeID,const bool isLeafNode){
360  this->parent = parent;
361  this->depth = depth;
362  this->nodeID = nodeID;
363  this->isLeafNode = isLeafNode;
364  return true;
365 }
366 
367 bool Node::setParent(Node *parent){
368  this->parent = parent;
369  return true;
370 }
371 
372 bool Node::setLeftChild(Node *leftChild){
373  this->leftChild = leftChild;
374  return true;
375 }
376 
377 bool Node::setRightChild(Node *rightChild){
378  this->rightChild = rightChild;
379  return true;
380 }
381 
382 bool Node::setDepth(const UINT depth){
383  this->depth = depth;
384  return true;
385 }
386 
387 bool Node::setNodeID(const UINT nodeID){
388  this->nodeID = nodeID;
389  return true;
390 }
391 
392 bool Node::setIsLeafNode(const bool isLeafNode){
393  this->isLeafNode = isLeafNode;
394  return true;
395 }
396 
397 GRT_END_NAMESPACE
virtual ~Node()
Definition: Node.cpp:56
Definition: Node.h:37
virtual bool print() const
Definition: Node.cpp:109
std::string getNodeType() const
Definition: Node.cpp:304
virtual bool getModel(std::ostream &stream) const
Definition: Node.cpp:120
UINT getDepth() const
Definition: Node.cpp:308
virtual bool save(std::fstream &file) const
Definition: Node.cpp:140
virtual Node * deepCopyNode() const
Definition: Node.cpp:276
bool getIsLeafNode() const
Definition: Node.cpp:343
virtual bool loadParametersFromFile(std::fstream &file)
Definition: Node.h:250
virtual bool saveParametersToFile(std::fstream &file) const
Definition: Node.h:241
This class contains the main Node base class.
bool getHasParent() const
Definition: Node.cpp:347
virtual bool clear()
Definition: Node.cpp:70
virtual bool computeFeatureWeights(VectorFloat &weights) const
Definition: Node.cpp:101
Node * createNewInstance() const
Definition: Node.cpp:39
UINT getPredictedNodeID() const
Definition: Node.cpp:316
static Node * createInstanceFromString(std::string const &nodeType)
Definition: Node.cpp:29
virtual bool computeLeafNodeWeights(MatrixFloat &weights) const
Definition: Node.cpp:105
UINT getNodeID() const
Definition: Node.cpp:312
virtual bool load(std::fstream &file)
Definition: Node.cpp:182
std::map< std::string, Node *(*)() > StringNodeMap
Definition: Node.h:215
virtual bool predict(const VectorFloat &x)
Definition: Node.cpp:60
bool getHasRightChild() const
Definition: Node.cpp:355
bool getHasLeftChild() const
Definition: Node.cpp:351
Node()
Definition: Node.cpp:43