4
* Tree structure of organism
6
* Created on: May 25, 2010
13
#include <srCore/organismTree.h>
16
treeNode::treeNode(string id, string rob, string con, osg::Vec3f pos, osg::Quat quat)
20
data.controllerName = con;
35
treeNode::treeNode(string id, string rob, string con, osg::Vec3f pos, osg::Quat quat, short sideParent)
39
data.controllerName = con;
61
treeNode* parentNode = sides[parent]->parentNode;
63
parentNode->sides[ sides[parent]->sideParent ] = NULL;
65
std::vector<short>::iterator iter(parentNode->children.begin());
67
while(parentNode->children[i] != sides[parent]->sideParent)
74
parentNode->children.erase(iter);
77
cout << "organismTree: " << data.robotName << " deleted" << endl;
81
void treeNode::addEdge(treeNode* node, short sideChild, short sideParent, short rot)
83
sides[sideParent] = new treeEdge(this, node, sideChild, sideParent, rot);
85
children.push_back(sideParent);
87
node->sides[sideChild] = sides[sideParent];
89
std::sort(children.begin(), children.end());
91
sides[children[0]]->childNode->prevSibling = NULL;
92
sides[children[children.size()-1]]->childNode->nextSibling = NULL;
94
for(unsigned int i=0; i<children.size(); i++)
97
sides[children[i]]->childNode->prevSibling = sides[children[i-1]]->childNode;
98
if(i<children.size()-1)
99
sides[children[i]]->childNode->nextSibling = sides[children[i+1]]->childNode;
103
void treeNode::delEdge(int i)
105
std::vector<short>::iterator iter(children.begin());
108
treeNode* child = sides[children[i]]->childNode;
110
child->sides[child->parent] = NULL; //loescht verweis auf kante des kindknotens
111
child->parent = -1; //setzt den index der kante des kindknotens
113
delete sides[children[i]]; //loescht kante
114
children.erase(iter); //loescht index der kante
116
for(unsigned int i=0; i<children.size(); i++)
119
sides[children[i]]->childNode->prevSibling = sides[children[i-1]]->childNode;
121
sides[children[0]]->childNode->prevSibling = NULL;
123
if(i<children.size()-1)
124
sides[children[i]]->childNode->nextSibling = sides[children[i+1]]->childNode;
126
sides[children[children.size()-1]]->childNode->nextSibling = NULL;
132
bool operator() (treeNode* const &lhs, treeNode* const &rhs ) const
136
bool treeNode::operator<(const treeNode& other) const
142
numChild[0] = children.size();
144
numChild[0] = children.size() + 1;
147
numChild[1] = other.children.size();
149
numChild[1] = other.children.size() + 1;
152
if(numChild[0] == numChild[1])
154
maxDepth[0] = getMaxDepthAsRoot();
155
maxDepth[1] = other.getMaxDepthAsRoot();
157
if(maxDepth[0] == maxDepth[1]) return true;
158
else return (maxDepth[0] < maxDepth[1]);
161
else return (numChild[0] > numChild[1]);
164
bool treeNode::isEqual(const treeNode* other) const
170
numChild[0] = children.size();
172
numChild[0] = children.size() + 1;
174
if(other->parent==-1)
175
numChild[1] = other->children.size();
177
numChild[1] = other->children.size() + 1;
180
if(numChild[0] == numChild[1])
182
maxDepth[0] = getMaxDepthAsRoot();
183
maxDepth[1] = other->getMaxDepthAsRoot();
185
return maxDepth[0] == maxDepth[1];
190
int treeNode::getMaxDepthAsRoot() const
192
vector<short> maxDepthRootSides;
195
maxDepthRootSides.push_back(maxDepth);
199
const treeNode* tmp = this;
200
while(tmp->sides[tmp->parent]->parentNode->parent != -1)
202
tmp = tmp->sides[tmp->parent]->parentNode;
206
short side_root = tmp->sides[tmp->parent]->sideParent;
207
tmp = tmp->sides[tmp->parent]->parentNode;
209
if(tmp->children.size()==1)
210
maxDepthRootSides.push_back(depth);
212
for(unsigned int i=0; i<tmp->children.size(); i++)
213
if(tmp->children[i]!=side_root)
214
maxDepthRootSides.push_back(tmp->sides[tmp->children[i]]->childNode->maxDepth + depth);
215
maxDepthRootSides.push_back(maxDepth-depth);
217
std::sort(maxDepthRootSides.begin(), maxDepthRootSides.end());
219
// for(unsigned int i=0; i<maxDepthRootSides.size(); i++)
220
// cout << "organismTree: " << data.robotName << " maxDepthRootSides[" << i << "]=" << maxDepthRootSides[i] << endl;
222
return maxDepthRootSides[maxDepthRootSides.size()-1];
226
treeNode::treeEdge::treeEdge(treeNode* parent, treeNode* child, short sideChild, short sideParent, short rot)
231
this->sideChild = sideChild;
232
this->sideParent = sideParent;
236
treeNode::treeEdge::~treeEdge()
243
organismTree::organismTree(string id, string rob, string con, osg::Vec3f pos, osg::Quat quat)
245
treeNode* node = new treeNode(id, rob, con, pos, quat);
252
organismTree::~organismTree()
257
organismTree::iterator organismTree::seed(string id, string rob, string con, osg::Vec3f pos, osg::Quat quat)
259
treeNode* node = new treeNode(id, rob, con, pos, quat);
265
return iterator(root);
268
organismTree::iterator organismTree::append_child(iterator parentNode, string id, string rob, string con, osg::Vec3f pos, osg::Quat quat, short sideChild, short sideParent, short rot)
270
treeNode* node = new treeNode(id, rob, con, pos, quat, sideChild);
272
parentNode.node->addEdge(node, sideChild, sideParent, rot);
274
node->maxDepth = getDepth(parentNode) + 1;
275
treeNode* tmp = node;
276
while(tmp->parent != -1)
278
if(tmp->sides[tmp->parent]->parentNode->maxDepth < tmp->maxDepth)
280
tmp->sides[tmp->parent]->parentNode->maxDepth = tmp->maxDepth;
281
tmp = tmp->sides[tmp->parent]->parentNode;
287
while(feet->children.size()>0)
288
feet = feet->sides[feet->children[feet->children.size()-1]]->childNode;
290
return iterator(node);
293
void organismTree::erase(iterator iter)
295
vector<short> children_copy;
296
bool need_new_feet = false;
298
for(unsigned int i=0; i<iter.node->children.size(); i++)
299
children_copy.push_back(iter.node->children[i]);
301
for(unsigned int i=0; i<children_copy.size(); i++)
302
erase(iterator(iter.node->sides[children_copy[i]]->childNode));
304
if(iter.node->nextSibling) iter.node->nextSibling->prevSibling = iter.node->prevSibling;
305
if(iter.node->prevSibling) iter.node->prevSibling->nextSibling = iter.node->nextSibling;
307
if(iter.node == root) root = NULL;
308
if(iter.node == feet) need_new_feet = true;
311
if(need_new_feet && root != NULL)
314
while(feet->children.size()>0)
315
feet = feet->sides[feet->children[feet->children.size()-1]]->childNode;
319
organismTree::iterator organismTree::begin()
321
return iterator(root);
324
organismTree::iterator organismTree::end()
326
return iterator(feet);
329
osg::Vec3f organismTree::getOrgPos()
331
return root->data.pos;
334
osg::Quat organismTree::getOrgQuat()
336
return root->data.quat;
339
int organismTree::getDepth(iterator iter)
342
treeNode* tr = iter.node;
346
tr = tr->sides[tr->parent]->parentNode;
352
void organismTree::rotate(iterator par, iterator child)
357
rotate(iterator(par.node->sides[par.node->parent]->parentNode), par);
359
for(unsigned int i=0; i<par.node->children.size(); i++)
361
int t = par.node->children[i];
362
if(par.node->sides[t]->childNode == child.node)
364
short sideParent = par.node->sides[t]->sideParent;
365
short sideChild = par.node->sides[t]->sideChild;
366
short rot = par.node->sides[t]->rot;
368
par.node->delEdge(i);
369
child.node->addEdge(par.node, sideParent, sideChild, rot);
370
par.node->parent = sideParent;
371
child.node->nextSibling = NULL;
372
child.node->prevSibling = NULL;
374
if(root == par.node) root = child.node;
375
if(feet == child.node) feet = par.node;
382
cout << "Error: <organismTree.cpp> ::rotate(iter arg1, iter arg2) Error, arg1 is not parent of arg2:" << endl;
386
void organismTree::printTree()
388
iterator iter = begin();
389
bool feet_touched = false;
393
cout << "organismTree: ";
394
if(iter.node->children.size()==0)
395
cout << iter->robotName << "(depth= " << getDepth(iter) << "), (maxDepth= " << iter.node->maxDepth << ") is leaf node. " << endl;
398
cout << iter->robotName << "(depth= " << getDepth(iter) << "), (maxDepth= " << iter.node->maxDepth << ") is parent of: ";
399
for(unsigned int i=0; i<iter.node->children.size(); i++)
401
cout << iter.node->sides[iter.node->children[i]]->childNode->data.robotName;
402
cout << " (" << iter.node->sides[iter.node->children[i]]->sideParent << ", ";
403
cout << iter.node->sides[iter.node->children[i]]->sideChild << "), ";
408
if(iter == feet) feet_touched = true;
413
string organismTree::generateString()
416
bool feet_touched = false;
417
iterator iter = begin();
418
map<int, string> side;
419
map<int, string> rot;
421
side[0] = "A"; rot[0] = "";
422
side[1] = "B"; rot[1] = "1";
423
side[2] = "C"; rot[2] = "2";
424
side[3] = "D"; rot[3] = "3";
426
unambig = iter->terminalID;
427
// cout << "organismTree: " << unambig <<endl;
429
if(iter == this->end()) feet_touched = true;
431
while( !(iter==root && feet_touched) ) {
432
if(iter.node->children.size() != 0) {
433
iter.node = iter.node->sides[iter.node->children[0]]->childNode;
435
unambig = unambig + "[" + side[iter.node->sides[iter.node->parent]->sideParent] + side[iter.node->sides[iter.node->parent]->sideChild];
436
unambig = unambig + rot[iter.node->sides[iter.node->parent]->rot];
437
unambig = unambig + iter->terminalID;
438
// cout << "organismTree: " << unambig <<endl;
441
while(iter.node->nextSibling == NULL) {
442
iter.node = iter.node->sides[iter.node->parent]->parentNode;
443
unambig = unambig + "]";
444
// cout << "organismTree: " << unambig <<endl;
446
if(root==iter.node && feet_touched)
449
iter.node = iter.node->nextSibling;
451
unambig = unambig + "]" + "[" + side[iter.node->sides[iter.node->parent]->sideParent] + side[iter.node->sides[iter.node->parent]->sideChild];
452
unambig = unambig + rot[iter.node->sides[iter.node->parent]->rot];
453
unambig = unambig + iter->terminalID;
454
// cout << "organismTree: " << unambig <<endl;
457
if(iter == this->end()) feet_touched = true;
463
void organismTree::generUnambigTree()
465
typedef map<treeNode*, int, treeNodeComp> mapType1; //container for the nodes of the tree and their index
467
typedef map<string, treeNode*> mapType2; //container for the best strings and the pointer to their root nodes
470
iterator iter = begin();
471
treeNode* root_backup = iter.node; //pointer to the root node
473
bool feet_touched = false;
475
while(!feet_touched) //fill the nodes and their index in the container
478
nodes.insert(std::pair<treeNode*, int>(iter.node, i));
480
if(iter == feet) feet_touched = true;
481
else { ++iter; ++i; }
483
std::cout << "org:feet = " << feet->data.robotName << std::endl;
484
std::cout << "org:map size = " << nodes.size() << std::endl;
486
for(mapType1::iterator it = nodes.begin(); it != nodes.end(); it++) //print the nodes in order
487
cout << "organismTree: map[" << it->first->data.robotName << ", " << it->second << "];" << endl;
489
mapType1::const_iterator it_first = nodes.begin();
490
for(mapType1::const_iterator it = nodes.begin(); it != nodes.end(); ++it) //go through the nodes, starting with the one with hightes order
492
if(it_first->first->isEqual(it->first)) //if the order of a node equals the highest order node
494
if(it->second != 0) //and its not the root node
496
rotate(iterator(it->first->sides[it->first->parent]->parentNode), iterator(it->first)); //rotate the node as root
498
//save the corresponding organismString and the node
499
candidates.insert(std::pair<string, treeNode*>(generateString(), it->first) );
500
rotate(iterator(root_backup->sides[root_backup->parent]->parentNode), iterator(root_backup)); //rotate back in primal state
502
else //if it is the root node no rotation needed
503
// candidates[generateString()] = it->first;
504
candidates.insert(std::pair<string, treeNode*>(generateString(), it->first) );
506
else //the first node with lesser order quits the loop
510
for(mapType2::const_iterator it = candidates.begin(); it != candidates.end(); ++it) //print the strings with highest order nodes as root
511
cout << "organismTree: candidate = " << it->first << endl;
513
treeNode* best = candidates.begin()->second; //generate the final highest order tree
514
if(best->parent != -1) rotate(iterator(best->sides[best->parent]->parentNode), iterator(best));
518
organismTree::iterator::iterator()
523
organismTree::iterator::iterator(treeNode* treeNode)
528
Robot organismTree::iterator::operator*() const
533
Robot* organismTree::iterator::operator->() const
535
return &(node->data);
538
organismTree::iterator& organismTree::iterator::operator++()
540
if(this->node->children.size() != 0)
541
this->node = this->node->sides[this->node->children[0]]->childNode;
544
while(this->node->nextSibling == NULL) {
545
this->node = this->node->sides[this->node->parent]->parentNode;
549
this->node = this->node->nextSibling;
555
organismTree::iterator& organismTree::iterator::operator--()
557
if(this->node->prevSibling) {
558
this->node = this->node->prevSibling;
559
if(this->node->children.size()>0)
560
while(this->node->children.size()>0 && this->node->sides[this->node->children[this->node->children.size()-1]]->childNode)
561
this->node = this->node->sides[this->node->children[this->node->children.size()-1]]->childNode;
564
this->node = this->node->sides[this->node->parent]->parentNode;
571
organismTree::iterator& organismTree::iterator::operator+=(unsigned int num)
580
organismTree::iterator& organismTree::iterator::operator-=(unsigned int num)
589
bool organismTree::iterator::operator==(const iterator& other) const
591
if(this->node == other.node) return true;
595
bool organismTree::iterator::operator!=(const iterator& other) const
597
if(this->node != other.node) return true;