Fawkes API  Fawkes Development Version
navgraph_node.h
1 
2 /***************************************************************************
3  * navgraph_node.h - Topological graph node
4  *
5  * Created: Fri Sep 21 16:01:26 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 __UTILS_GRAPH_TOPOLOGICAL_MAP_NODE_H_
24 #define __UTILS_GRAPH_TOPOLOGICAL_MAP_NODE_H_
25 
26 #include <utils/misc/string_conversions.h>
27 
28 #include <map>
29 #include <vector>
30 #include <string>
31 #include <cmath>
32 
33 namespace fawkes {
34 #if 0 /* just to make Emacs auto-indent happy */
35 }
36 #endif
37 
38 class NavGraphNode {
39  public:
40  NavGraphNode();
41 
42  NavGraphNode(const std::string &name, float x, float y,
43  std::map<std::string, std::string> properties);
44 
45  NavGraphNode(const std::string &name, float x, float y);
46 
47  /** Get name of node.
48  * @return name of node */
49  const std::string & name() const
50  { return name_; }
51 
52  /** Get X coordinate in global frame.
53  * @return X coordinate in global frame */
54  float x() const
55  { return x_; }
56 
57  /** Get Y coordinate in global frame.
58  * @return Y coordinate in global frame */
59  float y() const
60  { return y_; }
61 
62  /** Check if this node shall be unconnected.
63  * @return true if the node is unconnected, false otherwise */
64  bool unconnected() const
65  { return unconnected_; }
66 
67  void set_x(float x);
68  void set_y(float y);
69  void set_name(const std::string &name);
70  void set_unconnected(bool unconnected);
71 
72  /** Get euclidean distance from this node to another.
73  * @param n node to get distance to
74  * @return distance */
75  float distance(const NavGraphNode &n)
76  { return sqrtf(powf(x_ - n.x_, 2) + powf(y_ - n.y_, 2)); }
77 
78  /** Get euclidean distance from this node to a point.
79  * @param x point X coordinate
80  * @param y point Y coordinate
81  * @return distance */
82  float distance(float x, float y)
83  { return sqrtf(powf(x_ - x, 2) + powf(y_ - y, 2)); }
84 
85  /** Get all properties.
86  * @return property map
87  */
88  const std::map<std::string, std::string> & properties() const
89  { return properties_; }
90 
91  /** Check if node has specified property.
92  * @param property property key
93  * @return true if node has specified property, false otherwise
94  */
95  bool has_property(const std::string &property) const
96  { return properties_.find(property) != properties_.end(); }
97 
98  /** Check if node is valid, i.e. it has a name.
99  * @return true if node is valid, false otherwise
100  */
101  bool is_valid() const
102  { return name_ != ""; }
103 
104  void set_properties(const std::map<std::string, std::string> &properties);
105  void set_property(const std::string &property, const std::string &value);
106  void set_property(const std::string &property, float value);
107  void set_property(const std::string &property, int value);
108  void set_property(const std::string &property, bool value);
109 
110  std::string property(const std::string &prop) const;
111 
112  /** Get property converted to float.
113  * @param prop property key
114  * @return property value
115  */
116  float property_as_float(const std::string &prop) const
117  { return StringConversions::to_float(property(prop)); }
118 
119  /** Get property converted to int.
120  * @param prop property key
121  * @return property value
122  */
123  int property_as_int(const std::string &prop) const
124  { return StringConversions::to_int(property(prop)); }
125 
126  /** Get property converted to bol.
127  * @param prop property key
128  * @return property value
129  */
130  bool property_as_bool(const std::string &prop) const
131  { return StringConversions::to_bool(property(prop)); }
132 
133  /** Check nodes for equality.
134  * Nodes are equal if they have the same name.
135  * @param n node to compare with
136  * @return true if the node is the same as this one, false otherwise
137  */
138  bool operator==(const NavGraphNode &n) const
139  { return name_ == n.name_; }
140 
141  /** Check nodes for inequality.
142  * Nodes are inequal if they have different names.
143  * @param n node to compare with
144  * @return true if the node is different from this one, false otherwise
145  */
146  bool operator!=(const NavGraphNode &n) const
147  { return name_ != n.name_; }
148 
149  /** Check if node is valid.
150  * A node is valid if it has a name set.
151  * @return true if the node is valid, false otherwise
152  */
153  operator bool() const
154  { return name_ != ""; }
155 
156  void set_reachable_nodes(std::vector<std::string> reachable_nodes);
157 
158  /** Get reachable nodes.
159  * @return vector of directly reachable nodes.
160  */
161  const std::vector<std::string> & reachable_nodes() const
162  { return reachable_nodes_; }
163 
164  private:
165  std::string name_;
166  float x_;
167  float y_;
168  bool unconnected_;
169  std::map<std::string, std::string> properties_;
170  std::vector<std::string> reachable_nodes_;
171 };
172 
173 
174 } // end of namespace fawkes
175 
176 #endif
float distance(const NavGraphNode &n)
Get euclidean distance from this node to another.
Definition: navgraph_node.h:75
void set_properties(const std::map< std::string, std::string > &properties)
Overwrite properties with given ones.
int property_as_int(const std::string &prop) const
Get property converted to int.
Fawkes library namespace.
void set_unconnected(bool unconnected)
Set unconnected state of the node.
void set_y(float y)
Set Y position.
std::string property(const std::string &prop) const
Get specified property as string.
bool has_property(const std::string &property) const
Check if node has specified property.
Definition: navgraph_node.h:95
bool operator!=(const NavGraphNode &n) const
Check nodes for inequality.
bool is_valid() const
Check if node is valid, i.e.
static int to_int(std::string s)
Convert string to an int value.
const std::map< std::string, std::string > & properties() const
Get all properties.
Definition: navgraph_node.h:88
void set_property(const std::string &property, const std::string &value)
Set property.
const std::string & name() const
Get name of node.
Definition: navgraph_node.h:49
float y() const
Get Y coordinate in global frame.
Definition: navgraph_node.h:59
bool property_as_bool(const std::string &prop) const
Get property converted to bol.
NavGraphNode()
Constructor for invalid node.
void set_x(float x)
Set X position.
void set_name(const std::string &name)
Set name of node.
bool unconnected() const
Check if this node shall be unconnected.
Definition: navgraph_node.h:64
Topological graph node.
Definition: navgraph_node.h:38
void set_reachable_nodes(std::vector< std::string > reachable_nodes)
Set directly reachable nodes of node.
float x() const
Get X coordinate in global frame.
Definition: navgraph_node.h:54
static float to_float(std::string s)
Convert string to a float value.
float distance(float x, float y)
Get euclidean distance from this node to a point.
Definition: navgraph_node.h:82
float property_as_float(const std::string &prop) const
Get property converted to float.
bool operator==(const NavGraphNode &n) const
Check nodes for equality.
static bool to_bool(std::string s)
Convert string to a bool value.
const std::vector< std::string > & reachable_nodes() const
Get reachable nodes.