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.
20 #include "MLGraphUtilitiesSystem.h"
21 
22 #include <iterator>
23 #include <boost/version.hpp>
24 #include <boost/config.hpp>
25 #include <boost/operators.hpp>
26 #include <boost/graph/graph_traits.hpp>
27 #include <boost/graph/properties.hpp>
28 #include <boost/property_map/property_map.hpp>
29 
30 #include "mlGraphComponents.h"
31 
32 
33 ML_START_NAMESPACE
34 
35 // Following classes are inherited from Graph to define different types for a existing
36 // Graph object. Since the boost graph library defines the Graph properties via traits
37 // it is necessary to match different runtime states to different types.
38 // It will be necessary to cast a current Graph object to a pointer of one of the
39 // derived classes and use this pointer with the boost library
40 
42 class MLGRAPHUTILITIES_EXPORT UndirectedGraph : public Graph {};
43 
44 ML_END_NAMESPACE
45 
46 
47 
49 //
50 // Functions and traits for the plain ml::Graph class
51 //
53 
55 
56 
57 
60 
61 namespace boost {
62 
63 // forward declaration of an undirected edge
64 class ml_ug_edge;
65 
66  // typedef const UndirectedVesselGraph* ml_const_uv_graph_ptr;
67 
68 
69  // already defined by directed graph
79  public virtual incidence_graph_tag, // Graph is of type IncidenceGraph
80  public virtual vertex_list_graph_tag // Graph is of type VertexList
81  { };
82 
83 
85  template <> struct graph_traits<ml_undirected_graph_ptr> {
86 
87  // --- typedefs for boost::Graph ---
88 
90  typedef ml::VesselNode* vertex_descriptor;
91 
94 
96  typedef undirected_tag directed_category;
97 
101  typedef allow_parallel_edge_tag edge_parallel_category;
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 
136 } // namespace boost
137 
138 
139 namespace boost {
140 
145  {
146  typedef ml_ug_edge self;
147 
148  public:
149 
150 
151  ml_ug_edge() : _vertex(nullptr), _edge(nullptr) {};
152  ml_ug_edge(ml::VesselNode* vertex, ml::VesselEdge* edge) : _vertex(vertex), _edge(edge) {};
153 
154 
155  // source is always vertex
156  ml::VesselNode* source() {
157  return _vertex;
158  };
159 
160  // target is not _vertex
161  ml::VesselNode* target() {
162  if (_edge->isPred(_vertex))
163  {
164  return _edge->succNode();
165  }
166  return _edge->predNode();
167  };
168 
169  double weight() const { return _edge->getWeight(); };
170 
171  friend bool operator==(const self& a, const self& b ) {
172  return a._edge == b._edge;
173  }
174 
175  friend bool operator!=(const self& a, const self& b ) {
176  return !(a == b);
177  }
178 
179  protected:
180  ml::VesselNode* _vertex;
181  ml::VesselEdge* _edge;
182  };
183 
184 } // namespace boost
185 
189 class ml_ug_out_edge_iterator : public boost::forward_iterator_helper<
190  ml_ug_out_edge_iterator, // operand type
191  boost::ml_ug_edge, // value type
192  std::ptrdiff_t, // difference type
193  boost::ml_ug_edge*, // pointer type
194  boost::ml_ug_edge> // reference type
195 {
196 
197 typedef ml_ug_out_edge_iterator self;
198 
199 public:
200 
202 
204  std::vector<ml::VesselEdge*>::iterator edgeIter)
205  :_vertex(vertex)
206  ,_edgeIter(edgeIter)
207  {}
208 
210 
211  self& operator++()
212  {
213  ++_edgeIter;
214  return *this;
215  }
216 
217  friend bool operator==(const self& x, const self& y) {
218  return x._edgeIter == y._edgeIter; }
219 
220 protected:
221  ml::VesselNode* _vertex;
222  std::vector<ml::VesselEdge*>::iterator _edgeIter;
223 };
224 
225 
226 namespace boost {
227 
228  // --- functions required by boost::IncidenceGraph ---
229 
230 
235  {
236  return e.source();
237  }
238 
243  {
244  return e.target();
245  }
246 
259  inline std::pair<boost::graph_traits<ml_undirected_graph_ptr>::out_edge_iterator,
263  {
264  return std::make_pair(
267  }
268 
269 
276  {
278  }
279 
280 
281  // --- functions required by boost::VertexListGraph ---
282 
284  inline std::pair<boost::graph_traits<ml_undirected_graph_ptr>::vertex_iterator,
287  {
288  return std::make_pair( g->beginNode(),
289  g->endNode() );
290  }
291 
295  {
296  return static_cast<long> (g->numNodes());
297  }
298 
299 
303  {
304  return g->getNode(arrayIndex);
305  }
306 
307  // --- Property maps ---
308  // put_get_helper<class Reference, class LvaluePropertyMap> is a helper template which
309  // automatically generates the put() and get() functions for a property map
310 
311  // already defined by directed graph
314  class ml_ug_vertex_id_map : public boost::put_get_helper<unsigned int, ml_ug_vertex_id_map>
315  {
316  public:
317  // readonly property
318  typedef boost::readable_property_map_tag category;
319 
320  // vertex index is of type long
321  typedef unsigned int value_type;
322  typedef unsigned int reference;
323 
324  // key = Vertex
325  typedef ml::VesselNode* key_type;
326 
327  ml_ug_vertex_id_map() : _g(nullptr) {}
329  unsigned int operator[](ml::VesselNode* vertex) const { return static_cast<unsigned int>(_g->getIndex(vertex)); }
330 
331  protected:
333  };
334 
338  {
339  return ml_ug_vertex_id_map(g);
340  }
341 
342  // --- Property Map Traits classes ---
343  template<>
344  struct property_map<ml_undirected_graph_ptr, vertex_index_t>
345  {
348  };
349 
350 
351  //
352  class ml_ug_edge_weight_map : public boost::put_get_helper<double, ml_ug_edge_weight_map>
353  {
354  public:
355  // readonly property
356  typedef boost::lvalue_property_map_tag category;
357 
358  // vertex index is of type long
359  typedef double value_type;
360  typedef double const& reference;
361 
362  // key = edge
364 
366  //std::cout << "edge.getWeight() = " << edge.getWeight() << std::endl;
367  return edge.weight();
368  }
369  };
370 
372  {
373  return ml_ug_edge_weight_map();
374  }
375 
376  // --- Property Map Traits classes ---
377  template<>
378  struct property_map<ml_undirected_graph_ptr, edge_weight_t>
379  {
382  };
383 
384 
385 } // namespace boost
386 
387 #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 >::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 >::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 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...
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 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.
boost::graph_traits< ml_undirected_graph_ptr >::vertex_descriptor vertex(boost::graph_traits< ml_undirected_graph_ptr >::degree_size_type arrayIndex, const ml_undirected_graph_ptr g)
Returns the vertex at internal index.
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.
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.