VTK: Common/DataModel/vtkCompositeDataSetRange.h Source File

| VTK 9.3.1

vtkCompositeDataSetRange.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3
4 #ifndef vtkCompositeDataSetRange_h
5 #define vtkCompositeDataSetRange_h
6
7 #include "vtkCompositeDataIterator.h"
8 #include "vtkCompositeDataSet.h"
9 #include "vtkCompositeDataSetNodeReference.h"
10 #include "vtkMeta.h"
11 #include "vtkRange.h"
12 #include "vtkSmartPointer.h"
13
14 #include <cassert>
15
16 namespace vtk
17 {
18 VTK_ABI_NAMESPACE_BEGIN
19
20 // Pass these to vtk::Range(cds, options):
21 enum class CompositeDataSetOptions : unsigned int
22 {
23 None = 0,
24 SkipEmptyNodes = 1 << 1 // Skip null datasets.
25 };
26
27 VTK_ABI_NAMESPACE_END
28 } // end namespace vtk (for bitflag op definition)
29
30 VTK_ABI_NAMESPACE_BEGIN
31 VTK_GENERATE_BITFLAG_OPS(vtk::CompositeDataSetOptions)
32 VTK_ABI_NAMESPACE_END
33
34 namespace vtk
35 {
36 namespace detail
37 {
38 VTK_ABI_NAMESPACE_BEGIN
39
40 struct CompositeDataSetRange;
41 struct CompositeDataSetIterator;
42
43 using CompositeDataSetIteratorReference =
44 vtk::CompositeDataSetNodeReference<vtkCompositeDataIterator, CompositeDataSetIterator>;
45
46 //------------------------------------------------------------------------------
47 // vtkCompositeDataSet iterator. Returns vtk::CompositeDataSetNodeReference.
48 struct CompositeDataSetIterator
49 {
50 private:
51 using InternalIterator = vtkCompositeDataIterator;
52 using SmartIterator = vtkSmartPointer<InternalIterator>;
53
54 public:
55 using iterator_category = std::forward_iterator_tag;
56 using value_type = vtkDataObject*;
57 using difference_type = int;
58 using pointer = CompositeDataSetIteratorReference;
59 using reference = CompositeDataSetIteratorReference;
60
61 CompositeDataSetIterator(const CompositeDataSetIterator& o)
62 : Iterator(o.Iterator ? SmartIterator::Take(o.Iterator->NewInstance()) : nullptr)
63 {
64 this->CopyState(o.Iterator);
65 }
66
67 CompositeDataSetIterator(CompositeDataSetIterator&&) noexcept = default;
68
69 CompositeDataSetIterator& operator=(const CompositeDataSetIterator& o)
70 {
71 this->Iterator = o.Iterator ? SmartIterator::Take(o.Iterator->NewInstance()) : nullptr;
72 this->CopyState(o.Iterator);
73 return *this;
74 }
75
76 CompositeDataSetIterator& operator++() // prefix
77 {
78 this->Increment();
79 return *this;
80 }
81
82 CompositeDataSetIterator operator++(int) // postfix
83 {
84 CompositeDataSetIterator other(*this);
85 this->Increment();
86 return other;
87 }
88
89 reference operator*() const { return this->GetData(); }
90
91 pointer operator->() const { return this->GetData(); }
92
93 friend bool operator==(const CompositeDataSetIterator& lhs, const CompositeDataSetIterator& rhs)
94 {
95 // A null internal iterator means it is an 'end' sentinal.
96 InternalIterator* l = lhs.Iterator;
97 InternalIterator* r = rhs.Iterator;
98
99 if (!r && !l)
100 { // end == end
101 return true;
102 }
103 else if (!r)
104 { // right is end
105 return l->IsDoneWithTraversal() != 0;
106 }
107 else if (!l)
108 { // left is end
109 return r->IsDoneWithTraversal() != 0;
110 }
111 else
112 { // Both iterators are valid, check unique idx:
113 return r->GetCurrentFlatIndex() == l->GetCurrentFlatIndex();
114 }
115 }
116
117 friend bool operator!=(const CompositeDataSetIterator& lhs, const CompositeDataSetIterator& rhs)
118 {
119 return !(lhs == rhs); // let the compiler handle this one =)
120 }
121
122 friend void swap(CompositeDataSetIterator& lhs, CompositeDataSetIterator& rhs) noexcept
123 {
124 using std::swap;
125 swap(lhs.Iterator, rhs.Iterator);
126 }
127
128 friend struct CompositeDataSetRange;
129
130 protected:
131 // Note: This takes ownership of iter and manages its lifetime.
132 // Iter should not be used past this point by the caller.
133 CompositeDataSetIterator(SmartIterator&& iter) noexcept
134 : Iterator(std::move(iter))
135 {
136 }
137
138 // Note: Iterators constructed using this ctor will be considered
139 // 'end' iterators via a sentinal pattern.
140 CompositeDataSetIterator() noexcept
141 : Iterator(nullptr)
142 {
143 }
144
145 private:
146 void CopyState(InternalIterator* source)
147 {
148 if (source)
149 {
150 assert(this->Iterator != nullptr);
151 this->Iterator->SetDataSet(source->GetDataSet());
152 this->Iterator->SetSkipEmptyNodes(source->GetSkipEmptyNodes());
153 this->Iterator->InitTraversal();
154 // XXX(empty iteration): This assert fires for some iterator
155 // implementations if iterating over an empty dataset (because in this
156 // case, `begin() == end()`. This assert needs work.
157 // assert(!source->IsDoneWithTraversal());
158 this->AdvanceTo(source->GetCurrentFlatIndex());
159 }
160 }
161
162 void AdvanceTo(const unsigned int flatIdx)
163 {
164 assert(this->Iterator != nullptr);
165 assert(this->Iterator->GetCurrentFlatIndex() <= flatIdx);
166 while (this->Iterator->GetCurrentFlatIndex() < flatIdx)
167 {
168 this->Increment();
169 }
170 }
171
172 void Increment()
173 {
174 assert(this->Iterator != nullptr);
175 assert(!this->Iterator->IsDoneWithTraversal());
176 this->Iterator->GoToNextItem();
177 }
178
179 CompositeDataSetIteratorReference GetData() const
180 {
181 assert(this->Iterator != nullptr);
182 assert(!this->Iterator->IsDoneWithTraversal());
183 return CompositeDataSetIteratorReference{ this->Iterator };
184 }
185
186 mutable SmartIterator Iterator;
187 };
188
189 //------------------------------------------------------------------------------
190 // CompositeDataSet range proxy.
191 // The const_iterators/references are the same as the non-const versions, since
192 // vtkObjects marked const are unusable.
193 struct CompositeDataSetRange
194 {
195 private:
196 using InternalIterator = vtkCompositeDataIterator;
197 using SmartIterator = vtkSmartPointer<InternalIterator>;
198
199 public:
200 using size_type = int;
201 using iterator = CompositeDataSetIterator;
202 using const_iterator = CompositeDataSetIterator;
203 using reference = CompositeDataSetIteratorReference;
204 using const_reference = const CompositeDataSetIteratorReference;
205 using value_type = vtkDataObject*;
206
207 CompositeDataSetRange(
208 vtkCompositeDataSet* cds, CompositeDataSetOptions opts = CompositeDataSetOptions::None)
209 : CompositeDataSet(cds)
210 , Options(opts)
211 {
212 assert(this->CompositeDataSet);
213 }
214
215 vtkCompositeDataSet* GetCompositeDataSet() const noexcept { return this->CompositeDataSet; }
216
217 CompositeDataSetOptions GetOptions() const noexcept { return this->Options; }
218
219 // This is O(N), since the size requires traversal due to various options.
220 size_type size() const
221 {
222 size_type result = 0;
223 auto iter = this->NewIterator();
224 iter->InitTraversal();
225 while (!iter->IsDoneWithTraversal())
226 {
227 ++result;
228 iter->GoToNextItem();
229 }
230 return result;
231 }
232
233 iterator begin() const { return CompositeDataSetIterator{ this->NewIterator() }; }
234
235 iterator end() const { return CompositeDataSetIterator{}; }
236
237 // Note: These return mutable objects because const vtkObject are unusable.
238 const_iterator cbegin() const { return CompositeDataSetIterator{ this->NewIterator() }; }
239
240 // Note: These return mutable objects because const vtkObjects are unusable.
241 const_iterator cend() const { return CompositeDataSetIterator{}; }
242
243 private:
244 SmartIterator NewIterator() const
245 {
246 using Opts = vtk::CompositeDataSetOptions;
247
248 auto result = SmartIterator::Take(this->CompositeDataSet->NewIterator());
249 result->SetSkipEmptyNodes((this->Options & Opts::SkipEmptyNodes) != Opts::None);
250 result->InitTraversal();
251 return result;
252 }
253
254 mutable vtkSmartPointer<vtkCompositeDataSet> CompositeDataSet;
255 CompositeDataSetOptions Options;
256 };
257
258 VTK_ABI_NAMESPACE_END
259 }
260 } // end namespace vtk::detail
261
262 #endif // vtkCompositeDataSetRange_h
263
264 // VTK-HeaderTest-Exclude: vtkCompositeDataSetRange.h
vtkCompositeDataIterator
superclass for composite data iterators
Definition: vtkCompositeDataIterator.h:28
vtkCompositeDataIterator::SetDataSet
virtual void SetDataSet(vtkCompositeDataSet *ds)
Set the composite dataset this iterator is iterating over.
vtkCompositeDataIterator::IsDoneWithTraversal
virtual int IsDoneWithTraversal()=0
Test whether the iterator is finished with the traversal.
vtkCompositeDataIterator::InitTraversal
virtual void InitTraversal()
Begin iterating over the composite dataset structure.
vtkCompositeDataIterator::SetSkipEmptyNodes
virtual void SetSkipEmptyNodes(vtkTypeBool)
If SkipEmptyNodes is true, then nullptr datasets will be skipped.
vtkCompositeDataIterator::GoToNextItem
virtual void GoToNextItem()=0
Move the iterator to the next item in the collection.
vtkCompositeDataIterator::GetCurrentFlatIndex
virtual unsigned int GetCurrentFlatIndex()=0
Flat index is an index to identify the data in a composite data structure.
vtkCompositeDataSet
abstract superclass for composite (multi-block or AMR) datasets
Definition: vtkCompositeDataSet.h:51
vtkCompositeDataSet::NewIterator
virtual vtkCompositeDataIterator * NewIterator()=0
Return a new iterator (the iterator has to be deleted by user).
vtkDataObject
general representation of visualization data
Definition: vtkDataObject.h:64
vtkSmartPointer< InternalIterator >
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:270
vtk::CompositeDataSetNodeReference
A reference proxy into a vtkCompositeDataSet, obtained by dereferencing an iterator from the vtk::Ran...
Definition: vtkCompositeDataSetNodeReference.h:139
detail
Definition: vtkCellTreeLocator.h:59
vtkIOSSUtilities::GetData
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.
vtk::detail::CompositeDataSetIteratorReference
vtk::CompositeDataSetNodeReference< vtkCompositeDataIterator, CompositeDataSetIterator > CompositeDataSetIteratorReference
Definition: vtkCompositeDataSetRange.h:44
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkSMPThreadLocalAPI.h:30
vtk::CompositeDataSetOptions
CompositeDataSetOptions
Definition: vtkCompositeDataSetRange.h:22
vtk::CompositeDataSetOptions::None
@ None
vtk::CompositeDataSetOptions::SkipEmptyNodes
@ SkipEmptyNodes
vtk::detail::CompositeDataSetIterator
Definition: vtkCompositeDataSetRange.h:49
vtk::detail::CompositeDataSetIterator::operator++
CompositeDataSetIterator operator++(int)
Definition: vtkCompositeDataSetRange.h:82
vtk::detail::CompositeDataSetIterator::CompositeDataSetIterator
CompositeDataSetIterator(CompositeDataSetIterator &&) noexcept=default
vtk::detail::CompositeDataSetIterator::operator->
pointer operator->() const
Definition: vtkCompositeDataSetRange.h:91
vtk::detail::CompositeDataSetIterator::operator!=
friend bool operator!=(const CompositeDataSetIterator &lhs, const CompositeDataSetIterator &rhs)
Definition: vtkCompositeDataSetRange.h:117
vtk::detail::CompositeDataSetIterator::swap
friend void swap(CompositeDataSetIterator &lhs, CompositeDataSetIterator &rhs) noexcept
Definition: vtkCompositeDataSetRange.h:122
vtk::detail::CompositeDataSetIterator::operator*
reference operator*() const
Definition: vtkCompositeDataSetRange.h:89
vtk::detail::CompositeDataSetIterator::CompositeDataSetIterator
CompositeDataSetIterator(SmartIterator &&iter) noexcept
Definition: vtkCompositeDataSetRange.h:133
vtk::detail::CompositeDataSetIterator::operator++
CompositeDataSetIterator & operator++()
Definition: vtkCompositeDataSetRange.h:76
vtk::detail::CompositeDataSetIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: vtkCompositeDataSetRange.h:55
vtk::detail::CompositeDataSetIterator::difference_type
int difference_type
Definition: vtkCompositeDataSetRange.h:57
vtk::detail::CompositeDataSetIterator::CompositeDataSetIterator
CompositeDataSetIterator(const CompositeDataSetIterator &o)
Definition: vtkCompositeDataSetRange.h:61
vtk::detail::CompositeDataSetIterator::operator==
friend bool operator==(const CompositeDataSetIterator &lhs, const CompositeDataSetIterator &rhs)
Definition: vtkCompositeDataSetRange.h:93
vtk::detail::CompositeDataSetIterator::CompositeDataSetIterator
CompositeDataSetIterator() noexcept
Definition: vtkCompositeDataSetRange.h:140
vtk::detail::CompositeDataSetRange
Definition: vtkCompositeDataSetRange.h:194
vtk::detail::CompositeDataSetRange::begin
iterator begin() const
Definition: vtkCompositeDataSetRange.h:233
vtk::detail::CompositeDataSetRange::size
size_type size() const
Definition: vtkCompositeDataSetRange.h:220
vtk::detail::CompositeDataSetRange::end
iterator end() const
Definition: vtkCompositeDataSetRange.h:235
vtk::detail::CompositeDataSetRange::GetCompositeDataSet
vtkCompositeDataSet * GetCompositeDataSet() const noexcept
Definition: vtkCompositeDataSetRange.h:215
vtk::detail::CompositeDataSetRange::cend
const_iterator cend() const
Definition: vtkCompositeDataSetRange.h:241
vtk::detail::CompositeDataSetRange::GetOptions
CompositeDataSetOptions GetOptions() const noexcept
Definition: vtkCompositeDataSetRange.h:217
vtk::detail::CompositeDataSetRange::size_type
int size_type
Definition: vtkCompositeDataSetRange.h:200
vtk::detail::CompositeDataSetRange::cbegin
const_iterator cbegin() const
Definition: vtkCompositeDataSetRange.h:238
vtk::detail::CompositeDataSetRange::CompositeDataSetRange
CompositeDataSetRange(vtkCompositeDataSet *cds, CompositeDataSetOptions opts=CompositeDataSetOptions::None)
Definition: vtkCompositeDataSetRange.h:207
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:964
vtkCompositeDataIterator.h
vtkCompositeDataSetNodeReference.h
vtkCompositeDataSet.h
vtkMeta.h
This file contains a variety of metaprogramming constructs for working with vtk types.
vtkRange.h
vtkSmartPointer.h