Main MRPT website > C++ reference for MRPT 1.9.9
TMoveTree.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
13 #include <mrpt/utils/traits_map.h>
14 #include <mrpt/math/wrap2pi.h>
15 #include <mrpt/poses/CPose2D.h>
16 
18 
19 namespace mrpt
20 {
21 namespace nav
22 {
23 /** \addtogroup nav_planners Path planning
24  * \ingroup mrpt_nav_grp
25  * @{ */
26 
27 /** Generic base for metrics */
28 template <class node_t>
30 
31 /** This class contains motions and motions tree structures for the hybrid
32 * navigation algorithm
33 *
34 * <b>Usage:</b><br>
35 * \note: this class inheredit mrpt::graphs::CDirectedTree, please refer to
36 * inheritance for detail about generic tree methods
37 *
38 * - initialize a motions tree using .initializeMoveTree()
39 * - addEdge (from, to)
40 * - add here more instructions
41 *
42 *
43 * <b>Changes history</b>
44 * - 06/MAR/2014: Creation (MB)
45 * - 21/JAN/2015: Refactoring (JLBC)
46 */
47 template <class NODE_TYPE_DATA, class EDGE_TYPE,
48  class MAPS_IMPLEMENTATION = mrpt::utils::
49  map_traits_map_as_vector // Use std::map<> vs. std::vector<>
50  >
51 class TMoveTree : public mrpt::graphs::CDirectedTree<EDGE_TYPE>
52 {
53  public:
54  struct node_t : public NODE_TYPE_DATA
55  {
56  /** Duplicated ID (it's also in the map::iterator->first), but put here
57  * to make it available in path_t */
59  /** INVALID_NODEID for the root, a valid ID otherwise */
61  /** NULL for root, a valid edge otherwise */
62  EDGE_TYPE* edge_to_parent;
64  mrpt::utils::TNodeID node_id_, mrpt::utils::TNodeID parent_id_,
65  EDGE_TYPE* edge_to_parent_, const NODE_TYPE_DATA& data)
66  : NODE_TYPE_DATA(data),
67  node_id(node_id_),
68  parent_id(parent_id_),
69  edge_to_parent(edge_to_parent_)
70  {
71  }
72  node_t() {}
73  };
74 
76  typedef EDGE_TYPE edge_t;
77  /** Map: TNode_ID => Node info */
78  typedef
79  typename MAPS_IMPLEMENTATION::template map<mrpt::utils::TNodeID, node_t>
81  /** A topological path up-tree */
82  typedef std::list<node_t> path_t;
83 
84  /** Finds the nearest node to a given pose, using the given metric */
85  template <class NODE_TYPE_FOR_METRIC>
87  const NODE_TYPE_FOR_METRIC& query_pt,
88  const PoseDistanceMetric<NODE_TYPE_FOR_METRIC>& distanceMetricEvaluator,
89  double* out_distance = NULL,
90  const std::set<mrpt::utils::TNodeID>* ignored_nodes = NULL) const
91  {
92  ASSERT_(!m_nodes.empty())
93 
94  double min_d = std::numeric_limits<double>::max();
96  for (typename node_map_t::const_iterator it = m_nodes.begin();
97  it != m_nodes.end(); ++it)
98  {
99  if (ignored_nodes &&
100  ignored_nodes->find(it->first) != ignored_nodes->end())
101  continue; // ignore it
102  const NODE_TYPE_FOR_METRIC ptTo(query_pt.state);
103  const NODE_TYPE_FOR_METRIC ptFrom(it->second.state);
104  if (distanceMetricEvaluator.cannotBeNearerThan(ptFrom, ptTo, min_d))
105  continue; // Skip the more expensive calculation of exact
106  // distance
107  double d = distanceMetricEvaluator.distance(ptFrom, ptTo);
108  if (d < min_d)
109  {
110  min_d = d;
111  min_id = it->first;
112  }
113  }
114  if (out_distance) *out_distance = min_d;
115  return min_id;
116  }
117 
119  const mrpt::utils::TNodeID parent_id,
120  const mrpt::utils::TNodeID new_child_id,
121  const NODE_TYPE_DATA& new_child_node_data,
122  const EDGE_TYPE& new_edge_data)
123  {
124  // edge:
125  typename base_t::TListEdges& edges_of_parent =
126  base_t::edges_to_children[parent_id];
127  edges_of_parent.push_back(
128  typename base_t::TEdgeInfo(
129  new_child_id, false /*direction_child_to_parent*/,
130  new_edge_data));
131  // node:
132  m_nodes[new_child_id] = node_t(
133  new_child_id, parent_id, &edges_of_parent.back().data,
134  new_child_node_data);
135  }
136 
137  /** Insert a node without edges (should be used only for a tree root node)
138  */
140  const mrpt::utils::TNodeID node_id, const NODE_TYPE_DATA& node_data)
141  {
142  m_nodes[node_id] = node_t(node_id, INVALID_NODEID, NULL, node_data);
143  }
144 
145  mrpt::utils::TNodeID getNextFreeNodeID() const { return m_nodes.size(); }
146  const node_map_t& getAllNodes() const { return m_nodes; }
147  /** Builds the path (sequence of nodes, with info about next edge) up-tree
148  * from a `target_node` towards the root
149  * Nodes are ordered in the direction ROOT -> start_node
150  */
152  const mrpt::utils::TNodeID target_node, path_t& out_path) const
153  {
154  out_path.clear();
155  typename node_map_t::const_iterator it_src = m_nodes.find(target_node);
156  if (it_src == m_nodes.end())
157  throw std::runtime_error(
158  "backtrackPath: target_node not found in tree!");
159  const node_t* node = &it_src->second;
160  for (;;)
161  {
162  out_path.push_front(*node);
163 
164  mrpt::utils::TNodeID next_node_id = node->parent_id;
165  if (next_node_id == INVALID_NODEID)
166  {
167  break; // finished
168  }
169  else
170  {
171  typename node_map_t::const_iterator it_next =
172  m_nodes.find(next_node_id);
173  if (it_next == m_nodes.end())
174  throw std::runtime_error(
175  "backtrackPath: Node ID not found during tree "
176  "traversal!");
177  node = &it_next->second;
178  }
179  }
180  }
181 
182  private:
183  /** Info per node */
185 
186 }; // end TMoveTree
187 
188 /** An edge for the move tree used for planning in SE2 and TP-space */
190 {
191  /** The ID of the parent node in the tree */
193  /** state in SE2 as 2D pose (x, y, phi) - \note: it is not possible to
194  * initialize a motion without a state */
196  /** cost associated to each motion, this should be defined by the user
197  * according to a spefic cost function */
198  double cost;
199  /** indicate the type of trajectory used for this motion */
201  /** identify the trajectory number K of the type ptg_index */
202  int ptg_K;
203  /** identify the lenght of the trajectory for this motion */
204  double ptg_dist;
205 
207  const mrpt::utils::TNodeID parent_id_,
208  const mrpt::math::TPose2D end_pose_)
209  : parent_id(parent_id_),
210  end_state(end_pose_),
211  cost(0.0),
212  ptg_index(0),
213  ptg_K(0),
214  ptg_dist(0.0) // these are all PTGs parameters
215  {
216  }
218 };
219 
220 struct TNodeSE2
221 {
222  /** state in SE2 as 2D pose (x, y, phi) */
224  TNodeSE2(const mrpt::math::TPose2D& state_) : state(state_) {}
225  TNodeSE2() {}
226 };
227 
228 /** Pose metric for SE(2) */
229 template <>
231 {
233  const TNodeSE2& a, const TNodeSE2& b, const double d) const
234  {
235  if (std::abs(a.state.x - b.state.x) > d) return true;
236  if (std::abs(a.state.y - b.state.y) > d) return true;
237  return false;
238  }
239 
240  double distance(const TNodeSE2& a, const TNodeSE2& b) const
241  {
242  return mrpt::math::square(a.state.x - b.state.x) +
243  mrpt::math::square(a.state.y - b.state.y) +
245  mrpt::math::angDistance(a.state.phi, b.state.phi));
246  }
248 };
249 
251 {
252  /** state in SE2 as 2D pose (x, y, phi) */
254  TNodeSE2_TP(const mrpt::math::TPose2D& state_) : state(state_) {}
256 };
257 
258 /** Pose metric for SE(2) limited to a given PTG manifold. NOTE: This 'metric'
259  * is NOT symmetric for all PTGs: d(a,b)!=d(b,a) */
260 template <>
262 {
264  const TNodeSE2_TP& a, const TNodeSE2_TP& b, const double d) const
265  {
266  if (std::abs(a.state.x - b.state.x) > d) return true;
267  if (std::abs(a.state.y - b.state.y) > d) return true;
268  return false;
269  }
270  double distance(const TNodeSE2_TP& src, const TNodeSE2_TP& dst) const
271  {
272  double d;
273  int k;
275  relPose.inverseComposeFrom(
277  bool tp_point_is_exact =
278  m_ptg.inverseMap_WS2TP(relPose.x(), relPose.y(), k, d);
279  if (tp_point_is_exact)
280  return d * m_ptg.getRefDistance(); // de-normalize distance
281  else
282  return std::numeric_limits<double>::max(); // not in range: we
283  // can't evaluate this
284  // distance!
285  }
287  : m_ptg(ptg)
288  {
289  }
290 
291  private:
293 };
294 
295 // typedef TMoveTree<TNodeSE2 ,TMoveEdgeSE2> TMoveTreeSE2_TP; //!< tree data
296 // structure for planning in SE2
297 /** tree data structure for planning in SE2 within TP-Space manifolds */
299 
300 /** @} */
301 }
302 }
mrpt::utils::TNodeID parent_id
The ID of the parent node in the tree.
Definition: TMoveTree.h:192
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
TMapNode2ListEdges edges_to_children
The edges of each node.
Definition: CDirectedTree.h:86
mrpt::math::TPose2D state
state in SE2 as 2D pose (x, y, phi)
Definition: TMoveTree.h:253
node_t(mrpt::utils::TNodeID node_id_, mrpt::utils::TNodeID parent_id_, EDGE_TYPE *edge_to_parent_, const NODE_TYPE_DATA &data)
Definition: TMoveTree.h:63
A special kind of graph in the form of a tree with directed edges and optional edge annotations of te...
Definition: CDirectedTree.h:56
TMoveTree< TNodeSE2_TP, TMoveEdgeSE2_TP > TMoveTreeSE2_TP
tree data structure for planning in SE2 within TP-Space manifolds
Definition: TMoveTree.h:298
const mrpt::nav::CParameterizedTrajectoryGenerator & m_ptg
Definition: TMoveTree.h:292
TNodeSE2_TP(const mrpt::math::TPose2D &state_)
Definition: TMoveTree.h:254
TNodeSE2(const mrpt::math::TPose2D &state_)
Definition: TMoveTree.h:224
T angDistance(T from, T to)
Computes the shortest angular increment (or distance) between two planar orientations, such that it is constrained to [-pi,pi] and is correct for any combination of angles (e.g.
Definition: wrap2pi.h:98
#define INVALID_NODEID
std::list< node_t > path_t
A topological path up-tree.
Definition: TMoveTree.h:82
MAPS_IMPLEMENTATION::template map< mrpt::utils::TNodeID, node_t > node_map_t
Map: TNode_ID => Node info.
Definition: TMoveTree.h:80
const Scalar * const_iterator
Definition: eigen_plugins.h:27
const node_map_t & getAllNodes() const
Definition: TMoveTree.h:146
GLuint src
Definition: glext.h:7278
int ptg_index
indicate the type of trajectory used for this motion
Definition: TMoveTree.h:200
bool cannotBeNearerThan(const TNodeSE2 &a, const TNodeSE2 &b, const double d) const
Definition: TMoveTree.h:232
Generic base for metrics.
Definition: TMoveTree.h:29
int ptg_K
identify the trajectory number K of the type ptg_index
Definition: TMoveTree.h:202
double cost
cost associated to each motion, this should be defined by the user according to a spefic cost functio...
Definition: TMoveTree.h:198
EDGE_TYPE edge_t
Definition: TMoveTree.h:76
mrpt::graphs::CDirectedTree< EDGE_TYPE > base_t
Definition: TMoveTree.h:75
mrpt::utils::TNodeID node_id
Duplicated ID (it&#39;s also in the map::iterator->first), but put here to make it available in path_t...
Definition: TMoveTree.h:58
This is the base class for any user-defined PTG.
GLuint dst
Definition: glext.h:7135
uint64_t TNodeID
The type for node IDs in graphs of different types.
TMoveEdgeSE2_TP(const mrpt::utils::TNodeID parent_id_, const mrpt::math::TPose2D end_pose_)
Definition: TMoveTree.h:206
double distance(const TNodeSE2_TP &src, const TNodeSE2_TP &dst) const
Definition: TMoveTree.h:270
void insertNode(const mrpt::utils::TNodeID node_id, const NODE_TYPE_DATA &node_data)
Insert a node without edges (should be used only for a tree root node)
Definition: TMoveTree.h:139
GLubyte GLubyte b
Definition: glext.h:6279
This class contains motions and motions tree structures for the hybrid navigation algorithm...
Definition: TMoveTree.h:51
mrpt::utils::TNodeID getNearestNode(const NODE_TYPE_FOR_METRIC &query_pt, const PoseDistanceMetric< NODE_TYPE_FOR_METRIC > &distanceMetricEvaluator, double *out_distance=NULL, const std::set< mrpt::utils::TNodeID > *ignored_nodes=NULL) const
Finds the nearest node to a given pose, using the given metric.
Definition: TMoveTree.h:86
void insertNodeAndEdge(const mrpt::utils::TNodeID parent_id, const mrpt::utils::TNodeID new_child_id, const NODE_TYPE_DATA &new_child_node_data, const EDGE_TYPE &new_edge_data)
Definition: TMoveTree.h:118
void backtrackPath(const mrpt::utils::TNodeID target_node, path_t &out_path) const
Builds the path (sequence of nodes, with info about next edge) up-tree from a target_node towards the...
Definition: TMoveTree.h:151
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
mrpt::math::TPose2D state
state in SE2 as 2D pose (x, y, phi)
Definition: TMoveTree.h:223
PoseDistanceMetric(const mrpt::nav::CParameterizedTrajectoryGenerator &ptg)
Definition: TMoveTree.h:286
double distance(const TNodeSE2 &a, const TNodeSE2 &b) const
Definition: TMoveTree.h:240
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
T square(const T x)
Inline function for the square of a number.
mrpt::utils::TNodeID parent_id
INVALID_NODEID for the root, a valid ID otherwise.
Definition: TMoveTree.h:60
Lightweight 2D pose.
mrpt::utils::TNodeID getNextFreeNodeID() const
Definition: TMoveTree.h:145
EDGE_TYPE * edge_to_parent
NULL for root, a valid edge otherwise.
Definition: TMoveTree.h:62
#define ASSERT_(f)
double ptg_dist
identify the lenght of the trajectory for this motion
Definition: TMoveTree.h:204
An edge for the move tree used for planning in SE2 and TP-space.
Definition: TMoveTree.h:189
mrpt::math::TPose2D end_state
state in SE2 as 2D pose (x, y, phi) -
Definition: TMoveTree.h:195
void inverseComposeFrom(const CPose2D &A, const CPose2D &B)
Makes this method is slightly more efficient than "this= A - B;" since it avoids the temporary objec...
Definition: CPose2D.cpp:247
node_map_t m_nodes
Info per node.
Definition: TMoveTree.h:184
bool cannotBeNearerThan(const TNodeSE2_TP &a, const TNodeSE2_TP &b, const double d) const
Definition: TMoveTree.h:263
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
GLubyte GLubyte GLubyte a
Definition: glext.h:6279



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019