Blender V2.61 - r43446

tree.cpp

Go to the documentation of this file.
00001 
00004 // Copyright  (C)  2007  Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00005 
00006 // Version: 1.0
00007 // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00008 // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00009 // URL: http://www.orocos.org/kdl
00010 
00011 // This library is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU Lesser General Public
00013 // License as published by the Free Software Foundation; either
00014 // version 2.1 of the License, or (at your option) any later version.
00015 
00016 // This library is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00024 
00025 #include "tree.hpp"
00026 #include <sstream>
00027 namespace KDL {
00028 using namespace std;
00029 
00030 Tree::Tree() :
00031     nrOfJoints(0), nrOfSegments(0) {
00032     segments.insert(make_pair("root", TreeElement::Root()));
00033 }
00034 
00035 Tree::Tree(const Tree& in) {
00036     segments.clear();
00037     nrOfSegments = 0;
00038     nrOfJoints = 0;
00039 
00040     segments.insert(make_pair("root", TreeElement::Root()));
00041     this->addTree(in, "", "root");
00042 
00043 }
00044 
00045 Tree& Tree::operator=(const Tree& in) {
00046     segments.clear();
00047     nrOfSegments = 0;
00048     nrOfJoints = 0;
00049 
00050     segments.insert(make_pair("root", TreeElement::Root()));
00051     this->addTree(in, "", "root");
00052     return *this;
00053 }
00054 
00055 bool Tree::addSegment(const Segment& segment, const std::string& segment_name,
00056         const std::string& hook_name) {
00057     SegmentMap::iterator parent = segments.find(hook_name);
00058     //check if parent exists
00059     if (parent == segments.end())
00060         return false;
00061     pair<SegmentMap::iterator, bool> retval;
00062     //insert new element
00063     retval = segments.insert(make_pair(segment_name, TreeElement(segment,
00064             parent, nrOfJoints)));
00065     //check if insertion succeeded
00066     if (!retval.second)
00067         return false;
00068     //add iterator to new element in parents children list
00069     parent->second.children.push_back(retval.first);
00070     //increase number of segments
00071     nrOfSegments++;
00072     //increase number of joints
00073     nrOfJoints += segment.getJoint().getNDof();
00074     return true;
00075 }
00076 
00077 bool Tree::addChain(const Chain& chain, const std::string& chain_name,
00078         const std::string& hook_name) {
00079     string parent_name = hook_name;
00080     for (unsigned int i = 0; i < chain.getNrOfSegments(); i++) {
00081         ostringstream segment_name;
00082         segment_name << chain_name << "Segment" << i;
00083         if (this->addSegment(chain.getSegment(i), segment_name.str(),
00084                 parent_name))
00085             parent_name = segment_name.str();
00086         else
00087             return false;
00088     }
00089     return true;
00090 }
00091 
00092 bool Tree::addTree(const Tree& tree, const std::string& tree_name,
00093         const std::string& hook_name) {
00094     return this->addTreeRecursive(tree.getSegment("root"), tree_name, hook_name);
00095 }
00096 
00097 bool Tree::addTreeRecursive(SegmentMap::const_iterator root,
00098         const std::string& tree_name, const std::string& hook_name) {
00099     //get iterator for root-segment
00100     SegmentMap::const_iterator child;
00101     //try to add all of root's children
00102     for (unsigned int i = 0; i < root->second.children.size(); i++) {
00103         child = root->second.children[i];
00104         //Try to add the child
00105         if (this->addSegment(child->second.segment, tree_name + child->first,
00106                 hook_name)) {
00107             //if child is added, add all the child's children
00108             if (!(this->addTreeRecursive(child, tree_name, tree_name
00109                     + child->first)))
00110                 //if it didn't work, return false
00111                 return false;
00112         } else
00113             //If the child could not be added, return false
00114             return false;
00115     }
00116     return true;
00117 }
00118 
00119 }
00120