File: vtkTreeToBoostAdapter.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 (288 lines) | stat: -rw-r--r-- 8,430 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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkTreeToBoostAdapter.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 vtkTreeToBoostAdapter - adapter to the boost graph library (www.boost.org)
//
// .SECTION Description
// Including this header allows you to use a vtkTree directly in boost algorithms.
// You may use a tree only in algorithms that do not modify the graph.

#ifndef __vtkTreeToBoostAdapter_h
#define __vtkTreeToBoostAdapter_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 "vtkTree.h"

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

namespace boost {

  //===========================================================================
  // vtkTree*
  // VertexAndEdgeListGraphConcept
  // BidirectionalGraphConcept
  // AdjacencyGraaphConcept

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

    class tree_edge_iterator
      : public iterator_facade<tree_edge_iterator,
                               vtkIdType,
                               bidirectional_traversal_tag,
                               vtkIdType,
                               const vtkIdType*>
    {
    public:
      explicit tree_edge_iterator(vtkTree* t, vtkIdType s, const vtkIdType* a)
        : tree(t), source(s), adj(a), cur(0) {}

      explicit tree_edge_iterator()
        : tree(NULL), source(0), adj(NULL), cur(0) {}

      vtkIdType dereference() const 
        {
        vtkIdType v = adj[cur];
        if (v == tree->GetParent(source))
          {
          v = source;
          }
        if (v == 0)
          {
          return tree->GetRoot() - 1;
          }
        return v - 1;
        }

      bool equal(const tree_edge_iterator& other) const
        { 
        return tree == other.tree &&
               (adj+cur) == (other.adj+other.cur) &&
               source == other.source;
        }

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

    private:
      vtkTree* tree;
      const vtkIdType* adj;
      vtkIdType source;
      vtkIdType cur;
      friend class iterator_core_access;
    };


    typedef tree_edge_iterator out_edge_iterator;
    typedef tree_edge_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 directed_tag directed_category;
    typedef allow_parallel_edge_tag edge_parallel_category;
    typedef vtkTree_traversal_category traversal_category;
    typedef vtkIdType vertices_size_type;
    typedef vtkIdType edges_size_type;
    typedef vtkIdType degree_size_type;

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

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

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

  inline vtkIdType
  vtkTree_edge_id(
         graph_traits< vtkTree* >::edge_descriptor e,
         const vtkTree*& vtkNotUsed(g))
  {
    return e;
  }

  inline graph_traits< vtkTree* >::vertex_descriptor
  source(graph_traits< vtkTree* >::edge_descriptor e,
         vtkTree* g)
  {
    return g->GetSourceVertex(e);
  }

  inline graph_traits< vtkTree* >::vertex_descriptor
  target(graph_traits< vtkTree* >::edge_descriptor e,
         vtkTree* g)
  {
    return g->GetTargetVertex(e);
  }

  inline vtksys_stl::pair<
    graph_traits< vtkTree* >::vertex_iterator,
    graph_traits< vtkTree* >::vertex_iterator >  
  vertices(vtkTree* g)
  {
    return vtksys_stl::make_pair( 0, g->GetNumberOfVertices() );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkTree* >::edge_iterator,
    graph_traits< vtkTree* >::edge_iterator >  
  edges(vtkTree* g)
  {
    typedef graph_traits< vtkTree* >::edge_iterator
      Iter;
    return vtksys_stl::make_pair( Iter(0), Iter(g->GetNumberOfEdges()) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkTree* >::out_edge_iterator,
    graph_traits< vtkTree* >::out_edge_iterator >  
  out_edges(
    graph_traits< vtkTree* >::vertex_descriptor u, 
    vtkTree* g)
  {
    typedef graph_traits< vtkTree* >::out_edge_iterator Iter;
    vtkIdType nverts = 0;
    const vtkIdType* verts = 0;
    g->GetOutVertices(u, nverts, verts);
    return vtksys_stl::make_pair( Iter(g, u, verts), Iter(g, u, verts + nverts) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkTree* >::in_edge_iterator,
    graph_traits< vtkTree* >::in_edge_iterator >  
  in_edges(
    graph_traits< vtkTree* >::vertex_descriptor u, 
    vtkTree* g)
  {
    typedef graph_traits< vtkTree* >::in_edge_iterator Iter;
    vtkIdType nverts = 0;
    const vtkIdType* verts = 0;
    g->GetInVertices(u, nverts, verts);
    if (u == g->GetRoot())
      {
      return vtksys_stl::make_pair( Iter(g, u, NULL), Iter(g, u, NULL) );
      }
    return vtksys_stl::make_pair( Iter(g, u, verts), Iter(g, u, verts + 1) );
  }

  inline vtksys_stl::pair<
    graph_traits< vtkTree* >::adjacency_iterator,
    graph_traits< vtkTree* >::adjacency_iterator >
  adjacent_vertices(
    graph_traits< vtkTree* >::vertex_descriptor u, 
    vtkTree* g)
  {
    typedef graph_traits< vtkTree* >::adjacency_iterator Iter;
    typedef graph_traits< vtkTree* >::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< vtkTree* >::vertices_size_type
  num_vertices(vtkTree* g)
  {
    return g->GetNumberOfVertices();
  }  

  inline graph_traits< vtkTree* >::edges_size_type
  num_edges(vtkTree* g)
  {
    return g->GetNumberOfEdges();
  }  

  inline graph_traits< vtkTree* >::degree_size_type
  out_degree(
    graph_traits< vtkTree* >::vertex_descriptor u, 
    vtkTree* g)
  {
    return g->GetOutDegree(u);
  }

  inline graph_traits< vtkTree* >::degree_size_type
  in_degree(
    graph_traits< vtkTree* >::vertex_descriptor u, 
    vtkTree* g)
  {
    return g->GetInDegree(u);
  }

  inline graph_traits< vtkTree* >::degree_size_type
  degree(
    graph_traits< vtkTree* >::vertex_descriptor u, 
    vtkTree* g)
  {
    return g->GetDegree(u);
  }
  
} // namespace boost

#endif // __vtkTreeToBoostAdapter_h