tree.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /** \file itasc/kdl/tree.cpp
  2. * \ingroup itasc
  3. */
  4. // Copyright (C) 2007 Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  5. // Version: 1.0
  6. // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  7. // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  8. // URL: http://www.orocos.org/kdl
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Lesser General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2.1 of the License, or (at your option) any later version.
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. // Lesser General Public License for more details.
  17. // You should have received a copy of the GNU Lesser General Public
  18. // License along with this library; if not, write to the Free Software
  19. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #include "tree.hpp"
  21. #include <sstream>
  22. namespace KDL {
  23. using namespace std;
  24. Tree::Tree() :
  25. nrOfJoints(0), nrOfSegments(0) {
  26. TreeElement root;
  27. std::pair<std::string, TreeElement> val("root", root);
  28. segments.insert(val);
  29. }
  30. Tree::Tree(const Tree& in) {
  31. segments.clear();
  32. nrOfSegments = 0;
  33. nrOfJoints = 0;
  34. TreeElement root;
  35. std::pair<std::string, TreeElement> val("root", root);
  36. segments.insert(val);
  37. this->addTree(in, "", "root");
  38. }
  39. Tree& Tree::operator=(const Tree& in) {
  40. segments.clear();
  41. nrOfSegments = 0;
  42. nrOfJoints = 0;
  43. TreeElement root;
  44. std::pair<std::string, TreeElement> val("root", root);
  45. segments.insert(val);
  46. this->addTree(in, "", "root");
  47. return *this;
  48. }
  49. bool Tree::addSegment(const Segment& segment, const std::string& segment_name,
  50. const std::string& hook_name) {
  51. SegmentMap::iterator parent = segments.find(hook_name);
  52. //check if parent exists
  53. if (parent == segments.end())
  54. return false;
  55. pair<SegmentMap::iterator, bool> retval;
  56. //insert new element
  57. TreeElement elem(segment, *parent, nrOfJoints);
  58. std::pair<std::string, TreeElement> val(segment_name, elem);
  59. retval = segments.insert(val);
  60. //check if insertion succeeded
  61. if (!retval.second)
  62. return false;
  63. //add iterator to new element in parents children list
  64. parent->second.children.push_back(retval.first);
  65. //increase number of segments
  66. nrOfSegments++;
  67. //increase number of joints
  68. nrOfJoints += segment.getJoint().getNDof();
  69. return true;
  70. }
  71. bool Tree::addChain(const Chain& chain, const std::string& chain_name,
  72. const std::string& hook_name) {
  73. string parent_name = hook_name;
  74. for (unsigned int i = 0; i < chain.getNrOfSegments(); i++) {
  75. ostringstream segment_name;
  76. segment_name << chain_name << "Segment" << i;
  77. if (this->addSegment(chain.getSegment(i), segment_name.str(),
  78. parent_name))
  79. parent_name = segment_name.str();
  80. else
  81. return false;
  82. }
  83. return true;
  84. }
  85. bool Tree::addTree(const Tree& tree, const std::string& tree_name,
  86. const std::string& hook_name) {
  87. return this->addTreeRecursive(tree.getSegment("root"), tree_name, hook_name);
  88. }
  89. bool Tree::addTreeRecursive(SegmentMap::const_iterator root,
  90. const std::string& tree_name, const std::string& hook_name) {
  91. //get iterator for root-segment
  92. SegmentMap::const_iterator child;
  93. //try to add all of root's children
  94. for (unsigned int i = 0; i < root->second.children.size(); i++) {
  95. child = root->second.children[i];
  96. //Try to add the child
  97. if (this->addSegment(child->second.segment, tree_name + child->first,
  98. hook_name)) {
  99. //if child is added, add all the child's children
  100. if (!(this->addTreeRecursive(child, tree_name, tree_name
  101. + child->first)))
  102. //if it didn't work, return false
  103. return false;
  104. } else
  105. //If the child could not be added, return false
  106. return false;
  107. }
  108. return true;
  109. }
  110. }