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 <cmath>
29 #include <map>
30 #include <string>
31 #include <vector>
32 
33 namespace fawkes {
34 
35 class NavGraphNode
36 {
37 public:
38  NavGraphNode();
39 
40  NavGraphNode(const std::string & name,
41  float x,
42  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 &
50  name() const
51  {
52  return name_;
53  }
54 
55  /** Get X coordinate in global frame.
56  * @return X coordinate in global frame */
57  float
58  x() const
59  {
60  return x_;
61  }
62 
63  /** Get Y coordinate in global frame.
64  * @return Y coordinate in global frame */
65  float
66  y() const
67  {
68  return y_;
69  }
70 
71  /** Check if this node shall be unconnected.
72  * @return true if the node is unconnected, false otherwise */
73  bool
74  unconnected() const
75  {
76  return unconnected_;
77  }
78 
79  void set_x(float x);
80  void set_y(float y);
81  void set_name(const std::string &name);
82  void set_unconnected(bool unconnected);
83 
84  /** Get euclidean distance from this node to another.
85  * @param n node to get distance to
86  * @return distance */
87  float
88  distance(const NavGraphNode &n)
89  {
90  return sqrtf(powf(x_ - n.x_, 2) + powf(y_ - n.y_, 2));
91  }
92 
93  /** Get euclidean distance from this node to a point.
94  * @param x point X coordinate
95  * @param y point Y coordinate
96  * @return distance */
97  float
98  distance(float x, float y)
99  {
100  return sqrtf(powf(x_ - x, 2) + powf(y_ - y, 2));
101  }
102 
103  /** Get all properties.
104  * @return property map
105  */
106  const std::map<std::string, std::string> &
107  properties() const
108  {
109  return properties_;
110  }
111 
112  /** Check if node has specified property.
113  * @param property property key
114  * @return true if node has specified property, false otherwise
115  */
116  bool
117  has_property(const std::string &property) const
118  {
119  return properties_.find(property) != properties_.end();
120  }
121 
122  /** Check if node is valid, i.e. it has a name.
123  * @return true if node is valid, false otherwise
124  */
125  bool
126  is_valid() const
127  {
128  return name_ != "";
129  }
130 
131  void set_properties(const std::map<std::string, std::string> &properties);
132  void set_property(const std::string &property, const std::string &value);
133  void set_property(const std::string &property, float value);
134  void set_property(const std::string &property, int value);
135  void set_property(const std::string &property, bool value);
136 
137  std::string property(const std::string &prop) const;
138 
139  /** Get property converted to float.
140  * @param prop property key
141  * @return property value
142  */
143  float
144  property_as_float(const std::string &prop) const
145  {
147  }
148 
149  /** Get property converted to int.
150  * @param prop property key
151  * @return property value
152  */
153  int
154  property_as_int(const std::string &prop) const
155  {
156  return StringConversions::to_int(property(prop));
157  }
158 
159  /** Get property converted to bol.
160  * @param prop property key
161  * @return property value
162  */
163  bool
164  property_as_bool(const std::string &prop) const
165  {
166  return StringConversions::to_bool(property(prop));
167  }
168 
169  /** Check nodes for equality.
170  * Nodes are equal if they have the same name.
171  * @param n node to compare with
172  * @return true if the node is the same as this one, false otherwise
173  */
174  bool
175  operator==(const NavGraphNode &n) const
176  {
177  return name_ == n.name_;
178  }
179 
180  /** Check nodes for inequality.
181  * Nodes are inequal if they have different names.
182  * @param n node to compare with
183  * @return true if the node is different from this one, false otherwise
184  */
185  bool
186  operator!=(const NavGraphNode &n) const
187  {
188  return name_ != n.name_;
189  }
190 
191  /** Check if node is valid.
192  * A node is valid if it has a name set.
193  * @return true if the node is valid, false otherwise
194  */
195  operator bool() const
196  {
197  return name_ != "";
198  }
199 
200  void set_reachable_nodes(std::vector<std::string> reachable_nodes);
201 
202  /** Get reachable nodes.
203  * @return vector of directly reachable nodes.
204  */
205  const std::vector<std::string> &
206  reachable_nodes() const
207  {
208  return reachable_nodes_;
209  }
210 
211 private:
212  std::string name_;
213  float x_;
214  float y_;
215  bool unconnected_;
216  std::map<std::string, std::string> properties_;
217  std::vector<std::string> reachable_nodes_;
218 };
219 
220 } // end of namespace fawkes
221 
222 #endif
fawkes::NavGraphNode::set_properties
void set_properties(const std::map< std::string, std::string > &properties)
Overwrite properties with given ones.
Definition: navgraph_node.cpp:133
fawkes::NavGraphNode
Definition: navgraph_node.h:40
fawkes::NavGraphNode::has_property
bool has_property(const std::string &property) const
Check if node has specified property.
Definition: navgraph_node.h:127
fawkes::NavGraphNode::set_unconnected
void set_unconnected(bool unconnected)
Set unconnected state of the node.
Definition: navgraph_node.cpp:109
fawkes::NavGraphNode::distance
float distance(const NavGraphNode &n)
Get euclidean distance from this node to another.
Definition: navgraph_node.h:98
fawkes::NavGraphNode::operator==
bool operator==(const NavGraphNode &n) const
Check nodes for equality.
Definition: navgraph_node.h:185
fawkes::NavGraphNode::set_property
void set_property(const std::string &property, const std::string &value)
Set property.
Definition: navgraph_node.cpp:143
fawkes::NavGraphNode::set_y
void set_y(float y)
Set Y position.
Definition: navgraph_node.cpp:85
fawkes::NavGraphNode::name
const std::string & name() const
Get name of node.
Definition: navgraph_node.h:60
fawkes::NavGraphNode::properties
const std::map< std::string, std::string > & properties() const
Get all properties.
Definition: navgraph_node.h:117
fawkes::NavGraphNode::property
std::string property(const std::string &prop) const
Get specified property as string.
Definition: navgraph_node.cpp:119
fawkes::StringConversions::to_int
static int to_int(std::string s)
Convert string to an int value.
Definition: string_conversions.cpp:184
fawkes::NavGraphNode::unconnected
bool unconnected() const
Check if this node shall be unconnected.
Definition: navgraph_node.h:84
fawkes::NavGraphNode::set_name
void set_name(const std::string &name)
Set name of node.
Definition: navgraph_node.cpp:94
fawkes
fawkes::NavGraphNode::reachable_nodes
const std::vector< std::string > & reachable_nodes() const
Get reachable nodes.
Definition: navgraph_node.h:216
fawkes::NavGraphNode::x
float x() const
Get X coordinate in global frame.
Definition: navgraph_node.h:68
fawkes::NavGraphNode::NavGraphNode
NavGraphNode()
Constructor for invalid node.
Definition: navgraph_node.cpp:38
fawkes::NavGraphNode::property_as_float
float property_as_float(const std::string &prop) const
Get property converted to float.
Definition: navgraph_node.h:154
fawkes::StringConversions::to_bool
static bool to_bool(std::string s)
Convert string to a bool value.
Definition: string_conversions.cpp:224
fawkes::NavGraphNode::is_valid
bool is_valid() const
Check if node is valid, i.e.
Definition: navgraph_node.h:136
fawkes::NavGraphNode::y
float y() const
Get Y coordinate in global frame.
Definition: navgraph_node.h:76
fawkes::NavGraphNode::operator!=
bool operator!=(const NavGraphNode &n) const
Check nodes for inequality.
Definition: navgraph_node.h:196
fawkes::NavGraphNode::set_x
void set_x(float x)
Set X position.
Definition: navgraph_node.cpp:76
fawkes::NavGraphNode::set_reachable_nodes
void set_reachable_nodes(std::vector< std::string > reachable_nodes)
Set directly reachable nodes of node.
Definition: navgraph_node.cpp:182
fawkes::NavGraphNode::property_as_int
int property_as_int(const std::string &prop) const
Get property converted to int.
Definition: navgraph_node.h:164
fawkes::StringConversions::to_float
static float to_float(std::string s)
Convert string to a float value.
Definition: string_conversions.cpp:204
fawkes::NavGraphNode::property_as_bool
bool property_as_bool(const std::string &prop) const
Get property converted to bol.
Definition: navgraph_node.h:174