Gazebo Math

API Reference

8.0.0
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#include <gz/utils/NeverDestroyed.hh>
30
31namespace gz::math
32{
33// Inline bracket to help doxygen filtering.
34inline namespace GZ_MATH_VERSION_NAMESPACE {
35namespace graph
36{
39 using EdgeId = uint64_t;
40
42 template<typename E>
44 {
49 // cppcheck-suppress noExplicitConstructor
51 const E &_data = E(),
52 const double _weight = 1)
53 : vertices(_vertices),
54 data(_data),
55 weight(_weight)
56 {
57 };
58
61
63 public: E data;
64
66 public: double weight = 1;
67 };
68
72 template<typename E>
73 class Edge
74 {
80 public: explicit Edge(const VertexId_P &_vertices,
81 const E &_data,
82 const double _weight,
83 const EdgeId &_id = kNullId)
84 : id(_id),
85 vertices(_vertices),
86 data(_data),
87 weight(_weight)
88 {
89 }
90
93 public: EdgeId Id() const
94 {
95 return this->id;
96 }
97
100 public: VertexId_P Vertices() const
101 {
102 if (!this->Valid())
103 return {kNullId, kNullId};
104
105 return this->vertices;
106 }
107
110 public: const E &Data() const
111 {
112 return this->data;
113 }
114
117 public: E &Data()
118 {
119 return this->data;
120 }
121
125 public: double Weight() const
126 {
127 return this->weight;
128 }
129
132 public: void SetWeight(double _newWeight)
133 {
134 this->weight = _newWeight;
135 }
136
151 public: virtual VertexId From(const VertexId &_from) const = 0;
152
167 public: virtual VertexId To(const VertexId &_to) const = 0;
168
171 public: bool Valid() const
172 {
173 return this->id != kNullId;
174 }
175
177 private: EdgeId id = kNullId;
178
180 private: VertexId_P vertices;
181
183 private: E data;
184
187 private: double weight = 1.0;
188 };
189
193
197 template<typename EdgeType>
199
203 template<typename E>
204 class UndirectedEdge : public Edge<E>
205 {
207 // Deprecated in favor of NullEdge().
208 public: static UndirectedEdge<E> GZ_DEPRECATED(8) NullEdge;
209
216 const E &_data,
217 double _weight,
218 const EdgeId &_id = kNullId)
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
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 // Deprecated in favor of NullEdge().
261 template<typename E>
262 UndirectedEdge<E> UndirectedEdge<E>::NullEdge(
263 VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
264
268 template<typename E>
269 class DirectedEdge : public Edge<E>
270 {
272 // Deprecated in favor of NullEdge().
273 public: static DirectedEdge<E> GZ_DEPRECATED(8) NullEdge;
274
281 const E &_data,
282 double _weight,
283 const EdgeId &_id = kNullId)
285 {
286 }
287
291 public: VertexId Tail() const
292 {
293 return this->Vertices().first;
294 }
295
299 public: VertexId Head() const
300 {
301 return this->Vertices().second;
302 }
303
304 // Documentation inherited.
305 public: VertexId From(const VertexId &_from) const override
306 {
307 if (_from != this->Tail())
308 return kNullId;
309
310 return this->Head();
311 }
312
313 // Documentation inherited.
314 public: VertexId To(const VertexId &_to) const override
315 {
316 if (_to != this->Head())
317 return kNullId;
318
319 return this->Tail();
320 }
321
328 const DirectedEdge<E> &_e)
329 {
330 _out << " " << _e.Tail() << " -> " << _e.Head()
331 << " [label=" << _e.Weight() << "];" << std::endl;
332 return _out;
333 }
334 };
335
337 // Deprecated in favor of NullEdge().
338 template<typename E>
339 DirectedEdge<E> DirectedEdge<E>::NullEdge(
340 VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
341
343 template<typename E, typename EdgeType>
345 {
346 static gz::utils::NeverDestroyed<EdgeType> e(
347 VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
348 return e.Access();
349 }
350} // namespace graph
351} // namespace GZ_MATH_VERSION_NAMESPACE
352} // namespace gz::math
353#endif // GZ_MATH_GRAPH_EDGE_HH_