Gazebo Math

API Reference

7.5.1
gz/math/graph/Edge.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_MATH_GRAPH_EDGE_HH_
18 #define GZ_MATH_GRAPH_EDGE_HH_
19 
20 // uint64_t
21 #include <cstdint>
22 #include <functional>
23 #include <map>
24 #include <ostream>
25 #include <set>
26 
27 #include <gz/math/config.hh>
28 #include "gz/math/graph/Vertex.hh"
29 
30 namespace gz::math
31 {
32 // Inline bracket to help doxygen filtering.
33 inline namespace GZ_MATH_VERSION_NAMESPACE {
34 namespace graph
35 {
38  using EdgeId = uint64_t;
39 
41  template<typename E>
43  {
48  // cppcheck-suppress noExplicitConstructor
49  EdgeInitializer(const VertexId_P &_vertices,
50  const E &_data = E(),
51  const double _weight = 1)
52  : vertices(_vertices),
53  data(_data),
54  weight(_weight)
55  {
56  };
57 
60 
62  public: E data;
63 
65  public: double weight = 1;
66  };
67 
71  template<typename E>
72  class Edge
73  {
79  public: explicit Edge(const VertexId_P &_vertices,
80  const E &_data,
81  const double _weight,
82  const EdgeId &_id = kNullId)
83  : id(_id),
84  vertices(_vertices),
85  data(_data),
86  weight(_weight)
87  {
88  }
89 
92  public: EdgeId Id() const
93  {
94  return this->id;
95  }
96 
99  public: VertexId_P Vertices() const
100  {
101  if (!this->Valid())
102  return {kNullId, kNullId};
103 
104  return this->vertices;
105  }
106 
109  public: const E &Data() const
110  {
111  return this->data;
112  }
113 
116  public: E &Data()
117  {
118  return this->data;
119  }
120 
124  public: double Weight() const
125  {
126  return this->weight;
127  }
128 
131  public: void SetWeight(double _newWeight)
132  {
133  this->weight = _newWeight;
134  }
135 
150  public: virtual VertexId From(const VertexId &_from) const = 0;
151 
166  public: virtual VertexId To(const VertexId &_to) const = 0;
167 
170  public: bool Valid() const
171  {
172  return this->id != kNullId;
173  }
174 
176  private: EdgeId id = kNullId;
177 
179  private: VertexId_P vertices;
180 
182  private: E data;
183 
186  private: double weight = 1.0;
187  };
188 
192 
196  template<typename EdgeType>
198 
202  template<typename E>
203  class UndirectedEdge : public Edge<E>
204  {
206  public: static UndirectedEdge<E> NullEdge;
207 
213  public: explicit UndirectedEdge(const VertexId_P &_vertices,
214  const E &_data,
215  double _weight,
216  const EdgeId &_id = kNullId)
217  : Edge<E>(_vertices, _data, _weight, _id)
218  {
219  }
220 
221  // Documentation inherited.
222  public: VertexId From(const VertexId &_from) const override
223  {
224  if (!this->Valid())
225  return kNullId;
226 
227  if (this->Vertices().first != _from && this->Vertices().second != _from)
228  return kNullId;
229 
230  if (this->Vertices().first == _from)
231  return this->Vertices().second;
232 
233  return this->Vertices().first;
234  }
235 
236  // Documentation inherited.
237  public: VertexId To(const VertexId &_to) const override
238  {
239  return this->From(_to);
240  }
241 
247  public: friend std::ostream &operator<<(std::ostream &_out,
248  const UndirectedEdge<E> &_e)
249  {
250  auto vertices = _e.Vertices();
251  _out << " " << vertices.first << " -- " << vertices.second
252  << " [label=" << _e.Weight() << "];" << std::endl;
253  return _out;
254  }
255  };
256 
258  template<typename E>
259  UndirectedEdge<E> UndirectedEdge<E>::NullEdge(
260  VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
261 
265  template<typename E>
266  class DirectedEdge : public Edge<E>
267  {
269  public: static DirectedEdge<E> NullEdge;
270 
276  public: explicit DirectedEdge(const VertexId_P &_vertices,
277  const E &_data,
278  double _weight,
279  const EdgeId &_id = kNullId)
280  : Edge<E>(_vertices, _data, _weight, _id)
281  {
282  }
283 
287  public: VertexId Tail() const
288  {
289  return this->Vertices().first;
290  }
291 
295  public: VertexId Head() const
296  {
297  return this->Vertices().second;
298  }
299 
300  // Documentation inherited.
301  public: VertexId From(const VertexId &_from) const override
302  {
303  if (_from != this->Tail())
304  return kNullId;
305 
306  return this->Head();
307  }
308 
309  // Documentation inherited.
310  public: VertexId To(const VertexId &_to) const override
311  {
312  if (_to != this->Head())
313  return kNullId;
314 
315  return this->Tail();
316  }
317 
323  public: friend std::ostream &operator<<(std::ostream &_out,
324  const DirectedEdge<E> &_e)
325  {
326  _out << " " << _e.Tail() << " -> " << _e.Head()
327  << " [label=" << _e.Weight() << "];" << std::endl;
328  return _out;
329  }
330  };
331 
333  template<typename E>
334  DirectedEdge<E> DirectedEdge<E>::NullEdge(
335  VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
336 } // namespace graph
337 } // namespace GZ_MATH_VERSION_NAMESPACE
338 } // namespace gz::math
339 #endif // GZ_MATH_GRAPH_EDGE_HH_