Gazebo Math

API Reference

9.0.0~pre1
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 {
211 public: explicit UndirectedEdge(const VertexId_P &_vertices,
212 const E &_data,
213 double _weight,
214 const EdgeId &_id = kNullId)
216 {
217 }
218
219 // Documentation inherited.
220 public: VertexId From(const VertexId &_from) const override
221 {
222 if (!this->Valid())
223 return kNullId;
224
225 if (this->Vertices().first != _from && this->Vertices().second != _from)
226 return kNullId;
227
228 if (this->Vertices().first == _from)
229 return this->Vertices().second;
230
231 return this->Vertices().first;
232 }
233
234 // Documentation inherited.
235 public: VertexId To(const VertexId &_to) const override
236 {
237 return this->From(_to);
238 }
239
246 const UndirectedEdge<E> &_e)
247 {
248 auto vertices = _e.Vertices();
249 _out << " " << vertices.first << " -- " << vertices.second
250 << " [label=" << _e.Weight() << "];" << std::endl;
251 return _out;
252 }
253 };
254
258 template<typename E>
259 class DirectedEdge : public Edge<E>
260 {
266 public: explicit DirectedEdge(const VertexId_P &_vertices,
267 const E &_data,
268 double _weight,
269 const EdgeId &_id = kNullId)
271 {
272 }
273
277 public: VertexId Tail() const
278 {
279 return this->Vertices().first;
280 }
281
285 public: VertexId Head() const
286 {
287 return this->Vertices().second;
288 }
289
290 // Documentation inherited.
291 public: VertexId From(const VertexId &_from) const override
292 {
293 if (_from != this->Tail())
294 return kNullId;
295
296 return this->Head();
297 }
298
299 // Documentation inherited.
300 public: VertexId To(const VertexId &_to) const override
301 {
302 if (_to != this->Head())
303 return kNullId;
304
305 return this->Tail();
306 }
307
314 const DirectedEdge<E> &_e)
315 {
316 _out << " " << _e.Tail() << " -> " << _e.Head()
317 << " [label=" << _e.Weight() << "];" << std::endl;
318 return _out;
319 }
320 };
321
323 template<typename E, typename EdgeType>
325 {
326 static gz::utils::NeverDestroyed<EdgeType> e(
327 VertexId_P(kNullId, kNullId), E(), 1.0, kNullId);
328 return e.Access();
329 }
330} // namespace graph
331} // namespace GZ_MATH_VERSION_NAMESPACE
332} // namespace gz::math
333#endif // GZ_MATH_GRAPH_EDGE_HH_