VTK  9.1.20211115
vtkCompositeDataSetRange.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkCompositeDataSetRange.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #ifndef vtkCompositeDataSetRange_h
17 #define vtkCompositeDataSetRange_h
18 
20 #include "vtkCompositeDataSet.h"
22 #include "vtkMeta.h"
23 #include "vtkRange.h"
24 #include "vtkSmartPointer.h"
25 
26 #include <cassert>
27 
28 #ifndef __VTK_WRAP__
29 
30 namespace vtk
31 {
32 
33 // Pass these to vtk::Range(cds, options):
34 enum class CompositeDataSetOptions : unsigned int
35 {
36  None = 0,
37  SkipEmptyNodes = 1 << 1 // Skip null datasets.
38 };
39 
40 } // end namespace vtk (for bitflag op definition)
41 
42 VTK_GENERATE_BITFLAG_OPS(vtk::CompositeDataSetOptions)
43 
44 namespace vtk
45 {
46 
47 namespace detail
48 {
49 
50 struct CompositeDataSetRange;
51 struct CompositeDataSetIterator;
52 
55 
56 //------------------------------------------------------------------------------
57 // vtkCompositeDataSet iterator. Returns vtk::CompositeDataSetNodeReference.
59  : public std::iterator<std::forward_iterator_tag, vtkDataObject*, int,
60  CompositeDataSetIteratorReference, CompositeDataSetIteratorReference>
61 {
62 private:
63  using Superclass = std::iterator<std::forward_iterator_tag, vtkDataObject*, int,
67 
68 public:
69  using iterator_category = typename Superclass::iterator_category;
70  using value_type = typename Superclass::value_type;
71  using difference_type = typename Superclass::difference_type;
72  using pointer = typename Superclass::pointer;
73  using reference = typename Superclass::reference;
74 
76  : Iterator(o.Iterator ? SmartIterator::Take(o.Iterator->NewInstance()) : nullptr)
77  {
78  this->CopyState(o.Iterator);
79  }
80 
82 
84  {
85  this->Iterator = o.Iterator ? SmartIterator::Take(o.Iterator->NewInstance()) : nullptr;
86  this->CopyState(o.Iterator);
87  return *this;
88  }
89 
91  {
92  this->Increment();
93  return *this;
94  }
95 
97  {
98  CompositeDataSetIterator other(*this);
99  this->Increment();
100  return other;
101  }
102 
103  reference operator*() const { return this->GetData(); }
104 
105  pointer operator->() const { return this->GetData(); }
106 
108  {
109  // A null internal iterator means it is an 'end' sentinal.
110  InternalIterator* l = lhs.Iterator;
111  InternalIterator* r = rhs.Iterator;
112 
113  if (!r && !l)
114  { // end == end
115  return true;
116  }
117  else if (!r)
118  { // right is end
119  return l->IsDoneWithTraversal() != 0;
120  }
121  else if (!l)
122  { // left is end
123  return r->IsDoneWithTraversal() != 0;
124  }
125  else
126  { // Both iterators are valid, check unique idx:
127  return r->GetCurrentFlatIndex() == l->GetCurrentFlatIndex();
128  }
129  }
130 
132  {
133  return !(lhs == rhs); // let the compiler handle this one =)
134  }
135 
136  friend void swap(CompositeDataSetIterator& lhs, CompositeDataSetIterator& rhs) noexcept
137  {
138  using std::swap;
139  swap(lhs.Iterator, rhs.Iterator);
140  }
141 
142  friend struct CompositeDataSetRange;
143 
144 protected:
145  // Note: This takes ownership of iter and manages its lifetime.
146  // Iter should not be used past this point by the caller.
148  : Iterator(std::move(iter))
149  {
150  }
151 
152  // Note: Iterators constructed using this ctor will be considered
153  // 'end' iterators via a sentinal pattern.
155  : Iterator(nullptr)
156  {
157  }
158 
159 private:
160  void CopyState(InternalIterator* source)
161  {
162  if (source)
163  {
164  assert(this->Iterator != nullptr);
165  this->Iterator->SetDataSet(source->GetDataSet());
166  this->Iterator->SetSkipEmptyNodes(source->GetSkipEmptyNodes());
167  this->Iterator->InitTraversal();
168  assert(!source->IsDoneWithTraversal());
169  this->AdvanceTo(source->GetCurrentFlatIndex());
170  }
171  }
172 
173  void AdvanceTo(const unsigned int flatIdx)
174  {
175  assert(this->Iterator != nullptr);
176  assert(this->Iterator->GetCurrentFlatIndex() <= flatIdx);
177  while (this->Iterator->GetCurrentFlatIndex() < flatIdx)
178  {
179  this->Increment();
180  }
181  }
182 
183  void Increment()
184  {
185  assert(this->Iterator != nullptr);
186  assert(!this->Iterator->IsDoneWithTraversal());
187  this->Iterator->GoToNextItem();
188  }
189 
191  {
192  assert(this->Iterator != nullptr);
193  assert(!this->Iterator->IsDoneWithTraversal());
194  return CompositeDataSetIteratorReference{ this->Iterator };
195  }
196 
197  mutable SmartIterator Iterator;
198 };
199 
200 //------------------------------------------------------------------------------
201 // CompositeDataSet range proxy.
202 // The const_iterators/references are the same as the non-const versions, since
203 // vtkObjects marked const are unusable.
205 {
206 private:
209 
210 public:
211  using size_type = int;
217 
220  : CompositeDataSet(cds)
221  , Options(opts)
222  {
223  assert(this->CompositeDataSet);
224  }
225 
226  vtkCompositeDataSet* GetCompositeDataSet() const noexcept { return this->CompositeDataSet; }
227 
228  CompositeDataSetOptions GetOptions() const noexcept { return this->Options; }
229 
230  // This is O(N), since the size requires traversal due to various options.
231  size_type size() const
232  {
233  size_type result = 0;
234  auto iter = this->NewIterator();
235  iter->InitTraversal();
236  while (!iter->IsDoneWithTraversal())
237  {
238  ++result;
239  iter->GoToNextItem();
240  }
241  return result;
242  }
243 
244  iterator begin() const { return CompositeDataSetIterator{ this->NewIterator() }; }
245 
246  iterator end() const { return CompositeDataSetIterator{}; }
247 
248  // Note: These return mutable objects because const vtkObject are unusable.
249  const_iterator cbegin() const { return CompositeDataSetIterator{ this->NewIterator() }; }
250 
251  // Note: These return mutable objects because const vtkObjects are unusable.
253 
254 private:
255  SmartIterator NewIterator() const
256  {
257  using Opts = vtk::CompositeDataSetOptions;
258 
259  auto result = SmartIterator::Take(this->CompositeDataSet->NewIterator());
260  result->SetSkipEmptyNodes((this->Options & Opts::SkipEmptyNodes) != Opts::None);
261  result->InitTraversal();
262  return result;
263  }
264 
265  mutable vtkSmartPointer<vtkCompositeDataSet> CompositeDataSet;
266  CompositeDataSetOptions Options;
267 };
268 
269 }
270 } // end namespace vtk::detail
271 
272 #endif // __VTK_WRAP__
273 
274 #endif // vtkCompositeDataSetRange_h
275 
276 // VTK-HeaderTest-Exclude: vtkCompositeDataSetRange.h
superclass for composite data iterators
virtual void SetDataSet(vtkCompositeDataSet *ds)
Set the composite dataset this iterator is iterating over.
virtual int IsDoneWithTraversal()=0
Test whether the iterator is finished with the traversal.
virtual void InitTraversal()
Begin iterating over the composite dataset structure.
virtual void SetSkipEmptyNodes(vtkTypeBool)
If SkipEmptyNodes is true, then nullptr datasets will be skipped.
virtual void GoToNextItem()=0
Move the iterator to the next item in the collection.
virtual unsigned int GetCurrentFlatIndex()=0
Flat index is an index to identify the data in a composite data structure.
abstract superclass for composite (multi-block or AMR) datasets
virtual vtkCompositeDataIterator * NewIterator()=0
Return a new iterator (the iterator has to be deleted by user).
general representation of visualization data
static vtkSmartPointer< InternalIterator > Take(InternalIterator *t)
Transfer ownership of one reference to the given VTK object to a new smart pointer.
A reference proxy into a vtkCompositeDataSet, obtained by dereferencing an iterator from the vtk::Ran...
vtkSmartPointer< vtkDataArray > GetData(const Ioss::GroupingEntity *entity, const std::string &fieldname, Ioss::Transform *transform=nullptr, Cache *cache=nullptr, const std::string &cachekey=std::string())
Returns a VTK array for a given field (fieldname) on the chosen block (or set) entity.
@ reference
Definition: vtkX3D.h:470
vtk::CompositeDataSetNodeReference< vtkCompositeDataIterator, CompositeDataSetIterator > CompositeDataSetIteratorReference
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
CompositeDataSetIterator operator++(int)
CompositeDataSetIterator(CompositeDataSetIterator &&) noexcept=default
typename Superclass::difference_type difference_type
friend bool operator!=(const CompositeDataSetIterator &lhs, const CompositeDataSetIterator &rhs)
friend void swap(CompositeDataSetIterator &lhs, CompositeDataSetIterator &rhs) noexcept
CompositeDataSetIterator(SmartIterator &&iter) noexcept
CompositeDataSetIterator(const CompositeDataSetIterator &o)
friend bool operator==(const CompositeDataSetIterator &lhs, const CompositeDataSetIterator &rhs)
typename Superclass::iterator_category iterator_category
typename Superclass::reference reference
typename Superclass::value_type value_type
vtkCompositeDataSet * GetCompositeDataSet() const noexcept
CompositeDataSetOptions GetOptions() const noexcept
CompositeDataSetRange(vtkCompositeDataSet *cds, CompositeDataSetOptions opts=CompositeDataSetOptions::None)
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
This file contains a variety of metaprogramming constructs for working with vtk types.