VTK
dox/Infovis/vtkBoostGraphAdapter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkBoostGraphAdapter.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 /*-------------------------------------------------------------------------
00016   Copyright 2008 Sandia Corporation.
00017   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00018   the U.S. Government retains certain rights in this software.
00019 -------------------------------------------------------------------------*/
00029 #ifndef __vtkBoostGraphAdapter_h
00030 #define __vtkBoostGraphAdapter_h
00031 
00032 #include "vtkAbstractArray.h"
00033 #include "vtkDirectedGraph.h"
00034 #include "vtkDistributedGraphHelper.h"
00035 #include "vtkDataObject.h"
00036 #include "vtkDataArray.h"
00037 #include "vtkDoubleArray.h"
00038 #include "vtkFloatArray.h"
00039 #include "vtkIdTypeArray.h"
00040 #include "vtkInformation.h"
00041 #include "vtkIntArray.h"
00042 #include "vtkMutableDirectedGraph.h"
00043 #include "vtkMutableUndirectedGraph.h"
00044 #include "vtkTree.h"
00045 #include "vtkUndirectedGraph.h"
00046 #include "vtkVariant.h"
00047 
00048 #include <boost/version.hpp>
00049 
00050 namespace boost {
00051   //===========================================================================
00052   // VTK arrays as property maps
00053   // These need to be defined before including other boost stuff
00054 
00055   // Forward declarations are required here, so that we aren't forced
00056   // to include boost/property_map.hpp.
00057   template<typename> struct property_traits;
00058   struct read_write_property_map_tag;
00059 
00060 #define vtkPropertyMapMacro(T, V)                       \
00061   template <>                                           \
00062   struct property_traits<T*>                            \
00063     {                                                   \
00064     typedef V value_type;                               \
00065     typedef V reference;                                \
00066     typedef vtkIdType key_type;                         \
00067     typedef read_write_property_map_tag category;       \
00068     };                                                  \
00069                                                         \
00070   inline property_traits<T*>::reference                 \
00071   get(                                                  \
00072     T* const & arr,                                     \
00073     property_traits<T*>::key_type key)                  \
00074   {                                                     \
00075     return arr->GetValue(key);                          \
00076   }                                                     \
00077                                                         \
00078   inline void                                           \
00079   put(                                                  \
00080     T* arr,                                             \
00081     property_traits<T*>::key_type key,                  \
00082     const property_traits<T*>::value_type & value)      \
00083   {                                                     \
00084     arr->InsertValue(key, value);                       \
00085   }
00086 
00087   vtkPropertyMapMacro(vtkIntArray, int)
00088   vtkPropertyMapMacro(vtkIdTypeArray, vtkIdType)
00089   vtkPropertyMapMacro(vtkDoubleArray, double)
00090   vtkPropertyMapMacro(vtkFloatArray, float)
00091 
00092   // vtkDataArray
00093   template<>
00094   struct property_traits<vtkDataArray*>
00095   {
00096     typedef double value_type;
00097     typedef double reference;
00098     typedef vtkIdType  key_type;
00099     typedef read_write_property_map_tag category;
00100   };
00101 
00102   inline double
00103   get(vtkDataArray * const& arr, vtkIdType key)
00104   {
00105     return arr->GetTuple1(key);
00106   }
00107 
00108   inline void
00109   put(vtkDataArray *arr, vtkIdType key, const double& value)
00110   {
00111     arr->SetTuple1(key, value);
00112   }
00113 
00114   // vtkAbstractArray as a property map of vtkVariants
00115   template<>
00116   struct property_traits<vtkAbstractArray*>
00117   {
00118     typedef vtkVariant value_type;
00119     typedef vtkVariant reference;
00120     typedef vtkIdType  key_type;
00121     typedef read_write_property_map_tag category;
00122   };
00123 
00124   inline vtkVariant
00125   get(vtkAbstractArray * const& arr, vtkIdType key)
00126   {
00127     return arr->GetVariantValue(key);
00128   }
00129 
00130   inline void
00131   put(vtkAbstractArray *arr, vtkIdType key, const vtkVariant& value)
00132   {
00133     arr->InsertVariantValue(key, value);
00134   }
00135 }
00136 
00137 #include <vtksys/stl/utility> // STL Header
00138 
00139 #include <boost/config.hpp>
00140 #include <boost/iterator/iterator_facade.hpp>
00141 #include <boost/graph/graph_traits.hpp>
00142 #include <boost/graph/properties.hpp>
00143 #include <boost/graph/adjacency_iterator.hpp>
00144 
00145 // The functions and classes in this file allows the user to
00146 // treat a vtkDirectedGraph or vtkUndirectedGraph object
00147 // as a boost graph "as is".
00148 
00149 namespace boost {
00150 
00151   class vtk_vertex_iterator :
00152     public iterator_facade<vtk_vertex_iterator,
00153                            vtkIdType,
00154                            bidirectional_traversal_tag,
00155                            vtkIdType,
00156                            vtkIdType>
00157     {
00158     public:
00159       explicit vtk_vertex_iterator(vtkIdType i = 0) : index(i) {}
00160 
00161     private:
00162       vtkIdType dereference() const { return index; }
00163 
00164       bool equal(const vtk_vertex_iterator& other) const
00165         { return index == other.index; }
00166 
00167       void increment() { index++; }
00168       void decrement() { index--; }
00169 
00170       vtkIdType index;
00171 
00172       friend class iterator_core_access;
00173     };
00174 
00175   class vtk_edge_iterator :
00176     public iterator_facade<vtk_edge_iterator,
00177                            vtkEdgeType,
00178                            forward_traversal_tag,
00179                            vtkEdgeType,
00180                            vtkIdType>
00181     {
00182     public:
00183       explicit vtk_edge_iterator(vtkGraph *g = 0, vtkIdType v = 0) :
00184         directed(false), vertex(v), lastVertex(v), iter(0), end(0), graph(g)
00185         {
00186         if (graph)
00187           {
00188           lastVertex = graph->GetNumberOfVertices();
00189           }
00190 
00191         vtkIdType myRank = -1;
00192         vtkDistributedGraphHelper *helper
00193           = this->graph? this->graph->GetDistributedGraphHelper() : 0;
00194         if (helper)
00195           {
00196           myRank = this->graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
00197           vertex = helper->MakeDistributedId(myRank, vertex);
00198           lastVertex = helper->MakeDistributedId(myRank, lastVertex);
00199           }
00200 
00201         if (graph != 0)
00202           {
00203           directed = (vtkDirectedGraph::SafeDownCast(graph) != 0);
00204           while (vertex < lastVertex && this->graph->GetOutDegree(vertex) == 0)
00205             {
00206             ++vertex;
00207             }
00208 
00209           if (vertex < lastVertex)
00210             {
00211             // Get the outgoing edges of the first vertex that has outgoing
00212             // edges
00213             vtkIdType nedges;
00214             graph->GetOutEdges(vertex, iter, nedges);
00215             end = iter + nedges;
00216 
00217             if (!directed)
00218               {
00219               while(iter != 0
00220                     && (// Skip non-local edges
00221                         (helper && helper->GetEdgeOwner(iter->Id) != myRank)
00222                         // Skip entirely-local edges where Source > Target
00223                         || (((helper
00224                               && myRank == helper->GetVertexOwner(iter->Target))
00225                              || !helper)
00226                             && vertex > iter->Target)))
00227                 {
00228                 this->inc();
00229                 }
00230               }
00231             }
00232           else
00233             {
00234             iter = 0;
00235             }
00236           }
00237         }
00238 
00239     private:
00240       vtkEdgeType dereference() const
00241         { return vtkEdgeType(vertex, iter->Target, iter->Id); }
00242 
00243       bool equal(const vtk_edge_iterator& other) const
00244         { return vertex == other.vertex && iter == other.iter; }
00245 
00246       void increment()
00247         {
00248         inc();
00249         if (!directed)
00250           {
00251           vtkIdType myRank = -1;
00252           vtkDistributedGraphHelper *helper
00253             = this->graph? this->graph->GetDistributedGraphHelper() : 0;
00254           if (helper)
00255             {
00256             myRank = this->graph->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
00257             }
00258 
00259           while (iter != 0
00260                  && (// Skip non-local edges
00261                      (helper && helper->GetEdgeOwner(iter->Id) != myRank)
00262                      // Skip entirely-local edges where Source > Target
00263                      || (((helper
00264                            && myRank == helper->GetVertexOwner(iter->Target))
00265                           || !helper)
00266                          && vertex > iter->Target)))
00267             {
00268             inc();
00269             }
00270           }
00271         }
00272 
00273       void inc()
00274         {
00275         ++iter;
00276         if (iter == end)
00277           {
00278           // Find a vertex with nonzero out degree.
00279           ++vertex;
00280           while (vertex < lastVertex && this->graph->GetOutDegree(vertex) == 0)
00281             {
00282             ++vertex;
00283             }
00284 
00285           if (vertex < lastVertex)
00286             {
00287             vtkIdType nedges;
00288             graph->GetOutEdges(vertex, iter, nedges);
00289             end = iter + nedges;
00290             }
00291           else
00292             {
00293             iter = 0;
00294             }
00295           }
00296         }
00297 
00298       bool directed;
00299       vtkIdType vertex;
00300       vtkIdType lastVertex;
00301       const vtkOutEdgeType * iter;
00302       const vtkOutEdgeType * end;
00303       vtkGraph *graph;
00304 
00305       friend class iterator_core_access;
00306     };
00307 
00308   class vtk_out_edge_pointer_iterator :
00309     public iterator_facade<vtk_out_edge_pointer_iterator,
00310                            vtkEdgeType,
00311                            bidirectional_traversal_tag,
00312                            vtkEdgeType,
00313                            ptrdiff_t>
00314     {
00315     public:
00316       explicit vtk_out_edge_pointer_iterator(vtkGraph *g = 0, vtkIdType v = 0, bool end = false) :
00317         vertex(v)
00318       {
00319         if (g)
00320           {
00321           vtkIdType nedges;
00322           g->GetOutEdges(vertex, iter, nedges);
00323           if (end)
00324             {
00325             iter += nedges;
00326             }
00327           }
00328       }
00329 
00330     private:
00331       vtkEdgeType dereference() const { return vtkEdgeType(vertex, iter->Target, iter->Id); }
00332 
00333       bool equal(const vtk_out_edge_pointer_iterator& other) const
00334       { return iter == other.iter; }
00335 
00336       void increment() { iter++; }
00337       void decrement() { iter--; }
00338 
00339       vtkIdType vertex;
00340       const vtkOutEdgeType *iter;
00341 
00342       friend class iterator_core_access;
00343     };
00344 
00345   class vtk_in_edge_pointer_iterator :
00346     public iterator_facade<vtk_in_edge_pointer_iterator,
00347                            vtkEdgeType,
00348                            bidirectional_traversal_tag,
00349                            vtkEdgeType,
00350                            ptrdiff_t>
00351     {
00352     public:
00353       explicit vtk_in_edge_pointer_iterator(vtkGraph *g = 0, vtkIdType v = 0, bool end = false) :
00354         vertex(v)
00355       {
00356         if (g)
00357           {
00358           vtkIdType nedges;
00359           g->GetInEdges(vertex, iter, nedges);
00360           if (end)
00361             {
00362             iter += nedges;
00363             }
00364           }
00365       }
00366 
00367     private:
00368       vtkEdgeType dereference() const { return vtkEdgeType(iter->Source, vertex, iter->Id); }
00369 
00370       bool equal(const vtk_in_edge_pointer_iterator& other) const
00371       { return iter == other.iter; }
00372 
00373       void increment() { iter++; }
00374       void decrement() { iter--; }
00375 
00376       vtkIdType vertex;
00377       const vtkInEdgeType *iter;
00378 
00379       friend class iterator_core_access;
00380     };
00381 
00382   //===========================================================================
00383   // vtkGraph
00384   // VertexAndEdgeListGraphConcept
00385   // BidirectionalGraphConcept
00386   // AdjacencyGraphConcept
00387 
00388   struct vtkGraph_traversal_category :
00389     public virtual bidirectional_graph_tag,
00390     public virtual edge_list_graph_tag,
00391     public virtual vertex_list_graph_tag,
00392     public virtual adjacency_graph_tag { };
00393 
00394   template <>
00395   struct graph_traits<vtkGraph*> {
00396     typedef vtkIdType vertex_descriptor;
00397     static vertex_descriptor null_vertex() { return -1; }
00398     typedef vtkEdgeType edge_descriptor;
00399     static edge_descriptor null_edge() { return vtkEdgeType(-1, -1, -1); }
00400     typedef vtk_out_edge_pointer_iterator out_edge_iterator;
00401     typedef vtk_in_edge_pointer_iterator in_edge_iterator;
00402 
00403     typedef vtk_vertex_iterator vertex_iterator;
00404     typedef vtk_edge_iterator edge_iterator;
00405 
00406     typedef allow_parallel_edge_tag edge_parallel_category;
00407     typedef vtkGraph_traversal_category traversal_category;
00408     typedef vtkIdType vertices_size_type;
00409     typedef vtkIdType edges_size_type;
00410     typedef vtkIdType degree_size_type;
00411 
00412     typedef adjacency_iterator_generator<vtkGraph*,
00413       vertex_descriptor, out_edge_iterator>::type adjacency_iterator;
00414   };
00415 
00416 #if BOOST_VERSION >= 104500
00417   template<>
00418   struct graph_property_type< vtkGraph* > {
00419     typedef no_property type;
00420   };
00421 #endif
00422 
00423   template<>
00424   struct vertex_property_type< vtkGraph* > {
00425     typedef no_property type;
00426   };
00427 
00428   template<>
00429   struct edge_property_type< vtkGraph* > {
00430     typedef no_property type;
00431   };
00432 
00433 #if BOOST_VERSION >= 104500
00434   template<>
00435   struct graph_bundle_type< vtkGraph* > {
00436     typedef no_property type;
00437   };
00438 #endif
00439 
00440   template<>
00441   struct vertex_bundle_type< vtkGraph* > {
00442     typedef no_property type;
00443   };
00444 
00445   template<>
00446   struct edge_bundle_type< vtkGraph* > {
00447     typedef no_property type;
00448   };
00449 
00450   inline bool has_no_edges(vtkGraph* g)
00451     {
00452       return ((g->GetNumberOfEdges() > 0) ? false : true);
00453     }
00454 
00455   inline void remove_edge(graph_traits<vtkGraph*>::edge_descriptor e,
00456                           vtkGraph* g)
00457     {
00458     if(vtkMutableDirectedGraph::SafeDownCast(g))
00459       {
00460       vtkMutableDirectedGraph::SafeDownCast(g)->RemoveEdge(e.Id);
00461       }
00462     else if(vtkMutableUndirectedGraph::SafeDownCast(g))
00463       {
00464       vtkMutableUndirectedGraph::SafeDownCast(g)->RemoveEdge(e.Id);
00465       }
00466     }
00467 
00468   //===========================================================================
00469   // vtkDirectedGraph
00470 
00471   template <>
00472   struct graph_traits<vtkDirectedGraph*> : graph_traits<vtkGraph*>
00473   {
00474     typedef directed_tag directed_category;
00475   };
00476 
00477   // The graph_traits for a const graph are the same as a non-const graph.
00478   template <>
00479   struct graph_traits<const vtkDirectedGraph*> : graph_traits<vtkDirectedGraph*> { };
00480 
00481   // The graph_traits for a const graph are the same as a non-const graph.
00482   template <>
00483   struct graph_traits<vtkDirectedGraph* const> : graph_traits<vtkDirectedGraph*> { };
00484 
00485 #if BOOST_VERSION >= 104500
00486   // Internal graph properties
00487   template<>
00488   struct graph_property_type< vtkDirectedGraph* >
00489     : graph_property_type< vtkGraph* > { };
00490 
00491   // Internal graph properties
00492   template<>
00493   struct graph_property_type< vtkDirectedGraph* const >
00494     : graph_property_type< vtkGraph* > { };
00495 #endif
00496 
00497   // Internal vertex properties
00498   template<>
00499   struct vertex_property_type< vtkDirectedGraph* >
00500     : vertex_property_type< vtkGraph* > { };
00501 
00502   // Internal vertex properties
00503   template<>
00504   struct vertex_property_type< vtkDirectedGraph* const >
00505     : vertex_property_type< vtkGraph* > { };
00506 
00507   // Internal edge properties
00508   template<>
00509   struct edge_property_type< vtkDirectedGraph* >
00510     : edge_property_type< vtkGraph* > { };
00511 
00512   // Internal edge properties
00513   template<>
00514   struct edge_property_type< vtkDirectedGraph* const >
00515     : edge_property_type< vtkGraph* > { };
00516 
00517 #if BOOST_VERSION >= 104500
00518   // Internal graph properties
00519   template<>
00520   struct graph_bundle_type< vtkDirectedGraph* >
00521     : graph_bundle_type< vtkGraph* > { };
00522 
00523   // Internal graph properties
00524   template<>
00525   struct graph_bundle_type< vtkDirectedGraph* const >
00526     : graph_bundle_type< vtkGraph* > { };
00527 #endif
00528 
00529   // Internal vertex properties
00530   template<>
00531   struct vertex_bundle_type< vtkDirectedGraph* >
00532     : vertex_bundle_type< vtkGraph* > { };
00533 
00534   // Internal vertex properties
00535   template<>
00536   struct vertex_bundle_type< vtkDirectedGraph* const >
00537     : vertex_bundle_type< vtkGraph* > { };
00538 
00539   // Internal edge properties
00540   template<>
00541   struct edge_bundle_type< vtkDirectedGraph* >
00542     : edge_bundle_type< vtkGraph* > { };
00543 
00544   // Internal edge properties
00545   template<>
00546   struct edge_bundle_type< vtkDirectedGraph* const >
00547     : edge_bundle_type< vtkGraph* > { };
00548 
00549   //===========================================================================
00550   // vtkTree
00551 
00552   template <>
00553   struct graph_traits<vtkTree*> : graph_traits<vtkDirectedGraph*> { };
00554 
00555   // The graph_traits for a const graph are the same as a non-const graph.
00556   template <>
00557   struct graph_traits<const vtkTree*> : graph_traits<vtkTree*> { };
00558 
00559   // The graph_traits for a const graph are the same as a non-const graph.
00560   template <>
00561   struct graph_traits<vtkTree* const> : graph_traits<vtkTree*> { };
00562 
00563   //===========================================================================
00564   // vtkUndirectedGraph
00565   template <>
00566   struct graph_traits<vtkUndirectedGraph*> : graph_traits<vtkGraph*>
00567   {
00568     typedef undirected_tag directed_category;
00569   };
00570 
00571   // The graph_traits for a const graph are the same as a non-const graph.
00572   template <>
00573   struct graph_traits<const vtkUndirectedGraph*> : graph_traits<vtkUndirectedGraph*> { };
00574 
00575   // The graph_traits for a const graph are the same as a non-const graph.
00576   template <>
00577   struct graph_traits<vtkUndirectedGraph* const> : graph_traits<vtkUndirectedGraph*> { };
00578 
00579 #if BOOST_VERSION >= 104500
00580   // Internal graph properties
00581   template<>
00582   struct graph_property_type< vtkUndirectedGraph* >
00583     : graph_property_type< vtkGraph* > { };
00584 
00585   // Internal graph properties
00586   template<>
00587   struct graph_property_type< vtkUndirectedGraph* const >
00588     : graph_property_type< vtkGraph* > { };
00589 #endif
00590 
00591   // Internal vertex properties
00592   template<>
00593   struct vertex_property_type< vtkUndirectedGraph* >
00594     : vertex_property_type< vtkGraph* > { };
00595 
00596   // Internal vertex properties
00597   template<>
00598   struct vertex_property_type< vtkUndirectedGraph* const >
00599     : vertex_property_type< vtkGraph* > { };
00600 
00601   // Internal edge properties
00602   template<>
00603   struct edge_property_type< vtkUndirectedGraph* >
00604     : edge_property_type< vtkGraph* > { };
00605 
00606   // Internal edge properties
00607   template<>
00608   struct edge_property_type< vtkUndirectedGraph* const >
00609     : edge_property_type< vtkGraph* > { };
00610 
00611 #if BOOST_VERSION >= 104500
00612   // Internal graph properties
00613   template<>
00614   struct graph_bundle_type< vtkUndirectedGraph* >
00615     : graph_bundle_type< vtkGraph* > { };
00616 
00617   // Internal graph properties
00618   template<>
00619   struct graph_bundle_type< vtkUndirectedGraph* const >
00620     : graph_bundle_type< vtkGraph* > { };
00621 #endif
00622 
00623   // Internal vertex properties
00624   template<>
00625   struct vertex_bundle_type< vtkUndirectedGraph* >
00626     : vertex_bundle_type< vtkGraph* > { };
00627 
00628   // Internal vertex properties
00629   template<>
00630   struct vertex_bundle_type< vtkUndirectedGraph* const >
00631     : vertex_bundle_type< vtkGraph* > { };
00632 
00633   // Internal edge properties
00634   template<>
00635   struct edge_bundle_type< vtkUndirectedGraph* >
00636     : edge_bundle_type< vtkGraph* > { };
00637 
00638   // Internal edge properties
00639   template<>
00640   struct edge_bundle_type< vtkUndirectedGraph* const >
00641     : edge_bundle_type< vtkGraph* > { };
00642 
00643   //===========================================================================
00644   // vtkMutableDirectedGraph
00645 
00646   template <>
00647   struct graph_traits<vtkMutableDirectedGraph*> : graph_traits<vtkDirectedGraph*> { };
00648 
00649   // The graph_traits for a const graph are the same as a non-const graph.
00650   template <>
00651   struct graph_traits<const vtkMutableDirectedGraph*> : graph_traits<vtkMutableDirectedGraph*> { };
00652 
00653   // The graph_traits for a const graph are the same as a non-const graph.
00654   template <>
00655   struct graph_traits<vtkMutableDirectedGraph* const> : graph_traits<vtkMutableDirectedGraph*> { };
00656 
00657 #if BOOST_VERSION >= 104500
00658   // Internal graph properties
00659   template<>
00660   struct graph_property_type< vtkMutableDirectedGraph* >
00661     : graph_property_type< vtkDirectedGraph* > { };
00662 
00663   // Internal graph properties
00664   template<>
00665   struct graph_property_type< vtkMutableDirectedGraph* const >
00666     : graph_property_type< vtkDirectedGraph* > { };
00667 #endif
00668 
00669   // Internal vertex properties
00670   template<>
00671   struct vertex_property_type< vtkMutableDirectedGraph* >
00672     : vertex_property_type< vtkDirectedGraph* > { };
00673 
00674   // Internal vertex properties
00675   template<>
00676   struct vertex_property_type< vtkMutableDirectedGraph* const >
00677     : vertex_property_type< vtkDirectedGraph* > { };
00678 
00679   // Internal edge properties
00680   template<>
00681   struct edge_property_type< vtkMutableDirectedGraph* >
00682     : edge_property_type< vtkDirectedGraph* > { };
00683 
00684   // Internal edge properties
00685   template<>
00686   struct edge_property_type< vtkMutableDirectedGraph* const >
00687     : edge_property_type< vtkDirectedGraph* > { };
00688 
00689 #if BOOST_VERSION >= 104500
00690   // Internal graph properties
00691   template<>
00692   struct graph_bundle_type< vtkMutableDirectedGraph* >
00693     : graph_bundle_type< vtkDirectedGraph* > { };
00694 
00695   // Internal graph properties
00696   template<>
00697   struct graph_bundle_type< vtkMutableDirectedGraph* const >
00698     : graph_bundle_type< vtkDirectedGraph* > { };
00699 #endif
00700 
00701   // Internal vertex properties
00702   template<>
00703   struct vertex_bundle_type< vtkMutableDirectedGraph* >
00704     : vertex_bundle_type< vtkDirectedGraph* > { };
00705 
00706   // Internal vertex properties
00707   template<>
00708   struct vertex_bundle_type< vtkMutableDirectedGraph* const >
00709     : vertex_bundle_type< vtkDirectedGraph* > { };
00710 
00711   // Internal edge properties
00712   template<>
00713   struct edge_bundle_type< vtkMutableDirectedGraph* >
00714     : edge_bundle_type< vtkDirectedGraph* > { };
00715 
00716   // Internal edge properties
00717   template<>
00718   struct edge_bundle_type< vtkMutableDirectedGraph* const >
00719     : edge_bundle_type< vtkDirectedGraph* > { };
00720 
00721   //===========================================================================
00722   // vtkMutableUndirectedGraph
00723 
00724   template <>
00725   struct graph_traits<vtkMutableUndirectedGraph*> : graph_traits<vtkUndirectedGraph*> { };
00726 
00727   // The graph_traits for a const graph are the same as a non-const graph.
00728   template <>
00729   struct graph_traits<const vtkMutableUndirectedGraph*> : graph_traits<vtkMutableUndirectedGraph*> { };
00730 
00731   // The graph_traits for a const graph are the same as a non-const graph.
00732   template <>
00733   struct graph_traits<vtkMutableUndirectedGraph* const> : graph_traits<vtkMutableUndirectedGraph*> { };
00734 
00735 #if BOOST_VERSION >= 104500
00736   // Internal graph properties
00737   template<>
00738   struct graph_property_type< vtkMutableUndirectedGraph* >
00739     : graph_property_type< vtkUndirectedGraph* > { };
00740 
00741   // Internal graph properties
00742   template<>
00743   struct graph_property_type< vtkMutableUndirectedGraph* const >
00744     : graph_property_type< vtkUndirectedGraph* > { };
00745 #endif
00746 
00747   // Internal vertex properties
00748   template<>
00749   struct vertex_property_type< vtkMutableUndirectedGraph* >
00750     : vertex_property_type< vtkUndirectedGraph* > { };
00751 
00752   // Internal vertex properties
00753   template<>
00754   struct vertex_property_type< vtkMutableUndirectedGraph* const >
00755     : vertex_property_type< vtkUndirectedGraph* > { };
00756 
00757   // Internal edge properties
00758   template<>
00759   struct edge_property_type< vtkMutableUndirectedGraph* >
00760     : edge_property_type< vtkUndirectedGraph* > { };
00761 
00762   // Internal edge properties
00763   template<>
00764   struct edge_property_type< vtkMutableUndirectedGraph* const >
00765     : edge_property_type< vtkUndirectedGraph* > { };
00766 
00767 #if BOOST_VERSION >= 104500
00768   // Internal graph properties
00769   template<>
00770   struct graph_bundle_type< vtkMutableUndirectedGraph* >
00771     : graph_bundle_type< vtkUndirectedGraph* > { };
00772 
00773   // Internal graph properties
00774   template<>
00775   struct graph_bundle_type< vtkMutableUndirectedGraph* const >
00776     : graph_bundle_type< vtkUndirectedGraph* > { };
00777 #endif
00778 
00779   // Internal vertex properties
00780   template<>
00781   struct vertex_bundle_type< vtkMutableUndirectedGraph* >
00782     : vertex_bundle_type< vtkUndirectedGraph* > { };
00783 
00784   // Internal vertex properties
00785   template<>
00786   struct vertex_bundle_type< vtkMutableUndirectedGraph* const >
00787     : vertex_bundle_type< vtkUndirectedGraph* > { };
00788 
00789   // Internal edge properties
00790   template<>
00791   struct edge_bundle_type< vtkMutableUndirectedGraph* >
00792     : edge_bundle_type< vtkUndirectedGraph* > { };
00793 
00794   // Internal edge properties
00795   template<>
00796   struct edge_bundle_type< vtkMutableUndirectedGraph* const >
00797     : edge_bundle_type< vtkUndirectedGraph* > { };
00798 
00799   //===========================================================================
00800   // API implementation
00801   template <>
00802   class vertex_property< vtkGraph* > {
00803   public:
00804     typedef vtkIdType type;
00805   };
00806 
00807   template <>
00808   class edge_property< vtkGraph* > {
00809   public:
00810     typedef vtkIdType type;
00811   };
00812 } // end namespace boost
00813 
00814 inline boost::graph_traits< vtkGraph* >::vertex_descriptor
00815 source(boost::graph_traits< vtkGraph* >::edge_descriptor e,
00816        vtkGraph *)
00817 {
00818   return e.Source;
00819 }
00820 
00821 inline boost::graph_traits< vtkGraph* >::vertex_descriptor
00822 target(boost::graph_traits< vtkGraph* >::edge_descriptor e,
00823        vtkGraph *)
00824 {
00825   return e.Target;
00826 }
00827 
00828 inline vtksys_stl::pair<
00829   boost::graph_traits< vtkGraph* >::vertex_iterator,
00830   boost::graph_traits< vtkGraph* >::vertex_iterator >
00831 vertices(vtkGraph *g)
00832 {
00833   typedef boost::graph_traits< vtkGraph* >::vertex_iterator Iter;
00834   vtkIdType start = 0;
00835   if (vtkDistributedGraphHelper *helper = g->GetDistributedGraphHelper())
00836     {
00837     int rank =
00838       g->GetInformation()->Get(vtkDataObject::DATA_PIECE_NUMBER());
00839     start = helper->MakeDistributedId(rank, start);
00840     }
00841 
00842   return vtksys_stl::make_pair( Iter(start),
00843                                 Iter(start + g->GetNumberOfVertices()) );
00844 }
00845 
00846 inline vtksys_stl::pair<
00847   boost::graph_traits< vtkGraph* >::edge_iterator,
00848   boost::graph_traits< vtkGraph* >::edge_iterator >
00849 edges(vtkGraph *g)
00850 {
00851   typedef boost::graph_traits< vtkGraph* >::edge_iterator Iter;
00852   return vtksys_stl::make_pair( Iter(g), Iter(g, g->GetNumberOfVertices()) );
00853 }
00854 
00855 inline vtksys_stl::pair<
00856   boost::graph_traits< vtkGraph* >::out_edge_iterator,
00857   boost::graph_traits< vtkGraph* >::out_edge_iterator >
00858 out_edges(
00859   boost::graph_traits< vtkGraph* >::vertex_descriptor u,
00860   vtkGraph *g)
00861 {
00862   typedef boost::graph_traits< vtkGraph* >::out_edge_iterator Iter;
00863   vtksys_stl::pair<Iter, Iter> p = vtksys_stl::make_pair( Iter(g, u), Iter(g, u, true) );
00864   return p;
00865 }
00866 
00867 inline vtksys_stl::pair<
00868   boost::graph_traits< vtkGraph* >::in_edge_iterator,
00869   boost::graph_traits< vtkGraph* >::in_edge_iterator >
00870 in_edges(
00871   boost::graph_traits< vtkGraph* >::vertex_descriptor u,
00872   vtkGraph *g)
00873 {
00874   typedef boost::graph_traits< vtkGraph* >::in_edge_iterator Iter;
00875   vtksys_stl::pair<Iter, Iter> p = vtksys_stl::make_pair( Iter(g, u), Iter(g, u, true) );
00876   return p;
00877 }
00878 
00879 inline vtksys_stl::pair<
00880   boost::graph_traits< vtkGraph* >::adjacency_iterator,
00881   boost::graph_traits< vtkGraph* >::adjacency_iterator >
00882 adjacent_vertices(
00883   boost::graph_traits< vtkGraph* >::vertex_descriptor u,
00884   vtkGraph *g)
00885 {
00886   typedef boost::graph_traits< vtkGraph* >::adjacency_iterator Iter;
00887   typedef boost::graph_traits< vtkGraph* >::out_edge_iterator OutEdgeIter;
00888   vtksys_stl::pair<OutEdgeIter, OutEdgeIter> out = out_edges(u, g);
00889   return vtksys_stl::make_pair( Iter(out.first, &g), Iter(out.second, &g) );
00890 }
00891 
00892 inline boost::graph_traits< vtkGraph* >::vertices_size_type
00893 num_vertices(vtkGraph *g)
00894 {
00895   return g->GetNumberOfVertices();
00896 }
00897 
00898 inline boost::graph_traits< vtkGraph* >::edges_size_type
00899 num_edges(vtkGraph *g)
00900 {
00901   return g->GetNumberOfEdges();
00902 }
00903 
00904 inline boost::graph_traits< vtkGraph* >::degree_size_type
00905 out_degree(
00906   boost::graph_traits< vtkGraph* >::vertex_descriptor u,
00907   vtkGraph *g)
00908 {
00909   return g->GetOutDegree(u);
00910 }
00911 
00912 inline boost::graph_traits< vtkDirectedGraph* >::degree_size_type
00913 in_degree(
00914   boost::graph_traits< vtkDirectedGraph* >::vertex_descriptor u,
00915   vtkDirectedGraph *g)
00916 {
00917   return g->GetInDegree(u);
00918 }
00919 
00920 inline boost::graph_traits< vtkGraph* >::degree_size_type
00921 degree(
00922   boost::graph_traits< vtkGraph* >::vertex_descriptor u,
00923   vtkGraph *g)
00924 {
00925   return g->GetDegree(u);
00926 }
00927 
00928 inline boost::graph_traits< vtkMutableDirectedGraph* >::vertex_descriptor
00929 add_vertex(vtkMutableDirectedGraph *g)
00930 {
00931   return g->AddVertex();
00932 }
00933 
00934 inline vtksys_stl::pair<
00935   boost::graph_traits< vtkMutableDirectedGraph* >::edge_descriptor,
00936   bool>
00937 add_edge(
00938   boost::graph_traits< vtkMutableDirectedGraph* >::vertex_descriptor u,
00939   boost::graph_traits< vtkMutableDirectedGraph* >::vertex_descriptor v,
00940   vtkMutableDirectedGraph *g)
00941 {
00942   boost::graph_traits< vtkMutableDirectedGraph* >::edge_descriptor e = g->AddEdge(u, v);
00943   return vtksys_stl::make_pair(e, true);
00944 }
00945 
00946 inline boost::graph_traits< vtkMutableUndirectedGraph* >::vertex_descriptor
00947 add_vertex(vtkMutableUndirectedGraph *g)
00948 {
00949   return g->AddVertex();
00950 }
00951 
00952 inline vtksys_stl::pair<
00953   boost::graph_traits< vtkMutableUndirectedGraph* >::edge_descriptor,
00954   bool>
00955 add_edge(
00956   boost::graph_traits< vtkMutableUndirectedGraph* >::vertex_descriptor u,
00957   boost::graph_traits< vtkMutableUndirectedGraph* >::vertex_descriptor v,
00958   vtkMutableUndirectedGraph *g)
00959 {
00960   boost::graph_traits< vtkMutableUndirectedGraph* >::edge_descriptor e = g->AddEdge(u, v);
00961   return vtksys_stl::make_pair(e, true);
00962 }
00963 
00964 namespace boost {
00965   //===========================================================================
00966   // An edge map for vtkGraph.
00967   // This is a common input needed for algorithms.
00968 
00969   struct vtkGraphEdgeMap { };
00970 
00971   template <>
00972   struct property_traits<vtkGraphEdgeMap>
00973     {
00974     typedef vtkIdType value_type;
00975     typedef vtkIdType reference;
00976     typedef vtkEdgeType key_type;
00977     typedef readable_property_map_tag category;
00978     };
00979 
00980   inline property_traits<vtkGraphEdgeMap>::reference
00981   get(
00982     vtkGraphEdgeMap vtkNotUsed(arr),
00983     property_traits<vtkGraphEdgeMap>::key_type key)
00984   {
00985     return key.Id;
00986   }
00987 
00988   //===========================================================================
00989   // Helper for vtkGraph edge property maps
00990   // Automatically converts boost edge ids to vtkGraph edge ids.
00991 
00992   template<typename PMap>
00993   class vtkGraphEdgePropertyMapHelper
00994   {
00995   public:
00996     vtkGraphEdgePropertyMapHelper(PMap m) : pmap(m) { }
00997     PMap pmap;
00998     typedef typename property_traits<PMap>::value_type value_type;
00999     typedef typename property_traits<PMap>::reference reference;
01000     typedef vtkEdgeType key_type;
01001     typedef typename property_traits<PMap>::category category;
01002   };
01003 
01004   template<typename PMap>
01005   inline typename property_traits<PMap>::reference
01006   get(
01007     vtkGraphEdgePropertyMapHelper<PMap> helper,
01008     vtkEdgeType key)
01009   {
01010     return get(helper.pmap, key.Id);
01011   }
01012 
01013   template<typename PMap>
01014   inline void
01015   put(
01016     vtkGraphEdgePropertyMapHelper<PMap> helper,
01017     vtkEdgeType key,
01018     const typename property_traits<PMap>::value_type & value)
01019   {
01020     put(helper.pmap, key.Id, value);
01021   }
01022 
01023   //===========================================================================
01024   // An index map for vtkGraph
01025   // This is a common input needed for algorithms
01026 
01027   struct vtkGraphIndexMap { };
01028 
01029   template <>
01030   struct property_traits<vtkGraphIndexMap>
01031     {
01032     typedef vtkIdType value_type;
01033     typedef vtkIdType reference;
01034     typedef vtkIdType key_type;
01035     typedef readable_property_map_tag category;
01036     };
01037 
01038   inline property_traits<vtkGraphIndexMap>::reference
01039   get(
01040     vtkGraphIndexMap vtkNotUsed(arr),
01041     property_traits<vtkGraphIndexMap>::key_type key)
01042   {
01043     return key;
01044   }
01045 
01046   //===========================================================================
01047   // Helper for vtkGraph property maps
01048   // Automatically multiplies the property value by some value (default 1)
01049   template<typename PMap>
01050   class vtkGraphPropertyMapMultiplier
01051   {
01052   public:
01053     vtkGraphPropertyMapMultiplier(PMap m, float multi=1) : pmap(m),multiplier(multi){}
01054     PMap pmap;
01055     float multiplier;
01056     typedef typename property_traits<PMap>::value_type value_type;
01057     typedef typename property_traits<PMap>::reference reference;
01058     typedef typename property_traits<PMap>::key_type key_type;
01059     typedef typename property_traits<PMap>::category category;
01060   };
01061 
01062   template<typename PMap>
01063   inline typename property_traits<PMap>::reference
01064   get(
01065     vtkGraphPropertyMapMultiplier<PMap> multi,
01066     const typename property_traits<PMap>::key_type key)
01067   {
01068     return multi.multiplier * get(multi.pmap, key);
01069   }
01070 
01071   template<typename PMap>
01072   inline void
01073   put(
01074     vtkGraphPropertyMapMultiplier<PMap> multi,
01075     const typename property_traits<PMap>::key_type key,
01076     const typename property_traits<PMap>::value_type & value)
01077   {
01078     put(multi.pmap, key, value);
01079   }
01080 
01081   // Allow algorithms to automatically extract vtkGraphIndexMap from a
01082   // VTK graph
01083   template<>
01084   struct property_map<vtkGraph*, vertex_index_t>
01085   {
01086     typedef vtkGraphIndexMap type;
01087     typedef vtkGraphIndexMap const_type;
01088   };
01089 
01090   template<>
01091   struct property_map<vtkDirectedGraph*, vertex_index_t>
01092     : property_map<vtkGraph*, vertex_index_t> { };
01093 
01094   template<>
01095   struct property_map<vtkUndirectedGraph*, vertex_index_t>
01096     : property_map<vtkGraph*, vertex_index_t> { };
01097 
01098   inline vtkGraphIndexMap get(vertex_index_t, vtkGraph*) { return vtkGraphIndexMap(); }
01099 
01100   template<>
01101   struct property_map<vtkGraph*, edge_index_t>
01102   {
01103     typedef vtkGraphIndexMap type;
01104     typedef vtkGraphIndexMap const_type;
01105   };
01106 
01107   template<>
01108   struct property_map<vtkDirectedGraph*, edge_index_t>
01109     : property_map<vtkGraph*, edge_index_t> { };
01110 
01111   template<>
01112   struct property_map<vtkUndirectedGraph*, edge_index_t>
01113     : property_map<vtkGraph*, edge_index_t> { };
01114 
01115   inline vtkGraphIndexMap get(edge_index_t, vtkGraph*) { return vtkGraphIndexMap(); }
01116 
01117   // property_map specializations for const-qualified graphs
01118   template<>
01119   struct property_map<vtkDirectedGraph* const, vertex_index_t>
01120     : property_map<vtkDirectedGraph*, vertex_index_t> { };
01121 
01122   template<>
01123   struct property_map<vtkUndirectedGraph* const, vertex_index_t>
01124     : property_map<vtkUndirectedGraph*, vertex_index_t> { };
01125 
01126   template<>
01127   struct property_map<vtkDirectedGraph* const, edge_index_t>
01128     : property_map<vtkDirectedGraph*, edge_index_t> { };
01129 
01130   template<>
01131   struct property_map<vtkUndirectedGraph* const, edge_index_t>
01132     : property_map<vtkUndirectedGraph*, edge_index_t> { };
01133 } // namespace boost
01134 
01135 #if BOOST_VERSION > 104000
01136 #include <boost/property_map/vector_property_map.hpp>
01137 #else
01138 #include <boost/vector_property_map.hpp>
01139 #endif
01140 
01141 
01142 #endif // __vtkBoostGraphAdapter_h