GestureRecognitionToolkit  Version: 1.0 Revision: 04-03-15
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 namespace GRT{
24 
25 Node::StringNodeMap* Node::stringNodeMap = NULL;
26 UINT Node::numNodeInstances = 0;
27 
28 Node* Node::createInstanceFromString(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 VectorDouble &x){
60  warningLog << "predict(const VectorDouble &x) - Base class not overwritten!" << endl;
61  return false;
62 }
63 
64 bool Node::predict(const VectorDouble &x,VectorDouble &y){
65  warningLog << "predict(const VectorDouble &x) - Base class not overwritten!" << 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 
100 bool Node::print() const{
101 
102  ostringstream stream;
103  if( getModel( stream ) ){
104  cout << stream.str();
105  return true;
106  }
107 
108  return false;
109 }
110 
111 bool Node::getModel(ostream &stream) const{
112 
113  string tab = "";
114  for(UINT i=0; i<depth; i++) tab += "\t";
115 
116  stream << tab << "depth: " << depth << " isLeafNode: " << isLeafNode << " nodeID: " << nodeID << endl;
117 
118  if( leftChild != NULL ){
119  stream << tab << "LeftChild: " << endl;
120  leftChild->getModel( stream );
121  }
122 
123  if( rightChild != NULL ){
124  stream << tab << "RightChild: " << endl;
125  rightChild->getModel( stream );
126  }
127 
128  return true;
129 }
130 
131 bool Node::saveToFile(fstream &file) const{
132 
133  if(!file.is_open())
134  {
135  errorLog << "saveToFile(fstream &file) - File is not open!" << endl;
136  return false;
137  }
138 
139  file << "NodeType: " << nodeType << endl;
140  file << "Depth: " << depth << endl;
141  file << "NodeID: " << nodeID << endl;
142  file << "IsLeafNode: " << isLeafNode << endl;
143  file << "HasLeftChild: " << getHasLeftChild() << endl;
144  file << "HasRightChild: " << getHasRightChild() << endl;
145 
146  //If there is a left child then load the left child's data
147  if( getHasLeftChild() ){
148  file << "LeftChild\n";
149  if( !leftChild->saveToFile( file ) ){
150  errorLog << "saveToFile(fstream &file) - Failed to save left child at depth: " << depth << endl;
151  return false;
152  }
153  }
154 
155  //If there is a right child then load the right child's data
156  if( getHasRightChild() ){
157  file << "RightChild\n";
158  if( !rightChild->saveToFile( file ) ){
159  errorLog << "saveToFile(fstream &file) - Failed to save right child at depth: " << depth << endl;
160  return false;
161  }
162  }
163 
164  //Save the custom parameters to the file
165  if( !saveParametersToFile( file ) ){
166  errorLog << "saveToFile(fstream &file) - Failed to save parameters to file at depth: " << depth << endl;
167  return false;
168  }
169 
170  return true;
171 }
172 
173 bool Node::loadFromFile(fstream &file){
174 
175  //Clear any previous nodes
176  clear();
177 
178  if(!file.is_open())
179  {
180  errorLog << "loadFromFile(fstream &file) - File is not open!" << endl;
181  return false;
182  }
183 
184  string word;
185  bool hasLeftChild = false;
186  bool hasRightChild = false;
187 
188  file >> word;
189  if( word != "NodeType:" ){
190  errorLog << "loadFromFile(fstream &file) - Failed to find Node header!" << endl;
191  return false;
192  }
193  file >> nodeType;
194 
195  file >> word;
196  if( word != "Depth:" ){
197  errorLog << "loadFromFile(fstream &file) - Failed to find Depth header!" << endl;
198  return false;
199  }
200  file >> depth;
201 
202  file >> word;
203  if( word != "NodeID:" ){
204  errorLog << "loadFromFile(fstream &file) - Failed to find NodeID header!" << endl;
205  return false;
206  }
207  file >> nodeID;
208 
209  file >> word;
210  if( word != "IsLeafNode:" ){
211  errorLog << "loadFromFile(fstream &file) - Failed to find IsLeafNode header!" << endl;
212  return false;
213  }
214  file >> isLeafNode;
215 
216  file >> word;
217  if( word != "HasLeftChild:" ){
218  errorLog << "loadFromFile(fstream &file) - Failed to find HasLeftChild header!" << endl;
219  return false;
220  }
221  file >> hasLeftChild;
222 
223  file >> word;
224  if( word != "HasRightChild:" ){
225  errorLog << "loadFromFile(fstream &file) - Failed to find HasRightChild header!" << endl;
226  return false;
227  }
228  file >> hasRightChild;
229 
230  if( hasLeftChild ){
231  file >> word;
232  if( word != "LeftChild" ){
233  errorLog << "loadFromFile(fstream &file) - Failed to find LeftChild header!" << endl;
234  return false;
235  }
236  leftChild = createNewInstance();
237  leftChild->setParent( this );
238  if( !leftChild->loadFromFile(file) ){
239  errorLog << "loadFromFile(fstream &file) - Failed to load left child at depth: " << depth << endl;
240  return false;
241  }
242  }
243 
244  if( hasRightChild ){
245  file >> word;
246  if( word != "RightChild" ){
247  errorLog << "loadFromFile(fstream &file) - Failed to find RightChild header!" << endl;
248  return false;
249  }
250  rightChild = createNewInstance();
251  rightChild->setParent( this );
252  if( !rightChild->loadFromFile( file ) ){
253  errorLog << "loadFromFile(fstream &file) - Failed to load right child at depth: " << depth << endl;
254  return false;
255  }
256  }
257 
258  //Load the custom parameters from a file
259  if( !loadParametersFromFile( file ) ){
260  errorLog << "loadParametersFromFile(fstream &file) - Failed to load parameters from file at depth: " << depth << endl;
261  return false;
262  }
263 
264  return true;
265 }
266 
268 
269  Node *node = createNewInstance();
270 
271  if( node == NULL ){
272  return NULL;
273  }
274 
275  //Copy this node into the node
276  node->setNodeID( nodeID );
277  node->setDepth( depth );
278  node->setIsLeafNode( isLeafNode );
279 
280  //Recursively deep copy the left child
281  if( getHasLeftChild() ){
282  node->setLeftChild( leftChild->deepCopyNode() );
283  node->leftChild->setParent( node );
284  }
285 
286  //Recursively deep copy the right child
287  if( getHasRightChild() ){
288  node->setRightChild( rightChild->deepCopyNode() );
289  node->rightChild->setParent( node );
290  }
291 
292  return node;
293 }
294 
295 string Node::getNodeType() const{
296  return nodeType;
297 }
298 
299 UINT Node::getDepth() const{
300  return depth;
301 }
302 
303 UINT Node::getNodeID() const{
304  return nodeID;
305 }
306 
308  return predictedNodeID;
309 }
310 
311 UINT Node::getMaxDepth() const {
312 
313  UINT maxDepth = depth;
314 
315  //Search for the maximum depth in the left child
316  if( getHasLeftChild() ){
317  UINT maxLeftDepth = leftChild->getMaxDepth();
318  if( maxLeftDepth > maxDepth ){
319  maxDepth = maxLeftDepth;
320  }
321  }
322 
323  //Search for the maximum depth in the right child
324  if( getHasRightChild() ){
325  UINT maxRightDepth = rightChild->getMaxDepth();
326  if( maxRightDepth > maxDepth ){
327  maxDepth = maxRightDepth;
328  }
329  }
330 
331  return maxDepth;
332 }
333 
334 bool Node::getIsLeafNode() const{
335  return isLeafNode;
336 }
337 
338 bool Node::getHasParent() const{
339  return (parent != NULL);
340 }
341 
342 bool Node::getHasLeftChild() const {
343  return (leftChild != NULL);
344 }
345 
347  return (rightChild != NULL);
348 }
349 
350 bool Node::initNode(Node *parent,const UINT depth,const UINT nodeID,const bool isLeafNode){
351  this->parent = parent;
352  this->depth = depth;
353  this->nodeID = nodeID;
354  this->isLeafNode = isLeafNode;
355  return true;
356 }
357 
358 bool Node::setParent(Node *parent){
359  this->parent = parent;
360  return true;
361 }
362 
363 bool Node::setLeftChild(Node *leftChild){
364  this->leftChild = leftChild;
365  return true;
366 }
367 
368 bool Node::setRightChild(Node *rightChild){
369  this->rightChild = rightChild;
370  return true;
371 }
372 
373 bool Node::setDepth(const UINT depth){
374  this->depth = depth;
375  return true;
376 }
377 
378 bool Node::setNodeID(const UINT nodeID){
379  this->nodeID = nodeID;
380  return true;
381 }
382 
383 bool Node::setIsLeafNode(const bool isLeafNode){
384  this->isLeafNode = isLeafNode;
385  return true;
386 }
387 
388 } //End of namespace GRT
389 
virtual Node * deepCopyNode() const
Definition: Node.cpp:267
UINT getDepth() const
Definition: Node.cpp:299
UINT getPredictedNodeID() const
Definition: Node.cpp:307
bool getHasLeftChild() const
Definition: Node.cpp:342
Definition: AdaBoost.cpp:25
virtual bool loadFromFile(fstream &file)
Definition: Node.cpp:173
virtual bool saveToFile(fstream &file) const
Definition: Node.cpp:131
virtual bool getModel(ostream &stream) const
Definition: Node.cpp:111
string getNodeType() const
Definition: Node.cpp:295
virtual bool loadParametersFromFile(fstream &file)
Definition: Node.h:235
virtual bool saveParametersToFile(fstream &file) const
Definition: Node.h:223
std::map< string, Node *(*)() > StringNodeMap
Definition: Node.h:197
This class contains the main Node base class.
virtual ~Node()
Definition: Node.cpp:55
UINT getNodeID() const
Definition: Node.cpp:303
virtual bool predict(const VectorDouble &x)
Definition: Node.cpp:59
Node * createNewInstance() const
Definition: Node.cpp:38
bool getHasParent() const
Definition: Node.cpp:338
bool getIsLeafNode() const
Definition: Node.cpp:334
virtual bool clear()
Definition: Node.cpp:69
static Node * createInstanceFromString(string const &nodeType)
Definition: Node.cpp:28
Definition: Node.h:37
bool getHasRightChild() const
Definition: Node.cpp:346
Node()
Definition: Node.cpp:42
virtual bool print() const
Definition: Node.cpp:100