Tree.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotComponents
19  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl.txt
22  * GNU General Public License
23  */
24 #pragma once
25 
26 #include <deque>
27 #include <vector>
28 
30 
31 #include <RobotComponents/interface/components/MotionPlanning/Tasks/RRTConnect/DataStructures.h>
32 
33 namespace armarx::rrtconnect
34 {
35  struct NodeType
36  {
37  using ConfigType = VectorXf;
38 
40  cfg {std::move(cfg)},
41  parent(parent)
42  {}
43 
45  NodeId parent;
46  };
47 
48  class Tree
49  {
50  public:
51  using ConfigType = VectorXf;
52 
53  Tree(): nodes(1)
54  {
55  }
56 
57  Tree(std::size_t maximalWorkerCount): nodes(maximalWorkerCount)
58  {
59  }
60 
61  /**
62  * @brief Sets the maximal worker count to the given value.
63  * This value has to be set before the tree's useage. and should never be set twice.
64  * @param count The new count.
65  */
66  void setMaximalWorkerCount(std::size_t count)
67  {
68  nodes.resize(count);
69  }
70 
71  void setRoot(const ConfigType& root);
72 
73  const std::deque<std::deque<NodeType>>& getNodes() const
74  {
75  return nodes;
76  }
77 
78  std::vector<ConfigType> getReversedPathTo(NodeId nodeId) const;
79  std::vector<ConfigType> getPathTo(const NodeId& nodeId) const;
80  const NodeType& getNode(const NodeId& nodeId) const
81  {
82  ARMARX_CHECK_EXPRESSION(static_cast<std::size_t>(nodeId.workerId) < nodes.size());
83  ARMARX_CHECK_EXPRESSION(static_cast<std::size_t>(nodeId.nodeSubId) < nodes.at(nodeId.workerId).size());
84  return nodes.at(nodeId.workerId).at(nodeId.nodeSubId);
85  }
86 
87  void addNode(const ConfigType& cfg, const NodeId& parent, std::size_t workerId)
88  {
89  ARMARX_CHECK_EXPRESSION(workerId < nodes.size());
90  nodes.at(workerId).emplace_back(cfg, parent);
91  ++size;
92  }
93 
94  NodeId getNearestNeighbour(const ConfigType& cfg);
95 
96  std::size_t getSize() const
97  {
98  return size;
99  }
100 
101  void applyUpdate(const PerTreeUpdate& u, Ice::Long workerId)
102  {
103  for (const auto& n : u.nodes)
104  {
105  addNode(n.config, n.parent, workerId);
106  }
107  }
108 
109  private:
110  static const NodeId ROOT_NODE_ID;
111  std::deque<std::deque<NodeType>> nodes;
112  std::size_t size = 0;
113  };
114 }
115 
armarx::rrtconnect::NodeType
Definition: Tree.h:35
armarx::rrtconnect::Tree::getPathTo
std::vector< ConfigType > getPathTo(const NodeId &nodeId) const
armarx::rrtconnect::Tree::Tree
Tree(std::size_t maximalWorkerCount)
Definition: Tree.h:57
armarx::rrtconnect::Tree::setRoot
void setRoot(const ConfigType &root)
Definition: Tree.cpp:39
armarx::rrtconnect::Tree::ConfigType
VectorXf ConfigType
Definition: Tree.h:51
armarx::rrtconnect::Tree::getSize
std::size_t getSize() const
Definition: Tree.h:96
armarx::rrtconnect::Tree::Tree
Tree()
Definition: Tree.h:53
armarx::rrtconnect::Tree::getReversedPathTo
std::vector< ConfigType > getReversedPathTo(NodeId nodeId) const
Definition: Tree.cpp:52
armarx::rrtconnect::NodeType::cfg
ConfigType cfg
Definition: Tree.h:44
armarx::rrtconnect::Tree::getNodes
const std::deque< std::deque< NodeType > > & getNodes() const
Definition: Tree.h:73
armarx::rrtconnect::Tree::setMaximalWorkerCount
void setMaximalWorkerCount(std::size_t count)
Sets the maximal worker count to the given value.
Definition: Tree.h:66
armarx::rrtconnect::Tree::getNearestNeighbour
NodeId getNearestNeighbour(const ConfigType &cfg)
Definition: Tree.cpp:72
armarx::rrtconnect::Tree
Definition: Tree.h:48
armarx::rrtconnect
Definition: Task.cpp:29
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::rrtconnect::Tree::getNode
const NodeType & getNode(const NodeId &nodeId) const
Definition: Tree.h:80
ExpressionException.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::rrtconnect::Tree::addNode
void addNode(const ConfigType &cfg, const NodeId &parent, std::size_t workerId)
Definition: Tree.h:87
armarx::rrtconnect::NodeType::parent
NodeId parent
Definition: Tree.h:45
armarx::rrtconnect::NodeType::ConfigType
VectorXf ConfigType
Definition: Tree.h:37
armarx::rrtconnect::NodeType::NodeType
NodeType(ConfigType cfg, NodeId parent)
Definition: Tree.h:39
armarx::rrtconnect::Tree::applyUpdate
void applyUpdate(const PerTreeUpdate &u, Ice::Long workerId)
Definition: Tree.h:101