Tree.cpp
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 
25 #include "Tree.h"
26 
27 #include <algorithm>
28 #include <limits>
29 
31 
32 #include "../../util/Metrics.h"
33 
34 const armarx::rrtconnect::NodeId armarx::rrtconnect::Tree::ROOT_NODE_ID{0, 0};
35 
36 void
38 {
39  ARMARX_CHECK_EXPRESSION(!nodes.empty());
40  if (nodes.at(0).empty())
41  {
42  addNode(root, ROOT_NODE_ID, 0);
43  }
44  else
45  {
46  nodes.at(0).at(0).cfg = root;
47  }
48 }
49 
50 std::vector<armarx::rrtconnect::Tree::ConfigType>
51 armarx::rrtconnect::Tree::getReversedPathTo(armarx::rrtconnect::NodeId nodeId) const
52 {
53  std::vector<ConfigType> path;
54  while (nodeId != ROOT_NODE_ID)
55  {
56  const auto& node = getNode(nodeId);
57  path.emplace_back(node.cfg);
58  nodeId = node.parent;
59  }
60  path.emplace_back(getNode(ROOT_NODE_ID).cfg);
61  return path;
62 }
63 
64 std::vector<armarx::rrtconnect::Tree::ConfigType>
65 armarx::rrtconnect::Tree::getPathTo(const armarx::rrtconnect::NodeId& nodeId) const
66 {
67  std::vector<ConfigType> path = getReversedPathTo(nodeId);
68  std::reverse(path.begin(), path.end());
69  return path;
70 }
71 
72 armarx::rrtconnect::NodeId
74 {
75  float minDistance = std::numeric_limits<float>::infinity();
76  NodeId nearesNode;
77  for (std::size_t workerId = 0; workerId < nodes.size(); ++workerId)
78  {
79  const auto& workerNodes = nodes.at(workerId);
80  for (std::size_t nodeSubId = 0; nodeSubId < workerNodes.size(); ++nodeSubId)
81  {
82  NodeId currentNodeId{static_cast<Ice::Long>(workerId),
83  static_cast<Ice::Long>(nodeSubId)};
84  const auto& node = getNode(currentNodeId);
85  float currentDistance =
86  euclideanDistance(cfg.data(), cfg.data() + cfg.size(), node.cfg.data());
87  if (currentDistance < minDistance)
88  {
89  minDistance = currentDistance;
90  nearesNode = currentNodeId;
91  }
92  }
93  }
94  return nearesNode;
95 }
armarx::rrtconnect::Tree::getPathTo
std::vector< ConfigType > getPathTo(const NodeId &nodeId) const
armarx::rrtconnect::Tree::setRoot
void setRoot(const ConfigType &root)
Definition: Tree.cpp:37
armarx::rrtconnect::Tree::ConfigType
VectorXf ConfigType
Definition: Tree.h:50
Tree.h
armarx::rrtconnect::Tree::getReversedPathTo
std::vector< ConfigType > getReversedPathTo(NodeId nodeId) const
Definition: Tree.cpp:51
armarx::reverse
T reverse(const T &o)
Definition: container.h:33
armarx::rrtconnect::Tree::getNearestNeighbour
NodeId getNearestNeighbour(const ConfigType &cfg)
Definition: Tree.cpp:73
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
ExpressionException.h
armarx::euclideanDistance
float euclideanDistance(IteratorType1 first1, IteratorType1 last1, IteratorType2 first2)
Returns the euclidean distance.
Definition: Metrics.h:104
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:92