Fawkes API  Fawkes Development Version
static_list_edge_constraint.cpp
1 /***************************************************************************
2  * static_list_edge_constraint.cpp - edge constraint that holds a static
3  * of edges to block
4  *
5  * Created: Sat Jul 12 16:48:23 2014
6  * Copyright 2014 Sebastian Reuter
7  * 2014 Tim Niemueller
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
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 file in the doc directory.
21  */
22 
23 #include <navgraph/constraints/static_list_edge_constraint.h>
24 
25 #include <algorithm>
26 
27 namespace fawkes{
28 #if 0 /* just to make Emacs auto-indent happy */
29 }
30 #endif
31 
32 /** @class NavGraphStaticListEdgeConstraint <navgraph/constraints/static_list_edge_constraint.h>
33  * Constraint that holds a list of edges to block.
34  * @author Sebastian Reuter
35  * @author Tim Niemueller
36  */
37 
38 
39 /** Constructor.
40  * @param name name of edge constraint
41  */
44 {
45  modified_ = false;
46 }
47 
48 
49 
50 /** Constructor.
51  * @param name name of edge constraint
52  * @param edge_list list of edges to block
53  */
55  std::string name,
56  std::vector<fawkes::NavGraphEdge> &edge_list)
58 {
59  edge_list_ = edge_list;
60  modified_ = false;
61 }
62 
63 /** Virtual empty destructor. */
65 {
66 }
67 
68 
69 bool
71 {
72  if (modified_) {
73  modified_ = false;
74  return true;
75  } else {
76  return false;
77  }
78 }
79 
80 
81 /** Add a single edge to constraint list.
82  * @param edge edge to add to constraint list
83  */
84 void
86 {
87  if (! has_edge(edge)) {
88  modified_ = true;
89  edge_list_.push_back(edge);
90  }
91 }
92 
93 /** Add multiple edges to constraint list.
94  * @param edges edges to add to constraint list
95  */
96 void
98  const std::vector<fawkes::NavGraphEdge> &edges)
99 {
100  for (const NavGraphEdge &n : edges) {
101  add_edge(n);
102  }
103 }
104 
105 /** Remove a single edge from the constraint list.
106  * @param edge edge to remote
107  */
108 void
110 {
111  std::vector<NavGraphEdge>::iterator e
112  = std::find(edge_list_.begin(), edge_list_.end(), edge);
113  if (e != edge_list_.end()) {
114  modified_ = true;
115  edge_list_.erase(e);
116  }
117 }
118 
119 /** Check if constraint has a specific edge.
120  * @param edge edge to check
121  * @return true if edge is in list, false otherwise
122  */
123 bool
125 {
126  return (std::find(edge_list_.begin(), edge_list_.end(), edge) != edge_list_.end());
127 }
128 
129 
130 /** Get list of blocked edges.
131  * @return list of blocked edges
132  */
133 const std::vector<fawkes::NavGraphEdge> &
135 {
136  return edge_list_;
137 }
138 
139 
140 /** Remove all edges. */
141 void
143 {
144  if (! edge_list_.empty()) {
145  modified_ = true;
146  edge_list_.clear();
147  }
148 }
149 
150 
151 bool
153  const fawkes::NavGraphNode &to) throw()
154 {
155  for (NavGraphEdge &e : edge_list_) {
156  if ((e.from() == from.name() && e.to() == to.name()) ||
157  (e.from() == to.name() && e.to() == from.name()) )
158  {
159  return true;
160  }
161  }
162  return false;
163 }
164 
165 
166 } // end of namespace fawkes
Constraint that can be queried to check if an edge is blocked.
virtual bool blocks(const fawkes::NavGraphNode &from, const fawkes::NavGraphNode &to)
Check if constraint blocks an edge.
void remove_edge(const fawkes::NavGraphEdge &edge)
Remove a single edge from the constraint list.
Fawkes library namespace.
NavGraphStaticListEdgeConstraint(std::string name)
Constructor.
bool has_edge(const fawkes::NavGraphEdge &edge)
Check if constraint has a specific edge.
const std::vector< fawkes::NavGraphEdge > & edge_list() const
Get list of blocked edges.
virtual bool compute(void)
Perform compuations before graph search and to indicate re-planning.
std::string name()
Get name of constraint.
Topological graph edge.
Definition: navgraph_edge.h:39
Topological graph node.
Definition: navgraph_node.h:38
virtual ~NavGraphStaticListEdgeConstraint()
Virtual empty destructor.
void add_edges(const std::vector< fawkes::NavGraphEdge > &edges)
Add multiple edges to constraint list.
void add_edge(const fawkes::NavGraphEdge &edge)
Add a single edge to constraint list.