1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkUniformGridAMRDataIterator.cxx
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 #include "vtkUniformGridAMR.h"
16 #include "vtkUniformGridAMRDataIterator.h"
17 #include "vtkAMRInformation.h"
18 #include "vtkAMRDataInternals.h"
19 #include "vtkObjectFactory.h"
20 #include "vtkDataObject.h"
21 #include "vtkUniformGrid.h"
22 #include "vtkInformation.h"
23 #include <cassert>
24
25 //----------------------------------------------------------------
26 class AMRIndexIterator: public vtkObject
27 {
28 public:
29 static AMRIndexIterator* New();
30 vtkTypeMacro(AMRIndexIterator,vtkObject);
31
Initialize(const std::vector<int> * numBlocks)32 void Initialize(const std::vector<int>* numBlocks)
33 {
34 assert(numBlocks && !numBlocks->empty());
35 this->Level = 0;
36 this->Index = -1;
37 this->NumBlocks = numBlocks;
38 this->NumLevels = this->GetNumberOfLevels();
39 this->Next();
40 }
Next()41 void Next()
42 {
43 this->AdvanceIndex();
44 //advanc the level either when we are at the right level of out of levels
45 while(this->Level < this->NumLevels && static_cast<unsigned int>(this->Index)>= this->GetNumberOfBlocks(this->Level+1))
46 {
47 this->Level++;
48 }
49 }
IsDone()50 virtual bool IsDone() { return this->Level>=this->NumLevels;}
GetLevel()51 unsigned int GetLevel() { return this->Level; }
GetId()52 unsigned int GetId() { return this->Index - this->GetNumberOfBlocks(this->Level);}
GetFlatIndex()53 virtual unsigned int GetFlatIndex() { return this->Index;}
54 protected:
AMRIndexIterator()55 AMRIndexIterator(): Level(0), Index(0) {}
56 ~AMRIndexIterator() override = default;
57 unsigned int Level;
58 int Index;
59 unsigned int NumLevels;
60 const std::vector<int>* NumBlocks;
AdvanceIndex()61 virtual void AdvanceIndex() { this->Index++;}
GetNumberOfLevels()62 virtual unsigned int GetNumberOfLevels() { return static_cast<unsigned int>(this->NumBlocks->size()-1);};
GetNumberOfBlocks(int i)63 virtual unsigned int GetNumberOfBlocks(int i)
64 {
65 assert(i< static_cast<int>(this->NumBlocks->size()));
66 return (*this->NumBlocks)[i];
67 }
68
69
70 };
71 vtkStandardNewMacro(AMRIndexIterator);
72
73
74 //----------------------------------------------------------------
75
76 class AMRLoadedDataIndexIterator: public AMRIndexIterator
77 {
78 public:
79 static AMRLoadedDataIndexIterator* New();
80 vtkTypeMacro(AMRLoadedDataIndexIterator,AMRIndexIterator);
81 AMRLoadedDataIndexIterator() = default;
Initialize(const std::vector<int> * numBlocks,const vtkAMRDataInternals::BlockList * dataBlocks)82 void Initialize(const std::vector<int>* numBlocks, const vtkAMRDataInternals::BlockList* dataBlocks)
83 {
84 assert(numBlocks && !numBlocks->empty());
85 this->Level = 0;
86 this->InternalIdx = -1;
87 this->NumBlocks = numBlocks;
88 this->DataBlocks = dataBlocks;
89 this->NumLevels = this->GetNumberOfLevels();
90 this->Next();
91 }
92 protected:
AdvanceIndex()93 void AdvanceIndex() override
94 {
95 this->InternalIdx++;
96 Superclass::Index = static_cast<size_t>(this->InternalIdx) < this->DataBlocks->size()? (*this->DataBlocks)[this->InternalIdx].Index : 0;
97 }
IsDone()98 bool IsDone() override { return static_cast<size_t>(this->InternalIdx) >= this->DataBlocks->size();}
99 const vtkAMRDataInternals::BlockList* DataBlocks;
100 int InternalIdx;
101 private:
102 AMRLoadedDataIndexIterator(const AMRLoadedDataIndexIterator&) = delete;
103 void operator=(const AMRLoadedDataIndexIterator&) = delete;
104 };
105 vtkStandardNewMacro(AMRLoadedDataIndexIterator);
106
107 //----------------------------------------------------------------
108
109 vtkStandardNewMacro(vtkUniformGridAMRDataIterator);
110
vtkUniformGridAMRDataIterator()111 vtkUniformGridAMRDataIterator::vtkUniformGridAMRDataIterator()
112 {
113 this->Information = vtkSmartPointer<vtkInformation>::New();
114 this->AMR = nullptr;
115 this->AMRData = nullptr;
116 this->AMRInfo = nullptr;
117 }
118
119 vtkUniformGridAMRDataIterator::~vtkUniformGridAMRDataIterator() = default;
120
121
GetCurrentDataObject()122 vtkDataObject* vtkUniformGridAMRDataIterator::GetCurrentDataObject()
123 {
124 unsigned int level, id;
125 this->GetCurrentIndexPair(level,id);
126 vtkDataObject* obj = this->AMR->GetDataSet(level,id);
127 return obj;
128 }
129
130
GetCurrentMetaData()131 vtkInformation* vtkUniformGridAMRDataIterator::GetCurrentMetaData()
132 {
133 double bounds[6];
134 this->AMRInfo->GetBounds(this->GetCurrentLevel(), this->GetCurrentIndex(), bounds);
135 this->Information->Set(vtkDataObject::BOUNDING_BOX(),bounds,6);
136 return this->Information;
137 }
138
139
GetCurrentFlatIndex()140 unsigned int vtkUniformGridAMRDataIterator::GetCurrentFlatIndex()
141 {
142 return this->Iter->GetFlatIndex();
143 }
144
GetCurrentIndexPair(unsigned int & level,unsigned int & id)145 void vtkUniformGridAMRDataIterator::GetCurrentIndexPair(unsigned int& level, unsigned int& id)
146 {
147 level = this->Iter->GetLevel();
148 id = this->Iter->GetId();
149 }
150
151
GetCurrentLevel()152 unsigned int vtkUniformGridAMRDataIterator::GetCurrentLevel()
153 {
154 unsigned int level, id;
155 this->GetCurrentIndexPair(level,id);
156 return level;
157 }
158
159
GetCurrentIndex()160 unsigned int vtkUniformGridAMRDataIterator::GetCurrentIndex()
161 {
162 unsigned int level, id;
163 this->GetCurrentIndexPair(level,id);
164 return id;
165 }
166
PrintSelf(ostream & os,vtkIndent indent)167 void vtkUniformGridAMRDataIterator::PrintSelf(ostream& os, vtkIndent indent)
168 {
169 this->Superclass::PrintSelf(os, indent);
170 }
171
GoToFirstItem()172 void vtkUniformGridAMRDataIterator::GoToFirstItem()
173 {
174 if(!this->DataSet)
175 {
176 return;
177 }
178 this->AMR = vtkUniformGridAMR::SafeDownCast(this->DataSet);
179 this->AMRInfo = this->AMR->GetAMRInfo();
180 this->AMRData = this->AMR->GetAMRData();
181
182 if(this->AMRInfo)
183 {
184 if(this->GetSkipEmptyNodes())
185 {
186 vtkSmartPointer<AMRLoadedDataIndexIterator> itr = vtkSmartPointer<AMRLoadedDataIndexIterator>::New();
187 itr->Initialize(&this->AMRInfo->GetNumBlocks(), &this->AMR->GetAMRData()->GetAllBlocks());
188 this->Iter = itr;
189 }
190 else
191 {
192 this->Iter = vtkSmartPointer<AMRIndexIterator>::New();
193 this->Iter->Initialize(&this->AMRInfo->GetNumBlocks());
194 }
195 }
196 }
197
198
GoToNextItem()199 void vtkUniformGridAMRDataIterator::GoToNextItem()
200 {
201 this->Iter->Next();
202 }
203
204 //----------------------------------------------------------------------------
IsDoneWithTraversal()205 int vtkUniformGridAMRDataIterator::IsDoneWithTraversal()
206 {
207 return (!this->Iter) || this->Iter->IsDone();
208 }
209