MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
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{
42};
43
45
46
48//
49// Functions and traits for the plain ml::Graph class
50//
52
54
55
58
59namespace boost
60{
61
62 // forward declaration of an undirected edge
63 class ml_ug_edge;
64
65 // typedef const UndirectedVesselGraph* ml_const_uv_graph_ptr;
66
67
68 // already defined by directed graph
77 struct ml_undirected_graph_traversal_tag : 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
82
85 {
86
87 // --- typedefs for boost::Graph ---
88
90 typedef ml::VesselNode* vertex_descriptor;
91
94
97
102
105
106 // --- typedefs for boost::IncidenceGraph ---
107
110 typedef long degree_size_type;
111
112 // An out-edge iterator for a vertex v provides access to the out-edges of the vertex. As such,
113 // the value type of an out-edge iterator is the edge descriptor type of its graph.
115
116 // --- typedefs for boost::VertexListGraph ---
117
120 typedef ml::Graph::NodeIterator vertex_iterator;
121
123 typedef long vertices_size_type;
124
125 // to do: add missing functions/traits for other boost graph models
126 /*
127 typedef void in_edge_iterator;
128 typedef sgb_adj_iterator adjacency_iterator;
129 typedef void edge_iterator;
130 typedef long edge_size_type;
131 */
132 };
133
134
135} // namespace boost
136
137
138namespace boost
139{
140
145 {
146 typedef ml_ug_edge self;
147
148 public:
152 ml_ug_edge(ml::VesselNode* vertex, ml::VesselEdge* edge)
153 : _vertex(vertex)
154 , _edge(edge) {};
155
156
157 // source is always vertex
158 ml::VesselNode* source()
159 {
160 return _vertex;
161 };
162
163 // target is not _vertex
164 ml::VesselNode* target()
165 {
166 if (_edge->isPred(_vertex)) {
167 return _edge->succNode();
168 }
169 return _edge->predNode();
170 };
171
172 double weight() const
173 {
174 return _edge->getWeight();
175 };
176
177 friend bool operator==(const self& a, const self& b)
178 {
179 return a._edge == b._edge;
180 }
181
182 friend bool operator!=(const self& a, const self& b)
183 {
184 return !(a == b);
185 }
186
187 protected:
188 ml::VesselNode* _vertex;
189 ml::VesselEdge* _edge;
190 };
191
192} // namespace boost
193
197class ml_ug_out_edge_iterator : public boost::forward_iterator_helper<ml_ug_out_edge_iterator, // operand type
198 boost::ml_ug_edge, // value type
199 std::ptrdiff_t, // difference type
200 boost::ml_ug_edge*, // pointer type
201 boost::ml_ug_edge> // reference type
202{
203
205
206public:
210
211 ml_ug_out_edge_iterator(ml::VesselNode* vertex, std::vector<ml::VesselEdge*>::iterator edgeIter)
212 : _vertex(vertex)
214 {}
215
220
222 {
223 ++_edgeIter;
224 return *this;
225 }
226
227 friend bool operator==(const self& x, const self& y)
228 {
229 return x._edgeIter == y._edgeIter;
230 }
231
232protected:
233 ml::VesselNode* _vertex;
234 std::vector<ml::VesselEdge*>::iterator _edgeIter;
235};
236
237
238namespace boost
239{
240
241 // --- functions required by boost::IncidenceGraph ---
242
243
245 inline boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor
246 source(graph_traits<ml_undirected_graph_ptr>::edge_descriptor e, const ml_undirected_graph_ptr)
247 {
248 return e.source();
249 }
250
252 inline boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor
253 target(graph_traits<ml_undirected_graph_ptr>::edge_descriptor e, const ml_undirected_graph_ptr)
254 {
255 return e.target();
256 }
257
270 inline std::pair<boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator,
271 boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator>
272 out_edges(boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor vertex, const ml_undirected_graph_ptr)
273 {
274 return std::make_pair(
275 boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator(vertex, vertex->edges()->begin()),
276 boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator(vertex, vertex->edges()->end()));
277 }
278
279
283 inline boost::graph_traits<ml_undirected_graph_ptr>::degree_size_type
284 out_degree(boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor vertex, const ml_undirected_graph_ptr)
285 {
286 return static_cast<boost::graph_traits<ml_undirected_graph_ptr>::degree_size_type>(vertex->getEdgeNum());
287 }
288
289
290 // --- functions required by boost::VertexListGraph ---
291
293 inline std::pair<boost::graph_traits<ml_undirected_graph_ptr>::vertex_iterator,
294 boost::graph_traits<ml_undirected_graph_ptr>::vertex_iterator>
296 {
297 return std::make_pair(g->beginNode(), g->endNode());
298 }
299
301 inline boost::graph_traits<ml_undirected_graph_ptr>::vertices_size_type num_vertices(const ml_undirected_graph_ptr g)
302 {
303 return static_cast<long>(g->numNodes());
304 }
305
306
308 inline boost::graph_traits<ml_undirected_graph_ptr>::vertex_descriptor
309 vertex(boost::graph_traits<ml_undirected_graph_ptr>::degree_size_type arrayIndex, const ml_undirected_graph_ptr g)
310 {
311 return g->getNode(arrayIndex);
312 }
313
314 // --- Property maps ---
315 // put_get_helper<class Reference, class LvaluePropertyMap> is a helper template which
316 // automatically generates the put() and get() functions for a property map
317
318 // already defined by directed graph
321 class ml_ug_vertex_id_map : public boost::put_get_helper<unsigned int, ml_ug_vertex_id_map>
322 {
323 public:
324 // readonly property
325 typedef boost::readable_property_map_tag category;
326
327 // vertex index is of type long
328 typedef unsigned int value_type;
329 typedef unsigned int reference;
330
331 // key = Vertex
332 typedef ml::VesselNode* key_type;
333
340 unsigned int operator[](ml::VesselNode* vertex) const
341 {
342 return static_cast<unsigned int>(_g->getIndex(vertex));
343 }
344
345 protected:
347 };
348
355
356 // --- Property Map Traits classes ---
362
363
364 //
365 class ml_ug_edge_weight_map : public boost::put_get_helper<double, ml_ug_edge_weight_map>
366 {
367 public:
368 // readonly property
369 typedef boost::lvalue_property_map_tag category;
370
371 // vertex index is of type long
372 typedef double value_type;
373 typedef double const& reference;
374
375 // key = edge
377
379 {
380 // std::cout << "edge.getWeight() = " << edge.getWeight() << std::endl;
381 return edge.weight();
382 }
383 };
384
389
390 // --- Property Map Traits classes ---
396
397
398} // namespace boost
399
400#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<>
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
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.