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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020