tree.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (C) 2007 Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  2. // Version: 1.0
  3. // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  4. // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
  5. // URL: http://www.orocos.org/kdl
  6. // This library is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU Lesser General Public
  8. // License as published by the Free Software Foundation; either
  9. // version 2.1 of the License, or (at your option) any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #ifndef KDL_TREE_HPP
  18. #define KDL_TREE_HPP
  19. #include "segment.hpp"
  20. #include "chain.hpp"
  21. #include <string>
  22. #include <map>
  23. #include <Eigen/Core>
  24. namespace KDL
  25. {
  26. //Forward declaration
  27. class TreeElement;
  28. // Eigen allocator is needed for alignment of Eigen data types
  29. typedef std::map<std::string,TreeElement, std::less<std::string>, Eigen::aligned_allocator<std::pair<const std::string, TreeElement> > > SegmentMap;
  30. class TreeElement
  31. {
  32. public:
  33. TreeElement():q_nr(0),parent(0)
  34. {};
  35. public:
  36. Segment segment;
  37. unsigned int q_nr;
  38. SegmentMap::value_type const *parent;
  39. std::vector<SegmentMap::const_iterator > children;
  40. TreeElement(const Segment& segment_in,const SegmentMap::value_type& parent_in,unsigned int q_nr_in)
  41. {
  42. q_nr=q_nr_in;
  43. segment=segment_in;
  44. parent=&parent_in;
  45. };
  46. static TreeElement Root()
  47. {
  48. return TreeElement();
  49. };
  50. };
  51. /**
  52. * \brief This class encapsulates a <strong>tree</strong>
  53. * kinematic interconnection structure. It is build out of segments.
  54. *
  55. * @ingroup KinematicFamily
  56. */
  57. class Tree
  58. {
  59. private:
  60. SegmentMap segments;
  61. unsigned int nrOfJoints;
  62. unsigned int nrOfSegments;
  63. bool addTreeRecursive(SegmentMap::const_iterator root, const std::string& tree_name, const std::string& hook_name);
  64. public:
  65. /**
  66. * The constructor of a tree, a new tree is always empty
  67. */
  68. Tree();
  69. Tree(const Tree& in);
  70. Tree& operator= (const Tree& arg);
  71. /**
  72. * Adds a new segment to the end of the segment with
  73. * hook_name as segment_name
  74. *
  75. * @param segment new segment to add
  76. * @param segment_name name of the new segment
  77. * @param hook_name name of the segment to connect this
  78. * segment with.
  79. *
  80. * @return false if hook_name could not be found.
  81. */
  82. bool addSegment(const Segment& segment, const std::string& segment_name, const std::string& hook_name);
  83. /**
  84. * Adds a complete chain to the end of the segment with
  85. * hook_name as segment_name. Segment i of
  86. * the chain will get chain_name+".Segment"+i as segment_name.
  87. *
  88. * @param chain Chain to add
  89. * @param chain_name name of the chain
  90. * @param hook_name name of the segment to connect the chain with.
  91. *
  92. * @return false if hook_name could not be found.
  93. */
  94. bool addChain(const Chain& chain, const std::string& chain_name, const std::string& hook_name);
  95. /**
  96. * Adds a complete tree to the end of the segment with
  97. * hookname as segment_name. The segments of the tree will get
  98. * tree_name+segment_name as segment_name.
  99. *
  100. * @param tree Tree to add
  101. * @param tree_name name of the tree
  102. * @param hook_name name of the segment to connect the tree with
  103. *
  104. * @return false if hook_name could not be found
  105. */
  106. bool addTree(const Tree& tree, const std::string& tree_name,const std::string& hook_name);
  107. /**
  108. * Request the total number of joints in the tree.\n
  109. * <strong> Important:</strong> It is not the same as the
  110. * total number of segments since a segment does not need to have
  111. * a joint.
  112. *
  113. * @return total nr of joints
  114. */
  115. unsigned int getNrOfJoints()const
  116. {
  117. return nrOfJoints;
  118. };
  119. /**
  120. * Request the total number of segments in the tree.
  121. * @return total number of segments
  122. */
  123. unsigned int getNrOfSegments()const {return nrOfSegments;};
  124. /**
  125. * Request the segment of the tree with name segment_name.
  126. *
  127. * @param segment_name the name of the requested segment
  128. *
  129. * @return constant iterator pointing to the requested segment
  130. */
  131. SegmentMap::const_iterator getSegment(const std::string& segment_name)const
  132. {
  133. return segments.find(segment_name);
  134. };
  135. SegmentMap::value_type const* getSegmentPtr(const std::string& segment_name)const
  136. {
  137. SegmentMap::const_iterator it = segments.find(segment_name);
  138. if (it == segments.end())
  139. return 0;
  140. return &*it;
  141. };
  142. const SegmentMap& getSegments()const
  143. {
  144. return segments;
  145. }
  146. virtual ~Tree(){};
  147. };
  148. }
  149. #endif