forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkTemporalInterpolatedVelocityField.cxx
359 lines (350 loc) · 11.7 KB
/
vtkTemporalInterpolatedVelocityField.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTemporalInterpolatedVelocityField.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 "vtkTemporalInterpolatedVelocityField.h"
#include "vtkAbstractCellLocator.h"
#include "vtkCachingInterpolatedVelocityField.h"
#include "vtkDataArray.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkGenericCell.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include <vector>
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkTemporalInterpolatedVelocityField);
//------------------------------------------------------------------------------
vtkTemporalInterpolatedVelocityField::vtkTemporalInterpolatedVelocityField()
{
this->NumFuncs = 3; // u, v, w
this->NumIndepVars = 4; // x, y, z, t
this->IVF[0] = vtkSmartPointer<vtkCachingInterpolatedVelocityField>::New();
this->IVF[1] = vtkSmartPointer<vtkCachingInterpolatedVelocityField>::New();
this->LastGoodVelocity[0] = 0.0;
this->LastGoodVelocity[1] = 0.0;
this->LastGoodVelocity[2] = 0.0;
this->CurrentWeight = 0.0;
this->OneMinusWeight = 1.0;
this->ScaleCoeff = 1.0;
this->Vals1[0] = this->Vals1[1] = this->Vals1[2] = 0.0;
this->Vals2[0] = this->Vals2[1] = this->Vals2[2] = 0.0;
this->Times[0] = this->Times[1] = 0.0;
}
//------------------------------------------------------------------------------
vtkTemporalInterpolatedVelocityField::~vtkTemporalInterpolatedVelocityField()
{
this->NumFuncs = 0;
this->NumIndepVars = 0;
this->SetVectorsSelection(nullptr);
this->IVF[0] = nullptr;
this->IVF[1] = nullptr;
}
//------------------------------------------------------------------------------
void vtkTemporalInterpolatedVelocityField::SetDataSetAtTime(
int I, int N, double T, vtkDataSet* dataset, bool staticdataset)
{
this->Times[N] = T;
if ((this->Times[1] - this->Times[0]) > 0)
{
this->ScaleCoeff = 1.0 / (this->Times[1] - this->Times[0]);
}
if (N == 0)
{
this->IVF[N]->SetDataSet(I, dataset, staticdataset, nullptr);
}
// when the datasets for the second time set are added, set the static flag
if (N == 1)
{
bool is_static = staticdataset && this->IVF[0]->CacheList[I].StaticDataSet;
if (static_cast<size_t>(I) >= this->StaticDataSets.size())
{
this->StaticDataSets.resize(I + 1, is_static);
}
if (is_static)
{
this->IVF[N]->SetDataSet(I, dataset, staticdataset, this->IVF[0]->CacheList[I].BSPTree);
}
else
{
this->IVF[N]->SetDataSet(I, dataset, staticdataset, nullptr);
}
}
}
//------------------------------------------------------------------------------
bool vtkTemporalInterpolatedVelocityField::IsStatic(int datasetIndex)
{
return this->StaticDataSets[datasetIndex];
}
//------------------------------------------------------------------------------
void vtkTemporalInterpolatedVelocityField::SetVectorsSelection(const char* v)
{
this->IVF[0]->SelectVectors(v);
this->IVF[1]->SelectVectors(v);
}
//------------------------------------------------------------------------------
void vtkTemporalInterpolatedVelocityField::ClearCache()
{
this->IVF[0]->SetLastCellInfo(-1, 0);
this->IVF[1]->SetLastCellInfo(-1, 0);
}
//------------------------------------------------------------------------------
void vtkTemporalInterpolatedVelocityField::SetCachedCellIds(vtkIdType id[2], int ds[2])
{
if (id[0] != -1)
{
this->IVF[0]->SetLastCellInfo(id[0], ds[0]);
}
else
{
this->IVF[0]->SetLastCellInfo(-1, 0);
}
//
if (id[1] != -1)
{
this->IVF[1]->SetLastCellInfo(id[1], ds[1]);
}
else
{
this->IVF[1]->SetLastCellInfo(-1, 0);
}
}
//------------------------------------------------------------------------------
bool vtkTemporalInterpolatedVelocityField::GetCachedCellIds(vtkIdType id[2], int ds[2])
{
id[0] = this->IVF[0]->LastCellId;
ds[0] = (id[0] == -1) ? 0 : this->IVF[0]->LastCacheIndex;
//
id[1] = this->IVF[1]->LastCellId;
ds[1] = (id[1] == -1) ? 0 : this->IVF[1]->LastCacheIndex;
return ((id[0] >= 0) && (id[1] >= 0));
}
//------------------------------------------------------------------------------
void vtkTemporalInterpolatedVelocityField::AdvanceOneTimeStep()
{
for (unsigned int i = 0; i < this->IVF[0]->CacheList.size(); i++)
{
if (this->IsStatic(i))
{
this->IVF[0]->ClearLastCellInfo();
this->IVF[1]->ClearLastCellInfo();
}
else
{
this->IVF[0] = this->IVF[1];
this->IVF[1] = vtkSmartPointer<vtkCachingInterpolatedVelocityField>::New();
}
}
}
//------------------------------------------------------------------------------
void vtkTemporalInterpolatedVelocityField::ShowCacheResults()
{
vtkErrorMacro(<< ")\n"
<< "T0 - (cell hit : " << this->IVF[0]->CellCacheHit
<< " (dataset hit : " << this->IVF[0]->DataSetCacheHit - this->IVF[0]->CellCacheHit
<< " (miss : " << this->IVF[0]->CacheMiss << ")\n"
<< "T1 - (cell hit : " << this->IVF[1]->CellCacheHit
<< " (dataset hit : " << this->IVF[1]->DataSetCacheHit - this->IVF[1]->CellCacheHit
<< " (miss : " << this->IVF[1]->CacheMiss);
}
//------------------------------------------------------------------------------
static double vtkTIVFWeightTolerance = 1E-3;
// Evaluate u,v,w at x,y,z,t
int vtkTemporalInterpolatedVelocityField::TestPoint(double* x)
{
this->CurrentWeight = (x[3] - this->Times[0]) * this->ScaleCoeff;
this->OneMinusWeight = 1.0 - this->CurrentWeight;
if (this->CurrentWeight < (0.0 + vtkTIVFWeightTolerance))
{
this->CurrentWeight = 0.0;
}
if (this->CurrentWeight > (1.0 - vtkTIVFWeightTolerance))
{
this->CurrentWeight = 1.0;
}
//
// are we inside dataset at T0
//
if (this->IVF[0]->FunctionValues(x, this->Vals1))
{
// if we are inside at T0 and static, we must be inside at T1
if (this->IsStatic(this->IVF[0]->LastCacheIndex))
{
// compute using weights from dataset 0 and vectors from dataset 1
this->IVF[1]->SetLastCellInfo(this->IVF[0]->LastCellId, this->IVF[0]->LastCacheIndex);
this->IVF[0]->FastCompute(this->IVF[1]->Cache, this->Vals2);
for (int i = 0; i < this->NumFuncs; i++)
{
this->LastGoodVelocity[i] =
this->OneMinusWeight * this->Vals1[i] + this->CurrentWeight * this->Vals2[i];
}
return ID_INSIDE_ALL;
}
// dynamic, we need to test at T1
if (!this->IVF[1]->FunctionValues(x, this->Vals2))
{
// inside at T0, but outside at T1, return velocity for T0
for (int i = 0; i < this->NumFuncs; i++)
{
this->LastGoodVelocity[i] = this->Vals1[i];
}
return ID_OUTSIDE_T1;
}
// both valid, compute correct value
for (int i = 0; i < this->NumFuncs; i++)
{
this->LastGoodVelocity[i] =
this->OneMinusWeight * this->Vals1[i] + this->CurrentWeight * this->Vals2[i];
}
return ID_INSIDE_ALL;
}
// Outside at T0, either abort or use T1
// if we are outside at T0 and static, we must be outside at T1
if (this->IsStatic(this->IVF[0]->LastCacheIndex))
{
return ID_OUTSIDE_ALL;
}
// we are dynamic, so test T1
if (this->IVF[1]->FunctionValues(x, this->Vals2))
{
// inside at T1, but outside at T0, return velocity for T1
for (int i = 0; i < this->NumFuncs; i++)
{
this->LastGoodVelocity[i] = this->Vals2[i];
}
return ID_OUTSIDE_T0;
}
// failed both, so exit
return ID_OUTSIDE_ALL;
}
//------------------------------------------------------------------------------
// Evaluate u,v,w at x,y,z,t
int vtkTemporalInterpolatedVelocityField::QuickTestPoint(double* x)
{
// if outside, return 0
if (!this->IVF[0]->InsideTest(x))
{
return 0;
}
// if inside and static dataset hit, skip next test
if (!this->IsStatic(this->IVF[0]->LastCacheIndex))
{
if (!this->IVF[1]->InsideTest(x))
{
return 0;
}
}
return 1;
}
//------------------------------------------------------------------------------
// Evaluate u,v,w at x,y,z,t
int vtkTemporalInterpolatedVelocityField::FunctionValues(double* x, double* u)
{
if (this->TestPoint(x) == ID_OUTSIDE_ALL)
{
return 0;
}
for (int i = 0; i < this->NumFuncs; i++)
{
u[i] = this->LastGoodVelocity[i];
}
return 1;
}
//------------------------------------------------------------------------------
int vtkTemporalInterpolatedVelocityField::FunctionValuesAtT(int T, double* x, double* u)
{
//
// Try velocity at T0
//
if (T == 0)
{
if (!this->IVF[0]->FunctionValues(x, this->Vals1))
{
return 0;
}
for (int i = 0; i < this->NumFuncs; i++)
{
this->LastGoodVelocity[i] = u[i] = this->Vals1[i];
}
if (this->IsStatic(this->IVF[0]->LastCacheIndex))
{
this->IVF[1]->SetLastCellInfo(this->IVF[0]->LastCellId, this->IVF[0]->LastCacheIndex);
}
}
//
// Try velocity at T1
//
else if (T == 1)
{
if (!this->IVF[1]->FunctionValues(x, this->Vals2))
{
return 0;
}
for (int i = 0; i < this->NumFuncs; i++)
{
this->LastGoodVelocity[i] = u[i] = this->Vals2[i];
}
if (this->IsStatic(this->IVF[1]->LastCacheIndex))
{
this->IVF[0]->SetLastCellInfo(this->IVF[1]->LastCellId, this->IVF[1]->LastCacheIndex);
}
}
return 1;
}
//------------------------------------------------------------------------------
bool vtkTemporalInterpolatedVelocityField::InterpolatePoint(
vtkPointData* outPD1, vtkPointData* outPD2, vtkIdType outIndex)
{
bool ok1 = this->IVF[0]->InterpolatePoint(outPD1, outIndex);
bool ok2 = this->IVF[1]->InterpolatePoint(outPD2, outIndex);
return (ok1 || ok2);
}
//------------------------------------------------------------------------------
bool vtkTemporalInterpolatedVelocityField::InterpolatePoint(
int T, vtkPointData* outPD1, vtkIdType outIndex)
{
vtkCachingInterpolatedVelocityField* inivf = this->IVF[T];
// force use of correct weights/etc if static as only T0 are valid
if (T == 1 && this->IsStatic(this->IVF[T]->LastCacheIndex))
{
T = 0;
}
//
return this->IVF[T]->InterpolatePoint(inivf, outPD1, outIndex);
}
//------------------------------------------------------------------------------
bool vtkTemporalInterpolatedVelocityField::GetVorticityData(
int T, double pcoords[3], double* weights, vtkGenericCell*& cell, vtkDoubleArray* cellVectors)
{
// force use of correct weights/etc if static as only T0 are valid
if (T == 1 && this->IsStatic(this->IVF[T]->LastCacheIndex))
{
T = 0;
}
//
if (this->IVF[T]->GetLastWeights(weights) && this->IVF[T]->GetLastLocalCoordinates(pcoords) &&
(cell = this->IVF[T]->GetLastCell()))
{
vtkDataSet* ds = this->IVF[T]->Cache->DataSet;
vtkPointData* pd = ds->GetPointData();
vtkDataArray* da = pd->GetVectors(this->IVF[T]->GetVectorsSelection());
da->GetTuples(cell->PointIds, cellVectors);
return true;
}
return false;
}
//------------------------------------------------------------------------------
void vtkTemporalInterpolatedVelocityField::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "LastGoodVelocity: " << this->LastGoodVelocity[0] << ", "
<< this->LastGoodVelocity[1] << ", " << this->LastGoodVelocity[2] << endl;
os << indent << "CurrentWeight: " << this->CurrentWeight << endl;
}
//------------------------------------------------------------------------------