forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkCachedStreamingDemandDrivenPipeline.cxx
336 lines (296 loc) · 9.98 KB
/
vtkCachedStreamingDemandDrivenPipeline.cxx
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCachedStreamingDemandDrivenPipeline.cxx
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.
=========================================================================*/
#include "vtkCachedStreamingDemandDrivenPipeline.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationIntegerVectorKey.h"
#include "vtkObjectFactory.h"
#include "vtkAlgorithm.h"
#include "vtkAlgorithmOutput.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkPointData.h"
vtkStandardNewMacro(vtkCachedStreamingDemandDrivenPipeline);
//----------------------------------------------------------------------------
vtkCachedStreamingDemandDrivenPipeline
::vtkCachedStreamingDemandDrivenPipeline()
{
this->CacheSize = 0;
this->Data = NULL;
this->Times = NULL;
this->SetCacheSize(10);
}
//----------------------------------------------------------------------------
vtkCachedStreamingDemandDrivenPipeline
::~vtkCachedStreamingDemandDrivenPipeline()
{
this->SetCacheSize(0);
}
//----------------------------------------------------------------------------
void vtkCachedStreamingDemandDrivenPipeline::SetCacheSize(int size)
{
int idx;
if (size == this->CacheSize)
{
return;
}
this->Modified();
// free the old data
for (idx = 0; idx < this->CacheSize; ++idx)
{
if (this->Data[idx])
{
this->Data[idx]->Delete();
this->Data[idx] = NULL;
}
}
if (this->Data)
{
delete [] this->Data;
this->Data = NULL;
}
if (this->Times)
{
delete [] this->Times;
this->Times = NULL;
}
this->CacheSize = size;
if (size == 0)
{
return;
}
this->Data = new vtkDataObject* [size];
this->Times = new unsigned long [size];
for (idx = 0; idx < size; ++idx)
{
this->Data[idx] = NULL;
this->Times[idx] = 0;
}
}
//----------------------------------------------------------------------------
void vtkCachedStreamingDemandDrivenPipeline
::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "CacheSize: " << this->CacheSize << "\n";
}
//----------------------------------------------------------------------------
int vtkCachedStreamingDemandDrivenPipeline::Update()
{
return this->Superclass::Update();
}
//----------------------------------------------------------------------------
int vtkCachedStreamingDemandDrivenPipeline::Update(int port)
{
if(!this->UpdateInformation())
{
return 0;
}
if(port >= 0 && port < this->Algorithm->GetNumberOfOutputPorts())
{
int retval = 1;
// some streaming filters can request that the pipeline execute multiple
// times for a single update
do
{
retval =
this->PropagateUpdateExtent(port) && this->UpdateData(port) && retval;
}
while (this->ContinueExecuting);
return retval;
}
else
{
return 1;
}
}
//----------------------------------------------------------------------------
int vtkCachedStreamingDemandDrivenPipeline
::NeedToExecuteData(int outputPort,
vtkInformationVector** inInfoVec,
vtkInformationVector* outInfoVec)
{
// If no port is specified, check all ports. This behavior is
// implemented by the superclass.
if(outputPort < 0)
{
return this->Superclass::NeedToExecuteData(outputPort,
inInfoVec, outInfoVec);
}
// Does the superclass want to execute? We must skip our direct superclass
// because it looks at update extents but does not know about the cache
if(this->vtkDemandDrivenPipeline::NeedToExecuteData(outputPort,
inInfoVec, outInfoVec))
{
return 1;
}
// Has the algorithm asked to be executed again?
if(this->ContinueExecuting)
{
return 1;
}
// First look through the cached data to see if it is still valid.
int i;
unsigned long pmt = this->GetPipelineMTime();
for (i = 0; i < this->CacheSize; ++i)
{
if (this->Data[i] && this->Times[i] < pmt)
{
this->Data[i]->Delete();
this->Data[i] = NULL;
this->Times[i] = 0;
}
}
// We need to check the requested update extent. Get the output
// port information and data information. We do not need to check
// existence of values because it has already been verified by
// VerifyOutputInformation.
vtkInformation* outInfo = outInfoVec->GetInformationObject(outputPort);
vtkDataObject* dataObject = outInfo->Get(vtkDataObject::DATA_OBJECT());
vtkInformation* dataInfo = dataObject->GetInformation();
if(dataInfo->Get(vtkDataObject::DATA_EXTENT_TYPE()) == VTK_PIECES_EXTENT)
{
int updatePiece = outInfo->Get(UPDATE_PIECE_NUMBER());
int updateNumberOfPieces = outInfo->Get(UPDATE_NUMBER_OF_PIECES());
int updateGhostLevel = outInfo->Get(UPDATE_NUMBER_OF_GHOST_LEVELS());
// check to see if any data in the cache fits this request
for (i = 0; i < this->CacheSize; ++i)
{
if (this->Data[i])
{
dataInfo = this->Data[i]->GetInformation();
// Check the unstructured extent. If we do not have the requested
// piece, we need to execute.
int dataPiece = dataInfo->Get(vtkDataObject::DATA_PIECE_NUMBER());
int dataNumberOfPieces =
dataInfo->Get(vtkDataObject::DATA_NUMBER_OF_PIECES());
int dataGhostLevel =
dataInfo->Get(vtkDataObject::DATA_NUMBER_OF_GHOST_LEVELS());
if (dataInfo->Get(vtkDataObject::DATA_EXTENT_TYPE()) ==
VTK_PIECES_EXTENT && dataPiece == updatePiece &&
dataNumberOfPieces == updateNumberOfPieces &&
dataGhostLevel == updateGhostLevel)
{
// we have a matching data we must copy it to our output, but for
// now we don't support polydata
return 1;
}
}
}
}
else if (dataInfo->Get(vtkDataObject::DATA_EXTENT_TYPE()) == VTK_3D_EXTENT)
{
// Check the structured extent. If the update extent is outside
// of the extent and not empty, we need to execute.
int dataExtent[6];
int updateExtent[6];
outInfo->Get(UPDATE_EXTENT(), updateExtent);
// check to see if any data in the cache fits this request
for (i = 0; i < this->CacheSize; ++i)
{
if (this->Data[i])
{
dataInfo = this->Data[i]->GetInformation();
dataInfo->Get(vtkDataObject::DATA_EXTENT(), dataExtent);
if(dataInfo->Get(vtkDataObject::DATA_EXTENT_TYPE()) ==
VTK_3D_EXTENT &&
!(updateExtent[0] < dataExtent[0] ||
updateExtent[1] > dataExtent[1] ||
updateExtent[2] < dataExtent[2] ||
updateExtent[3] > dataExtent[3] ||
updateExtent[4] < dataExtent[4] ||
updateExtent[5] > dataExtent[5]) &&
(updateExtent[0] <= updateExtent[1] &&
updateExtent[2] <= updateExtent[3] &&
updateExtent[4] <= updateExtent[5]))
{
// we have a match
// Pass this data to output.
vtkImageData *id = vtkImageData::SafeDownCast(dataObject);
vtkImageData *id2 = vtkImageData::SafeDownCast(this->Data[i]);
if (id && id2)
{
id->SetExtent(dataExtent);
id->GetPointData()->PassData(id2->GetPointData());
// not sure if we need this
dataObject->DataHasBeenGenerated();
return 0;
}
}
}
}
}
// We do need to execute
return 1;
}
//----------------------------------------------------------------------------
int vtkCachedStreamingDemandDrivenPipeline
::ExecuteData(vtkInformation* request,
vtkInformationVector** inInfoVec,
vtkInformationVector* outInfoVec)
{
// only works for one in one out algorithms
if (request->Get(FROM_OUTPUT_PORT()) != 0)
{
vtkErrorMacro("vtkCachedStreamingDemandDrivenPipeline can only be used for algorithms with one output and one input");
return 0;
}
// first do the ususal thing
int result = this->Superclass::ExecuteData(request, inInfoVec, outInfoVec);
// then save the newly generated data
unsigned long bestTime = VTK_LARGE_INTEGER;
int bestIdx = 0;
// Save the image in cache.
// Find a spot to put the data.
for (int i = 0; i < this->CacheSize; ++i)
{
if (this->Data[i] == NULL)
{
bestIdx = i;
bestTime = 0;
break;
}
if (this->Times[i] < bestTime)
{
bestIdx = i;
bestTime = this->Times[i];
}
}
vtkInformation* outInfo = outInfoVec->GetInformationObject(0);
vtkDataObject* dataObject = outInfo->Get(vtkDataObject::DATA_OBJECT());
if (this->Data[bestIdx] == NULL)
{
this->Data[bestIdx] = dataObject->NewInstance();
}
this->Data[bestIdx]->ReleaseData();
vtkImageData *id = vtkImageData::SafeDownCast(dataObject);
if (id)
{
vtkInformation* inInfo = inInfoVec[0]->GetInformationObject(0);
vtkImageData *input =
vtkImageData::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
id->SetExtent(input->GetExtent());
id->GetPointData()->PassData(input->GetPointData());
id->DataHasBeenGenerated();
}
vtkImageData *id2 = vtkImageData::SafeDownCast(this->Data[bestIdx]);
if (id && id2)
{
id2->SetExtent(id->GetExtent());
id2->SetScalarType(id->GetScalarType());
id2->SetNumberOfScalarComponents(
id->GetNumberOfScalarComponents());
id2->GetPointData()->SetScalars(
id->GetPointData()->GetScalars());
}
this->Times[bestIdx] = dataObject->GetUpdateTime();
return result;
}