Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "tree.hpp"
00023 #include <sstream>
00024
00025 namespace KDL {
00026 using namespace std;
00027
00028 Tree::Tree(const std::string& _root_name) :
00029 nrOfSegments(0), nrOfJoints(0),root_name(_root_name) {
00030 segments.insert(make_pair(root_name, TreeElement::Root(root_name)));
00031 }
00032
00033 Tree::Tree(const Tree& in) {
00034 segments.clear();
00035 nrOfSegments = 0;
00036 nrOfJoints = 0;
00037 root_name = in.root_name;
00038
00039 segments.insert(make_pair(root_name, TreeElement::Root(root_name)));
00040 this->addTree(in, root_name);
00041 }
00042
00043 Tree& Tree::operator=(const Tree& in) {
00044 segments.clear();
00045 nrOfSegments = 0;
00046 nrOfJoints = 0;
00047 root_name = in.root_name;
00048
00049 segments.insert(make_pair(in.root_name, TreeElement::Root(root_name)));
00050 this->addTree(in, root_name);
00051 return *this;
00052 }
00053
00054 bool Tree::addSegment(const Segment& segment, const std::string& hook_name) {
00055 SegmentMap::iterator parent = segments.find(hook_name);
00056
00057 if (parent == segments.end())
00058 return false;
00059 pair<SegmentMap::iterator, bool> retval;
00060
00061 unsigned int q_nr = segment.getJoint().getType() != Joint::None ? nrOfJoints : 0;
00062 retval = segments.insert(make_pair(segment.getName(), TreeElement(segment, parent, q_nr)));
00063
00064 if (!retval.second)
00065 return false;
00066
00067 parent->second.children.push_back(retval.first);
00068
00069 nrOfSegments++;
00070
00071 if (segment.getJoint().getType() != Joint::None)
00072 nrOfJoints++;
00073 return true;
00074 }
00075
00076 bool Tree::addChain(const Chain& chain, const std::string& hook_name) {
00077 string parent_name = hook_name;
00078 for (unsigned int i = 0; i < chain.getNrOfSegments(); i++) {
00079 if (this->addSegment(chain.getSegment(i), parent_name))
00080 parent_name = chain.getSegment(i).getName();
00081 else
00082 return false;
00083 }
00084 return true;
00085 }
00086
00087 bool Tree::addTree(const Tree& tree, const std::string& hook_name) {
00088 return this->addTreeRecursive(tree.getRootSegment(), hook_name);
00089 }
00090
00091 bool Tree::addTreeRecursive(SegmentMap::const_iterator root, const std::string& hook_name) {
00092
00093 SegmentMap::const_iterator child;
00094
00095 for (unsigned int i = 0; i < root->second.children.size(); i++) {
00096 child = root->second.children[i];
00097
00098 if (this->addSegment(child->second.segment, hook_name)) {
00099
00100 if (!(this->addTreeRecursive(child, child->first)))
00101
00102 return false;
00103 } else
00104
00105 return false;
00106 }
00107 return true;
00108 }
00109
00110 bool Tree::getChain(const std::string& chain_root, const std::string& chain_tip, Chain& chain)const
00111 {
00112
00113 chain = Chain();
00114
00115
00116 vector<SegmentMap::key_type> parents_chain_root, parents_chain_tip;
00117 for (SegmentMap::const_iterator s=getSegment(chain_root); s!=segments.end(); s=s->second.parent){
00118 parents_chain_root.push_back(s->first);
00119 if (s->first == root_name) break;
00120 }
00121 if (parents_chain_root.empty() || parents_chain_root.back() != root_name) return false;
00122 for (SegmentMap::const_iterator s=getSegment(chain_tip); s!=segments.end(); s=s->second.parent){
00123 parents_chain_tip.push_back(s->first);
00124 if (s->first == root_name) break;
00125 }
00126 if (parents_chain_tip.empty() || parents_chain_tip.back() != root_name) return false;
00127
00128
00129 SegmentMap::key_type last_segment = root_name;
00130 while (!parents_chain_root.empty() && !parents_chain_tip.empty() &&
00131 parents_chain_root.back() == parents_chain_tip.back()){
00132 last_segment = parents_chain_root.back();
00133 parents_chain_root.pop_back();
00134 parents_chain_tip.pop_back();
00135 }
00136 parents_chain_root.push_back(last_segment);
00137
00138
00139
00140 for (unsigned int s=0; s<parents_chain_root.size()-1; s++){
00141 Segment seg = getSegment(parents_chain_root[s])->second.segment;
00142 Frame f_tip = seg.pose(0.0).Inverse();
00143 Joint jnt = seg.getJoint();
00144 if (jnt.getType() == Joint::RotX || jnt.getType() == Joint::RotY || jnt.getType() == Joint::RotZ || jnt.getType() == Joint::RotAxis)
00145 jnt = Joint(jnt.getName(), f_tip*jnt.JointOrigin(), f_tip.M*(-jnt.JointAxis()), Joint::RotAxis);
00146 else if (jnt.getType() == Joint::TransX || jnt.getType() == Joint::TransY || jnt.getType() == Joint::TransZ || jnt.getType() == Joint::TransAxis)
00147 jnt = Joint(jnt.getName(),f_tip*jnt.JointOrigin(), f_tip.M*(-jnt.JointAxis()), Joint::TransAxis);
00148 chain.addSegment(Segment(getSegment(parents_chain_root[s+1])->second.segment.getName(),
00149 jnt, f_tip, getSegment(parents_chain_root[s+1])->second.segment.getInertia()));
00150 }
00151
00152
00153 for (int s=parents_chain_tip.size()-1; s>-1; s--){
00154 chain.addSegment(getSegment(parents_chain_tip[s])->second.segment);
00155 }
00156 return true;
00157 }
00158
00159 }
00160
00161