vtkCachingInterpolatedVelocityField.cxx 14.2 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkCachingInterpolatedVelocityField.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 "vtkCachingInterpolatedVelocityField.h"

#include "vtkDataArray.h"
#include "vtkDataSet.h"
#include "vtkPointData.h"
#include "vtkGenericCell.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkAbstractCellLocator.h"
#include "vtkSmartPointer.h"
#include "vtkFloatArray.h"
#include "vtkDoubleArray.h"

#include "vtkCellLocator.h"
#define Custom_TreeType vtkCellLocator

31
#include <vector>
32
#ifdef JB_BSP_TREE
33
34
35
36
37
38
39
40
41
 #include "vtkModifiedBSPTree.h"
 #undef  Custom_TreeType
 #define Custom_TreeType vtkModifiedBSPTree
#endif
//---------------------------------------------------------------------------
vtkStandardNewMacro(vtkCachingInterpolatedVelocityField);
//---------------------------------------------------------------------------
const double IVFDataSetInfo::TOLERANCE_SCALE = 1.0E-8;
//---------------------------------------------------------------------------
42
43
44
45
46
47
48
49
50
51
IVFDataSetInfo::IVFDataSetInfo()
{
  this->VelocityFloat  = NULL;
  this->VelocityDouble = NULL;
  this->DataSet        = NULL;
  this->Cell           = NULL;
  this->BSPTree        = NULL;
  this->Tolerance      = 0.0;
  this->StaticDataSet  = false;
}
52
53
54
55
56
57
58
59
60
61
//---------------------------------------------------------------------------
void IVFDataSetInfo::SetDataSet(vtkDataSet *data, char *velocity, bool staticdataset, vtkAbstractCellLocator *locator)
{
  this->VelocityFloat  = NULL;
  this->VelocityDouble = NULL;
  this->DataSet        = data;
  this->Cell           = vtkSmartPointer<vtkGenericCell>::New();
  this->StaticDataSet  = staticdataset;
  if (locator) {
    this->BSPTree = locator;
62
  }
63
64
65
66
67
68
69
70
  else if (this->DataSet->IsA("vtkUnstructuredGrid")) {
    if (!this->BSPTree) {
      this->BSPTree = vtkSmartPointer<Custom_TreeType>::New();
    }
    this->BSPTree->SetLazyEvaluation(1);
    this->BSPTree->SetDataSet(this->DataSet);
    this->BSPTree->SetUseExistingSearchStructure(this->StaticDataSet);
  }
71
  this->Tolerance =
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
    this->DataSet->GetLength() * IVFDataSetInfo::TOLERANCE_SCALE;
  //
  vtkDataArray *vectors = this->DataSet->GetPointData()->GetArray(velocity);
  if (vtkFloatArray::SafeDownCast(vectors)) {
    this->VelocityFloat = vtkFloatArray::SafeDownCast(vectors)->GetPointer(0);
  }
  else if (vtkDoubleArray::SafeDownCast(vectors)) {
    this->VelocityDouble = vtkDoubleArray::SafeDownCast(vectors)->GetPointer(0);
  }
  else {
    vtkGenericWarningMacro("We only support float/double velocity vectors at the current time");
  }
}
//---------------------------------------------------------------------------
IVFDataSetInfo::IVFDataSetInfo(const IVFDataSetInfo &ivfci)
{
  this->VelocityFloat  = ivfci.VelocityFloat;
  this->VelocityDouble = ivfci.VelocityDouble;
  this->DataSet        = ivfci.DataSet;
  this->Cell           = ivfci.Cell;
  this->BSPTree        = ivfci.BSPTree;
  this->Tolerance      = ivfci.Tolerance;
  this->StaticDataSet  = ivfci.StaticDataSet;
}
//---------------------------------------------------------------------------
IVFDataSetInfo &IVFDataSetInfo::operator=(const IVFDataSetInfo &ivfci)
{
  this->VelocityFloat  = ivfci.VelocityFloat;
  this->VelocityDouble = ivfci.VelocityDouble;
  this->DataSet        = ivfci.DataSet;
  this->Cell           = ivfci.Cell;
  this->BSPTree        = ivfci.BSPTree;
  this->Tolerance      = ivfci.Tolerance;
  this->StaticDataSet  = ivfci.StaticDataSet;
  return *this;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
vtkCachingInterpolatedVelocityField::vtkCachingInterpolatedVelocityField()
{
  this->NumFuncs         = 3; // u, v, w
  this->NumIndepVars     = 4; // x, y, z, t
  this->VectorsSelection = NULL;
  this->TempCell         = vtkGenericCell::New();
  this->CellCacheHit     = 0;
  this->DataSetCacheHit  = 0;
  this->CacheMiss        = 0;
  this->LastCacheIndex   = 0;
  this->Cache            = NULL;
  this->LastCellId       = -1;
}
//---------------------------------------------------------------------------
vtkCachingInterpolatedVelocityField::~vtkCachingInterpolatedVelocityField()
{
  this->NumFuncs     = 0;
  this->NumIndepVars = 0;
  this->TempCell->Delete();
  this->SetVectorsSelection(0);
}
//---------------------------------------------------------------------------
void vtkCachingInterpolatedVelocityField::SetDataSet(int I, vtkDataSet* dataset, bool staticdataset, vtkAbstractCellLocator *locator)
{
135
136
#define max(x,y) ((x>y) ? (x) : (y))
  int N = max(I+1, static_cast<int>(this->CacheList.size()));
137
138
139
  this->CacheList.resize(N);
  this->CacheList[I].SetDataSet(dataset, this->VectorsSelection, staticdataset, locator);
  //
140
  int maxsize = max(static_cast<int>(this->Weights.size()), dataset->GetMaxCellSize());
141
142
143
  this->Weights.assign(maxsize, 0.0);
}
//---------------------------------------------------------------------------
144
void vtkCachingInterpolatedVelocityField::SetLastCellInfo(vtkIdType c, int datasetindex)
145
{
146
  if ((this->LastCacheIndex != datasetindex) || (this->LastCellId != c))
147
148
149
  {
    this->LastCacheIndex = datasetindex;
    this->Cache          = &this->CacheList[this->LastCacheIndex];
150
    this->LastCellId     = c;
151
152
153
154
155
    // if the dataset changes, then the cached cell is invalidated
    // we might as well prefetch the new cached cell - we'll need it on the next test anyway
    if (this->LastCellId!=-1)
    {
      this->Cache->DataSet->GetCell(this->LastCellId, this->Cache->Cell);
156
    }
157
158
159
  }
}
//---------------------------------------------------------------------------
160
161
162
void vtkCachingInterpolatedVelocityField::ClearLastCellInfo() {
  this->Cache      = NULL;
  this->LastCellId = -1;
163
164
}
//---------------------------------------------------------------------------
165
vtkGenericCell *vtkCachingInterpolatedVelocityField::GetLastCell()
166
{
167
  if (this->Cache)
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    {
    return this->Cache->Cell;
    }
  return NULL;
}
//---------------------------------------------------------------------------
// Evaluate {u,v,w} at {x,y,z,t}
int vtkCachingInterpolatedVelocityField::FunctionValues(double* x, double* f)
{
  // Test using whatever cached information we have
  if (this->Cache) {
    if (this->FunctionValues(this->Cache, x, f)) {
      this->DataSetCacheHit++;
      return 1;
    }
  }
184
  // don't reset this->Cache as we don't want to test it again,
185
  // see == comparison below
186

187
188
  int oldCacheIndex = this->LastCacheIndex;
  // now try each of the datasets in turn
189
  for (this->LastCacheIndex = 0;
190
191
192
193
194
195
196
       this->LastCacheIndex < static_cast<int>(this->CacheList.size());
       this->LastCacheIndex++)
    {
    IVFDataSetInfo *data = &this->CacheList[this->LastCacheIndex];
    if (data==this->Cache) continue;
    //
    this->LastCellId = -1;
197
    if (this->FunctionValues(data, x, f))
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
      {
      this->Cache = data;
      this->CacheMiss++;
      return 1;
      }
    }
  // failed, so clear data and set the cache index to something sensible
  this->CacheMiss++;
  this->ClearLastCellInfo();
  this->LastCacheIndex = oldCacheIndex;
  return 0;
}
//---------------------------------------------------------------------------
// same as FunctionValues, but only testing in/out of cells
int vtkCachingInterpolatedVelocityField::InsideTest(double* x)
{
  // Test using whatever cached information we have
  if (this->Cache) {
    // check the last cell
    int subId;
    if (this->LastCellId!=-1 && this->Cache->Cell->EvaluatePosition(
219
        x, 0, subId, this->Cache->PCoords,
220
221
222
223
        this->Cache->Tolerance, &this->Weights[0])==1) return 1;
    // check this dataset
    if (this->InsideTest(this->Cache, x)) return 1;
  }
224
  // don't reset this->Cache as we don't want to test it again,
225
226
227
  // see != comparison below

  // now try each of the other datasets in turn
228
  for (this->LastCacheIndex = 0;
229
230
231
232
233
234
235
       this->LastCacheIndex < static_cast<int>(this->CacheList.size());
       this->LastCacheIndex++)
    {
    IVFDataSetInfo *data = &this->CacheList[this->LastCacheIndex];
    if (data==this->Cache) continue;
    //
    this->LastCellId = -1;
236
    if (this->InsideTest(data,  x))
237
238
239
240
241
      {
      this->Cache = data;
      return 1;
      }
    }
242
  // failed, so clear data
243
244
245
246
247
  this->ClearLastCellInfo();
  return 0;
}
//---------------------------------------------------------------------------
int vtkCachingInterpolatedVelocityField::InsideTest(IVFDataSetInfo *data, double* x)
248
{
249
  int cellId = data->BSPTree->FindCell(x, data->Tolerance, data->Cell, data->PCoords, &this->Weights[0]);
250
  if (cellId!=-1)
251
252
253
254
255
256
257
258
259
260
261
262
263
264
    {
    this->LastCellId = cellId;
    return 1;
    }
  return 0;
}
//---------------------------------------------------------------------------
// Evaluate {u,v,w} at {x,y,z,t}
int vtkCachingInterpolatedVelocityField::FunctionValues(
  IVFDataSetInfo *data, double *x, double *f)
{
  int    subId;
  double dist2;

265
  if (this->LastCellId>=0)
266
267
268
269
270
271
    {
    bool inbox = true;
    if (data->BSPTree && !data->BSPTree->InsideCellBounds(x, this->LastCellId)) {
      inbox = false;
    }
    if (inbox && data->Cell->EvaluatePosition(
272
      x, 0, subId, data->PCoords, dist2, &this->Weights[0])==1)
273
274
275
276
277
278
279
280
281
282
      {
      this->FastCompute(data, f);
      this->CellCacheHit++;
      return 1;
      }
    }

  // we need to search the whole dataset
  if (data->BSPTree)
    {
283
    int cellId = data->BSPTree->FindCell(x, data->Tolerance,
284
285
286
287
288
      data->Cell, data->PCoords, &this->Weights[0]);
    this->LastCellId = cellId;
    }
  else
    {
289
290
291
292
293
294
    vtkGenericCell* tmpCell = 0;
    if (this->LastCellId >= 0)
      {
      data->DataSet->GetCell(this->LastCellId, this->TempCell);
      tmpCell = this->TempCell;
      }
295
    this->LastCellId =
296
      data->DataSet->FindCell(x, tmpCell, data->Cell, this->LastCellId,
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
      data->Tolerance, subId, data->PCoords, &this->Weights[0]);
    if (this->LastCellId != -1)
      {
      data->DataSet->GetCell(this->LastCellId, data->Cell);
      }
    }

  // if the cell is valid
  if (this->LastCellId != -1)
    {
    this->FastCompute(data, f);
    }
  else
    {
    return 0;
    }

  return 1;
}
//---------------------------------------------------------------------------
void vtkCachingInterpolatedVelocityField::FastCompute(
  IVFDataSetInfo *data, double f[3])
{
  f[0] = f[1] = f[2] = 0.0;
  int numPts = data->Cell->GetNumberOfPoints();
  // interpolate the vectors
  double *dvectors = data->VelocityDouble;
324
  if (dvectors)
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
    {
    for (int j=0; j<numPts; j++)
      {
      int id = data->Cell->PointIds->GetId(j);
      f[0] +=  dvectors[id*3 + 0] * this->Weights[j];
      f[1] +=  dvectors[id*3 + 1] * this->Weights[j];
      f[2] +=  dvectors[id*3 + 2] * this->Weights[j];
      }
    }
  else
    {
    float *fvectors = data->VelocityFloat;
    for (int j=0; j<numPts; j++)
      {
      int id = data->Cell->PointIds->GetId(j);
      f[0] +=  fvectors[id*3 + 0] * this->Weights[j];
      f[1] +=  fvectors[id*3 + 1] * this->Weights[j];
      f[2] +=  fvectors[id*3 + 2] * this->Weights[j];
      }
    }
}
//---------------------------------------------------------------------------
bool vtkCachingInterpolatedVelocityField::InterpolatePoint(
  vtkPointData *outPD, vtkIdType outIndex)
{
  if (!this->Cache || !this->Cache->DataSet)
    {
    return 0;
    }
  outPD->InterpolatePoint(
355
    this->Cache->DataSet->GetPointData(), outIndex,
356
    this->Cache->Cell->PointIds, &this->Weights[0]);
357
358
359
360
361
362
363
364
365
366
367
368
369
  return 1;
}
//---------------------------------------------------------------------------
bool vtkCachingInterpolatedVelocityField::InterpolatePoint(
  vtkCachingInterpolatedVelocityField* inCIVF, vtkPointData *outPD, vtkIdType outIndex)
{
  if (!this->Cache || !this->Cache->DataSet)
    {
    return 0;
    }
  vtkPointData* inPD = inCIVF->Cache->DataSet->GetPointData();
  outPD->InterpolatePoint(
    inPD, outIndex, this->Cache->Cell->PointIds, &this->Weights[0]);
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
  return 1;
}
//---------------------------------------------------------------------------
int vtkCachingInterpolatedVelocityField::GetLastWeights(double* w)
{
  int j, numPts;

  // If last cell is valid, fill w with the interpolation weights
  // and return true
  if (this->Cache && this->LastCellId >= 0)
    {
    numPts = this->Cache->Cell->GetNumberOfPoints();
    for (j=0; j < numPts; j++)
      {
      w[j] = this->Weights[j];
      }
    return 1;
    }
  // otherwise, return false
  else
    {
    return 0;
    }
}
//---------------------------------------------------------------------------
int vtkCachingInterpolatedVelocityField::GetLastLocalCoordinates(double pcoords[3])
{
  int j;

  // If last cell is valid, fill p with the local coordinates
  // and return true
  if (this->Cache && this->LastCellId >= 0)
    {
    for (j=0; j < 3; j++)
      {
      pcoords[j] = this->Cache->PCoords[j];
      }
    return 1;
    }
  // otherwise, return false
  else
    {
    return 0;
    }
}
//---------------------------------------------------------------------------
void vtkCachingInterpolatedVelocityField::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
419
420
421
422
423
424
425
426
427
428

  if (Weights.size()>0)
    {
    os << indent << "Weights: " << &this->Weights[0] << endl;
    }
  else
    {
    os << indent << "Weights: (none)" << endl;
    }

429
430
431
  os << indent << "Cell Cache hit: " << this->CellCacheHit << endl;
  os << indent << "DataSet Cache hit: " << this->DataSetCacheHit << endl;
  os << indent << "Cache miss: " << this->CacheMiss << endl;
432
  os << indent << "VectorsSelection: "
433
     << (this->VectorsSelection?this->VectorsSelection:"(none)") << endl;
434
435
436
437
438
439
440
441
442
443
444

  if (this->Cache)
    {
    os << indent << "Cache->DataSet : "
        << this->Cache->DataSet << endl;
    }
  else
    {
    os << indent << "Cache->DataSet : (none)" << endl;
    }

445
446
447
448
  os << indent << "LastCacheIndex : "
     << this->LastCacheIndex << endl;
}
//---------------------------------------------------------------------------