Fawkes API  Fawkes Development Version
navgraph_path.h
1 
2 /***************************************************************************
3  * navgraph_path.h - Topological graph - path
4  *
5  * Created: Mon Jan 12 10:50:52 2015
6  * Copyright 2015 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version. A runtime exception applies to
13  * this software (see LICENSE.GPL_WRE file mentioned below for details).
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
21  */
22 
23 #ifndef _LIBS_NAVGRAPH_NAVGRAPH_PATH_H_
24 #define _LIBS_NAVGRAPH_NAVGRAPH_PATH_H_
25 
26 #include <navgraph/navgraph_node.h>
27 
28 #include <cstdlib>
29 #include <string>
30 #include <vector>
31 
32 namespace fawkes {
33 
34 class NavGraph;
35 
37 {
38 public:
39  class Traversal
40  {
41  friend NavGraphPath;
42 
43  public:
44  Traversal();
45  Traversal(const NavGraphPath *path);
46 
47  const NavGraphNode &current() const;
48  const NavGraphNode &peek_next() const;
49  size_t current_index() const;
50  bool next();
51  void reset();
52  bool last() const;
53  size_t remaining() const;
54  float remaining_cost() const;
55 
56  bool running() const;
57  void invalidate();
58 
59  void set_current(size_t new_current);
60 
61  /** Get parent path the traversal belongs to.
62  * @return parent path */
63  const NavGraphPath &
64  path() const
65  {
66  return *path_;
67  }
68 
69  /** Check if traversal is initialized.
70  * @return true if traversal is initialized and can be used, false otherwise. */
71  operator bool() const
72  {
73  return path_ != NULL;
74  }
75 
76  private:
77  void assert_initialized() const;
78 
79  private:
80  const NavGraphPath *path_;
81  ssize_t current_;
82  };
83 
84  NavGraphPath();
85  NavGraphPath(const NavGraph *graph, std::vector<NavGraphNode> &nodes, float cost = -1);
86 
87  void add_node(const NavGraphNode &node, float cost_from_end = 0);
88  void set_nodes(const std::vector<NavGraphNode> &nodes, float cost = -1);
89 
90  const NavGraph & graph() const;
91  const NavGraphNode &goal() const;
92 
93  std::string get_path_as_string(const char delim = ':') const;
94  std::vector<std::string> get_node_names() const;
95 
96  /** Get nodes along the path.
97  * @return sequence of nodes that compose the path
98  */
99  const std::vector<NavGraphNode> &
100  nodes() const
101  {
102  return nodes_;
103  }
104 
105  /** Get nodes along the path as mutable vector.
106  * Use this with caution. Modifying the nodes invalidates any
107  * running traversal.
108  * @return sequence of nodes that compose the path
109  */
110  std::vector<NavGraphNode> &
112  {
113  return nodes_;
114  }
115 
116  bool contains(const NavGraphNode &node) const;
117 
118  bool empty() const;
119  size_t size() const;
120  void clear();
121 
122  Traversal traversal() const;
123 
124  /** Get cost of path from start to to end.
125  * The cost depends on the metrics used during path search. It's unit
126  * could be arbitrary, for example distance, required travel time, or
127  * some generalized number. Costs are mainly useful for comparison of
128  * paths. But make sure that the very same metrics were used to
129  * generate the path.
130  * @return cost of the path, or a value less than zero if cost is not known
131  */
132  float
133  cost() const
134  {
135  return cost_;
136  }
137 
138  bool operator<(const NavGraphPath &p) const;
139  bool operator==(const NavGraphPath &p) const;
140 
141 private:
142  const NavGraph * graph_;
143  std::vector<NavGraphNode> nodes_;
144  float cost_;
145 };
146 
147 } // end of namespace fawkes
148 
149 #endif
void clear()
Clear all nodes on this path.
size_t size() const
Get size of path.
void reset()
Reset an ongoing traversal.
std::vector< NavGraphNode > & nodes_mutable()
Get nodes along the path as mutable vector.
const NavGraphNode & current() const
Get current node in path.
std::string get_path_as_string(const char delim=':') const
Get string representation of path.
Fawkes library namespace.
bool operator<(const NavGraphPath &p) const
Check if this path is cheaper than the other path.
Topological map graph.
Definition: navgraph.h:49
Class representing a path for a NavGraph.
Definition: navgraph_path.h:36
void set_current(size_t new_current)
Set the current node.
void set_nodes(const std::vector< NavGraphNode > &nodes, float cost=-1)
Set nodes erasing the current path.
float cost() const
Get cost of path from start to to end.
bool last() const
Check if the current node is the last node in the path.
std::vector< std::string > get_node_names() const
Get names of nodes in path.
const NavGraphNode & peek_next() const
Peek on the next node.
size_t remaining() const
Get the number of remaining nodes in path traversal.
const NavGraph & graph() const
Get graph this path is based on.
const std::vector< NavGraphNode > & nodes() const
Get nodes along the path.
Traversal traversal() const
Get a new path traversal handle.
Sub-class representing a navgraph path traversal.
Definition: navgraph_path.h:39
bool running() const
Check if traversal is currently runnung.
const NavGraphPath & path() const
Get parent path the traversal belongs to.
Definition: navgraph_path.h:64
bool empty() const
Check if path is empty.
size_t current_index() const
Get index of current node in path.
Topological graph node.
Definition: navgraph_node.h:35
float remaining_cost() const
Get the remaining cost to the goal.
bool operator==(const NavGraphPath &p) const
Check if two paths are the same.
void invalidate()
Invalidate this traversal.
void add_node(const NavGraphNode &node, float cost_from_end=0)
Add a node to the path.
bool contains(const NavGraphNode &node) const
Check if the path contains a given node.
bool next()
Move on traversal to next node.
NavGraphPath()
Default constructor.
const NavGraphNode & goal() const
Get goal of path.