00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00029 #ifndef __vtkBoostGraphAdapter_h
00030 #define __vtkBoostGraphAdapter_h
00031
00032 #include "vtkDirectedGraph.h"
00033 #include "vtkDoubleArray.h"
00034 #include "vtkFloatArray.h"
00035 #include "vtkIdTypeArray.h"
00036 #include "vtkIntArray.h"
00037 #include "vtkMutableDirectedGraph.h"
00038 #include "vtkMutableUndirectedGraph.h"
00039 #include "vtkTree.h"
00040 #include "vtkUndirectedGraph.h"
00041 #include <stddef.h>
00042
00043
00044
00045 namespace boost {
00046 inline int
00047 get(
00048 vtkIntArray* & arr,
00049 vtkIdType& key)
00050 {
00051 return arr->GetValue(key);
00052 }
00053
00054 inline void
00055 put(
00056 vtkIntArray* arr,
00057 vtkIdType key,
00058 const int & value)
00059 {
00060 arr->InsertValue(key, value);
00061 }
00062 }
00063
00064 #include <vtksys/stl/utility>
00065
00066 #include <boost/config.hpp>
00067 #include <boost/iterator/iterator_facade.hpp>
00068 #include <boost/graph/graph_traits.hpp>
00069 #include <boost/graph/properties.hpp>
00070 #include <boost/graph/adjacency_iterator.hpp>
00071
00072
00073
00074
00075
00076 namespace boost {
00077
00078 class vtk_vertex_iterator :
00079 public iterator_facade<vtk_vertex_iterator,
00080 vtkIdType,
00081 bidirectional_traversal_tag,
00082 vtkIdType,
00083 vtkIdType>
00084 {
00085 public:
00086 explicit vtk_vertex_iterator(vtkIdType i = 0) : index(i) {}
00087
00088 private:
00089 vtkIdType dereference() const { return index; }
00090
00091 bool equal(const vtk_vertex_iterator& other) const
00092 { return index == other.index; }
00093
00094 void increment() { index++; }
00095 void decrement() { index--; }
00096
00097 vtkIdType index;
00098
00099 friend class iterator_core_access;
00100 };
00101
00102 class vtk_edge_iterator :
00103 public iterator_facade<vtk_edge_iterator,
00104 vtkEdgeType,
00105 forward_traversal_tag,
00106 vtkEdgeType,
00107 vtkIdType>
00108 {
00109 public:
00110 explicit vtk_edge_iterator(vtkGraph *g = 0, vtkIdType v = 0) :
00111 graph(g), vertex(v), iter(0), end(0), directed(false)
00112 {
00113 if (graph != 0 && vertex < graph->GetNumberOfVertices())
00114 {
00115 directed = (vtkDirectedGraph::SafeDownCast(graph) != 0);
00116 vtkIdType nedges;
00117 graph->GetOutEdges(vertex, iter, nedges);
00118 end = iter + nedges;
00119 }
00120 }
00121
00122
00123 private:
00124 vtkEdgeType dereference() const
00125 { return vtkEdgeType(vertex, iter->Target, iter->Id); }
00126
00127 bool equal(const vtk_edge_iterator& other) const
00128 { return iter == other.iter; }
00129
00130 void increment()
00131 {
00132 inc();
00133
00134
00135 if (!directed)
00136 {
00137 while (iter != 0 && vertex > iter->Target)
00138 {
00139 inc();
00140 }
00141 }
00142 }
00143
00144 void inc()
00145 {
00146 ++iter;
00147 if (iter != 0 && iter == end)
00148 {
00149 ++vertex;
00150 if (graph != 0 && vertex < graph->GetNumberOfVertices())
00151 {
00152 vtkIdType nedges;
00153 graph->GetOutEdges(vertex, iter, nedges);
00154 end = iter + nedges;
00155 }
00156 else
00157 {
00158 iter = 0;
00159 }
00160 }
00161 }
00162
00163 bool directed;
00164 vtkIdType vertex;
00165 const vtkOutEdgeType * iter;
00166 const vtkOutEdgeType * end;
00167 vtkGraph *graph;
00168
00169 friend class iterator_core_access;
00170 };
00171
00172 class vtk_out_edge_pointer_iterator :
00173 public iterator_facade<vtk_out_edge_pointer_iterator,
00174 vtkEdgeType,
00175 bidirectional_traversal_tag,
00176 vtkEdgeType,
00177 ptrdiff_t>
00178 {
00179 public:
00180 explicit vtk_out_edge_pointer_iterator(vtkGraph *g = 0, vtkIdType v = 0, bool end = false) :
00181 vertex(v)
00182 {
00183 if (g)
00184 {
00185 vtkIdType nedges;
00186 g->GetOutEdges(vertex, iter, nedges);
00187 if (end)
00188 {
00189 iter += nedges;
00190 }
00191 }
00192 }
00193
00194 private:
00195 vtkEdgeType dereference() const { return vtkEdgeType(vertex, iter->Target, iter->Id); }
00196
00197 bool equal(const vtk_out_edge_pointer_iterator& other) const
00198 { return iter == other.iter; }
00199
00200 void increment() { iter++; }
00201 void decrement() { iter--; }
00202
00203 vtkIdType vertex;
00204 const vtkOutEdgeType *iter;
00205
00206 friend class iterator_core_access;
00207 };
00208
00209 class vtk_in_edge_pointer_iterator :
00210 public iterator_facade<vtk_in_edge_pointer_iterator,
00211 vtkEdgeType,
00212 bidirectional_traversal_tag,
00213 vtkEdgeType,
00214 ptrdiff_t>
00215 {
00216 public:
00217 explicit vtk_in_edge_pointer_iterator(vtkGraph *g = 0, vtkIdType v = 0, bool end = false) :
00218 vertex(v)
00219 {
00220 if (g)
00221 {
00222 vtkIdType nedges;
00223 g->GetInEdges(vertex, iter, nedges);
00224 if (end)
00225 {
00226 iter += nedges;
00227 }
00228 }
00229 }
00230
00231 private:
00232 vtkEdgeType dereference() const { return vtkEdgeType(iter->Source, vertex, iter->Id); }
00233
00234 bool equal(const vtk_in_edge_pointer_iterator& other) const
00235 { return iter == other.iter; }
00236
00237 void increment() { iter++; }
00238 void decrement() { iter--; }
00239
00240 vtkIdType vertex;
00241 const vtkInEdgeType *iter;
00242
00243 friend class iterator_core_access;
00244 };
00245
00246
00247
00248
00249
00250
00251
00252 struct vtkGraph_traversal_category :
00253 public virtual bidirectional_graph_tag,
00254 public virtual edge_list_graph_tag,
00255 public virtual vertex_list_graph_tag,
00256 public virtual adjacency_graph_tag { };
00257
00258 template <>
00259 struct graph_traits<vtkGraph*> {
00260 typedef vtkIdType vertex_descriptor;
00261 static vertex_descriptor null_vertex() { return -1; }
00262 typedef vtkEdgeType edge_descriptor;
00263 static edge_descriptor null_edge() { return vtkEdgeType(-1, -1, -1); }
00264 typedef vtk_out_edge_pointer_iterator out_edge_iterator;
00265 typedef vtk_in_edge_pointer_iterator in_edge_iterator;
00266
00267 typedef vtk_vertex_iterator vertex_iterator;
00268 typedef vtk_edge_iterator edge_iterator;
00269
00270 typedef allow_parallel_edge_tag edge_parallel_category;
00271 typedef vtkGraph_traversal_category traversal_category;
00272 typedef vtkIdType vertices_size_type;
00273 typedef vtkIdType edges_size_type;
00274 typedef vtkIdType degree_size_type;
00275
00276 typedef adjacency_iterator_generator<vtkGraph*,
00277 vertex_descriptor, out_edge_iterator>::type adjacency_iterator;
00278 };
00279
00280
00281
00282
00283
00284 template <>
00285 struct graph_traits<vtkDirectedGraph*> : graph_traits<vtkGraph*>
00286 {
00287 typedef directed_tag directed_category;
00288 };
00289
00290
00291 template <>
00292 struct graph_traits<const vtkDirectedGraph*> : graph_traits<vtkDirectedGraph*> { };
00293
00294
00295 template <>
00296 struct graph_traits<vtkDirectedGraph* const> : graph_traits<vtkDirectedGraph*> { };
00297
00298
00299
00300
00301 template <>
00302 struct graph_traits<vtkTree*> : graph_traits<vtkDirectedGraph*> { };
00303
00304
00305 template <>
00306 struct graph_traits<const vtkTree*> : graph_traits<vtkTree*> { };
00307
00308
00309 template <>
00310 struct graph_traits<vtkTree* const> : graph_traits<vtkTree*> { };
00311
00312
00313
00314 template <>
00315 struct graph_traits<vtkUndirectedGraph*> : graph_traits<vtkGraph*>
00316 {
00317 typedef undirected_tag directed_category;
00318 };
00319
00320
00321 template <>
00322 struct graph_traits<const vtkUndirectedGraph*> : graph_traits<vtkUndirectedGraph*> { };
00323
00324
00325 template <>
00326 struct graph_traits<vtkUndirectedGraph* const> : graph_traits<vtkUndirectedGraph*> { };
00327
00328
00329
00330
00331 template <>
00332 struct graph_traits<vtkMutableDirectedGraph*> : graph_traits<vtkDirectedGraph*> { };
00333
00334
00335 template <>
00336 struct graph_traits<const vtkMutableDirectedGraph*> : graph_traits<vtkMutableDirectedGraph*> { };
00337
00338
00339 template <>
00340 struct graph_traits<vtkMutableDirectedGraph* const> : graph_traits<vtkMutableDirectedGraph*> { };
00341
00342
00343
00344
00345 template <>
00346 struct graph_traits<vtkMutableUndirectedGraph*> : graph_traits<vtkUndirectedGraph*> { };
00347
00348
00349 template <>
00350 struct graph_traits<const vtkMutableUndirectedGraph*> : graph_traits<vtkMutableUndirectedGraph*> { };
00351
00352
00353 template <>
00354 struct graph_traits<vtkMutableUndirectedGraph* const> : graph_traits<vtkMutableUndirectedGraph*> { };
00355
00356
00357
00358 template <>
00359 class vertex_property< vtkGraph* > {
00360 public:
00361 typedef vtkIdType type;
00362 };
00363
00364 template <>
00365 class edge_property< vtkGraph* > {
00366 public:
00367 typedef vtkIdType type;
00368 };
00369
00370 inline graph_traits< vtkGraph* >::vertex_descriptor
00371 source(graph_traits< vtkGraph* >::edge_descriptor e,
00372 vtkGraph *)
00373 {
00374 return e.Source;
00375 }
00376
00377 inline graph_traits< vtkGraph* >::vertex_descriptor
00378 target(graph_traits< vtkGraph* >::edge_descriptor e,
00379 vtkGraph *)
00380 {
00381 return e.Target;
00382 }
00383
00384 inline vtksys_stl::pair<
00385 graph_traits< vtkGraph* >::vertex_iterator,
00386 graph_traits< vtkGraph* >::vertex_iterator >
00387 vertices(vtkGraph *g)
00388 {
00389 typedef graph_traits< vtkGraph* >::vertex_iterator Iter;
00390 return vtksys_stl::make_pair( Iter(0), Iter(g->GetNumberOfVertices()) );
00391 }
00392
00393 inline vtksys_stl::pair<
00394 graph_traits< vtkGraph* >::edge_iterator,
00395 graph_traits< vtkGraph* >::edge_iterator >
00396 edges(vtkGraph *g)
00397 {
00398 typedef graph_traits< vtkGraph* >::edge_iterator Iter;
00399 return vtksys_stl::make_pair( Iter(g), Iter(g, g->GetNumberOfVertices()) );
00400 }
00401
00402 inline vtksys_stl::pair<
00403 graph_traits< vtkGraph* >::out_edge_iterator,
00404 graph_traits< vtkGraph* >::out_edge_iterator >
00405 out_edges(
00406 graph_traits< vtkGraph* >::vertex_descriptor u,
00407 vtkGraph *g)
00408 {
00409 typedef graph_traits< vtkGraph* >::out_edge_iterator Iter;
00410 vtksys_stl::pair<Iter, Iter> p = vtksys_stl::make_pair( Iter(g, u), Iter(g, u, true) );
00411 return p;
00412 }
00413
00414 inline vtksys_stl::pair<
00415 graph_traits< vtkGraph* >::in_edge_iterator,
00416 graph_traits< vtkGraph* >::in_edge_iterator >
00417 in_edges(
00418 graph_traits< vtkGraph* >::vertex_descriptor u,
00419 vtkGraph *g)
00420 {
00421 typedef graph_traits< vtkGraph* >::in_edge_iterator Iter;
00422 vtksys_stl::pair<Iter, Iter> p = vtksys_stl::make_pair( Iter(g, u), Iter(g, u, true) );
00423 return p;
00424 }
00425
00426 inline vtksys_stl::pair<
00427 graph_traits< vtkGraph* >::adjacency_iterator,
00428 graph_traits< vtkGraph* >::adjacency_iterator >
00429 adjacent_vertices(
00430 graph_traits< vtkGraph* >::vertex_descriptor u,
00431 vtkGraph *g)
00432 {
00433 typedef graph_traits< vtkGraph* >::adjacency_iterator Iter;
00434 typedef graph_traits< vtkGraph* >::out_edge_iterator OutEdgeIter;
00435 vtksys_stl::pair<OutEdgeIter, OutEdgeIter> out = out_edges(u, g);
00436 return vtksys_stl::make_pair( Iter(out.first, &g), Iter(out.second, &g) );
00437 }
00438
00439 inline graph_traits< vtkGraph* >::vertices_size_type
00440 num_vertices(vtkGraph *g)
00441 {
00442 return g->GetNumberOfVertices();
00443 }
00444
00445 inline graph_traits< vtkGraph* >::edges_size_type
00446 num_edges(vtkGraph *g)
00447 {
00448 return g->GetNumberOfEdges();
00449 }
00450
00451 inline graph_traits< vtkGraph* >::degree_size_type
00452 out_degree(
00453 graph_traits< vtkGraph* >::vertex_descriptor u,
00454 vtkGraph *g)
00455 {
00456 return g->GetOutDegree(u);
00457 }
00458
00459 inline graph_traits< vtkDirectedGraph* >::degree_size_type
00460 in_degree(
00461 graph_traits< vtkDirectedGraph* >::vertex_descriptor u,
00462 vtkDirectedGraph *g)
00463 {
00464 return g->GetInDegree(u);
00465 }
00466
00467 inline graph_traits< vtkGraph* >::degree_size_type
00468 degree(
00469 graph_traits< vtkGraph* >::vertex_descriptor u,
00470 vtkGraph *g)
00471 {
00472 return g->GetDegree(u);
00473 }
00474
00475 inline graph_traits< vtkMutableDirectedGraph* >::vertex_descriptor
00476 add_vertex(vtkMutableDirectedGraph *g)
00477 {
00478 return g->AddVertex();
00479 }
00480
00481 inline vtksys_stl::pair<
00482 graph_traits< vtkMutableDirectedGraph* >::edge_descriptor,
00483 bool>
00484 add_edge(
00485 graph_traits< vtkMutableDirectedGraph* >::vertex_descriptor u,
00486 graph_traits< vtkMutableDirectedGraph* >::vertex_descriptor v,
00487 vtkMutableDirectedGraph *g)
00488 {
00489 graph_traits< vtkMutableDirectedGraph* >::edge_descriptor e = g->AddEdge(u, v);
00490 return vtksys_stl::make_pair(e, true);
00491 }
00492
00493 inline graph_traits< vtkMutableUndirectedGraph* >::vertex_descriptor
00494 add_vertex(vtkMutableUndirectedGraph *g)
00495 {
00496 return g->AddVertex();
00497 }
00498
00499 inline vtksys_stl::pair<
00500 graph_traits< vtkMutableUndirectedGraph* >::edge_descriptor,
00501 bool>
00502 add_edge(
00503 graph_traits< vtkMutableUndirectedGraph* >::vertex_descriptor u,
00504 graph_traits< vtkMutableUndirectedGraph* >::vertex_descriptor v,
00505 vtkMutableUndirectedGraph *g)
00506 {
00507 graph_traits< vtkMutableUndirectedGraph* >::edge_descriptor e = g->AddEdge(u, v);
00508 return vtksys_stl::make_pair(e, true);
00509 }
00510
00511
00512
00513
00514
00515 #define vtkPropertyMapMacro(T, V) \
00516 template <> \
00517 struct property_traits<T*> \
00518 { \
00519 typedef V value_type; \
00520 typedef V reference; \
00521 typedef vtkIdType key_type; \
00522 typedef read_write_property_map_tag category; \
00523 }; \
00524 \
00525 inline property_traits<T*>::reference \
00526 get( \
00527 T* const & arr, \
00528 property_traits<T*>::key_type key) \
00529 { \
00530 return arr->GetValue(key); \
00531 } \
00532 \
00533 inline void \
00534 put( \
00535 T* arr, \
00536 property_traits<T*>::key_type key, \
00537 const property_traits<T*>::value_type & value) \
00538 { \
00539 arr->InsertValue(key, value); \
00540 }
00541
00542 template <>
00543 struct property_traits<vtkIntArray*>
00544 {
00545 typedef int value_type;
00546 typedef int reference;
00547 typedef vtkIdType key_type;
00548 typedef read_write_property_map_tag category;
00549 };
00550
00551 vtkPropertyMapMacro(vtkIdTypeArray, vtkIdType)
00552 vtkPropertyMapMacro(vtkDoubleArray, double)
00553 vtkPropertyMapMacro(vtkFloatArray, float)
00554
00555
00556
00557
00558
00559 struct vtkGraphEdgeMap { };
00560
00561 template <>
00562 struct property_traits<vtkGraphEdgeMap>
00563 {
00564 typedef vtkIdType value_type;
00565 typedef vtkIdType reference;
00566 typedef vtkEdgeType key_type;
00567 typedef readable_property_map_tag category;
00568 };
00569
00570 inline property_traits<vtkGraphEdgeMap>::reference
00571 get(
00572 vtkGraphEdgeMap vtkNotUsed(arr),
00573 property_traits<vtkGraphEdgeMap>::key_type key)
00574 {
00575 return key.Id;
00576 }
00577
00578
00579
00580
00581
00582 template<typename PMap>
00583 class vtkGraphEdgePropertyMapHelper
00584 {
00585 public:
00586 vtkGraphEdgePropertyMapHelper(PMap m) : pmap(m) { }
00587 PMap pmap;
00588 typedef typename property_traits<PMap>::value_type value_type;
00589 typedef typename property_traits<PMap>::reference reference;
00590 typedef vtkEdgeType key_type;
00591 typedef typename property_traits<PMap>::category category;
00592 };
00593
00594 template<typename PMap>
00595 inline typename property_traits<PMap>::reference
00596 get(
00597 vtkGraphEdgePropertyMapHelper<PMap> helper,
00598 vtkEdgeType key)
00599 {
00600 return get(helper.pmap, key.Id);
00601 }
00602
00603 template<typename PMap>
00604 inline void
00605 put(
00606 vtkGraphEdgePropertyMapHelper<PMap> helper,
00607 vtkEdgeType key,
00608 const typename property_traits<PMap>::value_type & value)
00609 {
00610 put(helper.pmap, key.Id, value);
00611 }
00612
00613
00614
00615
00616
00617 struct vtkGraphIndexMap { };
00618
00619 template <>
00620 struct property_traits<vtkGraphIndexMap>
00621 {
00622 typedef vtkIdType value_type;
00623 typedef vtkIdType reference;
00624 typedef vtkIdType key_type;
00625 typedef readable_property_map_tag category;
00626 };
00627
00628 inline property_traits<vtkGraphIndexMap>::reference
00629 get(
00630 vtkGraphIndexMap vtkNotUsed(arr),
00631 property_traits<vtkGraphIndexMap>::key_type key)
00632 {
00633 return key;
00634 }
00635
00636 }
00637
00638
00639 #endif // __vtkBoostGraphAdapter_h