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
32namespace fawkes {
33
34class NavGraph;
35
37{
38public:
40 {
41 friend NavGraphPath;
42
43 public:
44 Traversal();
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
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
141private:
142 const NavGraph * graph_;
143 std::vector<NavGraphNode> nodes_;
144 float cost_;
145};
146
147} // end of namespace fawkes
148
149#endif
Topological graph node.
Definition: navgraph_node.h:36
Sub-class representing a navgraph path traversal.
Definition: navgraph_path.h:40
bool last() const
Check if the current node is the last node in the path.
float remaining_cost() const
Get the remaining cost to the goal.
const NavGraphPath & path() const
Get parent path the traversal belongs to.
Definition: navgraph_path.h:64
size_t current_index() const
Get index of current node in path.
void invalidate()
Invalidate this traversal.
const NavGraphNode & current() const
Get current node in path.
bool next()
Move on traversal to next node.
size_t remaining() const
Get the number of remaining nodes in path traversal.
bool running() const
Check if traversal is currently runnung.
void set_current(size_t new_current)
Set the current node.
void reset()
Reset an ongoing traversal.
const NavGraphNode & peek_next() const
Peek on the next node.
Class representing a path for a NavGraph.
Definition: navgraph_path.h:37
std::vector< std::string > get_node_names() const
Get names of nodes in path.
Traversal traversal() const
Get a new path traversal handle.
bool empty() const
Check if path is empty.
bool operator<(const NavGraphPath &p) const
Check if this path is cheaper than the other path.
bool operator==(const NavGraphPath &p) const
Check if two paths are the same.
NavGraphPath()
Default constructor.
size_t size() const
Get size of path.
void clear()
Clear all nodes on this path.
const std::vector< NavGraphNode > & nodes() const
Get nodes along the path.
const NavGraph & graph() const
Get graph this path is based on.
float cost() const
Get cost of path from start to to end.
bool contains(const NavGraphNode &node) const
Check if the path contains a given node.
const NavGraphNode & goal() const
Get goal of path.
std::string get_path_as_string(const char delim=':') const
Get string representation of path.
void add_node(const NavGraphNode &node, float cost_from_end=0)
Add a node to the path.
void set_nodes(const std::vector< NavGraphNode > &nodes, float cost=-1)
Set nodes erasing the current path.
std::vector< NavGraphNode > & nodes_mutable()
Get nodes along the path as mutable vector.
Topological map graph.
Definition: navgraph.h:50
Fawkes library namespace.