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 <utils/misc/string_conversions.h>
27 
28 #include <navgraph/navgraph_node.h>
29 #include <utils/math/types.h>
30 
31 #include <map>
32 #include <string>
33 
34 namespace fawkes {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 class NavGraphEdge {
40  friend class NavGraph;
41 
42  public:
43  NavGraphEdge();
44 
45  NavGraphEdge(const std::string &from, 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,
50  bool directed = false);
51 
52  /** Get edge originating node name.
53  * @return edge originating node name */
54  const std::string & from() const
55  { return from_; }
56 
57  /** Get edge target node name.
58  * @return edge target node name */
59  const std::string & to() const
60  { return to_; }
61 
62 
63  /** Get edge originating node.
64  * @return edge originating node */
65  const NavGraphNode & from_node() const
66  { return from_node_; }
67 
68  /** Get edge target node.
69  * @return edge target node */
70  const NavGraphNode & to_node() const
71  { return to_node_; }
72 
73  fawkes::cart_coord_2d_t closest_point_on_edge(float x, float y) const;
74  bool intersects(float x1, float y1, float x2, float y2) const;
75  bool intersection(float x1, float y1, float x2, float y2,
76  fawkes::cart_coord_2d_t &ip) const;
77 
78  void set_from(const std::string &from);
79  void set_to(const std::string &to);
80  void set_directed(bool directed);
81 
82  /** Get all properties.
83  * @return property map
84  */
85  const std::map<std::string, std::string> & properties() const
86  { return properties_; }
87 
88  /** Check if node has specified property.
89  * @param property property key
90  * @return true if node has specified property, false otherwise
91  */
92  bool has_property(const std::string &property) const
93  { return properties_.find(property) != properties_.end(); }
94 
95  void set_properties(const std::map<std::string, std::string> &properties);
96  void set_property(const std::string &property, const std::string &value);
97  void set_property(const std::string &property, const char *value);
98  void set_property(const std::string &property, float value);
99  void set_property(const std::string &property, int value);
100  void set_property(const std::string &property, bool value);
101 
102  /** Get property converted to float.
103  * @param prop property key
104  * @return property value
105  */
106  float property_as_float(const std::string &prop) const
107  { return StringConversions::to_float(property(prop)); }
108 
109  /** Get property converted to int.
110  * @param prop property key
111  * @return property value
112  */
113  int property_as_int(const std::string &prop) const
114  { return StringConversions::to_int(property(prop)); }
115 
116  /** Get property converted to bol.
117  * @param prop property key
118  * @return property value
119  */
120  bool property_as_bool(const std::string &prop) const
121  { return StringConversions::to_bool(property(prop)); }
122 
123  /** Check if edge is valid.
124  * An edge is valid iff it has originating and target node name values.
125  * @return true if edge is valid, false otherwise
126  */
127  bool is_valid() const
128  { return from_ != "" && to_ != ""; }
129 
130  /** Check if edge is directed.
131  * @return true if edge is directed, false otherwise.
132  */
133  bool is_directed() const
134  { return directed_; }
135 
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 property_as_float(const std::string &prop)
144  { return StringConversions::to_float(property(prop)); }
145 
146  /** Get property converted to int.
147  * @param prop property key
148  * @return property value
149  */
150  int property_as_int(const std::string &prop)
151  { return StringConversions::to_int(property(prop)); }
152 
153  /** Get property converted to bol.
154  * @param prop property key
155  * @return property value
156  */
157  bool property_as_bool(const std::string &prop)
158  { return StringConversions::to_bool(property(prop)); }
159 
160  /** Check edges for equality.
161  * Edges are equal if they have the same origination and destination
162  * nodes and the same directed status.
163  * @param e edge to compare with
164  * @return true if the node is the same as this one, false otherwise
165  */
166  bool operator==(const NavGraphEdge &e) const
167  { return from_ == e.from_ && to_ == e.to_ && directed_ == e.directed_; }
168 
169 
170  /** Less than operator based on node from and to names.
171  * One edge is less than another if this is true for their respective names.
172  * @param e edge to compare with
173  * @return true if this edge is less than the given one
174  */
175  bool operator<(const NavGraphEdge &e) const
176  { return (from_ == e.from_ && to_ < e.to_) || (from_ < e.from_); }
177 
178 
179  /** Check of edge is valid.
180  * An edge is valid if both the originating and the target node
181  * name is set to a non-empty string.
182  * @return true if the node is valid, false otherwise
183  */
184  operator bool() const
185  { return from_ != "" && to_ != ""; }
186 
187  void set_nodes(const NavGraphNode &from_node, const NavGraphNode &to_node);
188 
189  private:
190  std::string from_;
191  std::string to_;
192  bool directed_;
193  std::map<std::string, std::string> properties_;
194 
195  NavGraphNode from_node_;
196  NavGraphNode to_node_;
197 };
198 
199 } // end of namespace fawkes
200 
201 #endif
const std::string & from() const
Get edge originating node name.
Definition: navgraph_edge.h:54
void set_property(const std::string &property, const std::string &value)
Set property.
bool property_as_bool(const std::string &prop)
Get property converted to bol.
void set_nodes(const NavGraphNode &from_node, const NavGraphNode &to_node)
Set nodes.
Cartesian coordinates (2D).
Definition: types.h:59
Fawkes library namespace.
Topological map graph.
Definition: navgraph.h:57
void set_properties(const std::map< std::string, std::string > &properties)
Overwrite properties with given ones.
bool is_directed() const
Check if edge is directed.
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.
std::string property(const std::string &prop) const
Get specified property as string.
float property_as_float(const std::string &prop)
Get property converted to float.
const NavGraphNode & from_node() const
Get edge originating node.
Definition: navgraph_edge.h:65
void set_to(const std::string &to)
Set target node name.
const std::map< std::string, std::string > & properties() const
Get all properties.
Definition: navgraph_edge.h:85
bool operator==(const NavGraphEdge &e) const
Check edges for equality.
void set_from(const std::string &from)
Set originating node name.
static int to_int(std::string s)
Convert string to an int value.
NavGraphEdge()
Constructor for an invalid edge.
int property_as_int(const std::string &prop) const
Get property converted to int.
fawkes::cart_coord_2d_t closest_point_on_edge(float x, float y) const
Get the point on edge closest to a given point.
bool property_as_bool(const std::string &prop) const
Get property converted to bol.
const std::string & to() const
Get edge target node name.
Definition: navgraph_edge.h:59
bool is_valid() const
Check if edge is valid.
bool intersects(float x1, float y1, float x2, float y2) const
Check if the edge intersects with another line segment.
void set_directed(bool directed)
Set directed state.
bool has_property(const std::string &property) const
Check if node has specified property.
Definition: navgraph_edge.h:92
Topological graph edge.
Definition: navgraph_edge.h:39
float property_as_float(const std::string &prop) const
Get property converted to float.
int property_as_int(const std::string &prop)
Get property converted to int.
Topological graph node.
Definition: navgraph_node.h:38
static float to_float(std::string s)
Convert string to a float value.
static bool to_bool(std::string s)
Convert string to a bool value.
const NavGraphNode & to_node() const
Get edge target node.
Definition: navgraph_edge.h:70
bool operator<(const NavGraphEdge &e) const
Less than operator based on node from and to names.