VTK
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.
147  CompositeDataSetIterator(SmartIterator&& iter) noexcept : Iterator(std::move(iter)) {}
148 
149  // Note: Iterators constructed using this ctor will be considered
150  // 'end' iterators via a sentinal pattern.
151  CompositeDataSetIterator() noexcept : Iterator(nullptr) {}
152 
153 private:
154  void CopyState(InternalIterator* source)
155  {
156  if (source)
157  {
158  assert(this->Iterator != nullptr);
159  this->Iterator->SetDataSet(source->GetDataSet());
160  this->Iterator->SetSkipEmptyNodes(source->GetSkipEmptyNodes());
161  this->Iterator->InitTraversal();
162  this->AdvanceTo(source->GetCurrentFlatIndex());
163  }
164  }
165 
166  void AdvanceTo(const unsigned int flatIdx)
167  {
168  assert(this->Iterator != nullptr);
169  assert(this->Iterator->GetCurrentFlatIndex() <= flatIdx);
170  while (this->Iterator->GetCurrentFlatIndex() < flatIdx)
171  {
172  this->Increment();
173  }
174  }
175 
176  void Increment()
177  {
178  assert(this->Iterator != nullptr);
179  assert(!this->Iterator->IsDoneWithTraversal());
180  this->Iterator->GoToNextItem();
181  }
182 
183  CompositeDataSetIteratorReference GetData() const
184  {
185  assert(this->Iterator != nullptr);
186  assert(!this->Iterator->IsDoneWithTraversal());
187  return CompositeDataSetIteratorReference{ this->Iterator };
188  }
189 
190  mutable SmartIterator Iterator;
191 };
192 
193 //------------------------------------------------------------------------------
194 // CompositeDataSet range proxy.
195 // The const_iterators/references are the same as the non-const versions, since
196 // vtkObjects marked const are unusable.
198 {
199 private:
202 
203 public:
204  using size_type = int;
210 
213  : CompositeDataSet(cds)
214  , Options(opts)
215  {
216  assert(this->CompositeDataSet);
217  }
218 
219  vtkCompositeDataSet* GetCompositeDataSet() const noexcept { return this->CompositeDataSet; }
220 
221  CompositeDataSetOptions GetOptions() const noexcept { return this->Options; }
222 
223  // This is O(N), since the size requires traversal due to various options.
224  size_type size() const
225  {
226  size_type result = 0;
227  auto iter = this->NewIterator();
228  iter->InitTraversal();
229  while (!iter->IsDoneWithTraversal())
230  {
231  ++result;
232  iter->GoToNextItem();
233  }
234  return result;
235  }
236 
237  iterator begin() const { return CompositeDataSetIterator{ this->NewIterator() }; }
238 
239  iterator end() const { return CompositeDataSetIterator{}; }
240 
241  // Note: These return mutable objects because const vtkObject are unusable.
242  const_iterator cbegin() const { return CompositeDataSetIterator{ this->NewIterator() }; }
243 
244  // Note: These return mutable objects because const vtkObjects are unusable.
246 
247 private:
248  SmartIterator NewIterator() const
249  {
250  using Opts = vtk::CompositeDataSetOptions;
251 
252  auto result = SmartIterator::Take(this->CompositeDataSet->NewIterator());
253  result->SetSkipEmptyNodes((this->Options & Opts::SkipEmptyNodes) != Opts::None);
254  result->InitTraversal();
255  return result;
256  }
257 
258  mutable vtkSmartPointer<vtkCompositeDataSet> CompositeDataSet;
259  CompositeDataSetOptions Options;
260 };
261 
262 }
263 } // end namespace vtk::detail
264 
265 #endif // __VTK_WRAP__
266 
267 #endif // vtkCompositeDataSetRange_h
268 
269 // VTK-HeaderTest-Exclude: vtkCompositeDataSetRange.h
vtk::detail::CompositeDataSetIterator::operator!=
friend bool operator!=(const CompositeDataSetIterator &lhs, const CompositeDataSetIterator &rhs)
Definition: vtkCompositeDataSetRange.h:131
vtk::detail::CompositeDataSetRange::GetCompositeDataSet
vtkCompositeDataSet * GetCompositeDataSet() const noexcept
Definition: vtkCompositeDataSetRange.h:219
vtk::detail::CompositeDataSetRange
Definition: vtkCompositeDataSetRange.h:197
vtkCompositeDataSet::NewIterator
virtual vtkCompositeDataIterator * NewIterator()=0
Return a new iterator (the iterator has to be deleted by user).
vtk::detail::CompositeDataSetIterator::operator==
friend bool operator==(const CompositeDataSetIterator &lhs, const CompositeDataSetIterator &rhs)
Definition: vtkCompositeDataSetRange.h:107
vtk::detail::CompositeDataSetIteratorReference
vtk::CompositeDataSetNodeReference< vtkCompositeDataIterator, CompositeDataSetIterator > CompositeDataSetIteratorReference
Definition: vtkCompositeDataSetRange.h:54
vtkRange.h
vtkCompositeDataIterator.h
vtk::detail::CompositeDataSetIterator::iterator_category
typename Superclass::iterator_category iterator_category
Definition: vtkCompositeDataSetRange.h:69
vtkMeta.h
vtk::detail::CompositeDataSetIterator::CompositeDataSetIterator
CompositeDataSetIterator(const CompositeDataSetIterator &o)
Definition: vtkCompositeDataSetRange.h:75
vtkCompositeDataIterator::SetSkipEmptyNodes
virtual void SetSkipEmptyNodes(vtkTypeBool)
If SkipEmptyNodes is true, then nullptr datasets will be skipped.
vtkSmartPointer< InternalIterator >::Take
static vtkSmartPointer< InternalIterator > Take(InternalIterator *t)
Transfer ownership of one reference to the given VTK object to a new smart pointer.
Definition: vtkSmartPointer.h:241
vtkSmartPointer< InternalIterator >
detail
Definition: vtkGenericDataArrayLookupHelper.h:31
vtk::detail::CompositeDataSetIterator::CompositeDataSetIterator
CompositeDataSetIterator(SmartIterator &&iter) noexcept
Definition: vtkCompositeDataSetRange.h:147
vtk::detail::CompositeDataSetRange::size_type
int size_type
Definition: vtkCompositeDataSetRange.h:204
vtkCompositeDataSet
abstract superclass for composite (multi-block or AMR) datasets
Definition: vtkCompositeDataSet.h:45
vtk::detail::CompositeDataSetIterator::reference
typename Superclass::reference reference
Definition: vtkCompositeDataSetRange.h:73
vtk::detail::CompositeDataSetIterator::operator++
CompositeDataSetIterator operator++(int)
Definition: vtkCompositeDataSetRange.h:96
vtk::CompositeDataSetOptions::None
vtk::detail::CompositeDataSetRange::end
iterator end() const
Definition: vtkCompositeDataSetRange.h:239
vtk::detail::CompositeDataSetIterator::pointer
typename Superclass::pointer pointer
Definition: vtkCompositeDataSetRange.h:72
vtkCompositeDataIterator::IsDoneWithTraversal
virtual int IsDoneWithTraversal()=0
Test whether the iterator is finished with the traversal.
vtkCompositeDataIterator::SetDataSet
virtual void SetDataSet(vtkCompositeDataSet *ds)
Set the composite dataset this iterator is iterating over.
vtk::detail::CompositeDataSetIterator::difference_type
typename Superclass::difference_type difference_type
Definition: vtkCompositeDataSetRange.h:71
vtkCompositeDataIterator
superclass for composite data iterators
Definition: vtkCompositeDataIterator.h:35
vtkCompositeDataSetNodeReference.h
vtk::detail::CompositeDataSetIterator::operator++
CompositeDataSetIterator & operator++()
Definition: vtkCompositeDataSetRange.h:90
vtkX3D::reference
Definition: vtkX3D.h:470
vtkSmartPointer.h
vtk::detail::CompositeDataSetRange::begin
iterator begin() const
Definition: vtkCompositeDataSetRange.h:237
vtk::detail::CompositeDataSetIterator::operator*
reference operator*() const
Definition: vtkCompositeDataSetRange.h:103
vtk::detail::CompositeDataSetRange::CompositeDataSetRange
CompositeDataSetRange(vtkCompositeDataSet *cds, CompositeDataSetOptions opts=CompositeDataSetOptions::None)
Definition: vtkCompositeDataSetRange.h:211
vtk::detail::CompositeDataSetIterator::value_type
typename Superclass::value_type value_type
Definition: vtkCompositeDataSetRange.h:70
vtk::detail::CompositeDataSetRange::size
size_type size() const
Definition: vtkCompositeDataSetRange.h:224
vtkCompositeDataSet.h
vtk::CompositeDataSetOptions
CompositeDataSetOptions
Definition: vtkCompositeDataSetRange.h:34
vtk::detail::CompositeDataSetRange::cend
const_iterator cend() const
Definition: vtkCompositeDataSetRange.h:245
vtk::detail::CompositeDataSetIterator::swap
friend void swap(CompositeDataSetIterator &lhs, CompositeDataSetIterator &rhs) noexcept
Definition: vtkCompositeDataSetRange.h:136
vtk::detail::CompositeDataSetIterator::CompositeDataSetIterator
CompositeDataSetIterator() noexcept
Definition: vtkCompositeDataSetRange.h:151
vtk::CompositeDataSetOptions::SkipEmptyNodes
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkAtomicTypeConcepts.h:21
vtkCompositeDataIterator::InitTraversal
virtual void InitTraversal()
Begin iterating over the composite dataset structure.
vtk::detail::CompositeDataSetRange::GetOptions
CompositeDataSetOptions GetOptions() const noexcept
Definition: vtkCompositeDataSetRange.h:221
vtkCompositeDataIterator::GetCurrentFlatIndex
virtual unsigned int GetCurrentFlatIndex()=0
Flat index is an index to identify the data in a composite data structure.
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:959
vtk::detail::CompositeDataSetRange::cbegin
const_iterator cbegin() const
Definition: vtkCompositeDataSetRange.h:242
vtkDataObject
general representation of visualization data
Definition: vtkDataObject.h:58
vtk::detail::CompositeDataSetIterator::operator->
pointer operator->() const
Definition: vtkCompositeDataSetRange.h:105
vtk::CompositeDataSetNodeReference
A reference proxy into a vtkCompositeDataSet, obtained by dereferencing an iterator from the vtk::Ran...
Definition: vtkCompositeDataSetNodeReference.h:148
vtk::detail::CompositeDataSetIterator
Definition: vtkCompositeDataSetRange.h:58
vtkCompositeDataIterator::GoToNextItem
virtual void GoToNextItem()=0
Move the iterator to the next item in the collection.