MeVisLab Toolbox Reference
mlGraphToBoostUndirectedGraph.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2012, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_GRAPH_TO_BOOST_UNDIRECTED_GRAPH_H
14#define ML_GRAPH_TO_BOOST_UNDIRECTED_GRAPH_H
15
16
18
19// Include dll-specific settings.
21
22#include <iterator>
23#include <boost/operators.hpp>
24#include <boost/graph/graph_traits.hpp>
25#include <boost/graph/properties.hpp>
26#include <boost/property_map/property_map.hpp>
27
28#include "mlGraphComponents.h"
29
30
31ML_START_NAMESPACE
32
33// Following classes are inherited from Graph to define different types for a existing
34// Graph object. Since the boost graph library defines the Graph properties via traits
35// it is necessary to match different runtime states to different types.
36// It will be necessary to cast a current Graph object to a pointer of one of the
37// derived classes and use this pointer with the boost library
38
41
42ML_END_NAMESPACE
43
44
45
47//
48// Functions and traits for the plain ml::Graph class
49//
51
53
54
55
58
59namespace boost {
60
61// forward declaration of an undirected edge
62class ml_ug_edge;
63
64 // typedef const UndirectedVesselGraph* ml_const_uv_graph_ptr;
65
66
67 // already defined by directed graph
77 public virtual incidence_graph_tag, // Graph is of type IncidenceGraph
78 public virtual vertex_list_graph_tag // Graph is of type VertexList
79 { };
80
81
83 template <> struct graph_traits<ml_undirected_graph_ptr> {
84
85 // --- typedefs for boost::Graph ---
86
88 typedef ml::VesselNode* vertex_descriptor;
89
92
94 typedef undirected_tag directed_category;
95
99 typedef allow_parallel_edge_tag edge_parallel_category;
100
103
104 // --- typedefs for boost::IncidenceGraph ---
105
108 typedef long degree_size_type;
109
110 // An out-edge iterator for a vertex v provides access to the out-edges of the vertex. As such,
111 // the value type of an out-edge iterator is the edge descriptor type of its graph.
113
114 // --- typedefs for boost::VertexListGraph ---
115
118 typedef ml::Graph::NodeIterator vertex_iterator;
119
121 typedef long vertices_size_type;
122
123 // to do: add missing functions/traits for other boost graph models
124 /*
125 typedef void in_edge_iterator;
126 typedef sgb_adj_iterator adjacency_iterator;
127 typedef void edge_iterator;
128 typedef long edge_size_type;
129 */
130 };
131
132
133
134} // namespace boost
135
136
137namespace boost {
138
143 {
144 typedef ml_ug_edge self;
145
146 public:
147
148
149 ml_ug_edge() : _vertex(nullptr), _edge(nullptr) {};
150 ml_ug_edge(ml::VesselNode* vertex, ml::VesselEdge* edge) : _vertex(vertex), _edge(edge) {};
151
152
153 // source is always vertex
154 ml::VesselNode* source() {
155 return _vertex;
156 };
157
158 // target is not _vertex
159 ml::VesselNode* target() {
160 if (_edge->isPred(_vertex))
161 {
162 return _edge->succNode();
163 }
164 return _edge->predNode();
165 };
166
167 double weight() const { return _edge->getWeight(); };
168
169 friend bool operator==(const self& a, const self& b ) {
170 return a._edge == b._edge;
171 }
172
173 friend bool operator!=(const self& a, const self& b ) {
174 return !(a == b);
175 }
176
177 protected:
178 ml::VesselNode* _vertex;
179 ml::VesselEdge* _edge;
180 };
181
182} // namespace boost
183
187class ml_ug_out_edge_iterator : public boost::forward_iterator_helper<
188 ml_ug_out_edge_iterator, // operand type
189 boost::ml_ug_edge, // value type
190 std::ptrdiff_t, // difference type
191 boost::ml_ug_edge*, // pointer type
192 boost::ml_ug_edge> // reference type
193{
194
196
197public:
198
200
201 ml_ug_out_edge_iterator(ml::VesselNode* vertex,
202 std::vector<ml::VesselEdge*>::iterator edgeIter)
203 :_vertex(vertex)
204 ,_edgeIter(edgeIter)
205 {}
206
208
210 {
211 ++_edgeIter;
212 return *this;
213 }
214
215 friend bool operator==(const self& x, const self& y) {
216 return x._edgeIter == y._edgeIter; }
217
218protected:
219 ml::VesselNode* _vertex;
220 std::vector<ml::VesselEdge*>::iterator _edgeIter;
221};
222
223
224namespace boost {
225
226 // --- functions required by boost::IncidenceGraph ---
227
228
230 inline boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor
231 source(graph_traits<ml_undirected_graph_ptr>::edge_descriptor e,
233 {
234 return e.source();
235 }
236
238 inline boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor
239 target(graph_traits<ml_undirected_graph_ptr>::edge_descriptor e,
241 {
242 return e.target();
243 }
244
257 inline std::pair<boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator,
258 boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator>
259 out_edges(boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor vertex,
261 {
262 return std::make_pair(
263 boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator(vertex, vertex->edges()->begin()),
264 boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator(vertex, vertex->edges()->end()) );
265 }
266
267
271 inline boost::graph_traits<ml_undirected_graph_ptr>::degree_size_type
272 out_degree(boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor vertex,
274 {
275 return static_cast<boost::graph_traits<ml_undirected_graph_ptr>::degree_size_type>(vertex->getEdgeNum());
276 }
277
278
279 // --- functions required by boost::VertexListGraph ---
280
282 inline std::pair<boost::graph_traits<ml_undirected_graph_ptr>::vertex_iterator,
283 boost::graph_traits<ml_undirected_graph_ptr>::vertex_iterator>
285 {
286 return std::make_pair( g->beginNode(),
287 g->endNode() );
288 }
289
291 inline boost::graph_traits<ml_undirected_graph_ptr>::vertices_size_type
293 {
294 return static_cast<long> (g->numNodes());
295 }
296
297
299 inline boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor
300 vertex(boost::graph_traits<ml_undirected_graph_ptr>::degree_size_type arrayIndex, const ml_undirected_graph_ptr g)
301 {
302 return g->getNode(arrayIndex);
303 }
304
305 // --- Property maps ---
306 // put_get_helper<class Reference, class LvaluePropertyMap> is a helper template which
307 // automatically generates the put() and get() functions for a property map
308
309 // already defined by directed graph
312 class ml_ug_vertex_id_map : public boost::put_get_helper<unsigned int, ml_ug_vertex_id_map>
313 {
314 public:
315 // readonly property
316 typedef boost::readable_property_map_tag category;
317
318 // vertex index is of type long
319 typedef unsigned int value_type;
320 typedef unsigned int reference;
321
322 // key = Vertex
323 typedef ml::VesselNode* key_type;
324
325 ml_ug_vertex_id_map() : _g(nullptr) {}
327 unsigned int operator[](ml::VesselNode* vertex) const { return static_cast<unsigned int>(_g->getIndex(vertex)); }
328
329 protected:
331 };
332
336 {
337 return ml_ug_vertex_id_map(g);
338 }
339
340 // --- Property Map Traits classes ---
341 template<>
342 struct property_map<ml_undirected_graph_ptr, vertex_index_t>
343 {
346 };
347
348
349 //
350 class ml_ug_edge_weight_map : public boost::put_get_helper<double, ml_ug_edge_weight_map>
351 {
352 public:
353 // readonly property
354 typedef boost::lvalue_property_map_tag category;
355
356 // vertex index is of type long
357 typedef double value_type;
358 typedef double const& reference;
359
360 // key = edge
362
364 //std::cout << "edge.getWeight() = " << edge.getWeight() << std::endl;
365 return edge.weight();
366 }
367 };
368
370 {
371 return ml_ug_edge_weight_map();
372 }
373
374 // --- Property Map Traits classes ---
375 template<>
376 struct property_map<ml_undirected_graph_ptr, edge_weight_t>
377 {
380 };
381
382
383} // namespace boost
384
385#endif
#define MLGRAPHUTILITIES_EXPORT
Definiert systemspezifische Macros, die f"ur diese DLL gelten sollen.
value_type operator[](boost::ml_ug_edge edge) const
boost::lvalue_property_map_tag category
Simple wrapper to store an ml::VesselEdge together with a ml::VesselNode, so that the source() functi...
friend bool operator==(const self &a, const self &b)
ml_ug_edge(ml::VesselNode *vertex, ml::VesselEdge *edge)
friend bool operator!=(const self &a, const self &b)
Vertex id Maps each vertex to a unique id.
ml_ug_vertex_id_map(ml_undirected_graph_ptr g)
boost::readable_property_map_tag category
unsigned int operator[](ml::VesselNode *vertex) const
Tags a Graph object to be handled as undirected graph containing VesselEdges.
Iterator class to iterate over all out edges of a vertex (out_edge_iterator).
ml_ug_out_edge_iterator(ml::VesselNode *vertex, std::vector< ml::VesselEdge * >::iterator edgeIter)
std::vector< ml::VesselEdge * >::iterator _edgeIter
friend bool operator==(const self &x, const self &y)
ml::UndirectedGraph * ml_undirected_graph_ptr
typedef for use with graph_traits<>
Forward declaration for the boost::mutex class.
boost::graph_traits< ml_graph_ptr >::vertices_size_type num_vertices(const ml_graph_ptr g)
Returns the number of vertices in the graph g.
std::pair< boost::graph_traits< ml_graph_ptr >::out_edge_iterator, boost::graph_traits< ml_graph_ptr >::out_edge_iterator > out_edges(boost::graph_traits< ml_graph_ptr >::vertex_descriptor vertex, const ml_graph_ptr)
Returns an iterator-range providing access to the out-edges (for directed graphs) or incident edges (...
boost::graph_traits< ml_graph_ptr >::vertex_descriptor vertex(boost::graph_traits< ml_graph_ptr >::degree_size_type arrayIndex, const ml_graph_ptr g)
Returns the vertex at internal index.
ml_vertex_id_map get(vertex_index_t, ml_graph_ptr g)
get() function for vertex id property map vertex_index_t just necessary for overloading
std::pair< boost::graph_traits< ml_graph_ptr >::vertex_iterator, boost::graph_traits< ml_graph_ptr >::vertex_iterator > vertices(ml_graph_ptr g)
Returns an iterator-range providing access to all the vertices in the graph g.
boost::graph_traits< ml_graph_ptr >::vertex_descriptor source(graph_traits< ml_graph_ptr >::edge_descriptor e, const ml_graph_ptr)
Returns the vertex descriptor for u of the edge (u,v) represented by e.
boost::graph_traits< ml_graph_ptr >::degree_size_type out_degree(boost::graph_traits< ml_graph_ptr >::vertex_descriptor vertex, const ml_graph_ptr)
Returns the number of out-edges (for directed graphs) or the number of incident edges (for undirected...
boost::graph_traits< ml_graph_ptr >::vertex_descriptor target(graph_traits< ml_graph_ptr >::edge_descriptor e, const ml_graph_ptr)
Returns the vertex descriptor for v of the edge (u,v) represented by e.
ml_ug_edge edge_descriptor
The type for edge representative objects.
long degree_size_type
The unsigned intergral type used for representing the number out-edges or incident edges of a vertex.
long vertices_size_type
The unsigned integer type used to represent the number of vertices in the graph.
ml::Graph::NodeIterator vertex_iterator
A vertex iterator (obtained via vertices(g)) provides access to all of the vertices in a graph.
ml_undirected_graph_traversal_tag traversal_category
This describes the ways in which the vertices and edges of the graph can be visited.
ml::VesselNode * vertex_descriptor
The type for vertex representative objects.
allow_parallel_edge_tag edge_parallel_category
This describes whether the graph class allows the insertion of parallel edges (edges with the same so...
undirected_tag directed_category
This type shall be convertible to directed_tag or undirected_tag.
This describes the ways in which the vertices and edges of the graph can be visited.