forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkGenericProbeFilter.cxx
271 lines (225 loc) · 7.88 KB
/
vtkGenericProbeFilter.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGenericProbeFilter.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 "vtkGenericProbeFilter.h"
#include "vtkCell.h"
#include "vtkIdTypeArray.h"
#include "vtkImageData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkGenericDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkGenericCellIterator.h"
#include "vtkGenericAdaptorCell.h"
#include "vtkGenericAttribute.h"
#include "vtkGenericAttributeCollection.h"
#include "vtkStreamingDemandDrivenPipeline.h"
vtkStandardNewMacro(vtkGenericProbeFilter);
//----------------------------------------------------------------------------
vtkGenericProbeFilter::vtkGenericProbeFilter()
{
this->ValidPoints = vtkIdTypeArray::New();
this->SetNumberOfInputPorts(2);
}
//----------------------------------------------------------------------------
vtkGenericProbeFilter::~vtkGenericProbeFilter()
{
this->ValidPoints->Delete();
this->ValidPoints = NULL;
}
//----------------------------------------------------------------------------
void vtkGenericProbeFilter::SetSourceData(vtkGenericDataSet *input)
{
this->SetInputData(1, input);
}
//----------------------------------------------------------------------------
vtkGenericDataSet *vtkGenericProbeFilter::GetSource()
{
if (this->GetNumberOfInputConnections(1) < 1)
{
return NULL;
}
return vtkGenericDataSet::SafeDownCast(
this->GetExecutive()->GetInputData(1, 0));
}
//----------------------------------------------------------------------------
int vtkGenericProbeFilter::RequestInformation(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *sourceInfo = inputVector[1]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// A variation of the bug fix from John Biddiscombe.
// Make sure that the scalar type and number of components
// are propagated from the source not the input.
if (vtkImageData::HasScalarType(sourceInfo))
{
vtkImageData::SetScalarType(vtkImageData::GetScalarType(sourceInfo),
outInfo);
}
if (vtkImageData::HasNumberOfScalarComponents(sourceInfo))
{
vtkImageData::SetNumberOfScalarComponents(
vtkImageData::GetNumberOfScalarComponents(sourceInfo),
outInfo);
}
return 1;
}
//----------------------------------------------------------------------------
int vtkGenericProbeFilter::RequestData(
vtkInformation *vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *sourceInfo = inputVector[1]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the input and output
vtkDataSet *input = vtkDataSet::SafeDownCast(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkDataSet *output = vtkDataSet::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkGenericDataSet *source = vtkGenericDataSet::SafeDownCast(
sourceInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkIdType ptId, numPts;
double x[3], tol2;
int subId;
double pcoords[3];
vtkDebugMacro(<<"Probing data");
if (source == NULL)
{
vtkErrorMacro (<< "Source is NULL.");
return 1;
}
// First, copy the input to the output as a starting point
output->CopyStructure( input );
numPts = input->GetNumberOfPoints();
this->ValidPoints->Allocate(numPts);
vtkPointData *outputPD = output->GetPointData();
vtkCellData *outputCD = output->GetCellData();
// prepare the output attributes
vtkGenericAttributeCollection *attributes=source->GetAttributes();
vtkGenericAttribute *attribute;
vtkDataArray *attributeArray;
int c=attributes->GetNumberOfAttributes();
vtkDataSetAttributes *dsAttributes;
int attributeType;
double *tuples = new double[attributes->GetMaxNumberOfComponents()];
for(int i = 0; i<c; ++i)
{
attribute=attributes->GetAttribute(i);
attributeType=attribute->GetType();
if(attribute->GetCentering()==vtkPointCentered)
{
dsAttributes=outputPD;
}
else // vtkCellCentered
{
dsAttributes=outputCD;
}
attributeArray=vtkDataArray::CreateDataArray(attribute->GetComponentType());
attributeArray->SetNumberOfComponents(attribute->GetNumberOfComponents());
attributeArray->SetName(attribute->GetName());
dsAttributes->AddArray(attributeArray);
attributeArray->Delete();
if(dsAttributes->GetAttribute(attributeType)==0)
{
dsAttributes->SetActiveAttribute(dsAttributes->GetNumberOfArrays()-1,attributeType);
}
}
// Use tolerance as a function of size of source data
//
tol2 = source->GetLength();
tol2 = tol2 ? tol2*tol2 / 1000.0 : 0.001;
cout<<"tol2="<<tol2<<endl;
// Loop over all input points, interpolating source data
//
int abort=0;
// Need to use source to create a cellIt since this class is virtual
vtkGenericCellIterator *cellIt = source->NewCellIterator();
vtkIdType progressInterval=numPts/20 + 1;
for (ptId=0; ptId < numPts && !abort; ptId++)
{
if ( !(ptId % progressInterval) )
{
this->UpdateProgress(static_cast<double>(ptId)/numPts);
abort = GetAbortExecute();
}
// Get the xyz coordinate of the point in the input dataset
input->GetPoint(ptId, x);
// Find the cell that contains xyz and get it
if(source->FindCell(x,cellIt,tol2,subId,pcoords))
{
vtkGenericAdaptorCell *cellProbe = cellIt->GetCell();
// for each cell-centered attribute: copy the value
for(int attrib = 0; attrib<c; ++attrib)
{
if(attributes->GetAttribute(attrib)->GetCentering()==vtkCellCentered)
{
vtkDataArray *array=outputCD->GetArray(attributes->GetAttribute(attrib)->GetName());
double *values=attributes->GetAttribute(attrib)->GetTuple(cellProbe);
array->InsertNextTuple(values);
}
}
// for each point-centered attribute: interpolate the value
int j=0;
for(int attribute_idx = 0; attribute_idx<c; ++attribute_idx)
{
vtkGenericAttribute *a = attributes->GetAttribute(attribute_idx);
if(a->GetCentering()==vtkPointCentered)
{
cellProbe->InterpolateTuple(a,pcoords,tuples);
outputPD->GetArray(j)->InsertTuple(ptId,tuples);
++j;
}
}
this->ValidPoints->InsertNextValue(ptId);
}
else
{
outputPD->NullPoint(ptId);
}
}
cellIt->Delete();
delete[] tuples;
return 1;
}
//----------------------------------------------------------------------------
void vtkGenericProbeFilter::PrintSelf(ostream& os, vtkIndent indent)
{
vtkGenericDataSet *source = this->GetSource();
this->Superclass::PrintSelf(os,indent);
os << indent << "Source: " << source << "\n";
os << indent << "ValidPoints: " << this->ValidPoints << "\n";
}
//----------------------------------------------------------------------------
int vtkGenericProbeFilter::FillInputPortInformation(
int port,
vtkInformation* info)
{
if(!this->Superclass::FillInputPortInformation(port, info))
{
return 0;
}
if(port==1)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkGenericDataSet");
}
else
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
}
return 1;
}