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 <vector>
29 #include <string>
30 #include <cstdlib>
31 
32 namespace fawkes {
33 #if 0 /* just to make Emacs auto-indent happy */
34 }
35 #endif
36 
37 class NavGraph;
38 
39 class NavGraphPath {
40  public:
41  class Traversal {
42  friend class NavGraphPath;
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 & path() const
64  { return *path_; }
65 
66  /** Check if traversal is initialized.
67  * @return true if traversal is initialized and can be used, false otherwise. */
68  operator bool() const
69  { return path_ != NULL; }
70 
71  private:
72  void assert_initialized() const;
73 
74  private:
75  const NavGraphPath *path_;
76  ssize_t current_;
77  };
78 
79  NavGraphPath();
80  NavGraphPath(const NavGraph *graph, std::vector<NavGraphNode> &nodes, float cost = -1);
81 
82  void add_node(const NavGraphNode &node, float cost_from_end = 0);
83  void set_nodes(const std::vector<NavGraphNode> &nodes, float cost = -1);
84 
85  const NavGraph & graph() const;
86  const NavGraphNode & goal() const;
87 
88  std::string get_path_as_string(const char delim = ':') const;
89  std::vector<std::string> get_node_names() const;
90 
91  /** Get nodes along the path.
92  * @return sequence of nodes that compose the path
93  */
94  const std::vector<NavGraphNode> & nodes() const
95  { return nodes_; }
96 
97  /** Get nodes along the path as mutable vector.
98  * Use this with caution. Modifying the nodes invalidates any
99  * running traversal.
100  * @return sequence of nodes that compose the path
101  */
102  std::vector<NavGraphNode> & nodes_mutable()
103  { return nodes_; }
104 
105  bool contains(const NavGraphNode &node) const;
106 
107  bool empty() const;
108  size_t size() const;
109  void clear();
110 
111  Traversal traversal() const;
112 
113  /** Get cost of path from start to to end.
114  * The cost depends on the metrics used during path search. It's unit
115  * could be arbitrary, for example distance, required travel time, or
116  * some generalized number. Costs are mainly useful for comparison of
117  * paths. But make sure that the very same metrics were used to
118  * generate the path.
119  * @return cost of the path, or a value less than zero if cost is not known
120  */
121  float cost() const
122  { return cost_; }
123 
124  bool operator<(const NavGraphPath &p) const;
125  bool operator==(const NavGraphPath &p) const;
126 
127  private:
128  const NavGraph *graph_;
129  std::vector<NavGraphNode> nodes_;
130  float cost_;
131 
132 };
133 
134 
135 } // end of namespace fawkes
136 
137 #endif
std::vector< NavGraphNode > & nodes_mutable()
Get nodes along the path as mutable vector.
Fawkes library namespace.
Topological map graph.
Definition: navgraph.h:57
Class representing a path for a NavGraph.
Definition: navgraph_path.h:39
float cost() const
Get cost of path from start to to end.
const std::vector< NavGraphNode > & nodes() const
Get nodes along the path.
Definition: navgraph_path.h:94
Sub-class representing a navgraph path traversal.
Definition: navgraph_path.h:41
const NavGraphPath & path() const
Get parent path the traversal belongs to.
Definition: navgraph_path.h:63
Topological graph node.
Definition: navgraph_node.h:38