Gazebo Math

API Reference

7.4.0
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>
29
30namespace gz
31{
32namespace math
33{
34// Inline bracket to help doxygen filtering.
35inline namespace GZ_MATH_VERSION_NAMESPACE {
36namespace graph
37{
40 using EdgeId = uint64_t;
41
43 template<typename E>
45 {
50 // cppcheck-suppress noExplicitConstructor
51 EdgeInitializer(const VertexId_P &_vertices,
52 const E &_data = E(),
53 const double _weight = 1)
54 : vertices(_vertices),
55 data(_data),
56 weight(_weight)
57 {
58 };
59
62
64 public: E data;
65
67 public: double weight = 1;
68 };
69
73 template<typename E>
74 class Edge
75 {
81 public: explicit Edge(const VertexId_P &_vertices,
82 const E &_data,
83 const double _weight,
84 const EdgeId &_id = kNullId)
85 : id(_id),
86 vertices(_vertices),
87 data(_data),
88 weight(_weight)
89 {
90 }
91
94 public: EdgeId Id() const
95 {
96 return this->id;
97 }
98
101 public: VertexId_P Vertices() const
102 {
103 if (!this->Valid())
104 return {kNullId, kNullId};
105
106 return this->vertices;
107 }
108
111 public: const E &Data() const
112 {
113 return this->data;
114 }
115
118 public: E &Data()
119 {
120 return this->data;
121 }
122
126 public: double Weight() const
127 {
128 return this->weight;
129 }
130
133 public: void SetWeight(double _newWeight)
134 {
135 this->weight = _newWeight;
136 }
137
152 public: virtual VertexId From(const VertexId &_from) const = 0;
153
168 public: virtual VertexId To(const VertexId &_to) const = 0;
169
172 public: bool Valid() const
173 {
174 return this->id != kNullId;
175 }
176
178 private: EdgeId id = kNullId;
179
181 private: VertexId_P vertices;
182
184 private: E data;
185
188 private: double weight = 1.0;
189 };
190
194
198 template<typename EdgeType>
200
204 template<typename E>
205 class UndirectedEdge : public Edge<E>
206 {
209
215 public: explicit UndirectedEdge(const VertexId_P &_vertices,
216 const E &_data,
217 double _weight,
218 const EdgeId &_id = kNullId)
219 : Edge<E>(_vertices, _data, _weight, _id)
220 {
221 }
222
223 // Documentation inherited.
224 public: VertexId From(const VertexId &_from) const override
225 {
226 if (!this->Valid())
227 return kNullId;
228
229 if (this->Vertices().first != _from && this->Vertices().second != _from)
230 return kNullId;
231
232 if (this->Vertices().first == _from)
233 return this->Vertices().second;
234
235 return this->Vertices().first;
236 }
237
238 // Documentation inherited.
239 public: VertexId To(const VertexId &_to) const override
240 {
241 return this->From(_to);
242 }
243
249 public: friend std::ostream &operator<<(std::ostream &_out,
250 const UndirectedEdge<E> &_e)
251 {
252 auto vertices = _e.Vertices();
253 _out << " " << vertices.first << " -- " << vertices.second
254 << " [label=" << _e.Weight() << "];" << std::endl;
255 return _out;
256 }
257 };
258
260 template<typename E>
261 UndirectedEdge<E> UndirectedEdge<E>::NullEdge(
262 VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
263
267 template<typename E>
268 class DirectedEdge : public Edge<E>
269 {
271 public: static DirectedEdge<E> NullEdge;
272
278 public: explicit DirectedEdge(const VertexId_P &_vertices,
279 const E &_data,
280 double _weight,
281 const EdgeId &_id = kNullId)
282 : Edge<E>(_vertices, _data, _weight, _id)
283 {
284 }
285
289 public: VertexId Tail() const
290 {
291 return this->Vertices().first;
292 }
293
297 public: VertexId Head() const
298 {
299 return this->Vertices().second;
300 }
301
302 // Documentation inherited.
303 public: VertexId From(const VertexId &_from) const override
304 {
305 if (_from != this->Tail())
306 return kNullId;
307
308 return this->Head();
309 }
310
311 // Documentation inherited.
312 public: VertexId To(const VertexId &_to) const override
313 {
314 if (_to != this->Head())
315 return kNullId;
316
317 return this->Tail();
318 }
319
325 public: friend std::ostream &operator<<(std::ostream &_out,
326 const DirectedEdge<E> &_e)
327 {
328 _out << " " << _e.Tail() << " -> " << _e.Head()
329 << " [label=" << _e.Weight() << "];" << std::endl;
330 return _out;
331 }
332 };
333
335 template<typename E>
336 DirectedEdge<E> DirectedEdge<E>::NullEdge(
337 VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
338}
339}
340}
341}
342#endif