Fawkes API  Fawkes Development Version
navgraph.h
1 
2 /***************************************************************************
3  * navgraph.h - Topological graph
4  *
5  * Created: Fri Sep 21 15:48:00 2012
6  * Copyright 2012 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_H_
24 #define __LIBS_NAVGRAPH_NAVGRAPH_H_
25 
26 #include <navgraph/navgraph_node.h>
27 #include <navgraph/navgraph_edge.h>
28 #include <navgraph/navgraph_path.h>
29 #include <core/utils/lockptr.h>
30 
31 #include <vector>
32 #include <list>
33 #include <string>
34 #include <functional>
35 
36 namespace fawkes {
37 #if 0 /* just to make Emacs auto-indent happy */
38 }
39 #endif
40 
41 namespace navgraph {
42 #if 0 /* just to make Emacs auto-indent happy */
43 }
44 #endif
45  typedef
46  std::function<float (const fawkes::NavGraphNode &, const fawkes::NavGraphNode &)>
47  EstimateFunction;
48  typedef
49  std::function<float (const fawkes::NavGraphNode &, const fawkes::NavGraphNode &)>
50  CostFunction;
51 
52  extern const char *PROP_ORIENTATION;
53 }
54 
55 class NavGraphConstraintRepo;
56 
57 class NavGraph
58 {
59  public:
60  /** Connect mode enum for connect_node_* methods. */
61  typedef enum {
62  CLOSEST_NODE, ///< Connect to closest node
63  CLOSEST_EDGE, ///< Connect to closest edge
64  CLOSEST_EDGE_OR_NODE ///< try to connect to closest edge,
65  ///< if that fails, connect to closest node
67 
68  /** Mode to use to add edges. */
69  typedef enum {
70  EDGE_FORCE, ///< add nodes no matter what (be careful)
71  EDGE_NO_INTERSECTION, ///< Only add edge if it does not intersect
72  ///< with any existing edge
73  EDGE_SPLIT_INTERSECTION ///< Add the edge, but if it intersects with
74  ///< an existing edges add new points at the
75  ///< intersection points for both, the conflicting
76  ///< edges and the new edge
77  } EdgeMode;
78 
79  NavGraph(const std::string &graph_name);
80  virtual ~NavGraph();
81 
82  std::string name() const;
83  const std::vector<NavGraphNode> & nodes() const;
84  const std::vector<NavGraphEdge> & edges() const;
85  fawkes::LockPtr<NavGraphConstraintRepo> constraint_repo() const;
86 
87  const std::map<std::string, std::string> & default_properties() const;
88  bool has_default_property(const std::string &property) const;
89 
90  std::string default_property(const std::string &prop) const;
91  float default_property_as_float(const std::string &prop) const;
92  int default_property_as_int(const std::string &prop) const;
93  bool default_property_as_bool(const std::string &prop) const;
94 
95  void set_default_property(const std::string &property, const std::string &value);
96  void set_default_property(const std::string &property, float value);
97  void set_default_property(const std::string &property, int value);
98  void set_default_property(const std::string &property, bool value);
99  void set_default_properties(const std::map<std::string, std::string> &properties);
100 
101  void apply_default_properties(NavGraphNode &node);
102 
103  NavGraphNode node(const std::string &name) const;
104 
105  NavGraphNode closest_node(float pos_x, float pos_y,
106  const std::string &property = "") const;
107 
108  NavGraphNode closest_node_to(const std::string &node_name,
109  const std::string &property = "") const;
110 
111  NavGraphNode closest_node(float pos_x, float pos_y, bool consider_unconnected,
112  const std::string &property = "") const;
113 
114  NavGraphNode closest_node_to(const std::string &node_name, bool consider_unconnected,
115  const std::string &property = "") const;
116 
117  NavGraphNode closest_node_with_unconnected(float pos_x, float pos_y,
118  const std::string &property = "") const;
119 
120  NavGraphNode closest_node_to_with_unconnected(const std::string &node_name,
121  const std::string &property = "") const;
122 
123  NavGraphEdge edge(const std::string &from, const std::string &to) const;
124  NavGraphEdge closest_edge(float pos_x, float pos_y) const;
125 
126  std::vector<NavGraphNode> search_nodes(const std::string &property) const;
127 
128  std::vector<std::string> reachable_nodes(const std::string &node_name) const;
129 
130  fawkes::NavGraphPath search_path(const std::string &from, const std::string &to,
131  bool use_constraints = true, bool compute_constraints = true);
132 
133  fawkes::NavGraphPath search_path(const std::string &from, const std::string &to,
134  navgraph::EstimateFunction estimate_func,
135  navgraph::CostFunction cost_func,
136  bool use_constraints = true, bool compute_constraints = true);
137 
138  fawkes::NavGraphPath search_path(const NavGraphNode &from,
139  const NavGraphNode &to,
140  bool use_constraints = true, bool compute_constraints = true);
141 
142  fawkes::NavGraphPath search_path(const NavGraphNode &from,
143  const NavGraphNode &to,
144  navgraph::EstimateFunction estimate_func,
145  navgraph::CostFunction cost_func,
146  bool use_constraints = true, bool compute_constraints = true);
147 
148  void add_node(const NavGraphNode &node);
149  void add_node_and_connect(const NavGraphNode &node, ConnectionMode conn_mode);
150  void connect_node_to_closest_node(const NavGraphNode &n);
151  void connect_node_to_closest_edge(const NavGraphNode &n);
152  void add_edge(const NavGraphEdge &edge, EdgeMode mode = EDGE_NO_INTERSECTION,
153  bool allow_existing = false);
154  void remove_node(const NavGraphNode &node);
155  void remove_node(const std::string &node_name);
156  void remove_edge(const NavGraphEdge &edge);
157  void remove_edge(const std::string &from, const std::string &to);
158  void clear();
159 
160  void update_node(const NavGraphNode &node);
161  void update_edge(const NavGraphEdge &edge);
162 
163  bool node_exists(const NavGraphNode &node) const;
164  bool node_exists(const std::string &name) const;
165  bool edge_exists(const NavGraphEdge &edge) const;
166  bool edge_exists(const std::string &from, const std::string &to) const;
167 
168  void calc_reachability(bool allow_multi_graph = false);
169 
170  NavGraph & operator=(const NavGraph &g);
171 
172  void set_notifications_enabled(bool enabled);
173  void notify_of_change() throw();
174 
176  public:
177  virtual ~ChangeListener();
178  virtual void graph_changed() throw() = 0;
179  };
180 
181  void add_change_listener(ChangeListener *listener);
182  void remove_change_listener(ChangeListener *listener);
183 
184 
185  /** Check if the default euclidean distance search is used.
186  * @return true if the default cost and cost estimation functions
187  * are used, false of custom ones have been set.
188  */
189  bool uses_default_search() const
190  { return search_default_funcs_; }
191 
192  void set_search_funcs(navgraph::EstimateFunction estimate_func,
193  navgraph::CostFunction cost_func);
194 
195  void unset_search_funcs();
196 
197  float cost(const NavGraphNode &from, const NavGraphNode &to) const;
198 
199  static std::string format_name(const char *format, ...);
200  std::string gen_unique_name(const char *prefix = "U-");
201 
202  private:
203  void assert_valid_edges();
204  void assert_connected();
205  void edge_add_no_intersection(const NavGraphEdge &edge);
206  void edge_add_split_intersection(const NavGraphEdge &edge);
207 
208  private:
209  std::string graph_name_;
210  std::vector<NavGraphNode> nodes_;
211  std::vector<NavGraphEdge> edges_;
213  std::list<ChangeListener *> change_listeners_;
214  std::map<std::string, std::string> default_properties_;
215 
216  bool search_default_funcs_;
217  navgraph::EstimateFunction search_estimate_func_;
218  navgraph::CostFunction search_cost_func_;
219 
220  bool reachability_calced_;
221 
222 
223  bool notifications_enabled_;
224 };
225 
226 
227 } // end of namespace fawkes
228 
229 #endif
Fawkes library namespace.
Topological map graph.
Definition: navgraph.h:57
Class representing a path for a NavGraph.
Definition: navgraph_path.h:39
Connect to closest node.
Definition: navgraph.h:62
LockPtr<> is a reference-counting shared lockable smartpointer.
Definition: lockptr.h:57
EdgeMode
Mode to use to add edges.
Definition: navgraph.h:69
bool uses_default_search() const
Check if the default euclidean distance search is used.
Definition: navgraph.h:189
add nodes no matter what (be careful)
Definition: navgraph.h:70
Topological graph edge.
Definition: navgraph_edge.h:39
Topological graph node.
Definition: navgraph_node.h:38
Connect to closest edge.
Definition: navgraph.h:63
Topological graph change listener.
Definition: navgraph.h:175
ConnectionMode
Connect mode enum for connect_node_* methods.
Definition: navgraph.h:61