Fawkes API  Fawkes Development Version
navgraph_edge.h
1 
2 /***************************************************************************
3  * navgraph_edge.h - Topological graph edge
4  *
5  * Created: Fri Sep 21 16:08:27 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_EDGE_H_
24 #define _UTILS_GRAPH_TOPOLOGICAL_MAP_EDGE_H_
25 
26 #include <navgraph/navgraph_node.h>
27 #include <utils/math/types.h>
28 #include <utils/misc/string_conversions.h>
29 
30 #include <map>
31 #include <string>
32 
33 namespace fawkes {
34 
35 class NavGraph;
36 
38 {
39  friend NavGraph;
40 
41 public:
42  NavGraphEdge();
43 
44  NavGraphEdge(const std::string & from,
45  const std::string & to,
46  std::map<std::string, std::string> properties,
47  bool directed = false);
48 
49  NavGraphEdge(const std::string &from, const std::string &to, bool directed = false);
50 
51  /** Get edge originating node name.
52  * @return edge originating node name */
53  const std::string &
54  from() const
55  {
56  return from_;
57  }
58 
59  /** Get edge target node name.
60  * @return edge target node name */
61  const std::string &
62  to() const
63  {
64  return to_;
65  }
66 
67  /** Get edge originating node.
68  * @return edge originating node */
69  const NavGraphNode &
70  from_node() const
71  {
72  return from_node_;
73  }
74 
75  /** Get edge target node.
76  * @return edge target node */
77  const NavGraphNode &
78  to_node() const
79  {
80  return to_node_;
81  }
82 
83  fawkes::cart_coord_2d_t closest_point_on_edge(float x, float y) const;
84  float distance(float x, float y) const;
85  bool intersects(float x1, float y1, float x2, float y2) const;
86  bool intersection(float x1, float y1, float x2, float y2, fawkes::cart_coord_2d_t &ip) const;
87 
88  void set_from(const std::string &from);
89  void set_to(const std::string &to);
90  void set_directed(bool directed);
91 
92  /** Get all properties.
93  * @return property map
94  */
95  const std::map<std::string, std::string> &
96  properties() const
97  {
98  return properties_;
99  }
100 
101  /** Check if node has specified property.
102  * @param property property key
103  * @return true if node has specified property, false otherwise
104  */
105  bool
106  has_property(const std::string &property) const
107  {
108  return properties_.find(property) != properties_.end();
109  }
110 
111  void set_properties(const std::map<std::string, std::string> &properties);
112  void set_property(const std::string &property, const std::string &value);
113  void set_property(const std::string &property, const char *value);
114  void set_property(const std::string &property, float value);
115  void set_property(const std::string &property, int value);
116  void set_property(const std::string &property, bool value);
117 
118  /** Get property converted to float.
119  * @param prop property key
120  * @return property value
121  */
122  float
123  property_as_float(const std::string &prop) const
124  {
126  }
127 
128  /** Get property converted to int.
129  * @param prop property key
130  * @return property value
131  */
132  int
133  property_as_int(const std::string &prop) const
134  {
135  return StringConversions::to_int(property(prop));
136  }
137 
138  /** Get property converted to bol.
139  * @param prop property key
140  * @return property value
141  */
142  bool
143  property_as_bool(const std::string &prop) const
144  {
145  return StringConversions::to_bool(property(prop));
146  }
147 
148  /** Check if edge is valid.
149  * An edge is valid iff it has originating and target node name values.
150  * @return true if edge is valid, false otherwise
151  */
152  bool
153  is_valid() const
154  {
155  return from_ != "" && to_ != "";
156  }
157 
158  /** Check if edge is directed.
159  * @return true if edge is directed, false otherwise.
160  */
161  bool
162  is_directed() const
163  {
164  return directed_;
165  }
166 
167  std::string property(const std::string &prop) const;
168 
169  /** Get property converted to float.
170  * @param prop property key
171  * @return property value
172  */
173  float
174  property_as_float(const std::string &prop)
175  {
177  }
178 
179  /** Get property converted to int.
180  * @param prop property key
181  * @return property value
182  */
183  int
184  property_as_int(const std::string &prop)
185  {
186  return StringConversions::to_int(property(prop));
187  }
188 
189  /** Get property converted to bol.
190  * @param prop property key
191  * @return property value
192  */
193  bool
194  property_as_bool(const std::string &prop)
195  {
196  return StringConversions::to_bool(property(prop));
197  }
198 
199  /** Check edges for equality.
200  * Edges are equal if they have the same origination and destination
201  * nodes and the same directed status.
202  * @param e edge to compare with
203  * @return true if the node is the same as this one, false otherwise
204  */
205  bool
206  operator==(const NavGraphEdge &e) const
207  {
208  return from_ == e.from_ && to_ == e.to_ && directed_ == e.directed_;
209  }
210 
211  /** Less than operator based on node from and to names.
212  * One edge is less than another if this is true for their respective names.
213  * @param e edge to compare with
214  * @return true if this edge is less than the given one
215  */
216  bool
217  operator<(const NavGraphEdge &e) const
218  {
219  return (from_ == e.from_ && to_ < e.to_) || (from_ < e.from_);
220  }
221 
222  /** Check of edge is valid.
223  * An edge is valid if both the originating and the target node
224  * name is set to a non-empty string.
225  * @return true if the node is valid, false otherwise
226  */
227  operator bool() const
228  {
229  return from_ != "" && to_ != "";
230  }
231 
232  void set_nodes(const NavGraphNode &from_node, const NavGraphNode &to_node);
233 
234 private:
235  std::string from_;
236  std::string to_;
237  bool directed_;
238  std::map<std::string, std::string> properties_;
239 
240  NavGraphNode from_node_;
241  NavGraphNode to_node_;
242 };
243 
244 } // end of namespace fawkes
245 
246 #endif
fawkes::NavGraphNode
Topological graph node.
Definition: navgraph_node.h:36
fawkes::NavGraphEdge::property_as_float
float property_as_float(const std::string &prop)
Get property converted to float.
Definition: navgraph_edge.h:174
fawkes::NavGraphEdge::property_as_int
int property_as_int(const std::string &prop) const
Get property converted to int.
Definition: navgraph_edge.h:133
fawkes::NavGraphEdge::operator<
bool operator<(const NavGraphEdge &e) const
Less than operator based on node from and to names.
Definition: navgraph_edge.h:217
fawkes::NavGraphEdge
Topological graph edge.
Definition: navgraph_edge.h:38
fawkes::NavGraphEdge::is_valid
bool is_valid() const
Check if edge is valid.
Definition: navgraph_edge.h:153
fawkes::NavGraph
Topological map graph.
Definition: navgraph.h:50
fawkes::NavGraphEdge::property_as_int
int property_as_int(const std::string &prop)
Get property converted to int.
Definition: navgraph_edge.h:184
fawkes::NavGraphEdge::set_nodes
void set_nodes(const NavGraphNode &from_node, const NavGraphNode &to_node)
Set nodes.
Definition: navgraph_edge.cpp:106
fawkes::StringConversions::to_int
static int to_int(std::string s)
Convert string to an int value.
Definition: string_conversions.cpp:184
fawkes::NavGraphEdge::property_as_bool
bool property_as_bool(const std::string &prop)
Get property converted to bol.
Definition: navgraph_edge.h:194
fawkes::NavGraphEdge::set_to
void set_to(const std::string &to)
Set target node name.
Definition: navgraph_edge.cpp:96
fawkes::NavGraphEdge::to_node
const NavGraphNode & to_node() const
Get edge target node.
Definition: navgraph_edge.h:78
fawkes::NavGraphEdge::NavGraphEdge
NavGraphEdge()
Constructor for an invalid edge.
Definition: navgraph_edge.cpp:49
fawkes
Fawkes library namespace.
fawkes::NavGraphEdge::from_node
const NavGraphNode & from_node() const
Get edge originating node.
Definition: navgraph_edge.h:70
fawkes::NavGraphEdge::is_directed
bool is_directed() const
Check if edge is directed.
Definition: navgraph_edge.h:162
fawkes::StringConversions::to_bool
static bool to_bool(std::string s)
Convert string to a bool value.
Definition: string_conversions.cpp:224
fawkes::NavGraphEdge::intersection
bool intersection(float x1, float y1, float x2, float y2, fawkes::cart_coord_2d_t &ip) const
Check if the edge intersects with another line segment.
Definition: navgraph_edge.cpp:266
fawkes::NavGraphEdge::set_from
void set_from(const std::string &from)
Set originating node name.
Definition: navgraph_edge.cpp:87
fawkes::NavGraphEdge::property_as_bool
bool property_as_bool(const std::string &prop) const
Get property converted to bol.
Definition: navgraph_edge.h:143
fawkes::NavGraphEdge::to
const std::string & to() const
Get edge target node name.
Definition: navgraph_edge.h:62
fawkes::cart_coord_2d_struct
Cartesian coordinates (2D).
Definition: types.h:65
fawkes::NavGraphEdge::property
std::string property(const std::string &prop) const
Get specified property as string.
Definition: navgraph_edge.cpp:137
fawkes::NavGraphEdge::operator==
bool operator==(const NavGraphEdge &e) const
Check edges for equality.
Definition: navgraph_edge.h:206
fawkes::NavGraphEdge::set_directed
void set_directed(bool directed)
Set directed state.
Definition: navgraph_edge.cpp:127
fawkes::NavGraphEdge::distance
float distance(float x, float y) const
Get distance of point to closest point on edge.
Definition: navgraph_edge.cpp:244
fawkes::NavGraphEdge::closest_point_on_edge
fawkes::cart_coord_2d_t closest_point_on_edge(float x, float y) const
Get the point on edge closest to a given point.
Definition: navgraph_edge.cpp:219
fawkes::NavGraphEdge::has_property
bool has_property(const std::string &property) const
Check if node has specified property.
Definition: navgraph_edge.h:106
fawkes::NavGraphEdge::from
const std::string & from() const
Get edge originating node name.
Definition: navgraph_edge.h:54
fawkes::NavGraphEdge::property_as_float
float property_as_float(const std::string &prop) const
Get property converted to float.
Definition: navgraph_edge.h:123
fawkes::NavGraphEdge::properties
const std::map< std::string, std::string > & properties() const
Get all properties.
Definition: navgraph_edge.h:96
fawkes::NavGraphEdge::set_property
void set_property(const std::string &property, const std::string &value)
Set property.
Definition: navgraph_edge.cpp:161
fawkes::StringConversions::to_float
static float to_float(std::string s)
Convert string to a float value.
Definition: string_conversions.cpp:204
fawkes::NavGraphEdge::intersects
bool intersects(float x1, float y1, float x2, float y2) const
Check if the edge intersects with another line segment.
Definition: navgraph_edge.cpp:295
fawkes::NavGraphEdge::set_properties
void set_properties(const std::map< std::string, std::string > &properties)
Overwrite properties with given ones.
Definition: navgraph_edge.cpp:151