File: vtkGraphToBoostAdapter.h

package info (click to toggle)
paraview 3.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 124,600 kB
  • ctags: 133,728
  • sloc: cpp: 958,817; ansic: 509,658; tcl: 45,787; xml: 23,401; python: 19,574; perl: 3,112; yacc: 1,787; java: 1,517; sh: 665; asm: 471; lex: 400; makefile: 168; objc: 28
file content (771 lines) | stat: -rw-r--r-- 23,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkGraphToBoostAdapter.h,v $

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/*----------------------------------------------------------------------------
 Copyright (c) Sandia Corporation
 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
// .NAME vtkGraphToBoostAdapter - adapter to the boost graph library (www.boost.org)
//
// .SECTION Description
// Including this header allows you to use a vtkGraph in boost algorithms.
// To do this, first wrap the class in a vtkBoostDirectedGraph or
// vtkBoostUndirectedGraph depending on whether your graph is directed or undirected.
// You may then use these objects directly in boost graph algorithms.

#ifndef __vtkGraphToBoostAdapter_h
#define __vtkGraphToBoostAdapter_h

#include <vtksys/stl/utility>

#include <boost/config.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/adjacency_iterator.hpp>
#include <boost/property_map.hpp>
#include <boost/vector_property_map.hpp>

#include "vtkGraph.h"
#include "vtkIntArray.h"
#include "vtkIdTypeArray.h"
#include "vtkFloatArray.h"
#include "vtkDoubleArray.h"

// The functions and classes in this file allows the user to
// treat a vtkBoostDirectedGraph object as a boost graph "as is". No
// wrapper is needed for the vtkAbstractGraph object.

namespace boost {

  struct vtkGraph_traversal_category : 
    public virtual bidirectional_graph_tag,
    public virtual edge_list_graph_tag,
    public virtual vertex_list_graph_tag,
    public virtual adjacency_graph_tag { };
    
  struct vtkBoostDirectedGraph;
  
  template <>
  struct graph_traits< vtkBoostDirectedGraph > {
    typedef vtkIdType vertex_descriptor;
    static vertex_descriptor null_vertex() { return -1; }
    typedef vtkIdType edge_descriptor;
    typedef vtkIdType* out_edge_iterator;
    typedef vtkIdType* in_edge_iterator;

    class index_iterator
      : public iterator_facade<index_iterator,
                               vtkIdType,
                               bidirectional_traversal_tag,
                               vtkIdType,
                               const vtkIdType*>
    {
    public:
      explicit index_iterator(vtkIdType i = 0) : index(i) {}

    private:
      vtkIdType dereference() const { return index; }

      bool equal(const index_iterator& other) const
      { return index == other.index; }

      void increment() { index++; }
      void decrement() { index--; }

      vtkIdType index;

      friend class iterator_core_access;
    };

    typedef index_iterator vertex_iterator;
    typedef index_iterator edge_iterator;

    typedef directed_tag directed_category;
    typedef allow_parallel_edge_tag edge_parallel_category;
    typedef vtkGraph_traversal_category traversal_category;
    typedef vtkIdType vertices_size_type;
    typedef vtkIdType edges_size_type;
    typedef vtkIdType degree_size_type;

    typedef adjacency_iterator_generator<vtkBoostDirectedGraph, vertex_descriptor, out_edge_iterator>::type adjacency_iterator;
  };


  //===========================================================================
  // vtkBoostDirectedGraph
  // VertexAndEdgeListGraphConcept
  // BidirectionalGraphConcept
  // MutableGraphConcept
  // AdjacencyGraphConcept
  struct vtkBoostDirectedGraph
  {
    vtkBoostDirectedGraph() 
    {
      this->Graph = vtkGraph::New();
      this->Graph->SetDirected(true);
    }

    vtkBoostDirectedGraph(vtkGraph* g) : Graph(g) 
    {
      assert(g->GetDirected());
      this->Graph->Register(NULL);
    }

    vtkBoostDirectedGraph(vtkBoostDirectedGraph& other)
    {
      this->Graph = other.Graph;
      this->Graph->Register(NULL);
    }

    ~vtkBoostDirectedGraph()
    {
      if (this->Graph != NULL)
        {
        this->Graph->Delete();
        this->Graph = NULL;
        }
    }

    vtkGraph* Graph;
  };

  // The graph_traits for a const graph are the same as a non-const graph.
  template <>
  struct graph_traits<const vtkBoostDirectedGraph> : graph_traits<vtkBoostDirectedGraph> { };
  
  template <>
  class vertex_property< vtkBoostDirectedGraph > {
  public:
    typedef vtkIdType type;
  };

  template <>
  class edge_property< vtkBoostDirectedGraph > {
  public:
    typedef vtkIdType type;
  };

  inline graph_traits< vtkBoostDirectedGraph >::vertex_descriptor
  source(graph_traits< vtkBoostDirectedGraph >::edge_descriptor e,
         const vtkBoostDirectedGraph& g)
  {
    return g.Graph->GetSourceVertex(e);
  }

  inline graph_traits< vtkBoostDirectedGraph >::vertex_descriptor
  target(graph_traits< vtkBoostDirectedGraph >::edge_descriptor e,
         const vtkBoostDirectedGraph& g)
  {
    return g.Graph->GetTargetVertex(e);
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostDirectedGraph >::vertex_iterator,
    graph_traits< vtkBoostDirectedGraph >::vertex_iterator >  
  vertices(const vtkBoostDirectedGraph& g)
  {
    typedef graph_traits< vtkBoostDirectedGraph >::vertex_iterator Iter;
    return vtksys_stl::make_pair( 0, g.Graph->GetNumberOfVertices() );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostDirectedGraph >::edge_iterator,
    graph_traits< vtkBoostDirectedGraph >::edge_iterator >  
  edges(const vtkBoostDirectedGraph& g)
  {
    typedef graph_traits< vtkBoostDirectedGraph >::edge_iterator
      Iter;
    return vtksys_stl::make_pair( Iter(0), Iter(g.Graph->GetNumberOfEdges()) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostDirectedGraph >::out_edge_iterator,
    graph_traits< vtkBoostDirectedGraph >::out_edge_iterator >  
  out_edges(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u, 
    const vtkBoostDirectedGraph& g)
  {
    typedef graph_traits< vtkBoostDirectedGraph >::out_edge_iterator Iter;
    vtkIdType nedges;
    const vtkIdType* edges;
    g.Graph->GetOutEdges(u, nedges, edges);
    return vtksys_stl::make_pair( Iter(edges), Iter(edges + nedges) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostDirectedGraph >::in_edge_iterator,
    graph_traits< vtkBoostDirectedGraph >::in_edge_iterator >  
  in_edges(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u, 
    const vtkBoostDirectedGraph& g)
  {
    typedef graph_traits< vtkBoostDirectedGraph >::in_edge_iterator Iter;
    vtkIdType nedges;
    const vtkIdType* edges;
    g.Graph->GetInEdges(u, nedges, edges);
    return vtksys_stl::make_pair( Iter(edges), Iter(edges + nedges) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostDirectedGraph >::adjacency_iterator,
    graph_traits< vtkBoostDirectedGraph >::adjacency_iterator >
  adjacent_vertices(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u, 
    const vtkBoostDirectedGraph& g)
  {
    typedef graph_traits< vtkBoostDirectedGraph >::adjacency_iterator Iter;
    typedef graph_traits< vtkBoostDirectedGraph >::out_edge_iterator OutEdgeIter;
    vtksys_stl::pair<OutEdgeIter, OutEdgeIter> out = out_edges(u, g);
    return vtksys_stl::make_pair( Iter(out.first, &g), Iter(out.second, &g) );
  }

  inline graph_traits< vtkBoostDirectedGraph >::vertices_size_type
  num_vertices(const vtkBoostDirectedGraph& g)
  {
    return g.Graph->GetNumberOfVertices();
  }  

  inline graph_traits< vtkBoostDirectedGraph >::edges_size_type
  num_edges(const vtkBoostDirectedGraph& g)
  {
    return g.Graph->GetNumberOfEdges();
  }  

  inline graph_traits< vtkBoostDirectedGraph >::degree_size_type
  out_degree(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u, 
    const vtkBoostDirectedGraph& g)
  {
    return g.Graph->GetOutDegree(u);
  }

  inline graph_traits< vtkBoostDirectedGraph >::degree_size_type
  in_degree(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u, 
    const vtkBoostDirectedGraph& g)
  {
    return g.Graph->GetInDegree(u);
  }

  inline graph_traits< vtkBoostDirectedGraph >::degree_size_type
  degree(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u, 
    const vtkBoostDirectedGraph& g)
  {
    return g.Graph->GetDegree(u);
  }
  
  inline graph_traits< vtkBoostDirectedGraph >::vertex_descriptor
  add_vertex(vtkBoostDirectedGraph& g)
  {
    return g.Graph->AddVertex();
  }

  inline void clear_vertex(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u,
    vtkBoostDirectedGraph& g)
  {
    g.Graph->ClearVertex(u);
  }

  inline void remove_vertex(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u,
    vtkBoostDirectedGraph& g)
  {
    g.Graph->RemoveVertex(u);
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostDirectedGraph >::edge_descriptor,
    bool>
  add_edge(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u,
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor v,
    vtkBoostDirectedGraph& g)
  {
    return vtksys_stl::make_pair(g.Graph->AddEdge(u, v), true);
  }

  inline void
  remove_edge(
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor u,
    graph_traits< vtkBoostDirectedGraph >::vertex_descriptor v,
    vtkBoostDirectedGraph& g)
  {
    // TODO: This may fail if multiple edges are removed and renumbered
    graph_traits< vtkBoostDirectedGraph >::out_edge_iterator 
      i,iend;
    for (boost::tie(i,iend) = out_edges(u,g); i != iend; ++i)
      {
      if (target(*i,g) == v)
        {
        g.Graph->RemoveEdge(*i);
        }
      }
  }

  inline void
  remove_edge(
    graph_traits< vtkBoostDirectedGraph >::edge_descriptor e,
    vtkBoostDirectedGraph& g)
  {
    g.Graph->RemoveEdge(e);
  }

  struct vtkBoostUndirectedGraph;
  
    template <>
  struct graph_traits< vtkBoostUndirectedGraph > {
    typedef vtkIdType vertex_descriptor;
    static vertex_descriptor null_vertex() { return -1; }

    typedef vtkIdType edge_descriptor;

    class edge_flip_iterator
      : public iterator_facade<edge_flip_iterator,
                               vtkIdType,
                               bidirectional_traversal_tag,
                               vtkIdType,
                               const vtkIdType*>
    {
    public:
      explicit edge_flip_iterator()
      : graph(NULL), ptr(NULL), ref(0), outgoing(true) {}

      explicit edge_flip_iterator(
        vtkGraph* g, const vtkIdType* p, vtkIdType r, bool out) 
      : graph(g), ptr(p), ref(r), outgoing(out) {}

    private:
      vtkIdType dereference() const
      {
        vtkIdType edge_id = *ptr;
        bool flip = outgoing ? ref == graph->GetTargetVertex(edge_id) : ref == graph->GetSourceVertex(edge_id);
        return flip ? -(edge_id + 1) : edge_id;
      }

      bool equal(const edge_flip_iterator& other) const
      {
        return graph == other.graph &&
          ptr == other.ptr &&
          ref == other.ref &&
          outgoing == other.outgoing;
      }

      void increment() { ptr++; }
      void decrement() { ptr--; }

      vtkGraph* graph;
      const vtkIdType* ptr;
      vtkIdType ref;
      bool outgoing;

      friend class iterator_core_access;
    };

    typedef edge_flip_iterator out_edge_iterator;
    typedef edge_flip_iterator in_edge_iterator;

    class index_iterator
      : public iterator_facade<index_iterator,
                               vtkIdType,
                               bidirectional_traversal_tag,
                               vtkIdType,
                               const vtkIdType*>
    {
    public:
      explicit index_iterator(vtkIdType i = 0) : index(i) {}

    private:
      vtkIdType dereference() const { return index; }

      bool equal(const index_iterator& other) const
      { return index == other.index; }

      void increment() { index++; }
      void decrement() { index--; }

      vtkIdType index;

      friend class iterator_core_access;
    };

    typedef index_iterator vertex_iterator;
    typedef index_iterator edge_iterator;

    typedef undirected_tag directed_category;
    typedef allow_parallel_edge_tag edge_parallel_category;
    typedef vtkGraph_traversal_category traversal_category;
    typedef vtkIdType vertices_size_type;
    typedef vtkIdType edges_size_type;
    typedef vtkIdType degree_size_type;

    typedef adjacency_iterator_generator<vtkBoostUndirectedGraph, vertex_descriptor, out_edge_iterator>::type adjacency_iterator;
  };


  
  //===========================================================================
  // vtkBoostUndirectedGraph
  // VertexAndEdgeListGraphConcept
  // BidirectionalGraphConcept
  // MutableGraphConcept
  // AdjacencyGraphConcept
  struct vtkBoostUndirectedGraph
  {
    vtkBoostUndirectedGraph() 
    {
      this->Graph = vtkGraph::New();
      this->Graph->SetDirected(true);
    }

    vtkBoostUndirectedGraph(vtkGraph* g) : Graph(g) 
    {
      assert(!g->GetDirected());
      this->Graph->Register(NULL);
    }

    vtkBoostUndirectedGraph(vtkBoostUndirectedGraph& other)
    {
      this->Graph = other.Graph;
      this->Graph->Register(NULL);
    }

    ~vtkBoostUndirectedGraph()
    {
      if (this->Graph != NULL)
        {
        this->Graph->Delete();
        this->Graph = NULL;
        }
    }

    vtkGraph* Graph;
  };

  // The graph_traits for a const graph are the same as a non-const graph.
  template <>
  struct graph_traits<const vtkBoostUndirectedGraph> : graph_traits<vtkBoostUndirectedGraph> { };

  template <>
  class vertex_property< vtkBoostUndirectedGraph > {
  public:
    typedef vtkIdType type;
  };

  template <>
  class edge_property< vtkBoostUndirectedGraph > {
  public:
    typedef vtkIdType type;
  };

  inline vtkIdType
  vtkGraph_edge_id(graph_traits< vtkBoostUndirectedGraph >::edge_descriptor e)
  {
    if (e < 0)
      {
      return -e-1;
      }
    return e;
  }

  inline graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor
  source(graph_traits< vtkBoostUndirectedGraph >::edge_descriptor e,
         const vtkBoostUndirectedGraph& g)
  {
    // If outgoing edge, return reference vertex; otherwise, return opposite vertex
    if (e < 0)
      {
      return g.Graph->GetTargetVertex(-e-1);
      }
    return g.Graph->GetSourceVertex(e);
  }

  inline graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor
  target(graph_traits< vtkBoostUndirectedGraph >::edge_descriptor e,
         const vtkBoostUndirectedGraph& g)
  {
    // If outgoing edge, return opposite vertex; otherwise, return reference vertex
    if (e < 0)
      {
      return g.Graph->GetSourceVertex(-e-1);
      }
    return g.Graph->GetTargetVertex(e);
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostUndirectedGraph >::vertex_iterator,
    graph_traits< vtkBoostUndirectedGraph >::vertex_iterator >  
  vertices(const vtkBoostUndirectedGraph& g)
  {
    typedef graph_traits< vtkBoostUndirectedGraph >::vertex_iterator Iter;
    return vtksys_stl::make_pair( Iter(0), Iter(g.Graph->GetNumberOfVertices()) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostUndirectedGraph >::edge_iterator,
    graph_traits< vtkBoostUndirectedGraph >::edge_iterator >  
  edges(const vtkBoostUndirectedGraph& g)
  {
    typedef graph_traits< vtkBoostUndirectedGraph >::edge_iterator
      Iter;
    return vtksys_stl::make_pair( Iter(0), Iter(g.Graph->GetNumberOfEdges()) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostUndirectedGraph >::out_edge_iterator,
    graph_traits< vtkBoostUndirectedGraph >::out_edge_iterator >  
  out_edges(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u, 
    const vtkBoostUndirectedGraph& g)
  {
    typedef graph_traits< vtkBoostUndirectedGraph >::out_edge_iterator Iter;
    vtkIdType nedges;
    const vtkIdType* edges;
    g.Graph->GetIncidentEdges(u, nedges, edges);
    return vtksys_stl::make_pair( Iter(g.Graph, edges, u, true), Iter(g.Graph, edges + nedges, u, true) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostUndirectedGraph >::in_edge_iterator,
    graph_traits< vtkBoostUndirectedGraph >::in_edge_iterator >  
  in_edges(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u, 
    const vtkBoostUndirectedGraph& g)
  {
    typedef graph_traits< vtkBoostUndirectedGraph >::in_edge_iterator Iter;
    vtkIdType nedges;
    const vtkIdType* edges;
    g.Graph->GetIncidentEdges(u, nedges, edges);
    return vtksys_stl::make_pair( Iter(g.Graph, edges, u, false), Iter(g.Graph, edges + nedges, u, false) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostUndirectedGraph >::adjacency_iterator,
    graph_traits< vtkBoostUndirectedGraph >::adjacency_iterator >
  adjacent_vertices(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u, 
    const vtkBoostUndirectedGraph& g)
  {
    typedef graph_traits< vtkBoostUndirectedGraph >::adjacency_iterator Iter;
    typedef graph_traits< vtkBoostUndirectedGraph >::out_edge_iterator OutEdgeIter;
    vtksys_stl::pair<OutEdgeIter, OutEdgeIter> out = out_edges(u, g);
    return vtksys_stl::make_pair( Iter(out.first, &g), Iter(out.second, &g) );
  }

  inline graph_traits< vtkBoostUndirectedGraph >::vertices_size_type
  num_vertices(const vtkBoostUndirectedGraph& g)
  {
    return g.Graph->GetNumberOfVertices();
  }  

  inline graph_traits< vtkBoostUndirectedGraph >::edges_size_type
  num_edges(const vtkBoostUndirectedGraph& g)
  {
    return g.Graph->GetNumberOfEdges();
  }  

  inline graph_traits< vtkBoostUndirectedGraph >::degree_size_type
  out_degree(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u, 
    const vtkBoostUndirectedGraph& g)
  {
    return g.Graph->GetDegree(u);
  }

  inline graph_traits< vtkBoostUndirectedGraph >::degree_size_type
  in_degree(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u, 
    const vtkBoostUndirectedGraph& g)
  {
    return g.Graph->GetDegree(u);
  }

  inline graph_traits< vtkBoostUndirectedGraph >::degree_size_type
  degree(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u, 
    const vtkBoostUndirectedGraph& g)
  {
    return g.Graph->GetDegree(u);
  }
  
  inline graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor
  add_vertex(vtkBoostUndirectedGraph& g)
  {
    return g.Graph->AddVertex();
  }

  inline void clear_vertex(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u,
    vtkBoostUndirectedGraph& g)
  {
    g.Graph->ClearVertex(u);
  }

  inline void remove_vertex(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u,
    vtkBoostUndirectedGraph& g)
  {
    g.Graph->RemoveVertex(u);
  }

  inline vtksys_stl::pair<
    graph_traits< vtkBoostUndirectedGraph >::edge_descriptor,
    bool>
  add_edge(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u,
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor v,
    vtkBoostUndirectedGraph& g)
  {
    typedef graph_traits< vtkBoostUndirectedGraph >::edge_descriptor Edge;
    Edge e = g.Graph->AddEdge(u, v);
    return vtksys_stl::make_pair(e, true);
  }

  inline void
  remove_edge(
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor u,
    graph_traits< vtkBoostUndirectedGraph >::vertex_descriptor v,
    vtkBoostUndirectedGraph& g)
  {
    // TODO: This may fail if multiple edges are removed and renumbered
    graph_traits< vtkBoostUndirectedGraph >::out_edge_iterator 
      i,iend;
    for (boost::tie(i,iend) = out_edges(u,g); i != iend; ++i)
      {
      if (target(*i,g) == v)
        {
        vtkIdType e = *i;
        if (e < 0)
          {
          g.Graph->RemoveEdge(-e-1);
          }
        else
          {
          g.Graph->RemoveEdge(e);
          }
        }
      }
  }

  inline void
  remove_edge(
    graph_traits< vtkBoostUndirectedGraph >::edge_descriptor e,
    vtkBoostUndirectedGraph& g)
  {
    if (e < 0)
      {
      g.Graph->RemoveEdge(-e-1);
      }
    else
      {
      g.Graph->RemoveEdge(e);
      }
  }


  //===========================================================================
  // VTK arrays as property maps

#define vtkPropertyMapMacro(T, V)                       \
  template <>                                           \
  struct property_traits<T*>                            \
    {                                                   \
    typedef V value_type;                               \
    typedef V reference;                                \
    typedef vtkIdType key_type;                         \
    typedef read_write_property_map_tag category;       \
    };                                                  \
                                                        \
  inline property_traits<T*>::reference                 \
  get(                                                  \
    T* arr,                                             \
    property_traits<T*>::key_type key)                  \
  {                                                     \
    return arr->GetValue(key);                          \
  }                                                     \
                                                        \
  inline void                                           \
  put(                                                  \
    T* arr,                                             \
    property_traits<T*>::key_type key,                  \
    const property_traits<T*>::value_type & value)      \
  {                                                     \
    arr->InsertValue(key, value);                       \
  }

  vtkPropertyMapMacro(vtkIdTypeArray, vtkIdType)
  vtkPropertyMapMacro(vtkIntArray, int)
  vtkPropertyMapMacro(vtkDoubleArray, double)
  vtkPropertyMapMacro(vtkFloatArray, float)

  template<typename PMap>
  class vtkGraphEdgePropertyMapHelper
  {
  public:
    vtkGraphEdgePropertyMapHelper(PMap m) : pmap(m) { }
    PMap pmap;
    typedef typename property_traits<PMap>::value_type value_type;
    typedef typename property_traits<PMap>::reference reference;
    typedef typename property_traits<PMap>::key_type key_type;
    typedef typename property_traits<PMap>::category category;
  };

  template<typename PMap>
  inline typename property_traits<PMap>::reference
  get(
    vtkGraphEdgePropertyMapHelper<PMap> helper,
    typename property_traits<PMap>::key_type key)
  {
    return get(helper.pmap, vtkGraph_edge_id(key));
  } 

  template<typename PMap>
  inline void
  put(
    vtkGraphEdgePropertyMapHelper<PMap> helper,
    typename property_traits<PMap>::key_type key,
    const typename property_traits<PMap>::value_type & value)
  {
    put(helper.pmap, vtkGraph_edge_id(key), value);
  }

  //===========================================================================
  // An index map for vtkGraph
  // This is a common input needed for algorithms

  struct vtkGraphIndexMap { };

  template <>
  struct property_traits<vtkGraphIndexMap>
    {
    typedef vtkIdType value_type;
    typedef vtkIdType reference;
    typedef vtkIdType key_type;
    typedef readable_property_map_tag category;
    };

  inline property_traits<vtkGraphIndexMap>::reference
  get(
    vtkGraphIndexMap vtkNotUsed(arr), 
    property_traits<vtkGraphIndexMap>::key_type key)
  {
    return key;
  }

} // namespace boost


#endif // __vtkGraphToBoostAdapter_h