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