forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkCellLocatorInterpolatedVelocityField.cxx
301 lines (263 loc) · 9.31 KB
/
vtkCellLocatorInterpolatedVelocityField.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCellLocatorInterpolatedVelocityField.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 "vtkCellLocatorInterpolatedVelocityField.h"
#include "vtkMath.h"
#include "vtkDataSet.h"
#include "vtkDataArray.h"
#include "vtkPointData.h"
#include "vtkGenericCell.h"
#include "vtkCellLocator.h"
#include "vtkSmartPointer.h"
#include "vtkObjectFactory.h"
#include "vtkModifiedBSPTree.h"
vtkStandardNewMacro ( vtkCellLocatorInterpolatedVelocityField );
vtkCxxSetObjectMacro( vtkCellLocatorInterpolatedVelocityField, CellLocatorPrototype, vtkAbstractCellLocator );
//----------------------------------------------------------------------------
typedef vtkstd::vector< vtkSmartPointer < vtkAbstractCellLocator > > CellLocatorsTypeBase;
class vtkCellLocatorInterpolatedVelocityFieldCellLocatorsType : public CellLocatorsTypeBase { };
//----------------------------------------------------------------------------
vtkCellLocatorInterpolatedVelocityField::vtkCellLocatorInterpolatedVelocityField()
{
this->LastCellLocator = 0;
this->CellLocatorPrototype = 0;
this->CellLocators = new vtkCellLocatorInterpolatedVelocityFieldCellLocatorsType;
}
//----------------------------------------------------------------------------
vtkCellLocatorInterpolatedVelocityField::~vtkCellLocatorInterpolatedVelocityField()
{
this->LastCellLocator = 0;
this->SetCellLocatorPrototype( 0 );
if ( this->CellLocators )
{
delete this->CellLocators;
this->CellLocators = 0;
}
}
//----------------------------------------------------------------------------
void vtkCellLocatorInterpolatedVelocityField::SetLastCellId
( vtkIdType c, int dataindex )
{
this->LastCellId = c;
this->LastDataSet = ( *this->DataSets )[dataindex];
this->LastCellLocator = ( *this->CellLocators )[dataindex].GetPointer();
this->LastDataSetIndex = dataindex;
// If the dataset changes, then the cached cell is invalidated. We might as
// well prefetch the cached cell either way.
if ( this->LastCellId != -1 )
{
this->LastDataSet->GetCell( this->LastCellId, this->GenCell );
}
}
//----------------------------------------------------------------------------
int vtkCellLocatorInterpolatedVelocityField::FunctionValues
( double * x, double * f )
{
vtkDataSet * vds = NULL;
vtkAbstractCellLocator * loc = NULL;
if( !this->LastDataSet && !this->DataSets->empty() )
{
vds = ( *this->DataSets )[0];
loc = ( *this->CellLocators )[0].GetPointer();
this->LastDataSet = vds;
this->LastCellLocator = loc;
this->LastDataSetIndex = 0;
}
else
{
vds = this->LastDataSet;
loc = this->LastCellLocator;
}
int retVal;
if ( loc )
{
// resort to vtkAbstractCellLocator::FindCell()
retVal = this->FunctionValues( vds, loc, x, f );
}
else
{
// turn to vtkImageData/vtkRectilinearGrid::FindCell()
retVal = this->FunctionValues( vds, x, f );
}
if ( !retVal )
{
for( this->LastDataSetIndex = 0;
this->LastDataSetIndex < static_cast<int>( this->DataSets->size() );
this->LastDataSetIndex ++ )
{
vds = this->DataSets->operator[]( this->LastDataSetIndex );
loc = this->CellLocators->operator[]( this->LastDataSetIndex ).GetPointer();
if( vds && vds != this->LastDataSet )
{
this->ClearLastCellId();
if ( loc )
{
// resort to vtkAbstractCellLocator::FindCell()
retVal = this->FunctionValues( vds, loc, x, f );
}
else
{
// turn to vtkImageData/vtkRectilinearGrid::FindCell()
retVal = this->FunctionValues( vds, x, f );
}
if ( retVal )
{
this->LastDataSet = vds;
this->LastCellLocator = loc;
vds = NULL;
loc = NULL;
return retVal;
}
}
}
this->LastCellId = -1;
this->LastDataSet = ( *this->DataSets )[0];
this->LastCellLocator = ( *this->CellLocators )[0].GetPointer();
this->LastDataSetIndex = 0;
vds = NULL;
loc = NULL;
return 0;
}
vds = NULL;
loc = NULL;
return retVal;
}
//----------------------------------------------------------------------------
int vtkCellLocatorInterpolatedVelocityField::FunctionValues
( vtkDataSet * dataset, vtkAbstractCellLocator * loc, double * x, double * f )
{
f[0] = f[1] = f[2] = 0.0;
vtkDataArray * vectors = NULL;
if ( !dataset || !loc || !dataset->IsA( "vtkPointSet" ) ||
!( vectors = dataset->GetPointData()
->GetVectors( this->VectorsSelection )
)
)
{
vtkErrorMacro( <<"Can't evaluate dataset!" );
vectors = NULL;
return 0;
}
int i;
int subIdx;
int numPts;
int pntIdx;
int bFound = 0;
double vector[3];
double dstns2 = 0.0;
double toler2 = dataset->GetLength() *
vtkCellLocatorInterpolatedVelocityField::TOLERANCE_SCALE;
// check if the point is in the cached cell AND can be successfully evaluated
if ( this->LastCellId != -1 &&
this->GenCell->EvaluatePosition
( x, 0, subIdx, this->LastPCoords, dstns2, this->Weights ) == 1
)
{
bFound = 1;
this->CacheHit ++;
}
if ( !bFound )
{
// cache missing or evaluation failure and then we have to find the cell
this->CacheMiss += !( !( this->LastCellId + 1 ) );
this->LastCellId = loc->FindCell( x, toler2, this->GenCell,
this->LastPCoords, this->Weights );
bFound = !( !( this->LastCellId + 1 ) );
}
// interpolate vectors if possible
if ( bFound )
{
numPts = this->GenCell->GetNumberOfPoints();
for ( i = 0; i < numPts; i ++ )
{
pntIdx = this->GenCell->PointIds->GetId( i );
vectors->GetTuple( pntIdx, vector );
f[0] += vector[0] * this->Weights[i];
f[1] += vector[1] * this->Weights[i];
f[2] += vector[2] * this->Weights[i];
}
if ( this->NormalizeVector == true )
{
vtkMath::Normalize( f );
}
}
vectors = NULL;
return bFound;
}
//----------------------------------------------------------------------------
void vtkCellLocatorInterpolatedVelocityField::AddDataSet( vtkDataSet * dataset )
{
if ( !dataset )
{
vtkErrorMacro( <<"Dataset NULL!" );
return;
}
// insert the dataset (do NOT register the dataset to 'this')
this->DataSets->push_back( dataset );
// We need to attach a valid vtkAbstractCellLocator to any vtkPointSet for
// robust cell location as vtkPointSet::FindCell() may incur failures. For
// any non-vtkPointSet dataset, either vtkImageData or vtkRectilinearGrid,
// we do not need to associate a vtkAbstractCellLocator with it (though a
// NULL vtkAbstractCellLocator is still inserted to this->CellLocators to
// enable proper access to those valid cell locators) since these two kinds
// of datasets themselves are able to guarantee robust as well as fast cell
// location via vtkImageData/vtkRectilinearGrid::FindCell().
vtkSmartPointer< vtkAbstractCellLocator > locator = 0; // MUST inited with 0
if ( dataset->IsA( "vtkPointSet" ) )
{
if ( !this->CellLocatorPrototype )
{
locator = vtkSmartPointer < vtkModifiedBSPTree >::New();
}
else
{
locator.TakeReference( this->CellLocatorPrototype->NewInstance() );
}
locator->SetLazyEvaluation( 1 );
locator->SetDataSet( dataset );
}
this->CellLocators->push_back( locator );
int size = dataset->GetMaxCellSize();
if ( size > this->WeightsSize )
{
this->WeightsSize = size;
if ( this->Weights )
{
delete[] this->Weights;
this->Weights = NULL;
}
this->Weights = new double[size];
}
}
//----------------------------------------------------------------------------
void vtkCellLocatorInterpolatedVelocityField::CopyParameters
( vtkAbstractInterpolatedVelocityField * from )
{
this->Superclass::CopyParameters( from );
if ( from->IsA( "vtkCellLocatorInterpolatedVelocityField" ) )
{
this->SetCellLocatorPrototype
( vtkCellLocatorInterpolatedVelocityField::SafeDownCast( from )
->GetCellLocatorPrototype()
);
}
}
//----------------------------------------------------------------------------
void vtkCellLocatorInterpolatedVelocityField::PrintSelf( ostream & os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "CellLocators: " << this->CellLocators << endl;
if ( this->CellLocators )
{
os << indent << "Number of Cell Locators: " << this->CellLocators->size();
}
os << indent << "LastCellLocator: " << this->LastCellLocator << endl;
os << indent << "CellLocatorPrototype: " << this->CellLocatorPrototype << endl;
}