vigra - vigra/graphs.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

graphs.hxx |

1 /************************************************************************/
2 /* */
3 /* Copyright 2011-2012 Stefan Schmidt and Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36 /**
37 * This header provides definitions of graph-related types
38 * and optionally provides a gateway to popular graph libraries
39 * (for now, BGL is supported).
40 */
41
42 #ifndef VIGRA_GRAPH_HXX
43 #define VIGRA_GRAPH_HXX
44
45 #include "metaprogramming.hxx"
46 #include "tinyvector.hxx"
47
48 #ifdef WITH_BOOST_GRAPH
49
50 # include <boost/tuple/tuple.hpp>
51 # include <boost/graph/graph_traits.hpp>
52 # include <boost/graph/properties.hpp>
53
54 #else // not WITH_BOOST_GRAPH
55
56 // emulate the BGL-style interface
57 namespace boost {
58
59 struct no_property {};
60
61 // tag classes were copied from boost:
62 // directed_category tags
63 struct directed_tag { };
64 struct undirected_tag { };
65 struct bidirectional_tag : public directed_tag { };
66
67 // traversal_category tags
68 struct incidence_graph_tag { };
69 struct adjacency_graph_tag { };
70 struct bidirectional_graph_tag : public incidence_graph_tag { };
71 struct vertex_list_graph_tag { };
72 struct edge_list_graph_tag { };
73 struct adjacency_matrix_tag { };
74
75 // edge_parallel_category tags
76 struct allow_parallel_edge_tag { };
77 struct disallow_parallel_edge_tag { };
78
79 // property maps:
80 struct readable_property_map_tag { };
81 struct writable_property_map_tag { };
82 struct read_write_property_map_tag
83 : public readable_property_map_tag,
84 public writable_property_map_tag {};
85 struct lvalue_property_map_tag
86 : public read_write_property_map_tag {};
87
88 struct vertex_index_t {};
89
90 struct edge_property_tag {};
91
92 #ifndef BOOST_TUPLE_HPP
93
94 // tie() support for std::pair, similar to Boost's one:
95 // (necessary because std::pair doesn't define a suitable assignment operator)
96 template<class T1, class T2>
97 class tie_adapter
98 {
99 public:
100 tie_adapter(T1 &x, T2 &y)
101 : x_(x), y_(y)
102 {}
103
104 template<class X, class Y>
105 tie_adapter & operator=(const std::pair<X, Y> &pair)
106 {
107 x_ = pair.first;
108 y_ = pair.second;
109 return *this;
110 }
111
112 protected:
113 T1 &x_;
114 T2 &y_;
115 };
116
117 template<class T1, class T2>
118 inline
119 tie_adapter<T1, T2>
120 tie(T1& t1, T2& t2)
121 {
122 return tie_adapter<T1, T2>(t1, t2);
123 }
124 #endif
125
126 // graph_traits class template
127 template <typename G>
128 struct graph_traits {
129 typedef typename G::vertex_descriptor vertex_descriptor;
130 typedef typename G::edge_descriptor edge_descriptor;
131 typedef typename G::adjacency_iterator adjacency_iterator;
132 typedef typename G::out_edge_iterator out_edge_iterator;
133 typedef typename G::in_edge_iterator in_edge_iterator;
134 typedef typename G::vertex_iterator vertex_iterator;
135 typedef typename G::edge_iterator edge_iterator;
136
137 typedef typename G::directed_category directed_category;
138 typedef typename G::edge_parallel_category edge_parallel_category;
139 typedef typename G::traversal_category traversal_category;
140
141 typedef typename G::vertices_size_type vertices_size_type;
142 typedef typename G::edges_size_type edges_size_type;
143 typedef typename G::degree_size_type degree_size_type;
144
145 static inline vertex_descriptor null_vertex()
146 {
147 return vertex_descriptor(-1);
148 }
149 };
150
151 // property_traits class template
152 template <typename PropMap>
153 struct property_traits
154 {
155 typedef typename PropMap::key_type key_type;
156 typedef typename PropMap::value_type value_type;
157 typedef typename PropMap::reference reference;
158 typedef typename PropMap::category category;
159 };
160
161 namespace {
162
163 vertex_index_t vertex_index;
164
165 } // anonymous namespace
166
167 } // namespace boost
168
169 #endif // WITH_BOOST_GRAPH
170
171 namespace vigra {
172 namespace boost_graph {
173
174 // vigra::boost_graph contains algorithms that are compatible to the Boost Graph Library
175 using namespace boost;
176
177 }} // namespace vigra::boost_graph
178
179
180
181
182 #if 0
183 namespace vigragraph {
184
185 // custom get_helper for read-only property maps (by-value)
186
187 template <class ValueType, class ReadablePropertyMap>
188 struct get_helper { };
189
190 template <class PropertyMap, class ValueType, class K>
191 inline ValueType
192 get(const get_helper<ValueType, PropertyMap>& pa, const K& k)
193 {
194 const ValueType v = static_cast<const PropertyMap&>(pa)[k];
195 return v;
196 }
197
198
199 // ! A fallback template for adjacent_vertices() called with
200 // a vertex_iterator
201 // (which may be specialized to be implemented more efficiently;
202 // the reason is that the iterator may have more information than
203 // the plain vertex_descriptor, e.g. it knows the neighborhood
204 // already which otherwise needs to be reconstructed.)
205 template<class GRAPH>
206 inline
207 std::pair<typename graph_traits<GRAPH>::adjacency_iterator,
208 typename graph_traits<GRAPH>::adjacency_iterator >
209 adjacent_vertices_at_iterator(typename graph_traits<GRAPH>::vertex_iterator const &v,
210 GRAPH const &g)
211 {
212 // the default implementation just derefences the iterator
213 // to yield a vertex_descriptor and forwards the call
214 std::cout << "FB" << std::endl;
215 return adjacent_vertices(*v, g);
216 }
217
218 } // namespace vigragraph
219 #endif
220
221 #ifdef WITH_LEMON
222 # include <lemon/core.h>
223 #else // not WITH_LEMON
224
225 // emulate the lemon interface
226 namespace lemon {
227
228 struct Invalid {
229 public:
230 bool operator==(Invalid) const { return true; }
231 bool operator!=(Invalid) const { return false; }
232 bool operator< (Invalid) const { return false; }
233
234 template <class T, int N>
235 operator vigra::TinyVector<T, N>() const
236 {
237 return vigra::TinyVector<T, N>(-1);
238 }
239 };
240
241 static const Invalid INVALID = Invalid();
242
243 typedef vigra::VigraTrueType True;
244 typedef vigra::VigraFalseType False;
245
246 } // namespace lemon
247
248 #endif // WITH_LEMON
249
250 namespace lemon {
251
252 template <class T>
253 inline bool operator==(T const & t, Invalid)
254 {
255 return t == T(Invalid());
256 }
257
258 template <class T>
259 inline bool operator==(Invalid, T const & t)
260 {
261 return t == T(Invalid());
262 }
263
264 template <class T>
265 inline bool operator!=(T const & t, Invalid)
266 {
267 return t != T(Invalid());
268 }
269
270 template <class T>
271 inline bool operator!=(Invalid, T const & t)
272 {
273 return t != T(Invalid());
274 }
275
276 } // namespace lemon
277
278 namespace vigra {
279 namespace lemon_graph {
280
281 // vigra::lemon_graph contains algorithms that are compatible to the LEMON graph library
282 using namespace lemon;
283
284 }} // namespace vigra::lemon_graph
285
286 #endif // VIGRA_GRAPH_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.10.0 (Mon Nov 18 2013)