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